[Feature]add MT2731_MP2_MR2_SVN388 baseline version
Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/trustzone/teeloader/mt8521/Makefile b/src/bsp/trustzone/teeloader/mt8521/Makefile
new file mode 100644
index 0000000..8c0efe0
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/Makefile
@@ -0,0 +1,49 @@
+CC := ${CROSS_COMPILE}gcc
+AR := ${CROSS_COMPILE}ar
+LD := ${CROSS_COMPILE}ld
+OBJCOPY := ${CROSS_COMPILE}objcopy
+
+LDS = tllink.lds
+
+DIR_INC = ./include
+DIR_SRC = ./src
+DIR_PREBUILT = ./prebuild
+DIR_OBJ = ${TL_RAW_OUT}/obj
+DIR_BIN = ${TL_RAW_OUT}/bin
+
+ASRCS = $(wildcard $(DIR_SRC)/*.s)
+CSRCS = $(wildcard $(DIR_SRC)/*.c)
+SRCS = $(ASRCS) $(CSRCS)
+AOBJS = $(patsubst %.s, $(DIR_OBJ)/%.o, $(notdir $(ASRCS)))
+COBJS = $(patsubst %.c, $(DIR_OBJ)/%.o, $(notdir $(CSRCS)))
+SOBJS = $(wildcard $(DIR_PREBUILT)/*.a)
+OBJS = $(AOBJS) $(COBJS) $(SOBJS)
+
+TARGET = teeloader
+BIN_TARGET = $(DIR_BIN)/$(TARGET)
+
+all: $(OBJS)
+ @if [ ! -d `dirname $(BIN_TARGET).elf` ] ; then \
+ mkdir -p `dirname $(BIN_TARGET).elf`; \
+ fi
+ sed "s/%BASE_ADDR%/${BASE_ADDR}/g" $(LDS) > $(DIR_OBJ)/$(LDS)
+ $(LD) --start-group $^ --end-group -T$(DIR_OBJ)/$(LDS) -o $(BIN_TARGET).elf
+ -echo "teeloader binary created"
+ $(OBJCOPY) -O binary $(BIN_TARGET).elf $(BIN_TARGET).bin
+ ./zero_padding.sh $(BIN_TARGET).bin ${TL_ALIGN_SIZE}
+
+$(DIR_OBJ)/%.o: $(DIR_SRC)/%.c
+ @if [ ! -d `dirname $@` ] ; then \
+ mkdir -p `dirname $@`; \
+ fi
+ $(CC) -I$(DIR_INC) -DTL_VERIFY_ENABLE=${TL_VERIFY_ENABLE} -DBASE_ADDR=${BASE_ADDR} -DTL_ALIGN_SIZE=${TL_ALIGN_SIZE} -c $^ -o $@
+
+$(DIR_OBJ)/%.o: $(DIR_SRC)/%.s
+ @if [ ! -d `dirname $@` ] ; then \
+ mkdir -p `dirname $@`; \
+ fi
+ $(CC) -c $^ -o $@
+
+.PHONY: clean
+clean:
+ -@rm -rf $(DIR_OBJ)/* $(DIR_BIN)/*
diff --git a/src/bsp/trustzone/teeloader/mt8521/cus_tzimg_dec_key.py b/src/bsp/trustzone/teeloader/mt8521/cus_tzimg_dec_key.py
new file mode 100755
index 0000000..213c87e
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/cus_tzimg_dec_key.py
@@ -0,0 +1,149 @@
+# This script modfiy teeloader tz_keys.h MTEE_IMG_VFY_PUBK item when customer set they own keys
+
+import os
+import struct
+import sys
+import binascii
+
+IMG_KEY_PATH = "./teeloader/include"
+IMG_KEY_NAME = "mtee_key.pem"
+
+TARGET_H_FILE_PATH = "./teeloader/include"
+TARGET_H_FILE_NAME = "tz_keys.h"
+
+def parse_key(keyfile):
+ temp_key_file = "%s.temp" % keyfile
+ if os.path.exists(temp_key_file):
+ os.remove(temp_key_file)
+
+ cmdline = "openssl rsa -text -in %s -pubout > %s" % (keyfile, temp_key_file)
+ os.system(cmdline)
+ #check if keyfile generated success
+ if not os.path.exists(temp_key_file):
+ print "[error] command line excute failed: %s , please check you linux environment" % cmdline
+ exit(-1)
+
+ #parse keys
+ #read keyfile
+ lines = ""
+ for line in open(temp_key_file):
+ lines += line
+
+ #replace \r \next
+ lines = lines.replace("\n", "")
+ lines = lines.replace(" ", "")
+
+ #get public key
+ #from modulus to publicExponent
+ start_idx = lines.find("modulus:") + len("modulus:")
+ end_idx = lines.find("publicExponent")
+ public_key = lines[start_idx:end_idx]
+ if public_key[:2] == "00":
+ public_key = public_key[2:]
+ public_key = public_key.replace(":", "")
+ print "public_key :" + public_key
+
+ #get private key
+ #from privateExponent to prime1
+ start_idx = lines.find("privateExponent:") + len("privateExponent:")
+ end_idx = lines.find("prime1")
+ private_key = lines[start_idx:end_idx]
+ if private_key[:2] == "00":
+ private_key = private_key[2:]
+ private_key = private_key.replace(":", "")
+ print "\nprivate_key:" + private_key
+
+ os.remove(temp_key_file)
+
+ return (public_key, private_key)
+
+def string_to_bin(string,output_file):
+ if os.path.exists(output_file):
+ os.remove(output_file)
+
+ if (len(string) != 256*2):
+ print "[error] key length is not 256, please check"
+ exit(-1)
+
+ of = open(output_file, 'wb')
+
+ for i in range(0,len(string),2):
+ num=(int(string[i],16)<<4) + int(string[i+1],16)
+ byte=struct.pack('B',num)
+ of.write(byte)
+
+ of.close
+
+def bin_file_to_h_file(in_bin_file,out_h_file):
+ if not os.path.exists(in_bin_file):
+ print "[error] File %s not exist, please check" % in_bin_file
+ exit(-2)
+
+ if os.path.exists(out_h_file):
+ os.remove(out_h_file)
+
+ of = open(out_h_file, 'w')
+
+ j = 0
+ with open(in_bin_file, 'rb') as f:
+ content = f.read()
+ for i in range(len(content)):
+ if j % 8 == 0:
+ of.write("\n ")
+ j += 1
+
+ of.write("0x%s," % binascii.hexlify(content[i])),
+ of.write("\n")
+
+ f.close()
+ of.close()
+
+def gen_final_h_file(in_h_file,out_h_file):
+ if not os.path.exists(in_h_file):
+ print "[error] File %s not exist, please check" % in_h_file
+ exit(-2)
+
+ if os.path.exists(out_h_file):
+ os.remove(out_h_file)
+
+ inf = open(in_h_file, 'r')
+ of = open(out_h_file, 'w')
+
+ of.write("u8 MTEE_IMG_VFY_PUBK[256] = {")
+ of.write(inf.read())
+ of.write("};\n")
+
+ inf.close()
+ of.close()
+
+if __name__ == "__main__":
+ key_file_path = IMG_KEY_PATH + "/" + IMG_KEY_NAME
+ if not os.path.exists(key_file_path):
+ print "[error] File %s not exist, please check" % key_file_path
+ exit(-1)
+
+ (img_auth_public_key, img_auth_private_key) = parse_key(key_file_path)
+
+ public_key_out_file = "%s.pub_out" % key_file_path
+ private_key_out_file = "%s.pri_out" % key_file_path
+
+ string_to_bin(img_auth_public_key,public_key_out_file)
+ string_to_bin(img_auth_private_key,private_key_out_file)
+
+ h_file = "%s.temp" % public_key_out_file
+ bin_file_to_h_file(public_key_out_file,h_file);
+
+ final_h_file = "%s.h" % h_file
+ gen_final_h_file(h_file,final_h_file)
+
+ target_h_file = TARGET_H_FILE_PATH + "/" + TARGET_H_FILE_NAME
+ cmd="cp -f %s %s" %(final_h_file , target_h_file)
+ os.system(cmd)
+
+ temp_file = "./teeloader/include/mtee_key.pem.temp"
+ if os.path.exists(temp_file):
+ os.remove(temp_file)
+ os.remove(h_file)
+ os.remove(final_h_file)
+ os.remove(public_key_out_file)
+ os.remove(private_key_out_file)
diff --git a/src/bsp/trustzone/teeloader/mt8521/cus_tzimg_enc_key.py b/src/bsp/trustzone/teeloader/mt8521/cus_tzimg_enc_key.py
new file mode 100755
index 0000000..b39aa05
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/cus_tzimg_enc_key.py
@@ -0,0 +1,80 @@
+# This script modfiy TRUSTZONE_IMG_PROTECT_CFG.ini AUTH_PARAM_N and AUTH_PARAM_D items when customer set they own keys
+
+import os
+import sys
+
+TARGET_PLATFORM = sys.argv[1]
+MTEE_IMG_KEY_FILE = "./teeloader/include/mtee_key.pem"
+TRUSTZONE_IMG_PROTECT_CFG_FILE = "./mtee/build/cfg/" + TARGET_PLATFORM + "/TRUSTZONE_IMG_PROTECT_CFG.ini"
+TRUSTZONE_IMG_PROTECT_CFG_FILE_TEMP = "./mtee/build/cfg/" + TARGET_PLATFORM + "/TRUSTZONE_IMG_PROTECT_CFG_TEMP.ini"
+
+def parse_key(keyfile):
+ temp_key_file = "%s.temp" % keyfile
+ cmdline = "openssl rsa -text -in %s -pubout > %s" % (keyfile, temp_key_file)
+ os.system(cmdline)
+ #check if keyfile generated success
+ if not os.path.exists(temp_key_file):
+ print "[error] command line excute failed: %s , please check you linux environment" % cmdline
+ exit(-1)
+
+ #parse keys
+ #read keyfile
+ lines = ""
+ for line in open(temp_key_file):
+ lines += line
+
+ #replace \r \next
+ lines = lines.replace("\n", "")
+ lines = lines.replace(" ", "")
+
+ #get public key
+ #from modulus to publicExponent
+ start_idx = lines.find("modulus:") + len("modulus:")
+ end_idx = lines.find("publicExponent")
+ public_key = lines[start_idx:end_idx]
+ if public_key[:2] == "00":
+ public_key = public_key[2:]
+ public_key = public_key.replace(":", "")
+ #print "public_key :" + public_key
+
+ #get private key
+ #from privateExponent to prime1
+ start_idx = lines.find("privateExponent:") + len("privateExponent:")
+ end_idx = lines.find("prime1")
+ private_key = lines[start_idx:end_idx]
+ if private_key[:2] == "00":
+ private_key = private_key[2:]
+ private_key = private_key.replace(":", "")
+ #print "private_key:" + private_key
+
+ os.remove(temp_key_file)
+
+ return (public_key, private_key)
+
+#check paramters
+if len(sys.argv) < 2:
+ print "miss parameter"
+ print "usage: python cus_mtee_enc_key.py platform(ex: mt2701)\n"
+
+#generate temp file used to modify original config
+if os.path.exists(TRUSTZONE_IMG_PROTECT_CFG_FILE_TEMP):
+ os.remove(TRUSTZONE_IMG_PROTECT_CFG_FILE_TEMP)
+cmd = "cp -f " + TRUSTZONE_IMG_PROTECT_CFG_FILE + " " + TRUSTZONE_IMG_PROTECT_CFG_FILE_TEMP
+os.system(cmd)
+
+#modify AUTH_PARAM_N and AUTH_PARAM_D
+(img_auth_public_key, img_auth_private_key) = parse_key(MTEE_IMG_KEY_FILE)
+img_auth_key_list = open(TRUSTZONE_IMG_PROTECT_CFG_FILE_TEMP).readlines()
+tmp = open(TRUSTZONE_IMG_PROTECT_CFG_FILE, "w+")
+for i in range(0, len(img_auth_key_list)):
+ if img_auth_key_list[i].startswith("AUTH_PARAM_N"):
+ img_auth_key_list[i] = "AUTH_PARAM_N = 0x" + img_auth_public_key + "\n"
+ tmp.write(img_auth_key_list[i])
+ elif img_auth_key_list[i].startswith("AUTH_PARAM_D"):
+ img_auth_key_list[i] = "AUTH_PARAM_D = 0x" + img_auth_private_key + "\n"
+ tmp.write(img_auth_key_list[i])
+ else:
+ tmp.write(img_auth_key_list[i])
+
+#job done, remove temp file
+os.remove(TRUSTZONE_IMG_PROTECT_CFG_FILE_TEMP)
\ No newline at end of file
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/blkdev.h b/src/bsp/trustzone/teeloader/mt8521/include/blkdev.h
new file mode 100644
index 0000000..64077dd
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/blkdev.h
@@ -0,0 +1,65 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef BLKDEV_H
+#define BLKDEV_H
+
+#include "typedefs.h"
+
+typedef struct blkdev blkdev_t;
+
+struct blkdev {
+ u32 type; /* block device type */
+ u32 blksz; /* block size. (read/write unit) */
+ u32 erasesz; /* erase size */
+ u32 blks; /* number of blocks in the device */
+ u32 offset; /* user area offset in blksz unit */
+ u8 *blkbuf; /* block size buffer */
+ void *priv; /* device private data */
+ blkdev_t *next; /* next block device */
+ int (*bread)(blkdev_t *bdev, u32 blknr, u32 blks, u8 *buf, u32 part_id);
+ int (*bwrite)(blkdev_t *bdev, u32 blknr, u32 blks, u8 *buf, u32 part_id);
+};
+
+extern int blkdev_register(blkdev_t *bdev);
+extern int blkdev_read(blkdev_t *bdev, u64 src, u32 size, u8 *dst, u32 part_id);
+extern int blkdev_write(blkdev_t *bdev, u64 dst, u32 size, u8 *src, u32 part_id);
+extern int blkdev_bread(blkdev_t *bdev, u32 blknr, u32 blks, u8 *buf, u32 part_id);
+extern int blkdev_bwrite(blkdev_t *bdev, u32 blknr, u32 blks, u8 *buf, u32 part_id);
+extern blkdev_t *blkdev_get(u32 type);
+
+#endif /* BLKDEV_H */
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/buffer.h b/src/bsp/trustzone/teeloader/mt8521/include/buffer.h
new file mode 100644
index 0000000..05c8568
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/buffer.h
@@ -0,0 +1,75 @@
+/*
+* This software/firmware and related documentation ("MediaTek Software") are
+* protected under relevant copyright laws. The information contained herein
+* is confidential and proprietary to MediaTek Inc. and/or its licensors.
+* Without the prior written permission of MediaTek inc. and/or its licensors,
+* any reproduction, modification, use or disclosure of MediaTek Software,
+* and information contained herein, in whole or in part, shall be strictly prohibited.
+*/
+/* MediaTek Inc. (C) 2016. All rights reserved.
+*
+* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+* THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+* CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+* SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+* CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+*
+* The following software/firmware and/or related documentation ("MediaTek Software")
+* have been modified by MediaTek Inc. All revisions are subject to any receiver's
+* applicable license agreements with MediaTek Inc.
+*/
+
+#ifndef BUFFER_ADDR_H
+#define BUFFER_ADDR_H
+
+#include "dram_buffer.h"
+
+#define SEC_SECRO_BUFFER_START sec_secro_buf
+#define SEC_SECRO_BUFFER_LENGTH DRAM_SEC_SECRO_BUFFER_LENGTH
+
+#define SEC_WORKING_BUFFER_START sec_working_buf
+#define SEC_WORKING_BUFFER_LENGTH DRAM_SEC_WORKING_BUFFER_LENGTH
+
+#define SEC_UTIL_BUFFER_START sec_util_buf
+#define SEC_UTIL_BUFFER_LENGTH DRAM_SEC_UTIL_BUFFER_LENGTH
+
+/*SecLib.a use DRAM*/
+#define SEC_LIB_HEAP_START sec_lib_heap_buf
+#define SEC_LIB_HEAP_LENGTH DRAM_SEC_LIB_HEAP_LENGTH
+
+/*For v3 verify check buffer */
+#define SEC_IMG_BUFFER_START sec_img_buf
+#define SEC_IMG_BUFFER_LENGTH DRAM_SEC_IMG_BUFFER_LENGTH
+
+#define SEC_CHUNK_BUFFER_START sec_chunk_buf
+#define SEC_CHUNK_BUFFER_LENGTH DRAM_SEC_CHUNK_BUFFER_LENGTH
+
+#define DA_RAM_ADDR (CFG_DA_RAM_ADDR)
+#define DA_RAM_LENGTH (0x30000)
+
+#define DA_RAM_RELOCATE_ADDR (CFG_DA_RAM_ADDR + DA_RAM_LENGTH)
+#define DA_RAM_RELOCATE_LENGTH (DA_RAM_LENGTH)
+
+#define sec_secro_buf g_dram_buf->sec_secro_buf
+#define sec_working_buf g_sec_buf.sram_sec_working_buf
+#define sec_util_buf g_dram_buf->sec_util_buf
+#define sec_lib_heap_buf g_dram_buf->sec_lib_heap_buf
+#define sec_img_buf g_sec_buf.sram_sec_img_buf
+#define sec_chunk_buf g_dram_buf->sec_chunk_buf
+#endif
+
+
+
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/dram_buffer.h b/src/bsp/trustzone/teeloader/mt8521/include/dram_buffer.h
new file mode 100644
index 0000000..df86f38
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/dram_buffer.h
@@ -0,0 +1,142 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef DRAM_BUFFER_H
+#define DRAM_BUFFER_H
+
+#include "partition.h"
+
+#define BMT_BUFFER_SIZE 0x10000
+#define PART_HDR_BUF_SIZE 512
+#define GPT_BUFFER_SIZE (0x4000)
+#define STORAGE_BUFFER_SIZE 0x10000
+#define IMG_HDR_BUF_SIZE 512
+#define LOG_BUFFER_MAX_SIZE (0x10000)
+#define DRAM_SEC_SECRO_BUFFER_LENGTH (0x3000)
+#define DRAM_SEC_WORKING_BUFFER_LENGTH 0x2000
+#define DRAM_SEC_UTIL_BUFFER_LENGTH 0x1000
+#define DRAM_SEC_LIB_HEAP_LENGTH 0x4000
+#define DRAM_SEC_IMG_BUFFER_LENGTH 0x3000
+#define DRAM_SEC_CHUNK_BUFFER_LENGTH 0x100000
+#define CFG_DRAM_ADDR (0x00240000)
+#define MAX_MAIN_SIZE (0x1000)
+#define MAX_SPAR_SIZE (0x80)
+#define BMT_DAT_BUFFER_SIZE (MAX_MAIN_SIZE + MAX_SPAR_SIZE)
+#define PMT_DAT_BUFFER_SIZE (MAX_MAIN_SIZE + MAX_SPAR_SIZE)
+#define PMT_READ_BUFFER_SIZE (MAX_MAIN_SIZE)
+#define NAND_NFI_BUFFER_SIZE 0x1000
+#define PART_MAX_NUM 20
+
+#if CFG_BYPASS_EMI
+typedef struct {
+ u8 bmt_buf[0x1000];
+ u8 bmt_dat_buf[BMT_DAT_BUFFER_SIZE];
+ u8 nand_nfi_buf[0x1000];
+ part_hdr_t part_hdr_buf[1];
+ u32 crc32_table[256];
+ u8 pgpt_header_buf[512];
+ u8 sgpt_header_buf[512];
+ u8 pgpt_entries_buf[GPT_BUFFER_SIZE];
+ u8 sgpt_entries_buf[GPT_BUFFER_SIZE];
+ unsigned char storage_buffer[16];
+ u8 img_hdr_buf[IMG_HDR_BUF_SIZE];
+ unsigned int part_num;
+ part_hdr_t part_info[2];
+ part_t partition_info[2];
+#ifdef MTK_EMMC_SUPPORT
+ struct part_meta_info meta_info[1];
+#endif
+ u32 bootarg;
+ u8 log_dram_buf[0x1000];
+ u8 sec_secro_buf[DRAM_SEC_SECRO_BUFFER_LENGTH];
+ u8 sec_working_buf[DRAM_SEC_WORKING_BUFFER_LENGTH];/*This dram Buffer not used for security concern*/
+ u8 sec_util_buf[DRAM_SEC_UTIL_BUFFER_LENGTH];
+ u8 sec_lib_heap_buf[DRAM_SEC_LIB_HEAP_LENGTH];
+ u8 sec_img_buf[DRAM_SEC_IMG_BUFFER_LENGTH]; /*This dram Buffer not used for security concern*/
+ u8 sec_chunk_buf[0x4000];
+ u32 *boottag;
+} dram_buf_t;
+#else
+typedef struct {
+ /*bmt.c*/
+ u8 bmt_buf[BMT_BUFFER_SIZE];
+ u8 bmt_dat_buf[BMT_DAT_BUFFER_SIZE];
+ /*nand.c*/
+ u8 nand_nfi_buf[NAND_NFI_BUFFER_SIZE];
+
+ /*download.c*/
+ part_hdr_t part_hdr_buf[PART_HDR_BUF_SIZE];
+ /*efi.c*/
+ u32 crc32_table[256];
+ u8 pgpt_header_buf[512];
+ u8 sgpt_header_buf[512];
+ u8 pgpt_entries_buf[GPT_BUFFER_SIZE];
+ u8 sgpt_entries_buf[GPT_BUFFER_SIZE];
+ /*mmc_common_inter.c*/
+ unsigned char storage_buffer[STORAGE_BUFFER_SIZE];
+ /*partition.c*/
+ u8 img_hdr_buf[IMG_HDR_BUF_SIZE];
+ unsigned int part_num;
+ part_hdr_t part_info[PART_MAX_NUM];
+ part_t partition_info[128];
+
+#ifdef MTK_EMMC_SUPPORT
+ struct part_meta_info meta_info[128];
+#endif
+ u32 bootarg;
+ u8 log_dram_buf[LOG_BUFFER_MAX_SIZE];
+ u8 sec_secro_buf[DRAM_SEC_SECRO_BUFFER_LENGTH];
+ u8 sec_working_buf[DRAM_SEC_WORKING_BUFFER_LENGTH];/*This dram Buffer not used for security concern*/
+ u8 sec_util_buf[DRAM_SEC_UTIL_BUFFER_LENGTH];
+ u8 sec_lib_heap_buf[DRAM_SEC_LIB_HEAP_LENGTH];
+ u8 sec_img_buf[DRAM_SEC_IMG_BUFFER_LENGTH]; /*This dram Buffer not used for security concern*/
+ u8 sec_chunk_buf[DRAM_SEC_CHUNK_BUFFER_LENGTH];
+ u32 *boottag;
+} dram_buf_t;
+#endif
+
+typedef struct {
+ u8 sram_sec_working_buf[DRAM_SEC_WORKING_BUFFER_LENGTH];
+ u8 sram_sec_img_buf[DRAM_SEC_IMG_BUFFER_LENGTH];
+} sec_buf_t;
+
+void init_dram_buffer();
+u64 platform_memory_size(void);
+dram_buf_t *g_dram_buf;
+sec_buf_t g_sec_buf;
+
+#endif /*DRAM_BUFFER_H*/
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/partition.h b/src/bsp/trustzone/teeloader/mt8521/include/partition.h
new file mode 100644
index 0000000..7c0fb8f
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/partition.h
@@ -0,0 +1,94 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef PARTITION_H
+#define PARTITION_H
+
+#include "typedefs.h"
+#include "blkdev.h"
+
+#define PART_HEADER_DEFAULT_ADDR (0xFFFFFFFF)
+#define LOAD_ADDR_MODE_BACKWARD (0x00000000)
+#define PART_MAGIC 0x58881688
+
+typedef union {
+ struct {
+ unsigned int magic; /* partition magic */
+ unsigned int dsize; /* partition data size */
+ char name[32]; /* partition name */
+ unsigned int maddr; /* partition memory address */
+ unsigned int mode; /* memory addressing mode */
+ } info;
+ unsigned char data[512];
+} part_hdr_t;
+
+#if 1
+#define PART_META_INFO_NAMELEN 64
+#define PART_META_INFO_UUIDLEN 16
+
+struct part_meta_info {
+ u8 name[PART_META_INFO_NAMELEN];
+ u8 uuid[PART_META_INFO_UUIDLEN];
+};
+
+typedef struct {
+ unsigned long start_sect;
+ unsigned long nr_sects;
+ unsigned int part_id;
+ struct part_meta_info *info;
+} part_t;
+#else
+typedef struct {
+ unsigned char *name; /* partition name */
+ unsigned long startblk; /* partition start blk */
+ unsigned long size; /* partition size */
+ unsigned long blks; /* partition blks */
+ unsigned long flags; /* partition flags */
+ unsigned int part_id;
+} part_t;
+#endif
+
+extern int part_init(void);
+extern part_t *part_get(char *name);
+extern int part_load(blkdev_t *bdev, part_t *part, u32 *addr, u32 offset, u32 *size);
+extern void part_dump(void);
+
+extern part_t *get_part(char *name);
+extern void put_part(part_t *part);
+extern int part_load_raw_part(blkdev_t *bdev, part_t *part, u32 *addr, u32 offset, u32 *size);
+
+#endif /* PARTITION_H */
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/print.h b/src/bsp/trustzone/teeloader/mt8521/include/print.h
new file mode 100644
index 0000000..50e2385
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/print.h
@@ -0,0 +1,47 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef PRINT_H
+#define PRINT_H
+
+extern void dbg_print(char *sz, ...);
+extern void print(char *sz, ...);
+extern void log_buf_ctrl(int drambuf);
+extern void log_ctrl(int enable);
+extern int log_status(void);
+
+#endif /* PRINT_H */
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/stdlib.h b/src/bsp/trustzone/teeloader/mt8521/include/stdlib.h
new file mode 100644
index 0000000..91eb793
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/stdlib.h
@@ -0,0 +1,44 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __STDLIB_H__
+#define __STDLIB_H__
+
+int atoi(const char *s);
+long long atoll(const char *num);
+
+#endif
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/string.h b/src/bsp/trustzone/teeloader/mt8521/include/string.h
new file mode 100644
index 0000000..5436663
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/string.h
@@ -0,0 +1,48 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef STRING_H
+#define STRING_H
+
+extern int strlen(const char *s);
+extern int strcmp(const char *cs, const char *ct);
+extern int strncmp(const char *cs, const char *ct, int count);
+extern void *memset(void *s, int c, int count);
+extern void *memcpy(void *dest, const void *src, int count);
+extern int memcmp(const void *cs, const void *ct, int count);
+
+#endif /* STRING_H */
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/typedefs.h b/src/bsp/trustzone/teeloader/mt8521/include/typedefs.h
new file mode 100644
index 0000000..dcf93d1
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/typedefs.h
@@ -0,0 +1,303 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef _TYPEDEFS_H_
+#define _TYPEDEFS_H_
+
+#define __NOBITS_SECTION__(x) __attribute__((section(#x ", \"aw\", %nobits@")))
+#define __SRAM__ __NOBITS_SECTION__(.secbuf)
+typedef unsigned long ulong;
+typedef unsigned char uchar;
+typedef unsigned int uint;
+typedef signed char int8;
+typedef signed short int16;
+typedef signed long int32;
+typedef signed int intx;
+typedef unsigned char uint8;
+typedef unsigned short uint16;
+typedef unsigned long uint32;
+typedef unsigned int uintx;
+
+//------------------------------------------------------------------
+
+typedef volatile unsigned char *P_kal_uint8;
+typedef volatile unsigned short *P_kal_uint16;
+typedef volatile unsigned int *P_kal_uint32;
+
+typedef long LONG;
+typedef unsigned char UBYTE;
+typedef short SHORT;
+
+typedef signed char kal_int8;
+typedef signed short kal_int16;
+typedef signed int kal_int32;
+typedef long long kal_int64;
+typedef unsigned char kal_uint8;
+typedef unsigned short kal_uint16;
+typedef unsigned int kal_uint32;
+typedef unsigned long long kal_uint64;
+typedef char kal_char;
+
+typedef unsigned int *UINT32P;
+typedef volatile unsigned short *UINT16P;
+typedef volatile unsigned char *UINT8P;
+typedef unsigned char *U8P;
+
+typedef volatile unsigned char *P_U8;
+typedef volatile signed char *P_S8;
+typedef volatile unsigned short *P_U16;
+typedef volatile signed short *P_S16;
+typedef volatile unsigned int *P_U32;
+typedef volatile signed int *P_S32;
+typedef unsigned long long *P_U64;
+typedef signed long long *P_S64;
+
+typedef unsigned char U8;
+typedef signed char S8;
+typedef unsigned short U16;
+typedef signed short S16;
+typedef unsigned int U32;
+typedef signed int S32;
+typedef unsigned long long U64;
+typedef signed long long S64;
+typedef unsigned char bool;
+
+//------------------------------------------------------------------
+
+typedef unsigned char UINT8;
+typedef unsigned short UINT16;
+typedef unsigned int UINT32;
+typedef unsigned short USHORT;
+typedef signed char INT8;
+typedef signed short INT16;
+typedef signed int INT32;
+typedef signed int DWORD;
+typedef void VOID;
+typedef unsigned char BYTE;
+typedef float FLOAT;
+
+typedef char *LPCSTR;
+typedef short *LPWSTR;
+
+//------------------------------------------------------------------
+
+typedef char __s8;
+typedef unsigned char __u8;
+typedef short __s16;
+typedef unsigned short __u16;
+typedef int __s32;
+typedef unsigned int __u32;
+typedef long long __s64;
+typedef unsigned long long __u64;
+typedef signed char s8;
+typedef unsigned char u8;
+typedef signed short s16;
+typedef unsigned short u16;
+typedef signed int s32;
+typedef unsigned int u32;
+typedef signed long long s64;
+typedef unsigned long long u64;
+#define BITS_PER_LONG 32
+/* Dma addresses are 32-bits wide. */
+typedef u32 dma_addr_t;
+
+//------------------------------------------------------------------
+
+#define FALSE 0
+#define TRUE 1
+
+#define IMPORT EXTERN
+#ifndef __cplusplus
+#define EXTERN extern
+#else
+#define EXTERN extern "C"
+#endif
+#define LOCAL static
+#define GLOBAL
+#define EXPORT GLOBAL
+
+#define EQ ==
+#define NEQ !=
+#define AND &&
+#define OR ||
+#define XOR(A,B) ((!(A) AND (B)) OR ((A) AND !(B)))
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+enum boolean {
+ false,
+ true
+};
+
+enum {
+ RX,
+ TX,
+ NONE
+};
+
+#ifndef BOOL
+typedef unsigned char BOOL;
+#endif
+
+typedef enum {
+ KAL_FALSE = 0,
+ KAL_TRUE = 1,
+} kal_bool;
+
+/*==== EXPORTED MACRO ===================================================*/
+
+#define MAXIMUM(A,B) (((A)>(B))?(A):(B))
+#define MINIMUM(A,B) (((A)<(B))?(A):(B))
+
+#define READ_REGISTER_UINT32(reg) \
+ (*(volatile UINT32 * const)(reg))
+
+#define WRITE_REGISTER_UINT32(reg, val) \
+ (*(volatile UINT32 * const)(reg)) = (val)
+
+#define READ_REGISTER_UINT16(reg) \
+ (*(volatile UINT16 * const)(reg))
+
+#define WRITE_REGISTER_UINT16(reg, val) \
+ (*(volatile UINT16 * const)(reg)) = (val)
+
+#define READ_REGISTER_UINT8(reg) \
+ (*(volatile UINT8 * const)(reg))
+
+#define WRITE_REGISTER_UINT8(reg, val) \
+ (*(volatile UINT8 * const)(reg)) = (val)
+
+#define INREG8(x) READ_REGISTER_UINT8((UINT8*)(x))
+#define OUTREG8(x, y) WRITE_REGISTER_UINT8((UINT8*)(x), (UINT8)(y))
+#define SETREG8(x, y) OUTREG8(x, INREG8(x)|(y))
+#define CLRREG8(x, y) OUTREG8(x, INREG8(x)&~(y))
+#define MASKREG8(x, y, z) OUTREG8(x, (INREG8(x)&~(y))|(z))
+
+#define INREG16(x) READ_REGISTER_UINT16((UINT16*)(x))
+#define OUTREG16(x, y) WRITE_REGISTER_UINT16((UINT16*)(x),(UINT16)(y))
+#define SETREG16(x, y) OUTREG16(x, INREG16(x)|(y))
+#define CLRREG16(x, y) OUTREG16(x, INREG16(x)&~(y))
+#define MASKREG16(x, y, z) OUTREG16(x, (INREG16(x)&~(y))|(z))
+
+#define INREG32(x) READ_REGISTER_UINT32((UINT32*)(x))
+#define OUTREG32(x, y) WRITE_REGISTER_UINT32((UINT32*)(x), (UINT32)(y))
+#define SETREG32(x, y) OUTREG32(x, INREG32(x)|(y))
+#define CLRREG32(x, y) OUTREG32(x, INREG32(x)&~(y))
+#define MASKREG32(x, y, z) OUTREG32(x, (INREG32(x)&~(y))|(z))
+
+#define DRV_Reg8(addr) INREG8(addr)
+#define DRV_WriteReg8(addr, data) OUTREG8(addr, data)
+#define DRV_SetReg8(addr, data) SETREG8(addr, data)
+#define DRV_ClrReg8(addr, data) CLRREG8(addr, data)
+
+#define DRV_Reg16(addr) INREG16(addr)
+#define DRV_WriteReg16(addr, data) OUTREG16(addr, data)
+#define DRV_SetReg16(addr, data) SETREG16(addr, data)
+#define DRV_ClrReg16(addr, data) CLRREG16(addr, data)
+
+#define DRV_Reg32(addr) INREG32(addr)
+#define DRV_WriteReg32(addr, data) OUTREG32(addr, data)
+#define DRV_SetReg32(addr, data) SETREG32(addr, data)
+#define DRV_ClrReg32(addr, data) CLRREG32(addr, data)
+
+// !!! DEPRECATED, WILL BE REMOVED LATER !!!
+#define DRV_Reg(addr) DRV_Reg16(addr)
+#define DRV_WriteReg(addr, data) DRV_WriteReg16(addr, data)
+#define DRV_SetReg(addr, data) DRV_SetReg16(addr, data)
+#define DRV_ClrReg(addr, data) DRV_ClrReg16(addr, data)
+
+#define __raw_readb(REG) DRV_Reg8(REG)
+#define __raw_readw(REG) DRV_Reg16(REG)
+#define __raw_readl(REG) DRV_Reg32(REG)
+#define __raw_writeb(VAL, REG) DRV_WriteReg8(REG,VAL)
+#define __raw_writew(VAL, REG) DRV_WriteReg16(REG,VAL)
+#define __raw_writel(VAL, REG) DRV_WriteReg32(REG,VAL)
+
+#define dsb() \
+ __asm__ __volatile__("mcr p15, 0, %0, c7, c10, 4" : : "r" (0) : "memory")
+
+#if 0
+extern void platform_assert(char *file, int line, char *expr);
+
+#define ASSERT(expr) \
+ do{ if(!(expr)){platform_assert(__FILE__, __LINE__, #expr);} }while(0)
+#endif
+
+// compile time assert
+//#define COMPILE_ASSERT(condition) ((void)sizeof(char[1 - 2*!!!(condition)]))
+
+#define printf print
+#define tl_printf tl_print
+//#define BUG_ON(expr) ASSERT(!(expr))
+
+#if defined(MACH_TYPE_MT6735M)
+#define printf(fmt, args...) do{}while(0)
+#endif
+
+//------------------------------------------------------------------
+
+#if 0
+typedef char *va_list;
+#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
+#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
+#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
+#define va_end(ap) ( ap = (va_list)0 )
+#else
+#include <stdarg.h>
+#endif
+
+#define READ_REG(REG) __raw_readl(REG)
+#define WRITE_REG(VAL, REG) __raw_writel(VAL, REG)
+
+#ifndef min
+#define min(x, y) (x < y ? x : y)
+#endif
+#ifndef max
+#define max(x, y) (x > y ? x : y)
+#endif
+
+#endif
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/tz_init.h b/src/bsp/trustzone/teeloader/mt8521/include/tz_init.h
new file mode 100644
index 0000000..948103e
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/tz_init.h
@@ -0,0 +1,89 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef TRUSTZONE_H
+#define TRUSTZONE_H
+
+#include "tz_keys.h"
+#include "typedefs.h"
+
+#define ATF_BOOTCFG_MAGIC (0x4D415446) // String MATF in little-endian
+#define DEVINFO_SIZE 4
+
+#define MCUSYS_CFGREG_BASE (0x10000000 + 0x00200000)
+#define RVBADDRESS_CPU0 (MCUSYS_CFGREG_BASE + 0x38)
+
+/*
+ RSA2048 public key for verifying mtee image
+ It should be the same as AUTH_PARAM_N in alps\mediatek\custom\mt6752_evb\trustzone\TRUSTZONE_IMG_PROTECT_CFG.ini
+*/
+#define MTEE_IMG_VFY_PUBK_SZ 256
+
+typedef struct {
+ u32 atf_magic;
+ u32 tee_support;
+ u32 tee_entry;
+ u32 tee_boot_arg_addr;
+ u32 hwuid[4]; // HW Unique id for t-base used
+ u32 HRID[2]; // HW random id for t-base used
+ u32 atf_log_port;
+ u32 atf_log_baudrate;
+ u32 atf_log_buf_start;
+ u32 atf_log_buf_size;
+ u32 atf_irq_num;
+ u32 devinfo[DEVINFO_SIZE];
+ u32 atf_aee_debug_buf_start;
+ u32 atf_aee_debug_buf_size;
+#if CFG_TEE_SUPPORT
+ u32 tee_rpmb_size;
+#endif
+} atf_arg_t, *atf_arg_t_ptr;
+
+/**************************************************************************
+ * EXPORTED FUNCTIONS
+ **************************************************************************/
+void tee_get_secmem_start(u32 *addr);
+void tee_get_secmem_size(u32 *size);
+void tee_set_entry(u32 addr);
+void tee_set_hwuid(u8 *id, u32 size);
+int tee_verify_image(u32 *addr, u32 size);
+u32 tee_get_load_addr(u32 maddr);
+void trustzone_pre_init(void);
+void trustzone_post_init(void);
+void trustzone_jump(u32 addr, u32 arg1, u32 arg2);
+
+#endif /* TRUSTZONE_H */
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/tz_keys.h b/src/bsp/trustzone/teeloader/mt8521/include/tz_keys.h
new file mode 100644
index 0000000..c412839
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/tz_keys.h
@@ -0,0 +1,55 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+u8 MTEE_IMG_VFY_PUBK[256] = {
+ 0xDA, 0xCD, 0x8B, 0x5F, 0xDA, 0x8A, 0x76, 0x6F, 0xB7, 0xBC, 0xAA, 0x43, 0xF0, 0xB1, 0x69, 0x15,
+ 0xCE, 0x7B, 0x47, 0x71, 0x4F, 0x13, 0x95, 0xFD, 0xEB, 0xCF, 0x12, 0xA2, 0xD4, 0x11, 0x55, 0xB0,
+ 0xFB, 0x58, 0x7A, 0x51, 0xFE, 0xCC, 0xCB, 0x4D, 0xDA, 0x1C, 0x8E, 0x5E, 0xB9, 0xEB, 0x69, 0xB8,
+ 0x6D, 0xAF, 0x2C, 0x62, 0x0F, 0x6C, 0x27, 0x35, 0x21, 0x5A, 0x5F, 0x22, 0xC0, 0xB6, 0xCE, 0x37,
+ 0x7A, 0xA0, 0xD0, 0x7E, 0xB3, 0x8E, 0xD3, 0x40, 0xB5, 0x62, 0x9F, 0xC2, 0x89, 0x04, 0x94, 0xB0,
+ 0x78, 0xA6, 0x3D, 0x6D, 0x07, 0xFD, 0xEA, 0xCD, 0xBE, 0x3E, 0x7F, 0x27, 0xFD, 0xE4, 0xB1, 0x43,
+ 0xF4, 0x9D, 0xB4, 0x97, 0x14, 0x37, 0xE6, 0xD0, 0x0D, 0x9E, 0x18, 0xB5, 0x6F, 0x02, 0xDA, 0xBE,
+ 0xB0, 0x00, 0x0B, 0x6E, 0x79, 0x51, 0x6D, 0x0C, 0x80, 0x74, 0xB5, 0xA4, 0x25, 0x69, 0xFD, 0x0D,
+ 0x91, 0x96, 0x65, 0x5D, 0x2A, 0x40, 0x30, 0xD4, 0x2D, 0xFE, 0x05, 0xE9, 0xF6, 0x48, 0x83, 0xE6,
+ 0xD5, 0xF7, 0x9A, 0x5B, 0xFA, 0x3E, 0x70, 0x14, 0xC9, 0xA6, 0x28, 0x53, 0xDC, 0x1F, 0x21, 0xD5,
+ 0xD6, 0x26, 0xF4, 0xD0, 0x84, 0x6D, 0xB1, 0x64, 0x52, 0x18, 0x7D, 0xD7, 0x76, 0xE8, 0x88, 0x6B,
+ 0x48, 0xC2, 0x10, 0xC9, 0xE2, 0x08, 0x05, 0x9E, 0x7C, 0xAF, 0xC9, 0x97, 0xFD, 0x2C, 0xA2, 0x10,
+ 0x77, 0x5C, 0x1A, 0x5D, 0x9A, 0xA2, 0x61, 0x25, 0x2F, 0xB9, 0x75, 0x26, 0x8D, 0x97, 0x0C, 0x62,
+ 0x73, 0x38, 0x71, 0xD5, 0x78, 0x14, 0x09, 0x8A, 0x45, 0x3D, 0xF9, 0x2B, 0xC6, 0xCA, 0x19, 0x02,
+ 0x5C, 0xD9, 0xD4, 0x30, 0xF0, 0x2E, 0xE4, 0x6F, 0x80, 0xDE, 0x6C, 0x63, 0xEA, 0x80, 0x2B, 0xEF,
+ 0x90, 0x67, 0x3A, 0xAC, 0x4C, 0x66, 0x67, 0xF2, 0x88, 0x3F, 0xB4, 0x50, 0x1F, 0xA7, 0x74, 0x55
+};
diff --git a/src/bsp/trustzone/teeloader/mt8521/include/uart.h b/src/bsp/trustzone/teeloader/mt8521/include/uart.h
new file mode 100644
index 0000000..717090f
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/include/uart.h
@@ -0,0 +1,60 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef UART_H
+#define UART_H
+
+typedef unsigned int uint32_t;
+typedef unsigned long uintptr_t;
+
+#define REG32(addr) ((volatile uint32_t *)(uintptr_t)(addr))
+
+#define writel(v, a) (*REG32(a) = (v))
+#define readl(a) (*REG32(a))
+
+#define UART_BASE(uart) (uart)
+#define UART_LSR(uart) (UART_BASE(uart)+0x14)
+#define UART_LSR_THRE (1 << 5)
+#define UART_THR(uart) (UART_BASE(uart)+0x0) /* Write only */
+
+#define IO_PHYS 0x10000000
+#define UART1_BASE (IO_PHYS + 0x01002000)
+
+int uart_putc(char c);
+
+#endif
+
diff --git a/src/bsp/trustzone/teeloader/mt8521/prebuild/DaVerifyLib.a b/src/bsp/trustzone/teeloader/mt8521/prebuild/DaVerifyLib.a
new file mode 100644
index 0000000..ff62514
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/prebuild/DaVerifyLib.a
Binary files differ
diff --git a/src/bsp/trustzone/teeloader/mt8521/prebuild/HwCryptoLib.a b/src/bsp/trustzone/teeloader/mt8521/prebuild/HwCryptoLib.a
new file mode 100644
index 0000000..be64f3e
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/prebuild/HwCryptoLib.a
Binary files differ
diff --git a/src/bsp/trustzone/teeloader/mt8521/prebuild/SecLib.a b/src/bsp/trustzone/teeloader/mt8521/prebuild/SecLib.a
new file mode 100644
index 0000000..d8be539
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/prebuild/SecLib.a
Binary files differ
diff --git a/src/bsp/trustzone/teeloader/mt8521/prebuild/SecPlat.a b/src/bsp/trustzone/teeloader/mt8521/prebuild/SecPlat.a
new file mode 100644
index 0000000..c2a3b26
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/prebuild/SecPlat.a
Binary files differ
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/div0.c b/src/bsp/trustzone/teeloader/mt8521/src/div0.c
new file mode 100644
index 0000000..278cf23
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/div0.c
@@ -0,0 +1,42 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+#include <typedefs.h>
+
+void __div0(void)
+{
+ //ASSERT(0);
+}
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/dram_buffer.c b/src/bsp/trustzone/teeloader/mt8521/src/dram_buffer.c
new file mode 100644
index 0000000..4547fd6
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/dram_buffer.c
@@ -0,0 +1,59 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#include "dram_buffer.h"
+#include "typedefs.h"
+
+#define MOD "[Dram_Buffer]"
+
+dram_buf_t *g_dram_buf = 0;
+
+u64 platform_memory_size(void)
+{
+ /* now use hard code */
+ u64 mem_size = 99;
+ return mem_size;
+}
+
+void init_dram_buffer()
+{
+ u32 structure_size = sizeof(dram_buf_t);
+
+ /*allocate dram_buf */
+ g_dram_buf = BASE_ADDR - 0x200000;
+ memset((void *)&(g_dram_buf->bootarg), 0, sizeof(u32));
+}
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/dummy.s b/src/bsp/trustzone/teeloader/mt8521/src/dummy.s
new file mode 100644
index 0000000..cd4d98c
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/dummy.s
@@ -0,0 +1,15 @@
+.globl get_timer
+.type get_timer, function
+
+.globl part_load
+.type part_load, function
+
+.globl part_get
+.type part_get, function
+
+get_timer:
+part_load:
+part_get:
+ mov pc, lr
+
+
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/init.s b/src/bsp/trustzone/teeloader/mt8521/src/init.s
new file mode 100644
index 0000000..3da06a1
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/init.s
@@ -0,0 +1,230 @@
+.section .text.start
+
+.equ MODE_USR ,0x10
+.equ MODE_FIQ ,0x11
+.equ MODE_IRQ ,0x12
+.equ MODE_SVC ,0x13
+.equ MODE_MON ,0x16
+.equ MODE_ABT ,0x17
+.equ MODE_UNDEF ,0x1B
+.equ MODE_SYS ,0x1F
+.equ I_BIT ,0x80
+.equ F_BIT ,0x40
+.equ INT_BIT ,0xC0
+
+/* .equ RVBADDRESS_CPU0 ,0x10200038 */
+@.extern sys_stack
+@.extern sys_stack_sz
+@.extern bl31_base_addr
+@.extern rst_vector_base_addr
+/* wenmin: use this hard code to build pass, need check right number */
+sys_stack = 0xBFBF0000
+sys_stack_sz = 0x1000
+bl31_base_addr = 0x31
+rst_vector_base_addr = 0x0
+
+.globl _start
+.type _start, function
+_start:
+ b resethandler
+bss_start:
+ .word _bss_start
+bss_end:
+ .word _bss_end
+stack:
+ .long sys_stack
+stacksz:
+ .long sys_stack_sz
+tee_entry:
+ .word 0x0
+
+resethandler:
+ ldr r5, =tee_entry
+ str r0, [r5]
+ MOV r0, #0
+ MOV r1, #0
+ MOV r2, #0
+ MOV r3, #0
+ MOV r4, #0
+ MOV r5, #0
+ MOV r6, #0
+ MOV r7, #0
+ MOV r8, #0
+ MOV r9, #0
+ MOV r10, #0
+ MOV r11, #0
+ MOV r12, #0
+ MOV sp, #0
+ MOV lr, #0
+
+ /* CONFIG_ARM_ERRATA_826319 */
+ mrc p15, 0, r8, c1, c0, 0 @ Read System Control Register into Rt
+ bic r8, r8, #0x4 @ disable D-Cache
+ bic r8, r8, #0x1000 @ clear I-Cache
+ mcr p15, 0, r8, c1, c0, 0 @ Write Rt to System Control Register
+
+ /* set the cpu to SVC32 mode */
+ MRS r0,cpsr
+ BIC r0,r0,#0x1f
+ ORR r0,r0,#0xd3
+ MSR cpsr,r0
+
+ /* disable interrupt */
+ MRS r0, cpsr
+ MOV r1, #INT_BIT
+ ORR r0, r0, r1
+ MSR cpsr_cxsf, r0
+
+ /* enable I+Z+SMP bits and disable D bit */
+ MRC p15, 0, ip, c1, c0, 0
+ ORR ip, ip, #0x1840 /* I+Z+SMP bits */
+ BIC ip, ip, #0x4 /* C bit */
+ MCR p15, 0, ip, c1, c0, 0
+
+clear_bss :
+ LDR r0, bss_start /* find start of bss segment */
+ LDR r1, bss_end /* stop here */
+ MOV r2, #0x00000000 /* clear */
+
+ CMP r0, r1
+ BEQ setup_stk
+
+ /* clear loop... */
+clbss_l :
+ STR r2, [r0]
+ ADD r0, r0, #4
+ CMP r0, r1
+ BNE clbss_l
+
+setup_stk :
+ /* setup stack */
+ LDR r0, stack
+ LDR r1, =stacksz
+
+ /* buffer overflow detect pattern */
+ LDR r2, =0xDEADBEFF
+ STR r2, [r0]
+
+ LDR r1, [r1]
+ SUB r1, r1, #0x04
+ ADD r1, r0, r1
+
+ MOV sp, r1
+
+entry :
+ LDR r0, =tee_entry
+ LDR r0, [r0]
+ B teeloader_main
+
+.globl jump
+.type jump, function
+jump:
+ MOV r4, r1 /* r4 argument */
+ MOV r5, r2 /* r5 argument */
+ MOV pc, r0 /* jump to addr */
+
+.globl apmcu_icache_invalidate
+.type apmcu_icache_invalidate, function
+apmcu_icache_invalidate:
+ MOV r0, #0
+ MCR p15, 0, r0, c7, c5, 0 /* CHECKME: c5 or c1 */
+ BX lr
+
+.globl apmcu_isb
+.type apmcu_isb, function
+apmcu_isb:
+ ISB
+ BX lr
+
+.globl apmcu_disable_icache
+.type apmcu_disable_icache, function
+apmcu_disable_icache:
+ MOV r0,#0
+ MCR p15,0,r0,c7,c5,6 /* Flush entire branch target cache */
+ MRC p15,0,r0,c1,c0,0
+ BIC r0,r0,#0x1800 /* I+Z bits */
+ MCR p15,0,r0,c1,c0,0
+ BX lr
+
+.globl apmcu_disable_smp
+.type apmcu_disable_smp, function
+apmcu_disable_smp:
+ MRC p15,0,r0,c1,c0,1
+ BIC r0,r0,#0x040 /* SMP bit */
+ MCR p15,0,r0,c1,c0,1
+ BX lr
+
+.section .text.arch64
+.globl jumparch64
+.type jumparch64, function
+jumparch64:
+ MOV r4, r1 /* r4 argument */
+ MOV r5, r2 /* r5 argument */
+ MOV r6, r0 /* keep LK jump addr */
+
+ MOV r7, r3 /* r3 = TEE boot entry, relocate to r7 */
+
+ /* setup the reset vector base address after warm reset to Aarch64 */
+ LDR r0, =bl31_base_addr
+ LDR r0,[r0]
+
+ LDR r1, =rst_vector_base_addr
+ LDR r1,[r1]
+ str r0,[r1]
+
+ /* setup the excution state after warm reset: 1:Aarch64, 0:Aarch32 */
+ MRC p15,0,r0,c12,c0,2
+ orr r0, r0, #1
+ MCR p15,0,r0,c12,c0,2
+ DSB
+ ISB
+
+ /* do warm reset:reset request */
+ MRC p15,0,r0,c12,c0,2
+ orr r0, r0, #2
+ MCR p15,0,r0,c12,c0,2
+ DSB
+ ISB
+
+ /* set r0 as 0xC000_0000 for ATF OP code check */
+ MOV r0, #0xC0000000
+
+.globl WFI_LOOP
+.type WFI_LOOP, function
+WFI_LOOP:
+ /* enter WFI to request a warm reset */
+ WFI
+ B WFI_LOOP
+
+.globl jumparch64_slt
+.type jumparch64_slt, function
+jumparch64_slt:
+ /* setup the reset vector base address after warm reset to Aarch64 */
+ /* ldr r1,=RVBADDRESS_CPU0 */
+ /* ldr r1,[r1] */
+ /* LDR r0, =0x40000000 */
+ LDR r0, =0x40000000
+ LDR r1, =0x10200038
+ str r0,[r1]
+
+ /* setup the excution state after warm reset: 1:Aarch64, 0:Aarch32 */
+ MRC p15,0,r0,c12,c0,2
+ orr r0, r0, #1
+ MCR p15,0,r0,c12,c0,2
+ DSB
+ ISB
+
+ /* do warm reset:reset request */
+ MRC p15,0,r0,c12,c0,2
+ orr r0, r0, #2
+ MCR p15,0,r0,c12,c0,2
+ DSB
+ ISB
+
+ /* set r0 as 0x40000300 for dtb */
+ ldr r0, =0x40000300
+
+1:
+ /* enter WFI to request a warm reset */
+ WFI
+ B 1b
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/main.c b/src/bsp/trustzone/teeloader/mt8521/src/main.c
new file mode 100644
index 0000000..72f0b16
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/main.c
Binary files differ
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/print.c b/src/bsp/trustzone/teeloader/mt8521/src/print.c
new file mode 100644
index 0000000..4c8140a
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/print.c
@@ -0,0 +1,287 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#include "typedefs.h"
+#include "print.h"
+#include "dram_buffer.h"
+#include "uart.h"
+
+#define C_LOG_SRAM_BUF_SIZE (20480)
+static char log_sram_buf[C_LOG_SRAM_BUF_SIZE];
+
+#define LOG_BUFFER_MAX_SIZE (0x10000)
+#define log_dram_buf g_dram_buf->log_dram_buf
+
+char *log_ptr;
+char *log_hdr;
+char *log_end;
+static int g_log_drambuf = 1;
+static int g_log_disable = 0;
+
+static int g_log_miss_chrs = 0;
+
+static void outchar(const char c)
+{
+ if (g_log_disable) {
+ if (log_ptr < log_end)
+ *log_ptr++ = (char)c;
+ else
+ g_log_miss_chrs++;
+ } else {
+ uart_putc(c);
+ }
+}
+
+static void outstr(const unsigned char *s)
+{
+ while (*s) {
+ if (*s == '\n')
+ outchar('\r');
+ outchar(*s++);
+ }
+}
+
+static void outdec(unsigned long n)
+{
+ if (n >= 10) {
+ outdec(n / 10);
+ n %= 10;
+ }
+ outchar((unsigned char)(n + '0'));
+}
+
+static void outhex(unsigned long n, long depth)
+{
+ if (depth)
+ depth--;
+
+ if ((n & ~0xf) || depth) {
+ outhex(n >> 4, depth);
+ n &= 0xf;
+ }
+
+ if (n < 10) {
+ outchar((unsigned char)(n + '0'));
+ } else {
+ outchar((unsigned char)(n - 10 + 'A'));
+ }
+}
+
+void log_buf_ctrl(int drambuf)
+{
+ if (drambuf) {
+ if ((g_log_disable) && (!g_log_drambuf)) {
+ char *buf_ptr = log_hdr;
+ U32 buf_len = log_ptr - log_hdr;
+
+ log_hdr = (char *)log_dram_buf;
+ log_end = log_hdr + LOG_BUFFER_MAX_SIZE;
+ log_ptr = log_hdr;
+ if (buf_len) {
+ memcpy(log_hdr, buf_ptr, buf_len);
+ log_ptr = log_hdr + buf_len;
+ }
+ if (g_log_miss_chrs) {
+ outstr("\n{MISS: ");
+ outdec(g_log_miss_chrs);
+ outstr(" chars}\n");
+ g_log_miss_chrs = 0;
+ }
+ } else if (!g_log_disable) {
+ log_hdr = (char *)log_dram_buf;
+ log_end = log_hdr + LOG_BUFFER_MAX_SIZE;
+ log_ptr = log_hdr;
+ }
+ } else {
+ log_hdr = (char *)log_sram_buf;
+ log_end = log_hdr + C_LOG_SRAM_BUF_SIZE;
+ log_ptr = log_hdr;
+ }
+
+ g_log_drambuf = drambuf ? 1 : 0;
+}
+
+void log_ctrl(int enable)
+{
+ u32 len;
+ char *ptr;
+
+ g_log_disable = enable ? 0 : 1;
+
+ /* flush log and reset log buf ptr */
+ if (enable) {
+ ptr = (char *)log_hdr;
+ len = (u32) log_ptr - (u32) ptr;
+ for (; len; len--) {
+ //outchar(*ptr++);
+ }
+ log_ptr = log_hdr;
+ }
+}
+
+int log_status(void)
+{
+ return g_log_disable == 0 ? 1 : 0;
+}
+
+void dbg_print(char *fmt, ...)
+{
+ print(fmt);
+}
+
+void tl_vprint(char *fmt, va_list vl)
+{
+ unsigned char c;
+ unsigned int reg = 1; /* argument register number (32-bit) */
+
+ while (*fmt) {
+ c = *fmt++;
+ switch (c) {
+ case '%':
+ c = *fmt++;
+ switch (c) {
+ case 'x':
+ outhex(va_arg(vl, unsigned long), 0);
+ break;
+ case 'B':
+ outhex(va_arg(vl, unsigned long), 2);
+ break;
+ case 'H':
+ outhex(va_arg(vl, unsigned long), 4);
+ break;
+ case 'X':
+ outhex(va_arg(vl, unsigned long), 8);
+ break;
+ case 'l':
+ if (*fmt == 'l' && *(fmt + 1) == 'x') {
+ u32 ltmp;
+ u32 htmp;
+
+#ifdef __ARM_EABI__
+ /* Normally, compiler uses r0 to r6 to pass 32-bit or 64-bit
+ * arguments. But with EABI, 64-bit arguments will be aligned
+ * to an _even_ numbered register. for example:
+ *
+ * int foo(int a, long long b, int c)
+ *
+ * EABI: r0: a, r1: unused, r2-r3: b, r4: c
+ * Normal: r0: a, r1-r2: b, r3:c
+ *
+ * For this reason, need to align to even numbered register
+ * to retrieve 64-bit argument.
+ */
+
+ /* odd and unused argument */
+ if (reg & 0x1) {
+ /* 64-bit argument starts from next 32-bit register */
+ reg++;
+ /* ignore this 32-bit register */
+ ltmp = va_arg(vl, unsigned int);
+ }
+ reg++; /* 64-bit argument uses one more 32-bit register */
+#endif
+ ltmp = va_arg(vl, unsigned int);
+ htmp = va_arg(vl, unsigned int);
+
+ outhex(htmp, 8);
+ outhex(ltmp, 8);
+ fmt += 2;
+ }
+ break;
+ case 'd':
+ {
+ long l;
+
+ l = va_arg(vl, long);
+ if (l < 0) {
+ outchar('-');
+ l = -l;
+ }
+ outdec((unsigned long)l);
+ }
+ break;
+ case 'u':
+ outdec(va_arg(vl, unsigned long));
+ break;
+ case 's':
+ outstr((const unsigned char *)
+ va_arg(vl, char *));
+ break;
+ case '%':
+ outchar('%');
+ break;
+ case 'c':
+ c = va_arg(vl, int);
+ outchar(c);
+ break;
+ default:
+ outchar(' ');
+ break;
+ }
+ reg++; /* one argument uses 32-bit register */
+ break;
+ case '\r':
+ if (*fmt == '\n')
+ fmt++;
+ c = '\n';
+ // fall through
+ case '\n':
+ outchar('\r');
+ // fall through
+ default:
+ outchar(c);
+ }
+ }
+}
+
+void print(char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+
+ va_end(args);
+}
+
+void tl_print(char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ tl_vprint(fmt, args);
+ va_end(args);
+}
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/stdlib.c b/src/bsp/trustzone/teeloader/mt8521/src/stdlib.c
new file mode 100644
index 0000000..73c7d1f
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/stdlib.c
@@ -0,0 +1,164 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef NULL
+#define NULL 0
+#endif
+
+char *strchr(const char *p, int ch)
+{
+ for (;; ++p) {
+ if (*p == ch)
+ return ((char *)p);
+ if (!*p)
+ return ((char *)NULL);
+ }
+ /* NOTREACHED */
+}
+
+int atoi(const char *s)
+{
+ /* make digits[] size 4 bytes align */
+ static const char digits[12] = "0123456789"; /* legal digits in order */
+ unsigned val = 0; /* value we're accumulating */
+ int neg = 0; /* set to true if we see a minus sign */
+
+ /* skip whitespace */
+ while (*s == ' ' || *s == '\t') {
+ s++;
+ }
+
+ /* check for sign */
+ if (*s == '-') {
+ neg = 1;
+ s++;
+ } else if (*s == '+') {
+ s++;
+ }
+
+ /* process each digit */
+ while (*s) {
+ const char *where;
+ unsigned digit;
+
+ /* look for the digit in the list of digits */
+ where = strchr(digits, *s);
+ if (where == 0) {
+ /* not found; not a digit, so stop */
+ break;
+ }
+
+ /* get the index into the digit list, which is the value */
+ digit = (where - digits);
+
+ /* could (should?) check for overflow here */
+
+ /* shift the number over and add in the new digit */
+ val = val * 10 + digit;
+
+ /* look at the next character */
+ s++;
+ }
+
+ /* handle negative numbers */
+ if (neg) {
+ return -val;
+ }
+
+ /* done */
+ return val;
+}
+
+int isdigit(char c)
+{
+ return ((c >= '0') && (c <= '9'));
+}
+
+int isxdigit(char c)
+{
+ return isdigit(c) || ((c >= 'a') && (c <= 'f'))
+ || ((c >= 'A') && (c <= 'F'));
+}
+
+int hexval(char c)
+{
+ if ((c >= '0') && (c <= '9')) {
+ return c - '0';
+ }
+
+ if ((c >= 'a') && (c <= 'f')) {
+ return c - 'a' + 10;
+ }
+
+ if ((c >= 'A') && (c <= 'F')) {
+ return c - 'A' + 10;
+ }
+}
+
+long long atoll(const char *num)
+{
+ long long value = 0;
+ unsigned long long max;
+ int neg = 0;
+
+ if (num[0] == '0' && num[1] == 'x') {
+ // hex
+ num += 2;
+ while (*num && isxdigit(*num)) {
+ value = value * 16 + hexval(*num++);
+ }
+ } else {
+ // decimal
+ if (num[0] == '-') {
+ neg = 1;
+ num++;
+ }
+ while (*num && isdigit(*num))
+ value = value * 10 + *num++ - '0';
+ }
+
+ if (neg)
+ value = -value;
+
+ max = value;
+ return value;
+}
+
+void longjmperror(void)
+{
+ //ASSERT(0);
+}
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/string.c b/src/bsp/trustzone/teeloader/mt8521/src/string.c
new file mode 100644
index 0000000..aa455ad
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/string.c
@@ -0,0 +1,119 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+//---------------------------------------------------------------------------
+int strlen(const char *s)
+{
+ const char *sc;
+
+ for (sc = s; *sc != '\0'; ++sc) {
+ }
+ return sc - s;
+}
+
+//---------------------------------------------------------------------------
+
+//---------------------------------------------------------------------------
+int strcmp(const char *cs, const char *ct)
+{
+ signed char __res;
+
+ while (1) {
+ if ((__res = *cs - *ct++) != 0 || !*cs++)
+ break;
+ }
+ return __res;
+}
+
+//---------------------------------------------------------------------------
+
+//---------------------------------------------------------------------------
+int strncmp(const char *cs, const char *ct, int count)
+{
+ signed char __res = 0;
+
+ while (count) {
+ if ((__res = *cs - *ct++) != 0 || !*cs++)
+ break;
+ count--;
+ }
+ return __res;
+}
+
+//---------------------------------------------------------------------------
+
+//---------------------------------------------------------------------------
+void *memset(void *s, int c, int count)
+{
+ char *xs = s;
+
+ while (count--)
+ *xs++ = c;
+
+ return s;
+}
+
+//---------------------------------------------------------------------------
+
+//---------------------------------------------------------------------------
+void *memcpy(void *dest, const void *src, int count)
+{
+ char *tmp = dest;
+ const char *s = src;
+
+ while (count--)
+ *tmp++ = *s++;
+
+ return dest;
+}
+
+//---------------------------------------------------------------------------
+
+//---------------------------------------------------------------------------
+int memcmp(const void *cs, const void *ct, int count)
+{
+ const unsigned char *su1, *su2;
+ int res = 0;
+
+ for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
+ if ((res = *su1 - *su2) != 0)
+ break;
+
+ return res;
+}
+
+//---------------------------------------------------------------------------
diff --git a/src/bsp/trustzone/teeloader/mt8521/src/uart.c b/src/bsp/trustzone/teeloader/mt8521/src/uart.c
new file mode 100644
index 0000000..c42cb04
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/src/uart.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 MediaTek Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "uart.h"
+
+int uart_putc(char c)
+{
+ while (!(readl(UART_LSR(UART1_BASE)) & UART_LSR_THRE));
+
+ if (c == '\n')
+ writel((unsigned int)'\r', UART_THR(UART1_BASE));
+
+ writel((unsigned int)c, UART_THR(UART1_BASE));
+
+ return 0;
+}
diff --git a/src/bsp/trustzone/teeloader/mt8521/tllink.lds b/src/bsp/trustzone/teeloader/mt8521/tllink.lds
new file mode 100644
index 0000000..d55d335
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/tllink.lds
@@ -0,0 +1,51 @@
+OUTPUT_ARCH(arm)
+
+ENTRY(_start)
+
+SECTIONS {
+
+ . = %BASE_ADDR%;
+ .start ALIGN(4) : {
+ *(.text.start)
+ }
+
+ . = . + 0x01FC;
+ .rom_info ALIGN(4) : {
+ *(.data.rom_info)
+ }
+ .text ALIGN(4) : {
+ *(.text)
+ *(.text.*)
+ }
+ .rodata ALIGN(4) : {
+ *(.rodata)
+ *(.rodata.*)
+ }
+ .data ALIGN(4) : {
+ *(.data)
+ *(.data.*)
+ }
+ .got ALIGN(4) : {
+ *(.got)
+ *(.got.*)
+ }
+
+ . = %BASE_ADDR%-0x100000 ;
+ .bss ALIGN(16) : {
+ _bss_start = .;
+ *(.bss)
+ *(.bss.*)
+ *(COMMON)
+ /* make _bss_end as 4 bytes alignment */
+ . = ALIGN(4);
+ _bss_end = .;
+ }
+
+ .secbuf ALIGN(4) : {
+ _secbuf_start = .;
+ *(.secbuf)
+ _secbuf_end = .;
+ }
+
+}
+
diff --git a/src/bsp/trustzone/teeloader/mt8521/zero_padding.sh b/src/bsp/trustzone/teeloader/mt8521/zero_padding.sh
new file mode 100755
index 0000000..79b2c5b
--- /dev/null
+++ b/src/bsp/trustzone/teeloader/mt8521/zero_padding.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+FILE_PATH=$1
+ALIGNMENT=$2
+PADDING_SIZE=0
+
+FILE_SIZE=$(($(wc -c < "${FILE_PATH}")))
+REMAINDER=$((${FILE_SIZE} % ${ALIGNMENT}))
+FILE_DIR=$(dirname "${FILE_PATH}")
+if [ ${REMAINDER} -ne 0 ]; then
+ PADDING_SIZE=$((${ALIGNMENT} - ${REMAINDER}))
+ dd if=/dev/zero of=${FILE_DIR}/padding.txt bs=$PADDING_SIZE count=1
+ cat ${FILE_DIR}/padding.txt>>${FILE_PATH}
+ #rm ${FILE_DIR}/padding.txt
+fi