bl1_main.c 7.5 KB

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