Arm-Trusted-Firmware代码分析

ATF

BL1

bl1_entrypoint

bl1/bl1.ld.S
ENTRY(bl1_entrypoint)

  • el3_entrypoint_common

    _exception_vectors设置el3异常向量, MVBAR

    • bl1_exceptions

      • SynchronousExceptionA64

        • smc_handler64

          • BL1_SMC_RUN_IMAGE

          • smc_handler

            • bl1_smc_handler

              • bl1_fwu_smc_handler
  • bl1_setup

    • bl1_early_platform_setup
    • bl1_plat_arch_setup
  • pauth_init_enable_el3

    if ENABLE_PAUTH
    PAC

  • bl1_main

    • bl1_arch_setup

    • auth_mod_init

      if TRUSTED_BOARD_BOOT

      • crypto_mod_init
      • img_parser_init
    • bl1_platform_setup

      • arm_bl1_platform_setup
      • soc_css_security_setup
    • bl1_plat_get_next_image_id

      • NS_BL2U_IMAGE_ID
      • BL2_IMAGE_ID
    • bl1_load_bl2

      image_id == BL2_IMAGE_ID

      • bl1_plat_get_image_desc

      • bl1_plat_handle_pre_image_load

      • load_auth_image

        • load_auth_image_internal

          • load_auth_image_recursive

            • load_image

            • auth_mod_verify_img

              • img_parser_check_integrity
              • auth_hash
              • auth_signature
              • auth_nvctr
          • load_image_flush

            • load_image
    • bl1_prepare_next_image

      • bl1_plat_get_image_desc

      • next_bl_ep->spsr

      • bl1_plat_set_ep_info

        Allow platform to make change

      • cm_init_my_context

        • cm_get_context

        • cm_setup_context

          • write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
          • write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
          • write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
      • cm_prepare_el3_exit

        • cm_el1_sysregs_context_restore
        • cm_set_next_eret_context
      • desc->state = IMAGE_STATE_EXECUTED;

  • el3_exit

BL2

bl2_entrypoint(BL2_AT_EL3)

bl2/bl2_el3.ld.S
ENTRY(bl2_entrypoint)
bl2/aarch64/bl2_el3_entrypoint.S

  • el3_entrypoint_common

    • bl2_el3_exceptions
  • bl2_el3_setup

    • bl2_el3_early_platform_setup
    • bl2_el3_plat_arch_setup
  • pauth_init_enable_el3

  • bl2_main

    • bl2_arch_setup

    • auth_mod_init

      • crypto_mod_init
      • img_parser_init
    • bl2_plat_preload_setup

      Fix PIE

    • bl2_load_images

    • pauth_disable_el1

    • bl2_run_next_image

bl2_entrypoint(!BL2_AT_EL3)

bl2/bl2.ld.S
ENTRY(bl2_entrypoint)
bl2/aarch32/bl2_entrypoint.S

  • msr vbar_el1, x0 #early_exceptions

  • RW memory

  • BSS

  • inv_dcache_range

    禁止数据cache

  • plat_set_my_stack

  • update_stack_protector_canary

  • bl2_setup

    • bl2_early_platform_setup2
    • bl2_plat_arch_setup
  • pauth_init_enable_el1

  • bl2_main

    • bl2_arch_setup

    • auth_mod_init

      if TRUSTED_BOARD_BOOT

      • crypto_mod_init
      • img_parser_init
    • bl2_plat_preload_setup

      Fix PIE

    • bl2_load_images

      • plat_get_bl_image_load_info
      • bl2_plat_handle_pre_image_load
      • load_auth_image
      • bl2_plat_handle_post_image_load
      • plat_flush_next_bl_params
    • disable_mmu_icache_secure

    • pauth_disable_el1

    • smc(BL1_SMC_RUN_IMAGE, (unsigned long)next_bl_ep_info, 0, 0, 0, 0, 0, 0);

BL3

BL31

bl31/bl31.ld.S
ENTRY(bl31_entrypoint)

  • bl31_entrypoint

    • el3_entrypoint_common

      • runtime_exceptions

        • sync_exception_aarch64

          • handle_sync_exception

            • smc_handler64
            • smc_handler32
        • sync_exception_aarch32

          • handle_sync_exception
    • bl31_setup

      • bl31_early_platform_setup2
      • bl31_plat_arch_setup
    • pauth_init_enable_el3

    • bl31_main

      • bl31_platform_setup

      • bl31_lib_init

      • runtime_svc_init

        • service = &RT_SVC_DESCS_START[index]

        • service->init()

        • rt_svc_descs_indices[start_idx] = index;

          根据service的call type和start oen确定唯一的index,并将所有call type映射到同一个index
          start_idx = (uint8_t)get_unique_oen(service->start_oen, service->call_type);
          end_idx = (uint8_t)get_unique_oen(service->end_oen, service->call_type);

      • (*bl32_init)()

        if (bl32_init != NULL)

      • bl31_prepare_next_image_entry

        • cm_init_my_context

          • cm_get_context

          • cm_setup_context

            • write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
            • write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
            • write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
        • cm_prepare_el3_exit

          • cm_el1_sysregs_context_restore
          • cm_set_next_eret_context
      • bl31_plat_runtime_setup

    • el3_exit

BL32

BL33

RT_SVC_DESCS_START

DECLARE_RT_SVC

static const rt_svc_desc_t _svc_desc ## _name
__section(“rt_svc_descs”) __used = {
.start_oen = (_start),
.end_oen = (_end),
.call_type = (_type),
.name = #_name,
.init = (_setup),
.handle = (_smch)
}

tspd

  • tspd_setup

    • bl31_register_bl32_init

      • bl32_init = &tspd_init

        • tspd_synchronous_sp_entry
  • tspd_smc_handler

optee

  • opteed_setup

    • bl31_plat_get_next_image_ep_info

    • opteed_init_optee_ep_state

    • bl31_register_bl32_init

      • bl32_init = &opteed_init

        • opteed_synchronous_sp_entry
  • opteed_smc_handler

Arm-Trusted-Firmware代码分析