system.sh 2.5 KB

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