From c2f052acaeb480e89d3ce39c47f49ad16b3464ac Mon Sep 17 00:00:00 2001
From: Ben Whitten <ben.whitten@gmail.com>
Date: Wed, 3 May 2017 22:15:20 +0100
Subject: [PATCH] at91: convert boards to generic build target

Evaluation boards are left in component form to ease flashing
using vendor tooling and instructions. These boards also do
not include the EOF marker in the UBIFS as the bootloaders
are recent and easily upgradeable.

The end product boards use factory.bin images based on the
dts layout and include EOF markers as bootloader UBI support
is not determined.

Signed-off-by: Ben Whitten <ben.whitten@gmail.com>
Signed-off-by: Mathias Kresin <dev@kresin.me>
---
 target/linux/at91/Makefile                    |   2 +
 target/linux/at91/image/Makefile              | 112 +++++++-----------
 target/linux/at91/image/legacy.mk             | 111 +++++++++++++++++
 target/linux/at91/image/sama5d3.mk            |   8 ++
 target/linux/at91/image/ubinize.cfg           |  13 --
 .../linux/at91/legacy/profiles/00-default.mk  |  16 ---
 target/linux/at91/legacy/profiles/atmel.mk    |  86 --------------
 target/linux/at91/legacy/profiles/calamp.mk   |  23 ----
 target/linux/at91/sama5d3/profiles/atmel.mk   |  14 ---
 9 files changed, 163 insertions(+), 222 deletions(-)
 create mode 100644 target/linux/at91/image/legacy.mk
 create mode 100644 target/linux/at91/image/sama5d3.mk
 delete mode 100644 target/linux/at91/image/ubinize.cfg
 delete mode 100644 target/linux/at91/legacy/profiles/00-default.mk
 delete mode 100644 target/linux/at91/legacy/profiles/atmel.mk
 delete mode 100644 target/linux/at91/legacy/profiles/calamp.mk
 delete mode 100644 target/linux/at91/sama5d3/profiles/atmel.mk

diff --git a/target/linux/at91/Makefile b/target/linux/at91/Makefile
index bf62051d72..a3e0be7c8a 100644
--- a/target/linux/at91/Makefile
+++ b/target/linux/at91/Makefile
@@ -17,6 +17,8 @@ KERNEL_PATCHVER:=4.4
 
 include $(INCLUDE_DIR)/target.mk
 
+KERNELNAME:=zImage dtbs
+
 DEFAULT_PACKAGES += kmod-usb-ohci kmod-at91-udc kmod-usb-gadget-eth
 
 $(eval $(call BuildTarget))
diff --git a/target/linux/at91/image/Makefile b/target/linux/at91/image/Makefile
index 0a4c95de7f..d1ae61484a 100644
--- a/target/linux/at91/image/Makefile
+++ b/target/linux/at91/image/Makefile
@@ -7,88 +7,60 @@
 include $(TOPDIR)/rules.mk
 include $(INCLUDE_DIR)/image.mk
 
-UBIFS_OPTS = -F -m 2048 -e 126KiB -c 2048 -U
-UBI_OPTS = -m 2048 -p 128KiB -s 512
+KERNEL_LOADADDR := 0x20008000
 
-AT91SAMA5D3XPLAINED_UBIFS_OPTS = -m 2048 -e 124KiB -c 2048
-AT91SAMA5D3XPLAINED_UBI_OPTS = -m 2048 -p 128KiB -s 2048
+ifeq ($(SUBTARGET),legacy)
+include ./legacy.mk
+UBIFS_OPTS := -m 2048 -e 126KiB -c 2048
+endif
+ifeq ($(SUBTARGET),sama5d3)
+include ./sama5d3.mk
+UBIFS_OPTS := -m 2048 -e 124KiB -c 2048
+endif
 
-define Image/Prepare
-	cp $(LINUX_DIR)/arch/arm/boot/Image $(KDIR)/Image
-	cp $(LINUX_DIR)/arch/arm/boot/zImage $(KDIR)/zImage
+define Device/Default
+  PROFILES := Default
+  FILESYSTEMS := squashfs ubifs
+  DEVICE_DTS := $(1)
+  KERNEL_NAME := zImage
+  KERNEL_SIZE := 4096k
+  KERNEL := kernel-bin | append-dtb | lzma | uImage lzma
+  BLOCKSIZE := 128k
+  PAGESIZE := 2048
+  SUBPAGESIZE := 512
+  DTB_SIZE :=
 endef
 
