xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision 4b9340abdc23f25b75282cdc8b84a06c8b10cd27)
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 
25*4b9340abSAndre Przywara /*
26*4b9340abSAndre Przywara  * Iterate over all /configurations subnodes and call a platform specific
27*4b9340abSAndre Przywara  * function to find the matching configuration.
28*4b9340abSAndre Przywara  * Returns the node offset or a negative error number.
29*4b9340abSAndre Przywara  */
30*4b9340abSAndre Przywara static int spl_fit_find_config_node(const void *fdt)
31f1dcee59SSimon Glass {
32*4b9340abSAndre Przywara 	const char *name;
33*4b9340abSAndre 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);
56*4b9340abSAndre Przywara 
57*4b9340abSAndre Przywara 		return node;
58f1dcee59SSimon Glass 	}
59f1dcee59SSimon Glass 
60*4b9340abSAndre Przywara 	return -ENOENT;
61f1dcee59SSimon Glass }
62f1dcee59SSimon Glass 
63*4b9340abSAndre Przywara static int spl_fit_select_index(const void *fit, int images, int *offsetp,
64*4b9340abSAndre Przywara 				const char *type, int index)
65*4b9340abSAndre Przywara {
66*4b9340abSAndre Przywara 	const char *name, *str;
67*4b9340abSAndre Przywara 	int node, conf_node;
68*4b9340abSAndre Przywara 	int len, i;
69f1dcee59SSimon Glass 
70*4b9340abSAndre Przywara 	*offsetp = 0;
71*4b9340abSAndre Przywara 	conf_node = spl_fit_find_config_node(fit);
72*4b9340abSAndre Przywara 	if (conf_node < 0) {
73f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
74f1dcee59SSimon Glass 		printf("No matching DT out of these options:\n");
75*4b9340abSAndre Przywara 		for (node = fdt_first_subnode(fit, conf_node);
76f1dcee59SSimon Glass 		     node >= 0;
77*4b9340abSAndre Przywara 		     node = fdt_next_subnode(fit, node)) {
78*4b9340abSAndre Przywara 			name = fdt_getprop(fit, node, "description", &len);
79f1dcee59SSimon Glass 			printf("   %s\n", name);
80f1dcee59SSimon Glass 		}
81f1dcee59SSimon Glass #endif
82*4b9340abSAndre Przywara 		return conf_node;
83*4b9340abSAndre Przywara 	}
84f1dcee59SSimon Glass 
85*4b9340abSAndre Przywara 	name = fdt_getprop(fit, conf_node, type, &len);
86*4b9340abSAndre Przywara 	if (!name) {
87*4b9340abSAndre Przywara 		debug("cannot find property '%s': %d\n", type, len);
88*4b9340abSAndre Przywara 		return -EINVAL;
89*4b9340abSAndre Przywara 	}
90*4b9340abSAndre Przywara 
91*4b9340abSAndre Przywara 	str = name;
92*4b9340abSAndre Przywara 	for (i = 0; i < index; i++) {
93*4b9340abSAndre Przywara 		str = strchr(str, '\0') + 1;
94*4b9340abSAndre Przywara 		if (!str || (str - name >= len)) {
95*4b9340abSAndre Przywara 			debug("no string for index %d\n", index);
96*4b9340abSAndre Przywara 			return -E2BIG;
97*4b9340abSAndre Przywara 		}
98*4b9340abSAndre Przywara 	}
99*4b9340abSAndre Przywara 
100*4b9340abSAndre Przywara 	debug("%s: '%s'\n", type, str);
101*4b9340abSAndre Przywara 	node = fdt_subnode_offset(fit, images, str);
102*4b9340abSAndre Przywara 	if (node < 0) {
103*4b9340abSAndre Przywara 		debug("cannot find image node '%s': %d\n", str, node);
104*4b9340abSAndre Przywara 		return -EINVAL;
105*4b9340abSAndre Przywara 	}
106*4b9340abSAndre Przywara 
107*4b9340abSAndre Przywara 	*offsetp = fdt_getprop_u32(fit, node, "data-offset");
108*4b9340abSAndre Przywara 	len = fdt_getprop_u32(fit, node, "data-size");
109*4b9340abSAndre Przywara 
110*4b9340abSAndre Przywara 	return len;
111f1dcee59SSimon Glass }
112f1dcee59SSimon Glass 
113eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset)
114eafd5410SLokesh Vutla {
115eafd5410SLokesh Vutla 	/*
116eafd5410SLokesh Vutla 	 * If it is a FS read, get the first address before offset which is
117eafd5410SLokesh Vutla 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
118eafd5410SLokesh Vutla 	 * block number to which offset belongs.
119eafd5410SLokesh Vutla 	 */
120eafd5410SLokesh Vutla 	if (info->filename)
121eafd5410SLokesh Vutla 		return offset & ~(ARCH_DMA_MINALIGN - 1);
122eafd5410SLokesh Vutla 
123eafd5410SLokesh Vutla 	return offset / info->bl_len;
124eafd5410SLokesh Vutla }
125eafd5410SLokesh Vutla 
126eafd5410SLokesh Vutla static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
127eafd5410SLokesh Vutla {
128eafd5410SLokesh Vutla 	/*
129eafd5410SLokesh Vutla 	 * If it is a FS read, get the difference between the offset and
130eafd5410SLokesh Vutla 	 * the first address before offset which is aligned to
131eafd5410SLokesh Vutla 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
132eafd5410SLokesh Vutla 	 * block.
133eafd5410SLokesh Vutla 	 */
134eafd5410SLokesh Vutla 	if (info->filename)
135eafd5410SLokesh Vutla 		return offset & (ARCH_DMA_MINALIGN - 1);
136eafd5410SLokesh Vutla 
137eafd5410SLokesh Vutla 	return offset % info->bl_len;
138eafd5410SLokesh Vutla }
139eafd5410SLokesh Vutla 
140eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size,
141eafd5410SLokesh Vutla 				  int offset)
142eafd5410SLokesh Vutla {
1433cc1f380SLokesh Vutla 	data_size = data_size + get_aligned_image_overhead(info, offset);
1443cc1f380SLokesh Vutla 
145eafd5410SLokesh Vutla 	if (info->filename)
1463cc1f380SLokesh Vutla 		return data_size;
147eafd5410SLokesh Vutla 
148eafd5410SLokesh Vutla 	return (data_size + info->bl_len - 1) / info->bl_len;
149eafd5410SLokesh Vutla }
150eafd5410SLokesh Vutla 
151f4d7d859SSimon Glass int spl_load_simple_fit(struct spl_image_info *spl_image,
152f4d7d859SSimon Glass 			struct spl_load_info *info, ulong sector, void *fit)
153f1dcee59SSimon Glass {
154f1dcee59SSimon Glass 	int sectors;
155f1dcee59SSimon Glass 	ulong size, load;
156f1dcee59SSimon Glass 	unsigned long count;
157f1dcee59SSimon Glass 	int node, images;
158f1dcee59SSimon Glass 	void *load_ptr;
159f1dcee59SSimon Glass 	int fdt_offset, fdt_len;
160f1dcee59SSimon Glass 	int data_offset, data_size;
161eafd5410SLokesh Vutla 	int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
162f1dcee59SSimon Glass 	int src_sector;
163da74d1f3SDaniel Allred 	void *dst, *src;
164f1dcee59SSimon Glass 
165f1dcee59SSimon Glass 	/*
166f1dcee59SSimon Glass 	 * Figure out where the external images start. This is the base for the
167f1dcee59SSimon Glass 	 * data-offset properties in each image.
168f1dcee59SSimon Glass 	 */
169f1dcee59SSimon Glass 	size = fdt_totalsize(fit);
170f1dcee59SSimon Glass 	size = (size + 3) & ~3;
171f1dcee59SSimon Glass 	base_offset = (size + 3) & ~3;
172f1dcee59SSimon Glass 
173f1dcee59SSimon Glass 	/*
174f1dcee59SSimon Glass 	 * So far we only have one block of data from the FIT. Read the entire
175f1dcee59SSimon Glass 	 * thing, including that first block, placing it so it finishes before
176f1dcee59SSimon Glass 	 * where we will load the image.
177f1dcee59SSimon Glass 	 *
178f1dcee59SSimon Glass 	 * Note that we will load the image such that its first byte will be
179f1dcee59SSimon Glass 	 * at the load address. Since that byte may be part-way through a
180f1dcee59SSimon Glass 	 * block, we may load the image up to one block before the load
181f1dcee59SSimon Glass 	 * address. So take account of that here by subtracting an addition
182f1dcee59SSimon Glass 	 * block length from the FIT start position.
183f1dcee59SSimon Glass 	 *
184f1dcee59SSimon Glass 	 * In fact the FIT has its own load address, but we assume it cannot
185f1dcee59SSimon Glass 	 * be before CONFIG_SYS_TEXT_BASE.
186f1dcee59SSimon Glass 	 */
1878b528709SLokesh Vutla 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
1888b528709SLokesh Vutla 			align_len) & ~align_len);
189eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, size, 0);
190f1dcee59SSimon Glass 	count = info->read(info, sector, sectors, fit);
191f1dcee59SSimon Glass 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
192f1dcee59SSimon Glass 	      sector, sectors, fit, count);
193f1dcee59SSimon Glass 	if (count == 0)
194f1dcee59SSimon Glass 		return -EIO;
195f1dcee59SSimon Glass 
196f1dcee59SSimon Glass 	/* find the firmware image to load */
197f1dcee59SSimon Glass 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
198f1dcee59SSimon Glass 	if (images < 0) {
199f1dcee59SSimon Glass 		debug("%s: Cannot find /images node: %d\n", __func__, images);
200f1dcee59SSimon Glass 		return -1;
201f1dcee59SSimon Glass 	}
202f1dcee59SSimon Glass 	node = fdt_first_subnode(fit, images);
203f1dcee59SSimon Glass 	if (node < 0) {
204f1dcee59SSimon Glass 		debug("%s: Cannot find first image node: %d\n", __func__, node);
205f1dcee59SSimon Glass 		return -1;
206f1dcee59SSimon Glass 	}
207f1dcee59SSimon Glass 
208f1dcee59SSimon Glass 	/* Get its information and set up the spl_image structure */
209f1dcee59SSimon Glass 	data_offset = fdt_getprop_u32(fit, node, "data-offset");
210f1dcee59SSimon Glass 	data_size = fdt_getprop_u32(fit, node, "data-size");
211f1dcee59SSimon Glass 	load = fdt_getprop_u32(fit, node, "load");
212f1dcee59SSimon Glass 	debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
213f4d7d859SSimon Glass 	spl_image->load_addr = load;
214f4d7d859SSimon Glass 	spl_image->entry_point = load;
215f4d7d859SSimon Glass 	spl_image->os = IH_OS_U_BOOT;
216f1dcee59SSimon Glass 
217f1dcee59SSimon Glass 	/*
218f1dcee59SSimon Glass 	 * Work out where to place the image. We read it so that the first
219f1dcee59SSimon Glass 	 * byte will be at 'load'. This may mean we need to load it starting
220f1dcee59SSimon Glass 	 * before then, since we can only read whole blocks.
221f1dcee59SSimon Glass 	 */
222f1dcee59SSimon Glass 	data_offset += base_offset;
223eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, data_size, data_offset);
224f1dcee59SSimon Glass 	load_ptr = (void *)load;
225f1dcee59SSimon Glass 	debug("U-Boot size %x, data %p\n", data_size, load_ptr);
226eafd5410SLokesh Vutla 	dst = load_ptr;
227f1dcee59SSimon Glass 
228f1dcee59SSimon Glass 	/* Read the image */
229eafd5410SLokesh Vutla 	src_sector = sector + get_aligned_image_offset(info, data_offset);
230eafd5410SLokesh Vutla 	debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
231eafd5410SLokesh Vutla 	      dst, src_sector, sectors);
232f1dcee59SSimon Glass 	count = info->read(info, src_sector, sectors, dst);
233f1dcee59SSimon Glass 	if (count != sectors)
234f1dcee59SSimon Glass 		return -EIO;
235eafd5410SLokesh Vutla 	debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
236eafd5410SLokesh Vutla 	      data_size);
237da74d1f3SDaniel Allred 	src = dst + get_aligned_image_overhead(info, data_offset);
238da74d1f3SDaniel Allred 
239da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
240da74d1f3SDaniel Allred 	board_fit_image_post_process((void **)&src, (size_t *)&data_size);
241da74d1f3SDaniel Allred #endif
242da74d1f3SDaniel Allred 
243da74d1f3SDaniel Allred 	memcpy(dst, src, data_size);
244f1dcee59SSimon Glass 
245f1dcee59SSimon Glass 	/* Figure out which device tree the board wants to use */
246*4b9340abSAndre Przywara 	fdt_len = spl_fit_select_index(fit, images, &fdt_offset,
247*4b9340abSAndre Przywara 				       FIT_FDT_PROP, 0);
248f1dcee59SSimon Glass 	if (fdt_len < 0)
249f1dcee59SSimon Glass 		return fdt_len;
250f1dcee59SSimon Glass 
251f1dcee59SSimon Glass 	/*
252f1dcee59SSimon Glass 	 * Read the device tree and place it after the image. There may be
253f1dcee59SSimon Glass 	 * some extra data before it since we can only read entire blocks.
254eafd5410SLokesh Vutla 	 * And also align the destination address to ARCH_DMA_MINALIGN.
255f1dcee59SSimon Glass 	 */
256eafd5410SLokesh Vutla 	dst = (void *)((load + data_size + align_len) & ~align_len);
257f1dcee59SSimon Glass 	fdt_offset += base_offset;
258eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
259eafd5410SLokesh Vutla 	src_sector = sector + get_aligned_image_offset(info, fdt_offset);
260eafd5410SLokesh Vutla 	count = info->read(info, src_sector, sectors, dst);
261eafd5410SLokesh Vutla 	debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
262eafd5410SLokesh Vutla 	      dst, src_sector, sectors);
263f1dcee59SSimon Glass 	if (count != sectors)
264f1dcee59SSimon Glass 		return -EIO;
265f1dcee59SSimon Glass 
266f1dcee59SSimon Glass 	/*
267f1dcee59SSimon Glass 	 * Copy the device tree so that it starts immediately after the image.
268f1dcee59SSimon Glass 	 * After this we will have the U-Boot image and its device tree ready
269f1dcee59SSimon Glass 	 * for us to start.
270f1dcee59SSimon Glass 	 */
271eafd5410SLokesh Vutla 	debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
272eafd5410SLokesh Vutla 	      fdt_len);
273da74d1f3SDaniel Allred 	src = dst + get_aligned_image_overhead(info, fdt_offset);
274da74d1f3SDaniel Allred 	dst = load_ptr + data_size;
275da74d1f3SDaniel Allred 
276da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
277da74d1f3SDaniel Allred 	board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
278da74d1f3SDaniel Allred #endif
279da74d1f3SDaniel Allred 
280da74d1f3SDaniel Allred 	memcpy(dst, src, fdt_len);
281f1dcee59SSimon Glass 
282f1dcee59SSimon Glass 	return 0;
283f1dcee59SSimon Glass }
284