1 // SPDX-License-Identifier: GPL-2.0+
14 #include <tee/optee_ta_avb.h>
16 static struct udevice *tee;
19 static int avb_ta_open_session(void)
21 const struct tee_optee_ta_uuid uuid = TA_AVB_UUID;
22 struct tee_open_session_arg arg;
25 tee = tee_find_device(tee, NULL, NULL, NULL);
29 memset(&arg, 0, sizeof(arg));
30 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
31 rc = tee_open_session(tee, &arg, 0, NULL);
33 session = arg.session;
38 static int invoke_func(u32 func, ulong num_param, struct tee_param *param)
40 struct tee_invoke_arg arg;
43 if (avb_ta_open_session())
46 memset(&arg, 0, sizeof(arg));
48 arg.session = session;
50 if (tee_invoke_func(tee, &arg, num_param, param))
55 case TEE_ERROR_OUT_OF_MEMORY:
56 case TEE_ERROR_STORAGE_NO_SPACE:
58 case TEE_ERROR_ITEM_NOT_FOUND:
60 case TEE_ERROR_TARGET_DEAD:
62 * The TA has paniced, close the session to reload the TA
63 * for the next request.
65 tee_close_session(tee, session);
73 static int read_persistent_value(const char *name,
76 size_t *out_num_bytes_read)
79 struct tee_shm *shm_name;
80 struct tee_shm *shm_buf;
81 struct tee_param param[2];
82 size_t name_size = strlen(name) + 1;
85 if (avb_ta_open_session())
88 rc = tee_shm_alloc(tee, name_size,
89 TEE_SHM_ALLOC, &shm_name);
93 rc = tee_shm_alloc(tee, buffer_size,
94 TEE_SHM_ALLOC, &shm_buf);
100 memcpy(shm_name->addr, name, name_size);
102 memset(param, 0, sizeof(param));
103 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
104 param[0].u.memref.shm = shm_name;
105 param[0].u.memref.size = name_size;
106 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
107 param[1].u.memref.shm = shm_buf;
108 param[1].u.memref.size = buffer_size;
110 rc = invoke_func(TA_AVB_CMD_READ_PERSIST_VALUE,
115 if (param[1].u.memref.size > buffer_size) {
120 *out_num_bytes_read = param[1].u.memref.size;
122 memcpy(out_buffer, shm_buf->addr, *out_num_bytes_read);
125 tee_shm_free(shm_buf);
127 tee_shm_free(shm_name);
132 static int write_persistent_value(const char *name,
137 struct tee_shm *shm_name;
138 struct tee_shm *shm_buf;
139 struct tee_param param[2];
140 size_t name_size = strlen(name) + 1;
143 if (avb_ta_open_session())
149 rc = tee_shm_alloc(tee, name_size,
150 TEE_SHM_ALLOC, &shm_name);
154 rc = tee_shm_alloc(tee, value_size,
155 TEE_SHM_ALLOC, &shm_buf);
161 memcpy(shm_name->addr, name, name_size);
162 memcpy(shm_buf->addr, value, value_size);
164 memset(param, 0, sizeof(param));
165 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
166 param[0].u.memref.shm = shm_name;
167 param[0].u.memref.size = name_size;
168 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
169 param[1].u.memref.shm = shm_buf;
170 param[1].u.memref.size = value_size;
172 rc = invoke_func(TA_AVB_CMD_WRITE_PERSIST_VALUE,
178 tee_shm_free(shm_buf);
180 tee_shm_free(shm_name);
185 int do_optee_rpmb_read(struct cmd_tbl *cmdtp, int flag, int argc,
195 return CMD_RET_USAGE;
198 bytes = dectoul(argv[2], &endp);
199 if (*endp && *endp != '\n')
200 return CMD_RET_USAGE;
202 buffer = malloc(bytes);
204 return CMD_RET_FAILURE;
206 if (read_persistent_value(name, bytes, buffer, &bytes_read) == 0) {
207 printf("Read %zu bytes, value = %s\n", bytes_read,
210 return CMD_RET_SUCCESS;
213 printf("Failed to read persistent value\n");
217 return CMD_RET_FAILURE;
220 int do_optee_rpmb_write(struct cmd_tbl *cmdtp, int flag, int argc,
227 return CMD_RET_USAGE;
232 if (write_persistent_value(name, strlen(value) + 1,
233 (const uint8_t *)value) == 0) {
234 printf("Wrote %zu bytes\n", strlen(value) + 1);
235 return CMD_RET_SUCCESS;
238 printf("Failed to write persistent value\n");
240 return CMD_RET_FAILURE;
243 static struct cmd_tbl cmd_optee_rpmb[] = {
244 U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_optee_rpmb_read, "", ""),
245 U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_optee_rpmb_write, "", ""),
248 static int do_optee_rpmb(struct cmd_tbl *cmdtp, int flag, int argc,
253 cp = find_cmd_tbl(argv[1], cmd_optee_rpmb, ARRAY_SIZE(cmd_optee_rpmb));
258 if (!cp || argc > cp->maxargs)
259 return CMD_RET_USAGE;
261 if (flag == CMD_FLAG_REPEAT)
262 return CMD_RET_FAILURE;
264 return cp->cmd(cmdtp, flag, argc, argv);
268 optee_rpmb, 29, 0, do_optee_rpmb,
269 "Provides commands for testing secure storage on RPMB on OPTEE",
270 "read_pvalue <name> <bytes> - read a persistent value <name>\n"
271 "optee_rpmb write_pvalue <name> <value> - write a persistent value <name>\n"