hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * Copyright 2017, Rockchip Electronics Co., Ltd
 * hisping lin, <hisping.lin@rock-chips.com>
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
#include <common.h>
#include <optee_include/OpteeClientMem.h>
#include <optee_include/OpteeClientSMC.h>
#include <optee_include/OpteeClientRPC.h>
#include <optee_include/teesmc.h>
#include <optee_include/teesmc_v2.h>
 
 
#define TEEC_SMC_DEFAULT_CACHE_ATTRIBUTES \
   (TEESMC_ATTR_CACHE_DEFAULT << TEESMC_ATTR_CACHE_SHIFT);
 
static void SetTeeSmc32Params(TEEC_Operation *operation,
   t_teesmc32_param *TeeSmc32Param);
static void GetTeeSmc32Params(t_teesmc32_param *TeeSmc32Param,
   TEEC_Operation *operation);
static TEEC_Result OpteeSmcCall(t_teesmc32_arg *TeeSmc32Arg);
 
void tee_uuid_to_octets(uint8_t *d, const TEEC_UUID *s)
{
   d[0] = s->timeLow >> 24;
   d[1] = s->timeLow >> 16;
   d[2] = s->timeLow >> 8;
   d[3] = s->timeLow;
   d[4] = s->timeMid >> 8;
   d[5] = s->timeMid;
   d[6] = s->timeHiAndVersion >> 8;
   d[7] = s->timeHiAndVersion;
   memcpy(d + 8, s->clockSeqAndNode, sizeof(s->clockSeqAndNode));
}
 
/*
 * This function opens a new Session between the Client application and the
 * specified TEE application.
 *
 * Only connection_method == TEEC_LOGIN_PUBLIC is supported connection_data and
 * operation shall be set to NULL.
 */
