xref: /openbmc/u-boot/common/image-fdt.c (revision 9cefbd64)
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  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27 
28 #include <common.h>
29 #include <fdt_support.h>
30 #include <errno.h>
31 #include <image.h>
32 #include <libfdt.h>
33 #include <asm/io.h>
34 
35 #ifndef CONFIG_SYS_FDT_PAD
36 #define CONFIG_SYS_FDT_PAD 0x3000
37 #endif
38 
39 DECLARE_GLOBAL_DATA_PTR;
40 
41 static void fdt_error(const char *msg)
42 {
43 	puts("ERROR: ");
44 	puts(msg);
45 	puts(" - must RESET the board to recover.\n");
46 }
47 
48 static const image_header_t *image_get_fdt(ulong fdt_addr)
49 {
50 	const image_header_t *fdt_hdr = map_sysmem(fdt_addr, 0);
51 
52 	image_print_contents(fdt_hdr);
53 
54 	puts("   Verifying Checksum ... ");
55 	if (!image_check_hcrc(fdt_hdr)) {
56 		fdt_error("fdt header checksum invalid");
57 		return NULL;
58 	}
59 
60 	if (!image_check_dcrc(fdt_hdr)) {
61 		fdt_error("fdt checksum invalid");
62 		return NULL;
63 	}
64 	puts("OK\n");
65 
66 	if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
67 		fdt_error("uImage is not a fdt");
68 		return NULL;
69 	}
70 	if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
71 		fdt_error("uImage is compressed");
72 		return NULL;
73 	}
74 	if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
75 		fdt_error("uImage data is not a fdt");
76 		return NULL;
77 	}
78 	return fdt_hdr;
79 }
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 
95 	if (fdt_check_header(fdt_blob) != 0)
96 		return;
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 
108 /**
109  * boot_relocate_fdt - relocate flat device tree
110  * @lmb: pointer to lmb handle, will be used for memory mgmt
111  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
112  * @of_size: pointer to a ulong variable, will hold fdt length
113  *
114  * boot_relocate_fdt() allocates a region of memory within the bootmap and
115  * relocates the of_flat_tree into that region, even if the fdt is already in
116  * the bootmap.  It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
117  * bytes.
118  *
119  * of_flat_tree and of_size are set to final (after relocation) values
120  *
121  * returns:
122  *      0 - success
123  *      1 - failure
124  */
125 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
126 {
127 	void	*fdt_blob = *of_flat_tree;
128 	void	*of_start = NULL;
129 	char	*fdt_high;
130 	ulong	of_len = 0;
131 	int	err;
132 	int	disable_relocation = 0;
133 
134 	/* nothing to do */
135 	if (*of_size == 0)
136 		return 0;
137 
138 	if (fdt_check_header(fdt_blob) != 0) {
139 		fdt_error("image is not a fdt");
140 		goto error;
141 	}
142 
143 	/* position on a 4K boundary before the alloc_current */
144 	/* Pad the FDT by a specified amount */
145 	of_len = *of_size + CONFIG_SYS_FDT_PAD;
146 
147 	/* If fdt_high is set use it to select the relocation address */
148 	fdt_high = getenv("fdt_high");
149 	if (fdt_high) {
150 		void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
151 
152 		if (((ulong) desired_addr) == ~0UL) {
153 			/* All ones means use fdt in place */
154 			of_start = fdt_blob;
155 			lmb_reserve(lmb, (ulong)of_start, of_len);
156 			disable_relocation = 1;
157 		} else if (desired_addr) {
158 			of_start =
159 			    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
160 							   (ulong)desired_addr);
161 			if (of_start == NULL) {
162 				puts("Failed using fdt_high value for Device Tree");
163 				goto error;
164 			}
165 		} else {
166 			of_start =
167 			    (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
168 		}
169 	} else {
170 		of_start =
171 		    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
172 						   getenv_bootm_mapsize()
173 						   + getenv_bootm_low());
174 	}
175 
176 	if (of_start == NULL) {
177 		puts("device tree - allocation error\n");
178 		goto error;
179 	}
180 
181 	if (disable_relocation) {
182 		/*
183 		 * We assume there is space after the existing fdt to use
184 		 * for padding
185 		 */
186 		fdt_set_totalsize(of_start, of_len);
187 		printf("   Using Device Tree in place at %p, end %p\n",
188 		       of_start, of_start + of_len - 1);
189 	} else {
190 		debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
191 		      fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
192 
193 		printf("   Loading Device Tree to %p, end %p ... ",
194 		       of_start, of_start + of_len - 1);
195 
196 		err = fdt_open_into(fdt_blob, of_start, of_len);
197 		if (err != 0) {
198 			fdt_error("fdt move failed");
199 			goto error;
200 		}
201 		puts("OK\n");
202 	}
203 
204 	*of_flat_tree = of_start;
205 	*of_size = of_len;
206 
207 	set_working_fdt_addr(*of_flat_tree);
208 	return 0;
209 
210 error:
211 	return 1;
212 }
213 
214 #if defined(CONFIG_FIT)
215 /**
216  * fit_check_fdt - verify FIT format FDT subimage
217  * @fit_hdr: pointer to the FIT  header
218  * fdt_noffset: FDT subimage node offset within FIT image
219  * @verify: data CRC verification flag
220  *
221  * fit_check_fdt() verifies integrity of the FDT subimage and from
222  * specified FIT image.
223  *
224  * returns:
225  *     1, on success
226  *     0, on failure
227  */
228 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
229 {
230 	fit_image_print(fit, fdt_noffset, "   ");
231 
232 	if (verify) {
233 		puts("   Verifying Hash Integrity ... ");
234 		if (!fit_image_verify(fit, fdt_noffset)) {
235 			fdt_error("Bad Data Hash");
236 			return 0;
237 		}
238 		puts("OK\n");
239 	}
240 
241 	if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
242 		fdt_error("Not a FDT image");
243 		return 0;
244 	}
245 
246 	if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
247 		fdt_error("FDT image is compressed");
248 		return 0;
249 	}
250 
251 	return 1;
252 }
253 #endif
254 
255 /**
256  * boot_get_fdt - main fdt handling routine
257  * @argc: command argument count
258  * @argv: command argument list
259  * @images: pointer to the bootm images structure
260  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
261  * @of_size: pointer to a ulong variable, will hold fdt length
262  *
263  * boot_get_fdt() is responsible for finding a valid flat device tree image.
264  * Curently supported are the following ramdisk sources:
265  *      - multicomponent kernel/ramdisk image,
266  *      - commandline provided address of decicated ramdisk image.
267  *
268  * returns:
269  *     0, if fdt image was found and valid, or skipped
270  *     of_flat_tree and of_size are set to fdt start address and length if
271  *     fdt image is found and valid
272  *
273  *     1, if fdt image is found but corrupted
274  *     of_flat_tree and of_size are set to 0 if no fdt exists
275  */
276 int boot_get_fdt(int flag, int argc, char * const argv[],
277 		bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
278 {
279 	const image_header_t *fdt_hdr;
280 	ulong		fdt_addr;
281 	char		*fdt_blob = NULL;
282 	ulong		image_start, image_data, image_end;
283 	ulong		load_start, load_end;
284 	void		*buf;
285 #if defined(CONFIG_FIT)
286 	void		*fit_hdr;
287 	const char	*fit_uname_config = NULL;
288 	const char	*fit_uname_fdt = NULL;
289 	ulong		default_addr;
290 	int		cfg_noffset;
291 	int		fdt_noffset;
292 	const void	*data;
293 	size_t		size;
294 #endif
295 
296 	*of_flat_tree = NULL;
297 	*of_size = 0;
298 
299 	if (argc > 3 || genimg_has_config(images)) {
300 #if defined(CONFIG_FIT)
301 		if (argc > 3) {
302 			/*
303 			 * If the FDT blob comes from the FIT image and the
304 			 * FIT image address is omitted in the command line
305 			 * argument, try to use ramdisk or os FIT image
306 			 * address or default load address.
307 			 */
308 			if (images->fit_uname_rd)
309 				default_addr = (ulong)images->fit_hdr_rd;
310 			else if (images->fit_uname_os)
311 				default_addr = (ulong)images->fit_hdr_os;
312 			else
313 				default_addr = load_addr;
314 
315 			if (fit_parse_conf(argv[3], default_addr,
316 					   &fdt_addr, &fit_uname_config)) {
317 				debug("*  fdt: config '%s' from image at 0x%08lx\n",
318 				      fit_uname_config, fdt_addr);
319 			} else if (fit_parse_subimage(argv[3], default_addr,
320 				   &fdt_addr, &fit_uname_fdt)) {
321 				debug("*  fdt: subimage '%s' from image at 0x%08lx\n",
322 				      fit_uname_fdt, fdt_addr);
323 			} else
324 #endif
325 			{
326 				fdt_addr = simple_strtoul(argv[3], NULL, 16);
327 				debug("*  fdt: cmdline image address = 0x%08lx\n",
328 				      fdt_addr);
329 			}
330 #if defined(CONFIG_FIT)
331 		} else {
332 			/* use FIT configuration provided in first bootm
333 			 * command argument
334 			 */
335 			fdt_addr = map_to_sysmem(images->fit_hdr_os);
336 			fit_uname_config = images->fit_uname_cfg;
337 			debug("*  fdt: using config '%s' from image at 0x%08lx\n",
338 			      fit_uname_config, fdt_addr);
339 
340 			/*
341 			 * Check whether configuration has FDT blob defined,
342 			 * if not quit silently.
343 			 */
344 			fit_hdr = images->fit_hdr_os;
345 			cfg_noffset = fit_conf_get_node(fit_hdr,
346 					fit_uname_config);
347 			if (cfg_noffset < 0) {
348 				debug("*  fdt: no such config\n");
349 				return 0;
350 			}
351 
352 			fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
353 					cfg_noffset);
354 			if (fdt_noffset < 0) {
355 				debug("*  fdt: no fdt in config\n");
356 				return 0;
357 			}
358 		}
359 #endif
360 
361 		debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
362 		      fdt_addr);
363 
364 		/* copy from dataflash if needed */
365 		fdt_addr = genimg_get_image(fdt_addr);
366 
367 		/*
368 		 * Check if there is an FDT image at the
369 		 * address provided in the second bootm argument
370 		 * check image type, for FIT images get a FIT node.
371 		 */
372 		buf = map_sysmem(fdt_addr, 0);
373 		switch (genimg_get_format(buf)) {
374 		case IMAGE_FORMAT_LEGACY:
375 			/* verify fdt_addr points to a valid image header */
376 			printf("## Flattened Device Tree from Legacy Image at %08lx\n",
377 			       fdt_addr);
378 			fdt_hdr = image_get_fdt(fdt_addr);
379 			if (!fdt_hdr)
380 				goto error;
381 
382 			/*
383 			 * move image data to the load address,
384 			 * make sure we don't overwrite initial image
385 			 */
386 			image_start = (ulong)fdt_hdr;
387 			image_data = (ulong)image_get_data(fdt_hdr);
388 			image_end = image_get_image_end(fdt_hdr);
389 
390 			load_start = image_get_load(fdt_hdr);
391 			load_end = load_start + image_get_data_size(fdt_hdr);
392 
393 			if (load_start == image_start ||
394 			    load_start == image_data) {
395 				fdt_blob = (char *)image_data;
396 				break;
397 			}
398 
399 			if ((load_start < image_end) &&
400 			    (load_end > image_start)) {
401 				fdt_error("fdt overwritten");
402 				goto error;
403 			}
404 
405 			debug("   Loading FDT from 0x%08lx to 0x%08lx\n",
406 			      image_data, load_start);
407 
408 			memmove((void *)load_start,
409 				(void *)image_data,
410 				image_get_data_size(fdt_hdr));
411 
412 			fdt_blob = (char *)load_start;
413 			break;
414 		case IMAGE_FORMAT_FIT:
415 			/*
416 			 * This case will catch both: new uImage format
417 			 * (libfdt based) and raw FDT blob (also libfdt
418 			 * based).
419 			 */
420 #if defined(CONFIG_FIT)
421 			/* check FDT blob vs FIT blob */
422 			if (fit_check_format(buf)) {
423 				/*
424 				 * FIT image
425 				 */
426 				fit_hdr = buf;
427 				printf("## Flattened Device Tree from FIT Image at %08lx\n",
428 				       fdt_addr);
429 
430 				if (!fit_uname_fdt) {
431 					/*
432 					 * no FDT blob image node unit name,
433 					 * try to get config node first. If
434 					 * config unit node name is NULL
435 					 * fit_conf_get_node() will try to
436 					 * find default config node
437 					 */
438 					cfg_noffset = fit_conf_get_node(fit_hdr,
439 							fit_uname_config);
440 
441 					if (cfg_noffset < 0) {
442 						fdt_error("Could not find configuration node\n");
443 						goto error;
444 					}
445 
446 					fit_uname_config = fdt_get_name(fit_hdr,
447 							cfg_noffset, NULL);
448 					printf("   Using '%s' configuration\n",
449 					       fit_uname_config);
450 
451 					fdt_noffset = fit_conf_get_fdt_node(
452 							fit_hdr,
453 							cfg_noffset);
454 					fit_uname_fdt = fit_get_name(fit_hdr,
455 							fdt_noffset, NULL);
456 				} else {
457 					/*
458 					 * get FDT component image node
459 					 * offset
460 					 */
461 					fdt_noffset = fit_image_get_node(
462 								fit_hdr,
463 								fit_uname_fdt);
464 				}
465 				if (fdt_noffset < 0) {
466 					fdt_error("Could not find subimage node\n");
467 					goto error;
468 				}
469 
470 				printf("   Trying '%s' FDT blob subimage\n",
471 				       fit_uname_fdt);
472 
473 				if (!fit_check_fdt(fit_hdr, fdt_noffset,
474 						   images->verify))
475 					goto error;
476 
477 				/* get ramdisk image data address and length */
478 				if (fit_image_get_data(fit_hdr, fdt_noffset,
479 						       &data, &size)) {
480 					fdt_error("Could not find FDT subimage data");
481 					goto error;
482 				}
483 
484 				/*
485 				 * verify that image data is a proper FDT
486 				 * blob
487 				 */
488 				if (fdt_check_header((char *)data) != 0) {
489 					fdt_error("Subimage data is not a FTD");
490 					goto error;
491 				}
492 
493 				/*
494 				 * move image data to the load address,
495 				 * make sure we don't overwrite initial image
496 				 */
497 				image_start = (ulong)fit_hdr;
498 				image_end = fit_get_end(fit_hdr);
499 
500 				if (fit_image_get_load(fit_hdr, fdt_noffset,
501 						       &load_start) == 0) {
502 					load_end = load_start + size;
503 
504 					if ((load_start < image_end) &&
505 					    (load_end > image_start)) {
506 						fdt_error("FDT overwritten");
507 						goto error;
508 					}
509 
510 					printf("   Loading FDT from 0x%08lx to 0x%08lx\n",
511 					       (ulong)data, load_start);
512 
513 					memmove((void *)load_start,
514 						(void *)data, size);
515 
516 					fdt_blob = (char *)load_start;
517 				} else {
518 					fdt_blob = (char *)data;
519 				}
520 
521 				images->fit_hdr_fdt = fit_hdr;
522 				images->fit_uname_fdt = fit_uname_fdt;
523 				images->fit_noffset_fdt = fdt_noffset;
524 				break;
525 			} else
526 #endif
527 			{
528 				/*
529 				 * FDT blob
530 				 */
531 				fdt_blob = buf;
532 				debug("*  fdt: raw FDT blob\n");
533 				printf("## Flattened Device Tree blob at %08lx\n",
534 				       (long)fdt_addr);
535 			}
536 			break;
537 		default:
538 			puts("ERROR: Did not find a cmdline Flattened Device Tree\n");
539 			goto error;
540 		}
541 
542 		printf("   Booting using the fdt blob at 0x%p\n", fdt_blob);
543 
544 	} else if (images->legacy_hdr_valid &&
545 			image_check_type(&images->legacy_hdr_os_copy,
546 					 IH_TYPE_MULTI)) {
547 		ulong fdt_data, fdt_len;
548 
549 		/*
550 		 * Now check if we have a legacy multi-component image,
551 		 * get second entry data start address and len.
552 		 */
553 		printf("## Flattened Device Tree from multi component Image at %08lX\n",
554 		       (ulong)images->legacy_hdr_os);
555 
556 		image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
557 				   &fdt_len);
558 		if (fdt_len) {
559 			fdt_blob = (char *)fdt_data;
560 			printf("   Booting using the fdt at 0x%p\n", fdt_blob);
561 
562 			if (fdt_check_header(fdt_blob) != 0) {
563 				fdt_error("image is not a fdt");
564 				goto error;
565 			}
566 
567 			if (fdt_totalsize(fdt_blob) != fdt_len) {
568 				fdt_error("fdt size != image size");
569 				goto error;
570 			}
571 		} else {
572 			debug("## No Flattened Device Tree\n");
573 			return 0;
574 		}
575 	} else {
576 		debug("## No Flattened Device Tree\n");
577 		return 0;
578 	}
579 
580 	*of_flat_tree = fdt_blob;
581 	*of_size = fdt_totalsize(fdt_blob);
582 	debug("   of_flat_tree at 0x%08lx size 0x%08lx\n",
583 	      (ulong)*of_flat_tree, *of_size);
584 
585 	return 0;
586 
587 error:
588 	*of_flat_tree = NULL;
589 	*of_size = 0;
590 	return 1;
591 }
592 
593 /*
594  * Verify the device tree.
595  *
596  * This function is called after all device tree fix-ups have been enacted,
597  * so that the final device tree can be verified.  The definition of "verified"
598  * is up to the specific implementation.  However, it generally means that the
599  * addresses of some of the devices in the device tree are compared with the
600  * actual addresses at which U-Boot has placed them.
601  *
602  * Returns 1 on success, 0 on failure.  If 0 is returned, U-boot will halt the
603  * boot process.
604  */
605 __weak int ft_verify_fdt(void *fdt)
606 {
607 	return 1;
608 }
609 
610 __weak int arch_fixup_memory_node(void *blob)
611 {
612 	return 0;
613 }
614 
615 int image_setup_libfdt(bootm_headers_t *images, void *blob,
616 		       int of_size, struct lmb *lmb)
617 {
618 	ulong *initrd_start = &images->initrd_start;
619 	ulong *initrd_end = &images->initrd_end;
620 	int ret;
621 
622 	if (fdt_chosen(blob, 1) < 0) {
623 		puts("ERROR: /chosen node create failed");
624 		puts(" - must RESET the board to recover.\n");
625 		return -1;
626 	}
627 	arch_fixup_memory_node(blob);
628 	if (IMAAGE_OF_BOARD_SETUP)
629 		ft_board_setup(blob, gd->bd);
630 	fdt_fixup_ethernet(blob);
631 
632 	/* Delete the old LMB reservation */
633 	lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
634 		 (phys_size_t)fdt_totalsize(blob));
635 
636 	ret = fdt_resize(blob);
637 	if (ret < 0)
638 		return ret;
639 	of_size = ret;
640 
641 	if (*initrd_start && *initrd_end) {
642 		of_size += FDT_RAMDISK_OVERHEAD;
643 		fdt_set_totalsize(blob, of_size);
644 	}
645 	/* Create a new LMB reservation */
646 	lmb_reserve(lmb, (ulong)blob, of_size);
647 
648 	fdt_initrd(blob, *initrd_start, *initrd_end, 1);
649 	if (!ft_verify_fdt(blob))
650 		return -1;
651 
652 	return 0;
653 }
654