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 <random_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 = { 0 };
44 	TEEC_UUID uuid = TA_RANDOM_UUID;
45 	uint8_t random_uuid[16] = { 0 };
46 	uint32_t err_origin;
47 	int i;
48 
49 	/* Initialize a context connecting us to the TEE */
50 	res = TEEC_InitializeContext(NULL, &ctx);
51 	if (res != TEEC_SUCCESS)
52 		errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
53 
54 	/*
55 	 * Open a session to the Random example TA, the TA will print "hello
56 	 * world!" in the log when the session is created.
57 	 */
58 	res = TEEC_OpenSession(&ctx, &sess, &uuid,
59 			       TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
60 	if (res != TEEC_SUCCESS)
61 		errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
62 			res, err_origin);
63 
64 	/*
65 	 * Execute a function in the TA by invoking it, in this case
66 	 * we're incrementing a number.
67 	 *
68 	 * The value of command ID part and how the parameters are
69 	 * interpreted is part of the interface provided by the TA.
70 	 */
71 
72 	/* Clear the TEEC_Operation struct */
73 	memset(&op, 0, sizeof(op));
74 
75 	/*
76 	 * Prepare the argument. Pass a value in the first parameter,
77 	 * the remaining three parameters are unused.
78 	 */
79 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
80 					 TEEC_NONE, TEEC_NONE, TEEC_NONE);
81 	op.params[0].tmpref.buffer = random_uuid;
82 	op.params[0].tmpref.size = sizeof(random_uuid);
83 
84 	/*
85 	 * TA_EXAMPLE_RANDOM_GENERATE is the actual function in the TA to be
86 	 * called.
87 	 */
88 	printf("Invoking TA to generate random UUID... \n");
89 	res = TEEC_InvokeCommand(&sess, TA_RANDOM_CMD_GENERATE,
90 				 &op, &err_origin);
91 	if (res != TEEC_SUCCESS)
92 		errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
93 			res, err_origin);
94 
95 	printf("TA generated UUID value = 0x");
96 	for (i = 0; i < 16; i++)
97 		printf("%x", random_uuid[i]);
98 	printf("\n");
99 
100 	/*
101 	 * We're done with the TA, close the session and
102 	 * destroy the context.
103 	 *
104 	 * The TA will print "Goodbye!" in the log when the
105 	 * session is closed.
106 	 */
107 
108 	TEEC_CloseSession(&sess);
109 
110 	TEEC_FinalizeContext(&ctx);
111 
112 	return 0;
113 }
114