嵌入式开发论坛

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

3. ATF(ARM Trusted firmware)启动---bl2

[复制链接]

59

主题

64

帖子

255

积分

版主

Rank: 7Rank: 7Rank: 7

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

BL2 image将会为后续image的加载执行相关的初始化操作。主要是内存,MMU,串口以及EL3软件运行环境的设置,并且加载bl3x的image到RAM中。通过查看bl2.ld.S文件就可以发现,bl2 image的入口函数是bl2_entrypoint。该函数定义在bl2/aarch64/bl2_entrypoint.S文件中。
1.bl2_entrypoint该函数的内容如下,该函数最终会出发smc操作,从bl1中将CPU的控制权转交给bl31:

  1. func bl2_entrypoint
  2.         <span class="hljs-comment">/*---------------------------------------------
  3.          * Save from x1 the extents of the tzram
  4.          * available to BL2 for future use.
  5.          * x0 is not currently used.
  6.          * ---------------------------------------------
  7.          */</span>
  8.         mov        x20, x1

  9.         <span class="hljs-comment">/* ---------------------------------------------
  10.          * Set the exception vector to something sane.
  11.          * ---------------------------------------------
  12.          */</span>
  13.         adr        x0, early_exceptions        <span class="hljs-comment">//设定异常向量</span>
  14.         msr        vbar_el1, x0
  15.         isb

  16.         <span class="hljs-comment">/* ---------------------------------------------
  17.          * Enable the SError interrupt now that the
  18.          * exception vectors have been setup.
  19.          * ---------------------------------------------
  20.          */</span>
  21.         msr        daifclr, #DAIF_ABT_BIT

  22.         <span class="hljs-comment">/* ---------------------------------------------
  23.          * Enable the instruction cache, stack pointer
  24.          * and data access alignment checks
  25.          * ---------------------------------------------
  26.          */</span>
  27.         mov        x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
  28.         mrs        x0, sctlr_el1
  29.         orr        x0, x0, x1
  30.         msr        sctlr_el1, x0
  31.         isb

  32.         <span class="hljs-comment">/* ---------------------------------------------
  33.          * Invalidate the RW memory used by the BL2
  34.          * image. This includes the data and NOBITS
  35.          * sections. This is done to safeguard against
  36.          * possible corruption of this memory by dirty
  37.          * cache lines in a system cache as a result of
  38.          * use by an earlier boot loader stage.
  39.          * ---------------------------------------------
  40.          */</span>
  41.         adr        x0, __RW_START__
  42.         adr        x1, __RW_END__
  43.         sub        x1, x1, x0
  44.         bl        inv_dcache_range

  45.         <span class="hljs-comment">/* ---------------------------------------------
  46.          * Zero out NOBITS sections. There are 2 of them:
  47.          *   - the .bss section;
  48.          *   - the coherent memory section.
  49.          * ---------------------------------------------
  50.          */</span>
  51.         ldr        x0, =__BSS_START__
  52.         ldr        x1, =__BSS_SIZE__
  53.         bl        zeromem

  54. #<span class="hljs-keyword">if</span> USE_COHERENT_MEM
  55.         ldr        x0, =__COHERENT_RAM_START__
  56.         ldr        x1, =__COHERENT_RAM_UNALIGNED_SIZE__
  57.         bl        zeromem
  58. #endif

  59.         <span class="hljs-comment">/* --------------------------------------------
  60.          * Allocate a stack whose memory will be marked
  61.          * as Normal-IS-WBWA when the MMU is enabled.
  62.          * There is no risk of reading stale stack
  63.          * memory after enabling the MMU as only the
  64.          * primary cpu is running at the moment.
  65.          * --------------------------------------------
  66.          */</span>
  67.         bl        plat_set_my_stack        <span class="hljs-comment">//初始化bl2运行的栈</span>

  68.         <span class="hljs-comment">/* ---------------------------------------------
  69.          * Initialize the stack protector canary before
  70.          * any C code is called.
  71.          * ---------------------------------------------
  72.          */</span>
  73. #<span class="hljs-keyword">if</span> STACK_PROTECTOR_ENABLED
  74.         bl        update_stack_protector_canary
  75. #endif

  76.         <span class="hljs-comment">/* ---------------------------------------------
  77.          * Perform early platform setup & platform
  78.          * specific early arch. setup e.g. mmu setup
  79.          * ---------------------------------------------
  80.          */</span>
  81.         mov        x0, x20
  82.         bl        bl2_early_platform_setup        <span class="hljs-comment">//设置平台相关</span>
  83.         bl        bl2_plat_arch_setup        <span class="hljs-comment">//设置架构相关</span>

  84.         <span class="hljs-comment">/* ---------------------------------------------
  85.          * Jump to main function.
  86.          * ---------------------------------------------
  87.          */</span>
  88.         bl        bl2_main        <span class="hljs-comment">//跳转到BL2的主要函数执行,从该函数中跳转到bl31以及bl31,</span>

  89.         <span class="hljs-comment">/* ---------------------------------------------
  90.          * Should never reach this point.
  91.          * ---------------------------------------------
  92.          */</span>
  93.         no_ret        plat_panic_handler

  94. endfunc bl2_entrypoint
