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