Skip to content

Commit 97db368

Browse files
author
Hernan Gatta
committed
Add sample for using Generic RPC's.
1 parent 559b214 commit 97db368

File tree

11 files changed

+613
-0
lines changed

11 files changed

+613
-0
lines changed

rpc/Android.mk

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
###################### optee-rpc ######################
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_rpc
17+
LOCAL_VENDOR_MODULE := true
18+
LOCAL_MODULE_TAGS := optional
19+
include $(BUILD_EXECUTABLE)
20+
21+
include $(LOCAL_PATH)/ta/Android.mk

rpc/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
project (optee_example_rpc 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+
add_compile_options(-g)
12+
add_compile_options(-O0)
13+
add_compile_options(-no-pie)
14+
add_compile_options(-no-pic)
15+
16+
target_link_libraries (${PROJECT_NAME} PRIVATE teec)
17+
18+
install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})

rpc/Makefile

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

rpc/host/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 -g -O0 -no-pie -no-pic
12+
LDADD += -lteec -L$(TEEC_EXPORT)/lib
13+
14+
BINARY = optee_example_rpc
15+
16+
.PHONY: all
17+
all: $(BINARY)
18+
19+
$(BINARY): $(OBJS)
20+
$(CC) -o $@ $< $(LDADD)
21+
22+
.PHONY: clean
23+
clean:
24+
rm -f $(OBJS) $(BINARY)
25+
26+
%.o: %.c
27+
$(CC) $(CFLAGS) -c $< -o $@

rpc/host/main.c

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright (c) 2019, Microsoft Corporation
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+
#include <tee_client_api.h>
33+
#include <tee_client_api_extensions.h>
34+
35+
#include <rpc.h>
36+
37+
static TEEC_Result rpc_callback(int func_id, TEEC_Parameter *parameters,
38+
void *context)
39+
{
40+
struct ta_rpc_func_params *par;
41+
const char *msg = "wonderful world!";
42+
const char *full_msg = "What a wonderful world!";
43+
44+
printf("Have RPC: %i\n", func_id);
45+
46+
switch (func_id)
47+
{
48+
case TA_RPC_FUNC_NO_PARAMS:
49+
break;
50+
case TA_RPC_FUNC_PARAMS_IN:
51+
printf("First param: 0x%x, 0x%x.\n", parameters[0].value.a,
52+
parameters[0].value.b);
53+
printf("Fourth param: %s\n",
54+
(char *)parameters[3].tmpref.buffer);
55+
break;
56+
case TA_RPC_FUNC_PARAMS_INOUT:
57+
par = (struct ta_rpc_func_params *)parameters[2].tmpref.buffer;
58+
59+
printf("First param: 0x%x, 0x%x.\n", parameters[0].value.a,
60+
parameters[0].value.b);
61+
printf("Second param: 0x%x, 0x%x.\n", parameters[1].value.a,
62+
parameters[1].value.b);
63+
printf("Third param: %s\n", par->second);
64+
printf("Fourth param: %s\n",
65+
(char *)parameters[3].tmpref.buffer);
66+
67+
parameters[1].value.a = 0x3;
68+
parameters[1].value.b = 0xA;
69+
70+
memcpy(par->second, msg, strlen(msg) + 1);
71+
break;
72+
case TA_RPC_FUNC_PARAMS_OUT:
73+
parameters[0].value.a = 0x4;
74+
parameters[0].value.b = 0xB;
75+
76+
parameters[3].tmpref.size = strlen(full_msg) + 1;
77+
memcpy(parameters[3].tmpref.buffer, full_msg,
78+
parameters[3].tmpref.size);
79+
break;
80+
default:
81+
TEEC_ERROR_BAD_PARAMETERS;
82+
}
83+
84+
return TEEC_SUCCESS;
85+
}
86+
87+
int main(void)
88+
{
89+
TEEC_Result res;
90+
TEEC_Context ctx;
91+
TEEC_Session sess;
92+
TEEC_Operation op;
93+
TEEC_UUID uuid = TA_RPC_UUID;
94+
uint32_t err_origin;
95+
96+
res = TEEC_InitializeContext(NULL, &ctx);
97+
if (res != TEEC_SUCCESS)
98+
errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
99+
100+
res = TEEC_OpenSessionEx(&ctx, &sess, &uuid,
101+
TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin,
102+
rpc_callback, &sess);
103+
if (res != TEEC_SUCCESS)
104+
errx(1, "TEEC_OpenSessionEx failed with code 0x%x origin 0x%x",
105+
res, err_origin);
106+
107+
/*
108+
* First Call
109+
*/
110+
111+
memset(&op, 0, sizeof(op));
112+
op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
113+
TEEC_NONE);
114+
115+
printf("Invoking TA to perform RPC back to host\n");
116+
res = TEEC_InvokeCommand(&sess, TA_RPC_CMD_TEST_RPC_NO_PARAMS, &op,
117+
&err_origin);
118+
if (res != TEEC_SUCCESS)
119+
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
120+
res, err_origin);
121+
printf("Performed call\n");
122+
123+
/*
124+
* Second Call
125+
*/
126+
127+
printf("Invoking TA to perform RPC back to host\n");
128+
res = TEEC_InvokeCommand(&sess, TA_RPC_CMD_TEST_RPC_PARAM_IN, &op,
129+
&err_origin);
130+
if (res != TEEC_SUCCESS)
131+
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
132+
res, err_origin);
133+
printf("Performed call\n");
134+
135+
/*
136+
* Third Call
137+
*/
138+
139+
printf("Invoking TA to perform RPC back to host\n");
140+
res = TEEC_InvokeCommand(&sess, TA_RPC_CMD_TEST_RPC_PARAM_INOUT, &op,
141+
&err_origin);
142+
if (res != TEEC_SUCCESS)
143+
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
144+
res, err_origin);
145+
printf("Performed call\n");
146+
147+
/*
148+
* Fourth Call
149+
*/
150+
151+
printf("Invoking TA to perform RPC back to host\n");
152+
res = TEEC_InvokeCommand(&sess, TA_RPC_CMD_TEST_RPC_PARAM_OUT, &op,
153+
&err_origin);
154+
if (res != TEEC_SUCCESS)
155+
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
156+
res, err_origin);
157+
printf("Performed call\n");
158+
159+
TEEC_CloseSession(&sess);
160+
TEEC_FinalizeContext(&ctx);
161+
162+
return 0;
163+
}

