日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【OPTEE开发】从TA到PTA的功能设计

發布時間:2025/3/15 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【OPTEE开发】从TA到PTA的功能设计 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、功能需求
  • 二、TA到PTA的總體設計
    • 1. 軟件層總體設計
      • 1.1 總體結構圖設計
      • 1.2 TA調用其他TA的接口設計
    • 2. 設計思路
  • 三、詳細設計
    • 1. 修改代碼清單
    • 2. 子模塊設計
      • 2.1 libhanlerot庫設計
      • 2.2 PTA模塊設計
      • 2.3 TA模塊設計

一、功能需求

上篇”從TA到安全驅動的功能設計“介紹了TA通過syscall的方式調用到driver的功能,那本篇主要描述一下如何實現TA調用PTA 完成基本功能的通路。
主要包括新增PTA實現幾個基本接口功能、新增和封裝PTA對應的lib庫 便于TA調用這兩個環節。

下面以實際例子進行實戰介紹,加深一下對TA調用PTA流程的理解。

原創不易,轉載請注明出處:https://blog.csdn.net/jackone12347/article/details/122555702

二、TA到PTA的總體設計

1. 軟件層總體設計

1.1 總體結構圖設計

1.2 TA調用其他TA的接口設計

API名稱API功能
TEE_OpenTASession建立兩個TA之間的session
TEE_InvokeTACommand通過已創建的session和command id去調用另一個TA提供的操作接口
TEE_CloseTASession關閉兩個TA之間的session
TEE_PARAM_TYPES定義兩個TA之間傳遞的parameter參數

libhandlerot接口lib庫中可以只實現這三個接口,但handlerot PTA中需要實現如下五個接口:

pseudo_ta_register(.uuid = PTA_ROT_UUID, .name = TA_NAME,.flags = PTA_DEFAULT_FLAGS | TA_FLAG_SECURE_DATA_PATH | TA_FLAG_CONCURRENT,.create_entry_point = create_ta,.destroy_entry_point = destroy_ta,.open_session_entry_point = open_session,.close_session_entry_point = close_session,.invoke_command_entry_point = invoke_command);

關于param參數的使用,這里說明一下
在調用InvokeTACommand函數時需要傳入params參數,params可傳入四個參數,類型有如下幾種,params使用場景大致有這幾種情況
1、當進入和返回都不需要攜帶參數時,使用TEE_PARAM_TYPE_NONE;
2、當只需要傳入參數,不需要返回參數時,使用TEE_PARAM_TYPE_VALUE_INPUT;
不需要傳入參數,只需要返回參數時,使用TEE_PARAM_TYPE_VALUE_OUTPUT;即需要傳入 也需要返回,使用TEE_PARAM_TYPE_VALUE_INOUT;
3、當需要分配buffer內存時,使用TEE_PARAM_TYPE_MEMREF_X,使用區別和第2條相同。

PARAM類型功能
TEE_PARAM_TYPE_NONE無需傳入參數和返回參數
TEE_PARAM_TYPE_VALUE_INPUT只需要傳入參數
TEE_PARAM_TYPE_VALUE_OUTPUT只需要return返回參數
TEE_PARAM_TYPE_VALUE_INOUT即需要傳入也需要返回參數
TEE_PARAM_TYPE_MEMREF_INPUT需要分配buff,只需要傳入參數
TEE_PARAM_TYPE_MEMREF_OUTPUT需要分配buff,只需要返回參數
TEE_PARAM_TYPE_MEMREF_INOUT需要分配buff,即需要返回參數 ,也需要返回參數

2. 設計思路

需要先構思一下,如何實現這個功能,把需求分解為兩部分。
1)封裝libhandlerot lib庫,便于TA中直接調用lib接口;
2)新增handle rot的PTA,實現libhandlerot中的幾個接口。

三、詳細設計

1. 修改代碼清單

先看一下總體修改了哪些代碼,修改的代碼量不是很多。
@optee_os/lib/libhandlerot lib庫中增加如下code

├── include │ └── libhandlerot.h ├── libhandlerot.c └── sub.mk

handle rot PTA中增加如下實現和對外頭文件

