Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | ac22dd5 | 2021-03-22 14:22:14 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved. |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <arch_helpers.h> |
| 11 | #include <common/debug.h> |
Yann Gautier | a3bd8d1 | 2021-06-18 11:33:26 +0200 | [diff] [blame] | 12 | #include <common/desc_image_load.h> |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 13 | #include <drivers/fwu/fwu.h> |
| 14 | #include <drivers/fwu/fwu_metadata.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 15 | #include <drivers/io/io_block.h> |
| 16 | #include <drivers/io/io_driver.h> |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 17 | #include <drivers/io/io_fip.h> |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 18 | #include <drivers/io/io_memmap.h> |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 19 | #include <drivers/io/io_mtd.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 20 | #include <drivers/io/io_storage.h> |
| 21 | #include <drivers/mmc.h> |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 22 | #include <drivers/partition/efi.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 23 | #include <drivers/partition/partition.h> |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 24 | #include <drivers/raw_nand.h> |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 25 | #include <drivers/spi_nand.h> |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 26 | #include <drivers/spi_nor.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 27 | #include <drivers/st/io_mmc.h> |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 28 | #include <drivers/st/stm32_fmc2_nand.h> |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 29 | #include <drivers/st/stm32_qspi.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 30 | #include <drivers/st/stm32_sdmmc2.h> |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 31 | #include <drivers/usb_device.h> |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 32 | #include <lib/fconf/fconf.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 33 | #include <lib/mmio.h> |
| 34 | #include <lib/utils.h> |
| 35 | #include <plat/common/platform.h> |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 36 | #include <tools_share/firmware_image_package.h> |
| 37 | |
| 38 | #include <platform_def.h> |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 39 | #include <stm32cubeprogrammer.h> |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 40 | #include <stm32mp_fconf_getter.h> |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 41 | #include <usb_dfu.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 42 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 43 | /* IO devices */ |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 44 | uintptr_t fip_dev_handle; |
| 45 | uintptr_t storage_dev_handle; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 46 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 47 | static const io_dev_connector_t *fip_dev_con; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 48 | |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 49 | #if STM32MP_SDMMC || STM32MP_EMMC |
Yann Gautier | ac22dd5 | 2021-03-22 14:22:14 +0100 | [diff] [blame] | 50 | static struct mmc_device_info mmc_info; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 51 | |
Yann Gautier | f9af3bc | 2018-11-09 15:57:18 +0100 | [diff] [blame] | 52 | static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 53 | |
Yann Gautier | a3bd8d1 | 2021-06-18 11:33:26 +0200 | [diff] [blame] | 54 | static io_block_dev_spec_t mmc_block_dev_spec = { |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 55 | /* It's used as temp buffer in block driver */ |
| 56 | .buffer = { |
| 57 | .offset = (size_t)&block_buffer, |
| 58 | .length = MMC_BLOCK_SIZE, |
| 59 | }, |
| 60 | .ops = { |
| 61 | .read = mmc_read_blocks, |
| 62 | .write = NULL, |
| 63 | }, |
| 64 | .block_size = MMC_BLOCK_SIZE, |
| 65 | }; |
Vyacheslav Yurkov | e43a080 | 2021-06-04 10:10:51 +0200 | [diff] [blame] | 66 | |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 67 | static const io_dev_connector_t *mmc_dev_con; |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 68 | #endif /* STM32MP_SDMMC || STM32MP_EMMC */ |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 69 | |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 70 | #if STM32MP_SPI_NOR |
| 71 | static io_mtd_dev_spec_t spi_nor_dev_spec = { |
| 72 | .ops = { |
| 73 | .init = spi_nor_init, |
| 74 | .read = spi_nor_read, |
| 75 | }, |
| 76 | }; |
| 77 | #endif |
| 78 | |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 79 | #if STM32MP_RAW_NAND |
| 80 | static io_mtd_dev_spec_t nand_dev_spec = { |
| 81 | .ops = { |
| 82 | .init = nand_raw_init, |
| 83 | .read = nand_read, |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 84 | .seek = nand_seek_bb |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 85 | }, |
| 86 | }; |
| 87 | |
| 88 | static const io_dev_connector_t *nand_dev_con; |
| 89 | #endif |
| 90 | |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 91 | #if STM32MP_SPI_NAND |
| 92 | static io_mtd_dev_spec_t spi_nand_dev_spec = { |
| 93 | .ops = { |
| 94 | .init = spi_nand_init, |
| 95 | .read = nand_read, |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 96 | .seek = nand_seek_bb |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 97 | }, |
| 98 | }; |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 99 | #endif |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 100 | |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 101 | #if STM32MP_SPI_NAND || STM32MP_SPI_NOR |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 102 | static const io_dev_connector_t *spi_dev_con; |
| 103 | #endif |
| 104 | |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 105 | #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 106 | static const io_dev_connector_t *memmap_dev_con; |
| 107 | #endif |
| 108 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 109 | io_block_spec_t image_block_spec = { |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 110 | .offset = 0U, |
| 111 | .length = 0U, |
| 112 | }; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 113 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 114 | int open_fip(const uintptr_t spec) |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 115 | { |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 116 | return io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 117 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 118 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 119 | int open_storage(const uintptr_t spec) |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 120 | { |
| 121 | return io_dev_init(storage_dev_handle, 0); |
| 122 | } |
Vyacheslav Yurkov | e43a080 | 2021-06-04 10:10:51 +0200 | [diff] [blame] | 123 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 124 | static void print_boot_device(boot_api_context_t *boot_context) |
| 125 | { |
| 126 | switch (boot_context->boot_interface_selected) { |
| 127 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 128 | INFO("Using SDMMC\n"); |
| 129 | break; |
| 130 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
| 131 | INFO("Using EMMC\n"); |
| 132 | break; |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 133 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI: |
| 134 | INFO("Using QSPI NOR\n"); |
| 135 | break; |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 136 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: |
| 137 | INFO("Using FMC NAND\n"); |
| 138 | break; |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 139 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI: |
| 140 | INFO("Using SPI NAND\n"); |
| 141 | break; |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 142 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: |
| 143 | INFO("Using UART\n"); |
| 144 | break; |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 145 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: |
| 146 | INFO("Using USB\n"); |
| 147 | break; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 148 | default: |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 149 | ERROR("Boot interface %u not found\n", |
| 150 | boot_context->boot_interface_selected); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 151 | panic(); |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | if (boot_context->boot_interface_instance != 0U) { |
| 156 | INFO(" Instance %d\n", boot_context->boot_interface_instance); |
| 157 | } |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 160 | #if STM32MP_SDMMC || STM32MP_EMMC |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 161 | static void boot_mmc(enum mmc_device_type mmc_dev_type, |
| 162 | uint16_t boot_interface_instance) |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 163 | { |
| 164 | int io_result __unused; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 165 | struct stm32_sdmmc2_params params; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 166 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 167 | zeromem(¶ms, sizeof(struct stm32_sdmmc2_params)); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 168 | |
Yann Gautier | ac22dd5 | 2021-03-22 14:22:14 +0100 | [diff] [blame] | 169 | mmc_info.mmc_dev_type = mmc_dev_type; |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 170 | |
| 171 | switch (boot_interface_instance) { |
| 172 | case 1: |
| 173 | params.reg_base = STM32MP_SDMMC1_BASE; |
| 174 | break; |
| 175 | case 2: |
| 176 | params.reg_base = STM32MP_SDMMC2_BASE; |
| 177 | break; |
| 178 | case 3: |
| 179 | params.reg_base = STM32MP_SDMMC3_BASE; |
| 180 | break; |
| 181 | default: |
| 182 | WARN("SDMMC instance not found, using default\n"); |
| 183 | if (mmc_dev_type == MMC_IS_SD) { |
| 184 | params.reg_base = STM32MP_SDMMC1_BASE; |
| 185 | } else { |
| 186 | params.reg_base = STM32MP_SDMMC2_BASE; |
| 187 | } |
| 188 | break; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Yann Gautier | ac22dd5 | 2021-03-22 14:22:14 +0100 | [diff] [blame] | 191 | params.device_info = &mmc_info; |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 192 | if (stm32_sdmmc2_mmc_init(¶ms) != 0) { |
| 193 | ERROR("SDMMC%u init failed\n", boot_interface_instance); |
| 194 | panic(); |
| 195 | } |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 196 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 197 | /* Open MMC as a block device to read GPT table */ |
| 198 | io_result = register_io_dev_block(&mmc_dev_con); |
| 199 | if (io_result != 0) { |
| 200 | panic(); |
| 201 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 202 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 203 | io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec, |
| 204 | &storage_dev_handle); |
| 205 | assert(io_result == 0); |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 206 | } |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 207 | #endif /* STM32MP_SDMMC || STM32MP_EMMC */ |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 208 | |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 209 | #if STM32MP_SPI_NOR |
| 210 | static void boot_spi_nor(boot_api_context_t *boot_context) |
| 211 | { |
| 212 | int io_result __unused; |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 213 | |
| 214 | io_result = stm32_qspi_init(); |
| 215 | assert(io_result == 0); |
| 216 | |
| 217 | io_result = register_io_dev_mtd(&spi_dev_con); |
| 218 | assert(io_result == 0); |
| 219 | |
| 220 | /* Open connections to device */ |
| 221 | io_result = io_dev_open(spi_dev_con, |
| 222 | (uintptr_t)&spi_nor_dev_spec, |
| 223 | &storage_dev_handle); |
| 224 | assert(io_result == 0); |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 225 | } |
| 226 | #endif /* STM32MP_SPI_NOR */ |
| 227 | |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 228 | #if STM32MP_RAW_NAND |
| 229 | static void boot_fmc2_nand(boot_api_context_t *boot_context) |
| 230 | { |
| 231 | int io_result __unused; |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 232 | |
| 233 | io_result = stm32_fmc2_init(); |
| 234 | assert(io_result == 0); |
| 235 | |
| 236 | /* Register the IO device on this platform */ |
| 237 | io_result = register_io_dev_mtd(&nand_dev_con); |
| 238 | assert(io_result == 0); |
| 239 | |
| 240 | /* Open connections to device */ |
| 241 | io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec, |
| 242 | &storage_dev_handle); |
| 243 | assert(io_result == 0); |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 244 | } |
| 245 | #endif /* STM32MP_RAW_NAND */ |
| 246 | |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 247 | #if STM32MP_SPI_NAND |
| 248 | static void boot_spi_nand(boot_api_context_t *boot_context) |
| 249 | { |
| 250 | int io_result __unused; |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 251 | |
| 252 | io_result = stm32_qspi_init(); |
| 253 | assert(io_result == 0); |
| 254 | |
| 255 | io_result = register_io_dev_mtd(&spi_dev_con); |
| 256 | assert(io_result == 0); |
| 257 | |
| 258 | /* Open connections to device */ |
| 259 | io_result = io_dev_open(spi_dev_con, |
| 260 | (uintptr_t)&spi_nand_dev_spec, |
| 261 | &storage_dev_handle); |
| 262 | assert(io_result == 0); |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 263 | } |
| 264 | #endif /* STM32MP_SPI_NAND */ |
| 265 | |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 266 | #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 267 | static void mmap_io_setup(void) |
| 268 | { |
| 269 | int io_result __unused; |
| 270 | |
| 271 | io_result = register_io_dev_memmap(&memmap_dev_con); |
| 272 | assert(io_result == 0); |
| 273 | |
| 274 | io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL, |
| 275 | &storage_dev_handle); |
| 276 | assert(io_result == 0); |
| 277 | } |
| 278 | |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 279 | #if STM32MP_UART_PROGRAMMER |
| 280 | static void stm32cubeprogrammer_uart(void) |
| 281 | { |
| 282 | int ret __unused; |
| 283 | boot_api_context_t *boot_context = |
| 284 | (boot_api_context_t *)stm32mp_get_boot_ctx_address(); |
| 285 | uintptr_t uart_base; |
| 286 | |
| 287 | uart_base = get_uart_address(boot_context->boot_interface_instance); |
| 288 | ret = stm32cubeprog_uart_load(uart_base, DWL_BUFFER_BASE, DWL_BUFFER_SIZE); |
| 289 | assert(ret == 0); |
| 290 | } |
| 291 | #endif |
| 292 | |
| 293 | #if STM32MP_USB_PROGRAMMER |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 294 | static void stm32cubeprogrammer_usb(void) |
| 295 | { |
| 296 | int ret __unused; |
| 297 | struct usb_handle *pdev; |
| 298 | |
| 299 | /* Init USB on platform */ |
| 300 | pdev = usb_dfu_plat_init(); |
| 301 | |
| 302 | ret = stm32cubeprog_usb_load(pdev, DWL_BUFFER_BASE, DWL_BUFFER_SIZE); |
| 303 | assert(ret == 0); |
| 304 | } |
| 305 | #endif |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 306 | #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */ |
| 307 | |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 308 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 309 | void stm32mp_io_setup(void) |
| 310 | { |
| 311 | int io_result __unused; |
| 312 | boot_api_context_t *boot_context = |
| 313 | (boot_api_context_t *)stm32mp_get_boot_ctx_address(); |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 314 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 315 | print_boot_device(boot_context); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 316 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 317 | if ((boot_context->boot_partition_used_toboot == 1U) || |
| 318 | (boot_context->boot_partition_used_toboot == 2U)) { |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 319 | INFO("Boot used partition fsbl%u\n", |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 320 | boot_context->boot_partition_used_toboot); |
| 321 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 322 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 323 | io_result = register_io_dev_fip(&fip_dev_con); |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 324 | assert(io_result == 0); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 325 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 326 | io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL, |
| 327 | &fip_dev_handle); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 328 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 329 | switch (boot_context->boot_interface_selected) { |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 330 | #if STM32MP_SDMMC |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 331 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 332 | dmbsy(); |
| 333 | boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance); |
| 334 | break; |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 335 | #endif |
| 336 | #if STM32MP_EMMC |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 337 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
| 338 | dmbsy(); |
| 339 | boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 340 | break; |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 341 | #endif |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 342 | #if STM32MP_SPI_NOR |
| 343 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI: |
| 344 | dmbsy(); |
| 345 | boot_spi_nor(boot_context); |
| 346 | break; |
| 347 | #endif |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 348 | #if STM32MP_RAW_NAND |
| 349 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: |
| 350 | dmbsy(); |
| 351 | boot_fmc2_nand(boot_context); |
| 352 | break; |
| 353 | #endif |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 354 | #if STM32MP_SPI_NAND |
| 355 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI: |
| 356 | dmbsy(); |
| 357 | boot_spi_nand(boot_context); |
| 358 | break; |
| 359 | #endif |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 360 | #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER |
| 361 | #if STM32MP_UART_PROGRAMMER |
| 362 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: |
| 363 | #endif |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 364 | #if STM32MP_USB_PROGRAMMER |
| 365 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 366 | #endif |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 367 | dmbsy(); |
| 368 | mmap_io_setup(); |
| 369 | break; |
| 370 | #endif |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 371 | |
| 372 | default: |
| 373 | ERROR("Boot interface %d not supported\n", |
| 374 | boot_context->boot_interface_selected); |
Yann Gautier | 4c2b73d | 2021-06-30 17:04:22 +0200 | [diff] [blame] | 375 | panic(); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 376 | break; |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | int bl2_plat_handle_pre_image_load(unsigned int image_id) |
| 381 | { |
| 382 | static bool gpt_init_done __unused; |
| 383 | uint16_t boot_itf = stm32mp_get_boot_itf_selected(); |
| 384 | |
| 385 | switch (boot_itf) { |
| 386 | #if STM32MP_SDMMC || STM32MP_EMMC |
| 387 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 388 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
| 389 | if (!gpt_init_done) { |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 390 | /* |
| 391 | * With FWU Multi Bank feature enabled, the selection of |
| 392 | * the image to boot will be done by fwu_init calling the |
| 393 | * platform hook, plat_fwu_set_images_source. |
| 394 | */ |
| 395 | #if !PSA_FWU_SUPPORT |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 396 | const partition_entry_t *entry; |
| 397 | |
| 398 | partition_init(GPT_IMAGE_ID); |
| 399 | entry = get_partition_entry(FIP_IMAGE_NAME); |
| 400 | if (entry == NULL) { |
| 401 | ERROR("Could NOT find the %s partition!\n", |
| 402 | FIP_IMAGE_NAME); |
| 403 | return -ENOENT; |
| 404 | } |
| 405 | |
| 406 | image_block_spec.offset = entry->start; |
| 407 | image_block_spec.length = entry->length; |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 408 | #endif |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 409 | gpt_init_done = true; |
Yann Gautier | a3bd8d1 | 2021-06-18 11:33:26 +0200 | [diff] [blame] | 410 | } else { |
| 411 | bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); |
| 412 | |
| 413 | mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base; |
| 414 | mmc_block_dev_spec.buffer.length = bl_mem_params->image_info.image_max_size; |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | break; |
| 418 | #endif |
| 419 | |
| 420 | #if STM32MP_RAW_NAND || STM32MP_SPI_NAND |
| 421 | #if STM32MP_RAW_NAND |
| 422 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: |
| 423 | #endif |
| 424 | #if STM32MP_SPI_NAND |
| 425 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI: |
| 426 | #endif |
| 427 | image_block_spec.offset = STM32MP_NAND_FIP_OFFSET; |
| 428 | break; |
| 429 | #endif |
| 430 | |
| 431 | #if STM32MP_SPI_NOR |
| 432 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI: |
| 433 | image_block_spec.offset = STM32MP_NOR_FIP_OFFSET; |
| 434 | break; |
| 435 | #endif |
| 436 | |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 437 | #if STM32MP_UART_PROGRAMMER |
| 438 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: |
| 439 | if (image_id == FW_CONFIG_ID) { |
| 440 | stm32cubeprogrammer_uart(); |
| 441 | /* FIP loaded at DWL address */ |
| 442 | image_block_spec.offset = DWL_BUFFER_BASE; |
| 443 | image_block_spec.length = DWL_BUFFER_SIZE; |
| 444 | } |
| 445 | break; |
| 446 | #endif |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 447 | #if STM32MP_USB_PROGRAMMER |
| 448 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: |
| 449 | if (image_id == FW_CONFIG_ID) { |
| 450 | stm32cubeprogrammer_usb(); |
| 451 | /* FIP loaded at DWL address */ |
| 452 | image_block_spec.offset = DWL_BUFFER_BASE; |
| 453 | image_block_spec.length = DWL_BUFFER_SIZE; |
| 454 | } |
| 455 | break; |
| 456 | #endif |
| 457 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 458 | default: |
| 459 | ERROR("FIP Not found\n"); |
| 460 | panic(); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 461 | } |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 462 | |
| 463 | return 0; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Return an IO device handle and specification which can be used to access |
| 468 | * an image. Use this to enforce platform load policy. |
| 469 | */ |
| 470 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 471 | uintptr_t *image_spec) |
| 472 | { |
| 473 | int rc; |
| 474 | const struct plat_io_policy *policy; |
| 475 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 476 | policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 477 | rc = policy->check(policy->image_spec); |
| 478 | if (rc == 0) { |
| 479 | *image_spec = policy->image_spec; |
| 480 | *dev_handle = *(policy->dev_handle); |
| 481 | } |
| 482 | |
| 483 | return rc; |
| 484 | } |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 485 | |
| 486 | #if (STM32MP_SDMMC || STM32MP_EMMC) && PSA_FWU_SUPPORT |
| 487 | /* |
| 488 | * Eventually, this function will return the |
| 489 | * boot index to be passed on to the Update |
| 490 | * Agent after performing certain checks like |
| 491 | * a watchdog timeout, or Auth failure while |
| 492 | * trying to load from a certain bank. |
| 493 | * For now, since we do not have that logic |
| 494 | * implemented, just pass the active_index |
| 495 | * read from the metadata. |
| 496 | */ |
| 497 | uint32_t plat_fwu_get_boot_idx(void) |
| 498 | { |
| 499 | const struct fwu_metadata *metadata; |
| 500 | |
| 501 | metadata = fwu_get_metadata(); |
| 502 | |
| 503 | return metadata->active_index; |
| 504 | } |
| 505 | |
| 506 | static void *stm32_get_image_spec(const uuid_t *img_type_uuid) |
| 507 | { |
| 508 | unsigned int i; |
| 509 | |
| 510 | for (i = 0U; i < MAX_NUMBER_IDS; i++) { |
| 511 | if ((guidcmp(&policies[i].img_type_guid, img_type_uuid)) == 0) { |
| 512 | return (void *)policies[i].image_spec; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return NULL; |
| 517 | } |
| 518 | |
| 519 | void plat_fwu_set_images_source(const struct fwu_metadata *metadata) |
| 520 | { |
| 521 | unsigned int i; |
| 522 | uint32_t boot_idx; |
| 523 | const partition_entry_t *entry; |
| 524 | const uuid_t *img_type_uuid, *img_uuid; |
| 525 | io_block_spec_t *image_spec; |
| 526 | |
| 527 | boot_idx = plat_fwu_get_boot_idx(); |
| 528 | assert(boot_idx < NR_OF_FW_BANKS); |
| 529 | |
| 530 | for (i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) { |
| 531 | img_type_uuid = &metadata->img_entry[i].img_type_uuid; |
| 532 | image_spec = stm32_get_image_spec(img_type_uuid); |
| 533 | if (image_spec == NULL) { |
| 534 | ERROR("Unable to get image spec for the image in the metadata\n"); |
| 535 | panic(); |
| 536 | } |
| 537 | |
| 538 | img_uuid = |
| 539 | &metadata->img_entry[i].img_props[boot_idx].img_uuid; |
| 540 | |
| 541 | entry = get_partition_entry_by_uuid(img_uuid); |
| 542 | if (entry == NULL) { |
| 543 | ERROR("Unable to find the partition with the uuid mentioned in metadata\n"); |
| 544 | panic(); |
| 545 | } |
| 546 | |
| 547 | image_spec->offset = entry->start; |
| 548 | image_spec->length = entry->length; |
| 549 | } |
| 550 | } |
Sughosh Ganu | d1f8713 | 2021-12-01 16:46:34 +0530 | [diff] [blame] | 551 | |
| 552 | static int plat_set_image_source(unsigned int image_id, |
| 553 | uintptr_t *handle, |
| 554 | uintptr_t *image_spec, |
| 555 | const char *part_name) |
| 556 | { |
| 557 | struct plat_io_policy *policy; |
| 558 | io_block_spec_t *spec; |
| 559 | const partition_entry_t *entry = get_partition_entry(part_name); |
| 560 | |
| 561 | if (entry == NULL) { |
| 562 | ERROR("Unable to find the %s partition\n", part_name); |
| 563 | return -ENOENT; |
| 564 | } |
| 565 | |
| 566 | policy = &policies[image_id]; |
| 567 | |
| 568 | spec = (io_block_spec_t *)policy->image_spec; |
| 569 | spec->offset = entry->start; |
| 570 | spec->length = entry->length; |
| 571 | |
| 572 | *image_spec = policy->image_spec; |
| 573 | *handle = *policy->dev_handle; |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | int plat_fwu_set_metadata_image_source(unsigned int image_id, |
| 579 | uintptr_t *handle, |
| 580 | uintptr_t *image_spec) |
| 581 | { |
| 582 | char *part_name; |
| 583 | |
| 584 | assert((image_id == FWU_METADATA_IMAGE_ID) || |
| 585 | (image_id == BKUP_FWU_METADATA_IMAGE_ID)); |
| 586 | |
| 587 | partition_init(GPT_IMAGE_ID); |
| 588 | |
| 589 | if (image_id == FWU_METADATA_IMAGE_ID) { |
| 590 | part_name = METADATA_PART_1; |
| 591 | } else { |
| 592 | part_name = METADATA_PART_2; |
| 593 | } |
| 594 | |
| 595 | return plat_set_image_source(image_id, handle, image_spec, |
| 596 | part_name); |
| 597 | } |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 598 | #endif /* (STM32MP_SDMMC || STM32MP_EMMC) && PSA_FWU_SUPPORT */ |