arm_bl1_setup.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (c) 2015-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 <bl1/bl1.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <lib/fconf/fconf.h>
  13. #include <lib/fconf/fconf_dyn_cfg_getter.h>
  14. #if TRANSFER_LIST
  15. #include <lib/transfer_list.h>
  16. #endif
  17. #include <lib/utils.h>
  18. #include <lib/xlat_tables/xlat_tables_compat.h>
  19. #include <plat/arm/common/plat_arm.h>
  20. #include <plat/common/platform.h>
  21. /* Weak definitions may be overridden in specific ARM standard platform */
  22. #pragma weak bl1_early_platform_setup
  23. #pragma weak bl1_plat_arch_setup
  24. #pragma weak bl1_plat_sec_mem_layout
  25. #pragma weak arm_bl1_early_platform_setup
  26. #pragma weak bl1_plat_prepare_exit
  27. #pragma weak bl1_plat_get_next_image_id
  28. #pragma weak plat_arm_bl1_fwu_needed
  29. #pragma weak arm_bl1_plat_arch_setup
  30. #pragma weak arm_bl1_platform_setup
  31. #define MAP_BL1_TOTAL MAP_REGION_FLAT( \
  32. bl1_tzram_layout.total_base, \
  33. bl1_tzram_layout.total_size, \
  34. MT_MEMORY | MT_RW | EL3_PAS)
  35. /*
  36. * If SEPARATE_CODE_AND_RODATA=1 we define a region for each section
  37. * otherwise one region is defined containing both
  38. */
  39. #if SEPARATE_CODE_AND_RODATA
  40. #define MAP_BL1_RO MAP_REGION_FLAT( \
  41. BL_CODE_BASE, \
  42. BL1_CODE_END - BL_CODE_BASE, \
  43. MT_CODE | EL3_PAS), \
  44. MAP_REGION_FLAT( \
  45. BL1_RO_DATA_BASE, \
  46. BL1_RO_DATA_END \
  47. - BL_RO_DATA_BASE, \
  48. MT_RO_DATA | EL3_PAS)
  49. #else
  50. #define MAP_BL1_RO MAP_REGION_FLAT( \
  51. BL_CODE_BASE, \
  52. BL1_CODE_END - BL_CODE_BASE, \
  53. MT_CODE | EL3_PAS)
  54. #endif
  55. /* Data structure which holds the extents of the trusted SRAM for BL1*/
  56. static meminfo_t bl1_tzram_layout;
  57. /* Boolean variable to hold condition whether firmware update needed or not */
  58. static bool is_fwu_needed;
  59. struct transfer_list_header *secure_tl;
  60. struct meminfo *bl1_plat_sec_mem_layout(void)
  61. {
  62. return &bl1_tzram_layout;
  63. }
  64. /*******************************************************************************
  65. * BL1 specific platform actions shared between ARM standard platforms.
  66. ******************************************************************************/
  67. void arm_bl1_early_platform_setup(void)
  68. {
  69. #if !ARM_DISABLE_TRUSTED_WDOG
  70. /* Enable watchdog */
  71. plat_arm_secure_wdt_start();
  72. #endif
  73. /* Initialize the console to provide early debug support */
  74. arm_console_boot_init();
  75. /* Allow BL1 to see the whole Trusted RAM */
  76. bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
  77. bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
  78. #if TRANSFER_LIST
  79. secure_tl = transfer_list_ensure((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE,
  80. PLAT_ARM_FW_HANDOFF_SIZE);
  81. assert(secure_tl != NULL);
  82. #endif
  83. }
  84. void bl1_early_platform_setup(void)
  85. {
  86. arm_bl1_early_platform_setup();
  87. /*
  88. * Initialize Interconnect for this cluster during cold boot.
  89. * No need for locks as no other CPU is active.
  90. */
  91. plat_arm_interconnect_init();
  92. /*
  93. * Enable Interconnect coherency for the primary CPU's cluster.
  94. */
  95. plat_arm_interconnect_enter_coherency();
  96. }
  97. /******************************************************************************
  98. * Perform the very early platform specific architecture setup shared between
  99. * ARM standard platforms. This only does basic initialization. Later
  100. * architectural setup (bl1_arch_setup()) does not do anything platform
  101. * specific.
  102. *****************************************************************************/
  103. void arm_bl1_plat_arch_setup(void)
  104. {
  105. #if USE_COHERENT_MEM
  106. /* Ensure ARM platforms don't use coherent memory in BL1. */
  107. assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
  108. #endif
  109. const mmap_region_t bl_regions[] = {
  110. MAP_BL1_TOTAL,
  111. MAP_BL1_RO,
  112. #if USE_ROMLIB
  113. ARM_MAP_ROMLIB_CODE,
  114. ARM_MAP_ROMLIB_DATA,
  115. #endif
  116. {0}
  117. };
  118. setup_page_tables(bl_regions, plat_arm_get_mmap());
  119. #ifdef __aarch64__
  120. enable_mmu_el3(0);
  121. #else
  122. enable_mmu_svc_mon(0);
  123. #endif /* __aarch64__ */
  124. arm_setup_romlib();
  125. }
  126. void bl1_plat_arch_setup(void)
  127. {
  128. arm_bl1_plat_arch_setup();
  129. }
  130. /*
  131. * Perform the platform specific architecture setup shared between
  132. * ARM standard platforms.
  133. */
  134. void arm_bl1_platform_setup(void)
  135. {
  136. const struct dyn_cfg_dtb_info_t *config_info __unused;
  137. uint32_t fw_config_max_size __unused;
  138. image_info_t config_image_info __unused;
  139. struct transfer_list_entry *te __unused;
  140. image_desc_t *desc;
  141. int err __unused = 1;
  142. /* Initialise the IO layer and register platform IO devices */
  143. plat_arm_io_setup();
  144. /* Check if we need FWU before further processing */
  145. is_fwu_needed = plat_arm_bl1_fwu_needed();
  146. if (is_fwu_needed) {
  147. ERROR("Skip platform setup as FWU detected\n");
  148. return;
  149. }
  150. #if TRANSFER_LIST
  151. #if CRYPTO_SUPPORT
  152. te = transfer_list_add(secure_tl, TL_TAG_MBEDTLS_HEAP_INFO,
  153. sizeof(struct crypto_heap_info), NULL);
  154. assert(te != NULL);
  155. struct crypto_heap_info *heap_info =
  156. (struct crypto_heap_info *)transfer_list_entry_data(te);
  157. arm_get_mbedtls_heap(&heap_info->addr, &heap_info->size);
  158. #endif /* CRYPTO_SUPPORT */
  159. desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
  160. /*
  161. * The event log might have been updated prior to this, make sure we have an
  162. * up to date tl before setting the handoff arguments.
  163. */
  164. transfer_list_update_checksum(secure_tl);
  165. transfer_list_set_handoff_args(secure_tl, &desc->ep_info);
  166. #else
  167. /* Set global DTB info for fixed fw_config information */
  168. fw_config_max_size = ARM_FW_CONFIG_LIMIT - ARM_FW_CONFIG_BASE;
  169. set_config_info(ARM_FW_CONFIG_BASE, ~0UL, fw_config_max_size, FW_CONFIG_ID);
  170. /* Fill the device tree information struct with the info from the config dtb */
  171. err = fconf_load_config(FW_CONFIG_ID);
  172. if (err < 0) {
  173. ERROR("Loading of FW_CONFIG failed %d\n", err);
  174. plat_error_handler(err);
  175. }
  176. /*
  177. * FW_CONFIG loaded successfully. If FW_CONFIG device tree parsing
  178. * is successful then load TB_FW_CONFIG device tree.
  179. */
  180. config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, FW_CONFIG_ID);
  181. if (config_info != NULL) {
  182. err = fconf_populate_dtb_registry(config_info->config_addr);
  183. if (err < 0) {
  184. ERROR("Parsing of FW_CONFIG failed %d\n", err);
  185. plat_error_handler(err);
  186. }
  187. /* load TB_FW_CONFIG */
  188. err = fconf_load_config(TB_FW_CONFIG_ID);
  189. if (err < 0) {
  190. ERROR("Loading of TB_FW_CONFIG failed %d\n", err);
  191. plat_error_handler(err);
  192. }
  193. } else {
  194. ERROR("Invalid FW_CONFIG address\n");
  195. plat_error_handler(err);
  196. }
  197. desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
  198. /* The BL2 ep_info arg0 is modified to point to FW_CONFIG */
  199. assert(desc != NULL);
  200. desc->ep_info.args.arg0 = config_info->config_addr;
  201. #if CRYPTO_SUPPORT
  202. /* Share the Mbed TLS heap info with other images */
  203. arm_bl1_set_mbedtls_heap();
  204. #endif /* CRYPTO_SUPPORT */
  205. #endif /* TRANSFER_LIST */
  206. /*
  207. * Allow access to the System counter timer module and program
  208. * counter frequency for non secure images during FWU
  209. */
  210. #ifdef ARM_SYS_TIMCTL_BASE
  211. arm_configure_sys_timer();
  212. #endif
  213. #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
  214. write_cntfrq_el0(plat_get_syscnt_freq2());
  215. #endif
  216. }
  217. void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
  218. {
  219. #if !ARM_DISABLE_TRUSTED_WDOG
  220. /* Disable watchdog before leaving BL1 */
  221. plat_arm_secure_wdt_stop();
  222. #endif
  223. #ifdef EL3_PAYLOAD_BASE
  224. /*
  225. * Program the EL3 payload's entry point address into the CPUs mailbox
  226. * in order to release secondary CPUs from their holding pen and make
  227. * them jump there.
  228. */
  229. plat_arm_program_trusted_mailbox(ep_info->pc);
  230. dsbsy();
  231. sev();
  232. #endif
  233. }
  234. /*
  235. * On Arm platforms, the FWU process is triggered when the FIP image has
  236. * been tampered with.
  237. */
  238. bool plat_arm_bl1_fwu_needed(void)
  239. {
  240. return !arm_io_is_toc_valid();
  241. }
  242. /*******************************************************************************
  243. * The following function checks if Firmware update is needed,
  244. * by checking if TOC in FIP image is valid or not.
  245. ******************************************************************************/
  246. unsigned int bl1_plat_get_next_image_id(void)
  247. {
  248. return is_fwu_needed ? NS_BL1U_IMAGE_ID : BL2_IMAGE_ID;
  249. }
  250. // Use the default implementation of this function when Firmware Handoff is
  251. // disabled to avoid duplicating its logic.
  252. #if TRANSFER_LIST
  253. int bl1_plat_handle_post_image_load(unsigned int image_id)
  254. {
  255. image_desc_t *image_desc __unused;
  256. assert(image_id == BL2_IMAGE_ID);
  257. struct transfer_list_entry *te;
  258. /* Convey this information to BL2 via its TL. */
  259. te = transfer_list_add(secure_tl, TL_TAG_SRAM_LAYOUT64,
  260. sizeof(meminfo_t), NULL);
  261. assert(te != NULL);
  262. bl1_plat_calc_bl2_layout(&bl1_tzram_layout,
  263. (meminfo_t *)transfer_list_entry_data(te));
  264. transfer_list_update_checksum(secure_tl);
  265. /**
  266. * Before exiting make sure the contents of the TL are flushed in case there's no
  267. * support for hardware cache coherency.
  268. */
  269. flush_dcache_range((uintptr_t)secure_tl, secure_tl->size);
  270. return 0;
  271. }
  272. #endif /* TRANSFER_LIST*/