ed3432273c63b95499d422bd57dba384f9c97fb8
[openwrt/openwrt.git] / target / linux / ath79 / base-files / etc / hotplug.d / firmware / 11-ath10k-caldata
1 #!/bin/sh
2
3 . /lib/functions/k2t.sh
4
5 # xor multiple hex values of the same length
6 xor() {
7 local val
8 local ret="0x$1"
9 local retlen=${#1}
10
11 shift
12 while [ -n "$1" ]; do
13 val="0x$1"
14 ret=$((ret ^ val))
15 shift
16 done
17
18 printf "%0${retlen}x" "$ret"
19 }
20
21 ath10kcal_die() {
22 echo "ath10cal: " "$*"
23 exit 1
24 }
25
26 ath10kcal_from_file() {
27 local source=$1
28 local offset=$(($2))
29 local count=$(($3))
30
31 dd if=$source of=/lib/firmware/$FIRMWARE iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
32 ath10kcal_die "failed to extract calibration data from $source"
33 }
34
35 ath10kcal_extract() {
36 local part=$1
37 local offset=$(($2))
38 local count=$(($3))
39 local mtd
40
41 mtd=$(find_mtd_chardev $part)
42 [ -n "$mtd" ] || \
43 ath10kcal_die "no mtd device found for partition $part"
44
45 dd if=$mtd of=/lib/firmware/$FIRMWARE iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
46 ath10kcal_die "failed to extract calibration data from $mtd"
47 }
48
49 ath10kcal_patch_mac() {
50 local mac=$1
51
52 [ -z "$mac" ] && return
53
54 macaddr_2bin $mac | dd of=/lib/firmware/$FIRMWARE conv=notrunc oflag=seek_bytes bs=6 seek=6 count=1
55 }
56
57 ath10kcal_patch_mac_crc() {
58 local mac=$1
59 local mac_offset=6
60 local chksum_offset=2
61 local xor_mac
62 local xor_fw_mac
63 local xor_fw_chksum
64
65 xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
66 xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
67
68 ath10kcal_patch_mac "$mac" && {
69 xor_mac=${mac//:/}
70 xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
71
72 xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
73 xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
74
75 printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
76 dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=$chksum_offset count=2
77 }
78 }
79
80 [ -e /lib/firmware/$FIRMWARE ] && exit 0
81
82 . /lib/functions.sh
83 . /lib/functions/system.sh
84
85 board=$(board_name)
86
87 case "$FIRMWARE" in
88 "ath10k/cal-pci-0000:00:00.0.bin")
89 case $board in
90 comfast,cf-wr650ac-v1|\
91 comfast,cf-wr650ac-v2|\
92 yuncore,a770)
93 ath10kcal_extract "art" 0x5000 0x844
94 ;;
95 devolo,dvl1200e|\
96 devolo,dvl1200i|\
97 devolo,dvl1750c|\
98 devolo,dvl1750e|\
99 devolo,dvl1750i|\
100 devolo,dvl1750x)
101 ath10kcal_extract "art" 0x5000 0x844
102 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary art 0x0) -1)
103 ;;
104 dlink,dir-859-a1)
105 ath10kcal_extract "art" 0x5000 0x844
106 ath10kcal_patch_mac $(mtd_get_mac_ascii devdata "wlan5mac")
107 ;;
108 elecom,wrc-1750ghbk2-i)
109 ath10kcal_extract "art" 0x5000 0x844
110 ;;
111 engenius,ecb1750)
112 ath10kcal_extract "art" 0x5000 0x844
113 ath10kcal_patch_mac $(mtd_get_mac_ascii u-boot-env athaddr)
114 ;;
115 engenius,epg5000|\
116 iodata,wn-ac1167dgr|\
117 iodata,wn-ac1600dgr2)
118 ath10kcal_extract "art" 0x5000 0x844
119 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_ascii u-boot-env ethaddr) +1)
120 ;;
121 engenius,ews511ap)
122 ath10kcal_extract "art" 0x5000 0x844
123 ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) +1)
124 ;;
125 glinet,gl-ar750|\
126 glinet,gl-ar750s)
127 ath10kcal_extract "art" 0x5000 0x844
128 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary art 0x0) +1)
129 ;;
130 glinet,gl-x750)
131 ath10kcal_extract "art" 0x5000 0x844
132 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary art 0x0) +2)
133 ;;
134 nec,wg800hp)
135 ath10kcal_extract "art" 0x5000 0x844
136 ath10kcal_patch_mac_crc $(mtd_get_mac_text board_data 0x880)
137 ;;
138 ocedo,koala|\
139 ocedo,ursus)
140 ath10kcal_extract "art" 0x5000 0x844
141 ath10kcal_patch_mac $(mtd_get_mac_binary art 0xc)
142 ;;
143 openmesh,om5p-ac-v2)
144 ath10kcal_extract "art" 0x5000 0x844
145 ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) +16)
146 ;;
147 qihoo,c301)
148 ath10kcal_extract "radiocfg" 0x5000 0x844
149 ath10kcal_patch_mac_crc $(mtd_get_mac_ascii devdata wlan5mac)
150 ;;
151 tplink,archer-a7-v5|\
152 tplink,archer-c2-v3|\
153 tplink,archer-c7-v4|\
154 tplink,archer-c7-v5|\
155 tplink,archer-c25-v1)
156 ath10kcal_extract "art" 0x5000 0x844
157 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary info 0x8) -1)
158 ;;
159 tplink,archer-c5-v1|\
160 tplink,archer-c7-v2)
161 ath10kcal_extract "art" 0x5000 0x844
162 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary u-boot 0x1fc00) -1)
163 ;;
164 tplink,archer-d50-v1)
165 ath10kcal_extract "art" 0x5000 0x844
166 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary romfile 0xf100) +2)
167 ;;
168 tplink,re350k-v1)
169 ath10kcal_extract "art" 0x5000 0x844
170 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary config 0x10008) +2)
171 ;;
172 tplink,re355-v1|\
173 tplink,re450-v1)
174 ath10kcal_extract "art" 0x5000 0x844
175 ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) -2)
176 ;;
177 tplink,re450-v2)
178 ath10kcal_extract "art" 0x5000 0x844
179 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary info 0x8) +1)
180 ;;
181 ubnt,unifiac-lite|\
182 ubnt,unifiac-mesh|\
183 ubnt,unifiac-mesh-pro|\
184 ubnt,lap-120|\
185 ubnt,nanobeam-ac|\
186 ubnt,nanostation-ac|\
187 ubnt,nanostation-ac-loco|\
188 ubnt,unifiac-pro)
189 ath10kcal_extract "EEPROM" 0x5000 0x844
190 ;;
191 esac
192 ;;
193 "ath10k/pre-cal-pci-0000:00:00.0.bin")
194 case $board in
195 dlink,dir-842-c1|\
196 dlink,dir-842-c2|\
197 dlink,dir-842-c3|\
198 nec,wg1200cr)
199 ath10kcal_extract "art" 0x5000 0x2f20
200 ath10kcal_patch_mac_crc $(mtd_get_mac_ascii devdata wlan5mac)
201 ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
202 /lib/firmware/ath10k/QCA9888/hw2.0/board.bin
203 ;;
204 netgear,ex6400|\
205 netgear,ex7300)
206 ath10kcal_extract "caldata" 0x5000 0x2f20
207 ath10kcal_patch_mac $(mtd_get_mac_binary caldata 0xc)
208 ;;
209 phicomm,k2t)
210 ath10kcal_extract "art" 0x5000 0x2f20
211 ath10kcal_patch_mac_crc $(k2t_get_mac "5g_mac")
212 ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
213 /lib/firmware/ath10k/QCA9888/hw2.0/board.bin
214 ;;
215 tplink,archer-c58-v1|\
216 tplink,archer-c59-v1|\
217 tplink,archer-c60-v1|\
218 tplink,archer-c60-v2|\
219 tplink,archer-c6-v2)
220 ath10kcal_extract "art" 0x5000 0x2f20
221 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary mac 0x8) -1)
222 ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
223 /lib/firmware/ath10k/QCA9888/hw2.0/board.bin
224 ;;
225 esac
226 ;;
227 *)
228 exit 1
229 ;;
230 esac