xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision 5c8c8faccf0e660040ada1a9376b74a6807d4f5f)
1 /*
2  * Copyright (C) 2016 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <errno.h>
10 #include <image.h>
11 #include <libfdt.h>
12 #include <spl.h>
13 
14 #define FDT_ERROR ((ulong)(-1))
15 
16 static ulong fdt_getprop_u32(const void *fdt, int node, const char *prop)
17 {
18 	const u32 *cell;
19 	int len;
20 
21 	cell = fdt_getprop(fdt, node, prop, &len);
22 	if (!cell || len != sizeof(*cell))
23 		return FDT_ERROR;
24 
25 	return fdt32_to_cpu(*cell);
26 }
27 
28 /*
29  * Iterate over all /configurations subnodes and call a platform specific
30  * function to find the matching configuration.
31  * Returns the node offset or a negative error number.
32  */
33 static int spl_fit_find_config_node(const void *fdt)
34 {
35 	const char *name;
36 	int conf, node, len;
37 
38 	conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
39 	if (conf < 0) {
40 		debug("%s: Cannot find /configurations node: %d\n", __func__,
41 		      conf);
42 		return -EINVAL;
43 	}
44 	for (node = fdt_first_subnode(fdt, conf);
45 	     node >= 0;
46 	     node = fdt_next_subnode(fdt, node)) {
47 		name = fdt_getprop(fdt, node, "description", &len);
48 		if (!name) {
49 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
50 			printf("%s: Missing FDT description in DTB\n",
51 			       __func__);
52 #endif
53 			return -EINVAL;
54 		}
55 		if (board_fit_config_name_match(name))
56 			continue;
57 
58 		debug("Selecting config '%s'", name);
59 
60 		return node;
61 	}
62 
63 	return -ENOENT;
64 }
65 
66 /**
67  * spl_fit_get_image_node(): By using the matching configuration subnode,
68  * retrieve the name of an image, specified by a property name and an index
69  * into that.
70  * @fit:	Pointer to the FDT blob.
71  * @images:	Offset of the /images subnode.
72  * @type:	Name of the property within the configuration subnode.
73  * @index:	Index into the list of strings in this property.
74  *
75  * Return:	the node offset of the respective image node or a negative
76  * 		error number.
77  */
78 static int spl_fit_get_image_node(const void *fit, int images,
79 				  const char *type, int index)
80 {
81 	const char *name, *str;
82 	int node, conf_node;
83 	int len, i;
84 
85 	conf_node = spl_fit_find_config_node(fit);
86 	if (conf_node < 0) {
87 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
88 		printf("No matching DT out of these options:\n");
89 		for (node = fdt_first_subnode(fit, conf_node);
90 		     node >= 0;
91 		     node = fdt_next_subnode(fit, node)) {
92 			name = fdt_getprop(fit, node, "description", &len);
93 			printf("   %s\n", name);
94 		}
95 #endif
96 		return conf_node;
97 	}
98 
99 	name = fdt_getprop(fit, conf_node, type, &len);
100 	if (!name) {
101 		debug("cannot find property '%s': %d\n", type, len);
102 		return -EINVAL;
103 	}
104 
105 	str = name;
106 	for (i = 0; i < index; i++) {
107 		str = strchr(str, '\0') + 1;
108 		if (!str || (str - name >= len)) {
109 			debug("no string for index %d\n", index);
110 			return -E2BIG;
111 		}
112 	}
113 
114 	debug("%s: '%s'\n", type, str);
115 	node = fdt_subnode_offset(fit, images, str);
116 	if (node < 0) {
117 		debug("cannot find image node '%s': %d\n", str, node);
118 		return -EINVAL;
119 	}
120 
121 	return node;
122 }
123 
124 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
125 {
126 	/*
127 	 * If it is a FS read, get the first address before offset which is
128 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
129 	 * block number to which offset belongs.
130 	 */
131 	if (info->filename)
132 		return offset & ~(ARCH_DMA_MINALIGN - 1);
133 
134 	return offset / info->bl_len;
135 }
136 
137 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
138 {
139 	/*
140 	 * If it is a FS read, get the difference between the offset and
141 	 * the first address before offset which is aligned to
142 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
143 	 * block.
144 	 */
145 	if (info->filename)
146 		return offset & (ARCH_DMA_MINALIGN - 1);
147 
148 	return offset % info->bl_len;
149 }
150 
151 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
152 				  int offset)
153 {
154 	data_size = data_size + get_aligned_image_overhead(info, offset);
155 
156 	if (info->filename)
157 		return data_size;
158 
159 	return (data_size + info->bl_len - 1) / info->bl_len;
160 }
161 
162 int spl_load_simple_fit(struct spl_image_info *spl_image,
163 			struct spl_load_info *info, ulong sector, void *fit)
164 {
165 	int sectors;
166 	ulong size, load;
167 	unsigned long count;
168 	int node, images;
169 	void *load_ptr;
170 	int fdt_offset, fdt_len;
171 	int data_offset, data_size;
172 	int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
173 	int src_sector;
174 	void *dst, *src;
175 
176 	/*
177 	 * Figure out where the external images start. This is the base for the
178 	 * data-offset properties in each image.
179 	 */
180 	size = fdt_totalsize(fit);
181 	size = (size + 3) & ~3;
182 	base_offset = (size + 3) & ~3;
183 
184 	/*
185 	 * So far we only have one block of data from the FIT. Read the entire
186 	 * thing, including that first block, placing it so it finishes before
187 	 * where we will load the image.
188 	 *
189 	 * Note that we will load the image such that its first byte will be
190 	 * at the load address. Since that byte may be part-way through a
191 	 * block, we may load the image up to one block before the load
192 	 * address. So take account of that here by subtracting an addition
193 	 * block length from the FIT start position.
194 	 *
195 	 * In fact the FIT has its own load address, but we assume it cannot
196 	 * be before CONFIG_SYS_TEXT_BASE.
197 	 */
198 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
199 			align_len) & ~align_len);
200 	sectors = get_aligned_image_size(info, size, 0);
201 	count = info->read(info, sector, sectors, fit);
202 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
203 	      sector, sectors, fit, count);
204 	if (count == 0)
205 		return -EIO;
206 
207 	/* find the node holding the images information */
208 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
209 	if (images < 0) {
210 		debug("%s: Cannot find /images node: %d\n", __func__, images);
211 		return -1;
212 	}
213 
214 	/* find the U-Boot image */
215 	node = spl_fit_get_image_node(fit, images, "firmware", 0);
216 	if (node < 0) {
217 		debug("could not find firmware image, trying loadables...\n");
218 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
219 	}
220 	if (node < 0) {
221 		debug("%s: Cannot find u-boot image node: %d\n",
222 		      __func__, node);
223 		return -1;
224 	}
225 
226 	/* Get its information and set up the spl_image structure */
227 	data_offset = fdt_getprop_u32(fit, node, "data-offset");
228 	if (data_offset == FDT_ERROR)
229 		return -ENOENT;
230 	data_size = fdt_getprop_u32(fit, node, "data-size");
231 	if (data_size == FDT_ERROR)
232 		return -ENOENT;
233 	load = fdt_getprop_u32(fit, node, "load");
234 	debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
235 	spl_image->load_addr = load;
236 	spl_image->entry_point = load;
237 	spl_image->os = IH_OS_U_BOOT;
238 
239 	/*
240 	 * Work out where to place the image. We read it so that the first
241 	 * byte will be at 'load'. This may mean we need to load it starting
242 	 * before then, since we can only read whole blocks.
243 	 */
244 	data_offset += base_offset;
245 	sectors = get_aligned_image_size(info, data_size, data_offset);
246 	load_ptr = (void *)load;
247 	debug("U-Boot size %x, data %p\n", data_size, load_ptr);
248 	dst = load_ptr;
249 
250 	/* Read the image */
251 	src_sector = sector + get_aligned_image_offset(info, data_offset);
252 	debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
253 	      dst, src_sector, sectors);
254 	count = info->read(info, src_sector, sectors, dst);
255 	if (count != sectors)
256 		return -EIO;
257 	debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
258 	      data_size);
259 	src = dst + get_aligned_image_overhead(info, data_offset);
260 
261 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
262 	board_fit_image_post_process((void **)&src, (size_t *)&data_size);
263 #endif
264 
265 	memcpy(dst, src, data_size);
266 
267 	/* Figure out which device tree the board wants to use */
268 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
269 	if (node < 0) {
270 		debug("%s: cannot find FDT node\n", __func__);
271 		return node;
272 	}
273 	fdt_offset = fdt_getprop_u32(fit, node, "data-offset");
274 	fdt_len = fdt_getprop_u32(fit, node, "data-size");
275 	if (fdt_offset == FDT_ERROR || fdt_len == FDT_ERROR) {
276 		debug("%s: cannot load FDT data\n" __func__);
277 		return -ENOENT;
278 	}
279 
280 	/*
281 	 * Read the device tree and place it after the image. There may be
282 	 * some extra data before it since we can only read entire blocks.
283 	 * And also align the destination address to ARCH_DMA_MINALIGN.
284 	 */
285 	dst = (void *)((load + data_size + align_len) & ~align_len);
286 	fdt_offset += base_offset;
287 	sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
288 	src_sector = sector + get_aligned_image_offset(info, fdt_offset);
289 	count = info->read(info, src_sector, sectors, dst);
290 	debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
291 	      dst, src_sector, sectors);
292 	if (count != sectors)
293 		return -EIO;
294 
295 	/*
296 	 * Copy the device tree so that it starts immediately after the image.
297 	 * After this we will have the U-Boot image and its device tree ready
298 	 * for us to start.
299 	 */
300 	debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
301 	      fdt_len);
302 	src = dst + get_aligned_image_overhead(info, fdt_offset);
303 	dst = load_ptr + data_size;
304 
305 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
306 	board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
307 #endif
308 
309 	memcpy(dst, src, fdt_len);
310 
311 	return 0;
312 }
313