arm_bl1_setup.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #if TRANSFER_LIST
  60. static struct transfer_list_header *secure_tl;
  61. #endif
  62. struct meminfo *bl1_plat_sec_mem_layout(void)
  63. {
  64. return &bl1_tzram_layout;
  65. }
  66. /*******************************************************************************
  67. * BL1 specific platform actions shared between ARM standard platforms.
  68. ******************************************************************************/
  69. void arm_bl1_early_platform_setup(void)
  70. {
  71. #if !ARM_DISABLE_TRUSTED_WDOG
  72. /* Enable watchdog */
  73. plat_arm_secure_wdt_start();
  74. #endif
  75. /* Initialize the console to provide early debug support */
  76. arm_console_boot_init();
  77. /* Allow BL1 to see the whole Trusted RAM */
  78. bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
  79. bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
  80. }
  81. void bl1_early_platform_setup(void)
  82. {
  83. arm_bl1_early_platform_setup();
  84. /*
  85. * Initialize Interconnect for this cluster during cold boot.
  86. * No need for locks as no other CPU is active.
  87. */
  88. plat_arm_interconnect_init();
  89. /*
  90. * Enable Interconnect coherency for the primary CPU's cluster.
  91. */
  92. plat_arm_interconnect_enter_coherency();
  93. }
  94. /******************************************************************************
  95. * Perform the very early platform specific architecture setup shared between
  96. * ARM standard platforms. This only does basic initialization. Later
  97. * architectural setup (bl1_arch_setup()) does not do anything platform
  98. * specific.
  99. *****************************************************************************/
  100. void arm_bl1_plat_arch_setup(void)
  101. {
  102. #if USE_COHERENT_MEM
  103. /* Ensure ARM platforms don't use coherent memory in BL1. */
  104. assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
  105. #endif
  106. const mmap_region_t bl_regions[] = {
  107. MAP_BL1_TOTAL,
  108. MAP_BL1_RO,
  109. #if USE_ROMLIB
  110. ARM_MAP_ROMLIB_CODE,
  111. ARM_MAP_ROMLIB_DATA,
  112. #endif
  113. {0}
  114. };
  115. setup_page_tables(bl_regions, plat_arm_get_mmap());
  116. #ifdef __aarch64__
  117. enable_mmu_el3(0);
  118. #else
  119. enable_mmu_svc_mon(0);
  120. #endif /* __aarch64__ */
  121. arm_setup_romlib();
  122. }
  123. void bl1_plat_arch_setup(void)
  124. {
  125. arm_bl1_plat_arch_setup();
  126. }
  127. /*
  128. * Perform the platform specific architecture setup shared between
  129. * ARM standard platforms.
  130. */
  131. void arm_bl1_platform_setup(void)
  132. {
  133. const struct dyn_cfg_dtb_info_t *config_info __unused;
  134. uint32_t fw_config_max_size __unused;
  135. image_info_t config_image_info __unused;
  136. struct transfer_list_entry *te __unused;
  137. image_desc_t *desc;
  138. int err = -1;
  139. /* Initialise the IO layer and register platform IO devices */
  140. plat_arm_io_setup();
  141. /* Check if we need FWU before further processing */
  142. is_fwu_needed = plat_arm_bl1_fwu_needed();
  143. if (is_fwu_needed) {
  144. ERROR("Skip platform setup as FWU detected\n");
  145. return;
  146. }
  147. #if TRANSFER_LIST
  148. secure_tl = transfer_list_init((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE,
  149. PLAT_ARM_FW_HANDOFF_SIZE);
  150. if (secure_tl == NULL) {
  151. ERROR("Secure transfer list initialisation failed!\n");
  152. panic();
  153. }
  154. te = transfer_list_add(secure_tl, TL_TAG_TB_FW_CONFIG,
  155. ARM_TB_FW_CONFIG_MAX_SIZE, NULL);
  156. assert(te != NULL);
  157. /*
  158. * Set the load address of TB_FW_CONFIG in the data section of the TE just
  159. * allocated in the secure transfer list.
  160. */
  161. SET_PARAM_HEAD(&config_image_info, PARAM_IMAGE_BINARY, VERSION_2, 0);
  162. config_image_info.image_base = (uintptr_t)transfer_list_entry_data(te);
  163. config_image_info.image_max_size = te->data_size;
  164. VERBOSE("FCONF: Loading config with image ID: %u\n", TB_FW_CONFIG_ID);
  165. err = load_auth_image(TB_FW_CONFIG_ID, &config_image_info);
  166. if (err != 0) {
  167. VERBOSE("Failed to load config %u\n", TB_FW_CONFIG_ID);
  168. plat_error_handler(err);
  169. }
  170. transfer_list_update_checksum(secure_tl);
  171. fconf_populate("TB_FW", (uintptr_t)transfer_list_entry_data(te));
  172. #else
  173. /* Set global DTB info for fixed fw_config information */
  174. fw_config_max_size = ARM_FW_CONFIG_LIMIT - ARM_FW_CONFIG_BASE;
  175. set_config_info(ARM_FW_CONFIG_BASE, ~0UL, fw_config_max_size, FW_CONFIG_ID);
  176. /* Fill the device tree information struct with the info from the config dtb */
  177. err = fconf_load_config(FW_CONFIG_ID);
  178. if (err < 0) {
  179. ERROR("Loading of FW_CONFIG failed %d\n", err);
  180. plat_error_handler(err);
  181. }
  182. /*
  183. * FW_CONFIG loaded successfully. If FW_CONFIG device tree parsing
  184. * is successful then load TB_FW_CONFIG device tree.
  185. */
  186. config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, FW_CONFIG_ID);
  187. if (config_info != NULL) {
  188. err = fconf_populate_dtb_registry(config_info->config_addr);
  189. if (err < 0) {
  190. ERROR("Parsing of FW_CONFIG failed %d\n", err);
  191. plat_error_handler(err);
  192. }
  193. /* load TB_FW_CONFIG */
  194. err = fconf_load_config(TB_FW_CONFIG_ID);
  195. if (err < 0) {
  196. ERROR("Loading of TB_FW_CONFIG failed %d\n", err);
  197. plat_error_handler(err);
  198. }
  199. } else {
  200. ERROR("Invalid FW_CONFIG address\n");
  201. plat_error_handler(err);
  202. }
  203. #endif /* TRANSFER_LIST */
  204. desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
  205. #if TRANSFER_LIST
  206. transfer_list_set_handoff_args(secure_tl, &desc->ep_info);
  207. #else
  208. /* The BL2 ep_info arg0 is modified to point to FW_CONFIG */
  209. assert(desc != NULL);
  210. desc->ep_info.args.arg0 = config_info->config_addr;
  211. #endif /* TRANSFER_LIST */
  212. #if CRYPTO_SUPPORT
  213. /* Share the Mbed TLS heap info with other images */
  214. arm_bl1_set_mbedtls_heap();
  215. #endif /* CRYPTO_SUPPORT */
  216. /*
  217. * Allow access to the System counter timer module and program
  218. * counter frequency for non secure images during FWU
  219. */
  220. #ifdef ARM_SYS_TIMCTL_BASE
  221. arm_configure_sys_timer();
  222. #endif
  223. #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
  224. write_cntfrq_el0(plat_get_syscnt_freq2());
  225. #endif
  226. }
  227. void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
  228. {
  229. #if !ARM_DISABLE_TRUSTED_WDOG
  230. /* Disable watchdog before leaving BL1 */
  231. plat_arm_secure_wdt_stop();
  232. #endif
  233. #ifdef EL3_PAYLOAD_BASE
  234. /*
  235. * Program the EL3 payload's entry point address into the CPUs mailbox
  236. * in order to release secondary CPUs from their holding pen and make
  237. * them jump there.
  238. */
  239. plat_arm_program_trusted_mailbox(ep_info->pc);
  240. dsbsy();
  241. sev();
  242. #endif
  243. }
  244. /*
  245. * On Arm platforms, the FWU process is triggered when the FIP image has
  246. * been tampered with.
  247. */
  248. bool plat_arm_bl1_fwu_needed(void)
  249. {
  250. return !arm_io_is_toc_valid();
  251. }
  252. /*******************************************************************************
  253. * The following function checks if Firmware update is needed,
  254. * by checking if TOC in FIP image is valid or not.
  255. ******************************************************************************/
  256. unsigned int bl1_plat_get_next_image_id(void)
  257. {
  258. return is_fwu_needed ? NS_BL1U_IMAGE_ID : BL2_IMAGE_ID;
  259. }
  260. // Use the default implementation of this function when Firmware Handoff is
  261. // disabled to avoid duplicating its logic.
  262. #if TRANSFER_LIST
  263. int bl1_plat_handle_post_image_load(unsigned int image_id)
  264. {
  265. image_desc_t *image_desc __unused;
  266. assert(image_id == BL2_IMAGE_ID);
  267. struct transfer_list_entry *te;
  268. /* Convey this information to BL2 via its TL. */
  269. te = transfer_list_add(secure_tl, TL_TAG_SRAM_LAYOUT64,
  270. sizeof(meminfo_t), NULL);
  271. assert(te != NULL);
  272. bl1_plat_calc_bl2_layout(&bl1_tzram_layout,
  273. (meminfo_t *)transfer_list_entry_data(te));
  274. transfer_list_update_checksum(secure_tl);
  275. /**
  276. * Before exiting make sure the contents of the TL are flushed in case there's no
  277. * support for hardware cache coherency.
  278. */
  279. flush_dcache_range((uintptr_t)secure_tl, secure_tl->size);
  280. return 0;
  281. }
  282. #endif /* TRANSFER_LIST*/