From 214933d0e73dcafaae948b72e919ecea8e1ef01b Mon Sep 17 00:00:00 2001 From: LVZE Date: Thu, 29 Jun 2023 07:53:40 +0000 Subject: [PATCH 1/8] add sdk case : tee_upgrade --- test/CA/tee_upgrade/Makefile | 15 ++ test/CA/tee_upgrade/upgrade.c | 286 ++++++++++++++++++++++++++++++++++ 2 files changed, 301 insertions(+) create mode 100644 test/CA/tee_upgrade/Makefile create mode 100644 test/CA/tee_upgrade/upgrade.c diff --git a/test/CA/tee_upgrade/Makefile b/test/CA/tee_upgrade/Makefile new file mode 100644 index 0000000..4e52a12 --- /dev/null +++ b/test/CA/tee_upgrade/Makefile @@ -0,0 +1,15 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved. +obj-m := tee_upgrade.o +tee_upgrade-objs := upgrade.o + +EXTRA_CFLAGS += -I$(PWD)/../itrustee/platform/libhwsecurec/include/libhwsecurec -I$(PWD)/../iTrustee/platform/libhwsecurec/include + +KPATH := /usr/src/kernels +KDIR := $(KPATH)/$(shell ls $(KPATH)) + +EXTRA_CFLAGS += -I$(PWD)/../tzdriver + +all: + make -C $(KDIR) M=$(PWD) modules +clean: + -rm -vrf *.order *.symvers *.mod.c .tmp_version .*o.cmd *.o \ No newline at end of file diff --git a/test/CA/tee_upgrade/upgrade.c b/test/CA/tee_upgrade/upgrade.c new file mode 100644 index 0000000..5af2119 --- /dev/null +++ b/test/CA/tee_upgrade/upgrade.c @@ -0,0 +1,286 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved. + * Licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Description: C file template for CA + */ + +#include "teek_client_api.h" +#include +#include +#include +#include + +#define PART_IAMGE_LEN 0x100000 +#define UPGRADE_SEND_IMAGE_BEGIN 0xaa +#define UPGRADE_SEND_IMAGE_UPDATE 0xbb +#define UPGRADE_SEND_IMAGE_FINISH 0xcc +#define MAX_IMAGE_LENGTH 0x1000000 +#ifndef CONFIG_TEE_IMG_PATH +#define CONFIG_TEE_IMG_PATH "/var/itrustee/image/trustedcore.img" +#endif + +/* array index */ +#define ARRAY_INDEX2 2 +#define ARRAY_INDEX3 3 + +extern int tee_reboot(void); + +static int32_t teek_open_app_file(struct file *fp, char **fileBuf, uint32_t total_img_len) +{ + loff_t pos = 0; + uint32_t read_size; + char *fileBuffer = NULL; + + if (total_img_len == 0 || total_img_len > MAX_IMAGE_LENGTH) { + tloge("img len is invalied, len=%u\n", total_img_len); + return TEEC_ERROR_BAD_PARAMETERS; + } + + fileBuffer = vmalloc(total_img_len); + if (ZERO_OR_NULL_PTR((unsigned long)(uintptr_t)fileBuffer)) { + tloge("alloc TA file buffer(size=%u) failed\n", total_img_len); + return TEEC_ERROR_GENERIC; + } + + read_size = (uint32_t)kernel_read(fp, fileBuffer, total_img_len, &pos); + if (read_size != total_img_len) { + tloge("read ta file failed, read size/total size=%u/%u\n", read_size, total_img_len); + vfree(fileBuffer); + return TEEC_ERROR_GENERIC; + } + + *fileBuf = fileBuffer; + + return TEEC_SUCCESS; +} + +static int32_t teek_read_app(const char *load_file, char **fileBuf, uint32_t *file_len) +{ + int32_t ret; + struct file *fp = NULL; + + fp = filp_open(load_file, O_RDONLY, 0); + if (!fp || IS_ERR(fp)) { + tloge("open file error, err=%ld\n", PTR_ERR(fp)); + return TEEC_ERROR_BAD_PARAMETERS; + } + + if (!fp->f_inode) { + tloge("node is NULL\n"); + filp_close(fp, 0); + return TEEC_ERROR_BAD_PARAMETERS; + } + + *file_len = (uint32_t)(fp->f_inode->i_size); + + ret = teek_open_app_file(fp, fileBuf, *file_len); + if (ret != TEEC_SUCCESS) { + tloge("do read app fail\n"); + } + + if (fp != NULL) { + filp_close(fp, 0); + fp = NULL; + } + + return ret; +} + +static void teek_free_app(bool load_app_flag, char **fileBuf) +{ + if (load_app_flag && fileBuf != NULL && ZERO_OR_NULL_PTR((unsigned long)(uintptr_t)(*fileBuf))) { + vfree(*fileBuf); + *fileBuf = NULL; + } +} + +static int32_t teek_get_app(const char *ta_path, char **fileBuf, uint32_t *file_len) +{ + int32_t ret; + + if (!ta_path) + return TEEC_ERROR_BAD_PARAMETERS; + + if (!fileBuf || !file_len) { + tloge("load app params invalied\n"); + return TEEC_ERROR_BAD_PARAMETERS; + } + + ret = teek_read_app(ta_path, fileBuf, file_len); + if (ret != TEEC_SUCCESS) + tloge("teec load app error, err=%d\n", ret); + + return ret; +} + +static int send_image_begin(TEEC_Session *tee_session, uint32_t image_length) +{ + uint32_t origin = 0; + TEEC_Operation operation = {0}; + int ret = 0; + + operation.started = 1; + operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_VALUE_INPUT); + operation.params[ARRAY_INDEX3].value.a = image_length; + + ret = TEEK_InvokeCommand(tee_session, UPGRADE_SEND_IMAGE_BEGIN, &operation, &origin); + if (ret != 0) { + tloge("TEEK_InvokeCommand failed\n"); + return -1; + } + return 0; +} + +static int send_image_update(TEEC_Session *tee_session, char *fileBuf, uint32_t image_length) +{ + uint32_t origin = 0; + uint32_t idx = 0; + uint32_t left_len = image_length; + int ret = 0; + + TEEC_Operation operation = {0}; + operation.started = 1; + operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE); + + while (left_len > 0) { + uint32_t len = (left_len >= PART_IAMGE_LEN) ? PART_IAMGE_LEN : left_len; + operation.params[ARRAY_INDEX2].tmpref.buffer = fileBuf + idx * PART_IAMGE_LEN; + operation.params[ARRAY_INDEX2].tmpref.size = len; + ret = TEEK_InvokeCommand(tee_session, UPGRADE_SEND_IMAGE_UPDATE, &operation, &origin); + if (ret != 0) { + tloge("send image update failed\n"); + return -1; + } + left_len -= len; + idx++; + } + return 0; +} + +static int send_image_finish(TEEC_Session *tee_session) +{ + uint32_t origin = 0; + int ret = 0; + TEEC_Operation operation = {0}; + operation.started = 1; + operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE); + + ret = TEEK_InvokeCommand(tee_session, UPGRADE_SEND_IMAGE_FINISH, &operation, &origin); + if (ret != 0) { + tloge("send image finish failed\n"); + return -1; + } + + return ret; +} + +static int get_new_tee_image(TEEC_Session *tee_session) +{ + char *fileBuf = NULL; + uint32_t image_length = 0; + + int ret = teek_get_app(CONFIG_TEE_IMG_PATH, &fileBuf, &image_length); + if (ret != 0) { + tloge("get new tee image failed, use origin tee\n"); + teek_free_app(true, &fileBuf); + return -1; + } + + ret = send_image_begin(tee_session, image_length); + if (ret != 0) { + tloge("send image begin failed\n"); + teek_free_app(true, &fileBuf); + return -1; + } + + ret = send_image_update(tee_session, fileBuf, image_length); + if (ret != 0) { + tloge("send image update failed\n"); + teek_free_app(true, &fileBuf); + return -1; + } + + ret = send_image_finish(tee_session); + if (ret != 0) { + tloge("send image end failed\n"); + teek_free_app(true, &fileBuf); + return -1; + } + + teek_free_app(true, &fileBuf); + return 0; +} + + +// 9ab6f960-54f3-4317-a8f7-e92ed12b6ae2.sec +static const TEEC_UUID g_tee_uuid = { + 0x9ab6f960U, 0x54f3, 0x4317, + { 0xa8, 0xf7, 0xe9, 0x2e, 0xd1, 0x2b, 0x6a, 0xe2 } +}; + +static int32_t __init upgrade_init(void) +{ + TEEC_Result ret; + TEEC_Context ctx; + TEEC_Operation operation = {0}; + TEEC_Session tee_session; + uint32_t origin = 0; + uint32_t root_id = 0; + + ret = TEEK_InitializeContext(NULL, &ctx); + if (ret != 0) { + tloge("initialize context failed\n"); + return -1; + } + + operation.started = 1; + operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_MEMREF_TEMP_INPUT, TEEC_MEMREF_TEMP_INPUT); + operation.params[ARRAY_INDEX2].tmpref.buffer = (void *)(&root_id); + operation.params[ARRAY_INDEX2].tmpref.size = sizeof(root_id); + operation.params[ARRAY_INDEX3].tmpref.buffer = (void *)("tee_upgrade"); + operation.params[ARRAY_INDEX3].tmpref.size = strlen("tee_upgrade") + 1; + + ctx.ta_path = (uint8_t *)("/var/itrustee/image/9ab6f960-54f3-4317-a8f7-e92ed12b6ae2.sec"); + ret = TEEK_OpenSession(&ctx, &tee_session, &g_tee_uuid, TEEC_LOGIN_IDENTIFY, NULL, &operation, &origin); + if (ret != 0) { + tloge("TEEK_OpenSession failed\n"); + TEEK_FinalizeContext(&ctx); + return -1; + } + + ret = get_new_tee_image(&tee_session); + if (ret != 0) { + TEEK_CloseSession(&tee_session); + TEEK_FinalizeContext(&ctx); + return -1; + } + + TEEK_CloseSession(&tee_session); + TEEK_FinalizeContext(&ctx); + ret = (TEEC_Result)tee_reboot(); + if (ret != TEEC_SUCCESS) + return -1; + tlogi("teeos upgrade done\n"); + return 0; +} + + +static void __exit upgrade_exit(void) +{ + tlogi("remove upgrade ca\n"); +} + +module_init(upgrade_init); +module_exit(upgrade_exit); + +MODULE_AUTHOR("Huawei Tech. Co., Ltd."); +MODULE_DESCRIPTION("TEE UPGRADE"); +MODULE_LICENSE("GPL"); +MODULE_VERSION("V1.0"); -- Gitee From 7e84c3deb6d8bf4195220311541630eb9c54a899 Mon Sep 17 00:00:00 2001 From: LVZE Date: Tue, 4 Jul 2023 02:04:11 +0000 Subject: [PATCH 2/8] update test/CA/tee_upgrade/Makefile. Signed-off-by: LVZE --- test/CA/tee_upgrade/Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/CA/tee_upgrade/Makefile b/test/CA/tee_upgrade/Makefile index 4e52a12..4168b23 100644 --- a/test/CA/tee_upgrade/Makefile +++ b/test/CA/tee_upgrade/Makefile @@ -2,7 +2,9 @@ obj-m := tee_upgrade.o tee_upgrade-objs := upgrade.o -EXTRA_CFLAGS += -I$(PWD)/../itrustee/platform/libhwsecurec/include/libhwsecurec -I$(PWD)/../iTrustee/platform/libhwsecurec/include +APP_CFLAGS += -I$(ITRUSTEE_BUILD_PATH)/thirdparty/open_source/libboundscheck/include + +APP_LDFLAGS += -lboundscheck KPATH := /usr/src/kernels KDIR := $(KPATH)/$(shell ls $(KPATH)) -- Gitee From 86c3d5a456d0f9be267f2752870a6c2e2f456812 Mon Sep 17 00:00:00 2001 From: LVZE Date: Tue, 4 Jul 2023 02:05:41 +0000 Subject: [PATCH 3/8] update test/CA/tee_upgrade/Makefile. Signed-off-by: LVZE --- test/CA/tee_upgrade/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/CA/tee_upgrade/Makefile b/test/CA/tee_upgrade/Makefile index 4168b23..945eef4 100644 --- a/test/CA/tee_upgrade/Makefile +++ b/test/CA/tee_upgrade/Makefile @@ -2,6 +2,8 @@ obj-m := tee_upgrade.o tee_upgrade-objs := upgrade.o +ITRUSTEE_BUILD_PATH=${CUR_DIR}/../../../ + APP_CFLAGS += -I$(ITRUSTEE_BUILD_PATH)/thirdparty/open_source/libboundscheck/include APP_LDFLAGS += -lboundscheck -- Gitee From b3d4f083b2322dd3dbd4d89217f1127e8f91ab4a Mon Sep 17 00:00:00 2001 From: LVZE Date: Tue, 4 Jul 2023 02:08:04 +0000 Subject: [PATCH 4/8] update test/CA/tee_upgrade/Makefile. Signed-off-by: LVZE --- test/CA/tee_upgrade/Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/CA/tee_upgrade/Makefile b/test/CA/tee_upgrade/Makefile index 945eef4..b259a98 100644 --- a/test/CA/tee_upgrade/Makefile +++ b/test/CA/tee_upgrade/Makefile @@ -4,13 +4,14 @@ tee_upgrade-objs := upgrade.o ITRUSTEE_BUILD_PATH=${CUR_DIR}/../../../ -APP_CFLAGS += -I$(ITRUSTEE_BUILD_PATH)/thirdparty/open_source/libboundscheck/include +EXTRA_CFLAGS += -I$(ITRUSTEE_BUILD_PATH)/thirdparty/open_source/libboundscheck/include -APP_LDFLAGS += -lboundscheck +EXTRA_LDFLAGS += -lboundscheck KPATH := /usr/src/kernels KDIR := $(KPATH)/$(shell ls $(KPATH)) +//The dynamic upgrade of tee depends on the tzdriver library. EXTRA_CFLAGS += -I$(PWD)/../tzdriver all: -- Gitee From 94d379f8ff0f29f7c92b9875ef4ebd1820e69cff Mon Sep 17 00:00:00 2001 From: LVZE Date: Tue, 4 Jul 2023 02:08:20 +0000 Subject: [PATCH 5/8] update test/CA/tee_upgrade/Makefile. Signed-off-by: LVZE --- test/CA/tee_upgrade/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CA/tee_upgrade/Makefile b/test/CA/tee_upgrade/Makefile index b259a98..c0d4612 100644 --- a/test/CA/tee_upgrade/Makefile +++ b/test/CA/tee_upgrade/Makefile @@ -11,7 +11,7 @@ EXTRA_LDFLAGS += -lboundscheck KPATH := /usr/src/kernels KDIR := $(KPATH)/$(shell ls $(KPATH)) -//The dynamic upgrade of tee depends on the tzdriver library. +#The dynamic upgrade of tee depends on the tzdriver library. EXTRA_CFLAGS += -I$(PWD)/../tzdriver all: -- Gitee From 66288248b724ab4f1d4590ec1a963005861849c6 Mon Sep 17 00:00:00 2001 From: LVZE Date: Tue, 4 Jul 2023 02:10:41 +0000 Subject: [PATCH 6/8] update test/CA/tee_upgrade/Makefile. Signed-off-by: LVZE --- test/CA/tee_upgrade/Makefile | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/CA/tee_upgrade/Makefile b/test/CA/tee_upgrade/Makefile index c0d4612..3c0ec18 100644 --- a/test/CA/tee_upgrade/Makefile +++ b/test/CA/tee_upgrade/Makefile @@ -6,8 +6,6 @@ ITRUSTEE_BUILD_PATH=${CUR_DIR}/../../../ EXTRA_CFLAGS += -I$(ITRUSTEE_BUILD_PATH)/thirdparty/open_source/libboundscheck/include -EXTRA_LDFLAGS += -lboundscheck - KPATH := /usr/src/kernels KDIR := $(KPATH)/$(shell ls $(KPATH)) -- Gitee From 4d60f86546034e7d8ab08c6a619ec6dae3f52ba2 Mon Sep 17 00:00:00 2001 From: LVZE Date: Tue, 4 Jul 2023 02:27:10 +0000 Subject: [PATCH 7/8] update test/CA/tee_upgrade/Makefile. Signed-off-by: LVZE --- test/CA/tee_upgrade/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/test/CA/tee_upgrade/Makefile b/test/CA/tee_upgrade/Makefile index 3c0ec18..2bbd625 100644 --- a/test/CA/tee_upgrade/Makefile +++ b/test/CA/tee_upgrade/Makefile @@ -2,6 +2,7 @@ obj-m := tee_upgrade.o tee_upgrade-objs := upgrade.o +CUR_DIR=$(shell pwd) ITRUSTEE_BUILD_PATH=${CUR_DIR}/../../../ EXTRA_CFLAGS += -I$(ITRUSTEE_BUILD_PATH)/thirdparty/open_source/libboundscheck/include -- Gitee From ec7880ea5a97f6bbfedde9e31549fdf0d7862c1d Mon Sep 17 00:00:00 2001 From: LVZE Date: Tue, 4 Jul 2023 02:47:06 +0000 Subject: [PATCH 8/8] update test/CA/tee_upgrade/Makefile. Signed-off-by: LVZE --- test/CA/tee_upgrade/Makefile | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/CA/tee_upgrade/Makefile b/test/CA/tee_upgrade/Makefile index 2bbd625..34fdfa0 100644 --- a/test/CA/tee_upgrade/Makefile +++ b/test/CA/tee_upgrade/Makefile @@ -2,10 +2,7 @@ obj-m := tee_upgrade.o tee_upgrade-objs := upgrade.o -CUR_DIR=$(shell pwd) -ITRUSTEE_BUILD_PATH=${CUR_DIR}/../../../ - -EXTRA_CFLAGS += -I$(ITRUSTEE_BUILD_PATH)/thirdparty/open_source/libboundscheck/include +EXTRA_CFLAGS += -I$(PWD)/../../../thirdparty/open_source/libboundscheck/include KPATH := /usr/src/kernels KDIR := $(KPATH)/$(shell ls $(KPATH)) -- Gitee