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