bl2_plat_setup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 <errno.h>
  8. #include <string.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <common/desc_image_load.h>
  13. #include <drivers/generic_delay_timer.h>
  14. #include <drivers/mmc.h>
  15. #include <drivers/st/bsec.h>
  16. #include <drivers/st/regulator_fixed.h>
  17. #include <drivers/st/stm32_iwdg.h>
  18. #include <drivers/st/stm32_rng.h>
  19. #include <drivers/st/stm32_uart.h>
  20. #include <drivers/st/stm32mp1_clk.h>
  21. #include <drivers/st/stm32mp1_pwr.h>
  22. #include <drivers/st/stm32mp1_ram.h>
  23. #include <drivers/st/stm32mp_pmic.h>
  24. #include <lib/fconf/fconf.h>
  25. #include <lib/fconf/fconf_dyn_cfg_getter.h>
  26. #include <lib/mmio.h>
  27. #include <lib/optee_utils.h>
  28. #include <lib/xlat_tables/xlat_tables_v2.h>
  29. #include <plat/common/platform.h>
  30. #include <platform_def.h>
  31. #include <stm32mp_common.h>
  32. #include <stm32mp1_dbgmcu.h>
  33. #if DEBUG
  34. static const char debug_msg[] = {
  35. "***************************************************\n"
  36. "** DEBUG ACCESS PORT IS OPEN! **\n"
  37. "** This boot image is only for debugging purpose **\n"
  38. "** and is unsafe for production use. **\n"
  39. "** **\n"
  40. "** If you see this message and you are not **\n"
  41. "** debugging report this immediately to your **\n"
  42. "** vendor! **\n"
  43. "***************************************************\n"
  44. };
  45. #endif
  46. static void print_reset_reason(void)
  47. {
  48. uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_MP_RSTSCLRR);
  49. if (rstsr == 0U) {
  50. WARN("Reset reason unknown\n");
  51. return;
  52. }
  53. INFO("Reset reason (0x%x):\n", rstsr);
  54. if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) == 0U) {
  55. if ((rstsr & RCC_MP_RSTSCLRR_STDBYRSTF) != 0U) {
  56. INFO("System exits from STANDBY\n");
  57. return;
  58. }
  59. if ((rstsr & RCC_MP_RSTSCLRR_CSTDBYRSTF) != 0U) {
  60. INFO("MPU exits from CSTANDBY\n");
  61. return;
  62. }
  63. }
  64. if ((rstsr & RCC_MP_RSTSCLRR_PORRSTF) != 0U) {
  65. INFO(" Power-on Reset (rst_por)\n");
  66. return;
  67. }
  68. if ((rstsr & RCC_MP_RSTSCLRR_BORRSTF) != 0U) {
  69. INFO(" Brownout Reset (rst_bor)\n");
  70. return;
  71. }
  72. #if STM32MP15
  73. if ((rstsr & RCC_MP_RSTSCLRR_MCSYSRSTF) != 0U) {
  74. if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
  75. INFO(" System reset generated by MCU (MCSYSRST)\n");
  76. } else {
  77. INFO(" Local reset generated by MCU (MCSYSRST)\n");
  78. }
  79. return;
  80. }
  81. #endif
  82. if ((rstsr & RCC_MP_RSTSCLRR_MPSYSRSTF) != 0U) {
  83. INFO(" System reset generated by MPU (MPSYSRST)\n");
  84. return;
  85. }
  86. if ((rstsr & RCC_MP_RSTSCLRR_HCSSRSTF) != 0U) {
  87. INFO(" Reset due to a clock failure on HSE\n");
  88. return;
  89. }
  90. if ((rstsr & RCC_MP_RSTSCLRR_IWDG1RSTF) != 0U) {
  91. INFO(" IWDG1 Reset (rst_iwdg1)\n");
  92. return;
  93. }
  94. if ((rstsr & RCC_MP_RSTSCLRR_IWDG2RSTF) != 0U) {
  95. INFO(" IWDG2 Reset (rst_iwdg2)\n");
  96. return;
  97. }
  98. if ((rstsr & RCC_MP_RSTSCLRR_MPUP0RSTF) != 0U) {
  99. INFO(" MPU Processor 0 Reset\n");
  100. return;
  101. }
  102. #if STM32MP15
  103. if ((rstsr & RCC_MP_RSTSCLRR_MPUP1RSTF) != 0U) {
  104. INFO(" MPU Processor 1 Reset\n");
  105. return;
  106. }
  107. #endif
  108. if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
  109. INFO(" Pad Reset from NRST\n");
  110. return;
  111. }
  112. if ((rstsr & RCC_MP_RSTSCLRR_VCORERSTF) != 0U) {
  113. INFO(" Reset due to a failure of VDD_CORE\n");
  114. return;
  115. }
  116. ERROR(" Unidentified reset reason\n");
  117. }
  118. void bl2_el3_early_platform_setup(u_register_t arg0,
  119. u_register_t arg1 __unused,
  120. u_register_t arg2 __unused,
  121. u_register_t arg3 __unused)
  122. {
  123. stm32mp_save_boot_ctx_address(arg0);
  124. }
  125. void bl2_platform_setup(void)
  126. {
  127. int ret;
  128. ret = stm32mp1_ddr_probe();
  129. if (ret < 0) {
  130. ERROR("Invalid DDR init: error %d\n", ret);
  131. panic();
  132. }
  133. /* Map DDR for binary load, now with cacheable attribute */
  134. ret = mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
  135. STM32MP_DDR_MAX_SIZE, MT_MEMORY | MT_RW | MT_SECURE);
  136. if (ret < 0) {
  137. ERROR("DDR mapping: error %d\n", ret);
  138. panic();
  139. }
  140. }
  141. #if STM32MP15
  142. static void update_monotonic_counter(void)
  143. {
  144. uint32_t version;
  145. uint32_t otp;
  146. CASSERT(STM32_TF_VERSION <= MAX_MONOTONIC_VALUE,
  147. assert_stm32mp1_monotonic_counter_reach_max);
  148. /* Check if monotonic counter needs to be incremented */
  149. if (stm32_get_otp_index(MONOTONIC_OTP, &otp, NULL) != 0) {
  150. panic();
  151. }
  152. if (stm32_get_otp_value_from_idx(otp, &version) != 0) {
  153. panic();
  154. }
  155. if ((version + 1U) < BIT(STM32_TF_VERSION)) {
  156. uint32_t result;
  157. /* Need to increment the monotonic counter. */
  158. version = BIT(STM32_TF_VERSION) - 1U;
  159. result = bsec_program_otp(version, otp);
  160. if (result != BSEC_OK) {
  161. ERROR("BSEC: MONOTONIC_OTP program Error %u\n",
  162. result);
  163. panic();
  164. }
  165. INFO("Monotonic counter has been incremented (value 0x%x)\n",
  166. version);
  167. }
  168. }
  169. #endif
  170. void bl2_el3_plat_arch_setup(void)
  171. {
  172. const char *board_model;
  173. boot_api_context_t *boot_context =
  174. (boot_api_context_t *)stm32mp_get_boot_ctx_address();
  175. uintptr_t pwr_base;
  176. uintptr_t rcc_base;
  177. if (bsec_probe() != 0U) {
  178. panic();
  179. }
  180. mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
  181. BL_CODE_END - BL_CODE_BASE,
  182. MT_CODE | MT_SECURE);
  183. /* Prevent corruption of preloaded Device Tree */
  184. mmap_add_region(DTB_BASE, DTB_BASE,
  185. DTB_LIMIT - DTB_BASE,
  186. MT_RO_DATA | MT_SECURE);
  187. configure_mmu();
  188. if (dt_open_and_check(STM32MP_DTB_BASE) < 0) {
  189. panic();
  190. }
  191. pwr_base = stm32mp_pwr_base();
  192. rcc_base = stm32mp_rcc_base();
  193. /*
  194. * Disable the backup domain write protection.
  195. * The protection is enable at each reset by hardware
  196. * and must be disabled by software.
  197. */
  198. mmio_setbits_32(pwr_base + PWR_CR1, PWR_CR1_DBP);
  199. while ((mmio_read_32(pwr_base + PWR_CR1) & PWR_CR1_DBP) == 0U) {
  200. ;
  201. }
  202. /* Reset backup domain on cold boot cases */
  203. if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) {
  204. mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
  205. while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) ==
  206. 0U) {
  207. ;
  208. }
  209. mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
  210. }
  211. /*
  212. * Set minimum reset pulse duration to 31ms for discrete power
  213. * supplied boards.
  214. */
  215. if (dt_pmic_status() <= 0) {
  216. mmio_clrsetbits_32(rcc_base + RCC_RDLSICR,
  217. RCC_RDLSICR_MRD_MASK,
  218. 31U << RCC_RDLSICR_MRD_SHIFT);
  219. }
  220. generic_delay_timer_init();
  221. #if STM32MP_UART_PROGRAMMER
  222. /* Disable programmer UART before changing clock tree */
  223. if (boot_context->boot_interface_selected ==
  224. BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) {
  225. uintptr_t uart_prog_addr =
  226. get_uart_address(boot_context->boot_interface_instance);
  227. stm32_uart_stop(uart_prog_addr);
  228. }
  229. #endif
  230. if (stm32mp1_clk_probe() < 0) {
  231. panic();
  232. }
  233. if (stm32mp1_clk_init() < 0) {
  234. panic();
  235. }
  236. stm32_save_boot_info(boot_context);
  237. #if STM32MP_USB_PROGRAMMER && STM32MP15
  238. /* Deconfigure all UART RX pins configured by ROM code */
  239. stm32mp1_deconfigure_uart_pins();
  240. #endif
  241. if (stm32mp_uart_console_setup() != 0) {
  242. goto skip_console_init;
  243. }
  244. stm32mp_print_cpuinfo();
  245. board_model = dt_get_board_model();
  246. if (board_model != NULL) {
  247. NOTICE("Model: %s\n", board_model);
  248. }
  249. stm32mp_print_boardinfo();
  250. if (boot_context->auth_status != BOOT_API_CTX_AUTH_NO) {
  251. NOTICE("Bootrom authentication %s\n",
  252. (boot_context->auth_status == BOOT_API_CTX_AUTH_FAILED) ?
  253. "failed" : "succeeded");
  254. }
  255. skip_console_init:
  256. #if !TRUSTED_BOARD_BOOT
  257. if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) {
  258. /* Closed chip mandates authentication */
  259. ERROR("Secure chip: TRUSTED_BOARD_BOOT must be enabled\n");
  260. panic();
  261. }
  262. #endif
  263. if (fixed_regulator_register() != 0) {
  264. panic();
  265. }
  266. if (dt_pmic_status() > 0) {
  267. initialize_pmic();
  268. if (pmic_voltages_init() != 0) {
  269. ERROR("PMIC voltages init failed\n");
  270. panic();
  271. }
  272. print_pmic_info_and_debug();
  273. }
  274. stm32mp_syscfg_init();
  275. if (stm32_iwdg_init() < 0) {
  276. panic();
  277. }
  278. stm32_iwdg_refresh();
  279. if (bsec_read_debug_conf() != 0U) {
  280. if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) {
  281. #if DEBUG
  282. WARN("\n%s", debug_msg);
  283. #else
  284. ERROR("***Debug opened on closed chip***\n");
  285. #endif
  286. }
  287. }
  288. #if STM32MP13
  289. if (stm32_rng_init() != 0) {
  290. panic();
  291. }
  292. #endif
  293. stm32mp1_arch_security_setup();
  294. print_reset_reason();
  295. #if STM32MP15
  296. if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) {
  297. update_monotonic_counter();
  298. }
  299. #endif
  300. stm32mp_syscfg_enable_io_compensation_finish();
  301. fconf_populate("TB_FW", STM32MP_DTB_BASE);
  302. stm32mp_io_setup();
  303. }
  304. /*******************************************************************************
  305. * This function can be used by the platforms to update/use image
  306. * information for given `image_id`.
  307. ******************************************************************************/
  308. int bl2_plat_handle_post_image_load(unsigned int image_id)
  309. {
  310. int err = 0;
  311. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  312. bl_mem_params_node_t *bl32_mem_params;
  313. bl_mem_params_node_t *pager_mem_params __unused;
  314. bl_mem_params_node_t *paged_mem_params __unused;
  315. const struct dyn_cfg_dtb_info_t *config_info;
  316. bl_mem_params_node_t *tos_fw_mem_params;
  317. unsigned int i;
  318. unsigned int idx;
  319. unsigned long long ddr_top __unused;
  320. const unsigned int image_ids[] = {
  321. BL32_IMAGE_ID,
  322. BL33_IMAGE_ID,
  323. HW_CONFIG_ID,
  324. TOS_FW_CONFIG_ID,
  325. };
  326. assert(bl_mem_params != NULL);
  327. switch (image_id) {
  328. case FW_CONFIG_ID:
  329. /* Set global DTB info for fixed fw_config information */
  330. set_config_info(STM32MP_FW_CONFIG_BASE, ~0UL, STM32MP_FW_CONFIG_MAX_SIZE,
  331. FW_CONFIG_ID);
  332. fconf_populate("FW_CONFIG", STM32MP_FW_CONFIG_BASE);
  333. idx = dyn_cfg_dtb_info_get_index(TOS_FW_CONFIG_ID);
  334. /* Iterate through all the fw config IDs */
  335. for (i = 0U; i < ARRAY_SIZE(image_ids); i++) {
  336. if ((image_ids[i] == TOS_FW_CONFIG_ID) && (idx == FCONF_INVALID_IDX)) {
  337. continue;
  338. }
  339. bl_mem_params = get_bl_mem_params_node(image_ids[i]);
  340. assert(bl_mem_params != NULL);
  341. config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, image_ids[i]);
  342. if (config_info == NULL) {
  343. continue;
  344. }
  345. bl_mem_params->image_info.image_base = config_info->config_addr;
  346. bl_mem_params->image_info.image_max_size = config_info->config_max_size;
  347. bl_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
  348. switch (image_ids[i]) {
  349. case BL32_IMAGE_ID:
  350. bl_mem_params->ep_info.pc = config_info->config_addr;
  351. /* In case of OPTEE, initialize address space with tos_fw addr */
  352. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  353. assert(pager_mem_params != NULL);
  354. pager_mem_params->image_info.image_base = config_info->config_addr;
  355. pager_mem_params->image_info.image_max_size =
  356. config_info->config_max_size;
  357. /* Init base and size for pager if exist */
  358. paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
  359. if (paged_mem_params != NULL) {
  360. paged_mem_params->image_info.image_base = STM32MP_DDR_BASE +
  361. (dt_get_ddr_size() - STM32MP_DDR_S_SIZE);
  362. paged_mem_params->image_info.image_max_size =
  363. STM32MP_DDR_S_SIZE;
  364. }
  365. break;
  366. case BL33_IMAGE_ID:
  367. bl_mem_params->ep_info.pc = config_info->config_addr;
  368. break;
  369. case HW_CONFIG_ID:
  370. case TOS_FW_CONFIG_ID:
  371. break;
  372. default:
  373. return -EINVAL;
  374. }
  375. }
  376. break;
  377. case BL32_IMAGE_ID:
  378. if ((bl_mem_params->image_info.image_base != 0UL) &&
  379. (optee_header_is_valid(bl_mem_params->image_info.image_base))) {
  380. image_info_t *paged_image_info = NULL;
  381. /* BL32 is OP-TEE header */
  382. bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
  383. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  384. assert(pager_mem_params != NULL);
  385. paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
  386. if (paged_mem_params != NULL) {
  387. paged_image_info = &paged_mem_params->image_info;
  388. }
  389. err = parse_optee_header(&bl_mem_params->ep_info,
  390. &pager_mem_params->image_info,
  391. paged_image_info);
  392. if (err != 0) {
  393. ERROR("OPTEE header parse error.\n");
  394. panic();
  395. }
  396. /* Set optee boot info from parsed header data */
  397. if (paged_mem_params != NULL) {
  398. bl_mem_params->ep_info.args.arg0 =
  399. paged_mem_params->image_info.image_base;
  400. } else {
  401. bl_mem_params->ep_info.args.arg0 = 0U;
  402. }
  403. bl_mem_params->ep_info.args.arg1 = 0U; /* Unused */
  404. bl_mem_params->ep_info.args.arg2 = 0U; /* No DT supported */
  405. } else {
  406. bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
  407. tos_fw_mem_params = get_bl_mem_params_node(TOS_FW_CONFIG_ID);
  408. assert(tos_fw_mem_params != NULL);
  409. bl_mem_params->image_info.image_max_size +=
  410. tos_fw_mem_params->image_info.image_max_size;
  411. bl_mem_params->ep_info.args.arg0 = 0;
  412. }
  413. break;
  414. case BL33_IMAGE_ID:
  415. bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
  416. assert(bl32_mem_params != NULL);
  417. bl32_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
  418. #if PSA_FWU_SUPPORT
  419. stm32_fwu_set_boot_idx();
  420. #endif /* PSA_FWU_SUPPORT */
  421. break;
  422. default:
  423. /* Do nothing in default case */
  424. break;
  425. }
  426. #if STM32MP_SDMMC || STM32MP_EMMC
  427. /*
  428. * Invalidate remaining data read from MMC but not flushed by load_image_flush().
  429. * We take the worst case which is 2 MMC blocks.
  430. */
  431. if ((image_id != FW_CONFIG_ID) &&
  432. ((bl_mem_params->image_info.h.attr & IMAGE_ATTRIB_SKIP_LOADING) == 0U)) {
  433. inv_dcache_range(bl_mem_params->image_info.image_base +
  434. bl_mem_params->image_info.image_size,
  435. 2U * MMC_BLOCK_SIZE);
  436. }
  437. #endif /* STM32MP_SDMMC || STM32MP_EMMC */
  438. return err;
  439. }
  440. void bl2_el3_plat_prepare_exit(void)
  441. {
  442. #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
  443. uint16_t boot_itf = stm32mp_get_boot_itf_selected();
  444. if ((boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) ||
  445. (boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB)) {
  446. /* Invalidate the downloaded buffer used with io_memmap */
  447. inv_dcache_range(DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
  448. }
  449. #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */
  450. stm32mp1_security_setup();
  451. }