From 5260c1003cee20b004f4628c72ef3985885ca901 Mon Sep 17 00:00:00 2001
From: Mathias Kresin <dev@kresin.me>
Date: Wed, 4 Jul 2018 06:39:05 +0200
Subject: [PATCH] ar71xx: mikrotik: cleanup nand image build code

Use the LOADER_TYPE variable to specify that we need the elf preloader
and append the loader via the corresponding build recipe. It allows to
enable initramfs images again for mikrotik NAND images, which caused a
build error before.

Add the minor header only to the kernel of the sysupgrade images, as it
is only required for the bootloader to find the kernel on flash.

Signed-off-by: Mathias Kresin <dev@kresin.me>
---
 target/linux/ar71xx/image/mikrotik.mk | 34 ++++++++++++++++-----------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/target/linux/ar71xx/image/mikrotik.mk b/target/linux/ar71xx/image/mikrotik.mk
index a0a7b7a2dd..3238a885a8 100644
--- a/target/linux/ar71xx/image/mikrotik.mk
+++ b/target/linux/ar71xx/image/mikrotik.mk
@@ -2,44 +2,50 @@ define Device/mikrotik
   PROFILES := Default
   DEVICE_PACKAGES := kmod-usb-core kmod-usb-ohci kmod-usb2 kmod-usb-ledtrig-usbport
   BOARD_NAME := routerboard
-  KERNEL_INITRAMFS :=
-  KERNEL_NAME := loader-generic.elf
-  KERNEL := kernel-bin | kernel2minor -s 2048 -e -c
-  FILESYSTEMS := squashfs
-  IMAGES := sysupgrade.bin
-  IMAGE/sysupgrade.bin := sysupgrade-tar
+  LOADER_TYPE := elf
+  KERNEL_NAME := vmlinux.bin.lzma
+  KERNEL := kernel-bin | loader-kernel
+  KERNEL_INITRAMFS_NAME := vmlinux-initramfs.bin.lzma
+  MIKROTIK_CHUNKSIZE :=
+  IMAGE/sysupgrade.bin/squashfs :=
+endef
+DEVICE_VARS += MIKROTIK_CHUNKSIZE
+
+define Device/mikrotik-nand
+  $(Device/mikrotik)
+  IMAGE/sysupgrade.bin/squashfs = append-kernel | \
+	kernel2minor -s $$(MIKROTIK_CHUNKSIZE) -e -c | sysupgrade-tar kernel=$$$$@
 endef
 
 define Device/nand-64m
-  $(Device/mikrotik)
+  $(Device/mikrotik-nand)
+  MIKROTIK_CHUNKSIZE := 512
   DEVICE_TITLE := MikroTik RouterBoard (64 MB NAND)
-  KERNEL := kernel-bin | kernel2minor -s 512 -e -c
 endef
 TARGET_DEVICES += nand-64m
 
 define Device/nand-large
-  $(Device/mikrotik)
+  $(Device/mikrotik-nand)
+  MIKROTIK_CHUNKSIZE := 2048
   DEVICE_TITLE := MikroTik RouterBoard (>= 128 MB NAND)
-  KERNEL := kernel-bin | kernel2minor -s 2048 -e -c
 endef
 TARGET_DEVICES += nand-large
 
 define Device/nand-large-ac
-  $(Device/mikrotik)
+  $(Device/mikrotik-nand)
+  MIKROTIK_CHUNKSIZE := 2048
   DEVICE_TITLE := MikroTik RouterBoard (>= 128 MB NAND, 802.11ac)
   DEVICE_PACKAGES += kmod-ath10k ath10k-firmware-qca988x
-  KERNEL := kernel-bin | kernel2minor -s 2048 -e -c
   SUPPORTED_DEVICES := rb-921gs-5hpacd-r2
 endef
 TARGET_DEVICES += nand-large-ac
 
 define Device/rb-nor-flash-16M
+  $(Device/mikrotik)
   DEVICE_TITLE := MikroTik RouterBoard (16 MB SPI NOR)
   DEVICE_PACKAGES := rbcfg rssileds -nand-utils kmod-ledtrig-gpio
   IMAGE_SIZE := 16000k
-  LOADER_TYPE := elf
   KERNEL_INSTALL := 1
-  KERNEL := kernel-bin | lzma | loader-kernel
   SUPPORTED_DEVICES := rb-750-r2 rb-750up-r2 rb-750p-pbr2 rb-911-2hn rb-911-5hn rb-941-2nd rb-951ui-2nd rb-952ui-5ac2nd rb-962uigs-5hact2hnt rb-lhg-5nd rb-map-2nd rb-mapl-2nd rb-wap-2nd rb-wapr-2nd
   IMAGE/sysupgrade.bin := append-kernel | kernel2minor -s 1024 -e | pad-to $$$$(BLOCKSIZE) | \
 	append-rootfs | pad-rootfs | append-metadata | check-size $$$$(IMAGE_SIZE)