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