[PATCH v2 2/7] ath79: switch some RedBoot based divices to OKLI loader

Tomasz Maciej Nowak tmn505 at gmail.com
Sun Mar 27 13:05:29 PDT 2022


After the kernel has switched version to 5.10, JA76PF2 and
RouterStations lost the capability to sysupgrade the OpenWrt version.
The cause is the lack of porting the patches responsible for partial
flash erase block writing and these boards FIS directory and RedBoot
config partitions share the same erase block. Because of that the FIS
directory can't be updated to accommodate kernel/rootfs partition size
changes. This could be remedied by bootloader update, but it is very
intrusive and could potentially lead to non-trivial recovery procedure,
if something went wrong. The less difficult option is to use OpenWrt
kernel loader, which will let us use static partition sizes and employ
mtd splitter, to dynamically adjust kernel and rootfs partition sizes.
On sysupgrade from ath79 19.07 or 21.02 image, which still let to modify
FIS directory, the loader will be written to kernel partition, while the
kernel+rootfs to rootfs partition.

The caveats are:
* image format changes, no possible upgrade from ar71xx target images
* downgrade to any older OpenWrt version will require TFTP recovery or
  usage of bootloader command line interface

To downgrade to 19.07 or 21.02, or to upgrade if one is already on
OpenWrt with kernel 5.10, for RouterStations use TFTP recovery
procedure. For JA76PF2 use instructions from this commit message:
0cc87b3bacee (" ath79: image: disable sysupgrade images for
routerstations and ja76pf2"), replacing kernel image with loader
(loader.bin suffix) and rootfs image with firmware (firmware.bin
suffix).

Fixes: 15aa53d7ee65 ("ath79: switch to Kernel 5.10")
Signed-off-by: Tomasz Maciej Nowak <tmn505 at gmail.com>
---
 .../linux/ath79/dts/ar7161_jjplus_ja76pf2.dts | 39 ++++++++++++++++++-
 .../ath79/dts/ar7161_ubnt_routerstation.dtsi  | 35 ++++++++++++++++-
 .../etc/uci-defaults/05_fix-compat-version    |  5 ++-
 .../base-files/lib/upgrade/platform.sh        | 20 +++++-----
 target/linux/ath79/image/Makefile             |  8 ----
 target/linux/ath79/image/generic-ubnt.mk      | 26 ++++++++-----
 target/linux/ath79/image/generic.mk           | 25 ++++++++----
 7 files changed, 120 insertions(+), 38 deletions(-)

diff --git a/target/linux/ath79/dts/ar7161_jjplus_ja76pf2.dts b/target/linux/ath79/dts/ar7161_jjplus_ja76pf2.dts
index 93a029964a63..bcb017c88c54 100644
--- a/target/linux/ath79/dts/ar7161_jjplus_ja76pf2.dts
+++ b/target/linux/ath79/dts/ar7161_jjplus_ja76pf2.dts
@@ -4,6 +4,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
 	model = "jjPlus JA76PF2";
@@ -124,9 +125,45 @@
 		spi-max-frequency = <25000000>;
 
 		partitions {
+			compatible = "fixed-partitions";
 			#address-cells = <1>;
 			#size-cells = <1>;
-			compatible = "ecoscentric,redboot-fis-partitions";
+
+			partition at 0 {
+				label = "RedBoot";
+				reg = <0x000000 0x040000>;
+				read-only;
+			};
+
+			partition at 40000 {
+				label = "loader";
+				reg = <0x040000 0x020000>;
+			};
+
+			partition at 60000 {
+				label = "firmware";
+				reg = <0x060000 0xf80000>;
+				compatible = "openwrt,uimage", "denx,uimage";
+				openwrt,ih-magic = <IH_MAGIC_OKLI>;
+			};
+
+			partition at fe0000 {
+				label = "FIS directory";
+				reg = <0xfe0000 0x00f000>;
+				read-only;
+			};
+
+			partition at fef000 {
+				label = "RedBoot config";
+				reg = <0xfef000 0x001000>;
+				read-only;
+			};
+
+			partition at ff0000 {
+				label = "Atheros Board Data";
+				reg = <0xff0000 0x10000>;
+				read-only;
+			};
 		};
 	};
 };
