blob: de857b91edbf60eb2b3fee96ac9d8fed64935361 [file] [log] [blame]
Igor Opaniuk06fe31c2017-08-07 02:37:51 +03001/*
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 <stdio.h>
30#include <string.h>
31
32/* OP-TEE TEE client API (built by optee_client) */
33#include <tee_client_api.h>
34
35/* To the the UUID (found the the TA's h-file(s)) */
Etienne Carriered3cb2762017-11-09 15:45:53 +010036#include <random_ta.h>
Igor Opaniuk06fe31c2017-08-07 02:37:51 +030037
38int main(int argc, char *argv[])
39{
40 TEEC_Result res;
41 TEEC_Context ctx;
42 TEEC_Session sess;
43 TEEC_Operation op = { 0 };
Etienne Carriered3cb2762017-11-09 15:45:53 +010044 TEEC_UUID uuid = TA_RANDOM_UUID;
Igor Opaniuk06fe31c2017-08-07 02:37:51 +030045 uint8_t random_uuid[16] = { 0 };
46 uint32_t err_origin;
47
48 /* Initialize a context connecting us to the TEE */
49 res = TEEC_InitializeContext(NULL, &ctx);
50 if (res != TEEC_SUCCESS)
51 errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
52
53 /*
54 * Open a session to the Random example TA, the TA will print "hello
55 * world!" in the log when the session is created.
56 */
57 res = TEEC_OpenSession(&ctx, &sess, &uuid,
58 TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
59 if (res != TEEC_SUCCESS)
60 errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
61 res, err_origin);
62
63 /*
64 * Execute a function in the TA by invoking it, in this case
65 * we're incrementing a number.
66 *
67 * The value of command ID part and how the parameters are
68 * interpreted is part of the interface provided by the TA.
69 */
70
71 /* Clear the TEEC_Operation struct */
72 memset(&op, 0, sizeof(op));
73
74 /*
75 * Prepare the argument. Pass a value in the first parameter,
76 * the remaining three parameters are unused.
77 */
Etienne Carriered3cb2762017-11-09 15:45:53 +010078 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
79 TEEC_NONE, TEEC_NONE, TEEC_NONE);
Igor Opaniuk06fe31c2017-08-07 02:37:51 +030080 op.params[0].tmpref.buffer = random_uuid;
81 op.params[0].tmpref.size = sizeof(random_uuid);
82
83 /*
84 * TA_EXAMPLE_RANDOM_GENERATE is the actual function in the TA to be
85 * called.
86 */
87 printf("Invoking TA to generate random UUID... \n");
Etienne Carriered3cb2762017-11-09 15:45:53 +010088 res = TEEC_InvokeCommand(&sess, TA_RANDOM_CMD_GENERATE,
89 &op, &err_origin);
Igor Opaniuk06fe31c2017-08-07 02:37:51 +030090 if (res != TEEC_SUCCESS)
91 errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
92 res, err_origin);
93
94 printf("TA generated UUID value = 0x");
95 for (int i = 0; i < 16; i++)
96 printf("%x", random_uuid[i]);
97 printf("\n");
98
99 /*
100 * We're done with the TA, close the session and
101 * destroy the context.
102 *
103 * The TA will print "Goodbye!" in the log when the
104 * session is closed.
105 */
106
107 TEEC_CloseSession(&sess);
108
109 TEEC_FinalizeContext(&ctx);
110
111 return 0;
112}