1c9d75b3cSYann Gautier /* 2f87de907SNicolas Toromanoff * Copyright (c) 2015-2022, 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> 17*cd791164SLionel 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> 28c9d75b3cSYann Gautier #include <drivers/st/io_mmc.h> 2912e21dfdSLionel Debieve #include <drivers/st/stm32_fmc2_nand.h> 3057044228SLionel Debieve #include <drivers/st/stm32_qspi.h> 31c9d75b3cSYann Gautier #include <drivers/st/stm32_sdmmc2.h> 32fa92fef0SPatrick Delaunay #include <drivers/usb_device.h> 33d5a84eeaSYann Gautier #include <lib/fconf/fconf.h> 34c9d75b3cSYann Gautier #include <lib/mmio.h> 35c9d75b3cSYann Gautier #include <lib/utils.h> 36c9d75b3cSYann Gautier #include <plat/common/platform.h> 371d204ee4SYann Gautier #include <tools_share/firmware_image_package.h> 381d204ee4SYann Gautier 391d204ee4SYann Gautier #include <platform_def.h> 40fa92fef0SPatrick Delaunay #include <stm32cubeprogrammer.h> 411dab28f9SLionel Debieve #include <stm32mp_efi.h> 42d5a84eeaSYann Gautier #include <stm32mp_fconf_getter.h> 43b1391b29SYann Gautier #include <stm32mp_io_storage.h> 44fa92fef0SPatrick Delaunay #include <usb_dfu.h> 45c9d75b3cSYann Gautier 46c9d75b3cSYann Gautier /* IO devices */ 471d204ee4SYann Gautier uintptr_t fip_dev_handle; 481d204ee4SYann Gautier uintptr_t storage_dev_handle; 49c9d75b3cSYann Gautier 501d204ee4SYann Gautier static const io_dev_connector_t *fip_dev_con; 51c9d75b3cSYann Gautier 52*cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none 53*cd791164SLionel Debieve static const io_dev_connector_t *enc_dev_con; 54*cd791164SLionel Debieve uintptr_t enc_dev_handle; 55*cd791164SLionel Debieve #endif 56*cd791164SLionel Debieve 5746554b64SNicolas Le Bayon #if STM32MP_SDMMC || STM32MP_EMMC 58cddf1bd7SYann Gautier static struct mmc_device_info mmc_info; 59c9d75b3cSYann Gautier 60c9d75b3cSYann Gautier static uint32_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 127*cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none 128*cd791164SLionel Debieve int open_enc_fip(const uintptr_t spec) 129*cd791164SLionel Debieve { 130*cd791164SLionel Debieve int result; 131*cd791164SLionel Debieve uintptr_t local_image_handle; 132*cd791164SLionel Debieve 133*cd791164SLionel Debieve result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID); 134*cd791164SLionel Debieve if (result != 0) { 135*cd791164SLionel Debieve return result; 136*cd791164SLionel Debieve } 137*cd791164SLionel Debieve 138*cd791164SLionel Debieve result = io_open(enc_dev_handle, spec, &local_image_handle); 139*cd791164SLionel Debieve if (result != 0) { 140*cd791164SLionel Debieve return result; 141*cd791164SLionel Debieve } 142*cd791164SLionel Debieve 143*cd791164SLionel Debieve VERBOSE("Using encrypted FIP\n"); 144*cd791164SLionel Debieve io_close(local_image_handle); 145*cd791164SLionel Debieve 146*cd791164SLionel Debieve return 0; 147*cd791164SLionel Debieve } 148*cd791164SLionel Debieve #endif 149*cd791164SLionel 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; 195b1b218fbSLionel Debieve case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI: 196b1b218fbSLionel Debieve INFO("Using QSPI 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; 20157044228SLionel Debieve case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI: 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 { 226c9d75b3cSYann Gautier int io_result __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 { 298b1b218fbSLionel Debieve int io_result __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 31412e21dfdSLionel Debieve #if STM32MP_RAW_NAND 31512e21dfdSLionel Debieve static void boot_fmc2_nand(boot_api_context_t *boot_context) 31612e21dfdSLionel Debieve { 31712e21dfdSLionel Debieve int io_result __unused; 31812e21dfdSLionel Debieve 31912e21dfdSLionel Debieve io_result = stm32_fmc2_init(); 32012e21dfdSLionel Debieve assert(io_result == 0); 32112e21dfdSLionel Debieve 32212e21dfdSLionel Debieve /* Register the IO device on this platform */ 32312e21dfdSLionel Debieve io_result = register_io_dev_mtd(&nand_dev_con); 32412e21dfdSLionel Debieve assert(io_result == 0); 32512e21dfdSLionel Debieve 32612e21dfdSLionel Debieve /* Open connections to device */ 32712e21dfdSLionel Debieve io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec, 32812e21dfdSLionel Debieve &storage_dev_handle); 32912e21dfdSLionel Debieve assert(io_result == 0); 33012e21dfdSLionel Debieve } 33112e21dfdSLionel Debieve #endif /* STM32MP_RAW_NAND */ 33212e21dfdSLionel Debieve 33357044228SLionel Debieve #if STM32MP_SPI_NAND 33457044228SLionel Debieve static void boot_spi_nand(boot_api_context_t *boot_context) 33557044228SLionel Debieve { 33657044228SLionel Debieve int io_result __unused; 33757044228SLionel Debieve 33857044228SLionel Debieve io_result = stm32_qspi_init(); 33957044228SLionel Debieve assert(io_result == 0); 34057044228SLionel Debieve 34157044228SLionel Debieve io_result = register_io_dev_mtd(&spi_dev_con); 34257044228SLionel Debieve assert(io_result == 0); 34357044228SLionel Debieve 34457044228SLionel Debieve /* Open connections to device */ 34557044228SLionel Debieve io_result = io_dev_open(spi_dev_con, 34657044228SLionel Debieve (uintptr_t)&spi_nand_dev_spec, 34757044228SLionel Debieve &storage_dev_handle); 34857044228SLionel Debieve assert(io_result == 0); 34957044228SLionel Debieve } 35057044228SLionel Debieve #endif /* STM32MP_SPI_NAND */ 35157044228SLionel Debieve 3529083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER 353fa92fef0SPatrick Delaunay static void mmap_io_setup(void) 354fa92fef0SPatrick Delaunay { 355fa92fef0SPatrick Delaunay int io_result __unused; 356fa92fef0SPatrick Delaunay 357fa92fef0SPatrick Delaunay io_result = register_io_dev_memmap(&memmap_dev_con); 358fa92fef0SPatrick Delaunay assert(io_result == 0); 359fa92fef0SPatrick Delaunay 360fa92fef0SPatrick Delaunay io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL, 361fa92fef0SPatrick Delaunay &storage_dev_handle); 362fa92fef0SPatrick Delaunay assert(io_result == 0); 363fa92fef0SPatrick Delaunay } 364fa92fef0SPatrick Delaunay 3659083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER 3669083fa11SPatrick Delaunay static void stm32cubeprogrammer_uart(void) 3679083fa11SPatrick Delaunay { 3689083fa11SPatrick Delaunay int ret __unused; 3699083fa11SPatrick Delaunay boot_api_context_t *boot_context = 3709083fa11SPatrick Delaunay (boot_api_context_t *)stm32mp_get_boot_ctx_address(); 3719083fa11SPatrick Delaunay uintptr_t uart_base; 3729083fa11SPatrick Delaunay 3739083fa11SPatrick Delaunay uart_base = get_uart_address(boot_context->boot_interface_instance); 3749083fa11SPatrick Delaunay ret = stm32cubeprog_uart_load(uart_base, DWL_BUFFER_BASE, DWL_BUFFER_SIZE); 3759083fa11SPatrick Delaunay assert(ret == 0); 3769083fa11SPatrick Delaunay } 3779083fa11SPatrick Delaunay #endif 3789083fa11SPatrick Delaunay 3799083fa11SPatrick Delaunay #if STM32MP_USB_PROGRAMMER 380fa92fef0SPatrick Delaunay static void stm32cubeprogrammer_usb(void) 381fa92fef0SPatrick Delaunay { 382fa92fef0SPatrick Delaunay int ret __unused; 383fa92fef0SPatrick Delaunay struct usb_handle *pdev; 384fa92fef0SPatrick Delaunay 385fa92fef0SPatrick Delaunay /* Init USB on platform */ 386fa92fef0SPatrick Delaunay pdev = usb_dfu_plat_init(); 387fa92fef0SPatrick Delaunay 388fa92fef0SPatrick Delaunay ret = stm32cubeprog_usb_load(pdev, DWL_BUFFER_BASE, DWL_BUFFER_SIZE); 389fa92fef0SPatrick Delaunay assert(ret == 0); 390fa92fef0SPatrick Delaunay } 391fa92fef0SPatrick Delaunay #endif 3929083fa11SPatrick Delaunay #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */ 3939083fa11SPatrick Delaunay 394fa92fef0SPatrick Delaunay 3950b1aa772SYann Gautier void stm32mp_io_setup(void) 3960b1aa772SYann Gautier { 3970b1aa772SYann Gautier int io_result __unused; 3980b1aa772SYann Gautier boot_api_context_t *boot_context = 3990b1aa772SYann Gautier (boot_api_context_t *)stm32mp_get_boot_ctx_address(); 4000b1aa772SYann Gautier 4010b1aa772SYann Gautier print_boot_device(boot_context); 4020b1aa772SYann Gautier 4030b1aa772SYann Gautier if ((boot_context->boot_partition_used_toboot == 1U) || 4040b1aa772SYann Gautier (boot_context->boot_partition_used_toboot == 2U)) { 4051d204ee4SYann Gautier INFO("Boot used partition fsbl%u\n", 4060b1aa772SYann Gautier boot_context->boot_partition_used_toboot); 4070b1aa772SYann Gautier } 4080b1aa772SYann Gautier 4091d204ee4SYann Gautier io_result = register_io_dev_fip(&fip_dev_con); 4100b1aa772SYann Gautier assert(io_result == 0); 4110b1aa772SYann Gautier 4121d204ee4SYann Gautier io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL, 4131d204ee4SYann Gautier &fip_dev_handle); 4140b1aa772SYann Gautier 415*cd791164SLionel Debieve #ifndef DECRYPTION_SUPPORT_none 416*cd791164SLionel Debieve io_result = register_io_dev_enc(&enc_dev_con); 417*cd791164SLionel Debieve assert(io_result == 0); 418*cd791164SLionel Debieve 419*cd791164SLionel Debieve io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL, 420*cd791164SLionel Debieve &enc_dev_handle); 421*cd791164SLionel Debieve assert(io_result == 0); 422*cd791164SLionel Debieve #endif 423*cd791164SLionel Debieve 4240b1aa772SYann Gautier switch (boot_context->boot_interface_selected) { 42546554b64SNicolas Le Bayon #if STM32MP_SDMMC 4260b1aa772SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: 4270b1aa772SYann Gautier dmbsy(); 4280b1aa772SYann Gautier boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance); 4290b1aa772SYann Gautier break; 43046554b64SNicolas Le Bayon #endif 43146554b64SNicolas Le Bayon #if STM32MP_EMMC 4320b1aa772SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: 4330b1aa772SYann Gautier dmbsy(); 4340b1aa772SYann Gautier boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance); 435c9d75b3cSYann Gautier break; 43646554b64SNicolas Le Bayon #endif 437b1b218fbSLionel Debieve #if STM32MP_SPI_NOR 438b1b218fbSLionel Debieve case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI: 439b1b218fbSLionel Debieve dmbsy(); 440b1b218fbSLionel Debieve boot_spi_nor(boot_context); 441b1b218fbSLionel Debieve break; 442b1b218fbSLionel Debieve #endif 44312e21dfdSLionel Debieve #if STM32MP_RAW_NAND 44412e21dfdSLionel Debieve case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: 44512e21dfdSLionel Debieve dmbsy(); 44612e21dfdSLionel Debieve boot_fmc2_nand(boot_context); 44712e21dfdSLionel Debieve break; 44812e21dfdSLionel Debieve #endif 44957044228SLionel Debieve #if STM32MP_SPI_NAND 45057044228SLionel Debieve case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI: 45157044228SLionel Debieve dmbsy(); 45257044228SLionel Debieve boot_spi_nand(boot_context); 45357044228SLionel Debieve break; 45457044228SLionel Debieve #endif 4559083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER 4569083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER 4579083fa11SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: 4589083fa11SPatrick Delaunay #endif 459fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER 460fa92fef0SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: 4619083fa11SPatrick Delaunay #endif 462fa92fef0SPatrick Delaunay dmbsy(); 463fa92fef0SPatrick Delaunay mmap_io_setup(); 464fa92fef0SPatrick Delaunay break; 465fa92fef0SPatrick Delaunay #endif 466c9d75b3cSYann Gautier 467c9d75b3cSYann Gautier default: 468c9d75b3cSYann Gautier ERROR("Boot interface %d not supported\n", 469c9d75b3cSYann Gautier boot_context->boot_interface_selected); 47071693a66SYann Gautier panic(); 471c9d75b3cSYann Gautier break; 472c9d75b3cSYann Gautier } 473c9d75b3cSYann Gautier } 474c9d75b3cSYann Gautier 4751d204ee4SYann Gautier int bl2_plat_handle_pre_image_load(unsigned int image_id) 4761d204ee4SYann Gautier { 4771d204ee4SYann Gautier static bool gpt_init_done __unused; 4781d204ee4SYann Gautier uint16_t boot_itf = stm32mp_get_boot_itf_selected(); 4791d204ee4SYann Gautier 4801d204ee4SYann Gautier switch (boot_itf) { 4811d204ee4SYann Gautier #if STM32MP_SDMMC || STM32MP_EMMC 4821d204ee4SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: 48395e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT 48495e4908eSAhmad Fatoum if (image_block_spec.offset == STM32MP_EMMC_BOOT_FIP_OFFSET) { 48595e4908eSAhmad Fatoum break; 48695e4908eSAhmad Fatoum } 48795e4908eSAhmad Fatoum #endif 48895e4908eSAhmad Fatoum /* fallthrough */ 48995e4908eSAhmad Fatoum case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: 4901d204ee4SYann Gautier if (!gpt_init_done) { 4918dd75531SSughosh Ganu /* 4928dd75531SSughosh Ganu * With FWU Multi Bank feature enabled, the selection of 4938dd75531SSughosh Ganu * the image to boot will be done by fwu_init calling the 4948dd75531SSughosh Ganu * platform hook, plat_fwu_set_images_source. 4958dd75531SSughosh Ganu */ 4968dd75531SSughosh Ganu #if !PSA_FWU_SUPPORT 4971d204ee4SYann Gautier const partition_entry_t *entry; 4981dab28f9SLionel Debieve const struct efi_guid img_type_guid = STM32MP_FIP_GUID; 4991dab28f9SLionel Debieve uuid_t img_type_uuid; 5001d204ee4SYann Gautier 5011dab28f9SLionel Debieve guidcpy(&img_type_uuid, &img_type_guid); 5021d204ee4SYann Gautier partition_init(GPT_IMAGE_ID); 5031dab28f9SLionel Debieve entry = get_partition_entry_by_type(&img_type_uuid); 5041dab28f9SLionel Debieve if (entry == NULL) { 5051d204ee4SYann Gautier entry = get_partition_entry(FIP_IMAGE_NAME); 5061d204ee4SYann Gautier if (entry == NULL) { 5071d204ee4SYann Gautier ERROR("Could NOT find the %s partition!\n", 5081d204ee4SYann Gautier FIP_IMAGE_NAME); 5091dab28f9SLionel Debieve 5101d204ee4SYann Gautier return -ENOENT; 5111d204ee4SYann Gautier } 5121dab28f9SLionel Debieve } 5131d204ee4SYann Gautier 5141d204ee4SYann Gautier image_block_spec.offset = entry->start; 5151d204ee4SYann Gautier image_block_spec.length = entry->length; 5168dd75531SSughosh Ganu #endif 5171d204ee4SYann Gautier gpt_init_done = true; 51818b415beSYann Gautier } else { 51918b415beSYann Gautier bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); 5202deff904SYann Gautier assert(bl_mem_params != NULL); 52118b415beSYann Gautier 52218b415beSYann Gautier mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base; 52318b415beSYann Gautier mmc_block_dev_spec.buffer.length = bl_mem_params->image_info.image_max_size; 5241d204ee4SYann Gautier } 5251d204ee4SYann Gautier 5261d204ee4SYann Gautier break; 5271d204ee4SYann Gautier #endif 5281d204ee4SYann Gautier 5291d204ee4SYann Gautier #if STM32MP_RAW_NAND || STM32MP_SPI_NAND 5301d204ee4SYann Gautier #if STM32MP_RAW_NAND 5311d204ee4SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: 5321d204ee4SYann Gautier #endif 5331d204ee4SYann Gautier #if STM32MP_SPI_NAND 5341d204ee4SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI: 5351d204ee4SYann Gautier #endif 5361d204ee4SYann Gautier image_block_spec.offset = STM32MP_NAND_FIP_OFFSET; 5371d204ee4SYann Gautier break; 5381d204ee4SYann Gautier #endif 5391d204ee4SYann Gautier 5401d204ee4SYann Gautier #if STM32MP_SPI_NOR 5411d204ee4SYann Gautier case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI: 5421d204ee4SYann Gautier image_block_spec.offset = STM32MP_NOR_FIP_OFFSET; 5431d204ee4SYann Gautier break; 5441d204ee4SYann Gautier #endif 5451d204ee4SYann Gautier 5469083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER 5479083fa11SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: 5489083fa11SPatrick Delaunay if (image_id == FW_CONFIG_ID) { 5499083fa11SPatrick Delaunay stm32cubeprogrammer_uart(); 5509083fa11SPatrick Delaunay /* FIP loaded at DWL address */ 5519083fa11SPatrick Delaunay image_block_spec.offset = DWL_BUFFER_BASE; 5529083fa11SPatrick Delaunay image_block_spec.length = DWL_BUFFER_SIZE; 5539083fa11SPatrick Delaunay } 5549083fa11SPatrick Delaunay break; 5559083fa11SPatrick Delaunay #endif 556fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER 557fa92fef0SPatrick Delaunay case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: 558fa92fef0SPatrick Delaunay if (image_id == FW_CONFIG_ID) { 559fa92fef0SPatrick Delaunay stm32cubeprogrammer_usb(); 560fa92fef0SPatrick Delaunay /* FIP loaded at DWL address */ 561fa92fef0SPatrick Delaunay image_block_spec.offset = DWL_BUFFER_BASE; 562fa92fef0SPatrick Delaunay image_block_spec.length = DWL_BUFFER_SIZE; 563fa92fef0SPatrick Delaunay } 564fa92fef0SPatrick Delaunay break; 565fa92fef0SPatrick Delaunay #endif 566fa92fef0SPatrick Delaunay 5671d204ee4SYann Gautier default: 5681d204ee4SYann Gautier ERROR("FIP Not found\n"); 5691d204ee4SYann Gautier panic(); 5701d204ee4SYann Gautier } 5711d204ee4SYann Gautier 5721d204ee4SYann Gautier return 0; 5731d204ee4SYann Gautier } 5741d204ee4SYann Gautier 575c9d75b3cSYann Gautier /* 576c9d75b3cSYann Gautier * Return an IO device handle and specification which can be used to access 577c9d75b3cSYann Gautier * an image. Use this to enforce platform load policy. 578c9d75b3cSYann Gautier */ 579c9d75b3cSYann Gautier int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, 580c9d75b3cSYann Gautier uintptr_t *image_spec) 581c9d75b3cSYann Gautier { 582c9d75b3cSYann Gautier int rc; 583c9d75b3cSYann Gautier const struct plat_io_policy *policy; 584c9d75b3cSYann Gautier 585d5a84eeaSYann Gautier policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id); 586c9d75b3cSYann Gautier rc = policy->check(policy->image_spec); 587c9d75b3cSYann Gautier if (rc == 0) { 588c9d75b3cSYann Gautier *image_spec = policy->image_spec; 589c9d75b3cSYann Gautier *dev_handle = *(policy->dev_handle); 590c9d75b3cSYann Gautier } 591c9d75b3cSYann Gautier 592c9d75b3cSYann Gautier return rc; 593c9d75b3cSYann Gautier } 5948dd75531SSughosh Ganu 5958dd75531SSughosh Ganu #if (STM32MP_SDMMC || STM32MP_EMMC) && PSA_FWU_SUPPORT 5968dd75531SSughosh Ganu /* 597f87de907SNicolas Toromanoff * In each boot in non-trial mode, we set the BKP register to 598f87de907SNicolas Toromanoff * FWU_MAX_TRIAL_REBOOT, and return the active_index from metadata. 599f87de907SNicolas Toromanoff * 600f87de907SNicolas Toromanoff * As long as the update agent didn't update the "accepted" field in metadata 601f87de907SNicolas Toromanoff * (i.e. we are in trial mode), we select the new active_index. 602f87de907SNicolas Toromanoff * To avoid infinite boot loop at trial boot we decrement a BKP register. 603f87de907SNicolas Toromanoff * If this counter is 0: 604f87de907SNicolas Toromanoff * - an unexpected TAMPER event raised (that resets the BKP registers to 0) 605f87de907SNicolas Toromanoff * - a power-off occurs before the update agent was able to update the 606f87de907SNicolas Toromanoff * "accepted' field 607f87de907SNicolas Toromanoff * - we already boot FWU_MAX_TRIAL_REBOOT times in trial mode. 608f87de907SNicolas Toromanoff * we select the previous_active_index. 6098dd75531SSughosh Ganu */ 610f87de907SNicolas Toromanoff #define INVALID_BOOT_IDX 0xFFFFFFFF 611f87de907SNicolas Toromanoff 6128dd75531SSughosh Ganu uint32_t plat_fwu_get_boot_idx(void) 6138dd75531SSughosh Ganu { 614f87de907SNicolas Toromanoff /* 615f87de907SNicolas Toromanoff * Select boot index and update boot counter only once per boot 616f87de907SNicolas Toromanoff * even if this function is called several times. 617f87de907SNicolas Toromanoff */ 618f87de907SNicolas Toromanoff static uint32_t boot_idx = INVALID_BOOT_IDX; 619f87de907SNicolas Toromanoff const struct fwu_metadata *data; 6208dd75531SSughosh Ganu 621f87de907SNicolas Toromanoff data = fwu_get_metadata(); 6228dd75531SSughosh Ganu 623f87de907SNicolas Toromanoff if (boot_idx == INVALID_BOOT_IDX) { 624f87de907SNicolas Toromanoff boot_idx = data->active_index; 625f87de907SNicolas Toromanoff if (fwu_is_trial_run_state()) { 626f87de907SNicolas Toromanoff if (stm32_get_and_dec_fwu_trial_boot_cnt() == 0U) { 627f87de907SNicolas Toromanoff WARN("Trial FWU fails %u times\n", 628f87de907SNicolas Toromanoff FWU_MAX_TRIAL_REBOOT); 629f87de907SNicolas Toromanoff boot_idx = data->previous_active_index; 630f87de907SNicolas Toromanoff } 631f87de907SNicolas Toromanoff } else { 632f87de907SNicolas Toromanoff stm32_set_max_fwu_trial_boot_cnt(); 633f87de907SNicolas Toromanoff } 634f87de907SNicolas Toromanoff } 635f87de907SNicolas Toromanoff 636f87de907SNicolas Toromanoff return boot_idx; 6378dd75531SSughosh Ganu } 6388dd75531SSughosh Ganu 6398dd75531SSughosh Ganu static void *stm32_get_image_spec(const uuid_t *img_type_uuid) 6408dd75531SSughosh Ganu { 6418dd75531SSughosh Ganu unsigned int i; 6428dd75531SSughosh Ganu 6438dd75531SSughosh Ganu for (i = 0U; i < MAX_NUMBER_IDS; i++) { 6448dd75531SSughosh Ganu if ((guidcmp(&policies[i].img_type_guid, img_type_uuid)) == 0) { 6458dd75531SSughosh Ganu return (void *)policies[i].image_spec; 6468dd75531SSughosh Ganu } 6478dd75531SSughosh Ganu } 6488dd75531SSughosh Ganu 6498dd75531SSughosh Ganu return NULL; 6508dd75531SSughosh Ganu } 6518dd75531SSughosh Ganu 6528dd75531SSughosh Ganu void plat_fwu_set_images_source(const struct fwu_metadata *metadata) 6538dd75531SSughosh Ganu { 6548dd75531SSughosh Ganu unsigned int i; 6558dd75531SSughosh Ganu uint32_t boot_idx; 6568dd75531SSughosh Ganu const partition_entry_t *entry; 6578dd75531SSughosh Ganu const uuid_t *img_type_uuid, *img_uuid; 6588dd75531SSughosh Ganu io_block_spec_t *image_spec; 6598dd75531SSughosh Ganu 6608dd75531SSughosh Ganu boot_idx = plat_fwu_get_boot_idx(); 6618dd75531SSughosh Ganu assert(boot_idx < NR_OF_FW_BANKS); 6628dd75531SSughosh Ganu 6638dd75531SSughosh Ganu for (i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) { 6648dd75531SSughosh Ganu img_type_uuid = &metadata->img_entry[i].img_type_uuid; 6658dd75531SSughosh Ganu image_spec = stm32_get_image_spec(img_type_uuid); 6668dd75531SSughosh Ganu if (image_spec == NULL) { 6678dd75531SSughosh Ganu ERROR("Unable to get image spec for the image in the metadata\n"); 6688dd75531SSughosh Ganu panic(); 6698dd75531SSughosh Ganu } 6708dd75531SSughosh Ganu 6718dd75531SSughosh Ganu img_uuid = 6728dd75531SSughosh Ganu &metadata->img_entry[i].img_props[boot_idx].img_uuid; 6738dd75531SSughosh Ganu 6748dd75531SSughosh Ganu entry = get_partition_entry_by_uuid(img_uuid); 6758dd75531SSughosh Ganu if (entry == NULL) { 6768dd75531SSughosh Ganu ERROR("Unable to find the partition with the uuid mentioned in metadata\n"); 6778dd75531SSughosh Ganu panic(); 6788dd75531SSughosh Ganu } 6798dd75531SSughosh Ganu 6808dd75531SSughosh Ganu image_spec->offset = entry->start; 6818dd75531SSughosh Ganu image_spec->length = entry->length; 6828dd75531SSughosh Ganu } 6838dd75531SSughosh Ganu } 6840ca180f6SSughosh Ganu 6850ca180f6SSughosh Ganu static int plat_set_image_source(unsigned int image_id, 6860ca180f6SSughosh Ganu uintptr_t *handle, 6870ca180f6SSughosh Ganu uintptr_t *image_spec, 6880ca180f6SSughosh Ganu const char *part_name) 6890ca180f6SSughosh Ganu { 6900ca180f6SSughosh Ganu struct plat_io_policy *policy; 6910ca180f6SSughosh Ganu io_block_spec_t *spec; 6920ca180f6SSughosh Ganu const partition_entry_t *entry = get_partition_entry(part_name); 6930ca180f6SSughosh Ganu 6940ca180f6SSughosh Ganu if (entry == NULL) { 6950ca180f6SSughosh Ganu ERROR("Unable to find the %s partition\n", part_name); 6960ca180f6SSughosh Ganu return -ENOENT; 6970ca180f6SSughosh Ganu } 6980ca180f6SSughosh Ganu 6990ca180f6SSughosh Ganu policy = &policies[image_id]; 7000ca180f6SSughosh Ganu 7010ca180f6SSughosh Ganu spec = (io_block_spec_t *)policy->image_spec; 7020ca180f6SSughosh Ganu spec->offset = entry->start; 7030ca180f6SSughosh Ganu spec->length = entry->length; 7040ca180f6SSughosh Ganu 7050ca180f6SSughosh Ganu *image_spec = policy->image_spec; 7060ca180f6SSughosh Ganu *handle = *policy->dev_handle; 7070ca180f6SSughosh Ganu 7080ca180f6SSughosh Ganu return 0; 7090ca180f6SSughosh Ganu } 7100ca180f6SSughosh Ganu 7110ca180f6SSughosh Ganu int plat_fwu_set_metadata_image_source(unsigned int image_id, 7120ca180f6SSughosh Ganu uintptr_t *handle, 7130ca180f6SSughosh Ganu uintptr_t *image_spec) 7140ca180f6SSughosh Ganu { 7150ca180f6SSughosh Ganu char *part_name; 7160ca180f6SSughosh Ganu 7170ca180f6SSughosh Ganu assert((image_id == FWU_METADATA_IMAGE_ID) || 7180ca180f6SSughosh Ganu (image_id == BKUP_FWU_METADATA_IMAGE_ID)); 7190ca180f6SSughosh Ganu 7200ca180f6SSughosh Ganu partition_init(GPT_IMAGE_ID); 7210ca180f6SSughosh Ganu 7220ca180f6SSughosh Ganu if (image_id == FWU_METADATA_IMAGE_ID) { 7230ca180f6SSughosh Ganu part_name = METADATA_PART_1; 7240ca180f6SSughosh Ganu } else { 7250ca180f6SSughosh Ganu part_name = METADATA_PART_2; 7260ca180f6SSughosh Ganu } 7270ca180f6SSughosh Ganu 7280ca180f6SSughosh Ganu return plat_set_image_source(image_id, handle, image_spec, 7290ca180f6SSughosh Ganu part_name); 7300ca180f6SSughosh Ganu } 7318dd75531SSughosh Ganu #endif /* (STM32MP_SDMMC || STM32MP_EMMC) && PSA_FWU_SUPPORT */ 732