1c9d75b3cSYann Gautier /* 2ae81d48dSYann Gautier * Copyright (c) 2015-2024, 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; 50ae81d48dSYann Gautier static uint32_t nand_block_sz __maybe_unused; 51c9d75b3cSYann Gautier 52cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none 53cd791164SLionel Debieve static const io_dev_connector_t *enc_dev_con; 54cd791164SLionel Debieve uintptr_t enc_dev_handle; 55cd791164SLionel Debieve #endif 56cd791164SLionel Debieve 5746554b64SNicolas Le Bayon #if STM32MP_SDMMC || STM32MP_EMMC 58cddf1bd7SYann Gautier static struct mmc_device_info mmc_info; 59c9d75b3cSYann Gautier 60a2500ab7SYann Gautier static uint8_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE); 61c9d75b3cSYann Gautier 6218b415beSYann Gautier static io_block_dev_spec_t mmc_block_dev_spec = { 63c9d75b3cSYann Gautier /* It's used as temp buffer in block driver */ 64c9d75b3cSYann Gautier .buffer = { 65c9d75b3cSYann Gautier .offset = (size_t)&block_buffer, 66c9d75b3cSYann Gautier .length = MMC_BLOCK_SIZE, 67c9d75b3cSYann Gautier }, 68c9d75b3cSYann Gautier .ops = { 69c9d75b3cSYann Gautier .read = mmc_read_blocks, 70c9d75b3cSYann Gautier .write = NULL, 71c9d75b3cSYann Gautier }, 72c9d75b3cSYann Gautier .block_size = MMC_BLOCK_SIZE, 73c9d75b3cSYann Gautier }; 74c9d75b3cSYann Gautier 75c9d75b3cSYann Gautier static const io_dev_connector_t *mmc_dev_con; 7646554b64SNicolas Le Bayon #endif /* STM32MP_SDMMC || STM32MP_EMMC */ 77c9d75b3cSYann Gautier 78b1b218fbSLionel Debieve #if STM32MP_SPI_NOR 79b1b218fbSLionel Debieve static io_mtd_dev_spec_t spi_nor_dev_spec = { 80b1b218fbSLionel Debieve .ops = { 81b1b218fbSLionel Debieve .init = spi_nor_init, 82b1b218fbSLionel Debieve .read = spi_nor_read, 83b1b218fbSLionel Debieve }, 84b1b218fbSLionel Debieve }; 85b1b218fbSLionel Debieve #endif 86b1b218fbSLionel Debieve 8712e21dfdSLionel Debieve #if STM32MP_RAW_NAND 8812e21dfdSLionel Debieve static io_mtd_dev_spec_t nand_dev_spec = { 8912e21dfdSLionel Debieve .ops = { 9012e21dfdSLionel Debieve .init = nand_raw_init, 9112e21dfdSLionel Debieve .read = nand_read, 921d204ee4SYann Gautier .seek = nand_seek_bb 9312e21dfdSLionel Debieve }, 9412e21dfdSLionel Debieve }; 9512e21dfdSLionel Debieve 9612e21dfdSLionel Debieve static const io_dev_connector_t *nand_dev_con; 9712e21dfdSLionel Debieve #endif 9812e21dfdSLionel Debieve 9957044228SLionel Debieve #if STM32MP_SPI_NAND 10057044228SLionel Debieve static io_mtd_dev_spec_t spi_nand_dev_spec = { 10157044228SLionel Debieve .ops = { 10257044228SLionel Debieve .init = spi_nand_init, 10357044228SLionel Debieve .read = nand_read, 1041d204ee4SYann Gautier .seek = nand_seek_bb 10557044228SLionel Debieve }, 10657044228SLionel Debieve }; 107b1b218fbSLionel Debieve #endif 10857044228SLionel Debieve 109b1b218fbSLionel Debieve #if STM32MP_SPI_NAND || STM32MP_SPI_NOR 11057044228SLionel Debieve static const io_dev_connector_t *spi_dev_con; 11157044228SLionel Debieve #endif 11257044228SLionel Debieve 1139083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER 114fa92fef0SPatrick Delaunay static const io_dev_connector_t *memmap_dev_con; 115fa92fef0SPatrick Delaunay #endif 116fa92fef0SPatrick Delaunay 117d5a84eeaSYann Gautier io_block_spec_t image_block_spec = { 1181d204ee4SYann Gautier .offset = 0U, 1191d204ee4SYann Gautier .length = 0U, 120c9d75b3cSYann Gautier }; 121c9d75b3cSYann Gautier 122d5a84eeaSYann Gautier int open_fip(const uintptr_t spec) 123c9d75b3cSYann Gautier { 1241d204ee4SYann Gautier return io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); 125c9d75b3cSYann Gautier } 126c9d75b3cSYann Gautier 127cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none 128cd791164SLionel Debieve int open_enc_fip(const uintptr_t spec) 129cd791164SLionel Debieve { 130cd791164SLionel Debieve int result; 131cd791164SLionel Debieve uintptr_t local_image_handle; 132cd791164SLionel Debieve 133cd791164SLionel Debieve result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID); 134cd791164SLionel Debieve if (result != 0) { 135cd791164SLionel Debieve return result; 136cd791164SLionel Debieve } 137cd791164SLionel Debieve 138cd791164SLionel Debieve result = io_open(enc_dev_handle, spec, &local_image_handle); 139cd791164SLionel Debieve if (result != 0) { 140cd791164SLionel Debieve return result; 141cd791164SLionel Debieve } 142cd791164SLionel Debieve 143cd791164SLionel Debieve VERBOSE("Using encrypted FIP\n"); 144cd791164SLionel Debieve io_close(local_image_handle); 145cd791164SLionel Debieve 146cd791164SLionel Debieve return 0; 147cd791164SLionel Debieve } 148cd791164SLionel Debieve #endif 149cd791164SLionel Debieve 150d5a84eeaSYann Gautier int open_storage(const uintptr_t spec) 151c9d75b3cSYann Gautier { 152c9d75b3cSYann Gautier return io_dev_init(storage_dev_handle, 0); 153c9d75b3cSYann Gautier } 154c9d75b3cSYann Gautier 15595e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT 15695e4908eSAhmad Fatoum static uint32_t get_boot_part_fip_header(void) 15795e4908eSAhmad Fatoum { 15895e4908eSAhmad Fatoum io_block_spec_t emmc_boot_fip_block_spec = { 15995e4908eSAhmad Fatoum .offset = STM32MP_EMMC_BOOT_FIP_OFFSET, 16095e4908eSAhmad Fatoum .length = MMC_BLOCK_SIZE, /* We are interested only in first 4 bytes */ 16195e4908eSAhmad Fatoum }; 16295e4908eSAhmad Fatoum uint32_t magic = 0U; 16395e4908eSAhmad Fatoum int io_result; 16495e4908eSAhmad Fatoum size_t bytes_read; 16595e4908eSAhmad Fatoum uintptr_t fip_hdr_handle; 16695e4908eSAhmad Fatoum 16795e4908eSAhmad Fatoum io_result = io_open(storage_dev_handle, (uintptr_t)&emmc_boot_fip_block_spec, 16895e4908eSAhmad Fatoum &fip_hdr_handle); 16995e4908eSAhmad Fatoum assert(io_result == 0); 17095e4908eSAhmad Fatoum 17195e4908eSAhmad Fatoum io_result = io_read(fip_hdr_handle, (uintptr_t)&magic, sizeof(magic), 17295e4908eSAhmad Fatoum &bytes_read); 17395e4908eSAhmad Fatoum if ((io_result != 0) || (bytes_read != sizeof(magic))) { 17495e4908eSAhmad Fatoum panic(); 17595e4908eSAhmad Fatoum } 17695e4908eSAhmad Fatoum 17795e4908eSAhmad Fatoum io_close(fip_hdr_handle); 17895e4908eSAhmad Fatoum 17995e4908eSAhmad Fatoum VERBOSE("%s: eMMC boot magic at offset 256K: %08x\n", 18095e4908eSAhmad Fatoum __func__, magic); 18195e4908eSAhmad Fatoum 18295e4908eSAhmad Fatoum return magic; 18395e4908eSAhmad Fatoum } 18495e4908eSAhmad Fatoum #endif 18595e4908eSAhmad Fatoum 186c9d75b3cSYann Gautier static void print_boot_device(boot_api_context_t *boot_context) 187c9d75b3cSYann Gautier { 188c9d75b3cSYann Gautier switch (boot_context->boot_interface_selected) { 189c9d75b3cSYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: 190c9d75b3cSYann Gautier INFO("Using SDMMC\n"); 191c9d75b3cSYann Gautier break; 192c9d75b3cSYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: 193c9d75b3cSYann Gautier INFO("Using EMMC\n"); 194c9d75b3cSYann Gautier break; 195b0ce4024SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: 196b0ce4024SYann Gautier INFO("Using SPI NOR\n"); 197b1b218fbSLionel Debieve break; 19812e21dfdSLionel Debieve case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: 19912e21dfdSLionel Debieve INFO("Using FMC NAND\n"); 20012e21dfdSLionel Debieve break; 201b0ce4024SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: 20257044228SLionel Debieve INFO("Using SPI NAND\n"); 20357044228SLionel Debieve break; 2049083fa11SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: 2059083fa11SPatrick Delaunay INFO("Using UART\n"); 2069083fa11SPatrick Delaunay break; 207fa92fef0SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: 208fa92fef0SPatrick Delaunay INFO("Using USB\n"); 209fa92fef0SPatrick Delaunay break; 210c9d75b3cSYann Gautier default: 2111d204ee4SYann Gautier ERROR("Boot interface %u not found\n", 2121d204ee4SYann Gautier boot_context->boot_interface_selected); 213c9d75b3cSYann Gautier panic(); 214c9d75b3cSYann Gautier break; 215c9d75b3cSYann Gautier } 216c9d75b3cSYann Gautier 217c9d75b3cSYann Gautier if (boot_context->boot_interface_instance != 0U) { 218c9d75b3cSYann Gautier INFO(" Instance %d\n", boot_context->boot_interface_instance); 219c9d75b3cSYann Gautier } 220c9d75b3cSYann Gautier } 221c9d75b3cSYann Gautier 22246554b64SNicolas Le Bayon #if STM32MP_SDMMC || STM32MP_EMMC 2230b1aa772SYann Gautier static void boot_mmc(enum mmc_device_type mmc_dev_type, 2240b1aa772SYann Gautier uint16_t boot_interface_instance) 225c9d75b3cSYann Gautier { 226dfbadfd9SNicolas Toromanoff int io_result __maybe_unused; 227c9d75b3cSYann Gautier struct stm32_sdmmc2_params params; 228c9d75b3cSYann Gautier 22942beea8dSYann Gautier zeromem(¶ms, sizeof(struct stm32_sdmmc2_params)); 230c9d75b3cSYann Gautier 231cddf1bd7SYann Gautier mmc_info.mmc_dev_type = mmc_dev_type; 232c9d75b3cSYann Gautier 2330b1aa772SYann Gautier switch (boot_interface_instance) { 234c9d75b3cSYann Gautier case 1: 2353f9c9784SYann Gautier params.reg_base = STM32MP_SDMMC1_BASE; 236c9d75b3cSYann Gautier break; 237c9d75b3cSYann Gautier case 2: 2383f9c9784SYann Gautier params.reg_base = STM32MP_SDMMC2_BASE; 239c9d75b3cSYann Gautier break; 240c9d75b3cSYann Gautier case 3: 2413f9c9784SYann Gautier params.reg_base = STM32MP_SDMMC3_BASE; 242c9d75b3cSYann Gautier break; 243c9d75b3cSYann Gautier default: 244c9d75b3cSYann Gautier WARN("SDMMC instance not found, using default\n"); 2450b1aa772SYann Gautier if (mmc_dev_type == MMC_IS_SD) { 2460b1aa772SYann Gautier params.reg_base = STM32MP_SDMMC1_BASE; 2470b1aa772SYann Gautier } else { 2480b1aa772SYann Gautier params.reg_base = STM32MP_SDMMC2_BASE; 2490b1aa772SYann Gautier } 250c9d75b3cSYann Gautier break; 251c9d75b3cSYann Gautier } 252c9d75b3cSYann Gautier 25353d5b8ffSYann Gautier if (mmc_dev_type != MMC_IS_EMMC) { 25453d5b8ffSYann Gautier params.flags = MMC_FLAG_SD_CMD6; 25553d5b8ffSYann Gautier } 25653d5b8ffSYann Gautier 257cddf1bd7SYann Gautier params.device_info = &mmc_info; 258c9d75b3cSYann Gautier if (stm32_sdmmc2_mmc_init(¶ms) != 0) { 2590b1aa772SYann Gautier ERROR("SDMMC%u init failed\n", boot_interface_instance); 260c9d75b3cSYann Gautier panic(); 261c9d75b3cSYann Gautier } 262c9d75b3cSYann Gautier 26395e4908eSAhmad Fatoum /* Open MMC as a block device to read FIP */ 264c9d75b3cSYann Gautier io_result = register_io_dev_block(&mmc_dev_con); 265c9d75b3cSYann Gautier if (io_result != 0) { 266c9d75b3cSYann Gautier panic(); 267c9d75b3cSYann Gautier } 268c9d75b3cSYann Gautier 2690b1aa772SYann Gautier io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec, 270c9d75b3cSYann Gautier &storage_dev_handle); 271c9d75b3cSYann Gautier assert(io_result == 0); 27295e4908eSAhmad Fatoum 27395e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT 27495e4908eSAhmad Fatoum if (mmc_dev_type == MMC_IS_EMMC) { 27595e4908eSAhmad Fatoum io_result = mmc_part_switch_current_boot(); 27695e4908eSAhmad Fatoum assert(io_result == 0); 27795e4908eSAhmad Fatoum 27895e4908eSAhmad Fatoum if (get_boot_part_fip_header() != TOC_HEADER_NAME) { 27995e4908eSAhmad Fatoum WARN("%s: Can't find FIP header on eMMC boot partition. Trying GPT\n", 28095e4908eSAhmad Fatoum __func__); 28195e4908eSAhmad Fatoum io_result = mmc_part_switch_user(); 28295e4908eSAhmad Fatoum assert(io_result == 0); 28395e4908eSAhmad Fatoum return; 28495e4908eSAhmad Fatoum } 28595e4908eSAhmad Fatoum 28695e4908eSAhmad Fatoum VERBOSE("%s: FIP header found on eMMC boot partition\n", 28795e4908eSAhmad Fatoum __func__); 28895e4908eSAhmad Fatoum image_block_spec.offset = STM32MP_EMMC_BOOT_FIP_OFFSET; 289e7cb4a86SYann Gautier image_block_spec.length = mmc_boot_part_size() - STM32MP_EMMC_BOOT_FIP_OFFSET; 29095e4908eSAhmad Fatoum } 29195e4908eSAhmad Fatoum #endif 2920b1aa772SYann Gautier } 29346554b64SNicolas Le Bayon #endif /* STM32MP_SDMMC || STM32MP_EMMC */ 2940b1aa772SYann Gautier 295b1b218fbSLionel Debieve #if STM32MP_SPI_NOR 296b1b218fbSLionel Debieve static void boot_spi_nor(boot_api_context_t *boot_context) 297b1b218fbSLionel Debieve { 298dfbadfd9SNicolas Toromanoff int io_result __maybe_unused; 299b1b218fbSLionel Debieve 300b1b218fbSLionel Debieve io_result = stm32_qspi_init(); 301b1b218fbSLionel Debieve assert(io_result == 0); 302b1b218fbSLionel Debieve 303b1b218fbSLionel Debieve io_result = register_io_dev_mtd(&spi_dev_con); 304b1b218fbSLionel Debieve assert(io_result == 0); 305b1b218fbSLionel Debieve 306b1b218fbSLionel Debieve /* Open connections to device */ 307b1b218fbSLionel Debieve io_result = io_dev_open(spi_dev_con, 308b1b218fbSLionel Debieve (uintptr_t)&spi_nor_dev_spec, 309b1b218fbSLionel Debieve &storage_dev_handle); 310b1b218fbSLionel Debieve assert(io_result == 0); 311b1b218fbSLionel Debieve } 312b1b218fbSLionel Debieve #endif /* STM32MP_SPI_NOR */ 313b1b218fbSLionel Debieve 314ae81d48dSYann Gautier #if STM32MP_RAW_NAND || STM32MP_SPI_NAND 315ae81d48dSYann Gautier /* 316ae81d48dSYann Gautier * This function returns 0 if it can find an alternate 317ae81d48dSYann Gautier * image to be loaded or a negative errno otherwise. 318ae81d48dSYann Gautier */ 319ae81d48dSYann Gautier static int try_nand_backup_partitions(unsigned int image_id) 320ae81d48dSYann Gautier { 321ae81d48dSYann Gautier static unsigned int backup_id; 322ae81d48dSYann Gautier static unsigned int backup_block_nb; 323ae81d48dSYann Gautier 324ae81d48dSYann Gautier /* Check if NAND storage used */ 325ae81d48dSYann Gautier if (nand_block_sz == 0U) { 326ae81d48dSYann Gautier return -ENODEV; 327ae81d48dSYann Gautier } 328ae81d48dSYann Gautier 329ae81d48dSYann Gautier if (backup_id != image_id) { 330ae81d48dSYann Gautier backup_block_nb = PLATFORM_MTD_MAX_PART_SIZE / nand_block_sz; 331ae81d48dSYann Gautier backup_id = image_id; 332ae81d48dSYann Gautier } 333ae81d48dSYann Gautier 334ae81d48dSYann Gautier if (backup_block_nb-- == 0U) { 335ae81d48dSYann Gautier return -ENOSPC; 336ae81d48dSYann Gautier } 337ae81d48dSYann Gautier 338*795a559bSYann Gautier #if PSA_FWU_SUPPORT 339*795a559bSYann Gautier if (((image_block_spec.offset < STM32MP_NAND_FIP_B_OFFSET) && 340*795a559bSYann Gautier ((image_block_spec.offset + nand_block_sz) >= STM32MP_NAND_FIP_B_OFFSET)) || 341*795a559bSYann Gautier (image_block_spec.offset + nand_block_sz >= STM32MP_NAND_FIP_B_MAX_OFFSET)) { 342*795a559bSYann Gautier return 0; 343*795a559bSYann Gautier } 344*795a559bSYann Gautier #endif 345*795a559bSYann Gautier 346ae81d48dSYann Gautier image_block_spec.offset += nand_block_sz; 347ae81d48dSYann Gautier 348ae81d48dSYann Gautier return 0; 349ae81d48dSYann Gautier } 350ae81d48dSYann Gautier 351ae81d48dSYann Gautier static const struct plat_try_images_ops try_img_ops = { 352ae81d48dSYann Gautier .next_instance = try_nand_backup_partitions, 353ae81d48dSYann Gautier }; 354ae81d48dSYann Gautier #endif /* STM32MP_RAW_NAND || STM32MP_SPI_NAND */ 355ae81d48dSYann Gautier 35612e21dfdSLionel Debieve #if STM32MP_RAW_NAND 35712e21dfdSLionel Debieve static void boot_fmc2_nand(boot_api_context_t *boot_context) 35812e21dfdSLionel Debieve { 359dfbadfd9SNicolas Toromanoff int io_result __maybe_unused; 36012e21dfdSLionel Debieve 361ae81d48dSYann Gautier plat_setup_try_img_ops(&try_img_ops); 362ae81d48dSYann Gautier 36312e21dfdSLionel Debieve io_result = stm32_fmc2_init(); 36412e21dfdSLionel Debieve assert(io_result == 0); 36512e21dfdSLionel Debieve 36612e21dfdSLionel Debieve /* Register the IO device on this platform */ 36712e21dfdSLionel Debieve io_result = register_io_dev_mtd(&nand_dev_con); 36812e21dfdSLionel Debieve assert(io_result == 0); 36912e21dfdSLionel Debieve 37012e21dfdSLionel Debieve /* Open connections to device */ 37112e21dfdSLionel Debieve io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec, 37212e21dfdSLionel Debieve &storage_dev_handle); 37312e21dfdSLionel Debieve assert(io_result == 0); 374ae81d48dSYann Gautier 375ae81d48dSYann Gautier nand_block_sz = nand_dev_spec.erase_size; 37612e21dfdSLionel Debieve } 37712e21dfdSLionel Debieve #endif /* STM32MP_RAW_NAND */ 37812e21dfdSLionel Debieve 37957044228SLionel Debieve #if STM32MP_SPI_NAND 38057044228SLionel Debieve static void boot_spi_nand(boot_api_context_t *boot_context) 38157044228SLionel Debieve { 382dfbadfd9SNicolas Toromanoff int io_result __maybe_unused; 38357044228SLionel Debieve 384ae81d48dSYann Gautier plat_setup_try_img_ops(&try_img_ops); 385ae81d48dSYann Gautier 38657044228SLionel Debieve io_result = stm32_qspi_init(); 38757044228SLionel Debieve assert(io_result == 0); 38857044228SLionel Debieve 38957044228SLionel Debieve io_result = register_io_dev_mtd(&spi_dev_con); 39057044228SLionel Debieve assert(io_result == 0); 39157044228SLionel Debieve 39257044228SLionel Debieve /* Open connections to device */ 39357044228SLionel Debieve io_result = io_dev_open(spi_dev_con, 39457044228SLionel Debieve (uintptr_t)&spi_nand_dev_spec, 39557044228SLionel Debieve &storage_dev_handle); 39657044228SLionel Debieve assert(io_result == 0); 397ae81d48dSYann Gautier 398ae81d48dSYann Gautier nand_block_sz = spi_nand_dev_spec.erase_size; 39957044228SLionel Debieve } 40057044228SLionel Debieve #endif /* STM32MP_SPI_NAND */ 40157044228SLionel Debieve 4029083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER 403fa92fef0SPatrick Delaunay static void mmap_io_setup(void) 404fa92fef0SPatrick Delaunay { 405dfbadfd9SNicolas Toromanoff int io_result __maybe_unused; 406fa92fef0SPatrick Delaunay 407fa92fef0SPatrick Delaunay io_result = register_io_dev_memmap(&memmap_dev_con); 408fa92fef0SPatrick Delaunay assert(io_result == 0); 409fa92fef0SPatrick Delaunay 410fa92fef0SPatrick Delaunay io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL, 411fa92fef0SPatrick Delaunay &storage_dev_handle); 412fa92fef0SPatrick Delaunay assert(io_result == 0); 413fa92fef0SPatrick Delaunay } 414fa92fef0SPatrick Delaunay 4159083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER 4169083fa11SPatrick Delaunay static void stm32cubeprogrammer_uart(void) 4179083fa11SPatrick Delaunay { 418dfbadfd9SNicolas Toromanoff int ret __maybe_unused; 4199083fa11SPatrick Delaunay boot_api_context_t *boot_context = 4209083fa11SPatrick Delaunay (boot_api_context_t *)stm32mp_get_boot_ctx_address(); 4219083fa11SPatrick Delaunay uintptr_t uart_base; 4229083fa11SPatrick Delaunay 4239083fa11SPatrick Delaunay uart_base = get_uart_address(boot_context->boot_interface_instance); 4249083fa11SPatrick Delaunay ret = stm32cubeprog_uart_load(uart_base, DWL_BUFFER_BASE, DWL_BUFFER_SIZE); 4259083fa11SPatrick Delaunay assert(ret == 0); 4269083fa11SPatrick Delaunay } 4279083fa11SPatrick Delaunay #endif 4289083fa11SPatrick Delaunay 4299083fa11SPatrick Delaunay #if STM32MP_USB_PROGRAMMER 430fa92fef0SPatrick Delaunay static void stm32cubeprogrammer_usb(void) 431fa92fef0SPatrick Delaunay { 432dfbadfd9SNicolas Toromanoff int ret __maybe_unused; 433fa92fef0SPatrick Delaunay struct usb_handle *pdev; 434fa92fef0SPatrick Delaunay 435fa92fef0SPatrick Delaunay /* Init USB on platform */ 436fa92fef0SPatrick Delaunay pdev = usb_dfu_plat_init(); 437fa92fef0SPatrick Delaunay 438fa92fef0SPatrick Delaunay ret = stm32cubeprog_usb_load(pdev, DWL_BUFFER_BASE, DWL_BUFFER_SIZE); 439fa92fef0SPatrick Delaunay assert(ret == 0); 440fa92fef0SPatrick Delaunay } 441fa92fef0SPatrick Delaunay #endif 4429083fa11SPatrick Delaunay #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */ 4439083fa11SPatrick Delaunay 4440b1aa772SYann Gautier void stm32mp_io_setup(void) 4450b1aa772SYann Gautier { 446dfbadfd9SNicolas Toromanoff int io_result __maybe_unused; 4470b1aa772SYann Gautier boot_api_context_t *boot_context = 4480b1aa772SYann Gautier (boot_api_context_t *)stm32mp_get_boot_ctx_address(); 4490b1aa772SYann Gautier 4500b1aa772SYann Gautier print_boot_device(boot_context); 4510b1aa772SYann Gautier 4520b1aa772SYann Gautier if ((boot_context->boot_partition_used_toboot == 1U) || 4530b1aa772SYann Gautier (boot_context->boot_partition_used_toboot == 2U)) { 4541d204ee4SYann Gautier INFO("Boot used partition fsbl%u\n", 4550b1aa772SYann Gautier boot_context->boot_partition_used_toboot); 4560b1aa772SYann Gautier } 4570b1aa772SYann Gautier 4581d204ee4SYann Gautier io_result = register_io_dev_fip(&fip_dev_con); 4590b1aa772SYann Gautier assert(io_result == 0); 4600b1aa772SYann Gautier 4611d204ee4SYann Gautier io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL, 4621d204ee4SYann Gautier &fip_dev_handle); 4630b1aa772SYann Gautier 464cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none 465cd791164SLionel Debieve io_result = register_io_dev_enc(&enc_dev_con); 466cd791164SLionel Debieve assert(io_result == 0); 467cd791164SLionel Debieve 468cd791164SLionel Debieve io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL, 469cd791164SLionel Debieve &enc_dev_handle); 470cd791164SLionel Debieve assert(io_result == 0); 471cd791164SLionel Debieve #endif 472cd791164SLionel Debieve 4730b1aa772SYann Gautier switch (boot_context->boot_interface_selected) { 47446554b64SNicolas Le Bayon #if STM32MP_SDMMC 4750b1aa772SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: 4760b1aa772SYann Gautier dmbsy(); 4770b1aa772SYann Gautier boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance); 4780b1aa772SYann Gautier break; 47946554b64SNicolas Le Bayon #endif 48046554b64SNicolas Le Bayon #if STM32MP_EMMC 4810b1aa772SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: 4820b1aa772SYann Gautier dmbsy(); 4830b1aa772SYann Gautier boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance); 484c9d75b3cSYann Gautier break; 48546554b64SNicolas Le Bayon #endif 486b1b218fbSLionel Debieve #if STM32MP_SPI_NOR 487b0ce4024SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: 488b1b218fbSLionel Debieve dmbsy(); 489b1b218fbSLionel Debieve boot_spi_nor(boot_context); 490b1b218fbSLionel Debieve break; 491b1b218fbSLionel Debieve #endif 49212e21dfdSLionel Debieve #if STM32MP_RAW_NAND 49312e21dfdSLionel Debieve case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: 49412e21dfdSLionel Debieve dmbsy(); 49512e21dfdSLionel Debieve boot_fmc2_nand(boot_context); 49612e21dfdSLionel Debieve break; 49712e21dfdSLionel Debieve #endif 49857044228SLionel Debieve #if STM32MP_SPI_NAND 499b0ce4024SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: 50057044228SLionel Debieve dmbsy(); 50157044228SLionel Debieve boot_spi_nand(boot_context); 50257044228SLionel Debieve break; 50357044228SLionel Debieve #endif 5049083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER 5059083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER 5069083fa11SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: 5079083fa11SPatrick Delaunay #endif 508fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER 509fa92fef0SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: 5109083fa11SPatrick Delaunay #endif 511fa92fef0SPatrick Delaunay dmbsy(); 512fa92fef0SPatrick Delaunay mmap_io_setup(); 513fa92fef0SPatrick Delaunay break; 514fa92fef0SPatrick Delaunay #endif 515c9d75b3cSYann Gautier 516c9d75b3cSYann Gautier default: 517c9d75b3cSYann Gautier ERROR("Boot interface %d not supported\n", 518c9d75b3cSYann Gautier boot_context->boot_interface_selected); 51971693a66SYann Gautier panic(); 520c9d75b3cSYann Gautier break; 521c9d75b3cSYann Gautier } 522c9d75b3cSYann Gautier } 523c9d75b3cSYann Gautier 5241d204ee4SYann Gautier int bl2_plat_handle_pre_image_load(unsigned int image_id) 5251d204ee4SYann Gautier { 526dfbadfd9SNicolas Toromanoff static bool gpt_init_done __maybe_unused; 5271d204ee4SYann Gautier uint16_t boot_itf = stm32mp_get_boot_itf_selected(); 5281d204ee4SYann Gautier 5291d204ee4SYann Gautier switch (boot_itf) { 5301d204ee4SYann Gautier #if STM32MP_SDMMC || STM32MP_EMMC 5311d204ee4SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: 53295e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT 53395e4908eSAhmad Fatoum if (image_block_spec.offset == STM32MP_EMMC_BOOT_FIP_OFFSET) { 53495e4908eSAhmad Fatoum break; 53595e4908eSAhmad Fatoum } 53695e4908eSAhmad Fatoum #endif 53795e4908eSAhmad Fatoum /* fallthrough */ 53895e4908eSAhmad Fatoum case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: 5391d204ee4SYann Gautier if (!gpt_init_done) { 5408dd75531SSughosh Ganu /* 5418dd75531SSughosh Ganu * With FWU Multi Bank feature enabled, the selection of 5428dd75531SSughosh Ganu * the image to boot will be done by fwu_init calling the 5438dd75531SSughosh Ganu * platform hook, plat_fwu_set_images_source. 5448dd75531SSughosh Ganu */ 5458dd75531SSughosh Ganu #if !PSA_FWU_SUPPORT 5461d204ee4SYann Gautier const partition_entry_t *entry; 5478d08a1dfSSughosh Ganu const struct efi_guid fip_guid = STM32MP_FIP_GUID; 5481d204ee4SYann Gautier 5491d204ee4SYann Gautier partition_init(GPT_IMAGE_ID); 5508d08a1dfSSughosh Ganu entry = get_partition_entry_by_type(&fip_guid); 5511dab28f9SLionel Debieve if (entry == NULL) { 5521d204ee4SYann Gautier entry = get_partition_entry(FIP_IMAGE_NAME); 5531d204ee4SYann Gautier if (entry == NULL) { 5541d204ee4SYann Gautier ERROR("Could NOT find the %s partition!\n", 5551d204ee4SYann Gautier FIP_IMAGE_NAME); 5561dab28f9SLionel Debieve 5571d204ee4SYann Gautier return -ENOENT; 5581d204ee4SYann Gautier } 5591dab28f9SLionel Debieve } 5601d204ee4SYann Gautier 5611d204ee4SYann Gautier image_block_spec.offset = entry->start; 5621d204ee4SYann Gautier image_block_spec.length = entry->length; 5638dd75531SSughosh Ganu #endif 5641d204ee4SYann Gautier gpt_init_done = true; 56518b415beSYann Gautier } else { 56618b415beSYann Gautier bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); 567dfbadfd9SNicolas Toromanoff 5682deff904SYann Gautier assert(bl_mem_params != NULL); 56918b415beSYann Gautier 57018b415beSYann Gautier mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base; 57118b415beSYann Gautier mmc_block_dev_spec.buffer.length = bl_mem_params->image_info.image_max_size; 5721d204ee4SYann Gautier } 5731d204ee4SYann Gautier 5741d204ee4SYann Gautier break; 5751d204ee4SYann Gautier #endif 5761d204ee4SYann Gautier 5771d204ee4SYann Gautier #if STM32MP_RAW_NAND || STM32MP_SPI_NAND 5781d204ee4SYann Gautier #if STM32MP_RAW_NAND 5791d204ee4SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: 5801d204ee4SYann Gautier #endif 5811d204ee4SYann Gautier #if STM32MP_SPI_NAND 582b0ce4024SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: 5831d204ee4SYann Gautier #endif 584*795a559bSYann Gautier /* 585*795a559bSYann Gautier * With FWU Multi Bank feature enabled, the selection of 586*795a559bSYann Gautier * the image to boot will be done by fwu_init calling the 587*795a559bSYann Gautier * platform hook, plat_fwu_set_images_source. 588*795a559bSYann Gautier */ 589*795a559bSYann Gautier #if !PSA_FWU_SUPPORT 5901d204ee4SYann Gautier image_block_spec.offset = STM32MP_NAND_FIP_OFFSET; 591*795a559bSYann Gautier #endif 5921d204ee4SYann Gautier break; 5931d204ee4SYann Gautier #endif 5941d204ee4SYann Gautier 5951d204ee4SYann Gautier #if STM32MP_SPI_NOR 596b0ce4024SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: 597dfbadfd9SNicolas Toromanoff /* 598dfbadfd9SNicolas Toromanoff * With FWU Multi Bank feature enabled, the selection of 599dfbadfd9SNicolas Toromanoff * the image to boot will be done by fwu_init calling the 600dfbadfd9SNicolas Toromanoff * platform hook, plat_fwu_set_images_source. 601dfbadfd9SNicolas Toromanoff */ 602dfbadfd9SNicolas Toromanoff #if !PSA_FWU_SUPPORT 6031d204ee4SYann Gautier image_block_spec.offset = STM32MP_NOR_FIP_OFFSET; 604dfbadfd9SNicolas Toromanoff #endif 6051d204ee4SYann Gautier break; 6061d204ee4SYann Gautier #endif 6071d204ee4SYann Gautier 6089083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER 6099083fa11SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: 6109083fa11SPatrick Delaunay if (image_id == FW_CONFIG_ID) { 6119083fa11SPatrick Delaunay stm32cubeprogrammer_uart(); 6129083fa11SPatrick Delaunay /* FIP loaded at DWL address */ 6139083fa11SPatrick Delaunay image_block_spec.offset = DWL_BUFFER_BASE; 6149083fa11SPatrick Delaunay image_block_spec.length = DWL_BUFFER_SIZE; 6159083fa11SPatrick Delaunay } 6169083fa11SPatrick Delaunay break; 6179083fa11SPatrick Delaunay #endif 618fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER 619fa92fef0SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: 620fa92fef0SPatrick Delaunay if (image_id == FW_CONFIG_ID) { 621fa92fef0SPatrick Delaunay stm32cubeprogrammer_usb(); 622fa92fef0SPatrick Delaunay /* FIP loaded at DWL address */ 623fa92fef0SPatrick Delaunay image_block_spec.offset = DWL_BUFFER_BASE; 624fa92fef0SPatrick Delaunay image_block_spec.length = DWL_BUFFER_SIZE; 625fa92fef0SPatrick Delaunay } 626fa92fef0SPatrick Delaunay break; 627fa92fef0SPatrick Delaunay #endif 628fa92fef0SPatrick Delaunay 6291d204ee4SYann Gautier default: 6301d204ee4SYann Gautier ERROR("FIP Not found\n"); 6311d204ee4SYann Gautier panic(); 6321d204ee4SYann Gautier } 6331d204ee4SYann Gautier 6341d204ee4SYann Gautier return 0; 6351d204ee4SYann Gautier } 6361d204ee4SYann Gautier 637c9d75b3cSYann Gautier /* 638c9d75b3cSYann Gautier * Return an IO device handle and specification which can be used to access 639c9d75b3cSYann Gautier * an image. Use this to enforce platform load policy. 640c9d75b3cSYann Gautier */ 641c9d75b3cSYann Gautier int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, 642c9d75b3cSYann Gautier uintptr_t *image_spec) 643c9d75b3cSYann Gautier { 644c9d75b3cSYann Gautier int rc; 645c9d75b3cSYann Gautier const struct plat_io_policy *policy; 646c9d75b3cSYann Gautier 647d5a84eeaSYann Gautier policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id); 648c9d75b3cSYann Gautier rc = policy->check(policy->image_spec); 649c9d75b3cSYann Gautier if (rc == 0) { 650c9d75b3cSYann Gautier *image_spec = policy->image_spec; 651c9d75b3cSYann Gautier *dev_handle = *(policy->dev_handle); 652c9d75b3cSYann Gautier } 653c9d75b3cSYann Gautier 654c9d75b3cSYann Gautier return rc; 655c9d75b3cSYann Gautier } 6568dd75531SSughosh Ganu 657*795a559bSYann Gautier #if PSA_FWU_SUPPORT 6588dd75531SSughosh Ganu /* 659f87de907SNicolas Toromanoff * In each boot in non-trial mode, we set the BKP register to 660f87de907SNicolas Toromanoff * FWU_MAX_TRIAL_REBOOT, and return the active_index from metadata. 661f87de907SNicolas Toromanoff * 662f87de907SNicolas Toromanoff * As long as the update agent didn't update the "accepted" field in metadata 663f87de907SNicolas Toromanoff * (i.e. we are in trial mode), we select the new active_index. 664f87de907SNicolas Toromanoff * To avoid infinite boot loop at trial boot we decrement a BKP register. 665f87de907SNicolas Toromanoff * If this counter is 0: 666f87de907SNicolas Toromanoff * - an unexpected TAMPER event raised (that resets the BKP registers to 0) 667f87de907SNicolas Toromanoff * - a power-off occurs before the update agent was able to update the 668f87de907SNicolas Toromanoff * "accepted' field 669f87de907SNicolas Toromanoff * - we already boot FWU_MAX_TRIAL_REBOOT times in trial mode. 670f87de907SNicolas Toromanoff * we select the previous_active_index. 6718dd75531SSughosh Ganu */ 6728dd75531SSughosh Ganu uint32_t plat_fwu_get_boot_idx(void) 6738dd75531SSughosh Ganu { 674f87de907SNicolas Toromanoff /* 675f87de907SNicolas Toromanoff * Select boot index and update boot counter only once per boot 676f87de907SNicolas Toromanoff * even if this function is called several times. 677f87de907SNicolas Toromanoff */ 678f87de907SNicolas Toromanoff static uint32_t boot_idx = INVALID_BOOT_IDX; 6798dd75531SSughosh Ganu 680f87de907SNicolas Toromanoff if (boot_idx == INVALID_BOOT_IDX) { 68161660514SSughosh Ganu const struct fwu_metadata *data = fwu_get_metadata(); 68261660514SSughosh Ganu 683f87de907SNicolas Toromanoff boot_idx = data->active_index; 68461660514SSughosh Ganu 685588b01b5SSughosh Ganu if (data->bank_state[boot_idx] == FWU_BANK_STATE_VALID) { 686f87de907SNicolas Toromanoff if (stm32_get_and_dec_fwu_trial_boot_cnt() == 0U) { 687f87de907SNicolas Toromanoff WARN("Trial FWU fails %u times\n", 688f87de907SNicolas Toromanoff FWU_MAX_TRIAL_REBOOT); 68961660514SSughosh Ganu boot_idx = fwu_get_alternate_boot_bank(); 690f87de907SNicolas Toromanoff } 69161660514SSughosh Ganu } else if (data->bank_state[boot_idx] == 69261660514SSughosh Ganu FWU_BANK_STATE_ACCEPTED) { 693f87de907SNicolas Toromanoff stm32_set_max_fwu_trial_boot_cnt(); 69461660514SSughosh Ganu } else { 69561660514SSughosh Ganu ERROR("The active bank(%u) of the platform is in Invalid State.\n", 69661660514SSughosh Ganu boot_idx); 69761660514SSughosh Ganu boot_idx = fwu_get_alternate_boot_bank(); 69861660514SSughosh Ganu stm32_clear_fwu_trial_boot_cnt(); 699f87de907SNicolas Toromanoff } 700f87de907SNicolas Toromanoff } 701f87de907SNicolas Toromanoff 702f87de907SNicolas Toromanoff return boot_idx; 7038dd75531SSughosh Ganu } 7048dd75531SSughosh Ganu 7058d08a1dfSSughosh Ganu static void *stm32_get_image_spec(const struct efi_guid *img_type_guid) 7068dd75531SSughosh Ganu { 7078dd75531SSughosh Ganu unsigned int i; 7088dd75531SSughosh Ganu 7098dd75531SSughosh Ganu for (i = 0U; i < MAX_NUMBER_IDS; i++) { 7108d08a1dfSSughosh Ganu if ((guidcmp(&policies[i].img_type_guid, img_type_guid)) == 0) { 7118dd75531SSughosh Ganu return (void *)policies[i].image_spec; 7128dd75531SSughosh Ganu } 7138dd75531SSughosh Ganu } 7148dd75531SSughosh Ganu 7158dd75531SSughosh Ganu return NULL; 7168dd75531SSughosh Ganu } 7178dd75531SSughosh Ganu 7188dd75531SSughosh Ganu void plat_fwu_set_images_source(const struct fwu_metadata *metadata) 7198dd75531SSughosh Ganu { 7208dd75531SSughosh Ganu unsigned int i; 7218dd75531SSughosh Ganu uint32_t boot_idx; 722dfbadfd9SNicolas Toromanoff const partition_entry_t *entry __maybe_unused; 7238d08a1dfSSughosh Ganu const struct fwu_image_entry *img_entry; 7248d08a1dfSSughosh Ganu const void *img_type_guid; 7258d08a1dfSSughosh Ganu const void *img_guid; 7268dd75531SSughosh Ganu io_block_spec_t *image_spec; 727dfbadfd9SNicolas Toromanoff const uint16_t boot_itf = stm32mp_get_boot_itf_selected(); 7288dd75531SSughosh Ganu 7298dd75531SSughosh Ganu boot_idx = plat_fwu_get_boot_idx(); 7308dd75531SSughosh Ganu assert(boot_idx < NR_OF_FW_BANKS); 73161660514SSughosh Ganu VERBOSE("Selecting to boot from bank %u\n", boot_idx); 7328dd75531SSughosh Ganu 7338d08a1dfSSughosh Ganu img_entry = (void *)&metadata->fw_desc.img_entry; 7348dd75531SSughosh Ganu for (i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) { 7358d08a1dfSSughosh Ganu img_type_guid = &img_entry[i].img_type_guid; 736dfbadfd9SNicolas Toromanoff 7378d08a1dfSSughosh Ganu img_guid = &img_entry[i].img_bank_info[boot_idx].img_guid; 738dfbadfd9SNicolas Toromanoff 7398d08a1dfSSughosh Ganu image_spec = stm32_get_image_spec(img_type_guid); 7408dd75531SSughosh Ganu if (image_spec == NULL) { 7418dd75531SSughosh Ganu ERROR("Unable to get image spec for the image in the metadata\n"); 7428dd75531SSughosh Ganu panic(); 7438dd75531SSughosh Ganu } 7448dd75531SSughosh Ganu 745dfbadfd9SNicolas Toromanoff switch (boot_itf) { 746dfbadfd9SNicolas Toromanoff #if (STM32MP_SDMMC || STM32MP_EMMC) 747dfbadfd9SNicolas Toromanoff case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: 748dfbadfd9SNicolas Toromanoff case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: 7498d08a1dfSSughosh Ganu entry = get_partition_entry_by_guid(img_guid); 7508dd75531SSughosh Ganu if (entry == NULL) { 751dfbadfd9SNicolas Toromanoff ERROR("No partition with the uuid mentioned in metadata\n"); 7528dd75531SSughosh Ganu panic(); 7538dd75531SSughosh Ganu } 7548dd75531SSughosh Ganu 7558dd75531SSughosh Ganu image_spec->offset = entry->start; 7568dd75531SSughosh Ganu image_spec->length = entry->length; 757dfbadfd9SNicolas Toromanoff break; 758dfbadfd9SNicolas Toromanoff #endif 759dfbadfd9SNicolas Toromanoff #if STM32MP_SPI_NOR 760b0ce4024SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: 7618d08a1dfSSughosh Ganu if (guidcmp(img_guid, &STM32MP_NOR_FIP_A_GUID) == 0) { 762dfbadfd9SNicolas Toromanoff image_spec->offset = STM32MP_NOR_FIP_A_OFFSET; 7638d08a1dfSSughosh Ganu } else if (guidcmp(img_guid, &STM32MP_NOR_FIP_B_GUID) == 0) { 764dfbadfd9SNicolas Toromanoff image_spec->offset = STM32MP_NOR_FIP_B_OFFSET; 765dfbadfd9SNicolas Toromanoff } else { 766dfbadfd9SNicolas Toromanoff ERROR("Invalid uuid mentioned in metadata\n"); 767dfbadfd9SNicolas Toromanoff panic(); 768dfbadfd9SNicolas Toromanoff } 769dfbadfd9SNicolas Toromanoff break; 770dfbadfd9SNicolas Toromanoff #endif 771*795a559bSYann Gautier #if (STM32MP_RAW_NAND || STM32MP_SPI_NAND) 772*795a559bSYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: 773*795a559bSYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: 774*795a559bSYann Gautier if (guidcmp(img_guid, &STM32MP_NAND_FIP_A_GUID) == 0) { 775*795a559bSYann Gautier image_spec->offset = STM32MP_NAND_FIP_A_OFFSET; 776*795a559bSYann Gautier } else if (guidcmp(img_guid, &STM32MP_NAND_FIP_B_GUID) == 0) { 777*795a559bSYann Gautier image_spec->offset = STM32MP_NAND_FIP_B_OFFSET; 778*795a559bSYann Gautier } else { 779*795a559bSYann Gautier ERROR("Invalid uuid mentioned in metadata\n"); 780*795a559bSYann Gautier panic(); 781*795a559bSYann Gautier } 782*795a559bSYann Gautier break; 783*795a559bSYann Gautier #endif 784dfbadfd9SNicolas Toromanoff default: 785dfbadfd9SNicolas Toromanoff panic(); 786dfbadfd9SNicolas Toromanoff break; 787dfbadfd9SNicolas Toromanoff } 7888dd75531SSughosh Ganu } 7898dd75531SSughosh Ganu } 7900ca180f6SSughosh Ganu 7910ca180f6SSughosh Ganu static int plat_set_image_source(unsigned int image_id, 7920ca180f6SSughosh Ganu uintptr_t *handle, 793dfbadfd9SNicolas Toromanoff uintptr_t *image_spec) 7940ca180f6SSughosh Ganu { 7950ca180f6SSughosh Ganu struct plat_io_policy *policy; 796dfbadfd9SNicolas Toromanoff io_block_spec_t *spec __maybe_unused; 797dfbadfd9SNicolas Toromanoff const partition_entry_t *entry __maybe_unused; 798dfbadfd9SNicolas Toromanoff const uint16_t boot_itf = stm32mp_get_boot_itf_selected(); 799dfbadfd9SNicolas Toromanoff 800dfbadfd9SNicolas Toromanoff policy = &policies[image_id]; 801dfbadfd9SNicolas Toromanoff spec = (io_block_spec_t *)policy->image_spec; 802dfbadfd9SNicolas Toromanoff 803dfbadfd9SNicolas Toromanoff switch (boot_itf) { 804dfbadfd9SNicolas Toromanoff #if (STM32MP_SDMMC || STM32MP_EMMC) 805dfbadfd9SNicolas Toromanoff case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: 806dfbadfd9SNicolas Toromanoff case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: 807dfbadfd9SNicolas Toromanoff partition_init(GPT_IMAGE_ID); 808dfbadfd9SNicolas Toromanoff 809dfbadfd9SNicolas Toromanoff if (image_id == FWU_METADATA_IMAGE_ID) { 810dfbadfd9SNicolas Toromanoff entry = get_partition_entry(METADATA_PART_1); 811dfbadfd9SNicolas Toromanoff } else { 812dfbadfd9SNicolas Toromanoff entry = get_partition_entry(METADATA_PART_2); 813dfbadfd9SNicolas Toromanoff } 8140ca180f6SSughosh Ganu 8150ca180f6SSughosh Ganu if (entry == NULL) { 816dfbadfd9SNicolas Toromanoff ERROR("Unable to find a metadata partition\n"); 8170ca180f6SSughosh Ganu return -ENOENT; 8180ca180f6SSughosh Ganu } 8190ca180f6SSughosh Ganu 8200ca180f6SSughosh Ganu spec->offset = entry->start; 8210ca180f6SSughosh Ganu spec->length = entry->length; 822dfbadfd9SNicolas Toromanoff break; 823dfbadfd9SNicolas Toromanoff #endif 824dfbadfd9SNicolas Toromanoff 825dfbadfd9SNicolas Toromanoff #if STM32MP_SPI_NOR 826b0ce4024SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: 827dfbadfd9SNicolas Toromanoff if (image_id == FWU_METADATA_IMAGE_ID) { 828dfbadfd9SNicolas Toromanoff spec->offset = STM32MP_NOR_METADATA1_OFFSET; 829dfbadfd9SNicolas Toromanoff } else { 830dfbadfd9SNicolas Toromanoff spec->offset = STM32MP_NOR_METADATA2_OFFSET; 831dfbadfd9SNicolas Toromanoff } 832dfbadfd9SNicolas Toromanoff 833dfbadfd9SNicolas Toromanoff spec->length = sizeof(struct fwu_metadata); 834dfbadfd9SNicolas Toromanoff break; 835dfbadfd9SNicolas Toromanoff #endif 836*795a559bSYann Gautier 837*795a559bSYann Gautier #if (STM32MP_RAW_NAND || STM32MP_SPI_NAND) 838*795a559bSYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: 839*795a559bSYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: 840*795a559bSYann Gautier if (image_id == FWU_METADATA_IMAGE_ID) { 841*795a559bSYann Gautier spec->offset = STM32MP_NAND_METADATA1_OFFSET; 842*795a559bSYann Gautier } else { 843*795a559bSYann Gautier spec->offset = STM32MP_NAND_METADATA2_OFFSET; 844*795a559bSYann Gautier } 845*795a559bSYann Gautier 846*795a559bSYann Gautier spec->length = sizeof(struct fwu_metadata); 847*795a559bSYann Gautier break; 848*795a559bSYann Gautier #endif 849dfbadfd9SNicolas Toromanoff default: 850dfbadfd9SNicolas Toromanoff panic(); 851dfbadfd9SNicolas Toromanoff break; 852dfbadfd9SNicolas Toromanoff } 8530ca180f6SSughosh Ganu 8540ca180f6SSughosh Ganu *image_spec = policy->image_spec; 8550ca180f6SSughosh Ganu *handle = *policy->dev_handle; 8560ca180f6SSughosh Ganu 8570ca180f6SSughosh Ganu return 0; 8580ca180f6SSughosh Ganu } 8590ca180f6SSughosh Ganu 8600ca180f6SSughosh Ganu int plat_fwu_set_metadata_image_source(unsigned int image_id, 8610ca180f6SSughosh Ganu uintptr_t *handle, 8620ca180f6SSughosh Ganu uintptr_t *image_spec) 8630ca180f6SSughosh Ganu { 8640ca180f6SSughosh Ganu assert((image_id == FWU_METADATA_IMAGE_ID) || 8650ca180f6SSughosh Ganu (image_id == BKUP_FWU_METADATA_IMAGE_ID)); 8660ca180f6SSughosh Ganu 867dfbadfd9SNicolas Toromanoff return plat_set_image_source(image_id, handle, image_spec); 8680ca180f6SSughosh Ganu } 869*795a559bSYann Gautier #endif /* PSA_FWU_SUPPORT */ 870