b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | # Copyright (C) 2019 OpenWrt.org |
| 2 | |
| 3 | . /lib/functions.sh |
| 4 | . /lib/functions/system.sh |
| 5 | |
| 6 | caldata_dd() { |
| 7 | local source=$1 |
| 8 | local target=$2 |
| 9 | local count=$(($3)) |
| 10 | local offset=$(($4)) |
| 11 | |
| 12 | dd if=$source of=$target iflag=skip_bytes,fullblock bs=$count skip=$offset count=1 2>/dev/null |
| 13 | return $? |
| 14 | } |
| 15 | |
| 16 | caldata_die() { |
| 17 | echo "caldata: " "$*" |
| 18 | exit 1 |
| 19 | } |
| 20 | |
| 21 | caldata_extract() { |
| 22 | local part=$1 |
| 23 | local offset=$(($2)) |
| 24 | local count=$(($3)) |
| 25 | local mtd |
| 26 | |
| 27 | mtd=$(find_mtd_chardev $part) |
| 28 | [ -n "$mtd" ] || caldata_die "no mtd device found for partition $part" |
| 29 | |
| 30 | caldata_dd $mtd /lib/firmware/$FIRMWARE $count $offset || \ |
| 31 | caldata_die "failed to extract calibration data from $mtd" |
| 32 | } |
| 33 | |
| 34 | caldata_extract_ubi() { |
| 35 | local part=$1 |
| 36 | local offset=$(($2)) |
| 37 | local count=$(($3)) |
| 38 | local ubidev |
| 39 | local ubi |
| 40 | |
| 41 | . /lib/upgrade/nand.sh |
| 42 | |
| 43 | ubidev=$(nand_find_ubi $CI_UBIPART) |
| 44 | ubi=$(nand_find_volume $ubidev $part) |
| 45 | [ -n "$ubi" ] || caldata_die "no UBI volume found for $part" |
| 46 | |
| 47 | caldata_dd /dev/$ubi /lib/firmware/$FIRMWARE $count $offset || \ |
| 48 | caldata_die "failed to extract calibration data from $ubi" |
| 49 | } |
| 50 | |
| 51 | caldata_extract_mmc() { |
| 52 | local part=$1 |
| 53 | local offset=$(($2)) |
| 54 | local count=$(($3)) |
| 55 | local mmc_part |
| 56 | |
| 57 | mmc_part=$(find_mmc_part $part) |
| 58 | [ -n "$mmc_part" ] || caldata_die "no mmc partition found for partition $part" |
| 59 | |
| 60 | caldata_dd $mmc_part /lib/firmware/$FIRMWARE $count $offset || \ |
| 61 | caldata_die "failed to extract calibration data from $mmc_part" |
| 62 | } |
| 63 | |
| 64 | caldata_extract_reverse() { |
| 65 | local part=$1 |
| 66 | local offset=$2 |
| 67 | local count=$(($3)) |
| 68 | local mtd |
| 69 | local reversed |
| 70 | local caldata |
| 71 | |
| 72 | mtd=$(find_mtd_chardev "$part") |
| 73 | reversed=$(hexdump -v -s $offset -n $count -e '1/1 "%02x "' $mtd) |
| 74 | |
| 75 | for byte in $reversed; do |
| 76 | caldata="\x${byte}${caldata}" |
| 77 | done |
| 78 | |
| 79 | printf "%b" "$caldata" > /lib/firmware/$FIRMWARE |
| 80 | } |
| 81 | |
| 82 | caldata_from_file() { |
| 83 | local source=$1 |
| 84 | local offset=$(($2)) |
| 85 | local count=$(($3)) |
| 86 | local target=$4 |
| 87 | |
| 88 | [ -n "$target" ] || target=/lib/firmware/$FIRMWARE |
| 89 | |
| 90 | caldata_dd $source $target $count $offset || \ |
| 91 | caldata_die "failed to extract calibration data from $source" |
| 92 | } |
| 93 | |
| 94 | caldata_sysfsload_from_file() { |
| 95 | local source=$1 |
| 96 | local offset=$(($2)) |
| 97 | local count=$(($3)) |
| 98 | local target_dir="/sys/$DEVPATH" |
| 99 | local target="$target_dir/data" |
| 100 | |
| 101 | [ -d "$target_dir" ] || \ |
| 102 | caldata_die "no sysfs dir to write: $target" |
| 103 | |
| 104 | echo 1 > "$target_dir/loading" |
| 105 | caldata_dd $source $target $count $offset |
| 106 | if [ $? != 0 ]; then |
| 107 | echo 1 > "$target_dir/loading" |
| 108 | caldata_die "failed to extract calibration data from $source" |
| 109 | else |
| 110 | echo 0 > "$target_dir/loading" |
| 111 | fi |
| 112 | } |
| 113 | |
| 114 | caldata_valid() { |
| 115 | local expected="$1" |
| 116 | local target=$2 |
| 117 | |
| 118 | [ -n "$target" ] || target=/lib/firmware/$FIRMWARE |
| 119 | |
| 120 | magic=$(hexdump -v -n 2 -e '1/1 "%02x"' $target) |
| 121 | [ "$magic" = "$expected" ] |
| 122 | return $? |
| 123 | } |
| 124 | |
| 125 | caldata_patch_data() { |
| 126 | local data=$1 |
| 127 | local data_count=$((${#1} / 2)) |
| 128 | [ -n "$2" ] && local data_offset=$(($2)) |
| 129 | [ -n "$3" ] && local chksum_offset=$(($3)) |
| 130 | local target=$4 |
| 131 | local fw_data |
| 132 | local fw_chksum |
| 133 | |
| 134 | [ -z "$data" -o -z "$data_offset" ] && return |
| 135 | |
| 136 | [ -n "$target" ] || target=/lib/firmware/$FIRMWARE |
| 137 | |
| 138 | fw_data=$(hexdump -v -n $data_count -s $data_offset -e '1/1 "%02x"' $target) |
| 139 | |
| 140 | if [ "$data" != "$fw_data" ]; then |
| 141 | |
| 142 | if [ -n "$chksum_offset" ]; then |
| 143 | fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '1/1 "%02x"' $target) |
| 144 | fw_chksum=$(xor $fw_chksum $(data_2xor_val $fw_data) $(data_2xor_val $data)) |
| 145 | |
| 146 | data_2bin $fw_chksum | \ |
| 147 | dd of=$target conv=notrunc bs=1 seek=$chksum_offset count=2 || \ |
| 148 | caldata_die "failed to write chksum to eeprom file" |
| 149 | fi |
| 150 | |
| 151 | data_2bin $data | \ |
| 152 | dd of=$target conv=notrunc bs=1 seek=$data_offset count=$data_count || \ |
| 153 | caldata_die "failed to write data to eeprom file" |
| 154 | fi |
| 155 | } |
| 156 | |
| 157 | ath9k_patch_mac() { |
| 158 | local mac=$1 |
| 159 | local target=$2 |
| 160 | |
| 161 | caldata_patch_data "${mac//:/}" 0x2 "" "$target" |
| 162 | } |
| 163 | |
| 164 | ath9k_patch_mac_crc() { |
| 165 | local mac=$1 |
| 166 | local mac_offset=$2 |
| 167 | local chksum_offset=$((mac_offset - 10)) |
| 168 | local target=$4 |
| 169 | |
| 170 | caldata_patch_data "${mac//:/}" "$mac_offset" "$chksum_offset" "$target" |
| 171 | } |
| 172 | |
| 173 | ath10k_patch_mac() { |
| 174 | local mac=$1 |
| 175 | local target=$2 |
| 176 | |
| 177 | caldata_patch_data "${mac//:/}" 0x6 0x2 "$target" |
| 178 | } |
| 179 | |
| 180 | ath11k_patch_mac() { |
| 181 | local mac=$1 |
| 182 | # mac_id from 0 to 5 |
| 183 | local mac_id=$2 |
| 184 | local target=$3 |
| 185 | |
| 186 | [ -z "$mac_id" ] && return |
| 187 | |
| 188 | caldata_patch_data "${mac//:/}" $(printf "0x%x" $(($mac_id * 0x6 + 0xe))) 0xa "$target" |
| 189 | } |
| 190 | |
| 191 | ath10k_remove_regdomain() { |
| 192 | local target=$1 |
| 193 | |
| 194 | caldata_patch_data "0000" 0xc 0x2 "$target" |
| 195 | } |
| 196 | |
| 197 | ath11k_remove_regdomain() { |
| 198 | local target=$1 |
| 199 | local regdomain |
| 200 | local regdomain_data |
| 201 | |
| 202 | regdomain=$(hexdump -v -n 2 -s 0x34 -e '1/1 "%02x"' $target) |
| 203 | caldata_patch_data "0000" 0x34 0xa "$target" |
| 204 | |
| 205 | for offset in 0x450 0x458 0x500 0x5a8; do |
| 206 | regdomain_data=$(hexdump -v -n 2 -s $offset -e '1/1 "%02x"' $target) |
| 207 | |
| 208 | if [ "$regdomain" == "$regdomain_data" ]; then |
| 209 | caldata_patch_data "0000" $offset 0xa "$target" |
| 210 | fi |
| 211 | done |
| 212 | } |
| 213 | |
| 214 | ath11k_set_macflag() { |
| 215 | local target=$1 |
| 216 | |
| 217 | caldata_patch_data "0100" 0x3e 0xa "$target" |
| 218 | } |