#!/bin/bash -x
#trustedcore packager
#created by h00206996

COMPARE_IMAGE=0
KERNEL_TEXT_BASE=$1
echo "kernel text base=${KERNEL_TEXT_BASE}"
PLATFORM_CHOOSE=$2
echo "platform is ${PLATFORM_CHOOSE}"

RELEASE_OUT_PATH=$3

SUPPORT_TEST_TA=$4
SUPPORT_SEC_MMZ=$5
SUPPORT_VFMW=$6
SUPPORT_SMMU=$7
SUPPORT_HISI_TEST=$8
SUPPORT_DMX=$9
SUPPORT_PVR=${10}
SUPPORT_HDMI=${11}
SUPPORT_WIDEVINE=${12}
SUPPORT_HISI_DEMO=${13}
SUPPORT_HISI_VDP=${14}
SUPPORT_VMX_ULTRA=${15}
SUPPORT_VMX_ULTRA_VMXTAC_TEST=${16}
SUPPORT_VMX_ULTRA_ITAC_TEST=${17}
SUPPORT_VPSS=${18}
SUPPORT_HISI_DRM=${19}

IMG_PATH=${RELEASE_OUT_PATH}
KERNEL_NAME=rtosck.img
KERNEL_PATH=${IMG_PATH}/${KERNEL_NAME}
DST_PATH=${IMG_PATH}/trustedcore.img
CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/../../.. && pwd )"
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"


TASK_GLOBAL_NAME=globaltask.img
TASK_GLOBAL_UUID="00000000000000000000000000000000"
TASK_GLOBAL_PATH=${IMG_PATH}/${TASK_GLOBAL_NAME}

TASK_STORAGE_NAME=task_storage.elf
TASK_STORAGE_UUID="02020202020202020202020202020202"
TASK_STORAGE_PATH=${CURDIR}/release/internal_tasks/${TASK_STORAGE_NAME}

TASK_REET_NAME=task_reet.elf
TASK_REET_UUID="0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A0A"
TASK_REET_PATH=${CURDIR}/release/internal_tasks/${TASK_REET_NAME}

TASK16_NAME=task_ssa.elf
TASK16_UUID="999286b954da42359e7796e81fea1ee4"
TASK16_PATH=${CURDIR}/release/internal_tasks/${TASK16_NAME}

#Add new internal task here:
TASK_LIST="${TASK_GLOBAL_PATH},${TASK_GLOBAL_NAME%.*},${TASK_GLOBAL_UUID},0x1000 \
    ${TASK_STORAGE_PATH},${TASK_STORAGE_NAME%.*},${TASK_STORAGE_UUID},0x1000 \
    ${TASK_REET_PATH},${TASK_REET_NAME%.*},${TASK_REET_UUID},0x1000 \
    ${TASK16_PATH},${TASK16_NAME%.*},${TASK16_UUID},0x1000 \
    "

############################ TASK_HISI_LIST ###############################
#NOTE: MUST end or start with blank
TASK_HISI_LIST=

#SUPPORT_TEST_TA
if [ $SUPPORT_TEST_TA -gt 0 ];then
	TASK_ECHO_NAME=task_echo.elf
	TASK_ECHO_UUID="01010101010101010101010101010101"
	TASK_ECHO_PATH=${CURDIR}/release/internal_tasks/${TASK_ECHO_NAME}
	TASK_ECHO_CFG="${TASK_ECHO_PATH},${TASK_ECHO_NAME%.*},${TASK_ECHO_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_ECHO_CFG}

	TASK_TEETEST_NAME=task_teetest.elf
	TASK_TEETEST_UUID="03030303030303030303030303030303"
	TASK_TEETEST_PATH=${CURDIR}/release/internal_tasks/${TASK_TEETEST_NAME}
	TASK_TEETEST_CFG="${TASK_TEETEST_PATH},${TASK_TEETEST_NAME%.*},${TASK_TEETEST_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_TEETEST_CFG}
fi

#SUPPORT_HISI_DEMO
if [ $SUPPORT_HISI_DEMO -gt 0 ];then
	TASK_HISI_DEMO_NAME=task_hisi_demo.elf
	TASK_HISI_DEMO_UUID="0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B"
	TASK_HISI_DEMO_PATH=${IMG_PATH}/${TASK_HISI_DEMO_NAME}
	TASK_HISI_DEMO_CFG="${TASK_HISI_DEMO_PATH},${TASK_HISI_DEMO_NAME%.*},${TASK_HISI_DEMO_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_HISI_DEMO_CFG}
