#
# Copyright (C) 2024 Teltonika-Networks
#

include $(TOPDIR)/rules.mk

PKG_NAME:=uboot-mt798x

PKG_BUILD_DIR_NAND = $(PKG_BUILD_DIR)-nand
PKG_BUILD_DIR_EMMC = $(PKG_BUILD_DIR)-emmc

PKG_SOURCE_VERSION:=1.16.0

PKG_ASLR_PIE:=0

PKG_LICENSE:=GPL-2.0-or-later

DEVICE_VENDOR='teltonika'

include $(INCLUDE_DIR)/package.mk

define Package/uboot-mt7981
	SECTION:=boot
	CATEGORY:=Boot Loaders
	TITLE:=Uboot for Teltonika MT7981 based boards
	BUILD_SUBTARGET:=mt7981
endef

define Package/uboot-mt7981-emmc
	SECTION:=boot
	CATEGORY:=Boot Loaders
	TITLE:=Uboot for Teltonika MT7981 based boards
	BUILD_SUBTARGET:=mt7981
endef

define Package/uboot-mt7986
	SECTION:=boot
	CATEGORY:=Boot Loaders
	TITLE:=Uboot for Teltonika MT7986 based boards
	BUILD_SUBTARGET:=mt7986a
endef

OVERRIDE_PATH := /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

define uboot_compile
	env -i PATH=$(OVERRIDE_PATH) HOME=$(1) \
		$(MAKE) -C $(1) \
		BUILD_TOPDIR=$(1) \
		CROSS_COMPILE=$(TOOLCHAIN_DIR)/bin/$(TARGET_CROSS) \
		STAGING_DIR=$(1) \
		DEVICE_VENDOR=$(DEVICE_VENDOR)
endef

define Build/Prepare
	$(call Build/Prepare/Default)
endef

ifdef CONFIG_PACKAGE_uboot-mt7981
define Build/Compile/uboot-mt7981
	mkdir -p $(PKG_BUILD_DIR_NAND)
	$(CP) $(PKG_BUILD_DIR)/* $(PKG_BUILD_DIR_NAND)

	$(MAKE) -C $(PKG_BUILD_DIR_NAND) mt7981_tlt_$(call device_shortname)_defconfig
	$(call uboot_compile,$(PKG_BUILD_DIR_NAND))
endef
endif

ifdef CONFIG_PACKAGE_uboot-mt7981-emmc
define Build/Compile/uboot-mt7981-emmc
	mkdir -p $(PKG_BUILD_DIR_EMMC)
	$(CP) $(PKG_BUILD_DIR)/* $(PKG_BUILD_DIR_EMMC)

	$(MAKE) -C $(PKG_BUILD_DIR_EMMC) mt7981_tlt_rutcv2_defconfig
	$(call uboot_compile,$(PKG_BUILD_DIR_EMMC))
endef
endif

ifdef CONFIG_PACKAGE_uboot-mt7986
define Build/Compile/uboot-mt7986
	mkdir -p $(PKG_BUILD_DIR_EMMC)
	$(CP) $(PKG_BUILD_DIR)/* $(PKG_BUILD_DIR_EMMC)

	$(MAKE) -C $(PKG_BUILD_DIR_EMMC) mt7986a_tlt_$(call device_shortname)_defconfig
	$(call uboot_compile,$(PKG_BUILD_DIR_EMMC))
endef
endif

define Build/Compile
	$(call Build/Compile/uboot-mt7981)
	$(call Build/Compile/uboot-mt7981-emmc)
	$(call Build/Compile/uboot-mt7986)
endef

define Package/uboot-mt7981/install
	$(CP) $(PKG_BUILD_DIR_NAND)/u-boot.bin $(BIN_DIR)/u-boot_tlt-mt7981.bin
	$(CP) $(PKG_BUILD_DIR_NAND)/u-boot_webui.bin $(BIN_DIR)/u-boot_tlt-mt7981_webui.bin
	$(CP) $(PKG_BUILD_DIR_NAND)/version $(BIN_DIR)/u-boot_version
endef

define Package/uboot-mt7981-emmc/install
	$(CP) $(PKG_BUILD_DIR_EMMC)/u-boot.bin $(BIN_DIR)/u-boot_tlt-mt7981-emmc.bin
	$(CP) $(PKG_BUILD_DIR_EMMC)/u-boot_webui.bin $(BIN_DIR)/u-boot_tlt-mt7981-emmc_webui.bin
	$(CP) $(PKG_BUILD_DIR_EMMC)/version $(BIN_DIR)/u-boot_version
endef

define Package/uboot-mt7986/install
	$(CP) $(PKG_BUILD_DIR_EMMC)/u-boot.bin $(BIN_DIR)/u-boot_tlt-mt7986a.bin
	$(CP) $(PKG_BUILD_DIR_EMMC)/u-boot_webui.bin $(BIN_DIR)/u-boot_tlt-mt7986a_webui.bin
	$(CP) $(PKG_BUILD_DIR_EMMC)/version $(BIN_DIR)/u-boot_version
endef

$(eval $(call BuildPackage,uboot-mt7981))
$(eval $(call BuildPackage,uboot-mt7981-emmc))
$(eval $(call BuildPackage,uboot-mt7986))
