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