Skip to content

Commit 8ea8897

Browse files
author
Hernan Gatta
committed
ocall: add an example to show how to use OCALLs
The example first initializes a TEE context using the new settings API. It then opens a session against the example OCALL TA via the new API to configure sessions. Thereafter, it performs a function invocation into the OCALL TA to request that the latter call it back. The OCALL includes value and memref parameters that are passed in multiple directions. Via these parameters the TA and CA exchange information the same way they would during a normal CA-to-TA function invocation. Signed-off-by: Hernan Gatta <[email protected]>
1 parent f7f5a3a commit 8ea8897

File tree

11 files changed

+519
-0
lines changed

11 files changed

+519
-0
lines changed

ocall/Android.mk

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
###################### optee-hello-world ######################
2+
LOCAL_PATH := $(call my-dir)
3+
4+
OPTEE_CLIENT_EXPORT = $(LOCAL_PATH)/../../optee_client/out/export
5+
6+
include $(CLEAR_VARS)
7+
LOCAL_CFLAGS += -DANDROID_BUILD
8+
LOCAL_CFLAGS += -Wall
9+
10+
LOCAL_SRC_FILES += host/main.c
11+
12+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/ta/include \
13+
$(OPTEE_CLIENT_EXPORT)/include \
14+
15+
LOCAL_SHARED_LIBRARIES := libteec
16+
LOCAL_MODULE := optee_example_ocall
17+
LOCAL_VENDOR_MODULE := true
18+
LOCAL_MODULE_TAGS := optional
19+
include $(BUILD_EXECUTABLE)
20+
21+
include $(LOCAL_PATH)/ta/Android.mk

ocall/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
project (optee_example_ocall C)
2+
3+
set (SRC host/main.c)
4+
5+
add_executable (${PROJECT_NAME} ${SRC})
6+
7+
target_include_directories(${PROJECT_NAME}
8+
PRIVATE ta/include
9+
PRIVATE include)
10+
11+
target_link_libraries (${PROJECT_NAME} PRIVATE teec)
12+
13+
install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})

ocall/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export V?=0
2+
3+
# If _HOST or _TA specific compilers are not specified, then use CROSS_COMPILE
4+
HOST_CROSS_COMPILE ?= $(CROSS_COMPILE)
5+
TA_CROSS_COMPILE ?= $(CROSS_COMPILE)
6+
7+
.PHONY: all
8+
all:
9+
$(MAKE) -C host CROSS_COMPILE="$(HOST_CROSS_COMPILE)" --no-builtin-variables
10+
$(MAKE) -C ta CROSS_COMPILE="$(TA_CROSS_COMPILE)" LDFLAGS=""
11+
12+
.PHONY: clean
13+
clean:
14+
$(MAKE) -C host clean
15+
$(MAKE) -C ta clean

ocall/host/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CC ?= $(CROSS_COMPILE)gcc
2+
LD ?= $(CROSS_COMPILE)ld
3+
AR ?= $(CROSS_COMPILE)ar
4+
NM ?= $(CROSS_COMPILE)nm
5+
OBJCOPY ?= $(CROSS_COMPILE)objcopy
6+
OBJDUMP ?= $(CROSS_COMPILE)objdump
7+
READELF ?= $(CROSS_COMPILE)readelf
8+
9+
OBJS = main.o
10+
11+
CFLAGS += -Wall -I../ta/include -I$(TEEC_EXPORT)/include -I./include
12+
#Add/link other required libraries here
13+
LDADD += -lteec -L$(TEEC_EXPORT)/lib
14+
15+
BINARY = optee_example_hello_world
16+
17+
.PHONY: all
18+
all: $(BINARY)
19+
20+
$(BINARY): $(OBJS)
21+
$(CC) -o $@ $< $(LDADD)
22+
23+
.PHONY: clean
24+
clean:
25+
rm -f $(OBJS) $(BINARY)
26+
27+
%.o: %.c
28+
$(CC) $(CFLAGS) -c $< -o $@

