1 /*
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 /* For the UUID (found in the TA's h-file(s)) */
36 #include <hello_world_ta.h>
37
main(void)38 int main(void)
39 {
40 TEEC_Result res;
41 TEEC_Context ctx;
42 TEEC_Session sess;
43 TEEC_Operation op;
44 TEEC_UUID uuid = TA_HELLO_WORLD_UUID;
45 uint32_t err_origin;
46
47 /* Initialize a context connecting us to the TEE */
48 res = TEEC_InitializeContext(NULL, &ctx);
49 if (res != TEEC_SUCCESS)
50 errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
51
52 /*
53 * Open a session to the "hello world" TA, the TA will print "hello
54 * world!" in the log when the session is created.
55 */
56 res = TEEC_OpenSession(&ctx, &sess, &uuid,
57 TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
58 if (res != TEEC_SUCCESS)
59 errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
60 res, err_origin);
61
62 /*
63 * Execute a function in the TA by invoking it, in this case
64 * we're incrementing a number.
65 *
66 * The value of command ID part and how the parameters are
67 * interpreted is part of the interface provided by the TA.
68 */
69
70 /* Clear the TEEC_Operation struct */
71 memset(&op, 0, sizeof(op));
72
73 /*
74 * Prepare the argument. Pass a value in the first parameter,
75 * the remaining three parameters are unused.
76 */
77 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE,
78 TEEC_NONE, TEEC_NONE);
79 op.params[0].value.a = 42;
80
81 /*
82 * TA_HELLO_WORLD_CMD_INC_VALUE is the actual function in the TA to be
83 * called.
84 */
85 printf("Invoking TA to increment %d\n", op.params[0].value.a);
86 res = TEEC_InvokeCommand(&sess, TA_HELLO_WORLD_CMD_INC_VALUE, &op,
87 &err_origin);
88 if (res != TEEC_SUCCESS)
89 errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
90 res, err_origin);
91 printf("TA incremented value to %d\n", op.params[0].value.a);
92
93 /*
94 * We're done with the TA, close the session and
95 * destroy the context.
96 *
97 * The TA will print "Goodbye!" in the log when the
98 * session is closed.
99 */
100
101 TEEC_CloseSession(&sess);
102
103 TEEC_FinalizeContext(&ctx);
104
105 return 0;
106 }
107