lantiq: complete AVM FRITZ!Box 3370 support
[openwrt/openwrt.git] / target / linux / lantiq / base-files / etc / hotplug.d / firmware / 12-ath9k-eeprom
1 #!/bin/sh
2 # Based on ar71xx 10-ath9k-eeprom
3
4 [ -e /lib/firmware/$FIRMWARE ] && exit 0
5
6 . /lib/functions.sh
7 . /lib/functions/system.sh
8 . /lib/upgrade/nand.sh
9
10 # xor multiple hex values of the same length
11 xor() {
12 local val
13 local ret="0x$1"
14 local retlen=${#1}
15
16 shift
17 while [ -n "$1" ]; do
18 val="0x$1"
19 ret=$((ret ^ val))
20 shift
21 done
22
23 printf "%0${retlen}x" "$ret"
24 }
25
26 ath9k_eeprom_die() {
27 echo "ath9k eeprom: $*"
28 exit 1
29 }
30
31 ath9k_eeprom_extract_raw() {
32 local source=$1
33 local offset=$2
34 local swap=$3
35 local size=4096
36 local bs=1
37 local conv=
38
39 if [ $swap -gt 0 ]; then
40 bs=2
41 conv="conv=swab"
42 size=$((size / bs))
43 offset=$((offset / bs))
44 fi
45
46 dd if=$source of=/lib/firmware/$FIRMWARE bs=$bs skip=$offset count=$size $conv 2>/dev/null || \
47 ath9k_eeprom_die "failed to extract from $mtd"
48 }
49
50 ath9k_eeprom_extract_reverse() {
51 local part=$1
52 local offset=$2
53 local count=$3
54 local mtd
55 local reversed
56 local caldata
57
58 mtd=$(find_mtd_chardev "$part")
59 reversed=$(hexdump -v -s $offset -n $count -e '/1 "%02x "' $mtd)
60
61 for byte in $reversed; do
62 caldata="\x${byte}${caldata}"
63 done
64
65 printf "%b" "$caldata" > /lib/firmware/$FIRMWARE
66 }
67
68 ath9k_eeprom_extract() {
69 local part=$1
70 local offset=$2
71 local swap=$3
72 local mtd
73
74 mtd=$(find_mtd_chardev $part)
75 [ -n "$mtd" ] || \
76 ath9k_eeprom_die "no mtd device found for partition $part"
77
78 ath9k_eeprom_extract_raw $mtd $offset $swap
79 }
80
81 ath9k_ubi_eeprom_extract() {
82 local part=$1
83 local offset=$2
84 local swap=$3
85 local ubidev=$(nand_find_ubi $CI_UBIPART)
86 local ubi
87
88 ubi=$(nand_find_volume $ubidev $part)
89 [ -n "$ubi" ] || \
90 ath9k_eeprom_die "no UBI volume found for $part"
91
92 ath9k_eeprom_extract_raw /dev/$ubi $offset $swap
93 }
94
95 ath9k_patch_fw_mac_crc() {
96 local mac=$1
97 local mac_offset=$2
98 local chksum_offset=$((mac_offset - 10))
99
100 ath9k_patch_fw_mac "${mac}" "${mac_offset}" "${chksum_offset}"
101 }
102
103 ath9k_patch_fw_mac() {
104 local mac=$1
105 local mac_offset=$2
106 local chksum_offset=$3
107 local xor_mac
108 local xor_fw_mac
109 local xor_fw_chksum
110
111 [ -z "$mac" -o -z "$mac_offset" ] && return
112
113 [ -n "$chksum_offset" ] && {
114 xor_mac=${mac//:/}
115 xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
116
117 xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
118 xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
119
120 xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
121 xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
122
123 printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
124 dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=$chksum_offset count=2
125 }
126
127 macaddr_2bin $mac | dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=$mac_offset count=6
128 }
129
130 case "$FIRMWARE" in
131 "ath9k-eeprom-pci-0000:00:0e.0.bin" | \
132 "ath9k-eeprom-pci-0000:01:00.0.bin" | \
133 "ath9k-eeprom-pci-0000:02:00.0.bin")
134 board=$(board_name)
135
136 case "$board" in
137 arcadyan,arv7518pw)
138 ath9k_eeprom_extract "boardconfig" 1024 1
139 ;;
140 arcadyan,arv8539pw22)
141 ath9k_eeprom_extract "art" 1024 1
142 ;;
143 bt,homehub-v2b)
144 ath9k_eeprom_extract "art" 0 1
145 ath9k_patch_fw_mac_crc "00:00:00:00:00:00" 524
146 ;;
147 bt,homehub-v3a)
148 ath9k_eeprom_extract "art-copy" 0 1
149 ath9k_patch_fw_mac_crc $(macaddr_add $(mtd_get_mac_ascii uboot_env ethaddr) +2) 268
150 ;;
151 bt,homehub-v5a)
152 ath9k_ubi_eeprom_extract "caldata" 4096 0
153 ath9k_patch_fw_mac_crc $(macaddr_add $(mtd_get_mac_binary_ubi caldata 4364) +2) 268
154 ;;
155 netgear,dgn3500|netgear,dgn3500b)
156 ath9k_eeprom_extract "calibration" 61440 0
157 ath9k_patch_fw_mac_crc $(macaddr_add $(mtd_get_mac_ascii uboot-env ethaddr) +2) 524
158 ;;
159 avm,fritz3370-rev2-hynix|\
160 avm,fritz3370-rev2-micron)
161 ath9k_eeprom_extract_reverse "urlader" 5441 1088
162 ;;
163 avm,fritz7320|avm,fritz7360sl)
164 ath9k_eeprom_extract "urlader" 2437 0
165 ;;
166 tplink,tdw8970|tplink,tdw8980)
167 ath9k_eeprom_extract "boardconfig" 135168 0
168 ;;
169 *)
170 ath9k_eeprom_die "board $board is not supported yet"
171 ;;
172 esac
173 ;;
174 esac