Skip to content

Commit fa3b9f5

Browse files
qzydustinrobimarko
authored andcommitted
qualcommax: ipq60xx: enable dual-boot for 360v6
Add dual-partition upgrade support for Qihoo 360v6 using the generic bootconfig.sh library. This enables safe system upgrades with automatic failover capability. The device uses Qualcomm's bootconfig structure to control A/B partition switching. The bootloader dynamically maps physical NAND partitions to logical MTD devices based on the bootconfig, ensuring firmware always writes to the inactive partition. Implementation details: - Use bootconfig.sh library (copied from ipq50xx) for bootconfig operations - Operate on 'rootfs' partition by name instead of hardcoded offset - Add magic header validation for safety - Remove OEM UBI volumes (wifi_fw and ubi_rootfs) before sysupgrade - Toggle bootconfig before removing OEM volumes Hardware details: - SoC: Qualcomm IPQ6000 - Flash: NAND with dual rootfs partitions (mtd16/mtd17) - Bootconfig: controls slot selection via partition name lookup Installation: Standard sysupgrade process. After upgrade, the system will boot from the new partition while preserving the old system as backup. The OEM volume cleanup is necessary because these volumes are created by the stock firmware and are not automatically cleaned by the standard nand_upgrade_prepare_ubi() function, which only removes volumes named 'kernel', 'rootfs', and 'rootfs_data'. Without this cleanup, the remaining OEM volumes consume available space, causing the creation of rootfs_data to fail during sysupgrade. Tested on Qihoo 360v6 running stock firmware and OpenWrt. Signed-off-by: Zhenyu Qi <[email protected]> Link: openwrt/openwrt#21154 Signed-off-by: Robert Marko <[email protected]>
1 parent 08a1cac commit fa3b9f5

File tree

2 files changed

+223
-1
lines changed

2 files changed

+223
-1
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
. /lib/functions.sh
2+
3+
PART_SIZE=20
4+
NAME_SIZE=16
5+
MAX_NUM_PARTS=16
6+
7+
MAGIC_START_HEX="a0 a1 a2 a3"
8+
MAGIC_START_TRY_HEX="a1 a1 a2 a3"
9+
MAGIC_END_HEX="b0 b1 b2 b3"
10+
11+
validate_bootconfig_magic() {
12+
local file=$1
13+
magic_start=$(hexdump -v -n 4 -e '4/1 "%02x "' "$file")
14+
magic_end=$(hexdump -v -s 332 -n 4 -e '4/1 "%02x "' "$file")
15+
16+
if [ "$magic_start" != "$MAGIC_START_HEX" ] && \
17+
[ "$magic_start" != "$MAGIC_START_TRY_HEX" ]; then
18+
echo "Not a valid bootconfig file, start magic does not match" >&2
19+
return 1
20+
fi
21+
22+
if [ "$magic_end" != "$MAGIC_END_HEX" ]; then
23+
echo "Not a valid bootconfig file, end magic does not match" >&2
24+
return 1
25+
fi
26+
return 0
27+
}
28+
29+
get_bootconfig_numparts() {
30+
local file=$1
31+
numpartshex=$(hexdump -v -s 8 -n 4 -e '4/1 "%02x "' "$file")
32+
numparts=$(( 0x$(echo $numpartshex | awk '{print $4$3$2$1}') ))
33+
echo ${numparts}
34+
}
35+
36+
get_bootconfig_partidx() {
37+
local file=$1
38+
local partname=$2
39+
local numparts=$(get_bootconfig_numparts "$file")
40+
41+
if [ -z "$numparts" ]; then
42+
echo "Could not get number of partitions" >&2
43+
return
44+
fi
45+
46+
if [ $numparts -gt $MAX_NUM_PARTS ]; then
47+
numparts=$MAX_NUM_PARTS
48+
fi
49+
50+
for i in $(seq 0 $((numparts -1))); do
51+
nameoffset=$((12 + i * $PART_SIZE))
52+
nameraw=$(dd if="$file" bs=1 skip="$nameoffset" count=12 2>/dev/null)
53+
name=${nameraw//S'\x00'/}
54+
if [ "$partname" = "$name" ]; then
55+
echo $i
56+
fi
57+
done
58+
}
59+
60+
get_bootconfig_primaryboot() {
61+
local file=$1
62+
local partname=$2
63+
64+
local partidx=$(get_bootconfig_partidx "$file" "$partname")
65+
if ! echo "$partidx" | grep -Eq '^[0-9]+$'; then
66+
echo "Could not get partition index for $partname in $file" >&2
67+
return
68+
fi
69+
70+
if [ "$partidx" -ge 0 ] && [ "$partidx" -lt $MAX_NUM_PARTS ]; then
71+
offset=$((12 + $partidx * $PART_SIZE + $NAME_SIZE))
72+
primaryboothex=$(hexdump -v -s "$offset" -n 4 -e '4/1 "%02x "' $file)
73+
primaryboot=$(( 0x$(echo $primaryboothex | awk '{print $4$3$2$1}') ))
74+
echo $primaryboot
75+
fi
76+
}
77+
78+
_set_bootconfig_primaryboot() {
79+
local file=$1
80+
local partname=$2
81+
local primaryboot=$3
82+
local primaryboothex
83+
local partidx
84+
local primarybootoffset
85+
86+
partidx=$(get_bootconfig_partidx "$file" "$partname")
87+
if ! echo "$partidx" | grep -Eq '^[0-9]+$'; then
88+
echo "Could not get partition index for $2" >&2
89+
return 1
90+
fi
91+
primarybootoffset=$((12 + $partidx * $PART_SIZE + $NAME_SIZE))
92+
93+
case "$primaryboot" in
94+
0)
95+
printf "\x00\x00\x00\x00" | dd of="$file" seek="$primarybootoffset" bs=1 count=4 conv=notrunc 2>/dev/null
96+
;;
97+
1)
98+
printf "\x01\x00\x00\x00" | dd of="$file" seek="$primarybootoffset" bs=1 count=4 conv=notrunc 2>/dev/null
99+
;;
100+
*)
101+
echo "invalid argument: primaryboot must be 0 or 1" >&2
102+
return 1
103+
;;
104+
esac
105+
}
106+
107+
set_bootconfig_primaryboot() {
108+
local file=$1
109+
local partname=$2
110+
local primaryboot=$3
111+
112+
[ -z "$file" ] || [ -z "$partname" ] || [ -z "$primaryboot" ] && {
113+
echo "usage: $0 <file> <partition name> <0|1>"
114+
return 1
115+
}
116+
117+
[ ! -e "$file" ] && {
118+
echo "file $file not found" >&2
119+
return 1
120+
}
121+
122+
[ ! -w $file ] && {
123+
echo "file $file not writable" >&2
124+
return 1
125+
}
126+
127+
validate_bootconfig_magic "$file"
128+
[ $? -ne 0 ] && return 1
129+
130+
_set_bootconfig_primaryboot $file $partname $primaryboot
131+
[ $? -ne 0 ] && return 1
132+
133+
return 0
134+
}
135+
136+
toggle_bootconfig_primaryboot() {
137+
local file=$1
138+
local partname=$2
139+
local primaryboot
140+
141+
[ -z "$file" ] || [ -z "$partname" ] && {
142+
echo "usage: $0 <file> <partition name>"
143+
return 1
144+
}
145+
146+
[ ! -e "$file" ] && {
147+
echo "file $file not found" >&2
148+
return 1
149+
}
150+
151+
[ ! -w $file ] && {
152+
echo "file $file not writable" >&2
153+
return 1
154+
}
155+
156+
validate_bootconfig_magic "$file"
157+
[ $? -ne 0 ] && return 1
158+
159+
primaryboot=$(get_bootconfig_primaryboot "$1" "$2")
160+
161+
case "$primaryboot" in
162+
0)
163+
_set_bootconfig_primaryboot "$1" "$2" 1
164+
;;
165+
1)
166+
_set_bootconfig_primaryboot "$1" "$2" 0
167+
;;
168+
*)
169+
echo "invalid value: primaryboot must be 0 or 1" >&2
170+
return 1
171+
;;
172+
esac
173+
174+
[ $? -ne 0 ] && return 1
175+
176+
return 0
177+
}

