blob: f63134dec86806c437877e8c15be437a1862c8a5 [file] [log] [blame]
Etienne Carriere75141172020-05-16 11:58:23 +02001// SPDX-License-Identifier: BSD-2-Clause
Pascal Brandc639ac82015-07-02 08:53:34 +02002/*
3 * Copyright (c) 2014, STMicroelectronics International N.V.
4 * All rights reserved.
Pascal Brandc639ac82015-07-02 08:53:34 +02005 */
6
7#include "storage.h"
Etienne Carriere294ffbd2018-04-26 14:20:35 +02008#include "ta_storage.h"
Pascal Brandc639ac82015-07-02 08:53:34 +02009
10#include <tee_api.h>
Pascal Brand90f23352016-05-19 15:15:47 +020011#include <trace.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020012
13#define ASSERT_PARAM_TYPE(pt) \
14do { \
15 if ((pt) != param_types) \
16 return TEE_ERROR_BAD_PARAMETERS; \
17} while (0)
18
Jens Wiklanderc5231592015-11-11 09:27:27 +010019#define VAL2HANDLE(v) (void *)(uintptr_t)(v)
20
Etienne Carriere294ffbd2018-04-26 14:20:35 +020021TEE_Result ta_storage_cmd_open(uint32_t command,
22 uint32_t param_types, TEE_Param params[4])
Pascal Brandc639ac82015-07-02 08:53:34 +020023{
Etienne Carriere102092e2019-03-28 15:24:22 +010024 TEE_Result res = TEE_ERROR_GENERIC;
25 TEE_ObjectHandle o = TEE_HANDLE_NULL;
26 void *object_id = NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +010027
Pascal Brandc639ac82015-07-02 08:53:34 +020028 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
29 (TEE_PARAM_TYPE_MEMREF_INPUT,
Jerome Forissier0e99d6a2016-07-25 14:21:43 +020030 TEE_PARAM_TYPE_VALUE_INOUT,
31 TEE_PARAM_TYPE_VALUE_INPUT,
Pascal Brandc639ac82015-07-02 08:53:34 +020032 TEE_PARAM_TYPE_NONE));
33
Etienne Carriere294ffbd2018-04-26 14:20:35 +020034 switch (command) {
35 case TA_STORAGE_CMD_OPEN:
Jerome Forissierc7a73732022-02-23 14:46:00 +010036 if (params[0].memref.buffer) {
37 object_id = TEE_Malloc(params[0].memref.size, 0);
38 if (!object_id)
39 return TEE_ERROR_OUT_OF_MEMORY;
Etienne Carriere9811a802018-02-16 16:11:42 +010040
Jerome Forissierc7a73732022-02-23 14:46:00 +010041 TEE_MemMove(object_id, params[0].memref.buffer,
42 params[0].memref.size);
43 }
Etienne Carriere294ffbd2018-04-26 14:20:35 +020044 break;
45 case TA_STORAGE_CMD_OPEN_ID_IN_SHM:
46 object_id = params[0].memref.buffer;
47 break;
48 default:
49 return TEE_ERROR_NOT_SUPPORTED;
50 }
Etienne Carriere9811a802018-02-16 16:11:42 +010051
Jerome Forissier0e99d6a2016-07-25 14:21:43 +020052 res = TEE_OpenPersistentObject(params[2].value.a,
Etienne Carriere294ffbd2018-04-26 14:20:35 +020053 object_id, params[0].memref.size,
Jens Wiklanderc5231592015-11-11 09:27:27 +010054 params[1].value.a, &o);
55
56 params[1].value.b = (uintptr_t)o;
Etienne Carriere294ffbd2018-04-26 14:20:35 +020057
58 if (command == TA_STORAGE_CMD_OPEN)
59 TEE_Free(object_id);
Etienne Carriere9811a802018-02-16 16:11:42 +010060
Jens Wiklanderc5231592015-11-11 09:27:27 +010061 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +020062}
63
Etienne Carriere294ffbd2018-04-26 14:20:35 +020064TEE_Result ta_storage_cmd_create(uint32_t command,
65 uint32_t param_types, TEE_Param params[4])
Pascal Brandc639ac82015-07-02 08:53:34 +020066{
Etienne Carriere102092e2019-03-28 15:24:22 +010067 TEE_Result res = TEE_ERROR_GENERIC;
68 TEE_ObjectHandle o = TEE_HANDLE_NULL;
69 void *object_id = NULL;
70 TEE_ObjectHandle ref_handle = TEE_HANDLE_NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +010071
Pascal Brandc639ac82015-07-02 08:53:34 +020072 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
73 (TEE_PARAM_TYPE_MEMREF_INPUT,
74 TEE_PARAM_TYPE_VALUE_INOUT,
75 TEE_PARAM_TYPE_VALUE_INPUT,
76 TEE_PARAM_TYPE_MEMREF_INPUT));
77
Etienne Carriere294ffbd2018-04-26 14:20:35 +020078 switch (command) {
79 case TA_STORAGE_CMD_CREATE:
Jerome Forissierc7a73732022-02-23 14:46:00 +010080 if (params[0].memref.buffer) {
81 object_id = TEE_Malloc(params[0].memref.size, 0);
82 if (!object_id)
83 return TEE_ERROR_OUT_OF_MEMORY;
Etienne Carriere9811a802018-02-16 16:11:42 +010084
Jerome Forissierc7a73732022-02-23 14:46:00 +010085 TEE_MemMove(object_id, params[0].memref.buffer,
86 params[0].memref.size);
87 }
Etienne Carriere294ffbd2018-04-26 14:20:35 +020088 break;
89 case TA_STORAGE_CMD_CREATE_ID_IN_SHM:
90 object_id = params[0].memref.buffer;
91 break;
92 default:
93 return TEE_ERROR_NOT_SUPPORTED;
94 }
95
96 ref_handle = (TEE_ObjectHandle)(uintptr_t)params[2].value.a;
Etienne Carriere9811a802018-02-16 16:11:42 +010097
Jerome Forissier0e99d6a2016-07-25 14:21:43 +020098 res = TEE_CreatePersistentObject(params[2].value.b,
Etienne Carriere294ffbd2018-04-26 14:20:35 +020099 object_id, params[0].memref.size,
100 params[1].value.a, ref_handle,
Etienne Carriere9811a802018-02-16 16:11:42 +0100101 params[3].memref.buffer,
102 params[3].memref.size, &o);
103
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200104 if (command == TA_STORAGE_CMD_CREATE)
105 TEE_Free(object_id);
106
Jens Wiklanderc5231592015-11-11 09:27:27 +0100107 params[1].value.b = (uintptr_t)o;
Etienne Carriere9811a802018-02-16 16:11:42 +0100108
Jens Wiklanderc5231592015-11-11 09:27:27 +0100109 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200110}
111
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200112TEE_Result ta_storage_cmd_create_overwrite(uint32_t command,
113 uint32_t param_types,
Pascal Brandeb84c442016-04-19 17:49:49 +0200114 TEE_Param params[4])
115{
Etienne Carriere102092e2019-03-28 15:24:22 +0100116 TEE_Result res = TEE_ERROR_GENERIC;
Cedric Auger3ad6b8a2019-09-11 16:38:42 +0200117 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Etienne Carriere102092e2019-03-28 15:24:22 +0100118 void *object_id = NULL;
Pascal Brandeb84c442016-04-19 17:49:49 +0200119
120 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
121 (TEE_PARAM_TYPE_MEMREF_INPUT,
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200122 TEE_PARAM_TYPE_VALUE_INPUT,
Pascal Brandeb84c442016-04-19 17:49:49 +0200123 TEE_PARAM_TYPE_NONE,
124 TEE_PARAM_TYPE_NONE));
125
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200126 switch (command) {
127 case TA_STORAGE_CMD_CREATE_OVERWRITE:
128 object_id = TEE_Malloc(params[0].memref.size, 0);
129 if (!object_id)
130 return TEE_ERROR_OUT_OF_MEMORY;
Etienne Carriere9811a802018-02-16 16:11:42 +0100131
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200132 TEE_MemMove(object_id, params[0].memref.buffer,
133 params[0].memref.size);
134 break;
135 case TA_STORAGE_CMD_CREATEOVER_ID_IN_SHM:
136 object_id = params[0].memref.buffer;
137 break;
138 default:
139 return TEE_ERROR_NOT_SUPPORTED;
140 }
Etienne Carriere9811a802018-02-16 16:11:42 +0100141
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200142 res = TEE_CreatePersistentObject(params[1].value.a,
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200143 object_id, params[0].memref.size,
Etienne Carriere9811a802018-02-16 16:11:42 +0100144 TEE_DATA_FLAG_OVERWRITE,
Cedric Auger3ad6b8a2019-09-11 16:38:42 +0200145 NULL, NULL, 0, &o);
146 TEE_CloseObject(o);
Etienne Carriere9811a802018-02-16 16:11:42 +0100147
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200148 if (command == TA_STORAGE_CMD_CREATE_OVERWRITE)
149 TEE_Free(object_id);
Etienne Carriere9811a802018-02-16 16:11:42 +0100150
Pascal Brandeb84c442016-04-19 17:49:49 +0200151 return res;
152}
153
Pascal Brandc639ac82015-07-02 08:53:34 +0200154TEE_Result ta_storage_cmd_close(uint32_t param_types, TEE_Param params[4])
155{
156 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
157 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
158 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
159
Jens Wiklanderc5231592015-11-11 09:27:27 +0100160 TEE_CloseObject((TEE_ObjectHandle)(uintptr_t)params[0].value.a);
Pascal Brandc639ac82015-07-02 08:53:34 +0200161
162 return TEE_SUCCESS;
163}
164
165TEE_Result ta_storage_cmd_read(uint32_t param_types, TEE_Param params[4])
166{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100167 TEE_ObjectHandle o = VAL2HANDLE(params[1].value.a);
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200168 TEE_Result res = TEE_SUCCESS;
169 void *b0 = NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100170
Pascal Brandc639ac82015-07-02 08:53:34 +0200171 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
172 (TEE_PARAM_TYPE_MEMREF_OUTPUT,
173 TEE_PARAM_TYPE_VALUE_INOUT, TEE_PARAM_TYPE_NONE,
174 TEE_PARAM_TYPE_NONE));
175
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200176 b0 = TEE_Malloc(params[0].memref.size, 0);
177 if (!b0)
178 return TEE_ERROR_OUT_OF_MEMORY;
179
180 res = TEE_ReadObjectData(o, b0, params[0].memref.size,
181 &params[1].value.b);
182 if (!res)
183 TEE_MemMove(params[0].memref.buffer, b0, params[0].memref.size);
184 TEE_Free(b0);
185
186 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200187}
188
189TEE_Result ta_storage_cmd_write(uint32_t param_types, TEE_Param params[4])
190{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100191 TEE_ObjectHandle o = VAL2HANDLE(params[1].value.a);
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200192 TEE_Result res = TEE_SUCCESS;
193 void *b0 = NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100194
Pascal Brandc639ac82015-07-02 08:53:34 +0200195 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
196 (TEE_PARAM_TYPE_MEMREF_INPUT,
197 TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
198 TEE_PARAM_TYPE_NONE));
199
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200200 b0 = TEE_Malloc(params[0].memref.size, 0);
201 if (!b0)
202 return TEE_ERROR_OUT_OF_MEMORY;
203 TEE_MemMove(b0, params[0].memref.buffer, params[0].memref.size);
204
205 res = TEE_WriteObjectData(o, b0, params[0].memref.size);
206 TEE_Free(b0);
207
208 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200209}
210
211TEE_Result ta_storage_cmd_seek(uint32_t param_types, TEE_Param params[4])
212{
Etienne Carriere102092e2019-03-28 15:24:22 +0100213 TEE_Result res = TEE_ERROR_GENERIC;
Pascal Brandc639ac82015-07-02 08:53:34 +0200214 TEE_ObjectInfo info;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100215 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
Etienne Carriere102092e2019-03-28 15:24:22 +0100216 int32_t offs = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200217
218 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
219 (TEE_PARAM_TYPE_VALUE_INPUT,
220 TEE_PARAM_TYPE_VALUE_INOUT, TEE_PARAM_TYPE_NONE,
221 TEE_PARAM_TYPE_NONE));
222
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200223 offs = *(int32_t *)&params[0].value.b;
224 res = TEE_SeekObjectData(o, offs, params[1].value.a);
Jens Wiklanderc5231592015-11-11 09:27:27 +0100225 if (res != TEE_SUCCESS)
226 return res;
227 res = TEE_GetObjectInfo1(o, &info);
Pascal Brandc639ac82015-07-02 08:53:34 +0200228
229 params[1].value.b = info.dataPosition;
230
231 return res;
232}
233
234TEE_Result ta_storage_cmd_unlink(uint32_t param_types, TEE_Param params[4])
235{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100236 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
237
Pascal Brandc639ac82015-07-02 08:53:34 +0200238 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
239 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
240 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
241
Jens Wiklanderc5231592015-11-11 09:27:27 +0100242 TEE_CloseAndDeletePersistentObject1(o);
Pascal Brandc639ac82015-07-02 08:53:34 +0200243
244 return TEE_SUCCESS;
245}
246
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200247TEE_Result ta_storage_cmd_rename(uint32_t command, uint32_t param_types,
248 TEE_Param params[4])
Pascal Brandc639ac82015-07-02 08:53:34 +0200249{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100250 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
Etienne Carriere102092e2019-03-28 15:24:22 +0100251 void *object_id = NULL;
252 TEE_Result res = TEE_ERROR_GENERIC;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100253
Pascal Brandc639ac82015-07-02 08:53:34 +0200254 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
255 (TEE_PARAM_TYPE_VALUE_INPUT,
256 TEE_PARAM_TYPE_MEMREF_INPUT, TEE_PARAM_TYPE_NONE,
257 TEE_PARAM_TYPE_NONE));
258
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200259 switch (command) {
260 case TA_STORAGE_CMD_RENAME:
Jerome Forissierc7a73732022-02-23 14:46:00 +0100261 if (params[0].memref.buffer) {
262 object_id = TEE_Malloc(params[1].memref.size, 0);
263 if (!object_id)
264 return TEE_ERROR_OUT_OF_MEMORY;
Etienne Carriere9811a802018-02-16 16:11:42 +0100265
Jerome Forissierc7a73732022-02-23 14:46:00 +0100266 TEE_MemMove(object_id, params[1].memref.buffer,
267 params[1].memref.size);
268 }
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200269 break;
270 case TA_STORAGE_CMD_RENAME_ID_IN_SHM:
271 object_id = params[1].memref.buffer;
272 break;
273 default:
274 return TEE_ERROR_NOT_SUPPORTED;
275 }
276
277 res = TEE_RenamePersistentObject(o, object_id, params[1].memref.size);
278
279 if (command == TA_STORAGE_CMD_RENAME)
280 TEE_Free(object_id);
Etienne Carriere9811a802018-02-16 16:11:42 +0100281
282 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200283}
284
285TEE_Result ta_storage_cmd_trunc(uint32_t param_types, TEE_Param params[4])
286{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100287 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
288
Pascal Brandc639ac82015-07-02 08:53:34 +0200289 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
290 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
291 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
292
Jens Wiklanderc5231592015-11-11 09:27:27 +0100293 return TEE_TruncateObjectData(o, params[0].value.b);
Pascal Brandc639ac82015-07-02 08:53:34 +0200294}
295
296TEE_Result ta_storage_cmd_alloc_enum(uint32_t param_types, TEE_Param params[4])
297{
Etienne Carriere102092e2019-03-28 15:24:22 +0100298 TEE_Result res = TEE_ERROR_GENERIC;
299 TEE_ObjectEnumHandle oe = TEE_HANDLE_NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100300
Pascal Brandc639ac82015-07-02 08:53:34 +0200301 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
302 (TEE_PARAM_TYPE_VALUE_OUTPUT, TEE_PARAM_TYPE_NONE,
303 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
304
Jens Wiklanderc5231592015-11-11 09:27:27 +0100305 res = TEE_AllocatePersistentObjectEnumerator(&oe);
306 params[0].value.a = (uintptr_t)oe;
307 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200308}
309
310TEE_Result ta_storage_cmd_free_enum(uint32_t param_types, TEE_Param params[4])
311{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100312 TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a);
313
Pascal Brandc639ac82015-07-02 08:53:34 +0200314 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
315 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
316 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
317
Jens Wiklanderc5231592015-11-11 09:27:27 +0100318 TEE_FreePersistentObjectEnumerator(oe);
Pascal Brandc639ac82015-07-02 08:53:34 +0200319 return TEE_SUCCESS;
320}
321
322TEE_Result ta_storage_cmd_reset_enum(uint32_t param_types, TEE_Param params[4])
323{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100324 TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a);
325
Pascal Brandc639ac82015-07-02 08:53:34 +0200326 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
327 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
328 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
329
Jens Wiklanderc5231592015-11-11 09:27:27 +0100330 TEE_ResetPersistentObjectEnumerator(oe);
Pascal Brandc639ac82015-07-02 08:53:34 +0200331 return TEE_SUCCESS;
332}
333
334TEE_Result ta_storage_cmd_start_enum(uint32_t param_types, TEE_Param params[4])
335{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100336 TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a);
337
Pascal Brandc639ac82015-07-02 08:53:34 +0200338 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
339 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
340 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
341
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200342 return TEE_StartPersistentObjectEnumerator(oe, params[0].value.b);
Pascal Brandc639ac82015-07-02 08:53:34 +0200343}
344
345TEE_Result ta_storage_cmd_next_enum(uint32_t param_types, TEE_Param params[4])
346{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100347 TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a);
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200348 TEE_Result res = TEE_SUCCESS;
Etienne Carriere102092e2019-03-28 15:24:22 +0100349 TEE_ObjectInfo *obj = NULL;
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200350 void *b2 = NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100351
Pascal Brandc603e0d2016-04-25 12:37:18 +0200352 if (TEE_PARAM_TYPE_GET(param_types, 0) != TEE_PARAM_TYPE_VALUE_INPUT)
353 return TEE_ERROR_BAD_PARAMETERS;
354 if (TEE_PARAM_TYPE_GET(param_types, 2) != TEE_PARAM_TYPE_MEMREF_OUTPUT)
355 return TEE_ERROR_BAD_PARAMETERS;
356 if (TEE_PARAM_TYPE_GET(param_types, 3) != TEE_PARAM_TYPE_NONE)
357 return TEE_ERROR_BAD_PARAMETERS;
Pascal Brandc639ac82015-07-02 08:53:34 +0200358
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200359 if (params[2].memref.size < TEE_OBJECT_ID_MAX_LEN)
360 return TEE_ERROR_SHORT_BUFFER;
361
Pascal Brandc603e0d2016-04-25 12:37:18 +0200362 if (TEE_PARAM_TYPE_GET(param_types, 1) == TEE_PARAM_TYPE_NONE)
363 obj = NULL;
364 else if (TEE_PARAM_TYPE_GET(param_types, 1) ==
365 TEE_PARAM_TYPE_MEMREF_OUTPUT) {
366 if (params[1].memref.size < sizeof(TEE_ObjectInfo)) {
367 params[1].memref.size = sizeof(TEE_ObjectInfo);
368 return TEE_ERROR_SHORT_BUFFER;
369 }
370 params[1].memref.size = sizeof(TEE_ObjectInfo);
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200371 obj = TEE_Malloc(sizeof(TEE_ObjectInfo), 0);
372 if (!obj)
373 return TEE_ERROR_OUT_OF_MEMORY;
Pascal Brandc603e0d2016-04-25 12:37:18 +0200374 } else
375 return TEE_ERROR_BAD_PARAMETERS;
Pascal Brandc639ac82015-07-02 08:53:34 +0200376
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200377 b2 = TEE_Malloc(params[2].memref.size, 0);
378 if (!b2) {
379 res = TEE_ERROR_OUT_OF_MEMORY;
380 goto out;
381 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200382
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200383 res = TEE_GetNextPersistentObject(oe, obj, b2, &params[2].memref.size);
384 if (res)
385 goto out;
386
387 TEE_MemMove(params[2].memref.buffer, b2, params[2].memref.size);
388 if (obj)
389 TEE_MemMove(params[1].memref.buffer, obj, sizeof(*obj));
390out:
391 TEE_Free(b2);
392 TEE_Free(obj);
393
394 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200395}
Pascal Brand90f23352016-05-19 15:15:47 +0200396
397static TEE_Result check_obj(TEE_ObjectInfo *o1, TEE_ObjectInfo *o2)
398{
399 if ((o1->objectType != o2->objectType) ||
400 (o1->keySize != o2->keySize) ||
401 (o1->maxKeySize != o2->maxKeySize) ||
402 (o1->objectUsage != o2->objectUsage))
403 return TEE_ERROR_GENERIC;
404 return TEE_SUCCESS;
405}
406
407TEE_Result ta_storage_cmd_key_in_persistent(uint32_t param_types,
408 TEE_Param params[4])
409{
410 TEE_Result result = TEE_SUCCESS;
Etienne Carriere102092e2019-03-28 15:24:22 +0100411 TEE_ObjectHandle transient_key = TEE_HANDLE_NULL;
412 TEE_ObjectHandle persistent_key = TEE_HANDLE_NULL;
413 TEE_ObjectHandle key = TEE_HANDLE_NULL;
414 TEE_OperationHandle encrypt_op = TEE_HANDLE_NULL;
Pascal Brand90f23352016-05-19 15:15:47 +0200415 TEE_ObjectInfo keyInfo;
416 TEE_ObjectInfo keyInfo2;
417 TEE_ObjectInfo keyInfo3;
418 uint32_t alg = TEE_ALG_AES_CBC_NOPAD;
419 void *IV = NULL;
420 size_t IVlen = 16;
421 size_t key_size = 256;
422 uint32_t objectID = 1;
423 uint32_t flags = TEE_DATA_FLAG_ACCESS_READ |
424 TEE_DATA_FLAG_ACCESS_WRITE |
425 TEE_DATA_FLAG_ACCESS_WRITE_META |
426 TEE_DATA_FLAG_SHARE_READ |
427 TEE_DATA_FLAG_SHARE_WRITE;
428
Etienne Carriere102092e2019-03-28 15:24:22 +0100429 TEE_MemFill(&keyInfo, 0, sizeof(keyInfo));
430 TEE_MemFill(&keyInfo2, 0, sizeof(keyInfo2));
431 TEE_MemFill(&keyInfo3, 0, sizeof(keyInfo3));
432
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200433 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
434 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
435 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
Pascal Brand90f23352016-05-19 15:15:47 +0200436
437 result = TEE_AllocateTransientObject(TEE_TYPE_AES, key_size,
438 &transient_key);
439 if (result != TEE_SUCCESS) {
440 EMSG("Failed to Allocate transient object handle : 0x%x",
441 result);
442 goto cleanup1;
443 }
444
445 result = TEE_GenerateKey(transient_key, key_size, NULL, 0);
446 if (result != TEE_SUCCESS) {
447 EMSG("Failed to generate a transient key: 0x%x", result);
448 goto cleanup2;
449 }
450
451 TEE_GetObjectInfo1(transient_key, &keyInfo);
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200452 result = TEE_CreatePersistentObject(params[0].value.a,
Pascal Brand90f23352016-05-19 15:15:47 +0200453 &objectID, sizeof(objectID),
454 flags, transient_key, NULL, 0,
455 &persistent_key);
456 if (result != TEE_SUCCESS) {
457 EMSG("Failed to create a persistent key: 0x%x", result);
458 goto cleanup2;
459 }
460
461 TEE_GetObjectInfo1(persistent_key, &keyInfo2);
462 result = check_obj(&keyInfo, &keyInfo2);
463 if (result != TEE_SUCCESS) {
464 EMSG("keyInfo and keyInfo2 are different");
465 goto cleanup2;
466 }
467
468 TEE_CloseObject(persistent_key);
469
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200470 result = TEE_OpenPersistentObject(params[0].value.a,
Pascal Brand90f23352016-05-19 15:15:47 +0200471 &objectID, sizeof(objectID),
472 flags, &key);
473 if (result != TEE_SUCCESS) {
474 EMSG("Failed to open persistent key: 0x%x", result);
475 goto cleanup2;
476 }
477
478 TEE_GetObjectInfo(key, &keyInfo3);
479 result = check_obj(&keyInfo3, &keyInfo2);
480 if (result != TEE_SUCCESS) {
481 EMSG("keyInfo2 and keyInfo3 are different");
482 goto cleanup2;
483 }
484
485 result = TEE_AllocateOperation(&encrypt_op, alg, TEE_MODE_ENCRYPT,
486 keyInfo3.maxObjectSize);
487 if (result != TEE_SUCCESS) {
488 EMSG("Failed to allocate an operation: 0x%x", result);
489 goto cleanup3;
490 }
491
492 result = TEE_SetOperationKey(encrypt_op, key);
493 if (result != TEE_SUCCESS) {
494 EMSG("Failed to set operation key: 0x%x", result);
495 goto cleanup4;
496 }
497
498 IV = TEE_Malloc(IVlen, 0);
499 if (!IV) {
500 EMSG("Out of memory for IV.");
501 result = TEE_ERROR_OUT_OF_MEMORY;
502 goto cleanup4;
503 }
504
505 TEE_CipherInit(encrypt_op, IV, IVlen);
506 TEE_Free(IV);
507
508cleanup4:
509 TEE_FreeOperation(encrypt_op);
510cleanup3:
511 TEE_CloseAndDeletePersistentObject1(key);
512cleanup2:
513 TEE_FreeTransientObject(transient_key);
514cleanup1:
515 return result;
516}
517
Pascal Brand29ee18f2016-05-23 14:13:56 +0200518TEE_Result ta_storage_cmd_loop(uint32_t param_types, TEE_Param params[4])
519{
520 TEE_ObjectHandle object = TEE_HANDLE_NULL;
Etienne Carriere102092e2019-03-28 15:24:22 +0100521 TEE_Result res = TEE_ERROR_GENERIC;
Pascal Brand29ee18f2016-05-23 14:13:56 +0200522 int object_id = 0;
523 uint32_t flags = TEE_DATA_FLAG_OVERWRITE |
524 TEE_DATA_FLAG_ACCESS_WRITE_META;
525 int i = 0;
526
Pascal Brand29ee18f2016-05-23 14:13:56 +0200527 (void)params;
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200528 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
529 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
530 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
Pascal Brand29ee18f2016-05-23 14:13:56 +0200531
532 for (i = 0; i < 20; i++) {
533 DMSG("\n\nLOOP : %d", i);
534 object = TEE_HANDLE_NULL;
535 object_id = i;
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200536 res = TEE_CreatePersistentObject(params[0].value.a,
Pascal Brand29ee18f2016-05-23 14:13:56 +0200537 &object_id, sizeof(int), flags,
538 TEE_HANDLE_NULL, NULL, 0,
539 &object);
540
541 if (res != TEE_SUCCESS) {
542 EMSG("FAIL");
543 return res;
544 }
545
546 res = TEE_CloseAndDeletePersistentObject1(object);
547 if (res != TEE_SUCCESS) {
548 EMSG("FAIL");
549 return res;
550 }
551 }
552
553 return TEE_SUCCESS;
554}
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200555
556TEE_Result ta_storage_cmd_restrict_usage(uint32_t param_types,
557 TEE_Param params[4])
558{
Etienne Carriere102092e2019-03-28 15:24:22 +0100559 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200560
561 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
562 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
563 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
564
565 o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a;
566 TEE_RestrictObjectUsage1(o, params[0].value.b);
567 return TEE_SUCCESS;
568}
569
570TEE_Result ta_storage_cmd_alloc_obj(uint32_t param_types, TEE_Param params[4])
571{
Etienne Carriere102092e2019-03-28 15:24:22 +0100572 TEE_Result res = TEE_ERROR_GENERIC;
573 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200574
575 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
576 (TEE_PARAM_TYPE_VALUE_INPUT,
577 TEE_PARAM_TYPE_VALUE_OUTPUT,
578 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
579
580 res = TEE_AllocateTransientObject(params[0].value.a, params[0].value.b,
581 &o);
582 params[1].value.a = (uint32_t)(uintptr_t)o;
583 return res;
584}
585
586TEE_Result ta_storage_cmd_free_obj(uint32_t param_types, TEE_Param params[4])
587{
Etienne Carriere102092e2019-03-28 15:24:22 +0100588 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200589
590 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
591 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
592 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
593
594 o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a;
595 TEE_FreeTransientObject(o);
596 return TEE_SUCCESS;
597}
598
599TEE_Result ta_storage_cmd_reset_obj(uint32_t param_types, TEE_Param params[4])
600{
Etienne Carriere102092e2019-03-28 15:24:22 +0100601 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200602
603 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
604 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
605 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
606
607 o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a;
608 TEE_ResetTransientObject(o);
609 return TEE_SUCCESS;
610}
Guanchao Liang31a9cbf2016-12-20 00:35:26 +0800611
612TEE_Result ta_storage_cmd_get_obj_info(uint32_t param_types,
613 TEE_Param params[4])
614{
Etienne Carriere102092e2019-03-28 15:24:22 +0100615 TEE_Result res = TEE_ERROR_GENERIC;
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200616 TEE_ObjectInfo info = { };
Guanchao Liang31a9cbf2016-12-20 00:35:26 +0800617 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
618
619 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
620 (TEE_PARAM_TYPE_VALUE_INPUT,
621 TEE_PARAM_TYPE_MEMREF_OUTPUT, TEE_PARAM_TYPE_NONE,
622 TEE_PARAM_TYPE_NONE));
623
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200624 if (params[1].memref.size < sizeof(info))
625 return TEE_ERROR_SHORT_BUFFER;
626 res = TEE_GetObjectInfo1(o, &info);
627 if (!res) {
628 params[1].memref.size = sizeof(info);
629 TEE_MemMove(params[1].memref.buffer, &info, sizeof(info));
630 }
Guanchao Liang31a9cbf2016-12-20 00:35:26 +0800631
632 return res;
633}