Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | d6b75ea | 2024-01-05 11:45:31 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2024, 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> |
Lionel Debieve | 5adcd50 | 2022-10-05 16:51:12 +0200 | [diff] [blame] | 17 | #include <drivers/io/io_encrypted.h> |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 18 | #include <drivers/io/io_fip.h> |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 19 | #include <drivers/io/io_memmap.h> |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 20 | #include <drivers/io/io_mtd.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 21 | #include <drivers/io/io_storage.h> |
| 22 | #include <drivers/mmc.h> |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 23 | #include <drivers/partition/efi.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 24 | #include <drivers/partition/partition.h> |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 25 | #include <drivers/raw_nand.h> |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 26 | #include <drivers/spi_nand.h> |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 27 | #include <drivers/spi_nor.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> |
Lionel Debieve | 5e111c5 | 2022-02-24 18:58:46 +0100 | [diff] [blame] | 40 | #include <stm32mp_efi.h> |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 41 | #include <stm32mp_fconf_getter.h> |
Yann Gautier | 8636a5f | 2022-05-06 15:27:32 +0200 | [diff] [blame] | 42 | #include <stm32mp_io_storage.h> |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 43 | #include <usb_dfu.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 44 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 45 | /* IO devices */ |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 46 | uintptr_t fip_dev_handle; |
| 47 | uintptr_t storage_dev_handle; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 48 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 49 | static const io_dev_connector_t *fip_dev_con; |
Yann Gautier | d6b75ea | 2024-01-05 11:45:31 +0100 | [diff] [blame] | 50 | static uint32_t nand_block_sz __maybe_unused; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 51 | |
Lionel Debieve | 5adcd50 | 2022-10-05 16:51:12 +0200 | [diff] [blame] | 52 | #ifndef DECRYPTION_SUPPORT_none |
| 53 | static const io_dev_connector_t *enc_dev_con; |
| 54 | uintptr_t enc_dev_handle; |
| 55 | #endif |
| 56 | |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 57 | #if STM32MP_SDMMC || STM32MP_EMMC |
Yann Gautier | ac22dd5 | 2021-03-22 14:22:14 +0100 | [diff] [blame] | 58 | static struct mmc_device_info mmc_info; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 59 | |
Yann Gautier | 438c4c6 | 2023-08-17 11:44:07 +0200 | [diff] [blame] | 60 | static uint8_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 61 | |
Yann Gautier | a3bd8d1 | 2021-06-18 11:33:26 +0200 | [diff] [blame] | 62 | static io_block_dev_spec_t mmc_block_dev_spec = { |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 63 | /* It's used as temp buffer in block driver */ |
| 64 | .buffer = { |
| 65 | .offset = (size_t)&block_buffer, |
| 66 | .length = MMC_BLOCK_SIZE, |
| 67 | }, |
| 68 | .ops = { |
| 69 | .read = mmc_read_blocks, |
| 70 | .write = NULL, |
| 71 | }, |
| 72 | .block_size = MMC_BLOCK_SIZE, |
| 73 | }; |
Vyacheslav Yurkov | e43a080 | 2021-06-04 10:10:51 +0200 | [diff] [blame] | 74 | |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 75 | static const io_dev_connector_t *mmc_dev_con; |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 76 | #endif /* STM32MP_SDMMC || STM32MP_EMMC */ |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 77 | |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 78 | #if STM32MP_SPI_NOR |
| 79 | static io_mtd_dev_spec_t spi_nor_dev_spec = { |
| 80 | .ops = { |
| 81 | .init = spi_nor_init, |
| 82 | .read = spi_nor_read, |
| 83 | }, |
| 84 | }; |
| 85 | #endif |
| 86 | |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 87 | #if STM32MP_RAW_NAND |
| 88 | static io_mtd_dev_spec_t nand_dev_spec = { |
| 89 | .ops = { |
| 90 | .init = nand_raw_init, |
| 91 | .read = nand_read, |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 92 | .seek = nand_seek_bb |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 93 | }, |
| 94 | }; |
| 95 | |
| 96 | static const io_dev_connector_t *nand_dev_con; |
| 97 | #endif |
| 98 | |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 99 | #if STM32MP_SPI_NAND |
| 100 | static io_mtd_dev_spec_t spi_nand_dev_spec = { |
| 101 | .ops = { |
| 102 | .init = spi_nand_init, |
| 103 | .read = nand_read, |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 104 | .seek = nand_seek_bb |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 105 | }, |
| 106 | }; |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 107 | #endif |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 108 | |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 109 | #if STM32MP_SPI_NAND || STM32MP_SPI_NOR |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 110 | static const io_dev_connector_t *spi_dev_con; |
| 111 | #endif |
| 112 | |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 113 | #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 114 | static const io_dev_connector_t *memmap_dev_con; |
| 115 | #endif |
| 116 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 117 | io_block_spec_t image_block_spec = { |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 118 | .offset = 0U, |
| 119 | .length = 0U, |
| 120 | }; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 121 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 122 | int open_fip(const uintptr_t spec) |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 123 | { |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 124 | return io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 125 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 126 | |
Lionel Debieve | 5adcd50 | 2022-10-05 16:51:12 +0200 | [diff] [blame] | 127 | #ifndef DECRYPTION_SUPPORT_none |
| 128 | int open_enc_fip(const uintptr_t spec) |
| 129 | { |
| 130 | int result; |
| 131 | uintptr_t local_image_handle; |
| 132 | |
| 133 | result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID); |
| 134 | if (result != 0) { |
| 135 | return result; |
| 136 | } |
| 137 | |
| 138 | result = io_open(enc_dev_handle, spec, &local_image_handle); |
| 139 | if (result != 0) { |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | VERBOSE("Using encrypted FIP\n"); |
| 144 | io_close(local_image_handle); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | #endif |
| 149 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 150 | int open_storage(const uintptr_t spec) |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 151 | { |
| 152 | return io_dev_init(storage_dev_handle, 0); |
| 153 | } |
Vyacheslav Yurkov | e43a080 | 2021-06-04 10:10:51 +0200 | [diff] [blame] | 154 | |
Ahmad Fatoum | bd68528 | 2022-05-19 07:42:33 +0200 | [diff] [blame] | 155 | #if STM32MP_EMMC_BOOT |
| 156 | static uint32_t get_boot_part_fip_header(void) |
| 157 | { |
| 158 | io_block_spec_t emmc_boot_fip_block_spec = { |
| 159 | .offset = STM32MP_EMMC_BOOT_FIP_OFFSET, |
| 160 | .length = MMC_BLOCK_SIZE, /* We are interested only in first 4 bytes */ |
| 161 | }; |
| 162 | uint32_t magic = 0U; |
| 163 | int io_result; |
| 164 | size_t bytes_read; |
| 165 | uintptr_t fip_hdr_handle; |
| 166 | |
| 167 | io_result = io_open(storage_dev_handle, (uintptr_t)&emmc_boot_fip_block_spec, |
| 168 | &fip_hdr_handle); |
| 169 | assert(io_result == 0); |
| 170 | |
| 171 | io_result = io_read(fip_hdr_handle, (uintptr_t)&magic, sizeof(magic), |
| 172 | &bytes_read); |
| 173 | if ((io_result != 0) || (bytes_read != sizeof(magic))) { |
| 174 | panic(); |
| 175 | } |
| 176 | |
| 177 | io_close(fip_hdr_handle); |
| 178 | |
| 179 | VERBOSE("%s: eMMC boot magic at offset 256K: %08x\n", |
| 180 | __func__, magic); |
| 181 | |
| 182 | return magic; |
| 183 | } |
| 184 | #endif |
| 185 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 186 | static void print_boot_device(boot_api_context_t *boot_context) |
| 187 | { |
| 188 | switch (boot_context->boot_interface_selected) { |
| 189 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 190 | INFO("Using SDMMC\n"); |
| 191 | break; |
| 192 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
| 193 | INFO("Using EMMC\n"); |
| 194 | break; |
Yann Gautier | cdb0ec9 | 2020-08-31 15:00:19 +0200 | [diff] [blame] | 195 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: |
| 196 | INFO("Using SPI NOR\n"); |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 197 | break; |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 198 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: |
| 199 | INFO("Using FMC NAND\n"); |
| 200 | break; |
Yann Gautier | cdb0ec9 | 2020-08-31 15:00:19 +0200 | [diff] [blame] | 201 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 202 | INFO("Using SPI NAND\n"); |
| 203 | break; |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 204 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: |
| 205 | INFO("Using UART\n"); |
| 206 | break; |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 207 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: |
| 208 | INFO("Using USB\n"); |
| 209 | break; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 210 | default: |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 211 | ERROR("Boot interface %u not found\n", |
| 212 | boot_context->boot_interface_selected); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 213 | panic(); |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | if (boot_context->boot_interface_instance != 0U) { |
| 218 | INFO(" Instance %d\n", boot_context->boot_interface_instance); |
| 219 | } |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 222 | #if STM32MP_SDMMC || STM32MP_EMMC |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 223 | static void boot_mmc(enum mmc_device_type mmc_dev_type, |
| 224 | uint16_t boot_interface_instance) |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 225 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 226 | int io_result __maybe_unused; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 227 | struct stm32_sdmmc2_params params; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 228 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 229 | zeromem(¶ms, sizeof(struct stm32_sdmmc2_params)); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 230 | |
Yann Gautier | ac22dd5 | 2021-03-22 14:22:14 +0100 | [diff] [blame] | 231 | mmc_info.mmc_dev_type = mmc_dev_type; |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 232 | |
| 233 | switch (boot_interface_instance) { |
| 234 | case 1: |
| 235 | params.reg_base = STM32MP_SDMMC1_BASE; |
| 236 | break; |
| 237 | case 2: |
| 238 | params.reg_base = STM32MP_SDMMC2_BASE; |
| 239 | break; |
| 240 | case 3: |
| 241 | params.reg_base = STM32MP_SDMMC3_BASE; |
| 242 | break; |
| 243 | default: |
| 244 | WARN("SDMMC instance not found, using default\n"); |
| 245 | if (mmc_dev_type == MMC_IS_SD) { |
| 246 | params.reg_base = STM32MP_SDMMC1_BASE; |
| 247 | } else { |
| 248 | params.reg_base = STM32MP_SDMMC2_BASE; |
| 249 | } |
| 250 | break; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Yann Gautier | b218faa | 2019-08-14 16:44:48 +0200 | [diff] [blame] | 253 | if (mmc_dev_type != MMC_IS_EMMC) { |
| 254 | params.flags = MMC_FLAG_SD_CMD6; |
| 255 | } |
| 256 | |
Yann Gautier | ac22dd5 | 2021-03-22 14:22:14 +0100 | [diff] [blame] | 257 | params.device_info = &mmc_info; |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 258 | if (stm32_sdmmc2_mmc_init(¶ms) != 0) { |
| 259 | ERROR("SDMMC%u init failed\n", boot_interface_instance); |
| 260 | panic(); |
| 261 | } |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 262 | |
Ahmad Fatoum | bd68528 | 2022-05-19 07:42:33 +0200 | [diff] [blame] | 263 | /* Open MMC as a block device to read FIP */ |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 264 | io_result = register_io_dev_block(&mmc_dev_con); |
| 265 | if (io_result != 0) { |
| 266 | panic(); |
| 267 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 268 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 269 | io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec, |
| 270 | &storage_dev_handle); |
| 271 | assert(io_result == 0); |
Ahmad Fatoum | bd68528 | 2022-05-19 07:42:33 +0200 | [diff] [blame] | 272 | |
| 273 | #if STM32MP_EMMC_BOOT |
| 274 | if (mmc_dev_type == MMC_IS_EMMC) { |
| 275 | io_result = mmc_part_switch_current_boot(); |
| 276 | assert(io_result == 0); |
| 277 | |
| 278 | if (get_boot_part_fip_header() != TOC_HEADER_NAME) { |
| 279 | WARN("%s: Can't find FIP header on eMMC boot partition. Trying GPT\n", |
| 280 | __func__); |
| 281 | io_result = mmc_part_switch_user(); |
| 282 | assert(io_result == 0); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | VERBOSE("%s: FIP header found on eMMC boot partition\n", |
| 287 | __func__); |
| 288 | image_block_spec.offset = STM32MP_EMMC_BOOT_FIP_OFFSET; |
Yann Gautier | 637cd9e | 2022-09-02 08:36:40 +0200 | [diff] [blame] | 289 | image_block_spec.length = mmc_boot_part_size() - STM32MP_EMMC_BOOT_FIP_OFFSET; |
Ahmad Fatoum | bd68528 | 2022-05-19 07:42:33 +0200 | [diff] [blame] | 290 | } |
| 291 | #endif |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 292 | } |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 293 | #endif /* STM32MP_SDMMC || STM32MP_EMMC */ |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 294 | |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 295 | #if STM32MP_SPI_NOR |
| 296 | static void boot_spi_nor(boot_api_context_t *boot_context) |
| 297 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 298 | int io_result __maybe_unused; |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 299 | |
| 300 | io_result = stm32_qspi_init(); |
| 301 | assert(io_result == 0); |
| 302 | |
| 303 | io_result = register_io_dev_mtd(&spi_dev_con); |
| 304 | assert(io_result == 0); |
| 305 | |
| 306 | /* Open connections to device */ |
| 307 | io_result = io_dev_open(spi_dev_con, |
| 308 | (uintptr_t)&spi_nor_dev_spec, |
| 309 | &storage_dev_handle); |
| 310 | assert(io_result == 0); |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 311 | } |
| 312 | #endif /* STM32MP_SPI_NOR */ |
| 313 | |
Yann Gautier | d6b75ea | 2024-01-05 11:45:31 +0100 | [diff] [blame] | 314 | #if STM32MP_RAW_NAND || STM32MP_SPI_NAND |
| 315 | /* |
| 316 | * This function returns 0 if it can find an alternate |
| 317 | * image to be loaded or a negative errno otherwise. |
| 318 | */ |
| 319 | static int try_nand_backup_partitions(unsigned int image_id) |
| 320 | { |
| 321 | static unsigned int backup_id; |
| 322 | static unsigned int backup_block_nb; |
| 323 | |
| 324 | /* Check if NAND storage used */ |
| 325 | if (nand_block_sz == 0U) { |
| 326 | return -ENODEV; |
| 327 | } |
| 328 | |
| 329 | if (backup_id != image_id) { |
| 330 | backup_block_nb = PLATFORM_MTD_MAX_PART_SIZE / nand_block_sz; |
| 331 | backup_id = image_id; |
| 332 | } |
| 333 | |
| 334 | if (backup_block_nb-- == 0U) { |
| 335 | return -ENOSPC; |
| 336 | } |
| 337 | |
Yann Gautier | 723133b | 2023-05-31 17:30:01 +0200 | [diff] [blame^] | 338 | #if PSA_FWU_SUPPORT |
| 339 | if (((image_block_spec.offset < STM32MP_NAND_FIP_B_OFFSET) && |
| 340 | ((image_block_spec.offset + nand_block_sz) >= STM32MP_NAND_FIP_B_OFFSET)) || |
| 341 | (image_block_spec.offset + nand_block_sz >= STM32MP_NAND_FIP_B_MAX_OFFSET)) { |
| 342 | return 0; |
| 343 | } |
| 344 | #endif |
| 345 | |
Yann Gautier | d6b75ea | 2024-01-05 11:45:31 +0100 | [diff] [blame] | 346 | image_block_spec.offset += nand_block_sz; |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static const struct plat_try_images_ops try_img_ops = { |
| 352 | .next_instance = try_nand_backup_partitions, |
| 353 | }; |
| 354 | #endif /* STM32MP_RAW_NAND || STM32MP_SPI_NAND */ |
| 355 | |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 356 | #if STM32MP_RAW_NAND |
| 357 | static void boot_fmc2_nand(boot_api_context_t *boot_context) |
| 358 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 359 | int io_result __maybe_unused; |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 360 | |
Yann Gautier | d6b75ea | 2024-01-05 11:45:31 +0100 | [diff] [blame] | 361 | plat_setup_try_img_ops(&try_img_ops); |
| 362 | |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 363 | io_result = stm32_fmc2_init(); |
| 364 | assert(io_result == 0); |
| 365 | |
| 366 | /* Register the IO device on this platform */ |
| 367 | io_result = register_io_dev_mtd(&nand_dev_con); |
| 368 | assert(io_result == 0); |
| 369 | |
| 370 | /* Open connections to device */ |
| 371 | io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec, |
| 372 | &storage_dev_handle); |
| 373 | assert(io_result == 0); |
Yann Gautier | d6b75ea | 2024-01-05 11:45:31 +0100 | [diff] [blame] | 374 | |
| 375 | nand_block_sz = nand_dev_spec.erase_size; |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 376 | } |
| 377 | #endif /* STM32MP_RAW_NAND */ |
| 378 | |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 379 | #if STM32MP_SPI_NAND |
| 380 | static void boot_spi_nand(boot_api_context_t *boot_context) |
| 381 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 382 | int io_result __maybe_unused; |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 383 | |
Yann Gautier | d6b75ea | 2024-01-05 11:45:31 +0100 | [diff] [blame] | 384 | plat_setup_try_img_ops(&try_img_ops); |
| 385 | |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 386 | io_result = stm32_qspi_init(); |
| 387 | assert(io_result == 0); |
| 388 | |
| 389 | io_result = register_io_dev_mtd(&spi_dev_con); |
| 390 | assert(io_result == 0); |
| 391 | |
| 392 | /* Open connections to device */ |
| 393 | io_result = io_dev_open(spi_dev_con, |
| 394 | (uintptr_t)&spi_nand_dev_spec, |
| 395 | &storage_dev_handle); |
| 396 | assert(io_result == 0); |
Yann Gautier | d6b75ea | 2024-01-05 11:45:31 +0100 | [diff] [blame] | 397 | |
| 398 | nand_block_sz = spi_nand_dev_spec.erase_size; |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 399 | } |
| 400 | #endif /* STM32MP_SPI_NAND */ |
| 401 | |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 402 | #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 403 | static void mmap_io_setup(void) |
| 404 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 405 | int io_result __maybe_unused; |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 406 | |
| 407 | io_result = register_io_dev_memmap(&memmap_dev_con); |
| 408 | assert(io_result == 0); |
| 409 | |
| 410 | io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL, |
| 411 | &storage_dev_handle); |
| 412 | assert(io_result == 0); |
| 413 | } |
| 414 | |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 415 | #if STM32MP_UART_PROGRAMMER |
| 416 | static void stm32cubeprogrammer_uart(void) |
| 417 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 418 | int ret __maybe_unused; |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 419 | boot_api_context_t *boot_context = |
| 420 | (boot_api_context_t *)stm32mp_get_boot_ctx_address(); |
| 421 | uintptr_t uart_base; |
| 422 | |
| 423 | uart_base = get_uart_address(boot_context->boot_interface_instance); |
| 424 | ret = stm32cubeprog_uart_load(uart_base, DWL_BUFFER_BASE, DWL_BUFFER_SIZE); |
| 425 | assert(ret == 0); |
| 426 | } |
| 427 | #endif |
| 428 | |
| 429 | #if STM32MP_USB_PROGRAMMER |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 430 | static void stm32cubeprogrammer_usb(void) |
| 431 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 432 | int ret __maybe_unused; |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 433 | struct usb_handle *pdev; |
| 434 | |
| 435 | /* Init USB on platform */ |
| 436 | pdev = usb_dfu_plat_init(); |
| 437 | |
| 438 | ret = stm32cubeprog_usb_load(pdev, DWL_BUFFER_BASE, DWL_BUFFER_SIZE); |
| 439 | assert(ret == 0); |
| 440 | } |
| 441 | #endif |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 442 | #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */ |
| 443 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 444 | void stm32mp_io_setup(void) |
| 445 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 446 | int io_result __maybe_unused; |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 447 | boot_api_context_t *boot_context = |
| 448 | (boot_api_context_t *)stm32mp_get_boot_ctx_address(); |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 449 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 450 | print_boot_device(boot_context); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 451 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 452 | if ((boot_context->boot_partition_used_toboot == 1U) || |
| 453 | (boot_context->boot_partition_used_toboot == 2U)) { |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 454 | INFO("Boot used partition fsbl%u\n", |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 455 | boot_context->boot_partition_used_toboot); |
| 456 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 457 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 458 | io_result = register_io_dev_fip(&fip_dev_con); |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 459 | assert(io_result == 0); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 460 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 461 | io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL, |
| 462 | &fip_dev_handle); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 463 | |
Lionel Debieve | 5adcd50 | 2022-10-05 16:51:12 +0200 | [diff] [blame] | 464 | #ifndef DECRYPTION_SUPPORT_none |
| 465 | io_result = register_io_dev_enc(&enc_dev_con); |
| 466 | assert(io_result == 0); |
| 467 | |
| 468 | io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL, |
| 469 | &enc_dev_handle); |
| 470 | assert(io_result == 0); |
| 471 | #endif |
| 472 | |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 473 | switch (boot_context->boot_interface_selected) { |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 474 | #if STM32MP_SDMMC |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 475 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 476 | dmbsy(); |
| 477 | boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance); |
| 478 | break; |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 479 | #endif |
| 480 | #if STM32MP_EMMC |
Yann Gautier | eae3fbf | 2019-04-23 13:34:03 +0200 | [diff] [blame] | 481 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
| 482 | dmbsy(); |
| 483 | boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 484 | break; |
Nicolas Le Bayon | 3f42cad | 2019-09-03 09:52:05 +0200 | [diff] [blame] | 485 | #endif |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 486 | #if STM32MP_SPI_NOR |
Yann Gautier | cdb0ec9 | 2020-08-31 15:00:19 +0200 | [diff] [blame] | 487 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: |
Lionel Debieve | cb0dbc4 | 2019-09-25 09:11:31 +0200 | [diff] [blame] | 488 | dmbsy(); |
| 489 | boot_spi_nor(boot_context); |
| 490 | break; |
| 491 | #endif |
Lionel Debieve | 402a46b | 2019-11-04 12:28:15 +0100 | [diff] [blame] | 492 | #if STM32MP_RAW_NAND |
| 493 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: |
| 494 | dmbsy(); |
| 495 | boot_fmc2_nand(boot_context); |
| 496 | break; |
| 497 | #endif |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 498 | #if STM32MP_SPI_NAND |
Yann Gautier | cdb0ec9 | 2020-08-31 15:00:19 +0200 | [diff] [blame] | 499 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: |
Lionel Debieve | 186b046 | 2019-09-24 18:30:12 +0200 | [diff] [blame] | 500 | dmbsy(); |
| 501 | boot_spi_nand(boot_context); |
| 502 | break; |
| 503 | #endif |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 504 | #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER |
| 505 | #if STM32MP_UART_PROGRAMMER |
| 506 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: |
| 507 | #endif |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 508 | #if STM32MP_USB_PROGRAMMER |
| 509 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 510 | #endif |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 511 | dmbsy(); |
| 512 | mmap_io_setup(); |
| 513 | break; |
| 514 | #endif |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 515 | |
| 516 | default: |
| 517 | ERROR("Boot interface %d not supported\n", |
| 518 | boot_context->boot_interface_selected); |
Yann Gautier | 4c2b73d | 2021-06-30 17:04:22 +0200 | [diff] [blame] | 519 | panic(); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 520 | break; |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | int bl2_plat_handle_pre_image_load(unsigned int image_id) |
| 525 | { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 526 | static bool gpt_init_done __maybe_unused; |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 527 | uint16_t boot_itf = stm32mp_get_boot_itf_selected(); |
| 528 | |
| 529 | switch (boot_itf) { |
| 530 | #if STM32MP_SDMMC || STM32MP_EMMC |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 531 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
Ahmad Fatoum | bd68528 | 2022-05-19 07:42:33 +0200 | [diff] [blame] | 532 | #if STM32MP_EMMC_BOOT |
| 533 | if (image_block_spec.offset == STM32MP_EMMC_BOOT_FIP_OFFSET) { |
| 534 | break; |
| 535 | } |
| 536 | #endif |
| 537 | /* fallthrough */ |
| 538 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 539 | if (!gpt_init_done) { |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 540 | /* |
| 541 | * With FWU Multi Bank feature enabled, the selection of |
| 542 | * the image to boot will be done by fwu_init calling the |
| 543 | * platform hook, plat_fwu_set_images_source. |
| 544 | */ |
| 545 | #if !PSA_FWU_SUPPORT |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 546 | const partition_entry_t *entry; |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 547 | const struct efi_guid fip_guid = STM32MP_FIP_GUID; |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 548 | |
| 549 | partition_init(GPT_IMAGE_ID); |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 550 | entry = get_partition_entry_by_type(&fip_guid); |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 551 | if (entry == NULL) { |
Lionel Debieve | 5e111c5 | 2022-02-24 18:58:46 +0100 | [diff] [blame] | 552 | entry = get_partition_entry(FIP_IMAGE_NAME); |
| 553 | if (entry == NULL) { |
| 554 | ERROR("Could NOT find the %s partition!\n", |
| 555 | FIP_IMAGE_NAME); |
| 556 | |
| 557 | return -ENOENT; |
| 558 | } |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | image_block_spec.offset = entry->start; |
| 562 | image_block_spec.length = entry->length; |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 563 | #endif |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 564 | gpt_init_done = true; |
Yann Gautier | a3bd8d1 | 2021-06-18 11:33:26 +0200 | [diff] [blame] | 565 | } else { |
| 566 | bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 567 | |
Yann Gautier | c6f77b0 | 2022-05-06 09:50:43 +0200 | [diff] [blame] | 568 | assert(bl_mem_params != NULL); |
Yann Gautier | a3bd8d1 | 2021-06-18 11:33:26 +0200 | [diff] [blame] | 569 | |
| 570 | mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base; |
| 571 | 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] | 572 | } |
| 573 | |
| 574 | break; |
| 575 | #endif |
| 576 | |
| 577 | #if STM32MP_RAW_NAND || STM32MP_SPI_NAND |
| 578 | #if STM32MP_RAW_NAND |
| 579 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: |
| 580 | #endif |
| 581 | #if STM32MP_SPI_NAND |
Yann Gautier | cdb0ec9 | 2020-08-31 15:00:19 +0200 | [diff] [blame] | 582 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 583 | #endif |
Yann Gautier | 723133b | 2023-05-31 17:30:01 +0200 | [diff] [blame^] | 584 | /* |
| 585 | * With FWU Multi Bank feature enabled, the selection of |
| 586 | * the image to boot will be done by fwu_init calling the |
| 587 | * platform hook, plat_fwu_set_images_source. |
| 588 | */ |
| 589 | #if !PSA_FWU_SUPPORT |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 590 | image_block_spec.offset = STM32MP_NAND_FIP_OFFSET; |
Yann Gautier | 723133b | 2023-05-31 17:30:01 +0200 | [diff] [blame^] | 591 | #endif |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 592 | break; |
| 593 | #endif |
| 594 | |
| 595 | #if STM32MP_SPI_NOR |
Yann Gautier | cdb0ec9 | 2020-08-31 15:00:19 +0200 | [diff] [blame] | 596 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 597 | /* |
| 598 | * With FWU Multi Bank feature enabled, the selection of |
| 599 | * the image to boot will be done by fwu_init calling the |
| 600 | * platform hook, plat_fwu_set_images_source. |
| 601 | */ |
| 602 | #if !PSA_FWU_SUPPORT |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 603 | image_block_spec.offset = STM32MP_NOR_FIP_OFFSET; |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 604 | #endif |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 605 | break; |
| 606 | #endif |
| 607 | |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 608 | #if STM32MP_UART_PROGRAMMER |
| 609 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART: |
| 610 | if (image_id == FW_CONFIG_ID) { |
| 611 | stm32cubeprogrammer_uart(); |
| 612 | /* FIP loaded at DWL address */ |
| 613 | image_block_spec.offset = DWL_BUFFER_BASE; |
| 614 | image_block_spec.length = DWL_BUFFER_SIZE; |
| 615 | } |
| 616 | break; |
| 617 | #endif |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 618 | #if STM32MP_USB_PROGRAMMER |
| 619 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB: |
| 620 | if (image_id == FW_CONFIG_ID) { |
| 621 | stm32cubeprogrammer_usb(); |
| 622 | /* FIP loaded at DWL address */ |
| 623 | image_block_spec.offset = DWL_BUFFER_BASE; |
| 624 | image_block_spec.length = DWL_BUFFER_SIZE; |
| 625 | } |
| 626 | break; |
| 627 | #endif |
| 628 | |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 629 | default: |
| 630 | ERROR("FIP Not found\n"); |
| 631 | panic(); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 632 | } |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 633 | |
| 634 | return 0; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Return an IO device handle and specification which can be used to access |
| 639 | * an image. Use this to enforce platform load policy. |
| 640 | */ |
| 641 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 642 | uintptr_t *image_spec) |
| 643 | { |
| 644 | int rc; |
| 645 | const struct plat_io_policy *policy; |
| 646 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 647 | policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 648 | rc = policy->check(policy->image_spec); |
| 649 | if (rc == 0) { |
| 650 | *image_spec = policy->image_spec; |
| 651 | *dev_handle = *(policy->dev_handle); |
| 652 | } |
| 653 | |
| 654 | return rc; |
| 655 | } |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 656 | |
Yann Gautier | 723133b | 2023-05-31 17:30:01 +0200 | [diff] [blame^] | 657 | #if PSA_FWU_SUPPORT |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 658 | /* |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 659 | * In each boot in non-trial mode, we set the BKP register to |
| 660 | * FWU_MAX_TRIAL_REBOOT, and return the active_index from metadata. |
| 661 | * |
| 662 | * As long as the update agent didn't update the "accepted" field in metadata |
| 663 | * (i.e. we are in trial mode), we select the new active_index. |
| 664 | * To avoid infinite boot loop at trial boot we decrement a BKP register. |
| 665 | * If this counter is 0: |
| 666 | * - an unexpected TAMPER event raised (that resets the BKP registers to 0) |
| 667 | * - a power-off occurs before the update agent was able to update the |
| 668 | * "accepted' field |
| 669 | * - we already boot FWU_MAX_TRIAL_REBOOT times in trial mode. |
| 670 | * we select the previous_active_index. |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 671 | */ |
| 672 | uint32_t plat_fwu_get_boot_idx(void) |
| 673 | { |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 674 | /* |
| 675 | * Select boot index and update boot counter only once per boot |
| 676 | * even if this function is called several times. |
| 677 | */ |
| 678 | static uint32_t boot_idx = INVALID_BOOT_IDX; |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 679 | |
| 680 | if (boot_idx == INVALID_BOOT_IDX) { |
Sughosh Ganu | da28e4c | 2024-02-20 14:20:41 +0530 | [diff] [blame] | 681 | const struct fwu_metadata *data = fwu_get_metadata(); |
| 682 | |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 683 | boot_idx = data->active_index; |
Sughosh Ganu | da28e4c | 2024-02-20 14:20:41 +0530 | [diff] [blame] | 684 | |
Sughosh Ganu | 63576f0 | 2024-02-01 16:56:27 +0530 | [diff] [blame] | 685 | if (data->bank_state[boot_idx] == FWU_BANK_STATE_VALID) { |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 686 | if (stm32_get_and_dec_fwu_trial_boot_cnt() == 0U) { |
| 687 | WARN("Trial FWU fails %u times\n", |
| 688 | FWU_MAX_TRIAL_REBOOT); |
Sughosh Ganu | da28e4c | 2024-02-20 14:20:41 +0530 | [diff] [blame] | 689 | boot_idx = fwu_get_alternate_boot_bank(); |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 690 | } |
Sughosh Ganu | da28e4c | 2024-02-20 14:20:41 +0530 | [diff] [blame] | 691 | } else if (data->bank_state[boot_idx] == |
| 692 | FWU_BANK_STATE_ACCEPTED) { |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 693 | stm32_set_max_fwu_trial_boot_cnt(); |
Sughosh Ganu | da28e4c | 2024-02-20 14:20:41 +0530 | [diff] [blame] | 694 | } else { |
| 695 | ERROR("The active bank(%u) of the platform is in Invalid State.\n", |
| 696 | boot_idx); |
| 697 | boot_idx = fwu_get_alternate_boot_bank(); |
| 698 | stm32_clear_fwu_trial_boot_cnt(); |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 699 | } |
| 700 | } |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 701 | |
Nicolas Toromanoff | 5a937cd | 2022-02-07 10:12:04 +0100 | [diff] [blame] | 702 | return boot_idx; |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 703 | } |
| 704 | |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 705 | static void *stm32_get_image_spec(const struct efi_guid *img_type_guid) |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 706 | { |
| 707 | unsigned int i; |
| 708 | |
| 709 | for (i = 0U; i < MAX_NUMBER_IDS; i++) { |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 710 | if ((guidcmp(&policies[i].img_type_guid, img_type_guid)) == 0) { |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 711 | return (void *)policies[i].image_spec; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | return NULL; |
| 716 | } |
| 717 | |
| 718 | void plat_fwu_set_images_source(const struct fwu_metadata *metadata) |
| 719 | { |
| 720 | unsigned int i; |
| 721 | uint32_t boot_idx; |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 722 | const partition_entry_t *entry __maybe_unused; |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 723 | const struct fwu_image_entry *img_entry; |
| 724 | const void *img_type_guid; |
| 725 | const void *img_guid; |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 726 | io_block_spec_t *image_spec; |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 727 | const uint16_t boot_itf = stm32mp_get_boot_itf_selected(); |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 728 | |
| 729 | boot_idx = plat_fwu_get_boot_idx(); |
| 730 | assert(boot_idx < NR_OF_FW_BANKS); |
Sughosh Ganu | da28e4c | 2024-02-20 14:20:41 +0530 | [diff] [blame] | 731 | VERBOSE("Selecting to boot from bank %u\n", boot_idx); |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 732 | |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 733 | img_entry = (void *)&metadata->fw_desc.img_entry; |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 734 | for (i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) { |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 735 | img_type_guid = &img_entry[i].img_type_guid; |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 736 | |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 737 | img_guid = &img_entry[i].img_bank_info[boot_idx].img_guid; |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 738 | |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 739 | image_spec = stm32_get_image_spec(img_type_guid); |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 740 | if (image_spec == NULL) { |
| 741 | ERROR("Unable to get image spec for the image in the metadata\n"); |
| 742 | panic(); |
| 743 | } |
| 744 | |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 745 | switch (boot_itf) { |
| 746 | #if (STM32MP_SDMMC || STM32MP_EMMC) |
| 747 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 748 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 749 | entry = get_partition_entry_by_guid(img_guid); |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 750 | if (entry == NULL) { |
| 751 | ERROR("No partition with the uuid mentioned in metadata\n"); |
| 752 | panic(); |
| 753 | } |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 754 | |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 755 | image_spec->offset = entry->start; |
| 756 | image_spec->length = entry->length; |
| 757 | break; |
| 758 | #endif |
| 759 | #if STM32MP_SPI_NOR |
Yann Gautier | cdb0ec9 | 2020-08-31 15:00:19 +0200 | [diff] [blame] | 760 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 761 | if (guidcmp(img_guid, &STM32MP_NOR_FIP_A_GUID) == 0) { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 762 | image_spec->offset = STM32MP_NOR_FIP_A_OFFSET; |
Sughosh Ganu | 52794a3 | 2024-02-02 15:35:18 +0530 | [diff] [blame] | 763 | } else if (guidcmp(img_guid, &STM32MP_NOR_FIP_B_GUID) == 0) { |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 764 | image_spec->offset = STM32MP_NOR_FIP_B_OFFSET; |
| 765 | } else { |
| 766 | ERROR("Invalid uuid mentioned in metadata\n"); |
| 767 | panic(); |
| 768 | } |
| 769 | break; |
| 770 | #endif |
Yann Gautier | 723133b | 2023-05-31 17:30:01 +0200 | [diff] [blame^] | 771 | #if (STM32MP_RAW_NAND || STM32MP_SPI_NAND) |
| 772 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: |
| 773 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: |
| 774 | if (guidcmp(img_guid, &STM32MP_NAND_FIP_A_GUID) == 0) { |
| 775 | image_spec->offset = STM32MP_NAND_FIP_A_OFFSET; |
| 776 | } else if (guidcmp(img_guid, &STM32MP_NAND_FIP_B_GUID) == 0) { |
| 777 | image_spec->offset = STM32MP_NAND_FIP_B_OFFSET; |
| 778 | } else { |
| 779 | ERROR("Invalid uuid mentioned in metadata\n"); |
| 780 | panic(); |
| 781 | } |
| 782 | break; |
| 783 | #endif |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 784 | default: |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 785 | panic(); |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 786 | break; |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 787 | } |
Sughosh Ganu | b721f8a | 2021-12-01 16:45:11 +0530 | [diff] [blame] | 788 | } |
| 789 | } |
Sughosh Ganu | d1f8713 | 2021-12-01 16:46:34 +0530 | [diff] [blame] | 790 | |
| 791 | static int plat_set_image_source(unsigned int image_id, |
| 792 | uintptr_t *handle, |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 793 | uintptr_t *image_spec) |
Sughosh Ganu | d1f8713 | 2021-12-01 16:46:34 +0530 | [diff] [blame] | 794 | { |
| 795 | struct plat_io_policy *policy; |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 796 | io_block_spec_t *spec __maybe_unused; |
| 797 | const partition_entry_t *entry __maybe_unused; |
| 798 | const uint16_t boot_itf = stm32mp_get_boot_itf_selected(); |
Sughosh Ganu | d1f8713 | 2021-12-01 16:46:34 +0530 | [diff] [blame] | 799 | |
| 800 | policy = &policies[image_id]; |
Sughosh Ganu | d1f8713 | 2021-12-01 16:46:34 +0530 | [diff] [blame] | 801 | spec = (io_block_spec_t *)policy->image_spec; |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 802 | |
| 803 | switch (boot_itf) { |
| 804 | #if (STM32MP_SDMMC || STM32MP_EMMC) |
| 805 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 806 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
| 807 | partition_init(GPT_IMAGE_ID); |
| 808 | |
| 809 | if (image_id == FWU_METADATA_IMAGE_ID) { |
| 810 | entry = get_partition_entry(METADATA_PART_1); |
| 811 | } else { |
| 812 | entry = get_partition_entry(METADATA_PART_2); |
| 813 | } |
| 814 | |
| 815 | if (entry == NULL) { |
| 816 | ERROR("Unable to find a metadata partition\n"); |
| 817 | return -ENOENT; |
| 818 | } |
| 819 | |
| 820 | spec->offset = entry->start; |
| 821 | spec->length = entry->length; |
| 822 | break; |
| 823 | #endif |
| 824 | |
| 825 | #if STM32MP_SPI_NOR |
Yann Gautier | cdb0ec9 | 2020-08-31 15:00:19 +0200 | [diff] [blame] | 826 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI: |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 827 | if (image_id == FWU_METADATA_IMAGE_ID) { |
| 828 | spec->offset = STM32MP_NOR_METADATA1_OFFSET; |
| 829 | } else { |
| 830 | spec->offset = STM32MP_NOR_METADATA2_OFFSET; |
| 831 | } |
| 832 | |
| 833 | spec->length = sizeof(struct fwu_metadata); |
| 834 | break; |
| 835 | #endif |
Yann Gautier | 723133b | 2023-05-31 17:30:01 +0200 | [diff] [blame^] | 836 | |
| 837 | #if (STM32MP_RAW_NAND || STM32MP_SPI_NAND) |
| 838 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC: |
| 839 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI: |
| 840 | if (image_id == FWU_METADATA_IMAGE_ID) { |
| 841 | spec->offset = STM32MP_NAND_METADATA1_OFFSET; |
| 842 | } else { |
| 843 | spec->offset = STM32MP_NAND_METADATA2_OFFSET; |
| 844 | } |
| 845 | |
| 846 | spec->length = sizeof(struct fwu_metadata); |
| 847 | break; |
| 848 | #endif |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 849 | default: |
| 850 | panic(); |
| 851 | break; |
| 852 | } |
Sughosh Ganu | d1f8713 | 2021-12-01 16:46:34 +0530 | [diff] [blame] | 853 | |
| 854 | *image_spec = policy->image_spec; |
| 855 | *handle = *policy->dev_handle; |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | int plat_fwu_set_metadata_image_source(unsigned int image_id, |
| 861 | uintptr_t *handle, |
| 862 | uintptr_t *image_spec) |
| 863 | { |
Sughosh Ganu | d1f8713 | 2021-12-01 16:46:34 +0530 | [diff] [blame] | 864 | assert((image_id == FWU_METADATA_IMAGE_ID) || |
| 865 | (image_id == BKUP_FWU_METADATA_IMAGE_ID)); |
| 866 | |
Nicolas Toromanoff | 44adc4f | 2022-02-07 10:12:29 +0100 | [diff] [blame] | 867 | return plat_set_image_source(image_id, handle, image_spec); |
Sughosh Ganu | d1f8713 | 2021-12-01 16:46:34 +0530 | [diff] [blame] | 868 | } |
Yann Gautier | 723133b | 2023-05-31 17:30:01 +0200 | [diff] [blame^] | 869 | #endif /* PSA_FWU_SUPPORT */ |