fvp_r_bl1_main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include "../../../../bl1/bl1_private.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/console.h>
  16. #include <lib/cpus/errata.h>
  17. #include <lib/utils.h>
  18. #include <smccc_helpers.h>
  19. #include <tools_share/uuid.h>
  20. #include <plat/arm/common/plat_arm.h>
  21. #include <plat/common/platform.h>
  22. #include <platform_def.h>
  23. void cm_prepare_el2_exit(void);
  24. void bl1_run_next_image(const struct entry_point_info *bl_ep_info);
  25. /*******************************************************************************
  26. * Function to perform late architectural and platform specific initialization.
  27. * It also queries the platform to load and run next BL image. Only called
  28. * by the primary cpu after a cold boot.
  29. ******************************************************************************/
  30. void bl1_transfer_bl33(void)
  31. {
  32. unsigned int image_id;
  33. /* Get the image id of next image to load and run. */
  34. image_id = bl1_plat_get_next_image_id();
  35. #if !ARM_DISABLE_TRUSTED_WDOG
  36. /* Disable watchdog before leaving BL1 */
  37. plat_arm_secure_wdt_stop();
  38. #endif
  39. bl1_run_next_image(&bl1_plat_get_image_desc(image_id)->ep_info);
  40. }
  41. /*******************************************************************************
  42. * This function locates and loads the BL33 raw binary image in the trusted SRAM.
  43. * Called by the primary cpu after a cold boot.
  44. * TODO: Add support for alternative image load mechanism e.g using virtio/elf
  45. * loader etc.
  46. ******************************************************************************/
  47. void bl1_load_bl33(void)
  48. {
  49. image_desc_t *desc;
  50. image_info_t *info;
  51. int err;
  52. /* Get the image descriptor */
  53. desc = bl1_plat_get_image_desc(BL33_IMAGE_ID);
  54. assert(desc != NULL);
  55. /* Get the image info */
  56. info = &desc->image_info;
  57. INFO("BL1: Loading BL33\n");
  58. err = bl1_plat_handle_pre_image_load(BL33_IMAGE_ID);
  59. if (err != 0) {
  60. ERROR("Failure in pre image load handling of BL33 (%d)\n", err);
  61. plat_error_handler(err);
  62. }
  63. err = load_auth_image(BL33_IMAGE_ID, info);
  64. if (err != 0) {
  65. ERROR("Failed to load BL33 firmware.\n");
  66. plat_error_handler(err);
  67. }
  68. /* Allow platform to handle image information. */
  69. err = bl1_plat_handle_post_image_load(BL33_IMAGE_ID);
  70. if (err != 0) {
  71. ERROR("Failure in post image load handling of BL33 (%d)\n", err);
  72. plat_error_handler(err);
  73. }
  74. NOTICE("BL1: Booting BL33\n");
  75. }
  76. /*******************************************************************************
  77. * Helper utility to calculate the BL2 memory layout taking into consideration
  78. * the BL1 RW data assuming that it is at the top of the memory layout.
  79. ******************************************************************************/
  80. void bl1_calc_bl2_mem_layout(const meminfo_t *bl1_mem_layout,
  81. meminfo_t *bl2_mem_layout)
  82. {
  83. assert(bl1_mem_layout != NULL);
  84. assert(bl2_mem_layout != NULL);
  85. /*
  86. * Remove BL1 RW data from the scope of memory visible to BL2.
  87. * This is assuming BL1 RW data is at the top of bl1_mem_layout.
  88. */
  89. assert(bl1_mem_layout->total_base < BL1_RW_BASE);
  90. bl2_mem_layout->total_base = bl1_mem_layout->total_base;
  91. bl2_mem_layout->total_size = BL1_RW_BASE - bl1_mem_layout->total_base;
  92. flush_dcache_range((uintptr_t)bl2_mem_layout, sizeof(meminfo_t));
  93. }
  94. /*******************************************************************************
  95. * This function prepares for entry to BL33
  96. ******************************************************************************/
  97. void bl1_prepare_next_image(unsigned int image_id)
  98. {
  99. unsigned int mode = MODE_EL1;
  100. image_desc_t *desc;
  101. entry_point_info_t *next_bl_ep;
  102. #if CTX_INCLUDE_AARCH32_REGS
  103. /*
  104. * Ensure that the build flag to save AArch32 system registers in CPU
  105. * context is not set for AArch64-only platforms.
  106. */
  107. if (el_implemented(1) == EL_IMPL_A64ONLY) {
  108. ERROR("EL1 supports AArch64-only. Please set build flag %s",
  109. "CTX_INCLUDE_AARCH32_REGS = 0\n");
  110. panic();
  111. }
  112. #endif
  113. /* Get the image descriptor. */
  114. desc = bl1_plat_get_image_desc(image_id);
  115. assert(desc != NULL);
  116. /* Get the entry point info. */
  117. next_bl_ep = &desc->ep_info;
  118. /* FVP-R is only secure */
  119. assert(GET_SECURITY_STATE(next_bl_ep->h.attr) == SECURE);
  120. /* Prepare the SPSR for the next BL image. */
  121. next_bl_ep->spsr = (uint32_t)SPSR_64((uint64_t) mode,
  122. (uint64_t)MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  123. /* Allow platform to make change */
  124. bl1_plat_set_ep_info(image_id, next_bl_ep);
  125. /* Prepare context for the next EL */
  126. cm_prepare_el2_exit();
  127. /* Indicate that image is in execution state. */
  128. desc->state = IMAGE_STATE_EXECUTED;
  129. print_entry_point_info(next_bl_ep);
  130. }
  131. /*******************************************************************************
  132. * Setup function for BL1.
  133. ******************************************************************************/
  134. void bl1_setup(void)
  135. {
  136. /* Perform early platform-specific setup */
  137. bl1_early_platform_setup();
  138. /* Perform late platform-specific setup */
  139. bl1_plat_arch_setup();
  140. }
  141. /*******************************************************************************
  142. * Function to perform late architectural and platform specific initialization.
  143. * It also queries the platform to load and run next BL image. Only called
  144. * by the primary cpu after a cold boot.
  145. ******************************************************************************/
  146. void bl1_main(void)
  147. {
  148. unsigned int image_id;
  149. /* Announce our arrival */
  150. NOTICE(FIRMWARE_WELCOME_STR);
  151. NOTICE("BL1: %s\n", version_string);
  152. NOTICE("BL1: %s\n", build_message);
  153. INFO("BL1: RAM %p - %p\n", (void *)BL1_RAM_BASE, (void *)BL1_RAM_LIMIT);
  154. print_errata_status();
  155. #if ENABLE_ASSERTIONS
  156. u_register_t val;
  157. /*
  158. * Ensure that MMU/Caches and coherency are turned on
  159. */
  160. val = read_sctlr_el2();
  161. assert((val & SCTLR_M_BIT) != 0U);
  162. assert((val & SCTLR_C_BIT) != 0U);
  163. assert((val & SCTLR_I_BIT) != 0U);
  164. /*
  165. * Check that Cache Writeback Granule (CWG) in CTR_EL0 matches the
  166. * provided platform value
  167. */
  168. val = (read_ctr_el0() >> CTR_CWG_SHIFT) & CTR_CWG_MASK;
  169. /*
  170. * If CWG is zero, then no CWG information is available but we can
  171. * at least check the platform value is less than the architectural
  172. * maximum.
  173. */
  174. if (val != 0) {
  175. assert(SIZE_FROM_LOG2_WORDS(val) == CACHE_WRITEBACK_GRANULE);
  176. } else {
  177. assert(MAX_CACHE_LINE_SIZE >= CACHE_WRITEBACK_GRANULE);
  178. }
  179. #endif /* ENABLE_ASSERTIONS */
  180. /* Perform remaining generic architectural setup from ELmax */
  181. bl1_arch_setup();
  182. #if TRUSTED_BOARD_BOOT
  183. /* Initialize authentication module */
  184. auth_mod_init();
  185. #endif /* TRUSTED_BOARD_BOOT */
  186. /* Perform platform setup in BL1. */
  187. bl1_platform_setup();
  188. /* Get the image id of next image to load and run. */
  189. image_id = bl1_plat_get_next_image_id();
  190. /*
  191. * We currently interpret any image id other than
  192. * BL2_IMAGE_ID as the start of firmware update.
  193. */
  194. if (image_id == BL33_IMAGE_ID) {
  195. bl1_load_bl33();
  196. } else {
  197. NOTICE("BL1-FWU: *******FWU Process Started*******\n");
  198. }
  199. bl1_prepare_next_image(image_id);
  200. console_flush();
  201. bl1_transfer_bl33();
  202. }
  203. /*******************************************************************************
  204. * Function called just before handing over to the next BL to inform the user
  205. * about the boot progress. In debug mode, also print details about the BL
  206. * image's execution context.
  207. ******************************************************************************/
  208. void bl1_print_next_bl_ep_info(const entry_point_info_t *bl_ep_info)
  209. {
  210. NOTICE("BL1: Booting BL31\n");
  211. print_entry_point_info(bl_ep_info);
  212. }
  213. #if SPIN_ON_BL1_EXIT
  214. void print_debug_loop_message(void)
  215. {
  216. NOTICE("BL1: Debug loop, spinning forever\n");
  217. NOTICE("BL1: Please connect the debugger to continue\n");
  218. }
  219. #endif