system.sh 3.6 KB

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