欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

TEEC_Context和TEEC_InitializeContext介绍

发布时间:2025/3/21 编程问答 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 TEEC_Context和TEEC_InitializeContext介绍 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

思考:
TEEC_InitializeContext做了哪些事情?
TEEC_Context是干什么用的?
Userspace的TEEC_Context、Kernel Space的tee_context的区别?

本质(最佳理解): Represents a connection between a client application and a TEE

1、从UserSpace角度来看Context

本质 :在调用TEEC_InitializeContext时,上层返回一个ctx结构体,该结构体中有三个元素,其中fd是open返回的文件描述符,另外两个元素有上层hardcoding. filename/fd和kernel层的inode—device绑定. 在Kernel层,也有一个Kernel层的ctx结构体(与Userspace的不一样),该结构体会在Kernel层填充。TEEC_InitializeContext函数调用,在Kernel层就结束,不会进入TEE.

TEEC_InitializeContext返回一个TEEC_Context结构体,该结构体有三个元素:

  • fd : Userpace通过open调用Kernel返回的文件描述符(如下图所示,fd和Kernel层的tee_device绑定起来)
  • reg_mem
  • memref_null
(optee_client/public/tee_client_api.h)/*** struct TEEC_Context - Represents a connection between a client application* and a TEE.*/ typedef struct {/* Implementation defined */int fd;bool reg_mem;bool memref_null; } TEEC_Context; (optee_client/libteec/src/tee_client_api.c) TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *ctx) {char devname[PATH_MAX] = { 0 };int fd = 0;size_t n = 0;if (!ctx)return TEEC_ERROR_BAD_PARAMETERS;for (n = 0; n < TEEC_MAX_DEV_SEQ; n++) {uint32_t gen_caps = 0;snprintf(devname, sizeof(devname), "/dev/tee%zu", n);fd = teec_open_dev(devname, name, &gen_caps);if (fd >= 0) {ctx->fd = fd;ctx->reg_mem = gen_caps & TEE_GEN_CAP_REG_MEM;ctx->memref_null = gen_caps & TEE_GEN_CAP_MEMREF_NULL;return TEEC_SUCCESS;}}return TEEC_ERROR_ITEM_NOT_FOUND; }static int teec_open_dev(const char *devname, const char *capabilities,uint32_t *gen_caps) {int fd = 0;struct tee_ioctl_version_data vers;memset(&vers, 0, sizeof(vers));fd = open(devname, O_RDWR);if (fd < 0)return -1;if (ioctl(fd, TEE_IOC_VERSION, &vers)) {EMSG("TEE_IOC_VERSION failed");goto err;}/* We can only handle GP TEEs */if (!(vers.gen_caps & TEE_GEN_CAP_GP))goto err;if (capabilities) {if (strcmp(capabilities, "optee-tz") == 0) {if (vers.impl_id != TEE_IMPL_ID_OPTEE)goto err;if (!(vers.impl_caps & TEE_OPTEE_CAP_TZ))goto err;} else {/* Unrecognized capability requested */goto err;}}*gen_caps = vers.gen_caps;return fd;err:close(fd);return -1;}

Userspace调用KernelSpace的open device流程:

2、Kernel Space中的tee_context

在teedev_open讲tee_device填充到teedev. 其它元素在后续使用时填充

struct tee_context {struct tee_device *teedev;void *data;struct kref refcount;bool releasing;bool supp_nowait;bool cap_memref_null; }; static struct tee_context *teedev_open(struct tee_device *teedev) {int rc;struct tee_context *ctx;if (!tee_device_get(teedev))return ERR_PTR(-EINVAL);ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);if (!ctx) {rc = -ENOMEM;goto err;}kref_init(&ctx->refcount);ctx->teedev = teedev;rc = teedev->desc->ops->open(ctx);if (rc)goto err;return ctx; err:kfree(ctx);tee_device_put(teedev);return ERR_PTR(rc);}

3、代码使用示例

TEEC_Context teec_ctx;
TEEC_Session session = { .ctx = 0 };

1、TEEC_InitializeContext(_device, &teec_ctx); // 初始化一个Context,表示此Application和TEE的连接

2、TEEC_OpenSession(&teec_ctx, session, uuid, TEEC_LOGIN_PUBLIC, NULL, op, ret_orig); // open session,表示此Application和TA的连接

3、TEEC_InvokeCommand(session, TA_ZHOUHEHE_CMD_1, &op, &ret_orig);

4、TEEC_CloseSession(session);

5、TEEC_FinalizeContext(&teec_ctx); // 终止Context

总结

以上是生活随笔为你收集整理的TEEC_Context和TEEC_InitializeContext介绍的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。