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