xref: /rk3399_rockchip-uboot/common/image-fdt.c (revision 6222c401243b38707fd88ccf5e4c35678c183f8d)
1 /*
2  * Copyright (c) 2013, Google Inc.
3  *
4  * (C) Copyright 2008 Semihalf
5  *
6  * (C) Copyright 2000-2006
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #include <common.h>
13 #include <fdtdec.h>
14 #include <fdt_support.h>
15 #include <errno.h>
16 #include <image.h>
17 #include <libfdt.h>
18 #include <mapmem.h>
19 #include <asm/io.h>
20 
21 #ifndef CONFIG_SYS_FDT_PAD
22 #define CONFIG_SYS_FDT_PAD 0x3000
23 #endif
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
27 static void fdt_error(const char *msg)
28 {
29 	puts("ERROR: ");
30 	puts(msg);
31 	puts(" - must RESET the board to recover.\n");
32 }
33 
34 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
35 static const image_header_t *image_get_fdt(ulong fdt_addr)
36 {
37 	const image_header_t *fdt_hdr = map_sysmem(fdt_addr, 0);
38 
39 	image_print_contents(fdt_hdr);
40 
41 	puts("   Verifying Checksum ... ");
42 	if (!image_check_hcrc(fdt_hdr)) {
43 		fdt_error("fdt header checksum invalid");
44 		return NULL;
45 	}
46 
47 	if (!image_check_dcrc(fdt_hdr)) {
48 		fdt_error("fdt checksum invalid");
49 		return NULL;
50 	}
51 	puts("OK\n");
52 
53 	if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
54 		fdt_error("uImage is not a fdt");
55 		return NULL;
56 	}
57 	if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
58 		fdt_error("uImage is compressed");
59 		return NULL;
60 	}
61 	if (fdt_check_header((void *)image_get_data(fdt_hdr)) != 0) {
62 		fdt_error("uImage data is not a fdt");
63 		return NULL;
64 	}
65 	return fdt_hdr;
66 }
67 #endif
68 
69 /**
70  * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
71  * @lmb: pointer to lmb handle, will be used for memory mgmt
72  * @fdt_blob: pointer to fdt blob base address
73  *
74  * Adds the memreserve regions in the dtb to the lmb block.  Adding the
75  * memreserve regions prevents u-boot from using them to store the initrd
76  * or the fdt blob.
77  */
78 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
79 {
80 	uint64_t addr, size;
81 	int i, total;
82 	int rsv_offset, offset;
83 	fdt_size_t rsv_size;
84 	fdt_addr_t rsv_addr;
85 	/* we needn't repeat do reserve, do_bootm_linux would call this again */
86 	static int rsv_done;
87 
88 	if (fdt_check_header(fdt_blob) != 0 || rsv_done)
89 		return;
90 
91 	rsv_done = 1;
92 
93 	total = fdt_num_mem_rsv(fdt_blob);
94 	for (i = 0; i < total; i++) {
95 		if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
96 			continue;
97 		printf("   reserving fdt memory region: addr=%llx size=%llx\n",
98 		       (unsigned long long)addr, (unsigned long long)size);
99 		lmb_reserve(lmb, addr, size);
100 	}
101 
102 	rsv_offset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory");
103 	if (rsv_offset == -FDT_ERR_NOTFOUND)
104 		return;
105 
106 	for (offset = fdt_first_subnode(fdt_blob, rsv_offset);
107 	     offset >= 0;
108 	     offset = fdt_next_subnode(fdt_blob, offset)) {
109 		rsv_addr = fdtdec_get_addr_size_auto_noparent(fdt_blob, offset,
110 							      "reg", 0,
111 							      &rsv_size, false);
112 		if (rsv_addr == FDT_ADDR_T_NONE || !rsv_size)
113 			continue;
114 		printf("  'reserved-memory' region: addr=%llx size=%llx\n",
115 			(unsigned long long)rsv_addr, (unsigned long long)rsv_size);
116 		lmb_reserve(lmb, rsv_addr, rsv_size);
117 	}
118 }
119 
120 /**
121  * boot_relocate_fdt - relocate flat device tree
122  * @lmb: pointer to lmb handle, will be used for memory mgmt
123  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
124  * @of_size: pointer to a ulong variable, will hold fdt length
125  *
126  * boot_relocate_fdt() allocates a region of memory within the bootmap and
127  * relocates the of_flat_tree into that region, even if the fdt is already in
128  * the bootmap.  It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
129  * bytes.
130  *
131  * of_flat_tree and of_size are set to final (after relocation) values
132  *
133  * returns:
134  *      0 - success
135  *      1 - failure
136  */
137 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
138 {
139 	void	*fdt_blob = *of_flat_tree;
140 	void	*of_start = NULL;
141 	char	*fdt_high;
142 	ulong	of_len = 0;
143 	int	err;
144 	int	disable_relocation = 0;
145 
146 	/* nothing to do */
147 	if (*of_size == 0)
148 		return 0;
149 
150 	if (fdt_check_header(fdt_blob) != 0) {
151 		fdt_error("image is not a fdt");
152 		goto error;
153 	}
154 
155 	/* position on a 4K boundary before the alloc_current */
156 	/* Pad the FDT by a specified amount */
157 	of_len = *of_size + CONFIG_SYS_FDT_PAD;
158 
159 	/* If fdt_high is set use it to select the relocation address */
160 	fdt_high = env_get("fdt_high");
161 	if (fdt_high) {
162 		void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
163 
164 		if (((ulong) desired_addr) == ~0UL) {
165 			/* All ones means use fdt in place */
166 			of_start = fdt_blob;
167 			lmb_reserve(lmb, (ulong)of_start, of_len);
168 			disable_relocation = 1;
169 		} else if (desired_addr) {
170 			of_start =
171 			    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
172 							   (ulong)desired_addr);
173 			if (of_start == NULL) {
174 				puts("Failed using fdt_high value for Device Tree");
175 				goto error;
176 			}
177 		} else {
178 			of_start =
179 			    (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
180 		}
181 	} else {
182 		of_start =
183 		    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
184 						   env_get_bootm_mapsize()
185 						   + env_get_bootm_low());
186 	}
187 
188 	if (of_start == NULL) {
189 		puts("device tree - allocation error\n");
190 		goto error;
191 	}
192 
193 	if (disable_relocation) {
194 		/*
195 		 * We assume there is space after the existing fdt to use
196 		 * for padding
197 		 */
198 		fdt_set_totalsize(of_start, of_len);
199 		printf("   Using Device Tree in place at %p, end %p\n",
200 		       of_start, of_start + of_len - 1);
201 	} else {
202 		debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
203 		      fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
204 
205 		printf("   Loading Device Tree to %p, end %p ... ",
206 		       of_start, of_start + of_len - 1);
207 
208 		err = fdt_open_into(fdt_blob, of_start, of_len);
209 		if (err != 0) {
210 			fdt_error("fdt move failed");
211 			goto error;
212 		}
213 		puts("OK\n");
214 	}
215 
216 	*of_flat_tree = of_start;
217 	*of_size = of_len;
218 
219 	set_working_fdt_addr((ulong)*of_flat_tree);
220 	return 0;
221 
222 error:
223 	return 1;
224 }
225 
226 /**
227  * boot_get_fdt - main fdt handling routine
228  * @argc: command argument count
229  * @argv: command argument list
230  * @arch: architecture (IH_ARCH_...)
231  * @images: pointer to the bootm images structure
232  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
233  * @of_size: pointer to a ulong variable, will hold fdt length
234  *
235  * boot_get_fdt() is responsible for finding a valid flat device tree image.
236  * Curently supported are the following ramdisk sources:
237  *      - multicomponent kernel/ramdisk image,
238  *      - commandline provided address of decicated ramdisk image.
239  *
240  * returns:
241  *     0, if fdt image was found and valid, or skipped
242  *     of_flat_tree and of_size are set to fdt start address and length if
243  *     fdt image is found and valid
244  *
245  *     1, if fdt image is found but corrupted
246  *     of_flat_tree and of_size are set to 0 if no fdt exists
247  */
248 int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
249 		bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
250 {
251 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
252 	const image_header_t *fdt_hdr;
253 	ulong		load, load_end;
254 	ulong		image_start, image_data, image_end;
255 #endif
256 	ulong		fdt_addr;
257 	char		*fdt_blob = NULL;
258 	void		*buf;
259 #if CONFIG_IS_ENABLED(FIT)
260 	const char	*fit_uname_config = images->fit_uname_cfg;
261 	const char	*fit_uname_fdt = NULL;
262 	ulong		default_addr;
263 	int		fdt_noffset;
264 #endif
265 	const char *select = NULL;
266 	int		ok_no_fdt = 0;
267 
268 	*of_flat_tree = NULL;
269 	*of_size = 0;
270 
271 	if (argc > 2)
272 		select = argv[2];
273 	if (select || genimg_has_config(images)) {
274 #if CONFIG_IS_ENABLED(FIT)
275 		if (select) {
276 			/*
277 			 * If the FDT blob comes from the FIT image and the
278 			 * FIT image address is omitted in the command line
279 			 * argument, try to use ramdisk or os FIT image
280 			 * address or default load address.
281 			 */
282 			if (images->fit_uname_rd)
283 				default_addr = (ulong)images->fit_hdr_rd;
284 			else if (images->fit_uname_os)
285 				default_addr = (ulong)images->fit_hdr_os;
286 			else
287 				default_addr = load_addr;
288 
289 			if (fit_parse_conf(select, default_addr,
290 					   &fdt_addr, &fit_uname_config)) {
291 				debug("*  fdt: config '%s' from image at 0x%08lx\n",
292 				      fit_uname_config, fdt_addr);
293 			} else if (fit_parse_subimage(select, default_addr,
294 				   &fdt_addr, &fit_uname_fdt)) {
295 				debug("*  fdt: subimage '%s' from image at 0x%08lx\n",
296 				      fit_uname_fdt, fdt_addr);
297 			} else
298 #endif
299 			{
300 				fdt_addr = simple_strtoul(select, NULL, 16);
301 				debug("*  fdt: cmdline image address = 0x%08lx\n",
302 				      fdt_addr);
303 			}
304 #if CONFIG_IS_ENABLED(FIT)
305 		} else {
306 			/* use FIT configuration provided in first bootm
307 			 * command argument
308 			 */
309 			fdt_addr = map_to_sysmem(images->fit_hdr_os);
310 			fdt_noffset = fit_get_node_from_config(images,
311 							       FIT_FDT_PROP,
312 							       fdt_addr);
313 			if (fdt_noffset == -ENOENT)
314 				return 0;
315 			else if (fdt_noffset < 0)
316 				return 1;
317 		}
318 #endif
319 		debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
320 		      fdt_addr);
321 
322 		/*
323 		 * Check if there is an FDT image at the
324 		 * address provided in the second bootm argument
325 		 * check image type, for FIT images get a FIT node.
326 		 */
327 		buf = map_sysmem(fdt_addr, 0);
328 		switch (genimg_get_format(buf)) {
329 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
330 		case IMAGE_FORMAT_LEGACY:
331 			/* verify fdt_addr points to a valid image header */
332 			printf("## Flattened Device Tree from Legacy Image at %08lx\n",
333 			       fdt_addr);
334 			fdt_hdr = image_get_fdt(fdt_addr);
335 			if (!fdt_hdr)
336 				goto no_fdt;
337 
338 			/*
339 			 * move image data to the load address,
340 			 * make sure we don't overwrite initial image
341 			 */
342 			image_start = (ulong)fdt_hdr;
343 			image_data = (ulong)image_get_data(fdt_hdr);
344 			image_end = image_get_image_end(fdt_hdr);
345 
346 			load = image_get_load(fdt_hdr);
347 			load_end = load + image_get_data_size(fdt_hdr);
348 
349 			if (load == image_start ||
350 			    load == image_data) {
351 				fdt_addr = load;
352 				break;
353 			}
354 
355 			if ((load < image_end) && (load_end > image_start)) {
356 				fdt_error("fdt overwritten");
357 				goto error;
358 			}
359 
360 			debug("   Loading FDT from 0x%08lx to 0x%08lx\n",
361 			      image_data, load);
362 
363 			memmove((void *)load,
364 				(void *)image_data,
365 				image_get_data_size(fdt_hdr));
366 
367 			fdt_addr = load;
368 			break;
369 #endif
370 		case IMAGE_FORMAT_FIT:
371 			/*
372 			 * This case will catch both: new uImage format
373 			 * (libfdt based) and raw FDT blob (also libfdt
374 			 * based).
375 			 */
376 #if CONFIG_IS_ENABLED(FIT)
377 			/* check FDT blob vs FIT blob */
378 			if (fit_check_format(buf)) {
379 				ulong load, len;
380 
381 				fdt_noffset = boot_get_fdt_fit(images,
382 					fdt_addr, &fit_uname_fdt,
383 					&fit_uname_config,
384 					arch, &load, &len);
385 
386 				images->fit_hdr_fdt = map_sysmem(fdt_addr, 0);
387 				images->fit_uname_fdt = fit_uname_fdt;
388 				images->fit_noffset_fdt = fdt_noffset;
389 				fdt_addr = load;
390 
391 				break;
392 			} else
393 #endif
394 			{
395 				/*
396 				 * FDT blob
397 				 */
398 				debug("*  fdt: raw FDT blob\n");
399 				printf("## Flattened Device Tree blob at %08lx\n",
400 				       (long)fdt_addr);
401 			}
402 			break;
403 		default:
404 			puts("ERROR: Did not find a cmdline Flattened Device Tree\n");
405 			goto no_fdt;
406 		}
407 
408 		printf("   Booting using the fdt blob at %#08lx\n", fdt_addr);
409 		fdt_blob = map_sysmem(fdt_addr, 0);
410 	} else if (images->legacy_hdr_valid &&
411 			image_check_type(&images->legacy_hdr_os_copy,
412 					 IH_TYPE_MULTI)) {
413 		ulong fdt_data, fdt_len;
414 
415 		/*
416 		 * Now check if we have a legacy multi-component image,
417 		 * get second entry data start address and len.
418 		 */
419 		printf("## Flattened Device Tree from multi component Image at %08lX\n",
420 		       (ulong)images->legacy_hdr_os);
421 
422 		image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
423 				   &fdt_len);
424 		if (fdt_len) {
425 			fdt_blob = (char *)fdt_data;
426 			printf("   Booting using the fdt at 0x%p\n", fdt_blob);
427 
428 			if (fdt_check_header(fdt_blob) != 0) {
429 				fdt_error("image is not a fdt");
430 				goto error;
431 			}
432 
433 			if (fdt_totalsize(fdt_blob) != fdt_len) {
434 				fdt_error("fdt size != image size");
435 				goto error;
436 			}
437 		} else {
438 			debug("## No Flattened Device Tree\n");
439 			goto no_fdt;
440 		}
441 	} else {
442 		debug("## No Flattened Device Tree\n");
443 		goto no_fdt;
444 	}
445 
446 	*of_flat_tree = fdt_blob;
447 	*of_size = fdt_totalsize(fdt_blob);
448 	debug("   of_flat_tree at 0x%08lx size 0x%08lx\n",
449 	      (ulong)*of_flat_tree, *of_size);
450 
451 	return 0;
452 
453 no_fdt:
454 	ok_no_fdt = 1;
455 error:
456 	*of_flat_tree = NULL;
457 	*of_size = 0;
458 	if (!select && ok_no_fdt) {
459 		debug("Continuing to boot without FDT\n");
460 		return 0;
461 	}
462 	return 1;
463 }
464 
465 /*
466  * Verify the device tree.
467  *
468  * This function is called after all device tree fix-ups have been enacted,
469  * so that the final device tree can be verified.  The definition of "verified"
470  * is up to the specific implementation.  However, it generally means that the
471  * addresses of some of the devices in the device tree are compared with the
472  * actual addresses at which U-Boot has placed them.
473  *
474  * Returns 1 on success, 0 on failure.  If 0 is returned, U-Boot will halt the
475  * boot process.
476  */
477 __weak int ft_verify_fdt(void *fdt)
478 {
479 	return 1;
480 }
481 
482 int image_setup_libfdt(bootm_headers_t *images, void *blob,
483 		       int of_size, struct lmb *lmb)
484 {
485 	ulong *initrd_start = &images->initrd_start;
486 	ulong *initrd_end = &images->initrd_end;
487 	int ret = -EPERM;
488 	int fdt_ret;
489 
490 	if (fdt_root(blob) < 0) {
491 		printf("ERROR: root node setup failed\n");
492 		goto err;
493 	}
494 	if (fdt_chosen(blob) < 0) {
495 		printf("ERROR: /chosen node create failed\n");
496 		goto err;
497 	}
498 	if (arch_fixup_fdt(blob) < 0) {
499 		printf("ERROR: arch-specific fdt fixup failed\n");
500 		goto err;
501 	}
502 	/* Update ethernet nodes */
503 	fdt_fixup_ethernet(blob);
504 	if (IMAGE_OF_BOARD_SETUP) {
505 		fdt_ret = ft_board_setup(blob, gd->bd);
506 		if (fdt_ret) {
507 			printf("ERROR: board-specific fdt fixup failed: %s\n",
508 			       fdt_strerror(fdt_ret));
509 			goto err;
510 		}
511 	}
512 	if (IMAGE_OF_SYSTEM_SETUP) {
513 		fdt_ret = ft_system_setup(blob, gd->bd);
514 		if (fdt_ret) {
515 			printf("ERROR: system-specific fdt fixup failed: %s\n",
516 			       fdt_strerror(fdt_ret));
517 			goto err;
518 		}
519 	}
520 
521 	/* Delete the old LMB reservation */
522 	if (lmb)
523 		lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
524 			 (phys_size_t)fdt_totalsize(blob));
525 
526 	ret = fdt_shrink_to_minimum(blob, 0);
527 	if (ret < 0)
528 		goto err;
529 	of_size = ret;
530 
531 	if (*initrd_start && *initrd_end) {
532 		of_size += FDT_RAMDISK_OVERHEAD;
533 		fdt_set_totalsize(blob, of_size);
534 	}
535 	/* Create a new LMB reservation */
536 	if (lmb)
537 		lmb_reserve(lmb, (ulong)blob, of_size);
538 
539 	fdt_initrd(blob, *initrd_start, *initrd_end);
540 	if (!ft_verify_fdt(blob))
541 		goto err;
542 
543 #if defined(CONFIG_SOC_KEYSTONE)
544 	if (IMAGE_OF_BOARD_SETUP)
545 		ft_board_setup_ex(blob, gd->bd);
546 #endif
547 
548 	return 0;
549 err:
550 	printf(" - must RESET the board to recover.\n\n");
551 
552 	return ret;
553 }
554