复制代码



2. bl2_main
该函数主要实现将bl3x的image加载RAM中,并通过smc调用执行bl1中指定的smc handle将CPU的全向交给bl31。
  1. <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">bl2_main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span>
  2. </span>{
  3.         <span class="hljs-keyword">entry_point_info_t</span> *next_bl_ep_info;

  4.         NOTICE(<span class="hljs-string">"BL2: %s\n"</span>, version_string);
  5.         NOTICE(<span class="hljs-string">"BL2: %s\n"</span>, build_message);

  6.         <span class="hljs-comment">/* Perform remaining generic architectural setup in S-EL1 */</span>
  7.         bl2_arch_setup();

  8. <span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> TRUSTED_BOARD_BOOT</span>
  9.         <span class="hljs-comment">/* Initialize authentication module */</span>
  10.         auth_mod_init();        <span class="hljs-comment">//初始化image验证模块</span>
  11. <span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">/* TRUSTED_BOARD_BOOT */</span></span>

  12.         <span class="hljs-comment">/* Load the subsequent bootloader images. */</span>
  13.         next_bl_ep_info = bl2_load_images();        <span class="hljs-comment">//加载bl3x image到RAM中并返回bl31的入口地址</span>

  14. <span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> AARCH32</span>
  15.         <span class="hljs-comment">/*
  16.          * For AArch32 state BL1 and BL2 share the MMU setup.
  17.          * Given that BL2 does not map BL1 regions, MMU needs
  18.          * to be disabled in order to go back to BL1.
  19.          */</span>
  20.         disable_mmu_icache_secure();
  21. <span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">/* AARCH32 */</span></span>

  22.         console_flush();

  23.         <span class="hljs-comment">/*
  24.          * Run next BL image via an SMC to BL1. Information on how to pass
  25.          * control to the BL32 (if present) and BL33 software images will
  26.          * be passed to next BL image as an argument.
  27.          */</span>
  28. <span class="hljs-comment">/* 调用smc指令,触发在bl1中设定的smc异常中断处理函数,跳转到bl31 */</span>
  29.         smc(BL1_SMC_RUN_IMAGE, (<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">long</span>)next_bl_ep_info, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);
  30. }
复制代码





3. bl2_load_images
该函数用来加载bl3x的image到RAM中,返回一个具有image入口信息的变量。smc handle根据该变量跳转到bl31进行执行


  1. <span class="hljs-keyword">entry_point_info_t</span> *bl2_load_images(<span class="hljs-keyword">void</span>)
  2. {
  3.         <span class="hljs-keyword">bl_params_t</span> *bl2_to_next_bl_params;
  4.         <span class="hljs-keyword">bl_load_info_t</span> *bl2_load_info;
  5.         <span class="hljs-keyword">const</span> <span class="hljs-keyword">bl_load_info_node_t</span> *bl2_node_info;
  6.         <span class="hljs-keyword">int</span> plat_setup_done = <span class="hljs-number">0</span>;
  7.         <span class="hljs-keyword">int</span> err;

  8.         <span class="hljs-comment">/*
  9.          * Get information about the images to load.
  10.          */</span>
  11. <span class="hljs-comment">/* 获取bl3x image的加载和入口信息 */</span>
  12.         bl2_load_info = plat_get_bl_image_load_info();

  13. <span class="hljs-comment">/* 检查返回的bl2_load_info中的信息是否正确 */</span>
  14.         assert(bl2_load_info);
  15.         assert(bl2_load_info->head);
  16.         assert(bl2_load_info->h.type == PARAM_BL_LOAD_INFO);
  17.         assert(bl2_load_info->h.version >= VERSION_2);

  18. <span class="hljs-comment">/* 将bl2_load_info中的head变量的值赋值为bl2_node_info,即将bl31 image的入口信息传递給bl2_node_info变量 */</span>
  19.         bl2_node_info = bl2_load_info->head;

  20. <span class="hljs-comment">/* 进入loop循环, */</span>
  21.         <span class="hljs-keyword">while</span> (bl2_node_info) {
  22.                 <span class="hljs-comment">/*
  23.                  * Perform platform setup before loading the image,
  24.                  * if indicated in the image attributes AND if NOT
  25.                  * already done before.
  26.                  */</span>
  27. <span class="hljs-comment">/* 在加载特定的bl3x image到RAM之前先确定是否需要做平台的初始化 */</span>
  28.                 <span class="hljs-keyword">if</span> (bl2_node_info->image_info->h.attr & IMAGE_ATTRIB_PLAT_SETUP) {
  29.                         <span class="hljs-keyword">if</span> (plat_setup_done) {
  30.                                 WARN(<span class="hljs-string">"BL2: Platform setup already done!!\n"</span>);
  31.                         } <span class="hljs-keyword">else</span> {
  32.                                 INFO(<span class="hljs-string">"BL2: Doing platform setup\n"</span>);
  33.                                 bl2_platform_setup();
  34.                                 plat_setup_done = <span class="hljs-number">1</span>;
  35.                         }
  36.                 }

  37. <span class="hljs-comment">/* 对bl3x image进行电子验签,如果通过则执行加载操作 */</span>
  38.                 <span class="hljs-keyword">if</span> (!(bl2_node_info->image_info->h.attr & IMAGE_ATTRIB_SKIP_LOADING)) {
  39.                         INFO(<span class="hljs-string">"BL2: Loading image id %d\n"</span>, bl2_node_info->image_id);
  40.                         err = load_auth_image(bl2_node_info->image_id,
  41.                                 bl2_node_info->image_info);
  42.                         <span class="hljs-keyword">if</span> (err) {
  43.                                 ERROR(<span class="hljs-string">"BL2: Failed to load image (%i)\n"</span>, err);
  44.                                 plat_error_handler(err);
  45.                         }
  46.                 } <span class="hljs-keyword">else</span> {
  47.                         INFO(<span class="hljs-string">"BL2: Skip loading image id %d\n"</span>, bl2_node_info->image_id);
  48.                 }

  49.                 <span class="hljs-comment">/* Allow platform to handle image information. */</span>
  50. <span class="hljs-comment">/* 可以根据实际需要更改,通过给定image ID来更改image的加载信息 */</span>
  51.                 err = bl2_plat_handle_post_image_load(bl2_node_info->image_id);
  52.                 <span class="hljs-keyword">if</span> (err) {
  53.                         ERROR(<span class="hljs-string">"BL2: Failure in post image load handling (%i)\n"</span>, err);
  54.                         plat_error_handler(err);
  55.                 }

  56.                 <span class="hljs-comment">/* Go to next image */</span>
  57.                 bl2_node_info = bl2_node_info->next_load_info;
  58.         }

  59.         <span class="hljs-comment">/*
  60.          * Get information to pass to the next image.
  61.          */</span>
  62. <span class="hljs-comment">/* 获取下一个执行的Image的入口信息,并且将以后会被执行的image的入口信息组合成链表 ,t通过判断image des中的ep_info.h.attr的值是否为(EXECUTABLE|EP_FIRST_EX)来确定接下来第一个被执行的image*/</span>
  63.         bl2_to_next_bl_params = plat_get_next_bl_params();
  64.         assert(bl2_to_next_bl_params);
  65.         assert(bl2_to_next_bl_params->head);
  66.         assert(bl2_to_next_bl_params->h.type == PARAM_BL_PARAMS);
  67.         assert(bl2_to_next_bl_params->h.version >= VERSION_2);

  68.         <span class="hljs-comment">/* Flush the parameters to be passed to next image */</span>
  69.         plat_flush_next_bl_params();

  70. <span class="hljs-comment">/* 返回下一个进入的image的入口信息,即bl31的入口信息 */</span>
  71.         <span class="hljs-keyword">return</span> bl2_to_next_bl_params->head->ep_info;
  72. }
复制代码




4.REGISTER_BL_IMAGE_DESCS(bl2_mem_params_descs)

该宏的执行将会初始化组成bl2加载bl3x image的列表使用到的重要全局变量,其中bl2_mem_params_descs变量的定义如下:

  1. <span class="hljs-keyword">static</span> <span class="hljs-keyword">bl_mem_params_node_t</span> bl2_mem_params_descs[] = {
  2. #ifdef SCP_BL2_BASE
  3.         <span class="hljs-comment">/* Fill SCP_BL2 related information if it exists */</span>
  4.     {
  5.             .image_id = SCP_BL2_IMAGE_ID,

  6.             SET_STATIC_PARAM_HEAD(ep_info, PARAM_IMAGE_BINARY,
  7.                     VERSION_2, <span class="hljs-keyword">entry_point_info_t</span>, SECURE | NON_EXECUTABLE),

  8.             SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
  9.                     VERSION_2, <span class="hljs-keyword">image_info_t</span>, <span class="hljs-number">0</span>),
  10.             .image_info.image_base = SCP_BL2_BASE,
  11.             .image_info.image_max_size = PLAT_CSS_MAX_SCP_BL2_SIZE,

  12.             .next_handoff_image_id = INVALID_IMAGE_ID,
  13.     },
  14. #endif <span class="hljs-comment">/* SCP_BL2_BASE */</span>

  15. #ifdef EL3_PAYLOAD_BASE
  16.         <span class="hljs-comment">/* Fill EL3 payload related information (BL31 is EL3 payload)*/</span>
  17.     {
  18.             .image_id = BL31_IMAGE_ID,

  19.             SET_STATIC_PARAM_HEAD(ep_info, PARAM_EP,
  20.                     VERSION_2, <span class="hljs-keyword">entry_point_info_t</span>,
  21.                     SECURE | EXECUTABLE | EP_FIRST_EXE),
  22.             .ep_info.pc = EL3_PAYLOAD_BASE,
  23.             .ep_info.spsr = SPSR_64(MODE_EL3, MODE_SP_ELX,
  24.                     DISABLE_ALL_EXCEPTIONS),

  25.             SET_STATIC_PARAM_HEAD(image_info, PARAM_EP,
  26.                     VERSION_2, <span class="hljs-keyword">image_info_t</span>,
  27.                     IMAGE_ATTRIB_PLAT_SETUP | IMAGE_ATTRIB_SKIP_LOADING),

  28.             .next_handoff_image_id = INVALID_IMAGE_ID,
  29.     },

  30. #<span class="hljs-keyword">else</span> <span class="hljs-comment">/* EL3_PAYLOAD_BASE */</span>

  31.         <span class="hljs-comment">/* Fill BL31 related information */</span>
  32.     {
  33.             .image_id = BL31_IMAGE_ID,

  34.             SET_STATIC_PARAM_HEAD(ep_info, PARAM_EP,
  35.                     VERSION_2, <span class="hljs-keyword">entry_point_info_t</span>,
  36.                     SECURE | EXECUTABLE | EP_FIRST_EXE),
  37.             .ep_info.pc = BL31_BASE,
  38.             .ep_info.spsr = SPSR_64(MODE_EL3, MODE_SP_ELX,
  39.                     DISABLE_ALL_EXCEPTIONS),
  40. #<span class="hljs-keyword">if</span> DEBUG
  41.             .ep_info.args.arg1 = ARM_BL31_PLAT_PARAM_VAL,
  42. #endif

  43.             SET_STATIC_PARAM_HEAD(image_info, PARAM_EP,
  44.                     VERSION_2, <span class="hljs-keyword">image_info_t</span>, IMAGE_ATTRIB_PLAT_SETUP),
  45.             .image_info.image_base = BL31_BASE,
  46.             .image_info.image_max_size = BL31_LIMIT - BL31_BASE,

  47. # ifdef BL32_BASE
  48.             .next_handoff_image_id = BL32_IMAGE_ID,
  49. # <span class="hljs-keyword">else</span>
  50.             .next_handoff_image_id = BL33_IMAGE_ID,
  51. # endif
  52.     },

  53. # ifdef BL32_BASE
  54.         <span class="hljs-comment">/* Fill BL32 related information */</span>
  55.     {
  56.             .image_id = BL32_IMAGE_ID,

  57.             SET_STATIC_PARAM_HEAD(ep_info, PARAM_EP,
  58.                     VERSION_2, <span class="hljs-keyword">entry_point_info_t</span>, SECURE | EXECUTABLE),
  59.             .ep_info.pc = BL32_BASE,

  60.             SET_STATIC_PARAM_HEAD(image_info, PARAM_EP,
  61.                     VERSION_2, <span class="hljs-keyword">image_info_t</span>, <span class="hljs-number">0</span>),
  62.             .image_info.image_base = BL32_BASE,
  63.             .image_info.image_max_size = BL32_LIMIT - BL32_BASE,

  64.             .next_handoff_image_id = BL33_IMAGE_ID,
  65.     },
  66. # endif <span class="hljs-comment">/* BL32_BASE */</span>

  67.         <span class="hljs-comment">/* Fill BL33 related information */</span>
  68.     {
  69.             .image_id = BL33_IMAGE_ID,
  70.             SET_STATIC_PARAM_HEAD(ep_info, PARAM_EP,
  71.                     VERSION_2, <span class="hljs-keyword">entry_point_info_t</span>, NON_SECURE | EXECUTABLE),
  72. # ifdef PRELOADED_BL33_BASE
  73.             .ep_info.pc = PRELOADED_BL33_BASE,

  74.             SET_STATIC_PARAM_HEAD(image_info, PARAM_EP,
  75.                     VERSION_2, <span class="hljs-keyword">image_info_t</span>, IMAGE_ATTRIB_SKIP_LOADING),
  76. # <span class="hljs-keyword">else</span>
  77.             .ep_info.pc = PLAT_ARM_NS_IMAGE_OFFSET,

  78.             SET_STATIC_PARAM_HEAD(image_info, PARAM_EP,
  79.                     VERSION_2, <span class="hljs-keyword">image_info_t</span>, <span class="hljs-number">0</span>),
  80.             .image_info.image_base = PLAT_ARM_NS_IMAGE_OFFSET,
  81.             .image_info.image_max_size = ARM_DRAM1_SIZE,
  82. # endif <span class="hljs-comment">/* PRELOADED_BL33_BASE */</span>

  83.             .next_handoff_image_id = INVALID_IMAGE_ID,
  84.     }
  85. #endif <span class="hljs-comment">/* EL3_PAYLOAD_BASE */</span>
  86. };
复制代码



在该变量中规定了SCP_BL2, EL3_payload, bl32, bl33 image的相关信息,例如:

image的入口地址信息:ep_info
image在RAM中的基地址:image_base
image的基本信息:image_info
image的ID值:image_id

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2019-5-24 06:43 , Processed in 0.088211 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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