blob: 7510ee1124c59a5d717efd0f9337e7cae7741e4e [file] [log] [blame]
Etienne Carriere41343db2017-03-17 15:38:52 +01001/*
2 * Copyright (c) 2016, Linaro Limited
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <err.h>
29#include <fcntl.h>
Etienne Carrierec45d9762017-03-21 15:45:13 +010030#include <pta_invoke_tests.h>
Etienne Carriere41343db2017-03-17 15:38:52 +010031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/ioctl.h>
35#include <sys/mman.h>
36#include <tee_client_api.h>
37#include <tee_client_api_extensions.h>
38#include <unistd.h>
39
Etienne Carriere41343db2017-03-17 15:38:52 +010040#include "crypto_common.h"
Etienne Carriere50abf9a2017-03-24 11:33:50 +010041#include "sdp_basic.h"
42#include "xtest_test.h"
Etienne Carriere41343db2017-03-17 15:38:52 +010043
Etienne Carriere3a34b982017-10-24 15:31:34 +020044#include "include/uapi/linux/ion_old.h"
45
Etienne Carriere41343db2017-03-17 15:38:52 +010046/*
47 * SDP basic test setup overview.
48 *
49 * - A dedicated trusted application (SDP basic TA) supports 3 commands:
50 * - 'inject' data from a nonsecure buffer into a secure buffer
51 * - 'transform' data inside a secure buffer (bitwise invert + unsigned incr)
52 * - 'dump' data from a secure buffer into a nonsecure buffer
53
54 * - This test client application (CA) invokes the TA for these 3 operations,
55 * inject random value, trasforming them then dump them.
56 *
57 * To do so, CA allocates a 'SDP secure buffer' and invoke the TA for these 3
58 * operations (inject then transform then dump) over the allocate buffer.
59 *
60 * The secure buffer is currently allocation through ION support adn
61 * registered to OP-TEE and as shared memory.
62 *
63 * To enhance test coverage against buffer alignement usecase, the CA invokes
64 * the TA with a variable offset inside the buffer. As CA injects random data
65 * into the buffer, the CA uses one of the random bytes to set the value of the
66 * offset in the accessed secure buffer.
67 *
68 * For debugging support, the CA may map (in nonsecure world) the secure
69 * buffer to read its content. As this is unsafe on a hardened platform, this
70 * operation is default disable. When enable, error only print out a warning
71 * trace but does not actually fail the test. This also give an easy way to
72 * check that some HW complains on access violation when nonsecure accesses
73 * secure data.
74 */
75
Etienne Carriere41343db2017-03-17 15:38:52 +010076struct tee_ctx {
77 TEEC_Context ctx;
78 TEEC_Session sess;
79};
80
Etienne Carriere3a34b982017-10-24 15:31:34 +020081/*
82 * Old ION API to allocate and export a buffer
83 */
84static int allocate_ion_buffer_old_api(size_t size, int heap_type_id, int ion)
85{
86 struct ion0_allocation_data alloc_data;
87 struct ion0_handle_data hdl_data;
88 struct ion0_fd_data fd_data;
89 int fd = -1;
90
91 alloc_data.len = size;
92 alloc_data.align = 0;
93 alloc_data.flags = 0;
94 alloc_data.heap_id_mask = 1 << heap_type_id;
95 if (ioctl(ion, ION0_IOC_ALLOC, &alloc_data) == -1) {
96 fprintf(stderr, "Error: old ION allocate API failed\n");
97 return fd;
98 }
99
100 fd_data.handle = alloc_data.handle;
101 if (ioctl(ion, ION0_IOC_SHARE, &fd_data) != -1)
102 fd = fd_data.fd;
103 else
104 fprintf(stderr, "Error: old ION share API failed\n");
105
106 hdl_data.handle = alloc_data.handle;
107 (void)ioctl(ion, ION0_IOC_FREE, &hdl_data);
108
109 return fd;
110}
111
Etienne Carriere8bc31422017-08-24 14:48:30 +0200112int allocate_ion_buffer(size_t size, int heap_type_id, int verbosity)
Etienne Carriere41343db2017-03-17 15:38:52 +0100113{
Etienne Carriere8bc31422017-08-24 14:48:30 +0200114 struct ion_heap_query query_data;
115 struct ion_heap_data heap_data[32];
Etienne Carriere41343db2017-03-17 15:38:52 +0100116 struct ion_allocation_data alloc_data;
Etienne Carriere41343db2017-03-17 15:38:52 +0100117 int ion;
118 int fd = -1;
Etienne Carriere8bc31422017-08-24 14:48:30 +0200119 unsigned int idx;
Etienne Carriere41343db2017-03-17 15:38:52 +0100120
121 ion = open("/dev/ion", O_RDWR);
122 if (ion < 0) {
Etienne Carriere8bc31422017-08-24 14:48:30 +0200123 fprintf(stderr, "Error: failed to open /dev/ion\n");
Etienne Carriere41343db2017-03-17 15:38:52 +0100124 verbose("Seems no ION heap is available.\n");
125 verbose("To test ION allocation you can enable\n");
126 verbose("CONFIG_ION and CONFIG_ION_DUMMY in your\n");
127 verbose("linux kernel configuration.\n");
128 return fd;
129 }
130
Etienne Carriere8bc31422017-08-24 14:48:30 +0200131 if (heap_type_id < 0)
132 heap_type_id = DEFAULT_ION_HEAP_TYPE;
Etienne Carriere41343db2017-03-17 15:38:52 +0100133
Etienne Carriere8bc31422017-08-24 14:48:30 +0200134 memset(&query_data, 0, sizeof(query_data));
135 if (ioctl(ion, ION_IOC_HEAP_QUERY, &query_data) < 0) {
136 fprintf(stderr, "Error: failed to query the number of heaps\n");
137 goto out;
138 }
139
140 query_data.heaps = (__u64)(unsigned long)&heap_data;
141 if (ioctl(ion, ION_IOC_HEAP_QUERY, &query_data) < 0) {
Etienne Carriere3a34b982017-10-24 15:31:34 +0200142 fprintf(stderr, "Info: can't query heaps data, try old API\n");
143 fd = allocate_ion_buffer_old_api(size, heap_type_id, ion);
Etienne Carriere8bc31422017-08-24 14:48:30 +0200144 goto out;
145 }
146
147 for (idx = 0; idx < query_data.cnt; idx++)
148 if (heap_data[idx].type == (unsigned int)heap_type_id)
149 break;
150 if (idx == query_data.cnt) {
151 fprintf(stderr, "Error: target heap type %d not found\n",
152 heap_type_id);
153 goto out;
154 }
155
156 verbose("Allocate in ION heap '%s' (type=%u, id=%u)\n",
157 heap_data[idx].name, heap_data[idx].type,
158 heap_data[idx].heap_id);
Etienne Carriere41343db2017-03-17 15:38:52 +0100159
160 alloc_data.len = size;
Etienne Carriere41343db2017-03-17 15:38:52 +0100161 alloc_data.flags = 0;
Etienne Carriere8bc31422017-08-24 14:48:30 +0200162 alloc_data.heap_id_mask = 1 << heap_data[idx].heap_id;
163 if (ioctl(ion, ION_IOC_ALLOC, &alloc_data) < 0) {
164 fprintf(stderr, "Error: failed to allocate in target heap\n");
Etienne Carriere41343db2017-03-17 15:38:52 +0100165 goto out;
Etienne Carriere8bc31422017-08-24 14:48:30 +0200166 }
Etienne Carriere41343db2017-03-17 15:38:52 +0100167
Etienne Carriere8bc31422017-08-24 14:48:30 +0200168 fd = alloc_data.fd;
Etienne Carriere41343db2017-03-17 15:38:52 +0100169out:
170 close(ion);
171 return fd;
172}
173
174static void finalize_tee_ctx(struct tee_ctx *ctx)
175{
176 if (!ctx)
177 return;
178
179 TEEC_CloseSession(&ctx->sess);
180 TEEC_FinalizeContext(&ctx->ctx);
181}
182
Etienne Carrierec45d9762017-03-21 15:45:13 +0100183static int create_tee_ctx(struct tee_ctx *ctx, enum test_target_ta target_ta)
Etienne Carriere41343db2017-03-17 15:38:52 +0100184{
185 TEEC_Result teerc;
Etienne Carriereb296a642017-03-29 15:23:56 +0200186 const TEEC_UUID *uuid;
Etienne Carriere41343db2017-03-17 15:38:52 +0100187 uint32_t err_origin;
188
Etienne Carrierec45d9762017-03-21 15:45:13 +0100189 switch (target_ta) {
190 case TEST_NS_TO_TA:
191 case TEST_TA_TO_TA:
192 case TEST_TA_TO_PTA:
Etienne Carriere50abf9a2017-03-24 11:33:50 +0100193 uuid = &sdp_basic_ta_uuid;
Etienne Carrierec45d9762017-03-21 15:45:13 +0100194 break;
195 case TEST_NS_TO_PTA:
Etienne Carriere50abf9a2017-03-24 11:33:50 +0100196 uuid = &pta_invoke_tests_ta_uuid;
Etienne Carrierec45d9762017-03-21 15:45:13 +0100197 break;
198 default:
199 return -1;
200 }
201
Etienne Carriere41343db2017-03-17 15:38:52 +0100202 teerc = TEEC_InitializeContext(NULL, &ctx->ctx);
203 if (teerc != TEEC_SUCCESS)
204 return -1;
205
Etienne Carrierec45d9762017-03-21 15:45:13 +0100206 teerc = TEEC_OpenSession(&ctx->ctx, &ctx->sess, uuid,
Etienne Carriere41343db2017-03-17 15:38:52 +0100207 TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
208 if (teerc != TEEC_SUCCESS)
Etienne Carrierec45d9762017-03-21 15:45:13 +0100209 fprintf(stderr, "Error: open session to target test %s failed %x %d\n",
210 (target_ta == TEST_NS_TO_PTA) ? "pTA" : "TA",
Etienne Carriere41343db2017-03-17 15:38:52 +0100211 teerc, err_origin);
212
213 return (teerc == TEEC_SUCCESS) ? 0 : -1;
214}
215
216static int tee_register_buffer(struct tee_ctx *ctx, void **shm_ref, int fd)
217{
218 TEEC_Result teerc;
219 TEEC_SharedMemory *shm;
220
221 shm = malloc(sizeof(*shm));
222 if (!shm)
223 return 1;
224
225 shm->flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
226 teerc = TEEC_RegisterSharedMemoryFileDescriptor(&ctx->ctx, shm, fd);
227 if (teerc != TEEC_SUCCESS) {
228 fprintf(stderr, "Error: TEEC_RegisterMemoryFileDescriptor() failed %x\n",
229 teerc);
230 return 1;
231 }
232
233 *shm_ref = shm;
234 return 0;
235}
236
237static void tee_deregister_buffer(struct tee_ctx *ctx, void *shm_ref)
238{
239 (void)ctx;
240
241 if (!shm_ref)
242 return;
243
244 TEEC_ReleaseSharedMemory((TEEC_SharedMemory *)shm_ref);
245 free(shm_ref);
246}
247
248static int inject_sdp_data(struct tee_ctx *ctx,
Etienne Carrieref690b912017-03-21 15:44:13 +0100249 void *in, size_t offset, size_t len, void *shm_ref, int ind)
Etienne Carriere41343db2017-03-17 15:38:52 +0100250{
251 TEEC_SharedMemory *shm = (TEEC_SharedMemory *)shm_ref;
252 TEEC_Result teerc;
253 TEEC_Operation op;
254 uint32_t err_origin;
Etienne Carrierec45d9762017-03-21 15:45:13 +0100255 unsigned cmd;
256
257 switch (ind) {
258 case TEST_NS_TO_TA:
259 cmd = TA_SDP_BASIC_CMD_INJECT;
260 break;
261 case TEST_TA_TO_TA:
262 cmd = TA_SDP_BASIC_CMD_INVOKE_INJECT;
263 break;
264 case TEST_TA_TO_PTA:
265 cmd = TA_SDP_BASIC_CMD_PTA_INJECT;
266 break;
267 case TEST_NS_TO_PTA:
268 cmd = PTA_INVOKE_TESTS_CMD_COPY_NSEC_TO_SEC;
269 break;
270 default:
271 return -1;
272 }
Etienne Carriere41343db2017-03-17 15:38:52 +0100273
274 memset(&op, 0, sizeof(op));
275 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
276 TEEC_MEMREF_PARTIAL_OUTPUT,
277 TEEC_NONE, TEEC_NONE);
278
279 op.params[0].tmpref.buffer = in;
280 op.params[0].tmpref.size = len;
281
282 op.params[1].memref.parent = shm;
283 op.params[1].memref.size = len;
284 op.params[1].memref.offset = offset;
285
Etienne Carrieref690b912017-03-21 15:44:13 +0100286 teerc = TEEC_InvokeCommand(&ctx->sess, cmd, &op, &err_origin);
Etienne Carriere41343db2017-03-17 15:38:52 +0100287 if (teerc != TEEC_SUCCESS)
288 fprintf(stderr, "Error: invoke SDP test TA (inject) failed %x %d\n",
289 teerc, err_origin);
290
291 return (teerc == TEEC_SUCCESS) ? 0 : -1;
292}
293
294static int transform_sdp_data(struct tee_ctx *ctx,
Etienne Carrieref690b912017-03-21 15:44:13 +0100295 size_t offset, size_t len, void *shm_ref, int ind)
Etienne Carriere41343db2017-03-17 15:38:52 +0100296{
297 TEEC_SharedMemory *shm = (TEEC_SharedMemory *)shm_ref;
298 TEEC_Result teerc;
299 TEEC_Operation op;
300 uint32_t err_origin;
Etienne Carrierec45d9762017-03-21 15:45:13 +0100301 unsigned cmd;
302
303 switch (ind) {
304 case TEST_NS_TO_TA:
305 cmd = TA_SDP_BASIC_CMD_TRANSFORM;
306 break;
307 case TEST_TA_TO_TA:
308 cmd = TA_SDP_BASIC_CMD_INVOKE_TRANSFORM;
309 break;
310 case TEST_TA_TO_PTA:
311 cmd = TA_SDP_BASIC_CMD_PTA_TRANSFORM;
312 break;
313 case TEST_NS_TO_PTA:
314 cmd = PTA_INVOKE_TESTS_CMD_READ_MODIFY_SEC;
315 break;
316 default:
317 return -1;
318 }
Etienne Carriere41343db2017-03-17 15:38:52 +0100319
320 memset(&op, 0, sizeof(op));
321 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
322 TEEC_NONE, TEEC_NONE, TEEC_NONE);
323 op.params[0].memref.parent = shm;
324 op.params[0].memref.size = len;
325 op.params[0].memref.offset = offset;
326
Etienne Carrieref690b912017-03-21 15:44:13 +0100327 teerc = TEEC_InvokeCommand(&ctx->sess, cmd, &op, &err_origin);
Etienne Carriere41343db2017-03-17 15:38:52 +0100328 if (teerc != TEEC_SUCCESS)
329 fprintf(stderr, "Error: invoke SDP test TA (transform) failed %x %d\n",
330 teerc, err_origin);
331
332 return (teerc == TEEC_SUCCESS) ? 0 : -1;
333}
334
335static int dump_sdp_data(struct tee_ctx *ctx,
Etienne Carrieref690b912017-03-21 15:44:13 +0100336 void *out, size_t offset, size_t len, void *shm_ref, int ind)
Etienne Carriere41343db2017-03-17 15:38:52 +0100337{
338 TEEC_SharedMemory *shm = (TEEC_SharedMemory *)shm_ref;
339 TEEC_Result teerc;
340 TEEC_Operation op;
341 uint32_t err_origin;
Etienne Carrierec45d9762017-03-21 15:45:13 +0100342 unsigned cmd;
343
344 switch (ind) {
345 case TEST_NS_TO_TA:
346 cmd = TA_SDP_BASIC_CMD_DUMP;
347 break;
348 case TEST_TA_TO_TA:
349 cmd = TA_SDP_BASIC_CMD_INVOKE_DUMP;
350 break;
351 case TEST_TA_TO_PTA:
352 cmd = TA_SDP_BASIC_CMD_PTA_DUMP;
353 break;
354 case TEST_NS_TO_PTA:
355 cmd = PTA_INVOKE_TESTS_CMD_COPY_SEC_TO_NSEC;
356 break;
357 default:
358 return -1;
359 }
Etienne Carriere41343db2017-03-17 15:38:52 +0100360
361 memset(&op, 0, sizeof(op));
362 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
363 TEEC_MEMREF_TEMP_OUTPUT,
364 TEEC_NONE, TEEC_NONE);
365 op.params[0].memref.parent = shm;
366 op.params[0].memref.size = len;
367 op.params[0].memref.offset = offset;
368
369 op.params[1].tmpref.buffer = out;
370 op.params[1].tmpref.size = len;
371
Etienne Carrieref690b912017-03-21 15:44:13 +0100372 teerc = TEEC_InvokeCommand(&ctx->sess, cmd, &op, &err_origin);
Etienne Carriere41343db2017-03-17 15:38:52 +0100373 if (teerc != TEEC_SUCCESS)
374 fprintf(stderr, "Error: invoke SDP test TA (dump) failed %x %d\n",
375 teerc, err_origin);
376
377 return (teerc == TEEC_SUCCESS) ? 0 : -1;
378}
379
380static int check_sdp_dumped(struct tee_ctx *ctx, void *ref, size_t len,
381 void *out)
382{
383 char *bref = (char *)ref;
384 char *data = (char *)out;
385 int err = 0;
386
387 (void)ctx;
388
389 while(len--)
390 if (*data++ != (unsigned char)(~(*bref++) + 1))
391 err++;
392
393 return err;
394}
395
396/*
397 * Consider 32kByte + 1 of random data is sufficient for an accurate test
398 * whatever the test buffer size is. Random buffer is read as a ring buffer.
399 */
400#define RANDOM_BUFFER_SIZE (32 * 1024 + 1)
401static int get_random_bytes(char *out, size_t len)
402{
403 static char *rand_buf = NULL;
404 static size_t rand_idx = 0;
405 int rc;
406
407 if (!rand_buf) {
408 const char rand_dev[] = "/dev/urandom";
409 int fd;
410
411 rand_buf = malloc(RANDOM_BUFFER_SIZE);
412 if (!rand_buf) {
413 fprintf(stderr, "failed to random buffer memory (%d bytes)\n",
414 RANDOM_BUFFER_SIZE);
415 return -1;
416 }
417
418 fd = open(rand_dev, O_RDONLY);
419 if (fd < 0) {
420 fprintf(stderr, "failed to open %s\n", rand_dev);
421 return -1;
422 }
423
424 rc = read(fd, rand_buf, RANDOM_BUFFER_SIZE);
425 if (rc != RANDOM_BUFFER_SIZE) {
426 fprintf(stderr, "failed to read %d bytes from %s\n",
427 RANDOM_BUFFER_SIZE, rand_dev);
428 return -1;
429 }
430 close(fd);
431 }
432
433 while (len) {
434 size_t t_len = (RANDOM_BUFFER_SIZE < len) ? RANDOM_BUFFER_SIZE : len;
435
436 if ((rand_idx + t_len) > RANDOM_BUFFER_SIZE) {
437 int sz_end = RANDOM_BUFFER_SIZE - rand_idx;
438 int sz_beg = t_len - sz_end;
439
440 memcpy(out, rand_buf + rand_idx, sz_end);
441 memcpy(out + sz_end, rand_buf , sz_beg);
442 rand_idx = sz_beg;
443 } else {
444 memcpy(out, rand_buf + rand_idx, t_len);
445 rand_idx += t_len;
446 }
447 len -= t_len;
448 }
449 return 0;
450}
451
452
Etienne Carriere50abf9a2017-03-24 11:33:50 +0100453int sdp_basic_test(enum test_target_ta ta, size_t size, size_t loop,
Etienne Carriereb9a95822017-04-26 15:03:53 +0200454 int ion_heap, int rnd_offset, int verbosity)
Etienne Carriere41343db2017-03-17 15:38:52 +0100455{
456 struct tee_ctx *ctx = NULL;
457 unsigned char *test_buf = NULL;
458 unsigned char *ref_buf = NULL;
459 void *shm_ref = NULL;
460 unsigned int err = 1;
461 int fd = -1;
462 size_t sdp_size = size;
463 size_t offset;
Etienne Carrieref690b912017-03-21 15:44:13 +0100464 size_t loop_cnt;
Etienne Carriere41343db2017-03-17 15:38:52 +0100465
466 if (!loop) {
467 fprintf(stderr, "Error: null loop value\n");
468 return 1;
469 }
470
471 /* reduce size to enable offset tests (max offset is 255 bytes) */
472 if (rnd_offset)
473 size -= 255;
474
475 test_buf = malloc(size);
476 ref_buf = malloc(size);
477 if (!test_buf || !ref_buf) {
478 verbose("failed to allocate memory\n");
479 goto out;
480 }
481
Etienne Carriereb9a95822017-04-26 15:03:53 +0200482 fd = allocate_ion_buffer(sdp_size, ion_heap, verbosity);
Etienne Carriere41343db2017-03-17 15:38:52 +0100483 if (fd < 0) {
Etienne Carriere5fdf6352017-03-24 11:49:50 +0100484 verbose("Failed to allocate SDP buffer (%zu bytes) in ION heap %d: %d\n",
Etienne Carriere41343db2017-03-17 15:38:52 +0100485 sdp_size, ion_heap, fd);
486 goto out;
487 }
488
489 /* register secure buffer to TEE */
490 ctx = malloc(sizeof(*ctx));
491 if (!ctx)
492 goto out;
Etienne Carrierec45d9762017-03-21 15:45:13 +0100493 if (create_tee_ctx(ctx, ta))
Etienne Carriere41343db2017-03-17 15:38:52 +0100494 goto out;
495 if (tee_register_buffer(ctx, &shm_ref, fd))
496 goto out;
497
498 /* release registered fd: tee should still hold refcount on resource */
499 close(fd);
500 fd = -1;
501
502 /* invoke trusted application with secure buffer as memref parameter */
Etienne Carrieref690b912017-03-21 15:44:13 +0100503 for (loop_cnt = loop; loop_cnt; loop_cnt--) {
Etienne Carriere41343db2017-03-17 15:38:52 +0100504 /* get an buffer of random-like values */
505 if (get_random_bytes((char *)ref_buf, size))
506 goto out;
507 memcpy(test_buf, ref_buf, size);
508 /* random offset [0 255] */
509 offset = (unsigned int)*ref_buf;
510
511 /* TA writes into SDP buffer */
Etienne Carrierec45d9762017-03-21 15:45:13 +0100512 if (inject_sdp_data(ctx, test_buf, offset, size, shm_ref, ta))
Etienne Carriere41343db2017-03-17 15:38:52 +0100513 goto out;
514
515 /* TA reads/writes into SDP buffer */
Etienne Carrierec45d9762017-03-21 15:45:13 +0100516 if (transform_sdp_data(ctx, offset, size, shm_ref, ta))
Etienne Carriere41343db2017-03-17 15:38:52 +0100517 goto out;
518
519 /* TA reads into SDP buffer */
Etienne Carrierec45d9762017-03-21 15:45:13 +0100520 if (dump_sdp_data(ctx, test_buf, offset, size, shm_ref, ta))
Etienne Carrieref690b912017-03-21 15:44:13 +0100521 goto out;
522
523 /* check dumped data are the expected ones */
524 if (check_sdp_dumped(ctx, ref_buf, size, test_buf)) {
525 fprintf(stderr, "check SDP data: %d errors\n", err);
526 goto out;
527 }
528 }
529
Etienne Carriere41343db2017-03-17 15:38:52 +0100530 err = 0;
Etienne Carriere41343db2017-03-17 15:38:52 +0100531out:
Etienne Carriere41343db2017-03-17 15:38:52 +0100532 if (fd >= 0)
533 close(fd);
534 if (shm_ref)
535 tee_deregister_buffer(ctx, shm_ref);
536 finalize_tee_ctx(ctx);
537 free(ctx);
538 free(ref_buf);
539 free(test_buf);
540 return err;
541}
542
543#define _TO_STR(x) #x
544#define TO_STR(x) _TO_STR(x)
545
546static void usage(const char *progname, size_t size, int loop, int ion_heap)
547{
548 fprintf(stderr, "Usage: %s [OPTION]\n", progname);
549 fprintf(stderr,
550 "Testing basic accesses to secure buffer (SDP) on OP-TEE.\n"
551 "Allocates a secure buffer and invoke a TA to access it.\n"
552 "TA is used to init/transform/dump the secure buffer.\n"
553 "CA check dumped content.\n\n");
554
555 fprintf(stderr, "Options:\n");
556 fprintf(stderr, " -h|--help Print this help and exit\n");
557 fprintf(stderr, " -v Be verbose\n");
558 fprintf(stderr, " -s SIZE SDP buffer byte size [%zu]\n", size);
559 fprintf(stderr, " -n LOOP Test loop iterations [%u]\n", loop);
560 fprintf(stderr, " --ion-heap ID Target ION heap ID [%d]\n", ion_heap);
561 fprintf(stderr, " --no-offset No random offset [0 255] in buffer\n");
562}
563
564#define NEXT_ARG(i) \
565 do { \
566 if (++i == argc) { \
567 fprintf(stderr, "%s: %s: missing argument\n", \
568 argv[0], argv[i-1]); \
569 return 1; \
570 } \
571 } while (0);
572
Jerome Forissiere1342522017-05-19 09:25:53 +0200573#define CHECK_RESULT(_res, _exp, _action) \
574 if ((_res) == (_exp)) { \
575 verbose("Test passed\n"); \
576 } else { \
577 verbose("Test failed!\n"); \
578 _action; \
579 }
580
Etienne Carriere41343db2017-03-17 15:38:52 +0100581int sdp_basic_runner_cmd_parser(int argc, char *argv[])
582{
583 size_t test_size = 5000;
584 size_t test_loop = 1000;
585 int ion_heap = DEFAULT_ION_HEAP_TYPE;
586 int rnd_offset = 1;
Etienne Carriereb9a95822017-04-26 15:03:53 +0200587 int verbosity = 1;
Jerome Forissiere1342522017-05-19 09:25:53 +0200588 int err;
Etienne Carriere41343db2017-03-17 15:38:52 +0100589 int i;
590
591 /* Parse command line */
592 for (i = 1; i < argc; i++) {
593 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
594 usage(argv[0], test_size, test_loop, ion_heap);
595 return 0;
596 }
597 }
598 for (i = 1; i < argc; i++) {
599 if (!strcmp(argv[i], "-v")) {
600 verbosity++;
601 } else if (!strcmp(argv[i], "-s")) {
602 NEXT_ARG(i);
603 test_size = atoi(argv[i]);
604 } else if (!strcmp(argv[i], "-n")) {
605 NEXT_ARG(i);
606 test_loop = atoi(argv[i]);
607 } else if (!strcmp(argv[i], "--ion-heap")) {
608 NEXT_ARG(i);
609 ion_heap = atoi(argv[i]);
610 } else if (!strcmp(argv[i], "--no-offset")) {
611 rnd_offset = 0;
612 } else {
613 fprintf(stderr, "%s: invalid argument: %s\n",
614 argv[0], argv[i]);
615 usage(argv[0], test_size, test_loop, ion_heap);
616 return 1;
617 }
618 }
619
Jerome Forissiere1342522017-05-19 09:25:53 +0200620 verbose("\nSecure Data Path basic access: "
621 "NS invokes SDP TA\n");
622 err = sdp_basic_test(TEST_NS_TO_TA, test_size, test_loop, ion_heap,
623 rnd_offset, verbosity);
624 CHECK_RESULT(err, 0, return 1);
Etienne Carriere41343db2017-03-17 15:38:52 +0100625
Jerome Forissiere1342522017-05-19 09:25:53 +0200626 verbose("\nSecure Data Path basic access: "
627 "SDP TA invokes SDP TA\n");
628 err = sdp_basic_test(TEST_TA_TO_TA, test_size, test_loop, ion_heap,
629 rnd_offset, verbosity);
630 CHECK_RESULT(err, 0, return 1);
Etienne Carrierec45d9762017-03-21 15:45:13 +0100631
Jerome Forissiere1342522017-05-19 09:25:53 +0200632 verbose("\nSecure Data Path basic access: "
633 "SDP TA invokes SDP pTA\n");
634 err = sdp_basic_test(TEST_TA_TO_PTA, test_size, test_loop, ion_heap,
635 rnd_offset, verbosity);
636 CHECK_RESULT(err, 0, return 1);
Etienne Carriere41343db2017-03-17 15:38:52 +0100637
Jerome Forissiere1342522017-05-19 09:25:53 +0200638 verbose("\nSecure Data Path basic access: "
639 "NS invokes SDP pTA (shall fail)\n");
640 err = sdp_basic_test(TEST_NS_TO_PTA, test_size, test_loop, ion_heap,
641 rnd_offset, verbosity);
642 CHECK_RESULT(err, 1, return 1);
Etienne Carriered1655822017-03-21 15:45:24 +0100643
Etienne Carriere41343db2017-03-17 15:38:52 +0100644 return 0;
645}