xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision d3bfb68b0cea76141a6b6334341d8b38dd1f1812)
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>
11f1dcee59SSimon Glass #include <image.h>
120e00a84cSMasahiro Yamada #include <linux/libfdt.h>
13f1dcee59SSimon Glass #include <spl.h>
1490d1164aSKever Yang #include <malloc.h>
15f1dcee59SSimon Glass 
16b81c4739SYork Sun #ifndef CONFIG_SYS_BOOTM_LEN
17b81c4739SYork Sun #define CONFIG_SYS_BOOTM_LEN	(64 << 20)
18b81c4739SYork Sun #endif
19b81c4739SYork Sun 
20736806fbSAndre Przywara /**
211f1cf67bSPhilipp Tomsich  * spl_fit_get_image_name(): By using the matching configuration subnode,
22736806fbSAndre Przywara  * retrieve the name of an image, specified by a property name and an index
23736806fbSAndre Przywara  * into that.
24736806fbSAndre Przywara  * @fit:	Pointer to the FDT blob.
25736806fbSAndre Przywara  * @images:	Offset of the /images subnode.
26736806fbSAndre Przywara  * @type:	Name of the property within the configuration subnode.
27736806fbSAndre Przywara  * @index:	Index into the list of strings in this property.
281f1cf67bSPhilipp Tomsich  * @outname:	Name of the image
29736806fbSAndre Przywara  *
301f1cf67bSPhilipp Tomsich  * Return:	0 on success, or a negative error number
31736806fbSAndre Przywara  */
321f1cf67bSPhilipp Tomsich static int spl_fit_get_image_name(const void *fit, int images,
331f1cf67bSPhilipp Tomsich 				  const char *type, int index,
341f1cf67bSPhilipp Tomsich 				  char **outname)
354b9340abSAndre Przywara {
364b9340abSAndre Przywara 	const char *name, *str;
371f1cf67bSPhilipp Tomsich 	__maybe_unused int node;
381f1cf67bSPhilipp Tomsich 	int conf_node;
394b9340abSAndre Przywara 	int len, i;
40f1dcee59SSimon Glass 
413863f840SCooper Jr., Franklin 	conf_node = fit_find_config_node(fit);
424b9340abSAndre Przywara 	if (conf_node < 0) {
43f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
44f1dcee59SSimon Glass 		printf("No matching DT out of these options:\n");
454b9340abSAndre Przywara 		for (node = fdt_first_subnode(fit, conf_node);
46f1dcee59SSimon Glass 		     node >= 0;
474b9340abSAndre Przywara 		     node = fdt_next_subnode(fit, node)) {
484b9340abSAndre Przywara 			name = fdt_getprop(fit, node, "description", &len);
49f1dcee59SSimon Glass 			printf("   %s\n", name);
50f1dcee59SSimon Glass 		}
51f1dcee59SSimon Glass #endif
524b9340abSAndre Przywara 		return conf_node;
534b9340abSAndre Przywara 	}
54f1dcee59SSimon Glass 
554b9340abSAndre Przywara 	name = fdt_getprop(fit, conf_node, type, &len);
564b9340abSAndre Przywara 	if (!name) {
574b9340abSAndre Przywara 		debug("cannot find property '%s': %d\n", type, len);
584b9340abSAndre Przywara 		return -EINVAL;
594b9340abSAndre Przywara 	}
604b9340abSAndre Przywara 
614b9340abSAndre Przywara 	str = name;
624b9340abSAndre Przywara 	for (i = 0; i < index; i++) {
634b9340abSAndre Przywara 		str = strchr(str, '\0') + 1;
644b9340abSAndre Przywara 		if (!str || (str - name >= len)) {
654b9340abSAndre Przywara 			debug("no string for index %d\n", index);
664b9340abSAndre Przywara 			return -E2BIG;
674b9340abSAndre Przywara 		}
684b9340abSAndre Przywara 	}
694b9340abSAndre Przywara 
701f1cf67bSPhilipp Tomsich 	*outname = (char *)str;
711f1cf67bSPhilipp Tomsich 	return 0;
721f1cf67bSPhilipp Tomsich }
731f1cf67bSPhilipp Tomsich 
741f1cf67bSPhilipp Tomsich /**
751f1cf67bSPhilipp Tomsich  * spl_fit_get_image_node(): By using the matching configuration subnode,
761f1cf67bSPhilipp Tomsich  * retrieve the name of an image, specified by a property name and an index
771f1cf67bSPhilipp Tomsich  * into that.
781f1cf67bSPhilipp Tomsich  * @fit:	Pointer to the FDT blob.
791f1cf67bSPhilipp Tomsich  * @images:	Offset of the /images subnode.
801f1cf67bSPhilipp Tomsich  * @type:	Name of the property within the configuration subnode.
811f1cf67bSPhilipp Tomsich  * @index:	Index into the list of strings in this property.
821f1cf67bSPhilipp Tomsich  *
831f1cf67bSPhilipp Tomsich  * Return:	the node offset of the respective image node or a negative
841f1cf67bSPhilipp Tomsich  *		error number.
851f1cf67bSPhilipp Tomsich  */
861f1cf67bSPhilipp Tomsich static int spl_fit_get_image_node(const void *fit, int images,
871f1cf67bSPhilipp Tomsich 				  const char *type, int index)
881f1cf67bSPhilipp Tomsich {
891f1cf67bSPhilipp Tomsich 	char *str;
901f1cf67bSPhilipp Tomsich 	int err;
911f1cf67bSPhilipp Tomsich 	int node;
921f1cf67bSPhilipp Tomsich 
931f1cf67bSPhilipp Tomsich 	err = spl_fit_get_image_name(fit, images, type, index, &str);
941f1cf67bSPhilipp Tomsich 	if (err)
951f1cf67bSPhilipp Tomsich 		return err;
961f1cf67bSPhilipp Tomsich 
974b9340abSAndre Przywara 	debug("%s: '%s'\n", type, str);
981f1cf67bSPhilipp Tomsich 
994b9340abSAndre Przywara 	node = fdt_subnode_offset(fit, images, str);
1004b9340abSAndre Przywara 	if (node < 0) {
1014b9340abSAndre Przywara 		debug("cannot find image node '%s': %d\n", str, node);
1024b9340abSAndre Przywara 		return -EINVAL;
1034b9340abSAndre Przywara 	}
1044b9340abSAndre Przywara 
105736806fbSAndre Przywara 	return node;
106f1dcee59SSimon Glass }
107f1dcee59SSimon Glass 
108eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset)
109eafd5410SLokesh Vutla {
110eafd5410SLokesh Vutla 	/*
111eafd5410SLokesh Vutla 	 * If it is a FS read, get the first address before offset which is
112eafd5410SLokesh Vutla 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
113eafd5410SLokesh Vutla 	 * block number to which offset belongs.
114eafd5410SLokesh Vutla 	 */
115eafd5410SLokesh Vutla 	if (info->filename)
116eafd5410SLokesh Vutla 		return offset & ~(ARCH_DMA_MINALIGN - 1);
117eafd5410SLokesh Vutla 
118eafd5410SLokesh Vutla 	return offset / info->bl_len;
119eafd5410SLokesh Vutla }
120eafd5410SLokesh Vutla 
121eafd5410SLokesh Vutla static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
122eafd5410SLokesh Vutla {
123eafd5410SLokesh Vutla 	/*
124eafd5410SLokesh Vutla 	 * If it is a FS read, get the difference between the offset and
125eafd5410SLokesh Vutla 	 * the first address before offset which is aligned to
126eafd5410SLokesh Vutla 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
127eafd5410SLokesh Vutla 	 * block.
128eafd5410SLokesh Vutla 	 */
129eafd5410SLokesh Vutla 	if (info->filename)
130eafd5410SLokesh Vutla 		return offset & (ARCH_DMA_MINALIGN - 1);
131eafd5410SLokesh Vutla 
132eafd5410SLokesh Vutla 	return offset % info->bl_len;
133eafd5410SLokesh Vutla }
134eafd5410SLokesh Vutla 
135eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size,
136eafd5410SLokesh Vutla 				  int offset)
137eafd5410SLokesh Vutla {
1383cc1f380SLokesh Vutla 	data_size = data_size + get_aligned_image_overhead(info, offset);
1393cc1f380SLokesh Vutla 
140eafd5410SLokesh Vutla 	if (info->filename)
1413cc1f380SLokesh Vutla 		return data_size;
142eafd5410SLokesh Vutla 
143eafd5410SLokesh Vutla 	return (data_size + info->bl_len - 1) / info->bl_len;
144eafd5410SLokesh Vutla }
145eafd5410SLokesh Vutla 
1468baa3818SAndre Przywara /**
1478baa3818SAndre Przywara  * spl_load_fit_image(): load the image described in a certain FIT node
1488baa3818SAndre Przywara  * @info:	points to information about the device to load data from
1498baa3818SAndre Przywara  * @sector:	the start sector of the FIT image on the device
1508baa3818SAndre Przywara  * @fit:	points to the flattened device tree blob describing the FIT
1518baa3818SAndre Przywara  *		image
1528baa3818SAndre Przywara  * @base_offset: the beginning of the data area containing the actual
1538baa3818SAndre Przywara  *		image data, relative to the beginning of the FIT
1548baa3818SAndre Przywara  * @node:	offset of the DT node describing the image to load (relative
1558baa3818SAndre Przywara  *		to @fit)
1568baa3818SAndre Przywara  * @image_info:	will be filled with information about the loaded image
1578baa3818SAndre Przywara  *		If the FIT node does not contain a "load" (address) property,
1588baa3818SAndre Przywara  *		the image gets loaded to the address pointed to by the
1598baa3818SAndre Przywara  *		load_addr member in this struct.
1608baa3818SAndre Przywara  *
1618baa3818SAndre Przywara  * Return:	0 on success or a negative error number.
1628baa3818SAndre Przywara  */
1638baa3818SAndre Przywara static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
1648baa3818SAndre Przywara 			      void *fit, ulong base_offset, int node,
1658baa3818SAndre Przywara 			      struct spl_image_info *image_info)
1668baa3818SAndre Przywara {
1670ea10b9fSYork Sun 	int offset;
1688baa3818SAndre Przywara 	size_t length;
1690ea10b9fSYork Sun 	int len;
170d18ad2e6SYork Sun 	ulong size;
171*d3bfb68bSJoseph Chen 	ulong comp_addr, load_addr, load_ptr;
1728baa3818SAndre Przywara 	void *src;
1738baa3818SAndre Przywara 	ulong overhead;
1748baa3818SAndre Przywara 	int nr_sectors;
1758baa3818SAndre Przywara 	int align_len = ARCH_DMA_MINALIGN - 1;
176b81c4739SYork Sun 	uint8_t image_comp = -1, type = -1;
1770ea10b9fSYork Sun 	const void *data;
178b86dc419SPeng Fan 	bool external_data = false;
179b81c4739SYork Sun 
180b81c4739SYork Sun 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
181b81c4739SYork Sun 		if (fit_image_get_comp(fit, node, &image_comp))
182b81c4739SYork Sun 			puts("Cannot get image compression format.\n");
183b81c4739SYork Sun 		else
184b81c4739SYork Sun 			debug("%s ", genimg_get_comp_name(image_comp));
185b81c4739SYork Sun 
186b81c4739SYork Sun 		if (fit_image_get_type(fit, node, &type))
187b81c4739SYork Sun 			puts("Cannot get image type.\n");
188b81c4739SYork Sun 		else
189b81c4739SYork Sun 			debug("%s ", genimg_get_type_name(type));
190*d3bfb68bSJoseph Chen 	} else {
191*d3bfb68bSJoseph Chen 		fit_image_get_comp(fit, node, &image_comp);
192b81c4739SYork Sun 	}
1938baa3818SAndre Przywara 
1940ea10b9fSYork Sun 	if (fit_image_get_load(fit, node, &load_addr))
1958baa3818SAndre Przywara 		load_addr = image_info->load_addr;
1960ea10b9fSYork Sun 
197*d3bfb68bSJoseph Chen 	if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE) {
198*d3bfb68bSJoseph Chen 		/* Empirically, 1MB is enough for U-Boot, tee and atf */
199*d3bfb68bSJoseph Chen 		if (fit_image_get_comp_addr(fit, node, &comp_addr))
200*d3bfb68bSJoseph Chen 			comp_addr = load_addr + SZ_1M;
201*d3bfb68bSJoseph Chen 	} else {
202*d3bfb68bSJoseph Chen 		comp_addr = load_addr;
203*d3bfb68bSJoseph Chen 	}
204*d3bfb68bSJoseph Chen 
205b86dc419SPeng Fan 	if (!fit_image_get_data_position(fit, node, &offset)) {
206b86dc419SPeng Fan 		external_data = true;
207b86dc419SPeng Fan 	} else if (!fit_image_get_data_offset(fit, node, &offset)) {
2080ea10b9fSYork Sun 		offset += base_offset;
209b86dc419SPeng Fan 		external_data = true;
210b86dc419SPeng Fan 	}
211b86dc419SPeng Fan 
212b86dc419SPeng Fan 	if (external_data) {
213b86dc419SPeng Fan 		/* External data */
2140ea10b9fSYork Sun 		if (fit_image_get_data_size(fit, node, &len))
2150ea10b9fSYork Sun 			return -ENOENT;
2160ea10b9fSYork Sun 
217*d3bfb68bSJoseph Chen 		load_ptr = (comp_addr + align_len) & ~align_len;
21863363c40SAndy Yan #if  defined(CONFIG_ARCH_ROCKCHIP)
219a5e7a2d4SKever Yang 		if ((load_ptr < CONFIG_SYS_SDRAM_BASE) ||
220a5e7a2d4SKever Yang 		     (load_ptr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE))
22190d1164aSKever Yang 			load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len);
22290d1164aSKever Yang #endif
2230ea10b9fSYork Sun 		length = len;
2248baa3818SAndre Przywara 
2258baa3818SAndre Przywara 		overhead = get_aligned_image_overhead(info, offset);
2268baa3818SAndre Przywara 		nr_sectors = get_aligned_image_size(info, length, offset);
2278baa3818SAndre Przywara 
2280ea10b9fSYork Sun 		if (info->read(info,
2290ea10b9fSYork Sun 			       sector + get_aligned_image_offset(info, offset),
2308baa3818SAndre Przywara 			       nr_sectors, (void *)load_ptr) != nr_sectors)
2318baa3818SAndre Przywara 			return -EIO;
2328baa3818SAndre Przywara 
2330ea10b9fSYork Sun 		debug("External data: dst=%lx, offset=%x, size=%lx\n",
2340ea10b9fSYork Sun 		      load_ptr, offset, (unsigned long)length);
2358baa3818SAndre Przywara 		src = (void *)load_ptr + overhead;
2360ea10b9fSYork Sun 	} else {
2370ea10b9fSYork Sun 		/* Embedded data */
2380ea10b9fSYork Sun 		if (fit_image_get_data(fit, node, &data, &length)) {
2390ea10b9fSYork Sun 			puts("Cannot get image data/size\n");
2400ea10b9fSYork Sun 			return -ENOENT;
2410ea10b9fSYork Sun 		}
2420ea10b9fSYork Sun 		debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
2430ea10b9fSYork Sun 		      (unsigned long)length);
2440ea10b9fSYork Sun 		src = (void *)data;
2450ea10b9fSYork Sun 	}
2460ea10b9fSYork Sun 
247a515b6d2SJoseph Chen 	/* Check hashes and signature */
248*d3bfb68bSJoseph Chen 	if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE)
249*d3bfb68bSJoseph Chen 		printf("## Checking %s 0x%08lx (%s @0x%08lx) ... ",
250*d3bfb68bSJoseph Chen 		       fit_get_name(fit, node, NULL), load_addr,
251*d3bfb68bSJoseph Chen 		       (char *)fdt_getprop(fit, node, FIT_COMP_PROP, NULL),
252*d3bfb68bSJoseph Chen 		       (long)src);
253*d3bfb68bSJoseph Chen 	else
254*d3bfb68bSJoseph Chen 		printf("## Checking %s 0x%08lx ... ",
255*d3bfb68bSJoseph Chen 		       fit_get_name(fit, node, NULL), load_addr);
256*d3bfb68bSJoseph Chen 
257b92c7cf4SJoseph Chen #ifdef CONFIG_FIT_SPL_PRINT
258b92c7cf4SJoseph Chen 	printf("\n");
259b92c7cf4SJoseph Chen 	fit_image_print(fit, node, "");
260b92c7cf4SJoseph Chen #endif
261f3219d95SBen Whitten 	if (!fit_image_verify_with_data(fit, node,
262f3219d95SBen Whitten 					 src, length))
263f3219d95SBen Whitten 		return -EPERM;
264f3219d95SBen Whitten 	puts("OK\n");
265f3219d95SBen Whitten 
2668baa3818SAndre Przywara #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
2679c633288SJoseph Chen 	board_fit_image_post_process(fit, node, (ulong *)&load_addr,
2689c633288SJoseph Chen 				     (ulong **)&src, &length);
2698baa3818SAndre Przywara #endif
2708baa3818SAndre Przywara 
271b81c4739SYork Sun 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT)	&&
272b81c4739SYork Sun 	    IS_ENABLED(CONFIG_SPL_GZIP)		&&
273b81c4739SYork Sun 	    image_comp == IH_COMP_GZIP		&&
274b81c4739SYork Sun 	    type == IH_TYPE_KERNEL) {
275d18ad2e6SYork Sun 		size = length;
276b81c4739SYork Sun 		if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
277d18ad2e6SYork Sun 			   src, &size)) {
278b81c4739SYork Sun 			puts("Uncompressing error\n");
279b81c4739SYork Sun 			return -EIO;
280b81c4739SYork Sun 		}
281d18ad2e6SYork Sun 		length = size;
282b81c4739SYork Sun 	} else {
2838baa3818SAndre Przywara 		memcpy((void *)load_addr, src, length);
284b81c4739SYork Sun 	}
2858baa3818SAndre Przywara 
2868baa3818SAndre Przywara 	if (image_info) {
2878baa3818SAndre Przywara 		image_info->load_addr = load_addr;
2888baa3818SAndre Przywara 		image_info->size = length;
2898baa3818SAndre Przywara 		image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
2908baa3818SAndre Przywara 	}
2918baa3818SAndre Przywara 
2928baa3818SAndre Przywara 	return 0;
2938baa3818SAndre Przywara }
2948baa3818SAndre Przywara 
2959719e14fSPhilipp Tomsich static int spl_fit_append_fdt(struct spl_image_info *spl_image,
2969719e14fSPhilipp Tomsich 			      struct spl_load_info *info, ulong sector,
2979719e14fSPhilipp Tomsich 			      void *fit, int images, ulong base_offset)
2989719e14fSPhilipp Tomsich {
2999719e14fSPhilipp Tomsich 	struct spl_image_info image_info;
3009719e14fSPhilipp Tomsich 	int node, ret;
3019719e14fSPhilipp Tomsich 
3029719e14fSPhilipp Tomsich 	/* Figure out which device tree the board wants to use */
3039719e14fSPhilipp Tomsich 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
3049719e14fSPhilipp Tomsich 	if (node < 0) {
3059719e14fSPhilipp Tomsich 		debug("%s: cannot find FDT node\n", __func__);
3069719e14fSPhilipp Tomsich 		return node;
3079719e14fSPhilipp Tomsich 	}
3089719e14fSPhilipp Tomsich 
3099719e14fSPhilipp Tomsich 	/*
3109719e14fSPhilipp Tomsich 	 * Read the device tree and place it after the image.
3119719e14fSPhilipp Tomsich 	 * Align the destination address to ARCH_DMA_MINALIGN.
3129719e14fSPhilipp Tomsich 	 */
3139719e14fSPhilipp Tomsich 	image_info.load_addr = spl_image->load_addr + spl_image->size;
3149719e14fSPhilipp Tomsich 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
3159719e14fSPhilipp Tomsich 				 &image_info);
3161f1cf67bSPhilipp Tomsich 
3171f1cf67bSPhilipp Tomsich 	if (ret < 0)
3181f1cf67bSPhilipp Tomsich 		return ret;
3191f1cf67bSPhilipp Tomsich 
3201f1cf67bSPhilipp Tomsich 	/* Make the load-address of the FDT available for the SPL framework */
3211f1cf67bSPhilipp Tomsich 	spl_image->fdt_addr = (void *)image_info.load_addr;
3228118cce3SPhilipp Tomsich #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
3231f1cf67bSPhilipp Tomsich 	/* Try to make space, so we can inject details on the loadables */
3241f1cf67bSPhilipp Tomsich 	ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
3258118cce3SPhilipp Tomsich #endif
3261f1cf67bSPhilipp Tomsich 
3271f1cf67bSPhilipp Tomsich 	return ret;
3281f1cf67bSPhilipp Tomsich }
3291f1cf67bSPhilipp Tomsich 
3301f1cf67bSPhilipp Tomsich static int spl_fit_record_loadable(const void *fit, int images, int index,
3311f1cf67bSPhilipp Tomsich 				   void *blob, struct spl_image_info *image)
3321f1cf67bSPhilipp Tomsich {
3338118cce3SPhilipp Tomsich 	int ret = 0;
3348118cce3SPhilipp Tomsich #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
3351f1cf67bSPhilipp Tomsich 	char *name;
3368118cce3SPhilipp Tomsich 	int node;
3371f1cf67bSPhilipp Tomsich 
3381f1cf67bSPhilipp Tomsich 	ret = spl_fit_get_image_name(fit, images, "loadables",
3391f1cf67bSPhilipp Tomsich 				     index, &name);
3401f1cf67bSPhilipp Tomsich 	if (ret < 0)
3411f1cf67bSPhilipp Tomsich 		return ret;
3421f1cf67bSPhilipp Tomsich 
3431f1cf67bSPhilipp Tomsich 	node = spl_fit_get_image_node(fit, images, "loadables", index);
3441f1cf67bSPhilipp Tomsich 
3451f1cf67bSPhilipp Tomsich 	ret = fdt_record_loadable(blob, index, name, image->load_addr,
3461f1cf67bSPhilipp Tomsich 				  image->size, image->entry_point,
3471f1cf67bSPhilipp Tomsich 				  fdt_getprop(fit, node, "type", NULL),
3481f1cf67bSPhilipp Tomsich 				  fdt_getprop(fit, node, "os", NULL));
3498118cce3SPhilipp Tomsich #endif
3509719e14fSPhilipp Tomsich 	return ret;
3519719e14fSPhilipp Tomsich }
3529719e14fSPhilipp Tomsich 
3538118cce3SPhilipp Tomsich static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
3548118cce3SPhilipp Tomsich {
3558118cce3SPhilipp Tomsich #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
3568118cce3SPhilipp Tomsich 	return -ENOTSUPP;
3578118cce3SPhilipp Tomsich #else
3588118cce3SPhilipp Tomsich 	return fit_image_get_os(fit, noffset, os);
3598118cce3SPhilipp Tomsich #endif
3608118cce3SPhilipp Tomsich }
3618118cce3SPhilipp Tomsich 
3629a65720bSJason Zhu __weak int spl_fit_standalone_release(uintptr_t entry_point)
363342d050eSJason Zhu {
364342d050eSJason Zhu 	return 0;
365342d050eSJason Zhu }
366342d050eSJason Zhu 
367569a1737SJoseph Chen static void *spl_fit_load_blob(struct spl_load_info *info,
368569a1737SJoseph Chen 			       ulong sector, void *fit_header,
369569a1737SJoseph Chen 			       int *base_offset)
370f1dcee59SSimon Glass {
371569a1737SJoseph Chen 	int align_len = ARCH_DMA_MINALIGN - 1;
372569a1737SJoseph Chen 	ulong count;
3738baa3818SAndre Przywara 	ulong size;
374569a1737SJoseph Chen 	int sectors;
375569a1737SJoseph Chen 	void *fit;
376f1dcee59SSimon Glass 
377f1dcee59SSimon Glass 	/*
378ebec805aSYork Sun 	 * For FIT with external data, figure out where the external images
379ebec805aSYork Sun 	 * start. This is the base for the data-offset properties in each
380ebec805aSYork Sun 	 * image.
381f1dcee59SSimon Glass 	 */
382569a1737SJoseph Chen 	size = fdt_totalsize(fit_header);
383acd43290SJoseph Chen 	size = FIT_ALIGN(size);
384569a1737SJoseph Chen 	*base_offset = FIT_ALIGN(size);
385f1dcee59SSimon Glass 
386f1dcee59SSimon Glass 	/*
387f1dcee59SSimon Glass 	 * So far we only have one block of data from the FIT. Read the entire
388f1dcee59SSimon Glass 	 * thing, including that first block, placing it so it finishes before
389f1dcee59SSimon Glass 	 * where we will load the image.
390f1dcee59SSimon Glass 	 *
391f1dcee59SSimon Glass 	 * Note that we will load the image such that its first byte will be
392f1dcee59SSimon Glass 	 * at the load address. Since that byte may be part-way through a
393f1dcee59SSimon Glass 	 * block, we may load the image up to one block before the load
394f1dcee59SSimon Glass 	 * address. So take account of that here by subtracting an addition
395f1dcee59SSimon Glass 	 * block length from the FIT start position.
396f1dcee59SSimon Glass 	 *
397f1dcee59SSimon Glass 	 * In fact the FIT has its own load address, but we assume it cannot
398f1dcee59SSimon Glass 	 * be before CONFIG_SYS_TEXT_BASE.
399ebec805aSYork Sun 	 *
400ebec805aSYork Sun 	 * For FIT with data embedded, data is loaded as part of FIT image.
401ebec805aSYork Sun 	 * For FIT with external data, data is not loaded in this step.
402f1dcee59SSimon Glass 	 */
4038b528709SLokesh Vutla 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
4048b528709SLokesh Vutla 			align_len) & ~align_len);
405eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, size, 0);
406f1dcee59SSimon Glass 	count = info->read(info, sector, sectors, fit);
407f1dcee59SSimon Glass 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
408f1dcee59SSimon Glass 	      sector, sectors, fit, count);
409f1dcee59SSimon Glass 	if (count == 0)
410569a1737SJoseph Chen 		return NULL;
411569a1737SJoseph Chen 
412569a1737SJoseph Chen 	return fit;
413569a1737SJoseph Chen }
414569a1737SJoseph Chen 
415e12dde2dSJoseph Chen #ifdef CONFIG_SPL_FIT_LOAD_KERNEL
416e12dde2dSJoseph Chen #ifdef CONFIG_SPL_LIBDISK_SUPPORT
417e12dde2dSJoseph Chen __weak const char *spl_kernel_partition(struct spl_image_info *spl,
418e12dde2dSJoseph Chen 					struct spl_load_info *info)
419e12dde2dSJoseph Chen {
420e12dde2dSJoseph Chen 	return PART_BOOT;
421e12dde2dSJoseph Chen }
422e12dde2dSJoseph Chen #endif
423e12dde2dSJoseph Chen 
424e12dde2dSJoseph Chen static int spl_load_kernel_fit(struct spl_image_info *spl_image,
425e12dde2dSJoseph Chen 			       struct spl_load_info *info)
426e12dde2dSJoseph Chen {
427e12dde2dSJoseph Chen 	/*
428e12dde2dSJoseph Chen 	 * Never change the image order.
429e12dde2dSJoseph Chen 	 *
430e12dde2dSJoseph Chen 	 * Considering thunder-boot feature, there maybe asynchronous
431e12dde2dSJoseph Chen 	 * loading operation of these images and ramdisk is usually to
432e12dde2dSJoseph Chen 	 * be the last one.
433e12dde2dSJoseph Chen 	 *
434e12dde2dSJoseph Chen 	 * The .its content rule of kernel fit image follows U-Boot proper.
435e12dde2dSJoseph Chen 	 */
436e12dde2dSJoseph Chen 	const char *images[] = { FIT_FDT_PROP, FIT_KERNEL_PROP, FIT_RAMDISK_PROP, };
437e12dde2dSJoseph Chen 	struct spl_image_info image_info;
438e12dde2dSJoseph Chen 	char fit_header[info->bl_len];
439e12dde2dSJoseph Chen 	int images_noffset;
440e12dde2dSJoseph Chen 	int base_offset;
441e12dde2dSJoseph Chen 	int sector;
442e12dde2dSJoseph Chen 	int node, ret, i;
443e12dde2dSJoseph Chen 	void *fit;
444e12dde2dSJoseph Chen 
445e12dde2dSJoseph Chen 	if (spl_image->next_stage != SPL_NEXT_STAGE_KERNEL)
446e12dde2dSJoseph Chen 		return 0;
447e12dde2dSJoseph Chen 
448e12dde2dSJoseph Chen #ifdef CONFIG_SPL_LIBDISK_SUPPORT
449e12dde2dSJoseph Chen 	const char *part_name = PART_BOOT;
450e12dde2dSJoseph Chen 	disk_partition_t part_info;
451e12dde2dSJoseph Chen 
452e12dde2dSJoseph Chen 	part_name = spl_kernel_partition(spl_image, info);
453e12dde2dSJoseph Chen 	if (part_get_info_by_name(info->dev, part_name, &part_info) <= 0) {
454e12dde2dSJoseph Chen 		printf("%s: no partition\n", __func__);
455e12dde2dSJoseph Chen 		return -EINVAL;
456e12dde2dSJoseph Chen 	}
457e12dde2dSJoseph Chen 	sector = part_info.start;
458e12dde2dSJoseph Chen #else
459e12dde2dSJoseph Chen 	sector = CONFIG_SPL_FIT_LOAD_KERNEL_SECTOR;
460e12dde2dSJoseph Chen #endif
461e12dde2dSJoseph Chen 	if (info->read(info, sector, 1, &fit_header) != 1) {
462e12dde2dSJoseph Chen 		debug("%s: no memory\n", __func__);
463e12dde2dSJoseph Chen 		return -EIO;
464e12dde2dSJoseph Chen 	}
465e12dde2dSJoseph Chen 
466e12dde2dSJoseph Chen 	if (image_get_magic((void *)&fit_header) != FDT_MAGIC) {
467e12dde2dSJoseph Chen 		debug("%s: Not fit magic\n", __func__);
468e12dde2dSJoseph Chen 		return -EINVAL;
469e12dde2dSJoseph Chen 	}
470e12dde2dSJoseph Chen 
471e12dde2dSJoseph Chen 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
472e12dde2dSJoseph Chen 	if (!fit) {
473e12dde2dSJoseph Chen 		debug("%s: Cannot load blob\n", __func__);
474e12dde2dSJoseph Chen 		return -ENODEV;
475e12dde2dSJoseph Chen 	}
476e12dde2dSJoseph Chen 
477e12dde2dSJoseph Chen 	/* verify the configure node by keys, if required */
478e12dde2dSJoseph Chen #ifdef CONFIG_SPL_FIT_SIGNATURE
479e12dde2dSJoseph Chen 	int conf_noffset;
480e12dde2dSJoseph Chen 
481e12dde2dSJoseph Chen 	conf_noffset = fit_conf_get_node(fit, NULL);
482e12dde2dSJoseph Chen 	if (conf_noffset <= 0) {
483e12dde2dSJoseph Chen 		printf("No default config node\n");
484e12dde2dSJoseph Chen 		return -EINVAL;
485e12dde2dSJoseph Chen 	}
486e12dde2dSJoseph Chen 
487e12dde2dSJoseph Chen 	ret = fit_config_verify(fit, conf_noffset);
488e12dde2dSJoseph Chen 	if (ret) {
489e12dde2dSJoseph Chen 		printf("fit verify configure failed, ret=%d\n", ret);
490e12dde2dSJoseph Chen 		return ret;
491e12dde2dSJoseph Chen 	}
492e12dde2dSJoseph Chen 	printf("\n");
493e12dde2dSJoseph Chen #endif
494e12dde2dSJoseph Chen 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
495e12dde2dSJoseph Chen 	if (images_noffset < 0) {
496e12dde2dSJoseph Chen 		debug("%s: Cannot find /images node: %d\n",
497e12dde2dSJoseph Chen 		      __func__, images_noffset);
498e12dde2dSJoseph Chen 		return images_noffset;
499e12dde2dSJoseph Chen 	}
500e12dde2dSJoseph Chen 
501e12dde2dSJoseph Chen 	for (i = 0; i < ARRAY_SIZE(images); i++) {
502e12dde2dSJoseph Chen 		node = spl_fit_get_image_node(fit, images_noffset,
503e12dde2dSJoseph Chen 					      images[i], 0);
504e12dde2dSJoseph Chen 		if (node < 0) {
505e12dde2dSJoseph Chen 			debug("No image: %s\n", images[i]);
506e12dde2dSJoseph Chen 			continue;
507e12dde2dSJoseph Chen 		}
508e12dde2dSJoseph Chen 
509e12dde2dSJoseph Chen 		ret = spl_load_fit_image(info, sector, fit, base_offset,
510e12dde2dSJoseph Chen 					 node, &image_info);
511e12dde2dSJoseph Chen 		if (ret)
512e12dde2dSJoseph Chen 			return ret;
513e12dde2dSJoseph Chen 
514e12dde2dSJoseph Chen 		/* initial addr or entry point */
515e12dde2dSJoseph Chen 		if (!strcmp(images[i], FIT_FDT_PROP))
516e12dde2dSJoseph Chen 			spl_image->fdt_addr = (void *)image_info.load_addr;
517e12dde2dSJoseph Chen 		else if (!strcmp(images[i], FIT_KERNEL_PROP))
518e12dde2dSJoseph Chen 			spl_image->entry_point_os = image_info.load_addr;
519e12dde2dSJoseph Chen 	}
520e12dde2dSJoseph Chen 
521e12dde2dSJoseph Chen 	debug("entry_point=0x%08lx, entry_point_os=0x%08lx, fdt_addr=0x%08lx\n",
522e12dde2dSJoseph Chen 	      spl_image->entry_point, spl_image->entry_point_os,
523e12dde2dSJoseph Chen 	      (ulong)spl_image->fdt_addr);
524e12dde2dSJoseph Chen 
525e12dde2dSJoseph Chen 	return 0;
526e12dde2dSJoseph Chen }
527e12dde2dSJoseph Chen #endif
528e12dde2dSJoseph Chen 
529569a1737SJoseph Chen static int spl_internal_load_simple_fit(struct spl_image_info *spl_image,
530569a1737SJoseph Chen 					struct spl_load_info *info,
531569a1737SJoseph Chen 					ulong sector, void *fit_header)
532569a1737SJoseph Chen {
533569a1737SJoseph Chen 	struct spl_image_info image_info;
534569a1737SJoseph Chen 	int base_offset;
535569a1737SJoseph Chen 	int images, ret;
536569a1737SJoseph Chen 	int index = 0;
537569a1737SJoseph Chen 	int node = -1;
538569a1737SJoseph Chen 	void *fit;
539569a1737SJoseph Chen 
540569a1737SJoseph Chen 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
541569a1737SJoseph Chen 	if (!fit) {
542569a1737SJoseph Chen 		debug("%s: Cannot load blob\n", __func__);
543569a1737SJoseph Chen 		return -1;
544569a1737SJoseph Chen 	}
545f1dcee59SSimon Glass 
546736806fbSAndre Przywara 	/* find the node holding the images information */
547f1dcee59SSimon Glass 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
548f1dcee59SSimon Glass 	if (images < 0) {
549f1dcee59SSimon Glass 		debug("%s: Cannot find /images node: %d\n", __func__, images);
550f1dcee59SSimon Glass 		return -1;
551f1dcee59SSimon Glass 	}
552736806fbSAndre Przywara 
5531f452cbfSJoseph Chen 	/* if board sigs verify required, check self */
5541f452cbfSJoseph Chen 	if (fit_board_verify_required_sigs() &&
5551f452cbfSJoseph Chen 	    !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
5561f452cbfSJoseph Chen 		printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n");
5571f452cbfSJoseph Chen 		hang();
5581f452cbfSJoseph Chen 	}
5591f452cbfSJoseph Chen 
560583377c4SJoseph Chen 	/* verify the configure node by keys, if required */
561583377c4SJoseph Chen #ifdef CONFIG_SPL_FIT_SIGNATURE
562583377c4SJoseph Chen 	int conf_noffset;
563583377c4SJoseph Chen 
564583377c4SJoseph Chen 	conf_noffset = fit_conf_get_node(fit, NULL);
565a7560f55SJoseph Chen 	if (conf_noffset <= 0) {
566a7560f55SJoseph Chen 		printf("No default config node\n");
567a7560f55SJoseph Chen 		return -EINVAL;
568a7560f55SJoseph Chen 	}
569a7560f55SJoseph Chen 
570583377c4SJoseph Chen 	ret = fit_config_verify(fit, conf_noffset);
571583377c4SJoseph Chen 	if (ret) {
572583377c4SJoseph Chen 		printf("fit verify configure failed, ret=%d\n", ret);
573583377c4SJoseph Chen 		return ret;
574583377c4SJoseph Chen 	}
575583377c4SJoseph Chen 	printf("\n");
576583377c4SJoseph Chen 
5777a137075SJoseph Chen #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT
5787a137075SJoseph Chen 	uint32_t this_index, min_index;
5797a137075SJoseph Chen 
5807a137075SJoseph Chen 	ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL,
5817a137075SJoseph Chen 					&this_index, &min_index);
5827a137075SJoseph Chen 	if (ret) {
5837a137075SJoseph Chen 		printf("fit failed to get rollback index, ret=%d\n", ret);
5847a137075SJoseph Chen 		return ret;
5857a137075SJoseph Chen 	} else if (this_index < min_index) {
5867a137075SJoseph Chen 		printf("fit reject rollback: %d < %d(min)\n",
5877a137075SJoseph Chen 		       this_index, min_index);
5887a137075SJoseph Chen 		return -EINVAL;
5897a137075SJoseph Chen 	}
5907a137075SJoseph Chen 
591e5ca21e8SJoseph Chen 	printf("rollback index: %d >= %d(min), OK\n", this_index, min_index);
5927a137075SJoseph Chen #endif
5937a137075SJoseph Chen #endif
594342d050eSJason Zhu 
595342d050eSJason Zhu 	/*
596342d050eSJason Zhu 	 * If required to start the other core before load "loadables"
597342d050eSJason Zhu 	 * firmwares, use the config "standalone" to load the other core's
598342d050eSJason Zhu 	 * firmware, then start it.
599342d050eSJason Zhu 	 * Normally, different cores' firmware is attach to the config
600342d050eSJason Zhu 	 * "loadables" and load them together.
601342d050eSJason Zhu 	 */
602342d050eSJason Zhu 	if (node < 0)
603342d050eSJason Zhu 		node = spl_fit_get_image_node(fit, images, FIT_STANDALONE_PROP,
604342d050eSJason Zhu 					      0);
605342d050eSJason Zhu 	if (node > 0) {
606342d050eSJason Zhu 		/* Load the image and set up the spl_image structure */
607342d050eSJason Zhu 		ret = spl_load_fit_image(info, sector, fit, base_offset, node,
6089a65720bSJason Zhu 					 &image_info);
609342d050eSJason Zhu 		if (!ret) {
6109a65720bSJason Zhu 			if (image_info.entry_point == FDT_ERROR)
6119a65720bSJason Zhu 				image_info.entry_point = image_info.load_addr;
6129a65720bSJason Zhu 
6139a65720bSJason Zhu 			ret = spl_fit_standalone_release(image_info.entry_point);
614342d050eSJason Zhu 			if (ret)
615342d050eSJason Zhu 				printf("Start standalone fail, ret = %d\n",
616342d050eSJason Zhu 				       ret);
617342d050eSJason Zhu 		}
618342d050eSJason Zhu 
619e12dde2dSJoseph Chen 		/* standalone is special one, continue to find others */
620342d050eSJason Zhu 		node = -1;
621342d050eSJason Zhu 	}
622342d050eSJason Zhu 
6239719e14fSPhilipp Tomsich 	/*
6249719e14fSPhilipp Tomsich 	 * Find the U-Boot image using the following search order:
6259719e14fSPhilipp Tomsich 	 *   - start at 'firmware' (e.g. an ARM Trusted Firmware)
6269719e14fSPhilipp Tomsich 	 *   - fall back 'kernel' (e.g. a Falcon-mode OS boot
6279719e14fSPhilipp Tomsich 	 *   - fall back to using the first 'loadables' entry
6289719e14fSPhilipp Tomsich 	 */
629ebec805aSYork Sun 	if (node < 0)
630c04fe8bfSMichal Simek 		node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
631c04fe8bfSMichal Simek 					      0);
6329719e14fSPhilipp Tomsich #ifdef CONFIG_SPL_OS_BOOT
6339719e14fSPhilipp Tomsich 	if (node < 0)
6349719e14fSPhilipp Tomsich 		node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
6359719e14fSPhilipp Tomsich #endif
636f1dcee59SSimon Glass 	if (node < 0) {
637736806fbSAndre Przywara 		debug("could not find firmware image, trying loadables...\n");
638736806fbSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
639411cf32dSAndre Przywara 		/*
640411cf32dSAndre Przywara 		 * If we pick the U-Boot image from "loadables", start at
641411cf32dSAndre Przywara 		 * the second image when later loading additional images.
642411cf32dSAndre Przywara 		 */
643411cf32dSAndre Przywara 		index = 1;
644736806fbSAndre Przywara 	}
645736806fbSAndre Przywara 	if (node < 0) {
646736806fbSAndre Przywara 		debug("%s: Cannot find u-boot image node: %d\n",
647736806fbSAndre Przywara 		      __func__, node);
648f1dcee59SSimon Glass 		return -1;
649f1dcee59SSimon Glass 	}
650f1dcee59SSimon Glass 
6518baa3818SAndre Przywara 	/* Load the image and set up the spl_image structure */
6528baa3818SAndre Przywara 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
6538baa3818SAndre Przywara 				 spl_image);
6548baa3818SAndre Przywara 	if (ret)
6558baa3818SAndre Przywara 		return ret;
6568baa3818SAndre Przywara 
6579719e14fSPhilipp Tomsich 	/*
6589719e14fSPhilipp Tomsich 	 * For backward compatibility, we treat the first node that is
6599719e14fSPhilipp Tomsich 	 * as a U-Boot image, if no OS-type has been declared.
6609719e14fSPhilipp Tomsich 	 */
6618118cce3SPhilipp Tomsich 	if (!spl_fit_image_get_os(fit, node, &spl_image->os))
662ebec805aSYork Sun 		debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
6639719e14fSPhilipp Tomsich #if !defined(CONFIG_SPL_OS_BOOT)
6649719e14fSPhilipp Tomsich 	else
665f4d7d859SSimon Glass 		spl_image->os = IH_OS_U_BOOT;
666ebec805aSYork Sun #endif
667f1dcee59SSimon Glass 
668f1dcee59SSimon Glass 	/*
6699719e14fSPhilipp Tomsich 	 * Booting a next-stage U-Boot may require us to append the FDT.
6709719e14fSPhilipp Tomsich 	 * We allow this to fail, as the U-Boot image might embed its FDT.
671f1dcee59SSimon Glass 	 */
6729719e14fSPhilipp Tomsich 	if (spl_image->os == IH_OS_U_BOOT)
6739719e14fSPhilipp Tomsich 		spl_fit_append_fdt(spl_image, info, sector, fit,
6749719e14fSPhilipp Tomsich 				   images, base_offset);
675411cf32dSAndre Przywara 
676411cf32dSAndre Przywara 	/* Now check if there are more images for us to load */
677411cf32dSAndre Przywara 	for (; ; index++) {
6789719e14fSPhilipp Tomsich 		uint8_t os_type = IH_OS_INVALID;
6799719e14fSPhilipp Tomsich 
680411cf32dSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", index);
681411cf32dSAndre Przywara 		if (node < 0)
682411cf32dSAndre Przywara 			break;
683411cf32dSAndre Przywara 
684e12dde2dSJoseph Chen 		if (!spl_fit_image_get_os(fit, node, &os_type))
685e12dde2dSJoseph Chen 			debug("Loadable is %s\n", genimg_get_os_name(os_type));
686e12dde2dSJoseph Chen 
687e12dde2dSJoseph Chen 		/* skip U-Boot ? */
688e12dde2dSJoseph Chen 		if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL &&
689e12dde2dSJoseph Chen 		    os_type == IH_OS_U_BOOT)
690e12dde2dSJoseph Chen 		    continue;
691e12dde2dSJoseph Chen 
692411cf32dSAndre Przywara 		ret = spl_load_fit_image(info, sector, fit, base_offset, node,
693411cf32dSAndre Przywara 					 &image_info);
694411cf32dSAndre Przywara 		if (ret < 0)
695411cf32dSAndre Przywara 			continue;
696411cf32dSAndre Przywara 
6971f1cf67bSPhilipp Tomsich 		if (os_type == IH_OS_U_BOOT) {
6981f1cf67bSPhilipp Tomsich 			spl_fit_append_fdt(&image_info, info, sector,
6999719e14fSPhilipp Tomsich 					   fit, images, base_offset);
7001f1cf67bSPhilipp Tomsich 			spl_image->fdt_addr = image_info.fdt_addr;
7011f1cf67bSPhilipp Tomsich 		}
7029719e14fSPhilipp Tomsich 
703411cf32dSAndre Przywara 		/*
704411cf32dSAndre Przywara 		 * If the "firmware" image did not provide an entry point,
705411cf32dSAndre Przywara 		 * use the first valid entry point from the loadables.
706411cf32dSAndre Przywara 		 */
707411cf32dSAndre Przywara 		if (spl_image->entry_point == FDT_ERROR &&
708411cf32dSAndre Przywara 		    image_info.entry_point != FDT_ERROR)
709411cf32dSAndre Przywara 			spl_image->entry_point = image_info.entry_point;
7101f1cf67bSPhilipp Tomsich 
7111f1cf67bSPhilipp Tomsich 		/* Record our loadables into the FDT */
7121f1cf67bSPhilipp Tomsich 		if (spl_image->fdt_addr)
7131f1cf67bSPhilipp Tomsich 			spl_fit_record_loadable(fit, images, index,
7141f1cf67bSPhilipp Tomsich 						spl_image->fdt_addr,
7151f1cf67bSPhilipp Tomsich 						&image_info);
716411cf32dSAndre Przywara 	}
717411cf32dSAndre Przywara 
718411cf32dSAndre Przywara 	/*
719411cf32dSAndre Przywara 	 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
720411cf32dSAndre Przywara 	 * Makefile will set it to 0 and it will end up as the entry point
721411cf32dSAndre Przywara 	 * here. What it actually means is: use the load address.
722411cf32dSAndre Przywara 	 */
723411cf32dSAndre Przywara 	if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
724411cf32dSAndre Przywara 		spl_image->entry_point = spl_image->load_addr;
725411cf32dSAndre Przywara 
726411cf32dSAndre Przywara 	return 0;
727f1dcee59SSimon Glass }
72822c7c1a8SJoseph Chen 
72922c7c1a8SJoseph Chen int spl_load_simple_fit(struct spl_image_info *spl_image,
73022c7c1a8SJoseph Chen 			struct spl_load_info *info, ulong sector, void *fit)
73122c7c1a8SJoseph Chen {
73222c7c1a8SJoseph Chen 	ulong sector_offs = sector;
733e12dde2dSJoseph Chen 	int ret = -EINVAL;
73422c7c1a8SJoseph Chen 	int i;
73522c7c1a8SJoseph Chen 
73622c7c1a8SJoseph Chen 	for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) {
73722c7c1a8SJoseph Chen 		if (i > 0) {
73822c7c1a8SJoseph Chen 			sector_offs +=
73922c7c1a8SJoseph Chen 			   i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len);
74022c7c1a8SJoseph Chen 			printf("Trying fit image at 0x%lx sector\n", sector_offs);
74122c7c1a8SJoseph Chen 			if (info->read(info, sector_offs, 1, fit) != 1) {
74222c7c1a8SJoseph Chen 				printf("IO error\n");
74322c7c1a8SJoseph Chen 				continue;
74422c7c1a8SJoseph Chen 			}
74522c7c1a8SJoseph Chen 		}
74622c7c1a8SJoseph Chen 
74722c7c1a8SJoseph Chen 		if (image_get_magic(fit) != FDT_MAGIC) {
748e12dde2dSJoseph Chen 			printf("Not fit magic\n");
74922c7c1a8SJoseph Chen 			continue;
75022c7c1a8SJoseph Chen 		}
75122c7c1a8SJoseph Chen 
752e12dde2dSJoseph Chen 		ret = spl_internal_load_simple_fit(spl_image, info,
753e12dde2dSJoseph Chen 						   sector_offs, fit);
754e12dde2dSJoseph Chen 		if (!ret) {
755e12dde2dSJoseph Chen #ifdef CONFIG_SPL_FIT_LOAD_KERNEL
756e12dde2dSJoseph Chen 			ret = spl_load_kernel_fit(spl_image, info);
757e12dde2dSJoseph Chen #endif
758e12dde2dSJoseph Chen 			return ret;
759e12dde2dSJoseph Chen 		}
76022c7c1a8SJoseph Chen 	}
76122c7c1a8SJoseph Chen 
762e12dde2dSJoseph Chen 	return ret;
76322c7c1a8SJoseph Chen }
76422c7c1a8SJoseph Chen 
765