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