From 17094e9e642ffca19c6b6681766d2bcbb8e6e565 Mon Sep 17 00:00:00 2001
From: Mathias Kresin <dev@kresin.me>
Date: Sun, 2 Oct 2016 11:10:02 +0200
Subject: [PATCH] lantiq: rework VG3503J image

Use the new image build code and remove the lzma loader. The lzma
loader was used to cheat the signature validation of the bootloader and
I found another way to do this.

To migrate boards already using LEDE/OpenWrt to the new image the
following steps need to be done once:

  VR9 # run reset_uboot_config
  VR9 # reset
  VR9 # setenv ethaddr AA:BB:CC:DD:EE:FF
  VR9 # setenv preboot ping 1.1.1.1\;bootm 0xb001f000
  VR9 # saveenv
  VR9 # tftp 0x81000000 lede-lantiq-xrx200-VG3503J-squashfs-sysupgrade.bin
  VR9 # erase 0xb0020000 $(filesize)
  VR9 # cp.b 0x81000000 0xb0020000 $(filesize)

The mac address is printed on the label at the bottom of the case.

The following steps are need to be done during first install:

  VR9 # setenv preboot ping 1.1.1.1\;bootm 0xb001f000
  VR9 # saveenv
  VR9 # tftp 0x81000000 lede-lantiq-xrx200-VG3503J-squashfs-sysupgrade.bin
  VR9 # erase 0xb0020000 $(filesize)
  VR9 # cp.b 0x81000000 0xb0020000 $(filesize)

The image uses the uImage firmware splitter now instead of hardcoded
kernel and rootfs partitions. The firmware partition size was extended
to use flash space that was reserved for partitions required only by
the ECI firmware.

Due to the changes an upgrade to a later LEDE revision from a running
LEDE is supported now.

A default switch config was added and the device uses the same MAC
addresses as the ECI firmware now instead of the same for all VG3503J.

Signed-off-by: Mathias Kresin <dev@kresin.me>
---
 .../lantiq/base-files/etc/board.d/02_network  |  7 +++
 target/linux/lantiq/dts/VG3503J.dts           | 33 ++----------
 target/linux/lantiq/image/Makefile            | 54 ++-----------------
 3 files changed, 15 insertions(+), 79 deletions(-)

diff --git a/target/linux/lantiq/base-files/etc/board.d/02_network b/target/linux/lantiq/base-files/etc/board.d/02_network
index dad4458496..d824c66d1a 100755
--- a/target/linux/lantiq/base-files/etc/board.d/02_network
+++ b/target/linux/lantiq/base-files/etc/board.d/02_network
@@ -120,6 +120,13 @@ TDW8970|TDW8980)
 		"0:lan:2" "2:lan:3" "4:lan:4" "5:lan:1" "6t@eth0"
 	;;
 
+VG3503J)
+	lan_mac=$(mtd_get_mac_ascii uboot-env ethaddr)
+	wan_mac=$(macaddr_add "$lan_mac" 1)
+	ucidef_add_switch "switch0" \
+		"2:lan:1" "4:lan:2" "6t@eth0"
+	;;
+
 VR200v)
 	wan_mac=$(macaddr_add "$(mtd_get_mac_binary romfile 61696)" 1)
 	ucidef_add_switch "switch0" \
diff --git a/target/linux/lantiq/dts/VG3503J.dts b/target/linux/lantiq/dts/VG3503J.dts
index b8437ebd12..5b4f84668c 100644
--- a/target/linux/lantiq/dts/VG3503J.dts
+++ b/target/linux/lantiq/dts/VG3503J.dts
@@ -42,37 +42,12 @@
 					};
 
 					partition@20000 {
-						label = "kernel";
-						reg = <0x20000 0x300000>;
-					};
-
-					partition@1a0000 {
-						label = "rootfs";
-						reg = <0x320000 0x420000>;
-					};
-
-					partition@740000 {
-						label = "btagent";
-						reg = <0x740000 0x80000>;
-					};
-
-					partition@7c0000 {
-						label = "pri_bfocus_cfg";
-						reg = <0x7c0000 0x10000>;
-					};
-
-					partition@7d0000 {
-						label = "sec_bfocus_cfg";
-						reg = <0x7d0000 0x10000>;
-					};
-
-					partition@7e0000 {
-						label = "sysconfig";
-						reg = <0x7e0000 0x10000>;
+						label = "firmware";
+						reg = <0x20000 0x7d0000>;
 					};
 
 					partition@7f0000 {
-						label = "misc_cfg";
+						label = "uboot-env";
 						reg = <0x7f0000 0x10000>;
 					};
 				};
@@ -145,7 +120,7 @@
 		#address-cells = <1>;
 		#size-cells = <0>;
 		reg = <0>;
