xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision 0e00a84cdedf7a1949486746225b35984b351eca)
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>
9f1dcee59SSimon Glass #include <errno.h>
10f1dcee59SSimon Glass #include <image.h>
11*0e00a84cSMasahiro Yamada #include <linux/libfdt.h>
12f1dcee59SSimon Glass #include <spl.h>
1390d1164aSKever Yang #include <malloc.h>
14f1dcee59SSimon Glass 
15b81c4739SYork Sun #ifndef CONFIG_SYS_BOOTM_LEN
16b81c4739SYork Sun #define CONFIG_SYS_BOOTM_LEN	(64 << 20)
17b81c4739SYork Sun #endif
18b81c4739SYork Sun 
19736806fbSAndre Przywara /**
201f1cf67bSPhilipp Tomsich  * spl_fit_get_image_name(): By using the matching configuration subnode,
21736806fbSAndre Przywara  * retrieve the name of an image, specified by a property name and an index
22736806fbSAndre Przywara  * into that.
23736806fbSAndre Przywara  * @fit:	Pointer to the FDT blob.
24736806fbSAndre Przywara  * @images:	Offset of the /images subnode.
25736806fbSAndre Przywara  * @type:	Name of the property within the configuration subnode.
26736806fbSAndre Przywara  * @index:	Index into the list of strings in this property.
271f1cf67bSPhilipp Tomsich  * @outname:	Name of the image
28736806fbSAndre Przywara  *
291f1cf67bSPhilipp Tomsich  * Return:	0 on success, or a negative error number
30736806fbSAndre Przywara  */
311f1cf67bSPhilipp Tomsich static int spl_fit_get_image_name(const void *fit, int images,
321f1cf67bSPhilipp Tomsich 				  const char *type, int index,
331f1cf67bSPhilipp Tomsich 				  char **outname)
344b9340abSAndre Przywara {
354b9340abSAndre Przywara 	const char *name, *str;
361f1cf67bSPhilipp Tomsich 	__maybe_unused int node;
371f1cf67bSPhilipp Tomsich 	int conf_node;
384b9340abSAndre Przywara 	int len, i;
39f1dcee59SSimon Glass 
403863f840SCooper Jr., Franklin 	conf_node = fit_find_config_node(fit);
414b9340abSAndre Przywara 	if (conf_node < 0) {
42f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
43f1dcee59SSimon Glass 		printf("No matching DT out of these options:\n");
444b9340abSAndre Przywara 		for (node = fdt_first_subnode(fit, conf_node);
45f1dcee59SSimon Glass 		     node >= 0;
464b9340abSAndre Przywara 		     node = fdt_next_subnode(fit, node)) {
474b9340abSAndre Przywara 			name = fdt_getprop(fit, node, "description", &len);
48f1dcee59SSimon Glass 			printf("   %s\n", name);
49f1dcee59SSimon Glass 		}
50f1dcee59SSimon Glass #endif
514b9340abSAndre Przywara 		return conf_node;
524b9340abSAndre Przywara 	}
53f1dcee59SSimon Glass 
544b9340abSAndre Przywara 	name = fdt_getprop(fit, conf_node, type, &len);
554b9340abSAndre Przywara 	if (!name) {
564b9340abSAndre Przywara 		debug("cannot find property '%s': %d\n", type, len);
574b9340abSAndre Przywara 		return -EINVAL;
584b9340abSAndre Przywara 	}
594b9340abSAndre Przywara 
604b9340abSAndre Przywara 	str = name;
614b9340abSAndre Przywara 	for (i = 0; i < index; i++) {
624b9340abSAndre Przywara 		str = strchr(str, '\0') + 1;
634b9340abSAndre Przywara 		if (!str || (str - name >= len)) {
644b9340abSAndre Przywara 			debug("no string for index %d\n", index);
654b9340abSAndre Przywara 			return -E2BIG;
664b9340abSAndre Przywara 		}
674b9340abSAndre Przywara 	}
684b9340abSAndre Przywara 
691f1cf67bSPhilipp Tomsich 	*outname = (char *)str;
701f1cf67bSPhilipp Tomsich 	return 0;
711f1cf67bSPhilipp Tomsich }
721f1cf67bSPhilipp Tomsich 
731f1cf67bSPhilipp Tomsich /**
741f1cf67bSPhilipp Tomsich  * spl_fit_get_image_node(): By using the matching configuration subnode,
751f1cf67bSPhilipp Tomsich  * retrieve the name of an image, specified by a property name and an index
761f1cf67bSPhilipp Tomsich  * into that.
771f1cf67bSPhilipp Tomsich  * @fit:	Pointer to the FDT blob.
781f1cf67bSPhilipp Tomsich  * @images:	Offset of the /images subnode.
791f1cf67bSPhilipp Tomsich  * @type:	Name of the property within the configuration subnode.
801f1cf67bSPhilipp Tomsich  * @index:	Index into the list of strings in this property.
811f1cf67bSPhilipp Tomsich  *
821f1cf67bSPhilipp Tomsich  * Return:	the node offset of the respective image node or a negative
831f1cf67bSPhilipp Tomsich  *		error number.
841f1cf67bSPhilipp Tomsich  */
851f1cf67bSPhilipp Tomsich static int spl_fit_get_image_node(const void *fit, int images,
861f1cf67bSPhilipp Tomsich 				  const char *type, int index)
871f1cf67bSPhilipp Tomsich {
881f1cf67bSPhilipp Tomsich 	char *str;
891f1cf67bSPhilipp Tomsich 	int err;
901f1cf67bSPhilipp Tomsich 	int node;
911f1cf67bSPhilipp Tomsich 
921f1cf67bSPhilipp Tomsich 	err = spl_fit_get_image_name(fit, images, type, index, &str);
931f1cf67bSPhilipp Tomsich 	if (err)
941f1cf67bSPhilipp Tomsich 		return err;
951f1cf67bSPhilipp Tomsich 
964b9340abSAndre Przywara 	debug("%s: '%s'\n", type, str);
971f1cf67bSPhilipp Tomsich 
984b9340abSAndre Przywara 	node = fdt_subnode_offset(fit, images, str);
994b9340abSAndre Przywara 	if (node < 0) {
1004b9340abSAndre Przywara 		debug("cannot find image node '%s': %d\n", str, node);
1014b9340abSAndre Przywara 		return -EINVAL;
1024b9340abSAndre Przywara 	}
1034b9340abSAndre Przywara 
104736806fbSAndre Przywara 	return node;
105f1dcee59SSimon Glass }
106f1dcee59SSimon Glass 
107eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset)
108eafd5410SLokesh Vutla {
109eafd5410SLokesh Vutla 	/*
110eafd5410SLokesh Vutla 	 * If it is a FS read, get the first address before offset which is
111eafd5410SLokesh Vutla 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
112eafd5410SLokesh Vutla 	 * block number to which offset belongs.
113eafd5410SLokesh Vutla 	 */
114eafd5410SLokesh Vutla 	if (info->filename)
115eafd5410SLokesh Vutla 		return offset & ~(ARCH_DMA_MINALIGN - 1);
116eafd5410SLokesh Vutla 
117eafd5410SLokesh Vutla 	return offset / info->bl_len;
118eafd5410SLokesh Vutla }
119eafd5410SLokesh Vutla 
120eafd5410SLokesh Vutla static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
121eafd5410SLokesh Vutla {
122eafd5410SLokesh Vutla 	/*
123eafd5410SLokesh Vutla 	 * If it is a FS read, get the difference between the offset and
124eafd5410SLokesh Vutla 	 * the first address before offset which is aligned to
125eafd5410SLokesh Vutla 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
126eafd5410SLokesh Vutla 	 * block.
127eafd5410SLokesh Vutla 	 */
128eafd5410SLokesh Vutla 	if (info->filename)
129eafd5410SLokesh Vutla 		return offset & (ARCH_DMA_MINALIGN - 1);
130eafd5410SLokesh Vutla 
131eafd5410SLokesh Vutla 	return offset % info->bl_len;
132eafd5410SLokesh Vutla }
133eafd5410SLokesh Vutla 
134eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size,
135eafd5410SLokesh Vutla 				  int offset)
136eafd5410SLokesh Vutla {
1373cc1f380SLokesh Vutla 	data_size = data_size + get_aligned_image_overhead(info, offset);
1383cc1f380SLokesh Vutla 
139eafd5410SLokesh Vutla 	if (info->filename)
1403cc1f380SLokesh Vutla 		return data_size;
141eafd5410SLokesh Vutla 
142eafd5410SLokesh Vutla 	return (data_size + info->bl_len - 1) / info->bl_len;
143eafd5410SLokesh Vutla }
144eafd5410SLokesh Vutla 
1458baa3818SAndre Przywara /**
1468baa3818SAndre Przywara  * spl_load_fit_image(): load the image described in a certain FIT node
1478baa3818SAndre Przywara  * @info:	points to information about the device to load data from
1488baa3818SAndre Przywara  * @sector:	the start sector of the FIT image on the device
1498baa3818SAndre Przywara  * @fit:	points to the flattened device tree blob describing the FIT
1508baa3818SAndre Przywara  *		image
1518baa3818SAndre Przywara  * @base_offset: the beginning of the data area containing the actual
1528baa3818SAndre Przywara  *		image data, relative to the beginning of the FIT
1538baa3818SAndre Przywara  * @node:	offset of the DT node describing the image to load (relative
1548baa3818SAndre Przywara  *		to @fit)
1558baa3818SAndre Przywara  * @image_info:	will be filled with information about the loaded image
1568baa3818SAndre Przywara  *		If the FIT node does not contain a "load" (address) property,
1578baa3818SAndre Przywara  *		the image gets loaded to the address pointed to by the
1588baa3818SAndre Przywara  *		load_addr member in this struct.
1598baa3818SAndre Przywara  *
1608baa3818SAndre Przywara  * Return:	0 on success or a negative error number.
1618baa3818SAndre Przywara  */
1628baa3818SAndre Przywara static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
1638baa3818SAndre Przywara 			      void *fit, ulong base_offset, int node,
1648baa3818SAndre Przywara 			      struct spl_image_info *image_info)
1658baa3818SAndre Przywara {
1660ea10b9fSYork Sun 	int offset;
1678baa3818SAndre Przywara 	size_t length;
1680ea10b9fSYork Sun 	int len;
169d18ad2e6SYork Sun 	ulong size;
1708baa3818SAndre Przywara 	ulong load_addr, load_ptr;
1718baa3818SAndre Przywara 	void *src;
1728baa3818SAndre Przywara 	ulong overhead;
1738baa3818SAndre Przywara 	int nr_sectors;
1748baa3818SAndre Przywara 	int align_len = ARCH_DMA_MINALIGN - 1;
175b81c4739SYork Sun 	uint8_t image_comp = -1, type = -1;
1760ea10b9fSYork Sun 	const void *data;
177b81c4739SYork Sun 
178b81c4739SYork Sun 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
179b81c4739SYork Sun 		if (fit_image_get_comp(fit, node, &image_comp))
180b81c4739SYork Sun 			puts("Cannot get image compression format.\n");
181b81c4739SYork Sun 		else
182b81c4739SYork Sun 			debug("%s ", genimg_get_comp_name(image_comp));
183b81c4739SYork Sun 
184b81c4739SYork Sun 		if (fit_image_get_type(fit, node, &type))
185b81c4739SYork Sun 			puts("Cannot get image type.\n");
186b81c4739SYork Sun 		else
187b81c4739SYork Sun 			debug("%s ", genimg_get_type_name(type));
188b81c4739SYork Sun 	}
1898baa3818SAndre Przywara 
1900ea10b9fSYork Sun 	if (fit_image_get_load(fit, node, &load_addr))
1918baa3818SAndre Przywara 		load_addr = image_info->load_addr;
1920ea10b9fSYork Sun 
1930ea10b9fSYork Sun 	if (!fit_image_get_data_offset(fit, node, &offset)) {
1940ea10b9fSYork Sun 		/* External data */
1950ea10b9fSYork Sun 		offset += base_offset;
1960ea10b9fSYork Sun 		if (fit_image_get_data_size(fit, node, &len))
1970ea10b9fSYork Sun 			return -ENOENT;
1980ea10b9fSYork Sun 
1998baa3818SAndre Przywara 		load_ptr = (load_addr + align_len) & ~align_len;
20090d1164aSKever Yang #ifdef CONFIG_ROCKCHIP_RK3399
20190d1164aSKever Yang 		if ((load_ptr & 0xffff0000) == CONFIG_ROCKCHIP_IRAM_START_ADDR)
20290d1164aSKever Yang 			load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len);
20390d1164aSKever Yang #endif
2040ea10b9fSYork Sun 		length = len;
2058baa3818SAndre Przywara 
2068baa3818SAndre Przywara 		overhead = get_aligned_image_overhead(info, offset);
2078baa3818SAndre Przywara 		nr_sectors = get_aligned_image_size(info, length, offset);
2088baa3818SAndre Przywara 
2090ea10b9fSYork Sun 		if (info->read(info,
2100ea10b9fSYork Sun 			       sector + get_aligned_image_offset(info, offset),
2118baa3818SAndre Przywara 			       nr_sectors, (void *)load_ptr) != nr_sectors)
2128baa3818SAndre Przywara 			return -EIO;
2138baa3818SAndre Przywara 
2140ea10b9fSYork Sun 		debug("External data: dst=%lx, offset=%x, size=%lx\n",
2150ea10b9fSYork Sun 		      load_ptr, offset, (unsigned long)length);
2168baa3818SAndre Przywara 		src = (void *)load_ptr + overhead;
2170ea10b9fSYork Sun 	} else {
2180ea10b9fSYork Sun 		/* Embedded data */
2190ea10b9fSYork Sun 		if (fit_image_get_data(fit, node, &data, &length)) {
2200ea10b9fSYork Sun 			puts("Cannot get image data/size\n");
2210ea10b9fSYork Sun 			return -ENOENT;
2220ea10b9fSYork Sun 		}
2230ea10b9fSYork Sun 		debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
2240ea10b9fSYork Sun 		      (unsigned long)length);
2250ea10b9fSYork Sun 		src = (void *)data;
2260ea10b9fSYork Sun 	}
2270ea10b9fSYork Sun 
2288baa3818SAndre Przywara #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
2298baa3818SAndre Przywara 	board_fit_image_post_process(&src, &length);
2308baa3818SAndre Przywara #endif
2318baa3818SAndre Przywara 
232b81c4739SYork Sun 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT)	&&
233b81c4739SYork Sun 	    IS_ENABLED(CONFIG_SPL_GZIP)		&&
234b81c4739SYork Sun 	    image_comp == IH_COMP_GZIP		&&
235b81c4739SYork Sun 	    type == IH_TYPE_KERNEL) {
236d18ad2e6SYork Sun 		size = length;
237b81c4739SYork Sun 		if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
238d18ad2e6SYork Sun 			   src, &size)) {
239b81c4739SYork Sun 			puts("Uncompressing error\n");
240b81c4739SYork Sun 			return -EIO;
241b81c4739SYork Sun 		}
242d18ad2e6SYork Sun 		length = size;
243b81c4739SYork Sun 	} else {
2448baa3818SAndre Przywara 		memcpy((void *)load_addr, src, length);
245b81c4739SYork Sun 	}
2468baa3818SAndre Przywara 
2478baa3818SAndre Przywara 	if (image_info) {
2488baa3818SAndre Przywara 		image_info->load_addr = load_addr;
2498baa3818SAndre Przywara 		image_info->size = length;
2508baa3818SAndre Przywara 		image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
2518baa3818SAndre Przywara 	}
2528baa3818SAndre Przywara 
2538baa3818SAndre Przywara 	return 0;
2548baa3818SAndre Przywara }
2558baa3818SAndre Przywara 
2569719e14fSPhilipp Tomsich static int spl_fit_append_fdt(struct spl_image_info *spl_image,
2579719e14fSPhilipp Tomsich 			      struct spl_load_info *info, ulong sector,
2589719e14fSPhilipp Tomsich 			      void *fit, int images, ulong base_offset)
2599719e14fSPhilipp Tomsich {
2609719e14fSPhilipp Tomsich 	struct spl_image_info image_info;
2619719e14fSPhilipp Tomsich 	int node, ret;
2629719e14fSPhilipp Tomsich 
2639719e14fSPhilipp Tomsich 	/* Figure out which device tree the board wants to use */
2649719e14fSPhilipp Tomsich 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
2659719e14fSPhilipp Tomsich 	if (node < 0) {
2669719e14fSPhilipp Tomsich 		debug("%s: cannot find FDT node\n", __func__);
2679719e14fSPhilipp Tomsich 		return node;
2689719e14fSPhilipp Tomsich 	}
2699719e14fSPhilipp Tomsich 
2709719e14fSPhilipp Tomsich 	/*
2719719e14fSPhilipp Tomsich 	 * Read the device tree and place it after the image.
2729719e14fSPhilipp Tomsich 	 * Align the destination address to ARCH_DMA_MINALIGN.
2739719e14fSPhilipp Tomsich 	 */
2749719e14fSPhilipp Tomsich 	image_info.load_addr = spl_image->load_addr + spl_image->size;
2759719e14fSPhilipp Tomsich 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
2769719e14fSPhilipp Tomsich 				 &image_info);
2771f1cf67bSPhilipp Tomsich 
2781f1cf67bSPhilipp Tomsich 	if (ret < 0)
2791f1cf67bSPhilipp Tomsich 		return ret;
2801f1cf67bSPhilipp Tomsich 
2811f1cf67bSPhilipp Tomsich 	/* Make the load-address of the FDT available for the SPL framework */
2821f1cf67bSPhilipp Tomsich 	spl_image->fdt_addr = (void *)image_info.load_addr;
2838118cce3SPhilipp Tomsich #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
2841f1cf67bSPhilipp Tomsich 	/* Try to make space, so we can inject details on the loadables */
2851f1cf67bSPhilipp Tomsich 	ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
2868118cce3SPhilipp Tomsich #endif
2871f1cf67bSPhilipp Tomsich 
2881f1cf67bSPhilipp Tomsich 	return ret;
2891f1cf67bSPhilipp Tomsich }
2901f1cf67bSPhilipp Tomsich 
2911f1cf67bSPhilipp Tomsich static int spl_fit_record_loadable(const void *fit, int images, int index,
2921f1cf67bSPhilipp Tomsich 				   void *blob, struct spl_image_info *image)
2931f1cf67bSPhilipp Tomsich {
2948118cce3SPhilipp Tomsich 	int ret = 0;
2958118cce3SPhilipp Tomsich #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
2961f1cf67bSPhilipp Tomsich 	char *name;
2978118cce3SPhilipp Tomsich 	int node;
2981f1cf67bSPhilipp Tomsich 
2991f1cf67bSPhilipp Tomsich 	ret = spl_fit_get_image_name(fit, images, "loadables",
3001f1cf67bSPhilipp Tomsich 				     index, &name);
3011f1cf67bSPhilipp Tomsich 	if (ret < 0)
3021f1cf67bSPhilipp Tomsich 		return ret;
3031f1cf67bSPhilipp Tomsich 
3041f1cf67bSPhilipp Tomsich 	node = spl_fit_get_image_node(fit, images, "loadables", index);
3051f1cf67bSPhilipp Tomsich 
3061f1cf67bSPhilipp Tomsich 	ret = fdt_record_loadable(blob, index, name, image->load_addr,
3071f1cf67bSPhilipp Tomsich 				  image->size, image->entry_point,
3081f1cf67bSPhilipp Tomsich 				  fdt_getprop(fit, node, "type", NULL),
3091f1cf67bSPhilipp Tomsich 				  fdt_getprop(fit, node, "os", NULL));
3108118cce3SPhilipp Tomsich #endif
3119719e14fSPhilipp Tomsich 	return ret;
3129719e14fSPhilipp Tomsich }
3139719e14fSPhilipp Tomsich 
3148118cce3SPhilipp Tomsich static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
3158118cce3SPhilipp Tomsich {
3168118cce3SPhilipp Tomsich #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
3178118cce3SPhilipp Tomsich 	return -ENOTSUPP;
3188118cce3SPhilipp Tomsich #else
3198118cce3SPhilipp Tomsich 	return fit_image_get_os(fit, noffset, os);
3208118cce3SPhilipp Tomsich #endif
3218118cce3SPhilipp Tomsich }
3228118cce3SPhilipp Tomsich 
323f4d7d859SSimon Glass int spl_load_simple_fit(struct spl_image_info *spl_image,
324f4d7d859SSimon Glass 			struct spl_load_info *info, ulong sector, void *fit)
325f1dcee59SSimon Glass {
326f1dcee59SSimon Glass 	int sectors;
3278baa3818SAndre Przywara 	ulong size;
328f1dcee59SSimon Glass 	unsigned long count;
3298baa3818SAndre Przywara 	struct spl_image_info image_info;
330ebec805aSYork Sun 	int node = -1;
331ebec805aSYork Sun 	int images, ret;
332eafd5410SLokesh Vutla 	int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
333411cf32dSAndre Przywara 	int index = 0;
334f1dcee59SSimon Glass 
335f1dcee59SSimon Glass 	/*
336ebec805aSYork Sun 	 * For FIT with external data, figure out where the external images
337ebec805aSYork Sun 	 * start. This is the base for the data-offset properties in each
338ebec805aSYork Sun 	 * image.
339f1dcee59SSimon Glass 	 */
340f1dcee59SSimon Glass 	size = fdt_totalsize(fit);
341f1dcee59SSimon Glass 	size = (size + 3) & ~3;
342f1dcee59SSimon Glass 	base_offset = (size + 3) & ~3;
343f1dcee59SSimon Glass 
344f1dcee59SSimon Glass 	/*
345f1dcee59SSimon Glass 	 * So far we only have one block of data from the FIT. Read the entire
346f1dcee59SSimon Glass 	 * thing, including that first block, placing it so it finishes before
347f1dcee59SSimon Glass 	 * where we will load the image.
348f1dcee59SSimon Glass 	 *
349f1dcee59SSimon Glass 	 * Note that we will load the image such that its first byte will be
350f1dcee59SSimon Glass 	 * at the load address. Since that byte may be part-way through a
351f1dcee59SSimon Glass 	 * block, we may load the image up to one block before the load
352f1dcee59SSimon Glass 	 * address. So take account of that here by subtracting an addition
353f1dcee59SSimon Glass 	 * block length from the FIT start position.
354f1dcee59SSimon Glass 	 *
355f1dcee59SSimon Glass 	 * In fact the FIT has its own load address, but we assume it cannot
356f1dcee59SSimon Glass 	 * be before CONFIG_SYS_TEXT_BASE.
357ebec805aSYork Sun 	 *
358ebec805aSYork Sun 	 * For FIT with data embedded, data is loaded as part of FIT image.
359ebec805aSYork Sun 	 * For FIT with external data, data is not loaded in this step.
360f1dcee59SSimon Glass 	 */
3618b528709SLokesh Vutla 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
3628b528709SLokesh Vutla 			align_len) & ~align_len);
363eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, size, 0);
364f1dcee59SSimon Glass 	count = info->read(info, sector, sectors, fit);
365f1dcee59SSimon Glass 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
366f1dcee59SSimon Glass 	      sector, sectors, fit, count);
367f1dcee59SSimon Glass 	if (count == 0)
368f1dcee59SSimon Glass 		return -EIO;
369f1dcee59SSimon Glass 
370736806fbSAndre Przywara 	/* find the node holding the images information */
371f1dcee59SSimon Glass 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
372f1dcee59SSimon Glass 	if (images < 0) {
373f1dcee59SSimon Glass 		debug("%s: Cannot find /images node: %d\n", __func__, images);
374f1dcee59SSimon Glass 		return -1;
375f1dcee59SSimon Glass 	}
376736806fbSAndre Przywara 
3779719e14fSPhilipp Tomsich 	/*
3789719e14fSPhilipp Tomsich 	 * Find the U-Boot image using the following search order:
3799719e14fSPhilipp Tomsich 	 *   - start at 'firmware' (e.g. an ARM Trusted Firmware)
3809719e14fSPhilipp Tomsich 	 *   - fall back 'kernel' (e.g. a Falcon-mode OS boot
3819719e14fSPhilipp Tomsich 	 *   - fall back to using the first 'loadables' entry
3829719e14fSPhilipp Tomsich 	 */
383ebec805aSYork Sun 	if (node < 0)
384736806fbSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "firmware", 0);
3859719e14fSPhilipp Tomsich #ifdef CONFIG_SPL_OS_BOOT
3869719e14fSPhilipp Tomsich 	if (node < 0)
3879719e14fSPhilipp Tomsich 		node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
3889719e14fSPhilipp Tomsich #endif
389f1dcee59SSimon Glass 	if (node < 0) {
390736806fbSAndre Przywara 		debug("could not find firmware image, trying loadables...\n");
391736806fbSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
392411cf32dSAndre Przywara 		/*
393411cf32dSAndre Przywara 		 * If we pick the U-Boot image from "loadables", start at
394411cf32dSAndre Przywara 		 * the second image when later loading additional images.
395411cf32dSAndre Przywara 		 */
396411cf32dSAndre Przywara 		index = 1;
397736806fbSAndre Przywara 	}
398736806fbSAndre Przywara 	if (node < 0) {
399736806fbSAndre Przywara 		debug("%s: Cannot find u-boot image node: %d\n",
400736806fbSAndre Przywara 		      __func__, node);
401f1dcee59SSimon Glass 		return -1;
402f1dcee59SSimon Glass 	}
403f1dcee59SSimon Glass 
4048baa3818SAndre Przywara 	/* Load the image and set up the spl_image structure */
4058baa3818SAndre Przywara 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
4068baa3818SAndre Przywara 				 spl_image);
4078baa3818SAndre Przywara 	if (ret)
4088baa3818SAndre Przywara 		return ret;
4098baa3818SAndre Przywara 
4109719e14fSPhilipp Tomsich 	/*
4119719e14fSPhilipp Tomsich 	 * For backward compatibility, we treat the first node that is
4129719e14fSPhilipp Tomsich 	 * as a U-Boot image, if no OS-type has been declared.
4139719e14fSPhilipp Tomsich 	 */
4148118cce3SPhilipp Tomsich 	if (!spl_fit_image_get_os(fit, node, &spl_image->os))
415ebec805aSYork Sun 		debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
4169719e14fSPhilipp Tomsich #if !defined(CONFIG_SPL_OS_BOOT)
4179719e14fSPhilipp Tomsich 	else
418f4d7d859SSimon Glass 		spl_image->os = IH_OS_U_BOOT;
419ebec805aSYork Sun #endif
420f1dcee59SSimon Glass 
421f1dcee59SSimon Glass 	/*
4229719e14fSPhilipp Tomsich 	 * Booting a next-stage U-Boot may require us to append the FDT.
4239719e14fSPhilipp Tomsich 	 * We allow this to fail, as the U-Boot image might embed its FDT.
424f1dcee59SSimon Glass 	 */
4259719e14fSPhilipp Tomsich 	if (spl_image->os == IH_OS_U_BOOT)
4269719e14fSPhilipp Tomsich 		spl_fit_append_fdt(spl_image, info, sector, fit,
4279719e14fSPhilipp Tomsich 				   images, base_offset);
428411cf32dSAndre Przywara 
429411cf32dSAndre Przywara 	/* Now check if there are more images for us to load */
430411cf32dSAndre Przywara 	for (; ; index++) {
4319719e14fSPhilipp Tomsich 		uint8_t os_type = IH_OS_INVALID;
4329719e14fSPhilipp Tomsich 
433411cf32dSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", index);
434411cf32dSAndre Przywara 		if (node < 0)
435411cf32dSAndre Przywara 			break;
436411cf32dSAndre Przywara 
437411cf32dSAndre Przywara 		ret = spl_load_fit_image(info, sector, fit, base_offset, node,
438411cf32dSAndre Przywara 					 &image_info);
439411cf32dSAndre Przywara 		if (ret < 0)
440411cf32dSAndre Przywara 			continue;
441411cf32dSAndre Przywara 
4428118cce3SPhilipp Tomsich 		if (!spl_fit_image_get_os(fit, node, &os_type))
4439719e14fSPhilipp Tomsich 			debug("Loadable is %s\n", genimg_get_os_name(os_type));
4449719e14fSPhilipp Tomsich 
4451f1cf67bSPhilipp Tomsich 		if (os_type == IH_OS_U_BOOT) {
4461f1cf67bSPhilipp Tomsich 			spl_fit_append_fdt(&image_info, info, sector,
4479719e14fSPhilipp Tomsich 					   fit, images, base_offset);
4481f1cf67bSPhilipp Tomsich 			spl_image->fdt_addr = image_info.fdt_addr;
4491f1cf67bSPhilipp Tomsich 		}
4509719e14fSPhilipp Tomsich 
451411cf32dSAndre Przywara 		/*
452411cf32dSAndre Przywara 		 * If the "firmware" image did not provide an entry point,
453411cf32dSAndre Przywara 		 * use the first valid entry point from the loadables.
454411cf32dSAndre Przywara 		 */
455411cf32dSAndre Przywara 		if (spl_image->entry_point == FDT_ERROR &&
456411cf32dSAndre Przywara 		    image_info.entry_point != FDT_ERROR)
457411cf32dSAndre Przywara 			spl_image->entry_point = image_info.entry_point;
4581f1cf67bSPhilipp Tomsich 
4591f1cf67bSPhilipp Tomsich 		/* Record our loadables into the FDT */
4601f1cf67bSPhilipp Tomsich 		if (spl_image->fdt_addr)
4611f1cf67bSPhilipp Tomsich 			spl_fit_record_loadable(fit, images, index,
4621f1cf67bSPhilipp Tomsich 						spl_image->fdt_addr,
4631f1cf67bSPhilipp Tomsich 						&image_info);
464411cf32dSAndre Przywara 	}
465411cf32dSAndre Przywara 
466411cf32dSAndre Przywara 	/*
467411cf32dSAndre Przywara 	 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
468411cf32dSAndre Przywara 	 * Makefile will set it to 0 and it will end up as the entry point
469411cf32dSAndre Przywara 	 * here. What it actually means is: use the load address.
470411cf32dSAndre Przywara 	 */
471411cf32dSAndre Przywara 	if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
472411cf32dSAndre Przywara 		spl_image->entry_point = spl_image->load_addr;
473411cf32dSAndre Przywara 
474411cf32dSAndre Przywara 	return 0;
475f1dcee59SSimon Glass }
476