system.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. dd bs=1 skip=$offset count=6 if=$part 2>/dev/null | hexdump -v -n 6 -e '5/1 "%02x:" 1/1 "%02x"'
  33. }
  34. mtd_get_part_size() {
  35. local part_name=$1
  36. local first dev size erasesize name
  37. while read dev size erasesize name; do
  38. name=${name#'"'}; name=${name%'"'}
  39. if [ "$name" = "$part_name" ]; then
  40. echo $((0x$size))
  41. break
  42. fi
  43. done < /proc/mtd
  44. }
  45. macaddr_add() {
  46. local mac=$1
  47. local val=$2
  48. local oui=${mac%:*:*:*}
  49. local nic=${mac#*:*:*:}
  50. nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
  51. echo $oui:$nic
  52. }
  53. macaddr_setbit_la()
  54. {
  55. local mac=$1
  56. printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
  57. }
  58. macaddr_2bin()
  59. {
  60. local mac=$1
  61. echo -ne \\x${mac//:/\\x}
  62. }
  63. macaddr_canonicalize()
  64. {
  65. local mac="$1"
  66. local canon=""
  67. mac=$(echo -n $mac | tr -d \")
  68. [ ${#mac} -gt 17 ] && return
  69. [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
  70. for octet in ${mac//[\.:-]/ }; do
  71. case "${#octet}" in
  72. 1)
  73. octet="0${octet}"
  74. ;;
  75. 2)
  76. ;;
  77. 4)
  78. octet="${octet:0:2} ${octet:2:2}"
  79. ;;
  80. 12)
  81. octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
  82. ;;
  83. *)
  84. return
  85. ;;
  86. esac
  87. canon=${canon}${canon:+ }${octet}
  88. done
  89. [ ${#canon} -ne 17 ] && return
  90. printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
  91. }