"Das U-Boot" Source Tree
at master 2224 lines 55 kB view raw
1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * (C) Copyright 2007 4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com 5 * 6 * Copyright 2010-2011 Freescale Semiconductor, Inc. 7 */ 8 9#include <dm.h> 10#include <abuf.h> 11#include <env.h> 12#include <log.h> 13#include <mapmem.h> 14#include <net.h> 15#include <rng.h> 16#include <stdio_dev.h> 17#include <dm/device_compat.h> 18#include <dm/ofnode.h> 19#include <linux/ctype.h> 20#include <linux/types.h> 21#include <asm/global_data.h> 22#include <asm/unaligned.h> 23#include <linux/libfdt.h> 24#include <fdt_support.h> 25#include <exports.h> 26#include <fdtdec.h> 27#include <version.h> 28#include <video.h> 29 30DECLARE_GLOBAL_DATA_PTR; 31 32/** 33 * fdt_getprop_u32_default_node - Return a node's property or a default 34 * 35 * @fdt: ptr to device tree 36 * @off: offset of node 37 * @cell: cell offset in property 38 * @prop: property name 39 * @dflt: default value if the property isn't found 40 * 41 * Convenience function to return a node's property or a default value if 42 * the property doesn't exist. 43 */ 44u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell, 45 const char *prop, const u32 dflt) 46{ 47 const fdt32_t *val; 48 int len; 49 50 val = fdt_getprop(fdt, off, prop, &len); 51 52 /* Check if property exists */ 53 if (!val) 54 return dflt; 55 56 /* Check if property is long enough */ 57 if (len < ((cell + 1) * sizeof(uint32_t))) 58 return dflt; 59 60 return fdt32_to_cpu(*val); 61} 62 63/** 64 * fdt_getprop_u32_default - Find a node and return it's property or a default 65 * 66 * @fdt: ptr to device tree 67 * @path: path of node 68 * @prop: property name 69 * @dflt: default value if the property isn't found 70 * 71 * Convenience function to find a node and return it's property or a 72 * default value if it doesn't exist. 73 */ 74u32 fdt_getprop_u32_default(const void *fdt, const char *path, 75 const char *prop, const u32 dflt) 76{ 77 int off; 78 79 off = fdt_path_offset(fdt, path); 80 if (off < 0) 81 return dflt; 82 83 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt); 84} 85 86/** 87 * fdt_find_and_setprop: Find a node and set it's property 88 * 89 * @fdt: ptr to device tree 90 * @node: path of node 91 * @prop: property name 92 * @val: ptr to new value 93 * @len: length of new property value 94 * @create: flag to create the property if it doesn't exist 95 * 96 * Convenience function to directly set a property given the path to the node. 97 */ 98int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, 99 const void *val, int len, int create) 100{ 101 int nodeoff = fdt_path_offset(fdt, node); 102 103 if (nodeoff < 0) 104 return nodeoff; 105 106 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL)) 107 return 0; /* create flag not set; so exit quietly */ 108 109 return fdt_setprop(fdt, nodeoff, prop, val, len); 110} 111 112/** 113 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node 114 * 115 * @fdt: pointer to the device tree blob 116 * @parentoffset: structure block offset of a node 117 * @name: name of the subnode to locate 118 * 119 * fdt_subnode_offset() finds a subnode of the node with a given name. 120 * If the subnode does not exist, it will be created. 121 */ 122int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name) 123{ 124 int offset; 125 126 offset = fdt_subnode_offset(fdt, parentoffset, name); 127 128 if (offset == -FDT_ERR_NOTFOUND) 129 offset = fdt_add_subnode(fdt, parentoffset, name); 130 131 if (offset < 0) 132 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset)); 133 134 return offset; 135} 136 137#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX) 138static int fdt_fixup_stdout(void *fdt, int chosenoff) 139{ 140 int err; 141 int aliasoff; 142 char sername[9] = { 0 }; 143 const void *path; 144 int len; 145 char tmp[256]; /* long enough */ 146 147 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); 148 149 aliasoff = fdt_path_offset(fdt, "/aliases"); 150 if (aliasoff < 0) { 151 err = aliasoff; 152 goto noalias; 153 } 154 155 path = fdt_getprop(fdt, aliasoff, sername, &len); 156 if (!path) { 157 err = len; 158 goto noalias; 159 } 160 161 /* fdt_setprop may break "path" so we copy it to tmp buffer */ 162 memcpy(tmp, path, len); 163 164 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len); 165 if (err < 0) 166 printf("WARNING: could not set linux,stdout-path %s.\n", 167 fdt_strerror(err)); 168 169 return err; 170 171noalias: 172 printf("WARNING: %s: could not read %s alias: %s\n", 173 __func__, sername, fdt_strerror(err)); 174 175 return 0; 176} 177#else 178static int fdt_fixup_stdout(void *fdt, int chosenoff) 179{ 180 return 0; 181} 182#endif 183 184static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name, 185 uint64_t val, int is_u64) 186{ 187 if (is_u64) 188 return fdt_setprop_u64(fdt, nodeoffset, name, val); 189 else 190 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val); 191} 192 193int fdt_root(void *fdt) 194{ 195 char *serial; 196 int err; 197 198 err = fdt_check_header(fdt); 199 if (err < 0) { 200 printf("fdt_root: %s\n", fdt_strerror(err)); 201 return err; 202 } 203 204 serial = env_get("serial#"); 205 if (serial) { 206 err = fdt_setprop(fdt, 0, "serial-number", serial, 207 strlen(serial) + 1); 208 209 if (err < 0) { 210 printf("WARNING: could not set serial-number %s.\n", 211 fdt_strerror(err)); 212 return err; 213 } 214 } 215 216 return 0; 217} 218 219int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end) 220{ 221 int nodeoffset; 222 int err, j, total; 223 int is_u64; 224 uint64_t addr, size; 225 226 /* just return if the size of initrd is zero */ 227 if (initrd_start == initrd_end) 228 return 0; 229 230 /* find or create "/chosen" node. */ 231 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); 232 if (nodeoffset < 0) 233 return nodeoffset; 234 235 total = fdt_num_mem_rsv(fdt); 236 237 /* 238 * Look for an existing entry and update it. If we don't find 239 * the entry, we will j be the next available slot. 240 */ 241 for (j = 0; j < total; j++) { 242 err = fdt_get_mem_rsv(fdt, j, &addr, &size); 243 if (addr == initrd_start) { 244 fdt_del_mem_rsv(fdt, j); 245 break; 246 } 247 } 248 249 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start); 250 if (err < 0) { 251 printf("fdt_initrd: %s\n", fdt_strerror(err)); 252 return err; 253 } 254 255 is_u64 = (fdt_address_cells(fdt, 0) == 2); 256 257 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start", 258 (uint64_t)initrd_start, is_u64); 259 260 if (err < 0) { 261 printf("WARNING: could not set linux,initrd-start %s.\n", 262 fdt_strerror(err)); 263 return err; 264 } 265 266 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end", 267 (uint64_t)initrd_end, is_u64); 268 269 if (err < 0) { 270 printf("WARNING: could not set linux,initrd-end %s.\n", 271 fdt_strerror(err)); 272 273 return err; 274 } 275 276 return 0; 277} 278 279int fdt_kaslrseed(void *fdt, bool overwrite) 280{ 281 int len, err, nodeoffset; 282 struct udevice *dev; 283 const u64 *orig; 284 u64 data = 0; 285 286 err = fdt_check_header(fdt); 287 if (err < 0) 288 return err; 289 290 /* find or create "/chosen" node. */ 291 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); 292 if (nodeoffset < 0) 293 return nodeoffset; 294 295 /* return without error if we are not overwriting and existing non-zero node */ 296 orig = fdt_getprop(fdt, nodeoffset, "kaslr-seed", &len); 297 if (orig && len == sizeof(*orig)) 298 data = fdt64_to_cpu(*orig); 299 if (data && !overwrite) { 300 debug("not overwriting existing kaslr-seed\n"); 301 return 0; 302 } 303 err = uclass_get_device(UCLASS_RNG, 0, &dev); 304 if (err) { 305 printf("No RNG device\n"); 306 return err; 307 } 308 err = dm_rng_read(dev, &data, sizeof(data)); 309 if (err) { 310 dev_err(dev, "dm_rng_read failed: %d\n", err); 311 return err; 312 } 313 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", &data, sizeof(data)); 314 if (err < 0) 315 printf("WARNING: could not set kaslr-seed %s.\n", fdt_strerror(err)); 316 317 return err; 318} 319 320/** 321 * board_fdt_chosen_bootargs - boards may override this function to use 322 * alternative kernel command line arguments 323 */ 324__weak const char *board_fdt_chosen_bootargs(const struct fdt_property *fdt_ba) 325{ 326 return env_get("bootargs"); 327} 328 329int fdt_chosen(void *fdt) 330{ 331 struct abuf buf = {}; 332 int nodeoffset; 333 int err; 334 const char *str; /* used to set string properties */ 335 336 err = fdt_check_header(fdt); 337 if (err < 0) { 338 printf("fdt_chosen: %s\n", fdt_strerror(err)); 339 return err; 340 } 341 342 /* find or create "/chosen" node. */ 343 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); 344 if (nodeoffset < 0) 345 return nodeoffset; 346 347 /* if DM_RNG enabled automatically inject kaslr-seed node unless: 348 * CONFIG_MEASURED_BOOT enabled: as dt modifications break measured boot 349 * CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT enabled: as that implementation does not use dm yet 350 */ 351 if (IS_ENABLED(CONFIG_DM_RNG) && 352 !IS_ENABLED(CONFIG_MEASURED_BOOT) && 353 !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)) 354 fdt_kaslrseed(fdt, false); 355 356 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) { 357 err = fdt_setprop(fdt, nodeoffset, "rng-seed", 358 abuf_data(&buf), abuf_size(&buf)); 359 abuf_uninit(&buf); 360 if (err < 0) { 361 printf("WARNING: could not set rng-seed %s.\n", 362 fdt_strerror(err)); 363 return err; 364 } 365 } 366 367 str = board_fdt_chosen_bootargs(fdt_get_property(fdt, nodeoffset, 368 "bootargs", NULL)); 369 370 if (str) { 371 err = fdt_setprop(fdt, nodeoffset, "bootargs", str, 372 strlen(str) + 1); 373 if (err < 0) { 374 printf("WARNING: could not set bootargs %s.\n", 375 fdt_strerror(err)); 376 return err; 377 } 378 } 379 380 /* add u-boot version */ 381 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION, 382 strlen(PLAIN_VERSION) + 1); 383 if (err < 0) { 384 printf("WARNING: could not set u-boot,version %s.\n", 385 fdt_strerror(err)); 386 return err; 387 } 388 389 return fdt_fixup_stdout(fdt, nodeoffset); 390} 391 392void do_fixup_by_path(void *fdt, const char *path, const char *prop, 393 const void *val, int len, int create) 394{ 395#if defined(DEBUG) 396 int i; 397 debug("Updating property '%s/%s' = ", path, prop); 398 for (i = 0; i < len; i++) 399 debug(" %.2x", *(u8*)(val+i)); 400 debug("\n"); 401#endif 402 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); 403 if (rc) 404 printf("Unable to update property %s:%s, err=%s\n", 405 path, prop, fdt_strerror(rc)); 406} 407 408void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, 409 u32 val, int create) 410{ 411 fdt32_t tmp = cpu_to_fdt32(val); 412 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create); 413} 414 415void do_fixup_by_prop(void *fdt, 416 const char *pname, const void *pval, int plen, 417 const char *prop, const void *val, int len, 418 int create) 419{ 420 int off; 421#if defined(DEBUG) 422 int i; 423 debug("Updating property '%s' = ", prop); 424 for (i = 0; i < len; i++) 425 debug(" %.2x", *(u8*)(val+i)); 426 debug("\n"); 427#endif 428 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen); 429 while (off != -FDT_ERR_NOTFOUND) { 430 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) 431 fdt_setprop(fdt, off, prop, val, len); 432 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen); 433 } 434} 435 436void do_fixup_by_prop_u32(void *fdt, 437 const char *pname, const void *pval, int plen, 438 const char *prop, u32 val, int create) 439{ 440 fdt32_t tmp = cpu_to_fdt32(val); 441 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create); 442} 443 444void do_fixup_by_compat(void *fdt, const char *compat, 445 const char *prop, const void *val, int len, int create) 446{ 447 int off = -1; 448#if defined(DEBUG) 449 int i; 450 debug("Updating property '%s' = ", prop); 451 for (i = 0; i < len; i++) 452 debug(" %.2x", *(u8*)(val+i)); 453 debug("\n"); 454#endif 455 fdt_for_each_node_by_compatible(off, fdt, -1, compat) 456 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) 457 fdt_setprop(fdt, off, prop, val, len); 458} 459 460void do_fixup_by_compat_u32(void *fdt, const char *compat, 461 const char *prop, u32 val, int create) 462{ 463 fdt32_t tmp = cpu_to_fdt32(val); 464 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create); 465} 466 467#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY 468/* 469 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream 470 */ 471static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size, 472 int n) 473{ 474 int i; 475 int address_cells = fdt_address_cells(fdt, 0); 476 int size_cells = fdt_size_cells(fdt, 0); 477 char *p = buf; 478 479 for (i = 0; i < n; i++) { 480 if (address_cells == 2) 481 put_unaligned_be64(address[i], p); 482 else 483 *(fdt32_t *)p = cpu_to_fdt32(address[i]); 484 p += 4 * address_cells; 485 486 if (size_cells == 2) 487 put_unaligned_be64(size[i], p); 488 else 489 *(fdt32_t *)p = cpu_to_fdt32(size[i]); 490 p += 4 * size_cells; 491 } 492 493 return p - (char *)buf; 494} 495 496#if CONFIG_NR_DRAM_BANKS > 4 497#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS 498#else 499#define MEMORY_BANKS_MAX 4 500#endif 501 502/** 503 * fdt_fixup_memory_banks - Update DT memory node 504 * @blob: Pointer to DT blob 505 * @start: Pointer to memory start addresses array 506 * @size: Pointer to memory sizes array 507 * @banks: Number of memory banks 508 * 509 * Return: 0 on success, negative value on failure 510 * 511 * Based on the passed number of banks and arrays, the function is able to 512 * update existing DT memory nodes to match run time detected/changed memory 513 * configuration. Implementation is handling one specific case with only one 514 * memory node where multiple tuples could be added/updated. 515 * The case where multiple memory nodes with a single tuple (base, size) are 516 * used, this function is only updating the first memory node without removing 517 * others. 518 */ 519int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks) 520{ 521 int err, nodeoffset; 522 int len, i; 523 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */ 524 525 if (banks > MEMORY_BANKS_MAX) { 526 printf("%s: num banks %d exceeds hardcoded limit %d." 527 " Recompile with higher MEMORY_BANKS_MAX?\n", 528 __FUNCTION__, banks, MEMORY_BANKS_MAX); 529 return -1; 530 } 531 532 err = fdt_check_header(blob); 533 if (err < 0) { 534 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); 535 return err; 536 } 537 538 /* find or create "/memory" node. */ 539 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory"); 540 if (nodeoffset < 0) 541 return nodeoffset; 542 543 err = fdt_setprop(blob, nodeoffset, "device_type", "memory", 544 sizeof("memory")); 545 if (err < 0) { 546 printf("WARNING: could not set %s %s.\n", "device_type", 547 fdt_strerror(err)); 548 return err; 549 } 550 551 for (i = 0; i < banks; i++) { 552 if (start[i] == 0 && size[i] == 0) 553 break; 554 } 555 556 banks = i; 557 558 if (!banks) 559 return 0; 560 561 len = fdt_pack_reg(blob, tmp, start, size, banks); 562 563 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); 564 if (err < 0) { 565 printf("WARNING: could not set %s %s.\n", 566 "reg", fdt_strerror(err)); 567 return err; 568 } 569 return 0; 570} 571 572int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas) 573{ 574 int err, nodeoffset; 575 int len; 576 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */ 577 578 if (areas > 8) { 579 printf("%s: num areas %d exceeds hardcoded limit %d\n", 580 __func__, areas, 8); 581 return -1; 582 } 583 584 err = fdt_check_header(blob); 585 if (err < 0) { 586 printf("%s: %s\n", __func__, fdt_strerror(err)); 587 return err; 588 } 589 590 /* find or create "/memory" node. */ 591 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory"); 592 if (nodeoffset < 0) 593 return nodeoffset; 594 595 len = fdt_pack_reg(blob, tmp, start, size, areas); 596 597 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len); 598 if (err < 0) { 599 printf("WARNING: could not set %s %s.\n", 600 "reg", fdt_strerror(err)); 601 return err; 602 } 603 604 return 0; 605} 606#endif 607 608int fdt_fixup_memory(void *blob, u64 start, u64 size) 609{ 610 return fdt_fixup_memory_banks(blob, &start, &size, 1); 611} 612 613void fdt_fixup_ethernet(void *fdt) 614{ 615 int i = 0, j, prop; 616 char *tmp, *end; 617 char mac[16]; 618 const char *path; 619 unsigned char mac_addr[ARP_HLEN]; 620 int offset; 621#ifdef FDT_SEQ_MACADDR_FROM_ENV 622 int nodeoff; 623 const struct fdt_property *fdt_prop; 624#endif 625 626 if (fdt_path_offset(fdt, "/aliases") < 0) 627 return; 628 629 /* Cycle through all aliases */ 630 for (prop = 0; ; prop++) { 631 const char *name; 632 633 /* FDT might have been edited, recompute the offset */ 634 offset = fdt_first_property_offset(fdt, 635 fdt_path_offset(fdt, "/aliases")); 636 /* Select property number 'prop' */ 637 for (j = 0; j < prop; j++) 638 offset = fdt_next_property_offset(fdt, offset); 639 640 if (offset < 0) 641 break; 642 643 path = fdt_getprop_by_offset(fdt, offset, &name, NULL); 644 if (!strncmp(name, "ethernet", 8)) { 645 /* Treat plain "ethernet" same as "ethernet0". */ 646 if (!strcmp(name, "ethernet") 647#ifdef FDT_SEQ_MACADDR_FROM_ENV 648 || !strcmp(name, "ethernet0") 649#endif 650 ) 651 i = 0; 652#ifndef FDT_SEQ_MACADDR_FROM_ENV 653 else 654 i = trailing_strtol(name); 655#endif 656 if (i != -1) { 657 if (i == 0) 658 strcpy(mac, "ethaddr"); 659 else 660 sprintf(mac, "eth%daddr", i); 661 } else { 662 continue; 663 } 664#ifdef FDT_SEQ_MACADDR_FROM_ENV 665 nodeoff = fdt_path_offset(fdt, path); 666 fdt_prop = fdt_get_property(fdt, nodeoff, "status", 667 NULL); 668 if (fdt_prop && !strcmp(fdt_prop->data, "disabled")) 669 continue; 670 i++; 671#endif 672 tmp = env_get(mac); 673 if (!tmp) 674 continue; 675 676 for (j = 0; j < 6; j++) { 677 mac_addr[j] = tmp ? 678 hextoul(tmp, &end) : 0; 679 if (tmp) 680 tmp = (*end) ? end + 1 : end; 681 } 682 683 do_fixup_by_path(fdt, path, "mac-address", 684 &mac_addr, 6, 0); 685 do_fixup_by_path(fdt, path, "local-mac-address", 686 &mac_addr, 6, 1); 687 } 688 } 689} 690 691int fdt_record_loadable(void *blob, u32 index, const char *name, 692 uintptr_t load_addr, u32 size, uintptr_t entry_point, 693 const char *type, const char *os, const char *arch) 694{ 695 int err, node; 696 697 err = fdt_check_header(blob); 698 if (err < 0) { 699 printf("%s: %s\n", __func__, fdt_strerror(err)); 700 return err; 701 } 702 703 /* find or create "/fit-images" node */ 704 node = fdt_find_or_add_subnode(blob, 0, "fit-images"); 705 if (node < 0) 706 return node; 707 708 /* find or create "/fit-images/<name>" node */ 709 node = fdt_find_or_add_subnode(blob, node, name); 710 if (node < 0) 711 return node; 712 713 fdt_setprop_u64(blob, node, "load", load_addr); 714 if (entry_point != -1) 715 fdt_setprop_u64(blob, node, "entry", entry_point); 716 fdt_setprop_u32(blob, node, "size", size); 717 if (type) 718 fdt_setprop_string(blob, node, "type", type); 719 if (os) 720 fdt_setprop_string(blob, node, "os", os); 721 if (arch) 722 fdt_setprop_string(blob, node, "arch", arch); 723 724 return node; 725} 726 727int fdt_shrink_to_minimum(void *blob, uint extrasize) 728{ 729 int i; 730 uint64_t addr, size; 731 int total, ret; 732 uint actualsize; 733 int fdt_memrsv = 0; 734 735 if (!blob) 736 return 0; 737 738 total = fdt_num_mem_rsv(blob); 739 for (i = 0; i < total; i++) { 740 fdt_get_mem_rsv(blob, i, &addr, &size); 741 if (addr == (uintptr_t)blob) { 742 fdt_del_mem_rsv(blob, i); 743 fdt_memrsv = 1; 744 break; 745 } 746 } 747 748 /* 749 * Calculate the actual size of the fdt 750 * plus the size needed for 5 fdt_add_mem_rsv, one 751 * for the fdt itself and 4 for a possible initrd 752 * ((initrd-start + initrd-end) * 2 (name & value)) 753 */ 754 actualsize = fdt_off_dt_strings(blob) + 755 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry); 756 757 actualsize += extrasize; 758 /* Make it so the fdt ends on a page boundary */ 759 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000); 760 actualsize = actualsize - ((uintptr_t)blob & 0xfff); 761 762 /* Change the fdt header to reflect the correct size */ 763 fdt_set_totalsize(blob, actualsize); 764 765 if (fdt_memrsv) { 766 /* Add the new reservation */ 767 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize); 768 if (ret < 0) 769 return ret; 770 } 771 772 return actualsize; 773} 774 775/** 776 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled" 777 * 778 * @blob: ptr to device tree 779 */ 780int fdt_delete_disabled_nodes(void *blob) 781{ 782 while (1) { 783 int ret, offset; 784 785 offset = fdt_node_offset_by_prop_value(blob, -1, "status", 786 "disabled", 9); 787 if (offset < 0) 788 break; 789 790 ret = fdt_del_node(blob, offset); 791 if (ret < 0) 792 return ret; 793 } 794 795 return 0; 796} 797 798#ifdef CONFIG_PCI 799#define CFG_SYS_PCI_NR_INBOUND_WIN 4 800 801#define FDT_PCI_PREFETCH (0x40000000) 802#define FDT_PCI_MEM32 (0x02000000) 803#define FDT_PCI_IO (0x01000000) 804#define FDT_PCI_MEM64 (0x03000000) 805 806int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) { 807 808 int addrcell, sizecell, len, r; 809 u32 *dma_range; 810 /* sized based on pci addr cells, size-cells, & address-cells */ 811 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN]; 812 813 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1); 814 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1); 815 816 dma_range = &dma_ranges[0]; 817 for (r = 0; r < hose->region_count; r++) { 818 u64 bus_start, phys_start, size; 819 820 /* skip if !PCI_REGION_SYS_MEMORY */ 821 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) 822 continue; 823 824 bus_start = (u64)hose->regions[r].bus_start; 825 phys_start = (u64)hose->regions[r].phys_start; 826 size = (u64)hose->regions[r].size; 827 828 dma_range[0] = 0; 829 if (size >= 0x100000000ull) 830 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64); 831 else 832 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32); 833 if (hose->regions[r].flags & PCI_REGION_PREFETCH) 834 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH); 835#ifdef CONFIG_SYS_PCI_64BIT 836 dma_range[1] = cpu_to_fdt32(bus_start >> 32); 837#else 838 dma_range[1] = 0; 839#endif 840 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff); 841 842 if (addrcell == 2) { 843 dma_range[3] = cpu_to_fdt32(phys_start >> 32); 844 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff); 845 } else { 846 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff); 847 } 848 849 if (sizecell == 2) { 850 dma_range[3 + addrcell + 0] = 851 cpu_to_fdt32(size >> 32); 852 dma_range[3 + addrcell + 1] = 853 cpu_to_fdt32(size & 0xffffffff); 854 } else { 855 dma_range[3 + addrcell + 0] = 856 cpu_to_fdt32(size & 0xffffffff); 857 } 858 859 dma_range += (3 + addrcell + sizecell); 860 } 861 862 len = dma_range - &dma_ranges[0]; 863 if (len) 864 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4); 865 866 return 0; 867} 868#endif 869 870int fdt_increase_size(void *fdt, int add_len) 871{ 872 int newlen; 873 874 newlen = fdt_totalsize(fdt) + add_len; 875 876 /* Open in place with a new len */ 877 return fdt_open_into(fdt, fdt, newlen); 878} 879 880#ifdef CONFIG_FDT_FIXUP_PARTITIONS 881#include <jffs2/load_kernel.h> 882#include <mtd_node.h> 883 884static int fdt_del_subnodes(const void *blob, int parent_offset) 885{ 886 int off, ndepth; 887 int ret; 888 889 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth); 890 (off >= 0) && (ndepth > 0); 891 off = fdt_next_node(blob, off, &ndepth)) { 892 if (ndepth == 1) { 893 debug("delete %s: offset: %x\n", 894 fdt_get_name(blob, off, 0), off); 895 ret = fdt_del_node((void *)blob, off); 896 if (ret < 0) { 897 printf("Can't delete node: %s\n", 898 fdt_strerror(ret)); 899 return ret; 900 } else { 901 ndepth = 0; 902 off = parent_offset; 903 } 904 } 905 } 906 return 0; 907} 908 909static int fdt_del_partitions(void *blob, int parent_offset) 910{ 911 const void *prop; 912 int ndepth = 0; 913 int off; 914 int ret; 915 916 off = fdt_next_node(blob, parent_offset, &ndepth); 917 if (off > 0 && ndepth == 1) { 918 prop = fdt_getprop(blob, off, "label", NULL); 919 if (prop == NULL) { 920 /* 921 * Could not find label property, nand {}; node? 922 * Check subnode, delete partitions there if any. 923 */ 924 return fdt_del_partitions(blob, off); 925 } else { 926 ret = fdt_del_subnodes(blob, parent_offset); 927 if (ret < 0) { 928 printf("Can't remove subnodes: %s\n", 929 fdt_strerror(ret)); 930 return ret; 931 } 932 } 933 } 934 return 0; 935} 936 937static int fdt_node_set_part_info(void *blob, int parent_offset, 938 struct mtd_device *dev) 939{ 940 struct list_head *pentry; 941 struct part_info *part; 942 int off, ndepth = 0; 943 int part_num, ret; 944 int sizecell; 945 char buf[64]; 946 947 ret = fdt_del_partitions(blob, parent_offset); 948 if (ret < 0) 949 return ret; 950 951 /* 952 * Check if size/address is 1 or 2 cells. 953 * We assume #address-cells and #size-cells have same value. 954 */ 955 sizecell = fdt_getprop_u32_default_node(blob, parent_offset, 956 0, "#size-cells", 1); 957 958 /* 959 * Check if it is nand {}; subnode, adjust 960 * the offset in this case 961 */ 962 off = fdt_next_node(blob, parent_offset, &ndepth); 963 if (off > 0 && ndepth == 1) 964 parent_offset = off; 965 966 part_num = 0; 967 list_for_each_prev(pentry, &dev->parts) { 968 int newoff; 969 970 part = list_entry(pentry, struct part_info, link); 971 972 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n", 973 part_num, part->name, part->size, 974 part->offset, part->mask_flags); 975 976 sprintf(buf, "partition@%llx", part->offset); 977add_sub: 978 ret = fdt_add_subnode(blob, parent_offset, buf); 979 if (ret == -FDT_ERR_NOSPACE) { 980 ret = fdt_increase_size(blob, 512); 981 if (!ret) 982 goto add_sub; 983 else 984 goto err_size; 985 } else if (ret < 0) { 986 printf("Can't add partition node: %s\n", 987 fdt_strerror(ret)); 988 return ret; 989 } 990 newoff = ret; 991 992 /* Check MTD_WRITEABLE_CMD flag */ 993 if (part->mask_flags & 1) { 994add_ro: 995 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0); 996 if (ret == -FDT_ERR_NOSPACE) { 997 ret = fdt_increase_size(blob, 512); 998 if (!ret) 999 goto add_ro; 1000 else 1001 goto err_size; 1002 } else if (ret < 0) 1003 goto err_prop; 1004 } 1005 1006add_reg: 1007 if (sizecell == 2) { 1008 ret = fdt_setprop_u64(blob, newoff, 1009 "reg", part->offset); 1010 if (!ret) 1011 ret = fdt_appendprop_u64(blob, newoff, 1012 "reg", part->size); 1013 } else { 1014 ret = fdt_setprop_u32(blob, newoff, 1015 "reg", part->offset); 1016 if (!ret) 1017 ret = fdt_appendprop_u32(blob, newoff, 1018 "reg", part->size); 1019 } 1020 1021 if (ret == -FDT_ERR_NOSPACE) { 1022 ret = fdt_increase_size(blob, 512); 1023 if (!ret) 1024 goto add_reg; 1025 else 1026 goto err_size; 1027 } else if (ret < 0) 1028 goto err_prop; 1029 1030add_label: 1031 ret = fdt_setprop_string(blob, newoff, "label", part->name); 1032 if (ret == -FDT_ERR_NOSPACE) { 1033 ret = fdt_increase_size(blob, 512); 1034 if (!ret) 1035 goto add_label; 1036 else 1037 goto err_size; 1038 } else if (ret < 0) 1039 goto err_prop; 1040 1041 part_num++; 1042 } 1043 return 0; 1044err_size: 1045 printf("Can't increase blob size: %s\n", fdt_strerror(ret)); 1046 return ret; 1047err_prop: 1048 printf("Can't add property: %s\n", fdt_strerror(ret)); 1049 return ret; 1050} 1051 1052/* 1053 * Update partitions in nor/nand nodes using info from 1054 * mtdparts environment variable. The nodes to update are 1055 * specified by node_info structure which contains mtd device 1056 * type and compatible string: E. g. the board code in 1057 * ft_board_setup() could use: 1058 * 1059 * struct node_info nodes[] = { 1060 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, }, 1061 * { "cfi-flash", MTD_DEV_TYPE_NOR, }, 1062 * }; 1063 * 1064 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes)); 1065 */ 1066void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info, 1067 int node_info_size) 1068{ 1069 struct mtd_device *dev; 1070 int i, idx; 1071 int noff, parts; 1072 bool inited = false; 1073 1074 for (i = 0; i < node_info_size; i++) { 1075 idx = 0; 1076 1077 fdt_for_each_node_by_compatible(noff, blob, -1, 1078 node_info[i].compat) { 1079 const char *prop; 1080 1081 prop = fdt_getprop(blob, noff, "status", NULL); 1082 if (prop && !strcmp(prop, "disabled")) 1083 continue; 1084 1085 debug("%s: %s, mtd dev type %d\n", 1086 fdt_get_name(blob, noff, 0), 1087 node_info[i].compat, node_info[i].type); 1088 1089 if (!inited) { 1090 if (mtdparts_init() != 0) 1091 return; 1092 inited = true; 1093 } 1094 1095 dev = device_find(node_info[i].type, idx++); 1096 if (dev) { 1097 parts = fdt_subnode_offset(blob, noff, 1098 "partitions"); 1099 if (parts < 0) 1100 parts = noff; 1101 1102 if (fdt_node_set_part_info(blob, parts, dev)) 1103 return; /* return on error */ 1104 } 1105 } 1106 } 1107} 1108#endif 1109 1110int fdt_copy_fixed_partitions(void *blob) 1111{ 1112 ofnode node, subnode; 1113 int off, suboff, res; 1114 char path[256]; 1115 int address_cells, size_cells; 1116 u8 i, j, child_count; 1117 1118 node = ofnode_by_compatible(ofnode_null(), "fixed-partitions"); 1119 while (ofnode_valid(node)) { 1120 /* copy the U-Boot fixed partition */ 1121 address_cells = ofnode_read_simple_addr_cells(node); 1122 size_cells = ofnode_read_simple_size_cells(node); 1123 1124 res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path)); 1125 if (res) 1126 return res; 1127 1128 off = fdt_path_offset(blob, path); 1129 if (off < 0) 1130 return -ENODEV; 1131 1132 off = fdt_find_or_add_subnode(blob, off, "partitions"); 1133 res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions"); 1134 if (res) 1135 return res; 1136 1137 res = fdt_setprop_u32(blob, off, "#address-cells", address_cells); 1138 if (res) 1139 return res; 1140 1141 res = fdt_setprop_u32(blob, off, "#size-cells", size_cells); 1142 if (res) 1143 return res; 1144 1145 /* 1146 * parse partition in reverse order as fdt_find_or_add_subnode() only 1147 * insert the new node after the parent's properties 1148 */ 1149 child_count = ofnode_get_child_count(node); 1150 for (i = child_count; i > 0 ; i--) { 1151 subnode = ofnode_first_subnode(node); 1152 if (!ofnode_valid(subnode)) 1153 break; 1154 1155 for (j = 0; (j < i - 1); j++) 1156 subnode = ofnode_next_subnode(subnode); 1157 1158 if (!ofnode_valid(subnode)) 1159 break; 1160 1161 const u32 *reg; 1162 int len; 1163 1164 suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode)); 1165 res = fdt_setprop_string(blob, suboff, "label", 1166 ofnode_read_string(subnode, "label")); 1167 if (res) 1168 return res; 1169 1170 reg = ofnode_get_property(subnode, "reg", &len); 1171 res = fdt_setprop(blob, suboff, "reg", reg, len); 1172 if (res) 1173 return res; 1174 } 1175 1176 /* go to next fixed-partitions node */ 1177 node = ofnode_by_compatible(node, "fixed-partitions"); 1178 } 1179 1180 return 0; 1181} 1182 1183void fdt_del_node_and_alias(void *blob, const char *alias) 1184{ 1185 int off = fdt_path_offset(blob, alias); 1186 1187 if (off < 0) 1188 return; 1189 1190 fdt_del_node(blob, off); 1191 1192 off = fdt_path_offset(blob, "/aliases"); 1193 fdt_delprop(blob, off, alias); 1194} 1195 1196/* Max address size we deal with */ 1197#define OF_MAX_ADDR_CELLS 4 1198#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ 1199 (ns) > 0) 1200 1201/* Debug utility */ 1202#ifdef DEBUG 1203static void of_dump_addr(const char *s, const fdt32_t *addr, int na) 1204{ 1205 printf("%s", s); 1206 while(na--) 1207 printf(" %08x", *(addr++)); 1208 printf("\n"); 1209} 1210#else 1211static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { } 1212#endif 1213 1214/** 1215 * struct of_bus - Callbacks for bus specific translators 1216 * @name: A string used to identify this bus in debug output. 1217 * @addresses: The name of the DT property from which addresses are 1218 * to be read, typically "reg". 1219 * @match: Return non-zero if the node whose parent is at 1220 * parentoffset in the FDT blob corresponds to a bus 1221 * of this type, otherwise return zero. If NULL a match 1222 * is assumed. 1223 * @count_cells:Count how many cells (be32 values) a node whose parent 1224 * is at parentoffset in the FDT blob will require to 1225 * represent its address (written to *addrc) & size 1226 * (written to *sizec). 1227 * @map: Map the address addr from the address space of this 1228 * bus to that of its parent, making use of the ranges 1229 * read from DT to an array at range. na and ns are the 1230 * number of cells (be32 values) used to hold and address 1231 * or size, respectively, for this bus. pna is the number 1232 * of cells used to hold an address for the parent bus. 1233 * Returns the address in the address space of the parent 1234 * bus. 1235 * @translate: Update the value of the address cells at addr within an 1236 * FDT by adding offset to it. na specifies the number of 1237 * cells used to hold the address being translated. Returns 1238 * zero on success, non-zero on error. 1239 * 1240 * Each bus type will include a struct of_bus in the of_busses array, 1241 * providing implementations of some or all of the functions used to 1242 * match the bus & handle address translation for its children. 1243 */ 1244struct of_bus { 1245 const char *name; 1246 const char *addresses; 1247 int (*match)(const void *blob, int parentoffset); 1248 void (*count_cells)(const void *blob, int parentoffset, 1249 int *addrc, int *sizec); 1250 u64 (*map)(fdt32_t *addr, const fdt32_t *range, 1251 int na, int ns, int pna); 1252 int (*translate)(fdt32_t *addr, u64 offset, int na); 1253}; 1254 1255/* Default translator (generic bus) */ 1256void fdt_support_default_count_cells(const void *blob, int parentoffset, 1257 int *addrc, int *sizec) 1258{ 1259 const fdt32_t *prop; 1260 1261 if (addrc) 1262 *addrc = fdt_address_cells(blob, parentoffset); 1263 1264 if (sizec) { 1265 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL); 1266 if (prop) 1267 *sizec = be32_to_cpup(prop); 1268 else 1269 *sizec = 1; 1270 } 1271} 1272 1273static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range, 1274 int na, int ns, int pna) 1275{ 1276 u64 cp, s, da; 1277 1278 cp = fdt_read_number(range, na); 1279 s = fdt_read_number(range + na + pna, ns); 1280 da = fdt_read_number(addr, na); 1281 1282 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da); 1283 1284 if (da < cp || da >= (cp + s)) 1285 return OF_BAD_ADDR; 1286 return da - cp; 1287} 1288 1289static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na) 1290{ 1291 u64 a = fdt_read_number(addr, na); 1292 memset(addr, 0, na * 4); 1293 a += offset; 1294 if (na > 1) 1295 addr[na - 2] = cpu_to_fdt32(a >> 32); 1296 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu); 1297 1298 return 0; 1299} 1300 1301#ifdef CONFIG_OF_ISA_BUS 1302 1303/* ISA bus translator */ 1304static int of_bus_isa_match(const void *blob, int parentoffset) 1305{ 1306 const char *name; 1307 1308 name = fdt_get_name(blob, parentoffset, NULL); 1309 if (!name) 1310 return 0; 1311 1312 return !strcmp(name, "isa"); 1313} 1314 1315static void of_bus_isa_count_cells(const void *blob, int parentoffset, 1316 int *addrc, int *sizec) 1317{ 1318 if (addrc) 1319 *addrc = 2; 1320 if (sizec) 1321 *sizec = 1; 1322} 1323 1324static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range, 1325 int na, int ns, int pna) 1326{ 1327 u64 cp, s, da; 1328 1329 /* Check address type match */ 1330 if ((addr[0] ^ range[0]) & cpu_to_be32(1)) 1331 return OF_BAD_ADDR; 1332 1333 cp = fdt_read_number(range + 1, na - 1); 1334 s = fdt_read_number(range + na + pna, ns); 1335 da = fdt_read_number(addr + 1, na - 1); 1336 1337 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da); 1338 1339 if (da < cp || da >= (cp + s)) 1340 return OF_BAD_ADDR; 1341 return da - cp; 1342} 1343 1344static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na) 1345{ 1346 return of_bus_default_translate(addr + 1, offset, na - 1); 1347} 1348 1349#endif /* CONFIG_OF_ISA_BUS */ 1350 1351/* Array of bus specific translators */ 1352static struct of_bus of_busses[] = { 1353#ifdef CONFIG_OF_ISA_BUS 1354 /* ISA */ 1355 { 1356 .name = "isa", 1357 .addresses = "reg", 1358 .match = of_bus_isa_match, 1359 .count_cells = of_bus_isa_count_cells, 1360 .map = of_bus_isa_map, 1361 .translate = of_bus_isa_translate, 1362 }, 1363#endif /* CONFIG_OF_ISA_BUS */ 1364 /* Default */ 1365 { 1366 .name = "default", 1367 .addresses = "reg", 1368 .count_cells = fdt_support_default_count_cells, 1369 .map = of_bus_default_map, 1370 .translate = of_bus_default_translate, 1371 }, 1372}; 1373 1374static struct of_bus *of_match_bus(const void *blob, int parentoffset) 1375{ 1376 struct of_bus *bus; 1377 1378 if (ARRAY_SIZE(of_busses) == 1) 1379 return of_busses; 1380 1381 for (bus = of_busses; bus; bus++) { 1382 if (!bus->match || bus->match(blob, parentoffset)) 1383 return bus; 1384 } 1385 1386 /* 1387 * We should always have matched the default bus at least, since 1388 * it has a NULL match field. If we didn't then it somehow isn't 1389 * in the of_busses array or something equally catastrophic has 1390 * gone wrong. 1391 */ 1392 assert(0); 1393 return NULL; 1394} 1395 1396static int of_translate_one(const void *blob, int parent, struct of_bus *bus, 1397 struct of_bus *pbus, fdt32_t *addr, 1398 int na, int ns, int pna, const char *rprop) 1399{ 1400 const fdt32_t *ranges; 1401 int rlen; 1402 int rone; 1403 u64 offset = OF_BAD_ADDR; 1404 1405 /* Normally, an absence of a "ranges" property means we are 1406 * crossing a non-translatable boundary, and thus the addresses 1407 * below the current not cannot be converted to CPU physical ones. 1408 * Unfortunately, while this is very clear in the spec, it's not 1409 * what Apple understood, and they do have things like /uni-n or 1410 * /ht nodes with no "ranges" property and a lot of perfectly 1411 * useable mapped devices below them. Thus we treat the absence of 1412 * "ranges" as equivalent to an empty "ranges" property which means 1413 * a 1:1 translation at that level. It's up to the caller not to try 1414 * to translate addresses that aren't supposed to be translated in 1415 * the first place. --BenH. 1416 */ 1417 ranges = fdt_getprop(blob, parent, rprop, &rlen); 1418 if (ranges == NULL || rlen == 0) { 1419 offset = fdt_read_number(addr, na); 1420 memset(addr, 0, pna * 4); 1421 debug("OF: no ranges, 1:1 translation\n"); 1422 goto finish; 1423 } 1424 1425 debug("OF: walking ranges...\n"); 1426 1427 /* Now walk through the ranges */ 1428 rlen /= 4; 1429 rone = na + pna + ns; 1430 for (; rlen >= rone; rlen -= rone, ranges += rone) { 1431 offset = bus->map(addr, ranges, na, ns, pna); 1432 if (offset != OF_BAD_ADDR) 1433 break; 1434 } 1435 if (offset == OF_BAD_ADDR) { 1436 debug("OF: not found !\n"); 1437 return 1; 1438 } 1439 memcpy(addr, ranges + na, 4 * pna); 1440 1441 finish: 1442 of_dump_addr("OF: parent translation for:", addr, pna); 1443 debug("OF: with offset: %llu\n", offset); 1444 1445 /* Translate it into parent bus space */ 1446 return pbus->translate(addr, offset, pna); 1447} 1448 1449/* 1450 * Translate an address from the device-tree into a CPU physical address, 1451 * this walks up the tree and applies the various bus mappings on the 1452 * way. 1453 * 1454 * Note: We consider that crossing any level with #size-cells == 0 to mean 1455 * that translation is impossible (that is we are not dealing with a value 1456 * that can be mapped to a cpu physical address). This is not really specified 1457 * that way, but this is traditionally the way IBM at least do things 1458 */ 1459static u64 __of_translate_address(const void *blob, int node_offset, 1460 const fdt32_t *in_addr, const char *rprop) 1461{ 1462 int parent; 1463 struct of_bus *bus, *pbus; 1464 fdt32_t addr[OF_MAX_ADDR_CELLS]; 1465 int na, ns, pna, pns; 1466 u64 result = OF_BAD_ADDR; 1467 1468 debug("OF: ** translation for device %s **\n", 1469 fdt_get_name(blob, node_offset, NULL)); 1470 1471 /* Get parent & match bus type */ 1472 parent = fdt_parent_offset(blob, node_offset); 1473 if (parent < 0) 1474 goto bail; 1475 bus = of_match_bus(blob, parent); 1476 1477 /* Cound address cells & copy address locally */ 1478 bus->count_cells(blob, parent, &na, &ns); 1479 if (!OF_CHECK_COUNTS(na, ns)) { 1480 printf("%s: Bad cell count for %s\n", __FUNCTION__, 1481 fdt_get_name(blob, node_offset, NULL)); 1482 goto bail; 1483 } 1484 memcpy(addr, in_addr, na * 4); 1485 1486 debug("OF: bus is %s (na=%d, ns=%d) on %s\n", 1487 bus->name, na, ns, fdt_get_name(blob, parent, NULL)); 1488 of_dump_addr("OF: translating address:", addr, na); 1489 1490 /* Translate */ 1491 for (;;) { 1492 /* Switch to parent bus */ 1493 node_offset = parent; 1494 parent = fdt_parent_offset(blob, node_offset); 1495 1496 /* If root, we have finished */ 1497 if (parent < 0) { 1498 debug("OF: reached root node\n"); 1499 result = fdt_read_number(addr, na); 1500 break; 1501 } 1502 1503 /* Get new parent bus and counts */ 1504 pbus = of_match_bus(blob, parent); 1505 pbus->count_cells(blob, parent, &pna, &pns); 1506 if (!OF_CHECK_COUNTS(pna, pns)) { 1507 printf("%s: Bad cell count for %s\n", __FUNCTION__, 1508 fdt_get_name(blob, node_offset, NULL)); 1509 break; 1510 } 1511 1512 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", 1513 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL)); 1514 1515 /* Apply bus translation */ 1516 if (of_translate_one(blob, node_offset, bus, pbus, 1517 addr, na, ns, pna, rprop)) 1518 break; 1519 1520 /* Complete the move up one level */ 1521 na = pna; 1522 ns = pns; 1523 bus = pbus; 1524 1525 of_dump_addr("OF: one level translation:", addr, na); 1526 } 1527 bail: 1528 1529 return result; 1530} 1531 1532u64 fdt_translate_address(const void *blob, int node_offset, 1533 const fdt32_t *in_addr) 1534{ 1535 return __of_translate_address(blob, node_offset, in_addr, "ranges"); 1536} 1537 1538u64 fdt_translate_dma_address(const void *blob, int node_offset, 1539 const fdt32_t *in_addr) 1540{ 1541 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges"); 1542} 1543 1544int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu, 1545 dma_addr_t *bus, u64 *size) 1546{ 1547 bool found_dma_ranges = false; 1548 struct of_bus *bus_node; 1549 const fdt32_t *ranges; 1550 int na, ns, pna, pns; 1551 int parent = node; 1552 int ret = 0; 1553 int len; 1554 1555 /* Find the closest dma-ranges property */ 1556 while (parent >= 0) { 1557 ranges = fdt_getprop(blob, parent, "dma-ranges", &len); 1558 1559 /* Ignore empty ranges, they imply no translation required */ 1560 if (ranges && len > 0) 1561 break; 1562 1563 /* Once we find 'dma-ranges', then a missing one is an error */ 1564 if (found_dma_ranges && !ranges) { 1565 ret = -EINVAL; 1566 goto out; 1567 } 1568 1569 if (ranges) 1570 found_dma_ranges = true; 1571 1572 parent = fdt_parent_offset(blob, parent); 1573 } 1574 1575 if (!ranges || parent < 0) { 1576 debug("no dma-ranges found for node %s\n", 1577 fdt_get_name(blob, node, NULL)); 1578 ret = -ENOENT; 1579 goto out; 1580 } 1581 1582 /* switch to that node */ 1583 node = parent; 1584 parent = fdt_parent_offset(blob, node); 1585 if (parent < 0) { 1586 printf("Found dma-ranges in root node, shouldn't happen\n"); 1587 ret = -EINVAL; 1588 goto out; 1589 } 1590 1591 /* Get the address sizes both for the bus and its parent */ 1592 bus_node = of_match_bus(blob, node); 1593 bus_node->count_cells(blob, node, &na, &ns); 1594 if (!OF_CHECK_COUNTS(na, ns)) { 1595 printf("%s: Bad cell count for %s\n", __FUNCTION__, 1596 fdt_get_name(blob, node, NULL)); 1597 return -EINVAL; 1598 goto out; 1599 } 1600 1601 bus_node = of_match_bus(blob, parent); 1602 bus_node->count_cells(blob, parent, &pna, &pns); 1603 if (!OF_CHECK_COUNTS(pna, pns)) { 1604 printf("%s: Bad cell count for %s\n", __FUNCTION__, 1605 fdt_get_name(blob, parent, NULL)); 1606 return -EINVAL; 1607 goto out; 1608 } 1609 1610 *bus = fdt_read_number(ranges, na); 1611 *cpu = fdt_translate_dma_address(blob, node, ranges + na); 1612 *size = fdt_read_number(ranges + na + pna, ns); 1613out: 1614 return ret; 1615} 1616 1617/** 1618 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and 1619 * who's reg property matches a physical cpu address 1620 * 1621 * @blob: ptr to device tree 1622 * @compat: compatible string to match 1623 * @compat_off: property name 1624 * 1625 */ 1626int fdt_node_offset_by_compat_reg(void *blob, const char *compat, 1627 phys_addr_t compat_off) 1628{ 1629 int len, off; 1630 1631 fdt_for_each_node_by_compatible(off, blob, -1, compat) { 1632 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len); 1633 if (reg && compat_off == fdt_translate_address(blob, off, reg)) 1634 return off; 1635 } 1636 1637 return -FDT_ERR_NOTFOUND; 1638} 1639 1640static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap) 1641{ 1642 char path[512]; 1643 int len; 1644 1645 len = vsnprintf(path, sizeof(path), fmt, ap); 1646 if (len < 0 || len + 1 > sizeof(path)) 1647 return -FDT_ERR_NOSPACE; 1648 1649 return fdt_path_offset(blob, path); 1650} 1651 1652/** 1653 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path 1654 * 1655 * @blob: ptr to device tree 1656 * @fmt: path format 1657 * @ap: vsnprintf arguments 1658 */ 1659int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...) 1660{ 1661 va_list ap; 1662 int res; 1663 1664 va_start(ap, fmt); 1665 res = vnode_offset_by_pathf(blob, fmt, ap); 1666 va_end(ap); 1667 1668 return res; 1669} 1670 1671/* 1672 * fdt_set_phandle: Create a phandle property for the given node 1673 * 1674 * @fdt: ptr to device tree 1675 * @nodeoffset: node to update 1676 * @phandle: phandle value to set (must be unique) 1677 */ 1678int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle) 1679{ 1680 int ret; 1681 1682#ifdef DEBUG 1683 int off = fdt_node_offset_by_phandle(fdt, phandle); 1684 1685 if ((off >= 0) && (off != nodeoffset)) { 1686 char buf[64]; 1687 1688 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf)); 1689 printf("Trying to update node %s with phandle %u ", 1690 buf, phandle); 1691 1692 fdt_get_path(fdt, off, buf, sizeof(buf)); 1693 printf("that already exists in node %s.\n", buf); 1694 return -FDT_ERR_BADPHANDLE; 1695 } 1696#endif 1697 1698 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle); 1699 1700 return ret; 1701} 1702 1703/* 1704 * fdt_create_phandle: Get or create a phandle property for the given node 1705 * 1706 * @fdt: ptr to device tree 1707 * @nodeoffset: node to update 1708 */ 1709unsigned int fdt_create_phandle(void *fdt, int nodeoffset) 1710{ 1711 /* see if there is a phandle already */ 1712 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset); 1713 1714 /* if we got 0, means no phandle so create one */ 1715 if (phandle == 0) { 1716 int ret; 1717 1718 ret = fdt_generate_phandle(fdt, &phandle); 1719 if (ret < 0) { 1720 printf("Can't generate phandle: %s\n", 1721 fdt_strerror(ret)); 1722 return 0; 1723 } 1724 1725 ret = fdt_set_phandle(fdt, nodeoffset, phandle); 1726 if (ret < 0) { 1727 printf("Can't set phandle %u: %s\n", phandle, 1728 fdt_strerror(ret)); 1729 return 0; 1730 } 1731 } 1732 1733 return phandle; 1734} 1735 1736/** 1737 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with 1738 * given compatible 1739 * 1740 * @fdt: ptr to device tree 1741 * @compat: node's compatible string 1742 */ 1743unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat) 1744{ 1745 int offset = fdt_node_offset_by_compatible(fdt, -1, compat); 1746 1747 if (offset < 0) { 1748 printf("Can't find node with compatible \"%s\": %s\n", compat, 1749 fdt_strerror(offset)); 1750 return 0; 1751 } 1752 1753 return fdt_create_phandle(fdt, offset); 1754} 1755 1756/** 1757 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by 1758 * sprintf-formatted path 1759 * 1760 * @fdt: ptr to device tree 1761 * @fmt, ...: path format string and arguments to pass to sprintf 1762 */ 1763unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...) 1764{ 1765 va_list ap; 1766 int offset; 1767 1768 va_start(ap, fmt); 1769 offset = vnode_offset_by_pathf(fdt, fmt, ap); 1770 va_end(ap); 1771 1772 if (offset < 0) { 1773 printf("Can't find node by given path: %s\n", 1774 fdt_strerror(offset)); 1775 return 0; 1776 } 1777 1778 return fdt_create_phandle(fdt, offset); 1779} 1780 1781/* 1782 * fdt_set_node_status: Set status for the given node 1783 * 1784 * @fdt: ptr to device tree 1785 * @nodeoffset: node to update 1786 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL 1787 */ 1788int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status) 1789{ 1790 int ret = 0; 1791 1792 if (nodeoffset < 0) 1793 return nodeoffset; 1794 1795 switch (status) { 1796 case FDT_STATUS_OKAY: 1797 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay"); 1798 break; 1799 case FDT_STATUS_DISABLED: 1800 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled"); 1801 break; 1802 case FDT_STATUS_FAIL: 1803 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail"); 1804 break; 1805 default: 1806 printf("Invalid fdt status: %x\n", status); 1807 ret = -1; 1808 break; 1809 } 1810 1811 return ret; 1812} 1813 1814/* 1815 * fdt_set_status_by_alias: Set status for the given node given an alias 1816 * 1817 * @fdt: ptr to device tree 1818 * @alias: alias of node to update 1819 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL 1820 */ 1821int fdt_set_status_by_alias(void *fdt, const char* alias, 1822 enum fdt_status status) 1823{ 1824 int offset = fdt_path_offset(fdt, alias); 1825 1826 return fdt_set_node_status(fdt, offset, status); 1827} 1828 1829/** 1830 * fdt_set_status_by_compatible: Set node status for first node with given 1831 * compatible 1832 * 1833 * @fdt: ptr to device tree 1834 * @compat: node's compatible string 1835 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL 1836 */ 1837int fdt_set_status_by_compatible(void *fdt, const char *compat, 1838 enum fdt_status status) 1839{ 1840 int offset = fdt_node_offset_by_compatible(fdt, -1, compat); 1841 1842 if (offset < 0) 1843 return offset; 1844 1845 return fdt_set_node_status(fdt, offset, status); 1846} 1847 1848/** 1849 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted 1850 * path 1851 * 1852 * @fdt: ptr to device tree 1853 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL 1854 * @fmt, ...: path format string and arguments to pass to sprintf 1855 */ 1856int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt, 1857 ...) 1858{ 1859 va_list ap; 1860 int offset; 1861 1862 va_start(ap, fmt); 1863 offset = vnode_offset_by_pathf(fdt, fmt, ap); 1864 va_end(ap); 1865 1866 if (offset < 0) 1867 return offset; 1868 1869 return fdt_set_node_status(fdt, offset, status); 1870} 1871 1872/* 1873 * Verify the physical address of device tree node for a given alias 1874 * 1875 * This function locates the device tree node of a given alias, and then 1876 * verifies that the physical address of that device matches the given 1877 * parameter. It displays a message if there is a mismatch. 1878 * 1879 * Returns 1 on success, 0 on failure 1880 */ 1881int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr) 1882{ 1883 const char *path; 1884 const fdt32_t *reg; 1885 int node, len; 1886 u64 dt_addr; 1887 1888 path = fdt_getprop(fdt, anode, alias, NULL); 1889 if (!path) { 1890 /* If there's no such alias, then it's not a failure */ 1891 return 1; 1892 } 1893 1894 node = fdt_path_offset(fdt, path); 1895 if (node < 0) { 1896 printf("Warning: device tree alias '%s' points to invalid " 1897 "node %s.\n", alias, path); 1898 return 0; 1899 } 1900 1901 reg = fdt_getprop(fdt, node, "reg", &len); 1902 if (!reg) { 1903 printf("Warning: device tree node '%s' has no address.\n", 1904 path); 1905 return 0; 1906 } 1907 1908 dt_addr = fdt_translate_address(fdt, node, reg); 1909 if (addr != dt_addr) { 1910 printf("Warning: U-Boot configured device %s at address %llu,\n" 1911 "but the device tree has it address %llx.\n", 1912 alias, addr, dt_addr); 1913 return 0; 1914 } 1915 1916 return 1; 1917} 1918 1919/* 1920 * Returns the base address of an SOC or PCI node 1921 */ 1922u64 fdt_get_base_address(const void *fdt, int node) 1923{ 1924 int size; 1925 const fdt32_t *prop; 1926 1927 prop = fdt_getprop(fdt, node, "reg", &size); 1928 1929 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR; 1930} 1931 1932/* 1933 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells, 1934 * or 3 cells specially for a PCI address. 1935 */ 1936static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off, 1937 uint64_t *val, int cells) 1938{ 1939 const fdt32_t *prop32; 1940 const unaligned_fdt64_t *prop64; 1941 1942 if ((cell_off + cells) > prop_len) 1943 return -FDT_ERR_NOSPACE; 1944 1945 prop32 = &prop[cell_off]; 1946 1947 /* 1948 * Special handling for PCI address in PCI bus <ranges> 1949 * 1950 * PCI child address is made up of 3 cells. Advance the cell offset 1951 * by 1 so that the PCI child address can be correctly read. 1952 */ 1953 if (cells == 3) 1954 cell_off += 1; 1955 prop64 = (const fdt64_t *)&prop[cell_off]; 1956 1957 switch (cells) { 1958 case 1: 1959 *val = fdt32_to_cpu(*prop32); 1960 break; 1961 case 2: 1962 case 3: 1963 *val = fdt64_to_cpu(*prop64); 1964 break; 1965 default: 1966 return -FDT_ERR_NOSPACE; 1967 } 1968 1969 return 0; 1970} 1971 1972/** 1973 * fdt_read_range - Read a node's n'th range property 1974 * 1975 * @fdt: ptr to device tree 1976 * @node: offset of node 1977 * @n: range index 1978 * @child_addr: pointer to storage for the "child address" field 1979 * @addr: pointer to storage for the CPU view translated physical start 1980 * @len: pointer to storage for the range length 1981 * 1982 * Convenience function that reads and interprets a specific range out of 1983 * a number of the "ranges" property array. 1984 */ 1985int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr, 1986 uint64_t *addr, uint64_t *len) 1987{ 1988 int pnode = fdt_parent_offset(fdt, node); 1989 const fdt32_t *ranges; 1990 int pacells; 1991 int acells; 1992 int scells; 1993 int ranges_len; 1994 int cell = 0; 1995 int r = 0; 1996 1997 /* 1998 * The "ranges" property is an array of 1999 * { <child address> <parent address> <size in child address space> } 2000 * 2001 * All 3 elements can span a diffent number of cells. Fetch their size. 2002 */ 2003 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1); 2004 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1); 2005 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1); 2006 2007 /* Now try to get the ranges property */ 2008 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len); 2009 if (!ranges) 2010 return -FDT_ERR_NOTFOUND; 2011 ranges_len /= sizeof(uint32_t); 2012 2013 /* Jump to the n'th entry */ 2014 cell = n * (pacells + acells + scells); 2015 2016 /* Read <child address> */ 2017 if (child_addr) { 2018 r = fdt_read_prop(ranges, ranges_len, cell, child_addr, 2019 acells); 2020 if (r) 2021 return r; 2022 } 2023 cell += acells; 2024 2025 /* Read <parent address> */ 2026 if (addr) 2027 *addr = fdt_translate_address(fdt, node, ranges + cell); 2028 cell += pacells; 2029 2030 /* Read <size in child address space> */ 2031 if (len) { 2032 r = fdt_read_prop(ranges, ranges_len, cell, len, scells); 2033 if (r) 2034 return r; 2035 } 2036 2037 return 0; 2038} 2039 2040/** 2041 * fdt_setup_simplefb_node - Fill and enable a simplefb node 2042 * 2043 * @fdt: ptr to device tree 2044 * @node: offset of the simplefb node 2045 * @base_address: framebuffer base address 2046 * @width: width in pixels 2047 * @height: height in pixels 2048 * @stride: bytes per line 2049 * @format: pixel format string 2050 * 2051 * Convenience function to fill and enable a simplefb node. 2052 */ 2053int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width, 2054 u32 height, u32 stride, const char *format) 2055{ 2056 char name[32]; 2057 fdt32_t cells[4]; 2058 int i, addrc, sizec, ret; 2059 2060 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node), 2061 &addrc, &sizec); 2062 i = 0; 2063 if (addrc == 2) 2064 cells[i++] = cpu_to_fdt32(base_address >> 32); 2065 cells[i++] = cpu_to_fdt32(base_address); 2066 if (sizec == 2) 2067 cells[i++] = 0; 2068 cells[i++] = cpu_to_fdt32(height * stride); 2069 2070 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i); 2071 if (ret < 0) 2072 return ret; 2073 2074 snprintf(name, sizeof(name), "framebuffer@%llx", base_address); 2075 ret = fdt_set_name(fdt, node, name); 2076 if (ret < 0) 2077 return ret; 2078 2079 ret = fdt_setprop_u32(fdt, node, "width", width); 2080 if (ret < 0) 2081 return ret; 2082 2083 ret = fdt_setprop_u32(fdt, node, "height", height); 2084 if (ret < 0) 2085 return ret; 2086 2087 ret = fdt_setprop_u32(fdt, node, "stride", stride); 2088 if (ret < 0) 2089 return ret; 2090 2091 ret = fdt_setprop_string(fdt, node, "format", format); 2092 if (ret < 0) 2093 return ret; 2094 2095 ret = fdt_setprop_string(fdt, node, "status", "okay"); 2096 if (ret < 0) 2097 return ret; 2098 2099 return 0; 2100} 2101 2102#if CONFIG_IS_ENABLED(VIDEO) 2103int fdt_add_fb_mem_rsv(void *blob) 2104{ 2105 struct fdt_memory mem; 2106 2107 /* nothing to do when the frame buffer is not defined */ 2108 if (gd->video_bottom == gd->video_top) 2109 return 0; 2110 2111 /* reserved with no-map tag the video buffer */ 2112 mem.start = gd->video_bottom; 2113 mem.end = gd->video_top - 1; 2114 2115 return fdtdec_add_reserved_memory(blob, "framebuffer", &mem, NULL, 0, NULL, 2116 FDTDEC_RESERVED_MEMORY_NO_MAP); 2117} 2118#endif 2119 2120/* 2121 * Update native-mode in display-timings from display environment variable. 2122 * The node to update are specified by path. 2123 */ 2124int fdt_fixup_display(void *blob, const char *path, const char *display) 2125{ 2126 int off, toff; 2127 2128 if (!display || !path) 2129 return -FDT_ERR_NOTFOUND; 2130 2131 toff = fdt_path_offset(blob, path); 2132 if (toff >= 0) 2133 toff = fdt_subnode_offset(blob, toff, "display-timings"); 2134 if (toff < 0) 2135 return toff; 2136 2137 for (off = fdt_first_subnode(blob, toff); 2138 off >= 0; 2139 off = fdt_next_subnode(blob, off)) { 2140 uint32_t h = fdt_get_phandle(blob, off); 2141 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL), 2142 fdt32_to_cpu(h)); 2143 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0) 2144 return fdt_setprop_u32(blob, toff, "native-mode", h); 2145 } 2146 return toff; 2147} 2148 2149#ifdef CONFIG_OF_LIBFDT_OVERLAY 2150/** 2151 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting 2152 * 2153 * @fdt: ptr to device tree 2154 * @fdto: ptr to device tree overlay 2155 * 2156 * Convenience function to apply an overlay and display helpful messages 2157 * in the case of an error 2158 */ 2159int fdt_overlay_apply_verbose(void *fdt, void *fdto) 2160{ 2161 int err; 2162 bool has_symbols; 2163 2164 err = fdt_path_offset(fdt, "/__symbols__"); 2165 has_symbols = err >= 0; 2166 2167 err = fdt_overlay_apply(fdt, fdto); 2168 if (err < 0) { 2169 printf("failed on fdt_overlay_apply(): %s\n", 2170 fdt_strerror(err)); 2171 if (!has_symbols) { 2172 printf("base fdt does not have a /__symbols__ node\n"); 2173 printf("make sure you've compiled with -@\n"); 2174 } 2175 } 2176 return err; 2177} 2178#endif 2179 2180/** 2181 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL 2182 * 2183 * @blobp: Pointer to FDT pointer 2184 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL) 2185 */ 2186int fdt_valid(struct fdt_header **blobp) 2187{ 2188 const void *blob = *blobp; 2189 int err; 2190 2191 if (!blob) { 2192 printf("The address of the fdt is invalid (NULL).\n"); 2193 return 0; 2194 } 2195 2196 err = fdt_check_header(blob); 2197 if (err == 0) 2198 return 1; /* valid */ 2199 2200 if (err < 0) { 2201 printf("libfdt fdt_check_header(): %s", fdt_strerror(err)); 2202 /* 2203 * Be more informative on bad version. 2204 */ 2205 if (err == -FDT_ERR_BADVERSION) { 2206 if (fdt_version(blob) < 2207 FDT_FIRST_SUPPORTED_VERSION) { 2208 printf(" - too old, fdt %d < %d", 2209 fdt_version(blob), 2210 FDT_FIRST_SUPPORTED_VERSION); 2211 } 2212 if (fdt_last_comp_version(blob) > 2213 FDT_LAST_SUPPORTED_VERSION) { 2214 printf(" - too new, fdt %d > %d", 2215 fdt_version(blob), 2216 FDT_LAST_SUPPORTED_VERSION); 2217 } 2218 } 2219 printf("\n"); 2220 *blobp = NULL; 2221 return 0; 2222 } 2223 return 1; 2224}