xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision 8baa381882e8b5e8684b331f09b04e2e2784fb0d)
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>
11f1dcee59SSimon Glass #include <libfdt.h>
12f1dcee59SSimon Glass #include <spl.h>
13f1dcee59SSimon Glass 
145c8c8facSAndre Przywara #define FDT_ERROR ((ulong)(-1))
155c8c8facSAndre Przywara 
16f1dcee59SSimon Glass static ulong fdt_getprop_u32(const void *fdt, int node, const char *prop)
17f1dcee59SSimon Glass {
18f1dcee59SSimon Glass 	const u32 *cell;
19f1dcee59SSimon Glass 	int len;
20f1dcee59SSimon Glass 
21f1dcee59SSimon Glass 	cell = fdt_getprop(fdt, node, prop, &len);
225c8c8facSAndre Przywara 	if (!cell || len != sizeof(*cell))
235c8c8facSAndre Przywara 		return FDT_ERROR;
245c8c8facSAndre Przywara 
25f1dcee59SSimon Glass 	return fdt32_to_cpu(*cell);
26f1dcee59SSimon Glass }
27f1dcee59SSimon Glass 
284b9340abSAndre Przywara /*
294b9340abSAndre Przywara  * Iterate over all /configurations subnodes and call a platform specific
304b9340abSAndre Przywara  * function to find the matching configuration.
314b9340abSAndre Przywara  * Returns the node offset or a negative error number.
324b9340abSAndre Przywara  */
334b9340abSAndre Przywara static int spl_fit_find_config_node(const void *fdt)
34f1dcee59SSimon Glass {
354b9340abSAndre Przywara 	const char *name;
364b9340abSAndre Przywara 	int conf, node, len;
37f1dcee59SSimon Glass 
38f1dcee59SSimon Glass 	conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
39f1dcee59SSimon Glass 	if (conf < 0) {
40f1dcee59SSimon Glass 		debug("%s: Cannot find /configurations node: %d\n", __func__,
41f1dcee59SSimon Glass 		      conf);
42f1dcee59SSimon Glass 		return -EINVAL;
43f1dcee59SSimon Glass 	}
44f1dcee59SSimon Glass 	for (node = fdt_first_subnode(fdt, conf);
45f1dcee59SSimon Glass 	     node >= 0;
46f1dcee59SSimon Glass 	     node = fdt_next_subnode(fdt, node)) {
47f1dcee59SSimon Glass 		name = fdt_getprop(fdt, node, "description", &len);
485adfa265SMichal Simek 		if (!name) {
495adfa265SMichal Simek #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
505adfa265SMichal Simek 			printf("%s: Missing FDT description in DTB\n",
515adfa265SMichal Simek 			       __func__);
525adfa265SMichal Simek #endif
53f1dcee59SSimon Glass 			return -EINVAL;
545adfa265SMichal Simek 		}
55f1dcee59SSimon Glass 		if (board_fit_config_name_match(name))
56f1dcee59SSimon Glass 			continue;
57f1dcee59SSimon Glass 
58f1dcee59SSimon Glass 		debug("Selecting config '%s'", name);
594b9340abSAndre Przywara 
604b9340abSAndre Przywara 		return node;
61f1dcee59SSimon Glass 	}
62f1dcee59SSimon Glass 
634b9340abSAndre Przywara 	return -ENOENT;
64f1dcee59SSimon Glass }
65f1dcee59SSimon Glass 
66736806fbSAndre Przywara /**
67736806fbSAndre Przywara  * spl_fit_get_image_node(): By using the matching configuration subnode,
68736806fbSAndre Przywara  * retrieve the name of an image, specified by a property name and an index
69736806fbSAndre Przywara  * into that.
70736806fbSAndre Przywara  * @fit:	Pointer to the FDT blob.
71736806fbSAndre Przywara  * @images:	Offset of the /images subnode.
72736806fbSAndre Przywara  * @type:	Name of the property within the configuration subnode.
73736806fbSAndre Przywara  * @index:	Index into the list of strings in this property.
74736806fbSAndre Przywara  *
75736806fbSAndre Przywara  * Return:	the node offset of the respective image node or a negative
76736806fbSAndre Przywara  * 		error number.
77736806fbSAndre Przywara  */
78736806fbSAndre Przywara static int spl_fit_get_image_node(const void *fit, int images,
794b9340abSAndre Przywara 				  const char *type, int index)
804b9340abSAndre Przywara {
814b9340abSAndre Przywara 	const char *name, *str;
824b9340abSAndre Przywara 	int node, conf_node;
834b9340abSAndre Przywara 	int len, i;
84f1dcee59SSimon Glass 
854b9340abSAndre Przywara 	conf_node = spl_fit_find_config_node(fit);
864b9340abSAndre Przywara 	if (conf_node < 0) {
87f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
88f1dcee59SSimon Glass 		printf("No matching DT out of these options:\n");
894b9340abSAndre Przywara 		for (node = fdt_first_subnode(fit, conf_node);
90f1dcee59SSimon Glass 		     node >= 0;
914b9340abSAndre Przywara 		     node = fdt_next_subnode(fit, node)) {
924b9340abSAndre Przywara 			name = fdt_getprop(fit, node, "description", &len);
93f1dcee59SSimon Glass 			printf("   %s\n", name);
94f1dcee59SSimon Glass 		}
95f1dcee59SSimon Glass #endif
964b9340abSAndre Przywara 		return conf_node;
974b9340abSAndre Przywara 	}
98f1dcee59SSimon Glass 
994b9340abSAndre Przywara 	name = fdt_getprop(fit, conf_node, type, &len);
1004b9340abSAndre Przywara 	if (!name) {
1014b9340abSAndre Przywara 		debug("cannot find property '%s': %d\n", type, len);
1024b9340abSAndre Przywara 		return -EINVAL;
1034b9340abSAndre Przywara 	}
1044b9340abSAndre Przywara 
1054b9340abSAndre Przywara 	str = name;
1064b9340abSAndre Przywara 	for (i = 0; i < index; i++) {
1074b9340abSAndre Przywara 		str = strchr(str, '\0') + 1;
1084b9340abSAndre Przywara 		if (!str || (str - name >= len)) {
1094b9340abSAndre Przywara 			debug("no string for index %d\n", index);
1104b9340abSAndre Przywara 			return -E2BIG;
1114b9340abSAndre Przywara 		}
1124b9340abSAndre Przywara 	}
1134b9340abSAndre Przywara 
1144b9340abSAndre Przywara 	debug("%s: '%s'\n", type, str);
1154b9340abSAndre Przywara 	node = fdt_subnode_offset(fit, images, str);
1164b9340abSAndre Przywara 	if (node < 0) {
1174b9340abSAndre Przywara 		debug("cannot find image node '%s': %d\n", str, node);
1184b9340abSAndre Przywara 		return -EINVAL;
1194b9340abSAndre Przywara 	}
1204b9340abSAndre Przywara 
121736806fbSAndre Przywara 	return node;
122f1dcee59SSimon Glass }
123f1dcee59SSimon Glass 
124eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset)
125eafd5410SLokesh Vutla {
126eafd5410SLokesh Vutla 	/*
127eafd5410SLokesh Vutla 	 * If it is a FS read, get the first address before offset which is
128eafd5410SLokesh Vutla 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
129eafd5410SLokesh Vutla 	 * block number to which offset belongs.
130eafd5410SLokesh Vutla 	 */
131eafd5410SLokesh Vutla 	if (info->filename)
132eafd5410SLokesh Vutla 		return offset & ~(ARCH_DMA_MINALIGN - 1);
133eafd5410SLokesh Vutla 
134eafd5410SLokesh Vutla 	return offset / info->bl_len;
135eafd5410SLokesh Vutla }
136eafd5410SLokesh Vutla 
137eafd5410SLokesh Vutla static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
138eafd5410SLokesh Vutla {
139eafd5410SLokesh Vutla 	/*
140eafd5410SLokesh Vutla 	 * If it is a FS read, get the difference between the offset and
141eafd5410SLokesh Vutla 	 * the first address before offset which is aligned to
142eafd5410SLokesh Vutla 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
143eafd5410SLokesh Vutla 	 * block.
144eafd5410SLokesh Vutla 	 */
145eafd5410SLokesh Vutla 	if (info->filename)
146eafd5410SLokesh Vutla 		return offset & (ARCH_DMA_MINALIGN - 1);
147eafd5410SLokesh Vutla 
148eafd5410SLokesh Vutla 	return offset % info->bl_len;
149eafd5410SLokesh Vutla }
150eafd5410SLokesh Vutla 
151eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size,
152eafd5410SLokesh Vutla 				  int offset)
153eafd5410SLokesh Vutla {
1543cc1f380SLokesh Vutla 	data_size = data_size + get_aligned_image_overhead(info, offset);
1553cc1f380SLokesh Vutla 
156eafd5410SLokesh Vutla 	if (info->filename)
1573cc1f380SLokesh Vutla 		return data_size;
158eafd5410SLokesh Vutla 
159eafd5410SLokesh Vutla 	return (data_size + info->bl_len - 1) / info->bl_len;
160eafd5410SLokesh Vutla }
161eafd5410SLokesh Vutla 
162*8baa3818SAndre Przywara /**
163*8baa3818SAndre Przywara  * spl_load_fit_image(): load the image described in a certain FIT node
164*8baa3818SAndre Przywara  * @info:	points to information about the device to load data from
165*8baa3818SAndre Przywara  * @sector:	the start sector of the FIT image on the device
166*8baa3818SAndre Przywara  * @fit:	points to the flattened device tree blob describing the FIT
167*8baa3818SAndre Przywara  * 		image
168*8baa3818SAndre Przywara  * @base_offset: the beginning of the data area containing the actual
169*8baa3818SAndre Przywara  *		image data, relative to the beginning of the FIT
170*8baa3818SAndre Przywara  * @node:	offset of the DT node describing the image to load (relative
171*8baa3818SAndre Przywara  * 		to @fit)
172*8baa3818SAndre Przywara  * @image_info:	will be filled with information about the loaded image
173*8baa3818SAndre Przywara  * 		If the FIT node does not contain a "load" (address) property,
174*8baa3818SAndre Przywara  * 		the image gets loaded to the address pointed to by the
175*8baa3818SAndre Przywara  * 		load_addr member in this struct.
176*8baa3818SAndre Przywara  *
177*8baa3818SAndre Przywara  * Return:	0 on success or a negative error number.
178*8baa3818SAndre Przywara  */
179*8baa3818SAndre Przywara static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
180*8baa3818SAndre Przywara 			      void *fit, ulong base_offset, int node,
181*8baa3818SAndre Przywara 			      struct spl_image_info *image_info)
182*8baa3818SAndre Przywara {
183*8baa3818SAndre Przywara 	ulong offset;
184*8baa3818SAndre Przywara 	size_t length;
185*8baa3818SAndre Przywara 	ulong load_addr, load_ptr;
186*8baa3818SAndre Przywara 	void *src;
187*8baa3818SAndre Przywara 	ulong overhead;
188*8baa3818SAndre Przywara 	int nr_sectors;
189*8baa3818SAndre Przywara 	int align_len = ARCH_DMA_MINALIGN - 1;
190*8baa3818SAndre Przywara 
191*8baa3818SAndre Przywara 	offset = fdt_getprop_u32(fit, node, "data-offset");
192*8baa3818SAndre Przywara 	if (offset == FDT_ERROR)
193*8baa3818SAndre Przywara 		return -ENOENT;
194*8baa3818SAndre Przywara 	offset += base_offset;
195*8baa3818SAndre Przywara 	length = fdt_getprop_u32(fit, node, "data-size");
196*8baa3818SAndre Przywara 	if (length == FDT_ERROR)
197*8baa3818SAndre Przywara 		return -ENOENT;
198*8baa3818SAndre Przywara 	load_addr = fdt_getprop_u32(fit, node, "load");
199*8baa3818SAndre Przywara 	if (load_addr == FDT_ERROR && image_info)
200*8baa3818SAndre Przywara 		load_addr = image_info->load_addr;
201*8baa3818SAndre Przywara 	load_ptr = (load_addr + align_len) & ~align_len;
202*8baa3818SAndre Przywara 
203*8baa3818SAndre Przywara 	overhead = get_aligned_image_overhead(info, offset);
204*8baa3818SAndre Przywara 	nr_sectors = get_aligned_image_size(info, length, offset);
205*8baa3818SAndre Przywara 
206*8baa3818SAndre Przywara 	if (info->read(info, sector + get_aligned_image_offset(info, offset),
207*8baa3818SAndre Przywara 		       nr_sectors, (void*)load_ptr) != nr_sectors)
208*8baa3818SAndre Przywara 		return -EIO;
209*8baa3818SAndre Przywara 	debug("image: dst=%lx, offset=%lx, size=%lx\n", load_ptr, offset,
210*8baa3818SAndre Przywara 	      (unsigned long)length);
211*8baa3818SAndre Przywara 
212*8baa3818SAndre Przywara 	src = (void *)load_ptr + overhead;
213*8baa3818SAndre Przywara #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
214*8baa3818SAndre Przywara 	board_fit_image_post_process(&src, &length);
215*8baa3818SAndre Przywara #endif
216*8baa3818SAndre Przywara 
217*8baa3818SAndre Przywara 	memcpy((void*)load_addr, src, length);
218*8baa3818SAndre Przywara 
219*8baa3818SAndre Przywara 	if (image_info) {
220*8baa3818SAndre Przywara 		image_info->load_addr = load_addr;
221*8baa3818SAndre Przywara 		image_info->size = length;
222*8baa3818SAndre Przywara 		image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
223*8baa3818SAndre Przywara 	}
224*8baa3818SAndre Przywara 
225*8baa3818SAndre Przywara 	return 0;
226*8baa3818SAndre Przywara }
227*8baa3818SAndre Przywara 
228f4d7d859SSimon Glass int spl_load_simple_fit(struct spl_image_info *spl_image,
229f4d7d859SSimon Glass 			struct spl_load_info *info, ulong sector, void *fit)
230f1dcee59SSimon Glass {
231f1dcee59SSimon Glass 	int sectors;
232*8baa3818SAndre Przywara 	ulong size;
233f1dcee59SSimon Glass 	unsigned long count;
234*8baa3818SAndre Przywara 	struct spl_image_info image_info;
235*8baa3818SAndre Przywara 	int node, images, ret;
236eafd5410SLokesh Vutla 	int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
237f1dcee59SSimon Glass 
238f1dcee59SSimon Glass 	/*
239f1dcee59SSimon Glass 	 * Figure out where the external images start. This is the base for the
240f1dcee59SSimon Glass 	 * data-offset properties in each image.
241f1dcee59SSimon Glass 	 */
242f1dcee59SSimon Glass 	size = fdt_totalsize(fit);
243f1dcee59SSimon Glass 	size = (size + 3) & ~3;
244f1dcee59SSimon Glass 	base_offset = (size + 3) & ~3;
245f1dcee59SSimon Glass 
246f1dcee59SSimon Glass 	/*
247f1dcee59SSimon Glass 	 * So far we only have one block of data from the FIT. Read the entire
248f1dcee59SSimon Glass 	 * thing, including that first block, placing it so it finishes before
249f1dcee59SSimon Glass 	 * where we will load the image.
250f1dcee59SSimon Glass 	 *
251f1dcee59SSimon Glass 	 * Note that we will load the image such that its first byte will be
252f1dcee59SSimon Glass 	 * at the load address. Since that byte may be part-way through a
253f1dcee59SSimon Glass 	 * block, we may load the image up to one block before the load
254f1dcee59SSimon Glass 	 * address. So take account of that here by subtracting an addition
255f1dcee59SSimon Glass 	 * block length from the FIT start position.
256f1dcee59SSimon Glass 	 *
257f1dcee59SSimon Glass 	 * In fact the FIT has its own load address, but we assume it cannot
258f1dcee59SSimon Glass 	 * be before CONFIG_SYS_TEXT_BASE.
259f1dcee59SSimon Glass 	 */
2608b528709SLokesh Vutla 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
2618b528709SLokesh Vutla 			align_len) & ~align_len);
262eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, size, 0);
263f1dcee59SSimon Glass 	count = info->read(info, sector, sectors, fit);
264f1dcee59SSimon Glass 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
265f1dcee59SSimon Glass 	      sector, sectors, fit, count);
266f1dcee59SSimon Glass 	if (count == 0)
267f1dcee59SSimon Glass 		return -EIO;
268f1dcee59SSimon Glass 
269736806fbSAndre Przywara 	/* find the node holding the images information */
270f1dcee59SSimon Glass 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
271f1dcee59SSimon Glass 	if (images < 0) {
272f1dcee59SSimon Glass 		debug("%s: Cannot find /images node: %d\n", __func__, images);
273f1dcee59SSimon Glass 		return -1;
274f1dcee59SSimon Glass 	}
275736806fbSAndre Przywara 
276736806fbSAndre Przywara 	/* find the U-Boot image */
277736806fbSAndre Przywara 	node = spl_fit_get_image_node(fit, images, "firmware", 0);
278f1dcee59SSimon Glass 	if (node < 0) {
279736806fbSAndre Przywara 		debug("could not find firmware image, trying loadables...\n");
280736806fbSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
281736806fbSAndre Przywara 	}
282736806fbSAndre Przywara 	if (node < 0) {
283736806fbSAndre Przywara 		debug("%s: Cannot find u-boot image node: %d\n",
284736806fbSAndre Przywara 		      __func__, node);
285f1dcee59SSimon Glass 		return -1;
286f1dcee59SSimon Glass 	}
287f1dcee59SSimon Glass 
288*8baa3818SAndre Przywara 	/* Load the image and set up the spl_image structure */
289*8baa3818SAndre Przywara 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
290*8baa3818SAndre Przywara 				 spl_image);
291*8baa3818SAndre Przywara 	if (ret)
292*8baa3818SAndre Przywara 		return ret;
293*8baa3818SAndre Przywara 
294f4d7d859SSimon Glass 	spl_image->os = IH_OS_U_BOOT;
295f1dcee59SSimon Glass 
296f1dcee59SSimon Glass 	/* Figure out which device tree the board wants to use */
297736806fbSAndre Przywara 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
298736806fbSAndre Przywara 	if (node < 0) {
299736806fbSAndre Przywara 		debug("%s: cannot find FDT node\n", __func__);
300736806fbSAndre Przywara 		return node;
301736806fbSAndre Przywara 	}
302f1dcee59SSimon Glass 
303f1dcee59SSimon Glass 	/*
304*8baa3818SAndre Przywara 	 * Read the device tree and place it after the image.
305*8baa3818SAndre Przywara 	 * Align the destination address to ARCH_DMA_MINALIGN.
306f1dcee59SSimon Glass 	 */
307*8baa3818SAndre Przywara 	image_info.load_addr = spl_image->load_addr + spl_image->size;
308*8baa3818SAndre Przywara 	return spl_load_fit_image(info, sector, fit, base_offset, node,
309*8baa3818SAndre Przywara 				  &image_info);
310f1dcee59SSimon Glass }
311