1
0

system.sh 2.8 KB

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