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