Skip to content

Commit 7b383d0

Browse files
gpouliosxiaoxiang781216
authored andcommitted
examples/optee_gp: Add a OP-TEE GP API client example
Add an example app that opens a session with the devices pseudo-TA and enumerates the available devices (prints their UUIDs only) using the GlobalPlatform API and libteec. The example showcases: - initializing the context - opening a session - invoking a command using NULL references - invoking a command using temp shared memory - invoking a command using registered shared memory - closing the session - finalizing the context Enabled with CONFIG_EXAMPLES_OPTEE_GP. Signed-off-by: George Poulios <[email protected]>
1 parent 67db0af commit 7b383d0

File tree

5 files changed

+312
-0
lines changed

5 files changed

+312
-0
lines changed

examples/optee_gp/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# ##############################################################################
2+
# apps/examples/optee_gp/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
if(CONFIG_EXAMPLES_OPTEE_GP)
24+
nuttx_add_application(
25+
NAME
26+
${CONFIG_EXAMPLES_OPTEE_GP_PROGNAME}
27+
SRCS
28+
optee_gp_main.c
29+
STACKSIZE
30+
${CONFIG_EXAMPLES_OPTEE_GP_STACKSIZE}
31+
PRIORITY
32+
${CONFIG_EXAMPLES_OPTEE_GP_PRIORITY})
33+
endif()

examples/optee_gp/Kconfig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_OPTEE_GP
7+
tristate "OP-TEE GP API client example"
8+
depends on LIBTEEC
9+
default n
10+
---help---
11+
Enable the OP-TEE GP API client example which uses libteec
12+
13+
if EXAMPLES_OPTEE
14+
15+
config EXAMPLES_OPTEE_GP_PROGNAME
16+
string "Program name"
17+
default "optee_gp"
18+
---help---
19+
This is the name of the program that will be used when the NSH ELF
20+
program is installed.
21+
22+
config EXAMPLES_OPTEE_GP_PRIORITY
23+
int "OP-TEE GP task priority"
24+
default 100
25+
26+
config EXAMPLES_OPTEE_GP_STACKSIZE
27+
int "OP-TEE GP stack size"
28+
default DEFAULT_TASK_STACKSIZE
29+
30+
endif

examples/optee_gp/Make.defs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
############################################################################
2+
# apps/examples/optee_gp/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
ifneq ($(CONFIG_EXAMPLES_OPTEE_GP),)
24+
CONFIGURED_APPS += $(APPDIR)/examples/optee_gp
25+
endif

examples/optee_gp/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
############################################################################
2+
# apps/examples/optee_gp/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
include $(APPDIR)/Make.defs
24+
25+
# OP-TEE GP API client built-in application info
26+
27+
PROGNAME = $(CONFIG_EXAMPLES_OPTEE_GP_PROGNAME)
28+
PRIORITY = $(CONFIG_EXAMPLES_OPTEE_GP_PRIORITY)
29+
STACKSIZE = $(CONFIG_EXAMPLES_OPTEE_GP_STACKSIZE)
30+
MODULE = $(CONFIG_EXAMPLES_OPTEE_GP)
31+
32+
MAINSRC = optee_gp_main.c
33+
34+
include $(APPDIR)/Application.mk

