1 # SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
3 RAMFS_COPY_BIN
="bcm4908img expr"
7 # $(1): file to read from
8 # $(2): offset in bytes
9 # $(3): length in bytes
11 dd if="$1" skip
=$2 bs
=1 count
=$3 2>/dev
/null
14 # $(1): file to read from
15 # $(2): offset in bytes
17 dd if="$1" skip
=$2 bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/1 "%02x"'
20 platform_expected_image
() {
21 local machine
=$
(board_name
)
24 asus
,gt-ac5300
) echo "asus GT-AC5300";;
25 netgear
,r8000p
) echo "chk U12H359T00_NETGEAR";;
26 tplink
,archer-c2300-v1
) echo "";;
34 magic
=$
(get_hex_u32_be
"$1" 0)
42 size
=$
(wc -c "$1" | cut
-d ' ' -f 1)
44 magic
=$
(get_content
"$1" $
((size
- 20 - 64 + 8)) 12)
55 platform_check_image
() {
56 [ "$#" -gt 1 ] && return 1
58 local expected_image
=$
(platform_expected_image
)
61 bcm4908img info
-i "$1" > /dev
/null ||
{
62 echo "Failed to validate BCM4908 image" >&2
63 notify_firmware_broken
67 bcm4908img bootfs
-i "$1" ls |
grep -q "1-openwrt" ||
{
68 # OpenWrt images have 1-openwrt dummy file in the bootfs.
69 # Don't allow backup if it's missing
70 notify_firmware_no_backup
73 case "$(platform_identify "$1")" in
75 local size
=$
(wc -c "$1" | cut
-d ' ' -f 1)
76 local productid
=$
(get_content
"$1" $
((size
- 20 - 64 + 8)) 12)
78 [ -n "$expected_image" -a "asus $productid" != "$expected_image" ] && {
79 echo "Firmware productid mismatch ($productid)" >&2
84 local header_len
=$
((0x$
(get_hex_u32_be
"$1" 4)))
85 local board_id_len
=$
(($header_len - 40))
86 local board_id
=$
(dd if="$1" skip
=40 bs
=1 count
=$board_id_len 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
88 [ -n "$expected_image" -a "chk $board_id" != "$expected_image" ] && {
89 echo "Firmware board_id mismatch ($board_id)" >&2
94 echo "Invalid image type. Please use firmware specific for this device." >&2
95 notify_firmware_broken
103 # $1: cferam index increment value
104 platform_calc_new_cferam
() {
106 local dir
="/tmp/sysupgrade-bcm4908"
108 local mtd
=$
(find_mtd_part bootfs
)
110 echo "Failed to find bootfs partition" >&2
116 mount
-t jffs2
-o ro
$mtd $dir ||
{
117 echo "Failed to mount bootfs partition $mtd" >&2
122 local idx
=$
(ls $dir/cferam.??? |
sed -n 's/.*cferam\.\(\d\d\d\)/\1/p')
124 echo "Failed to find cferam current index" >&2
132 idx
=$
(($
(expr $idx + $inc) % 1000))
134 echo $
(printf "cferam.%03d" $idx)
137 platform_do_upgrade_ubi
() {
138 local dir
="/tmp/sysupgrade-bcm4908"
141 # Verify new bootfs size
142 local mtd_bootfs_size
=$
(grep "\"bootfs\"" /proc
/mtd |
sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
143 [ -z "$mtd_bootfs_size" ] && {
144 echo "Unable to find \"bootfs\" partition size"
147 mtd_bootfs_size
=$
((0x
$mtd_bootfs_size))
148 local img_bootfs_size
=$
(bcm4908img extract
-i "$1" -t bootfs |
wc -c)
149 [ $img_bootfs_size -gt $mtd_bootfs_size ] && {
150 echo "New bootfs doesn't fit MTD partition."
154 # Find cferam name for new firmware
155 # For UBI we always flash "firmware" so don't increase cferam index if
156 # there is "fallback". That could result in cferam.999 & cferam.001
157 [ -n "$(find_mtd_index backup)" -o -n "$(find_mtd_index fallback)" ] && inc
=0
158 local cferam
=$
(platform_calc_new_cferam
$inc)
159 [ -z "$cferam" ] && exit 1
161 # Prepare new firmware
162 bcm4908img bootfs
-i "$1" mv cferam
.000 $cferam ||
{
163 echo "Failed to rename cferam.000 to $cferam" >&2
167 # Extract rootfs for further flashing
170 bcm4908img extract
-i "$1" -t rootfs
> $dir/root ||
{
171 echo "Failed to extract rootfs" >&2
176 # Flash bootfs MTD partition with new one
177 mtd erase bootfs ||
{
178 echo "Failed to erase bootfs" >&2
182 bcm4908img extract
-i "$1" -t bootfs | mtd
write - bootfs ||
{
183 echo "Failed to flash bootfs" >&2
188 nand_do_upgrade
$dir/root
191 platform_do_upgrade
() {
192 # Try NAND aware UBI upgrade for OpenWrt images
193 # Below call will exit on success
194 bcm4908img bootfs
-i "$1" ls |
grep -q "1-openwrt" && platform_do_upgrade_ubi
"$1"
196 echo "Writing whole image to NAND flash. All erase counters will be lost."
198 # Find cferam name for new firmware
199 local cferam
=$
(platform_calc_new_cferam
1)
200 [ -z "$cferam" ] && exit 1
202 # Prepare new firmware
203 bcm4908img bootfs
-i "$1" mv cferam
.000 $cferam ||
{
204 echo "Failed to rename cferam.000 to $cferam" >&2
208 # Jush flash firmware partition as is
209 [ -n "$(find_mtd_index backup)" ] && PART_NAME
=backup
210 [ -n "$(find_mtd_index fallback)" ] && PART_NAME
=fallback
212 default_do_upgrade
"$1" "bcm4908img extract -t firmware"