blob: bd3903d3e769d5437fe3ba2b14d4fedec1e4326f [file] [log] [blame]
Yann Gautier4b0c72a2018-07-16 10:54:09 +02001/*
Yann Gautier3e334752023-02-01 15:04:30 +01002 * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
Yann Gautier4b0c72a2018-07-16 10:54:09 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautier4b0c72a2018-07-16 10:54:09 +02007#include <assert.h>
Yann Gautier658775c2021-07-06 10:00:44 +02008#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <string.h>
10
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <arch_helpers.h>
12#include <common/bl_common.h>
13#include <common/debug.h>
14#include <common/desc_image_load.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <drivers/generic_delay_timer.h>
Yann Gautiera3bd8d12021-06-18 11:33:26 +020016#include <drivers/mmc.h>
Yann Gautier3edc7c32019-05-20 19:17:08 +020017#include <drivers/st/bsec.h>
Pascal Pailletfc7b8052021-01-29 14:48:49 +010018#include <drivers/st/regulator_fixed.h>
Yann Gautier091eab52019-06-04 18:06:34 +020019#include <drivers/st/stm32_iwdg.h>
Nicolas Le Bayon5c66fab2020-12-02 16:23:49 +010020#include <drivers/st/stm32_rng.h>
Yann Gautier3d8497c2021-10-18 16:06:22 +020021#include <drivers/st/stm32_uart.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000022#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <drivers/st/stm32mp1_pwr.h>
24#include <drivers/st/stm32mp1_ram.h>
Yann Gautier0c810882021-12-17 09:53:04 +010025#include <drivers/st/stm32mp_pmic.h>
Yann Gautier658775c2021-07-06 10:00:44 +020026#include <lib/fconf/fconf.h>
27#include <lib/fconf/fconf_dyn_cfg_getter.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000028#include <lib/mmio.h>
Yann Gautierb3386f72019-04-19 09:41:01 +020029#include <lib/optee_utils.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000030#include <lib/xlat_tables/xlat_tables_v2.h>
31#include <plat/common/platform.h>
32
Yann Gautier0c810882021-12-17 09:53:04 +010033#include <platform_def.h>
Sughosh Ganu03e2f802021-12-01 15:56:27 +053034#include <stm32mp_common.h>
Yann Gautier091eab52019-06-04 18:06:34 +020035#include <stm32mp1_dbgmcu.h>
Yann Gautier4b0c72a2018-07-16 10:54:09 +020036
Lionel Debieve7192a002020-01-28 09:02:41 +010037#if DEBUG
38static const char debug_msg[] = {
39 "***************************************************\n"
40 "** DEBUG ACCESS PORT IS OPEN! **\n"
41 "** This boot image is only for debugging purpose **\n"
42 "** and is unsafe for production use. **\n"
43 "** **\n"
44 "** If you see this message and you are not **\n"
45 "** debugging report this immediately to your **\n"
46 "** vendor! **\n"
47 "***************************************************\n"
48};
49#endif
50
Yann Gautierf9d40d52019-01-17 14:41:46 +010051static void print_reset_reason(void)
52{
Yann Gautier3d78a2e2019-02-14 11:01:20 +010053 uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_MP_RSTSCLRR);
Yann Gautierf9d40d52019-01-17 14:41:46 +010054
55 if (rstsr == 0U) {
56 WARN("Reset reason unknown\n");
57 return;
58 }
59
60 INFO("Reset reason (0x%x):\n", rstsr);
61
62 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) == 0U) {
63 if ((rstsr & RCC_MP_RSTSCLRR_STDBYRSTF) != 0U) {
64 INFO("System exits from STANDBY\n");
65 return;
66 }
67
68 if ((rstsr & RCC_MP_RSTSCLRR_CSTDBYRSTF) != 0U) {
69 INFO("MPU exits from CSTANDBY\n");
70 return;
71 }
72 }
73
74 if ((rstsr & RCC_MP_RSTSCLRR_PORRSTF) != 0U) {
75 INFO(" Power-on Reset (rst_por)\n");
76 return;
77 }
78
79 if ((rstsr & RCC_MP_RSTSCLRR_BORRSTF) != 0U) {
80 INFO(" Brownout Reset (rst_bor)\n");
81 return;
82 }
83
Yann Gautiercc5f89a2020-02-12 09:36:23 +010084#if STM32MP15
Yann Gautierf9d40d52019-01-17 14:41:46 +010085 if ((rstsr & RCC_MP_RSTSCLRR_MCSYSRSTF) != 0U) {
86 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
87 INFO(" System reset generated by MCU (MCSYSRST)\n");
88 } else {
89 INFO(" Local reset generated by MCU (MCSYSRST)\n");
90 }
91 return;
92 }
Yann Gautiercc5f89a2020-02-12 09:36:23 +010093#endif
Yann Gautierf9d40d52019-01-17 14:41:46 +010094
95 if ((rstsr & RCC_MP_RSTSCLRR_MPSYSRSTF) != 0U) {
96 INFO(" System reset generated by MPU (MPSYSRST)\n");
97 return;
98 }
99
100 if ((rstsr & RCC_MP_RSTSCLRR_HCSSRSTF) != 0U) {
101 INFO(" Reset due to a clock failure on HSE\n");
102 return;
103 }
104
105 if ((rstsr & RCC_MP_RSTSCLRR_IWDG1RSTF) != 0U) {
106 INFO(" IWDG1 Reset (rst_iwdg1)\n");
107 return;
108 }
109
110 if ((rstsr & RCC_MP_RSTSCLRR_IWDG2RSTF) != 0U) {
111 INFO(" IWDG2 Reset (rst_iwdg2)\n");
112 return;
113 }
114
115 if ((rstsr & RCC_MP_RSTSCLRR_MPUP0RSTF) != 0U) {
116 INFO(" MPU Processor 0 Reset\n");
117 return;
118 }
119
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100120#if STM32MP15
Yann Gautierf9d40d52019-01-17 14:41:46 +0100121 if ((rstsr & RCC_MP_RSTSCLRR_MPUP1RSTF) != 0U) {
122 INFO(" MPU Processor 1 Reset\n");
123 return;
124 }
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100125#endif
Yann Gautierf9d40d52019-01-17 14:41:46 +0100126
127 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
128 INFO(" Pad Reset from NRST\n");
129 return;
130 }
131
132 if ((rstsr & RCC_MP_RSTSCLRR_VCORERSTF) != 0U) {
133 INFO(" Reset due to a failure of VDD_CORE\n");
134 return;
135 }
136
137 ERROR(" Unidentified reset reason\n");
138}
139
140void bl2_el3_early_platform_setup(u_register_t arg0,
141 u_register_t arg1 __unused,
142 u_register_t arg2 __unused,
143 u_register_t arg3 __unused)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200144{
Yann Gautiera2e2a302019-02-14 11:13:39 +0100145 stm32mp_save_boot_ctx_address(arg0);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200146}
147
148void bl2_platform_setup(void)
149{
Yann Gautiercaf575b2018-07-24 17:18:19 +0200150 int ret;
151
Yann Gautiercaf575b2018-07-24 17:18:19 +0200152 ret = stm32mp1_ddr_probe();
153 if (ret < 0) {
154 ERROR("Invalid DDR init: error %d\n", ret);
155 panic();
156 }
157
Yann Gautierf3bd87e2020-09-04 15:55:53 +0200158 /* Map DDR for binary load, now with cacheable attribute */
Yann Gautiera55169b2020-01-10 18:18:59 +0100159 ret = mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
Yann Gautierf3bd87e2020-09-04 15:55:53 +0200160 STM32MP_DDR_MAX_SIZE, MT_MEMORY | MT_RW | MT_SECURE);
161 if (ret < 0) {
162 ERROR("DDR mapping: error %d\n", ret);
163 panic();
164 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200165}
166
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100167#if STM32MP15
Yann Gautier5c1dab32019-04-17 15:12:58 +0200168static void update_monotonic_counter(void)
169{
170 uint32_t version;
171 uint32_t otp;
172
173 CASSERT(STM32_TF_VERSION <= MAX_MONOTONIC_VALUE,
174 assert_stm32mp1_monotonic_counter_reach_max);
175
176 /* Check if monotonic counter needs to be incremented */
177 if (stm32_get_otp_index(MONOTONIC_OTP, &otp, NULL) != 0) {
178 panic();
179 }
180
181 if (stm32_get_otp_value_from_idx(otp, &version) != 0) {
182 panic();
183 }
184
185 if ((version + 1U) < BIT(STM32_TF_VERSION)) {
186 uint32_t result;
187
188 /* Need to increment the monotonic counter. */
189 version = BIT(STM32_TF_VERSION) - 1U;
190
191 result = bsec_program_otp(version, otp);
192 if (result != BSEC_OK) {
193 ERROR("BSEC: MONOTONIC_OTP program Error %u\n",
194 result);
195 panic();
196 }
197 INFO("Monotonic counter has been incremented (value 0x%x)\n",
198 version);
199 }
200}
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100201#endif
Yann Gautier5c1dab32019-04-17 15:12:58 +0200202
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200203void bl2_el3_plat_arch_setup(void)
204{
Yann Gautier69035a82018-07-05 16:48:16 +0200205 const char *board_model;
Yann Gautier41934662018-07-20 11:36:05 +0200206 boot_api_context_t *boot_context =
Yann Gautiera2e2a302019-02-14 11:13:39 +0100207 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100208 uintptr_t pwr_base;
209 uintptr_t rcc_base;
Yann Gautier41934662018-07-20 11:36:05 +0200210
Nicolas Le Bayon97287cd2019-05-20 18:35:02 +0200211 if (bsec_probe() != 0U) {
212 panic();
213 }
214
Yann Gautierf9d40d52019-01-17 14:41:46 +0100215 mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
216 BL_CODE_END - BL_CODE_BASE,
217 MT_CODE | MT_SECURE);
218
Yann Gautierf9d40d52019-01-17 14:41:46 +0100219 /* Prevent corruption of preloaded Device Tree */
220 mmap_add_region(DTB_BASE, DTB_BASE,
221 DTB_LIMIT - DTB_BASE,
Yann Gautier3d33df62019-12-17 17:11:10 +0100222 MT_RO_DATA | MT_SECURE);
Yann Gautierf9d40d52019-01-17 14:41:46 +0100223
224 configure_mmu();
225
Yann Gautier05773eb2020-08-24 11:51:50 +0200226 if (dt_open_and_check(STM32MP_DTB_BASE) < 0) {
Yann Gautierf9d40d52019-01-17 14:41:46 +0100227 panic();
228 }
229
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100230 pwr_base = stm32mp_pwr_base();
231 rcc_base = stm32mp_rcc_base();
232
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200233 /*
234 * Disable the backup domain write protection.
235 * The protection is enable at each reset by hardware
236 * and must be disabled by software.
237 */
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100238 mmio_setbits_32(pwr_base + PWR_CR1, PWR_CR1_DBP);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200239
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100240 while ((mmio_read_32(pwr_base + PWR_CR1) & PWR_CR1_DBP) == 0U) {
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200241 ;
242 }
243
244 /* Reset backup domain on cold boot cases */
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100245 if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) {
246 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200247
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100248 while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) ==
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200249 0U) {
250 ;
251 }
252
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100253 mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200254 }
255
Yann Gautierc0882f42021-04-27 18:19:13 +0200256 /*
257 * Set minimum reset pulse duration to 31ms for discrete power
258 * supplied boards.
259 */
260 if (dt_pmic_status() <= 0) {
261 mmio_clrsetbits_32(rcc_base + RCC_RDLSICR,
262 RCC_RDLSICR_MRD_MASK,
263 31U << RCC_RDLSICR_MRD_SHIFT);
264 }
265
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200266 generic_delay_timer_init();
267
Yann Gautier3d8497c2021-10-18 16:06:22 +0200268#if STM32MP_UART_PROGRAMMER
269 /* Disable programmer UART before changing clock tree */
270 if (boot_context->boot_interface_selected ==
271 BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) {
272 uintptr_t uart_prog_addr =
273 get_uart_address(boot_context->boot_interface_instance);
274
275 stm32_uart_stop(uart_prog_addr);
276 }
277#endif
Yann Gautier9aea69e2018-07-24 17:13:36 +0200278 if (stm32mp1_clk_probe() < 0) {
279 panic();
280 }
281
282 if (stm32mp1_clk_init() < 0) {
283 panic();
284 }
285
Yann Gautier8402c292022-06-29 17:03:36 +0200286 stm32_save_boot_info(boot_context);
Yann Gautier6eef5252021-12-10 17:04:40 +0100287
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100288#if STM32MP_USB_PROGRAMMER && STM32MP15
Yann Gautiercd16df32021-06-04 14:04:05 +0200289 /* Deconfigure all UART RX pins configured by ROM code */
290 stm32mp1_deconfigure_uart_pins();
291#endif
292
Yann Gautier66baa962021-10-18 14:01:00 +0200293 if (stm32mp_uart_console_setup() != 0) {
Yann Gautier69035a82018-07-05 16:48:16 +0200294 goto skip_console_init;
295 }
296
Yann Gautierc7374052019-06-04 18:02:37 +0200297 stm32mp_print_cpuinfo();
298
Yann Gautier69035a82018-07-05 16:48:16 +0200299 board_model = dt_get_board_model();
300 if (board_model != NULL) {
Yann Gautierf9d40d52019-01-17 14:41:46 +0100301 NOTICE("Model: %s\n", board_model);
Yann Gautier69035a82018-07-05 16:48:16 +0200302 }
303
Yann Gautier35dc0772019-05-13 18:34:48 +0200304 stm32mp_print_boardinfo();
305
Lionel Debieve7bd96f42019-09-03 12:22:23 +0200306 if (boot_context->auth_status != BOOT_API_CTX_AUTH_NO) {
307 NOTICE("Bootrom authentication %s\n",
308 (boot_context->auth_status == BOOT_API_CTX_AUTH_FAILED) ?
309 "failed" : "succeeded");
310 }
311
Yann Gautier69035a82018-07-05 16:48:16 +0200312skip_console_init:
Lionel Debieve474ad812022-10-05 16:52:09 +0200313#if !TRUSTED_BOARD_BOOT
Yann Gautier3e334752023-02-01 15:04:30 +0100314 if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) {
Lionel Debieve474ad812022-10-05 16:52:09 +0200315 /* Closed chip mandates authentication */
316 ERROR("Secure chip: TRUSTED_BOARD_BOOT must be enabled\n");
317 panic();
318 }
319#endif
320
Pascal Pailletfc7b8052021-01-29 14:48:49 +0100321 if (fixed_regulator_register() != 0) {
322 panic();
323 }
324
Yann Gautier45c1e582020-09-17 11:54:52 +0200325 if (dt_pmic_status() > 0) {
326 initialize_pmic();
Yann Gautierb2ba78e2022-01-18 10:39:52 +0100327 if (pmic_voltages_init() != 0) {
328 ERROR("PMIC voltages init failed\n");
329 panic();
330 }
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100331 print_pmic_info_and_debug();
Yann Gautier45c1e582020-09-17 11:54:52 +0200332 }
333
334 stm32mp1_syscfg_init();
335
Yann Gautier091eab52019-06-04 18:06:34 +0200336 if (stm32_iwdg_init() < 0) {
337 panic();
338 }
339
340 stm32_iwdg_refresh();
341
Lionel Debieve7192a002020-01-28 09:02:41 +0100342 if (bsec_read_debug_conf() != 0U) {
Yann Gautier3e334752023-02-01 15:04:30 +0100343 if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) {
Lionel Debieve7192a002020-01-28 09:02:41 +0100344#if DEBUG
345 WARN("\n%s", debug_msg);
346#else
347 ERROR("***Debug opened on closed chip***\n");
348#endif
349 }
350 }
351
Nicolas Le Bayon5c66fab2020-12-02 16:23:49 +0100352#if STM32MP13
353 if (stm32_rng_init() != 0) {
354 panic();
355 }
356#endif
357
Yann Gautiercaf575b2018-07-24 17:18:19 +0200358 stm32mp1_arch_security_setup();
359
Yann Gautierf9d40d52019-01-17 14:41:46 +0100360 print_reset_reason();
361
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100362#if STM32MP15
Robin van der Gracht44ad5072024-01-24 09:29:13 +0100363 if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) {
364 update_monotonic_counter();
365 }
Yann Gautiercc5f89a2020-02-12 09:36:23 +0100366#endif
Yann Gautier5c1dab32019-04-17 15:12:58 +0200367
Yann Gautierb76c61a2020-12-16 10:17:35 +0100368 stm32mp1_syscfg_enable_io_compensation_finish();
369
Yann Gautier29f1f942021-07-13 18:07:41 +0200370 fconf_populate("TB_FW", STM32MP_DTB_BASE);
Yann Gautier29f1f942021-07-13 18:07:41 +0200371
Yann Gautiera2e2a302019-02-14 11:13:39 +0100372 stm32mp_io_setup();
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200373}
Yann Gautierb3386f72019-04-19 09:41:01 +0200374
Yann Gautierb3386f72019-04-19 09:41:01 +0200375/*******************************************************************************
376 * This function can be used by the platforms to update/use image
377 * information for given `image_id`.
378 ******************************************************************************/
379int bl2_plat_handle_post_image_load(unsigned int image_id)
380{
381 int err = 0;
382 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
383 bl_mem_params_node_t *bl32_mem_params;
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200384 bl_mem_params_node_t *pager_mem_params __unused;
385 bl_mem_params_node_t *paged_mem_params __unused;
Yann Gautier658775c2021-07-06 10:00:44 +0200386 const struct dyn_cfg_dtb_info_t *config_info;
387 bl_mem_params_node_t *tos_fw_mem_params;
388 unsigned int i;
Yann Gautierfd648352021-12-13 15:24:41 +0100389 unsigned int idx;
Yann Gautier658775c2021-07-06 10:00:44 +0200390 unsigned long long ddr_top __unused;
391 const unsigned int image_ids[] = {
392 BL32_IMAGE_ID,
393 BL33_IMAGE_ID,
394 HW_CONFIG_ID,
395 TOS_FW_CONFIG_ID,
396 };
Yann Gautierb3386f72019-04-19 09:41:01 +0200397
398 assert(bl_mem_params != NULL);
399
400 switch (image_id) {
Yann Gautier658775c2021-07-06 10:00:44 +0200401 case FW_CONFIG_ID:
402 /* Set global DTB info for fixed fw_config information */
Manish V Badarkhefab76282022-03-16 13:51:26 +0000403 set_config_info(STM32MP_FW_CONFIG_BASE, ~0UL, STM32MP_FW_CONFIG_MAX_SIZE,
404 FW_CONFIG_ID);
Yann Gautier658775c2021-07-06 10:00:44 +0200405 fconf_populate("FW_CONFIG", STM32MP_FW_CONFIG_BASE);
406
Yann Gautierfd648352021-12-13 15:24:41 +0100407 idx = dyn_cfg_dtb_info_get_index(TOS_FW_CONFIG_ID);
408
Yann Gautier658775c2021-07-06 10:00:44 +0200409 /* Iterate through all the fw config IDs */
410 for (i = 0U; i < ARRAY_SIZE(image_ids); i++) {
Yann Gautierfd648352021-12-13 15:24:41 +0100411 if ((image_ids[i] == TOS_FW_CONFIG_ID) && (idx == FCONF_INVALID_IDX)) {
412 continue;
413 }
414
Yann Gautier658775c2021-07-06 10:00:44 +0200415 bl_mem_params = get_bl_mem_params_node(image_ids[i]);
416 assert(bl_mem_params != NULL);
417
418 config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, image_ids[i]);
419 if (config_info == NULL) {
420 continue;
421 }
422
423 bl_mem_params->image_info.image_base = config_info->config_addr;
424 bl_mem_params->image_info.image_max_size = config_info->config_max_size;
425
426 bl_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
427
428 switch (image_ids[i]) {
429 case BL32_IMAGE_ID:
430 bl_mem_params->ep_info.pc = config_info->config_addr;
431
432 /* In case of OPTEE, initialize address space with tos_fw addr */
433 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
Yann Gautierc6f77b02022-05-06 09:50:43 +0200434 assert(pager_mem_params != NULL);
Yann Gautier658775c2021-07-06 10:00:44 +0200435 pager_mem_params->image_info.image_base = config_info->config_addr;
436 pager_mem_params->image_info.image_max_size =
437 config_info->config_max_size;
438
439 /* Init base and size for pager if exist */
440 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
Yann Gautiere622a3d2022-06-20 11:43:17 +0200441 if (paged_mem_params != NULL) {
442 paged_mem_params->image_info.image_base = STM32MP_DDR_BASE +
443 (dt_get_ddr_size() - STM32MP_DDR_S_SIZE -
444 STM32MP_DDR_SHMEM_SIZE);
445 paged_mem_params->image_info.image_max_size =
446 STM32MP_DDR_S_SIZE;
447 }
Yann Gautier658775c2021-07-06 10:00:44 +0200448 break;
449
450 case BL33_IMAGE_ID:
451 bl_mem_params->ep_info.pc = config_info->config_addr;
452 break;
453
454 case HW_CONFIG_ID:
455 case TOS_FW_CONFIG_ID:
456 break;
457
458 default:
459 return -EINVAL;
460 }
461 }
462 break;
Yann Gautier658775c2021-07-06 10:00:44 +0200463
Yann Gautierb3386f72019-04-19 09:41:01 +0200464 case BL32_IMAGE_ID:
Yann Gautier90f84d72021-07-13 14:44:09 +0200465 if (optee_header_is_valid(bl_mem_params->image_info.image_base)) {
Yann Gautiere622a3d2022-06-20 11:43:17 +0200466 image_info_t *paged_image_info = NULL;
467
Yann Gautier90f84d72021-07-13 14:44:09 +0200468 /* BL32 is OP-TEE header */
469 bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
470 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
Yann Gautiere622a3d2022-06-20 11:43:17 +0200471 assert(pager_mem_params != NULL);
472
Yann Gautier90f84d72021-07-13 14:44:09 +0200473 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
Yann Gautiere622a3d2022-06-20 11:43:17 +0200474 if (paged_mem_params != NULL) {
475 paged_image_info = &paged_mem_params->image_info;
476 }
Yann Gautierb3386f72019-04-19 09:41:01 +0200477
Yann Gautier90f84d72021-07-13 14:44:09 +0200478 err = parse_optee_header(&bl_mem_params->ep_info,
479 &pager_mem_params->image_info,
Yann Gautiere622a3d2022-06-20 11:43:17 +0200480 paged_image_info);
481 if (err != 0) {
Yann Gautier90f84d72021-07-13 14:44:09 +0200482 ERROR("OPTEE header parse error.\n");
483 panic();
484 }
Yann Gautierb3386f72019-04-19 09:41:01 +0200485
Yann Gautier90f84d72021-07-13 14:44:09 +0200486 /* Set optee boot info from parsed header data */
Yann Gautiere622a3d2022-06-20 11:43:17 +0200487 if (paged_mem_params != NULL) {
488 bl_mem_params->ep_info.args.arg0 =
489 paged_mem_params->image_info.image_base;
490 } else {
491 bl_mem_params->ep_info.args.arg0 = 0U;
492 }
493
494 bl_mem_params->ep_info.args.arg1 = 0U; /* Unused */
495 bl_mem_params->ep_info.args.arg2 = 0U; /* No DT supported */
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200496 } else {
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200497 bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
Yann Gautier658775c2021-07-06 10:00:44 +0200498 tos_fw_mem_params = get_bl_mem_params_node(TOS_FW_CONFIG_ID);
Yann Gautierc6f77b02022-05-06 09:50:43 +0200499 assert(tos_fw_mem_params != NULL);
Yann Gautier658775c2021-07-06 10:00:44 +0200500 bl_mem_params->image_info.image_max_size +=
501 tos_fw_mem_params->image_info.image_max_size;
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200502 bl_mem_params->ep_info.args.arg0 = 0;
Yann Gautier90f84d72021-07-13 14:44:09 +0200503 }
Yann Gautierb3386f72019-04-19 09:41:01 +0200504 break;
505
506 case BL33_IMAGE_ID:
507 bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
508 assert(bl32_mem_params != NULL);
509 bl32_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
Yann Gautier5d2eb552022-11-14 14:14:48 +0100510#if PSA_FWU_SUPPORT
Sughosh Ganu03e2f802021-12-01 15:56:27 +0530511 stm32mp1_fwu_set_boot_idx();
Yann Gautier5d2eb552022-11-14 14:14:48 +0100512#endif /* PSA_FWU_SUPPORT */
Yann Gautierb3386f72019-04-19 09:41:01 +0200513 break;
514
515 default:
516 /* Do nothing in default case */
517 break;
518 }
519
Yann Gautiera3bd8d12021-06-18 11:33:26 +0200520#if STM32MP_SDMMC || STM32MP_EMMC
521 /*
522 * Invalidate remaining data read from MMC but not flushed by load_image_flush().
523 * We take the worst case which is 2 MMC blocks.
524 */
525 if ((image_id != FW_CONFIG_ID) &&
526 ((bl_mem_params->image_info.h.attr & IMAGE_ATTRIB_SKIP_LOADING) == 0U)) {
527 inv_dcache_range(bl_mem_params->image_info.image_base +
528 bl_mem_params->image_info.image_size,
529 2U * MMC_BLOCK_SIZE);
530 }
531#endif /* STM32MP_SDMMC || STM32MP_EMMC */
532
Yann Gautierb3386f72019-04-19 09:41:01 +0200533 return err;
534}
Yann Gautierd2d9b962021-08-16 11:58:01 +0200535
536void bl2_el3_plat_prepare_exit(void)
537{
Yann Gautierde775e82022-11-25 15:33:09 +0100538#if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
Patrick Delaunay9c5ee782021-07-06 14:07:56 +0200539 uint16_t boot_itf = stm32mp_get_boot_itf_selected();
540
Yann Gautierde775e82022-11-25 15:33:09 +0100541 if ((boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) ||
542 (boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB)) {
Patrick Delaunay9c5ee782021-07-06 14:07:56 +0200543 /* Invalidate the downloaded buffer used with io_memmap */
544 inv_dcache_range(DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
Patrick Delaunay9c5ee782021-07-06 14:07:56 +0200545 }
Yann Gautierde775e82022-11-25 15:33:09 +0100546#endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */
Patrick Delaunay9c5ee782021-07-06 14:07:56 +0200547
Yann Gautierd2d9b962021-08-16 11:58:01 +0200548 stm32mp1_security_setup();
549}