ocall/host/main.c

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
/*
2+
* Copyright (c) 2020, Microsoft Corporation
3+
* All rights reserved.
4+
*
5+
* SPDX-License-Identifier: BSD-2-Clause
6+
*/
7+
8+
#include <err.h>
9+
#include <stdio.h>
10+
#include <string.h>
11+
12+
#include <tee_client_api.h>
13+
#include <tee_client_api_extensions.h>
14+
15+
#include <ocall_ta.h>
16+
17+
#define PTR_ADD(ptr, offs) ((void *)((uintptr_t)(ptr) + (uintptr_t)(offs)))
18+
#define GET_BUF(p) (PTR_ADD((p).memref.parent->buffer, (p).memref.offset))
19+
20+
static void print_uuid(TEEC_UUID *uuid)
21+
{
22+
printf("%x-%x-%x-%x%x-%x%x%x%x%x%x",
23+
uuid->timeLow,
24+
uuid->timeMid,
25+
uuid->timeHiAndVersion,
26+
uuid->clockSeqAndNode[0],
27+
uuid->clockSeqAndNode[1],
28+
uuid->clockSeqAndNode[2],
29+
uuid->clockSeqAndNode[3],
30+
uuid->clockSeqAndNode[4],
31+
uuid->clockSeqAndNode[5],
32+
uuid->clockSeqAndNode[6],
33+
uuid->clockSeqAndNode[7]);
34+
}
35+
36+
/*
37+
* This function is called by the TEE Client API whenever an OCALL arrives from
38+
* the TA.
39+
*
40+
* The 'taUUID' parameter carries the UUID of the TA that sent the OCALL. Since
41+
* a TA can open a session to another TA, it is possible to receive OCALLs from
42+
* other TAs that your TA calls into, if any.
43+
*
44+
* The 'commandId' indicates which function the TA wishes the CA to run.
45+
*
46+
* 'ctxData' is the arbitrary pointer that was set via the TEE context OCALL
47+
* setting, if any. Similarly, 'sessionData' is the arbitrary pointer set via
48+
* the session data setting, if it was supplied, or NULL.
49+
*
50+
* NOTE: Notice that the OCALL carries memory references. Currently, OP-TEE
51+
* takes any memref parameter that originates in the TA and marshals it to
52+
* the CA in a single shared memory object. Hence, the two buffers passed
53+
* from the TA wind up at different offsets within the same memory
54+
* reference. Therefore, it is necessary to take the offset into
55+
* consideration while manipulating these buffers.
56+
*/
57+
TEEC_Result ocall_handler(TEEC_UUID *taUUID, uint32_t commandId,
58+
uint32_t paramTypes,
59+
TEEC_Parameter params[TEEC_CONFIG_PAYLOAD_REF_COUNT],
60+
void *ctxData, void *sessionData)
61+
{
62+
printf("Received an OCALL for Command Id: %u\n", commandId);
63+
64+
printf("The TA that sent it is: ");
65+
print_uuid(taUUID);
66+
printf("\n");
67+
68+
uint32_t expected_pt;
69+
70+
char *msg = "This string was sent by the CA";
71+
72+
switch (commandId) {
73+
case CA_OCALL_CMD_REPLY_TA:
74+
expected_pt = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
75+
TEEC_VALUE_INOUT,
76+
TEEC_MEMREF_TEMP_INPUT,
77+
TEEC_MEMREF_TEMP_INOUT);
78+
if (paramTypes != expected_pt) {
79+
printf("Bad param types\n");
80+
return TEEC_ERROR_BAD_PARAMETERS;
81+
}
82+
83+
if (!params[2].memref.parent || !params[3].memref.parent) {
84+
printf("No buffer(s)\n");
85+
return TEEC_ERROR_BAD_PARAMETERS;
86+
}
87+
88+
printf("Input values: %u, %u\n", params[0].value.a,
89+
params[0].value.b);
90+
printf("Inout values: %u, %u\n", params[1].value.a,
91+
params[1].value.b);
92+
93+
printf("Input string: %s\n", (char *)GET_BUF(params[2]));
94+
printf("Input size: %zu\n", params[2].memref.size);
95+
96+
printf("Inout string: %s\n", (char *)GET_BUF(params[3]));
97+
printf("Inout size: %zu\n", params[3].memref.size);
98+
99+
/* Set the OCALL's INOUT parameters */
100+
params[1].value.a = 0x3;
101+
params[1].value.b = 0x4;
102+
103+
params[3].memref.size = strlen(msg) + 1;
104+
memcpy(GET_BUF(params[3]), msg, params[3].memref.size);
105+
106+
printf("OCALL handled\n");
107+
break;
108+
default:
109+
printf("Bad function ID\n");
110+
return TEEC_ERROR_BAD_PARAMETERS;
111+
}
112+
return TEEC_SUCCESS;
113+
}
114+
115+
int main(int argc, char* argv[])
116+
{
117+
TEEC_Context ctx;
118+
TEEC_Session sess;
119+
TEEC_UUID uuid = TA_OCALL_UUID;
120+
TEEC_Operation op = { 0 };
121+
122+
TEEC_Result res;
123+
uint32_t err_origin;
124+
125+
char buf[128];
126+
char *msg1 = "This string was sent by the CA";
127+
const char *msg2 = "The CA thinks this is a fun riddle";
128+
129+
/*
130+
* The TEE context OCALL setting allows setting the callback handler for
131+
* when an OCALL arrives from the TA. This handler is effectively the
132+
* equivalent of TA_InvokeCommandEntryPoint. Additionally, one may set
133+
* an arbitrary pointer that will be passed to the OCALL handler when
134+
* invoked.
135+
*
136+
* NOTE: You must pass this setting to the TEE context initialization
137+
* routine to receive OCALLs.
138+
*/
139+
TEEC_ContextSettingOcall ocall_setting = {
140+
.handler = ocall_handler,
141+
.data = &ctx,
142+
};
143+
144+
/* Array of TEE context settings */
145+
TEEC_ContextSetting ctx_settings = {
146+
.type = TEEC_CONTEXT_SETTING_OCALL,
147+
.u.ocall = &ocall_setting,
148+
};
149+
150+
/* Initialize a TEE context with settings */
151+
res = TEEC_InitializeContext2(NULL, &ctx, &ctx_settings, 1);
152+
if (res != TEEC_SUCCESS)
153+
errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
154+
155+
/*
156+
* The session data settings allows attaching an arbitrary pointer to
157+
* the session. This pointer will be passed to the OCALL handler when
158+
* invoked.
159+
*
160+
* NOTE: This is optional; you can use TEEC_OpenSession as well even if
161+
* you expect OCALLs.
162+
*/
163+
TEEC_SessionSettingData data_setting = {
164+
.data = &sess
165+
};
166+
167+
/* Array of session settings */
168+
TEEC_SessionSetting session_settings = {
169+
.type = TEEC_SESSION_SETTING_DATA,
170+
.u.data = &data_setting,
171+
};
172+
173+
/* Open a session with settings */
174+
res = TEEC_OpenSession2(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
175+
NULL, &err_origin, &session_settings, 1);
176+
if (res != TEEC_SUCCESS)
177+
errx(1, "TEEC_OpenSessionEx failed with code 0x%x origin 0x%x",
178+
res, err_origin);
179+
180+
/*
181+
* Set up the parameters for the function invocation. These are just to
182+
* show that the CA can pass parameters to the TA and that during the
183+
* function invocation that carries those parameters to the TA, the TA
184+
* can make an OCALL with parameters of its own choosing. That is, the
185+
* parameters passed from the CA to the TA do not interfere with those
186+
* passed from the TA to the CA, and vice-versa.
187+
*/
188+
op.paramTypes = TEEC_PARAM_TYPES(
189+
TEEC_VALUE_INPUT,
190+
TEEC_VALUE_INOUT,
191+
TEEC_MEMREF_TEMP_INPUT,
192+
TEEC_MEMREF_TEMP_INOUT);
193+
194+
op.params[0].value.a = 0x3;
195+
op.params[0].value.b = 0x4;
196+
197+
op.params[1].value.a = 0x5;
198+
op.params[1].value.b = 0x6;
199+
200+
op.params[2].tmpref.buffer = msg1;
201+
op.params[2].tmpref.size = strlen(msg1) + 1;
202+
203+
op.params[3].tmpref.buffer = buf;
204+
op.params[3].tmpref.size = sizeof(buf);
205+
memcpy(buf, msg2, strlen(msg2) + 1);
206+
207+
/* Ask the TA to call us back */
208+
res = TEEC_InvokeCommand(&sess, TA_OCALL_CMD_CALL_CA, &op, &err_origin);
209+
if (res != TEEC_SUCCESS)
210+
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
211+
res, err_origin);
212+
213+
/*
214+
* The code below executes after the OCALL has been handled in the
215+
* callback at the top of this file.
216+
*/
217+
218+
/*
219+
* Print out the values of the INOUT parameters of the original function
220+
* invocation that we got from the TA..
221+
*/
222+
printf("INOUT parameters from the original function invocation:\n");
223+
printf("Inout values: %u, %u\n", op.params[1].value.a,
224+
op.params[1].value.b);
225+
226+
printf("Inout string: %s\n", (char *)op.params[3].tmpref.buffer);
227+
printf("Inout size: %zu\n", op.params[3].tmpref.size);
228+
229+
/* All done */
230+
TEEC_CloseSession(&sess);
231+
232+
TEEC_FinalizeContext(&ctx);
233+
234+
return 0;
235+
}

