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