core/pta/handle_rot.c lib/libutee/include/pta_handle_rot.h

2. 子模塊設計

2.1 libhanlerot庫設計

新增libhandleroot.h頭文件,我們這里還是實現兩個接口,寫入數據handle_write_rot和讀取數據handle_read_rot。

/** libhandlerot interface*/#ifndef _LIB_HANDLE_ROT_H_ #define _LIB_HANDLE_ROT_H_#include <tee_internal_api.h> #include <pta_handle_rot.h>TEE_Result handle_write_rot(uint8_t* data, uint32_t size);TEE_Result handle_read_rot(uint8_t* data, uint32_t size);#endif /* _LIB_HANDLE_ROT_H_ */

這兩個接口的實現如下,封裝invoke_rot_pta中,通過command不同來區分read和write

TEE_Result handle_write_rot(uint8_t* data, uint32_t size) {TEE_Result res = TEE_ERROR_GENERIC;DMSG("[ROT] handle_write_rot entry.");res = invoke_rot_pta(PTA_ROT_CMD_WRITE, data, size);return res; }TEE_Result handle_read_rot(uint8_t* data, uint32_t size) {TEE_Result res = TEE_ERROR_GENERIC;res = invoke_rot_pta(PTA_ROT_CMD_READ, data, size);return res; }

invoke_rot_pta的實現如下,請注意TEE_PARAM_TYPES中的參數,在前面章節中有介紹過。
主要封裝了TEE_OpenTASession、TEE_PARAM_TYPES、TEE_InvokeTACommand、TEE_CloseTASession幾個接口,具體的實現需要在PTA中去完成。

/* * device root of trust handler */#include <string.h> #include <tee_internal_api_extensions.h> #include "libhandlerot.h"static TEE_Result invoke_rot_pta(uint32_t cmd_id, uint8_t* data, uint32_t size) {// Dispatch to PTATEE_Result res = TEE_ERROR_GENERIC;TEE_UUID uuid = PTA_ROT_UUID;TEE_TASessionHandle session = TEE_HANDLE_NULL;uint32_t ret_orig = 0;TEE_Param params[TEE_NUM_PARAMS];uint32_t param_types = 0;//open sessionres = TEE_OpenTASession(&uuid, TEE_TIMEOUT_INFINITE,0, params, &session, &ret_orig);if (res != TEE_SUCCESS) {EMSG("[libhandlerot] open pta session failed, res 0x%08x, origin 0x%08x",res, ret_orig);return res;}//invoke commandparam_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE);// data & sizeparams[0].memref.buffer = data;params[0].memref.size = size;DMSG("[libhandlerot] TEE_InvokeTACommand start.");res = TEE_InvokeTACommand(session, TEE_TIMEOUT_INFINITE, cmd_id,param_types, params, NULL);if (res != TEE_SUCCESS) {EMSG("[libhandlerot] Invoke command to pta failed, res 0x%08x, origin 0x%08x.",res, ret_orig);TEE_CloseTASession(session);session = TEE_HANDLE_NULL;return res;}//close sessionTEE_CloseTASession(session);session = TEE_HANDLE_NULL;return res; }

2.2 PTA模塊設計

handlerot PTA的設計,包含頭文件和實現文件。
pta_handle_rot.h實現如下:

#ifndef __PTA_HANDLE_ROT_H #define __PTA_HANDLE_ROT_H/** Interface to the rot pseudo-TA, which is used for handling* device root of trust function*/ //c7cf25d5-b62a-40bc-8841-2cefabbbf5bb #define PTA_ROT_UUID \{ 0xc7cf25d5, 0xb62a, 0x40bc, \{ 0x88, 0x41, 0x2c, 0xef, 0xab, 0xbb, 0xf5, 0xbb } }#define TA_NAME "handle_rot.ta"/** ROT PTA supported commands*/ #define PTA_ROT_CMD_READ 0 #define PTA_ROT_CMD_WRITE 1#endif /* __PTA_HANDLE_ROT_H */

頭文件中主要定義了UUID和TA_NAME,這個是TA的標識,需要唯一指定。

下面是handle_rot.c的實現

