Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | // SPDX-License-Identifier: GPL-2.0+ /* * Copyright 2019 NXP */ #include <config.h> #include <errno.h> #include <imx_container.h> #include <log.h> #include <malloc.h> #include <asm/io.h> #include <mmc.h> #include <spi_flash.h> #include <spl.h> #include <nand.h> #include <asm/arch/sys_proto.h> #include <asm/mach-imx/boot_mode.h> #define MMC_DEV 0 #define QSPI_DEV 1 #define NAND_DEV 2 #define QSPI_NOR_DEV 3 #define ROM_API_DEV 4 /* The unit of second image offset number which provision by the fuse bits */ #define SND_IMG_OFF_UNIT (0x100000UL) /* * If num = 0, off = (2 ^ 2) * 1MB * else If num = 2, off = (2 ^ 0) * 1MB * else off = (2 ^ num) * 1MB */ #define SND_IMG_NUM_TO_OFF(num) \ ((1UL << ((0 == (num)) ? 2 : (2 == (num)) ? 0 : (num))) * SND_IMG_OFF_UNIT) #define GET_SND_IMG_NUM(fuse) (((fuse) >> 24) & 0x1F) #if defined(CONFIG_IMX8QM) #define FUSE_IMG_SET_OFF_WORD 464 #elif defined(CONFIG_IMX8QXP) #define FUSE_IMG_SET_OFF_WORD 720 #endif #define MAX_V2X_CTNR_IMG_NUM (4) #define MIN_V2X_CTNR_IMG_NUM (2) #define IMG_FLAGS_IMG_TYPE_SHIFT (0u) #define IMG_FLAGS_IMG_TYPE_MASK (0xfU) #define IMG_FLAGS_IMG_TYPE(x) (((x) & IMG_FLAGS_IMG_TYPE_MASK) >> \ IMG_FLAGS_IMG_TYPE_SHIFT) #define IMG_FLAGS_CORE_ID_SHIFT (4u) #define IMG_FLAGS_CORE_ID_MASK (0xf0U) #define IMG_FLAGS_CORE_ID(x) (((x) & IMG_FLAGS_CORE_ID_MASK) >> \ IMG_FLAGS_CORE_ID_SHIFT) #define IMG_TYPE_V2X_PRI_FW (0x0Bu) /* Primary V2X FW */ #define IMG_TYPE_V2X_SND_FW (0x0Cu) /* Secondary V2X FW */ #define CORE_V2X_PRI 9 #define CORE_V2X_SND 10 static bool is_v2x_fw_container(ulong addr) { struct container_hdr *phdr; struct boot_img_t *img_entry; phdr = (struct container_hdr *)addr; if ((phdr->tag != 0x87 && phdr->tag != 0x82) || phdr->version != 0x0) { debug("Wrong container header\n"); return false; } if (phdr->num_images >= MIN_V2X_CTNR_IMG_NUM && phdr->num_images <= MAX_V2X_CTNR_IMG_NUM) { img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr)); if (IMG_FLAGS_IMG_TYPE(img_entry->hab_flags) == IMG_TYPE_V2X_PRI_FW && IMG_FLAGS_CORE_ID(img_entry->hab_flags) == CORE_V2X_PRI) { img_entry++; if (IMG_FLAGS_IMG_TYPE(img_entry->hab_flags) == IMG_TYPE_V2X_SND_FW && IMG_FLAGS_CORE_ID(img_entry->hab_flags) == CORE_V2X_SND) return true; } } return false; } int get_container_size(ulong addr, u16 *header_length) { struct container_hdr *phdr; struct boot_img_t *img_entry; struct signature_block_hdr *sign_hdr; u8 i = 0; u32 max_offset = 0, img_end; phdr = (struct container_hdr *)addr; if (!valid_container_hdr(phdr)) { debug("Wrong container header\n"); return -EFAULT; } max_offset = phdr->length_lsb + (phdr->length_msb << 8); if (header_length) *header_length = max_offset; img_entry = (struct boot_img_t *)(addr + sizeof(struct container_hdr)); for (i = 0; i < phdr->num_images; i++) { img_end = img_entry->offset + img_entry->size; if (img_end > max_offset) max_offset = img_end; debug("img[%u], end = 0x%x\n", i, img_end); img_entry++; } if (phdr->sig_blk_offset != 0) { sign_hdr = (struct signature_block_hdr *)(addr + phdr->sig_blk_offset); u16 len = sign_hdr->length_lsb + (sign_hdr->length_msb << 8); if (phdr->sig_blk_offset + len > max_offset) max_offset = phdr->sig_blk_offset + len; debug("sigblk, end = 0x%x\n", phdr->sig_blk_offset + len); } return max_offset; } static int get_dev_container_size(void *dev, int dev_type, unsigned long offset, u16 *header_length, bool *v2x_cntr) { u8 *buf = malloc(CONTAINER_HDR_ALIGNMENT); int ret = 0; if (!buf) { printf("Malloc buffer failed\n"); return -ENOMEM; } #ifdef CONFIG_SPL_MMC if (dev_type == MMC_DEV) { unsigned long count = 0; struct mmc *mmc = (struct mmc *)dev; count = blk_dread(mmc_get_blk_desc(mmc), offset / mmc->read_bl_len, CONTAINER_HDR_ALIGNMENT / mmc->read_bl_len, buf); if (count == 0) { printf("Read container image from MMC/SD failed\n"); return -EIO; } } #endif #ifdef CONFIG_SPL_SPI_LOAD if (dev_type == QSPI_DEV) { struct spi_flash *flash = (struct spi_flash *)dev; ret = spi_flash_read(flash, offset, CONTAINER_HDR_ALIGNMENT, buf); if (ret != 0) { printf("Read container image from QSPI failed\n"); return -EIO; } } #endif #ifdef CONFIG_SPL_NAND_SUPPORT if (dev_type == NAND_DEV) { ret = nand_spl_load_image(offset, CONTAINER_HDR_ALIGNMENT, buf); if (ret != 0) { printf("Read container image from NAND failed\n"); return -EIO; } } #endif #ifdef CONFIG_SPL_NOR_SUPPORT if (dev_type == QSPI_NOR_DEV) memcpy(buf, (const void *)offset, CONTAINER_HDR_ALIGNMENT); #endif #ifdef CONFIG_SPL_BOOTROM_SUPPORT if (dev_type == ROM_API_DEV) { ret = spl_romapi_raw_seekable_read(offset, CONTAINER_HDR_ALIGNMENT, buf); if (!ret) { printf("Read container image from ROM API failed\n"); return -EIO; } } #endif ret = get_container_size((ulong)buf, header_length); if (v2x_cntr) *v2x_cntr = is_v2x_fw_container((ulong)buf); free(buf); return ret; } static bool check_secondary_cnt_set(unsigned long *set_off) { #if IS_ENABLED(CONFIG_ARCH_IMX8) int ret; u8 set_id = 1; u32 fuse_val = 0; if (!(is_imx8qxp() && is_soc_rev(CHIP_REV_B))) { ret = sc_misc_get_boot_container(-1, &set_id); if (ret) return false; /* Secondary boot */ if (set_id == 2) { ret = sc_misc_otp_fuse_read(-1, FUSE_IMG_SET_OFF_WORD, &fuse_val); if (!ret) { if (set_off) *set_off = SND_IMG_NUM_TO_OFF(GET_SND_IMG_NUM(fuse_val)); return true; } } } #endif return false; } static unsigned long get_boot_device_offset(void *dev, int dev_type) { unsigned long offset = 0, sec_set_off = 0; bool sec_boot = false; if (dev_type == ROM_API_DEV) { offset = (unsigned long)dev; return offset; } sec_boot = check_secondary_cnt_set(&sec_set_off); if (sec_boot) printf("Secondary set selected\n"); else printf("Primary set selected\n"); if (dev_type == MMC_DEV) { struct mmc *mmc = (struct mmc *)dev; if (IS_SD(mmc) || mmc->part_config == MMCPART_NOAVAILABLE) { offset = sec_boot ? sec_set_off : CONTAINER_HDR_MMCSD_OFFSET; } else { u8 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config); if (part == EMMC_BOOT_PART_BOOT1 || part == EMMC_BOOT_PART_BOOT2) { if (is_imx8qxp() && is_soc_rev(CHIP_REV_B)) offset = CONTAINER_HDR_MMCSD_OFFSET; else offset = CONTAINER_HDR_EMMC_OFFSET; } else { offset = sec_boot ? sec_set_off : CONTAINER_HDR_MMCSD_OFFSET; } } } else if (dev_type == QSPI_DEV) { offset = sec_boot ? (sec_set_off + CONTAINER_HDR_QSPI_OFFSET) : CONTAINER_HDR_QSPI_OFFSET; } else if (dev_type == NAND_DEV) { offset = sec_boot ? (sec_set_off + CONTAINER_HDR_NAND_OFFSET) : CONTAINER_HDR_NAND_OFFSET; } else if (dev_type == QSPI_NOR_DEV) { offset = CONTAINER_HDR_QSPI_OFFSET + 0x08000000; } else { printf("Not supported dev_type: %d\n", dev_type); } debug("container set offset 0x%lx\n", offset); return offset; } static ulong get_imageset_end(void *dev, int dev_type) { unsigned long offset[3] = {}; int value_container[3] = {}; u16 hdr_length; bool v2x_fw = false; offset[0] = get_boot_device_offset(dev, dev_type); value_container[0] = get_dev_container_size(dev, dev_type, offset[0], &hdr_length, NULL); if (value_container[0] < 0) { printf("Parse seco container failed %d\n", value_container[0]); return 0; } debug("seco container size 0x%x\n", value_container[0]); if (is_imx95() || is_imx94()) { offset[1] = ALIGN(hdr_length, CONTAINER_HDR_ALIGNMENT) + offset[0]; value_container[1] = get_dev_container_size(dev, dev_type, offset[1], &hdr_length, &v2x_fw); if (value_container[1] < 0) { printf("Parse v2x container failed %d\n", value_container[1]); return value_container[0] + offset[0]; /* return seco container total size */ } if (v2x_fw) { debug("v2x container size 0x%x\n", value_container[1]); offset[2] = ALIGN(hdr_length, CONTAINER_HDR_ALIGNMENT) + offset[1]; } else { printf("no v2x container included\n"); offset[2] = offset[1]; } } else { /* Skip offset[1] */ offset[2] = ALIGN(hdr_length, CONTAINER_HDR_ALIGNMENT) + offset[0]; } value_container[2] = get_dev_container_size(dev, dev_type, offset[2], &hdr_length, NULL); if (value_container[2] < 0) { debug("Parse scu container image failed %d, only seco container\n", value_container[2]); if (is_imx95() || is_imx94()) return value_container[1] + offset[1]; /* return seco + v2x container total size */ else return value_container[0] + offset[0]; /* return seco container total size */ } debug("scu container size 0x%x\n", value_container[2]); return value_container[2] + offset[2]; } #ifdef CONFIG_SPL_SPI_LOAD unsigned int spl_spi_get_uboot_offs(struct spi_flash *flash) { ulong end; end = get_imageset_end(flash, QSPI_DEV); end = ROUND(end, SZ_1K); printf("Load image from QSPI 0x%lx\n", end); return end; } #endif #ifdef CONFIG_SPL_MMC unsigned long arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc, unsigned long raw_sect) { ulong end; end = get_imageset_end(mmc, MMC_DEV); end = ROUND(end, SZ_1K); printf("Load image from MMC/SD 0x%lx\n", end); return end / mmc->read_bl_len; } int spl_mmc_emmc_boot_partition(struct mmc *mmc) { int part; part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config); if (part == EMMC_BOOT_PART_BOOT1 || part == EMMC_BOOT_PART_BOOT2) { unsigned long sec_set_off = 0; bool sec_boot = false; sec_boot = check_secondary_cnt_set(&sec_set_off); if (sec_boot) part = (part == EMMC_BOOT_PART_BOOT1) ? EMMC_HWPART_BOOT2 : EMMC_HWPART_BOOT1; } else if (part == EMMC_BOOT_PART_USER) { part = EMMC_HWPART_DEFAULT; } return part; } #endif #ifdef CONFIG_SPL_NAND_SUPPORT uint32_t spl_nand_get_uboot_raw_page(void) { ulong end; end = get_imageset_end((void *)NULL, NAND_DEV); end = ROUND(end, SZ_16K); printf("Load image from NAND 0x%lx\n", end); return end; } #endif #ifdef CONFIG_SPL_NOR_SUPPORT unsigned long spl_nor_get_uboot_base(void) { ulong end; /* Calculate the image set end, * if it is less than CFG_SYS_UBOOT_BASE(0x8281000), * we use CFG_SYS_UBOOT_BASE * Otherwise, use the calculated address */ end = get_imageset_end((void *)NULL, QSPI_NOR_DEV); if (end <= CFG_SYS_UBOOT_BASE) end = CFG_SYS_UBOOT_BASE; else end = ROUND(end, SZ_1K); printf("Load image from NOR 0x%lx\n", end); return end; } #endif #ifdef CONFIG_SPL_BOOTROM_SUPPORT u32 __weak spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev) { return image_offset; } ulong spl_romapi_get_uboot_base(u32 image_offset, u32 rom_bt_dev) { ulong end; image_offset = spl_arch_boot_image_offset(image_offset, rom_bt_dev); end = get_imageset_end((void *)(ulong)image_offset, ROM_API_DEV); end = ROUND(end, SZ_1K); printf("Load image from 0x%lx by ROM_API\n", end); return end; } #endif |