system.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright (C) 2006-2013 OpenWrt.org
  2. get_mac_binary() {
  3. local path="$1"
  4. local offset="$2"
  5. if ! [ -e "$path" ]; then
  6. echo "get_mac_binary: file $path not found!" >&2
  7. return
  8. fi
  9. hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev/null
  10. }
  11. get_mac_label() {
  12. local basepath="/proc/device-tree"
  13. local macdevice="$(cat "$basepath/aliases/label-mac-device" 2>/dev/null)"
  14. local macaddr
  15. [ -n "$macdevice" ] && macaddr=$(get_mac_binary "$basepath/$macdevice/mac-address" 0 2>/dev/null)
  16. [ -n "$macaddr" ] || macaddr=$(get_mac_binary "$basepath/$macdevice/local-mac-address" 0 2>/dev/null)
  17. [ -n "$macaddr" ] || macaddr=$(uci -q get system.@system[0].label_macaddr)
  18. echo $macaddr
  19. }
  20. find_mtd_chardev() {
  21. local INDEX=$(find_mtd_index "$1")
  22. local PREFIX=/dev/mtd
  23. [ -d /dev/mtd ] && PREFIX=/dev/mtd/
  24. echo "${INDEX:+$PREFIX$INDEX}"
  25. }
  26. mtd_get_mac_ascii() {
  27. local mtdname="$1"
  28. local key="$2"
  29. local part
  30. local mac_dirty
  31. part=$(find_mtd_part "$mtdname")
  32. if [ -z "$part" ]; then
  33. echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
  34. return
  35. fi
  36. mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
  37. # "canonicalize" mac
  38. [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
  39. }
  40. mtd_get_mac_text() {
  41. local mtdname=$1
  42. local offset=$(($2))
  43. local part
  44. local mac_dirty
  45. part=$(find_mtd_part "$mtdname")
  46. if [ -z "$part" ]; then
  47. echo "mtd_get_mac_text: partition $mtdname not found!" >&2
  48. return
  49. fi
  50. if [ -z "$offset" ]; then
  51. echo "mtd_get_mac_text: offset missing!" >&2
  52. return
  53. fi
  54. mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
  55. # "canonicalize" mac
  56. [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
  57. }
  58. mtd_get_mac_binary() {
  59. local mtdname="$1"
  60. local offset="$2"
  61. local part
  62. part=$(find_mtd_part "$mtdname")
  63. get_mac_binary "$part" "$offset"
  64. }
  65. mtd_get_mac_binary_ubi() {
  66. local mtdname="$1"
  67. local offset="$2"
  68. . /lib/upgrade/nand.sh
  69. local ubidev=$(nand_find_ubi $CI_UBIPART)
  70. local part=$(nand_find_volume $ubidev $1)
  71. get_mac_binary "/dev/$part" "$offset"
  72. }
  73. mtd_get_part_size() {
  74. local part_name=$1
  75. local first dev size erasesize name
  76. while read dev size erasesize name; do
  77. name=${name#'"'}; name=${name%'"'}
  78. if [ "$name" = "$part_name" ]; then
  79. echo $((0x$size))
  80. break
  81. fi
  82. done < /proc/mtd
  83. }
  84. macaddr_add() {
  85. local mac=$1
  86. local val=$2
  87. local oui=${mac%:*:*:*}
  88. local nic=${mac#*:*:*:}
  89. nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
  90. echo $oui:$nic
  91. }
  92. macaddr_setbit_la() {
  93. local mac=$1
  94. printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
  95. }
  96. macaddr_2bin() {
  97. local mac=$1
  98. echo -ne \\x${mac//:/\\x}
  99. }
  100. macaddr_canonicalize() {
  101. local mac="$1"
  102. local canon=""
  103. mac=$(echo -n $mac | tr -d \")
  104. [ ${#mac} -gt 17 ] && return
  105. [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
  106. for octet in ${mac//[\.:-]/ }; do
  107. case "${#octet}" in
  108. 1)
  109. octet="0${octet}"
  110. ;;
  111. 2)
  112. ;;
  113. 4)
  114. octet="${octet:0:2} ${octet:2:2}"
  115. ;;
  116. 12)
  117. octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
  118. ;;
  119. *)
  120. return
  121. ;;
  122. esac
  123. canon=${canon}${canon:+ }${octet}
  124. done
  125. [ ${#canon} -ne 17 ] && return
  126. printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
  127. }