include ../../build/teegris_config.mk
include $(CONFIG_SW_TOOLCHAIN_ROOT)/platform.version
PLATFORM_NAME=${TEEGRIS_PLATFORM_VERSION_MAJOR}.${TEEGRIS_PLATFORM_VERSION_MINOR}.${TEEGRIS_PLATFORM_VERSION_REVISION}

$(info [ESECOMM] PLATFORM_NAME $(PLATFORM_NAME))

CONFIG_TA_ARCH=32
ifeq ($(CONFIG_SUPPORT_64ONLY), y)
CONFIG_TA_ARCH=64
else ifeq ($(PLATFORM_NAME), 4.2.1)
CONFIG_TA_ARCH=64
endif

include initialize.mk

SEC_PRODUCT := $(TEEGRIS_BUILD_MODEL)
SEC_SDK := sdk30
ifeq ($(TEEGRIS_VERSION), 4)
SEC_SDK := sdk40
endif

ESECHIP_VENDOR := gem
ifeq ($(ESECOMM_ESE_CHIP_VENDOR), NXP)
ESECHIP_VENDOR := nxp
endif

SUPPORT_ESE_SCP03 := yes
ifneq ($(SPF_ESECOMM_SUPPORT_ESE),)
SUPPORT_ESE_SCP03 := $(SPF_ESECOMM_SUPPORT_ESE)
endif

ESECOMM_SECURITY_PATCH=v1
ifeq ($(ESECOMM_SUPPORT_LEGACY_SECURITY_PATCH), yes)
ESECOMM_SECURITY_PATCH=legacy
endif

SUPPORT_ICCC_V4=no
#To leave a build log
ifeq ($(TEEGRIS_PLATFORM_VERSION_MAJOR), 4)
SUPPORT_ICCC_V4=yes
endif

SUPPORT_LOOPBACK_TEST=yes

#this module
ESE_SRC = $(CURRENT_PATH)/src
ESE_INC = $(CURRENT_PATH)/inc
ESE_OUT = $(CURRENT_PATH)/Out
ESE_ELF = $(ESE_OUT)/ta_ese
DIST = $(ESE_OUT)/00000000-0000-0000-0000-657365636f6d
RESULT_DIR = ./BF_TA_OUT

#This should be enabled only for debugging purpose
ifeq ($(BUILD_MODE), debug)
CFLAGS += -DENABLE_DEBUG_MODE
CFLAGS += -DENABLE_VENDOR_DBG_LOG
endif

CFLAGS += -Werror \
	-Wundef \
	-Wpointer-to-int-cast \
	-Wimplicit-function-declaration \
	-Wno-format

INCLUDE_DIRS += -I. \
	-I$(CURRENT_PATH)/inc \
	-I$(CURRENT_PATH)/inc/public \
	-I$(CURRENT_PATH)/inc/public/msgs \
	-I$(CURRENT_PATH)/tzapps_common/tz_common/public

INCLUDE_DIRS += -I$(CURRENT_PATH)/tz_vendor \
	-I$(CURRENT_PATH)/tz_vendor/TEEGRIS/inc

ESECOM_TZ_VENDOR_SRCS := \
	$(CURRENT_PATH)/tz_vendor/esecomm_tz_vendor_common.c \
	$(CURRENT_PATH)/tz_vendor/TEEGRIS/src/esecomm_tz_vendor_crypto.c \
	$(CURRENT_PATH)/tz_vendor/TEEGRIS/src/TZ_Vendor_tl.c

ESECOMM_ESE_SCP03_SRCS := \
	$(ESE_SRC)/shared/UnwrapSessionKey_tl.c \
	$(ESE_SRC)/shared/WrapSessionKey_tl.c \
	$(ESE_SRC)/shared/esecomm_scp03_helper.c \
	$(ESE_SRC)/shared/TransmitApduV2_tl.c 

SRCS += $(ESE_SRC)/Tl_main_esecomm.c \
	$(ESE_SRC)/shared/example_tl.c \
	$(ESE_SRC)/shared/DeriveCEKey_tl.c \
	$(ESE_SRC)/shared/Finalize_tl.c \
	$(ESE_SRC)/shared/GetODEKey_tl.c \
	$(ESE_SRC)/shared/GetTaProfile_tl.c \
	$(ESE_SRC)/shared/Initialize_tl.c \
	$(ESE_SRC)/shared/SaveODEKey_tl.c \
	$(ESE_SRC)/shared/process_cmd.c \
	$(ESE_SRC)/shared/tl_esecomm_ctx.c \
	$(ESE_SRC)/shared/esecomm_iccc_helper.c \
	$(ESE_SRC)/shared/esecomm_utils.c \
	$(ESE_SRC)/shared/esecomm_crypto.c \
	$(ESE_SRC)/shared/check_device_status.c \
	$(ESECOM_TZ_VENDOR_SRCS)

ifeq ($(SUPPORT_LOOPBACK_TEST), yes)
SRCS += $(ESE_SRC)/shared/LoopbackTest_tl.c
endif

ifeq ($(SUPPORT_ESE_SCP03), yes)
SRCS += $(ESE_SRC)/shared/esecomm_common.c
SRCS += $(ESECOMM_ESE_SCP03_SRCS)
endif

ifeq ($(SUPPORT_UNIT_TEST), yes)
SRCS += $(ESE_SRC)/shared/Unittest_tl.c
endif


#include build configurations including ICCC feature.
include build_common_configuration.mk

#OBJS: Not used
OBJS := $(patsubst %.cxx,%.o,$(patsubst %.c,%.o,$(SRCS)))

#sdk30 by default
STRIP_CMD := $(STRIP) $(STRIPFLAGS) $(ESE_ELF) -o $(ESE_ELF)
#STRIP_CMD :=

all: $(DIST)
	$(call show_make_info)
	$(call copy_to_vendor)

$(DIST): $(ESE_ELF)
	@$(signing_command)

$(ESE_ELF):
	@mkdir -p $(ESE_OUT) 
	@$(CC) $(CFLAGS) $(LDFLAGS) $(SRCS) $(LIB_DIRS) $(LIBS) $(INCLUDE_DIRS) -o $@
	$(STRIP_CMD)

clean:
	rm -f $(OBJS) $(DIST) $(ESE_ELF)
