system.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if [ -z "$part" ]; then
  63. echo "mtd_get_mac_binary: ubi volume $mtdname not found!" >&2
  64. return
  65. fi
  66. hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' /dev/$part 2>/dev/null
  67. }
  68. mtd_get_part_size() {
  69. local part_name=$1
  70. local first dev size erasesize name
  71. while read dev size erasesize name; do
  72. name=${name#'"'}; name=${name%'"'}
  73. if [ "$name" = "$part_name" ]; then
  74. echo $((0x$size))
  75. break
  76. fi
  77. done < /proc/mtd
  78. }
  79. macaddr_add() {
  80. local mac=$1
  81. local val=$2
  82. local oui=${mac%:*:*:*}
  83. local nic=${mac#*:*:*:}
  84. nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
  85. echo $oui:$nic
  86. }
  87. macaddr_setbit_la() {
  88. local mac=$1
  89. printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
  90. }
  91. macaddr_2bin() {
  92. local mac=$1
  93. echo -ne \\x${mac//:/\\x}
  94. }
  95. macaddr_canonicalize() {
  96. local mac="$1"
  97. local canon=""
  98. mac=$(echo -n $mac | tr -d \")
  99. [ ${#mac} -gt 17 ] && return
  100. [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
  101. for octet in ${mac//[\.:-]/ }; do
  102. case "${#octet}" in
  103. 1)
  104. octet="0${octet}"
  105. ;;
  106. 2)
  107. ;;
  108. 4)
  109. octet="${octet:0:2} ${octet:2:2}"
  110. ;;
  111. 12)
  112. octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
  113. ;;
  114. *)
  115. return
  116. ;;
  117. esac
  118. canon=${canon}${canon:+ }${octet}
  119. done
  120. [ ${#canon} -ne 17 ] && return
  121. printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
  122. }