1e64b93a46cb1fc82052d964b78a9d0d894881b5
[openwrt/openwrt.git] / target / linux / ipq40xx / base-files / etc / hotplug.d / firmware / 11-ath10k-caldata
1 #!/bin/sh
2
3 # xor multiple hex values of the same length
4 xor() {
5 local val
6 local ret="0x$1"
7 local retlen=${#1}
8
9 shift
10 while [ -n "$1" ]; do
11 val="0x$1"
12 ret=$((ret ^ val))
13 shift
14 done
15
16 printf "%0${retlen}x" "$ret"
17 }
18
19 ath10kcal_die() {
20 echo "ath10cal: " "$*"
21 exit 1
22 }
23
24 ath10kcal_extract() {
25 local part=$1
26 local offset=$2
27 local count=$3
28 local mtd
29
30 mtd=$(find_mtd_chardev $part)
31 [ -n "$mtd" ] || \
32 ath10kcal_die "no mtd device found for partition $part"
33
34 dd if=$mtd of=/lib/firmware/$FIRMWARE iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
35 ath10kcal_die "failed to extract calibration data from $mtd"
36 }
37
38 ath10kcal_ubi_extract() {
39 local part=$1
40 local offset=$2
41 local count=$3
42 local ubidev
43 local ubi
44
45 . /lib/upgrade/nand.sh
46
47 ubidev=$(nand_find_ubi $CI_UBIPART)
48 ubi=$(nand_find_volume $ubidev $part)
49 [ -n "$ubi" ] || \
50 ath10kcal_die "no UBI volume found for $part"
51
52 dd if=/dev/$ubi of=/lib/firmware/$FIRMWARE iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
53 ath10kcal_die "failed to extract from $ubi"
54 }
55
56 ath10kcal_patch_mac_crc() {
57 local mac=$1
58 local mac_offset=6
59 local chksum_offset=2
60 local xor_mac
61 local xor_fw_mac
62 local xor_fw_chksum
63
64 [ -z "$mac" ] && return
65
66 xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
67 xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
68
69 macaddr_2bin $mac | dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=6 count=6
70
71 xor_mac=${mac//:/}
72 xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
73
74 xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
75 xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
76
77 printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
78 dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=$chksum_offset count=2
79 }
80
81 ath10kcal_is_caldata_valid() {
82 local expected="$1"
83
84 magic=$(hexdump -v -n 2 -e '1/1 "%02x"' /lib/firmware/$FIRMWARE)
85 [[ "$magic" == "$expected" ]]
86 return $?
87 }
88
89 [ -e /lib/firmware/$FIRMWARE ] && exit 0
90
91 . /lib/functions.sh
92 . /lib/functions/system.sh
93
94 board=$(board_name)
95
96 case "$FIRMWARE" in
97 "ath10k/cal-pci-0000:01:00.0.bin")
98 case "$board" in
99 meraki,mr33)
100 ath10kcal_ubi_extract "ART" 36864 2116
101 ath10kcal_is_caldata_valid "4408" || ath10kcal_extract "ART" 36864 2116
102 ath10kcal_patch_mac_crc $(macaddr_add $(get_mac_binary "/sys/bus/i2c/devices/0-0050/eeprom" 102) +1)
103 ;;
104 esac
105 ;;
106 "ath10k/pre-cal-pci-0000:01:00.0.bin")
107 case "$board" in
108 asus,map-ac2200)
109 ath10kcal_ubi_extract "Factory" 36864 12064
110 ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
111 /lib/firmware/ath10k/QCA9888/hw2.0/board.bin
112 ;;
113 avm,fritzrepeater-3000)
114 /usr/bin/fritz_cal_extract -i 1 -s 0x3D000 -e 0x212 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader0") || \
115 /usr/bin/fritz_cal_extract -i 1 -s 0x3D000 -e 0x212 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader1")
116 ;;
117 linksys,ea8300)
118 ath10kcal_extract "ART" 36864 12064
119 # OEM assigns 4 sequential MACs
120 ath10kcal_patch_mac_crc $(macaddr_setbit_la $(macaddr_add "$(cat /sys/class/net/eth0/address)" 4))
121 ;;
122 openmesh,a62)
123 ath10kcal_extract "0:ART" 36864 12064
124 ;;
125 esac
126 ;;
127 "ath10k/pre-cal-ahb-a000000.wifi.bin")
128 case "$board" in
129 8dev,jalapeno |\
130 alfa-network,ap120c-ac |\
131 glinet,gl-b1300 |\
132 linksys,ea6350v3 |\
133 qcom,ap-dk01.1-c1)
134 ath10kcal_extract "ART" 4096 12064
135 ;;
136 asus,map-ac2200)
137 ath10kcal_ubi_extract "Factory" 4096 12064
138 ;;
139 asus,rt-ac58u)
140 CI_UBIPART=UBI_DEV
141 ath10kcal_ubi_extract "Factory" 4096 12064
142 ;;
143 avm,fritzbox-4040)
144 /usr/bin/fritz_cal_extract -i 1 -s 0x400 -e 0x207 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader_config")
145 ;;
146 avm,fritzbox-7530 |\
147 avm,fritzrepeater-3000)
148 /usr/bin/fritz_cal_extract -i 1 -s 0x3C000 -e 0x207 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader0") || \
149 /usr/bin/fritz_cal_extract -i 1 -s 0x3C000 -e 0x207 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader1")
150 ;;
151 compex,wpj428 |\
152 engenius,eap1300 |\
153 openmesh,a42 |\
154 openmesh,a62 |\
155 qxwlan,e2600ac-c1 |\
156 qxwlan,e2600ac-c2)
157 ath10kcal_extract "0:ART" 4096 12064
158 ;;
159 engenius,ens620ext)
160 ath10kcal_extract "ART" 4096 12064
161 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_ascii u-boot-env ethaddr) +2)
162 ;;
163 linksys,ea8300)
164 ath10kcal_extract "ART" 4096 12064
165 ath10kcal_patch_mac_crc $(macaddr_add "$(cat /sys/class/net/eth0/address)" 2)
166 ;;
167 meraki,mr33)
168 ath10kcal_ubi_extract "ART" 4096 12064
169 ath10kcal_is_caldata_valid "202f" || ath10kcal_extract "ART" 4096 12064
170 ath10kcal_patch_mac_crc $(macaddr_add $(get_mac_binary "/sys/bus/i2c/devices/0-0050/eeprom" 102) +2)
171 ;;
172 netgear,ex6100v2 |\
173 netgear,ex6150v2)
174 ath10kcal_extract "ART" 4096 12064
175 ath10kcal_patch_mac_crc $(mtd_get_mac_binary dnidata 0)
176 ;;
177 zyxel,nbg6617 |\
178 zyxel,wre6606)
179 ath10kcal_extract "ART" 4096 12064
180 ath10kcal_patch_mac_crc $(macaddr_add $(cat /sys/class/net/eth0/address) -2)
181 ;;
182 esac
183 ;;
184 "ath10k/pre-cal-ahb-a800000.wifi.bin")
185 case "$board" in
186 8dev,jalapeno |\
187 alfa-network,ap120c-ac |\
188 glinet,gl-b1300 |\
189 linksys,ea6350v3 |\
190 qcom,ap-dk01.1-c1)
191 ath10kcal_extract "ART" 20480 12064
192 ;;
193 asus,map-ac2200)
194 ath10kcal_ubi_extract "Factory" 20480 12064
195 ;;
196 asus,rt-ac58u)
197 CI_UBIPART=UBI_DEV
198 ath10kcal_ubi_extract "Factory" 20480 12064
199 ;;
200 avm,fritzbox-4040)
201 /usr/bin/fritz_cal_extract -i 1 -s 0x400 -e 0x208 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader_config")
202 ;;
203 avm,fritzbox-7530 |\
204 avm,fritzrepeater-3000)
205 /usr/bin/fritz_cal_extract -i 1 -s 0x3C800 -e 0x208 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader0") || \
206 /usr/bin/fritz_cal_extract -i 1 -s 0x3C800 -e 0x208 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader1")
207 ;;
208 compex,wpj428 |\
209 engenius,eap1300 |\
210 openmesh,a42 |\
211 openmesh,a62 |\
212 qxwlan,e2600ac-c1 |\
213 qxwlan,e2600ac-c2)
214 ath10kcal_extract "0:ART" 20480 12064
215 ;;
216 engenius,ens620ext)
217 ath10kcal_extract "ART" 20480 12064
218 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_ascii u-boot-env ethaddr) +3)
219 ;;
220 linksys,ea8300)
221 ath10kcal_extract "ART" 20480 12064
222 ath10kcal_patch_mac_crc $(macaddr_add "$(cat /sys/class/net/eth0/address)" 3)
223 ;;
224 meraki,mr33)
225 ath10kcal_ubi_extract "ART" 20480 12064
226 ath10kcal_is_caldata_valid "202f" || ath10kcal_extract "ART" 20480 12064
227 ath10kcal_patch_mac_crc $(macaddr_add $(get_mac_binary "/sys/bus/i2c/devices/0-0050/eeprom" 102) +3)
228 ;;
229 netgear,ex6100v2 |\
230 netgear,ex6150v2)
231 ath10kcal_extract "ART" 20480 12064
232 ath10kcal_patch_mac_crc $(mtd_get_mac_binary dnidata 12)
233 ;;
234 zyxel,nbg6617 |\
235 zyxel,wre6606)
236 ath10kcal_extract "ART" 20480 12064
237 ath10kcal_patch_mac_crc $(macaddr_add $(cat /sys/class/net/eth0/address) -1)
238 ;;
239 esac
240 ;;
241 *)
242 exit 1
243 ;;
244 esac