xref: /openbmc/u-boot/common/image-fdt.c (revision 2418d75a)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
244d3a306SSimon Glass /*
344d3a306SSimon Glass  * Copyright (c) 2013, Google Inc.
444d3a306SSimon Glass  *
544d3a306SSimon Glass  * (C) Copyright 2008 Semihalf
644d3a306SSimon Glass  *
744d3a306SSimon Glass  * (C) Copyright 2000-2006
844d3a306SSimon Glass  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
944d3a306SSimon Glass  */
1044d3a306SSimon Glass 
1144d3a306SSimon Glass #include <common.h>
1244d3a306SSimon Glass #include <fdt_support.h>
13e2237a2cSSimon Goldschmidt #include <fdtdec.h>
1444d3a306SSimon Glass #include <errno.h>
1544d3a306SSimon Glass #include <image.h>
16b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
170eb25b61SJoe Hershberger #include <mapmem.h>
1844d3a306SSimon Glass #include <asm/io.h>
1944d3a306SSimon Glass 
2044d3a306SSimon Glass #ifndef CONFIG_SYS_FDT_PAD
2144d3a306SSimon Glass #define CONFIG_SYS_FDT_PAD 0x3000
2244d3a306SSimon Glass #endif
2344d3a306SSimon Glass 
24c960a68eSMasahiro Yamada /* adding a ramdisk needs 0x44 bytes in version 2008.10 */
25c960a68eSMasahiro Yamada #define FDT_RAMDISK_OVERHEAD	0x80
26c960a68eSMasahiro Yamada 
2744d3a306SSimon Glass DECLARE_GLOBAL_DATA_PTR;
2844d3a306SSimon Glass 
fdt_error(const char * msg)2944d3a306SSimon Glass static void fdt_error(const char *msg)
3044d3a306SSimon Glass {
3144d3a306SSimon Glass 	puts("ERROR: ");
3244d3a306SSimon Glass 	puts(msg);
3344d3a306SSimon Glass 	puts(" - must RESET the board to recover.\n");
3444d3a306SSimon Glass }
3544d3a306SSimon Glass 
3621d29f7fSHeiko Schocher #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
image_get_fdt(ulong fdt_addr)3744d3a306SSimon Glass static const image_header_t *image_get_fdt(ulong fdt_addr)
3844d3a306SSimon Glass {
3944d3a306SSimon Glass 	const image_header_t *fdt_hdr = map_sysmem(fdt_addr, 0);
4044d3a306SSimon Glass 
4144d3a306SSimon Glass 	image_print_contents(fdt_hdr);
4244d3a306SSimon Glass 
4344d3a306SSimon Glass 	puts("   Verifying Checksum ... ");
4444d3a306SSimon Glass 	if (!image_check_hcrc(fdt_hdr)) {
4544d3a306SSimon Glass 		fdt_error("fdt header checksum invalid");
4644d3a306SSimon Glass 		return NULL;
4744d3a306SSimon Glass 	}
4844d3a306SSimon Glass 
4944d3a306SSimon Glass 	if (!image_check_dcrc(fdt_hdr)) {
5044d3a306SSimon Glass 		fdt_error("fdt checksum invalid");
5144d3a306SSimon Glass 		return NULL;
5244d3a306SSimon Glass 	}
5344d3a306SSimon Glass 	puts("OK\n");
5444d3a306SSimon Glass 
5544d3a306SSimon Glass 	if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
5644d3a306SSimon Glass 		fdt_error("uImage is not a fdt");
5744d3a306SSimon Glass 		return NULL;
5844d3a306SSimon Glass 	}
5944d3a306SSimon Glass 	if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
6044d3a306SSimon Glass 		fdt_error("uImage is compressed");
6144d3a306SSimon Glass 		return NULL;
6244d3a306SSimon Glass 	}
632f0877c7SMasahiro Yamada 	if (fdt_check_header((void *)image_get_data(fdt_hdr)) != 0) {
6444d3a306SSimon Glass 		fdt_error("uImage data is not a fdt");
6544d3a306SSimon Glass 		return NULL;
6644d3a306SSimon Glass 	}
6744d3a306SSimon Glass 	return fdt_hdr;
6844d3a306SSimon Glass }
6921d29f7fSHeiko Schocher #endif
7044d3a306SSimon Glass 
boot_fdt_reserve_region(struct lmb * lmb,uint64_t addr,uint64_t size)71e2237a2cSSimon Goldschmidt static void boot_fdt_reserve_region(struct lmb *lmb, uint64_t addr,
72e2237a2cSSimon Goldschmidt 				    uint64_t size)
73e2237a2cSSimon Goldschmidt {
74e1d7ed34SPatrick Delaunay 	long ret;
75e2237a2cSSimon Goldschmidt 
76e2237a2cSSimon Goldschmidt 	ret = lmb_reserve(lmb, addr, size);
77e1d7ed34SPatrick Delaunay 	if (ret >= 0) {
78e2237a2cSSimon Goldschmidt 		debug("   reserving fdt memory region: addr=%llx size=%llx\n",
79e2237a2cSSimon Goldschmidt 		      (unsigned long long)addr, (unsigned long long)size);
80e2237a2cSSimon Goldschmidt 	} else {
81e2237a2cSSimon Goldschmidt 		puts("ERROR: reserving fdt memory region failed ");
82e2237a2cSSimon Goldschmidt 		printf("(addr=%llx size=%llx)\n",
83e2237a2cSSimon Goldschmidt 		       (unsigned long long)addr, (unsigned long long)size);
84e2237a2cSSimon Goldschmidt 	}
85e2237a2cSSimon Goldschmidt }
86e2237a2cSSimon Goldschmidt 
8744d3a306SSimon Glass /**
88e2237a2cSSimon Goldschmidt  * boot_fdt_add_mem_rsv_regions - Mark the memreserve and reserved-memory
89e2237a2cSSimon Goldschmidt  * sections as unusable
9044d3a306SSimon Glass  * @lmb: pointer to lmb handle, will be used for memory mgmt
9144d3a306SSimon Glass  * @fdt_blob: pointer to fdt blob base address
9244d3a306SSimon Glass  *
93e2237a2cSSimon Goldschmidt  * Adds the and reserved-memorymemreserve regions in the dtb to the lmb block.
94e2237a2cSSimon Goldschmidt  * Adding the memreserve regions prevents u-boot from using them to store the
95e2237a2cSSimon Goldschmidt  * initrd or the fdt blob.
9644d3a306SSimon Glass  */
boot_fdt_add_mem_rsv_regions(struct lmb * lmb,void * fdt_blob)9744d3a306SSimon Glass void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
9844d3a306SSimon Glass {
9944d3a306SSimon Glass 	uint64_t addr, size;
100e2237a2cSSimon Goldschmidt 	int i, total, ret;
101e2237a2cSSimon Goldschmidt 	int nodeoffset, subnode;
102e2237a2cSSimon Goldschmidt 	struct fdt_resource res;
10344d3a306SSimon Glass 
10444d3a306SSimon Glass 	if (fdt_check_header(fdt_blob) != 0)
10544d3a306SSimon Glass 		return;
10644d3a306SSimon Glass 
107e2237a2cSSimon Goldschmidt 	/* process memreserve sections */
10844d3a306SSimon Glass 	total = fdt_num_mem_rsv(fdt_blob);
10944d3a306SSimon Glass 	for (i = 0; i < total; i++) {
11044d3a306SSimon Glass 		if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
11144d3a306SSimon Glass 			continue;
112e2237a2cSSimon Goldschmidt 		boot_fdt_reserve_region(lmb, addr, size);
113e2237a2cSSimon Goldschmidt 	}
114e2237a2cSSimon Goldschmidt 
115e2237a2cSSimon Goldschmidt 	/* process reserved-memory */
116e2237a2cSSimon Goldschmidt 	nodeoffset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory");
117e2237a2cSSimon Goldschmidt 	if (nodeoffset >= 0) {
118e2237a2cSSimon Goldschmidt 		subnode = fdt_first_subnode(fdt_blob, nodeoffset);
119e2237a2cSSimon Goldschmidt 		while (subnode >= 0) {
120e2237a2cSSimon Goldschmidt 			/* check if this subnode has a reg property */
121e2237a2cSSimon Goldschmidt 			ret = fdt_get_resource(fdt_blob, subnode, "reg", 0,
122e2237a2cSSimon Goldschmidt 					       &res);
123e2237a2cSSimon Goldschmidt 			if (!ret) {
124e2237a2cSSimon Goldschmidt 				addr = res.start;
125e2237a2cSSimon Goldschmidt 				size = res.end - res.start + 1;
126e2237a2cSSimon Goldschmidt 				boot_fdt_reserve_region(lmb, addr, size);
127e2237a2cSSimon Goldschmidt 			}
128e2237a2cSSimon Goldschmidt 
129e2237a2cSSimon Goldschmidt 			subnode = fdt_next_subnode(fdt_blob, subnode);
130e2237a2cSSimon Goldschmidt 		}
13144d3a306SSimon Glass 	}
13244d3a306SSimon Glass }
13344d3a306SSimon Glass 
13444d3a306SSimon Glass /**
13544d3a306SSimon Glass  * boot_relocate_fdt - relocate flat device tree
13644d3a306SSimon Glass  * @lmb: pointer to lmb handle, will be used for memory mgmt
13744d3a306SSimon Glass  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
13844d3a306SSimon Glass  * @of_size: pointer to a ulong variable, will hold fdt length
13944d3a306SSimon Glass  *
14044d3a306SSimon Glass  * boot_relocate_fdt() allocates a region of memory within the bootmap and
14144d3a306SSimon Glass  * relocates the of_flat_tree into that region, even if the fdt is already in
14244d3a306SSimon Glass  * the bootmap.  It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
14344d3a306SSimon Glass  * bytes.
14444d3a306SSimon Glass  *
14544d3a306SSimon Glass  * of_flat_tree and of_size are set to final (after relocation) values
14644d3a306SSimon Glass  *
14744d3a306SSimon Glass  * returns:
14844d3a306SSimon Glass  *      0 - success
14944d3a306SSimon Glass  *      1 - failure
15044d3a306SSimon Glass  */
boot_relocate_fdt(struct lmb * lmb,char ** of_flat_tree,ulong * of_size)15144d3a306SSimon Glass int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
15244d3a306SSimon Glass {
15344d3a306SSimon Glass 	void	*fdt_blob = *of_flat_tree;
15444d3a306SSimon Glass 	void	*of_start = NULL;
15544d3a306SSimon Glass 	char	*fdt_high;
15644d3a306SSimon Glass 	ulong	of_len = 0;
15744d3a306SSimon Glass 	int	err;
15844d3a306SSimon Glass 	int	disable_relocation = 0;
15944d3a306SSimon Glass 
16044d3a306SSimon Glass 	/* nothing to do */
16144d3a306SSimon Glass 	if (*of_size == 0)
16244d3a306SSimon Glass 		return 0;
16344d3a306SSimon Glass 
16444d3a306SSimon Glass 	if (fdt_check_header(fdt_blob) != 0) {
16544d3a306SSimon Glass 		fdt_error("image is not a fdt");
16644d3a306SSimon Glass 		goto error;
16744d3a306SSimon Glass 	}
16844d3a306SSimon Glass 
16944d3a306SSimon Glass 	/* position on a 4K boundary before the alloc_current */
17044d3a306SSimon Glass 	/* Pad the FDT by a specified amount */
17144d3a306SSimon Glass 	of_len = *of_size + CONFIG_SYS_FDT_PAD;
17244d3a306SSimon Glass 
17344d3a306SSimon Glass 	/* If fdt_high is set use it to select the relocation address */
17400caae6dSSimon Glass 	fdt_high = env_get("fdt_high");
17544d3a306SSimon Glass 	if (fdt_high) {
17644d3a306SSimon Glass 		void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
17744d3a306SSimon Glass 
17844d3a306SSimon Glass 		if (((ulong) desired_addr) == ~0UL) {
17944d3a306SSimon Glass 			/* All ones means use fdt in place */
18044d3a306SSimon Glass 			of_start = fdt_blob;
18144d3a306SSimon Glass 			lmb_reserve(lmb, (ulong)of_start, of_len);
18244d3a306SSimon Glass 			disable_relocation = 1;
18344d3a306SSimon Glass 		} else if (desired_addr) {
18444d3a306SSimon Glass 			of_start =
18544d3a306SSimon Glass 			    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
18644d3a306SSimon Glass 							   (ulong)desired_addr);
18744d3a306SSimon Glass 			if (of_start == NULL) {
18844d3a306SSimon Glass 				puts("Failed using fdt_high value for Device Tree");
18944d3a306SSimon Glass 				goto error;
19044d3a306SSimon Glass 			}
19144d3a306SSimon Glass 		} else {
19244d3a306SSimon Glass 			of_start =
19344d3a306SSimon Glass 			    (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
19444d3a306SSimon Glass 		}
19544d3a306SSimon Glass 	} else {
19644d3a306SSimon Glass 		of_start =
19744d3a306SSimon Glass 		    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
198723806ccSSimon Glass 						   env_get_bootm_mapsize()
199723806ccSSimon Glass 						   + env_get_bootm_low());
20044d3a306SSimon Glass 	}
20144d3a306SSimon Glass 
20244d3a306SSimon Glass 	if (of_start == NULL) {
20344d3a306SSimon Glass 		puts("device tree - allocation error\n");
20444d3a306SSimon Glass 		goto error;
20544d3a306SSimon Glass 	}
20644d3a306SSimon Glass 
20744d3a306SSimon Glass 	if (disable_relocation) {
20844d3a306SSimon Glass 		/*
20944d3a306SSimon Glass 		 * We assume there is space after the existing fdt to use
21044d3a306SSimon Glass 		 * for padding
21144d3a306SSimon Glass 		 */
21244d3a306SSimon Glass 		fdt_set_totalsize(of_start, of_len);
21344d3a306SSimon Glass 		printf("   Using Device Tree in place at %p, end %p\n",
21444d3a306SSimon Glass 		       of_start, of_start + of_len - 1);
21544d3a306SSimon Glass 	} else {
21644d3a306SSimon Glass 		debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
21744d3a306SSimon Glass 		      fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
21844d3a306SSimon Glass 
21944d3a306SSimon Glass 		printf("   Loading Device Tree to %p, end %p ... ",
22044d3a306SSimon Glass 		       of_start, of_start + of_len - 1);
22144d3a306SSimon Glass 
22244d3a306SSimon Glass 		err = fdt_open_into(fdt_blob, of_start, of_len);
22344d3a306SSimon Glass 		if (err != 0) {
22444d3a306SSimon Glass 			fdt_error("fdt move failed");
22544d3a306SSimon Glass 			goto error;
22644d3a306SSimon Glass 		}
22744d3a306SSimon Glass 		puts("OK\n");
22844d3a306SSimon Glass 	}
22944d3a306SSimon Glass 
23044d3a306SSimon Glass 	*of_flat_tree = of_start;
23144d3a306SSimon Glass 	*of_size = of_len;
23244d3a306SSimon Glass 
233596be5f3SSimon Goldschmidt 	if (CONFIG_IS_ENABLED(CMD_FDT))
234eaac4fb2SAlexander Graf 		set_working_fdt_addr(map_to_sysmem(*of_flat_tree));
23544d3a306SSimon Glass 	return 0;
23644d3a306SSimon Glass 
23744d3a306SSimon Glass error:
23844d3a306SSimon Glass 	return 1;
23944d3a306SSimon Glass }
24044d3a306SSimon Glass 
24144d3a306SSimon Glass /**
24244d3a306SSimon Glass  * boot_get_fdt - main fdt handling routine
24344d3a306SSimon Glass  * @argc: command argument count
24444d3a306SSimon Glass  * @argv: command argument list
24553f375faSSimon Glass  * @arch: architecture (IH_ARCH_...)
24644d3a306SSimon Glass  * @images: pointer to the bootm images structure
24744d3a306SSimon Glass  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
24844d3a306SSimon Glass  * @of_size: pointer to a ulong variable, will hold fdt length
24944d3a306SSimon Glass  *
25044d3a306SSimon Glass  * boot_get_fdt() is responsible for finding a valid flat device tree image.
25144d3a306SSimon Glass  * Curently supported are the following ramdisk sources:
25244d3a306SSimon Glass  *      - multicomponent kernel/ramdisk image,
25344d3a306SSimon Glass  *      - commandline provided address of decicated ramdisk image.
25444d3a306SSimon Glass  *
25544d3a306SSimon Glass  * returns:
25644d3a306SSimon Glass  *     0, if fdt image was found and valid, or skipped
25744d3a306SSimon Glass  *     of_flat_tree and of_size are set to fdt start address and length if
25844d3a306SSimon Glass  *     fdt image is found and valid
25944d3a306SSimon Glass  *
26044d3a306SSimon Glass  *     1, if fdt image is found but corrupted
26144d3a306SSimon Glass  *     of_flat_tree and of_size are set to 0 if no fdt exists
26244d3a306SSimon Glass  */
boot_get_fdt(int flag,int argc,char * const argv[],uint8_t arch,bootm_headers_t * images,char ** of_flat_tree,ulong * of_size)26353f375faSSimon Glass int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
26444d3a306SSimon Glass 		bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
26544d3a306SSimon Glass {
26621d29f7fSHeiko Schocher #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
26744d3a306SSimon Glass 	const image_header_t *fdt_hdr;
26821d29f7fSHeiko Schocher 	ulong		load, load_end;
26921d29f7fSHeiko Schocher 	ulong		image_start, image_data, image_end;
27021d29f7fSHeiko Schocher #endif
2716a7b406aSShawn Guo 	ulong		img_addr;
27244d3a306SSimon Glass 	ulong		fdt_addr;
27344d3a306SSimon Glass 	char		*fdt_blob = NULL;
27444d3a306SSimon Glass 	void		*buf;
27573223f0eSSimon Glass #if CONFIG_IS_ENABLED(FIT)
276f320a4d8SSimon Glass 	const char	*fit_uname_config = images->fit_uname_cfg;
27744d3a306SSimon Glass 	const char	*fit_uname_fdt = NULL;
27844d3a306SSimon Glass 	ulong		default_addr;
27944d3a306SSimon Glass 	int		fdt_noffset;
28044d3a306SSimon Glass #endif
281983c72f4SSimon Glass 	const char *select = NULL;
28248aead71SSuriyan Ramasami 	int		ok_no_fdt = 0;
28344d3a306SSimon Glass 
28444d3a306SSimon Glass 	*of_flat_tree = NULL;
28544d3a306SSimon Glass 	*of_size = 0;
28644d3a306SSimon Glass 
2876a7b406aSShawn Guo 	img_addr = simple_strtoul(argv[0], NULL, 16);
2886a7b406aSShawn Guo 	buf = map_sysmem(img_addr, 0);
2896a7b406aSShawn Guo 
290983c72f4SSimon Glass 	if (argc > 2)
291983c72f4SSimon Glass 		select = argv[2];
292983c72f4SSimon Glass 	if (select || genimg_has_config(images)) {
29373223f0eSSimon Glass #if CONFIG_IS_ENABLED(FIT)
294983c72f4SSimon Glass 		if (select) {
29544d3a306SSimon Glass 			/*
29644d3a306SSimon Glass 			 * If the FDT blob comes from the FIT image and the
29744d3a306SSimon Glass 			 * FIT image address is omitted in the command line
29844d3a306SSimon Glass 			 * argument, try to use ramdisk or os FIT image
29944d3a306SSimon Glass 			 * address or default load address.
30044d3a306SSimon Glass 			 */
30144d3a306SSimon Glass 			if (images->fit_uname_rd)
30244d3a306SSimon Glass 				default_addr = (ulong)images->fit_hdr_rd;
30344d3a306SSimon Glass 			else if (images->fit_uname_os)
30444d3a306SSimon Glass 				default_addr = (ulong)images->fit_hdr_os;
30544d3a306SSimon Glass 			else
30644d3a306SSimon Glass 				default_addr = load_addr;
30744d3a306SSimon Glass 
308983c72f4SSimon Glass 			if (fit_parse_conf(select, default_addr,
30944d3a306SSimon Glass 					   &fdt_addr, &fit_uname_config)) {
31044d3a306SSimon Glass 				debug("*  fdt: config '%s' from image at 0x%08lx\n",
31144d3a306SSimon Glass 				      fit_uname_config, fdt_addr);
312983c72f4SSimon Glass 			} else if (fit_parse_subimage(select, default_addr,
31344d3a306SSimon Glass 				   &fdt_addr, &fit_uname_fdt)) {
31444d3a306SSimon Glass 				debug("*  fdt: subimage '%s' from image at 0x%08lx\n",
31544d3a306SSimon Glass 				      fit_uname_fdt, fdt_addr);
31644d3a306SSimon Glass 			} else
31744d3a306SSimon Glass #endif
31844d3a306SSimon Glass 			{
319983c72f4SSimon Glass 				fdt_addr = simple_strtoul(select, NULL, 16);
32044d3a306SSimon Glass 				debug("*  fdt: cmdline image address = 0x%08lx\n",
32144d3a306SSimon Glass 				      fdt_addr);
32244d3a306SSimon Glass 			}
32373223f0eSSimon Glass #if CONFIG_IS_ENABLED(FIT)
32444d3a306SSimon Glass 		} else {
32544d3a306SSimon Glass 			/* use FIT configuration provided in first bootm
32644d3a306SSimon Glass 			 * command argument
32744d3a306SSimon Glass 			 */
32844d3a306SSimon Glass 			fdt_addr = map_to_sysmem(images->fit_hdr_os);
32953f375faSSimon Glass 			fdt_noffset = fit_get_node_from_config(images,
33053f375faSSimon Glass 							       FIT_FDT_PROP,
33153f375faSSimon Glass 							       fdt_addr);
332bd86ef11SPaul Burton 			if (fdt_noffset == -ENOENT)
33344d3a306SSimon Glass 				return 0;
33453f375faSSimon Glass 			else if (fdt_noffset < 0)
33553f375faSSimon Glass 				return 1;
33644d3a306SSimon Glass 		}
33744d3a306SSimon Glass #endif
33844d3a306SSimon Glass 		debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
33944d3a306SSimon Glass 		      fdt_addr);
34044d3a306SSimon Glass 
34144d3a306SSimon Glass 		/*
34244d3a306SSimon Glass 		 * Check if there is an FDT image at the
34344d3a306SSimon Glass 		 * address provided in the second bootm argument
34444d3a306SSimon Glass 		 * check image type, for FIT images get a FIT node.
34544d3a306SSimon Glass 		 */
34644d3a306SSimon Glass 		buf = map_sysmem(fdt_addr, 0);
34744d3a306SSimon Glass 		switch (genimg_get_format(buf)) {
34821d29f7fSHeiko Schocher #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
34944d3a306SSimon Glass 		case IMAGE_FORMAT_LEGACY:
35044d3a306SSimon Glass 			/* verify fdt_addr points to a valid image header */
35144d3a306SSimon Glass 			printf("## Flattened Device Tree from Legacy Image at %08lx\n",
35244d3a306SSimon Glass 			       fdt_addr);
35344d3a306SSimon Glass 			fdt_hdr = image_get_fdt(fdt_addr);
35444d3a306SSimon Glass 			if (!fdt_hdr)
35548aead71SSuriyan Ramasami 				goto no_fdt;
35644d3a306SSimon Glass 
35744d3a306SSimon Glass 			/*
35844d3a306SSimon Glass 			 * move image data to the load address,
35944d3a306SSimon Glass 			 * make sure we don't overwrite initial image
36044d3a306SSimon Glass 			 */
36144d3a306SSimon Glass 			image_start = (ulong)fdt_hdr;
36244d3a306SSimon Glass 			image_data = (ulong)image_get_data(fdt_hdr);
36344d3a306SSimon Glass 			image_end = image_get_image_end(fdt_hdr);
36444d3a306SSimon Glass 
36553f375faSSimon Glass 			load = image_get_load(fdt_hdr);
36653f375faSSimon Glass 			load_end = load + image_get_data_size(fdt_hdr);
36744d3a306SSimon Glass 
36853f375faSSimon Glass 			if (load == image_start ||
36953f375faSSimon Glass 			    load == image_data) {
3702ea47be0SPeng Fan 				fdt_addr = load;
37144d3a306SSimon Glass 				break;
37244d3a306SSimon Glass 			}
37344d3a306SSimon Glass 
37453f375faSSimon Glass 			if ((load < image_end) && (load_end > image_start)) {
37544d3a306SSimon Glass 				fdt_error("fdt overwritten");
37644d3a306SSimon Glass 				goto error;
37744d3a306SSimon Glass 			}
37844d3a306SSimon Glass 
37944d3a306SSimon Glass 			debug("   Loading FDT from 0x%08lx to 0x%08lx\n",
38053f375faSSimon Glass 			      image_data, load);
38144d3a306SSimon Glass 
38253f375faSSimon Glass 			memmove((void *)load,
38344d3a306SSimon Glass 				(void *)image_data,
38444d3a306SSimon Glass 				image_get_data_size(fdt_hdr));
38544d3a306SSimon Glass 
38653f375faSSimon Glass 			fdt_addr = load;
38744d3a306SSimon Glass 			break;
38821d29f7fSHeiko Schocher #endif
38944d3a306SSimon Glass 		case IMAGE_FORMAT_FIT:
39044d3a306SSimon Glass 			/*
39144d3a306SSimon Glass 			 * This case will catch both: new uImage format
39244d3a306SSimon Glass 			 * (libfdt based) and raw FDT blob (also libfdt
39344d3a306SSimon Glass 			 * based).
39444d3a306SSimon Glass 			 */
39573223f0eSSimon Glass #if CONFIG_IS_ENABLED(FIT)
39644d3a306SSimon Glass 			/* check FDT blob vs FIT blob */
39744d3a306SSimon Glass 			if (fit_check_format(buf)) {
39853f375faSSimon Glass 				ulong load, len;
39944d3a306SSimon Glass 
400169043d8SPantelis Antoniou 				fdt_noffset = boot_get_fdt_fit(images,
40153f375faSSimon Glass 					fdt_addr, &fit_uname_fdt,
402f320a4d8SSimon Glass 					&fit_uname_config,
403169043d8SPantelis Antoniou 					arch, &load, &len);
40444d3a306SSimon Glass 
405*cc2ae64dSHongwei Zhang 				if (fdt_noffset < 0)
406*cc2ae64dSHongwei Zhang 					goto error;
407*cc2ae64dSHongwei Zhang 
40853f375faSSimon Glass 				images->fit_hdr_fdt = map_sysmem(fdt_addr, 0);
40944d3a306SSimon Glass 				images->fit_uname_fdt = fit_uname_fdt;
41044d3a306SSimon Glass 				images->fit_noffset_fdt = fdt_noffset;
41153f375faSSimon Glass 				fdt_addr = load;
412169043d8SPantelis Antoniou 
41344d3a306SSimon Glass 				break;
41444d3a306SSimon Glass 			} else
41544d3a306SSimon Glass #endif
41644d3a306SSimon Glass 			{
41744d3a306SSimon Glass 				/*
41844d3a306SSimon Glass 				 * FDT blob
41944d3a306SSimon Glass 				 */
42044d3a306SSimon Glass 				debug("*  fdt: raw FDT blob\n");
42144d3a306SSimon Glass 				printf("## Flattened Device Tree blob at %08lx\n",
42244d3a306SSimon Glass 				       (long)fdt_addr);
42344d3a306SSimon Glass 			}
42444d3a306SSimon Glass 			break;
42544d3a306SSimon Glass 		default:
42644d3a306SSimon Glass 			puts("ERROR: Did not find a cmdline Flattened Device Tree\n");
42748aead71SSuriyan Ramasami 			goto no_fdt;
42844d3a306SSimon Glass 		}
42944d3a306SSimon Glass 
43053f375faSSimon Glass 		printf("   Booting using the fdt blob at %#08lx\n", fdt_addr);
43153f375faSSimon Glass 		fdt_blob = map_sysmem(fdt_addr, 0);
43244d3a306SSimon Glass 	} else if (images->legacy_hdr_valid &&
43344d3a306SSimon Glass 			image_check_type(&images->legacy_hdr_os_copy,
43444d3a306SSimon Glass 					 IH_TYPE_MULTI)) {
43544d3a306SSimon Glass 		ulong fdt_data, fdt_len;
43644d3a306SSimon Glass 
43744d3a306SSimon Glass 		/*
43844d3a306SSimon Glass 		 * Now check if we have a legacy multi-component image,
43944d3a306SSimon Glass 		 * get second entry data start address and len.
44044d3a306SSimon Glass 		 */
44144d3a306SSimon Glass 		printf("## Flattened Device Tree from multi component Image at %08lX\n",
44244d3a306SSimon Glass 		       (ulong)images->legacy_hdr_os);
44344d3a306SSimon Glass 
44444d3a306SSimon Glass 		image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
44544d3a306SSimon Glass 				   &fdt_len);
44644d3a306SSimon Glass 		if (fdt_len) {
44744d3a306SSimon Glass 			fdt_blob = (char *)fdt_data;
44844d3a306SSimon Glass 			printf("   Booting using the fdt at 0x%p\n", fdt_blob);
44944d3a306SSimon Glass 
45044d3a306SSimon Glass 			if (fdt_check_header(fdt_blob) != 0) {
45144d3a306SSimon Glass 				fdt_error("image is not a fdt");
45244d3a306SSimon Glass 				goto error;
45344d3a306SSimon Glass 			}
45444d3a306SSimon Glass 
45544d3a306SSimon Glass 			if (fdt_totalsize(fdt_blob) != fdt_len) {
45644d3a306SSimon Glass 				fdt_error("fdt size != image size");
45744d3a306SSimon Glass 				goto error;
45844d3a306SSimon Glass 			}
45944d3a306SSimon Glass 		} else {
46044d3a306SSimon Glass 			debug("## No Flattened Device Tree\n");
46148aead71SSuriyan Ramasami 			goto no_fdt;
46244d3a306SSimon Glass 		}
4636a7b406aSShawn Guo #ifdef CONFIG_ANDROID_BOOT_IMAGE
4646a7b406aSShawn Guo 	} else if (genimg_get_format(buf) == IMAGE_FORMAT_ANDROID) {
4656a7b406aSShawn Guo 		struct andr_img_hdr *hdr = buf;
4666a7b406aSShawn Guo 		ulong fdt_data, fdt_len;
4676a7b406aSShawn Guo 
4686a7b406aSShawn Guo 		if (android_image_get_second(hdr, &fdt_data, &fdt_len) != 0)
4696a7b406aSShawn Guo 			goto no_fdt;
4706a7b406aSShawn Guo 
4716a7b406aSShawn Guo 		fdt_blob = (char *)fdt_data;
4726a7b406aSShawn Guo 		if (fdt_check_header(fdt_blob) != 0)
4736a7b406aSShawn Guo 			goto no_fdt;
4746a7b406aSShawn Guo 
4756a7b406aSShawn Guo 		if (fdt_totalsize(fdt_blob) != fdt_len)
4766a7b406aSShawn Guo 			goto error;
4776a7b406aSShawn Guo 
4786a7b406aSShawn Guo 		debug("## Using FDT found in Android image second area\n");
4796a7b406aSShawn Guo #endif
48044d3a306SSimon Glass 	} else {
48144d3a306SSimon Glass 		debug("## No Flattened Device Tree\n");
48248aead71SSuriyan Ramasami 		goto no_fdt;
48344d3a306SSimon Glass 	}
48444d3a306SSimon Glass 
48544d3a306SSimon Glass 	*of_flat_tree = fdt_blob;
48644d3a306SSimon Glass 	*of_size = fdt_totalsize(fdt_blob);
48744d3a306SSimon Glass 	debug("   of_flat_tree at 0x%08lx size 0x%08lx\n",
48844d3a306SSimon Glass 	      (ulong)*of_flat_tree, *of_size);
48944d3a306SSimon Glass 
49044d3a306SSimon Glass 	return 0;
49144d3a306SSimon Glass 
49248aead71SSuriyan Ramasami no_fdt:
49348aead71SSuriyan Ramasami 	ok_no_fdt = 1;
49444d3a306SSimon Glass error:
49544d3a306SSimon Glass 	*of_flat_tree = NULL;
49644d3a306SSimon Glass 	*of_size = 0;
49748aead71SSuriyan Ramasami 	if (!select && ok_no_fdt) {
49848aead71SSuriyan Ramasami 		debug("Continuing to boot without FDT\n");
49948aead71SSuriyan Ramasami 		return 0;
50048aead71SSuriyan Ramasami 	}
50144d3a306SSimon Glass 	return 1;
50244d3a306SSimon Glass }
50313d06981SSimon Glass 
50413d06981SSimon Glass /*
50513d06981SSimon Glass  * Verify the device tree.
50613d06981SSimon Glass  *
50713d06981SSimon Glass  * This function is called after all device tree fix-ups have been enacted,
50813d06981SSimon Glass  * so that the final device tree can be verified.  The definition of "verified"
50913d06981SSimon Glass  * is up to the specific implementation.  However, it generally means that the
51013d06981SSimon Glass  * addresses of some of the devices in the device tree are compared with the
51113d06981SSimon Glass  * actual addresses at which U-Boot has placed them.
51213d06981SSimon Glass  *
513a187559eSBin Meng  * Returns 1 on success, 0 on failure.  If 0 is returned, U-Boot will halt the
51413d06981SSimon Glass  * boot process.
51513d06981SSimon Glass  */
ft_verify_fdt(void * fdt)51613d06981SSimon Glass __weak int ft_verify_fdt(void *fdt)
51713d06981SSimon Glass {
51813d06981SSimon Glass 	return 1;
51913d06981SSimon Glass }
52013d06981SSimon Glass 
arch_fixup_fdt(void * blob)5214280342aSAlexey Brodkin __weak int arch_fixup_fdt(void *blob)
5224280342aSAlexey Brodkin {
5234280342aSAlexey Brodkin 	return 0;
5244280342aSAlexey Brodkin }
5254280342aSAlexey Brodkin 
image_setup_libfdt(bootm_headers_t * images,void * blob,int of_size,struct lmb * lmb)52613d06981SSimon Glass int image_setup_libfdt(bootm_headers_t *images, void *blob,
52713d06981SSimon Glass 		       int of_size, struct lmb *lmb)
52813d06981SSimon Glass {
52913d06981SSimon Glass 	ulong *initrd_start = &images->initrd_start;
53013d06981SSimon Glass 	ulong *initrd_end = &images->initrd_end;
5316f4dbc21SSimon Glass 	int ret = -EPERM;
5326f4dbc21SSimon Glass 	int fdt_ret;
53313d06981SSimon Glass 
53410be5b5dSPaul Kocialkowski 	if (fdt_root(blob) < 0) {
53510be5b5dSPaul Kocialkowski 		printf("ERROR: root node setup failed\n");
53610be5b5dSPaul Kocialkowski 		goto err;
53710be5b5dSPaul Kocialkowski 	}
538bc6ed0f9SMasahiro Yamada 	if (fdt_chosen(blob) < 0) {
5396f4dbc21SSimon Glass 		printf("ERROR: /chosen node create failed\n");
5406f4dbc21SSimon Glass 		goto err;
54113d06981SSimon Glass 	}
542e29607edSMa Haijun 	if (arch_fixup_fdt(blob) < 0) {
5436f4dbc21SSimon Glass 		printf("ERROR: arch-specific fdt fixup failed\n");
5446f4dbc21SSimon Glass 		goto err;
545e29607edSMa Haijun 	}
54626d61195STom Rini 	/* Update ethernet nodes */
54726d61195STom Rini 	fdt_fixup_ethernet(blob);
5486f4dbc21SSimon Glass 	if (IMAGE_OF_BOARD_SETUP) {
5496f4dbc21SSimon Glass 		fdt_ret = ft_board_setup(blob, gd->bd);
5506f4dbc21SSimon Glass 		if (fdt_ret) {
5516f4dbc21SSimon Glass 			printf("ERROR: board-specific fdt fixup failed: %s\n",
5526f4dbc21SSimon Glass 			       fdt_strerror(fdt_ret));
5536f4dbc21SSimon Glass 			goto err;
5546f4dbc21SSimon Glass 		}
5556f4dbc21SSimon Glass 	}
556c654b517SSimon Glass 	if (IMAGE_OF_SYSTEM_SETUP) {
557d89212b7SMax Krummenacher 		fdt_ret = ft_system_setup(blob, gd->bd);
558d89212b7SMax Krummenacher 		if (fdt_ret) {
559c654b517SSimon Glass 			printf("ERROR: system-specific fdt fixup failed: %s\n",
560c654b517SSimon Glass 			       fdt_strerror(fdt_ret));
561c654b517SSimon Glass 			goto err;
562c654b517SSimon Glass 		}
563c654b517SSimon Glass 	}
56413d06981SSimon Glass 
56513d06981SSimon Glass 	/* Delete the old LMB reservation */
566dea2174dSAlexander Graf 	if (lmb)
56713d06981SSimon Glass 		lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
56813d06981SSimon Glass 			 (phys_size_t)fdt_totalsize(blob));
56913d06981SSimon Glass 
570ef476836SHannes Schmelzer 	ret = fdt_shrink_to_minimum(blob, 0);
57113d06981SSimon Glass 	if (ret < 0)
5726f4dbc21SSimon Glass 		goto err;
57313d06981SSimon Glass 	of_size = ret;
57413d06981SSimon Glass 
57513d06981SSimon Glass 	if (*initrd_start && *initrd_end) {
57613d06981SSimon Glass 		of_size += FDT_RAMDISK_OVERHEAD;
57713d06981SSimon Glass 		fdt_set_totalsize(blob, of_size);
57813d06981SSimon Glass 	}
57913d06981SSimon Glass 	/* Create a new LMB reservation */
580dea2174dSAlexander Graf 	if (lmb)
58113d06981SSimon Glass 		lmb_reserve(lmb, (ulong)blob, of_size);
58213d06981SSimon Glass 
583dbe963aeSMasahiro Yamada 	fdt_initrd(blob, *initrd_start, *initrd_end);
58413d06981SSimon Glass 	if (!ft_verify_fdt(blob))
5856f4dbc21SSimon Glass 		goto err;
58613d06981SSimon Glass 
5877c387646SKhoronzhuk, Ivan #if defined(CONFIG_SOC_KEYSTONE)
58800c200f1SVitaly Andrianov 	if (IMAGE_OF_BOARD_SETUP)
58900c200f1SVitaly Andrianov 		ft_board_setup_ex(blob, gd->bd);
59000c200f1SVitaly Andrianov #endif
59100c200f1SVitaly Andrianov 
59213d06981SSimon Glass 	return 0;
5936f4dbc21SSimon Glass err:
5946f4dbc21SSimon Glass 	printf(" - must RESET the board to recover.\n\n");
5956f4dbc21SSimon Glass 
5966f4dbc21SSimon Glass 	return ret;
59713d06981SSimon Glass }
598