Skip to content

Instantly share code, notes, and snippets.

@kumarabekur
Created October 24, 2017 11:33
Show Gist options
  • Save kumarabekur/fbbbc13f443269e97a63c897debb1e31 to your computer and use it in GitHub Desktop.
Save kumarabekur/fbbbc13f443269e97a63c897debb1e31 to your computer and use it in GitHub Desktop.
TEEC_RequestCancellation host and TA
/*
* Copyright (c) 2016, Linaro Limited
* 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.
*/
#define STR_TRACE_USER_TA "HELLO_WORLD"
#include <tee_internal_api.h>
#include <tee_internal_api_extensions.h>
#include "hello_world_ta.h"
#include <unistd.h>
/*
* Called when the instance of the TA is created. This is the first call in
* the TA.
*/
TEE_Result TA_CreateEntryPoint(void)
{
DMSG("has been called");
return TEE_SUCCESS;
}
/*
* Called when the instance of the TA is destroyed if the TA has not
* crashed or panicked. This is the last call in the TA.
*/
void TA_DestroyEntryPoint(void)
{
DMSG("has been called");
}
/*
* Called when a new session is opened to the TA. *sess_ctx can be updated
* with a value to be able to identify this session in subsequent calls to the
* TA. In this function you will normally do the global initialization for the
* TA.
*/
TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,
TEE_Param __maybe_unused params[4],
void __maybe_unused **sess_ctx)
{
uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE);
int cancel_flag,unmaskflag;
TEE_Result res;
if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;
/* Unused parameters */
(void)&params;
(void)&sess_ctx;
/*
* The DMSG() macro is non-standard, TEE Internal API doesn't
* specify any means to logging from a TA.
*/
DMSG("Hello World!\n");
cancel_flag = TEE_GetCancellationFlag();
DMSG("*****TA_OpenSessionEntryPoint***CANCEL FLAG =%d\n",cancel_flag);
unmaskflag = TEE_UnmaskCancellation();
DMSG("*******TA_OpenSessionEntryPoint******UNMASK FLAG =%d\n",unmaskflag);
cancel_flag = TEE_GetCancellationFlag();
DMSG("*******TA_OpenSessionEntryPoint*******CANCEL FLAG =%d\n",cancel_flag);
res = TEE_Wait(10000);
if (res != TEE_SUCCESS)
{
DMSG ("*****TA Cancel***\n");
}
/* If return value != TEE_SUCCESS the session will not be created. */
return TEE_SUCCESS;
}
/*
* Called when a session is closed, sess_ctx hold the value that was
* assigned by TA_OpenSessionEntryPoint().
*/
void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx)
{
(void)&sess_ctx; /* Unused parameter */
DMSG("Goodbye!\n");
}
static TEE_Result inc_value(uint32_t param_types,
TEE_Param params[4])
{
uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE);
int cancel_flag,unmaskflag;
DMSG("has been called");
if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;
cancel_flag = TEE_GetCancellationFlag();
DMSG("CANCEL FLAG =%d\n",cancel_flag);
unmaskflag = TEE_UnmaskCancellation();
DMSG("UNMASK FLAG =%d\n",unmaskflag);
cancel_flag = TEE_GetCancellationFlag();
DMSG("CANCEL FLAG =%d\n",cancel_flag);
DMSG("Got value: %u from NW", params[0].value.a);
params[0].value.a++;
TEE_Wait(10000);
DMSG("Increase value to: %u", params[0].value.a);
return TEE_SUCCESS;
}
/*
* Called when a TA is invoked. sess_ctx hold that value that was
* assigned by TA_OpenSessionEntryPoint(). The rest of the paramters
* comes from normal world.
*/
TEE_Result TA_InvokeCommandEntryPoint(void __maybe_unused *sess_ctx,
uint32_t cmd_id,
uint32_t param_types, TEE_Param params[4])
{
(void)&sess_ctx; /* Unused parameter */
switch (cmd_id) {
case TA_HELLO_WORLD_CMD_INC_VALUE:
return inc_value(param_types, params);
default:
return TEE_ERROR_BAD_PARAMETERS;
}
}
/*
* Copyright (c) 2016, Linaro Limited
* 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.
*/
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.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>
TEEC_Operation op;
void* cancel_thread(void* unused){
printf("### Thread ###\n");
sleep(5);
printf("******After Sleep\n");
TEEC_RequestCancellation(&op);
printf("******After TEEC_RequestCancellation \n");
op.started = 0;
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
TEEC_Result res;
TEEC_Context ctx;
TEEC_Session sess;
TEEC_UUID uuid = TA_HELLO_WORLD_UUID;
uint32_t err_origin;
pthread_t thread_id;
int ret;
/* 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 world" TA, the TA will print "hello
* world!" in the log when the session is created.
*/
memset(&op, 0, sizeof(op));
op.started = 0;
op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE,
TEEC_NONE, TEEC_NONE);
ret = pthread_create(&thread_id, NULL, &cancel_thread, NULL);
printf("RET value =%d\n",ret);
res = TEEC_OpenSession(&ctx, &sess, &uuid,
TEEC_LOGIN_PUBLIC, NULL, &op, &err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
res, err_origin);
sleep(6);
/*
* Execute a function in the TA by invoking it, in this case
* we're incrementing a number.
*
* 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 */
/*
* Prepare the argument. Pass a value in the first parameter,
* the remaining three parameters are unused.
*/
op.params[0].value.a = 42;
/*
* TA_HELLO_WORLD_CMD_INC_VALUE is the actual function in the TA to be
* called.
*/
printf("Invoking TA to increment %d\n", op.params[0].value.a);
res = TEEC_InvokeCommand(&sess, TA_HELLO_WORLD_CMD_INC_VALUE, &op,
&err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
res, err_origin);
printf("TA incremented value to %d\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.
*/
pthread_join(thread_id,NULL);
TEEC_CloseSession(&sess);
TEEC_FinalizeContext(&ctx);
return 0;
}
root@imx6ul-iwg18m-twr-pos:~# tee-supplicant &
[1] 645
root@imx6ul-iwg18m-twr-pos:~# hello_world
RET value =0
### Thread ###
DEBUG: USER-TA:TA_CreateEntryPoint:42: has been called
FLOW: USER-TA: tee_user_mem_alloc:343: Allocate: link:[0x111b98], buf:[0x111ba8:16]
DEBUG: USER-TA:TA_OpenSessionEntryPoint:82: Hello World!
DEBUG: USER-TA:TA_OpenSessionEntryPoint:88: *****TA_OpenSessionEntryPoint***CANCEL FLAG =0
DEBUG: USER-TA:TA_OpenSessionEntryPoint:92: *******TA_OpenSessionEntryPoint******UNMASK FLAG =1
DEBUG: USER-TA:TA_OpenSessionEntryPoint:95: *******TA_OpenSessionEntryPoint*******CANCEL FLAG =0
******After Sleep
******After TEEC_RequestCancellation
Invoking TA to increment 42DEBUG: USER-TA:inc_value:126: has been called
DEBUG: USER-TA:inc_value:131: CANCEL FLAG =0
DEBUG: USER-TA:inc_value:135: UNMASK FLAG =0
DEBUG: USER-TA:inc_value:138: CANCEL FLAG =0
DEBUG: USER-TA:inc_value:141: Got value: 42 from NW
DEBUG: USER-TA:inc_value:145: Increase value to: 43
TA incremented value to 43DEBUG: USER-TA:TA_CloseSessionEntryPoint:113: Goodbye!
FLOW: USER-TA: tee_user_mem_free:442: Free: link:[0x111b98], buf:[0x111ba8:16]
DEBUG: USER-TA:TA_DestroyEntryPoint:52: has been called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment