From: Rafał Miłecki Date: Wed, 8 Dec 2021 11:21:48 +0000 (+0100) Subject: bcm4908: sysupgrade: add pkgtb format support X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=5f05795aa7716879e46fabbd0c51ff20ef9f13bf;p=openwrt%2Fstaging%2Fnbd.git bcm4908: sysupgrade: add pkgtb format support BCM4908 devices with U-Boot use pkgtb firmware format. It's based on U-Boot's FIT: DTB with configurations, images & embedded data. This format contains bootfs, rootfs and optionally a first stage U-Boot loader. Contained images need to be extracted & flashed. Broadcom used two sets of firmwares: main & backup. It uses UBI volumes "metadata1" & "metadata2" for storing U-Boot env variables with info about flashed images. Signed-off-by: Rafał Miłecki --- diff --git a/target/linux/bcm4908/base-files/lib/upgrade/platform.sh b/target/linux/bcm4908/base-files/lib/upgrade/platform.sh index c057f2dc58..e7d4bfe4de 100644 --- a/target/linux/bcm4908/base-files/lib/upgrade/platform.sh +++ b/target/linux/bcm4908/base-files/lib/upgrade/platform.sh @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause -RAMFS_COPY_BIN="bcm4908img expr" +RAMFS_COPY_BIN="bcm4908img expr egrep fdtget fw_printenv fw_setenv tr" PART_NAME=firmware @@ -15,6 +15,12 @@ get_content() { dd if="$1" skip=$2 bs=1 count=$3 2>/dev/null } +# $(1): file to read from +# $(2): offset in bytes +get_hex_u32_le() { + dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/4 "%02x"' +} + # $(1): file to read from # $(2): offset in bytes get_hex_u32_be() { @@ -37,13 +43,23 @@ platform_identify() { magic=$(get_hex_u32_be "$1" 0) case "$magic" in + d00dfeed) + BCM4908_FW_FORMAT="pkgtb" + return + ;; 2a23245e) local header_len=$((0x$(get_hex_u32_be "$1" 4))) local board_id_len=$(($header_len - 40)) BCM4908_FW_FORMAT="chk" BCM4908_FW_BOARD_ID=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"') - BCM4908_FW_INT_IMG_FORMAT="bcm4908img" + magic=$(get_hex_u32_be "$1" "$header_len") + [ "$magic" = "d00dfeed" ] && { + BCM4908_FW_INT_IMG_FORMAT="pkgtb" + } || { + BCM4908_FW_INT_IMG_FORMAT="bcm4908img" + } + BCM4908_FW_INT_IMG_EXTRACT_CMD="dd skip=$header_len iflag=skip_bytes" return ;; esac @@ -71,6 +87,117 @@ platform_identify() { } } +# +# pkgtb helpers +# + +platform_pkgtb_get_image_name() { + local configuration=$($2 < $1 | fdtget - /configurations default) + [ -z "$configuration" ] && { + echo "Failed to read default configuration from pkgtb" >&2 + return + } + + local image_name=$($2 < $1 | fdtget - /configurations/$configuration $3) + [ -z "$image_name" ] && { + echo "Failed to read $3 from pkgtb configuration \"$configuration\"" >&2 + return + } + + echo "$image_name" +} + +platform_pkgtb_get_image() { + local cmd="${2:-cat}" + + local image_name=$(platform_pkgtb_get_image_name "$1" "$cmd" "$3") + + $cmd < $1 | fdtget -p - /images/$image_name | egrep -q "^data$" && { + $cmd < $1 | fdtget -t r - /images/$image_name data + return + } + + $cmd < $1 | fdtget -p - /images/$image_name | egrep -q "^data-position$" && { + local data_position=$($cmd < $1 | fdtget - /images/$image_name data-position) + local data_size=$($cmd < $1 | fdtget - /images/$image_name data-size) + $cmd < $1 2>/dev/null | dd skip=$data_position count=$data_size iflag=skip_bytes,count_bytes + return + } + + $cmd < $1 | fdtget -p - /images/$image_name | egrep -q "^data-offset" && { + local data_offset=$($cmd < $1 | fdtget - /images/$image_name data-offset) + local totalsize=$(get_hex_u32_be "$1" 4) + local data_position=$(((0x$totalsize + data_offset + 3) & ~3)) + local data_size=$($cmd < $1 | fdtget - /images/$image_name data-size) + $cmd < $1 2>/dev/null | dd skip=$data_position count=$data_size iflag=skip_bytes,count_bytes + return + } +} + +platform_pkgtb_setup_env_config() { + local size=$((0x$(get_hex_u32_le /dev/ubi0_1 4))) + + dd if=/dev/ubi0_1 of=/tmp/env.head count=8 iflag=count_bytes + dd if=/dev/ubi0_1 of=/tmp/env.body skip=8 iflag=skip_bytes + printf "%s\t0x%x\t0x%x\t0x%x" "/tmp/env.body" 0x0 $size $size > /tmp/env.config +} + +platform_pkgtb_get_upgrade_index() { + platform_pkgtb_setup_env_config + + case "$(fw_printenv -l /tmp -n -c /tmp/env.config COMMITTED)" in + 1) echo 2;; + 2) echo 1;; + *) echo 1;; + esac +} + +platform_pkgtb_commit() { + local size=$((0x$(get_hex_u32_le /dev/ubi0_1 4))) + local valid1=0 + local valid2=0 + local seq1 + local seq2 + local tmp + + platform_pkgtb_setup_env_config + + # Read current values + for valid in $(fw_printenv -l /tmp -n -c /tmp/env.config VALID | tr ',' ' '); do + case "$valid" in + 1) valid0=1;; + 2) valid1=2;; + esac + done + seq0=$(fw_printenv -l /tmp -n -c /tmp/env.config SEQ | cut -d ',' -f 1) + seq1=$(fw_printenv -l /tmp -n -c /tmp/env.config SEQ | cut -d ',' -f 2) + + # Calculate values + case "$1" in + 1) valid0=1; seq0=$(((seq1 + 1) % 1000));; + 2) valid1=2; seq1=$(((seq0 + 1) % 1000));; + esac + + # Update variables + fw_setenv -l /tmp -c /tmp/env.config COMMITTED "$1" + fw_setenv -l /tmp -c /tmp/env.config VALID "$valid0,$valid1" + fw_setenv -l /tmp -c /tmp/env.config SEQ "$seq0,$seq1" + + # Write + tmp=$(cat /tmp/env.head /tmp/env.body | wc -c) + cat /tmp/env.head /tmp/env.body | ubiupdatevol /dev/ubi0_1 -s $tmp - +} + +# +# check +# + +platform_check_pkgtb() { + local cmd="${2:-cat}" + + [ -n "$(platform_pkgtb_get_image_name "$1" "$cmd" "bootfs")" -a -n "$(platform_pkgtb_get_image_name "$1" "$cmd" "rootfs")" ] +} + platform_check_image() { [ "$#" -gt 1 ] && return 1 @@ -105,6 +232,13 @@ platform_check_image() { notify_firmware_no_backup } ;; + "pkgtb") + platform_check_pkgtb "$1" || { + echo "Failed to validate pkgtb firmware" >&2 + notify_firmware_broken + return 1 + } + ;; *) case "$BCM4908_FW_INT_IMG_FORMAT" in "bcm4908img") @@ -120,6 +254,13 @@ platform_check_image() { notify_firmware_no_backup } ;; + "pkgtb") + platform_check_pkgtb "$1" "$BCM4908_FW_INT_IMG_EXTRACT_CMD" || { + echo "Failed to validate pkgtb firmware" >&2 + notify_firmware_broken + return 1 + } + ;; esac ;; esac @@ -127,6 +268,36 @@ platform_check_image() { return $error } +# +# upgrade +# + +platform_do_upgrade_pkgtb() { + local cmd="${2:-cat}" + local size + local idx bootfs_id rootfs_id + + idx=$(platform_pkgtb_get_upgrade_index) + case "$idx" in + 1) bootfs_id=3; rootfs_id=4;; + 2) bootfs_id=5; rootfs_id=6;; + esac + + size=$(platform_pkgtb_get_image "$1" "$cmd" "bootfs" | wc -c) + ubirmvol /dev/ubi0 -N bootfs$idx + ubimkvol /dev/ubi0 -n $bootfs_id -N bootfs$idx -t static -s $size + platform_pkgtb_get_image "$1" "$cmd" "bootfs" | ubiupdatevol /dev/ubi0_$bootfs_id -s $size - + + size=$(platform_pkgtb_get_image "$1" "$cmd" "rootfs" | wc -c) + ubirmvol /dev/ubi0 -N rootfs$idx + ubimkvol /dev/ubi0 -n $rootfs_id -N rootfs$idx -t dynamic -s $size + platform_pkgtb_get_image "$1" "$cmd" "rootfs" | ubiupdatevol /dev/ubi0_$rootfs_id -s $size - + + platform_pkgtb_commit $idx + + nand_do_upgrade_success +} + # $1: cferam index increment value platform_calc_new_cferam() { local inc="$1" @@ -223,11 +394,17 @@ platform_do_upgrade() { "bcm4908img") bcm4908img bootfs -i "$1" ls | grep -q "1-openwrt" && platform_do_upgrade_ubi "$1" ;; + "pkgtb") + platform_do_upgrade_pkgtb "$1" + ;; *) case "$BCM4908_FW_INT_IMG_FORMAT" in "bcm4908img") bcm4908img bootfs -i "$1" ls | grep -q "1-openwrt" && platform_do_upgrade_ubi "$1" ;; + "pkgtb") + platform_do_upgrade_pkgtb "$1" "$BCM4908_FW_INT_IMG_EXTRACT_CMD" + ;; *) echo "NAND aware sysupgrade is unsupported for $BCM4908_FW_FORMAT format" ;; @@ -237,6 +414,10 @@ platform_do_upgrade() { # Above calls exit on success. # If we got here it isn't OpenWrt image or something went wrong. + [ "$BCM4908_FW_FORMAT" = "pkgtb" -o "$BCM4908_FW_INT_IMG_FORMAT" = "pkgtb" ] && { + echo "Failed to upgrade pkgtb. Fallback to raw flashing is impossible for this format." >&2 + exit 1 + } echo "Writing whole image to NAND flash. All erase counters will be lost." # Find cferam name for new firmware