CA
如下实现一个简单的CA代码示例,它使用了不同的Params类型
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.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>
#define CMD_TEST_VALUE 0x001
#define CMD_TEST_BUFFER 0x002
#define TA_HELLO_WORLD_UUID { 0xc02ffd4f, 0xa7a3, 0xb1c3,
{ 0x92, 0x08, 0x37, 0x06, 0xe4, 0xc8, 0xb1, 0xaf } }
int main(int argc ,char **argv)
{
TEEC_Result res;
TEEC_Context ctx;
TEEC_Session sess;
TEEC_Operation op;
TEEC_UUID uuid = TA_HELLO_WORLD_UUID;
uint32_t err_origin;
/* 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
*/
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);
/* Clear the TEEC_Operation struct */
memset(&op, 0, sizeof(op));
/*
* Prepare the argument.
*/
op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_OUTPUT,
TEEC_VALUE_INOUT, TEEC_NONE);
op.params[0].value.a = 10;
op.params[2].value.a = 11;
res = TEEC_InvokeCommand(&sess, CMD_TEST_VALUE, &op,
&err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
res, err_origin);
TEEC_CloseSession(&sess);
TEEC_FinalizeContext(&ctx);
return 0;
}
TA
如下实现一个简单的TA代码示例,它实现了不同的Params类型,处理CA数据
#include <tee_internal_api.h>
#include <tee_internal_api_extensions.h>
#include <hello_world_ta.h>
TEE_Result TA_CreateEntryPoint(void)
{
DMSG("has been called");
return TEE_SUCCESS;
}
void TA_DestroyEntryPoint(void)
{
DMSG("has been called");
}
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_NONE,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE,
TEE_PARAM_TYPE_NONE);
DMSG("has been called");
if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;
(void)¶ms;
(void)&sess_ctx;
IMSG("Hello World!\n");
return TEE_SUCCESS;
}
void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx)
{
(void)&sess_ctx; /* Unused parameter */
IMSG("Goodbye!\n");
}
static TEE_Result test_value(uint32_t param_types,
TEE_Param params[4])
{
uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT,
TEE_PARAM_TYPE_VALUE_OUTPUT,
TEE_PARAM_TYPE_INOUT,
TEE_PARAM_TYPE_NONE);
uint32_t value = 0;
DMSG("has been called");
if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;
IMSG("Got value: %u from CA", params[0].value.a);
value = params[0].value.a;
value += 100;
IMSG("increase value to: %u",value);
params[1].value.a = value;
IMSG("return increase data to normal world by params[1]");
params[2].value.a = 12;
IMSG("change data to normal world by params[2]");
return TEE_SUCCESS;
}
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 CMD_TEST_VALUE:
test_value(param_types, params);
default:
return TEE_ERROR_BAD_PARAMETERS;
}
}
本文 实现了一个使用3种 值传递 的params方式
- TEEC_VALUE_INPUT 只能ca向ta传递数据
- TEEC_VALUE_OUTPUT 只能ta向ca传递数据
- TEEC_VALUE_INOUT 双向传递数据