Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Linaro Limited |
| 4 | */ |
Igor Opaniuk | b905599 | 2024-04-04 15:19:51 +0200 | [diff] [blame] | 5 | |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <sandboxtee.h> |
| 8 | #include <tee.h> |
| 9 | #include <tee/optee_ta_avb.h> |
Igor Opaniuk | 592b98b | 2021-01-25 14:28:42 +0200 | [diff] [blame] | 10 | #include <tee/optee_ta_rpc_test.h> |
Jorge Ramirez-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 11 | #include <tee/optee_ta_scp03.h> |
Igor Opaniuk | 592b98b | 2021-01-25 14:28:42 +0200 | [diff] [blame] | 12 | |
| 13 | #include "optee/optee_msg.h" |
| 14 | #include "optee/optee_private.h" |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 15 | |
| 16 | /* |
Igor Opaniuk | 5e19fe8 | 2024-03-04 18:44:50 +0100 | [diff] [blame] | 17 | * The sandbox tee driver tries to emulate a generic Trusted Execution |
Igor Opaniuk | 592b98b | 2021-01-25 14:28:42 +0200 | [diff] [blame] | 18 | * Environment (TEE) with the Trusted Applications (TA) OPTEE_TA_AVB and |
| 19 | * OPTEE_TA_RPC_TEST available. |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 22 | static const u32 pstorage_max = 16; |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 23 | /** |
| 24 | * struct ta_entry - TA entries |
| 25 | * @uuid: UUID of an emulated TA |
Igor Opaniuk | 5e19fe8 | 2024-03-04 18:44:50 +0100 | [diff] [blame] | 26 | * @open_session Called when a session is opened to the TA |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 27 | * @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 | */ |
| 31 | struct ta_entry { |
| 32 | struct tee_optee_ta_uuid uuid; |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 33 | 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 Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
Igor Opaniuk | 592b98b | 2021-01-25 14:28:42 +0200 | [diff] [blame] | 40 | static 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 | |
| 60 | void *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-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 72 | #if defined(CONFIG_OPTEE_TA_SCP03) || defined(CONFIG_OPTEE_TA_AVB) |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 73 | static 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 | |
| 81 | static u32 check_params(u8 p0, u8 p1, u8 p2, u8 p3, uint num_params, |
| 82 | struct tee_param *params) |
| 83 | { |
Jorge Ramirez-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 84 | u8 p[] = { p0, p1, p2, p3 }; |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 85 | 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 | |
| 97 | bad_params: |
| 98 | printf("Bad param attrs\n"); |
| 99 | |
| 100 | return TEE_ERROR_BAD_PARAMETERS; |
| 101 | } |
Jorge Ramirez-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 102 | #endif |
| 103 | |
| 104 | #ifdef CONFIG_OPTEE_TA_SCP03 |
| 105 | static 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 | |
| 117 | static 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-Ortiz | 7f1a4c9 | 2023-04-08 17:35:31 +0200 | [diff] [blame] | 122 | static bool provisioned; |
Jorge Ramirez-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 123 | |
| 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-Ortiz | 7f1a4c9 | 2023-04-08 17:35:31 +0200 | [diff] [blame] | 134 | /* If SCP03 was not enabled, enable it */ |
| 135 | if (!enabled) |
Jorge Ramirez-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 136 | enabled = true; |
Jorge Ramirez-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 137 | |
Jorge Ramirez-Ortiz | 7f1a4c9 | 2023-04-08 17:35:31 +0200 | [diff] [blame] | 138 | /* If SCP03 was not provisioned, provision new keys */ |
| 139 | if (params[0].u.value.a && !provisioned) |
| 140 | provisioned = true; |
| 141 | |
| 142 | /* |
Igor Opaniuk | 5e19fe8 | 2024-03-04 18:44:50 +0100 | [diff] [blame] | 143 | * Either way, we assume both operations succeeded and that |
| 144 | * the communication channel has now been established |
Jorge Ramirez-Ortiz | 7f1a4c9 | 2023-04-08 17:35:31 +0200 | [diff] [blame] | 145 | */ |
Jorge Ramirez-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 146 | |
| 147 | return TEE_SUCCESS; |
| 148 | default: |
| 149 | return TEE_ERROR_NOT_SUPPORTED; |
| 150 | } |
| 151 | } |
| 152 | #endif |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 153 | |
Igor Opaniuk | 592b98b | 2021-01-25 14:28:42 +0200 | [diff] [blame] | 154 | #ifdef CONFIG_OPTEE_TA_AVB |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 155 | static u32 ta_avb_open_session(struct udevice *dev, uint num_params, |
| 156 | struct tee_param *params) |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 157 | { |
| 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 167 | static u32 ta_avb_invoke_func(struct udevice *dev, u32 func, uint num_params, |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 168 | struct tee_param *params) |
| 169 | { |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 170 | struct sandbox_tee_state *state = dev_get_priv(dev); |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 171 | struct env_entry e, *ep; |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 172 | char *name; |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 173 | u32 res; |
| 174 | uint slot; |
| 175 | u64 val; |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 176 | char *value; |
Igor Opaniuk | 8800cbe | 2024-04-21 22:48:39 +0200 | [diff] [blame] | 177 | u32 value_sz, tmp_sz; |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 178 | |
| 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 190 | if (slot >= ARRAY_SIZE(state->ta_avb_rollback_indexes)) { |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 191 | printf("Rollback index slot out of bounds %u\n", slot); |
| 192 | return TEE_ERROR_BAD_PARAMETERS; |
| 193 | } |
| 194 | |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 195 | val = state->ta_avb_rollback_indexes[slot]; |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 196 | 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 210 | if (slot >= ARRAY_SIZE(state->ta_avb_rollback_indexes)) { |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 211 | 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 216 | if (val < state->ta_avb_rollback_indexes[slot]) |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 217 | return TEE_ERROR_SECURITY; |
| 218 | |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 219 | state->ta_avb_rollback_indexes[slot] = val; |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 220 | 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 231 | params[0].u.value.a = state->ta_avb_lock_state; |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 232 | 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 243 | 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 Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return TEE_SUCCESS; |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 250 | 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 Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 266 | hsearch_r(e, ENV_FIND, &ep, &state->pstorage_htab, 0); |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 267 | if (!ep) |
| 268 | return TEE_ERROR_ITEM_NOT_FOUND; |
| 269 | |
Igor Opaniuk | 8800cbe | 2024-04-21 22:48:39 +0200 | [diff] [blame] | 270 | 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 276 | |
| 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 Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 294 | hsearch_r(e, ENV_FIND, &ep, &state->pstorage_htab, 0); |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 295 | if (ep) |
| 296 | hdelete_r(e.key, &state->pstorage_htab, 0); |
| 297 | |
| 298 | e.key = name; |
| 299 | e.data = value; |
Simon Glass | 3f0d680 | 2019-08-01 09:47:09 -0600 | [diff] [blame] | 300 | hsearch_r(e, ENV_ENTER, &ep, &state->pstorage_htab, 0); |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 301 | if (!ep) |
| 302 | return TEE_ERROR_OUT_OF_MEMORY; |
| 303 | |
| 304 | return TEE_SUCCESS; |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 305 | |
| 306 | default: |
| 307 | return TEE_ERROR_NOT_SUPPORTED; |
| 308 | } |
| 309 | } |
Igor Opaniuk | 592b98b | 2021-01-25 14:28:42 +0200 | [diff] [blame] | 310 | #endif /* OPTEE_TA_AVB */ |
| 311 | |
| 312 | #ifdef CONFIG_OPTEE_TA_RPC_TEST |
| 313 | static 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 | |
| 325 | static 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 | |
| 350 | static 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; |
| 397 | out: |
| 398 | tee_shm_free(shm); |
| 399 | |
| 400 | return res; |
| 401 | } |
| 402 | #endif /* CONFIG_OPTEE_TA_RPC_TEST */ |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 403 | |
| 404 | static 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 Opaniuk | 592b98b | 2021-01-25 14:28:42 +0200 | [diff] [blame] | 411 | #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-Ortiz | 5a8783c | 2021-02-14 16:27:25 +0100 | [diff] [blame] | 417 | #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 Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | static 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 | |
| 435 | static 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 | |
| 448 | static 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 | |
| 462 | static 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 483 | arg->ret = ta->open_session(dev, num_params, params); |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 484 | 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 | |
| 497 | static 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 519 | arg->ret = ta->invoke_func(dev, arg->func, num_params, params); |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 520 | arg->ret_origin = TEE_ORIGIN_TRUSTED_APP; |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static 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 | |
| 534 | static 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 Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 543 | static 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 | |
| 552 | static 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 Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 566 | static 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 | |
| 575 | static const struct udevice_id sandbox_tee_match[] = { |
| 576 | { .compatible = "sandbox,tee" }, |
| 577 | {}, |
| 578 | }; |
| 579 | |
| 580 | U_BOOT_DRIVER(sandbox_tee) = { |
| 581 | .name = "sandbox_tee", |
| 582 | .id = UCLASS_TEE, |
| 583 | .of_match = sandbox_tee_match, |
| 584 | .ops = &sandbox_tee_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 585 | .priv_auto = sizeof(struct sandbox_tee_state), |
Igor Opaniuk | fc1fe01 | 2019-04-09 15:38:14 +0200 | [diff] [blame] | 586 | .probe = sandbox_tee_probe, |
| 587 | .remove = sandbox_tee_remove, |
Jens Wiklander | eadf26f | 2018-09-25 16:40:18 +0200 | [diff] [blame] | 588 | }; |