xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision f4d7d8596f3a4f5bce500c622b75d2e9c8d6f989)
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 
25f1dcee59SSimon Glass static int spl_fit_select_fdt(const void *fdt, int images, int *fdt_offsetp)
26f1dcee59SSimon Glass {
27f1dcee59SSimon Glass 	const char *name, *fdt_name;
28f1dcee59SSimon Glass 	int conf, node, fdt_node;
29f1dcee59SSimon Glass 	int len;
30f1dcee59SSimon Glass 
31f1dcee59SSimon Glass 	*fdt_offsetp = 0;
32f1dcee59SSimon Glass 	conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
33f1dcee59SSimon Glass 	if (conf < 0) {
34f1dcee59SSimon Glass 		debug("%s: Cannot find /configurations node: %d\n", __func__,
35f1dcee59SSimon Glass 		      conf);
36f1dcee59SSimon Glass 		return -EINVAL;
37f1dcee59SSimon Glass 	}
38f1dcee59SSimon Glass 	for (node = fdt_first_subnode(fdt, conf);
39f1dcee59SSimon Glass 	     node >= 0;
40f1dcee59SSimon Glass 	     node = fdt_next_subnode(fdt, node)) {
41f1dcee59SSimon Glass 		name = fdt_getprop(fdt, node, "description", &len);
425adfa265SMichal Simek 		if (!name) {
435adfa265SMichal Simek #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
445adfa265SMichal Simek 			printf("%s: Missing FDT description in DTB\n",
455adfa265SMichal Simek 			       __func__);
465adfa265SMichal Simek #endif
47f1dcee59SSimon Glass 			return -EINVAL;
485adfa265SMichal Simek 		}
49f1dcee59SSimon Glass 		if (board_fit_config_name_match(name))
50f1dcee59SSimon Glass 			continue;
51f1dcee59SSimon Glass 
52f1dcee59SSimon Glass 		debug("Selecting config '%s'", name);
53f1dcee59SSimon Glass 		fdt_name = fdt_getprop(fdt, node, FIT_FDT_PROP, &len);
54f1dcee59SSimon Glass 		if (!fdt_name) {
55f1dcee59SSimon Glass 			debug("%s: Cannot find fdt name property: %d\n",
56f1dcee59SSimon Glass 			      __func__, len);
57f1dcee59SSimon Glass 			return -EINVAL;
58f1dcee59SSimon Glass 		}
59f1dcee59SSimon Glass 
60f1dcee59SSimon Glass 		debug(", fdt '%s'\n", fdt_name);
61f1dcee59SSimon Glass 		fdt_node = fdt_subnode_offset(fdt, images, fdt_name);
62f1dcee59SSimon Glass 		if (fdt_node < 0) {
63f1dcee59SSimon Glass 			debug("%s: Cannot find fdt node '%s': %d\n",
64f1dcee59SSimon Glass 			      __func__, fdt_name, fdt_node);
65f1dcee59SSimon Glass 			return -EINVAL;
66f1dcee59SSimon Glass 		}
67f1dcee59SSimon Glass 
68f1dcee59SSimon Glass 		*fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset");
69f1dcee59SSimon Glass 		len = fdt_getprop_u32(fdt, fdt_node, "data-size");
70cfe32a4bSLokesh Vutla 		debug("FIT: Selected '%s'\n", name);
71f1dcee59SSimon Glass 
72f1dcee59SSimon Glass 		return len;
73f1dcee59SSimon Glass 	}
74f1dcee59SSimon Glass 
75f1dcee59SSimon Glass #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
76f1dcee59SSimon Glass 	printf("No matching DT out of these options:\n");
77f1dcee59SSimon Glass 	for (node = fdt_first_subnode(fdt, conf);
78f1dcee59SSimon Glass 	     node >= 0;
79f1dcee59SSimon Glass 	     node = fdt_next_subnode(fdt, node)) {
808aa57a95SAndreas Dannenberg 		name = fdt_getprop(fdt, node, "description", &len);
81f1dcee59SSimon Glass 		printf("   %s\n", name);
82f1dcee59SSimon Glass 	}
83f1dcee59SSimon Glass #endif
84f1dcee59SSimon Glass 
85f1dcee59SSimon Glass 	return -ENOENT;
86f1dcee59SSimon Glass }
87f1dcee59SSimon Glass 
88eafd5410SLokesh Vutla static int get_aligned_image_offset(struct spl_load_info *info, int offset)
89eafd5410SLokesh Vutla {
90eafd5410SLokesh Vutla 	/*
91eafd5410SLokesh Vutla 	 * If it is a FS read, get the first address before offset which is
92eafd5410SLokesh Vutla 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
93eafd5410SLokesh Vutla 	 * block number to which offset belongs.
94eafd5410SLokesh Vutla 	 */
95eafd5410SLokesh Vutla 	if (info->filename)
96eafd5410SLokesh Vutla 		return offset & ~(ARCH_DMA_MINALIGN - 1);
97eafd5410SLokesh Vutla 
98eafd5410SLokesh Vutla 	return offset / info->bl_len;
99eafd5410SLokesh Vutla }
100eafd5410SLokesh Vutla 
101eafd5410SLokesh Vutla static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
102eafd5410SLokesh Vutla {
103eafd5410SLokesh Vutla 	/*
104eafd5410SLokesh Vutla 	 * If it is a FS read, get the difference between the offset and
105eafd5410SLokesh Vutla 	 * the first address before offset which is aligned to
106eafd5410SLokesh Vutla 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
107eafd5410SLokesh Vutla 	 * block.
108eafd5410SLokesh Vutla 	 */
109eafd5410SLokesh Vutla 	if (info->filename)
110eafd5410SLokesh Vutla 		return offset & (ARCH_DMA_MINALIGN - 1);
111eafd5410SLokesh Vutla 
112eafd5410SLokesh Vutla 	return offset % info->bl_len;
113eafd5410SLokesh Vutla }
114eafd5410SLokesh Vutla 
115eafd5410SLokesh Vutla static int get_aligned_image_size(struct spl_load_info *info, int data_size,
116eafd5410SLokesh Vutla 				  int offset)
117eafd5410SLokesh Vutla {
1183cc1f380SLokesh Vutla 	data_size = data_size + get_aligned_image_overhead(info, offset);
1193cc1f380SLokesh Vutla 
120eafd5410SLokesh Vutla 	if (info->filename)
1213cc1f380SLokesh Vutla 		return data_size;
122eafd5410SLokesh Vutla 
123eafd5410SLokesh Vutla 	return (data_size + info->bl_len - 1) / info->bl_len;
124eafd5410SLokesh Vutla }
125eafd5410SLokesh Vutla 
126*f4d7d859SSimon Glass int spl_load_simple_fit(struct spl_image_info *spl_image,
127*f4d7d859SSimon Glass 			struct spl_load_info *info, ulong sector, void *fit)
128f1dcee59SSimon Glass {
129f1dcee59SSimon Glass 	int sectors;
130f1dcee59SSimon Glass 	ulong size, load;
131f1dcee59SSimon Glass 	unsigned long count;
132f1dcee59SSimon Glass 	int node, images;
133f1dcee59SSimon Glass 	void *load_ptr;
134f1dcee59SSimon Glass 	int fdt_offset, fdt_len;
135f1dcee59SSimon Glass 	int data_offset, data_size;
136eafd5410SLokesh Vutla 	int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
137f1dcee59SSimon Glass 	int src_sector;
138da74d1f3SDaniel Allred 	void *dst, *src;
139f1dcee59SSimon Glass 
140f1dcee59SSimon Glass 	/*
141f1dcee59SSimon Glass 	 * Figure out where the external images start. This is the base for the
142f1dcee59SSimon Glass 	 * data-offset properties in each image.
143f1dcee59SSimon Glass 	 */
144f1dcee59SSimon Glass 	size = fdt_totalsize(fit);
145f1dcee59SSimon Glass 	size = (size + 3) & ~3;
146f1dcee59SSimon Glass 	base_offset = (size + 3) & ~3;
147f1dcee59SSimon Glass 
148f1dcee59SSimon Glass 	/*
149f1dcee59SSimon Glass 	 * So far we only have one block of data from the FIT. Read the entire
150f1dcee59SSimon Glass 	 * thing, including that first block, placing it so it finishes before
151f1dcee59SSimon Glass 	 * where we will load the image.
152f1dcee59SSimon Glass 	 *
153f1dcee59SSimon Glass 	 * Note that we will load the image such that its first byte will be
154f1dcee59SSimon Glass 	 * at the load address. Since that byte may be part-way through a
155f1dcee59SSimon Glass 	 * block, we may load the image up to one block before the load
156f1dcee59SSimon Glass 	 * address. So take account of that here by subtracting an addition
157f1dcee59SSimon Glass 	 * block length from the FIT start position.
158f1dcee59SSimon Glass 	 *
159f1dcee59SSimon Glass 	 * In fact the FIT has its own load address, but we assume it cannot
160f1dcee59SSimon Glass 	 * be before CONFIG_SYS_TEXT_BASE.
161f1dcee59SSimon Glass 	 */
1628b528709SLokesh Vutla 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
1638b528709SLokesh Vutla 			align_len) & ~align_len);
164eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, size, 0);
165f1dcee59SSimon Glass 	count = info->read(info, sector, sectors, fit);
166f1dcee59SSimon Glass 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
167f1dcee59SSimon Glass 	      sector, sectors, fit, count);
168f1dcee59SSimon Glass 	if (count == 0)
169f1dcee59SSimon Glass 		return -EIO;
170f1dcee59SSimon Glass 
171f1dcee59SSimon Glass 	/* find the firmware image to load */
172f1dcee59SSimon Glass 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
173f1dcee59SSimon Glass 	if (images < 0) {
174f1dcee59SSimon Glass 		debug("%s: Cannot find /images node: %d\n", __func__, images);
175f1dcee59SSimon Glass 		return -1;
176f1dcee59SSimon Glass 	}
177f1dcee59SSimon Glass 	node = fdt_first_subnode(fit, images);
178f1dcee59SSimon Glass 	if (node < 0) {
179f1dcee59SSimon Glass 		debug("%s: Cannot find first image node: %d\n", __func__, node);
180f1dcee59SSimon Glass 		return -1;
181f1dcee59SSimon Glass 	}
182f1dcee59SSimon Glass 
183f1dcee59SSimon Glass 	/* Get its information and set up the spl_image structure */
184f1dcee59SSimon Glass 	data_offset = fdt_getprop_u32(fit, node, "data-offset");
185f1dcee59SSimon Glass 	data_size = fdt_getprop_u32(fit, node, "data-size");
186f1dcee59SSimon Glass 	load = fdt_getprop_u32(fit, node, "load");
187f1dcee59SSimon Glass 	debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
188*f4d7d859SSimon Glass 	spl_image->load_addr = load;
189*f4d7d859SSimon Glass 	spl_image->entry_point = load;
190*f4d7d859SSimon Glass 	spl_image->os = IH_OS_U_BOOT;
191f1dcee59SSimon Glass 
192f1dcee59SSimon Glass 	/*
193f1dcee59SSimon Glass 	 * Work out where to place the image. We read it so that the first
194f1dcee59SSimon Glass 	 * byte will be at 'load'. This may mean we need to load it starting
195f1dcee59SSimon Glass 	 * before then, since we can only read whole blocks.
196f1dcee59SSimon Glass 	 */
197f1dcee59SSimon Glass 	data_offset += base_offset;
198eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, data_size, data_offset);
199f1dcee59SSimon Glass 	load_ptr = (void *)load;
200f1dcee59SSimon Glass 	debug("U-Boot size %x, data %p\n", data_size, load_ptr);
201eafd5410SLokesh Vutla 	dst = load_ptr;
202f1dcee59SSimon Glass 
203f1dcee59SSimon Glass 	/* Read the image */
204eafd5410SLokesh Vutla 	src_sector = sector + get_aligned_image_offset(info, data_offset);
205eafd5410SLokesh Vutla 	debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
206eafd5410SLokesh Vutla 	      dst, src_sector, sectors);
207f1dcee59SSimon Glass 	count = info->read(info, src_sector, sectors, dst);
208f1dcee59SSimon Glass 	if (count != sectors)
209f1dcee59SSimon Glass 		return -EIO;
210eafd5410SLokesh Vutla 	debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
211eafd5410SLokesh Vutla 	      data_size);
212da74d1f3SDaniel Allred 	src = dst + get_aligned_image_overhead(info, data_offset);
213da74d1f3SDaniel Allred 
214da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
215da74d1f3SDaniel Allred 	board_fit_image_post_process((void **)&src, (size_t *)&data_size);
216da74d1f3SDaniel Allred #endif
217da74d1f3SDaniel Allred 
218da74d1f3SDaniel Allred 	memcpy(dst, src, data_size);
219f1dcee59SSimon Glass 
220f1dcee59SSimon Glass 	/* Figure out which device tree the board wants to use */
221f1dcee59SSimon Glass 	fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset);
222f1dcee59SSimon Glass 	if (fdt_len < 0)
223f1dcee59SSimon Glass 		return fdt_len;
224f1dcee59SSimon Glass 
225f1dcee59SSimon Glass 	/*
226f1dcee59SSimon Glass 	 * Read the device tree and place it after the image. There may be
227f1dcee59SSimon Glass 	 * some extra data before it since we can only read entire blocks.
228eafd5410SLokesh Vutla 	 * And also align the destination address to ARCH_DMA_MINALIGN.
229f1dcee59SSimon Glass 	 */
230eafd5410SLokesh Vutla 	dst = (void *)((load + data_size + align_len) & ~align_len);
231f1dcee59SSimon Glass 	fdt_offset += base_offset;
232eafd5410SLokesh Vutla 	sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
233eafd5410SLokesh Vutla 	src_sector = sector + get_aligned_image_offset(info, fdt_offset);
234eafd5410SLokesh Vutla 	count = info->read(info, src_sector, sectors, dst);
235eafd5410SLokesh Vutla 	debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
236eafd5410SLokesh Vutla 	      dst, src_sector, sectors);
237f1dcee59SSimon Glass 	if (count != sectors)
238f1dcee59SSimon Glass 		return -EIO;
239f1dcee59SSimon Glass 
240f1dcee59SSimon Glass 	/*
241f1dcee59SSimon Glass 	 * Copy the device tree so that it starts immediately after the image.
242f1dcee59SSimon Glass 	 * After this we will have the U-Boot image and its device tree ready
243f1dcee59SSimon Glass 	 * for us to start.
244f1dcee59SSimon Glass 	 */
245eafd5410SLokesh Vutla 	debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
246eafd5410SLokesh Vutla 	      fdt_len);
247da74d1f3SDaniel Allred 	src = dst + get_aligned_image_overhead(info, fdt_offset);
248da74d1f3SDaniel Allred 	dst = load_ptr + data_size;
249da74d1f3SDaniel Allred 
250da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
251da74d1f3SDaniel Allred 	board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
252da74d1f3SDaniel Allred #endif
253da74d1f3SDaniel Allred 
254da74d1f3SDaniel Allred 	memcpy(dst, src, fdt_len);
255f1dcee59SSimon Glass 
256f1dcee59SSimon Glass 	return 0;
257f1dcee59SSimon Glass }
258