cbb50a8fc40b0e2c04f81ab9995be576704f8459
[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 devolo,dvl1200e|\
91 devolo,dvl1200i|\
92 devolo,dvl1750c|\
93 devolo,dvl1750e|\
94 devolo,dvl1750i|\
95 devolo,dvl1750x)
96 ath10kcal_extract "art" 20480 2116
97 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary art 0) -1)
98 ;;
99 dlink,dir-859-a1)
100 ath10kcal_extract "art" 20480 2116
101 ath10kcal_patch_mac $(mtd_get_mac_ascii devdata "wlan5mac")
102 ;;
103 elecom,wrc-1750ghbk2-i)
104 ath10kcal_extract "art" 20480 2116
105 ;;
106 engenius,ecb1750)
107 ath10kcal_extract "art" 20480 2116
108 ath10kcal_patch_mac $(mtd_get_mac_ascii u-boot-env athaddr)
109 ;;
110 engenius,epg5000|\
111 iodata,wn-ac1167dgr|\
112 iodata,wn-ac1600dgr2)
113 ath10kcal_extract "art" 20480 2116
114 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_ascii u-boot-env ethaddr) +1)
115 ;;
116 engenius,ews511ap)
117 ath10kcal_extract "art" 20480 2116
118 ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) +1)
119 ;;
120 glinet,gl-ar750s)
121 ath10kcal_extract "art" 20480 2116
122 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary art 0) +1)
123 ;;
124 glinet,gl-x750)
125 ath10kcal_extract "art" 20480 2116
126 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary art 0) +2)
127 ;;
128 nec,wg800hp)
129 ath10kcal_extract "art" 20480 2116
130 ath10kcal_patch_mac_crc $(mtd_get_mac_text board_data 2176)
131 ;;
132 ocedo,koala|\
133 ocedo,ursus)
134 ath10kcal_extract "art" 20480 2116
135 ath10kcal_patch_mac $(mtd_get_mac_binary art 12)
136 ;;
137 openmesh,om5p-ac-v2)
138 ath10kcal_extract "art" 20480 2116
139 ath10kcal_patch_mac $(macaddr_add $(cat /sys/class/net/eth0/address) +16)
140 ;;
141 tplink,archer-a7-v5|\
142 tplink,archer-c2-v3|\
143 tplink,archer-c7-v4|\
144 tplink,archer-c7-v5|\
145 tplink,archer-c25-v1)
146 ath10kcal_extract "art" 20480 2116
147 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary info 8) -1)
148 ;;
149 tplink,archer-c5-v1|\
150 tplink,archer-c7-v2)
151 ath10kcal_extract "art" 20480 2116
152 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary u-boot 0x1fc00) -1)
153 ;;
154 tplink,archer-d50-v1)
155 ath10kcal_extract "art" 20480 2116
156 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary romfile 0xf100) +2)
157 ;;
158 tplink,re350k-v1)
159 ath10kcal_extract "art" 20480 2116
160 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary config 0x10008) +2)
161 ;;
162 tplink,re450-v2)
163 ath10kcal_extract "art" 20480 2116
164 ath10kcal_patch_mac $(macaddr_add $(mtd_get_mac_binary info 8) +1)
165 ;;
166 ubnt,unifiac-lite|\
167 ubnt,unifiac-lr|\
168 ubnt,unifiac-mesh|\
169 ubnt,unifiac-mesh-pro|\
170 ubnt,lap-120|\
171 ubnt,nanobeam-ac|\
172 ubnt,nanostation-ac|\
173 ubnt,nanostation-ac-loco|\
174 ubnt,unifiac-pro)
175 ath10kcal_extract "EEPROM" 20480 2116
176 ;;
177 yuncore,a770)
178 ath10kcal_extract "art" 20480 2116
179 ;;
180 esac
181 ;;
182 "ath10k/pre-cal-pci-0000:00:00.0.bin")
183 case $board in
184 nec,wg1200cr)
185 ath10kcal_extract "art" 20480 12064
186 ath10kcal_patch_mac_crc $(mtd_get_mac_ascii devdata wlan5mac)
187 ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
188 /lib/firmware/ath10k/QCA9888/hw2.0/board.bin
189 ;;
190 netgear,ex6400|\
191 netgear,ex7300)
192 ath10kcal_extract "caldata" 20480 12064
193 ath10kcal_patch_mac $(mtd_get_mac_binary caldata 12)
194 ;;
195 phicomm,k2t)
196 ath10kcal_extract "art" 20480 12064
197 ath10kcal_patch_mac_crc $(k2t_get_mac "5g_mac")
198 ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
199 /lib/firmware/ath10k/QCA9888/hw2.0/board.bin
200 ;;
201 tplink,archer-c58-v1|\
202 tplink,archer-c59-v1|\
203 tplink,archer-c60-v1|\
204 tplink,archer-c60-v2|\
205 tplink,archer-c6-v2)
206 ath10kcal_extract "art" 20480 12064
207 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary mac 8) -1)
208 ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
209 /lib/firmware/ath10k/QCA9888/hw2.0/board.bin
210 ;;
211 yuncore,a782|\
212 yuncore,xd4200)
213 ath10kcal_extract "art" 20480 12064
214 ln -sf /lib/firmware/ath10k/pre-cal-pci-0000\:00\:00.0.bin \
215 /lib/firmware/ath10k/QCA9888/hw2.0/board.bin
216 ;;
217 esac
218 ;;
219 *)
220 exit 1
221 ;;
222 esac