xref: /rk3399_ARM-atf/plat/st/common/bl2_io_storage.c (revision 8d08a1df1e0effc42501e13c22435e546a275dae)
1c9d75b3cSYann Gautier /*
2dfbadfd9SNicolas Toromanoff  * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
3c9d75b3cSYann Gautier  *
4c9d75b3cSYann Gautier  * SPDX-License-Identifier: BSD-3-Clause
5c9d75b3cSYann Gautier  */
6c9d75b3cSYann Gautier 
7c9d75b3cSYann Gautier #include <assert.h>
8c9d75b3cSYann Gautier #include <string.h>
9c9d75b3cSYann Gautier 
10c9d75b3cSYann Gautier #include <arch_helpers.h>
11c9d75b3cSYann Gautier #include <common/debug.h>
1218b415beSYann Gautier #include <common/desc_image_load.h>
138dd75531SSughosh Ganu #include <drivers/fwu/fwu.h>
148dd75531SSughosh Ganu #include <drivers/fwu/fwu_metadata.h>
15c9d75b3cSYann Gautier #include <drivers/io/io_block.h>
16c9d75b3cSYann Gautier #include <drivers/io/io_driver.h>
17cd791164SLionel Debieve #include <drivers/io/io_encrypted.h>
181d204ee4SYann Gautier #include <drivers/io/io_fip.h>
19fa92fef0SPatrick Delaunay #include <drivers/io/io_memmap.h>
2012e21dfdSLionel Debieve #include <drivers/io/io_mtd.h>
21c9d75b3cSYann Gautier #include <drivers/io/io_storage.h>
22c9d75b3cSYann Gautier #include <drivers/mmc.h>
238dd75531SSughosh Ganu #include <drivers/partition/efi.h>
24c9d75b3cSYann Gautier #include <drivers/partition/partition.h>
2512e21dfdSLionel Debieve #include <drivers/raw_nand.h>
2657044228SLionel Debieve #include <drivers/spi_nand.h>
27b1b218fbSLionel Debieve #include <drivers/spi_nor.h>
2812e21dfdSLionel Debieve #include <drivers/st/stm32_fmc2_nand.h>
2957044228SLionel Debieve #include <drivers/st/stm32_qspi.h>
30c9d75b3cSYann Gautier #include <drivers/st/stm32_sdmmc2.h>
31fa92fef0SPatrick Delaunay #include <drivers/usb_device.h>
32d5a84eeaSYann Gautier #include <lib/fconf/fconf.h>
33c9d75b3cSYann Gautier #include <lib/mmio.h>
34c9d75b3cSYann Gautier #include <lib/utils.h>
35c9d75b3cSYann Gautier #include <plat/common/platform.h>
361d204ee4SYann Gautier #include <tools_share/firmware_image_package.h>
371d204ee4SYann Gautier 
381d204ee4SYann Gautier #include <platform_def.h>
39fa92fef0SPatrick Delaunay #include <stm32cubeprogrammer.h>
401dab28f9SLionel Debieve #include <stm32mp_efi.h>
41d5a84eeaSYann Gautier #include <stm32mp_fconf_getter.h>
42b1391b29SYann Gautier #include <stm32mp_io_storage.h>
43fa92fef0SPatrick Delaunay #include <usb_dfu.h>
44c9d75b3cSYann Gautier 
45c9d75b3cSYann Gautier /* IO devices */
461d204ee4SYann Gautier uintptr_t fip_dev_handle;
471d204ee4SYann Gautier uintptr_t storage_dev_handle;
48c9d75b3cSYann Gautier 
491d204ee4SYann Gautier static const io_dev_connector_t *fip_dev_con;
50c9d75b3cSYann Gautier 
51cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none
52cd791164SLionel Debieve static const io_dev_connector_t *enc_dev_con;
53cd791164SLionel Debieve uintptr_t enc_dev_handle;
54cd791164SLionel Debieve #endif
55cd791164SLionel Debieve 
5646554b64SNicolas Le Bayon #if STM32MP_SDMMC || STM32MP_EMMC
57cddf1bd7SYann Gautier static struct mmc_device_info mmc_info;
58c9d75b3cSYann Gautier 
59a2500ab7SYann Gautier static uint8_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
60c9d75b3cSYann Gautier 
6118b415beSYann Gautier static io_block_dev_spec_t mmc_block_dev_spec = {
62c9d75b3cSYann Gautier 	/* It's used as temp buffer in block driver */
63c9d75b3cSYann Gautier 	.buffer = {
64c9d75b3cSYann Gautier 		.offset = (size_t)&block_buffer,
65c9d75b3cSYann Gautier 		.length = MMC_BLOCK_SIZE,
66c9d75b3cSYann Gautier 	},
67c9d75b3cSYann Gautier 	.ops = {
68c9d75b3cSYann Gautier 		.read = mmc_read_blocks,
69c9d75b3cSYann Gautier 		.write = NULL,
70c9d75b3cSYann Gautier 	},
71c9d75b3cSYann Gautier 	.block_size = MMC_BLOCK_SIZE,
72c9d75b3cSYann Gautier };
73c9d75b3cSYann Gautier 
74c9d75b3cSYann Gautier static const io_dev_connector_t *mmc_dev_con;
7546554b64SNicolas Le Bayon #endif /* STM32MP_SDMMC || STM32MP_EMMC */
76c9d75b3cSYann Gautier 
77b1b218fbSLionel Debieve #if STM32MP_SPI_NOR
78b1b218fbSLionel Debieve static io_mtd_dev_spec_t spi_nor_dev_spec = {
79b1b218fbSLionel Debieve 	.ops = {
80b1b218fbSLionel Debieve 		.init = spi_nor_init,
81b1b218fbSLionel Debieve 		.read = spi_nor_read,
82b1b218fbSLionel Debieve 	},
83b1b218fbSLionel Debieve };
84b1b218fbSLionel Debieve #endif
85b1b218fbSLionel Debieve 
8612e21dfdSLionel Debieve #if STM32MP_RAW_NAND
8712e21dfdSLionel Debieve static io_mtd_dev_spec_t nand_dev_spec = {
8812e21dfdSLionel Debieve 	.ops = {
8912e21dfdSLionel Debieve 		.init = nand_raw_init,
9012e21dfdSLionel Debieve 		.read = nand_read,
911d204ee4SYann Gautier 		.seek = nand_seek_bb
9212e21dfdSLionel Debieve 	},
9312e21dfdSLionel Debieve };
9412e21dfdSLionel Debieve 
9512e21dfdSLionel Debieve static const io_dev_connector_t *nand_dev_con;
9612e21dfdSLionel Debieve #endif
9712e21dfdSLionel Debieve 
9857044228SLionel Debieve #if STM32MP_SPI_NAND
9957044228SLionel Debieve static io_mtd_dev_spec_t spi_nand_dev_spec = {
10057044228SLionel Debieve 	.ops = {
10157044228SLionel Debieve 		.init = spi_nand_init,
10257044228SLionel Debieve 		.read = nand_read,
1031d204ee4SYann Gautier 		.seek = nand_seek_bb
10457044228SLionel Debieve 	},
10557044228SLionel Debieve };
106b1b218fbSLionel Debieve #endif
10757044228SLionel Debieve 
108b1b218fbSLionel Debieve #if STM32MP_SPI_NAND || STM32MP_SPI_NOR
10957044228SLionel Debieve static const io_dev_connector_t *spi_dev_con;
11057044228SLionel Debieve #endif
11157044228SLionel Debieve 
1129083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
113fa92fef0SPatrick Delaunay static const io_dev_connector_t *memmap_dev_con;
114fa92fef0SPatrick Delaunay #endif
115fa92fef0SPatrick Delaunay 
116d5a84eeaSYann Gautier io_block_spec_t image_block_spec = {
1171d204ee4SYann Gautier 	.offset = 0U,
1181d204ee4SYann Gautier 	.length = 0U,
119c9d75b3cSYann Gautier };
120c9d75b3cSYann Gautier 
121d5a84eeaSYann Gautier int open_fip(const uintptr_t spec)
122c9d75b3cSYann Gautier {
1231d204ee4SYann Gautier 	return io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
124c9d75b3cSYann Gautier }
125c9d75b3cSYann Gautier 
126cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none
127cd791164SLionel Debieve int open_enc_fip(const uintptr_t spec)
128cd791164SLionel Debieve {
129cd791164SLionel Debieve 	int result;
130cd791164SLionel Debieve 	uintptr_t local_image_handle;
131cd791164SLionel Debieve 
132cd791164SLionel Debieve 	result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID);
133cd791164SLionel Debieve 	if (result != 0) {
134cd791164SLionel Debieve 		return result;
135cd791164SLionel Debieve 	}
136cd791164SLionel Debieve 
137cd791164SLionel Debieve 	result = io_open(enc_dev_handle, spec, &local_image_handle);
138cd791164SLionel Debieve 	if (result != 0) {
139cd791164SLionel Debieve 		return result;
140cd791164SLionel Debieve 	}
141cd791164SLionel Debieve 
142cd791164SLionel Debieve 	VERBOSE("Using encrypted FIP\n");
143cd791164SLionel Debieve 	io_close(local_image_handle);
144cd791164SLionel Debieve 
145cd791164SLionel Debieve 	return 0;
146cd791164SLionel Debieve }
147cd791164SLionel Debieve #endif
148cd791164SLionel Debieve 
149d5a84eeaSYann Gautier int open_storage(const uintptr_t spec)
150c9d75b3cSYann Gautier {
151c9d75b3cSYann Gautier 	return io_dev_init(storage_dev_handle, 0);
152c9d75b3cSYann Gautier }
153c9d75b3cSYann Gautier 
15495e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT
15595e4908eSAhmad Fatoum static uint32_t get_boot_part_fip_header(void)
15695e4908eSAhmad Fatoum {
15795e4908eSAhmad Fatoum 	io_block_spec_t emmc_boot_fip_block_spec = {
15895e4908eSAhmad Fatoum 		.offset = STM32MP_EMMC_BOOT_FIP_OFFSET,
15995e4908eSAhmad Fatoum 		.length = MMC_BLOCK_SIZE, /* We are interested only in first 4 bytes */
16095e4908eSAhmad Fatoum 	};
16195e4908eSAhmad Fatoum 	uint32_t magic = 0U;
16295e4908eSAhmad Fatoum 	int io_result;
16395e4908eSAhmad Fatoum 	size_t bytes_read;
16495e4908eSAhmad Fatoum 	uintptr_t fip_hdr_handle;
16595e4908eSAhmad Fatoum 
16695e4908eSAhmad Fatoum 	io_result = io_open(storage_dev_handle, (uintptr_t)&emmc_boot_fip_block_spec,
16795e4908eSAhmad Fatoum 			    &fip_hdr_handle);
16895e4908eSAhmad Fatoum 	assert(io_result == 0);
16995e4908eSAhmad Fatoum 
17095e4908eSAhmad Fatoum 	io_result = io_read(fip_hdr_handle, (uintptr_t)&magic, sizeof(magic),
17195e4908eSAhmad Fatoum 			    &bytes_read);
17295e4908eSAhmad Fatoum 	if ((io_result != 0) || (bytes_read != sizeof(magic))) {
17395e4908eSAhmad Fatoum 		panic();
17495e4908eSAhmad Fatoum 	}
17595e4908eSAhmad Fatoum 
17695e4908eSAhmad Fatoum 	io_close(fip_hdr_handle);
17795e4908eSAhmad Fatoum 
17895e4908eSAhmad Fatoum 	VERBOSE("%s: eMMC boot magic at offset 256K: %08x\n",
17995e4908eSAhmad Fatoum 		__func__, magic);
18095e4908eSAhmad Fatoum 
18195e4908eSAhmad Fatoum 	return magic;
18295e4908eSAhmad Fatoum }
18395e4908eSAhmad Fatoum #endif
18495e4908eSAhmad Fatoum 
185c9d75b3cSYann Gautier static void print_boot_device(boot_api_context_t *boot_context)
186c9d75b3cSYann Gautier {
187c9d75b3cSYann Gautier 	switch (boot_context->boot_interface_selected) {
188c9d75b3cSYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
189c9d75b3cSYann Gautier 		INFO("Using SDMMC\n");
190c9d75b3cSYann Gautier 		break;
191c9d75b3cSYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
192c9d75b3cSYann Gautier 		INFO("Using EMMC\n");
193c9d75b3cSYann Gautier 		break;
194b0ce4024SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
195b0ce4024SYann Gautier 		INFO("Using SPI NOR\n");
196b1b218fbSLionel Debieve 		break;
19712e21dfdSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
19812e21dfdSLionel Debieve 		INFO("Using FMC NAND\n");
19912e21dfdSLionel Debieve 		break;
200b0ce4024SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI:
20157044228SLionel Debieve 		INFO("Using SPI NAND\n");
20257044228SLionel Debieve 		break;
2039083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
2049083fa11SPatrick Delaunay 		INFO("Using UART\n");
2059083fa11SPatrick Delaunay 		break;
206fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
207fa92fef0SPatrick Delaunay 		INFO("Using USB\n");
208fa92fef0SPatrick Delaunay 		break;
209c9d75b3cSYann Gautier 	default:
2101d204ee4SYann Gautier 		ERROR("Boot interface %u not found\n",
2111d204ee4SYann Gautier 		      boot_context->boot_interface_selected);
212c9d75b3cSYann Gautier 		panic();
213c9d75b3cSYann Gautier 		break;
214c9d75b3cSYann Gautier 	}
215c9d75b3cSYann Gautier 
216c9d75b3cSYann Gautier 	if (boot_context->boot_interface_instance != 0U) {
217c9d75b3cSYann Gautier 		INFO("  Instance %d\n", boot_context->boot_interface_instance);
218c9d75b3cSYann Gautier 	}
219c9d75b3cSYann Gautier }
220c9d75b3cSYann Gautier 
22146554b64SNicolas Le Bayon #if STM32MP_SDMMC || STM32MP_EMMC
2220b1aa772SYann Gautier static void boot_mmc(enum mmc_device_type mmc_dev_type,
2230b1aa772SYann Gautier 		     uint16_t boot_interface_instance)
224c9d75b3cSYann Gautier {
225dfbadfd9SNicolas Toromanoff 	int io_result __maybe_unused;
226c9d75b3cSYann Gautier 	struct stm32_sdmmc2_params params;
227c9d75b3cSYann Gautier 
22842beea8dSYann Gautier 	zeromem(&params, sizeof(struct stm32_sdmmc2_params));
229c9d75b3cSYann Gautier 
230cddf1bd7SYann Gautier 	mmc_info.mmc_dev_type = mmc_dev_type;
231c9d75b3cSYann Gautier 
2320b1aa772SYann Gautier 	switch (boot_interface_instance) {
233c9d75b3cSYann Gautier 	case 1:
2343f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC1_BASE;
235c9d75b3cSYann Gautier 		break;
236c9d75b3cSYann Gautier 	case 2:
2373f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC2_BASE;
238c9d75b3cSYann Gautier 		break;
239c9d75b3cSYann Gautier 	case 3:
2403f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC3_BASE;
241c9d75b3cSYann Gautier 		break;
242c9d75b3cSYann Gautier 	default:
243c9d75b3cSYann Gautier 		WARN("SDMMC instance not found, using default\n");
2440b1aa772SYann Gautier 		if (mmc_dev_type == MMC_IS_SD) {
2450b1aa772SYann Gautier 			params.reg_base = STM32MP_SDMMC1_BASE;
2460b1aa772SYann Gautier 		} else {
2470b1aa772SYann Gautier 			params.reg_base = STM32MP_SDMMC2_BASE;
2480b1aa772SYann Gautier 		}
249c9d75b3cSYann Gautier 		break;
250c9d75b3cSYann Gautier 	}
251c9d75b3cSYann Gautier 
25253d5b8ffSYann Gautier 	if (mmc_dev_type != MMC_IS_EMMC) {
25353d5b8ffSYann Gautier 		params.flags = MMC_FLAG_SD_CMD6;
25453d5b8ffSYann Gautier 	}
25553d5b8ffSYann Gautier 
256cddf1bd7SYann Gautier 	params.device_info = &mmc_info;
257c9d75b3cSYann Gautier 	if (stm32_sdmmc2_mmc_init(&params) != 0) {
2580b1aa772SYann Gautier 		ERROR("SDMMC%u init failed\n", boot_interface_instance);
259c9d75b3cSYann Gautier 		panic();
260c9d75b3cSYann Gautier 	}
261c9d75b3cSYann Gautier 
26295e4908eSAhmad Fatoum 	/* Open MMC as a block device to read FIP */
263c9d75b3cSYann Gautier 	io_result = register_io_dev_block(&mmc_dev_con);
264c9d75b3cSYann Gautier 	if (io_result != 0) {
265c9d75b3cSYann Gautier 		panic();
266c9d75b3cSYann Gautier 	}
267c9d75b3cSYann Gautier 
2680b1aa772SYann Gautier 	io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
269c9d75b3cSYann Gautier 				&storage_dev_handle);
270c9d75b3cSYann Gautier 	assert(io_result == 0);
27195e4908eSAhmad Fatoum 
27295e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT
27395e4908eSAhmad Fatoum 	if (mmc_dev_type == MMC_IS_EMMC) {
27495e4908eSAhmad Fatoum 		io_result = mmc_part_switch_current_boot();
27595e4908eSAhmad Fatoum 		assert(io_result == 0);
27695e4908eSAhmad Fatoum 
27795e4908eSAhmad Fatoum 		if (get_boot_part_fip_header() != TOC_HEADER_NAME) {
27895e4908eSAhmad Fatoum 			WARN("%s: Can't find FIP header on eMMC boot partition. Trying GPT\n",
27995e4908eSAhmad Fatoum 			     __func__);
28095e4908eSAhmad Fatoum 			io_result = mmc_part_switch_user();
28195e4908eSAhmad Fatoum 			assert(io_result == 0);
28295e4908eSAhmad Fatoum 			return;
28395e4908eSAhmad Fatoum 		}
28495e4908eSAhmad Fatoum 
28595e4908eSAhmad Fatoum 		VERBOSE("%s: FIP header found on eMMC boot partition\n",
28695e4908eSAhmad Fatoum 			__func__);
28795e4908eSAhmad Fatoum 		image_block_spec.offset = STM32MP_EMMC_BOOT_FIP_OFFSET;
288e7cb4a86SYann Gautier 		image_block_spec.length = mmc_boot_part_size() - STM32MP_EMMC_BOOT_FIP_OFFSET;
28995e4908eSAhmad Fatoum 	}
29095e4908eSAhmad Fatoum #endif
2910b1aa772SYann Gautier }
29246554b64SNicolas Le Bayon #endif /* STM32MP_SDMMC || STM32MP_EMMC */
2930b1aa772SYann Gautier 
294b1b218fbSLionel Debieve #if STM32MP_SPI_NOR
295b1b218fbSLionel Debieve static void boot_spi_nor(boot_api_context_t *boot_context)
296b1b218fbSLionel Debieve {
297dfbadfd9SNicolas Toromanoff 	int io_result __maybe_unused;
298b1b218fbSLionel Debieve 
299b1b218fbSLionel Debieve 	io_result = stm32_qspi_init();
300b1b218fbSLionel Debieve 	assert(io_result == 0);
301b1b218fbSLionel Debieve 
302b1b218fbSLionel Debieve 	io_result = register_io_dev_mtd(&spi_dev_con);
303b1b218fbSLionel Debieve 	assert(io_result == 0);
304b1b218fbSLionel Debieve 
305b1b218fbSLionel Debieve 	/* Open connections to device */
306b1b218fbSLionel Debieve 	io_result = io_dev_open(spi_dev_con,
307b1b218fbSLionel Debieve 				(uintptr_t)&spi_nor_dev_spec,
308b1b218fbSLionel Debieve 				&storage_dev_handle);
309b1b218fbSLionel Debieve 	assert(io_result == 0);
310b1b218fbSLionel Debieve }
311b1b218fbSLionel Debieve #endif /* STM32MP_SPI_NOR */
312b1b218fbSLionel Debieve 
31312e21dfdSLionel Debieve #if STM32MP_RAW_NAND
31412e21dfdSLionel Debieve static void boot_fmc2_nand(boot_api_context_t *boot_context)
31512e21dfdSLionel Debieve {
316dfbadfd9SNicolas Toromanoff 	int io_result __maybe_unused;
31712e21dfdSLionel Debieve 
31812e21dfdSLionel Debieve 	io_result = stm32_fmc2_init();
31912e21dfdSLionel Debieve 	assert(io_result == 0);
32012e21dfdSLionel Debieve 
32112e21dfdSLionel Debieve 	/* Register the IO device on this platform */
32212e21dfdSLionel Debieve 	io_result = register_io_dev_mtd(&nand_dev_con);
32312e21dfdSLionel Debieve 	assert(io_result == 0);
32412e21dfdSLionel Debieve 
32512e21dfdSLionel Debieve 	/* Open connections to device */
32612e21dfdSLionel Debieve 	io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec,
32712e21dfdSLionel Debieve 				&storage_dev_handle);
32812e21dfdSLionel Debieve 	assert(io_result == 0);
32912e21dfdSLionel Debieve }
33012e21dfdSLionel Debieve #endif /* STM32MP_RAW_NAND */
33112e21dfdSLionel Debieve 
33257044228SLionel Debieve #if STM32MP_SPI_NAND
33357044228SLionel Debieve static void boot_spi_nand(boot_api_context_t *boot_context)
33457044228SLionel Debieve {
335dfbadfd9SNicolas Toromanoff 	int io_result __maybe_unused;
33657044228SLionel Debieve 
33757044228SLionel Debieve 	io_result = stm32_qspi_init();
33857044228SLionel Debieve 	assert(io_result == 0);
33957044228SLionel Debieve 
34057044228SLionel Debieve 	io_result = register_io_dev_mtd(&spi_dev_con);
34157044228SLionel Debieve 	assert(io_result == 0);
34257044228SLionel Debieve 
34357044228SLionel Debieve 	/* Open connections to device */
34457044228SLionel Debieve 	io_result = io_dev_open(spi_dev_con,
34557044228SLionel Debieve 				(uintptr_t)&spi_nand_dev_spec,
34657044228SLionel Debieve 				&storage_dev_handle);
34757044228SLionel Debieve 	assert(io_result == 0);
34857044228SLionel Debieve }
34957044228SLionel Debieve #endif /* STM32MP_SPI_NAND */
35057044228SLionel Debieve 
3519083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
352fa92fef0SPatrick Delaunay static void mmap_io_setup(void)
353fa92fef0SPatrick Delaunay {
354dfbadfd9SNicolas Toromanoff 	int io_result __maybe_unused;
355fa92fef0SPatrick Delaunay 
356fa92fef0SPatrick Delaunay 	io_result = register_io_dev_memmap(&memmap_dev_con);
357fa92fef0SPatrick Delaunay 	assert(io_result == 0);
358fa92fef0SPatrick Delaunay 
359fa92fef0SPatrick Delaunay 	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
360fa92fef0SPatrick Delaunay 				&storage_dev_handle);
361fa92fef0SPatrick Delaunay 	assert(io_result == 0);
362fa92fef0SPatrick Delaunay }
363fa92fef0SPatrick Delaunay 
3649083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
3659083fa11SPatrick Delaunay static void stm32cubeprogrammer_uart(void)
3669083fa11SPatrick Delaunay {
367dfbadfd9SNicolas Toromanoff 	int ret __maybe_unused;
3689083fa11SPatrick Delaunay 	boot_api_context_t *boot_context =
3699083fa11SPatrick Delaunay 		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
3709083fa11SPatrick Delaunay 	uintptr_t uart_base;
3719083fa11SPatrick Delaunay 
3729083fa11SPatrick Delaunay 	uart_base = get_uart_address(boot_context->boot_interface_instance);
3739083fa11SPatrick Delaunay 	ret = stm32cubeprog_uart_load(uart_base, DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
3749083fa11SPatrick Delaunay 	assert(ret == 0);
3759083fa11SPatrick Delaunay }
3769083fa11SPatrick Delaunay #endif
3779083fa11SPatrick Delaunay 
3789083fa11SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
379fa92fef0SPatrick Delaunay static void stm32cubeprogrammer_usb(void)
380fa92fef0SPatrick Delaunay {
381dfbadfd9SNicolas Toromanoff 	int ret __maybe_unused;
382fa92fef0SPatrick Delaunay 	struct usb_handle *pdev;
383fa92fef0SPatrick Delaunay 
384fa92fef0SPatrick Delaunay 	/* Init USB on platform */
385fa92fef0SPatrick Delaunay 	pdev = usb_dfu_plat_init();
386fa92fef0SPatrick Delaunay 
387fa92fef0SPatrick Delaunay 	ret = stm32cubeprog_usb_load(pdev, DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
388fa92fef0SPatrick Delaunay 	assert(ret == 0);
389fa92fef0SPatrick Delaunay }
390fa92fef0SPatrick Delaunay #endif
3919083fa11SPatrick Delaunay #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */
3929083fa11SPatrick Delaunay 
3930b1aa772SYann Gautier void stm32mp_io_setup(void)
3940b1aa772SYann Gautier {
395dfbadfd9SNicolas Toromanoff 	int io_result __maybe_unused;
3960b1aa772SYann Gautier 	boot_api_context_t *boot_context =
3970b1aa772SYann Gautier 		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
3980b1aa772SYann Gautier 
3990b1aa772SYann Gautier 	print_boot_device(boot_context);
4000b1aa772SYann Gautier 
4010b1aa772SYann Gautier 	if ((boot_context->boot_partition_used_toboot == 1U) ||
4020b1aa772SYann Gautier 	    (boot_context->boot_partition_used_toboot == 2U)) {
4031d204ee4SYann Gautier 		INFO("Boot used partition fsbl%u\n",
4040b1aa772SYann Gautier 		     boot_context->boot_partition_used_toboot);
4050b1aa772SYann Gautier 	}
4060b1aa772SYann Gautier 
4071d204ee4SYann Gautier 	io_result = register_io_dev_fip(&fip_dev_con);
4080b1aa772SYann Gautier 	assert(io_result == 0);
4090b1aa772SYann Gautier 
4101d204ee4SYann Gautier 	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
4111d204ee4SYann Gautier 				&fip_dev_handle);
4120b1aa772SYann Gautier 
413cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none
414cd791164SLionel Debieve 	io_result = register_io_dev_enc(&enc_dev_con);
415cd791164SLionel Debieve 	assert(io_result == 0);
416cd791164SLionel Debieve 
417cd791164SLionel Debieve 	io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL,
418cd791164SLionel Debieve 				&enc_dev_handle);
419cd791164SLionel Debieve 	assert(io_result == 0);
420cd791164SLionel Debieve #endif
421cd791164SLionel Debieve 
4220b1aa772SYann Gautier 	switch (boot_context->boot_interface_selected) {
42346554b64SNicolas Le Bayon #if STM32MP_SDMMC
4240b1aa772SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
4250b1aa772SYann Gautier 		dmbsy();
4260b1aa772SYann Gautier 		boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
4270b1aa772SYann Gautier 		break;
42846554b64SNicolas Le Bayon #endif
42946554b64SNicolas Le Bayon #if STM32MP_EMMC
4300b1aa772SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
4310b1aa772SYann Gautier 		dmbsy();
4320b1aa772SYann Gautier 		boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
433c9d75b3cSYann Gautier 		break;
43446554b64SNicolas Le Bayon #endif
435b1b218fbSLionel Debieve #if STM32MP_SPI_NOR
436b0ce4024SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
437b1b218fbSLionel Debieve 		dmbsy();
438b1b218fbSLionel Debieve 		boot_spi_nor(boot_context);
439b1b218fbSLionel Debieve 		break;
440b1b218fbSLionel Debieve #endif
44112e21dfdSLionel Debieve #if STM32MP_RAW_NAND
44212e21dfdSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
44312e21dfdSLionel Debieve 		dmbsy();
44412e21dfdSLionel Debieve 		boot_fmc2_nand(boot_context);
44512e21dfdSLionel Debieve 		break;
44612e21dfdSLionel Debieve #endif
44757044228SLionel Debieve #if STM32MP_SPI_NAND
448b0ce4024SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI:
44957044228SLionel Debieve 		dmbsy();
45057044228SLionel Debieve 		boot_spi_nand(boot_context);
45157044228SLionel Debieve 		break;
45257044228SLionel Debieve #endif
4539083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
4549083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
4559083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
4569083fa11SPatrick Delaunay #endif
457fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
458fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
4599083fa11SPatrick Delaunay #endif
460fa92fef0SPatrick Delaunay 		dmbsy();
461fa92fef0SPatrick Delaunay 		mmap_io_setup();
462fa92fef0SPatrick Delaunay 		break;
463fa92fef0SPatrick Delaunay #endif
464c9d75b3cSYann Gautier 
465c9d75b3cSYann Gautier 	default:
466c9d75b3cSYann Gautier 		ERROR("Boot interface %d not supported\n",
467c9d75b3cSYann Gautier 		      boot_context->boot_interface_selected);
46871693a66SYann Gautier 		panic();
469c9d75b3cSYann Gautier 		break;
470c9d75b3cSYann Gautier 	}
471c9d75b3cSYann Gautier }
472c9d75b3cSYann Gautier 
4731d204ee4SYann Gautier int bl2_plat_handle_pre_image_load(unsigned int image_id)
4741d204ee4SYann Gautier {
475dfbadfd9SNicolas Toromanoff 	static bool gpt_init_done __maybe_unused;
4761d204ee4SYann Gautier 	uint16_t boot_itf = stm32mp_get_boot_itf_selected();
4771d204ee4SYann Gautier 
4781d204ee4SYann Gautier 	switch (boot_itf) {
4791d204ee4SYann Gautier #if STM32MP_SDMMC || STM32MP_EMMC
4801d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
48195e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT
48295e4908eSAhmad Fatoum 		if (image_block_spec.offset == STM32MP_EMMC_BOOT_FIP_OFFSET) {
48395e4908eSAhmad Fatoum 			break;
48495e4908eSAhmad Fatoum 		}
48595e4908eSAhmad Fatoum #endif
48695e4908eSAhmad Fatoum 		/* fallthrough */
48795e4908eSAhmad Fatoum 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
4881d204ee4SYann Gautier 		if (!gpt_init_done) {
4898dd75531SSughosh Ganu /*
4908dd75531SSughosh Ganu  * With FWU Multi Bank feature enabled, the selection of
4918dd75531SSughosh Ganu  * the image to boot will be done by fwu_init calling the
4928dd75531SSughosh Ganu  * platform hook, plat_fwu_set_images_source.
4938dd75531SSughosh Ganu  */
4948dd75531SSughosh Ganu #if !PSA_FWU_SUPPORT
4951d204ee4SYann Gautier 			const partition_entry_t *entry;
496*8d08a1dfSSughosh Ganu 			const struct efi_guid fip_guid = STM32MP_FIP_GUID;
4971d204ee4SYann Gautier 
4981d204ee4SYann Gautier 			partition_init(GPT_IMAGE_ID);
499*8d08a1dfSSughosh Ganu 			entry = get_partition_entry_by_type(&fip_guid);
5001dab28f9SLionel Debieve 			if (entry == NULL) {
5011d204ee4SYann Gautier 				entry = get_partition_entry(FIP_IMAGE_NAME);
5021d204ee4SYann Gautier 				if (entry == NULL) {
5031d204ee4SYann Gautier 					ERROR("Could NOT find the %s partition!\n",
5041d204ee4SYann Gautier 					      FIP_IMAGE_NAME);
5051dab28f9SLionel Debieve 
5061d204ee4SYann Gautier 					return -ENOENT;
5071d204ee4SYann Gautier 				}
5081dab28f9SLionel Debieve 			}
5091d204ee4SYann Gautier 
5101d204ee4SYann Gautier 			image_block_spec.offset = entry->start;
5111d204ee4SYann Gautier 			image_block_spec.length = entry->length;
5128dd75531SSughosh Ganu #endif
5131d204ee4SYann Gautier 			gpt_init_done = true;
51418b415beSYann Gautier 		} else {
51518b415beSYann Gautier 			bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
516dfbadfd9SNicolas Toromanoff 
5172deff904SYann Gautier 			assert(bl_mem_params != NULL);
51818b415beSYann Gautier 
51918b415beSYann Gautier 			mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base;
52018b415beSYann Gautier 			mmc_block_dev_spec.buffer.length = bl_mem_params->image_info.image_max_size;
5211d204ee4SYann Gautier 		}
5221d204ee4SYann Gautier 
5231d204ee4SYann Gautier 		break;
5241d204ee4SYann Gautier #endif
5251d204ee4SYann Gautier 
5261d204ee4SYann Gautier #if STM32MP_RAW_NAND || STM32MP_SPI_NAND
5271d204ee4SYann Gautier #if STM32MP_RAW_NAND
5281d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
5291d204ee4SYann Gautier #endif
5301d204ee4SYann Gautier #if STM32MP_SPI_NAND
531b0ce4024SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI:
5321d204ee4SYann Gautier #endif
5331d204ee4SYann Gautier 		image_block_spec.offset = STM32MP_NAND_FIP_OFFSET;
5341d204ee4SYann Gautier 		break;
5351d204ee4SYann Gautier #endif
5361d204ee4SYann Gautier 
5371d204ee4SYann Gautier #if STM32MP_SPI_NOR
538b0ce4024SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
539dfbadfd9SNicolas Toromanoff /*
540dfbadfd9SNicolas Toromanoff  * With FWU Multi Bank feature enabled, the selection of
541dfbadfd9SNicolas Toromanoff  * the image to boot will be done by fwu_init calling the
542dfbadfd9SNicolas Toromanoff  * platform hook, plat_fwu_set_images_source.
543dfbadfd9SNicolas Toromanoff  */
544dfbadfd9SNicolas Toromanoff #if !PSA_FWU_SUPPORT
5451d204ee4SYann Gautier 		image_block_spec.offset = STM32MP_NOR_FIP_OFFSET;
546dfbadfd9SNicolas Toromanoff #endif
5471d204ee4SYann Gautier 		break;
5481d204ee4SYann Gautier #endif
5491d204ee4SYann Gautier 
5509083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
5519083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
5529083fa11SPatrick Delaunay 		if (image_id == FW_CONFIG_ID) {
5539083fa11SPatrick Delaunay 			stm32cubeprogrammer_uart();
5549083fa11SPatrick Delaunay 			/* FIP loaded at DWL address */
5559083fa11SPatrick Delaunay 			image_block_spec.offset = DWL_BUFFER_BASE;
5569083fa11SPatrick Delaunay 			image_block_spec.length = DWL_BUFFER_SIZE;
5579083fa11SPatrick Delaunay 		}
5589083fa11SPatrick Delaunay 		break;
5599083fa11SPatrick Delaunay #endif
560fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
561fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
562fa92fef0SPatrick Delaunay 		if (image_id == FW_CONFIG_ID) {
563fa92fef0SPatrick Delaunay 			stm32cubeprogrammer_usb();
564fa92fef0SPatrick Delaunay 			/* FIP loaded at DWL address */
565fa92fef0SPatrick Delaunay 			image_block_spec.offset = DWL_BUFFER_BASE;
566fa92fef0SPatrick Delaunay 			image_block_spec.length = DWL_BUFFER_SIZE;
567fa92fef0SPatrick Delaunay 		}
568fa92fef0SPatrick Delaunay 		break;
569fa92fef0SPatrick Delaunay #endif
570fa92fef0SPatrick Delaunay 
5711d204ee4SYann Gautier 	default:
5721d204ee4SYann Gautier 		ERROR("FIP Not found\n");
5731d204ee4SYann Gautier 		panic();
5741d204ee4SYann Gautier 	}
5751d204ee4SYann Gautier 
5761d204ee4SYann Gautier 	return 0;
5771d204ee4SYann Gautier }
5781d204ee4SYann Gautier 
579c9d75b3cSYann Gautier /*
580c9d75b3cSYann Gautier  * Return an IO device handle and specification which can be used to access
581c9d75b3cSYann Gautier  * an image. Use this to enforce platform load policy.
582c9d75b3cSYann Gautier  */
583c9d75b3cSYann Gautier int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
584c9d75b3cSYann Gautier 			  uintptr_t *image_spec)
585c9d75b3cSYann Gautier {
586c9d75b3cSYann Gautier 	int rc;
587c9d75b3cSYann Gautier 	const struct plat_io_policy *policy;
588c9d75b3cSYann Gautier 
589d5a84eeaSYann Gautier 	policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id);
590c9d75b3cSYann Gautier 	rc = policy->check(policy->image_spec);
591c9d75b3cSYann Gautier 	if (rc == 0) {
592c9d75b3cSYann Gautier 		*image_spec = policy->image_spec;
593c9d75b3cSYann Gautier 		*dev_handle = *(policy->dev_handle);
594c9d75b3cSYann Gautier 	}
595c9d75b3cSYann Gautier 
596c9d75b3cSYann Gautier 	return rc;
597c9d75b3cSYann Gautier }
5988dd75531SSughosh Ganu 
599dfbadfd9SNicolas Toromanoff #if (STM32MP_SDMMC || STM32MP_EMMC || STM32MP_SPI_NOR) && PSA_FWU_SUPPORT
6008dd75531SSughosh Ganu /*
601f87de907SNicolas Toromanoff  * In each boot in non-trial mode, we set the BKP register to
602f87de907SNicolas Toromanoff  * FWU_MAX_TRIAL_REBOOT, and return the active_index from metadata.
603f87de907SNicolas Toromanoff  *
604f87de907SNicolas Toromanoff  * As long as the update agent didn't update the "accepted" field in metadata
605f87de907SNicolas Toromanoff  * (i.e. we are in trial mode), we select the new active_index.
606f87de907SNicolas Toromanoff  * To avoid infinite boot loop at trial boot we decrement a BKP register.
607f87de907SNicolas Toromanoff  * If this counter is 0:
608f87de907SNicolas Toromanoff  *     - an unexpected TAMPER event raised (that resets the BKP registers to 0)
609f87de907SNicolas Toromanoff  *     - a power-off occurs before the update agent was able to update the
610f87de907SNicolas Toromanoff  *       "accepted' field
611f87de907SNicolas Toromanoff  *     - we already boot FWU_MAX_TRIAL_REBOOT times in trial mode.
612f87de907SNicolas Toromanoff  * we select the previous_active_index.
6138dd75531SSughosh Ganu  */
6148dd75531SSughosh Ganu uint32_t plat_fwu_get_boot_idx(void)
6158dd75531SSughosh Ganu {
616f87de907SNicolas Toromanoff 	/*
617f87de907SNicolas Toromanoff 	 * Select boot index and update boot counter only once per boot
618f87de907SNicolas Toromanoff 	 * even if this function is called several times.
619f87de907SNicolas Toromanoff 	 */
620f87de907SNicolas Toromanoff 	static uint32_t boot_idx = INVALID_BOOT_IDX;
6218dd75531SSughosh Ganu 
622f87de907SNicolas Toromanoff 	if (boot_idx == INVALID_BOOT_IDX) {
62361660514SSughosh Ganu 		const struct fwu_metadata *data = fwu_get_metadata();
62461660514SSughosh Ganu 
625f87de907SNicolas Toromanoff 		boot_idx = data->active_index;
62661660514SSughosh Ganu 
627588b01b5SSughosh Ganu 		if (data->bank_state[boot_idx] == FWU_BANK_STATE_VALID) {
628f87de907SNicolas Toromanoff 			if (stm32_get_and_dec_fwu_trial_boot_cnt() == 0U) {
629f87de907SNicolas Toromanoff 				WARN("Trial FWU fails %u times\n",
630f87de907SNicolas Toromanoff 				     FWU_MAX_TRIAL_REBOOT);
63161660514SSughosh Ganu 				boot_idx = fwu_get_alternate_boot_bank();
632f87de907SNicolas Toromanoff 			}
63361660514SSughosh Ganu 		} else if (data->bank_state[boot_idx] ==
63461660514SSughosh Ganu 			   FWU_BANK_STATE_ACCEPTED) {
635f87de907SNicolas Toromanoff 			stm32_set_max_fwu_trial_boot_cnt();
63661660514SSughosh Ganu 		} else {
63761660514SSughosh Ganu 			ERROR("The active bank(%u) of the platform is in Invalid State.\n",
63861660514SSughosh Ganu 				boot_idx);
63961660514SSughosh Ganu 			boot_idx = fwu_get_alternate_boot_bank();
64061660514SSughosh Ganu 			stm32_clear_fwu_trial_boot_cnt();
641f87de907SNicolas Toromanoff 		}
642f87de907SNicolas Toromanoff 	}
643f87de907SNicolas Toromanoff 
644f87de907SNicolas Toromanoff 	return boot_idx;
6458dd75531SSughosh Ganu }
6468dd75531SSughosh Ganu 
647*8d08a1dfSSughosh Ganu static void *stm32_get_image_spec(const struct efi_guid *img_type_guid)
6488dd75531SSughosh Ganu {
6498dd75531SSughosh Ganu 	unsigned int i;
6508dd75531SSughosh Ganu 
6518dd75531SSughosh Ganu 	for (i = 0U; i < MAX_NUMBER_IDS; i++) {
652*8d08a1dfSSughosh Ganu 		if ((guidcmp(&policies[i].img_type_guid, img_type_guid)) == 0) {
6538dd75531SSughosh Ganu 			return (void *)policies[i].image_spec;
6548dd75531SSughosh Ganu 		}
6558dd75531SSughosh Ganu 	}
6568dd75531SSughosh Ganu 
6578dd75531SSughosh Ganu 	return NULL;
6588dd75531SSughosh Ganu }
6598dd75531SSughosh Ganu 
6608dd75531SSughosh Ganu void plat_fwu_set_images_source(const struct fwu_metadata *metadata)
6618dd75531SSughosh Ganu {
6628dd75531SSughosh Ganu 	unsigned int i;
6638dd75531SSughosh Ganu 	uint32_t boot_idx;
664dfbadfd9SNicolas Toromanoff 	const partition_entry_t *entry __maybe_unused;
665*8d08a1dfSSughosh Ganu 	const struct fwu_image_entry *img_entry;
666*8d08a1dfSSughosh Ganu 	const void *img_type_guid;
667*8d08a1dfSSughosh Ganu 	const void *img_guid;
6688dd75531SSughosh Ganu 	io_block_spec_t *image_spec;
669dfbadfd9SNicolas Toromanoff 	const uint16_t boot_itf = stm32mp_get_boot_itf_selected();
6708dd75531SSughosh Ganu 
6718dd75531SSughosh Ganu 	boot_idx = plat_fwu_get_boot_idx();
6728dd75531SSughosh Ganu 	assert(boot_idx < NR_OF_FW_BANKS);
67361660514SSughosh Ganu 	VERBOSE("Selecting to boot from bank %u\n", boot_idx);
6748dd75531SSughosh Ganu 
675*8d08a1dfSSughosh Ganu 	img_entry = (void *)&metadata->fw_desc.img_entry;
6768dd75531SSughosh Ganu 	for (i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) {
677*8d08a1dfSSughosh Ganu 		img_type_guid = &img_entry[i].img_type_guid;
678dfbadfd9SNicolas Toromanoff 
679*8d08a1dfSSughosh Ganu 		img_guid = &img_entry[i].img_bank_info[boot_idx].img_guid;
680dfbadfd9SNicolas Toromanoff 
681*8d08a1dfSSughosh Ganu 		image_spec = stm32_get_image_spec(img_type_guid);
6828dd75531SSughosh Ganu 		if (image_spec == NULL) {
6838dd75531SSughosh Ganu 			ERROR("Unable to get image spec for the image in the metadata\n");
6848dd75531SSughosh Ganu 			panic();
6858dd75531SSughosh Ganu 		}
6868dd75531SSughosh Ganu 
687dfbadfd9SNicolas Toromanoff 		switch (boot_itf) {
688dfbadfd9SNicolas Toromanoff #if (STM32MP_SDMMC || STM32MP_EMMC)
689dfbadfd9SNicolas Toromanoff 		case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
690dfbadfd9SNicolas Toromanoff 		case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
691*8d08a1dfSSughosh Ganu 			entry = get_partition_entry_by_guid(img_guid);
6928dd75531SSughosh Ganu 			if (entry == NULL) {
693dfbadfd9SNicolas Toromanoff 				ERROR("No partition with the uuid mentioned in metadata\n");
6948dd75531SSughosh Ganu 				panic();
6958dd75531SSughosh Ganu 			}
6968dd75531SSughosh Ganu 
6978dd75531SSughosh Ganu 			image_spec->offset = entry->start;
6988dd75531SSughosh Ganu 			image_spec->length = entry->length;
699dfbadfd9SNicolas Toromanoff 			break;
700dfbadfd9SNicolas Toromanoff #endif
701dfbadfd9SNicolas Toromanoff #if STM32MP_SPI_NOR
702b0ce4024SYann Gautier 		case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
703*8d08a1dfSSughosh Ganu 			if (guidcmp(img_guid, &STM32MP_NOR_FIP_A_GUID) == 0) {
704dfbadfd9SNicolas Toromanoff 				image_spec->offset = STM32MP_NOR_FIP_A_OFFSET;
705*8d08a1dfSSughosh Ganu 			} else if (guidcmp(img_guid, &STM32MP_NOR_FIP_B_GUID) == 0) {
706dfbadfd9SNicolas Toromanoff 				image_spec->offset = STM32MP_NOR_FIP_B_OFFSET;
707dfbadfd9SNicolas Toromanoff 			} else {
708dfbadfd9SNicolas Toromanoff 				ERROR("Invalid uuid mentioned in metadata\n");
709dfbadfd9SNicolas Toromanoff 				panic();
710dfbadfd9SNicolas Toromanoff 			}
711dfbadfd9SNicolas Toromanoff 			break;
712dfbadfd9SNicolas Toromanoff #endif
713dfbadfd9SNicolas Toromanoff 		default:
714dfbadfd9SNicolas Toromanoff 			panic();
715dfbadfd9SNicolas Toromanoff 			break;
716dfbadfd9SNicolas Toromanoff 		}
7178dd75531SSughosh Ganu 	}
7188dd75531SSughosh Ganu }
7190ca180f6SSughosh Ganu 
7200ca180f6SSughosh Ganu static int plat_set_image_source(unsigned int image_id,
7210ca180f6SSughosh Ganu 				 uintptr_t *handle,
722dfbadfd9SNicolas Toromanoff 				 uintptr_t *image_spec)
7230ca180f6SSughosh Ganu {
7240ca180f6SSughosh Ganu 	struct plat_io_policy *policy;
725dfbadfd9SNicolas Toromanoff 	io_block_spec_t *spec __maybe_unused;
726dfbadfd9SNicolas Toromanoff 	const partition_entry_t *entry __maybe_unused;
727dfbadfd9SNicolas Toromanoff 	const uint16_t boot_itf = stm32mp_get_boot_itf_selected();
728dfbadfd9SNicolas Toromanoff 
729dfbadfd9SNicolas Toromanoff 	policy = &policies[image_id];
730dfbadfd9SNicolas Toromanoff 	spec = (io_block_spec_t *)policy->image_spec;
731dfbadfd9SNicolas Toromanoff 
732dfbadfd9SNicolas Toromanoff 	switch (boot_itf) {
733dfbadfd9SNicolas Toromanoff #if (STM32MP_SDMMC || STM32MP_EMMC)
734dfbadfd9SNicolas Toromanoff 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
735dfbadfd9SNicolas Toromanoff 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
736dfbadfd9SNicolas Toromanoff 		partition_init(GPT_IMAGE_ID);
737dfbadfd9SNicolas Toromanoff 
738dfbadfd9SNicolas Toromanoff 		if (image_id == FWU_METADATA_IMAGE_ID) {
739dfbadfd9SNicolas Toromanoff 			entry = get_partition_entry(METADATA_PART_1);
740dfbadfd9SNicolas Toromanoff 		} else {
741dfbadfd9SNicolas Toromanoff 			entry = get_partition_entry(METADATA_PART_2);
742dfbadfd9SNicolas Toromanoff 		}
7430ca180f6SSughosh Ganu 
7440ca180f6SSughosh Ganu 		if (entry == NULL) {
745dfbadfd9SNicolas Toromanoff 			ERROR("Unable to find a metadata partition\n");
7460ca180f6SSughosh Ganu 			return -ENOENT;
7470ca180f6SSughosh Ganu 		}
7480ca180f6SSughosh Ganu 
7490ca180f6SSughosh Ganu 		spec->offset = entry->start;
7500ca180f6SSughosh Ganu 		spec->length = entry->length;
751dfbadfd9SNicolas Toromanoff 		break;
752dfbadfd9SNicolas Toromanoff #endif
753dfbadfd9SNicolas Toromanoff 
754dfbadfd9SNicolas Toromanoff #if STM32MP_SPI_NOR
755b0ce4024SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
756dfbadfd9SNicolas Toromanoff 		if (image_id == FWU_METADATA_IMAGE_ID) {
757dfbadfd9SNicolas Toromanoff 			spec->offset = STM32MP_NOR_METADATA1_OFFSET;
758dfbadfd9SNicolas Toromanoff 		} else {
759dfbadfd9SNicolas Toromanoff 			spec->offset = STM32MP_NOR_METADATA2_OFFSET;
760dfbadfd9SNicolas Toromanoff 		}
761dfbadfd9SNicolas Toromanoff 
762dfbadfd9SNicolas Toromanoff 		spec->length = sizeof(struct fwu_metadata);
763dfbadfd9SNicolas Toromanoff 		break;
764dfbadfd9SNicolas Toromanoff #endif
765dfbadfd9SNicolas Toromanoff 	default:
766dfbadfd9SNicolas Toromanoff 		panic();
767dfbadfd9SNicolas Toromanoff 		break;
768dfbadfd9SNicolas Toromanoff 	}
7690ca180f6SSughosh Ganu 
7700ca180f6SSughosh Ganu 	*image_spec = policy->image_spec;
7710ca180f6SSughosh Ganu 	*handle = *policy->dev_handle;
7720ca180f6SSughosh Ganu 
7730ca180f6SSughosh Ganu 	return 0;
7740ca180f6SSughosh Ganu }
7750ca180f6SSughosh Ganu 
7760ca180f6SSughosh Ganu int plat_fwu_set_metadata_image_source(unsigned int image_id,
7770ca180f6SSughosh Ganu 				       uintptr_t *handle,
7780ca180f6SSughosh Ganu 				       uintptr_t *image_spec)
7790ca180f6SSughosh Ganu {
7800ca180f6SSughosh Ganu 	assert((image_id == FWU_METADATA_IMAGE_ID) ||
7810ca180f6SSughosh Ganu 	       (image_id == BKUP_FWU_METADATA_IMAGE_ID));
7820ca180f6SSughosh Ganu 
783dfbadfd9SNicolas Toromanoff 	return plat_set_image_source(image_id, handle, image_spec);
7840ca180f6SSughosh Ganu }
785dfbadfd9SNicolas Toromanoff #endif /* (STM32MP_SDMMC || STM32MP_EMMC || STM32MP_SPI_NOR) && PSA_FWU_SUPPORT */
786