1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Linaro Limited
7 #include <sandboxtee.h>
9 #include <tee/optee_ta_avb.h>
12 * The sandbox tee driver tries to emulate a generic Trusted Exectution
13 * Environment (TEE) with the Trusted Application (TA) OPTEE_TA_AVB
18 * struct ta_entry - TA entries
19 * @uuid: UUID of an emulated TA
20 * @open_session Called when a session is openened to the TA
21 * @invoke_func Called when a function in the TA is to be invoked
23 * This struct is used to register TAs in this sandbox emulation of a TEE.
26 struct tee_optee_ta_uuid uuid;
27 u32 (*open_session)(uint num_params, struct tee_param *params);
28 u32 (*invoke_func)(u32 func, uint num_params, struct tee_param *params);
31 #ifdef CONFIG_OPTEE_TA_AVB
32 static u32 get_attr(uint n, uint num_params, struct tee_param *params)
35 return TEE_PARAM_ATTR_TYPE_NONE;
37 return params[n].attr;
40 static u32 check_params(u8 p0, u8 p1, u8 p2, u8 p3, uint num_params,
41 struct tee_param *params)
43 u8 p[] = { p0, p1, p2, p3};
46 for (n = 0; n < ARRAY_SIZE(p); n++)
47 if (p[n] != get_attr(n, num_params, params))
50 for (; n < num_params; n++)
51 if (get_attr(n, num_params, params))
57 printf("Bad param attrs\n");
59 return TEE_ERROR_BAD_PARAMETERS;
62 static u64 ta_avb_rollback_indexes[TA_AVB_MAX_ROLLBACK_LOCATIONS];
63 static u32 ta_avb_lock_state;
65 static u32 ta_avb_open_session(uint num_params, struct tee_param *params)
68 * We don't expect additional parameters when opening a session to
71 return check_params(TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
72 TEE_PARAM_ATTR_TYPE_NONE, TEE_PARAM_ATTR_TYPE_NONE,
76 static u32 ta_avb_invoke_func(u32 func, uint num_params,
77 struct tee_param *params)
84 case TA_AVB_CMD_READ_ROLLBACK_INDEX:
85 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
86 TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT,
87 TEE_PARAM_ATTR_TYPE_NONE,
88 TEE_PARAM_ATTR_TYPE_NONE,
93 slot = params[0].u.value.a;
94 if (slot >= ARRAY_SIZE(ta_avb_rollback_indexes)) {
95 printf("Rollback index slot out of bounds %u\n", slot);
96 return TEE_ERROR_BAD_PARAMETERS;
99 val = ta_avb_rollback_indexes[slot];
100 params[1].u.value.a = val >> 32;
101 params[1].u.value.b = val;
104 case TA_AVB_CMD_WRITE_ROLLBACK_INDEX:
105 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
106 TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
107 TEE_PARAM_ATTR_TYPE_NONE,
108 TEE_PARAM_ATTR_TYPE_NONE,
113 slot = params[0].u.value.a;
114 if (slot >= ARRAY_SIZE(ta_avb_rollback_indexes)) {
115 printf("Rollback index slot out of bounds %u\n", slot);
116 return TEE_ERROR_BAD_PARAMETERS;
119 val = (u64)params[1].u.value.a << 32 | params[1].u.value.b;
120 if (val < ta_avb_rollback_indexes[slot])
121 return TEE_ERROR_SECURITY;
123 ta_avb_rollback_indexes[slot] = val;
126 case TA_AVB_CMD_READ_LOCK_STATE:
127 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT,
128 TEE_PARAM_ATTR_TYPE_NONE,
129 TEE_PARAM_ATTR_TYPE_NONE,
130 TEE_PARAM_ATTR_TYPE_NONE,
135 params[0].u.value.a = ta_avb_lock_state;
138 case TA_AVB_CMD_WRITE_LOCK_STATE:
139 res = check_params(TEE_PARAM_ATTR_TYPE_VALUE_INPUT,
140 TEE_PARAM_ATTR_TYPE_NONE,
141 TEE_PARAM_ATTR_TYPE_NONE,
142 TEE_PARAM_ATTR_TYPE_NONE,
147 if (ta_avb_lock_state != params[0].u.value.a) {
148 ta_avb_lock_state = params[0].u.value.a;
149 memset(ta_avb_rollback_indexes, 0,
150 sizeof(ta_avb_rollback_indexes));
156 return TEE_ERROR_NOT_SUPPORTED;
159 #endif /*OPTEE_TA_AVB*/
161 static const struct ta_entry ta_entries[] = {
162 #ifdef CONFIG_OPTEE_TA_AVB
163 { .uuid = TA_AVB_UUID,
164 .open_session = ta_avb_open_session,
165 .invoke_func = ta_avb_invoke_func,
170 static void sandbox_tee_get_version(struct udevice *dev,
171 struct tee_version_data *vers)
173 struct tee_version_data v = {
174 .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM,
180 static int sandbox_tee_close_session(struct udevice *dev, u32 session)
182 struct sandbox_tee_state *state = dev_get_priv(dev);
184 if (!state->ta || state->session != session)
193 static const struct ta_entry *find_ta_entry(u8 uuid[TEE_UUID_LEN])
195 struct tee_optee_ta_uuid u;
198 tee_optee_ta_uuid_from_octets(&u, uuid);
200 for (n = 0; n < ARRAY_SIZE(ta_entries); n++)
201 if (!memcmp(&u, &ta_entries[n].uuid, sizeof(u)))
202 return ta_entries + n;
207 static int sandbox_tee_open_session(struct udevice *dev,
208 struct tee_open_session_arg *arg,
209 uint num_params, struct tee_param *params)
211 struct sandbox_tee_state *state = dev_get_priv(dev);
212 const struct ta_entry *ta;
215 printf("A session is already open\n");
219 ta = find_ta_entry(arg->uuid);
221 printf("Cannot find TA\n");
222 arg->ret = TEE_ERROR_ITEM_NOT_FOUND;
223 arg->ret_origin = TEE_ORIGIN_TEE;
228 arg->ret = ta->open_session(num_params, params);
229 arg->ret_origin = TEE_ORIGIN_TRUSTED_APP;
232 state->ta = (void *)ta;
234 arg->session = state->session;
236 printf("Cannot open session, TA returns error\n");
242 static int sandbox_tee_invoke_func(struct udevice *dev,
243 struct tee_invoke_arg *arg,
244 uint num_params, struct tee_param *params)
246 struct sandbox_tee_state *state = dev_get_priv(dev);
247 struct ta_entry *ta = state->ta;
250 printf("Missing session\n");
255 printf("TA session not available\n");
259 if (arg->session != state->session) {
260 printf("Session mismatch\n");
264 arg->ret = ta->invoke_func(arg->func, num_params, params);
265 arg->ret_origin = TEE_ORIGIN_TRUSTED_APP;
270 static int sandbox_tee_shm_register(struct udevice *dev, struct tee_shm *shm)
272 struct sandbox_tee_state *state = dev_get_priv(dev);
279 static int sandbox_tee_shm_unregister(struct udevice *dev, struct tee_shm *shm)
281 struct sandbox_tee_state *state = dev_get_priv(dev);
288 static const struct tee_driver_ops sandbox_tee_ops = {
289 .get_version = sandbox_tee_get_version,
290 .open_session = sandbox_tee_open_session,
291 .close_session = sandbox_tee_close_session,
292 .invoke_func = sandbox_tee_invoke_func,
293 .shm_register = sandbox_tee_shm_register,
294 .shm_unregister = sandbox_tee_shm_unregister,
297 static const struct udevice_id sandbox_tee_match[] = {
298 { .compatible = "sandbox,tee" },
302 U_BOOT_DRIVER(sandbox_tee) = {
303 .name = "sandbox_tee",
305 .of_match = sandbox_tee_match,
306 .ops = &sandbox_tee_ops,
307 .priv_auto_alloc_size = sizeof(struct sandbox_tee_state),