From 65cccfa20e1ccd4adf1c7c533e2d4614334d6966 Mon Sep 17 00:00:00 2001 From: Tony Ambardar Date: Sun, 1 Oct 2023 21:53:56 -0700 Subject: [PATCH] image: simplify Linksys footer creation and fix alignment Current factory image sizes for Linksys devices are 256-byte aligned, while OEM FW images appear aligned with the 128K erase blocksize. This is not an issue writing factory images from the OpenWrt or Linksys GUIs, but can lead to failures when using a TFTP client from the Linksys bootloader. Simplify Linksys footer creation by migrating to a makefile build recipe, and add footer pre-padding to ensure the proper final image size. Finally, remove the linksys-image.sh script no longer needed. Link: https://github.com/openwrt/openwrt/pull/11405#issuecomment-1358510123 Link: https://github.com/openwrt/openwrt/pull/11405#issuecomment-1587517739 Signed-off-by: Tony Ambardar --- include/image-commands.mk | 25 +++++++++++++-- scripts/linksys-image.sh | 64 --------------------------------------- 2 files changed, 22 insertions(+), 67 deletions(-) delete mode 100755 scripts/linksys-image.sh diff --git a/include/image-commands.mk b/include/image-commands.mk index 77a35f3eec0070..a8b052a3ef116f 100644 --- a/include/image-commands.mk +++ b/include/image-commands.mk @@ -383,11 +383,30 @@ define Build/kernel-bin cp $< $@ endef +# Linksys image footer (256 bytes) is appended to the factory image and tested +# by both the Linksys Upgrader (as observed in civic) and OpenWrt sysupgrade. +# Final file is $BLOCKSIZE aligned to satisfy some devices' TFTP clients, by +# preceding the footer with padding (0xFF). +# Footer format: +# .LINKSYS. Checked by Linksys upgrader before continuing. (9 bytes) +# Upgrade version number, unchecked so arbitrary. (8 bytes) +# Model of device, space padded (0x20). (15 bytes) +# CRC checksum of factory image to flash. (8 bytes) +# Padding ('0' + 0x20 * 7) (8 bytes) +# Signature of signer, unchecked so arbitrary. (16 bytes) +# Padding with nulls (0x00) (192 bytes) define Build/linksys-image - $(TOPDIR)/scripts/linksys-image.sh \ + let \ + size="$$(stat -c%s $@)" \ + pad="$(subst k,* 1024,$(BLOCKSIZE))" \ + offset="256" \ + pad="(pad - ((size + offset) % pad)) % pad"; \ + dd if=/dev/zero bs=$$pad count=1 | tr '\000' '\377' >> $@ + printf ".LINKSYS.01000409%-15s%08X%-8s%-16s" \ "$(call param_get_default,type,$(1),$(DEVICE_NAME))" \ - $@ $@.new - mv $@.new $@ + "$$(cksum $@ | cut -d ' ' -f1)" \ + "0" "K0000000F0246434" >> $@ + dd if=/dev/zero bs=192 count=1 >> $@ endef define Build/lzma diff --git a/scripts/linksys-image.sh b/scripts/linksys-image.sh deleted file mode 100755 index d251b5da8ee84e..00000000000000 --- a/scripts/linksys-image.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/bin/sh -# -# Copyright (C) 2018 Oceanic Systems (UK) Ltd -# -# This is free software, licensed under the GNU General Public License v2. -# See /LICENSE for more information. -# -# Maintained by: Ryan Pannell -# -# Write Linksys signature for factory image -# This is appended to the factory image and is tested by the Linksys Upgrader - as observed in civic. -# The footer is 256 bytes. The format is: -# .LINKSYS. This is detected by the Linksys upgrader before continuing with upgrade. (9 bytes) -# The version number of upgrade. Not checked so use arbitrary value (8 bytes) -# Model of target device, padded (0x20) to (15 bytes) -# CRC checksum of the image to flash (8 byte) -# Padding ('0' + 0x20 *7) (8 bytes) -# Signature of signer. Not checked so use arbitrary value (16 bytes) -# Padding (0x00) (192 bytes) - -## version history -# * version 1: initial commit - -set -e - -ME="${0##*/}" - -usage() { - echo "Usage: $ME " - [ "$IMG_OUT" ] && rm -f "$IMG_OUT" - exit 1 -} - -[ "$#" -lt 3 ] && usage - -TYPE=$1 - -tmpdir="$( mktemp -d 2> /dev/null )" -if [ -z "$tmpdir" ]; then - # try OSX signature - tmpdir="$( mktemp -t 'ubitmp' -d )" -fi - -if [ -z "$tmpdir" ]; then - exit 1 -fi - -trap "rm -rf $tmpdir" EXIT - -IMG_TMP_OUT="${tmpdir}/out" - -IMG_IN=$2 -IMG_OUT="${IMG_IN}.new" - -[ ! -f "$IMG_IN" ] && echo "$ME: Not a valid image: $IMG_IN" && usage - -dd if="${IMG_IN}" of="${IMG_TMP_OUT}" -CRC=$(printf "%08X" $(dd if="${IMG_IN}" bs=$(stat -c%s "${IMG_IN}") count=1|cksum| cut -d ' ' -f1)) - -printf ".LINKSYS.01000409%-15s%-8s%-8s%-16s" "${TYPE}" "${CRC}" "0" "K0000000F0246434" >> "${IMG_TMP_OUT}" - -dd if=/dev/zero bs=1 count=192 conv=notrunc >> "${IMG_TMP_OUT}" - -cp "${IMG_TMP_OUT}" "${IMG_OUT}"