2
0

fixup-mac-address 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/system.sh
  4. . /lib/ramips.sh
  5. partname=""
  6. offset=""
  7. NEW_MAC=
  8. YES=
  9. board=$(ramips_board_name)
  10. case $board in
  11. witi)
  12. partname=factory
  13. offset=$((0xe000))
  14. ;;
  15. *)
  16. echo "Unsupported board"
  17. exit 1
  18. ;;
  19. esac
  20. while [ -n "$1" ]; do
  21. case "$1" in
  22. ??:??:??:??:??:??) NEW_MAC="$1";;
  23. -y) YES=1;;
  24. *)
  25. cat <<EOF
  26. Unknown option/argument '$1'
  27. Usage: $0 [-y] [<macaddr>]
  28. EOF
  29. exit 1
  30. ;;
  31. esac
  32. shift
  33. done
  34. ask_bool() {
  35. local message="$1"
  36. local default="$((! ${2:-0}))"
  37. [ -n "$YES" ] && return 0
  38. echo -n "$message "
  39. read opt
  40. case "$opt" in
  41. y|Y) return 0;;
  42. n|N) return 1;;
  43. *) return $default;;
  44. esac
  45. }
  46. convert_hex() {
  47. hexdump -e '/1 "%02x "'
  48. }
  49. gen_mac() {
  50. dd if=/dev/urandom bs=6 count=1 2>/dev/null
  51. }
  52. mac="$(mtd_get_mac_binary $partname $offset)"
  53. case "$mac" in
  54. 00:00:00:00:00:00);;
  55. ff:ff:ff:ff:ff:ff);;
  56. *)
  57. echo "Current MAC address: $mac"
  58. ask_bool "Overwrite (y/N)?" 0 || exit
  59. ;;
  60. esac
  61. if [ -n "$NEW_MAC" ]; then
  62. set -- $(echo "$NEW_MAC" | sed 's,:, ,g')
  63. else
  64. set -- $(gen_mac | convert_hex)
  65. set -- $(printf %02x $(( (0x$1 & 0xfe) | 0x02 ))) $2 $3 $4 $5 $6
  66. fi
  67. echo "New MAC address: $1:$2:$3:$4:$5:$6"
  68. ask_bool "Write to EEPROM (y/N)?" || exit
  69. part=$(find_mtd_part "$partname")
  70. [ -n "$part" ] || exit
  71. echo -ne "\x$1\x$2\x$3\x$4\x$5\x$6" | dd of=$part conv=notrunc bs=1 count=6 seek=$offset 2>/dev/null
  72. echo "Done"