前言
(默认你对optee有一点点点点了解)
一、hello_world分析
在\optee_examples\hello_world目录下,optee给出了一个简单的CA/TA示例。
hello_world的结构如下:
1、CA端
A.main.c
main.c文件有效代码如下:
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <tee_client_api.h>
#include <hello_world_ta.h>
int main(void)
{
TEEC_Result res;
TEEC_Context ctx;
TEEC_Session sess;
TEEC_Operation op;
TEEC_UUID uuid = TA_HELLO_WORLD_UUID;
uint32_t err_origin;
res = TEEC_InitializeContext(NULL, &ctx);
if (res != TEEC_SUCCESS)
errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
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);
memset(&op, 0, sizeof(op));
op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
op.params[0].value.a = 42;
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);
TEEC_CloseSession(&sess);
TEEC_FinalizeContext(&ctx);
return 0;
}
真的是个很简单的程序,由调用的指令id=TA_HELLO_WORLD_CMD_INC_VALUE来看,我们猜测这是一个加法相关操作。
整个函数只调用了一次InvokeCommand,之前和之后都打印出op.params[0].value.a。
B.Makefile
makefile源文件如下,我们可以看到make后会生成名为main的可执行文件。
CC ?= $(CROSS_COMPILE)gcc
LD ?= $(CROSS_COMPILE)ld
AR ?= $(CROSS_COMPILE)ar
NM ?= $(CROSS_COMPILE)nm
OBJCOPY ?= $(CROSS_COMPILE)objcopy
OBJDUMP ?= $(CROSS_COMPILE)objdump
READELF ?= $(CROSS_COMPILE)readelf
OBJS = main.o
CFLAGS += -Wall -I../ta/include -I$(TEEC_EXPORT)/include -I./include
#Add/link other required libraries here
LDADD += -lteec -L$(TEEC_EXPORT)/lib
BINARY = optee_example_hello_world
.PHONY: all
all: $(BINARY)
$(BINARY): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $< $(LDADD)
.PHONY: clean
clean:
rm -f $(OBJS) $(BINARY)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
2、TA端
A.android.mk
android.mk源文件如下,我们可以看到make后生成的是8aaaf200-2450-11e4-abe2-0002a5d5c51b.ta。
LOCAL_PATH := $(call my-dir)
local_module := 8aaaf200-2450-11e4-abe2-0002a5d5c51b.ta
include $(BUILD_OPTEE_MK)
B.hello_world_ta.c
hello_world_ta.c有效代码如下,我们可以看到原子操作不只是加法操作inc_value,还有减法操作dec_value,还有必备的TA_CreateEntryPoint、TA_DestroyEntryPoint、TA_OpenSessionEntryPoint、TA_CloseSessionEntryPoint、TA_InvokeCommandEntryPoint五个函数。
#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 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);
DMSG("has been called");
if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;
IMSG("Got value: %u from NW", params[0].value.a);
params[0].value.a++;
IMSG("Increase value to: %u", params[0].value.a);
return TEE_SUCCESS;
}
static TEE_Result dec_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);
DMSG("has been called");
if (param_types != exp_param_types)
return TEE_ERROR_BAD_PARAMETERS;
IMSG("Got value: %u from NW", params[0].value.a);
params[0].value.a--;
IMSG("Decrease value to: %u", params[0].value.a);
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;
switch (cmd_id) {
case TA_HELLO_WORLD_CMD_INC_VALUE:
return inc_value(param_types, params);
case TA_HELLO_WORLD_CMD_DEC_VALUE:
return dec_value(param_types, params);
default:
return TEE_ERROR_BAD_PARAMETERS;
}
}
C.user_ta_header_defines.h
#ifndef USER_TA_HEADER_DEFINES_H
#define USER_TA_HEADER_DEFINES_H
#include <hello_world_ta.h>
#define TA_UUID TA_HELLO_WORLD_UUID
#define TA_FLAGS TA_FLAG_EXEC_DDR
#define TA_STACK_SIZE (2 * 1024)
#define TA_DATA_SIZE (32 * 1024)
#define TA_VERSION "1.0"
#define TA_DESCRIPTION "Example of OP-TEE Hello World Trusted Application"
#define TA_CURRENT_TA_EXT_PROPERTIES \
{ "org.linaro.optee.examples.hello_world.property1", \
USER_TA_PROP_TYPE_STRING, "Some string" }, \
{ "org.linaro.optee.examples.hello_world.property2", \
USER_TA_PROP_TYPE_U32, &(const uint32_t){ 0x0010 } }
D.Makefile
CFG_TEE_TA_LOG_LEVEL ?= 4
BINARY=8aaaf200-2450-11e4-abe2-0002a5d5c51b
-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk
ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), )
clean:
@echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA'
@echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)'
endif
E.sub.mk
global-incdirs-y += include
srcs-y += hello_world_ta.c
F.include/hello_world_ta.h
#ifndef TA_HELLO_WORLD_H
#define TA_HELLO_WORLD_H
#define TA_HELLO_WORLD_UUID \
{ 0x8aaaf200, 0x2450, 0x11e4, { 0xab, 0xe2, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} }
#define TA_HELLO_WORLD_CMD_INC_VALUE 0
#define TA_HELLO_WORLD_CMD_DEC_VALUE 1
#endif /*TA_HELLO_WORLD_H*/
二、编写demo
写到一半忽然没动力写了.....要写自己的demo就是在这个基础上改改(uuid、Makefile...),如果有人看到了有疑问,欢迎留言噢!