arm_bl2_setup.c 9.1 KB

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