lantiq: use ath9k device tree bindings binding/owl-loader
[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/functions/lantiq.sh
9 . /lib/upgrade/nand.sh
10
11 # xor multiple hex values of the same length
12 xor() {
13 local val
14 local ret="0x$1"
15 local retlen=${#1}
16
17 shift
18 while [ -n "$1" ]; do
19 val="0x$1"
20 ret=$((ret ^ val))
21 shift
22 done
23
24 printf "%0${retlen}x" "$ret"
25 }
26
27 ath9k_eeprom_die() {
28 echo "ath9k eeprom: $*"
29 exit 1
30 }
31
32 ath9k_eeprom_extract_raw() {
33 local source=$1
34 local offset=$2
35 local size=4096
36
37 dd if=$source of=/lib/firmware/$FIRMWARE bs=1 skip=$offset count=$size 2>/dev/null || \
38 ath9k_eeprom_die "failed to extract from $mtd"
39 }
40
41 ath9k_eeprom_extract() {
42 local part=$1
43 local offset=$2
44 local mtd
45
46 mtd=$(find_mtd_chardev $part)
47 [ -n "$mtd" ] || \
48 ath9k_eeprom_die "no mtd device found for partition $part"
49
50 ath9k_eeprom_extract_raw $mtd $offset
51 }
52
53 ath9k_ubi_eeprom_extract() {
54 local part=$1
55 local offset=$2
56 local ubidev=$(nand_find_ubi $CI_UBIPART)
57 local ubi
58
59 ubi=$(nand_find_volume $ubidev $part)
60 [ -n "$ubi" ] || \
61 ath9k_eeprom_die "no UBI volume found for $part"
62
63 ath9k_eeprom_extract_raw /dev/$ubi $offset
64 }
65
66 ath9k_patch_firmware_mac() {
67 local mac=$1
68 local mac_offset=$2
69 local swap=$3
70 local chksum_offset=$4
71 local xor_mac
72 local xor_fw_mac
73 local xor_fw_chksum
74
75 [ -z "$mac" -o -z "$mac_offset" ] && return
76
77 [ $swap -gt 0 ] && mac="${mac:3:2}:${mac:0:2}:${mac:9:2}:${mac:6:2}:${mac:15:2}:${mac:12:2}"
78
79 [ -n "$chksum_offset" ] && {
80 xor_mac=${mac//:/}
81 xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
82
83 xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
84 xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
85
86 xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
87 xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
88
89 printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
90 dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=$chksum_offset count=2
91 }
92
93 macaddr_2bin $mac | dd of=/lib/firmware/$FIRMWARE conv=notrunc bs=1 seek=$mac_offset count=6
94 }
95
96 case "$FIRMWARE" in
97 "ath9k-eeprom-pci-0000:00:0e.0.bin" | \
98 "ath9k-eeprom-pci-0000:01:00.0.bin" | \
99 "ath9k-eeprom-pci-0000:02:00.0.bin")
100 board=$(lantiq_board_name)
101
102 case "$board" in
103 ARV7518PW)
104 ath9k_eeprom_extract "boardconfig" 1024
105 ;;
106 ARV8539PW22)
107 ath9k_eeprom_extract "art" 1024
108 ;;
109 BTHOMEHUBV2B)
110 ath9k_eeprom_extract "art" 0
111 ath9k_patch_firmware_mac "00:00:00:00:00:00" 524 1 514
112 ;;
113 BTHOMEHUBV3A)
114 ath9k_eeprom_extract "art-copy" 0
115 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_ascii uboot-env ethaddr) +2) 268 1 258
116 ;;
117 BTHOMEHUBV5A)
118 ath9k_eeprom_extract "caldata" 4096
119 ;;
120 DGN3500*)
121 ath9k_eeprom_extract "calibration" 61440
122 ath9k_patch_firmware_mac $(macaddr_add $(mtd_get_mac_ascii uboot-env ethaddr) +2) 524 0 514
123 ;;
124 FRITZ3370)
125 ath9k_eeprom_extract "urlader" 2437
126 ath9k_patch_firmware_mac "00:00:00:00:00:00" 2 0
127 ;;
128 FRITZ7320|FRITZ7360SL)
129 ath9k_eeprom_extract "urlader" 2437
130 ath9k_patch_firmware_mac "00:00:00:00:00:00" 268 0 258
131 ;;
132 TDW8970|TDW8980)
133 ath9k_eeprom_extract "boardconfig" 135168
134 ;;
135 *)
136 ath9k_eeprom_die "board $board is not supported yet"
137 ;;
138 esac
139 ;;
140 esac