CA
上一篇文章描述了 如何通过值传递的形式,CA向TA传递数据,值传递的数据量太小,本文我们使用地址传递方式,传输大量数据
#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_MEMREF_TEMP_INPUT, TEEC_MEMREF_TEMP_OUTPUT,
TEEC_MEMREF_TEMP_INOUT,TEEC_NONE);
uint8_t *data = (uint8_t *)malloc(100);
uint8_t *data1 = (uint8_t *)malloc(100);
uint8_t *data2 = (uint8_t *)malloc(100);
memcpy(data, "hello, i am ca", strlen("hello, i am ca"));
memcpy(data2, "CA msg here", strlen("CA msg here"));
op.params[0].tmpref.buffer = data ;
op.params[0].tmpref.size = 100;
op.params[1].tmpref.buffer = data1 ;
op.params[1].tmpref.size = 100;
op.params[2].tmpref.buffer = data2 ;
op.params[2].tmpref.size = 100;
res = TEEC_InvokeCommand(&sess, CMD_TEST_BUFFER, &op,
&err_origin);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
res, err_origin);
printf("buffer from TA: %s", op.params[1].params.tmpref);
printf("buffer changed from TA: %s", op.params[2].params.tmpref);
TEEC_CloseSession(&sess);
TEEC_FinalizeContext(&ctx);
return 0;
}
TA
#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_buffer(uint32_t param_types,
TEE_Param params[4])
{
uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
TEE_PARAM_TYPE_MEMREF_OUTPUT,
TEE_PARAM_TYPE_MEMREF_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 msg from ca :%s", params[0].memref.buffer);
memcpy(params[1].memref.buffer, "Hello, I am TA");
params[1].memref.size = strlen("Hello, I am TA");
IMSG("Change buffer to CA");
memcpy(params[2].memref.buffer, "TA here");
params[2].memref.size = strlen("TA here");
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_BUFFER:
test_buffer(param_types, params);
default:
return TEE_ERROR_BAD_PARAMETERS;
}
}
CA通过使用 如下几个参数标志,给TA 传递buffer数据
- TEEC_MEMREF_TEMP_INPUT
- TEEC_MEMREF_TEMP_OUTPUT
- TEEC_MEMREF_TEMP_INOUT
请注意,buffer的数据量不能无限大,ca 和 ta 的数据共享是通过sharemem作为中转站,sharemem是有大小限制的, 限制在bl32 内部CFG配置文件实现。
若要实现大数据传递,请多次分批传递数据