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