Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2019 NXP |
| 4 | */ |
| 5 | |
Tom Rini | 5ab32a0 | 2024-04-30 07:35:49 -0600 | [diff] [blame^] | 6 | #include <config.h> |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 7 | #include <errno.h> |
Sean Anderson | 952ed67 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 8 | #include <imx_container.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <mmc.h> |
| 13 | #include <spi_flash.h> |
Tom Rini | a3a142c | 2023-03-09 11:22:08 -0500 | [diff] [blame] | 14 | #include <spl.h> |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 15 | #include <nand.h> |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 16 | #include <asm/arch/sys_proto.h> |
| 17 | #include <asm/mach-imx/boot_mode.h> |
| 18 | |
| 19 | #define MMC_DEV 0 |
| 20 | #define QSPI_DEV 1 |
| 21 | #define NAND_DEV 2 |
| 22 | #define QSPI_NOR_DEV 3 |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 23 | #define ROM_API_DEV 4 |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 24 | |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 25 | /* The unit of second image offset number which provision by the fuse bits */ |
| 26 | #define SND_IMG_OFF_UNIT (0x100000UL) |
| 27 | |
| 28 | /* |
| 29 | * If num = 0, off = (2 ^ 2) * 1MB |
| 30 | * else If num = 2, off = (2 ^ 0) * 1MB |
| 31 | * else off = (2 ^ num) * 1MB |
| 32 | */ |
| 33 | #define SND_IMG_NUM_TO_OFF(num) \ |
| 34 | ((1UL << ((0 == (num)) ? 2 : (2 == (num)) ? 0 : (num))) * SND_IMG_OFF_UNIT) |
| 35 | |
| 36 | #define GET_SND_IMG_NUM(fuse) (((fuse) >> 24) & 0x1F) |
| 37 | |
| 38 | #if defined(CONFIG_IMX8QM) |
| 39 | #define FUSE_IMG_SET_OFF_WORD 464 |
| 40 | #elif defined(CONFIG_IMX8QXP) |
| 41 | #define FUSE_IMG_SET_OFF_WORD 720 |
| 42 | #endif |
| 43 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 44 | int get_container_size(ulong addr, u16 *header_length) |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 45 | { |
| 46 | struct container_hdr *phdr; |
| 47 | struct boot_img_t *img_entry; |
| 48 | struct signature_block_hdr *sign_hdr; |
| 49 | u8 i = 0; |
| 50 | u32 max_offset = 0, img_end; |
| 51 | |
| 52 | phdr = (struct container_hdr *)addr; |
Sean Anderson | c512668 | 2023-10-14 16:47:43 -0400 | [diff] [blame] | 53 | if (!valid_container_hdr(phdr)) { |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 54 | debug("Wrong container header\n"); |
| 55 | return -EFAULT; |
| 56 | } |
| 57 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 58 | max_offset = phdr->length_lsb + (phdr->length_msb << 8); |
| 59 | if (header_length) |
| 60 | *header_length = max_offset; |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 61 | |
| 62 | img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr)); |
| 63 | for (i = 0; i < phdr->num_images; i++) { |
| 64 | img_end = img_entry->offset + img_entry->size; |
| 65 | if (img_end > max_offset) |
| 66 | max_offset = img_end; |
| 67 | |
| 68 | debug("img[%u], end = 0x%x\n", i, img_end); |
| 69 | |
| 70 | img_entry++; |
| 71 | } |
| 72 | |
| 73 | if (phdr->sig_blk_offset != 0) { |
| 74 | sign_hdr = (struct signature_block_hdr *)(addr + phdr->sig_blk_offset); |
| 75 | u16 len = sign_hdr->length_lsb + (sign_hdr->length_msb << 8); |
| 76 | |
| 77 | if (phdr->sig_blk_offset + len > max_offset) |
| 78 | max_offset = phdr->sig_blk_offset + len; |
| 79 | |
| 80 | debug("sigblk, end = 0x%x\n", phdr->sig_blk_offset + len); |
| 81 | } |
| 82 | |
| 83 | return max_offset; |
| 84 | } |
| 85 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 86 | static int get_dev_container_size(void *dev, int dev_type, unsigned long offset, u16 *header_length) |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 87 | { |
| 88 | u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT); |
| 89 | int ret = 0; |
| 90 | |
| 91 | if (!buf) { |
| 92 | printf("Malloc buffer failed\n"); |
| 93 | return -ENOMEM; |
| 94 | } |
| 95 | |
Simon Glass | b58bfe0 | 2021-08-08 12:20:09 -0600 | [diff] [blame] | 96 | #ifdef CONFIG_SPL_MMC |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 97 | if (dev_type == MMC_DEV) { |
| 98 | unsigned long count = 0; |
| 99 | struct mmc *mmc = (struct mmc *)dev; |
| 100 | |
| 101 | count = blk_dread(mmc_get_blk_desc(mmc), |
| 102 | offset / mmc->read_bl_len, |
| 103 | CONTAINER_HDR_ALIGNMENT / mmc->read_bl_len, |
| 104 | buf); |
| 105 | if (count == 0) { |
| 106 | printf("Read container image from MMC/SD failed\n"); |
| 107 | return -EIO; |
| 108 | } |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | #ifdef CONFIG_SPL_SPI_LOAD |
| 113 | if (dev_type == QSPI_DEV) { |
| 114 | struct spi_flash *flash = (struct spi_flash *)dev; |
| 115 | |
| 116 | ret = spi_flash_read(flash, offset, |
| 117 | CONTAINER_HDR_ALIGNMENT, buf); |
| 118 | if (ret != 0) { |
| 119 | printf("Read container image from QSPI failed\n"); |
| 120 | return -EIO; |
| 121 | } |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 | #ifdef CONFIG_SPL_NAND_SUPPORT |
| 126 | if (dev_type == NAND_DEV) { |
| 127 | ret = nand_spl_load_image(offset, CONTAINER_HDR_ALIGNMENT, |
| 128 | buf); |
| 129 | if (ret != 0) { |
| 130 | printf("Read container image from NAND failed\n"); |
| 131 | return -EIO; |
| 132 | } |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #ifdef CONFIG_SPL_NOR_SUPPORT |
| 137 | if (dev_type == QSPI_NOR_DEV) |
| 138 | memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT); |
| 139 | #endif |
| 140 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 141 | #ifdef CONFIG_SPL_BOOTROM_SUPPORT |
| 142 | if (dev_type == ROM_API_DEV) { |
| 143 | ret = spl_romapi_raw_seekable_read(offset, CONTAINER_HDR_ALIGNMENT, buf); |
| 144 | if (!ret) { |
| 145 | printf("Read container image from ROM API failed\n"); |
| 146 | return -EIO; |
| 147 | } |
| 148 | } |
| 149 | #endif |
| 150 | |
| 151 | ret = get_container_size((ulong)buf, header_length); |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 152 | |
| 153 | free(buf); |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 158 | static bool check_secondary_cnt_set(unsigned long *set_off) |
| 159 | { |
| 160 | #if IS_ENABLED(CONFIG_ARCH_IMX8) |
| 161 | int ret; |
| 162 | u8 set_id = 1; |
| 163 | u32 fuse_val = 0; |
| 164 | |
| 165 | if (!(is_imx8qxp() && is_soc_rev(CHIP_REV_B))) { |
| 166 | ret = sc_misc_get_boot_container(-1, &set_id); |
| 167 | if (ret) |
| 168 | return false; |
| 169 | /* Secondary boot */ |
| 170 | if (set_id == 2) { |
| 171 | ret = sc_misc_otp_fuse_read(-1, FUSE_IMG_SET_OFF_WORD, &fuse_val); |
| 172 | if (!ret) { |
| 173 | if (set_off) |
| 174 | *set_off = SND_IMG_NUM_TO_OFF(GET_SND_IMG_NUM(fuse_val)); |
| 175 | return true; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 184 | static unsigned long get_boot_device_offset(void *dev, int dev_type) |
| 185 | { |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 186 | unsigned long offset = 0, sec_set_off = 0; |
| 187 | bool sec_boot = false; |
| 188 | |
| 189 | if (dev_type == ROM_API_DEV) { |
| 190 | offset = (unsigned long)dev; |
| 191 | return offset; |
| 192 | } |
| 193 | |
| 194 | sec_boot = check_secondary_cnt_set(&sec_set_off); |
| 195 | if (sec_boot) |
| 196 | printf("Secondary set selected\n"); |
| 197 | else |
| 198 | printf("Primary set selected\n"); |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 199 | |
| 200 | if (dev_type == MMC_DEV) { |
| 201 | struct mmc *mmc = (struct mmc *)dev; |
| 202 | |
| 203 | if (IS_SD(mmc) || mmc->part_config == MMCPART_NOAVAILABLE) { |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 204 | offset = sec_boot ? sec_set_off : CONTAINER_HDR_MMCSD_OFFSET; |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 205 | } else { |
| 206 | u8 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config); |
| 207 | |
| 208 | if (part == 1 || part == 2) { |
| 209 | if (is_imx8qxp() && is_soc_rev(CHIP_REV_B)) |
| 210 | offset = CONTAINER_HDR_MMCSD_OFFSET; |
| 211 | else |
| 212 | offset = CONTAINER_HDR_EMMC_OFFSET; |
| 213 | } else { |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 214 | offset = sec_boot ? sec_set_off : CONTAINER_HDR_MMCSD_OFFSET; |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } else if (dev_type == QSPI_DEV) { |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 218 | offset = sec_boot ? (sec_set_off + CONTAINER_HDR_QSPI_OFFSET) : |
| 219 | CONTAINER_HDR_QSPI_OFFSET; |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 220 | } else if (dev_type == NAND_DEV) { |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 221 | offset = sec_boot ? (sec_set_off + CONTAINER_HDR_NAND_OFFSET) : |
| 222 | CONTAINER_HDR_NAND_OFFSET; |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 223 | } else if (dev_type == QSPI_NOR_DEV) { |
| 224 | offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000; |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 225 | } else { |
| 226 | printf("Not supported dev_type: %d\n", dev_type); |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 229 | debug("container set offset 0x%lx\n", offset); |
| 230 | |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 231 | return offset; |
| 232 | } |
| 233 | |
| 234 | static int get_imageset_end(void *dev, int dev_type) |
| 235 | { |
| 236 | unsigned long offset1 = 0, offset2 = 0; |
| 237 | int value_container[2]; |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 238 | u16 hdr_length; |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 239 | |
| 240 | offset1 = get_boot_device_offset(dev, dev_type); |
| 241 | offset2 = CONTAINER_HDR_ALIGNMENT + offset1; |
| 242 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 243 | value_container[0] = get_dev_container_size(dev, dev_type, offset1, &hdr_length); |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 244 | if (value_container[0] < 0) { |
| 245 | printf("Parse seco container failed %d\n", value_container[0]); |
| 246 | return value_container[0]; |
| 247 | } |
| 248 | |
| 249 | debug("seco container size 0x%x\n", value_container[0]); |
| 250 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 251 | value_container[1] = get_dev_container_size(dev, dev_type, offset2, &hdr_length); |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 252 | if (value_container[1] < 0) { |
| 253 | debug("Parse scu container failed %d, only seco container\n", |
| 254 | value_container[1]); |
| 255 | /* return seco container total size */ |
| 256 | return value_container[0] + offset1; |
| 257 | } |
| 258 | |
| 259 | debug("scu container size 0x%x\n", value_container[1]); |
| 260 | |
| 261 | return value_container[1] + offset2; |
| 262 | } |
| 263 | |
| 264 | #ifdef CONFIG_SPL_SPI_LOAD |
Ye Li | e8f97df | 2024-03-28 18:50:55 +0800 | [diff] [blame] | 265 | unsigned int spl_spi_get_uboot_offs(struct spi_flash *flash) |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 266 | { |
| 267 | int end; |
| 268 | |
| 269 | end = get_imageset_end(flash, QSPI_DEV); |
| 270 | end = ROUND(end, SZ_1K); |
| 271 | |
| 272 | printf("Load image from QSPI 0x%x\n", end); |
| 273 | |
| 274 | return end; |
| 275 | } |
| 276 | #endif |
| 277 | |
Simon Glass | b58bfe0 | 2021-08-08 12:20:09 -0600 | [diff] [blame] | 278 | #ifdef CONFIG_SPL_MMC |
Marek Vasut | f9a921e | 2023-10-16 18:16:12 +0200 | [diff] [blame] | 279 | unsigned long arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc, |
| 280 | unsigned long raw_sect) |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 281 | { |
| 282 | int end; |
| 283 | |
| 284 | end = get_imageset_end(mmc, MMC_DEV); |
| 285 | end = ROUND(end, SZ_1K); |
| 286 | |
| 287 | printf("Load image from MMC/SD 0x%x\n", end); |
| 288 | |
| 289 | return end / mmc->read_bl_len; |
| 290 | } |
Peng Fan | 7e7d36f | 2023-06-15 18:09:17 +0800 | [diff] [blame] | 291 | |
| 292 | int spl_mmc_emmc_boot_partition(struct mmc *mmc) |
| 293 | { |
| 294 | int part; |
| 295 | |
| 296 | part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config); |
| 297 | if (part == 1 || part == 2) { |
| 298 | unsigned long sec_set_off = 0; |
| 299 | bool sec_boot = false; |
| 300 | |
| 301 | sec_boot = check_secondary_cnt_set(&sec_set_off); |
| 302 | if (sec_boot) |
| 303 | part = (part == 1) ? 2 : 1; |
| 304 | } else if (part == 7) { |
| 305 | part = 0; |
| 306 | } |
| 307 | |
| 308 | return part; |
| 309 | } |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 310 | #endif |
| 311 | |
| 312 | #ifdef CONFIG_SPL_NAND_SUPPORT |
| 313 | uint32_t spl_nand_get_uboot_raw_page(void) |
| 314 | { |
| 315 | int end; |
| 316 | |
| 317 | end = get_imageset_end((void *)NULL, NAND_DEV); |
| 318 | end = ROUND(end, SZ_16K); |
| 319 | |
| 320 | printf("Load image from NAND 0x%x\n", end); |
| 321 | |
| 322 | return end; |
| 323 | } |
| 324 | #endif |
| 325 | |
| 326 | #ifdef CONFIG_SPL_NOR_SUPPORT |
| 327 | unsigned long spl_nor_get_uboot_base(void) |
| 328 | { |
| 329 | int end; |
| 330 | |
| 331 | /* Calculate the image set end, |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 332 | * if it is less than CFG_SYS_UBOOT_BASE(0x8281000), |
| 333 | * we use CFG_SYS_UBOOT_BASE |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 334 | * Otherwise, use the calculated address |
| 335 | */ |
| 336 | end = get_imageset_end((void *)NULL, QSPI_NOR_DEV); |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 337 | if (end <= CFG_SYS_UBOOT_BASE) |
| 338 | end = CFG_SYS_UBOOT_BASE; |
Peng Fan | 0aef2f2 | 2019-09-23 10:18:44 +0800 | [diff] [blame] | 339 | else |
| 340 | end = ROUND(end, SZ_1K); |
| 341 | |
| 342 | printf("Load image from NOR 0x%x\n", end); |
| 343 | |
| 344 | return end; |
| 345 | } |
| 346 | #endif |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 347 | |
| 348 | #ifdef CONFIG_SPL_BOOTROM_SUPPORT |
Ye Li | 479fd4a | 2021-08-07 16:01:08 +0800 | [diff] [blame] | 349 | u32 __weak spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev) |
| 350 | { |
| 351 | return image_offset; |
| 352 | } |
| 353 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 354 | ulong spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev) |
| 355 | { |
| 356 | ulong end; |
| 357 | |
Ye Li | 479fd4a | 2021-08-07 16:01:08 +0800 | [diff] [blame] | 358 | image_offset = spl_arch_boot_image_offset(image_offset, rom_bt_dev); |
| 359 | |
Ye Li | 7a71c61 | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 360 | end = get_imageset_end((void *)(ulong)image_offset, ROM_API_DEV); |
| 361 | end = ROUND(end, SZ_1K); |
| 362 | |
| 363 | printf("Load image from 0x%lx by ROM_API\n", end); |
| 364 | |
| 365 | return end; |
| 366 | } |
| 367 | #endif |