fi

#SUPPORT_HISI_DRM
if [ $SUPPORT_HISI_DRM -gt 0 ];then
	TASK_HISI_DRM_NAME=task_stb_drm.elf
	TASK_HISI_DRM_UUID="14047d2df23648a0a0c4c1cccbb64546"
	TASK_HISI_DRM_PATH=${CURDIR}/release/internal_tasks/${TASK_HISI_DRM_NAME}
	TASK_HISI_DRM_CFG="${TASK_HISI_DRM_PATH},${TASK_HISI_DRM_NAME%.*},${TASK_HISI_DRM_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_HISI_DRM_CFG}
fi

#SEC_MMZ_SUPPORT
if [ $SUPPORT_SEC_MMZ -gt 0 ];then
	TASK_SEC_MMZ_NAME=task_sec_mmz.elf
	TASK_SEC_MMZ_UUID="0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C"
	TASK_SEC_MMZ_PATH=${IMG_PATH}/${TASK_SEC_MMZ_NAME}
	TASK_SEC_MMZ_CFG="${TASK_SEC_MMZ_PATH},${TASK_SEC_MMZ_NAME%.*},${TASK_SEC_MMZ_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_SEC_MMZ_CFG}
fi

#SEC_HDMI_SUPPORT
if [ $SUPPORT_HDMI -gt 0 ];then
	TASK_HDMI_NAME=task_sec_hdmi.elf
	TASK_HDMI_UUID="08080601010000000000000000000001"
	TASK_HDMI_PATH=${IMG_PATH}/${TASK_HDMI_NAME}
	TASK_HDMI_CFG="${TASK_HDMI_PATH},${TASK_HDMI_NAME%.*},${TASK_HDMI_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_HDMI_CFG}
fi


#VFMW_SUPPORT
if [ $SUPPORT_VFMW -gt 0 ];then
	TASK_HISI_VFMW_NAME=vfmw_task.elf
	TASK_HISI_VFMW_UUID="0D0D0D0D0D0D0D0D0D0D0D0D0D0D0D0D"
	TASK_HISI_VFMW_PATH=${IMG_PATH}/${TASK_HISI_VFMW_NAME}
	TASK_HISI_VFMW_CFG="${TASK_HISI_VFMW_PATH},${TASK_HISI_VFMW_NAME%.*},${TASK_HISI_VFMW_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_HISI_VFMW_CFG}
fi

#VPSS_SUPPORT
if [ $SUPPORT_VPSS -gt 0 ];then
	TASK_HISI_VPSS_NAME=task_hisi_vpss.elf
	TASK_HISI_VPSS_UUID="3acc8b1ba7e1e511b86d9a79f06e9478"
	TASK_HISI_VPSS_PATH=${IMG_PATH}/${TASK_HISI_VPSS_NAME}
	TASK_HISI_VPSS_CFG="${TASK_HISI_VPSS_PATH},${TASK_HISI_VPSS_NAME%.*},${TASK_HISI_VPSS_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_HISI_VPSS_CFG}
fi

#SUPPORT_SMMU
if [ $SUPPORT_SMMU -gt 0 ];then
	TASK_SMMU_NAME=smmu_task.elf
	TASK_SMMU_UUID="0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F"
	#TASK_SMMU_UUID="70e6926a816b19f4a6283fccaab121fa"
	TASK_SMMU_PATH=${IMG_PATH}/${TASK_SMMU_NAME}
	TASK_SMMU_CFG="${TASK_SMMU_PATH},${TASK_SMMU_NAME%.*},${TASK_SMMU_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_SMMU_CFG}
fi

#HISI_TEST_SUPPORT
if [ $SUPPORT_HISI_TEST -gt 0 ];then
	TASK_HISI_TEST_NAME=task_hisi_test.elf
	TASK_HISI_TEST_UUID="0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E"
	TASK_HISI_TEST_PATH=${IMG_PATH}/${TASK_HISI_TEST_NAME}
	TASK_HISI_TEST_CFG="${TASK_HISI_TEST_PATH},${TASK_HISI_TEST_NAME%.*},${TASK_HISI_TEST_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_HISI_TEST_CFG}
