1 /* 2 * Copyright (C) 2016 Google, Inc 3 * Written by Simon Glass <sjg@chromium.org> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <boot_rkimg.h> 10 #include <errno.h> 11 #include <fdt_support.h> 12 #include <image.h> 13 #include <malloc.h> 14 #include <mtd_blk.h> 15 #include <spl.h> 16 #include <spl_ab.h> 17 #include <linux/libfdt.h> 18 19 #ifndef CONFIG_SYS_BOOTM_LEN 20 #define CONFIG_SYS_BOOTM_LEN (64 << 20) 21 #endif 22 23 /** 24 * spl_fit_get_image_name(): By using the matching configuration subnode, 25 * retrieve the name of an image, specified by a property name and an index 26 * into that. 27 * @fit: Pointer to the FDT blob. 28 * @images: Offset of the /images subnode. 29 * @type: Name of the property within the configuration subnode. 30 * @index: Index into the list of strings in this property. 31 * @outname: Name of the image 32 * 33 * Return: 0 on success, or a negative error number 34 */ 35 static int spl_fit_get_image_name(const void *fit, int images, 36 const char *type, int index, 37 char **outname) 38 { 39 const char *name, *str; 40 __maybe_unused int node; 41 int conf_node; 42 int len, i; 43 44 conf_node = fit_find_config_node(fit); 45 if (conf_node < 0) { 46 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 47 printf("No matching DT out of these options:\n"); 48 for (node = fdt_first_subnode(fit, conf_node); 49 node >= 0; 50 node = fdt_next_subnode(fit, node)) { 51 name = fdt_getprop(fit, node, "description", &len); 52 printf(" %s\n", name); 53 } 54 #endif 55 return conf_node; 56 } 57 58 name = fdt_getprop(fit, conf_node, type, &len); 59 if (!name) { 60 debug("cannot find property '%s': %d\n", type, len); 61 return -EINVAL; 62 } 63 64 str = name; 65 for (i = 0; i < index; i++) { 66 str = strchr(str, '\0') + 1; 67 if (!str || (str - name >= len)) { 68 debug("no string for index %d\n", index); 69 return -E2BIG; 70 } 71 } 72 73 *outname = (char *)str; 74 return 0; 75 } 76 77 /** 78 * spl_fit_get_image_node(): By using the matching configuration subnode, 79 * retrieve the name of an image, specified by a property name and an index 80 * into that. 81 * @fit: Pointer to the FDT blob. 82 * @images: Offset of the /images subnode. 83 * @type: Name of the property within the configuration subnode. 84 * @index: Index into the list of strings in this property. 85 * 86 * Return: the node offset of the respective image node or a negative 87 * error number. 88 */ 89 static int spl_fit_get_image_node(const void *fit, int images, 90 const char *type, int index) 91 { 92 char *str; 93 int err; 94 int node; 95 96 err = spl_fit_get_image_name(fit, images, type, index, &str); 97 if (err) 98 return err; 99 100 debug("%s: '%s'\n", type, str); 101 102 node = fdt_subnode_offset(fit, images, str); 103 if (node < 0) { 104 debug("cannot find image node '%s': %d\n", str, node); 105 return -EINVAL; 106 } 107 108 return node; 109 } 110 111 static int get_aligned_image_offset(struct spl_load_info *info, int offset) 112 { 113 /* 114 * If it is a FS read, get the first address before offset which is 115 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the 116 * block number to which offset belongs. 117 */ 118 if (info->filename) 119 return offset & ~(ARCH_DMA_MINALIGN - 1); 120 121 return offset / info->bl_len; 122 } 123 124 static int get_aligned_image_overhead(struct spl_load_info *info, int offset) 125 { 126 /* 127 * If it is a FS read, get the difference between the offset and 128 * the first address before offset which is aligned to 129 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the 130 * block. 131 */ 132 if (info->filename) 133 return offset & (ARCH_DMA_MINALIGN - 1); 134 135 return offset % info->bl_len; 136 } 137 138 static int get_aligned_image_size(struct spl_load_info *info, int data_size, 139 int offset) 140 { 141 data_size = data_size + get_aligned_image_overhead(info, offset); 142 143 if (info->filename) 144 return data_size; 145 146 return (data_size + info->bl_len - 1) / info->bl_len; 147 } 148 149 /** 150 * spl_load_fit_image(): load the image described in a certain FIT node 151 * @info: points to information about the device to load data from 152 * @sector: the start sector of the FIT image on the device 153 * @fit: points to the flattened device tree blob describing the FIT 154 * image 155 * @base_offset: the beginning of the data area containing the actual 156 * image data, relative to the beginning of the FIT 157 * @node: offset of the DT node describing the image to load (relative 158 * to @fit) 159 * @image_info: will be filled with information about the loaded image 160 * If the FIT node does not contain a "load" (address) property, 161 * the image gets loaded to the address pointed to by the 162 * load_addr member in this struct. 163 * 164 * Return: 0 on success or a negative error number. 165 */ 166 static int spl_load_fit_image(struct spl_load_info *info, ulong sector, 167 void *fit, ulong base_offset, int node, 168 struct spl_image_info *image_info) 169 { 170 int offset; 171 size_t length; 172 int len; 173 ulong size; 174 ulong comp_addr, load_addr, load_ptr; 175 void *src; 176 ulong overhead; 177 int nr_sectors; 178 int align_len = ARCH_DMA_MINALIGN - 1; 179 uint8_t image_comp = -1, type = -1; 180 const void *data; 181 bool external_data = false; 182 183 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { 184 if (fit_image_get_comp(fit, node, &image_comp)) 185 puts("Cannot get image compression format.\n"); 186 else 187 debug("%s ", genimg_get_comp_name(image_comp)); 188 189 if (fit_image_get_type(fit, node, &type)) 190 puts("Cannot get image type.\n"); 191 else 192 debug("%s ", genimg_get_type_name(type)); 193 } else { 194 fit_image_get_comp(fit, node, &image_comp); 195 } 196 197 if (fit_image_get_load(fit, node, &load_addr)) 198 load_addr = image_info->load_addr; 199 200 if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE) { 201 /* Empirically, 2MB is enough for U-Boot, tee and atf */ 202 if (fit_image_get_comp_addr(fit, node, &comp_addr)) 203 comp_addr = load_addr + FIT_MAX_SPL_IMAGE_SZ; 204 } else { 205 comp_addr = load_addr; 206 } 207 208 if (!fit_image_get_data_position(fit, node, &offset)) { 209 external_data = true; 210 } else if (!fit_image_get_data_offset(fit, node, &offset)) { 211 offset += base_offset; 212 external_data = true; 213 } 214 215 if (external_data) { 216 /* External data */ 217 if (fit_image_get_data_size(fit, node, &len)) 218 return -ENOENT; 219 220 load_ptr = (comp_addr + align_len) & ~align_len; 221 #if defined(CONFIG_ARCH_ROCKCHIP) 222 if ((load_ptr < CONFIG_SYS_SDRAM_BASE) || 223 (load_ptr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE)) 224 load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len); 225 #endif 226 length = len; 227 228 overhead = get_aligned_image_overhead(info, offset); 229 nr_sectors = get_aligned_image_size(info, length, offset); 230 231 if (info->read(info, 232 sector + get_aligned_image_offset(info, offset), 233 nr_sectors, (void *)load_ptr) != nr_sectors) 234 return -EIO; 235 236 debug("External data: dst=%lx, offset=%x, size=%lx\n", 237 load_ptr, offset, (unsigned long)length); 238 src = (void *)load_ptr + overhead; 239 } else { 240 /* Embedded data */ 241 if (fit_image_get_data(fit, node, &data, &length)) { 242 puts("Cannot get image data/size\n"); 243 return -ENOENT; 244 } 245 debug("Embedded data: dst=%lx, size=%lx\n", load_addr, 246 (unsigned long)length); 247 src = (void *)data; 248 } 249 250 /* Check hashes and signature */ 251 if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE) 252 printf("## Checking %s 0x%08lx (%s @0x%08lx) ... ", 253 fit_get_name(fit, node, NULL), load_addr, 254 (char *)fdt_getprop(fit, node, FIT_COMP_PROP, NULL), 255 (long)src); 256 else 257 printf("## Checking %s 0x%08lx ... ", 258 fit_get_name(fit, node, NULL), load_addr); 259 260 #ifdef CONFIG_FIT_SPL_PRINT 261 printf("\n"); 262 fit_image_print(fit, node, ""); 263 #endif 264 if (!fit_image_verify_with_data(fit, node, 265 src, length)) 266 return -EPERM; 267 268 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS 269 board_fit_image_post_process(fit, node, (ulong *)&load_addr, 270 (ulong **)&src, &length); 271 #endif 272 puts("OK\n"); 273 274 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && 275 IS_ENABLED(CONFIG_SPL_GZIP) && 276 image_comp == IH_COMP_GZIP && 277 type == IH_TYPE_KERNEL) { 278 size = length; 279 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, 280 src, &size)) { 281 puts("Uncompressing error\n"); 282 return -EIO; 283 } 284 length = size; 285 } else { 286 memcpy((void *)load_addr, src, length); 287 } 288 289 if (image_info) { 290 image_info->load_addr = load_addr; 291 image_info->size = length; 292 image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); 293 } 294 295 return 0; 296 } 297 298 static int spl_fit_append_fdt(struct spl_image_info *spl_image, 299 struct spl_load_info *info, ulong sector, 300 void *fit, int images, ulong base_offset) 301 { 302 struct spl_image_info image_info; 303 int node, ret; 304 305 /* Figure out which device tree the board wants to use */ 306 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); 307 if (node < 0) { 308 debug("%s: cannot find FDT node\n", __func__); 309 return node; 310 } 311 312 /* 313 * Read the device tree and place it after the image. 314 * Align the destination address to ARCH_DMA_MINALIGN. 315 */ 316 image_info.load_addr = spl_image->load_addr + spl_image->size; 317 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 318 &image_info); 319 320 if (ret < 0) 321 return ret; 322 323 /* Make the load-address of the FDT available for the SPL framework */ 324 spl_image->fdt_addr = (void *)image_info.load_addr; 325 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 326 /* Try to make space, so we can inject details on the loadables */ 327 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192); 328 #endif 329 330 /* 331 * If need, load kernel FDT right after U-Boot FDT. 332 * 333 * kernel FDT is for U-Boot if there is not valid one 334 * from images, ie: resource.img, boot.img or recovery.img. 335 */ 336 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 1); 337 if (node < 0) { 338 debug("%s: cannot find FDT node\n", __func__); 339 return ret; 340 } 341 342 image_info.load_addr = 343 (ulong)spl_image->fdt_addr + fdt_totalsize(spl_image->fdt_addr); 344 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 345 &image_info); 346 347 return ret; 348 } 349 350 static int spl_fit_record_loadable(const void *fit, int images, int index, 351 void *blob, struct spl_image_info *image) 352 { 353 int ret = 0; 354 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 355 char *name; 356 int node; 357 358 ret = spl_fit_get_image_name(fit, images, "loadables", 359 index, &name); 360 if (ret < 0) 361 return ret; 362 363 node = spl_fit_get_image_node(fit, images, "loadables", index); 364 365 ret = fdt_record_loadable(blob, index, name, image->load_addr, 366 image->size, image->entry_point, 367 fdt_getprop(fit, node, "type", NULL), 368 fdt_getprop(fit, node, "os", NULL)); 369 #endif 370 return ret; 371 } 372 373 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os) 374 { 375 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 376 return -ENOTSUPP; 377 #else 378 return fit_image_get_os(fit, noffset, os); 379 #endif 380 } 381 382 __weak int spl_fit_standalone_release(uintptr_t entry_point) 383 { 384 return 0; 385 } 386 387 static void *spl_fit_load_blob(struct spl_load_info *info, 388 ulong sector, void *fit_header, 389 int *base_offset) 390 { 391 int align_len = ARCH_DMA_MINALIGN - 1; 392 ulong count; 393 ulong size; 394 int sectors; 395 void *fit; 396 397 /* 398 * For FIT with external data, figure out where the external images 399 * start. This is the base for the data-offset properties in each 400 * image. 401 */ 402 size = fdt_totalsize(fit_header); 403 size = FIT_ALIGN(size); 404 *base_offset = FIT_ALIGN(size); 405 406 /* 407 * So far we only have one block of data from the FIT. Read the entire 408 * thing, including that first block, placing it so it finishes before 409 * where we will load the image. 410 * 411 * Note that we will load the image such that its first byte will be 412 * at the load address. Since that byte may be part-way through a 413 * block, we may load the image up to one block before the load 414 * address. So take account of that here by subtracting an addition 415 * block length from the FIT start position. 416 * 417 * In fact the FIT has its own load address, but we assume it cannot 418 * be before CONFIG_SYS_TEXT_BASE. 419 * 420 * For FIT with data embedded, data is loaded as part of FIT image. 421 * For FIT with external data, data is not loaded in this step. 422 */ 423 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - 424 align_len) & ~align_len); 425 sectors = get_aligned_image_size(info, size, 0); 426 count = info->read(info, sector, sectors, fit); 427 #ifdef CONFIG_MTD_BLK 428 mtd_blk_map_fit(info->dev, sector, fit); 429 #endif 430 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", 431 sector, sectors, fit, count); 432 if (count == 0) 433 return NULL; 434 435 return fit; 436 } 437 438 #ifdef CONFIG_SPL_KERNEL_BOOT 439 #ifdef CONFIG_SPL_LIBDISK_SUPPORT 440 __weak const char *spl_kernel_partition(struct spl_image_info *spl, 441 struct spl_load_info *info) 442 { 443 return PART_BOOT; 444 } 445 #endif 446 447 static int spl_load_kernel_fit(struct spl_image_info *spl_image, 448 struct spl_load_info *info) 449 { 450 /* 451 * Never change the image order. 452 * 453 * Considering thunder-boot feature, there maybe asynchronous 454 * loading operation of these images and ramdisk is usually to 455 * be the last one. 456 * 457 * The .its content rule of kernel fit image follows U-Boot proper. 458 */ 459 const char *images[] = { FIT_FDT_PROP, FIT_KERNEL_PROP, FIT_RAMDISK_PROP, }; 460 struct spl_image_info image_info; 461 char fit_header[info->bl_len]; 462 int images_noffset; 463 int base_offset; 464 int sector; 465 int node, ret, i; 466 void *fit; 467 468 if (spl_image->next_stage != SPL_NEXT_STAGE_KERNEL) 469 return 0; 470 471 #ifdef CONFIG_SPL_LIBDISK_SUPPORT 472 const char *part_name = PART_BOOT; 473 disk_partition_t part_info; 474 475 part_name = spl_kernel_partition(spl_image, info); 476 if (part_get_info_by_name(info->dev, part_name, &part_info) <= 0) { 477 printf("%s: no partition\n", __func__); 478 return -EINVAL; 479 } 480 sector = part_info.start; 481 #else 482 sector = CONFIG_SPL_KERNEL_BOOT_SECTOR; 483 #endif 484 if (info->read(info, sector, 1, &fit_header) != 1) { 485 debug("%s: Failed to read header\n", __func__); 486 return -EIO; 487 } 488 489 if (image_get_magic((void *)&fit_header) != FDT_MAGIC) { 490 printf("%s: Not fit magic\n", __func__); 491 return -EINVAL; 492 } 493 494 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset); 495 if (!fit) { 496 debug("%s: Cannot load blob\n", __func__); 497 return -ENODEV; 498 } 499 500 /* verify the configure node by keys, if required */ 501 #ifdef CONFIG_SPL_FIT_SIGNATURE 502 int conf_noffset; 503 504 conf_noffset = fit_conf_get_node(fit, NULL); 505 if (conf_noffset <= 0) { 506 printf("No default config node\n"); 507 return -EINVAL; 508 } 509 510 ret = fit_config_verify(fit, conf_noffset); 511 if (ret) { 512 printf("fit verify configure failed, ret=%d\n", ret); 513 return ret; 514 } 515 printf("\n"); 516 #endif 517 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 518 if (images_noffset < 0) { 519 debug("%s: Cannot find /images node: %d\n", 520 __func__, images_noffset); 521 return images_noffset; 522 } 523 524 for (i = 0; i < ARRAY_SIZE(images); i++) { 525 node = spl_fit_get_image_node(fit, images_noffset, 526 images[i], 0); 527 if (node < 0) { 528 debug("No image: %s\n", images[i]); 529 continue; 530 } 531 532 ret = spl_load_fit_image(info, sector, fit, base_offset, 533 node, &image_info); 534 if (ret) 535 return ret; 536 537 /* initial addr or entry point */ 538 if (!strcmp(images[i], FIT_FDT_PROP)) { 539 spl_image->fdt_addr = (void *)image_info.load_addr; 540 #ifdef CONFIG_SPL_AB 541 char slot_suffix[3] = {0}; 542 543 if (!spl_get_current_slot(info->dev, "misc", slot_suffix)) 544 fdt_bootargs_append_ab((void *)image_info.load_addr, slot_suffix); 545 #endif 546 547 #ifdef CONFIG_SPL_MTD_SUPPORT 548 struct blk_desc *desc = info->dev; 549 550 if (desc->devnum == BLK_MTD_SPI_NAND) 551 fdt_bootargs_append((void *)image_info.load_addr, mtd_part_parse()); 552 #endif 553 } else if (!strcmp(images[i], FIT_KERNEL_PROP)) { 554 #if CONFIG_IS_ENABLED(OPTEE) 555 spl_image->entry_point_os = image_info.load_addr; 556 #endif 557 #if CONFIG_IS_ENABLED(ATF) 558 spl_image->entry_point_bl33 = image_info.load_addr; 559 #endif 560 } 561 } 562 563 debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n", 564 (ulong)spl_image->fdt_addr, 565 spl_image->entry_point, 566 #if CONFIG_IS_ENABLED(OPTEE) 567 spl_image->entry_point_os); 568 #endif 569 #if CONFIG_IS_ENABLED(ATF) 570 spl_image->entry_point_bl33); 571 #endif 572 573 return 0; 574 } 575 #endif 576 577 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image, 578 struct spl_load_info *info, 579 ulong sector, void *fit_header) 580 { 581 struct spl_image_info image_info; 582 int base_offset; 583 int images, ret; 584 int index = 0; 585 int node = -1; 586 void *fit; 587 588 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset); 589 if (!fit) { 590 debug("%s: Cannot load blob\n", __func__); 591 return -1; 592 } 593 594 /* find the node holding the images information */ 595 images = fdt_path_offset(fit, FIT_IMAGES_PATH); 596 if (images < 0) { 597 debug("%s: Cannot find /images node: %d\n", __func__, images); 598 return -1; 599 } 600 601 /* if board sigs verify required, check self */ 602 if (fit_board_verify_required_sigs() && 603 !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) { 604 printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n"); 605 hang(); 606 } 607 608 /* verify the configure node by keys, if required */ 609 #ifdef CONFIG_SPL_FIT_SIGNATURE 610 int conf_noffset; 611 612 conf_noffset = fit_conf_get_node(fit, NULL); 613 if (conf_noffset <= 0) { 614 printf("No default config node\n"); 615 return -EINVAL; 616 } 617 618 ret = fit_config_verify(fit, conf_noffset); 619 if (ret) { 620 printf("fit verify configure failed, ret=%d\n", ret); 621 return ret; 622 } 623 printf("\n"); 624 625 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT 626 uint32_t this_index, min_index; 627 628 ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL, 629 &this_index, &min_index); 630 if (ret) { 631 printf("fit failed to get rollback index, ret=%d\n", ret); 632 return ret; 633 } else if (this_index < min_index) { 634 printf("fit reject rollback: %d < %d(min)\n", 635 this_index, min_index); 636 return -EINVAL; 637 } 638 639 printf("rollback index: %d >= %d(min), OK\n", this_index, min_index); 640 #endif 641 #endif 642 643 /* 644 * If required to start the other core before load "loadables" 645 * firmwares, use the config "standalone" to load the other core's 646 * firmware, then start it. 647 * Normally, different cores' firmware is attach to the config 648 * "loadables" and load them together. 649 */ 650 if (node < 0) 651 node = spl_fit_get_image_node(fit, images, FIT_STANDALONE_PROP, 652 0); 653 if (node > 0) { 654 /* Load the image and set up the spl_image structure */ 655 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 656 &image_info); 657 if (ret) 658 return ret; 659 660 if (image_info.entry_point == FDT_ERROR) 661 image_info.entry_point = image_info.load_addr; 662 663 ret = spl_fit_standalone_release(image_info.entry_point); 664 if (ret) 665 printf("Start standalone fail, ret = %d\n", ret); 666 667 /* standalone is special one, continue to find others */ 668 node = -1; 669 } 670 671 /* 672 * Find the U-Boot image using the following search order: 673 * - start at 'firmware' (e.g. an ARM Trusted Firmware) 674 * - fall back 'kernel' (e.g. a Falcon-mode OS boot 675 * - fall back to using the first 'loadables' entry 676 */ 677 if (node < 0) 678 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP, 679 0); 680 #ifdef CONFIG_SPL_OS_BOOT 681 if (node < 0) 682 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0); 683 #endif 684 if (node < 0) { 685 debug("could not find firmware image, trying loadables...\n"); 686 node = spl_fit_get_image_node(fit, images, "loadables", 0); 687 /* 688 * If we pick the U-Boot image from "loadables", start at 689 * the second image when later loading additional images. 690 */ 691 index = 1; 692 } 693 if (node < 0) { 694 debug("%s: Cannot find u-boot image node: %d\n", 695 __func__, node); 696 return -1; 697 } 698 699 /* Load the image and set up the spl_image structure */ 700 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 701 spl_image); 702 if (ret) 703 return ret; 704 705 /* 706 * For backward compatibility, we treat the first node that is 707 * as a U-Boot image, if no OS-type has been declared. 708 */ 709 if (!spl_fit_image_get_os(fit, node, &spl_image->os)) 710 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os)); 711 #if !defined(CONFIG_SPL_OS_BOOT) 712 else 713 spl_image->os = IH_OS_U_BOOT; 714 #endif 715 716 /* 717 * Booting a next-stage U-Boot may require us to append the FDT. 718 * We allow this to fail, as the U-Boot image might embed its FDT. 719 */ 720 if (spl_image->os == IH_OS_U_BOOT) 721 spl_fit_append_fdt(spl_image, info, sector, fit, 722 images, base_offset); 723 724 /* Now check if there are more images for us to load */ 725 for (; ; index++) { 726 uint8_t os_type = IH_OS_INVALID; 727 728 node = spl_fit_get_image_node(fit, images, "loadables", index); 729 if (node < 0) 730 break; 731 732 if (!spl_fit_image_get_os(fit, node, &os_type)) 733 debug("Loadable is %s\n", genimg_get_os_name(os_type)); 734 735 /* skip U-Boot ? */ 736 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL && 737 os_type == IH_OS_U_BOOT) 738 continue; 739 740 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 741 &image_info); 742 if (ret < 0) 743 return ret; 744 745 if (os_type == IH_OS_U_BOOT) { 746 #if CONFIG_IS_ENABLED(ATF) 747 spl_image->entry_point_bl33 = image_info.load_addr; 748 #elif CONFIG_IS_ENABLED(OPTEE) 749 spl_image->entry_point_os = image_info.load_addr; 750 #endif 751 spl_fit_append_fdt(&image_info, info, sector, 752 fit, images, base_offset); 753 spl_image->fdt_addr = image_info.fdt_addr; 754 } 755 756 /* 757 * If the "firmware" image did not provide an entry point, 758 * use the first valid entry point from the loadables. 759 */ 760 if (spl_image->entry_point == FDT_ERROR && 761 image_info.entry_point != FDT_ERROR) 762 spl_image->entry_point = image_info.entry_point; 763 764 /* Record our loadables into the FDT */ 765 if (spl_image->fdt_addr) 766 spl_fit_record_loadable(fit, images, index, 767 spl_image->fdt_addr, 768 &image_info); 769 } 770 771 /* 772 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's 773 * Makefile will set it to 0 and it will end up as the entry point 774 * here. What it actually means is: use the load address. 775 */ 776 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) 777 spl_image->entry_point = spl_image->load_addr; 778 779 return 0; 780 } 781 782 int spl_load_simple_fit(struct spl_image_info *spl_image, 783 struct spl_load_info *info, ulong sector, void *fit) 784 { 785 ulong sector_offs = sector; 786 int ret = -EINVAL; 787 int i; 788 789 for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) { 790 if (i > 0) { 791 sector_offs += 792 i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len); 793 printf("Trying fit image at 0x%lx sector\n", sector_offs); 794 if (info->read(info, sector_offs, 1, fit) != 1) { 795 printf("IO error\n"); 796 continue; 797 } 798 } 799 800 if (image_get_magic(fit) != FDT_MAGIC) { 801 printf("Not fit magic\n"); 802 continue; 803 } 804 805 ret = spl_internal_load_simple_fit(spl_image, info, 806 sector_offs, fit); 807 if (!ret) { 808 #ifdef CONFIG_SPL_KERNEL_BOOT 809 ret = spl_load_kernel_fit(spl_image, info); 810 #endif 811 break; 812 } 813 } 814 #ifdef CONFIG_SPL_AB 815 /* 816 * If boot fail in spl, spl must decrease 1. If boot 817 * successfully, it is no need to do that and U-boot will 818 * always to decrease 1. If in thunderboot process, 819 * always need to decrease 1. 820 */ 821 if (IS_ENABLED(CONFIG_SPL_KERNEL_BOOT) || ret) 822 spl_ab_decrease_tries(info->dev); 823 #endif 824 return ret; 825 } 826 827