# Copyright (c) 2014 The Linux Foundation. All rights reserved.
#
#include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/image.mk

UBIFS_OPTS = -m 2048 -e 124KiB -c 4096 -U -F
UBI_OPTS = -m 2048 -p 128KiB

E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024)))

ifeq ($(strip $(call CompareKernelPatchVer,$(KERNEL_PATCHVER),eq,3.18)),1)
IPQ806X_KERNEL_LOADADDR = 0x42008000
IPQ40XX_KERNEL_LOADADDR = 0x80008000
else
IPQ806X_KERNEL_LOADADDR = 0x42208000
IPQ40XX_KERNEL_LOADADDR = 0x80208000
endif
IPQ807X_KERNEL_LOADADDR = 0x41208000
IPQ807X_64_KERNEL_LOADADDR = 0x41080000
IPQ60XX_KERNEL_LOADADDR = 0x41208000
IPQ60XX_64_KERNEL_LOADADDR = 0x41080000

ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
define Image/mkfs/ubifs_fit
	#"===> qsdk\target\linux\ipq\image\Makefile Image/mkfs/ubifs_fit 1"
	$(call Image/ubifs_128MB,$(1))
	$(call Image/ubifs_m4096_p256KiB,$(1))
	$(call Image/ubifs_m4096_p512KiB,$(1))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/mkfs/ubifs_fit 2"
endef
endif

define Image/BuildKernel/FIT
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/FIT 1"
	$(CP) $(KDIR)/Image $(BIN_DIR)/$(IMG_PREFIX)-vmlinux.bin
	gzip -9 -c $(KDIR)/Image > $(KDIR)/Image.gz
	$(call CompressLzma,$(KDIR)/Image,$(KDIR)/Image.gz)
	$(call Image/BuildKernel/MkFIT,$(1), $(KDIR)/Image.gz, $(DTS_DIR)/$(1).dtb,gzip,$(2),$(2))
	$(CP) $(KDIR)/fit-$(1).itb $(BIN_DIR)/$(IMG_PREFIX)-$(1)-fit-uImage.itb
	mkdir -p $(BIN_DIR)/dtbs/
	$(CP) $(DTS_DIR)/qcom-ipq*.dtb $(BIN_DIR)/dtbs/
	$(CP) $(KDIR)/Image $(BIN_DIR)/dtbs/
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/FIT 2"
endef

define Image/BuildKernel/FITInitramfs
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/FITInitramfs 1"
	$(CP) $(KDIR)/Image-initramfs $(BIN_DIR)/dtbs/
	$(CP) $(KDIR)/Image-initramfs $(BIN_DIR)/$(IMG_PREFIX)-vmlinux-initramfs.bin
	$(call Image/BuildKernel/MkFIT,$(1), $(KDIR)/Image-initramfs, $(DTS_DIR)/$(1).dtb, none,$(2),$(2))
	$(CP) $(KDIR)/fit-$(1).itb $(BIN_DIR)/$(IMG_PREFIX)-$(1)-fit-uImage-initramfs.itb
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/FITInitramfs 2"
endef

define Image/Prepare
	#"===> qsdk\target\linux\ipq\image\Makefile Image/Prepare 1"
	$(CP) $(LINUX_DIR)/vmlinux $(KDIR)/$(IMG_PREFIX)-vmlinux.elf
	#"===> qsdk\target\linux\ipq\image\Makefile Image/Prepare 2"
endef

define Image/BuildKernel/GenericFIT
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/GenericFIT 1"
	$(CP) $(KDIR)/Image $(BIN_DIR)/$(IMG_PREFIX)-vmlinux.bin
	gzip -9 -c $(KDIR)/Image > $(KDIR)/Image.gz
	$(call CompressLzma,$(KDIR)/Image,$(KDIR)/Image.gz)
	$(call Image/BuildKernel/MkFIT,$(1), $(KDIR)/Image.gz,,gzip,$(2),$(2))
	$(CP) $(KDIR)/fit-$(1).itb $(BIN_DIR)/$(IMG_PREFIX)-$(1)-fit-uImage.itb
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/GenericFIT 2"
endef

