package/base-files: caldata: work around dd's limitation
[openwrt/staging/jow.git] / package / base-files / files / lib / functions / caldata.sh
1 # Copyright (C) 2019 OpenWrt.org
2
3 . /lib/functions.sh
4 . /lib/functions/system.sh
5
6 caldata_die() {
7 echo "caldata: " "$*"
8 exit 1
9 }
10
11 caldata_extract() {
12 local part=$1
13 local offset=$(($2))
14 local count=$(($3))
15 local mtd
16
17 mtd=$(find_mtd_chardev $part)
18 [ -n "$mtd" ] || caldata_die "no mtd device found for partition $part"
19
20 dd if=$mtd of=/lib/firmware/$FIRMWARE iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
21 caldata_die "failed to extract calibration data from $mtd"
22 }
23
24 caldata_extract_ubi() {
25 local part=$1
26 local offset=$(($2))
27 local count=$(($3))
28 local ubidev
29 local ubi
30
31 . /lib/upgrade/nand.sh
32
33 ubidev=$(nand_find_ubi $CI_UBIPART)
34 ubi=$(nand_find_volume $ubidev $part)
35 [ -n "$ubi" ] || caldata_die "no UBI volume found for $part"
36
37 dd if=/dev/$ubi of=/lib/firmware/$FIRMWARE iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
38 caldata_die "failed to extract calibration data from $ubi"
39 }
40
41 caldata_extract_reverse() {
42 local part=$1
43 local offset=$2
44 local count=$(($3))
45 local mtd
46 local reversed
47 local caldata
48
49 mtd=$(find_mtd_chardev "$part")
50 reversed=$(hexdump -v -s $offset -n $count -e '/1 "%02x "' $mtd)
51
52 for byte in $reversed; do
53 caldata="\x${byte}${caldata}"
54 done
55
56 printf "%b" "$caldata" > /lib/firmware/$FIRMWARE
57 }
58
59 caldata_from_file() {
60 local source=$1
61 local offset=$(($2))
62 local count=$(($3))
63 local target=$4
64
65 [ -n "$target" ] || target=/lib/firmware/$FIRMWARE
66
67 # dd doesn't handle partial reads from special files: use cat
68 cat $source | dd of=$target iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
69 caldata_die "failed to extract calibration data from $source"
70 }
71
72 caldata_sysfsload_from_file() {
73 local source=$1
74 local offset=$(($2))
75 local count=$(($3))
76
77 # dd doesn't handle partial reads from special files: use cat
78 # test extract to /dev/null first
79 cat $source | dd of=/dev/null iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
80 caldata_die "failed to extract calibration data from $source"
81
82 # can't fail now
83 echo 1 > /sys/$DEVPATH/loading
84 cat $source | dd of=/sys/$DEVPATH/data iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null
85 echo 0 > /sys/$DEVPATH/loading
86 }
87
88 caldata_valid() {
89 local expected="$1"
90 local target=$2
91
92 [ -n "$target" ] || target=/lib/firmware/$FIRMWARE
93
94 magic=$(hexdump -v -n 2 -e '1/1 "%02x"' $target)
95 [ "$magic" = "$expected" ]
96 return $?
97 }
98
99 caldata_patch_chksum() {
100 local mac=$1
101 local mac_offset=$(($2))
102 local chksum_offset=$(($3))
103 local target=$4
104 local xor_mac
105 local xor_fw_mac
106 local xor_fw_chksum
107
108 xor_mac=${mac//:/}
109 xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
110
111 xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
112 xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
113
114 xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
115 xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
116
117 printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
118 dd of=$target conv=notrunc bs=1 seek=$chksum_offset count=2
119 }
120
121 caldata_patch_mac() {
122 local mac=$1
123 local mac_offset=$(($2))
124 local chksum_offset=$3
125 local target=$4
126
127 [ -z "$mac" -o -z "$mac_offset" ] && return
128
129 [ -n "$target" ] || target=/lib/firmware/$FIRMWARE
130
131 [ -n "$chksum_offset" ] && caldata_patch_chksum "$mac" "$mac_offset" "$chksum_offset" "$target"
132
133 macaddr_2bin $mac | dd of=$target conv=notrunc oflag=seek_bytes bs=6 seek=$mac_offset count=1 || \
134 caldata_die "failed to write MAC address to eeprom file"
135 }
136
137 ath9k_patch_mac() {
138 local mac=$1
139 local target=$2
140
141 caldata_patch_mac "$mac" 0x2 "" "$target"
142 }
143
144 ath9k_patch_mac_crc() {
145 local mac=$1
146 local mac_offset=$2
147 local chksum_offset=$((mac_offset - 10))
148 local target=$4
149
150 caldata_patch_mac "$mac" "$mac_offset" "$chksum_offset" "$target"
151 }
152
153 ath10k_patch_mac() {
154 local mac=$1
155 local target=$2
156
157 caldata_patch_mac "$mac" 0x6 0x2 "$target"
158 }