fi

#SUPPORT DMX
if [ $SUPPORT_DMX -gt 0 ];then
	TASK_DMX_NAME=task_hisi_dmx.elf
	TASK_DMX_UUID="10101010101010101010101010101010"
        TASK_DMX_PATH=${IMG_PATH}/${TASK_DMX_NAME}
        TASK_DMX_CFG="${TASK_DMX_PATH},${TASK_DMX_NAME%.*},${TASK_DMX_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_DMX_CFG}
fi

#SUPPORT PVR
if [ $SUPPORT_PVR -gt 0 ];then
	TASK_HISI_PVR_NAME=task_hisi_pvr.elf
	TASK_HISI_PVR_UUID="11111111111111111111111111111111"
        TASK_HISI_PVR_PATH=${IMG_PATH}/${TASK_HISI_PVR_NAME}
        TASK_PVR_CFG="${TASK_HISI_PVR_PATH},${TASK_HISI_PVR_NAME%.*},${TASK_HISI_PVR_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_PVR_CFG}
fi

#SUPPORT WIDEVINE
if [ $SUPPORT_WIDEVINE -gt 0 ];then
	TASK_WIDEVINE_NAME=task_wv_server.elf
	TASK_WIDEVINE_UUID="79b7778897894a7aa2beb60155eef5f8"
	TASK_WIDEVINE_PATH=${IMG_PATH}/${TASK_WIDEVINE_NAME}
	TASK_WIDEVINE_CFG="${TASK_WIDEVINE_PATH},${TASK_WIDEVINE_NAME%.*},${TASK_WIDEVINE_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_WIDEVINE_CFG}
fi

#SUPPORT_HISI_VDP
if [ $SUPPORT_HISI_VDP -gt 0 ];then
	TASK_HISI_VDP_NAME=task_hisi_vdp.elf
	TASK_HISI_VDP_UUID="1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E"
	TASK_HISI_VDP_PATH=${IMG_PATH}/${TASK_HISI_VDP_NAME}
	TASK_HISI_VDP_CFG="${TASK_HISI_VDP_PATH},${TASK_HISI_VDP_NAME%.*},${TASK_HISI_VDP_UUID},0x1000 "
	TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_HISI_VDP_CFG}
fi

#SUPPORT VMX_ULTRA
if [ $SUPPORT_VMX_ULTRA -gt 0 ];then
        TASK_STB_VMX_ULTRA_VMXTA_NAME=VMXTA.elf
        TASK_STB_VMX_ULTRA_VMXTA_UUID="22222222222222222222222222222222"
        TASK_STB_VMX_ULTRA_VMXTA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_VMXTA_NAME}
        TASK_VMX_ULTRA_VMXTA_CFG="${TASK_STB_VMX_ULTRA_VMXTA_PATH},${TASK_STB_VMX_ULTRA_VMXTA_NAME%.*},${TASK_STB_VMX_ULTRA_VMXTA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_VMXTA_CFG}

        TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME=videomarkTA.elf
        TASK_STB_VMX_ULTRA_VIDEOMARKTA_UUID="33333333333333333333333333333333"
        TASK_STB_VMX_ULTRA_VIDEOMARKTA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME}
        TASK_VMX_ULTRA_VIDEOMARKTA_CFG="${TASK_STB_VMX_ULTRA_VIDEOMARKTA_PATH},${TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME%.*},${TASK_STB_VMX_ULTRA_VIDEOMARKTA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_VIDEOMARKTA_CFG}
fi