define Image/BuildKernel/MultiDTBFIT
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/MultiDTBFIT 1"
	$(CP) $(KDIR)/Image $(BIN_DIR)/$(IMG_PREFIX)-vmlinux.bin
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/MultiDTBFIT 2"
	gzip -9 -c $(KDIR)/Image > $(KDIR)/Image.gz
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/MultiDTBFIT 3"
	$(foreach dtb, $(2), gzip -9 -c $(DTS_DIR)/$(dtb).dtb > $(DTS_DIR)/$(dtb).dtb.gz;)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/MultiDTBFIT 4"
	$(call CompressLzma,$(KDIR)/Image,$(KDIR)/Image.gz)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/MultiDTBFIT 5"
	$(call Image/BuildKernel/MkFITMulti,$(1),$(KDIR)/Image.gz,$(2),gzip,$(3),$(3),$(4),$(5))
	$(CP) $(KDIR)/fit-$(1).itb $(BIN_DIR)/$(IMG_PREFIX)-$(1)-fit-uImage.itb
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/MultiDTBFIT 6"
endef

# some tree's don't have all the device tree's so we filter out
# the ones that are not present
# $(1) is the filter to apply
#FindDeviceTrees ?= $(notdir $(basename $(shell find $(DTS_DIR)/ -name $(1)*.dtb $(2) 2>/dev/null)))
FindDeviceTrees ?= qcom-ipq807x-db.hk02 qcom-ipq807x-ac03

define Image/BuildKernel/template
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/template 1"
	$(CP) $(KDIR)/vmlinux$(subst openwrt-ipq$(if $(SUBTARGET),-$(SUBTARGET)),,$(IMG_PREFIX)).debug $(BIN_DIR)/$(IMG_PREFIX)-vmlinux.elf
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/template 2"
endef

