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