if [ $SUPPORT_VMX_ULTRA_VMXTAC_TEST -gt 0 ];then
        TASK_STB_VMX_ULTRA_VMXTA_NAME=VMXTA.elf
        TASK_STB_VMX_ULTRA_VMXTA_UUID="22222222222222222222222222222222"
        TASK_STB_VMX_ULTRA_VMXTA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_VMXTA_NAME}
        TASK_VMX_ULTRA_VMXTA_CFG="${TASK_STB_VMX_ULTRA_VMXTA_PATH},${TASK_STB_VMX_ULTRA_VMXTA_NAME%.*},${TASK_STB_VMX_ULTRA_VMXTA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_VMXTA_CFG}

        TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME=videomarkTA.elf
        TASK_STB_VMX_ULTRA_VIDEOMARKTA_UUID="33333333333333333333333333333333"
        TASK_STB_VMX_ULTRA_VIDEOMARKTA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME}
        TASK_VMX_ULTRA_VIDEOMARKTA_CFG="${TASK_STB_VMX_ULTRA_VIDEOMARKTA_PATH},${TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME%.*},${TASK_STB_VMX_ULTRA_VIDEOMARKTA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_VIDEOMARKTA_CFG}

        TASK_STB_VMX_ULTRA_CLIENTID2TA_NAME=vmxId2TA.elf
        TASK_STB_VMX_ULTRA_CLIENTID2TA_UUID="44444444444444444444444444444444"
        TASK_STB_VMX_ULTRA_CLIENTID2TA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_CLIENTID2TA_NAME}
        TASK_VMX_ULTRA_CLIENTID2TA_CFG="${TASK_STB_VMX_ULTRA_CLIENTID2TA_PATH},${TASK_STB_VMX_ULTRA_CLIENTID2TA_NAME%.*},${TASK_STB_VMX_ULTRA_CLIENTID2TA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_CLIENTID2TA_CFG}

        TASK_STB_VMX_ULTRA_CLIENTID3TA_NAME=vmxId3TA.elf
        TASK_STB_VMX_ULTRA_CLIENTID3TA_UUID="55555555555555555555555555555555"
        TASK_STB_VMX_ULTRA_CLIENTID3TA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_CLIENTID3TA_NAME}
        TASK_VMX_ULTRA_CLIENTID3TA_CFG="${TASK_STB_VMX_ULTRA_CLIENTID3TA_PATH},${TASK_STB_VMX_ULTRA_CLIENTID3TA_NAME%.*},${TASK_STB_VMX_ULTRA_CLIENTID3TA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_CLIENTID3TA_CFG}              
fi

if [ $SUPPORT_VMX_ULTRA_ITAC_TEST -gt 0 ];then
        TASK_STB_VMX_ULTRA_VMXTA_NAME=VMXTA.elf
        TASK_STB_VMX_ULTRA_VMXTA_UUID="22222222222222222222222222222222"
        TASK_STB_VMX_ULTRA_VMXTA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_VMXTA_NAME}
        TASK_VMX_ULTRA_VMXTA_CFG="${TASK_STB_VMX_ULTRA_VMXTA_PATH},${TASK_STB_VMX_ULTRA_VMXTA_NAME%.*},${TASK_STB_VMX_ULTRA_VMXTA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_VMXTA_CFG}

        TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME=videomarkTA.elf
        TASK_STB_VMX_ULTRA_VIDEOMARKTA_UUID="33333333333333333333333333333333"
        TASK_STB_VMX_ULTRA_VIDEOMARKTA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME}
        TASK_VMX_ULTRA_VIDEOMARKTA_CFG="${TASK_STB_VMX_ULTRA_VIDEOMARKTA_PATH},${TASK_STB_VMX_ULTRA_VIDEOMARKTA_NAME%.*},${TASK_STB_VMX_ULTRA_VIDEOMARKTA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_VIDEOMARKTA_CFG}

        TASK_STB_VMX_ULTRA_CLIENTID2TA_NAME=vmxId2TA.elf
        TASK_STB_VMX_ULTRA_CLIENTID2TA_UUID="44444444444444444444444444444444"
        TASK_STB_VMX_ULTRA_CLIENTID2TA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_CLIENTID2TA_NAME}
        TASK_VMX_ULTRA_CLIENTID2TA_CFG="${TASK_STB_VMX_ULTRA_CLIENTID2TA_PATH},${TASK_STB_VMX_ULTRA_CLIENTID2TA_NAME%.*},${TASK_STB_VMX_ULTRA_CLIENTID2TA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_CLIENTID2TA_CFG}

        TASK_STB_VMX_ULTRA_CLIENTID3TA_NAME=vmxId3TA.elf
        TASK_STB_VMX_ULTRA_CLIENTID3TA_UUID="55555555555555555555555555555555"
        TASK_STB_VMX_ULTRA_CLIENTID3TA_PATH=${IMG_PATH}/${TASK_STB_VMX_ULTRA_CLIENTID3TA_NAME}
        TASK_VMX_ULTRA_CLIENTID3TA_CFG="${TASK_STB_VMX_ULTRA_CLIENTID3TA_PATH},${TASK_STB_VMX_ULTRA_CLIENTID3TA_NAME%.*},${TASK_STB_VMX_ULTRA_CLIENTID3TA_UUID},0x1000 "
        TASK_HISI_LIST=${TASK_HISI_LIST}${TASK_VMX_ULTRA_CLIENTID3TA_CFG}              
