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