bl1_main.c 8.3 KB

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