Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ prepare-for-rootfs: examples
@for example in $(EXAMPLE_LIST); do \
if [ -e $$example/host/optee_example_$$example ]; then \
cp -p $$example/host/optee_example_$$example $(OUTPUT_DIR)/ca/; \
cp -p $$example/host/*.so $(OUTPUT_DIR)/ca/ > /dev/null; \
fi; \
cp -pr $$example/ta/*.ta $(OUTPUT_DIR)/ta/; \
done
Expand Down
21 changes: 21 additions & 0 deletions hello_ree/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
###################### optee-hello-ree ######################
LOCAL_PATH := $(call my-dir)

OPTEE_CLIENT_EXPORT = $(LOCAL_PATH)/../../optee_client/out/export

include $(CLEAR_VARS)
LOCAL_CFLAGS += -DANDROID_BUILD
LOCAL_CFLAGS += -Wall

LOCAL_SRC_FILES += host/main.c

LOCAL_C_INCLUDES := $(LOCAL_PATH)/ta/include \
$(OPTEE_CLIENT_EXPORT)/include \

LOCAL_SHARED_LIBRARIES := libteec
LOCAL_MODULE := optee_example_hello_ree
LOCAL_VENDOR_MODULE := true
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)

include $(LOCAL_PATH)/ta/Android.mk
17 changes: 17 additions & 0 deletions hello_ree/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
project (optee_example_hello_ree C)

set (SRC host/main.c)

add_executable (${PROJECT_NAME} ${SRC})

target_include_directories(${PROJECT_NAME}
PRIVATE ta/include
PRIVATE include)

add_library(0e3ade56-96dc-44cf-9b52-2e6a06d84380 SHARED
host/ree_dll.c)

target_link_libraries (${PROJECT_NAME} PRIVATE teec pthread 0e3ade56-96dc-44cf-9b52-2e6a06d84380)
target_include_directories(0e3ade56-96dc-44cf-9b52-2e6a06d84380 PRIVATE ta/include)

install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
15 changes: 15 additions & 0 deletions hello_ree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export V?=0

# If _HOST or _TA specific compilers are not specified, then use CROSS_COMPILE
HOST_CROSS_COMPILE ?= $(CROSS_COMPILE)
TA_CROSS_COMPILE ?= $(CROSS_COMPILE)

.PHONY: all
all:
$(MAKE) -C host CROSS_COMPILE="$(HOST_CROSS_COMPILE)" --no-builtin-variables
$(MAKE) -C ta CROSS_COMPILE="$(TA_CROSS_COMPILE)" LDFLAGS=""

.PHONY: clean
clean:
$(MAKE) -C host clean
$(MAKE) -C ta clean
32 changes: 32 additions & 0 deletions hello_ree/host/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CC ?= $(CROSS_COMPILE)gcc
LD ?= $(CROSS_COMPILE)ld
AR ?= $(CROSS_COMPILE)ar
NM ?= $(CROSS_COMPILE)nm
OBJCOPY ?= $(CROSS_COMPILE)objcopy
OBJDUMP ?= $(CROSS_COMPILE)objdump
READELF ?= $(CROSS_COMPILE)readelf

OBJS = main.o lib0e3ade56-96dc-44cf-9b52-2e6a06d84380.so

CFLAGS += -Wall -I../ta/include -I$(TEEC_EXPORT)/include -I./include
#Add/link other required libraries here
LDADD += -lteec -L$(TEEC_EXPORT)/lib -lpthread

BINARY = optee_example_hello_ree

.PHONY: all
all: $(BINARY)

$(BINARY): $(OBJS)
$(CC) -o $@ $< $(LDADD)

.PHONY: clean
clean:
rm -f $(OBJS) $(BINARY)

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

%.so: ree_dll.c
$(CC) -fPIC $(CFLAGS) -c $<
$(CC) -shared -fPIC -o $@ $^ $(LDADD) $(CFLAGS)
213 changes: 213 additions & 0 deletions hello_ree/host/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/*
* Copyright (c) 2016, Linaro Limited
* All rights reserved.
*
* Copyright (C) 2019 Intel Corporation All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/*
* o A Client Application (CA) requests the User Trusted Application (UTA)
* service by invoking TEEC_xx calls.
*
* o UTA may in turn require Rich Execution Environment (REE) services in order
* to fulfill the CA request. The REE service could be storage or generically,
* anything not supported by tee-supplicant.
*
* o This application uses two available solutions to provide service to TEE.
* It is based on using a UUID in the reverse direction (UTA -> CA)
* - Posix Message Queues: Message Queue is transparent to the caller.
*
* - Dynamic Library : The library needs to be developed with a specific
* function signature and compiled as .so and placed
* in a PATH (/lib64) where tee-supplicant can find

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to replace /lib64 with i.e. /lib64 or even /lib.

* it.
*
* For the service to invoked, it is expected that tee-supplicant is executing
* with permission to access /data folder, otherwise, invocation will fail and
* TA will receive a failure.
*/

#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: sort in alpha ordering


/* OP-TEE TEE client API (built by optee_client) */
#include <tee_client_api.h>

/* To the the UUID (found the the TA's h-file(s)) */
#include <hello_ree_ta.h>

/* OP-TEE REE client API to provide service to TEE */
#include <ree_service_api.h>

