xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision 0833bf5e4ebc978944965ccd325c2ad0ccef45c6)
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 <linux/libfdt.h>
12 #include <spl.h>
13 #include <malloc.h>
14 
15 #ifndef CONFIG_SYS_BOOTM_LEN
16 #define CONFIG_SYS_BOOTM_LEN	(64 << 20)
17 #endif
18 
19 /**
20  * spl_fit_get_image_name(): By using the matching configuration subnode,
21  * retrieve the name of an image, specified by a property name and an index
22  * into that.
23  * @fit:	Pointer to the FDT blob.
24  * @images:	Offset of the /images subnode.
25  * @type:	Name of the property within the configuration subnode.
26  * @index:	Index into the list of strings in this property.
27  * @outname:	Name of the image
28  *
29  * Return:	0 on success, or a negative error number
30  */
31 static int spl_fit_get_image_name(const void *fit, int images,
32 				  const char *type, int index,
33 				  char **outname)
34 {
35 	const char *name, *str;
36 	__maybe_unused int node;
37 	int conf_node;
38 	int len, i;
39 
40 	conf_node = fit_find_config_node(fit);
41 	if (conf_node < 0) {
42 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
43 		printf("No matching DT out of these options:\n");
44 		for (node = fdt_first_subnode(fit, conf_node);
45 		     node >= 0;
46 		     node = fdt_next_subnode(fit, node)) {
47 			name = fdt_getprop(fit, node, "description", &len);
48 			printf("   %s\n", name);
49 		}
50 #endif
51 		return conf_node;
52 	}
53 
54 	name = fdt_getprop(fit, conf_node, type, &len);
55 	if (!name) {
56 		debug("cannot find property '%s': %d\n", type, len);
57 		return -EINVAL;
58 	}
59 
60 	str = name;
61 	for (i = 0; i < index; i++) {
62 		str = strchr(str, '\0') + 1;
63 		if (!str || (str - name >= len)) {
64 			debug("no string for index %d\n", index);
65 			return -E2BIG;
66 		}
67 	}
68 
69 	*outname = (char *)str;
70 	return 0;
71 }
72 
73 /**
74  * spl_fit_get_image_node(): By using the matching configuration subnode,
75  * retrieve the name of an image, specified by a property name and an index
76  * into that.
77  * @fit:	Pointer to the FDT blob.
78  * @images:	Offset of the /images subnode.
79  * @type:	Name of the property within the configuration subnode.
80  * @index:	Index into the list of strings in this property.
81  *
82  * Return:	the node offset of the respective image node or a negative
83  *		error number.
84  */
85 static int spl_fit_get_image_node(const void *fit, int images,
86 				  const char *type, int index)
87 {
88 	char *str;
89 	int err;
90 	int node;
91 
92 	err = spl_fit_get_image_name(fit, images, type, index, &str);
93 	if (err)
94 		return err;
95 
96 	debug("%s: '%s'\n", type, str);
97 
98 	node = fdt_subnode_offset(fit, images, str);
99 	if (node < 0) {
100 		debug("cannot find image node '%s': %d\n", str, node);
101 		return -EINVAL;
102 	}
103 
104 	return node;
105 }
106 
107 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
108 {
109 	/*
110 	 * If it is a FS read, get the first address before offset which is
111 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
112 	 * block number to which offset belongs.
113 	 */
114 	if (info->filename)
115 		return offset & ~(ARCH_DMA_MINALIGN - 1);
116 
117 	return offset / info->bl_len;
118 }
119 
120 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
121 {
122 	/*
123 	 * If it is a FS read, get the difference between the offset and
124 	 * the first address before offset which is aligned to
125 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
126 	 * block.
127 	 */
128 	if (info->filename)
129 		return offset & (ARCH_DMA_MINALIGN - 1);
130 
131 	return offset % info->bl_len;
132 }
133 
134 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
135 				  int offset)
136 {
137 	data_size = data_size + get_aligned_image_overhead(info, offset);
138 
139 	if (info->filename)
140 		return data_size;
141 
142 	return (data_size + info->bl_len - 1) / info->bl_len;
143 }
144 
145 /**
146  * spl_load_fit_image(): load the image described in a certain FIT node
147  * @info:	points to information about the device to load data from
148  * @sector:	the start sector of the FIT image on the device
149  * @fit:	points to the flattened device tree blob describing the FIT
150  *		image
151  * @base_offset: the beginning of the data area containing the actual
152  *		image data, relative to the beginning of the FIT
153  * @node:	offset of the DT node describing the image to load (relative
154  *		to @fit)
155  * @image_info:	will be filled with information about the loaded image
156  *		If the FIT node does not contain a "load" (address) property,
157  *		the image gets loaded to the address pointed to by the
158  *		load_addr member in this struct.
159  *
160  * Return:	0 on success or a negative error number.
161  */
162 static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
163 			      void *fit, ulong base_offset, int node,
164 			      struct spl_image_info *image_info)
165 {
166 	int offset;
167 	size_t length;
168 	int len;
169 	ulong size;
170 	ulong load_addr, load_ptr;
171 	void *src;
172 	ulong overhead;
173 	int nr_sectors;
174 	int align_len = ARCH_DMA_MINALIGN - 1;
175 	uint8_t image_comp = -1, type = -1;
176 	const void *data;
177 	bool external_data = false;
178 #ifdef CONFIG_SPL_FIT_SIGNATURE
179 	int ret;
180 #endif
181 
182 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
183 		if (fit_image_get_comp(fit, node, &image_comp))
184 			puts("Cannot get image compression format.\n");
185 		else
186 			debug("%s ", genimg_get_comp_name(image_comp));
187 
188 		if (fit_image_get_type(fit, node, &type))
189 			puts("Cannot get image type.\n");
190 		else
191 			debug("%s ", genimg_get_type_name(type));
192 	}
193 
194 	if (fit_image_get_load(fit, node, &load_addr))
195 		load_addr = image_info->load_addr;
196 
197 	if (!fit_image_get_data_position(fit, node, &offset)) {
198 		external_data = true;
199 	} else if (!fit_image_get_data_offset(fit, node, &offset)) {
200 		offset += base_offset;
201 		external_data = true;
202 	}
203 
204 	if (external_data) {
205 		/* External data */
206 		if (fit_image_get_data_size(fit, node, &len))
207 			return -ENOENT;
208 
209 		load_ptr = (load_addr + align_len) & ~align_len;
210 #if  defined(CONFIG_ARCH_ROCKCHIP)
211 		if ((load_ptr < CONFIG_SYS_SDRAM_BASE) ||
212 		     (load_ptr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE))
213 			load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len);
214 #endif
215 		length = len;
216 
217 		overhead = get_aligned_image_overhead(info, offset);
218 		nr_sectors = get_aligned_image_size(info, length, offset);
219 
220 		if (info->read(info,
221 			       sector + get_aligned_image_offset(info, offset),
222 			       nr_sectors, (void *)load_ptr) != nr_sectors)
223 			return -EIO;
224 
225 		debug("External data: dst=%lx, offset=%x, size=%lx\n",
226 		      load_ptr, offset, (unsigned long)length);
227 		src = (void *)load_ptr + overhead;
228 	} else {
229 		/* Embedded data */
230 		if (fit_image_get_data(fit, node, &data, &length)) {
231 			puts("Cannot get image data/size\n");
232 			return -ENOENT;
233 		}
234 		debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
235 		      (unsigned long)length);
236 		src = (void *)data;
237 	}
238 
239 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
240 	board_fit_image_post_process(&src, &length);
241 #endif
242 
243 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT)	&&
244 	    IS_ENABLED(CONFIG_SPL_GZIP)		&&
245 	    image_comp == IH_COMP_GZIP		&&
246 	    type == IH_TYPE_KERNEL) {
247 		size = length;
248 		if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
249 			   src, &size)) {
250 			puts("Uncompressing error\n");
251 			return -EIO;
252 		}
253 		length = size;
254 	} else {
255 		memcpy((void *)load_addr, src, length);
256 	}
257 
258 	if (image_info) {
259 		image_info->load_addr = load_addr;
260 		image_info->size = length;
261 		image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
262 	}
263 
264 #ifdef CONFIG_SPL_FIT_SIGNATURE
265 	printf("## Checking hash(es) for Image %s ...\n",
266 	       fit_get_name(fit, node, NULL));
267 	ret = fit_image_verify_with_data(fit, node,
268 					 (const void *)load_addr, length);
269 	printf("\n");
270 	return !ret;
271 #else
272 	return 0;
273 #endif
274 }
275 
276 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
277 			      struct spl_load_info *info, ulong sector,
278 			      void *fit, int images, ulong base_offset)
279 {
280 	struct spl_image_info image_info;
281 	int node, ret;
282 
283 	/* Figure out which device tree the board wants to use */
284 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
285 	if (node < 0) {
286 		debug("%s: cannot find FDT node\n", __func__);
287 		return node;
288 	}
289 
290 	/*
291 	 * Read the device tree and place it after the image.
292 	 * Align the destination address to ARCH_DMA_MINALIGN.
293 	 */
294 	image_info.load_addr = spl_image->load_addr + spl_image->size;
295 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
296 				 &image_info);
297 
298 	if (ret < 0)
299 		return ret;
300 
301 	/* Make the load-address of the FDT available for the SPL framework */
302 	spl_image->fdt_addr = (void *)image_info.load_addr;
303 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
304 	/* Try to make space, so we can inject details on the loadables */
305 	ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
306 #endif
307 
308 	return ret;
309 }
310 
311 static int spl_fit_record_loadable(const void *fit, int images, int index,
312 				   void *blob, struct spl_image_info *image)
313 {
314 	int ret = 0;
315 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
316 	char *name;
317 	int node;
318 
319 	ret = spl_fit_get_image_name(fit, images, "loadables",
320 				     index, &name);
321 	if (ret < 0)
322 		return ret;
323 
324 	node = spl_fit_get_image_node(fit, images, "loadables", index);
325 
326 	ret = fdt_record_loadable(blob, index, name, image->load_addr,
327 				  image->size, image->entry_point,
328 				  fdt_getprop(fit, node, "type", NULL),
329 				  fdt_getprop(fit, node, "os", NULL));
330 #endif
331 	return ret;
332 }
333 
334 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
335 {
336 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
337 	return -ENOTSUPP;
338 #else
339 	return fit_image_get_os(fit, noffset, os);
340 #endif
341 }
342 
343 int spl_load_simple_fit(struct spl_image_info *spl_image,
344 			struct spl_load_info *info, ulong sector, void *fit)
345 {
346 	int sectors;
347 	ulong size;
348 	unsigned long count;
349 	struct spl_image_info image_info;
350 	int node = -1;
351 	int images, ret;
352 	int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
353 	int index = 0;
354 
355 	/*
356 	 * For FIT with external data, figure out where the external images
357 	 * start. This is the base for the data-offset properties in each
358 	 * image.
359 	 */
360 	size = fdt_totalsize(fit);
361 	size = (size + 3) & ~3;
362 	base_offset = (size + 3) & ~3;
363 
364 	/*
365 	 * So far we only have one block of data from the FIT. Read the entire
366 	 * thing, including that first block, placing it so it finishes before
367 	 * where we will load the image.
368 	 *
369 	 * Note that we will load the image such that its first byte will be
370 	 * at the load address. Since that byte may be part-way through a
371 	 * block, we may load the image up to one block before the load
372 	 * address. So take account of that here by subtracting an addition
373 	 * block length from the FIT start position.
374 	 *
375 	 * In fact the FIT has its own load address, but we assume it cannot
376 	 * be before CONFIG_SYS_TEXT_BASE.
377 	 *
378 	 * For FIT with data embedded, data is loaded as part of FIT image.
379 	 * For FIT with external data, data is not loaded in this step.
380 	 */
381 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
382 			align_len) & ~align_len);
383 	sectors = get_aligned_image_size(info, size, 0);
384 	count = info->read(info, sector, sectors, fit);
385 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
386 	      sector, sectors, fit, count);
387 	if (count == 0)
388 		return -EIO;
389 
390 	/* find the node holding the images information */
391 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
392 	if (images < 0) {
393 		debug("%s: Cannot find /images node: %d\n", __func__, images);
394 		return -1;
395 	}
396 
397 	/*
398 	 * Find the U-Boot image using the following search order:
399 	 *   - start at 'firmware' (e.g. an ARM Trusted Firmware)
400 	 *   - fall back 'kernel' (e.g. a Falcon-mode OS boot
401 	 *   - fall back to using the first 'loadables' entry
402 	 */
403 	if (node < 0)
404 		node = spl_fit_get_image_node(fit, images, "firmware", 0);
405 #ifdef CONFIG_SPL_OS_BOOT
406 	if (node < 0)
407 		node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
408 #endif
409 	if (node < 0) {
410 		debug("could not find firmware image, trying loadables...\n");
411 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
412 		/*
413 		 * If we pick the U-Boot image from "loadables", start at
414 		 * the second image when later loading additional images.
415 		 */
416 		index = 1;
417 	}
418 	if (node < 0) {
419 		debug("%s: Cannot find u-boot image node: %d\n",
420 		      __func__, node);
421 		return -1;
422 	}
423 
424 	/* Load the image and set up the spl_image structure */
425 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
426 				 spl_image);
427 	if (ret)
428 		return ret;
429 
430 	/*
431 	 * For backward compatibility, we treat the first node that is
432 	 * as a U-Boot image, if no OS-type has been declared.
433 	 */
434 	if (!spl_fit_image_get_os(fit, node, &spl_image->os))
435 		debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
436 #if !defined(CONFIG_SPL_OS_BOOT)
437 	else
438 		spl_image->os = IH_OS_U_BOOT;
439 #endif
440 
441 	/*
442 	 * Booting a next-stage U-Boot may require us to append the FDT.
443 	 * We allow this to fail, as the U-Boot image might embed its FDT.
444 	 */
445 	if (spl_image->os == IH_OS_U_BOOT)
446 		spl_fit_append_fdt(spl_image, info, sector, fit,
447 				   images, base_offset);
448 
449 	/* Now check if there are more images for us to load */
450 	for (; ; index++) {
451 		uint8_t os_type = IH_OS_INVALID;
452 
453 		node = spl_fit_get_image_node(fit, images, "loadables", index);
454 		if (node < 0)
455 			break;
456 
457 		ret = spl_load_fit_image(info, sector, fit, base_offset, node,
458 					 &image_info);
459 		if (ret < 0)
460 			continue;
461 
462 		if (!spl_fit_image_get_os(fit, node, &os_type))
463 			debug("Loadable is %s\n", genimg_get_os_name(os_type));
464 
465 		if (os_type == IH_OS_U_BOOT) {
466 			spl_fit_append_fdt(&image_info, info, sector,
467 					   fit, images, base_offset);
468 			spl_image->fdt_addr = image_info.fdt_addr;
469 		}
470 
471 		/*
472 		 * If the "firmware" image did not provide an entry point,
473 		 * use the first valid entry point from the loadables.
474 		 */
475 		if (spl_image->entry_point == FDT_ERROR &&
476 		    image_info.entry_point != FDT_ERROR)
477 			spl_image->entry_point = image_info.entry_point;
478 
479 		/* Record our loadables into the FDT */
480 		if (spl_image->fdt_addr)
481 			spl_fit_record_loadable(fit, images, index,
482 						spl_image->fdt_addr,
483 						&image_info);
484 	}
485 
486 	/*
487 	 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
488 	 * Makefile will set it to 0 and it will end up as the entry point
489 	 * here. What it actually means is: use the load address.
490 	 */
491 	if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
492 		spl_image->entry_point = spl_image->load_addr;
493 
494 	return 0;
495 }
496