#include <bench.h> #include <compiler.h> #include <kernel/misc.h> #include <kernel/mutex.h> #include <kernel/pseudo_ta.h> #include <malloc.h> #include <mm/core_memprot.h> #include <mm/mobj.h> #include <mm/tee_mm.h> #include <mm/tee_pager.h> #include <mm/vm.h> #include <optee_rpc_cmd.h> #include <pta_handle_rot.h> #include <stdio.h> #include <string_ext.h> #include <string.h> #include <trace.h>uint8_t g_rotbuffer[64] = {0};static TEE_Result create_ta(void) {DMSG("[PTA] create_ta.");return TEE_SUCCESS; }static void destroy_ta(void) {DMSG("[PTA] destroy_ta."); }static TEE_Result open_session(uint32_t nParamTypes __unused,TEE_Param pParams[TEE_NUM_PARAMS] __unused,void **ppSessionContext __unused) {DMSG("[PTA] open_session.");return TEE_SUCCESS; }static void close_session(void *pSessionContext __unused) {DMSG("[PTA] close_session."); }static TEE_Result handle_rot_read_impl(uint8_t* buffer, uint32_t size) {TEE_Result ret = TEE_SUCCESS;DMSG("[PTA] handle_rot_read_impl pta entry.");memcpy(buffer, &(g_rotbuffer[0]), size);size_t i = 0;for(; i < size; i++){DMSG("[PTA] handle_rot_read_impl data:0x%x\n", buffer[i]);}return ret; }static TEE_Result handle_rot_write_impl(uint8_t* buffer, uint32_t size) {TEE_Result ret = TEE_SUCCESS;memset(g_rotbuffer, 0, 64);memcpy(&(g_rotbuffer[0]), buffer, size);size_t j = 0;for(; j < size; j++){DMSG("[PTA] handle_rot_write_impl data:0x%x\n", g_rotbuffer[j]);}return ret; }static TEE_Result invoke_command(void *session_ctx __unused,uint32_t cmd_id, uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS]) {DMSG("[PTA] invoke_command pta entry.");//check input param whether is invalidif (param_types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE)) {FMSG("[PTA] invalid params \"%s\"", TA_NAME);return TEE_ERROR_BAD_PARAMETERS;}//get input data buffer and sizeuint8_t* buffer = params[0].memref.buffer;uint32_t size = params[0].memref.size;switch (cmd_id) {case PTA_ROT_CMD_READ:DMSG("[PTA] invoke_command pta read data.");return handle_rot_read_impl(buffer, size);case PTA_ROT_CMD_WRITE:DMSG("[PTA] invoke_command pta write data.");return handle_rot_write_impl(buffer, size);default:break;}return TEE_ERROR_BAD_PARAMETERS; }pseudo_ta_register(.uuid = PTA_ROT_UUID, .name = TA_NAME,.flags = PTA_DEFAULT_FLAGS | TA_FLAG_SECURE_DATA_PATH | TA_FLAG_CONCURRENT,.create_entry_point = create_ta,.destroy_entry_point = destroy_ta,.open_session_entry_point = open_session,.close_session_entry_point = close_session,.invoke_command_entry_point = invoke_command);

我這里把PTA中會用的的接口全添加了,包括create_ta、destroy_ta;代碼邏輯也比較簡單。

2.3 TA模塊設計

普通TA中調用libhandlerot庫,去調用PTA中的實現,demo代碼如下:

#include <libhandlerot.h>TEE_Result get_rot_data() {TEE_Result res = TEE_SUCCESS;DMSG("[TA] get_rot_data entry. \n");uint8_t data[64] = "aaaabbbbccccdddd1111222233334444aaaabbbbccccdddd1111222233334444";res = handle_write_rot((uint8_t *)data, 64);uint8_t *temp = NULL;temp = malloc(65);res = handle_read_rot(temp, 64);DMSG ("[TA] handle_read_rot result:%d", res);int i = 0;for(; i < 64; i++) {DMSG ("[TA] handle_read_rot content:0x%x", temp[i]);}free(temp);DMSG("[TA] get_rot_data done. \n");return res; }

運行結果:

