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