From 9f261e36de799322512f36a54689260e617c4fba Mon Sep 17 00:00:00 2001
From: "Leon M. George" <leon@georgemail.eu>
Date: Thu, 25 Jul 2019 11:59:56 +0200
Subject: [PATCH] ath79: build Compex WPJ531 cpximg files

Generate additional images that are compatible to the cpximg loader.
The cpximg loader can be started either by holding the reset button during
power up or by entering the u-boot prompt and entering 'cpximg'.
Once it's running, a TFTP-server under 192.168.1.1 will accept the image
appropriate for the board revision that is etched on the board.

For example, if the board is labelled '7A04':

tftp -v -m binary 192.168.1.1 -c put openwrt-ath79-generic-compex_wpj531-16m-squashfs-cpximg-7A04.bin

These files can also be used with the sysupgrade utility in stock images (add
SSH key in luci for root access).

Signed-off-by: Leon M. George <leon@georgemail.eu>
[fix sorting of definitions]
Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
---
 target/linux/ath79/image/generic.mk | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/target/linux/ath79/image/generic.mk b/target/linux/ath79/image/generic.mk
index 35def34250..f99fcdeda2 100644
--- a/target/linux/ath79/image/generic.mk
+++ b/target/linux/ath79/image/generic.mk
@@ -50,6 +50,29 @@ define Build/cybertan-trx
 	-rm $@-empty.bin
 endef
 
+define Build/mkmylofw_16m
+	$(eval device_id=$(word 1,$(1)))
+	$(eval revision=$(word 2,$(1)))
+
+	# On WPJ344, WPJ531, and WPJ563, the default boot command tries 0x9f680000
+	# first and fails if the remains of the stock image are sill there
+	# - resulting in an infinite boot loop.
+	# The size parameter is grown to have that block deleted if the firmware
+	# isn't big enough by itself.
+
+	let \
+		size="$$(stat -c%s $@)" \
+		pad="$(subst k,* 1024,$(BLOCKSIZE))" \
+		pad="(pad - (size % pad)) % pad" \
+		newsize='size + pad' ; \
+		[ $$newsize -lt $$((0x660000)) ] && newsize=0x660000 ; \
+		$(STAGING_DIR_HOST)/bin/mkmylofw \
+			-B WPE72 -i 0x11f6:$(device_id):0x11f6:$(device_id) -r $(revision) \
+			-s 0x1000000 -p0x30000:$$newsize:al:0x80060000:"OpenWRT":$@ \
+			$@.new
+		@mv $@.new $@
+endef
+
 define Build/mkwrggimg
 	$(STAGING_DIR_HOST)/bin/mkwrggimg -b \
 		-i $@ -o $@.imghdr -d /dev/mtdblock/1 \
@@ -426,6 +449,11 @@ define Device/compex_wpj531-16m
   DEVICE_MODEL := WPJ531
   DEVICE_VARIANT := 16M
   SUPPORTED_DEVICES += wpj531
+  IMAGES += cpximg-7a03.bin cpximg-7a04.bin cpximg-7a06.bin cpximg-7a07.bin
+  IMAGE/cpximg-7a03.bin := append-kernel | pad-to $$$$(BLOCKSIZE) | append-rootfs | pad-rootfs | mkmylofw_16m 0x68a 2
+  IMAGE/cpximg-7a04.bin := append-kernel | pad-to $$$$(BLOCKSIZE) | append-rootfs | pad-rootfs | mkmylofw_16m 0x693 3
+  IMAGE/cpximg-7a06.bin := append-kernel | pad-to $$$$(BLOCKSIZE) | append-rootfs | pad-rootfs | mkmylofw_16m 0x693 3
+  IMAGE/cpximg-7a07.bin := append-kernel | pad-to $$$$(BLOCKSIZE) | append-rootfs | pad-rootfs | mkmylofw_16m 0x693 3
 endef
 TARGET_DEVICES += compex_wpj531-16m
 
-- 
2.30.2