D/TA: get_rot_data:1516 [TA] get_rot_data entry. D/TA: handle_write_rot:60 [ROT] handle_write_rot entry. F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #5 (syscall_open_ta_session) D/TC:? 0 tee_ta_init_pseudo_ta_session:299 Lookup pseudo TA c7cf25d5-b62a-40bc-8841-2cefabbbf5bb D/TC:? 0 tee_ta_init_pseudo_ta_session:312 Open handle_rot.ta D/TC:? 0 tee_ta_init_pseudo_ta_session:329 handle_rot.ta : c7cf25d5-b62a-40bc-8841-2cefabbbf5bb D/TC:? 0 create_ta:29 [PTA] create_ta. D/TC:? 0 open_session:42 [PTA] open_session. D/TA: invoke_rot_pta:39 [ROT] TEE_InvokeTACommand start. F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #7 (syscall_invoke_ta_command) F/TC:? 0 trace_syscall:155 syscall #7 (syscall_invoke_ta_command) D/TC:? 0 invoke_command:88 [PTA] invoke_command pta entry. D/TC:? 0 invoke_command:109 [PTA] invoke_command pta write data. D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x61 D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x61 D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x61 D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x61 D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x62 D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x62 D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x62 ...省略 D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x34 D/TC:? 0 handle_rot_write_impl:78 [PTA] handle_rot_write_impl data:0x34 F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #7 (syscall_invoke_ta_command) F/TC:? 0 trace_syscall:155 syscall #6 (syscall_close_ta_session) D/TC:? 0 tee_ta_close_session:513 csess 0x902ebe80 id 3 D/TC:? 0 tee_ta_close_session:531 Destroy session D/TC:? 0 close_session:49 [PTA] close_session. D/TC:? 0 destroy_ta:35 [PTA] destroy_ta. F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #5 (syscall_open_ta_session) D/TC:? 0 tee_ta_init_session_with_context:607 Re-open TA c7cf25d5-b62a-40bc-8841-2cefabbbf5bb D/TC:? 0 create_ta:29 [PTA] create_ta. D/TC:? 0 open_session:42 [PTA] open_session. D/TA: invoke_rot_pta:39 [ROT] TEE_InvokeTACommand start. F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #7 (syscall_invoke_ta_command) F/TC:? 0 trace_syscall:155 syscall #7 (syscall_invoke_ta_command) D/TC:? 0 invoke_command:88 [PTA] invoke_command pta entry. D/TC:? 0 invoke_command:106 [PTA] invoke_command pta read data. D/TC:? 0 handle_rot_read_impl:56 [PTA] handle_rot_read_impl pta entry. D/TC:? 0 handle_rot_read_impl:62 [PTA] handle_rot_read_impl data:0x61 ...省略 D/TC:? 0 handle_rot_read_impl:62 [PTA] handle_rot_read_impl data:0x34 F/TC:? 0 trace_syscall:155 syscall #8 (syscall_check_access_rights) F/TC:? 0 trace_syscall:155 syscall #7 (syscall_invoke_ta_command) F/TC:? 0 trace_syscall:155 syscall #6 (syscall_close_ta_session) D/TC:? 0 tee_ta_close_session:513 csess 0x902ebc90 id 3 D/TC:? 0 tee_ta_close_session:531 Destroy session D/TC:? 0 close_session:49 [PTA] close_session. D/TC:? 0 destroy_ta:35 [PTA] destroy_ta. D/TA: get_rot_data:1526 [TA] handle_read_rot result:0 D/TA: get_rot_data:1529 [TA] handle_read_rot content:0x61 D/TA: get_rot_data:1529 [TA] handle_read_rot content:0x61 D/TA: get_rot_data:1529 [TA] handle_read_rot content:0x61 D/TA: get_rot_data:1529 [TA] handle_read_rot content:0x61 D/TA: get_rot_data:1529 [TA] handle_read_rot content:0x62 ...省略 D/TA: get_rot_data:1529 [TA] handle_read_rot content:0x34 D/TA: get_rot_data:1533 [TA] get_rot_data done.

總結

以上是生活随笔為你收集整理的【OPTEE开发】从TA到PTA的功能设计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。