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, info); 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(char *id, 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 printf("Trying kernel at 0x%x sector from '%s' part\n", sector, part_name); 485 486 if (info->read(info, sector, 1, &fit_header) != 1) { 487 debug("%s: Failed to read header\n", __func__); 488 return -EIO; 489 } 490 491 if (image_get_magic((void *)&fit_header) != FDT_MAGIC) { 492 printf("%s: Not fit magic\n", __func__); 493 return -EINVAL; 494 } 495 496 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset); 497 if (!fit) { 498 debug("%s: Cannot load blob\n", __func__); 499 return -ENODEV; 500 } 501 502 /* verify the configure node by keys, if required */ 503 #ifdef CONFIG_SPL_FIT_SIGNATURE 504 int conf_noffset; 505 506 conf_noffset = fit_conf_get_node(fit, NULL); 507 if (conf_noffset <= 0) { 508 printf("No default config node\n"); 509 return -EINVAL; 510 } 511 512 ret = fit_config_verify(fit, conf_noffset); 513 if (ret) { 514 printf("fit verify configure failed, ret=%d\n", ret); 515 return ret; 516 } 517 printf("\n"); 518 #endif 519 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 520 if (images_noffset < 0) { 521 debug("%s: Cannot find /images node: %d\n", 522 __func__, images_noffset); 523 return images_noffset; 524 } 525 526 for (i = 0; i < ARRAY_SIZE(images); i++) { 527 node = spl_fit_get_image_node(fit, images_noffset, 528 images[i], 0); 529 if (node < 0) { 530 debug("No image: %s\n", images[i]); 531 continue; 532 } 533 534 ret = spl_load_fit_image(info, sector, fit, base_offset, 535 node, &image_info); 536 if (ret) 537 return ret; 538 539 /* initial addr or entry point */ 540 if (!strcmp(images[i], FIT_FDT_PROP)) { 541 spl_image->fdt_addr = (void *)image_info.load_addr; 542 #ifdef CONFIG_SPL_AB 543 char slot_suffix[3] = {0}; 544 545 if (!spl_get_current_slot(info->dev, "misc", slot_suffix)) 546 fdt_bootargs_append_ab((void *)image_info.load_addr, slot_suffix); 547 #endif 548 549 #ifdef CONFIG_SPL_MTD_SUPPORT 550 struct blk_desc *desc = info->dev; 551 552 if (desc->devnum == BLK_MTD_SPI_NAND) 553 fdt_bootargs_append((void *)image_info.load_addr, mtd_part_parse(desc)); 554 #endif 555 } else if (!strcmp(images[i], FIT_KERNEL_PROP)) { 556 #if CONFIG_IS_ENABLED(OPTEE) 557 spl_image->entry_point_os = image_info.load_addr; 558 #endif 559 #if CONFIG_IS_ENABLED(ATF) 560 spl_image->entry_point_bl33 = image_info.load_addr; 561 #endif 562 } 563 } 564 565 debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n", 566 (ulong)spl_image->fdt_addr, 567 spl_image->entry_point, 568 #if CONFIG_IS_ENABLED(OPTEE) 569 spl_image->entry_point_os); 570 #endif 571 #if CONFIG_IS_ENABLED(ATF) 572 spl_image->entry_point_bl33); 573 #endif 574 575 return 0; 576 } 577 #endif 578 579 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image, 580 struct spl_load_info *info, 581 ulong sector, void *fit_header) 582 { 583 struct spl_image_info image_info; 584 char *desc; 585 int base_offset; 586 int images, ret; 587 int index = 0; 588 int node = -1; 589 void *fit; 590 591 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset); 592 if (!fit) { 593 debug("%s: Cannot load blob\n", __func__); 594 return -1; 595 } 596 597 /* find the node holding the images information */ 598 images = fdt_path_offset(fit, FIT_IMAGES_PATH); 599 if (images < 0) { 600 debug("%s: Cannot find /images node: %d\n", __func__, images); 601 return -1; 602 } 603 604 /* if board sigs verify required, check self */ 605 if (fit_board_verify_required_sigs() && 606 !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) { 607 printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n"); 608 hang(); 609 } 610 611 /* verify the configure node by keys, if required */ 612 #ifdef CONFIG_SPL_FIT_SIGNATURE 613 int conf_noffset; 614 615 conf_noffset = fit_conf_get_node(fit, NULL); 616 if (conf_noffset <= 0) { 617 printf("No default config node\n"); 618 return -EINVAL; 619 } 620 621 ret = fit_config_verify(fit, conf_noffset); 622 if (ret) { 623 printf("fit verify configure failed, ret=%d\n", ret); 624 return ret; 625 } 626 printf("\n"); 627 628 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT 629 uint32_t this_index, min_index; 630 631 ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL, 632 &this_index, &min_index); 633 if (ret) { 634 printf("fit failed to get rollback index, ret=%d\n", ret); 635 return ret; 636 } else if (this_index < min_index) { 637 printf("fit reject rollback: %d < %d(min)\n", 638 this_index, min_index); 639 return -EINVAL; 640 } 641 642 printf("rollback index: %d >= %d(min), OK\n", this_index, min_index); 643 #endif 644 #endif 645 646 /* 647 * If required to start the other core before load "loadables" 648 * firmwares, use the config "standalone" to load the other core's 649 * firmware, then start it. 650 * Normally, different cores' firmware is attach to the config 651 * "loadables" and load them together. 652 */ 653 for (; ; index++) { 654 node = spl_fit_get_image_node(fit, images, 655 FIT_STANDALONE_PROP, index); 656 if (node < 0) 657 break; 658 659 ret = spl_load_fit_image(info, sector, fit, base_offset, 660 node, &image_info); 661 if (ret) 662 return ret; 663 664 ret = fit_get_desc(fit, node, &desc); 665 if (ret) 666 return ret; 667 668 if (image_info.entry_point == FDT_ERROR) 669 image_info.entry_point = image_info.load_addr; 670 671 ret = spl_fit_standalone_release(desc, image_info.entry_point); 672 if (ret) 673 printf("%s: start standalone fail, ret=%d\n", desc, ret); 674 } 675 676 /* standalone is special one, continue to find others */ 677 node = -1; 678 index = 0; 679 680 /* 681 * Find the U-Boot image using the following search order: 682 * - start at 'firmware' (e.g. an ARM Trusted Firmware) 683 * - fall back 'kernel' (e.g. a Falcon-mode OS boot 684 * - fall back to using the first 'loadables' entry 685 */ 686 if (node < 0) 687 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP, 688 0); 689 #ifdef CONFIG_SPL_OS_BOOT 690 if (node < 0) 691 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0); 692 #endif 693 if (node < 0) { 694 debug("could not find firmware image, trying loadables...\n"); 695 node = spl_fit_get_image_node(fit, images, "loadables", 0); 696 /* 697 * If we pick the U-Boot image from "loadables", start at 698 * the second image when later loading additional images. 699 */ 700 index = 1; 701 } 702 if (node < 0) { 703 debug("%s: Cannot find u-boot image node: %d\n", 704 __func__, node); 705 return -1; 706 } 707 708 /* Load the image and set up the spl_image structure */ 709 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 710 spl_image); 711 if (ret) 712 return ret; 713 714 /* 715 * For backward compatibility, we treat the first node that is 716 * as a U-Boot image, if no OS-type has been declared. 717 */ 718 if (!spl_fit_image_get_os(fit, node, &spl_image->os)) 719 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os)); 720 #if !defined(CONFIG_SPL_OS_BOOT) 721 else 722 spl_image->os = IH_OS_U_BOOT; 723 #endif 724 725 /* 726 * Booting a next-stage U-Boot may require us to append the FDT. 727 * We allow this to fail, as the U-Boot image might embed its FDT. 728 */ 729 if (spl_image->os == IH_OS_U_BOOT) 730 spl_fit_append_fdt(spl_image, info, sector, fit, 731 images, base_offset); 732 733 /* Now check if there are more images for us to load */ 734 for (; ; index++) { 735 uint8_t os_type = IH_OS_INVALID; 736 737 node = spl_fit_get_image_node(fit, images, "loadables", index); 738 if (node < 0) 739 break; 740 741 if (!spl_fit_image_get_os(fit, node, &os_type)) 742 debug("Loadable is %s\n", genimg_get_os_name(os_type)); 743 744 /* skip U-Boot ? */ 745 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL && 746 os_type == IH_OS_U_BOOT) 747 continue; 748 749 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 750 &image_info); 751 if (ret < 0) 752 return ret; 753 754 if (os_type == IH_OS_U_BOOT) { 755 #if CONFIG_IS_ENABLED(ATF) 756 spl_image->entry_point_bl33 = image_info.load_addr; 757 #elif CONFIG_IS_ENABLED(OPTEE) 758 spl_image->entry_point_os = image_info.load_addr; 759 #endif 760 spl_fit_append_fdt(&image_info, info, sector, 761 fit, images, base_offset); 762 spl_image->fdt_addr = image_info.fdt_addr; 763 } 764 765 /* 766 * If the "firmware" image did not provide an entry point, 767 * use the first valid entry point from the loadables. 768 */ 769 if (spl_image->entry_point == FDT_ERROR && 770 image_info.entry_point != FDT_ERROR) 771 spl_image->entry_point = image_info.entry_point; 772 773 /* Record our loadables into the FDT */ 774 if (spl_image->fdt_addr) 775 spl_fit_record_loadable(fit, images, index, 776 spl_image->fdt_addr, 777 &image_info); 778 } 779 780 /* 781 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's 782 * Makefile will set it to 0 and it will end up as the entry point 783 * here. What it actually means is: use the load address. 784 */ 785 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) 786 spl_image->entry_point = spl_image->load_addr; 787 788 return 0; 789 } 790 791 int spl_load_simple_fit(struct spl_image_info *spl_image, 792 struct spl_load_info *info, ulong sector, void *fit) 793 { 794 ulong sector_offs = sector; 795 int ret = -EINVAL; 796 int i; 797 798 printf("Trying fit image at 0x%lx sector\n", sector_offs); 799 for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) { 800 if (i > 0) { 801 sector_offs += 802 i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len); 803 printf("Trying fit image at 0x%lx sector\n", sector_offs); 804 if (info->read(info, sector_offs, 1, fit) != 1) { 805 printf("IO error\n"); 806 continue; 807 } 808 } 809 810 if (image_get_magic(fit) != FDT_MAGIC) { 811 printf("Not fit magic\n"); 812 continue; 813 } 814 815 ret = spl_internal_load_simple_fit(spl_image, info, 816 sector_offs, fit); 817 if (!ret) { 818 #ifdef CONFIG_SPL_KERNEL_BOOT 819 ret = spl_load_kernel_fit(spl_image, info); 820 #endif 821 break; 822 } 823 } 824 #ifdef CONFIG_SPL_AB 825 /* If boot fail in spl, spl must decrease 1 and do_reset. */ 826 if (ret) 827 return spl_ab_decrease_reset(info->dev); 828 /* 829 * If boot successfully, it is no need to do decrease 830 * and U-boot will always decrease 1. 831 * If in thunderboot process, always need to decrease 1. 832 */ 833 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL) 834 spl_ab_decrease_tries(info->dev); 835 #endif 836 837 return ret; 838 } 839 840