blob: 3a1d34d6fc0a2b4c7c510235944c4758e482d65d [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 */
5#include <common.h>
6#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>
11
12#include "optee/optee_msg.h"
13#include "optee/optee_private.h"
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020014
15/*
16 * The sandbox tee driver tries to emulate a generic Trusted Exectution
Igor Opaniuk84643c12021-01-25 14:28:42 +020017 * Environment (TEE) with the Trusted Applications (TA) OPTEE_TA_AVB and
18 * OPTEE_TA_RPC_TEST available.
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020019 */
20
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +020021static const u32 pstorage_max = 16;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020022/**
23 * struct ta_entry - TA entries
24 * @uuid: UUID of an emulated TA
25 * @open_session Called when a session is openened to the TA
26 * @invoke_func Called when a function in the TA is to be invoked
27 *
28 * This struct is used to register TAs in this sandbox emulation of a TEE.
29 */
30struct ta_entry {
31 struct tee_optee_ta_uuid uuid;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +020032 u32 (*open_session)(struct udevice *dev, uint num_params,
33 struct tee_param *params);
34 u32 (*invoke_func)(struct udevice *dev,
35 u32 func, uint num_params,
36 struct tee_param *params);
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020037};
38
Igor Opaniuk84643c12021-01-25 14:28:42 +020039static int get_msg_arg(struct udevice *dev, uint num_params,
40 struct tee_shm **shmp, struct optee_msg_arg **msg_arg)
41{
42 int rc;
43 struct optee_msg_arg *ma;
44
45 rc = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
46 OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC,
47 shmp);
48 if (rc)
49 return rc;
50
51 ma = (*shmp)->addr;
52 memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
53 ma->num_params = num_params;
54 *msg_arg = ma;
55
56 return 0;
57}
58
59void *optee_alloc_and_init_page_list(void *buf, ulong len,
60 u64 *phys_buf_ptr)
61{
62 /*
63 * An empty stub is added just to fix linking issues.
64 * This function isn't supposed to be called in sandbox
65 * setup, otherwise replace this with a proper
66 * implementation from optee/core.c
67 */
68 return NULL;
69}
70
Jens Wiklanderc7b443a2018-09-25 16:40:18 +020071static u32 get_attr(uint n, uint num_params, struct tee_param *params)
72{
73 if (n >= num_params)
74 return TEE_PARAM_ATTR_TYPE_NONE;
75
76 return params[n].attr;
77}
78
79static u32 check_params(u8 p0, u8 p1, u8 p2, u8 p3, uint num_params,
80 struct tee_param *params)
81{
82 u8 p[] = { p0, p1, p2, p3};
83 uint n;
84
85 for (n = 0; n < ARRAY_SIZE(p); n++)
86 if (p[n] != get_attr(n, num_params, params))
87 goto bad_params;
88
89 for (; n < num_params; n++)
90 if (get_attr(n, num_params, params))
91 goto bad_params;
92
93 return TEE_SUCCESS;
94
95bad_params:
96 printf("Bad param attrs\n");
97
98 return TEE_ERROR_BAD_PARAMETERS;
99}
100
Igor Opaniuk84643c12021-01-25 14:28:42 +0200101#ifdef CONFIG_OPTEE_TA_AVB
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200102static u32 ta_avb_open_session(struct udevice *dev, uint num_params,
103 struct tee_param *params)
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200104{
105 /*
106 * We don't expect additional parameters when opening a session to
107 * this TA.
108 */
109 return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
110 TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
111 num_params, params);
112}
113
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200114static u32 ta_avb_invoke_func(struct udevice *dev, u32 func, uint num_params,
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200115 struct tee_param *params)
116{
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200117 struct sandbox_tee_state *state = dev_get_priv(dev);
Simon Glass1a236862019-08-02 09:44:18 -0600118 struct env_entry e, *ep;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200119 char *name;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200120 u32 res;
121 uint slot;
122 u64 val;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200123 char *value;
124 u32 value_sz;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200125
126 switch (func) {
127 case TA_AVB_CMD_READ_ROLLBACK_INDEX:
128 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
129 TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT,
130 TEE_PARAM_ATTR_TYPE_NONE,
131 TEE_PARAM_ATTR_TYPE_NONE,
132 num_params, params);
133 if (res)
134 return res;
135
136 slot = params[0].u.value.a;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200137 if (slot >= ARRAY_SIZE(state->ta_avb_rollback_indexes)) {
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200138 printf("Rollback index slot out of bounds %u\n", slot);
139 return TEE_ERROR_BAD_PARAMETERS;
140 }
141
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200142 val = state->ta_avb_rollback_indexes[slot];
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200143 params[1].u.value.a = val >> 32;
144 params[1].u.value.b = val;
145 return TEE_SUCCESS;
146
147 case TA_AVB_CMD_WRITE_ROLLBACK_INDEX:
148 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
149 TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
150 TEE_PARAM_ATTR_TYPE_NONE,
151 TEE_PARAM_ATTR_TYPE_NONE,
152 num_params, params);
153 if (res)
154 return res;
155
156 slot = params[0].u.value.a;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200157 if (slot >= ARRAY_SIZE(state->ta_avb_rollback_indexes)) {
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200158 printf("Rollback index slot out of bounds %u\n", slot);
159 return TEE_ERROR_BAD_PARAMETERS;
160 }
161
162 val = (u64)params[1].u.value.a << 32 | params[1].u.value.b;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200163 if (val < state->ta_avb_rollback_indexes[slot])
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200164 return TEE_ERROR_SECURITY;
165
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200166 state->ta_avb_rollback_indexes[slot] = val;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200167 return TEE_SUCCESS;
168
169 case TA_AVB_CMD_READ_LOCK_STATE:
170 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT,
171 TEE_PARAM_ATTR_TYPE_NONE,
172 TEE_PARAM_ATTR_TYPE_NONE,
173 TEE_PARAM_ATTR_TYPE_NONE,
174 num_params, params);
175 if (res)
176 return res;
177
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200178 params[0].u.value.a = state->ta_avb_lock_state;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200179 return TEE_SUCCESS;
180
181 case TA_AVB_CMD_WRITE_LOCK_STATE:
182 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
183 TEE_PARAM_ATTR_TYPE_NONE,
184 TEE_PARAM_ATTR_TYPE_NONE,
185 TEE_PARAM_ATTR_TYPE_NONE,
186 num_params, params);
187 if (res)
188 return res;
189
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200190 if (state->ta_avb_lock_state != params[0].u.value.a) {
191 state->ta_avb_lock_state = params[0].u.value.a;
192 memset(state->ta_avb_rollback_indexes, 0,
193 sizeof(state->ta_avb_rollback_indexes));
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200194 }
195
196 return TEE_SUCCESS;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200197 case TA_AVB_CMD_READ_PERSIST_VALUE:
198 res = check_params(TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
199 TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
200 TEE_PARAM_ATTR_TYPE_NONE,
201 TEE_PARAM_ATTR_TYPE_NONE,
202 num_params, params);
203 if (res)
204 return res;
205
206 name = params[0].u.memref.shm->addr;
207
208 value = params[1].u.memref.shm->addr;
209 value_sz = params[1].u.memref.size;
210
211 e.key = name;
212 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600213 hsearch_r(e, ENV_FIND, &ep, &state->pstorage_htab, 0);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200214 if (!ep)
215 return TEE_ERROR_ITEM_NOT_FOUND;
216
Igor Opaniukce32c022019-05-06 12:07:31 +0300217 value_sz = strlen(ep->data) + 1;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200218 memcpy(value, ep->data, value_sz);
219
220 return TEE_SUCCESS;
221 case TA_AVB_CMD_WRITE_PERSIST_VALUE:
222 res = check_params(TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
223 TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
224 TEE_PARAM_ATTR_TYPE_NONE,
225 TEE_PARAM_ATTR_TYPE_NONE,
226 num_params, params);
227 if (res)
228 return res;
229
230 name = params[0].u.memref.shm->addr;
231
232 value = params[1].u.memref.shm->addr;
233 value_sz = params[1].u.memref.size;
234
235 e.key = name;
236 e.data = NULL;
Simon Glass63a2f572019-08-01 09:47:09 -0600237 hsearch_r(e, ENV_FIND, &ep, &state->pstorage_htab, 0);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200238 if (ep)
239 hdelete_r(e.key, &state->pstorage_htab, 0);
240
241 e.key = name;
242 e.data = value;
Simon Glass63a2f572019-08-01 09:47:09 -0600243 hsearch_r(e, ENV_ENTER, &ep, &state->pstorage_htab, 0);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200244 if (!ep)
245 return TEE_ERROR_OUT_OF_MEMORY;
246
247 return TEE_SUCCESS;
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200248
249 default:
250 return TEE_ERROR_NOT_SUPPORTED;
251 }
252}
Igor Opaniuk84643c12021-01-25 14:28:42 +0200253#endif /* OPTEE_TA_AVB */
254
255#ifdef CONFIG_OPTEE_TA_RPC_TEST
256static u32 ta_rpc_test_open_session(struct udevice *dev, uint num_params,
257 struct tee_param *params)
258{
259 /*
260 * We don't expect additional parameters when opening a session to
261 * this TA.
262 */
263 return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
264 TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
265 num_params, params);
266}
267
268static void fill_i2c_rpc_params(struct optee_msg_arg *msg_arg, u64 bus_num,
269 u64 chip_addr, u64 xfer_flags, u64 op,
270 struct tee_param_memref memref)
271{
272 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
273 msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
274 msg_arg->params[2].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INOUT;
275 msg_arg->params[3].attr = OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT;
276
277 /* trigger I2C services of TEE supplicant */
278 msg_arg->cmd = OPTEE_MSG_RPC_CMD_I2C_TRANSFER;
279
280 msg_arg->params[0].u.value.a = op;
281 msg_arg->params[0].u.value.b = bus_num;
282 msg_arg->params[0].u.value.c = chip_addr;
283 msg_arg->params[1].u.value.a = xfer_flags;
284
285 /* buffer to read/write data */
286 msg_arg->params[2].u.rmem.shm_ref = (ulong)memref.shm;
287 msg_arg->params[2].u.rmem.size = memref.size;
288 msg_arg->params[2].u.rmem.offs = memref.shm_offs;
289
290 msg_arg->num_params = 4;
291}
292
293static u32 ta_rpc_test_invoke_func(struct udevice *dev, u32 func,
294 uint num_params,
295 struct tee_param *params)
296{
297 struct tee_shm *shm;
298 struct tee_param_memref memref_data;
299 struct optee_msg_arg *msg_arg;
300 int chip_addr, bus_num, op, xfer_flags;
301 int res;
302
303 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
304 TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
305 TEE_PARAM_ATTR_TYPE_NONE,
306 TEE_PARAM_ATTR_TYPE_NONE,
307 num_params, params);
308 if (res)
309 return TEE_ERROR_BAD_PARAMETERS;
310
311 bus_num = params[0].u.value.a;
312 chip_addr = params[0].u.value.b;
313 xfer_flags = params[0].u.value.c;
314 memref_data = params[1].u.memref;
315
316 switch (func) {
317 case TA_RPC_TEST_CMD_I2C_READ:
318 op = OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD;
319 break;
320 case TA_RPC_TEST_CMD_I2C_WRITE:
321 op = OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR;
322 break;
323 default:
324 return TEE_ERROR_NOT_SUPPORTED;
325 }
326
327 /*
328 * Fill params for an RPC call to tee supplicant
329 */
330 res = get_msg_arg(dev, 4, &shm, &msg_arg);
331 if (res)
332 goto out;
333
334 fill_i2c_rpc_params(msg_arg, bus_num, chip_addr, xfer_flags, op,
335 memref_data);
336
337 /* Make an RPC call to tee supplicant */
338 optee_suppl_cmd(dev, shm, 0);
339 res = msg_arg->ret;
340out:
341 tee_shm_free(shm);
342
343 return res;
344}
345#endif /* CONFIG_OPTEE_TA_RPC_TEST */
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200346
347static const struct ta_entry ta_entries[] = {
348#ifdef CONFIG_OPTEE_TA_AVB
349 { .uuid = TA_AVB_UUID,
350 .open_session = ta_avb_open_session,
351 .invoke_func = ta_avb_invoke_func,
352 },
353#endif
Igor Opaniuk84643c12021-01-25 14:28:42 +0200354#ifdef CONFIG_OPTEE_TA_RPC_TEST
355 { .uuid = TA_RPC_TEST_UUID,
356 .open_session = ta_rpc_test_open_session,
357 .invoke_func = ta_rpc_test_invoke_func,
358 },
359#endif
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200360};
361
362static void sandbox_tee_get_version(struct udevice *dev,
363 struct tee_version_data *vers)
364{
365 struct tee_version_data v = {
366 .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM,
367 };
368
369 *vers = v;
370}
371
372static int sandbox_tee_close_session(struct udevice *dev, u32 session)
373{
374 struct sandbox_tee_state *state = dev_get_priv(dev);
375
376 if (!state->ta || state->session != session)
377 return -EINVAL;
378
379 state->session = 0;
380 state->ta = NULL;
381
382 return 0;
383}
384
385static const struct ta_entry *find_ta_entry(u8 uuid[TEE_UUID_LEN])
386{
387 struct tee_optee_ta_uuid u;
388 uint n;
389
390 tee_optee_ta_uuid_from_octets(&u, uuid);
391
392 for (n = 0; n < ARRAY_SIZE(ta_entries); n++)
393 if (!memcmp(&u, &ta_entries[n].uuid, sizeof(u)))
394 return ta_entries + n;
395
396 return NULL;
397}
398
399static int sandbox_tee_open_session(struct udevice *dev,
400 struct tee_open_session_arg *arg,
401 uint num_params, struct tee_param *params)
402{
403 struct sandbox_tee_state *state = dev_get_priv(dev);
404 const struct ta_entry *ta;
405
406 if (state->ta) {
407 printf("A session is already open\n");
408 return -EBUSY;
409 }
410
411 ta = find_ta_entry(arg->uuid);
412 if (!ta) {
413 printf("Cannot find TA\n");
414 arg->ret = TEE_ERROR_ITEM_NOT_FOUND;
415 arg->ret_origin = TEE_ORIGIN_TEE;
416
417 return 0;
418 }
419
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200420 arg->ret = ta->open_session(dev, num_params, params);
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200421 arg->ret_origin = TEE_ORIGIN_TRUSTED_APP;
422
423 if (!arg->ret) {
424 state->ta = (void *)ta;
425 state->session = 1;
426 arg->session = state->session;
427 } else {
428 printf("Cannot open session, TA returns error\n");
429 }
430
431 return 0;
432}
433
434static int sandbox_tee_invoke_func(struct udevice *dev,
435 struct tee_invoke_arg *arg,
436 uint num_params, struct tee_param *params)
437{
438 struct sandbox_tee_state *state = dev_get_priv(dev);
439 struct ta_entry *ta = state->ta;
440
441 if (!arg->session) {
442 printf("Missing session\n");
443 return -EINVAL;
444 }
445
446 if (!ta) {
447 printf("TA session not available\n");
448 return -EINVAL;
449 }
450
451 if (arg->session != state->session) {
452 printf("Session mismatch\n");
453 return -EINVAL;
454 }
455
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200456 arg->ret = ta->invoke_func(dev, arg->func, num_params, params);
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200457 arg->ret_origin = TEE_ORIGIN_TRUSTED_APP;
458
459 return 0;
460}
461
462static int sandbox_tee_shm_register(struct udevice *dev, struct tee_shm *shm)
463{
464 struct sandbox_tee_state *state = dev_get_priv(dev);
465
466 state->num_shms++;
467
468 return 0;
469}
470
471static int sandbox_tee_shm_unregister(struct udevice *dev, struct tee_shm *shm)
472{
473 struct sandbox_tee_state *state = dev_get_priv(dev);
474
475 state->num_shms--;
476
477 return 0;
478}
479
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200480static int sandbox_tee_remove(struct udevice *dev)
481{
482 struct sandbox_tee_state *state = dev_get_priv(dev);
483
484 hdestroy_r(&state->pstorage_htab);
485
486 return 0;
487}
488
489static int sandbox_tee_probe(struct udevice *dev)
490{
491 struct sandbox_tee_state *state = dev_get_priv(dev);
492 /*
493 * With this hastable we emulate persistent storage,
494 * which should contain persistent values
495 * between different sessions/command invocations.
496 */
497 if (!hcreate_r(pstorage_max, &state->pstorage_htab))
498 return TEE_ERROR_OUT_OF_MEMORY;
499
500 return 0;
501}
502
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200503static const struct tee_driver_ops sandbox_tee_ops = {
504 .get_version = sandbox_tee_get_version,
505 .open_session = sandbox_tee_open_session,
506 .close_session = sandbox_tee_close_session,
507 .invoke_func = sandbox_tee_invoke_func,
508 .shm_register = sandbox_tee_shm_register,
509 .shm_unregister = sandbox_tee_shm_unregister,
510};
511
512static const struct udevice_id sandbox_tee_match[] = {
513 { .compatible = "sandbox,tee" },
514 {},
515};
516
517U_BOOT_DRIVER(sandbox_tee) = {
518 .name = "sandbox_tee",
519 .id = UCLASS_TEE,
520 .of_match = sandbox_tee_match,
521 .ops = &sandbox_tee_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700522 .priv_auto = sizeof(struct sandbox_tee_state),
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200523 .probe = sandbox_tee_probe,
524 .remove = sandbox_tee_remove,
Jens Wiklanderc7b443a2018-09-25 16:40:18 +0200525};