xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision 736806fbfa7a0ffc2bc18c1521e42ac578b1ad1e)
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 
14f1dcee59SSimon Glass static ulong fdt_getprop_u32(const void *fdt, int node, const char *prop)
15f1dcee59SSimon Glass {
16f1dcee59SSimon Glass 	const u32 *cell;
17f1dcee59SSimon Glass 	int len;
18f1dcee59SSimon Glass 
19f1dcee59SSimon Glass 	cell = fdt_getprop(fdt, node, prop, &len);
20f1dcee59SSimon Glass 	if (len != sizeof(*cell))
21f1dcee59SSimon Glass 		return -1U;
22f1dcee59SSimon Glass 	return fdt32_to_cpu(*cell);
23f1dcee59SSimon Glass }
24f1dcee59SSimon Glass 
254b9340abSAndre Przywara /*
264b9340abSAndre Przywara  * Iterate over all /configurations subnodes and call a platform specific
274b9340abSAndre Przywara  * function to find the matching configuration.
284b9340abSAndre Przywara  * Returns the node offset or a negative error number.
294b9340abSAndre Przywara  */
304b9340abSAndre Przywara static int spl_fit_find_config_node(const void *fdt)
31f1dcee59SSimon Glass {
324b9340abSAndre Przywara 	const char *name;
334b9340abSAndre Przywara 	int conf, node, len;
34f1dcee59SSimon Glass 
35f1dcee59SSimon Glass 	conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
36f1dcee59SSimon Glass 	if (conf < 0) {
37f1dcee59SSimon Glass 		debug("%s: Cannot find /configurations node: %d\n", __func__,
38f1dcee59SSimon Glass 		      conf);
39f1dcee59SSimon Glass 		return -EINVAL;
40f1dcee59SSimon Glass 	}
41f1dcee59SSimon Glass 	for (node = fdt_first_subnode(fdt, conf);
42f1dcee59SSimon Glass 	     node >= 0;
43f1dcee59SSimon Glass 	     node = fdt_next_subnode(fdt, node)) {
44f1dcee59SSimon Glass 		name = fdt_getprop(fdt, node, "description", &len);
455adfa265SMichal Simek 		if (!name) {
465adfa265SMichal Simek #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
475adfa265SMichal Simek 			printf("%s: Missing FDT description in DTB\n",
485adfa265SMichal Simek 			       __func__);
495adfa265SMichal Simek #endif
50f1dcee59SSimon Glass 			return -EINVAL;
515adfa265SMichal Simek 		}
52f1dcee59SSimon Glass 		if (board_fit_config_name_match(name))
53f1dcee59SSimon Glass 			continue;
54f1dcee59SSimon Glass 
55f1dcee59SSimon Glass 		debug("Selecting config '%s'", name);
564b9340abSAndre Przywara 
574b9340abSAndre Przywara 		return node;
58f1dcee59SSimon Glass 	}
59f1dcee59SSimon Glass 
604b9340abSAndre Przywara 	return -ENOENT;
61f1dcee59SSimon Glass }
62f1dcee59SSimon Glass 
63*736806fbSAndre Przywara /**
64*736806fbSAndre Przywara  * spl_fit_get_image_node(): By using the matching configuration subnode,
65*736806fbSAndre Przywara  * retrieve the name of an image, specified by a property name and an index
66*736806fbSAndre Przywara  * into that.
67*736806fbSAndre Przywara  * @fit:	Pointer to the FDT blob.
68*736806fbSAndre Przywara  * @images:	Offset of the /images subnode.
69*736806fbSAndre Przywara  * @type:	Name of the property within the configuration subnode.
70*736806fbSAndre Przywara  * @index:	Index into the list of strings in this property.
71*736806fbSAndre Przywara  *
72*736806fbSAndre Przywara  * Return:	the node offset of the respective image node or a negative
73*736806fbSAndre Przywara  * 		error number.
74*736806fbSAndre Przywara  */
75*736806fbSAndre Przywara static int spl_fit_get_image_node(const void *fit, int images,
764b9340abSAndre Przywara 				  const char *type, int index)
774b9340abSAndre Przywara {
784b9340abSAndre Przywara 	const char *name, *str;
794b9340abSAndre Przywara 	int node, conf_node;
804b9340abSAndre Przywara 	int len, i;
81f1dcee59SSimon Glass 
824b9340abSAndre Przywara 	conf_node = spl_fit_find_config_node(fit);
834b9340abSAndre Przywara 	if (conf_node < 0) {
84f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
85f1dcee59SSimon Glass 		printf("No matching DT out of these options:\n");
864b9340abSAndre Przywara 		for (node = fdt_first_subnode(fit, conf_node);
87f1dcee59SSimon Glass 		     node >= 0;
884b9340abSAndre Przywara 		     node = fdt_next_subnode(fit, node)) {
894b9340abSAndre Przywara 			name = fdt_getprop(fit, node, "description", &len);
90f1dcee59SSimon Glass 			printf("   %s\n", name);
91f1dcee59SSimon Glass 		}
92f1dcee59SSimon Glass #endif
934b9340abSAndre Przywara 		return conf_node;
944b9340abSAndre Przywara 	}
95f1dcee59SSimon Glass 
964b9340abSAndre Przywara 	name = fdt_getprop(fit, conf_node, type, &len);
974b9340abSAndre Przywara 	if (!name) {
984b9340abSAndre Przywara 		debug("cannot find property '%s': %d\n", type, len);
994b9340abSAndre Przywara 		return -EINVAL;
1004b9340abSAndre Przywara 	}
1014b9340abSAndre Przywara 
1024b9340abSAndre Przywara 	str = name;
1034b9340abSAndre Przywara 	for (i = 0; i < index; i++) {
1044b9340abSAndre Przywara 		str = strchr(str, '\0') + 1;
1054b9340abSAndre Przywara 		if (!str || (str - name >= len)) {
1064b9340abSAndre Przywara 			debug("no string for index %d\n", index);
1074b9340abSAndre Przywara 			return -E2BIG;
1084b9340abSAndre Przywara 		}
1094b9340abSAndre Przywara 	}
1104b9340abSAndre Przywara 
1114b9340abSAndre Przywara 	debug("%s: '%s'\n", type, str);
1124b9340abSAndre Przywara 	node = fdt_subnode_offset(fit, images, str);
1134b9340abSAndre Przywara 	if (node < 0) {
1144b9340abSAndre Przywara 		debug("cannot find image node '%s': %d\n", str, node);
1154b9340abSAndre Przywara 		return -EINVAL;
1164b9340abSAndre Przywara 	}
1174b9340abSAndre Przywara 
118*736806fbSAndre Przywara 	return node;
119f1dcee59SSimon Glass }
120f1dcee59SSimon Glass 
121eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset)
122eafd5410SLokesh Vutla {
123eafd5410SLokesh Vutla 	/*
124eafd5410SLokesh Vutla 	 * If it is a FS read, get the first address before offset which is
125eafd5410SLokesh Vutla 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
126eafd5410SLokesh Vutla 	 * block number to which offset belongs.
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_overhead(struct spl_load_info *info, int offset)
135eafd5410SLokesh Vutla {
136eafd5410SLokesh Vutla 	/*
137eafd5410SLokesh Vutla 	 * If it is a FS read, get the difference between the offset and
138eafd5410SLokesh Vutla 	 * the first address before offset which is aligned to
139eafd5410SLokesh Vutla 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
140eafd5410SLokesh Vutla 	 * block.
141eafd5410SLokesh Vutla 	 */
142eafd5410SLokesh Vutla 	if (info->filename)
143eafd5410SLokesh Vutla 		return offset & (ARCH_DMA_MINALIGN - 1);
144eafd5410SLokesh Vutla 
145eafd5410SLokesh Vutla 	return offset % info->bl_len;
146eafd5410SLokesh Vutla }
147eafd5410SLokesh Vutla 
148eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size,
149eafd5410SLokesh Vutla 				  int offset)
150eafd5410SLokesh Vutla {
1513cc1f380SLokesh Vutla 	data_size = data_size + get_aligned_image_overhead(info, offset);
1523cc1f380SLokesh Vutla 
153eafd5410SLokesh Vutla 	if (info->filename)
1543cc1f380SLokesh Vutla 		return data_size;
155eafd5410SLokesh Vutla 
156eafd5410SLokesh Vutla 	return (data_size + info->bl_len - 1) / info->bl_len;
157eafd5410SLokesh Vutla }
158eafd5410SLokesh Vutla 
159f4d7d859SSimon Glass int spl_load_simple_fit(struct spl_image_info *spl_image,
160f4d7d859SSimon Glass 			struct spl_load_info *info, ulong sector, void *fit)
161f1dcee59SSimon Glass {
162f1dcee59SSimon Glass 	int sectors;
163f1dcee59SSimon Glass 	ulong size, load;
164f1dcee59SSimon Glass 	unsigned long count;
165f1dcee59SSimon Glass 	int node, images;
166f1dcee59SSimon Glass 	void *load_ptr;
167f1dcee59SSimon Glass 	int fdt_offset, fdt_len;
168f1dcee59SSimon Glass 	int data_offset, data_size;
169eafd5410SLokesh Vutla 	int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
170f1dcee59SSimon Glass 	int src_sector;
171da74d1f3SDaniel Allred 	void *dst, *src;
172f1dcee59SSimon Glass 
173f1dcee59SSimon Glass 	/*
174f1dcee59SSimon Glass 	 * Figure out where the external images start. This is the base for the
175f1dcee59SSimon Glass 	 * data-offset properties in each image.
176f1dcee59SSimon Glass 	 */
177f1dcee59SSimon Glass 	size = fdt_totalsize(fit);
178f1dcee59SSimon Glass 	size = (size + 3) & ~3;
179f1dcee59SSimon Glass 	base_offset = (size + 3) & ~3;
180f1dcee59SSimon Glass 
181f1dcee59SSimon Glass 	/*
182f1dcee59SSimon Glass 	 * So far we only have one block of data from the FIT. Read the entire
183f1dcee59SSimon Glass 	 * thing, including that first block, placing it so it finishes before
184f1dcee59SSimon Glass 	 * where we will load the image.
185f1dcee59SSimon Glass 	 *
186f1dcee59SSimon Glass 	 * Note that we will load the image such that its first byte will be
187f1dcee59SSimon Glass 	 * at the load address. Since that byte may be part-way through a
188f1dcee59SSimon Glass 	 * block, we may load the image up to one block before the load
189f1dcee59SSimon Glass 	 * address. So take account of that here by subtracting an addition
190f1dcee59SSimon Glass 	 * block length from the FIT start position.
191f1dcee59SSimon Glass 	 *
192f1dcee59SSimon Glass 	 * In fact the FIT has its own load address, but we assume it cannot
193f1dcee59SSimon Glass 	 * be before CONFIG_SYS_TEXT_BASE.
194f1dcee59SSimon Glass 	 */
1958b528709SLokesh Vutla 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
1968b528709SLokesh Vutla 			align_len) & ~align_len);
197eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, size, 0);
198f1dcee59SSimon Glass 	count = info->read(info, sector, sectors, fit);
199f1dcee59SSimon Glass 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
200f1dcee59SSimon Glass 	      sector, sectors, fit, count);
201f1dcee59SSimon Glass 	if (count == 0)
202f1dcee59SSimon Glass 		return -EIO;
203f1dcee59SSimon Glass 
204*736806fbSAndre Przywara 	/* find the node holding the images information */
205f1dcee59SSimon Glass 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
206f1dcee59SSimon Glass 	if (images < 0) {
207f1dcee59SSimon Glass 		debug("%s: Cannot find /images node: %d\n", __func__, images);
208f1dcee59SSimon Glass 		return -1;
209f1dcee59SSimon Glass 	}
210*736806fbSAndre Przywara 
211*736806fbSAndre Przywara 	/* find the U-Boot image */
212*736806fbSAndre Przywara 	node = spl_fit_get_image_node(fit, images, "firmware", 0);
213f1dcee59SSimon Glass 	if (node < 0) {
214*736806fbSAndre Przywara 		debug("could not find firmware image, trying loadables...\n");
215*736806fbSAndre Przywara 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
216*736806fbSAndre Przywara 	}
217*736806fbSAndre Przywara 	if (node < 0) {
218*736806fbSAndre Przywara 		debug("%s: Cannot find u-boot image node: %d\n",
219*736806fbSAndre Przywara 		      __func__, node);
220f1dcee59SSimon Glass 		return -1;
221f1dcee59SSimon Glass 	}
222f1dcee59SSimon Glass 
223f1dcee59SSimon Glass 	/* Get its information and set up the spl_image structure */
224f1dcee59SSimon Glass 	data_offset = fdt_getprop_u32(fit, node, "data-offset");
225f1dcee59SSimon Glass 	data_size = fdt_getprop_u32(fit, node, "data-size");
226f1dcee59SSimon Glass 	load = fdt_getprop_u32(fit, node, "load");
227f1dcee59SSimon Glass 	debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
228f4d7d859SSimon Glass 	spl_image->load_addr = load;
229f4d7d859SSimon Glass 	spl_image->entry_point = load;
230f4d7d859SSimon Glass 	spl_image->os = IH_OS_U_BOOT;
231f1dcee59SSimon Glass 
232f1dcee59SSimon Glass 	/*
233f1dcee59SSimon Glass 	 * Work out where to place the image. We read it so that the first
234f1dcee59SSimon Glass 	 * byte will be at 'load'. This may mean we need to load it starting
235f1dcee59SSimon Glass 	 * before then, since we can only read whole blocks.
236f1dcee59SSimon Glass 	 */
237f1dcee59SSimon Glass 	data_offset += base_offset;
238eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, data_size, data_offset);
239f1dcee59SSimon Glass 	load_ptr = (void *)load;
240f1dcee59SSimon Glass 	debug("U-Boot size %x, data %p\n", data_size, load_ptr);
241eafd5410SLokesh Vutla 	dst = load_ptr;
242f1dcee59SSimon Glass 
243f1dcee59SSimon Glass 	/* Read the image */
244eafd5410SLokesh Vutla 	src_sector = sector + get_aligned_image_offset(info, data_offset);
245eafd5410SLokesh Vutla 	debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
246eafd5410SLokesh Vutla 	      dst, src_sector, sectors);
247f1dcee59SSimon Glass 	count = info->read(info, src_sector, sectors, dst);
248f1dcee59SSimon Glass 	if (count != sectors)
249f1dcee59SSimon Glass 		return -EIO;
250eafd5410SLokesh Vutla 	debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
251eafd5410SLokesh Vutla 	      data_size);
252da74d1f3SDaniel Allred 	src = dst + get_aligned_image_overhead(info, data_offset);
253da74d1f3SDaniel Allred 
254da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
255da74d1f3SDaniel Allred 	board_fit_image_post_process((void **)&src, (size_t *)&data_size);
256da74d1f3SDaniel Allred #endif
257da74d1f3SDaniel Allred 
258da74d1f3SDaniel Allred 	memcpy(dst, src, data_size);
259f1dcee59SSimon Glass 
260f1dcee59SSimon Glass 	/* Figure out which device tree the board wants to use */
261*736806fbSAndre Przywara 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
262*736806fbSAndre Przywara 	if (node < 0) {
263*736806fbSAndre Przywara 		debug("%s: cannot find FDT node\n", __func__);
264*736806fbSAndre Przywara 		return node;
265*736806fbSAndre Przywara 	}
266*736806fbSAndre Przywara 	fdt_offset = fdt_getprop_u32(fit, node, "data-offset");
267*736806fbSAndre Przywara 	fdt_len = fdt_getprop_u32(fit, node, "data-size");
268f1dcee59SSimon Glass 
269f1dcee59SSimon Glass 	/*
270f1dcee59SSimon Glass 	 * Read the device tree and place it after the image. There may be
271f1dcee59SSimon Glass 	 * some extra data before it since we can only read entire blocks.
272eafd5410SLokesh Vutla 	 * And also align the destination address to ARCH_DMA_MINALIGN.
273f1dcee59SSimon Glass 	 */
274eafd5410SLokesh Vutla 	dst = (void *)((load + data_size + align_len) & ~align_len);
275f1dcee59SSimon Glass 	fdt_offset += base_offset;
276eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
277eafd5410SLokesh Vutla 	src_sector = sector + get_aligned_image_offset(info, fdt_offset);
278eafd5410SLokesh Vutla 	count = info->read(info, src_sector, sectors, dst);
279eafd5410SLokesh Vutla 	debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
280eafd5410SLokesh Vutla 	      dst, src_sector, sectors);
281f1dcee59SSimon Glass 	if (count != sectors)
282f1dcee59SSimon Glass 		return -EIO;
283f1dcee59SSimon Glass 
284f1dcee59SSimon Glass 	/*
285f1dcee59SSimon Glass 	 * Copy the device tree so that it starts immediately after the image.
286f1dcee59SSimon Glass 	 * After this we will have the U-Boot image and its device tree ready
287f1dcee59SSimon Glass 	 * for us to start.
288f1dcee59SSimon Glass 	 */
289eafd5410SLokesh Vutla 	debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
290eafd5410SLokesh Vutla 	      fdt_len);
291da74d1f3SDaniel Allred 	src = dst + get_aligned_image_overhead(info, fdt_offset);
292da74d1f3SDaniel Allred 	dst = load_ptr + data_size;
293da74d1f3SDaniel Allred 
294da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
295da74d1f3SDaniel Allred 	board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
296da74d1f3SDaniel Allred #endif
297da74d1f3SDaniel Allred 
298da74d1f3SDaniel Allred 	memcpy(dst, src, fdt_len);
299f1dcee59SSimon Glass 
300f1dcee59SSimon Glass 	return 0;
301f1dcee59SSimon Glass }
302