system.sh 2.4 KB

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