xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision 5c8c8faccf0e660040ada1a9376b74a6807d4f5f)
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 
14*5c8c8facSAndre Przywara #define FDT_ERROR ((ulong)(-1))
15*5c8c8facSAndre 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);
22*5c8c8facSAndre Przywara 	if (!cell || len != sizeof(*cell))
23*5c8c8facSAndre Przywara 		return FDT_ERROR;
24*5c8c8facSAndre 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 
162f4d7d859SSimon Glass int spl_load_simple_fit(struct spl_image_info *spl_image,
163f4d7d859SSimon Glass 			struct spl_load_info *info, ulong sector, void *fit)
164f1dcee59SSimon Glass {
165f1dcee59SSimon Glass 	int sectors;
166f1dcee59SSimon Glass 	ulong size, load;
167f1dcee59SSimon Glass 	unsigned long count;
168f1dcee59SSimon Glass 	int node, images;
169f1dcee59SSimon Glass 	void *load_ptr;
170f1dcee59SSimon Glass 	int fdt_offset, fdt_len;
171f1dcee59SSimon Glass 	int data_offset, data_size;
172eafd5410SLokesh Vutla 	int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
173f1dcee59SSimon Glass 	int src_sector;
174da74d1f3SDaniel Allred 	void *dst, *src;
175f1dcee59SSimon Glass 
176f1dcee59SSimon Glass 	/*
177f1dcee59SSimon Glass 	 * Figure out where the external images start. This is the base for the
178f1dcee59SSimon Glass 	 * data-offset properties in each image.
179f1dcee59SSimon Glass 	 */
180f1dcee59SSimon Glass 	size = fdt_totalsize(fit);
181f1dcee59SSimon Glass 	size = (size + 3) & ~3;
182f1dcee59SSimon Glass 	base_offset = (size + 3) & ~3;
183f1dcee59SSimon Glass 
184f1dcee59SSimon Glass 	/*
185f1dcee59SSimon Glass 	 * So far we only have one block of data from the FIT. Read the entire
186f1dcee59SSimon Glass 	 * thing, including that first block, placing it so it finishes before
187f1dcee59SSimon Glass 	 * where we will load the image.
188f1dcee59SSimon Glass 	 *
189f1dcee59SSimon Glass 	 * Note that we will load the image such that its first byte will be
190f1dcee59SSimon Glass 	 * at the load address. Since that byte may be part-way through a
191f1dcee59SSimon Glass 	 * block, we may load the image up to one block before the load
192f1dcee59SSimon Glass 	 * address. So take account of that here by subtracting an addition
193f1dcee59SSimon Glass 	 * block length from the FIT start position.
194f1dcee59SSimon Glass 	 *
195f1dcee59SSimon Glass 	 * In fact the FIT has its own load address, but we assume it cannot
196f1dcee59SSimon Glass 	 * be before CONFIG_SYS_TEXT_BASE.
197f1dcee59SSimon Glass 	 */
1988b528709SLokesh Vutla 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
1998b528709SLokesh Vutla 			align_len) & ~align_len);
200eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, size, 0);
201f1dcee59SSimon Glass 	count = info->read(info, sector, sectors, fit);
202f1dcee59SSimon Glass 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
203f1dcee59SSimon Glass 	      sector, sectors, fit, count);
204f1dcee59SSimon Glass 	if (count == 0)
205f1dcee59SSimon Glass 		return -EIO;
206f1dcee59SSimon Glass 
207736806fbSAndre Przywara 	/* find the node holding the images information */
208f1dcee59SSimon Glass 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
209f1dcee59SSimon Glass 	if (images < 0) {
210f1dcee59SSimon Glass 		debug("%s: Cannot find /images node: %d\n", __func__, images);
211f1dcee59SSimon Glass 		return -1;
212f1dcee59SSimon Glass 	}
213736806fbSAndre Przywara 
214736806fbSAndre Przywara 	/* find the U-Boot image */
215736806fbSAndre Przywara 	node = spl_fit_get_image_node(fit, images, "firmware", 0);
216f1dcee59SSimon Glass 	if (node < 0) {
217736806fbSAndre Przywara 		debug("could not find firmware image, trying loadables...\n");
218736806fbSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
219736806fbSAndre Przywara 	}
220736806fbSAndre Przywara 	if (node < 0) {
221736806fbSAndre Przywara 		debug("%s: Cannot find u-boot image node: %d\n",
222736806fbSAndre Przywara 		      __func__, node);
223f1dcee59SSimon Glass 		return -1;
224f1dcee59SSimon Glass 	}
225f1dcee59SSimon Glass 
226f1dcee59SSimon Glass 	/* Get its information and set up the spl_image structure */
227f1dcee59SSimon Glass 	data_offset = fdt_getprop_u32(fit, node, "data-offset");
228*5c8c8facSAndre Przywara 	if (data_offset == FDT_ERROR)
229*5c8c8facSAndre Przywara 		return -ENOENT;
230f1dcee59SSimon Glass 	data_size = fdt_getprop_u32(fit, node, "data-size");
231*5c8c8facSAndre Przywara 	if (data_size == FDT_ERROR)
232*5c8c8facSAndre Przywara 		return -ENOENT;
233f1dcee59SSimon Glass 	load = fdt_getprop_u32(fit, node, "load");
234f1dcee59SSimon Glass 	debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
235f4d7d859SSimon Glass 	spl_image->load_addr = load;
236f4d7d859SSimon Glass 	spl_image->entry_point = load;
237f4d7d859SSimon Glass 	spl_image->os = IH_OS_U_BOOT;
238f1dcee59SSimon Glass 
239f1dcee59SSimon Glass 	/*
240f1dcee59SSimon Glass 	 * Work out where to place the image. We read it so that the first
241f1dcee59SSimon Glass 	 * byte will be at 'load'. This may mean we need to load it starting
242f1dcee59SSimon Glass 	 * before then, since we can only read whole blocks.
243f1dcee59SSimon Glass 	 */
244f1dcee59SSimon Glass 	data_offset += base_offset;
245eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, data_size, data_offset);
246f1dcee59SSimon Glass 	load_ptr = (void *)load;
247f1dcee59SSimon Glass 	debug("U-Boot size %x, data %p\n", data_size, load_ptr);
248eafd5410SLokesh Vutla 	dst = load_ptr;
249f1dcee59SSimon Glass 
250f1dcee59SSimon Glass 	/* Read the image */
251eafd5410SLokesh Vutla 	src_sector = sector + get_aligned_image_offset(info, data_offset);
252eafd5410SLokesh Vutla 	debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
253eafd5410SLokesh Vutla 	      dst, src_sector, sectors);
254f1dcee59SSimon Glass 	count = info->read(info, src_sector, sectors, dst);
255f1dcee59SSimon Glass 	if (count != sectors)
256f1dcee59SSimon Glass 		return -EIO;
257eafd5410SLokesh Vutla 	debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
258eafd5410SLokesh Vutla 	      data_size);
259da74d1f3SDaniel Allred 	src = dst + get_aligned_image_overhead(info, data_offset);
260da74d1f3SDaniel Allred 
261da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
262da74d1f3SDaniel Allred 	board_fit_image_post_process((void **)&src, (size_t *)&data_size);
263da74d1f3SDaniel Allred #endif
264da74d1f3SDaniel Allred 
265da74d1f3SDaniel Allred 	memcpy(dst, src, data_size);
266f1dcee59SSimon Glass 
267f1dcee59SSimon Glass 	/* Figure out which device tree the board wants to use */
268736806fbSAndre Przywara 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
269736806fbSAndre Przywara 	if (node < 0) {
270736806fbSAndre Przywara 		debug("%s: cannot find FDT node\n", __func__);
271736806fbSAndre Przywara 		return node;
272736806fbSAndre Przywara 	}
273736806fbSAndre Przywara 	fdt_offset = fdt_getprop_u32(fit, node, "data-offset");
274736806fbSAndre Przywara 	fdt_len = fdt_getprop_u32(fit, node, "data-size");
275*5c8c8facSAndre Przywara 	if (fdt_offset == FDT_ERROR || fdt_len == FDT_ERROR) {
276*5c8c8facSAndre Przywara 		debug("%s: cannot load FDT data\n" __func__);
277*5c8c8facSAndre Przywara 		return -ENOENT;
278*5c8c8facSAndre Przywara 	}
279f1dcee59SSimon Glass 
280f1dcee59SSimon Glass 	/*
281f1dcee59SSimon Glass 	 * Read the device tree and place it after the image. There may be
282f1dcee59SSimon Glass 	 * some extra data before it since we can only read entire blocks.
283eafd5410SLokesh Vutla 	 * And also align the destination address to ARCH_DMA_MINALIGN.
284f1dcee59SSimon Glass 	 */
285eafd5410SLokesh Vutla 	dst = (void *)((load + data_size + align_len) & ~align_len);
286f1dcee59SSimon Glass 	fdt_offset += base_offset;
287eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
288eafd5410SLokesh Vutla 	src_sector = sector + get_aligned_image_offset(info, fdt_offset);
289eafd5410SLokesh Vutla 	count = info->read(info, src_sector, sectors, dst);
290eafd5410SLokesh Vutla 	debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
291eafd5410SLokesh Vutla 	      dst, src_sector, sectors);
292f1dcee59SSimon Glass 	if (count != sectors)
293f1dcee59SSimon Glass 		return -EIO;
294f1dcee59SSimon Glass 
295f1dcee59SSimon Glass 	/*
296f1dcee59SSimon Glass 	 * Copy the device tree so that it starts immediately after the image.
297f1dcee59SSimon Glass 	 * After this we will have the U-Boot image and its device tree ready
298f1dcee59SSimon Glass 	 * for us to start.
299f1dcee59SSimon Glass 	 */
300eafd5410SLokesh Vutla 	debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
301eafd5410SLokesh Vutla 	      fdt_len);
302da74d1f3SDaniel Allred 	src = dst + get_aligned_image_overhead(info, fdt_offset);
303da74d1f3SDaniel Allred 	dst = load_ptr + data_size;
304da74d1f3SDaniel Allred 
305da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
306da74d1f3SDaniel Allred 	board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
307da74d1f3SDaniel Allred #endif
308da74d1f3SDaniel Allred 
309da74d1f3SDaniel Allred 	memcpy(dst, src, fdt_len);
310f1dcee59SSimon Glass 
311f1dcee59SSimon Glass 	return 0;
312f1dcee59SSimon Glass }
313