arm_bl2_setup.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <string.h>
  8. #include <platform_def.h>
  9. #include <arch_features.h>
  10. #include <arch_helpers.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <common/desc_image_load.h>
  14. #include <drivers/generic_delay_timer.h>
  15. #include <drivers/partition/partition.h>
  16. #include <lib/fconf/fconf.h>
  17. #include <lib/fconf/fconf_dyn_cfg_getter.h>
  18. #include <lib/gpt_rme/gpt_rme.h>
  19. #if TRANSFER_LIST
  20. #include <lib/transfer_list.h>
  21. #endif
  22. #ifdef SPD_opteed
  23. #include <lib/optee_utils.h>
  24. #endif
  25. #include <lib/utils.h>
  26. #include <plat/arm/common/plat_arm.h>
  27. #include <plat/common/platform.h>
  28. /* Data structure which holds the extents of the trusted SRAM for BL2 */
  29. static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
  30. /* Base address of fw_config received from BL1 */
  31. static uintptr_t config_base __unused;
  32. /*
  33. * Check that BL2_BASE is above ARM_FW_CONFIG_LIMIT. This reserved page is
  34. * for `meminfo_t` data structure and fw_configs passed from BL1.
  35. */
  36. #if TRANSFER_LIST
  37. CASSERT(BL2_BASE >= PLAT_ARM_EL3_FW_HANDOFF_BASE + PLAT_ARM_FW_HANDOFF_SIZE,
  38. assert_bl2_base_overflows);
  39. #elif !RESET_TO_BL2
  40. CASSERT(BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl2_base_overflows);
  41. #endif /* TRANSFER_LIST */
  42. /* Weak definitions may be overridden in specific ARM standard platform */
  43. #pragma weak bl2_early_platform_setup2
  44. #pragma weak bl2_platform_setup
  45. #pragma weak bl2_plat_arch_setup
  46. #pragma weak bl2_plat_sec_mem_layout
  47. #if ENABLE_RME
  48. #define MAP_BL2_TOTAL MAP_REGION_FLAT( \
  49. bl2_tzram_layout.total_base, \
  50. bl2_tzram_layout.total_size, \
  51. MT_MEMORY | MT_RW | MT_ROOT)
  52. #else
  53. #define MAP_BL2_TOTAL MAP_REGION_FLAT( \
  54. bl2_tzram_layout.total_base, \
  55. bl2_tzram_layout.total_size, \
  56. MT_MEMORY | MT_RW | MT_SECURE)
  57. #endif /* ENABLE_RME */
  58. #pragma weak arm_bl2_plat_handle_post_image_load
  59. struct transfer_list_header *secure_tl __unused;
  60. /*******************************************************************************
  61. * BL1 has passed the extents of the trusted SRAM that should be visible to BL2
  62. * in x0. This memory layout is sitting at the base of the free trusted SRAM.
  63. * Copy it to a safe location before its reclaimed by later BL2 functionality.
  64. ******************************************************************************/
  65. void arm_bl2_early_platform_setup(uintptr_t fw_config,
  66. struct meminfo *mem_layout)
  67. {
  68. struct transfer_list_entry *te __unused;
  69. int __maybe_unused ret;
  70. /* Initialize the console to provide early debug support */
  71. arm_console_boot_init();
  72. #if TRANSFER_LIST
  73. // TODO: modify the prototype of this function fw_config != bl2_tl
  74. secure_tl = (struct transfer_list_header *)fw_config;
  75. te = transfer_list_find(secure_tl, TL_TAG_SRAM_LAYOUT64);
  76. assert(te != NULL);
  77. bl2_tzram_layout = *(meminfo_t *)transfer_list_entry_data(te);
  78. transfer_list_rem(secure_tl, te);
  79. #else
  80. config_base = fw_config;
  81. /* Setup the BL2 memory layout */
  82. bl2_tzram_layout = *mem_layout;
  83. #endif
  84. /* Initialise the IO layer and register platform IO devices */
  85. plat_arm_io_setup();
  86. /* Load partition table */
  87. #if ARM_GPT_SUPPORT
  88. ret = gpt_partition_init();
  89. if (ret != 0) {
  90. ERROR("GPT partition initialisation failed!\n");
  91. panic();
  92. }
  93. #endif /* ARM_GPT_SUPPORT */
  94. }
  95. void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3)
  96. {
  97. arm_bl2_early_platform_setup((uintptr_t)arg0, (meminfo_t *)arg1);
  98. generic_delay_timer_init();
  99. }
  100. /*
  101. * Perform BL2 preload setup. Currently we initialise the dynamic
  102. * configuration here.
  103. */
  104. void bl2_plat_preload_setup(void)
  105. {
  106. #if TRANSFER_LIST
  107. /* Assume the secure TL hasn't been initialised if BL2 is running at EL3. */
  108. #if RESET_TO_BL2
  109. secure_tl = transfer_list_ensure((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE,
  110. PLAT_ARM_FW_HANDOFF_SIZE);
  111. if (secure_tl == NULL) {
  112. ERROR("Secure transfer list initialisation failed!\n");
  113. panic();
  114. }
  115. #endif
  116. arm_transfer_list_dyn_cfg_init(secure_tl);
  117. #else
  118. #if ARM_FW_CONFIG_LOAD_ENABLE
  119. arm_bl2_el3_plat_config_load();
  120. #endif /* ARM_FW_CONFIG_LOAD_ENABLE */
  121. arm_bl2_dyn_cfg_init();
  122. #endif
  123. #if ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT
  124. /* Always use the FIP from bank 0 */
  125. arm_set_fip_addr(0U);
  126. #endif /* ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT */
  127. }
  128. /*
  129. * Perform ARM standard platform setup.
  130. */
  131. void arm_bl2_platform_setup(void)
  132. {
  133. #if !ENABLE_RME
  134. /* Initialize the secure environment */
  135. plat_arm_security_setup();
  136. #endif
  137. #if defined(PLAT_ARM_MEM_PROT_ADDR)
  138. arm_nor_psci_do_static_mem_protect();
  139. #endif
  140. }
  141. void bl2_platform_setup(void)
  142. {
  143. arm_bl2_platform_setup();
  144. }
  145. /*******************************************************************************
  146. * Perform the very early platform specific architectural setup here.
  147. * When RME is enabled the secure environment is initialised before
  148. * initialising and enabling Granule Protection.
  149. * This function initialises the MMU in a quick and dirty way.
  150. ******************************************************************************/
  151. void arm_bl2_plat_arch_setup(void)
  152. {
  153. #if USE_COHERENT_MEM
  154. /* Ensure ARM platforms don't use coherent memory in BL2. */
  155. assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
  156. #endif
  157. const mmap_region_t bl_regions[] = {
  158. MAP_BL2_TOTAL,
  159. ARM_MAP_BL_RO,
  160. #if USE_ROMLIB
  161. ARM_MAP_ROMLIB_CODE,
  162. ARM_MAP_ROMLIB_DATA,
  163. #endif
  164. #if !TRANSFER_LIST
  165. ARM_MAP_BL_CONFIG_REGION,
  166. #endif /* TRANSFER_LIST */
  167. #if ENABLE_RME
  168. ARM_MAP_L0_GPT_REGION,
  169. #endif
  170. { 0 }
  171. };
  172. #if ENABLE_RME
  173. /* Initialise the secure environment */
  174. plat_arm_security_setup();
  175. #endif
  176. setup_page_tables(bl_regions, plat_arm_get_mmap());
  177. #ifdef __aarch64__
  178. #if ENABLE_RME
  179. /* BL2 runs in EL3 when RME enabled. */
  180. assert(is_feat_rme_present());
  181. enable_mmu_el3(0);
  182. /* Initialise and enable granule protection after MMU. */
  183. arm_gpt_setup();
  184. #else
  185. enable_mmu_el1(0);
  186. #endif
  187. #else
  188. enable_mmu_svc_mon(0);
  189. #endif
  190. arm_setup_romlib();
  191. }
  192. void bl2_plat_arch_setup(void)
  193. {
  194. const struct dyn_cfg_dtb_info_t *tb_fw_config_info __unused;
  195. struct transfer_list_entry *te __unused;
  196. arm_bl2_plat_arch_setup();
  197. #if TRANSFER_LIST
  198. #if CRYPTO_SUPPORT
  199. te = arm_transfer_list_set_heap_info(secure_tl);
  200. transfer_list_rem(secure_tl, te);
  201. #endif /* CRYPTO_SUPPORT */
  202. #else
  203. /* Fill the properties struct with the info from the config dtb */
  204. fconf_populate("FW_CONFIG", config_base);
  205. /* TB_FW_CONFIG was also loaded by BL1 */
  206. tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
  207. assert(tb_fw_config_info != NULL);
  208. fconf_populate("TB_FW", tb_fw_config_info->config_addr);
  209. #endif /* TRANSFER_LIST */
  210. }
  211. int arm_bl2_handle_post_image_load(unsigned int image_id)
  212. {
  213. int err = 0;
  214. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  215. #ifdef SPD_opteed
  216. bl_mem_params_node_t *pager_mem_params = NULL;
  217. bl_mem_params_node_t *paged_mem_params = NULL;
  218. #endif
  219. assert(bl_mem_params != NULL);
  220. switch (image_id) {
  221. #ifdef __aarch64__
  222. case BL32_IMAGE_ID:
  223. #ifdef SPD_opteed
  224. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  225. assert(pager_mem_params);
  226. paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
  227. assert(paged_mem_params);
  228. err = parse_optee_header(&bl_mem_params->ep_info,
  229. &pager_mem_params->image_info,
  230. &paged_mem_params->image_info);
  231. if (err != 0) {
  232. WARN("OPTEE header parse error.\n");
  233. }
  234. #endif
  235. bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry();
  236. break;
  237. #endif
  238. case BL33_IMAGE_ID:
  239. /* BL33 expects to receive the primary CPU MPID (through r0) */
  240. bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
  241. bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry();
  242. break;
  243. #ifdef SCP_BL2_BASE
  244. case SCP_BL2_IMAGE_ID:
  245. /* The subsequent handling of SCP_BL2 is platform specific */
  246. err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info);
  247. if (err) {
  248. WARN("Failure in platform-specific handling of SCP_BL2 image.\n");
  249. }
  250. break;
  251. #endif
  252. default:
  253. /* Do nothing in default case */
  254. break;
  255. }
  256. return err;
  257. }
  258. /*******************************************************************************
  259. * This function can be used by the platforms to update/use image
  260. * information for given `image_id`.
  261. ******************************************************************************/
  262. int arm_bl2_plat_handle_post_image_load(unsigned int image_id)
  263. {
  264. #if defined(SPD_spmd) && BL2_ENABLE_SP_LOAD
  265. /* For Secure Partitions we don't need post processing */
  266. if ((image_id >= (MAX_NUMBER_IDS - MAX_SP_IDS)) &&
  267. (image_id < MAX_NUMBER_IDS)) {
  268. return 0;
  269. }
  270. #endif
  271. #if TRANSFER_LIST
  272. if (image_id == HW_CONFIG_ID) {
  273. /* Refresh the now stale checksum following loading of HW_CONFIG into the TL. */
  274. transfer_list_update_checksum(secure_tl);
  275. }
  276. #endif /* TRANSFER_LIST */
  277. return arm_bl2_handle_post_image_load(image_id);
  278. }
  279. void arm_bl2_setup_next_ep_info(bl_mem_params_node_t *next_param_node)
  280. {
  281. entry_point_info_t *ep __unused;
  282. ep = transfer_list_set_handoff_args(secure_tl,
  283. &next_param_node->ep_info);
  284. assert(ep != NULL);
  285. arm_transfer_list_populate_ep_info(next_param_node, secure_tl);
  286. }