examples/optee_gp/optee_gp_main.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/****************************************************************************
2+
* apps/examples/optee_gp/optee_gp_main.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <nuttx/tee.h>
29+
#include <tee_client_api.h>
30+
#include <teec_trace.h>
31+
#include <uuid.h>
32+
33+
/****************************************************************************
34+
* Pre-processor definitions
35+
****************************************************************************/
36+
37+
/* This UUID is taken from the OP-TEE OS built-in pseudo TA:
38+
* https://github.com/OP-TEE/optee_os/blob/4.6.0/
39+
* lib/libutee/include/pta_device.h
40+
*/
41+
#define PTA_DEVICE_ENUM_UUID \
42+
{ \
43+
0x7011a688, 0xddde, 0x4053, \
44+
{ \
45+
0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8 \
46+
} \
47+
}
48+
49+
#define PTA_CMD_GET_DEVICES 0x0
50+
51+
/****************************************************************************
52+
* Public Functions
53+
****************************************************************************/
54+
55+
/****************************************************************************
56+
* optee_gp_main
57+
****************************************************************************/
58+
59+
int main(int argc, FAR char *argv[])
60+
{
61+
TEEC_Result res;
62+
TEEC_Context ctx;
63+
TEEC_Session sess;
64+
TEEC_Operation op;
65+
TEEC_UUID uuid = PTA_DEVICE_ENUM_UUID;
66+
void *buf;
67+
TEEC_SharedMemory io_shm;
68+
uint32_t err_origin;
69+
unsigned int count;
70+
const uuid_t *raw_ta_uuid;
71+
uuid_t ta_uuid;
72+
char *ta_uuid_s;
73+
74+
res = TEEC_InitializeContext(NULL, &ctx);
75+
if (res != TEEC_SUCCESS)
76+
{
77+
EMSG("TEEC_InitializeContext failed with code 0x%08x\n", res);
78+
goto exit;
79+
}
80+
81+
memset(&op, 0, sizeof(op));
82+
83+
/* Open a session with the devices pseudo TA */
84+
85+
res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
86+
&op, &err_origin);
87+
if (res != TEEC_SUCCESS)
88+
{
89+
EMSG("TEEC_Opensession failed with code 0x%08x origin 0x%08x", res,
90+
err_origin);
91+
goto exit_with_ctx;
92+
}
93+
94+
/* Invoke command with NULL buffer to get required size */
95+
96+
op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE,
97+
TEEC_NONE, TEEC_NONE);
98+
op.params[0].tmpref.buffer = NULL;
99+
op.params[0].tmpref.size = 0;
100+
101+
res = TEEC_InvokeCommand(&sess, PTA_CMD_GET_DEVICES, &op, &err_origin);
102+
if (err_origin != TEEC_ORIGIN_TRUSTED_APP ||
103+
res != TEEC_ERROR_SHORT_BUFFER)
104+
{
105+
EMSG("TEEC_InvokeCommand failed: code 0x%08x origin 0x%08x",
106+
res, err_origin);
107+
goto exit_with_session;
108+
}
109+
110+
/* Invoke command using temporary memory */
111+
112+
op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE,
113+
TEEC_NONE, TEEC_NONE);
114+
115+
op.params[0].tmpref.buffer = buf = malloc(op.params[0].tmpref.size);
116+
if (!op.params[0].tmpref.buffer)
117+
{
118+
EMSG("Failed to allocate %zu bytes of memory to share with TEE",
119+
op.params[0].tmpref.size);
120+
goto exit_with_session;
121+
}
122+
123+
res = TEEC_InvokeCommand(&sess, PTA_CMD_GET_DEVICES, &op, &err_origin);
124+
if (res != TEEC_SUCCESS)
125+
{
126+
EMSG("TEEC_InvokeCommand failed: code 0x%08x origin 0x%08x",
127+
res, err_origin);
128+
goto exit_with_buf;
129+
}
130+
131+
/* Invoke command using pre-allocated, pre-registered memory */
132+
133+
io_shm.size = op.params[0].tmpref.size;
134+
io_shm.flags = TEEC_MEM_OUTPUT;
135+
res = TEEC_AllocateSharedMemory(&ctx, &io_shm);
136+
if (res != TEEC_SUCCESS)
137+
{
138+
EMSG("TEEC_AllocateSharedMemory failed: code 0x%08x", res);
139+
goto exit_with_buf;
140+
}
141+
142+
op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_WHOLE, TEEC_NONE, TEEC_NONE,
143+
TEEC_NONE);
144+
op.params[0].memref.parent = &io_shm;
145+
146+
res = TEEC_InvokeCommand(&sess, PTA_CMD_GET_DEVICES, &op, &err_origin);
147+
if (res != TEEC_SUCCESS)
148+
{
149+
EMSG("TEEC_InvokeCommand failed: code 0x%08x origin 0x%08x",
150+
res, err_origin);
151+
goto exit_with_shm;
152+
}
153+
154+
/* Sanity check that both outputs are the same */
155+
156+
if (memcmp(buf, io_shm.buffer, io_shm.size))
157+
{
158+
EMSG("Different results with temp vs registered memory");
159+
goto exit_with_shm;
160+
}
161+
162+
/* Print results to stdout */
163+
164+
IMSG("Available devices:");
165+
166+
count = io_shm.size / sizeof(uuid_t);
167+
raw_ta_uuid = (uuid_t *)io_shm.buffer;
168+
169+
while (count--)
170+
{
171+
uuid_dec_be(raw_ta_uuid, &ta_uuid);
172+
uuid_to_string(&ta_uuid, &ta_uuid_s, NULL);
173+
174+
IMSG(" %s", ta_uuid_s);
175+
176+
free(ta_uuid_s);
177+
raw_ta_uuid++;
178+
}
179+
180+
exit_with_shm:
181+
TEEC_ReleaseSharedMemory(&io_shm);
182+
exit_with_buf:
183+
free(buf);
184+
exit_with_session:
185+
TEEC_CloseSession(&sess);
186+
exit_with_ctx:
187+
TEEC_FinalizeContext(&ctx);
188+
exit:
189+
return res;
190+
}

0 commit comments

Comments
 (0)