/**
* ree_msgq_service() - Message Queue based service handler
*/
void *ree_msgq_service(void *arg)
{
int ret;
void *ree_srvc_msgq_hdl = arg;
size_t num_params;
struct tee_params params[4] = {0};

/* Process the message */
do {
/* Wait for the message */
ret = ree_service_rcv(ree_srvc_msgq_hdl, &num_params, params);
if (ret) {
printf("Failed to receive msg\n");
goto err;
}

/* TODO: Check for the parameter count */
switch(params[0].u.value.a) {
case HELLO_REE_EXCHANGE_GREETINGS:
{
char *msg = "Hello! from MSGQ";
memcpy(params[2].u.memref.buffer, msg, strlen(msg) + 1);
printf("msgq: Received: %s\n",
(char *)params[1].u.memref.buffer);
break;
}

/* Setup the service for receiving next commands */
case OPTEE_MRC_REE_SERVICE_START:
printf("msgq: Nothing specific to start\n");
break;

/* Shutdown the service */
case OPTEE_MRC_REE_SERVICE_STOP:
printf("msgq: Nothing specific to stop\n");
break;

default:
printf("msgq: Unknown command received: %lu\n",
params[0].u.value.a);
break;
}

/* Send the response */
ret = ree_service_snd(ree_srvc_msgq_hdl, num_params, params, 0);
if (ret) {
printf("Failed to send status \n");
goto err;
}

} while (1);

err:
return NULL;
}

int main(int argc, char *argv[])
{
int ret;
pthread_t ree_msgq_thread;
TEEC_Result res;
TEEC_Context ctx;
TEEC_Session sess;
TEEC_Operation op;
TEEC_UUID uuid = TA_HELLO_REE_UUID;
TEEC_UUID msgq_ree_uuid = TA_HELLO_REE_MSGQ_REE_UUID;
uint32_t err_origin;
void *ree_srvc_msgq_hdl;

/* Start the Message Queue based REE service */
ret = ree_service_init(&msgq_ree_uuid, &ree_srvc_msgq_hdl);
if (ret)
errx(1, "Failed to register Hello REE MSQ service\n");

/* Listen to the service messages from UTA */
ret = pthread_create(&ree_msgq_thread, NULL,
ree_msgq_service, ree_srvc_msgq_hdl);
if (ret)
errx(1, "Failed to start hello world ree service\n");

/* Initialize a context connecting us to the TEE */
res = TEEC_InitializeContext(NULL, &ctx);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InitializeContext failed with code 0x%x", res);

/* Open a session to the "hello ree" TA */
res = TEEC_OpenSession(&ctx, &sess, &uuid,
TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
res, err_origin);

/*
* Execute a function in the TA by invoking it, in this case
* we're requesting a Random number from TA
*
* The value of command ID part and how the parameters are
* interpreted is part of the interface provided by the TA.
*/

/* Clear the TEEC_Operation struct */
memset(&op, 0, sizeof(op));

/*
* Prepare the argument. Pass a value in the first parameter,
* the remaining three parameters are unused.
*/
op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
TEEC_NONE, TEEC_NONE);

/* Get a 4 byte random number from TA */
printf("Invoking TA to fill random number %d\n", op.params[0].value.a);
res = TEEC_InvokeCommand(&sess, TA_HELLO_REE_FILL_RANDOM_NUMBER, &op,
&err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
res, err_origin);
printf("TA returned random number as %u\n", op.params[0].value.a);

/*
* We're done with the TA, close the session and
* destroy the context.
*
* The TA will print "Goodbye!" in the log when the
* session is closed.
*/

TEEC_CloseSession(&sess);

TEEC_FinalizeContext(&ctx);

/* Close down the message queue based REE service */
/*
* FIXME: need to move from errx interface, because it will leak
* the message queue ID floating in the system.
*/
ree_service_exit(ree_srvc_msgq_hdl);

/* Wait for the listening thread to exit */
pthread_join(ree_msgq_thread, NULL);

return 0;
}
45 changes: 45 additions & 0 deletions hello_ree/host/ree_dll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2019 Intel Corporation All Rights Reserved
*/

#include <stdio.h>
#include <string.h>

#include <hello_ree_ta.h>
#include <tee_client_api.h>
#include <ree_service_api.h>

/**
* process_tee_params() - dynamic library interface
* This implements the Dynamic Library based REE service. The function
* signature is fixed and needs to be implemented to provide service to
* UTA
*/
TEEC_Result process_tee_params(size_t num_params, struct tee_params *params)
{
switch (params[0].u.value.a) {
case HELLO_REE_EXCHANGE_GREETINGS:
{
char *msg = "Hello! from DLL";
memcpy(params[2].u.memref.buffer, msg, strlen(msg) + 1);
printf("dll: Received: %s\n",
(char *)params[1].u.memref.buffer);
break;
}

case OPTEE_MRC_REE_SERVICE_START:
printf("dll: Nothing specific to start\n");
break;

case OPTEE_MRC_REE_SERVICE_STOP:
printf("dll: Nothing specific to stop\n");
break;

default:
printf("dll: Unknown command received: %lu\n",
params[0].u.value.a);
break;
}

return 0;
}
4 changes: 4 additions & 0 deletions hello_ree/ta/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LOCAL_PATH := $(call my-dir)

local_module := 60eb5836-fc9b-446a-b277-25b650a8238f.ta
include $(BUILD_OPTEE_MK)
13 changes: 13 additions & 0 deletions hello_ree/ta/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CFG_TEE_TA_LOG_LEVEL ?= 4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: maybe 2 or 3 as default trace level.

CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL)

# The UUID for the Trusted Application
BINARY := 60eb5836-fc9b-446a-b277-25b650a8238f

-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk

ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), )
clean:
@echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA'
@echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)'
endif
Loading