ocall/ta/Android.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
LOCAL_PATH := $(call my-dir)
2+
3+
local_module := 9b2c0652-3b9b-4d83-971e-e56c40512793.ta
4+
include $(BUILD_OPTEE_MK)

ocall/ta/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CFG_TEE_TA_LOG_LEVEL ?= 4
2+
CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL)
3+
4+
# The UUID for the Trusted Application
5+
BINARY=9b2c0652-3b9b-4d83-971e-e56c40512793
6+
7+
-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk
8+
9+
ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), )
10+
clean:
11+
@echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA'
12+
@echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)'
13+
endif

ocall/ta/include/ocall_ta.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2020, Microsoft Corporation
3+
* All rights reserved.
4+
*
5+
* SPDX-License-Identifier: BSD-2-Clause
6+
*/
7+
8+
#ifndef TA_OCALL_H
9+
#define TA_OCALL_H
10+
11+
/* 9b2c0652-3b9b-4d83-971e-e56c40512793 */
12+
#define TA_OCALL_UUID \
13+
{ 0x9b2c0652, 0x3b9b, 0x4d83, \
14+
{ 0x97, 0x1e, 0xe5, 0x6c, 0x40, 0x51, 0x27, 0x93 } }
15+
16+
#define TA_OCALL_CMD_CALL_CA 0
17+
18+
#define CA_OCALL_CMD_REPLY_TA 100
19+
20+
#endif /*TA_OCALL_H*/

0 commit comments

Comments
 (0)