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