-
Notifications
You must be signed in to change notification settings - Fork 168
[RFC] hello_world: add demo code for using custom ree service #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| 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); | ||
|
|
@@ -101,6 +172,11 @@ int main(void) | |
| TEEC_CloseSession(&sess); | ||
|
|
||
| TEEC_FinalizeContext(&ctx); | ||
| #if 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| 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; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put |
||
| char msg[] = "Hello! from TEE"; | ||
| TEE_Param ree_params[4]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 " |
||
| uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT, | ||
| TEE_PARAM_TYPE_NONE, | ||
| TEE_PARAM_TYPE_NONE, | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is indeed an error, so here you should use |
||
| 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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#if 0?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.