Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qualcommax: ipq807x: use ath11k_patch_mac and ath11k_remove_regdomain for MX4200 and RAX120v2 #14512

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions package/base-files/files/lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ xor() {
printf "%0${retlen}x" "$ret"
}

data_2bin() {
local data=$1
local len=${#1}
local bin_data

for i in $(seq 0 2 $(($len - 1))); do
bin_data="${bin_data}\x${data:i:2}"
done

echo -ne $bin_data
}

data_2xor_val() {
local data=$1
local len=${#1}
local xor_data

for i in $(seq 0 4 $(($len - 1))); do
xor_data="${xor_data}${data:i:4} "
done

echo -n ${xor_data:0:-1}
}

append() {
local var="$1"
local value="$2"
Expand Down
96 changes: 65 additions & 31 deletions package/base-files/files/lib/functions/caldata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ caldata_extract_reverse() {
local caldata

mtd=$(find_mtd_chardev "$part")
reversed=$(hexdump -v -s $offset -n $count -e '/1 "%02x "' $mtd)
reversed=$(hexdump -v -s $offset -n $count -e '1/1 "%02x "' $mtd)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks caldata at least on zyxel,nbg7815 I tested this without using any of the auxiliary functions like caldata_patch etc. purely using caldata_extract

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the output for this two commands?

hexdump -v -s 0x1000 -n 10 -e '1/1 "%02x "' /dev/mtd17
hexdump -v -s 0x1000 -n 10 -e '/1 "%02x "' /dev/mtd17

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that it breaks sth. for nbg7815. I've tried it yesterday with 6.6 (vanilla, just pulling your 3 commits). I didn't look into it further. So I cannot confirm its this specific line. The output for both commands on my device is identical:

01 00 04 04 00 00 00 00 00 80

I just observed that within dmesg there was a line complaining "firmware not found". I'll try to make a new build in a few hours and try to collect more information if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run this bash script and compare checksum:

#!/bin/sh

caldata=""

reversed=$(hexdump -v -s 0x1000 -n 131072 -e '1/1 "%02x "' /dev/mtd17)

for byte in $reversed; do
	caldata="\x${byte}${caldata}"
done
printf "%b" "$caldata" > /tmp/pre-caldata.bin

caldata=""

reversed=$(hexdump -v -s 0x1000 -n 131072 -e '/1 "%02x "' /dev/mtd17)

for byte in $reversed; do
	caldata="\x${byte}${caldata}"
done
printf "%b" "$caldata" > /tmp/pre-caldata_2.bin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sth. is wrong in this for-loop. It get stuck forever. $reversed is filled.
I've dumped the hexdump into a file:
https://0x0.st/s/S2y3XX3Ui44ZS34PuHJFxQ/Xirw.bin
In case you don't want to change/wait for reanswer.
Both files are identical (checked via md5sum). So one is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pwned-pixel Can you check if these commands give you correct MAC addresses?

hexdump -s 14 -n 6 -C /lib/firmware/ath11k/IPQ8074/hw2.0/cal-ahb-c000000.wifi.bin
hexdump -s 20 -n 6 -C /lib/firmware/ath11k/IPQ8074/hw2.0/cal-ahb-c000000.wifi.bin
hexdump -s 26 -n 6 -C /lib/firmware/ath11k/IPQ8074/hw2.0/cal-ahb-c000000.wifi.bin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O. K. Forget about the written. Its now loading reliable with the following MAC's

root@OpenWrt:~# hexdump -s 14 -n 6 -C /lib/firmware/ath11k/IPQ8074/hw2.0/cal-ahb-c000000.wifi.bin
0000000e  bc cf 4f ff 1b a3                                 |..O...|
00000014
root@OpenWrt:~# hexdump -s 20 -n 6 -C /lib/firmware/ath11k/IPQ8074/hw2.0/cal-ahb-c000000.wifi.bin
00000014  bc cf 4f ff 1b a4                                 |..O...|
0000001a
root@OpenWrt:~# hexdump -s 26 -n 6 -C /lib/firmware/ath11k/IPQ8074/hw2.0/cal-ahb-c000000.wifi.bin
0000001a  00 03 7f 12 34 58                                 |....4X|
00000020

But the MAC's showing up in LuCI respectivley:

root@OpenWrt:/sys# cat devices/platform/soc@0/c000000.wifi/ieee80211/phy0/addresses 
00:03:7f:12:8e:43
root@OpenWrt:/sys# cat devices/platform/soc@0/c000000.wifi/ieee80211/phy1/addresses 
00:03:7f:12:72:17
root@OpenWrt:/sys# cat devices/platform/soc@0/c000000.wifi/ieee80211/phy2/addresses 
00:03:7f:12:c6:3b

are random.

Despite the last one didn't get changed how would I force an update? /sbin/wifi reload is doing nothing. Removing and reloading the driver isn't doing anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAC ids used in function starts from 1 not 0. That's why third MAC is not set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Y, that what I thought already. I've tried several combinations. But all the same. Then I realized this file is meant to not change across reboots. So editing the config file did nothing. Oh dear. ;D
Made a "normal" build now with all my settings and applications. Everything is working now. Thank you very much for your endurance in guiding me.

Final settings I've use are:

	zyxel,nbg7815)
		caldata_extract "0:art" 0x1000 0x20000
		label_mac=$(get_mac_label)
		ath11k_patch_mac $(macaddr_add $label_mac 2) 1 
		ath11k_patch_mac $(macaddr_add $label_mac 3) 2 
		ath11k_patch_mac $(macaddr_add $label_mac 4) 3 
		ath11k_fix_caldata
		;;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the interfaces are numbered from 0, I changed the parameters for function from 0 to 5.


for byte in $reversed; do
caldata="\x${byte}${caldata}"
Expand Down Expand Up @@ -122,49 +122,43 @@ caldata_valid() {
return $?
}

caldata_patch_chksum() {
local mac=$1
local mac_offset=$(($2))
caldata_patch_data() {
local data=$1
local data_count=$((${#1} / 2))
local data_offset=$(($2))
local chksum_offset=$(($3))
local target=$4
local xor_mac
local xor_fw_mac
local xor_fw_chksum
local fw_data
local fw_chksum

xor_mac=${mac//:/}
xor_mac="${xor_mac:0:4} ${xor_mac:4:4} ${xor_mac:8:4}"
[ -z "$data" -o -z "$data_offset" ] && return

xor_fw_mac=$(hexdump -v -n 6 -s $mac_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
xor_fw_mac="${xor_fw_mac:0:4} ${xor_fw_mac:4:4} ${xor_fw_mac:8:4}"
[ -n "$target" ] || target=/lib/firmware/$FIRMWARE

xor_fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '/1 "%02x"' /lib/firmware/$FIRMWARE)
xor_fw_chksum=$(xor $xor_fw_chksum $xor_fw_mac $xor_mac)
fw_data=$(hexdump -v -n $data_count -s $data_offset -e '1/1 "%02x"' $target)

printf "%b" "\x${xor_fw_chksum:0:2}\x${xor_fw_chksum:2:2}" | \
dd of=$target conv=notrunc bs=1 seek=$chksum_offset count=2
}
if [ "$data" != "$fw_data" ]; then

caldata_patch_mac() {
local mac=$1
local mac_offset=$(($2))
local chksum_offset=$3
local target=$4

[ -z "$mac" -o -z "$mac_offset" ] && return
if [ -n "$chksum_offset" ]; then
fw_chksum=$(hexdump -v -n 2 -s $chksum_offset -e '1/1 "%02x"' $target)
fw_chksum=$(xor $fw_chksum $(data_2xor_val $fw_data) $(data_2xor_val $data))

[ -n "$target" ] || target=/lib/firmware/$FIRMWARE
data_2bin $fw_chksum | \
dd of=$target conv=notrunc bs=1 seek=$chksum_offset count=2 || \
caldata_die "failed to write chksum to eeprom file"
fi

[ -n "$chksum_offset" ] && caldata_patch_chksum "$mac" "$mac_offset" "$chksum_offset" "$target"

macaddr_2bin $mac | dd of=$target conv=notrunc oflag=seek_bytes bs=6 seek=$mac_offset count=1 || \
caldata_die "failed to write MAC address to eeprom file"
data_2bin $data | \
dd of=$target conv=notrunc bs=1 seek=$data_offset count=$data_count || \
caldata_die "failed to write data to eeprom file"
fi
}

ath9k_patch_mac() {
local mac=$1
local target=$2

caldata_patch_mac "$mac" 0x2 "" "$target"
caldata_patch_data "${mac//:/}" 0x2 "" "$target"
}

ath9k_patch_mac_crc() {
Expand All @@ -173,12 +167,52 @@ ath9k_patch_mac_crc() {
local chksum_offset=$((mac_offset - 10))
local target=$4

caldata_patch_mac "$mac" "$mac_offset" "$chksum_offset" "$target"
caldata_patch_data "${mac//:/}" "$mac_offset" "$chksum_offset" "$target"
}

ath10k_patch_mac() {
local mac=$1
local target=$2

caldata_patch_mac "$mac" 0x6 0x2 "$target"
caldata_patch_data "${mac//:/}" 0x6 0x2 "$target"
}

ath11k_patch_mac() {
local mac=$1
# mac_id from 0 to 5
local mac_id=$2
local target=$3

[ -z "$mac_id" ] && return

caldata_patch_data "${mac//:/}" $(printf "0x%x" $(($mac_id * 0x6 + 0xe))) 0xa "$target"
}

ath10k_remove_regdomain() {
local target=$1

caldata_patch_data "0000" 0xc 0x2 "$target"
}

ath11k_remove_regdomain() {
local target=$1
local regdomain
local regdomain_data

regdomain=$(hexdump -v -n 2 -s 0x34 -e '1/1 "%02x"' $target)
caldata_patch_data "0000" 0x34 0xa "$target"

for offset in 0x450 0x458 0x500 0x5a8; do
regdomain_data=$(hexdump -v -n 2 -s $offset -e '1/1 "%02x"' $target)

if [ "$regdomain" == "$regdomain_data" ]; then
caldata_patch_data "0000" $offset 0xa "$target"
fi
done
}

ath11k_set_macflag() {
local target=$1

caldata_patch_data "0100" 0x3e 0xa "$target"
}
6 changes: 0 additions & 6 deletions package/base-files/files/lib/functions/system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,6 @@ macaddr_random() {
echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${randsrc}")")"
}

macaddr_2bin() {
local mac=$1

echo -ne \\x${mac//:/\\x}
}

macaddr_canonicalize() {
local mac="$1"
local canon=""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ ipq807x_setup_macs()
local label_mac=""

case "$board" in
linksys,mx4200v1|\
linksys,mx4200v2)
label_mac=$(mtd_get_mac_ascii devinfo hw_mac_addr)
for i in $(seq 3 5); do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ case "$FIRMWARE" in
dynalink,dl-wrx36|\
edgecore,eap102|\
edimax,cax1800|\
linksys,mx4200v1|\
linksys,mx4200v2|\
linksys,mx5300|\
netgear,rax120v2|\
netgear,wax218|\
netgear,wax620|\
netgear,wax630|\
Expand All @@ -33,6 +30,26 @@ case "$FIRMWARE" in
zyxel,nbg7815)
caldata_extract "0:art" 0x1000 0x20000
;;
linksys,mx4200v1)
caldata_extract "0:art" 0x1000 0x20000
ath11k_remove_regdomain
;;
linksys,mx4200v2)
caldata_extract "0:art" 0x1000 0x20000
label_mac=$(mtd_get_mac_ascii devinfo hw_mac_addr)
ath11k_patch_mac $(macaddr_add $label_mac 2) 0
ath11k_patch_mac $(macaddr_add $label_mac 1) 1
ath11k_patch_mac $(macaddr_add $label_mac 3) 2
ath11k_remove_regdomain
ath11k_set_macflag
;;
netgear,rax120v2)
caldata_extract "0:art" 0x1000 0x20000
ath11k_patch_mac $(mtd_get_mac_binary boarddata1 0xc) 0
ath11k_patch_mac $(mtd_get_mac_binary boarddata1 0x0) 1
ath11k_patch_mac $(mtd_get_mac_binary boarddata1 0x6) 2
ath11k_set_macflag
;;
prpl,haze|\
spectrum,sax1v1k)
caldata_extract_mmc "0:ART" 0x1000 0x20000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ case "$board" in
[ "$PHYNBR" = "0" ] && macaddr_add $(get_mac_label) 2 > /sys${DEVPATH}/macaddress
[ "$PHYNBR" = "1" ] && macaddr_add $(get_mac_label) 3 > /sys${DEVPATH}/macaddress
;;
linksys,mx4200v1|\
linksys,mx4200v2)
label_mac=$(mtd_get_mac_ascii devinfo hw_mac_addr)
[ "$PHYNBR" = "0" ] && macaddr_add $label_mac 2 > /sys${DEVPATH}/macaddress
[ "$PHYNBR" = "1" ] && macaddr_add $label_mac 1 > /sys${DEVPATH}/macaddress
[ "$PHYNBR" = "2" ] && macaddr_add $label_mac 3 > /sys${DEVPATH}/macaddress
;;
zbtlink,zbt-z800ax)
[ "$PHYNBR" = "0" ] && macaddr_add $(get_mac_label) -1 > /sys${DEVPATH}/macaddress
[ "$PHYNBR" = "1" ] && macaddr_add $(get_mac_label) -2 > /sys${DEVPATH}/macaddress
Expand Down