2 * Copyright (c) 2013, Google Inc.
4 * (C) Copyright 2008 Semihalf
6 * (C) Copyright 2000-2006
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <fdt_support.h>
35 #ifndef CONFIG_SYS_FDT_PAD
36 #define CONFIG_SYS_FDT_PAD 0x3000
39 DECLARE_GLOBAL_DATA_PTR;
41 static void fdt_error(const char *msg)
45 puts(" - must RESET the board to recover.\n");
48 static const image_header_t *image_get_fdt(ulong fdt_addr)
50 const image_header_t *fdt_hdr = map_sysmem(fdt_addr, 0);
52 image_print_contents(fdt_hdr);
54 puts(" Verifying Checksum ... ");
55 if (!image_check_hcrc(fdt_hdr)) {
56 fdt_error("fdt header checksum invalid");
60 if (!image_check_dcrc(fdt_hdr)) {
61 fdt_error("fdt checksum invalid");
66 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
67 fdt_error("uImage is not a fdt");
70 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
71 fdt_error("uImage is compressed");
74 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
75 fdt_error("uImage data is not a fdt");
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
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
90 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
95 if (fdt_check_header(fdt_blob) != 0)
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)
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);
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
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
119 * of_flat_tree and of_size are set to final (after relocation) values
125 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
127 void *fdt_blob = *of_flat_tree;
128 void *of_start = NULL;
132 int disable_relocation = 0;
138 if (fdt_check_header(fdt_blob) != 0) {
139 fdt_error("image is not a fdt");
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;
147 /* If fdt_high is set use it to select the relocation address */
148 fdt_high = getenv("fdt_high");
150 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
152 if (((ulong) desired_addr) == ~0UL) {
153 /* All ones means use fdt in place */
155 lmb_reserve(lmb, (ulong)of_start, of_len);
156 disable_relocation = 1;
157 } else if (desired_addr) {
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");
167 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
171 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
172 getenv_bootm_mapsize()
173 + getenv_bootm_low());
176 if (of_start == NULL) {
177 puts("device tree - allocation error\n");
181 if (disable_relocation) {
183 * We assume there is space after the existing fdt to use
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);
190 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
191 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
193 printf(" Loading Device Tree to %p, end %p ... ",
194 of_start, of_start + of_len - 1);
196 err = fdt_open_into(fdt_blob, of_start, of_len);
198 fdt_error("fdt move failed");
204 *of_flat_tree = of_start;
207 set_working_fdt_addr(*of_flat_tree);
215 * boot_get_fdt - main fdt handling routine
216 * @argc: command argument count
217 * @argv: command argument list
218 * @arch: architecture (IH_ARCH_...)
219 * @images: pointer to the bootm images structure
220 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
221 * @of_size: pointer to a ulong variable, will hold fdt length
223 * boot_get_fdt() is responsible for finding a valid flat device tree image.
224 * Curently supported are the following ramdisk sources:
225 * - multicomponent kernel/ramdisk image,
226 * - commandline provided address of decicated ramdisk image.
229 * 0, if fdt image was found and valid, or skipped
230 * of_flat_tree and of_size are set to fdt start address and length if
231 * fdt image is found and valid
233 * 1, if fdt image is found but corrupted
234 * of_flat_tree and of_size are set to 0 if no fdt exists
236 int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
237 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
239 const image_header_t *fdt_hdr;
241 char *fdt_blob = NULL;
242 ulong image_start, image_data, image_end;
243 ulong load, load_end;
245 #if defined(CONFIG_FIT)
246 const char *fit_uname_config = NULL;
247 const char *fit_uname_fdt = NULL;
252 *of_flat_tree = NULL;
255 if (argc > 3 || genimg_has_config(images)) {
256 #if defined(CONFIG_FIT)
259 * If the FDT blob comes from the FIT image and the
260 * FIT image address is omitted in the command line
261 * argument, try to use ramdisk or os FIT image
262 * address or default load address.
264 if (images->fit_uname_rd)
265 default_addr = (ulong)images->fit_hdr_rd;
266 else if (images->fit_uname_os)
267 default_addr = (ulong)images->fit_hdr_os;
269 default_addr = load_addr;
271 if (fit_parse_conf(argv[3], default_addr,
272 &fdt_addr, &fit_uname_config)) {
273 debug("* fdt: config '%s' from image at 0x%08lx\n",
274 fit_uname_config, fdt_addr);
275 } else if (fit_parse_subimage(argv[3], default_addr,
276 &fdt_addr, &fit_uname_fdt)) {
277 debug("* fdt: subimage '%s' from image at 0x%08lx\n",
278 fit_uname_fdt, fdt_addr);
282 fdt_addr = simple_strtoul(argv[3], NULL, 16);
283 debug("* fdt: cmdline image address = 0x%08lx\n",
286 #if defined(CONFIG_FIT)
288 /* use FIT configuration provided in first bootm
291 fdt_addr = map_to_sysmem(images->fit_hdr_os);
292 fdt_noffset = fit_get_node_from_config(images,
295 if (fdt_noffset == -ENOLINK)
297 else if (fdt_noffset < 0)
301 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
304 /* copy from dataflash if needed */
305 fdt_addr = genimg_get_image(fdt_addr);
308 * Check if there is an FDT image at the
309 * address provided in the second bootm argument
310 * check image type, for FIT images get a FIT node.
312 buf = map_sysmem(fdt_addr, 0);
313 switch (genimg_get_format(buf)) {
314 case IMAGE_FORMAT_LEGACY:
315 /* verify fdt_addr points to a valid image header */
316 printf("## Flattened Device Tree from Legacy Image at %08lx\n",
318 fdt_hdr = image_get_fdt(fdt_addr);
323 * move image data to the load address,
324 * make sure we don't overwrite initial image
326 image_start = (ulong)fdt_hdr;
327 image_data = (ulong)image_get_data(fdt_hdr);
328 image_end = image_get_image_end(fdt_hdr);
330 load = image_get_load(fdt_hdr);
331 load_end = load + image_get_data_size(fdt_hdr);
333 if (load == image_start ||
334 load == image_data) {
335 fdt_blob = (char *)image_data;
339 if ((load < image_end) && (load_end > image_start)) {
340 fdt_error("fdt overwritten");
344 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
347 memmove((void *)load,
349 image_get_data_size(fdt_hdr));
353 case IMAGE_FORMAT_FIT:
355 * This case will catch both: new uImage format
356 * (libfdt based) and raw FDT blob (also libfdt
359 #if defined(CONFIG_FIT)
360 /* check FDT blob vs FIT blob */
361 if (fit_check_format(buf)) {
364 fdt_noffset = fit_image_load(images,
366 fdt_addr, &fit_uname_fdt,
368 arch, IH_TYPE_FLATDT,
369 BOOTSTAGE_ID_FIT_FDT_START,
370 FIT_LOAD_OPTIONAL, &load, &len);
372 images->fit_hdr_fdt = map_sysmem(fdt_addr, 0);
373 images->fit_uname_fdt = fit_uname_fdt;
374 images->fit_noffset_fdt = fdt_noffset;
383 debug("* fdt: raw FDT blob\n");
384 printf("## Flattened Device Tree blob at %08lx\n",
389 puts("ERROR: Did not find a cmdline Flattened Device Tree\n");
393 printf(" Booting using the fdt blob at %#08lx\n", fdt_addr);
394 fdt_blob = map_sysmem(fdt_addr, 0);
395 } else if (images->legacy_hdr_valid &&
396 image_check_type(&images->legacy_hdr_os_copy,
398 ulong fdt_data, fdt_len;
401 * Now check if we have a legacy multi-component image,
402 * get second entry data start address and len.
404 printf("## Flattened Device Tree from multi component Image at %08lX\n",
405 (ulong)images->legacy_hdr_os);
407 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
410 fdt_blob = (char *)fdt_data;
411 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
413 if (fdt_check_header(fdt_blob) != 0) {
414 fdt_error("image is not a fdt");
418 if (fdt_totalsize(fdt_blob) != fdt_len) {
419 fdt_error("fdt size != image size");
423 debug("## No Flattened Device Tree\n");
427 debug("## No Flattened Device Tree\n");
431 *of_flat_tree = fdt_blob;
432 *of_size = fdt_totalsize(fdt_blob);
433 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
434 (ulong)*of_flat_tree, *of_size);
439 *of_flat_tree = NULL;
445 * Verify the device tree.
447 * This function is called after all device tree fix-ups have been enacted,
448 * so that the final device tree can be verified. The definition of "verified"
449 * is up to the specific implementation. However, it generally means that the
450 * addresses of some of the devices in the device tree are compared with the
451 * actual addresses at which U-Boot has placed them.
453 * Returns 1 on success, 0 on failure. If 0 is returned, U-boot will halt the
456 __weak int ft_verify_fdt(void *fdt)
461 __weak int arch_fixup_memory_node(void *blob)
466 int image_setup_libfdt(bootm_headers_t *images, void *blob,
467 int of_size, struct lmb *lmb)
469 ulong *initrd_start = &images->initrd_start;
470 ulong *initrd_end = &images->initrd_end;
473 if (fdt_chosen(blob, 1) < 0) {
474 puts("ERROR: /chosen node create failed");
475 puts(" - must RESET the board to recover.\n");
478 arch_fixup_memory_node(blob);
479 if (IMAAGE_OF_BOARD_SETUP)
480 ft_board_setup(blob, gd->bd);
481 fdt_fixup_ethernet(blob);
483 /* Delete the old LMB reservation */
484 lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
485 (phys_size_t)fdt_totalsize(blob));
487 ret = fdt_resize(blob);
492 if (*initrd_start && *initrd_end) {
493 of_size += FDT_RAMDISK_OVERHEAD;
494 fdt_set_totalsize(blob, of_size);
496 /* Create a new LMB reservation */
497 lmb_reserve(lmb, (ulong)blob, of_size);
499 fdt_initrd(blob, *initrd_start, *initrd_end, 1);
500 if (!ft_verify_fdt(blob))