xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision 4024d9e5d8d35ca6c69923fbc2c28f36dfae872f)
1f1dcee59SSimon Glass /*
2f1dcee59SSimon Glass  * Copyright (C) 2016 Google, Inc
3f1dcee59SSimon Glass  * Written by Simon Glass <sjg@chromium.org>
4f1dcee59SSimon Glass  *
5f1dcee59SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
6f1dcee59SSimon Glass  */
7f1dcee59SSimon Glass 
8f1dcee59SSimon Glass #include <common.h>
9e12dde2dSJoseph Chen #include <boot_rkimg.h>
10f1dcee59SSimon Glass #include <errno.h>
11a712f631SJason Zhu #include <fdt_support.h>
12f1dcee59SSimon Glass #include <image.h>
1390d1164aSKever Yang #include <malloc.h>
14a741b19cSJason Zhu #include <mtd_blk.h>
1536c449feSJoseph Chen #include <mp_boot.h>
161f5c7b64SJason Zhu #include <spl.h>
171f5c7b64SJason Zhu #include <spl_ab.h>
18a741b19cSJason Zhu #include <linux/libfdt.h>
19f1dcee59SSimon Glass 
20b81c4739SYork Sun #ifndef CONFIG_SYS_BOOTM_LEN
21b81c4739SYork Sun #define CONFIG_SYS_BOOTM_LEN	(64 << 20)
22b81c4739SYork Sun #endif
23b81c4739SYork Sun 
24736806fbSAndre Przywara /**
251f1cf67bSPhilipp Tomsich  * spl_fit_get_image_name(): By using the matching configuration subnode,
26736806fbSAndre Przywara  * retrieve the name of an image, specified by a property name and an index
27736806fbSAndre Przywara  * into that.
28736806fbSAndre Przywara  * @fit:	Pointer to the FDT blob.
29736806fbSAndre Przywara  * @images:	Offset of the /images subnode.
30736806fbSAndre Przywara  * @type:	Name of the property within the configuration subnode.
31736806fbSAndre Przywara  * @index:	Index into the list of strings in this property.
321f1cf67bSPhilipp Tomsich  * @outname:	Name of the image
33736806fbSAndre Przywara  *
341f1cf67bSPhilipp Tomsich  * Return:	0 on success, or a negative error number
35736806fbSAndre Przywara  */
361f1cf67bSPhilipp Tomsich static int spl_fit_get_image_name(const void *fit, int images,
371f1cf67bSPhilipp Tomsich 				  const char *type, int index,
381f1cf67bSPhilipp Tomsich 				  char **outname)
394b9340abSAndre Przywara {
404b9340abSAndre Przywara 	const char *name, *str;
411f1cf67bSPhilipp Tomsich 	__maybe_unused int node;
421f1cf67bSPhilipp Tomsich 	int conf_node;
434b9340abSAndre Przywara 	int len, i;
44f1dcee59SSimon Glass 
453863f840SCooper Jr., Franklin 	conf_node = fit_find_config_node(fit);
464b9340abSAndre Przywara 	if (conf_node < 0) {
47f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
48f1dcee59SSimon Glass 		printf("No matching DT out of these options:\n");
494b9340abSAndre Przywara 		for (node = fdt_first_subnode(fit, conf_node);
50f1dcee59SSimon Glass 		     node >= 0;
514b9340abSAndre Przywara 		     node = fdt_next_subnode(fit, node)) {
524b9340abSAndre Przywara 			name = fdt_getprop(fit, node, "description", &len);
53f1dcee59SSimon Glass 			printf("   %s\n", name);
54f1dcee59SSimon Glass 		}
55f1dcee59SSimon Glass #endif
564b9340abSAndre Przywara 		return conf_node;
574b9340abSAndre Przywara 	}
58f1dcee59SSimon Glass 
594b9340abSAndre Przywara 	name = fdt_getprop(fit, conf_node, type, &len);
604b9340abSAndre Przywara 	if (!name) {
614b9340abSAndre Przywara 		debug("cannot find property '%s': %d\n", type, len);
624b9340abSAndre Przywara 		return -EINVAL;
634b9340abSAndre Przywara 	}
644b9340abSAndre Przywara 
654b9340abSAndre Przywara 	str = name;
664b9340abSAndre Przywara 	for (i = 0; i < index; i++) {
674b9340abSAndre Przywara 		str = strchr(str, '\0') + 1;
684b9340abSAndre Przywara 		if (!str || (str - name >= len)) {
694b9340abSAndre Przywara 			debug("no string for index %d\n", index);
704b9340abSAndre Przywara 			return -E2BIG;
714b9340abSAndre Przywara 		}
724b9340abSAndre Przywara 	}
734b9340abSAndre Przywara 
741f1cf67bSPhilipp Tomsich 	*outname = (char *)str;
751f1cf67bSPhilipp Tomsich 	return 0;
761f1cf67bSPhilipp Tomsich }
771f1cf67bSPhilipp Tomsich 
781f1cf67bSPhilipp Tomsich /**
791f1cf67bSPhilipp Tomsich  * spl_fit_get_image_node(): By using the matching configuration subnode,
801f1cf67bSPhilipp Tomsich  * retrieve the name of an image, specified by a property name and an index
811f1cf67bSPhilipp Tomsich  * into that.
821f1cf67bSPhilipp Tomsich  * @fit:	Pointer to the FDT blob.
831f1cf67bSPhilipp Tomsich  * @images:	Offset of the /images subnode.
841f1cf67bSPhilipp Tomsich  * @type:	Name of the property within the configuration subnode.
851f1cf67bSPhilipp Tomsich  * @index:	Index into the list of strings in this property.
861f1cf67bSPhilipp Tomsich  *
871f1cf67bSPhilipp Tomsich  * Return:	the node offset of the respective image node or a negative
881f1cf67bSPhilipp Tomsich  *		error number.
891f1cf67bSPhilipp Tomsich  */
901f1cf67bSPhilipp Tomsich static int spl_fit_get_image_node(const void *fit, int images,
911f1cf67bSPhilipp Tomsich 				  const char *type, int index)
921f1cf67bSPhilipp Tomsich {
931f1cf67bSPhilipp Tomsich 	char *str;
941f1cf67bSPhilipp Tomsich 	int err;
951f1cf67bSPhilipp Tomsich 	int node;
961f1cf67bSPhilipp Tomsich 
971f1cf67bSPhilipp Tomsich 	err = spl_fit_get_image_name(fit, images, type, index, &str);
981f1cf67bSPhilipp Tomsich 	if (err)
991f1cf67bSPhilipp Tomsich 		return err;
1001f1cf67bSPhilipp Tomsich 
1014b9340abSAndre Przywara 	debug("%s: '%s'\n", type, str);
1021f1cf67bSPhilipp Tomsich 
1034b9340abSAndre Przywara 	node = fdt_subnode_offset(fit, images, str);
1044b9340abSAndre Przywara 	if (node < 0) {
1054b9340abSAndre Przywara 		debug("cannot find image node '%s': %d\n", str, node);
1064b9340abSAndre Przywara 		return -EINVAL;
1074b9340abSAndre Przywara 	}
1084b9340abSAndre Przywara 
109736806fbSAndre Przywara 	return node;
110f1dcee59SSimon Glass }
111f1dcee59SSimon Glass 
112eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset)
113eafd5410SLokesh Vutla {
114eafd5410SLokesh Vutla 	/*
115eafd5410SLokesh Vutla 	 * If it is a FS read, get the first address before offset which is
116eafd5410SLokesh Vutla 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
117eafd5410SLokesh Vutla 	 * block number to which offset belongs.
118eafd5410SLokesh Vutla 	 */
119eafd5410SLokesh Vutla 	if (info->filename)
120eafd5410SLokesh Vutla 		return offset & ~(ARCH_DMA_MINALIGN - 1);
121eafd5410SLokesh Vutla 
122eafd5410SLokesh Vutla 	return offset / info->bl_len;
123eafd5410SLokesh Vutla }
124eafd5410SLokesh Vutla 
125eafd5410SLokesh Vutla static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
126eafd5410SLokesh Vutla {
127eafd5410SLokesh Vutla 	/*
128eafd5410SLokesh Vutla 	 * If it is a FS read, get the difference between the offset and
129eafd5410SLokesh Vutla 	 * the first address before offset which is aligned to
130eafd5410SLokesh Vutla 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
131eafd5410SLokesh Vutla 	 * block.
132eafd5410SLokesh Vutla 	 */
133eafd5410SLokesh Vutla 	if (info->filename)
134eafd5410SLokesh Vutla 		return offset & (ARCH_DMA_MINALIGN - 1);
135eafd5410SLokesh Vutla 
136eafd5410SLokesh Vutla 	return offset % info->bl_len;
137eafd5410SLokesh Vutla }
138eafd5410SLokesh Vutla 
139eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size,
140eafd5410SLokesh Vutla 				  int offset)
141eafd5410SLokesh Vutla {
1423cc1f380SLokesh Vutla 	data_size = data_size + get_aligned_image_overhead(info, offset);
1433cc1f380SLokesh Vutla 
144eafd5410SLokesh Vutla 	if (info->filename)
1453cc1f380SLokesh Vutla 		return data_size;
146eafd5410SLokesh Vutla 
147eafd5410SLokesh Vutla 	return (data_size + info->bl_len - 1) / info->bl_len;
148eafd5410SLokesh Vutla }
149eafd5410SLokesh Vutla 
1508baa3818SAndre Przywara /**
1518baa3818SAndre Przywara  * spl_load_fit_image(): load the image described in a certain FIT node
1528baa3818SAndre Przywara  * @info:	points to information about the device to load data from
1538baa3818SAndre Przywara  * @sector:	the start sector of the FIT image on the device
1548baa3818SAndre Przywara  * @fit:	points to the flattened device tree blob describing the FIT
1558baa3818SAndre Przywara  *		image
1568baa3818SAndre Przywara  * @base_offset: the beginning of the data area containing the actual
1578baa3818SAndre Przywara  *		image data, relative to the beginning of the FIT
1588baa3818SAndre Przywara  * @node:	offset of the DT node describing the image to load (relative
1598baa3818SAndre Przywara  *		to @fit)
1608baa3818SAndre Przywara  * @image_info:	will be filled with information about the loaded image
1618baa3818SAndre Przywara  *		If the FIT node does not contain a "load" (address) property,
1628baa3818SAndre Przywara  *		the image gets loaded to the address pointed to by the
1638baa3818SAndre Przywara  *		load_addr member in this struct.
1648baa3818SAndre Przywara  *
1658baa3818SAndre Przywara  * Return:	0 on success or a negative error number.
1668baa3818SAndre Przywara  */
1678baa3818SAndre Przywara static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
1688baa3818SAndre Przywara 			      void *fit, ulong base_offset, int node,
1698baa3818SAndre Przywara 			      struct spl_image_info *image_info)
1708baa3818SAndre Przywara {
1710ea10b9fSYork Sun 	int offset;
1728baa3818SAndre Przywara 	size_t length;
1730ea10b9fSYork Sun 	int len;
174d18ad2e6SYork Sun 	ulong size;
175d3bfb68bSJoseph Chen 	ulong comp_addr, load_addr, load_ptr;
1768baa3818SAndre Przywara 	void *src;
1778baa3818SAndre Przywara 	ulong overhead;
1788baa3818SAndre Przywara 	int nr_sectors;
1798baa3818SAndre Przywara 	int align_len = ARCH_DMA_MINALIGN - 1;
180b81c4739SYork Sun 	uint8_t image_comp = -1, type = -1;
1810ea10b9fSYork Sun 	const void *data;
182b86dc419SPeng Fan 	bool external_data = false;
183b81c4739SYork Sun 
184b81c4739SYork Sun 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
185b81c4739SYork Sun 		if (fit_image_get_comp(fit, node, &image_comp))
186b81c4739SYork Sun 			puts("Cannot get image compression format.\n");
187b81c4739SYork Sun 		else
188b81c4739SYork Sun 			debug("%s ", genimg_get_comp_name(image_comp));
189b81c4739SYork Sun 
190b81c4739SYork Sun 		if (fit_image_get_type(fit, node, &type))
191b81c4739SYork Sun 			puts("Cannot get image type.\n");
192b81c4739SYork Sun 		else
193b81c4739SYork Sun 			debug("%s ", genimg_get_type_name(type));
194d3bfb68bSJoseph Chen 	} else {
195d3bfb68bSJoseph Chen 		fit_image_get_comp(fit, node, &image_comp);
196b81c4739SYork Sun 	}
1978baa3818SAndre Przywara 
1980ea10b9fSYork Sun 	if (fit_image_get_load(fit, node, &load_addr))
1998baa3818SAndre Przywara 		load_addr = image_info->load_addr;
2000ea10b9fSYork Sun 
201d3bfb68bSJoseph Chen 	if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE) {
2023e9875cdSJoseph Chen 		/* Empirically, 2MB is enough for U-Boot, tee and atf */
203d3bfb68bSJoseph Chen 		if (fit_image_get_comp_addr(fit, node, &comp_addr))
2043e9875cdSJoseph Chen 			comp_addr = load_addr + FIT_MAX_SPL_IMAGE_SZ;
205d3bfb68bSJoseph Chen 	} else {
206d3bfb68bSJoseph Chen 		comp_addr = load_addr;
207d3bfb68bSJoseph Chen 	}
208d3bfb68bSJoseph Chen 
209b86dc419SPeng Fan 	if (!fit_image_get_data_position(fit, node, &offset)) {
210b86dc419SPeng Fan 		external_data = true;
211b86dc419SPeng Fan 	} else if (!fit_image_get_data_offset(fit, node, &offset)) {
2120ea10b9fSYork Sun 		offset += base_offset;
213b86dc419SPeng Fan 		external_data = true;
214b86dc419SPeng Fan 	}
215b86dc419SPeng Fan 
216b86dc419SPeng Fan 	if (external_data) {
217b86dc419SPeng Fan 		/* External data */
2180ea10b9fSYork Sun 		if (fit_image_get_data_size(fit, node, &len))
2190ea10b9fSYork Sun 			return -ENOENT;
2200ea10b9fSYork Sun 
221d3bfb68bSJoseph Chen 		load_ptr = (comp_addr + align_len) & ~align_len;
22263363c40SAndy Yan #if  defined(CONFIG_ARCH_ROCKCHIP)
223a5e7a2d4SKever Yang 		if ((load_ptr < CONFIG_SYS_SDRAM_BASE) ||
224a5e7a2d4SKever Yang 		     (load_ptr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE))
22590d1164aSKever Yang 			load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len);
22690d1164aSKever Yang #endif
2270ea10b9fSYork Sun 		length = len;
2288baa3818SAndre Przywara 
2298baa3818SAndre Przywara 		overhead = get_aligned_image_overhead(info, offset);
2308baa3818SAndre Przywara 		nr_sectors = get_aligned_image_size(info, length, offset);
2318baa3818SAndre Przywara 
2320ea10b9fSYork Sun 		if (info->read(info,
2330ea10b9fSYork Sun 			       sector + get_aligned_image_offset(info, offset),
2348baa3818SAndre Przywara 			       nr_sectors, (void *)load_ptr) != nr_sectors)
2358baa3818SAndre Przywara 			return -EIO;
2368baa3818SAndre Przywara 
2370ea10b9fSYork Sun 		debug("External data: dst=%lx, offset=%x, size=%lx\n",
2380ea10b9fSYork Sun 		      load_ptr, offset, (unsigned long)length);
2398baa3818SAndre Przywara 		src = (void *)load_ptr + overhead;
2400ea10b9fSYork Sun 	} else {
2410ea10b9fSYork Sun 		/* Embedded data */
2420ea10b9fSYork Sun 		if (fit_image_get_data(fit, node, &data, &length)) {
2430ea10b9fSYork Sun 			puts("Cannot get image data/size\n");
2440ea10b9fSYork Sun 			return -ENOENT;
2450ea10b9fSYork Sun 		}
2460ea10b9fSYork Sun 		debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
2470ea10b9fSYork Sun 		      (unsigned long)length);
2480ea10b9fSYork Sun 		src = (void *)data;
2490ea10b9fSYork Sun 	}
2500ea10b9fSYork Sun 
251a515b6d2SJoseph Chen 	/* Check hashes and signature */
252d3bfb68bSJoseph Chen 	if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE)
253d3bfb68bSJoseph Chen 		printf("## Checking %s 0x%08lx (%s @0x%08lx) ... ",
254d3bfb68bSJoseph Chen 		       fit_get_name(fit, node, NULL), load_addr,
255d3bfb68bSJoseph Chen 		       (char *)fdt_getprop(fit, node, FIT_COMP_PROP, NULL),
256d3bfb68bSJoseph Chen 		       (long)src);
257d3bfb68bSJoseph Chen 	else
258d3bfb68bSJoseph Chen 		printf("## Checking %s 0x%08lx ... ",
259d3bfb68bSJoseph Chen 		       fit_get_name(fit, node, NULL), load_addr);
260d3bfb68bSJoseph Chen 
261b92c7cf4SJoseph Chen #ifdef CONFIG_FIT_SPL_PRINT
262b92c7cf4SJoseph Chen 	printf("\n");
263b92c7cf4SJoseph Chen 	fit_image_print(fit, node, "");
264b92c7cf4SJoseph Chen #endif
265f3219d95SBen Whitten 	if (!fit_image_verify_with_data(fit, node,
266f3219d95SBen Whitten 					 src, length))
267f3219d95SBen Whitten 		return -EPERM;
268f3219d95SBen Whitten 
2698baa3818SAndre Przywara #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
2709c633288SJoseph Chen 	board_fit_image_post_process(fit, node, (ulong *)&load_addr,
2719e67e3ddSJoseph Chen 				     (ulong **)&src, &length, info);
2728baa3818SAndre Przywara #endif
27350466f58SJoseph Chen 	puts("OK\n");
2748baa3818SAndre Przywara 
275b81c4739SYork Sun 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT)	&&
276b81c4739SYork Sun 	    IS_ENABLED(CONFIG_SPL_GZIP)		&&
277b81c4739SYork Sun 	    image_comp == IH_COMP_GZIP		&&
278b81c4739SYork Sun 	    type == IH_TYPE_KERNEL) {
279d18ad2e6SYork Sun 		size = length;
280b81c4739SYork Sun 		if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
281d18ad2e6SYork Sun 			   src, &size)) {
282b81c4739SYork Sun 			puts("Uncompressing error\n");
283b81c4739SYork Sun 			return -EIO;
284b81c4739SYork Sun 		}
285d18ad2e6SYork Sun 		length = size;
286b81c4739SYork Sun 	} else {
2878baa3818SAndre Przywara 		memcpy((void *)load_addr, src, length);
288b81c4739SYork Sun 	}
2898baa3818SAndre Przywara 
2908baa3818SAndre Przywara 	if (image_info) {
2918baa3818SAndre Przywara 		image_info->load_addr = load_addr;
2928baa3818SAndre Przywara 		image_info->size = length;
2938baa3818SAndre Przywara 		image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
2948baa3818SAndre Przywara 	}
2958baa3818SAndre Przywara 
2968baa3818SAndre Przywara 	return 0;
2978baa3818SAndre Przywara }
2988baa3818SAndre Przywara 
2999719e14fSPhilipp Tomsich static int spl_fit_append_fdt(struct spl_image_info *spl_image,
3009719e14fSPhilipp Tomsich 			      struct spl_load_info *info, ulong sector,
3019719e14fSPhilipp Tomsich 			      void *fit, int images, ulong base_offset)
3029719e14fSPhilipp Tomsich {
3039719e14fSPhilipp Tomsich 	struct spl_image_info image_info;
3049719e14fSPhilipp Tomsich 	int node, ret;
3059719e14fSPhilipp Tomsich 
3069719e14fSPhilipp Tomsich 	/* Figure out which device tree the board wants to use */
3079719e14fSPhilipp Tomsich 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
3089719e14fSPhilipp Tomsich 	if (node < 0) {
3099719e14fSPhilipp Tomsich 		debug("%s: cannot find FDT node\n", __func__);
3109719e14fSPhilipp Tomsich 		return node;
3119719e14fSPhilipp Tomsich 	}
3129719e14fSPhilipp Tomsich 
3139719e14fSPhilipp Tomsich 	/*
3149719e14fSPhilipp Tomsich 	 * Read the device tree and place it after the image.
3159719e14fSPhilipp Tomsich 	 * Align the destination address to ARCH_DMA_MINALIGN.
3169719e14fSPhilipp Tomsich 	 */
3179719e14fSPhilipp Tomsich 	image_info.load_addr = spl_image->load_addr + spl_image->size;
3189719e14fSPhilipp Tomsich 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
3199719e14fSPhilipp Tomsich 				 &image_info);
3201f1cf67bSPhilipp Tomsich 
3211f1cf67bSPhilipp Tomsich 	if (ret < 0)
3221f1cf67bSPhilipp Tomsich 		return ret;
3231f1cf67bSPhilipp Tomsich 
3241f1cf67bSPhilipp Tomsich 	/* Make the load-address of the FDT available for the SPL framework */
3251f1cf67bSPhilipp Tomsich 	spl_image->fdt_addr = (void *)image_info.load_addr;
3268118cce3SPhilipp Tomsich #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
3271f1cf67bSPhilipp Tomsich 	/* Try to make space, so we can inject details on the loadables */
328*4024d9e5SJoseph Chen 	fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
3298118cce3SPhilipp Tomsich #endif
3301f1cf67bSPhilipp Tomsich 
3319d23fbe3SJoseph Chen 	/*
3329d23fbe3SJoseph Chen 	 * If need, load kernel FDT right after U-Boot FDT.
3339d23fbe3SJoseph Chen 	 *
3349d23fbe3SJoseph Chen 	 * kernel FDT is for U-Boot if there is not valid one
3359d23fbe3SJoseph Chen 	 * from images, ie: resource.img, boot.img or recovery.img.
3369d23fbe3SJoseph Chen 	 */
3370f74a651SJoseph Chen 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 1);
3380f74a651SJoseph Chen 	if (node < 0) {
339*4024d9e5SJoseph Chen 		debug("%s: cannot find kernel FDT node\n", __func__);
340*4024d9e5SJoseph Chen 		/* attention: here return ret but not node */
3419d23fbe3SJoseph Chen 		return ret;
3420f74a651SJoseph Chen 	}
3439d23fbe3SJoseph Chen 
3449d23fbe3SJoseph Chen 	image_info.load_addr =
3459d23fbe3SJoseph Chen 		(ulong)spl_image->fdt_addr + fdt_totalsize(spl_image->fdt_addr);
3469d23fbe3SJoseph Chen 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
3479d23fbe3SJoseph Chen 				 &image_info);
3489d23fbe3SJoseph Chen 
3491f1cf67bSPhilipp Tomsich 	return ret;
3501f1cf67bSPhilipp Tomsich }
3511f1cf67bSPhilipp Tomsich 
3521f1cf67bSPhilipp Tomsich static int spl_fit_record_loadable(const void *fit, int images, int index,
3531f1cf67bSPhilipp Tomsich 				   void *blob, struct spl_image_info *image)
3541f1cf67bSPhilipp Tomsich {
3558118cce3SPhilipp Tomsich 	int ret = 0;
3568118cce3SPhilipp Tomsich #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
3571f1cf67bSPhilipp Tomsich 	char *name;
3588118cce3SPhilipp Tomsich 	int node;
3591f1cf67bSPhilipp Tomsich 
3601f1cf67bSPhilipp Tomsich 	ret = spl_fit_get_image_name(fit, images, "loadables",
3611f1cf67bSPhilipp Tomsich 				     index, &name);
3621f1cf67bSPhilipp Tomsich 	if (ret < 0)
3631f1cf67bSPhilipp Tomsich 		return ret;
3641f1cf67bSPhilipp Tomsich 
3651f1cf67bSPhilipp Tomsich 	node = spl_fit_get_image_node(fit, images, "loadables", index);
3661f1cf67bSPhilipp Tomsich 
3671f1cf67bSPhilipp Tomsich 	ret = fdt_record_loadable(blob, index, name, image->load_addr,
3681f1cf67bSPhilipp Tomsich 				  image->size, image->entry_point,
3691f1cf67bSPhilipp Tomsich 				  fdt_getprop(fit, node, "type", NULL),
3701f1cf67bSPhilipp Tomsich 				  fdt_getprop(fit, node, "os", NULL));
3718118cce3SPhilipp Tomsich #endif
3729719e14fSPhilipp Tomsich 	return ret;
3739719e14fSPhilipp Tomsich }
3749719e14fSPhilipp Tomsich 
3758118cce3SPhilipp Tomsich static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
3768118cce3SPhilipp Tomsich {
3778118cce3SPhilipp Tomsich #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
3788118cce3SPhilipp Tomsich 	return -ENOTSUPP;
3798118cce3SPhilipp Tomsich #else
3808118cce3SPhilipp Tomsich 	return fit_image_get_os(fit, noffset, os);
3818118cce3SPhilipp Tomsich #endif
3828118cce3SPhilipp Tomsich }
3838118cce3SPhilipp Tomsich 
384c1006ae0SJoseph Chen __weak int spl_fit_standalone_release(char *id, uintptr_t entry_point)
385342d050eSJason Zhu {
386342d050eSJason Zhu 	return 0;
387342d050eSJason Zhu }
388342d050eSJason Zhu 
389569a1737SJoseph Chen static void *spl_fit_load_blob(struct spl_load_info *info,
390569a1737SJoseph Chen 			       ulong sector, void *fit_header,
391569a1737SJoseph Chen 			       int *base_offset)
392f1dcee59SSimon Glass {
393569a1737SJoseph Chen 	int align_len = ARCH_DMA_MINALIGN - 1;
394569a1737SJoseph Chen 	ulong count;
3958baa3818SAndre Przywara 	ulong size;
396569a1737SJoseph Chen 	int sectors;
397569a1737SJoseph Chen 	void *fit;
398f1dcee59SSimon Glass 
399f1dcee59SSimon Glass 	/*
400ebec805aSYork Sun 	 * For FIT with external data, figure out where the external images
401ebec805aSYork Sun 	 * start. This is the base for the data-offset properties in each
402ebec805aSYork Sun 	 * image.
403f1dcee59SSimon Glass 	 */
404569a1737SJoseph Chen 	size = fdt_totalsize(fit_header);
405acd43290SJoseph Chen 	size = FIT_ALIGN(size);
406569a1737SJoseph Chen 	*base_offset = FIT_ALIGN(size);
407f1dcee59SSimon Glass 
408f1dcee59SSimon Glass 	/*
409f1dcee59SSimon Glass 	 * So far we only have one block of data from the FIT. Read the entire
410f1dcee59SSimon Glass 	 * thing, including that first block, placing it so it finishes before
411f1dcee59SSimon Glass 	 * where we will load the image.
412f1dcee59SSimon Glass 	 *
413f1dcee59SSimon Glass 	 * Note that we will load the image such that its first byte will be
414f1dcee59SSimon Glass 	 * at the load address. Since that byte may be part-way through a
415f1dcee59SSimon Glass 	 * block, we may load the image up to one block before the load
416f1dcee59SSimon Glass 	 * address. So take account of that here by subtracting an addition
417f1dcee59SSimon Glass 	 * block length from the FIT start position.
418f1dcee59SSimon Glass 	 *
419f1dcee59SSimon Glass 	 * In fact the FIT has its own load address, but we assume it cannot
420f1dcee59SSimon Glass 	 * be before CONFIG_SYS_TEXT_BASE.
421ebec805aSYork Sun 	 *
422ebec805aSYork Sun 	 * For FIT with data embedded, data is loaded as part of FIT image.
423ebec805aSYork Sun 	 * For FIT with external data, data is not loaded in this step.
424f1dcee59SSimon Glass 	 */
4258b528709SLokesh Vutla 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
4268b528709SLokesh Vutla 			align_len) & ~align_len);
427eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, size, 0);
428f1dcee59SSimon Glass 	count = info->read(info, sector, sectors, fit);
429a741b19cSJason Zhu #ifdef CONFIG_MTD_BLK
430a741b19cSJason Zhu 	mtd_blk_map_fit(info->dev, sector, fit);
431a741b19cSJason Zhu #endif
432f1dcee59SSimon Glass 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
433f1dcee59SSimon Glass 	      sector, sectors, fit, count);
434f1dcee59SSimon Glass 	if (count == 0)
435569a1737SJoseph Chen 		return NULL;
436569a1737SJoseph Chen 
437569a1737SJoseph Chen 	return fit;
438569a1737SJoseph Chen }
439569a1737SJoseph Chen 
440a00b516fSJoseph Chen #ifdef CONFIG_SPL_KERNEL_BOOT
441e12dde2dSJoseph Chen #ifdef CONFIG_SPL_LIBDISK_SUPPORT
442e12dde2dSJoseph Chen __weak const char *spl_kernel_partition(struct spl_image_info *spl,
443e12dde2dSJoseph Chen 					struct spl_load_info *info)
444e12dde2dSJoseph Chen {
445e12dde2dSJoseph Chen 	return PART_BOOT;
446e12dde2dSJoseph Chen }
447e12dde2dSJoseph Chen #endif
448e12dde2dSJoseph Chen 
449e12dde2dSJoseph Chen static int spl_load_kernel_fit(struct spl_image_info *spl_image,
450e12dde2dSJoseph Chen 			       struct spl_load_info *info)
451e12dde2dSJoseph Chen {
452e12dde2dSJoseph Chen 	/*
453e12dde2dSJoseph Chen 	 * Never change the image order.
454e12dde2dSJoseph Chen 	 *
455e12dde2dSJoseph Chen 	 * Considering thunder-boot feature, there maybe asynchronous
456e12dde2dSJoseph Chen 	 * loading operation of these images and ramdisk is usually to
457e12dde2dSJoseph Chen 	 * be the last one.
458e12dde2dSJoseph Chen 	 *
459e12dde2dSJoseph Chen 	 * The .its content rule of kernel fit image follows U-Boot proper.
460e12dde2dSJoseph Chen 	 */
461e12dde2dSJoseph Chen 	const char *images[] = { FIT_FDT_PROP, FIT_KERNEL_PROP, FIT_RAMDISK_PROP, };
462e12dde2dSJoseph Chen 	struct spl_image_info image_info;
463e12dde2dSJoseph Chen 	char fit_header[info->bl_len];
464e12dde2dSJoseph Chen 	int images_noffset;
465e12dde2dSJoseph Chen 	int base_offset;
466e12dde2dSJoseph Chen 	int sector;
467e12dde2dSJoseph Chen 	int node, ret, i;
468e12dde2dSJoseph Chen 	void *fit;
469e12dde2dSJoseph Chen 
470e12dde2dSJoseph Chen 	if (spl_image->next_stage != SPL_NEXT_STAGE_KERNEL)
471e12dde2dSJoseph Chen 		return 0;
472e12dde2dSJoseph Chen 
473e12dde2dSJoseph Chen #ifdef CONFIG_SPL_LIBDISK_SUPPORT
474e12dde2dSJoseph Chen 	const char *part_name = PART_BOOT;
475e12dde2dSJoseph Chen 	disk_partition_t part_info;
476e12dde2dSJoseph Chen 
477e12dde2dSJoseph Chen 	part_name = spl_kernel_partition(spl_image, info);
478e12dde2dSJoseph Chen 	if (part_get_info_by_name(info->dev, part_name, &part_info) <= 0) {
479e12dde2dSJoseph Chen 		printf("%s: no partition\n", __func__);
480e12dde2dSJoseph Chen 		return -EINVAL;
481e12dde2dSJoseph Chen 	}
482e12dde2dSJoseph Chen 	sector = part_info.start;
483e12dde2dSJoseph Chen #else
484a00b516fSJoseph Chen 	sector = CONFIG_SPL_KERNEL_BOOT_SECTOR;
485e12dde2dSJoseph Chen #endif
4869d3df7afSJoseph Chen 	printf("Trying kernel at 0x%x sector from '%s' part\n", sector, part_name);
4879d3df7afSJoseph Chen 
488e12dde2dSJoseph Chen 	if (info->read(info, sector, 1, &fit_header) != 1) {
48929725e85SJoseph Chen 		debug("%s: Failed to read header\n", __func__);
490e12dde2dSJoseph Chen 		return -EIO;
491e12dde2dSJoseph Chen 	}
492e12dde2dSJoseph Chen 
493e12dde2dSJoseph Chen 	if (image_get_magic((void *)&fit_header) != FDT_MAGIC) {
49429725e85SJoseph Chen 		printf("%s: Not fit magic\n", __func__);
495e12dde2dSJoseph Chen 		return -EINVAL;
496e12dde2dSJoseph Chen 	}
497e12dde2dSJoseph Chen 
498e12dde2dSJoseph Chen 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
499e12dde2dSJoseph Chen 	if (!fit) {
500e12dde2dSJoseph Chen 		debug("%s: Cannot load blob\n", __func__);
501e12dde2dSJoseph Chen 		return -ENODEV;
502e12dde2dSJoseph Chen 	}
503e12dde2dSJoseph Chen 
504e12dde2dSJoseph Chen 	/* verify the configure node by keys, if required */
505e12dde2dSJoseph Chen #ifdef CONFIG_SPL_FIT_SIGNATURE
506e12dde2dSJoseph Chen 	int conf_noffset;
507e12dde2dSJoseph Chen 
508e12dde2dSJoseph Chen 	conf_noffset = fit_conf_get_node(fit, NULL);
509e12dde2dSJoseph Chen 	if (conf_noffset <= 0) {
510e12dde2dSJoseph Chen 		printf("No default config node\n");
511e12dde2dSJoseph Chen 		return -EINVAL;
512e12dde2dSJoseph Chen 	}
513e12dde2dSJoseph Chen 
514e12dde2dSJoseph Chen 	ret = fit_config_verify(fit, conf_noffset);
515e12dde2dSJoseph Chen 	if (ret) {
516e12dde2dSJoseph Chen 		printf("fit verify configure failed, ret=%d\n", ret);
517e12dde2dSJoseph Chen 		return ret;
518e12dde2dSJoseph Chen 	}
519e12dde2dSJoseph Chen 	printf("\n");
520e12dde2dSJoseph Chen #endif
521e12dde2dSJoseph Chen 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
522e12dde2dSJoseph Chen 	if (images_noffset < 0) {
523e12dde2dSJoseph Chen 		debug("%s: Cannot find /images node: %d\n",
524e12dde2dSJoseph Chen 		      __func__, images_noffset);
525e12dde2dSJoseph Chen 		return images_noffset;
526e12dde2dSJoseph Chen 	}
527e12dde2dSJoseph Chen 
528e12dde2dSJoseph Chen 	for (i = 0; i < ARRAY_SIZE(images); i++) {
529e12dde2dSJoseph Chen 		node = spl_fit_get_image_node(fit, images_noffset,
530e12dde2dSJoseph Chen 					      images[i], 0);
531e12dde2dSJoseph Chen 		if (node < 0) {
532e12dde2dSJoseph Chen 			debug("No image: %s\n", images[i]);
533e12dde2dSJoseph Chen 			continue;
534e12dde2dSJoseph Chen 		}
535e12dde2dSJoseph Chen 
536e12dde2dSJoseph Chen 		ret = spl_load_fit_image(info, sector, fit, base_offset,
537e12dde2dSJoseph Chen 					 node, &image_info);
538e12dde2dSJoseph Chen 		if (ret)
539e12dde2dSJoseph Chen 			return ret;
540e12dde2dSJoseph Chen 
541e12dde2dSJoseph Chen 		/* initial addr or entry point */
542a712f631SJason Zhu 		if (!strcmp(images[i], FIT_FDT_PROP)) {
543e12dde2dSJoseph Chen 			spl_image->fdt_addr = (void *)image_info.load_addr;
544a712f631SJason Zhu #ifdef CONFIG_SPL_AB
545a712f631SJason Zhu 			char slot_suffix[3] = {0};
546a712f631SJason Zhu 
547a712f631SJason Zhu 			if (!spl_get_current_slot(info->dev, "misc", slot_suffix))
548a712f631SJason Zhu 				fdt_bootargs_append_ab((void *)image_info.load_addr, slot_suffix);
549a712f631SJason Zhu #endif
550c4ad0400SJason Zhu 
551c4ad0400SJason Zhu #ifdef CONFIG_SPL_MTD_SUPPORT
552c4ad0400SJason Zhu 			struct blk_desc *desc = info->dev;
553c4ad0400SJason Zhu 
554c4ad0400SJason Zhu 			if (desc->devnum == BLK_MTD_SPI_NAND)
555b6ffac1dSJason Zhu 				fdt_bootargs_append((void *)image_info.load_addr, mtd_part_parse(desc));
556c4ad0400SJason Zhu #endif
557a712f631SJason Zhu 		} else if (!strcmp(images[i], FIT_KERNEL_PROP)) {
558a00b516fSJoseph Chen #if CONFIG_IS_ENABLED(OPTEE)
559e12dde2dSJoseph Chen 			spl_image->entry_point_os = image_info.load_addr;
560a00b516fSJoseph Chen #endif
561a00b516fSJoseph Chen #if CONFIG_IS_ENABLED(ATF)
562a00b516fSJoseph Chen 			spl_image->entry_point_bl33 = image_info.load_addr;
563a00b516fSJoseph Chen #endif
564e12dde2dSJoseph Chen 		}
565a712f631SJason Zhu 	}
566e12dde2dSJoseph Chen 
567a00b516fSJoseph Chen 	debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n",
568a00b516fSJoseph Chen 	      (ulong)spl_image->fdt_addr,
569a00b516fSJoseph Chen 	      spl_image->entry_point,
570a00b516fSJoseph Chen #if CONFIG_IS_ENABLED(OPTEE)
571a00b516fSJoseph Chen 	      spl_image->entry_point_os);
572a00b516fSJoseph Chen #endif
573a00b516fSJoseph Chen #if CONFIG_IS_ENABLED(ATF)
574a00b516fSJoseph Chen 	      spl_image->entry_point_bl33);
575a00b516fSJoseph Chen #endif
576e12dde2dSJoseph Chen 
577e12dde2dSJoseph Chen 	return 0;
578e12dde2dSJoseph Chen }
579e12dde2dSJoseph Chen #endif
580e12dde2dSJoseph Chen 
581569a1737SJoseph Chen static int spl_internal_load_simple_fit(struct spl_image_info *spl_image,
582569a1737SJoseph Chen 					struct spl_load_info *info,
583569a1737SJoseph Chen 					ulong sector, void *fit_header)
584569a1737SJoseph Chen {
585569a1737SJoseph Chen 	struct spl_image_info image_info;
586c1006ae0SJoseph Chen 	char *desc;
587569a1737SJoseph Chen 	int base_offset;
588569a1737SJoseph Chen 	int images, ret;
589569a1737SJoseph Chen 	int index = 0;
590569a1737SJoseph Chen 	int node = -1;
591569a1737SJoseph Chen 	void *fit;
592569a1737SJoseph Chen 
593569a1737SJoseph Chen 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
594569a1737SJoseph Chen 	if (!fit) {
595569a1737SJoseph Chen 		debug("%s: Cannot load blob\n", __func__);
596569a1737SJoseph Chen 		return -1;
597569a1737SJoseph Chen 	}
598f1dcee59SSimon Glass 
599736806fbSAndre Przywara 	/* find the node holding the images information */
600f1dcee59SSimon Glass 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
601f1dcee59SSimon Glass 	if (images < 0) {
602f1dcee59SSimon Glass 		debug("%s: Cannot find /images node: %d\n", __func__, images);
603f1dcee59SSimon Glass 		return -1;
604f1dcee59SSimon Glass 	}
605736806fbSAndre Przywara 
6061f452cbfSJoseph Chen 	/* if board sigs verify required, check self */
6071f452cbfSJoseph Chen 	if (fit_board_verify_required_sigs() &&
6081f452cbfSJoseph Chen 	    !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
6091f452cbfSJoseph Chen 		printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n");
6101f452cbfSJoseph Chen 		hang();
6111f452cbfSJoseph Chen 	}
6121f452cbfSJoseph Chen 
613583377c4SJoseph Chen 	/* verify the configure node by keys, if required */
614583377c4SJoseph Chen #ifdef CONFIG_SPL_FIT_SIGNATURE
615583377c4SJoseph Chen 	int conf_noffset;
616583377c4SJoseph Chen 
617583377c4SJoseph Chen 	conf_noffset = fit_conf_get_node(fit, NULL);
618a7560f55SJoseph Chen 	if (conf_noffset <= 0) {
619a7560f55SJoseph Chen 		printf("No default config node\n");
620a7560f55SJoseph Chen 		return -EINVAL;
621a7560f55SJoseph Chen 	}
622a7560f55SJoseph Chen 
623583377c4SJoseph Chen 	ret = fit_config_verify(fit, conf_noffset);
624583377c4SJoseph Chen 	if (ret) {
625583377c4SJoseph Chen 		printf("fit verify configure failed, ret=%d\n", ret);
626583377c4SJoseph Chen 		return ret;
627583377c4SJoseph Chen 	}
628583377c4SJoseph Chen 	printf("\n");
629583377c4SJoseph Chen 
6307a137075SJoseph Chen #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT
6317a137075SJoseph Chen 	uint32_t this_index, min_index;
6327a137075SJoseph Chen 
6337a137075SJoseph Chen 	ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL,
6347a137075SJoseph Chen 					&this_index, &min_index);
6357a137075SJoseph Chen 	if (ret) {
6367a137075SJoseph Chen 		printf("fit failed to get rollback index, ret=%d\n", ret);
6377a137075SJoseph Chen 		return ret;
6387a137075SJoseph Chen 	} else if (this_index < min_index) {
6397a137075SJoseph Chen 		printf("fit reject rollback: %d < %d(min)\n",
6407a137075SJoseph Chen 		       this_index, min_index);
6417a137075SJoseph Chen 		return -EINVAL;
6427a137075SJoseph Chen 	}
6437a137075SJoseph Chen 
644e5ca21e8SJoseph Chen 	printf("rollback index: %d >= %d(min), OK\n", this_index, min_index);
6457a137075SJoseph Chen #endif
6467a137075SJoseph Chen #endif
647342d050eSJason Zhu 
648342d050eSJason Zhu 	/*
649342d050eSJason Zhu 	 * If required to start the other core before load "loadables"
650342d050eSJason Zhu 	 * firmwares, use the config "standalone" to load the other core's
651342d050eSJason Zhu 	 * firmware, then start it.
652342d050eSJason Zhu 	 * Normally, different cores' firmware is attach to the config
653342d050eSJason Zhu 	 * "loadables" and load them together.
654342d050eSJason Zhu 	 */
655c1006ae0SJoseph Chen 	for (; ; index++) {
656c1006ae0SJoseph Chen 		node = spl_fit_get_image_node(fit, images,
657c1006ae0SJoseph Chen 					      FIT_STANDALONE_PROP, index);
658342d050eSJason Zhu 		if (node < 0)
659c1006ae0SJoseph Chen 			break;
660c1006ae0SJoseph Chen 
661c1006ae0SJoseph Chen 		ret = spl_load_fit_image(info, sector, fit, base_offset,
662c1006ae0SJoseph Chen 					 node, &image_info);
663c1006ae0SJoseph Chen 		if (ret)
664c1006ae0SJoseph Chen 			return ret;
665c1006ae0SJoseph Chen 
666c1006ae0SJoseph Chen 		ret = fit_get_desc(fit, node, &desc);
667ecfe22e6SJoseph Chen 		if (ret)
668ecfe22e6SJoseph Chen 			return ret;
669ecfe22e6SJoseph Chen 
6709a65720bSJason Zhu 		if (image_info.entry_point == FDT_ERROR)
6719a65720bSJason Zhu 			image_info.entry_point = image_info.load_addr;
6729a65720bSJason Zhu 
673c1006ae0SJoseph Chen 		ret = spl_fit_standalone_release(desc, image_info.entry_point);
674342d050eSJason Zhu 		if (ret)
675c1006ae0SJoseph Chen 			printf("%s: start standalone fail, ret=%d\n", desc, ret);
676c1006ae0SJoseph Chen 	}
677342d050eSJason Zhu 
678e12dde2dSJoseph Chen 	/* standalone is special one, continue to find others */
679342d050eSJason Zhu 	node = -1;
680c1006ae0SJoseph Chen 	index = 0;
681342d050eSJason Zhu 
6829719e14fSPhilipp Tomsich 	/*
6839719e14fSPhilipp Tomsich 	 * Find the U-Boot image using the following search order:
6849719e14fSPhilipp Tomsich 	 *   - start at 'firmware' (e.g. an ARM Trusted Firmware)
6859719e14fSPhilipp Tomsich 	 *   - fall back 'kernel' (e.g. a Falcon-mode OS boot
6869719e14fSPhilipp Tomsich 	 *   - fall back to using the first 'loadables' entry
6879719e14fSPhilipp Tomsich 	 */
688ebec805aSYork Sun 	if (node < 0)
689c04fe8bfSMichal Simek 		node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
690c04fe8bfSMichal Simek 					      0);
6919719e14fSPhilipp Tomsich #ifdef CONFIG_SPL_OS_BOOT
6929719e14fSPhilipp Tomsich 	if (node < 0)
6939719e14fSPhilipp Tomsich 		node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
6949719e14fSPhilipp Tomsich #endif
695f1dcee59SSimon Glass 	if (node < 0) {
696736806fbSAndre Przywara 		debug("could not find firmware image, trying loadables...\n");
697736806fbSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
698411cf32dSAndre Przywara 		/*
699411cf32dSAndre Przywara 		 * If we pick the U-Boot image from "loadables", start at
700411cf32dSAndre Przywara 		 * the second image when later loading additional images.
701411cf32dSAndre Przywara 		 */
702411cf32dSAndre Przywara 		index = 1;
703736806fbSAndre Przywara 	}
704736806fbSAndre Przywara 	if (node < 0) {
705736806fbSAndre Przywara 		debug("%s: Cannot find u-boot image node: %d\n",
706736806fbSAndre Przywara 		      __func__, node);
707f1dcee59SSimon Glass 		return -1;
708f1dcee59SSimon Glass 	}
709f1dcee59SSimon Glass 
7108baa3818SAndre Przywara 	/* Load the image and set up the spl_image structure */
7118baa3818SAndre Przywara 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
7128baa3818SAndre Przywara 				 spl_image);
7138baa3818SAndre Przywara 	if (ret)
7148baa3818SAndre Przywara 		return ret;
7158baa3818SAndre Przywara 
7169719e14fSPhilipp Tomsich 	/*
7179719e14fSPhilipp Tomsich 	 * For backward compatibility, we treat the first node that is
7189719e14fSPhilipp Tomsich 	 * as a U-Boot image, if no OS-type has been declared.
7199719e14fSPhilipp Tomsich 	 */
7208118cce3SPhilipp Tomsich 	if (!spl_fit_image_get_os(fit, node, &spl_image->os))
721ebec805aSYork Sun 		debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
7229719e14fSPhilipp Tomsich #if !defined(CONFIG_SPL_OS_BOOT)
7239719e14fSPhilipp Tomsich 	else
724f4d7d859SSimon Glass 		spl_image->os = IH_OS_U_BOOT;
725ebec805aSYork Sun #endif
726f1dcee59SSimon Glass 
727*4024d9e5SJoseph Chen 	/* Booting a next-stage U-Boot may require us to append the FDT. */
728*4024d9e5SJoseph Chen 	if (spl_image->os == IH_OS_U_BOOT) {
729*4024d9e5SJoseph Chen 		ret = spl_fit_append_fdt(spl_image, info, sector, fit,
7309719e14fSPhilipp Tomsich 					 images, base_offset);
731*4024d9e5SJoseph Chen 		if (ret < 0)
732*4024d9e5SJoseph Chen 			return ret;
733*4024d9e5SJoseph Chen 	}
734411cf32dSAndre Przywara 
735411cf32dSAndre Przywara 	/* Now check if there are more images for us to load */
736411cf32dSAndre Przywara 	for (; ; index++) {
7379719e14fSPhilipp Tomsich 		uint8_t os_type = IH_OS_INVALID;
7389719e14fSPhilipp Tomsich 
739411cf32dSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", index);
740411cf32dSAndre Przywara 		if (node < 0)
741411cf32dSAndre Przywara 			break;
742411cf32dSAndre Przywara 
743e12dde2dSJoseph Chen 		if (!spl_fit_image_get_os(fit, node, &os_type))
744e12dde2dSJoseph Chen 			debug("Loadable is %s\n", genimg_get_os_name(os_type));
745e12dde2dSJoseph Chen 
746e12dde2dSJoseph Chen 		/* skip U-Boot ? */
747e12dde2dSJoseph Chen 		if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL &&
748e12dde2dSJoseph Chen 		    os_type == IH_OS_U_BOOT)
749e12dde2dSJoseph Chen 		    continue;
750e12dde2dSJoseph Chen 
751411cf32dSAndre Przywara 		ret = spl_load_fit_image(info, sector, fit, base_offset, node,
752411cf32dSAndre Przywara 					 &image_info);
753411cf32dSAndre Przywara 		if (ret < 0)
754ecfe22e6SJoseph Chen 			return ret;
755411cf32dSAndre Przywara 
7561f1cf67bSPhilipp Tomsich 		if (os_type == IH_OS_U_BOOT) {
75739b9f515SJoseph Chen #if CONFIG_IS_ENABLED(ATF)
75839b9f515SJoseph Chen 			spl_image->entry_point_bl33 = image_info.load_addr;
75939b9f515SJoseph Chen #elif CONFIG_IS_ENABLED(OPTEE)
76039b9f515SJoseph Chen 			spl_image->entry_point_os = image_info.load_addr;
76139b9f515SJoseph Chen #endif
762*4024d9e5SJoseph Chen 			ret = spl_fit_append_fdt(&image_info, info, sector,
7639719e14fSPhilipp Tomsich 						 fit, images, base_offset);
764*4024d9e5SJoseph Chen 			if (ret < 0)
765*4024d9e5SJoseph Chen 				return ret;
7661f1cf67bSPhilipp Tomsich 			spl_image->fdt_addr = image_info.fdt_addr;
7671f1cf67bSPhilipp Tomsich 		}
7689719e14fSPhilipp Tomsich 
769411cf32dSAndre Przywara 		/*
770411cf32dSAndre Przywara 		 * If the "firmware" image did not provide an entry point,
771411cf32dSAndre Przywara 		 * use the first valid entry point from the loadables.
772411cf32dSAndre Przywara 		 */
773411cf32dSAndre Przywara 		if (spl_image->entry_point == FDT_ERROR &&
774411cf32dSAndre Przywara 		    image_info.entry_point != FDT_ERROR)
775411cf32dSAndre Przywara 			spl_image->entry_point = image_info.entry_point;
7761f1cf67bSPhilipp Tomsich 
7771f1cf67bSPhilipp Tomsich 		/* Record our loadables into the FDT */
7781f1cf67bSPhilipp Tomsich 		if (spl_image->fdt_addr)
7791f1cf67bSPhilipp Tomsich 			spl_fit_record_loadable(fit, images, index,
7801f1cf67bSPhilipp Tomsich 						spl_image->fdt_addr,
7811f1cf67bSPhilipp Tomsich 						&image_info);
782411cf32dSAndre Przywara 	}
783411cf32dSAndre Przywara 
784411cf32dSAndre Przywara 	/*
785411cf32dSAndre Przywara 	 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
786411cf32dSAndre Przywara 	 * Makefile will set it to 0 and it will end up as the entry point
787411cf32dSAndre Przywara 	 * here. What it actually means is: use the load address.
788411cf32dSAndre Przywara 	 */
789411cf32dSAndre Przywara 	if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
790411cf32dSAndre Przywara 		spl_image->entry_point = spl_image->load_addr;
791411cf32dSAndre Przywara 
792411cf32dSAndre Przywara 	return 0;
793f1dcee59SSimon Glass }
79422c7c1a8SJoseph Chen 
79522c7c1a8SJoseph Chen int spl_load_simple_fit(struct spl_image_info *spl_image,
79622c7c1a8SJoseph Chen 			struct spl_load_info *info, ulong sector, void *fit)
79722c7c1a8SJoseph Chen {
79822c7c1a8SJoseph Chen 	ulong sector_offs = sector;
799e12dde2dSJoseph Chen 	int ret = -EINVAL;
80022c7c1a8SJoseph Chen 	int i;
80122c7c1a8SJoseph Chen 
80236c449feSJoseph Chen #ifdef CONFIG_MP_BOOT
80336c449feSJoseph Chen 	mpb_init_1(*info);
80436c449feSJoseph Chen #endif
80536c449feSJoseph Chen 
8067e6f3a66SJoseph Chen 	printf("Trying fit image at 0x%lx sector\n", sector_offs);
80722c7c1a8SJoseph Chen 	for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) {
80822c7c1a8SJoseph Chen 		if (i > 0) {
80922c7c1a8SJoseph Chen 			sector_offs +=
81022c7c1a8SJoseph Chen 			   i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len);
81122c7c1a8SJoseph Chen 			printf("Trying fit image at 0x%lx sector\n", sector_offs);
81222c7c1a8SJoseph Chen 			if (info->read(info, sector_offs, 1, fit) != 1) {
81322c7c1a8SJoseph Chen 				printf("IO error\n");
81422c7c1a8SJoseph Chen 				continue;
81522c7c1a8SJoseph Chen 			}
81622c7c1a8SJoseph Chen 		}
81722c7c1a8SJoseph Chen 
81822c7c1a8SJoseph Chen 		if (image_get_magic(fit) != FDT_MAGIC) {
819e12dde2dSJoseph Chen 			printf("Not fit magic\n");
82022c7c1a8SJoseph Chen 			continue;
82122c7c1a8SJoseph Chen 		}
82222c7c1a8SJoseph Chen 
823e12dde2dSJoseph Chen 		ret = spl_internal_load_simple_fit(spl_image, info,
824e12dde2dSJoseph Chen 						   sector_offs, fit);
825e12dde2dSJoseph Chen 		if (!ret) {
826a00b516fSJoseph Chen #ifdef CONFIG_SPL_KERNEL_BOOT
827e12dde2dSJoseph Chen 			ret = spl_load_kernel_fit(spl_image, info);
828e12dde2dSJoseph Chen #endif
8291f5c7b64SJason Zhu 			break;
830e12dde2dSJoseph Chen 		}
83122c7c1a8SJoseph Chen 	}
8321f5c7b64SJason Zhu #ifdef CONFIG_SPL_AB
833f34b44cfSXuhui Lin 	/* If boot fail in spl, spl must decrease 1 and do_reset. */
834f34b44cfSXuhui Lin 	if (ret)
835f34b44cfSXuhui Lin 		return spl_ab_decrease_reset(info->dev);
8361f5c7b64SJason Zhu 	/*
837f34b44cfSXuhui Lin 	 * If boot successfully, it is no need to do decrease
838f34b44cfSXuhui Lin 	 * and U-boot will always decrease 1.
839f34b44cfSXuhui Lin 	 * If in thunderboot process, always need to decrease 1.
8401f5c7b64SJason Zhu 	 */
841f34b44cfSXuhui Lin 	if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL)
8421f5c7b64SJason Zhu 		spl_ab_decrease_tries(info->dev);
8431f5c7b64SJason Zhu #endif
844f34b44cfSXuhui Lin 
845e12dde2dSJoseph Chen 	return ret;
84622c7c1a8SJoseph Chen }
84722c7c1a8SJoseph Chen 
848