# SPDX-License-Identifier: GPL-2.0-only

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/image.mk

KERNEL_LOADADDR = 0x80000000
KERNEL_ENTRY = 0x80000400

DEVICE_VARS += UBOOT_SIZE UBOOT_ENV_SIZE CONFIG_SIZE

define Build/append-tlt-uboot
  dd if="$(BIN_DIR)/u-boot_$(word 1,$(DEVICE_BOOT_NAME))$(word 1,$(1)).bin" >> $@
endef

define Build/append-tlt-uboot-env
  dd if=/dev/zero ibs=1 count=$(UBOOT_ENV_SIZE) | tr "\000" "\377" > "$@.pad"
  dd if="$@.pad" >> "$@"
  rm "$@.pad"
endef

define Build/append-tlt-config
  dd if=./bin/tlt-factory-config.bin >> $@
endef

define Device/Default
  PROFILES = Default
  KERNEL := kernel-bin | append-dtb | lzma | uImage lzma
  KERNEL_INITRAMFS := kernel-bin | append-dtb | gzip | uImage gzip
  DEVICE_DTS_DIR := ../dts-$(KERNEL_PATCHVER)
  DEVICE_DTS = $$(SOC)_$(1)
  DEVICE_VENDOR := TELTONIKA
  UBOOT_SIZE := 524288
  UBOOT_ENV_SIZE := 65536
  CONFIG_SIZE := 65536
  IMAGES := sysupgrade.bin \
	$(if $(CONFIG_BUILD_FACTORY_IMAGE),factory.bin)
  IMAGE/sysupgrade.bin := append-kernel | pad-to 64k | append-rootfs | pad-rootfs | \
	check-size | append-metadata | finalize-tlt-webui
  IMAGE/factory.bin := append-tlt-uboot | pad-to $$$$(UBOOT_SIZE) | append-tlt-uboot-env | \
	pad-to $$$$(UBOOT_ENV_SIZE) | append-tlt-config | pad-to $$$$(CONFIG_SIZE) | append-kernel | \
	pad-to 64k | append-rootfs | pad-rootfs | check-size | finalize-tlt-master-stendui
endef

include $(SUBTARGET).mk

$(eval $(call BuildImage))
