ifeq ($(DEVICE),AM263x)
	MCU_PLUS_SDK_PATH= C:/ti/mcu_plus_sdk_am263x_08_04_00_17
	include $(MCU_PLUS_SDK_PATH)/imports.mak
else ifeq ($(DEVICE),AM273x)
	MCU_PLUS_SDK_PATH= C:/ti/mcu_plus_sdk_am273x_08_04_00_17
	include $(MCU_PLUS_SDK_PATH)/imports.mak
else
	$(error Incorrect DEVICE name chosen)
endif

CG_TOOL_ROOT= C:/ti/ti-cgt-armllvm_1.3.0.LTS

STRIP=$(CG_TOOL_ROOT)/bin/tiarmstrip
OBJCOPY=$(CG_TOOL_ROOT)/bin/tiarmobjcopy

ifeq ($(OS), Windows_NT)
	PYTHON=python
else
	PYTHON=python3
endif

OUTFILE=$(OUTNAME).$(EXTN)
BOOTIMAGE_NAME:=$(OUTNAME).appimage
BOOTIMAGE_NAME_XIP:=$(OUTNAME).appimage_xip
BOOTIMAGE_NAME_SIGNED:=$(OUTNAME).appimage.signed
BOOTIMAGE_RPRC_NAME:=$(OUTNAME)_final.rprc
BOOTIMAGE_RPRC_NAME_XIP:=$(OUTNAME).rprc_xip
BOOTIMAGE_RPRC_NAME_TMP:=$(OUTNAME).rprc_tmp

#
# Generation of boot image which can be loaded by Secondary Boot Loader (SBL)
#
ifeq ($(OS),Windows_NT)
EXE_EXT=.exe
endif
ifeq ($(OS),Windows_NT)
  BOOTIMAGE_CERT_GEN_CMD=powershell -executionpolicy unrestricted -command $(MCU_PLUS_SDK_PATH)/tools/boot/signing/x509CertificateGen.ps1
else
  BOOTIMAGE_CERT_GEN_CMD=$(MCU_PLUS_SDK_PATH)/tools/boot/signing/x509CertificateGen.sh
endif
BOOTIMAGE_TEMP_OUT_FILE=temp.txt

ifeq ($(DEVICE),AM263x)
	BOOTIMAGE_CORE_ID_r5fss0-0 = 0
	BOOTIMAGE_CORE_ID_r5fss0-1 = 1
	BOOTIMAGE_CORE_ID_r5fss1-0 = 2
	BOOTIMAGE_CORE_ID_r5fss1-1 = 3
	SBL_RUN_ADDRESS=0x70002000
	FLASH_ADDR = 0x60000000
	SBL_DEV_ID=55
else ifeq ($(DEVICE),AM273x)
	BOOTIMAGE_CORE_ID_r5fss0-0 = 0
	BOOTIMAGE_CORE_ID_r5fss0-1 = 1
	BOOTIMAGE_CORE_ID_c66ss0   = 2
	SBL_RUN_ADDRESS=0x10200000
	FLASH_ADDR = 0xc6000000
	SBL_DEV_ID=55
else
	$(error Incorrect DEVICE name chosen)
endif

MULTI_CORE_IMAGE_GEN = $(CCS_NODE) $(MCU_PLUS_SDK_PATH)/tools/boot/multicoreImageGen/multicoreImageGen.js
OUTRPRC_CMD = $(CCS_NODE) $(MCU_PLUS_SDK_PATH)/tools/boot/out2rprc/elf2rprc.js

ifeq ($(OS),Windows_NT)
  XIPGEN_CMD=$(MCU_PLUS_SDK_PATH)/tools/boot/xipGen/xipGen.exe
else
  XIPGEN_CMD=$(MCU_PLUS_SDK_PATH)/tools/boot/xipGen/xipGen.out
endif

ifeq ($(DEVICE),AM263x)
	MULTI_CORE_IMAGE_PARAMS = \
		$(BOOTIMAGE_RPRC_NAME)@$(BOOTIMAGE_CORE_ID_r5fss0-0) \
		$(if $(filter-out undefined,$(R50_1_RPRC)),$(R50_1_RPRC)@$(BOOTIMAGE_CORE_ID_r5fss0-1)) \
		$(if $(filter-out undefined,$(R51_0_RPRC)),$(R51_0_RPRC)@$(BOOTIMAGE_CORE_ID_r5fss1-0)) \
		$(if $(filter-out undefined,$(R51_1_RPRC)),$(R51_1_RPRC)@$(BOOTIMAGE_CORE_ID_r5fss1-1))
else ifeq ($(DEVICE),AM273x)
	MULTI_CORE_IMAGE_PARAMS = \
		$(BOOTIMAGE_RPRC_NAME)@$(BOOTIMAGE_CORE_ID_r5fss0-0) \
		$(if $(filter-out undefined,$(R50_1_RPRC)),$(R50_1_RPRC)@$(BOOTIMAGE_CORE_ID_r5fss0-1)) \
		$(if $(filter-out undefined,$(C66x_RPRC)),$(C66x_RPRC)@$(BOOTIMAGE_CORE_ID_c66ss0-1))
else
	$(error Incorrect DEVICE name chosen)
endif

MULTI_CORE_IMAGE_PARAMS_XIP = \
	$(BOOTIMAGE_RPRC_NAME_XIP)@$(BOOTIMAGE_CORE_ID_r5fss0-0)  \

all:
	@echo  Starting Boot image generation: $(BOOTIMAGE_NAME)  ...
	$(OUTRPRC_CMD) $(OUTFILE) >> $(BOOTIMAGE_TEMP_OUT_FILE)
	$(COPY) $(OUTNAME).rprc $(BOOTIMAGE_RPRC_NAME)
	$(COPY) $(BOOTIMAGE_RPRC_NAME) $(BOOTIMAGE_RPRC_NAME_TMP)
	$(RM) $(BOOTIMAGE_RPRC_NAME)
	$(XIPGEN_CMD) -i $(BOOTIMAGE_RPRC_NAME_TMP) -o $(BOOTIMAGE_RPRC_NAME) -x $(BOOTIMAGE_RPRC_NAME_XIP) --flash-start-addr $(FLASH_ADDR) -v > $(BOOTIMAGE_TEMP_OUT_FILE)
	@echo  Generated XIP RPRC
	$(MULTI_CORE_IMAGE_GEN) --devID $(SBL_DEV_ID) --out $(BOOTIMAGE_NAME) $(MULTI_CORE_IMAGE_PARAMS) >> $(BOOTIMAGE_TEMP_OUT_FILE)
	@echo  Generated XIP Appimage
	$(MULTI_CORE_IMAGE_GEN) --devID $(SBL_DEV_ID) --out $(BOOTIMAGE_NAME_XIP) $(MULTI_CORE_IMAGE_PARAMS_XIP) >> $(BOOTIMAGE_TEMP_OUT_FILE)
	@echo  Cleaning Temp files
	$(RM) $(BOOTIMAGE_RPRC_NAME_TMP)
	$(RM) $(BOOTIMAGE_RPRC_NAME)
	@echo  Multicore Boot image generation for: $(BOOTIMAGE_NAME) Done !!!

clean:
	$(RM) $(filter-out %.out,$(wildcard $(OUTNAME).*))
	$(RM) $(BOOTIMAGE_TEMP_OUT_FILE)
	@echo Cleaning Done.
