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