2
0

fixup-mac-address 1.3 KB

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