605a03be1dde8cb691b31dd7610be3985ce6da35
[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 bs=1 skip=$offset count=$count 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 bs=1 skip=$offset count=$count 2>/dev/null || \
53 ath10kcal_die "failed to extract from $ubi"
54 }
55
56 ath10kcal_patch_mac() {
57 local mac=$1
58
59 [ -z "$mac" ] && return
60
61 macaddr_2bin $mac | dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=6 count=6
62 }
63
64 ath10kcal_patch_mac_crc() {
65 local mac=$1
66 local mac_offset=6
67 local chksum_offset=2
68 local xor_mac
69 local xor_fw_mac
70 local xor_fw_chksum
71
72 xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
73 xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
74
75 ath10kcal_patch_mac "$mac" && {
76 xor_mac=${mac//:/}
77 xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
78
79 xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
80 xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
81
82 printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
83 dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=$chksum_offset count=2
84 }
85 }
86
87 ath10kcal_is_caldata_valid() {
88 local expected="$1"
89
90 magic=$(hexdump -v -n 2 -e '1/1 "%02x"' /lib/firmware/$FIRMWARE)
91 [[ "$magic" == "$expected" ]]
92 return $?
93 }
94
95 [ -e /lib/firmware/$FIRMWARE ] && exit 0
96
97 . /lib/functions.sh
98 . /lib/functions/system.sh
99
100 board=$(board_name)
101
102
103 case "$FIRMWARE" in
104 "ath10k/cal-pci-0000:01:00.0.bin")
105 case "$board" in
106 meraki,mr33)
107 ath10kcal_ubi_extract "ART" 36864 2116
108 ath10kcal_is_caldata_valid "4408" || ath10kcal_extract "ART" 36864 2116
109 ath10kcal_patch_mac $(macaddr_add $(get_mac_binary "/sys/bus/i2c/devices/0-0050/eeprom" 102) +1)
110 ;;
111 esac
112 ;;
113 "ath10k/pre-cal-pci-0000:01:00.0.bin")
114 case "$board" in
115 openmesh,a62)
116 ath10kcal_extract "0:ART" 36864 12064
117 ;;
118 esac
119 ;;
120 "ath10k/pre-cal-ahb-a000000.wifi.bin")
121 case "$board" in
122 8dev,jalapeno |\
123 glinet,gl-b1300 |\
124 qcom,ap-dk01.1-c1)
125 ath10kcal_extract "ART" 4096 12064
126 ;;
127 asus,rt-ac58u)
128 CI_UBIPART=UBI_DEV
129 ath10kcal_ubi_extract "Factory" 4096 12064
130 ;;
131 avm,fritzbox-4040)
132 /usr/bin/fritz_cal_extract -i 1 -s 0x400 -e 0x207 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader_config")
133 ;;
134 meraki,mr33)
135 ath10kcal_ubi_extract "ART" 4096 12064
136 ath10kcal_is_caldata_valid "202f" || ath10kcal_extract "ART" 4096 12064
137 ath10kcal_patch_mac_crc $(macaddr_add $(get_mac_binary "/sys/bus/i2c/devices/0-0050/eeprom" 102) +2)
138 ;;
139 netgear,ex6100v2 |\
140 netgear,ex6150v2)
141 ath10kcal_extract "ART" 4096 12064
142 ath10kcal_patch_mac_crc $(mtd_get_mac_binary dnidata 0)
143 ;;
144 compex,wpj428 |\
145 engenius,eap1300 |\
146 openmesh,a42 |\
147 openmesh,a62)
148 ath10kcal_extract "0:ART" 4096 12064
149 ;;
150 zyxel,nbg6617 |\
151 zyxel,wre6606)
152 ath10kcal_extract "ART" 4096 12064
153 ath10kcal_patch_mac_crc $(macaddr_add $(cat /sys/class/net/eth0/address) -2)
154 ;;
155 esac
156 ;;
157 "ath10k/pre-cal-ahb-a800000.wifi.bin")
158 case "$board" in
159 8dev,jalapeno |\
160 glinet,gl-b1300 |\
161 qcom,ap-dk01.1-c1)
162 ath10kcal_extract "ART" 20480 12064
163 ;;
164 asus,rt-ac58u)
165 CI_UBIPART=UBI_DEV
166 ath10kcal_ubi_extract "Factory" 20480 12064
167 ;;
168 avm,fritzbox-4040)
169 /usr/bin/fritz_cal_extract -i 1 -s 0x400 -e 0x208 -l 12064 -o /lib/firmware/$FIRMWARE $(find_mtd_chardev "urlader_config")
170 ;;
171 meraki,mr33)
172 ath10kcal_ubi_extract "ART" 20480 12064
173 ath10kcal_is_caldata_valid "202f" || ath10kcal_extract "ART" 20480 12064
174 ath10kcal_patch_mac_crc $(macaddr_add $(get_mac_binary "/sys/bus/i2c/devices/0-0050/eeprom" 102) +3)
175 ;;
176 netgear,ex6100v2 |\
177 netgear,ex6150v2)
178 ath10kcal_extract "ART" 20480 12064
179 ath10kcal_patch_mac_crc $(mtd_get_mac_binary dnidata 12)
180 ;;
181 compex,wpj428 |\
182 engenius,eap1300 |\
183 openmesh,a42 |\
184 openmesh,a62)
185 ath10kcal_extract "0:ART" 20480 12064
186 ;;
187 zyxel,nbg6617 |\
188 zyxel,wre6606)
189 ath10kcal_extract "ART" 20480 12064
190 ath10kcal_patch_mac_crc $(macaddr_add $(cat /sys/class/net/eth0/address) -1)
191 ;;
192 esac
193 ;;
194 *)
195 exit 1
196 ;;
197 esac