-define MkuImageDtb
-	cat $(KDIR)/zImage $(DTS_DIR)/$(2).dtb > $(KDIR)/zImage-$(1)
-	mkimage -A arm -T kernel -C none -a 0x20008000 -e 0x20008000 \
-		-n "OpenWrt ARM $(LINUX_VERSION)" \
-		-d $(KDIR)/zImage-$(1) $(BIN_DIR)/$(IMG_PREFIX)-$(1)-uImage
+define Device/dtb
+  KERNEL := kernel-bin | lzma | uImage lzma
 endef
 
-define MkOftree
-	cp -u $(KDIR)/zImage $(BIN_DIR)/$(IMG_PREFIX)-zImage
-	cp $(DTS_DIR)/$(2).dtb $(BIN_DIR)/$(IMG_PREFIX)-$(1)-oftree.dtb
+define Device/evaluation
+  KERNEL_INSTALL := 1
+  KERNEL_SUFFIX := -uImage
+  IMAGES := root.ubi
+  IMAGE/root.ubi := append-ubi
 endef
 
-# Atmel
-Image/Build/Kernel/AT91SAM9263EK=$(call MkuImageDtb,9263ek,at91sam9263ek)
-Image/Build/Kernel/AT91SAM9G15EK=$(call MkuImageDtb,9g15ek,at91sam9g15ek)
-Image/Build/Kernel/AT91SAM9G20EK=$(call MkOftree,9g20ek,at91sam9g20ek)
-Image/Build/Kernel/AT91SAM9G20EK-2MMC=$(call MkOftree,9g20ek_2mmc,at91sam9g20ek_2mmc)
-Image/Build/Kernel/AT91SAM9G25EK=$(call MkuImageDtb,9g25ek,at91sam9g25ek)
-Image/Build/Kernel/AT91SAM9G35EK=$(call MkuImageDtb,9g35ek,at91sam9g35ek)
-Image/Build/Kernel/AT91SAM9M10G45EK=$(call MkuImageDtb,9m10g45ek,at91sam9m10g45ek)
-Image/Build/Kernel/AT91SAM9X25EK=$(call MkuImageDtb,9x25ek,at91sam9x25ek)
-Image/Build/Kernel/AT91SAM9X35EK=$(call MkuImageDtb,9x35ek,at91sam9x35ek)
-Image/Build/Kernel/AT91SAMA5D3XPLAINED=$(call MkuImageDtb,sama5,at91-sama5d3_xplained)
-# CalAmp
-Image/Build/Kernel/LMU5000=$(call MkuImageDtb,lmu5000,lmu5000)
-# Calao
-Image/Build/Kernel/TNYA9260=$(call MkuImageDtb,tny_a9260,tny_a9260)
-Image/Build/Kernel/TNYA9263=$(call MkuImageDtb,tny_a9263,tny_a9263)
-Image/Build/Kernel/TNYA9G20=$(call MkuImageDtb,tny_a9g20,tny_a9g20)
-Image/Build/Kernel/USBA9260=$(call MkuImageDtb,usb_a9260,usb_a9260)
-Image/Build/Kernel/USBA9263=$(call MkuImageDtb,usb_a9263,usb_a9263)
-Image/Build/Kernel/USBA9G20=$(call MkuImageDtb,usb_a9g20,usb_a9g20)
-# Ethernut
-Image/Build/Kernel/ETHERNUT5=$(call MkuImageDtb,ethernut5,ethernut5)
-# Exegin
-Image/Build/Kernel/Q5XR5=$(call MkOftree,q5xr5,at91-q5xr5)
-
-
-define Image/Build/Kernel/Default
-	$(call Image/Build/Kernel/AT91SAM9263EK)
-	$(call Image/Build/Kernel/AT91SAM9G15EK)
-	$(call Image/Build/Kernel/AT91SAM9G20EK)
-	$(call Image/Build/Kernel/AT91SAM9G20EK-2MMC)
-	$(call Image/Build/Kernel/AT91SAM9G25EK)
-	$(call Image/Build/Kernel/AT91SAM9G35EK)
-	$(call Image/Build/Kernel/AT91SAM9M10G45EK)
-	$(call Image/Build/Kernel/AT91SAM9X25EK)
-	$(call Image/Build/Kernel/AT91SAM9X35EK)
-	$(call Image/Build/Kernel/LMU5000)
-	$(call Image/Build/Kernel/TNYA9260)
-	$(call Image/Build/Kernel/TNYA9263)
-	$(call Image/Build/Kernel/TNYA9G20)
-	$(call Image/Build/Kernel/USBA9260)
-	$(call Image/Build/Kernel/USBA9263)
-	$(call Image/Build/Kernel/USBA9G20)
-	$(call Image/Build/Kernel/ETHERNUT5)
-	$(call Image/Build/Kernel/Q5XR5)
+define Device/evaluation-dtb
+  $(Device/evaluation)
+  $(Device/dtb)
+  IMAGES += dtb
+  IMAGE/dtb := install-dtb
 endef
 
