fixup-mac-address 1.3 KB

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