diff --git a/target/linux/ath79/dts/ar7161_ubnt_routerstation.dtsi b/target/linux/ath79/dts/ar7161_ubnt_routerstation.dtsi
index 116bc9cfc2ff..172b0af1eb3b 100644
--- a/target/linux/ath79/dts/ar7161_ubnt_routerstation.dtsi
+++ b/target/linux/ath79/dts/ar7161_ubnt_routerstation.dtsi
@@ -4,6 +4,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
 	aliases {
@@ -62,7 +63,39 @@
 		spi-max-frequency = <25000000>;
 
 		partitions {
-			compatible = "ecoscentric,redboot-fis-partitions";
+			compatible = "fixed-partitions";
+			#address-cells = <1>;
+			#size-cells = <1>;
+
+			partition at 0 {
+				label = "RedBoot";
+				reg = <0x000000 0x030000>;
+				read-only;
+			};
+
+			partition at 30000 {
+				label = "loader";
+				reg = <0x030000 0x020000>;
+			};
+
+			partition at 50000 {
+				label = "firmware";
+				reg = <0x050000 0xfa0000>;
+				compatible = "openwrt,uimage", "denx,uimage";
+				openwrt,ih-magic = <IH_MAGIC_OKLI>;
+			};
+
+			partition at ff0000 {
+				label = "FIS directory";
+				reg = <0xff0000 0x00f000>;
+				read-only;
+			};
+
+			partition at fff000 {
+				label = "RedBoot config";
+				reg = <0xfff000 0x001000>;
+				read-only;
+			};
 		};
 	};
 };
diff --git a/target/linux/ath79/generic/base-files/etc/uci-defaults/05_fix-compat-version b/target/linux/ath79/generic/base-files/etc/uci-defaults/05_fix-compat-version
index a6b7fa31621b..6646b7e5e7b1 100644
--- a/target/linux/ath79/generic/base-files/etc/uci-defaults/05_fix-compat-version
+++ b/target/linux/ath79/generic/base-files/etc/uci-defaults/05_fix-compat-version
@@ -1,8 +1,11 @@
 . /lib/functions.sh
 
 case "$(board_name)" in
+	jjplus,ja76pf2|\
 	meraki,mr12|\
-	meraki,mr16)
+	meraki,mr16|\
+	ubnt,routerstation|\
+	ubnt,routerstation-pro)
 		uci set system. at system[0].compat_version="2.0"
 		uci commit system
 		;;