fi

##############################################################

TASK_LIST=${TASK_LIST}${TASK_HISI_LIST}
echo "task list 2: is ${TASK_LIST}"

echo "1. repack globaltask.img and rtosck.img and place their symbol tables to the file tail"
gcc ${CURDIR}/tools/elf_extract.c -o ${IMG_PATH}/elf_extract

KERNEL_ELF=${IMG_PATH}/${KERNEL_NAME}.elf
KERNEL_SYMTAB_OFFSET=$(ls -l ${KERNEL_PATH}| awk '{print $5}')
${IMG_PATH}/elf_extract "rtosck" ${KERNEL_ELF} ${KERNEL_PATH}

TASK_GLOBAL_ELF=${IMG_PATH}/${TASK_GLOBAL_NAME}.elf
GLOBAL_SYMTAB_OFFSET=$(ls -l ${TASK_GLOBAL_PATH}| awk '{print $5}')
${IMG_PATH}/elf_extract "globaltask" ${TASK_GLOBAL_ELF} ${TASK_GLOBAL_PATH}

echo "2. generator symbol table header for globaltask.img and rtosck.img"
GLOBAL_SYMTAB_SIZE=$(readelf -S ${TASK_GLOBAL_ELF}| awk '{if ($2==".symtab") {print $6}}')
GLOBAL_STRTAB_SIZE=$(readelf -S ${TASK_GLOBAL_ELF}| awk '{if ($2==".strtab") {print $6}}')
GLOBAL_BSS_SIZE=$(readelf -S ${TASK_GLOBAL_ELF}| awk '{if ($2==".bss") {print $6}}')
KERNEL_SYMTAB_SIZE=$(readelf -S ${KERNEL_ELF}| awk '{if ($2==".symtab") {print $6}}')
KERNEL_STRTAB_SIZE=$(readelf -S ${KERNEL_ELF}| awk '{if ($2==".strtab") {print $6}}')
GLOBAL_SYM_HEAD="${GLOBAL_SYMTAB_OFFSET},${GLOBAL_SYMTAB_SIZE},${GLOBAL_STRTAB_SIZE},${GLOBAL_BSS_SIZE}"
KERNEL_SYM_HEAD="${KERNEL_SYMTAB_OFFSET},${KERNEL_SYMTAB_SIZE},${KERNEL_STRTAB_SIZE}"

echo "3. get globaltask.got size"
GOT_SIZE=$(size -A -x ${TASK_GLOBAL_ELF}| awk '{if ($1==".rel.dyn") {print $2}}')
echo "GOT_SIZE=${GOT_SIZE}"

echo "4. generate header"
#modify by l00202565, set globaltask relocation info to image header
ELFFILE=${CURDIR}/release/elfile
readelf -s -W ${TASK_GLOBAL_ELF} > ${ELFFILE}
chmod 777 ${CURDIR}/header.py
${CURDIR}/header.py ${DST_PATH} ${KERNEL_PATH} ${KERNEL_TEXT_BASE} ${GOT_SIZE} \
    ${GLOBAL_SYM_HEAD} ${KERNEL_SYM_HEAD} ${TASK_GLOBAL_ELF} ${ELFFILE} ${COMPARE_IMAGE} ${TASK_LIST}
rm ${ELFFILE}

#add header tag 'TRSTCORE' for STB/DPT
#chmod 777 ${SCRIPT_PATH}/header_tag.py
#${SCRIPT_PATH}/header_tag.py ${DST_PATH} ${DST_PATH}_tmp
#mv ${DST_PATH}_tmp ${DST_PATH}
rm ${KERNEL_PATH}
rm ${IMG_PATH}/${TASK_GLOBAL_NAME}
