• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #include <securec.h>
13 #include <sys/mman.h>
14 #include <tee_log.h>
15 #include <tee_mem_mgmt_api.h>
16 #include <tee_defines.h>
17 #include <ipclib.h>
18 #include <stdlib.h>
19 #include <ta_framework.h>
20 #include <chip_info.h>
21 #include <tee_ext_api.h>
22 #include <tee_config.h>
23 #include <tee_ss_agent_api.h>
24 #include "huk_derive_takey.h"
25 #include "huk_get_deviceid.h"
26 #include "huk_service_msg.h"
27 #include <ipclib_hal.h>
28 #include <spawn_ext.h>
29 
30 #define MAGIC_STR_LEN               20
31 
32 #define WEAK __attribute__((weak))
33 
34 #define BSS_START_MAGIC 0x12345678
35 #define BSS_END_MAGIX   0x87654321
36 
37 typedef TEE_Result (*cmd_func)(const struct huk_srv_msg *msg, struct huk_srv_rsp *rsp,
38     uint32_t sndr_pid, const TEE_UUID *uuid);
39 
40 struct cmd_operate_config_s {
41     uint32_t cmd_id;
42     cmd_func operate_func;
43 };
44 
45 static const struct cmd_operate_config_s g_cmd_operate_config[] = {
46     { CMD_HUK_DERIVE_TAKEY,         huk_task_derive_takey },
47     { CMD_HUK_GET_DEVICEID,         huk_task_get_deviceid },
48 };
49 #define CMD_COUNT (sizeof(g_cmd_operate_config) / sizeof(g_cmd_operate_config[0]))
50 
handle_cmd(const struct huk_srv_msg * msg,cref_t msghdl,uint32_t sndr_pid,uint16_t msg_type,const TEE_UUID * uuid)51 static void handle_cmd(const struct huk_srv_msg *msg, cref_t msghdl, uint32_t sndr_pid,
52                        uint16_t msg_type, const TEE_UUID *uuid)
53 {
54     uint32_t cmd_id;
55     uint32_t self_pid;
56     int32_t rc;
57     struct huk_srv_rsp rsp;
58     uint32_t i;
59 
60     (void)memset_s(&rsp, sizeof(rsp), 0, sizeof(rsp));
61     rsp.data.ret = TEE_ERROR_GENERIC;
62     cmd_id = msg->header.send.msg_id;
63     self_pid = get_self_taskid();
64     if (self_pid < 0) {
65         tloge("huk service get self pid error\n");
66         rsp.data.ret = TEE_ERROR_GENERIC;
67         goto ret_flow;
68     }
69 
70     for (i = 0; i < CMD_COUNT; i++) {
71         if ((cmd_id != g_cmd_operate_config[i].cmd_id) || (g_cmd_operate_config[i].operate_func == NULL))
72             continue;
73         rsp.data.ret = g_cmd_operate_config[i].operate_func(msg, &rsp, sndr_pid, uuid);
74         if (rsp.data.ret != TEE_SUCCESS && rsp.data.ret != TEE_ERROR_NOT_SUPPORTED)
75             tloge("cmd 0x%x error, ret = 0x%x\n", cmd_id, rsp.data.ret);
76         break;
77     }
78     if (i == CMD_COUNT)
79         tloge("the cmd id 0x%x is not supported\n", cmd_id);
80 
81 ret_flow:
82     if (msg_type == MSG_TYPE_CALL) {
83         rc = ipc_msg_reply(msghdl, &rsp, sizeof(rsp));
84         if (rc != 0)
85             tloge("reply error 0x%x\n", rc);
86     }
87 }
88 
tee_task_entry(int init_build)89 __attribute__((visibility ("default"))) void tee_task_entry(int init_build)
90 {
91     (void)init_build;
92     struct huk_srv_msg msg;
93     spawn_uuid_t uuid;
94     cref_t ch = 0;
95     struct src_msginfo info = {0};
96     int32_t ret;
97 
98     (void)memset_s(&msg, sizeof(msg), 0, sizeof(msg));
99     cref_t msghdl = ipc_get_my_msghdl();
100     if (!check_ref_valid(msghdl)) {
101         tloge("Cannot create msg hdl, %x\n", (int)msghdl);
102         exit((int)msghdl);
103     }
104 
105     if (ipc_create_channel_native(HUK_PATH, &ch) != 0) {
106         tloge("create main thread native channel failed!\n");
107         exit(-1);
108     }
109 
110     while (1) {
111         ret = ipc_msg_receive(ch, &msg, sizeof(msg), msghdl, &info, -1);
112         if (ret < 0) {
113             tloge("huk service: message receive failed, %llx\n", ret);
114             continue;
115         }
116 
117         if (getuuid((pid_t)info.src_pid, &uuid) != 0)
118             tloge("huk service get uuid failed\n");
119 
120         if (info.src_pid == 0)
121             handle_cmd(&msg, msghdl, GLOBAL_HANDLE, info.msg_type, &(uuid.uuid));
122         else
123             handle_cmd(&msg, msghdl, (uint32_t)pid_to_taskid(info.src_tid, info.src_pid),
124                        info.msg_type, &(uuid.uuid));
125     }
126 
127     tloge("huk service abort!\n");
128 }
129