target/linux/qualcommax/ipq60xx/base-files/lib/upgrade/platform.sh

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
. /lib/functions/bootconfig.sh
2+
13
PART_NAME=firmware
24
REQUIRE_IMAGE_METADATA=1
35

@@ -27,6 +29,43 @@ remove_oem_ubi_volume() {
2729
fi
2830
}
2931

32+
qihoo_bootconfig_toggle_rootfs() {
33+
local partname=$1
34+
local tempfile
35+
local mtdidx
36+
37+
mtdidx=$(find_mtd_index "$partname")
38+
[ ! "$mtdidx" ] && {
39+
echo "cannot find mtd index for $partname"
40+
return 1
41+
}
42+
43+
tempfile=/tmp/mtd"$mtdidx".bin
44+
dd if=/dev/mtd"$mtdidx" of="$tempfile" bs=1 count=336 2>/dev/null
45+
[ $? -ne 0 ] || [ ! -f "$tempfile" ] && {
46+
echo "failed to create a temp copy of /dev/mtd$mtdidx"
47+
return 1
48+
}
49+
50+
toggle_bootconfig_primaryboot "$tempfile" "rootfs"
51+
[ $? -ne 0 ] && {
52+
echo "failed to toggle primaryboot for rootfs partition"
53+
return 1
54+
}
55+
56+
mtd write "$tempfile" /dev/mtd"$mtdidx" 2>/dev/null
57+
[ $? -ne 0 ] && {
58+
echo "failed to write temp copy back to /dev/mtd$mtdidx"
59+
return 1
60+
}
61+
62+
# Update bootconfig1 if exists
63+
local mtdidx1=$(find_mtd_index "${partname}1")
64+
[ -n "$mtdidx1" ] && mtd write "$tempfile" /dev/mtd"$mtdidx1" 2>/dev/null
65+
66+
return 0
67+
}
68+
3069
tplink_get_boot_part() {
3170
local cur_boot_part
3271
local args
@@ -117,8 +156,14 @@ platform_do_upgrade() {
117156
;;
118157
glinet,gl-ax1800|\
119158
glinet,gl-axt1800|\
120-
netgear,wax214|\
159+
netgear,wax214)
160+
nand_do_upgrade "$1"
161+
;;
121162
qihoo,360v6)
163+
CI_UBIPART="rootfs_1"
164+
qihoo_bootconfig_toggle_rootfs "0:bootconfig"
165+
remove_oem_ubi_volume wifi_fw
166+
remove_oem_ubi_volume ubi_rootfs
122167
nand_do_upgrade "$1"
123168
;;
124169
netgear,wax610|\

0 commit comments

Comments
 (0)