TEEC_Result TEEC_SMC_OpenSession(TEEC_Context *context,
               TEEC_Session *session,
               const TEEC_UUID *destination,
               TEEC_Operation  *operation,
               uint32_t *error_origin)
{
   TEEC_Result TeecResult = TEEC_SUCCESS;
   uint32_t TeeSmc32ArgLength;
   uint32_t TeeSmcMetaSessionLength;
 
   t_teesmc32_arg *TeeSmc32Arg = NULL;
   t_teesmc32_param *TeeSmc32Param = NULL;
 
   t_teesmc_meta_open_session *TeeSmcMetaSession = NULL;
 
   uint32_t MetaNum = 2;
 
   *error_origin = TEEC_ORIGIN_API;
 
   TeeSmc32ArgLength =
       TEESMC32_GET_ARG_SIZE(TEEC_CONFIG_PAYLOAD_REF_COUNT + MetaNum);
 
   TeeSmc32Arg = (t_teesmc32_arg *)OpteeClientMemAlloc(TeeSmc32ArgLength);
 
   if (TeeSmc32Arg == NULL) {
       TeecResult = TEEC_ERROR_OUT_OF_MEMORY;
       goto Exit;
   }
 
   memset(TeeSmc32Arg, 0, TeeSmc32ArgLength);
 
   TeeSmcMetaSessionLength = sizeof(*TeeSmcMetaSession);
 
   TeeSmcMetaSession = (t_teesmc_meta_open_session *)
       OpteeClientMemAlloc(TeeSmcMetaSessionLength);
 
   if (TeeSmcMetaSession == NULL) {
       TeecResult = TEEC_ERROR_OUT_OF_MEMORY;
       goto Exit;
   }
 
   memset(TeeSmcMetaSession, 0, TeeSmcMetaSessionLength);
 
   TeeSmc32Arg->cmd = TEESMC_CMD_OPEN_SESSION;
   TeeSmc32Arg->num_params = TEEC_CONFIG_PAYLOAD_REF_COUNT + MetaNum;
 
   TeeSmc32Param = TEESMC32_GET_PARAMS(TeeSmc32Arg);
 
   memcpy(&TeeSmcMetaSession->uuid,
       destination,
       sizeof(TeeSmcMetaSession->uuid));
   TeeSmcMetaSession->clnt_login = TEEC_LOGIN_PUBLIC;
 
   TeeSmc32Param[0].u.memref.buf_ptr = (uint32_t) (size_t)TeeSmcMetaSession;
   TeeSmc32Param[0].u.memref.size = sizeof(*TeeSmcMetaSession);
 
#ifdef CONFIG_OPTEE_V1
   memcpy((void *)&TeeSmc32Param[0].u.value, &TeeSmcMetaSession->uuid, sizeof(TeeSmcMetaSession->uuid));
#endif
 
#ifdef CONFIG_OPTEE_V2
   uint8_t * session_uuid = (uint8_t *)&TeeSmcMetaSession->uuid;
   tee_uuid_to_octets(session_uuid, destination);
   memcpy((void *)&TeeSmc32Param[0].u.value, &TeeSmcMetaSession->uuid, sizeof(TeeSmcMetaSession->uuid));
#endif
   TeeSmc32Param[1].u.value.c = TeeSmcMetaSession->clnt_login;
 
   TeeSmc32Param[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT_V2 |
               OPTEE_MSG_ATTR_META_V2;
   TeeSmc32Param[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT_V2 |
               OPTEE_MSG_ATTR_META_V2;
 
   SetTeeSmc32Params(operation, TeeSmc32Param + MetaNum);
 
   *error_origin = TEEC_ORIGIN_COMMS;
 
   TeecResult = OpteeSmcCall(TeeSmc32Arg);
   if (TeecResult != TEEC_SUCCESS)
       goto Exit;
 
   session->id = TeeSmc32Arg->session;
   TeecResult = TeeSmc32Arg->ret;
   *error_origin = TeeSmc32Arg->ret_origin;
 
   GetTeeSmc32Params(TeeSmc32Param + MetaNum, operation);
 
Exit:
   if (TeeSmc32Arg != NULL)
       OpteeClientMemFree(TeeSmc32Arg);
 
   if (TeeSmcMetaSession != NULL)
       OpteeClientMemFree(TeeSmcMetaSession);
 
   return TeecResult;
}
 
/*
 * This function closes a session which has been opened with a TEE
 * application.
 *
 * Note that the GP specification does not allow for this API to fail and return
 * a failure code however we'll support this at the SMC level so we can get
 * see debug information about such failures.
 */
TEEC_Result TEEC_SMC_CloseSession(TEEC_Session *session,
               uint32_t *error_origin)
{
   TEEC_Result TeecResult = TEEC_SUCCESS;
   uint32_t TeeSmc32ArgLength;
 
   t_teesmc32_arg *TeeSmc32Arg = NULL;
 
   *error_origin = TEEC_ORIGIN_API;
 
   TeeSmc32ArgLength =
       TEESMC32_GET_ARG_SIZE(TEEC_CONFIG_PAYLOAD_REF_COUNT);
 
   TeeSmc32Arg = (t_teesmc32_arg *)OpteeClientMemAlloc(TeeSmc32ArgLength);
 
   if (TeeSmc32Arg == NULL) {
       TeecResult = TEEC_ERROR_OUT_OF_MEMORY;
       goto Exit;
   }
 
   memset(TeeSmc32Arg, 0, TeeSmc32ArgLength);
 
   TeeSmc32Arg->cmd = TEESMC_CMD_CLOSE_SESSION;
   TeeSmc32Arg->session = session->id;
 
   *error_origin = TEEC_ORIGIN_COMMS;
 
   TeecResult = OpteeSmcCall(TeeSmc32Arg);
 
   if (TeecResult != TEEC_SUCCESS)
       goto Exit;
 
   TeecResult = TeeSmc32Arg->ret;
   *error_origin = TeeSmc32Arg->ret_origin;
 
Exit:
   if (TeeSmc32Arg != NULL)
       OpteeClientMemFree(TeeSmc32Arg);
 
   return TeecResult;
}
 
/*
 * Invokes a TEE command (secure service, sub-PA or whatever).
 */
TEEC_Result TEEC_SMC_InvokeCommand(TEEC_Session *session,
               uint32_t cmd_id,
               TEEC_Operation *operation,
               uint32_t *error_origin)
{
   TEEC_Result TeecResult = TEEC_SUCCESS;
   uint32_t TeeSmc32ArgLength;
 
   t_teesmc32_arg *TeeSmc32Arg = NULL;
   t_teesmc32_param *TeeSmc32Param = NULL;
 
   *error_origin = TEEC_ORIGIN_API;
 
   TeeSmc32ArgLength =
       TEESMC32_GET_ARG_SIZE(TEEC_CONFIG_PAYLOAD_REF_COUNT);
 
   TeeSmc32Arg = (t_teesmc32_arg *)OpteeClientMemAlloc(TeeSmc32ArgLength);
 
   if (TeeSmc32Arg == NULL) {
       TeecResult = TEEC_ERROR_OUT_OF_MEMORY;
       goto Exit;
   }
 
   memset(TeeSmc32Arg, 0, TeeSmc32ArgLength);
 
   TeeSmc32Arg->cmd = TEESMC_CMD_INVOKE_COMMAND;
   TeeSmc32Arg->ta_func = cmd_id;
   TeeSmc32Arg->session = session->id;
   TeeSmc32Arg->num_params = TEEC_CONFIG_PAYLOAD_REF_COUNT;
 
   TeeSmc32Param = TEESMC32_GET_PARAMS(TeeSmc32Arg);
 
   SetTeeSmc32Params(operation, TeeSmc32Param);
 
   *error_origin = TEEC_ORIGIN_COMMS;
 
   TeecResult = OpteeSmcCall(TeeSmc32Arg);
   if (TeecResult != TEEC_SUCCESS)
       goto Exit;
 
   TeecResult = TeeSmc32Arg->ret;
   *error_origin = TeeSmc32Arg->ret_origin;
 
   GetTeeSmc32Params(TeeSmc32Param, operation);
 
Exit:
   if (TeeSmc32Arg != NULL)
       OpteeClientMemFree(TeeSmc32Arg);
 
 
   return TeecResult;
}
 
/*
 * Request a cancellation of a in-progress operation (best effort)
 *
 * Note that the GP specification does not allow for this API to fail and return
 * a failure code however we'll support this at the SMC level so we can get
 * see debug information about such failures.
 */
TEEC_Result TEEC_SMC_RequestCancellation(TEEC_Operation *operation,
                   uint32_t *error_origin)
{
   return TEEC_ERROR_NOT_IMPLEMENTED;
}
 
/*
 * Set the call parameter blocks in the
 * SMC call based on the TEEC parameter supplied.
 * This only handles the parameters supplied in
 * the originating call and not those
 * considered internal meta parameters and is
 * thus constrained by the build
 * constants exposed to callers.
 */
void SetTeeSmc32Params(TEEC_Operation *operation,
                       t_teesmc32_param *TeeSmc32Param)
{
   uint32_t ParamCount;
 
   for (ParamCount = 0;
       ParamCount < TEEC_CONFIG_PAYLOAD_REF_COUNT;
       ParamCount++) {
       uint32_t attr =
           TEEC_PARAM_TYPE_GET(operation->paramTypes, ParamCount);
 
       if (attr == TEEC_MEMREF_TEMP_INPUT ||
           attr == TEEC_MEMREF_TEMP_OUTPUT ||
           attr == TEEC_MEMREF_TEMP_INOUT) {
 
           attr += (OPTEE_MSG_ATTR_TYPE_TMEM_INPUT_V2 - TEEC_MEMREF_TEMP_INPUT);
           debug("TEEC: OPTEE_OS_V2 ARCH64 attr %x\n", attr);
 
           TeeSmc32Param[ParamCount].attr = attr;
           TeeSmc32Param[ParamCount].u.memref.buf_ptr =
           (uint32_t)(size_t)operation->params[ParamCount].tmpref.buffer;
           TeeSmc32Param[ParamCount].u.memref.size =
               operation->params[ParamCount].tmpref.size;
       } else {
           TeeSmc32Param[ParamCount].attr = attr;
           TeeSmc32Param[ParamCount].u.value.a =
               operation->params[ParamCount].value.a;
           TeeSmc32Param[ParamCount].u.value.b =
               operation->params[ParamCount].value.b;
       }
   }
}
 
/*
 * Get the return parameter blocks from
 * the SMC call into the TEEC parameter supplied.
 * This only handles the parameters supplied
 * in the originating call and not those
 * considered internal meta parameters and
 * is thus constrained by the build
 * constants exposed to callers.
 */
void GetTeeSmc32Params(t_teesmc32_param *TeeSmc32Param,
               TEEC_Operation *operation)
{
   uint32_t ParamCount;
 
   for (ParamCount = 0;
   ParamCount < TEEC_CONFIG_PAYLOAD_REF_COUNT;
   ParamCount++) {
       operation->params[ParamCount].value.a =
           TeeSmc32Param[ParamCount].u.value.a;
       operation->params[ParamCount].value.b =
           TeeSmc32Param[ParamCount].u.value.b;
   }
}
 
/*
 * Populate the SMC registers and make
 * the call with OpTEE specific handling.
 */
TEEC_Result OpteeSmcCall(t_teesmc32_arg *TeeSmc32Arg)
{
   TEEC_Result TeecResult = TEEC_SUCCESS;
   ARM_SMC_ARGS ArmSmcArgs = {0};
 
   ArmSmcArgs.Arg0 = OPTEE_SMC_CALL_WITH_ARG_V2;
   ArmSmcArgs.Arg1 = 0;
   ArmSmcArgs.Arg2 = (uint32_t) (size_t)TeeSmc32Arg;
 
   while (1) {
       tee_smc_call(&ArmSmcArgs);
       debug("TEEC: arg0=0x%x arg1=0x%x arg2=0x%x arg3=0x%x \n",
           ArmSmcArgs.Arg0, ArmSmcArgs.Arg1, ArmSmcArgs.Arg2, ArmSmcArgs.Arg3);
       if (TEESMC_RETURN_IS_RPC(ArmSmcArgs.Arg0)) {
           (void) OpteeRpcCallback(&ArmSmcArgs);
       } else if (ArmSmcArgs.Arg0 == TEESMC_RETURN_UNKNOWN_FUNCTION) {
           TeecResult = TEEC_ERROR_NOT_IMPLEMENTED;
           break;
       } else if (ArmSmcArgs.Arg0 != TEESMC_RETURN_OK) {
           TeecResult = TEEC_ERROR_COMMUNICATION;
           break;
       } else {
           TeecResult = TEEC_SUCCESS;
           break;
       }
   }
 
   return TeecResult;
}