bdc36070f2240a9cba76830657cf495c52f04d62
[openwrt/staging/chunkeey.git] / target / linux / ipq806x / 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_from_file() {
25 local source=$1
26 local offset=$(($2))
27 local count=$(($3))
28
29 dd if=$source of=/lib/firmware/$FIRMWARE bs=1 skip=$offset count=$count 2>/dev/null || \
30 ath10kcal_die "failed to extract calibration data from $source"
31 }
32
33 ath10kcal_extract() {
34 local part=$1
35 local offset=$(($2))
36 local count=$(($3))
37 local mtd
38
39 mtd=$(find_mtd_chardev $part)
40 [ -n "$mtd" ] || \
41 ath10kcal_die "no mtd device found for partition $part"
42
43 dd if=$mtd of=/lib/firmware/$FIRMWARE bs=1 skip=$offset count=$count 2>/dev/null || \
44 ath10kcal_die "failed to extract calibration data from $mtd"
45 }
46
47 ath10kcal_patch_mac_crc() {
48 local mac=$1
49 local mac_offset=6
50 local chksum_offset=2
51 local xor_mac
52 local xor_fw_mac
53 local xor_fw_chksum
54
55 [ -z "$mac" ] && return
56
57 xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
58 xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
59
60 macaddr_2bin $mac | dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=6 count=6
61
62 xor_mac=${mac//:/}
63 xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
64
65 xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
66 xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
67
68 printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
69 dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=$chksum_offset count=2
70 }
71
72 [ -e /lib/firmware/$FIRMWARE ] && exit 0
73
74 . /lib/functions.sh
75 . /lib/functions/system.sh
76
77 board=$(board_name)
78
79 case "$FIRMWARE" in
80 "ath10k/pre-cal-pci-0000:01:00.0.bin")
81 case $board in
82 buffalo,wxr-2533dhp)
83 ath10kcal_extract "ART" 0x1000 0x2f20
84 ath10kcal_patch_mac_crc $(mtd_get_mac_binary ART 0x1e)
85 ;;
86 linksys,ea8500)
87 ath10kcal_extract "art" 0x1000 0x2f20
88 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_ascii devinfo hw_mac_addr) +1)
89 ;;
90 nec,wg2600hp)
91 ath10kcal_extract "ART" 0x1000 0x2f20
92 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary PRODUCTDATA 0xc) +1)
93 ;;
94 netgear,d7800 |\
95 netgear,r7500v2 |\
96 netgear,r7800)
97 ath10kcal_extract "art" 0x1000 0x2f20
98 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary art 0x6) +1)
99 ;;
100 tplink,c2600)
101 ath10kcal_extract "radio" 0x1000 0x2f20
102 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary default-mac 0x8) -1)
103 ;;
104 tplink,vr2600v)
105 ath10kcal_extract "ART" 0x1000 0x2f20
106 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary default-mac 0x0) -1)
107 ;;
108 zyxel,nbg6817)
109 ath10kcal_extract "0:ART" 0x1000 0x2f20
110 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_ascii 0:APPSBLENV ethaddr) +1)
111 ;;
112 esac
113 ;;
114 "ath10k/pre-cal-pci-0001:01:00.0.bin")
115 case $board in
116 buffalo,wxr-2533dhp)
117 ath10kcal_extract "ART" 0x5000 0x2f20
118 ath10kcal_patch_mac_crc $(mtd_get_mac_binary ART 0x18)
119 ;;
120 linksys,ea8500)
121 ath10kcal_extract "art" 0x5000 0x2f20
122 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_ascii devinfo hw_mac_addr) +2)
123 ;;
124 nec,wg2600hp)
125 ath10kcal_extract "ART" 0x5000 0x2f20
126 ath10kcal_patch_mac_crc $(mtd_get_mac_binary PRODUCTDATA 0xc)
127 ;;
128 netgear,d7800 |\
129 netgear,r7500v2 |\
130 netgear,r7800)
131 ath10kcal_extract "art" 0x5000 0x2f20
132 ath10kcal_patch_mac_crc $(macaddr_add $(mtd_get_mac_binary art 0x6) +2)
133 ;;
134 tplink,c2600)
135 ath10kcal_extract "radio" 0x5000 0x2f20
136 ath10kcal_patch_mac_crc $(mtd_get_mac_binary default-mac 0x8)
137 ;;
138 tplink,vr2600v)
139 ath10kcal_extract "ART" 0x5000 0x2f20
140 ath10kcal_patch_mac_crc $(mtd_get_mac_binary default-mac 0x0)
141 ;;
142 zyxel,nbg6817)
143 ath10kcal_extract "0:ART" 0x5000 0x2f20
144 ath10kcal_patch_mac_crc $(mtd_get_mac_ascii 0:APPSBLENV ethaddr)
145 ;;
146 esac
147 ;;
148 *)
149 exit 1
150 ;;
151 esac