ar71xx: add support for Fritz!Box 4020
[openwrt/staging/mkresin.git] / target / linux / ar71xx / base-files / etc / hotplug.d / firmware / 10-ath9k-eeprom
1 #!/bin/sh
2
3 [ -e /lib/firmware/$FIRMWARE ] && exit 0
4
5 . /lib/functions.sh
6 . /lib/functions/system.sh
7
8 ath9k_eeprom_die() {
9 echo "ath9k eeprom: " "$*"
10 exit 1
11 }
12
13 ath9k_eeprom_extract() {
14 local part=$1
15 local offset=$2
16 local count=$3
17 local mtd
18
19 mtd=$(find_mtd_chardev $part)
20 [ -n "$mtd" ] || \
21 ath9k_eeprom_die "no mtd device found for partition $part"
22
23 dd if=$mtd of=/lib/firmware/$FIRMWARE bs=1 skip=$offset count=$count 2>/dev/null || \
24 ath9k_eeprom_die "failed to extract from $mtd"
25 }
26
27 ath9k_ubi_eeprom_extract() {
28 local part=$1
29 local offset=$2
30 local count=$3
31 local ubidev=$(nand_find_ubi $CI_UBIPART)
32 local ubi
33
34 ubi=$(nand_find_volume $ubidev $part)
35 [ -n "$ubi" ] || \
36 ath9k_eeprom_die "no UBI volume found for $part"
37
38 dd if=/dev/$ubi of=/lib/firmware/$FIRMWARE bs=1 skip=$offset count=$count 2>/dev/null || \
39 ath9k_eeprom_die "failed to extract from $ubi"
40 }
41
42 ath9k_eeprom_extract_reverse() {
43 local part=$1
44 local offset=$2
45 local count=$3
46 local mtd
47 local reversed
48 local caldata
49
50 mtd=$(find_mtd_chardev "$part")
51 reversed=$(hexdump -v -s $offset -n $count -e '/1 "%02x "' $mtd)
52
53 for byte in $reversed; do
54 caldata="\x${byte}${caldata}"
55 done
56
57 printf "%b" "$caldata" > /lib/firmware/$FIRMWARE
58 }
59
60 ath9k_patch_firmware_mac() {
61 local mac=$1
62
63 [ -z "$mac" ] && return
64
65 macaddr_2bin $mac | dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=2 count=6
66 }
67
68 board=$(board_name)
69
70 case "$FIRMWARE" in
71 "soc_wmac.eeprom")
72 case $board in
73 c-55|\
74 c-60)
75 ath9k_eeprom_extract "art" 4096 2048
76 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_binary art 0) +1)
77 ;;
78 fritz4020)
79 ath9k_eeprom_extract_reverse "urlader" 5441 1088
80 ;;
81 mr18)
82 . /lib/upgrade/nand.sh
83
84 if [ -n "$(nand_find_volume ubi0 caldata)" ]; then
85 ath9k_ubi_eeprom_extract "caldata" 4096 2048
86 else
87 ath9k_eeprom_extract "odm-caldata" 4096 2048
88 fi
89 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_binary_ubi board-config 102) +1)
90 ;;
91 r6100 | \
92 wndr3700v4 | \
93 wndr4300)
94 ath9k_eeprom_extract "caldata" 4096 2048
95 ath9k_patch_firmware_mac $(mtd_get_mac_binary caldata 0)
96 ;;
97 rambutan)
98 ath9k_eeprom_extract "art" 4096 2048
99 ;;
100 wlr8100)
101 ath9k_eeprom_extract "art" 4096 2048
102 ath9k_patch_firmware_mac $(mtd_get_mac_ascii u-boot-env "ethaddr")
103 ;;
104 z1)
105 . /lib/upgrade/nand.sh
106
107 if [ -n "$(nand_find_volume ubi0 caldata)" ]; then
108 ath9k_ubi_eeprom_extract "caldata" 4096 2048
109 else
110 ath9k_eeprom_extract "origcaldata" 4096 2048
111 fi
112 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_binary_ubi board-config 102) +2)
113 ;;
114 *)
115 ath9k_eeprom_die "board $board is not supported yet"
116 ;;
117 esac
118 ;;
119
120 "pci_wmac0.eeprom")
121 case $board in
122 c-55)
123 ath9k_eeprom_extract "art" 20480 2048
124 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_binary art 0) +2)
125 ;;
126 fritz300e)
127 ath9k_eeprom_extract_reverse "urloader" 5441 1088
128 ;;
129 mr18)
130 . /lib/upgrade/nand.sh
131
132 if [ -n "$(nand_find_volume ubi0 caldata)" ]; then
133 ath9k_ubi_eeprom_extract "caldata" 20480 2048
134 else
135 ath9k_eeprom_extract "odm-caldata" 20480 2048
136 fi
137 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_binary_ubi board-config 102) +2)
138 ;;
139 wndr3700v4 | \
140 wndr4300)
141 ath9k_eeprom_extract "caldata" 20480 2048
142 ath9k_patch_firmware_mac $(mtd_get_mac_binary caldata 12)
143 ;;
144 z1)
145 . /lib/upgrade/nand.sh
146
147 if [ -n "$(nand_find_volume ubi0 caldata)" ]; then
148 ath9k_ubi_eeprom_extract "caldata" 86016 4096
149 else
150 ath9k_eeprom_extract "origcaldata" 86016 4096
151 fi
152 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_binary_ubi board-config 102) +3)
153 ;;
154 *)
155 ath9k_eeprom_die "board $board is not supported yet"
156 ;;
157 esac
158 ;;
159
160 "pci_wmac1.eeprom")
161 case $board in
162 mr18)
163 . /lib/upgrade/nand.sh
164
165 if [ -n "$(nand_find_volume ubi0 caldata)" ]; then
166 ath9k_ubi_eeprom_extract "caldata" 36864 2048
167 else
168 ath9k_eeprom_extract "odm-caldata" 36864 2048
169 fi
170 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_binary_ubi board-config 102) +3)
171 ;;
172 *)
173 ath9k_eeprom_die "board $board is not supported yet"
174 ;;
175 esac
176 ;;
177 esac