define Image/BuildKernel/ipq806x
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq806x 1"
	$(foreach device, $(call FindDeviceTrees, qcom-ipq8064-), \
		$(call Image/BuildKernel/$(1),$(device),$(IPQ806X_KERNEL_LOADADDR));)

	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq806x-akxx, \
		$(call FindDeviceTrees, qcom-ipq806?-), \
		$(IPQ806X_KERNEL_LOADADDR),gzip,0x43F00000)
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-ap148_1xx, \
		$(call FindDeviceTrees, qcom-ipq8064-*ap148_1xx), \
		$(IPQ806X_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-ap148, \
		$(call FindDeviceTrees, qcom-ipq8064-*ap148,-not -name *ap148_1xx* -not -name *ap148-nand*), \
		$(IPQ806X_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-db149, \
		$(call FindDeviceTrees, qcom-ipq8064-*db149), \
		$(IPQ806X_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-ak01, \
		$(call FindDeviceTrees, qcom-ipq8064-*ak01), \
		$(IPQ806X_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-ap145_1xx, \
		$(call FindDeviceTrees, qcom-ipq8064-*ap145_1xx), \
		$(IPQ806X_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-ap145, \
		$(call FindDeviceTrees, qcom-ipq8064-*ap145,-not -name *ap145_1xx*), \
		$(IPQ806X_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-ap160_2xx, \
		$(call FindDeviceTrees, qcom-ipq8064-*ap160_2xx), \
		$(IPQ806X_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-ap160, \
		$(call FindDeviceTrees, qcom-ipq8064-*ap160,-not -name *ap160_2xx*), \
		$(IPQ806X_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq8064-ap161, \
		$(call FindDeviceTrees, qcom-ipq8064-*ap161), \
		$(IPQ806X_KERNEL_LOADADDR))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq806x 2"
endef

define Image/BuildKernel/ipq40xx
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq40xx 1"
	$(foreach device, $(call FindDeviceTrees, qcom-ipq40??-), \
		$(call Image/BuildKernel/$(1),$(device),$(IPQ40XX_KERNEL_LOADADDR));)

	$(call Image/BuildKernel/GenericFIT,qcom-ipq40xx,$(IPQ40XX_KERNEL_LOADADDR))
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq40xx-ap.dkxx, \
		$(call FindDeviceTrees, qcom-ipq40??-ap) $(call FindDeviceTrees, qcom-ipq40??-db), \
		$(IPQ40XX_KERNEL_LOADADDR))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq40xx 2"
endef

define Image/BuildKernel/ipq807x
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq807x 1"
	$(foreach device, $(call FindDeviceTrees, qcom-ipq807x-), \
		$(call Image/BuildKernel/$(1),$(device),$(IPQ807X_KERNEL_LOADADDR));)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq807x 2"

	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq807x 3"
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq807x-hkxx, \
		$(call FindDeviceTrees, qcom-ipq807x-), $(IPQ807X_KERNEL_LOADADDR))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq807x 4"
endef

define Image/BuildKernel/ipq807x_64
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq807x_64 1"
	$(foreach device, $(call FindDeviceTrees, qcom-ipq807x-), \
		$(call Image/BuildKernel/$(1),$(device),$(IPQ807X_64_KERNEL_LOADADDR));)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq807x_64 2"

	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq807x_64 3"
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq807x-hkxx, \
		$(call FindDeviceTrees, qcom-ipq807x-), $(IPQ807X_64_KERNEL_LOADADDR))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq807x_64 4"
endef

define Image/BuildKernel/ipq60xx
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq60xx 1"
	$(foreach device, $(call FindDeviceTrees, qcom-ipq6018-), \
		$(call Image/BuildKernel/$(1),$(device),$(IPQ60XX_KERNEL_LOADADDR));)

	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq60xx-cpxx, \
		$(call FindDeviceTrees, qcom-ipq6018-), $(IPQ60XX_KERNEL_LOADADDR))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq60xx 2"
endef

define Image/BuildKernel/ipq60xx_64
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq60xx_64 1"
	$(foreach device, $(call FindDeviceTrees, qcom-ipq6018-), \
		$(call Image/BuildKernel/$(1),$(device),$(IPQ60XX_64_KERNEL_LOADADDR));)
	$(call Image/BuildKernel/MultiDTBFIT,qcom-ipq60xx-cpxx, \
		$(call FindDeviceTrees, qcom-ipq6018-), $(IPQ60XX_64_KERNEL_LOADADDR))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/ipq60xx_64 2"
endef

define Image/BuildKernel
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel 1"
	$(call Image/BuildKernel/template,FIT)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel 2"
	$(call Image/BuildKernel/$(SUBTARGET),FIT)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel 3"
endef

define Image/BuildKernel/Initramfs
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/Initramfs 1"
	$(CP) $(KDIR)/vmlinux-initramfs$(subst openwrt-ipq$(if $(SUBTARGET),-$(SUBTARGET)),,$(IMG_PREFIX)).debug $(BIN_DIR)/$(IMG_PREFIX)-vmlinux-initramfs.elf
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/Initramfs 2"
	$(call Image/BuildKernel/template,FITInitramfs)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/Initramfs 3"
	$(call Image/BuildKernel/$(SUBTARGET),FITInitramfs)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/Initramfs 4"
endef

define Image/ubifs_m4096_p512KiB
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/Initramfs 1"
  #ifeq ($(CONFIG_TARGET_ipq_ipq806x),)
	$(eval UBI_OPTS:="-m 4096 -p 512KiB")
	$(call Image/mkfs/ubifs/generate,$(1))
	$(call Image/Build,ubi,$(1),-m4096-p512KiB)
  #endif
	#"===> qsdk\target\linux\ipq\image\Makefile Image/BuildKernel/Initramfs 2"
endef

define Image/ubifs_m4096_p256KiB
	#"===> qsdk\target\linux\ipq\image\Makefile Image/ubifs_m4096_p256KiB 1"
  #ifeq ($(CONFIG_TARGET_ipq_ipq806x),)
	$(eval UBI_OPTS:="-m 4096 -p 256KiB")
	$(call Image/mkfs/ubifs/generate,$(1))
	$(call Image/Build,ubi,$(1),-m4096-p256KiB)
  #endif
	#"===> qsdk\target\linux\ipq\image\Makefile Image/ubifs_m4096_p256KiB 2"
endef

define Image/ubifs_128MB
	#"===> qsdk\target\linux\ipq\image\Makefile Image/ubifs_128MB 1"
	$(eval UBI_OPTS:="-m 2048 -p 128KiB")
	#"===> qsdk\target\linux\ipq\image\Makefile Image/ubifs_128MB 2"
	$(call Image/mkfs/ubifs/generate,$(1))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/ubifs_128MB 3"
	$(call Image/Build,ubi,$(1))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/ubifs_128MB 4"
endef

define Image/Build/ubifs
	#"===> qsdk\target\linux\ipq\image\Makefile Image/Build/ubifs 1"
	$(call Image/mkfs/ubifs_fit,-$(SUBTARGET))
	#"===> qsdk\target\linux\ipq\image\Makefile Image/Build/ubifs 2"
endef

define Image/Build/squashfs
	#"===> qsdk\target\linux\ipq\image\Makefile Image/Build/squashfs 1"
	$(call prepare_generic_squashfs,$(KDIR)/root.squashfs)
	#"===> qsdk\target\linux\ipq\image\Makefile Image/Build/squashfs 2"
endef

define Image/Build
	#"===> qsdk\target\linux\ipq\image\Makefile Image/Build/squashfs 1"
	$(call Image/Build/$(1),$(1))
	dd if=$(KDIR)/root$(2).$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-$(1)-root$(3).img bs=2k conv=sync
	#"===> qsdk\target\linux\ipq\image\Makefile Image/Build/squashfs 2"
endef

#$(eval $(call BuildImage))
