blob: 86b16a3bb8d46cb43ed1d70b28c567c88448df73 [file] [log] [blame]
Jens Wiklanderc7b443a2018-09-25 16:40:18 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Linaro Limited
4 */
Igor Opaniuk4518a4d2024-04-04 15:19:51 +02005
Jens Wiklanderc7b443a2018-09-25 16:40:18 +02006#include <dm.h>
7#include <sandboxtee.h>
8#include <tee.h>
9#include <tee/optee_ta_avb.h>
Igor Opaniuk84643c12021-01-25 14:28:42 +020010#include <tee/optee_ta_rpc_test.h>
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +010011#include <tee/optee_ta_scp03.h>
Igor Opaniuk84643c12021-01-25 14:28:42 +020012
13#include "optee/optee_msg.h"
14#include "optee/optee_private.h"
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020015
16/*
Igor Opaniukd0fdd5a2024-03-04 18:44:50 +010017 * The sandbox tee driver tries to emulate a generic Trusted Execution
Igor Opaniuk84643c12021-01-25 14:28:42 +020018 * Environment (TEE) with the Trusted Applications (TA) OPTEE_TA_AVB and
19 * OPTEE_TA_RPC_TEST available.
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020020 */
21
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +020022static const u32 pstorage_max = 16;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020023/**
24 * struct ta_entry - TA entries
25 * @uuid: UUID of an emulated TA
Igor Opaniukd0fdd5a2024-03-04 18:44:50 +010026 * @open_session Called when a session is opened to the TA
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020027 * @invoke_func Called when a function in the TA is to be invoked
28 *
29 * This struct is used to register TAs in this sandbox emulation of a TEE.
30 */
31struct ta_entry {
32 struct tee_optee_ta_uuid uuid;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +020033 u32 (*open_session)(struct udevice *dev, uint num_params,
34 struct tee_param *params);
35 u32 (*invoke_func)(struct udevice *dev,
36 u32 func, uint num_params,
37 struct tee_param *params);
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020038};
39
Igor Opaniuk84643c12021-01-25 14:28:42 +020040static int get_msg_arg(struct udevice *dev, uint num_params,
41 struct tee_shm **shmp, struct optee_msg_arg **msg_arg)
42{
43 int rc;
44 struct optee_msg_arg *ma;
45
46 rc = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
47 OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC,
48 shmp);
49 if (rc)
50 return rc;
51
52 ma = (*shmp)->addr;
53 memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
54 ma->num_params = num_params;
55 *msg_arg = ma;
56
57 return 0;
58}
59
60void *optee_alloc_and_init_page_list(void *buf, ulong len,
61 u64 *phys_buf_ptr)
62{
63 /*
64 * An empty stub is added just to fix linking issues.
65 * This function isn't supposed to be called in sandbox
66 * setup, otherwise replace this with a proper
67 * implementation from optee/core.c
68 */
69 return NULL;
70}
71
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +010072#if defined(CONFIG_OPTEE_TA_SCP03) || defined(CONFIG_OPTEE_TA_AVB)
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020073static u32 get_attr(uint n, uint num_params, struct tee_param *params)
74{
75 if (n >= num_params)
76 return TEE_PARAM_ATTR_TYPE_NONE;
77
78 return params[n].attr;
79}
80
81static u32 check_params(u8 p0, u8 p1, u8 p2, u8 p3, uint num_params,
82 struct tee_param *params)
83{
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +010084 u8 p[] = { p0, p1, p2, p3 };
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020085 uint n;
86
87 for (n = 0; n < ARRAY_SIZE(p); n++)
88 if (p[n] != get_attr(n, num_params, params))
89 goto bad_params;
90
91 for (; n < num_params; n++)
92 if (get_attr(n, num_params, params))
93 goto bad_params;
94
95 return TEE_SUCCESS;
96
97bad_params:
98 printf("Bad param attrs\n");
99
100 return TEE_ERROR_BAD_PARAMETERS;
101}
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +0100102#endif
103
104#ifdef CONFIG_OPTEE_TA_SCP03
105static u32 pta_scp03_open_session(struct udevice *dev, uint num_params,
106 struct tee_param *params)
107{
108 /*
109 * We don't expect additional parameters when opening a session to
110 * this TA.
111 */
112 return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
113 TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
114 num_params, params);
115}
116
117static u32 pta_scp03_invoke_func(struct udevice *dev, u32 func, uint num_params,
118 struct tee_param *params)
119{
120 u32 res;
121 static bool enabled;
Jorge Ramirez-Ortiz5fbd4232023-04-08 17:35:31 +0200122 static bool provisioned;
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +0100123
124 switch (func) {
125 case PTA_CMD_ENABLE_SCP03:
126 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
127 TEE_PARAM_ATTR_TYPE_NONE,
128 TEE_PARAM_ATTR_TYPE_NONE,
129 TEE_PARAM_ATTR_TYPE_NONE,
130 num_params, params);
131 if (res)
132 return res;
133
Jorge Ramirez-Ortiz5fbd4232023-04-08 17:35:31 +0200134 /* If SCP03 was not enabled, enable it */
135 if (!enabled)
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +0100136 enabled = true;
Jorge Ramirez-Ortiz5fbd4232023-04-08 17:35:31 +0200137
138 /* If SCP03 was not provisioned, provision new keys */
139 if (params[0].u.value.a && !provisioned)
140 provisioned = true;
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +0100141
Jorge Ramirez-Ortiz5fbd4232023-04-08 17:35:31 +0200142 /*
Igor Opaniukd0fdd5a2024-03-04 18:44:50 +0100143 * Either way, we assume both operations succeeded and that
144 * the communication channel has now been established
Jorge Ramirez-Ortiz5fbd4232023-04-08 17:35:31 +0200145 */
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +0100146
147 return TEE_SUCCESS;
148 default:
149 return TEE_ERROR_NOT_SUPPORTED;
150 }
151}
152#endif
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200153
Igor Opaniuk84643c12021-01-25 14:28:42 +0200154#ifdef CONFIG_OPTEE_TA_AVB
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200155static u32 ta_avb_open_session(struct udevice *dev, uint num_params,
156 struct tee_param *params)
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200157{
158 /*
159 * We don't expect additional parameters when opening a session to
160 * this TA.
161 */
162 return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
163 TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
164 num_params, params);
165}
166
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200167static u32 ta_avb_invoke_func(struct udevice *dev, u32 func, uint num_params,
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200168 struct tee_param *params)
169{
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200170 struct sandbox_tee_state *state = dev_get_priv(dev);
Simon Glass1a236862019-08-02 09:44:18 -0600171 struct env_entry e, *ep;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200172 char *name;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200173 u32 res;
174 uint slot;
175 u64 val;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200176 char *value;
Igor Opaniukae7ddc32024-04-21 22:48:39 +0200177 u32 value_sz, tmp_sz;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200178
179 switch (func) {
180 case TA_AVB_CMD_READ_ROLLBACK_INDEX:
181 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
182 TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT,
183 TEE_PARAM_ATTR_TYPE_NONE,
184 TEE_PARAM_ATTR_TYPE_NONE,
185 num_params, params);
186 if (res)
187 return res;
188
189 slot = params[0].u.value.a;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200190 if (slot >= ARRAY_SIZE(state->ta_avb_rollback_indexes)) {
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200191 printf("Rollback index slot out of bounds %u\n", slot);
192 return TEE_ERROR_BAD_PARAMETERS;
193 }
194
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200195 val = state->ta_avb_rollback_indexes[slot];
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200196 params[1].u.value.a = val >> 32;
197 params[1].u.value.b = val;
198 return TEE_SUCCESS;
199
200 case TA_AVB_CMD_WRITE_ROLLBACK_INDEX:
201 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
202 TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
203 TEE_PARAM_ATTR_TYPE_NONE,
204 TEE_PARAM_ATTR_TYPE_NONE,
205 num_params, params);
206 if (res)
207 return res;
208
209 slot = params[0].u.value.a;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200210 if (slot >= ARRAY_SIZE(state->ta_avb_rollback_indexes)) {
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200211 printf("Rollback index slot out of bounds %u\n", slot);
212 return TEE_ERROR_BAD_PARAMETERS;
213 }
214
215 val = (u64)params[1].u.value.a << 32 | params[1].u.value.b;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200216 if (val < state->ta_avb_rollback_indexes[slot])
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200217 return TEE_ERROR_SECURITY;
218
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200219 state->ta_avb_rollback_indexes[slot] = val;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200220 return TEE_SUCCESS;
221
222 case TA_AVB_CMD_READ_LOCK_STATE:
223 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT,
224 TEE_PARAM_ATTR_TYPE_NONE,
225 TEE_PARAM_ATTR_TYPE_NONE,
226 TEE_PARAM_ATTR_TYPE_NONE,
227 num_params, params);
228 if (res)
229 return res;
230
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200231 params[0].u.value.a = state->ta_avb_lock_state;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200232 return TEE_SUCCESS;
233
234 case TA_AVB_CMD_WRITE_LOCK_STATE:
235 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
236 TEE_PARAM_ATTR_TYPE_NONE,
237 TEE_PARAM_ATTR_TYPE_NONE,
238 TEE_PARAM_ATTR_TYPE_NONE,
239 num_params, params);
240 if (res)
241 return res;
242
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200243 if (state->ta_avb_lock_state != params[0].u.value.a) {
244 state->ta_avb_lock_state = params[0].u.value.a;
245 memset(state->ta_avb_rollback_indexes, 0,
246 sizeof(state->ta_avb_rollback_indexes));
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200247 }
248
249 return TEE_SUCCESS;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200250 case TA_AVB_CMD_READ_PERSIST_VALUE:
251 res = check_params(TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
252 TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
253 TEE_PARAM_ATTR_TYPE_NONE,
254 TEE_PARAM_ATTR_TYPE_NONE,
255 num_params, params);
256 if (res)
257 return res;
258
259 name = params[0].u.memref.shm->addr;
260
261 value = params[1].u.memref.shm->addr;
262 value_sz = params[1].u.memref.size;
263
264 e.key = name;
265 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600266 hsearch_r(e, ENV_FIND, &ep, &state->pstorage_htab, 0);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200267 if (!ep)
268 return TEE_ERROR_ITEM_NOT_FOUND;
269
Igor Opaniukae7ddc32024-04-21 22:48:39 +0200270 tmp_sz = strlen(ep->data) + 1;
271 if (value_sz < tmp_sz)
272 return TEE_ERROR_SHORT_BUFFER;
273
274 memcpy(value, ep->data, tmp_sz);
275 params[1].u.memref.size = tmp_sz;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200276
277 return TEE_SUCCESS;
278 case TA_AVB_CMD_WRITE_PERSIST_VALUE:
279 res = check_params(TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
280 TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
281 TEE_PARAM_ATTR_TYPE_NONE,
282 TEE_PARAM_ATTR_TYPE_NONE,
283 num_params, params);
284 if (res)
285 return res;
286
287 name = params[0].u.memref.shm->addr;
288
289 value = params[1].u.memref.shm->addr;
290 value_sz = params[1].u.memref.size;
291
292 e.key = name;
293 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600294 hsearch_r(e, ENV_FIND, &ep, &state->pstorage_htab, 0);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200295 if (ep)
296 hdelete_r(e.key, &state->pstorage_htab, 0);
297
298 e.key = name;
299 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600300 hsearch_r(e, ENV_ENTER, &ep, &state->pstorage_htab, 0);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200301 if (!ep)
302 return TEE_ERROR_OUT_OF_MEMORY;
303
304 return TEE_SUCCESS;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200305
306 default:
307 return TEE_ERROR_NOT_SUPPORTED;
308 }
309}
Igor Opaniuk84643c12021-01-25 14:28:42 +0200310#endif /* OPTEE_TA_AVB */
311
312#ifdef CONFIG_OPTEE_TA_RPC_TEST
313static u32 ta_rpc_test_open_session(struct udevice *dev, uint num_params,
314 struct tee_param *params)
315{
316 /*
317 * We don't expect additional parameters when opening a session to
318 * this TA.
319 */
320 return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
321 TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
322 num_params, params);
323}
324
325static void fill_i2c_rpc_params(struct optee_msg_arg *msg_arg, u64 bus_num,
326 u64 chip_addr, u64 xfer_flags, u64 op,
327 struct tee_param_memref memref)
328{
329 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
330 msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
331 msg_arg->params[2].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INOUT;
332 msg_arg->params[3].attr = OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT;
333
334 /* trigger I2C services of TEE supplicant */
335 msg_arg->cmd = OPTEE_MSG_RPC_CMD_I2C_TRANSFER;
336
337 msg_arg->params[0].u.value.a = op;
338 msg_arg->params[0].u.value.b = bus_num;
339 msg_arg->params[0].u.value.c = chip_addr;
340 msg_arg->params[1].u.value.a = xfer_flags;
341
342 /* buffer to read/write data */
343 msg_arg->params[2].u.rmem.shm_ref = (ulong)memref.shm;
344 msg_arg->params[2].u.rmem.size = memref.size;
345 msg_arg->params[2].u.rmem.offs = memref.shm_offs;
346
347 msg_arg->num_params = 4;
348}
349
350static u32 ta_rpc_test_invoke_func(struct udevice *dev, u32 func,
351 uint num_params,
352 struct tee_param *params)
353{
354 struct tee_shm *shm;
355 struct tee_param_memref memref_data;
356 struct optee_msg_arg *msg_arg;
357 int chip_addr, bus_num, op, xfer_flags;
358 int res;
359
360 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
361 TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
362 TEE_PARAM_ATTR_TYPE_NONE,
363 TEE_PARAM_ATTR_TYPE_NONE,
364 num_params, params);
365 if (res)
366 return TEE_ERROR_BAD_PARAMETERS;
367
368 bus_num = params[0].u.value.a;
369 chip_addr = params[0].u.value.b;
370 xfer_flags = params[0].u.value.c;
371 memref_data = params[1].u.memref;
372
373 switch (func) {
374 case TA_RPC_TEST_CMD_I2C_READ:
375 op = OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD;
376 break;
377 case TA_RPC_TEST_CMD_I2C_WRITE:
378 op = OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR;
379 break;
380 default:
381 return TEE_ERROR_NOT_SUPPORTED;
382 }
383
384 /*
385 * Fill params for an RPC call to tee supplicant
386 */
387 res = get_msg_arg(dev, 4, &shm, &msg_arg);
388 if (res)
389 goto out;
390
391 fill_i2c_rpc_params(msg_arg, bus_num, chip_addr, xfer_flags, op,
392 memref_data);
393
394 /* Make an RPC call to tee supplicant */
395 optee_suppl_cmd(dev, shm, 0);
396 res = msg_arg->ret;
397out:
398 tee_shm_free(shm);
399
400 return res;
401}
402#endif /* CONFIG_OPTEE_TA_RPC_TEST */
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200403
404static const struct ta_entry ta_entries[] = {
405#ifdef CONFIG_OPTEE_TA_AVB
406 { .uuid = TA_AVB_UUID,
407 .open_session = ta_avb_open_session,
408 .invoke_func = ta_avb_invoke_func,
409 },
410#endif
Igor Opaniuk84643c12021-01-25 14:28:42 +0200411#ifdef CONFIG_OPTEE_TA_RPC_TEST
412 { .uuid = TA_RPC_TEST_UUID,
413 .open_session = ta_rpc_test_open_session,
414 .invoke_func = ta_rpc_test_invoke_func,
415 },
416#endif
Jorge Ramirez-Ortizf2abe832021-02-14 16:27:25 +0100417#ifdef CONFIG_OPTEE_TA_SCP03
418 { .uuid = PTA_SCP03_UUID,
419 .open_session = pta_scp03_open_session,
420 .invoke_func = pta_scp03_invoke_func,
421 },
422#endif
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200423};
424
425static void sandbox_tee_get_version(struct udevice *dev,
426 struct tee_version_data *vers)
427{
428 struct tee_version_data v = {
429 .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM,
430 };
431
432 *vers = v;
433}
434
435static int sandbox_tee_close_session(struct udevice *dev, u32 session)
436{
437 struct sandbox_tee_state *state = dev_get_priv(dev);
438
439 if (!state->ta || state->session != session)
440 return -EINVAL;
441
442 state->session = 0;
443 state->ta = NULL;
444
445 return 0;
446}
447
448static const struct ta_entry *find_ta_entry(u8 uuid[TEE_UUID_LEN])
449{
450 struct tee_optee_ta_uuid u;
451 uint n;
452
453 tee_optee_ta_uuid_from_octets(&u, uuid);
454
455 for (n = 0; n < ARRAY_SIZE(ta_entries); n++)
456 if (!memcmp(&u, &ta_entries[n].uuid, sizeof(u)))
457 return ta_entries + n;
458
459 return NULL;
460}
461
462static int sandbox_tee_open_session(struct udevice *dev,
463 struct tee_open_session_arg *arg,
464 uint num_params, struct tee_param *params)
465{
466 struct sandbox_tee_state *state = dev_get_priv(dev);
467 const struct ta_entry *ta;
468
469 if (state->ta) {
470 printf("A session is already open\n");
471 return -EBUSY;
472 }
473
474 ta = find_ta_entry(arg->uuid);
475 if (!ta) {
476 printf("Cannot find TA\n");
477 arg->ret = TEE_ERROR_ITEM_NOT_FOUND;
478 arg->ret_origin = TEE_ORIGIN_TEE;
479
480 return 0;
481 }
482
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200483 arg->ret = ta->open_session(dev, num_params, params);
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200484 arg->ret_origin = TEE_ORIGIN_TRUSTED_APP;
485
486 if (!arg->ret) {
487 state->ta = (void *)ta;
488 state->session = 1;
489 arg->session = state->session;
490 } else {
491 printf("Cannot open session, TA returns error\n");
492 }
493
494 return 0;
495}
496
497static int sandbox_tee_invoke_func(struct udevice *dev,
498 struct tee_invoke_arg *arg,
499 uint num_params, struct tee_param *params)
500{
501 struct sandbox_tee_state *state = dev_get_priv(dev);
502 struct ta_entry *ta = state->ta;
503
504 if (!arg->session) {
505 printf("Missing session\n");
506 return -EINVAL;
507 }
508
509 if (!ta) {
510 printf("TA session not available\n");
511 return -EINVAL;
512 }
513
514 if (arg->session != state->session) {
515 printf("Session mismatch\n");
516 return -EINVAL;
517 }
518
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200519 arg->ret = ta->invoke_func(dev, arg->func, num_params, params);
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200520 arg->ret_origin = TEE_ORIGIN_TRUSTED_APP;
521
522 return 0;
523}
524
525static int sandbox_tee_shm_register(struct udevice *dev, struct tee_shm *shm)
526{
527 struct sandbox_tee_state *state = dev_get_priv(dev);
528
529 state->num_shms++;
530
531 return 0;
532}
533
534static int sandbox_tee_shm_unregister(struct udevice *dev, struct tee_shm *shm)
535{
536 struct sandbox_tee_state *state = dev_get_priv(dev);
537
538 state->num_shms--;
539
540 return 0;
541}
542
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200543static int sandbox_tee_remove(struct udevice *dev)
544{
545 struct sandbox_tee_state *state = dev_get_priv(dev);
546
547 hdestroy_r(&state->pstorage_htab);
548
549 return 0;
550}
551
552static int sandbox_tee_probe(struct udevice *dev)
553{
554 struct sandbox_tee_state *state = dev_get_priv(dev);
555 /*
556 * With this hastable we emulate persistent storage,
557 * which should contain persistent values
558 * between different sessions/command invocations.
559 */
560 if (!hcreate_r(pstorage_max, &state->pstorage_htab))
561 return TEE_ERROR_OUT_OF_MEMORY;
562
563 return 0;
564}
565
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200566static const struct tee_driver_ops sandbox_tee_ops = {
567 .get_version = sandbox_tee_get_version,
568 .open_session = sandbox_tee_open_session,
569 .close_session = sandbox_tee_close_session,
570 .invoke_func = sandbox_tee_invoke_func,
571 .shm_register = sandbox_tee_shm_register,
572 .shm_unregister = sandbox_tee_shm_unregister,
573};
574
575static const struct udevice_id sandbox_tee_match[] = {
576 { .compatible = "sandbox,tee" },
577 {},
578};
579
580U_BOOT_DRIVER(sandbox_tee) = {
581 .name = "sandbox_tee",
582 .id = UCLASS_TEE,
583 .of_match = sandbox_tee_match,
584 .ops = &sandbox_tee_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700585 .priv_auto = sizeof(struct sandbox_tee_state),
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200586 .probe = sandbox_tee_probe,
587 .remove = sandbox_tee_remove,
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200588};