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