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 */
spl_fit_get_image_name(const void * fit,int images,const char * type,int index,char ** outname)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 */
spl_fit_get_image_node(const void * fit,int images,const char * type,int index)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
get_aligned_image_offset(struct spl_load_info * info,int offset)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
get_aligned_image_overhead(struct spl_load_info * info,int offset)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
get_aligned_image_size(struct spl_load_info * info,int data_size,int offset)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 */
spl_load_fit_image(struct spl_load_info * info,ulong sector,void * fit,ulong base_offset,int node,struct spl_image_info * image_info)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
spl_fit_append_fdt(struct spl_image_info * spl_image,struct spl_load_info * info,ulong sector,void * fit,int images,ulong base_offset)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 ret = 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 FDT node\n", __func__);
340 return ret;
341 }
342
343 image_info.load_addr =
344 (ulong)spl_image->fdt_addr + fdt_totalsize(spl_image->fdt_addr);
345 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
346 &image_info);
347
348 return ret;
349 }
350
spl_fit_record_loadable(const void * fit,int images,int index,void * blob,struct spl_image_info * image)351 static int spl_fit_record_loadable(const void *fit, int images, int index,
352 void *blob, struct spl_image_info *image)
353 {
354 int ret = 0;
355 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
356 char *name;
357 int node;
358
359 ret = spl_fit_get_image_name(fit, images, "loadables",
360 index, &name);
361 if (ret < 0)
362 return ret;
363
364 node = spl_fit_get_image_node(fit, images, "loadables", index);
365
366 ret = fdt_record_loadable(blob, index, name, image->load_addr,
367 image->size, image->entry_point,
368 fdt_getprop(fit, node, "type", NULL),
369 fdt_getprop(fit, node, "os", NULL));
370 #endif
371 return ret;
372 }
373
spl_fit_image_get_os(const void * fit,int noffset,uint8_t * os)374 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
375 {
376 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
377 return -ENOTSUPP;
378 #else
379 return fit_image_get_os(fit, noffset, os);
380 #endif
381 }
382
spl_fit_standalone_release(char * id,uintptr_t entry_point)383 __weak int spl_fit_standalone_release(char *id, uintptr_t entry_point)
384 {
385 return 0;
386 }
387
spl_fit_load_blob(struct spl_load_info * info,ulong sector,void * fit_header,int * base_offset)388 static void *spl_fit_load_blob(struct spl_load_info *info,
389 ulong sector, void *fit_header,
390 int *base_offset)
391 {
392 int align_len = ARCH_DMA_MINALIGN - 1;
393 ulong count;
394 ulong size;
395 int sectors;
396 void *fit;
397
398 /*
399 * For FIT with external data, figure out where the external images
400 * start. This is the base for the data-offset properties in each
401 * image.
402 */
403 size = fdt_totalsize(fit_header);
404 size = FIT_ALIGN(size);
405 *base_offset = FIT_ALIGN(size);
406
407 /*
408 * So far we only have one block of data from the FIT. Read the entire
409 * thing, including that first block, placing it so it finishes before
410 * where we will load the image.
411 *
412 * Note that we will load the image such that its first byte will be
413 * at the load address. Since that byte may be part-way through a
414 * block, we may load the image up to one block before the load
415 * address. So take account of that here by subtracting an addition
416 * block length from the FIT start position.
417 *
418 * In fact the FIT has its own load address, but we assume it cannot
419 * be before CONFIG_SYS_TEXT_BASE.
420 *
421 * For FIT with data embedded, data is loaded as part of FIT image.
422 * For FIT with external data, data is not loaded in this step.
423 */
424 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
425 align_len) & ~align_len);
426 sectors = get_aligned_image_size(info, size, 0);
427 count = info->read(info, sector, sectors, fit);
428 #ifdef CONFIG_MTD_BLK
429 mtd_blk_map_fit(info->dev, sector, fit);
430 #endif
431 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
432 sector, sectors, fit, count);
433 if (count == 0)
434 return NULL;
435
436 return fit;
437 }
438
439 #ifdef CONFIG_SPL_KERNEL_BOOT
440 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
spl_kernel_partition(struct spl_image_info * spl,struct spl_load_info * info)441 __weak const char *spl_kernel_partition(struct spl_image_info *spl,
442 struct spl_load_info *info)
443 {
444 return PART_BOOT;
445 }
446 #endif
447
spl_load_kernel_fit(struct spl_image_info * spl_image,struct spl_load_info * info)448 static int spl_load_kernel_fit(struct spl_image_info *spl_image,
449 struct spl_load_info *info)
450 {
451 /*
452 * Never change the image order.
453 *
454 * Considering thunder-boot feature, there maybe asynchronous
455 * loading operation of these images and ramdisk is usually to
456 * be the last one.
457 *
458 * The .its content rule of kernel fit image follows U-Boot proper.
459 */
460 const char *images[] = { FIT_FDT_PROP, FIT_KERNEL_PROP, FIT_RAMDISK_PROP, };
461 struct spl_image_info image_info;
462 char fit_header[info->bl_len];
463 int images_noffset;
464 int base_offset;
465 int sector;
466 int node, ret, i;
467 void *fit;
468
469 if (spl_image->next_stage != SPL_NEXT_STAGE_KERNEL)
470 return 0;
471
472 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
473 const char *part_name = PART_BOOT;
474 disk_partition_t part_info;
475
476 part_name = spl_kernel_partition(spl_image, info);
477 if (part_get_info_by_name(info->dev, part_name, &part_info) <= 0) {
478 printf("%s: no partition\n", __func__);
479 return -EINVAL;
480 }
481 sector = part_info.start;
482 #else
483 sector = CONFIG_SPL_KERNEL_BOOT_SECTOR;
484 #endif
485 printf("Trying kernel at 0x%x sector from '%s' part\n", sector, part_name);
486
487 if (info->read(info, sector, 1, &fit_header) != 1) {
488 debug("%s: Failed to read header\n", __func__);
489 return -EIO;
490 }
491
492 if (image_get_magic((void *)&fit_header) != FDT_MAGIC) {
493 printf("%s: Not fit magic\n", __func__);
494 return -EINVAL;
495 }
496
497 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
498 if (!fit) {
499 debug("%s: Cannot load blob\n", __func__);
500 return -ENODEV;
501 }
502
503 /* verify the configure node by keys, if required */
504 #ifdef CONFIG_SPL_FIT_SIGNATURE
505 int conf_noffset;
506
507 conf_noffset = fit_conf_get_node(fit, NULL);
508 if (conf_noffset <= 0) {
509 printf("No default config node\n");
510 return -EINVAL;
511 }
512
513 ret = fit_config_verify(fit, conf_noffset);
514 if (ret) {
515 printf("fit verify configure failed, ret=%d\n", ret);
516 return ret;
517 }
518 printf("\n");
519 #endif
520 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
521 if (images_noffset < 0) {
522 debug("%s: Cannot find /images node: %d\n",
523 __func__, images_noffset);
524 return images_noffset;
525 }
526
527 for (i = 0; i < ARRAY_SIZE(images); i++) {
528 node = spl_fit_get_image_node(fit, images_noffset,
529 images[i], 0);
530 if (node < 0) {
531 debug("No image: %s\n", images[i]);
532 continue;
533 }
534
535 ret = spl_load_fit_image(info, sector, fit, base_offset,
536 node, &image_info);
537 if (ret)
538 return ret;
539
540 /* initial addr or entry point */
541 if (!strcmp(images[i], FIT_FDT_PROP)) {
542 spl_image->fdt_addr = (void *)image_info.load_addr;
543 #ifdef CONFIG_SPL_AB
544 char slot_suffix[3] = {0};
545
546 if (!spl_get_current_slot(info->dev, "misc", slot_suffix))
547 fdt_bootargs_append_ab((void *)image_info.load_addr, slot_suffix);
548 #endif
549
550 #ifdef CONFIG_SPL_MTD_SUPPORT
551 struct blk_desc *desc = info->dev;
552
553 if (desc->devnum == BLK_MTD_SPI_NAND)
554 fdt_bootargs_append((void *)image_info.load_addr, mtd_part_parse(desc));
555 #endif
556 } else if (!strcmp(images[i], FIT_KERNEL_PROP)) {
557 #if CONFIG_IS_ENABLED(OPTEE)
558 spl_image->entry_point_os = image_info.load_addr;
559 #endif
560 #if CONFIG_IS_ENABLED(ATF)
561 spl_image->entry_point_bl33 = image_info.load_addr;
562 #endif
563 }
564 }
565
566 debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n",
567 (ulong)spl_image->fdt_addr,
568 spl_image->entry_point,
569 #if CONFIG_IS_ENABLED(OPTEE)
570 spl_image->entry_point_os);
571 #endif
572 #if CONFIG_IS_ENABLED(ATF)
573 spl_image->entry_point_bl33);
574 #endif
575
576 return 0;
577 }
578 #endif
579
spl_internal_load_simple_fit(struct spl_image_info * spl_image,struct spl_load_info * info,ulong sector,void * fit_header)580 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image,
581 struct spl_load_info *info,
582 ulong sector, void *fit_header)
583 {
584 struct spl_image_info image_info;
585 char *desc;
586 int base_offset;
587 int images, ret;
588 int index = 0;
589 int node = -1;
590 void *fit;
591
592 fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
593 if (!fit) {
594 debug("%s: Cannot load blob\n", __func__);
595 return -1;
596 }
597
598 /* find the node holding the images information */
599 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
600 if (images < 0) {
601 debug("%s: Cannot find /images node: %d\n", __func__, images);
602 return -1;
603 }
604
605 /* if board sigs verify required, check self */
606 if (fit_board_verify_required_sigs() &&
607 !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
608 printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n");
609 hang();
610 }
611
612 /* verify the configure node by keys, if required */
613 #ifdef CONFIG_SPL_FIT_SIGNATURE
614 int conf_noffset;
615
616 conf_noffset = fit_conf_get_node(fit, NULL);
617 if (conf_noffset <= 0) {
618 printf("No default config node\n");
619 return -EINVAL;
620 }
621
622 ret = fit_config_verify(fit, conf_noffset);
623 if (ret) {
624 printf("fit verify configure failed, ret=%d\n", ret);
625 return ret;
626 }
627 printf("\n");
628
629 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT
630 uint32_t this_index, min_index;
631
632 ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL,
633 &this_index, &min_index);
634 if (ret) {
635 printf("fit failed to get rollback index, ret=%d\n", ret);
636 return ret;
637 } else if (this_index < min_index) {
638 printf("fit reject rollback: %d < %d(min)\n",
639 this_index, min_index);
640 return -EINVAL;
641 }
642
643 printf("rollback index: %d >= %d(min), OK\n", this_index, min_index);
644 #endif
645 #endif
646
647 /*
648 * If required to start the other core before load "loadables"
649 * firmwares, use the config "standalone" to load the other core's
650 * firmware, then start it.
651 * Normally, different cores' firmware is attach to the config
652 * "loadables" and load them together.
653 */
654 for (; ; index++) {
655 node = spl_fit_get_image_node(fit, images,
656 FIT_STANDALONE_PROP, index);
657 if (node < 0)
658 break;
659
660 ret = spl_load_fit_image(info, sector, fit, base_offset,
661 node, &image_info);
662 if (ret)
663 return ret;
664
665 ret = fit_get_desc(fit, node, &desc);
666 if (ret)
667 return ret;
668
669 if (image_info.entry_point == FDT_ERROR)
670 image_info.entry_point = image_info.load_addr;
671
672 ret = spl_fit_standalone_release(desc, image_info.entry_point);
673 if (ret)
674 printf("%s: start standalone fail, ret=%d\n", desc, ret);
675 }
676
677 /* standalone is special one, continue to find others */
678 node = -1;
679 index = 0;
680
681 /*
682 * Find the U-Boot image using the following search order:
683 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
684 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
685 * - fall back to using the first 'loadables' entry
686 */
687 if (node < 0)
688 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
689 0);
690 #ifdef CONFIG_SPL_OS_BOOT
691 if (node < 0)
692 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
693 #endif
694 if (node < 0) {
695 debug("could not find firmware image, trying loadables...\n");
696 node = spl_fit_get_image_node(fit, images, "loadables", 0);
697 /*
698 * If we pick the U-Boot image from "loadables", start at
699 * the second image when later loading additional images.
700 */
701 index = 1;
702 }
703 if (node < 0) {
704 debug("%s: Cannot find u-boot image node: %d\n",
705 __func__, node);
706 return -1;
707 }
708
709 /* Load the image and set up the spl_image structure */
710 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
711 spl_image);
712 if (ret)
713 return ret;
714
715 /*
716 * For backward compatibility, we treat the first node that is
717 * as a U-Boot image, if no OS-type has been declared.
718 */
719 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
720 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
721 #if !defined(CONFIG_SPL_OS_BOOT)
722 else
723 spl_image->os = IH_OS_U_BOOT;
724 #endif
725
726 /*
727 * Booting a next-stage U-Boot may require us to append the FDT.
728 * We allow this to fail, as the U-Boot image might embed its FDT.
729 */
730 if (spl_image->os == IH_OS_U_BOOT)
731 spl_fit_append_fdt(spl_image, info, sector, fit,
732 images, base_offset);
733
734 /* Now check if there are more images for us to load */
735 for (; ; index++) {
736 uint8_t os_type = IH_OS_INVALID;
737
738 node = spl_fit_get_image_node(fit, images, "loadables", index);
739 if (node < 0)
740 break;
741
742 if (!spl_fit_image_get_os(fit, node, &os_type))
743 debug("Loadable is %s\n", genimg_get_os_name(os_type));
744
745 /* skip U-Boot ? */
746 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL &&
747 os_type == IH_OS_U_BOOT)
748 continue;
749
750 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
751 &image_info);
752 if (ret < 0)
753 return ret;
754
755 if (os_type == IH_OS_U_BOOT) {
756 #if CONFIG_IS_ENABLED(ATF)
757 spl_image->entry_point_bl33 = image_info.load_addr;
758 #elif CONFIG_IS_ENABLED(OPTEE)
759 spl_image->entry_point_os = image_info.load_addr;
760 #endif
761 spl_fit_append_fdt(&image_info, info, sector,
762 fit, images, base_offset);
763 spl_image->fdt_addr = image_info.fdt_addr;
764 }
765
766 /*
767 * If the "firmware" image did not provide an entry point,
768 * use the first valid entry point from the loadables.
769 */
770 if (spl_image->entry_point == FDT_ERROR &&
771 image_info.entry_point != FDT_ERROR)
772 spl_image->entry_point = image_info.entry_point;
773
774 /* Record our loadables into the FDT */
775 if (spl_image->fdt_addr)
776 spl_fit_record_loadable(fit, images, index,
777 spl_image->fdt_addr,
778 &image_info);
779 }
780
781 /*
782 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
783 * Makefile will set it to 0 and it will end up as the entry point
784 * here. What it actually means is: use the load address.
785 */
786 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
787 spl_image->entry_point = spl_image->load_addr;
788
789 return 0;
790 }
791
spl_load_simple_fit(struct spl_image_info * spl_image,struct spl_load_info * info,ulong sector,void * fit)792 int spl_load_simple_fit(struct spl_image_info *spl_image,
793 struct spl_load_info *info, ulong sector, void *fit)
794 {
795 ulong sector_offs = sector;
796 int ret = -EINVAL;
797 int i;
798
799 #ifdef CONFIG_MP_BOOT
800 mpb_init_1(*info);
801 #endif
802
803 printf("Trying fit image at 0x%lx sector\n", sector_offs);
804 for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) {
805 if (i > 0) {
806 sector_offs +=
807 i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len);
808 printf("Trying fit image at 0x%lx sector\n", sector_offs);
809 if (info->read(info, sector_offs, 1, fit) != 1) {
810 printf("IO error\n");
811 continue;
812 }
813 }
814
815 if (image_get_magic(fit) != FDT_MAGIC) {
816 printf("Not fit magic\n");
817 continue;
818 }
819
820 ret = spl_internal_load_simple_fit(spl_image, info,
821 sector_offs, fit);
822 if (!ret) {
823 #ifdef CONFIG_SPL_KERNEL_BOOT
824 ret = spl_load_kernel_fit(spl_image, info);
825 #endif
826 break;
827 }
828 }
829 #ifdef CONFIG_SPL_AB
830 /* If boot fail in spl, spl must decrease 1 and do_reset. */
831 if (ret)
832 return spl_ab_decrease_reset(info->dev);
833 /*
834 * If boot successfully, it is no need to do decrease
835 * and U-boot will always decrease 1.
836 * If in thunderboot process, always need to decrease 1.
837 */
838 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL)
839 spl_ab_decrease_tries(info->dev);
840 #endif
841
842 return ret;
843 }
844
845