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