96a239719113575da1e6ceafddfb0dfa4a98b8f1
[openwrt/.git] / target / linux / ramips / base-files / etc / hotplug.d / firmware / 10-rt2x00-eeprom
1 #!/bin/sh
2
3 rt2x00_eeprom_die() {
4         echo "rt2x00 eeprom: " "$*"
5         exit 1
6 }
7
8 rt2x00_eeprom_extract() {
9         local part=$1
10         local offset=$2
11         local count=$3
12         local mtd
13
14         mtd=$(find_mtd_part $part)
15         [ -n "$mtd" ] || \
16                 rt2x00_eeprom_die "no mtd device found for partition $part"
17
18         dd if=$mtd of=/lib/firmware/$FIRMWARE iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
19                 rt2x00_eeprom_die "failed to extract from $mtd"
20 }
21
22 jboot_eeprom_extract() {
23         local part=$1
24         local offset=$2
25         local mtd
26
27         mtd=$(find_mtd_part $part)
28         [ -n "$mtd" ] || \
29                 rt2x00_eeprom_die "no mtd device found for partition $part"
30
31         jboot_config_read -i $mtd -o $offset -e /lib/firmware/$FIRMWARE  2>/dev/null || \
32                 rt2x00_eeprom_die "failed to extract from $mtd"
33 }
34
35 rt2x00_eeprom_set_macaddr() {
36         local macaddr=$1
37
38         [ -n "$macaddr" ] || \
39                 rt2x00_eeprom_die "invalid wlan mac address"
40
41         macaddr_2bin $macaddr | dd of=/lib/firmware/$FIRMWARE \
42                                 conv=notrunc oflag=seek_bytes bs=6 seek=4 count=1 2>/dev/null || \
43                 rt2x00_eeprom_die "failed to write mac address to eeprom file"
44 }
45
46 FW="/lib/firmware/$FIRMWARE"
47 [ -e "$FW" ] && exit 0
48
49 . /lib/functions.sh
50 . /lib/functions/system.sh
51
52 board=$(board_name)
53
54 case "$FIRMWARE" in
55 "soc_wmac.eeprom")
56         case $board in
57         dlink,dwr-116-a1|\
58         dlink,dwr-118-a1|\
59         dlink,dwr-118-a2|\
60         dlink,dwr-921-c1|\
61         dlink,dwr-922-e2|\
62         lava,lr-25g001)
63                 wan_mac=$(jboot_config_read -m -i $(find_mtd_part "config") -o 0xE000)
64                 wifi_mac=$(macaddr_add "$wan_mac" 1)
65                 jboot_eeprom_extract "config" 0xE000
66                 rt2x00_eeprom_set_macaddr $wifi_mac
67                 ;;
68         tiny-ac)
69                 wifi_mac=$(mtd_get_mac_ascii u-boot-env INIC_MAC_ADDR)
70                 rt2x00_eeprom_extract "factory" 0 512
71                 rt2x00_eeprom_set_macaddr $wifi_mac
72                 ;;
73         *)
74                 rt2x00_eeprom_die "Please define mtd-eeprom in $board DTS file!"
75                 ;;
76         esac
77         ;;
78 esac