From 1f6ec4b29ebfda19d9df01f1cc289ed6454683cf Mon Sep 17 00:00:00 2001
From: Michael Pratt <mcpratt@pm.me>
Date: Wed, 24 Feb 2021 22:35:37 -0500
Subject: ath79: lzma-loader: allow setting custom kernel magic

...and max flash offset

The mtdsplit parser was recently refactored
to allow the kernel to have custom image header magic.

Let's also do this for the lzma-loader

For example:
When implemented together,
this allows the kernel to "appear" to be a rootfs
by OEM software in order to write an image
that is actually kernel + rootfs.

At the same time,
it would boot to openwrt normally
by setting the same magic in DTS.

Both of the variables
have a default value that is unchanged
when not defined in the makefiles

This has no effect on the size of the loader
when lzma compressed.

Signed-off-by: Michael Pratt <mcpratt@pm.me>
(cherry picked from commit 1b8bd17c2d07c7397015a86941db9e114e1d349a)
---
 target/linux/ath79/image/Makefile                 | 6 +++++-
 target/linux/ath79/image/lzma-loader/Makefile     | 2 ++
 target/linux/ath79/image/lzma-loader/src/Makefile | 5 +++++
 target/linux/ath79/image/lzma-loader/src/loader.c | 4 ++++
 4 files changed, 16 insertions(+), 1 deletion(-)

(limited to 'target/linux/ath79')

diff --git a/target/linux/ath79/image/Makefile b/target/linux/ath79/image/Makefile
index c2d2474336..d8b8800d91 100644
--- a/target/linux/ath79/image/Makefile
+++ b/target/linux/ath79/image/Makefile
@@ -4,6 +4,7 @@ include $(INCLUDE_DIR)/image.mk
 KERNEL_LOADADDR = 0x80060000
 
 DEVICE_VARS += LOADER_FLASH_OFFS LOADER_TYPE
+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
 
@@ -30,7 +31,10 @@ define Build/loader-kernel
 endef
 
 define Build/loader-okli-compile
-	$(call Build/loader-common,FLASH_OFFS=$(LOADER_FLASH_OFFS) FLASH_MAX=0)
+	$(call Build/loader-common, \
+		FLASH_OFFS=$(LOADER_FLASH_OFFS) \
+		FLASH_MAX=$(LOADER_FLASH_MAX) \
+		KERNEL_MAGIC=$(LOADER_KERNEL_MAGIC) )
 endef
 
 # Arguments: <output name> <kernel offset>
diff --git a/target/linux/ath79/image/lzma-loader/Makefile b/target/linux/ath79/image/lzma-loader/Makefile
index 0ff59bc56a..2b1ed82a98 100644
--- a/target/linux/ath79/image/lzma-loader/Makefile
+++ b/target/linux/ath79/image/lzma-loader/Makefile
@@ -13,6 +13,7 @@ LOADADDR	:= 0x80060000
 LOADER		:= loader.bin
 LOADER_NAME	:= $(basename $(notdir $(LOADER)))
 LOADER_DATA	:=
+KERNEL_MAGIC	:=
 TARGET_DIR	:=
 FLASH_OFFS	:=
 FLASH_MAX	:=
@@ -41,6 +42,7 @@ loader-compile: $(PKG_BUILD_DIR)/.prepared
 		LZMA_TEXT_START=$(LZMA_TEXT_START) \
 		LOADADDR=$(LOADADDR) \
 		LOADER_DATA=$(LOADER_DATA) \
+		KERNEL_MAGIC=$(KERNEL_MAGIC) \
 		FLASH_OFFS=$(FLASH_OFFS) \
 		FLASH_MAX=$(FLASH_MAX) \
 		BOARD="$(BOARD)" \
diff --git a/target/linux/ath79/image/lzma-loader/src/Makefile b/target/linux/ath79/image/lzma-loader/src/Makefile
index 7773f027a2..c7bb935a03 100644
--- a/target/linux/ath79/image/lzma-loader/src/Makefile
+++ b/target/linux/ath79/image/lzma-loader/src/Makefile
@@ -18,6 +18,7 @@
 LOADADDR	:=
 LZMA_TEXT_START	:= 0x80a00000
 LOADER_DATA	:=
+KERNEL_MAGIC	:=
 BOARD		:=
 FLASH_OFFS	:=
 FLASH_MAX	:=
@@ -54,6 +55,10 @@ OBJECTS		+= data.o
 CFLAGS		+= -DLZMA_WRAPPER=1 -DLOADADDR=$(LOADADDR)
 endif
 
+ifneq ($(strip $(KERNEL_MAGIC)),)
+CFLAGS		+= -DCONFIG_KERNEL_MAGIC=$(KERNEL_MAGIC)
+endif
+
 ifneq ($(strip $(KERNEL_CMDLINE)),)
 CFLAGS		+= -DCONFIG_KERNEL_CMDLINE='"$(KERNEL_CMDLINE)"'
 endif
diff --git a/target/linux/ath79/image/lzma-loader/src/loader.c b/target/linux/ath79/image/lzma-loader/src/loader.c
index 794c4b6285..0213e94574 100644
--- a/target/linux/ath79/image/lzma-loader/src/loader.c
+++ b/target/linux/ath79/image/lzma-loader/src/loader.c
@@ -189,7 +189,11 @@ static void lzma_init_data(void)
 
 		p = flash_base + flash_ofs;
 		magic = get_be32(p);
+#ifdef CONFIG_KERNEL_MAGIC
+		if (magic == CONFIG_KERNEL_MAGIC) {
+#else
 		if (magic == IH_MAGIC_OKLI) {
+#endif
 			hdr = (struct image_header *) p;
 			break;
 		}
-- 
cgit v1.2.3