diff --git a/target/linux/ath79/generic/base-files/lib/upgrade/platform.sh b/target/linux/ath79/generic/base-files/lib/upgrade/platform.sh
index 953d6a96d36f..642a9891ff29 100644
--- a/target/linux/ath79/generic/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ath79/generic/base-files/lib/upgrade/platform.sh
@@ -14,21 +14,21 @@ redboot_fis_do_upgrade() {
 	local kern_part="$2"
 	local magic=$(get_magic_word "$sysup_file")
 
-	if [ "$magic" = "4349" ]; then
-		local kern_length=0x$(dd if="$sysup_file" bs=2 skip=1 count=4 2>/dev/null)
+	if [ "$magic" = "7379" ]; then
+		local board_dir=$(tar tf $sysup_file | grep -m 1 '^sysupgrade-.*/$')
 
 		[ -f "$UPGRADE_BACKUP" ] && append="-j $UPGRADE_BACKUP"
-		dd if="$sysup_file" bs=64k skip=1 2>/dev/null | \
-			mtd -r $append -F$kern_part:$kern_length:0x80060000,rootfs write - $kern_part:rootfs
 
-	elif [ "$magic" = "7379" ]; then
-		local board_dir=$(tar tf $sysup_file | grep -m 1 '^sysupgrade-.*/$')
-		local kern_length=$(tar xf $sysup_file ${board_dir}kernel -O | wc -c)
+		if grep -q "mtd1.*loader" /proc/mtd; then
+			tar xf $sysup_file ${board_dir}kernel ${board_dir}root -O | \
+				mtd -r $append write - loader:firmware
 
-		[ -f "$UPGRADE_BACKUP" ] && append="-j $UPGRADE_BACKUP"
-		tar xf $sysup_file ${board_dir}kernel ${board_dir}root -O | \
-			mtd -r $append -F$kern_part:$kern_length:0x80060000,rootfs write - $kern_part:rootfs
+		else
+			local kern_length=$(tar xf $sysup_file ${board_dir}kernel -O | wc -c)
 
+			tar xf $sysup_file ${board_dir}kernel ${board_dir}root -O | \
+				mtd -r $append -F$kern_part:$kern_length:0x80060000,rootfs write - $kern_part:rootfs
+		fi
 	else
 		echo "Unknown image, aborting!"
 		return 1
diff --git a/target/linux/ath79/image/Makefile b/target/linux/ath79/image/Makefile
index 768ec5704490..32a4b2abed05 100644
--- a/target/linux/ath79/image/Makefile
+++ b/target/linux/ath79/image/Makefile
@@ -8,14 +8,6 @@ DEVICE_VARS += LOADER_FLASH_MAX LOADER_KERNEL_MAGIC
 DEVICE_VARS += NETGEAR_BOARD_ID NETGEAR_HW_ID
 DEVICE_VARS += RAS_BOARD RAS_ROOTFS_SIZE RAS_VERSION
 
-define Build/combined-image
-	sh $(TOPDIR)/scripts/combined-image.sh \
-		"$(IMAGE_KERNEL)" \
-		"$@" \
-		"$@.new"
-	@mv $@.new $@
-endef
-
 define Build/loader-common
 	rm -rf $@.src
 	$(MAKE) -C lzma-loader \
diff --git a/target/linux/ath79/image/generic-ubnt.mk b/target/linux/ath79/image/generic-ubnt.mk
index 0b613df62b4e..6570d2c170a1 100644
--- a/target/linux/ath79/image/generic-ubnt.mk
+++ b/target/linux/ath79/image/generic-ubnt.mk
@@ -10,7 +10,7 @@ UBNT_REVISION := $(VERSION_DIST)-$(REVISION)
 define Build/mkubntimage
 	-$(STAGING_DIR_HOST)/bin/mkfwimage -B $(UBNT_BOARD) \
 		-v $(UBNT_TYPE).$(UBNT_CHIP).v6.0.0-$(VERSION_DIST)-$(REVISION) \
-		-k $(IMAGE_KERNEL) -r $@ -o $@
+		-k $(if $(1),$(1),$(IMAGE_KERNEL)) -r $@ -o $@
 endef
 
 define Build/mkubntimage2
@@ -386,14 +386,24 @@ define Device/ubnt_routerstation_common
 	kmod-usb2 fconfig
   DEVICE_VENDOR := Ubiquiti
   SOC := ar7161
-  IMAGE_SIZE := 16128k
+  LOADER_TYPE := bin
+  LOADER_FLASH_OFFS := 0x50000
+  COMPILE := loader-$(1).bin
+  COMPILE/loader-$(1).bin := loader-okli-compile | lzma | pad-to 128k
+  IMAGE_SIZE := 16000k
   IMAGES += factory.bin
-  IMAGE/factory.bin := append-rootfs | pad-rootfs | mkubntimage | \
-	check-size
-  IMAGE/sysupgrade.bin := append-rootfs | pad-rootfs | combined-image | \
-	check-size | append-metadata
-  KERNEL := kernel-bin | append-dtb | lzma | pad-to $$(BLOCKSIZE)
+  IMAGE/factory.bin := append-kernel | uImage lzma -M 0x4f4b4c49 | pad-to $$$$(BLOCKSIZE) | \
+	append-rootfs | pad-rootfs | pad-to $$$$(BLOCKSIZE) | \
+	mkubntimage $$$$(KDIR)/loader-$(1).bin | check-size
+  IMAGE/sysupgrade.bin := append-kernel | uImage lzma -M 0x4f4b4c49 | pad-to $$$$(BLOCKSIZE) | \
+	append-rootfs | pad-rootfs | pad-to $$$$(BLOCKSIZE) | check-size | \
+	sysupgrade-tar kernel=$$$$(KDIR)/loader-$(1).bin rootfs=$$$$@ | append-metadata
+  KERNEL := kernel-bin | append-dtb | lzma
   KERNEL_INITRAMFS := kernel-bin | append-dtb
+  DEVICE_COMPAT_VERSION := 2.0
+  DEVICE_COMPAT_MESSAGE := Partition design has changed compared to older versions (19.07 and 21.02) \
+	due to kernel drivers restrictions. Upgrade via sysupgrade mechanism is one way operation. \
+	Downgrading OpenWrt version will involve usage of TFTP recovery or bootloader command line interface.
 endef
 
 define Device/ubnt_routerstation
@@ -403,7 +413,6 @@ define Device/ubnt_routerstation
   UBNT_TYPE := RSx
   UBNT_CHIP := ar7100
   DEVICE_PACKAGES += -swconfig
-  SUPPORTED_DEVICES += routerstation
 endef
 TARGET_DEVICES += ubnt_routerstation
 
@@ -413,7 +422,6 @@ define Device/ubnt_routerstation-pro
   UBNT_BOARD := RSPRO
   UBNT_TYPE := RSPRO
   UBNT_CHIP := ar7100pro
-  SUPPORTED_DEVICES += routerstation-pro
 endef
 TARGET_DEVICES += ubnt_routerstation-pro
 
diff --git a/target/linux/ath79/image/generic.mk b/target/linux/ath79/image/generic.mk
index 9d94c79212cc..24bd12d652b6 100644
--- a/target/linux/ath79/image/generic.mk
+++ b/target/linux/ath79/image/generic.mk
@@ -1453,15 +1453,24 @@ define Device/jjplus_ja76pf2
   DEVICE_VENDOR := jjPlus
   DEVICE_MODEL := JA76PF2
   DEVICE_PACKAGES += -kmod-ath9k -swconfig -wpad-basic-wolfssl -uboot-envtools fconfig kmod-hwmon-lm75
-  IMAGES += kernel.bin rootfs.bin
-  IMAGE/kernel.bin := append-kernel
-  IMAGE/rootfs.bin := append-rootfs | pad-rootfs
-  IMAGE/sysupgrade.bin := append-rootfs | pad-rootfs | combined-image | \
-	check-size | append-metadata
-  KERNEL := kernel-bin | append-dtb | lzma | pad-to $$(BLOCKSIZE)
+  LOADER_TYPE := bin
+  LOADER_FLASH_OFFS := 0x60000
+  COMPILE := loader-$(1).bin
+  COMPILE/loader-$(1).bin := loader-okli-compile | lzma | pad-to 128k
+  ARTIFACTS := loader.bin
+  ARTIFACT/loader.bin := append-loader-okli $(1)
+  IMAGES += firmware.bin
+  IMAGE/firmware.bin := append-kernel | uImage lzma -M 0x4f4b4c49 | pad-to $$$$(BLOCKSIZE) | \
+	append-rootfs | pad-rootfs | pad-to $$$$(BLOCKSIZE) | check-size
+  IMAGE/sysupgrade.bin := $$(IMAGE/firmware.bin) | \
+	sysupgrade-tar kernel=$$$$(KDIR)/loader-$(1).bin rootfs=$$$$@ | append-metadata
+  KERNEL := kernel-bin | append-dtb | lzma
   KERNEL_INITRAMFS := kernel-bin | append-dtb
-  IMAGE_SIZE := 16000k
-  SUPPORTED_DEVICES += ja76pf2
+  IMAGE_SIZE := 15872k
+  DEVICE_COMPAT_VERSION := 2.0
+  DEVICE_COMPAT_MESSAGE := Partition design has changed compared to older versions (19.07 and 21.02) \
+	due to kernel drivers restrictions. Upgrade via sysupgrade mechanism is one way operation. \
+	Downgrading OpenWrt version will involve usage of bootloader command line interface.
 endef
 TARGET_DEVICES += jjplus_ja76pf2
 
-- 
2.35.1




More information about the openwrt-devel mailing list