bl1_main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <platform_def.h>
  8. #include <arch.h>
  9. #include <arch_features.h>
  10. #include <arch_helpers.h>
  11. #include <bl1/bl1.h>
  12. #include <common/bl_common.h>
  13. #include <common/build_message.h>
  14. #include <common/debug.h>
  15. #include <drivers/auth/auth_mod.h>
  16. #include <drivers/auth/crypto_mod.h>
  17. #include <drivers/console.h>
  18. #include <lib/bootmarker_capture.h>
  19. #include <lib/cpus/errata.h>
  20. #include <lib/pmf/pmf.h>
  21. #include <lib/utils.h>
  22. #include <plat/common/platform.h>
  23. #include <smccc_helpers.h>
  24. #include <tools_share/uuid.h>
  25. #include "bl1_private.h"
  26. static void bl1_load_bl2(void);
  27. #if ENABLE_PAUTH
  28. uint64_t bl1_apiakey[2];
  29. #endif
  30. #if ENABLE_RUNTIME_INSTRUMENTATION
  31. PMF_REGISTER_SERVICE(bl_svc, PMF_RT_INSTR_SVC_ID,
  32. BL_TOTAL_IDS, PMF_DUMP_ENABLE)
  33. #endif
  34. /*******************************************************************************
  35. * Setup function for BL1.
  36. ******************************************************************************/
  37. void bl1_setup(void)
  38. {
  39. /* Enable early console if EARLY_CONSOLE flag is enabled */
  40. plat_setup_early_console();
  41. /* Perform early platform-specific setup */
  42. bl1_early_platform_setup();
  43. /* Perform late platform-specific setup */
  44. bl1_plat_arch_setup();
  45. #if CTX_INCLUDE_PAUTH_REGS
  46. /*
  47. * Assert that the ARMv8.3-PAuth registers are present or an access
  48. * fault will be triggered when they are being saved or restored.
  49. */
  50. assert(is_armv8_3_pauth_present());
  51. #endif /* CTX_INCLUDE_PAUTH_REGS */
  52. }
  53. /*******************************************************************************
  54. * Function to perform late architectural and platform specific initialization.
  55. * It also queries the platform to load and run next BL image. Only called
  56. * by the primary cpu after a cold boot.
  57. ******************************************************************************/
  58. void bl1_main(void)
  59. {
  60. unsigned int image_id;
  61. #if ENABLE_RUNTIME_INSTRUMENTATION
  62. PMF_CAPTURE_TIMESTAMP(bl_svc, BL1_ENTRY, PMF_CACHE_MAINT);
  63. #endif
  64. /* Announce our arrival */
  65. NOTICE(FIRMWARE_WELCOME_STR);
  66. NOTICE("BL1: %s\n", build_version_string);
  67. NOTICE("BL1: %s\n", build_message);
  68. INFO("BL1: RAM %p - %p\n", (void *)BL1_RAM_BASE, (void *)BL1_RAM_LIMIT);
  69. print_errata_status();
  70. #if ENABLE_ASSERTIONS
  71. u_register_t val;
  72. /*
  73. * Ensure that MMU/Caches and coherency are turned on
  74. */
  75. #ifdef __aarch64__
  76. val = read_sctlr_el3();
  77. #else
  78. val = read_sctlr();
  79. #endif
  80. assert((val & SCTLR_M_BIT) != 0);
  81. assert((val & SCTLR_C_BIT) != 0);
  82. assert((val & SCTLR_I_BIT) != 0);
  83. /*
  84. * Check that Cache Writeback Granule (CWG) in CTR_EL0 matches the
  85. * provided platform value
  86. */
  87. val = (read_ctr_el0() >> CTR_CWG_SHIFT) & CTR_CWG_MASK;
  88. /*
  89. * If CWG is zero, then no CWG information is available but we can
  90. * at least check the platform value is less than the architectural
  91. * maximum.
  92. */
  93. if (val != 0)
  94. assert(CACHE_WRITEBACK_GRANULE == SIZE_FROM_LOG2_WORDS(val));
  95. else
  96. assert(CACHE_WRITEBACK_GRANULE <= MAX_CACHE_LINE_SIZE);
  97. #endif /* ENABLE_ASSERTIONS */
  98. /* Perform remaining generic architectural setup from EL3 */
  99. bl1_arch_setup();
  100. crypto_mod_init();
  101. /* Initialize authentication module */
  102. auth_mod_init();
  103. /* Initialize the measured boot */
  104. bl1_plat_mboot_init();
  105. /* Perform platform setup in BL1. */
  106. bl1_platform_setup();
  107. #if ENABLE_PAUTH
  108. /* Store APIAKey_EL1 key */
  109. bl1_apiakey[0] = read_apiakeylo_el1();
  110. bl1_apiakey[1] = read_apiakeyhi_el1();
  111. #endif /* ENABLE_PAUTH */
  112. /* Get the image id of next image to load and run. */
  113. image_id = bl1_plat_get_next_image_id();
  114. /*
  115. * We currently interpret any image id other than
  116. * BL2_IMAGE_ID as the start of firmware update.
  117. */
  118. if (image_id == BL2_IMAGE_ID)
  119. bl1_load_bl2();
  120. else
  121. NOTICE("BL1-FWU: *******FWU Process Started*******\n");
  122. /* Teardown the measured boot driver */
  123. bl1_plat_mboot_finish();
  124. bl1_prepare_next_image(image_id);
  125. #if ENABLE_RUNTIME_INSTRUMENTATION
  126. PMF_CAPTURE_TIMESTAMP(bl_svc, BL1_EXIT, PMF_CACHE_MAINT);
  127. #endif
  128. console_flush();
  129. }
  130. /*******************************************************************************
  131. * This function locates and loads the BL2 raw binary image in the trusted SRAM.
  132. * Called by the primary cpu after a cold boot.
  133. * TODO: Add support for alternative image load mechanism e.g using virtio/elf
  134. * loader etc.
  135. ******************************************************************************/
  136. static void bl1_load_bl2(void)
  137. {
  138. image_desc_t *desc;
  139. image_info_t *info;
  140. int err;
  141. /* Get the image descriptor */
  142. desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
  143. assert(desc != NULL);
  144. /* Get the image info */
  145. info = &desc->image_info;
  146. INFO("BL1: Loading BL2\n");
  147. err = bl1_plat_handle_pre_image_load(BL2_IMAGE_ID);
  148. if (err != 0) {
  149. ERROR("Failure in pre image load handling of BL2 (%d)\n", err);
  150. plat_error_handler(err);
  151. }
  152. err = load_auth_image(BL2_IMAGE_ID, info);
  153. if (err != 0) {
  154. ERROR("Failed to load BL2 firmware.\n");
  155. plat_error_handler(err);
  156. }
  157. /* Allow platform to handle image information. */
  158. err = bl1_plat_handle_post_image_load(BL2_IMAGE_ID);
  159. if (err != 0) {
  160. ERROR("Failure in post image load handling of BL2 (%d)\n", err);
  161. plat_error_handler(err);
  162. }
  163. NOTICE("BL1: Booting BL2\n");
  164. }
  165. /*******************************************************************************
  166. * Function called just before handing over to the next BL to inform the user
  167. * about the boot progress. In debug mode, also print details about the BL
  168. * image's execution context.
  169. ******************************************************************************/
  170. void bl1_print_next_bl_ep_info(const entry_point_info_t *bl_ep_info)
  171. {
  172. #ifdef __aarch64__
  173. NOTICE("BL1: Booting BL31\n");
  174. #else
  175. NOTICE("BL1: Booting BL32\n");
  176. #endif /* __aarch64__ */
  177. print_entry_point_info(bl_ep_info);
  178. }
  179. #if SPIN_ON_BL1_EXIT
  180. void print_debug_loop_message(void)
  181. {
  182. NOTICE("BL1: Debug loop, spinning forever\n");
  183. NOTICE("BL1: Please connect the debugger to continue\n");
  184. }
  185. #endif
  186. /*******************************************************************************
  187. * Top level handler for servicing BL1 SMCs.
  188. ******************************************************************************/
  189. u_register_t bl1_smc_handler(unsigned int smc_fid,
  190. u_register_t x1,
  191. u_register_t x2,
  192. u_register_t x3,
  193. u_register_t x4,
  194. void *cookie,
  195. void *handle,
  196. unsigned int flags)
  197. {
  198. /* BL1 Service UUID */
  199. DEFINE_SVC_UUID2(bl1_svc_uid,
  200. U(0xd46739fd), 0xcb72, 0x9a4d, 0xb5, 0x75,
  201. 0x67, 0x15, 0xd6, 0xf4, 0xbb, 0x4a);
  202. #if TRUSTED_BOARD_BOOT
  203. /*
  204. * Dispatch FWU calls to FWU SMC handler and return its return
  205. * value
  206. */
  207. if (is_fwu_fid(smc_fid)) {
  208. return bl1_fwu_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
  209. handle, flags);
  210. }
  211. #endif
  212. switch (smc_fid) {
  213. case BL1_SMC_CALL_COUNT:
  214. SMC_RET1(handle, BL1_NUM_SMC_CALLS);
  215. case BL1_SMC_UID:
  216. SMC_UUID_RET(handle, bl1_svc_uid);
  217. case BL1_SMC_VERSION:
  218. SMC_RET1(handle, BL1_SMC_MAJOR_VER | BL1_SMC_MINOR_VER);
  219. default:
  220. WARN("Unimplemented BL1 SMC Call: 0x%x\n", smc_fid);
  221. SMC_RET1(handle, SMC_UNK);
  222. }
  223. }
  224. /*******************************************************************************
  225. * BL1 SMC wrapper. This function is only used in AArch32 mode to ensure ABI
  226. * compliance when invoking bl1_smc_handler.
  227. ******************************************************************************/
  228. u_register_t bl1_smc_wrapper(uint32_t smc_fid,
  229. void *cookie,
  230. void *handle,
  231. unsigned int flags)
  232. {
  233. u_register_t x1, x2, x3, x4;
  234. assert(handle != NULL);
  235. get_smc_params_from_ctx(handle, x1, x2, x3, x4);
  236. return bl1_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags);
  237. }