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_SPL_MTD_SUPPORT 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 } else if (!strcmp(images[i], FIT_RAMDISK_PROP)) { 592 fdt_initrd(spl_image->fdt_addr, image_info.load_addr, 593 image_info.load_addr + image_info.size); 594 } 595 } 596 597 debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n", 598 (ulong)spl_image->fdt_addr, 599 spl_image->entry_point, 600 #if CONFIG_IS_ENABLED(OPTEE) 601 spl_image->entry_point_os); 602 #endif 603 #if CONFIG_IS_ENABLED(ATF) 604 spl_image->entry_point_bl33); 605 #endif 606 607 return 0; 608 } 609 #endif 610 611 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image, 612 struct spl_load_info *info, 613 ulong sector, void *fit_header) 614 { 615 struct spl_image_info image_info; 616 char *desc; 617 int base_offset; 618 int images, ret; 619 int index = 0; 620 int node = -1; 621 void *fit; 622 623 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset); 624 if (!fit) { 625 debug("%s: Cannot load blob\n", __func__); 626 return -1; 627 } 628 629 /* find the node holding the images information */ 630 images = fdt_path_offset(fit, FIT_IMAGES_PATH); 631 if (images < 0) { 632 debug("%s: Cannot find /images node: %d\n", __func__, images); 633 return -1; 634 } 635 636 /* if board sigs verify required, check self */ 637 if (fit_board_verify_required_sigs() && 638 !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) { 639 printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n"); 640 hang(); 641 } 642 643 /* verify the configure node by keys, if required */ 644 #ifdef CONFIG_SPL_FIT_SIGNATURE 645 int conf_noffset; 646 647 conf_noffset = fit_conf_get_node(fit, NULL); 648 if (conf_noffset <= 0) { 649 printf("No default config node\n"); 650 return -EINVAL; 651 } 652 653 ret = fit_config_verify(fit, conf_noffset); 654 if (ret) { 655 printf("fit verify configure failed, ret=%d\n", ret); 656 return ret; 657 } 658 printf("\n"); 659 660 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT 661 uint32_t this_index, min_index; 662 663 ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL, 664 &this_index, &min_index); 665 if (ret) { 666 printf("fit failed to get rollback index, ret=%d\n", ret); 667 return ret; 668 } else if (this_index < min_index) { 669 printf("fit reject rollback: %d < %d(min)\n", 670 this_index, min_index); 671 return -EINVAL; 672 } 673 674 printf("rollback index: %d >= %d(min), OK\n", this_index, min_index); 675 #endif 676 #endif 677 678 /* 679 * If required to start the other core before load "loadables" 680 * firmwares, use the config "standalone" to load the other core's 681 * firmware, then start it. 682 * Normally, different cores' firmware is attach to the config 683 * "loadables" and load them together. 684 */ 685 for (; ; index++) { 686 node = spl_fit_get_image_node(fit, images, 687 FIT_STANDALONE_PROP, index); 688 if (node < 0) 689 break; 690 691 ret = spl_load_fit_image(info, sector, fit, base_offset, 692 node, &image_info); 693 if (ret) 694 return ret; 695 696 ret = fit_get_desc(fit, node, &desc); 697 if (ret) 698 return ret; 699 700 if (image_info.entry_point == FDT_ERROR) 701 image_info.entry_point = image_info.load_addr; 702 703 flush_dcache_range(image_info.load_addr, 704 image_info.load_addr + image_info.size); 705 ret = spl_fit_standalone_release(desc, image_info.entry_point); 706 if (ret) 707 printf("%s: start standalone fail, ret=%d\n", desc, ret); 708 } 709 710 /* standalone is special one, continue to find others */ 711 node = -1; 712 index = 0; 713 714 /* 715 * Find the U-Boot image using the following search order: 716 * - start at 'firmware' (e.g. an ARM Trusted Firmware) 717 * - fall back 'kernel' (e.g. a Falcon-mode OS boot 718 * - fall back to using the first 'loadables' entry 719 */ 720 if (node < 0) 721 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP, 722 0); 723 #ifdef CONFIG_SPL_OS_BOOT 724 if (node < 0) 725 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0); 726 #endif 727 if (node < 0) { 728 debug("could not find firmware image, trying loadables...\n"); 729 node = spl_fit_get_image_node(fit, images, "loadables", 0); 730 /* 731 * If we pick the U-Boot image from "loadables", start at 732 * the second image when later loading additional images. 733 */ 734 index = 1; 735 } 736 if (node < 0) { 737 debug("%s: Cannot find u-boot image node: %d\n", 738 __func__, node); 739 return -1; 740 } 741 742 /* Load the image and set up the spl_image structure */ 743 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 744 spl_image); 745 if (ret) 746 return ret; 747 748 /* 749 * For backward compatibility, we treat the first node that is 750 * as a U-Boot image, if no OS-type has been declared. 751 */ 752 if (!spl_fit_image_get_os(fit, node, &spl_image->os)) 753 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os)); 754 #if !defined(CONFIG_SPL_OS_BOOT) 755 else 756 spl_image->os = IH_OS_U_BOOT; 757 #endif 758 759 /* Booting a next-stage U-Boot may require us to append the FDT. */ 760 if (spl_image->os == IH_OS_U_BOOT) { 761 ret = spl_fit_append_fdt(spl_image, info, sector, fit, 762 images, base_offset); 763 if (ret < 0) 764 return ret; 765 } 766 767 /* Now check if there are more images for us to load */ 768 for (; ; index++) { 769 uint8_t os_type = IH_OS_INVALID; 770 771 node = spl_fit_get_image_node(fit, images, "loadables", index); 772 if (node < 0) 773 break; 774 775 if (!spl_fit_image_get_os(fit, node, &os_type)) 776 debug("Loadable is %s\n", genimg_get_os_name(os_type)); 777 778 /* skip U-Boot ? */ 779 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL && 780 os_type == IH_OS_U_BOOT) 781 continue; 782 783 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 784 &image_info); 785 if (ret < 0) 786 return ret; 787 788 if (os_type == IH_OS_U_BOOT) { 789 #if CONFIG_IS_ENABLED(ATF) 790 spl_image->entry_point_bl33 = image_info.load_addr; 791 #elif CONFIG_IS_ENABLED(OPTEE) 792 spl_image->entry_point_os = image_info.load_addr; 793 #endif 794 ret = spl_fit_append_fdt(&image_info, info, sector, 795 fit, images, base_offset); 796 if (ret < 0) 797 return ret; 798 spl_image->fdt_addr = image_info.fdt_addr; 799 } 800 801 /* 802 * If the "firmware" image did not provide an entry point, 803 * use the first valid entry point from the loadables. 804 */ 805 if (spl_image->entry_point == FDT_ERROR && 806 image_info.entry_point != FDT_ERROR) 807 spl_image->entry_point = image_info.entry_point; 808 809 /* Record our loadables into the FDT */ 810 if (spl_image->fdt_addr && spl_image->next_stage == SPL_NEXT_STAGE_UBOOT) 811 spl_fit_record_loadable(fit, images, index, 812 spl_image->fdt_addr, 813 &image_info); 814 #if CONFIG_IS_ENABLED(ATF) 815 else if (os_type == IH_OS_OP_TEE) 816 spl_image->entry_point_bl32 = image_info.load_addr; 817 #endif 818 } 819 820 /* 821 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's 822 * Makefile will set it to 0 and it will end up as the entry point 823 * here. What it actually means is: use the load address. 824 */ 825 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) 826 spl_image->entry_point = spl_image->load_addr; 827 828 return 0; 829 } 830 831 int spl_load_simple_fit(struct spl_image_info *spl_image, 832 struct spl_load_info *info, ulong sector, void *fit) 833 { 834 ulong sector_offs = sector; 835 int ret = -EINVAL; 836 int i; 837 838 #ifdef CONFIG_MP_BOOT 839 mpb_init_1(*info); 840 #endif 841 842 printf("Trying fit image at 0x%lx sector\n", sector_offs); 843 for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) { 844 if (i > 0) { 845 sector_offs += 846 i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len); 847 printf("Trying fit image at 0x%lx sector\n", sector_offs); 848 if (info->read(info, sector_offs, 1, fit) != 1) { 849 printf("IO error\n"); 850 continue; 851 } 852 } 853 854 if (image_get_magic(fit) != FDT_MAGIC) { 855 printf("Not fit magic\n"); 856 continue; 857 } 858 859 ret = spl_internal_load_simple_fit(spl_image, info, 860 sector_offs, fit); 861 if (!ret) { 862 #ifdef CONFIG_SPL_KERNEL_BOOT 863 ret = spl_load_kernel_fit(spl_image, info); 864 #endif 865 break; 866 } 867 } 868 #ifdef CONFIG_SPL_AB 869 /* If boot fail in spl, spl must decrease 1 and do_reset. */ 870 if (ret) 871 return spl_ab_decrease_reset(info->dev); 872 /* 873 * If boot successfully, it is no need to do decrease 874 * and U-boot will always decrease 1. 875 * If in thunderboot process, always need to decrease 1. 876 */ 877 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL) 878 spl_ab_decrease_tries(info->dev); 879 #endif 880 881 return ret; 882 } 883 884