rpc/ta/Android.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
LOCAL_PATH := $(call my-dir)
2+
3+
local_module := 926297c6-c2d3-4bd9-82b4-17460f55b6a4.ta
4+
5+
include $(BUILD_OPTEE_MK)

rpc/ta/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CFG_TEE_TA_LOG_LEVEL ?= 4
2+
CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL)
3+
4+
BINARY=926297c6-c2d3-4bd9-82b4-17460f55b6a4
5+
6+
-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk
7+
8+
ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), )
9+
clean:
10+
@echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA'
11+
@echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)'
12+
endif

rpc/ta/include/rpc.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2019, Microsoft Corporation
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+
#ifndef TA_RPC_H
29+
#define TA_RPC_H
30+
31+
#define TA_RPC_UUID \
32+
{ 0x926297c6, 0xc2d3, 0x4bd9, \
33+
{ 0x82, 0xb4, 0x17, 0x46, 0x0f, 0x55, 0xb6, 0xa4} }
34+
35+
#define TA_RPC_CMD_TEST_RPC_NO_PARAMS 0
36+
#define TA_RPC_CMD_TEST_RPC_PARAM_IN 1
37+
#define TA_RPC_CMD_TEST_RPC_PARAM_INOUT 2
38+
#define TA_RPC_CMD_TEST_RPC_PARAM_OUT 3
39+
40+
#define TA_RPC_FUNC_NO_PARAMS 1
41+
#define TA_RPC_FUNC_PARAMS_IN 2
42+
#define TA_RPC_FUNC_PARAMS_INOUT 3
43+
#define TA_RPC_FUNC_PARAMS_OUT 4
44+
45+
/* Sample function parameters marshalling structure. */
46+
struct ta_rpc_func_params {
47+
uint32_t first;
48+
char second[256];
49+
};
50+
51+
#endif /*TA_RPC_H*/

0 commit comments

Comments
 (0)