-		mac-address = [ 00 11 22 33 44 55 ];
+
 		lantiq,switch;
 		ethernet@2 {
 			compatible = "lantiq,xrx200-pdi-port";
diff --git a/target/linux/lantiq/image/Makefile b/target/linux/lantiq/image/Makefile
index 63871e8b3e..ed30093aa9 100644
--- a/target/linux/lantiq/image/Makefile
+++ b/target/linux/lantiq/image/Makefile
@@ -22,17 +22,6 @@ else
   UBIFS_OPTS := -m 2048 -e 126KiB -c 4096
 endif
 
-LOADER_MAKE := $(NO_TRACE_MAKE) -C lzma-loader KDIR=$(KDIR)
-
-define Image/BuildLoader/Template
-	-rm -rf $(KDIR)/lzma-loader
-	$(LOADER_MAKE) LOADER=loader$(2)-$(1).bin\
-		LZMA_TEXT_START=0x80a00000 \
-		LOADADDR=0x80002000 \
-		LOADER_DATA="$(KDIR)/vmlinux$(2)-$(1).lzma" BOARD="$(1)" \
-		compile loader.bin
-endef
-
 define Build/eva-image
 	lzma2eva $(KERNEL_LOADADDR) $(KERNEL_LOADADDR) $@ $@.new
 	( dd if=$@.new bs=64k conv=sync; cat ./eva.dummy.squashfs; ) > $@
@@ -98,12 +87,6 @@ define Image/BuildDGN3500B/squashfs
 endef
 
 
-define Image/BuildLoader/squashfs
-	dd if=$(KDIR)/loader-$(2).bin of=$(KDIR)/loader-$(2).bin.padded bs=3072k conv=sync
-	cat $(KDIR)/loader-$(2).bin.padded $(KDIR)/root.$(1) > $(BIN_DIR)/$(IMG_PREFIX)-$(2)-$(1).image
-	$(call prepare_generic_squashfs,$(BIN_DIR)/$(IMG_PREFIX)-$(2)-$(1).image)
-endef
-
 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
   define Image/BuildKernel/InitramfsTemplate
 	$(call PatchKernelLzma,$(1),-initramfs)
@@ -119,32 +102,6 @@ define Image/BuildKernel/Template
 	$(Image/BuildKernel/InitramfsTemplate)
 endef
 
-ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
-  define Image/BuildKernelLoader/InitramfsTemplate
-	$(CP) $(KDIR)/loader-initramfs-$(1).bin $(BIN_DIR)/$(IMG_PREFIX)-$(1)-vmlinux-initramfs-loader
-	$(CP) $(KDIR)/vmlinux-initramfs-$(1) $(BIN_DIR)/$(IMG_PREFIX)-$(1)-vmlinux-initramfs
-  endef
-endif
-
-define Image/BuildKernelLoader/Template
-	$(CP) $(KDIR)/loader-$(1).bin $(BIN_DIR)/$(IMG_PREFIX)-$(1)-vmlinux-loader
-	$(CP) $(KDIR)/vmlinux-$(1) $(BIN_DIR)/$(IMG_PREFIX)-$(1)-vmlinux
-	$(Image/BuildKernelLoader/InitramfsTemplate)
-endef
-
-ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
-  define Image/Prepare/InitramfsTemplate
-	$(call PatchKernelLzma,$(1),-initramfs)
-	$(call Image/BuildLoader/Template,$(1),-initramfs)
-  endef
-endif
-
-define Image/Prepare/Template
-	$(call PatchKernelLzma,$(1))
-	$(call Image/BuildLoader/Template,$(1))
-	$(Image/Prepare/InitramfsTemplate)
-endef
-
 ### Kernel scripts ###
 define Build/mkbrncmdline
    mkbrncmdline -i $@ -o $@.new BRN-BOOT
@@ -623,14 +580,11 @@ define Device/FRITZ7360SL
 endef
 TARGET_DEVICES += FRITZ7360SL
 
-Image/Prepare/Profile/VG3503J=$(call Image/Prepare/Template,VG3503J)
-Image/BuildKernel/Profile/VG3503J=$(call Image/BuildKernelLoader/Template,VG3503J)
-Image/Build/Profile/VG3503J=$(call Image/BuildLoader/$(1),$(1),VG3503J)
-
-define LegacyDevice/VG3503J
-  DEVICE_TITLE := BT Openreak - ECI VDSL Modem
+define Device/VG3503J
+  IMAGE_SIZE := 8000k
+  DEVICE_TITLE := BT OpenReach - ECI VDSL Modem
 endef
-LEGACY_DEVICES += VG3503J
+TARGET_DEVICES += VG3503J
 
 include tp-link.mk
 
-- 
GitLab