blob: 4ad8f8c0b0745662bfa432a1a33e4e2808d00e2c [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;
Jens Wiklandere1634fa2022-12-12 12:59:28 +0100169 size_t sz = 0;
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200170 void *b0 = NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100171
Pascal Brandc639ac82015-07-02 08:53:34 +0200172 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
173 (TEE_PARAM_TYPE_MEMREF_OUTPUT,
174 TEE_PARAM_TYPE_VALUE_INOUT, TEE_PARAM_TYPE_NONE,
175 TEE_PARAM_TYPE_NONE));
176
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200177 b0 = TEE_Malloc(params[0].memref.size, 0);
178 if (!b0)
179 return TEE_ERROR_OUT_OF_MEMORY;
180
Jens Wiklandere1634fa2022-12-12 12:59:28 +0100181 sz = params[1].value.b;
182 res = TEE_ReadObjectData(o, b0, params[0].memref.size, &sz);
183 params[1].value.b = sz;
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200184 if (!res)
185 TEE_MemMove(params[0].memref.buffer, b0, params[0].memref.size);
186 TEE_Free(b0);
187
188 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200189}
190
191TEE_Result ta_storage_cmd_write(uint32_t param_types, TEE_Param params[4])
192{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100193 TEE_ObjectHandle o = VAL2HANDLE(params[1].value.a);
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200194 TEE_Result res = TEE_SUCCESS;
195 void *b0 = NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100196
Pascal Brandc639ac82015-07-02 08:53:34 +0200197 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
198 (TEE_PARAM_TYPE_MEMREF_INPUT,
199 TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
200 TEE_PARAM_TYPE_NONE));
201
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200202 b0 = TEE_Malloc(params[0].memref.size, 0);
203 if (!b0)
204 return TEE_ERROR_OUT_OF_MEMORY;
205 TEE_MemMove(b0, params[0].memref.buffer, params[0].memref.size);
206
207 res = TEE_WriteObjectData(o, b0, params[0].memref.size);
208 TEE_Free(b0);
209
210 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200211}
212
213TEE_Result ta_storage_cmd_seek(uint32_t param_types, TEE_Param params[4])
214{
Etienne Carriere102092e2019-03-28 15:24:22 +0100215 TEE_Result res = TEE_ERROR_GENERIC;
Pascal Brandc639ac82015-07-02 08:53:34 +0200216 TEE_ObjectInfo info;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100217 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
Etienne Carriere102092e2019-03-28 15:24:22 +0100218 int32_t offs = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200219
220 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
221 (TEE_PARAM_TYPE_VALUE_INPUT,
222 TEE_PARAM_TYPE_VALUE_INOUT, TEE_PARAM_TYPE_NONE,
223 TEE_PARAM_TYPE_NONE));
224
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200225 offs = *(int32_t *)&params[0].value.b;
226 res = TEE_SeekObjectData(o, offs, params[1].value.a);
Jens Wiklanderc5231592015-11-11 09:27:27 +0100227 if (res != TEE_SUCCESS)
228 return res;
229 res = TEE_GetObjectInfo1(o, &info);
Pascal Brandc639ac82015-07-02 08:53:34 +0200230
231 params[1].value.b = info.dataPosition;
232
233 return res;
234}
235
236TEE_Result ta_storage_cmd_unlink(uint32_t param_types, TEE_Param params[4])
237{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100238 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
239
Pascal Brandc639ac82015-07-02 08:53:34 +0200240 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
241 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
242 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
243
Jens Wiklanderc5231592015-11-11 09:27:27 +0100244 TEE_CloseAndDeletePersistentObject1(o);
Pascal Brandc639ac82015-07-02 08:53:34 +0200245
246 return TEE_SUCCESS;
247}
248
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200249TEE_Result ta_storage_cmd_rename(uint32_t command, uint32_t param_types,
250 TEE_Param params[4])
Pascal Brandc639ac82015-07-02 08:53:34 +0200251{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100252 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
Etienne Carriere102092e2019-03-28 15:24:22 +0100253 void *object_id = NULL;
254 TEE_Result res = TEE_ERROR_GENERIC;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100255
Pascal Brandc639ac82015-07-02 08:53:34 +0200256 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
257 (TEE_PARAM_TYPE_VALUE_INPUT,
258 TEE_PARAM_TYPE_MEMREF_INPUT, TEE_PARAM_TYPE_NONE,
259 TEE_PARAM_TYPE_NONE));
260
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200261 switch (command) {
262 case TA_STORAGE_CMD_RENAME:
Jerome Forissierc7a73732022-02-23 14:46:00 +0100263 if (params[0].memref.buffer) {
264 object_id = TEE_Malloc(params[1].memref.size, 0);
265 if (!object_id)
266 return TEE_ERROR_OUT_OF_MEMORY;
Etienne Carriere9811a802018-02-16 16:11:42 +0100267
Jerome Forissierc7a73732022-02-23 14:46:00 +0100268 TEE_MemMove(object_id, params[1].memref.buffer,
269 params[1].memref.size);
270 }
Etienne Carriere294ffbd2018-04-26 14:20:35 +0200271 break;
272 case TA_STORAGE_CMD_RENAME_ID_IN_SHM:
273 object_id = params[1].memref.buffer;
274 break;
275 default:
276 return TEE_ERROR_NOT_SUPPORTED;
277 }
278
279 res = TEE_RenamePersistentObject(o, object_id, params[1].memref.size);
280
281 if (command == TA_STORAGE_CMD_RENAME)
282 TEE_Free(object_id);
Etienne Carriere9811a802018-02-16 16:11:42 +0100283
284 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200285}
286
287TEE_Result ta_storage_cmd_trunc(uint32_t param_types, TEE_Param params[4])
288{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100289 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
290
Pascal Brandc639ac82015-07-02 08:53:34 +0200291 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
292 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
293 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
294
Jens Wiklanderc5231592015-11-11 09:27:27 +0100295 return TEE_TruncateObjectData(o, params[0].value.b);
Pascal Brandc639ac82015-07-02 08:53:34 +0200296}
297
298TEE_Result ta_storage_cmd_alloc_enum(uint32_t param_types, TEE_Param params[4])
299{
Etienne Carriere102092e2019-03-28 15:24:22 +0100300 TEE_Result res = TEE_ERROR_GENERIC;
301 TEE_ObjectEnumHandle oe = TEE_HANDLE_NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100302
Pascal Brandc639ac82015-07-02 08:53:34 +0200303 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
304 (TEE_PARAM_TYPE_VALUE_OUTPUT, TEE_PARAM_TYPE_NONE,
305 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
306
Jens Wiklanderc5231592015-11-11 09:27:27 +0100307 res = TEE_AllocatePersistentObjectEnumerator(&oe);
308 params[0].value.a = (uintptr_t)oe;
309 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200310}
311
312TEE_Result ta_storage_cmd_free_enum(uint32_t param_types, TEE_Param params[4])
313{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100314 TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a);
315
Pascal Brandc639ac82015-07-02 08:53:34 +0200316 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
317 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
318 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
319
Jens Wiklanderc5231592015-11-11 09:27:27 +0100320 TEE_FreePersistentObjectEnumerator(oe);
Pascal Brandc639ac82015-07-02 08:53:34 +0200321 return TEE_SUCCESS;
322}
323
324TEE_Result ta_storage_cmd_reset_enum(uint32_t param_types, TEE_Param params[4])
325{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100326 TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a);
327
Pascal Brandc639ac82015-07-02 08:53:34 +0200328 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
329 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
330 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
331
Jens Wiklanderc5231592015-11-11 09:27:27 +0100332 TEE_ResetPersistentObjectEnumerator(oe);
Pascal Brandc639ac82015-07-02 08:53:34 +0200333 return TEE_SUCCESS;
334}
335
336TEE_Result ta_storage_cmd_start_enum(uint32_t param_types, TEE_Param params[4])
337{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100338 TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a);
339
Pascal Brandc639ac82015-07-02 08:53:34 +0200340 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
341 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
342 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
343
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200344 return TEE_StartPersistentObjectEnumerator(oe, params[0].value.b);
Pascal Brandc639ac82015-07-02 08:53:34 +0200345}
346
347TEE_Result ta_storage_cmd_next_enum(uint32_t param_types, TEE_Param params[4])
348{
Jens Wiklanderc5231592015-11-11 09:27:27 +0100349 TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a);
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200350 TEE_Result res = TEE_SUCCESS;
Etienne Carriere102092e2019-03-28 15:24:22 +0100351 TEE_ObjectInfo *obj = NULL;
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200352 void *b2 = NULL;
Jens Wiklanderc5231592015-11-11 09:27:27 +0100353
Pascal Brandc603e0d2016-04-25 12:37:18 +0200354 if (TEE_PARAM_TYPE_GET(param_types, 0) != TEE_PARAM_TYPE_VALUE_INPUT)
355 return TEE_ERROR_BAD_PARAMETERS;
356 if (TEE_PARAM_TYPE_GET(param_types, 2) != TEE_PARAM_TYPE_MEMREF_OUTPUT)
357 return TEE_ERROR_BAD_PARAMETERS;
358 if (TEE_PARAM_TYPE_GET(param_types, 3) != TEE_PARAM_TYPE_NONE)
359 return TEE_ERROR_BAD_PARAMETERS;
Pascal Brandc639ac82015-07-02 08:53:34 +0200360
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200361 if (params[2].memref.size < TEE_OBJECT_ID_MAX_LEN)
362 return TEE_ERROR_SHORT_BUFFER;
363
Pascal Brandc603e0d2016-04-25 12:37:18 +0200364 if (TEE_PARAM_TYPE_GET(param_types, 1) == TEE_PARAM_TYPE_NONE)
365 obj = NULL;
366 else if (TEE_PARAM_TYPE_GET(param_types, 1) ==
367 TEE_PARAM_TYPE_MEMREF_OUTPUT) {
368 if (params[1].memref.size < sizeof(TEE_ObjectInfo)) {
369 params[1].memref.size = sizeof(TEE_ObjectInfo);
370 return TEE_ERROR_SHORT_BUFFER;
371 }
372 params[1].memref.size = sizeof(TEE_ObjectInfo);
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200373 obj = TEE_Malloc(sizeof(TEE_ObjectInfo), 0);
374 if (!obj)
375 return TEE_ERROR_OUT_OF_MEMORY;
Pascal Brandc603e0d2016-04-25 12:37:18 +0200376 } else
377 return TEE_ERROR_BAD_PARAMETERS;
Pascal Brandc639ac82015-07-02 08:53:34 +0200378
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200379 b2 = TEE_Malloc(params[2].memref.size, 0);
380 if (!b2) {
381 res = TEE_ERROR_OUT_OF_MEMORY;
382 goto out;
383 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200384
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200385 res = TEE_GetNextPersistentObject(oe, obj, b2, &params[2].memref.size);
386 if (res)
387 goto out;
388
389 TEE_MemMove(params[2].memref.buffer, b2, params[2].memref.size);
390 if (obj)
391 TEE_MemMove(params[1].memref.buffer, obj, sizeof(*obj));
392out:
393 TEE_Free(b2);
394 TEE_Free(obj);
395
396 return res;
Pascal Brandc639ac82015-07-02 08:53:34 +0200397}
Pascal Brand90f23352016-05-19 15:15:47 +0200398
399static TEE_Result check_obj(TEE_ObjectInfo *o1, TEE_ObjectInfo *o2)
400{
401 if ((o1->objectType != o2->objectType) ||
Jens Wiklandere1634fa2022-12-12 12:59:28 +0100402 (o1->objectSize != o2->objectSize) ||
403 (o1->maxObjectSize != o2->maxObjectSize) ||
Pascal Brand90f23352016-05-19 15:15:47 +0200404 (o1->objectUsage != o2->objectUsage))
405 return TEE_ERROR_GENERIC;
406 return TEE_SUCCESS;
407}
408
409TEE_Result ta_storage_cmd_key_in_persistent(uint32_t param_types,
410 TEE_Param params[4])
411{
412 TEE_Result result = TEE_SUCCESS;
Etienne Carriere102092e2019-03-28 15:24:22 +0100413 TEE_ObjectHandle transient_key = TEE_HANDLE_NULL;
414 TEE_ObjectHandle persistent_key = TEE_HANDLE_NULL;
415 TEE_ObjectHandle key = TEE_HANDLE_NULL;
416 TEE_OperationHandle encrypt_op = TEE_HANDLE_NULL;
Pascal Brand90f23352016-05-19 15:15:47 +0200417 TEE_ObjectInfo keyInfo;
418 TEE_ObjectInfo keyInfo2;
419 TEE_ObjectInfo keyInfo3;
420 uint32_t alg = TEE_ALG_AES_CBC_NOPAD;
421 void *IV = NULL;
422 size_t IVlen = 16;
423 size_t key_size = 256;
424 uint32_t objectID = 1;
425 uint32_t flags = TEE_DATA_FLAG_ACCESS_READ |
426 TEE_DATA_FLAG_ACCESS_WRITE |
427 TEE_DATA_FLAG_ACCESS_WRITE_META |
428 TEE_DATA_FLAG_SHARE_READ |
429 TEE_DATA_FLAG_SHARE_WRITE;
430
Etienne Carriere102092e2019-03-28 15:24:22 +0100431 TEE_MemFill(&keyInfo, 0, sizeof(keyInfo));
432 TEE_MemFill(&keyInfo2, 0, sizeof(keyInfo2));
433 TEE_MemFill(&keyInfo3, 0, sizeof(keyInfo3));
434
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200435 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
436 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
437 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
Pascal Brand90f23352016-05-19 15:15:47 +0200438
439 result = TEE_AllocateTransientObject(TEE_TYPE_AES, key_size,
440 &transient_key);
441 if (result != TEE_SUCCESS) {
442 EMSG("Failed to Allocate transient object handle : 0x%x",
443 result);
444 goto cleanup1;
445 }
446
447 result = TEE_GenerateKey(transient_key, key_size, NULL, 0);
448 if (result != TEE_SUCCESS) {
449 EMSG("Failed to generate a transient key: 0x%x", result);
450 goto cleanup2;
451 }
452
453 TEE_GetObjectInfo1(transient_key, &keyInfo);
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200454 result = TEE_CreatePersistentObject(params[0].value.a,
Pascal Brand90f23352016-05-19 15:15:47 +0200455 &objectID, sizeof(objectID),
456 flags, transient_key, NULL, 0,
457 &persistent_key);
458 if (result != TEE_SUCCESS) {
459 EMSG("Failed to create a persistent key: 0x%x", result);
460 goto cleanup2;
461 }
462
463 TEE_GetObjectInfo1(persistent_key, &keyInfo2);
464 result = check_obj(&keyInfo, &keyInfo2);
465 if (result != TEE_SUCCESS) {
466 EMSG("keyInfo and keyInfo2 are different");
467 goto cleanup2;
468 }
469
470 TEE_CloseObject(persistent_key);
471
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200472 result = TEE_OpenPersistentObject(params[0].value.a,
Pascal Brand90f23352016-05-19 15:15:47 +0200473 &objectID, sizeof(objectID),
474 flags, &key);
475 if (result != TEE_SUCCESS) {
476 EMSG("Failed to open persistent key: 0x%x", result);
477 goto cleanup2;
478 }
479
480 TEE_GetObjectInfo(key, &keyInfo3);
481 result = check_obj(&keyInfo3, &keyInfo2);
482 if (result != TEE_SUCCESS) {
483 EMSG("keyInfo2 and keyInfo3 are different");
484 goto cleanup2;
485 }
486
487 result = TEE_AllocateOperation(&encrypt_op, alg, TEE_MODE_ENCRYPT,
488 keyInfo3.maxObjectSize);
489 if (result != TEE_SUCCESS) {
490 EMSG("Failed to allocate an operation: 0x%x", result);
491 goto cleanup3;
492 }
493
494 result = TEE_SetOperationKey(encrypt_op, key);
495 if (result != TEE_SUCCESS) {
496 EMSG("Failed to set operation key: 0x%x", result);
497 goto cleanup4;
498 }
499
500 IV = TEE_Malloc(IVlen, 0);
501 if (!IV) {
502 EMSG("Out of memory for IV.");
503 result = TEE_ERROR_OUT_OF_MEMORY;
504 goto cleanup4;
505 }
506
507 TEE_CipherInit(encrypt_op, IV, IVlen);
508 TEE_Free(IV);
509
510cleanup4:
511 TEE_FreeOperation(encrypt_op);
512cleanup3:
513 TEE_CloseAndDeletePersistentObject1(key);
514cleanup2:
515 TEE_FreeTransientObject(transient_key);
516cleanup1:
517 return result;
518}
519
Pascal Brand29ee18f2016-05-23 14:13:56 +0200520TEE_Result ta_storage_cmd_loop(uint32_t param_types, TEE_Param params[4])
521{
522 TEE_ObjectHandle object = TEE_HANDLE_NULL;
Etienne Carriere102092e2019-03-28 15:24:22 +0100523 TEE_Result res = TEE_ERROR_GENERIC;
Pascal Brand29ee18f2016-05-23 14:13:56 +0200524 int object_id = 0;
525 uint32_t flags = TEE_DATA_FLAG_OVERWRITE |
526 TEE_DATA_FLAG_ACCESS_WRITE_META;
527 int i = 0;
528
Pascal Brand29ee18f2016-05-23 14:13:56 +0200529 (void)params;
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200530 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
531 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
532 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
Pascal Brand29ee18f2016-05-23 14:13:56 +0200533
534 for (i = 0; i < 20; i++) {
535 DMSG("\n\nLOOP : %d", i);
536 object = TEE_HANDLE_NULL;
537 object_id = i;
Jerome Forissier0e99d6a2016-07-25 14:21:43 +0200538 res = TEE_CreatePersistentObject(params[0].value.a,
Pascal Brand29ee18f2016-05-23 14:13:56 +0200539 &object_id, sizeof(int), flags,
540 TEE_HANDLE_NULL, NULL, 0,
541 &object);
542
543 if (res != TEE_SUCCESS) {
544 EMSG("FAIL");
545 return res;
546 }
547
548 res = TEE_CloseAndDeletePersistentObject1(object);
549 if (res != TEE_SUCCESS) {
550 EMSG("FAIL");
551 return res;
552 }
553 }
554
555 return TEE_SUCCESS;
556}
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200557
558TEE_Result ta_storage_cmd_restrict_usage(uint32_t param_types,
559 TEE_Param params[4])
560{
Etienne Carriere102092e2019-03-28 15:24:22 +0100561 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200562
563 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
564 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
565 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
566
567 o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a;
568 TEE_RestrictObjectUsage1(o, params[0].value.b);
569 return TEE_SUCCESS;
570}
571
572TEE_Result ta_storage_cmd_alloc_obj(uint32_t param_types, TEE_Param params[4])
573{
Etienne Carriere102092e2019-03-28 15:24:22 +0100574 TEE_Result res = TEE_ERROR_GENERIC;
575 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200576
577 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
578 (TEE_PARAM_TYPE_VALUE_INPUT,
579 TEE_PARAM_TYPE_VALUE_OUTPUT,
580 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
581
582 res = TEE_AllocateTransientObject(params[0].value.a, params[0].value.b,
583 &o);
584 params[1].value.a = (uint32_t)(uintptr_t)o;
585 return res;
586}
587
588TEE_Result ta_storage_cmd_free_obj(uint32_t param_types, TEE_Param params[4])
589{
Etienne Carriere102092e2019-03-28 15:24:22 +0100590 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200591
592 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
593 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
594 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
595
596 o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a;
597 TEE_FreeTransientObject(o);
598 return TEE_SUCCESS;
599}
600
601TEE_Result ta_storage_cmd_reset_obj(uint32_t param_types, TEE_Param params[4])
602{
Etienne Carriere102092e2019-03-28 15:24:22 +0100603 TEE_ObjectHandle o = TEE_HANDLE_NULL;
Jens Wiklandere6d4ddd2016-09-14 15:50:48 +0200604
605 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
606 (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE,
607 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE));
608
609 o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a;
610 TEE_ResetTransientObject(o);
611 return TEE_SUCCESS;
612}
Guanchao Liang31a9cbf2016-12-20 00:35:26 +0800613
614TEE_Result ta_storage_cmd_get_obj_info(uint32_t param_types,
615 TEE_Param params[4])
616{
Etienne Carriere102092e2019-03-28 15:24:22 +0100617 TEE_Result res = TEE_ERROR_GENERIC;
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200618 TEE_ObjectInfo info = { };
Guanchao Liang31a9cbf2016-12-20 00:35:26 +0800619 TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a);
620
621 ASSERT_PARAM_TYPE(TEE_PARAM_TYPES
622 (TEE_PARAM_TYPE_VALUE_INPUT,
623 TEE_PARAM_TYPE_MEMREF_OUTPUT, TEE_PARAM_TYPE_NONE,
624 TEE_PARAM_TYPE_NONE));
625
Jens Wiklanderc9d6c2f2020-09-11 10:13:54 +0200626 if (params[1].memref.size < sizeof(info))
627 return TEE_ERROR_SHORT_BUFFER;
628 res = TEE_GetObjectInfo1(o, &info);
629 if (!res) {
630 params[1].memref.size = sizeof(info);
631 TEE_MemMove(params[1].memref.buffer, &info, sizeof(info));
632 }
Guanchao Liang31a9cbf2016-12-20 00:35:26 +0800633
634 return res;
635}