Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ target_include_directories(${PROJECT_NAME}
PRIVATE ta/include
PRIVATE include)

target_link_libraries (${PROJECT_NAME} PRIVATE teec)
target_link_libraries (${PROJECT_NAME} PRIVATE teec pthread)

install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
8 changes: 6 additions & 2 deletions hello_world/host/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ OBJCOPY ?= $(CROSS_COMPILE)objcopy
OBJDUMP ?= $(CROSS_COMPILE)objdump
READELF ?= $(CROSS_COMPILE)readelf

OBJS = main.o
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
LDADD += -lteec -L$(TEEC_EXPORT)/lib -lpthread

BINARY = optee_example_hello_world

Expand All @@ -26,3 +26,7 @@ clean:

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

%.so: ree_service.c
$(CC) -fPIC $(CFLAGS) -c $<
$(CC) -shared -fPIC -o $@ $^ $(LDADD) $(CFLAGS)
78 changes: 77 additions & 1 deletion hello_world/host/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,92 @@
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>

/* 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_world_ta.h>

int main(void)
#include <ree_service_api.h>

void *ree_hello_service(void *arg)
{
int ret;
void *service = arg;
size_t num_params;
struct tee_params params[4] = {0};

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

switch(params[0].u.value.a) {
case HELLO_WORLD_MSG:
{
char *msg = "Hello! from REE";
memcpy(params[2].u.memref.buffer, msg, strlen(msg) + 1);
printf("Received: %s\n", (char *)params[1].u.memref.buffer);
break;
}

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

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

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

/* Send the response */
ret = ree_snd_params(service, 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;
TEEC_Result res;
TEEC_Context ctx;
TEEC_Session sess;
TEEC_Operation op;
pthread_t ree_serv_thread;
TEEC_UUID uuid = TA_HELLO_WORLD_UUID;
uint32_t err_origin;
void *service;
REEC_UUID ree_uuid = TA_HELLO_WORLD_REE_UUID;

#if 0

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.

#if 0?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I will refactor the code, so, as to clearly demonstrate the functionality. I think you may have already figured out, that there are 2 methods to avail REE custom service.
o msgq
o dynlib

So, for my testing purposes, I was enabling either 1 of them.

On a side note, I need help in putting the generated dynlib into filesystem tarball which is generated in the end. I didn't spend enough time in the build system, as a quick hack (from out-br folder) was available to copy dynlib to the running board.

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.

What's dynlib? Is msgq Posix Message Queues?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

dynlib -> dynamic library (.so).
msgq -> Yes Posix Message Queue.

ret = ree_service_init(&ree_uuid, &service);
if (ret)
errx(1, "Failed to register custom ree service\n");

/* Create a hello world ree service */
ret = pthread_create(&ree_serv_thread, NULL, ree_hello_service, service);
if (ret)
errx(1, "Failed to start hello world ree service\n");
#endif

/* Initialize a context connecting us to the TEE */
res = TEEC_InitializeContext(NULL, &ctx);
Expand Down Expand Up @@ -101,6 +172,11 @@ int main(void)
TEEC_CloseSession(&sess);

TEEC_FinalizeContext(&ctx);
#if 0

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.

Ditto? If this is something we don't want to use by default, then I think it's better to add a compile time flag.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I will create separate flows for message queues and dynamic lib, so, both will be enabled by default.

ree_service_exit(service);
#endif

pthread_join(ree_serv_thread, NULL);

return 0;
}
33 changes: 33 additions & 0 deletions hello_world/host/ree_service.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <string.h>

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

