[PATCH] bcm4908: implement basic sysupgrade support

Rafał Miłecki zajec5 at gmail.com
Tue Mar 23 15:03:30 GMT 2021


From: Rafał Miłecki <rafal at milecki.pl>

It isn't UBI aware yet - that remains to be implemented.

Signed-off-by: Rafał Miłecki <rafal at milecki.pl>
---
 target/linux/bcm4908/Makefile                 |   4 +-
 .../base-files/lib/upgrade/platform.sh        | 114 ++++++++++++++++++
 2 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100644 target/linux/bcm4908/base-files/lib/upgrade/platform.sh

diff --git a/target/linux/bcm4908/Makefile b/target/linux/bcm4908/Makefile
index 5bee66fe2b..5f80c2f6ca 100644
--- a/target/linux/bcm4908/Makefile
+++ b/target/linux/bcm4908/Makefile
@@ -20,6 +20,8 @@ include $(INCLUDE_DIR)/target.mk
 
 KERNELNAME:=Image dtbs
 
-DEFAULT_PACKAGES += kmod-usb-ohci kmod-usb2 kmod-usb3
+DEFAULT_PACKAGES += \
+	bcm63xx-bootfs \
+	kmod-usb-ohci kmod-usb2 kmod-usb3
 
 $(eval $(call BuildTarget))
diff --git a/target/linux/bcm4908/base-files/lib/upgrade/platform.sh b/target/linux/bcm4908/base-files/lib/upgrade/platform.sh
new file mode 100644
index 0000000000..e285b9e013
--- /dev/null
+++ b/target/linux/bcm4908/base-files/lib/upgrade/platform.sh
@@ -0,0 +1,114 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+
+RAMFS_COPY_BIN="bcm63xx-bootfs"
+
+PART_NAME=firmware
+
+# $(1): file to read magic from
+# $(2): offset in bytes
+# $(3): length in bytes
+get_content() {
+	dd if="$1" skip=$2 bs=1 count=$3 2>/dev/null
+}
+
+platform_expected_image() {
+	local machine=$(board_name)
+
+	case "$machine" in
+		"asus,gt-ac5300")		echo "asus GT-AC5300"; return;;
+		"netgear,r8000p")		echo "chk U12H359T00_NETGEAR"; return;;
+		"tplink,archer-c2300-v1")	echo ""; return;;
+	esac
+}
+
+platform_identify() {
+	local magic
+	local size
+
+	size=$(wc -c "$1" | cut -d ' ' -f 1)
+
+	magic=$(get_content "$1" $(($size - 20 - 64 + 8)) 12)
+	[ "$magic" = "GT-AC5300" ] && {
+		echo "asus"
+		return
+	}
+
+	echo "unknown"
+}
+
+platform_check_image() {
+	[ "$#" -gt 1 ] && return 1
+
+	local expected_image="$(platform_expected_image)"
+	local file_type=$(platform_identify "$1")
+	local error=0
+
+	case "$file_type" in
+		"asus")
+			local size=$(wc -c "$1" | cut -d ' ' -f 1)
+			local productid=$(get_content "$1" $(($size - 20 - 64 + 8)) 12)
+
+			[ -n "$expected_image" -a "asus $productid" != "$expected_image" ] && {
+				echo "Firmware productid mismatch ($productid)"
+				error=1
+			}
+		;;
+		*)
+			echo "Invalid image type. Please use firmware specific for this device."
+			notify_firmware_broken
+			error=1
+		;;
+	esac
+
+	return $error
+}
+
+platform_calc_new_cferam() {
+	local mtd=$(find_mtd_part $PART_NAME)
+	[ -z "$mtd" ] && {
+		echo "Failed to find $PART_NAME partition" >&2
+		return
+	}
+
+	local idx=$(bcm63xx-bootfs ls $mtd | sed -n 's/cferam\.\(\d\d\d\)/\1/p')
+	[ -z "$idx" ] && {
+		echo "Failed to find cferam current index" >&2
+		return
+	}
+
+	idx=$(($idx + 1))
+	[ "$idx" = "1000" ] && idx=0
+
+	echo $(printf "cferam.%03d" $idx)
+}
+
+platform_img_from_asus_cmd() {
+	local size=$(wc -c "$1" | cut -d ' ' -f 1)
+
+	# Use bs=1 to workaround missing iflag=count_bytes
+	echo -n dd bs=1 count=$(($size - 20 - 64))
+}
+
+platform_do_upgrade() {
+	local file_type=$(platform_identify "$1")
+	local cmd=
+
+	# Find target cferam name
+	local cferam="$(platform_calc_new_cferam)"
+	[ -z "$cferam" ] && exit 1
+
+	bcm63xx-bootfs mv $1 cferam.000 $cferam || {
+		echo "Failed to rename cferam.000 to $cferam" >&2
+		exit 1
+	}
+
+	# Flash new firmware
+	case "$file_type" in
+		"asus")		cmd=$(platform_img_from_asus_cmd "$1");;
+	esac
+	mtd erase firmware
+	default_do_upgrade "$1" "$cmd"
+
+	echo "SUCCESS! Used $cferam"
+}
-- 
2.26.2




More information about the openwrt-devel mailing list