-define Image/BuildKernel
-	mkimage -A arm -T kernel -C none -a 0x20008000 -e 0x20008000 -n linux-2.6 \
-		-d $(KDIR)/Image $(BIN_DIR)/$(IMG_PREFIX)-uImage
-	if [ $(CONFIG_FLEXIBITY_ROOT) ]; then \
-		$(INSTALL_BIN) $(BIN_DIR)/$(IMG_PREFIX)-uImage $(TARGET_DIR)/uImage ; \
-	fi
-	$(call Image/Build/Kernel/$(PROFILE))
+define Device/production
+  UBINIZE_OPTS := -E 5
+  IMAGES := factory.bin
+  IMAGE/factory.bin := append-kernel | pad-to $$$$(KERNEL_SIZE) | append-ubi
 endef
 
-define Image/Build
-	$(call Image/Build/$(1))
-	cp $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-root.$(1)
+define Device/production-dtb
+  $(Device/production)
+  $(Device/dtb)
+  DTB_SIZE := 128k
+  IMAGE/factory.bin := append-dtb | pad-to $$$$(DTB_SIZE) \
+	  | append-kernel | pad-to $$$$(KERNEL_SIZE) | append-ubi
 endef
 
 $(eval $(call BuildImage))
diff --git a/target/linux/at91/image/legacy.mk b/target/linux/at91/image/legacy.mk
new file mode 100644
index 0000000000..7fe388135d
--- /dev/null
+++ b/target/linux/at91/image/legacy.mk
@@ -0,0 +1,111 @@
+define Device/at91sam9263ek
+  $(Device/evaluation-dtb)
+  DEVICE_TITLE := Atmel AT91SAM9263-EK
+endef
+TARGET_DEVICES += at91sam9263ek
+
+define Device/at91sam9g15ek
+  $(Device/evaluation)
+  DEVICE_TITLE := Atmel AT91SAM9G15-EK
+endef
+TARGET_DEVICES += at91sam9g15ek
+
+define Device/at91sam9g20ek
+  $(Device/evaluation-dtb)
+  DEVICE_TITLE := Atmel AT91SAM9G20-EK
+endef
+TARGET_DEVICES += at91sam9g20ek
+
+define Device/at91sam9g20ek_2mmc
+  $(Device/evaluation-dtb)
+  DEVICE_TITLE := Atmel AT91SAM9G20-EK 2MMC
+endef
+TARGET_DEVICES += at91sam9g20ek_2mmc
+
+define Device/at91sam9g25ek
+  $(Device/evaluation)
+  DEVICE_TITLE := Atmel AT91SAM9G25-EK
+endef
+TARGET_DEVICES += at91sam9g25ek
+
+define Device/at91sam9g35ek
+  $(Device/evaluation)
+  DEVICE_TITLE := Atmel AT91SAM9G35-EK
+endef
+TARGET_DEVICES += at91sam9g35ek
+
+define Device/at91sam9m10g45ek
+  $(Device/evaluation)
+  DEVICE_TITLE := Atmel AT91SAM9M10G45-EK
+endef
+TARGET_DEVICES += at91sam9m10g45ek
+
+define Device/at91sam9x25ek
+  $(Device/evaluation)
+  DEVICE_TITLE := Atmel AT91SAM9X25-EK
+endef
+TARGET_DEVICES += at91sam9x25ek
+
+define Device/at91sam9x35ek
+  $(Device/evaluation)
+  DEVICE_TITLE := Atmel AT91SAM9X35-EK
+endef
+TARGET_DEVICES += at91sam9x35ek
+
+define Device/lmu5000
+  $(Device/production)
+  DEVICE_TITLE := CalAmp LMU5000
+  DEVICE_PACKAGES := kmod-rtc-pcf2123 kmod-usb-acm kmod-usb-serial \
+    kmod-usb-serial-option kmod-usb-serial-sierrawireless kmod-gpio-mcp23s08
+endef
+TARGET_DEVICES += lmu5000
+
+define Device/tny_a9260
+  $(Device/production-dtb)
+  DEVICE_TITLE := Calao TNYA9260
+endef
+TARGET_DEVICES += tny_a9260
+
+define Device/tny_a9263
+  $(Device/production-dtb)
+  DEVICE_TITLE := Calao TNYA9263
+endef
+TARGET_DEVICES += tny_a9263
+
+define Device/tny_a9g20
+  $(Device/production-dtb)
+  DEVICE_TITLE := Calao TNYA9G20
+endef
+TARGET_DEVICES += tny_a9g20
+
+define Device/usb_a9260
+  $(Device/production-dtb)
+  DEVICE_TITLE := Calao USBA9260
+endef
+TARGET_DEVICES += usb_a9260
+
+define Device/usb_a9263
+  $(Device/production-dtb)
+  DEVICE_TITLE := Calao USBA9263
+endef
+TARGET_DEVICES += usb_a9263
+
+define Device/usb_a9g20
+  $(Device/production-dtb)
+  DEVICE_TITLE := Calao USBA9G20
+endef
+TARGET_DEVICES += usb_a9g20
+
+define Device/ethernut5
+  $(Device/evaluation)
+  DEVICE_TITLE := Ethernut 5
+  UBINIZE_OPTS := -E 5
+endef
+TARGET_DEVICES += ethernut5
+
+define Device/at91-q5xr5
+  $(Device/production-dtb)
+  DEVICE_TITLE := Exegin Q5XR5
+  KERNEL_SIZE := 2048k
+endef
+TARGET_DEVICES += at91-q5xr5
diff --git a/target/linux/at91/image/sama5d3.mk b/target/linux/at91/image/sama5d3.mk
new file mode 100644
index 0000000000..fb02ee1bc0
--- /dev/null
+++ b/target/linux/at91/image/sama5d3.mk
@@ -0,0 +1,8 @@
+define Device/at91-sama5d3_xplained
+  $(Device/evaluation-dtb)
+  DEVICE_TITLE := Atmel AT91SAMA5D3XPLAINED
+  KERNEL_SIZE := 6144k
+  SUBPAGESIZE := 2048
+endef
+TARGET_DEVICES += at91-sama5d3_xplained
+
diff --git a/target/linux/at91/image/ubinize.cfg b/target/linux/at91/image/ubinize.cfg
deleted file mode 100644
index e4149ec7d7..0000000000
--- a/target/linux/at91/image/ubinize.cfg
+++ /dev/null
@@ -1,13 +0,0 @@
-[rootfs]
-# Volume mode (other option is static)
-mode=ubi
-# Source image
-image=root.ubifs
-# Volume ID in UBI image
-vol_id=0
-# Allow for dynamic resize
-vol_type=dynamic
-# Volume name
-vol_name=rootfs
-# Autoresize volume at first mount
-vol_flags=autoresize
diff --git a/target/linux/at91/legacy/profiles/00-default.mk b/target/linux/at91/legacy/profiles/00-default.mk
deleted file mode 100644
index 3ff040d2e4..0000000000
--- a/target/linux/at91/legacy/profiles/00-default.mk
+++ /dev/null
@@ -1,16 +0,0 @@
-#
-# Copyright (C) 2013 OpenWrt.org
-#
-# This is free software, licensed under the GNU General Public License v2.
-# See /LICENSE for more information.
-#
-
-define Profile/Default
-	NAME:=Default Profile
-endef
-
-define Profile/Default/Description
-	Default package set compatible with most boards.
-endef
-
-$(eval $(call Profile,Default))
diff --git a/target/linux/at91/legacy/profiles/atmel.mk b/target/linux/at91/legacy/profiles/atmel.mk
deleted file mode 100644
index 4b14f939ee..0000000000
--- a/target/linux/at91/legacy/profiles/atmel.mk
+++ /dev/null
@@ -1,86 +0,0 @@
-#
-# Copyright (C) 2013 OpenWrt.org
-#
-# This is free software, licensed under the GNU General Public License v2.
-# See /LICENSE for more information.
-#
-
-define Profile/AT91SAM9263EK
-	NAME:=Atmel AT91SAM9263-EK
-endef
-define Profile/AT91SAM9263EK/Description
-	Atmel AT91SAM9263-EK eval board
-endef
-$(eval $(call Profile,AT91SAM9263EK))
-
-
-define Profile/AT91SAM9G15EK
-	NAME:=Atmel AT91SAM9G15-EK
-endef
-define Profile/AT91SAM9G15EK/Description
-	Atmel AT91SAM9G15-EK eval board
-endef
-$(eval $(call Profile,AT91SAM9G15EK))
-
-
-define Profile/AT91SAM9G20EK
-	NAME:=Atmel AT91SAM9G20-EK
-endef
-define Profile/AT91SAM9G20EK/Description
-	Atmel AT91SAM9G20-EK eval board
-endef
-$(eval $(call Profile,AT91SAM9G20EK))
-
-
-define Profile/AT91SAM9G20EK-2MMC
-	NAME:=Atmel AT91SAM9G20-EK 2MMC
-endef
-define Profile/AT91SAM9G20EK-2MMC/Description
-	Atmel AT91SAM9G20-EK eval board with 2 MMC
-endef
-$(eval $(call Profile,AT91SAM9G20EK-2MMC))
-
-
-define Profile/AT91SAM9G25EK
-	NAME:=Atmel AT91SAM9G25-EK
-endef
-define Profile/AT91SAM9G25EK/Description
-	Atmel AT91SAM9G25-EK eval board
-endef
-$(eval $(call Profile,AT91SAM9G25EK))
-
-
-define Profile/AT91SAM9G35EK
-	NAME:=Atmel AT91SAM9G35-EK
-endef
-define Profile/AT91SAM9G35EK/Description
-	Atmel AT91SAM9G35-EK eval board
-endef
-$(eval $(call Profile,AT91SAM9G35EK))
-
-
-define Profile/AT91SAM9M10G45EK
-	NAME:=Atmel AT91SAM9M10G45-EK
-endef
-define Profile/AT91SAM9M10G45EK/Description
-	Atmel AT91SAM9M10G45-EK eval board
-endef
-$(eval $(call Profile,AT91SAM9M10G45EK))
-
-
-define Profile/AT91SAM9X25EK
-	NAME:=Atmel AT91SAM9X25-EK
-endef
-define Profile/AT91SAM9X25EK/Description
-	Atmel AT91SAM9X25-EK eval board
-endef
-$(eval $(call Profile,AT91SAM9X25EK))
-
-
-define Profile/AT91SAM9X35EK
-	NAME:=Atmel AT91SAM9X35-EK
-endef
-define Profile/AT91SAM9X35EK/Description
-	Atmel AT91SAM9X35-EK eval board
-endef
-$(eval $(call Profile,AT91SAM9X35EK))
diff --git a/target/linux/at91/legacy/profiles/calamp.mk b/target/linux/at91/legacy/profiles/calamp.mk
deleted file mode 100644
index 33f78e17ac..0000000000
--- a/target/linux/at91/legacy/profiles/calamp.mk
+++ /dev/null
@@ -1,23 +0,0 @@
-#
-# Copyright (C) 2013 OpenWrt.org
-#
-# This is free software, licensed under the GNU General Public License v2.
-# See /LICENSE for more information.
-#
-
-define Profile/LMU5000
-	NAME:=CalAmp LMU5000
-	PACKAGES:= \
-		kmod-rtc-pcf2123 \
-		kmod-usb-acm \
-		kmod-usb-serial \
-		kmod-usb-serial-option \
-		kmod-usb-serial-sierrawireless \
-		kmod-gpio-mcp23s08
-endef
-
-define Profile/LMU5000/Description
-	CalAmp LMU5000
-endef
-
-$(eval $(call Profile,LMU5000))
diff --git a/target/linux/at91/sama5d3/profiles/atmel.mk b/target/linux/at91/sama5d3/profiles/atmel.mk
deleted file mode 100644
index 8a9b1ef272..0000000000
--- a/target/linux/at91/sama5d3/profiles/atmel.mk
+++ /dev/null
@@ -1,14 +0,0 @@
-#
-# Copyright (C) 2014 OpenWrt.org
-#
-# This is free software, licensed under the GNU General Public License v2.
-# See /LICENSE for more information.
-#
-
-define Profile/AT91SAMA5D3XPLAINED
-	NAME:=Atmel AT91SAMA5D3XPLAINED
-endef
-define Profile/AT91SAMA5D3XPLAINED/Description
-	Atmel AT91SAMA5D3EXPLAINED eval board
-endef
-$(eval $(call Profile,AT91SAMA5D3XPLAINED))
-- 
2.30.2