TEEC_Result process_tee_params(size_t num_params, struct tee_params *params)
{
switch (params[0].u.value.a) {
case HELLO_WORLD_MSG:
{
char *msg = "Hello! from REE";
memcpy(params[2].u.memref.buffer, msg, strlen(msg) + 1);
printf("DLSO: Received: %s\n", (char *)params[1].u.memref.buffer);
break;
}

case OPTEE_MRC_GENERIC_SERVICE_START:
printf("DLSO: Nothing specific to start\n");
break;

case OPTEE_MRC_GENERIC_SERVICE_STOP:
printf("DLSO: Nothing specific to stop\n");
break;

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

return 0;
}
44 changes: 43 additions & 1 deletion hello_world/ta/hello_world_ta.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <tee_internal_api_extensions.h>

#include <hello_world_ta.h>
#include <string.h>

/*
* Called when the instance of the TA is created. This is the first call in
Expand Down Expand Up @@ -97,6 +98,12 @@ void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx)
static TEE_Result inc_value(uint32_t param_types,
TEE_Param params[4])
{
TEE_UUID ree_uuid = TA_HELLO_WORLD_REE_UUID;
TEE_Result result = TEE_SUCCESS;
TEE_REESessionHandle ree_sess = NULL;
uint32_t ret_origin = 0, paramTypes;

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.

Please put paramTypes on its own line (and initialize the variable).

char msg[] = "Hello! from TEE";
TEE_Param ree_params[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.

Nothing wrong as such, but I think the general convention seems to be to just simply call this "params"

uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE,
Expand All @@ -107,10 +114,45 @@ static TEE_Result inc_value(uint32_t param_types,
if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;

IMSG("Got value: %u from NW", params[0].value.a);
EMSG("Got value: %u from NW", params[0].value.a);

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.

I think it still should be en I[informal] message and not an E[rror] message here and my other places in this function. Error should only be used when it's indeed an error.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I will fix the optee-examples fully before you can review in more detail. It was just POC intended for giving a view how to use the TEE -> REE service.

params[0].value.a++;
IMSG("Increase value to: %u", params[0].value.a);

EMSG("Opening a session on REE service\n");

result = TEE_OpenREESession(&ree_uuid, 0, 0, NULL,
&ree_sess, &ret_origin);
if (result != TEE_SUCCESS) {
DMSG("Failed to open up REE Session\n");

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.

This is indeed an error, so here you should use EMSG.

goto err;
}

/* Send a custom command */
paramTypes = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, /* Reserved */
TEE_PARAM_TYPE_MEMREF_INPUT,
TEE_PARAM_TYPE_MEMREF_OUTPUT,
TEE_PARAM_TYPE_NONE);
ree_params[1].memref.buffer = msg;
ree_params[1].memref.size = strlen(msg) + 1;
ree_params[2].memref.buffer = TEE_Malloc(64, TEE_MALLOC_FILL_ZERO);
ree_params[2].memref.size = 64;

if (!ree_params[2].memref.buffer)
return TEE_ERROR_BAD_PARAMETERS;

EMSG("------ Invoking command---\n");
result = TEE_InvokeREECommand(ree_sess, 0, HELLO_WORLD_MSG,
paramTypes, ree_params, &ret_origin);
if (result != TEE_SUCCESS) {
DMSG("Failed to invoke REE command\n");

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.

s/DMSG/EMSG/

goto err;
}
EMSG("TEE:: Received: %s\n", (char *)ree_params[2].memref.buffer);
TEE_Free(ree_params[2].memref.buffer);

TEE_CloseREESession(ree_sess);

err:
return TEE_SUCCESS;
}

Expand Down
6 changes: 6 additions & 0 deletions hello_world/ta/include/hello_world_ta.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@
{ 0x8aaaf200, 0x2450, 0x11e4, \
{ 0xab, 0xe2, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} }

#define TA_HELLO_WORLD_REE_UUID \
{ 0x0e3ade56, 0x96dc, 0x44cf, \
{ 0x9b, 0x52, 0x2e, 0x6a, 0x06, 0xd8, 0x43, 0x80} }

/* The function IDs implemented in this TA */
#define TA_HELLO_WORLD_CMD_INC_VALUE 0
#define TA_HELLO_WORLD_CMD_DEC_VALUE 1

#define HELLO_WORLD_MSG 16

#endif /*TA_HELLO_WORLD_H*/