package/base-files: caldata: allow setting target file
[openwrt/staging/blocktrron.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 if=$source of=$target iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
68 caldata_die "failed to extract calibration data from $source"
69 }
70
71 caldata_valid() {
72 local expected="$1"
73 local target=$2
74
75 [ -n "$target" ] || target=/lib/firmware/$FIRMWARE
76
77 magic=$(hexdump -v -n 2 -e '1/1 "%02x"' $target)
78 [ "$magic" = "$expected" ]
79 return $?
80 }
81
82 caldata_patch_chksum() {
83 local mac=$1
84 local mac_offset=$(($2))
85 local chksum_offset=$(($3))
86 local target=$4
87 local xor_mac
88 local xor_fw_mac
89 local xor_fw_chksum
90
91 xor_mac=${mac//:/}
92 xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
93
94 xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
95 xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
96
97 xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
98 xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
99
100 printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
101 dd of=$target conv=notrunc bs=1 seek=$chksum_offset count=2
102 }
103
104 caldata_patch_mac() {
105 local mac=$1
106 local mac_offset=$(($2))
107 local chksum_offset=$3
108 local target=$4
109
110 [ -z "$mac" -o -z "$mac_offset" ] && return
111
112 [ -n "$target" ] || target=/lib/firmware/$FIRMWARE
113
114 [ -n "$chksum_offset" ] && caldata_patch_chksum "$mac" "$mac_offset" "$chksum_offset" "$target"
115
116 macaddr_2bin $mac | dd of=$target conv=notrunc oflag=seek_bytes bs=6 seek=$mac_offset count=1 || \
117 caldata_die "failed to write MAC address to eeprom file"
118 }
119
120 ath9k_patch_mac() {
121 local mac=$1
122 local target=$2
123
124 caldata_patch_mac "$mac" 0x2 "" "$target"
125 }
126
127 ath9k_patch_mac_crc() {
128 local mac=$1
129 local mac_offset=$2
130 local chksum_offset=$((mac_offset - 10))
131 local target=$4
132
133 caldata_patch_mac "$mac" "$mac_offset" "$chksum_offset" "$target"
134 }
135
136 ath10k_patch_mac() {
137 local mac=$1
138 local target=$2
139
140 caldata_patch_mac "$mac" 0x6 0x2 "$target"
141 }