system.sh 3.7 KB

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