嵌入式开发论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 108|回复: 0

6. ATF(ARM Trusted firmware)启动---bl32(OP-TEE)

[复制链接]

59

主题

64

帖子

255

积分

版主

Rank: 7Rank: 7Rank: 7

积分
255
发表于 2018-11-29 18:22:05 | 显示全部楼层 |阅读模式


在bl31中的runtime_svc_init函数会初始化OP-TEE对应的service,通过调用该service的init函数来完成OP-TEE的启动。
1. opteed_setup
OP-TEE的service通过DECLARE_RT_SVC宏在编译的时候被存放到了rt_svc_des段中。启动该段中的init成员会被初始化成opteed_setup函数,该函数的内容如下:

  1. <span class="hljs-keyword">int32_t</span> opteed_setup(<span class="hljs-keyword">void</span>)
  2. {
  3.         <span class="hljs-keyword">entry_point_info_t</span> *optee_ep_info;
  4.         <span class="hljs-keyword">uint32_t</span> linear_id;

  5.         linear_id = plat_my_core_pos();

  6.         <span class="hljs-comment">/*
  7.          * Get information about the Secure Payload (BL32) image. Its
  8.          * absence is a critical failure.  <span class="hljs-doctag">TODO:</span> Add support to
  9.          * conditionally include the SPD service
  10.          */</span>
  11. <span class="hljs-comment">/* 获取bl32(OP-TEE)镜像的描述信息 */</span>
  12.         optee_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
  13.         <span class="hljs-keyword">if</span> (!optee_ep_info) {
  14.                 WARN(<span class="hljs-string">"No OPTEE provided by BL2 boot loader, Booting device"</span>
  15.                         <span class="hljs-string">" without OPTEE initialization. SMC`s destined for OPTEE"</span>
  16.                         <span class="hljs-string">" will return SMC_UNK\n"</span>);
  17.                 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
  18.         }

  19.         <span class="hljs-comment">/*
  20.          * If there's no valid entry point for SP, we return a non-zero value
  21.          * signalling failure initializing the service. We bail out without
  22.          * registering any handlers
  23.          */</span>
  24.         <span class="hljs-keyword">if</span> (!optee_ep_info->pc)
  25.                 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;

  26.         <span class="hljs-comment">/*
  27.          * We could inspect the SP image and determine it's execution
  28.          * state i.e whether AArch32 or AArch64. Assuming it's AArch32
  29.          * for the time being.
  30.          */</span>
  31.         opteed_rw = OPTEE_AARCH64;
  32. <span class="hljs-comment">/* 初始化CPU的smc上下文 */</span>
  33.         opteed_init_optee_ep_state(optee_ep_info,
  34.                                 opteed_rw,
  35.                                 optee_ep_info->pc,
  36.                                 &opteed_sp_context[linear_id]);

  37.         <span class="hljs-comment">/*
  38.          * All OPTEED initialization done. Now register our init function with
  39.          * BL31 for deferred invocation
  40.          */</span>
  41. <span class="hljs-comment">/* 使用Opteed_init初始化bl32_init变量,以备在bl31中调用 */</span>
  42.         bl31_register_bl32_init(&opteed_init);

  43.         <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
  44. }
复制代码



2. opteed_init

该函数将会在bl31中的所有service执行完init操作之后,通过执行存放在bl32_init变量中的函数指针来实现调用。该函数调用之后会进入到OP-TEE OS的初始化阶段。

  1. <span class="hljs-function"><span class="hljs-keyword">static</span> int32_t <span class="hljs-title">opteed_init</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
  2. </span>{
  3.         <span class="hljs-keyword">uint32_t</span> linear_id = plat_my_core_pos();
  4.         <span class="hljs-keyword">optee_context_t</span> *optee_ctx = &opteed_sp_context[linear_id];
  5.         <span class="hljs-keyword">entry_point_info_t</span> *optee_entry_point;
  6.         <span class="hljs-keyword">uint64_t</span> rc;

  7.         <span class="hljs-comment">/*
  8.          * Get information about the OPTEE (BL32) image. Its
  9.          * absence is a critical failure.
  10.          */</span>
  11. <span class="hljs-comment">/* 获取OPTEE image的信息 */</span>
  12.         optee_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
  13.         assert(optee_entry_point);

  14. <span class="hljs-comment">/* 使用optee image的entry point信息初始化cpu的上下文 */</span>
  15.         cm_init_my_context(optee_entry_point);

  16.         <span class="hljs-comment">/*
  17.          * Arrange for an entry into OPTEE. It will be returned via
  18.          * OPTEE_ENTRY_DONE case
  19.          */</span>
  20. <span class="hljs-comment">/* 开始设置CPU参数,最终会调用opteed_enter_sp函数执行跳转到OPTEE的操作 */</span>
  21.         rc = opteed_synchronous_sp_entry(optee_ctx);
  22.         assert(rc != <span class="hljs-number">0</span>);

  23.         <span class="hljs-keyword">return</span> rc;
  24. }
复制代码



3. opteed_enter_sp

opteed_entr_sp函数完成跳转到OP-TEE image进行执行的操作,该函数中将会保存一些列的寄存器值,设定好堆栈信息,然后通过调用el3_eixt函数来实现跳转操作

  1. func opteed_enter_sp
  2.         <span class="hljs-comment">/* Make space for the registers that we're going to save */</span>
  3.         mov        x3, sp
  4.         str        x3, [x0, #<span class="hljs-number">0</span>]
  5.         sub        sp, sp, #OPTEED_C_RT_CTX_SIZE

  6.         <span class="hljs-comment">/* Save callee-saved registers on to the stack */</span>
  7.         stp        x19, x20, [sp, #OPTEED_C_RT_CTX_X19]
  8.         stp        x21, x22, [sp, #OPTEED_C_RT_CTX_X21]
  9.         stp        x23, x24, [sp, #OPTEED_C_RT_CTX_X23]
  10.         stp        x25, x26, [sp, #OPTEED_C_RT_CTX_X25]
  11.         stp        x27, x28, [sp, #OPTEED_C_RT_CTX_X27]
  12.         stp        x29, x30, [sp, #OPTEED_C_RT_CTX_X29]

  13.         <span class="hljs-comment">/* ---------------------------------------------
  14.          * Everything is setup now. el3_exit() will
  15.          * use the secure context to restore to the
  16.          * general purpose and EL3 system registers to
  17.          * ERET into OPTEE.
  18.          * ---------------------------------------------
  19.          */</span>
  20.         b        el3_exit        <span class="hljs-comment">//使用设定好的安全CPU上下文,退出EL3进入OPTEE</span>
  21. endfunc opteed_enter_sp
复制代码








回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|TEE and Virtualization

GMT+8, 2019-3-22 20:25 , Processed in 0.097587 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表