1#ifndef LIBFDT_H 2#define LIBFDT_H 3/* 4 * libfdt - Flat Device Tree manipulation 5 * Copyright (C) 2006 David Gibson, IBM Corporation. 6 * 7 * libfdt is dual licensed: you can use it either under the terms of 8 * the GPL, or the BSD license, at your option. 9 * 10 * a) This library is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of the 13 * License, or (at your option) any later version. 14 * 15 * This library is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public 21 * License along with this library; if not, write to the Free 22 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 23 * MA 02110-1301 USA 24 * 25 * Alternatively, 26 * 27 * b) Redistribution and use in source and binary forms, with or 28 * without modification, are permitted provided that the following 29 * conditions are met: 30 * 31 * 1. Redistributions of source code must retain the above 32 * copyright notice, this list of conditions and the following 33 * disclaimer. 34 * 2. Redistributions in binary form must reproduce the above 35 * copyright notice, this list of conditions and the following 36 * disclaimer in the documentation and/or other materials 37 * provided with the distribution. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 40 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 44 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 49 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 50 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 51 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 52 */ 53 54#include "libfdt_env.h" 55#include "fdt.h" 56 57#define FDT_FIRST_SUPPORTED_VERSION 0x02 58#define FDT_LAST_SUPPORTED_VERSION 0x11 59 60/* Error codes: informative error codes */ 61#define FDT_ERR_NOTFOUND 1 62 /* FDT_ERR_NOTFOUND: The requested node or property does not exist */ 63#define FDT_ERR_EXISTS 2 64 /* FDT_ERR_EXISTS: Attempted to create a node or property which 65 * already exists */ 66#define FDT_ERR_NOSPACE 3 67 /* FDT_ERR_NOSPACE: Operation needed to expand the device 68 * tree, but its buffer did not have sufficient space to 69 * contain the expanded tree. Use fdt_open_into() to move the 70 * device tree to a buffer with more space. */ 71 72/* Error codes: codes for bad parameters */ 73#define FDT_ERR_BADOFFSET 4 74 /* FDT_ERR_BADOFFSET: Function was passed a structure block 75 * offset which is out-of-bounds, or which points to an 76 * unsuitable part of the structure for the operation. */ 77#define FDT_ERR_BADPATH 5 78 /* FDT_ERR_BADPATH: Function was passed a badly formatted path 79 * (e.g. missing a leading / for a function which requires an 80 * absolute path) */ 81#define FDT_ERR_BADPHANDLE 6 82 /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle. 83 * This can be caused either by an invalid phandle property 84 * length, or the phandle value was either 0 or -1, which are 85 * not permitted. */ 86#define FDT_ERR_BADSTATE 7 87 /* FDT_ERR_BADSTATE: Function was passed an incomplete device 88 * tree created by the sequential-write functions, which is 89 * not sufficiently complete for the requested operation. */ 90 91/* Error codes: codes for bad device tree blobs */ 92#define FDT_ERR_TRUNCATED 8 93 /* FDT_ERR_TRUNCATED: FDT or a sub-block is improperly 94 * terminated (overflows, goes outside allowed bounds, or 95 * isn't properly terminated). */ 96#define FDT_ERR_BADMAGIC 9 97 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a 98 * device tree at all - it is missing the flattened device 99 * tree magic number. */ 100#define FDT_ERR_BADVERSION 10 101 /* FDT_ERR_BADVERSION: Given device tree has a version which 102 * can't be handled by the requested operation. For 103 * read-write functions, this may mean that fdt_open_into() is 104 * required to convert the tree to the expected version. */ 105#define FDT_ERR_BADSTRUCTURE 11 106 /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt 107 * structure block or other serious error (e.g. misnested 108 * nodes, or subnodes preceding properties). */ 109#define FDT_ERR_BADLAYOUT 12 110 /* FDT_ERR_BADLAYOUT: For read-write functions, the given 111 * device tree has it's sub-blocks in an order that the 112 * function can't handle (memory reserve map, then structure, 113 * then strings). Use fdt_open_into() to reorganize the tree 114 * into a form suitable for the read-write operations. */ 115 116/* "Can't happen" error indicating a bug in libfdt */ 117#define FDT_ERR_INTERNAL 13 118 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion. 119 * Should never be returned, if it is, it indicates a bug in 120 * libfdt itself. */ 121 122/* Errors in device tree content */ 123#define FDT_ERR_BADNCELLS 14 124 /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells 125 * or similar property with a bad format or value */ 126 127#define FDT_ERR_BADVALUE 15 128 /* FDT_ERR_BADVALUE: Device tree has a property with an unexpected 129 * value. For example: a property expected to contain a string list 130 * is not NUL-terminated within the length of its value. */ 131 132#define FDT_ERR_BADOVERLAY 16 133 /* FDT_ERR_BADOVERLAY: The device tree overlay, while 134 * correctly structured, cannot be applied due to some 135 * unexpected or missing value, property or node. */ 136 137#define FDT_ERR_NOPHANDLES 17 138 /* FDT_ERR_NOPHANDLES: The device tree doesn't have any 139 * phandle available anymore without causing an overflow */ 140 141#define FDT_ERR_MAX 17 142 143/**********************************************************************/ 144/* Low-level functions (you probably don't need these) */ 145/**********************************************************************/ 146 147#ifndef SWIG /* This function is not useful in Python */ 148const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen); 149#endif 150static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) 151{ 152 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen); 153} 154 155uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset); 156 157/* 158 * Alignment helpers: 159 * These helpers access words from a device tree blob. They're 160 * built to work even with unaligned pointers on platforms (ike 161 * ARM) that don't like unaligned loads and stores 162 */ 163 164static inline uint32_t fdt32_ld(const fdt32_t *p) 165{ 166 fdt32_t v; 167 168 memcpy(&v, p, sizeof(v)); 169 return fdt32_to_cpu(v); 170} 171 172static inline uint64_t fdt64_ld(const fdt64_t *p) 173{ 174 fdt64_t v; 175 176 memcpy(&v, p, sizeof(v)); 177 return fdt64_to_cpu(v); 178} 179 180/**********************************************************************/ 181/* Traversal functions */ 182/**********************************************************************/ 183 184int fdt_next_node(const void *fdt, int offset, int *depth); 185 186/** 187 * fdt_first_subnode() - get offset of first direct subnode 188 * 189 * @fdt: FDT blob 190 * @offset: Offset of node to check 191 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none 192 */ 193int fdt_first_subnode(const void *fdt, int offset); 194 195/** 196 * fdt_next_subnode() - get offset of next direct subnode 197 * 198 * After first calling fdt_first_subnode(), call this function repeatedly to 199 * get direct subnodes of a parent node. 200 * 201 * @fdt: FDT blob 202 * @offset: Offset of previous subnode 203 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more 204 * subnodes 205 */ 206int fdt_next_subnode(const void *fdt, int offset); 207 208/** 209 * fdt_for_each_subnode - iterate over all subnodes of a parent 210 * 211 * @node: child node (int, lvalue) 212 * @fdt: FDT blob (const void *) 213 * @parent: parent node (int) 214 * 215 * This is actually a wrapper around a for loop and would be used like so: 216 * 217 * fdt_for_each_subnode(node, fdt, parent) { 218 * Use node 219 * ... 220 * } 221 * 222 * if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) { 223 * Error handling 224 * } 225 * 226 * Note that this is implemented as a macro and @node is used as 227 * iterator in the loop. The parent variable be constant or even a 228 * literal. 229 * 230 */ 231#define fdt_for_each_subnode(node, fdt, parent) \ 232 for (node = fdt_first_subnode(fdt, parent); \ 233 node >= 0; \ 234 node = fdt_next_subnode(fdt, node)) 235 236/**********************************************************************/ 237/* General functions */ 238/**********************************************************************/ 239#define fdt_get_header(fdt, field) \ 240 (fdt32_ld(&((const struct fdt_header *)(fdt))->field)) 241#define fdt_magic(fdt) (fdt_get_header(fdt, magic)) 242#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize)) 243#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct)) 244#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings)) 245#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap)) 246#define fdt_version(fdt) (fdt_get_header(fdt, version)) 247#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version)) 248#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys)) 249#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings)) 250#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct)) 251 252#define fdt_set_hdr_(name) \ 253 static inline void fdt_set_##name(void *fdt, uint32_t val) \ 254 { \ 255 struct fdt_header *fdth = (struct fdt_header *)fdt; \ 256 fdth->name = cpu_to_fdt32(val); \ 257 } 258fdt_set_hdr_(magic); 259fdt_set_hdr_(totalsize); 260fdt_set_hdr_(off_dt_struct); 261fdt_set_hdr_(off_dt_strings); 262fdt_set_hdr_(off_mem_rsvmap); 263fdt_set_hdr_(version); 264fdt_set_hdr_(last_comp_version); 265fdt_set_hdr_(boot_cpuid_phys); 266fdt_set_hdr_(size_dt_strings); 267fdt_set_hdr_(size_dt_struct); 268#undef fdt_set_hdr_ 269enum display_mode { 270 MODE_SHOW_VALUE, /* show values for node properties */ 271 MODE_LIST_PROPS, /* list the properties for a node */ 272 MODE_LIST_SUBNODES, /* list the subnodes of a node */ 273}; 274 275/* Holds information which controls our output and options */ 276struct display_info { 277 int type; /* data type (s/i/u/x or 0 for default) */ 278 int size; /* data size (1/2/4) */ 279 enum display_mode mode; /* display mode that we are using */ 280 const char *default_val; /* default value if node/property not found */ 281}; 282 283/** 284 * fdt_header_size - return the size of the tree's header 285 * @fdt: pointer to a flattened device tree 286 */ 287size_t fdt_header_size_(uint32_t version); 288static inline size_t fdt_header_size(const void *fdt) 289{ 290 return fdt_header_size_(fdt_version(fdt)); 291} 292 293/** 294 * fdt_check_header - sanity check a device tree header 295 296 * @fdt: pointer to data which might be a flattened device tree 297 * 298 * fdt_check_header() checks that the given buffer contains what 299 * appears to be a flattened device tree, and that the header contains 300 * valid information (to the extent that can be determined from the 301 * header alone). 302 * 303 * returns: 304 * 0, if the buffer appears to contain a valid device tree 305 * -FDT_ERR_BADMAGIC, 306 * -FDT_ERR_BADVERSION, 307 * -FDT_ERR_BADSTATE, 308 * -FDT_ERR_TRUNCATED, standard meanings, as above 309 */ 310int fdt_check_header(const void *fdt); 311 312/** 313 * fdt_move - move a device tree around in memory 314 * @fdt: pointer to the device tree to move 315 * @buf: pointer to memory where the device is to be moved 316 * @bufsize: size of the memory space at buf 317 * 318 * fdt_move() relocates, if possible, the device tree blob located at 319 * fdt to the buffer at buf of size bufsize. The buffer may overlap 320 * with the existing device tree blob at fdt. Therefore, 321 * fdt_move(fdt, fdt, fdt_totalsize(fdt)) 322 * should always succeed. 323 * 324 * returns: 325 * 0, on success 326 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree 327 * -FDT_ERR_BADMAGIC, 328 * -FDT_ERR_BADVERSION, 329 * -FDT_ERR_BADSTATE, standard meanings 330 */ 331int fdt_move(const void *fdt, void *buf, int bufsize); 332 333/**********************************************************************/ 334/* Read-only functions */ 335/**********************************************************************/ 336 337int fdt_check_full(const void *fdt, size_t bufsize); 338 339/** 340 * fdt_get_string - retrieve a string from the strings block of a device tree 341 * @fdt: pointer to the device tree blob 342 * @stroffset: offset of the string within the strings block (native endian) 343 * @lenp: optional pointer to return the string's length 344 * 345 * fdt_get_string() retrieves a pointer to a single string from the 346 * strings block of the device tree blob at fdt, and optionally also 347 * returns the string's length in *lenp. 348 * 349 * returns: 350 * a pointer to the string, on success 351 * NULL, if stroffset is out of bounds, or doesn't point to a valid string 352 */ 353const char *fdt_get_string(const void *fdt, int stroffset, int *lenp); 354 355/** 356 * fdt_string - retrieve a string from the strings block of a device tree 357 * @fdt: pointer to the device tree blob 358 * @stroffset: offset of the string within the strings block (native endian) 359 * 360 * fdt_string() retrieves a pointer to a single string from the 361 * strings block of the device tree blob at fdt. 362 * 363 * returns: 364 * a pointer to the string, on success 365 * NULL, if stroffset is out of bounds, or doesn't point to a valid string 366 */ 367const char *fdt_string(const void *fdt, int stroffset); 368 369/** 370 * fdt_get_max_phandle - retrieves the highest phandle in a tree 371 * @fdt: pointer to the device tree blob 372 * 373 * fdt_get_max_phandle retrieves the highest phandle in the given 374 * device tree. This will ignore badly formatted phandles, or phandles 375 * with a value of 0 or -1. 376 * 377 * returns: 378 * the highest phandle on success 379 * 0, if no phandle was found in the device tree 380 * -1, if an error occurred 381 */ 382uint32_t fdt_get_max_phandle(const void *fdt); 383 384/** 385 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries 386 * @fdt: pointer to the device tree blob 387 * 388 * Returns the number of entries in the device tree blob's memory 389 * reservation map. This does not include the terminating 0,0 entry 390 * or any other (0,0) entries reserved for expansion. 391 * 392 * returns: 393 * the number of entries 394 */ 395int fdt_num_mem_rsv(const void *fdt); 396 397/** 398 * fdt_get_mem_rsv - retrieve one memory reserve map entry 399 * @fdt: pointer to the device tree blob 400 * @address, @size: pointers to 64-bit variables 401 * 402 * On success, *address and *size will contain the address and size of 403 * the n-th reserve map entry from the device tree blob, in 404 * native-endian format. 405 * 406 * returns: 407 * 0, on success 408 * -FDT_ERR_BADMAGIC, 409 * -FDT_ERR_BADVERSION, 410 * -FDT_ERR_BADSTATE, standard meanings 411 */ 412int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size); 413 414/** 415 * fdt_subnode_offset_namelen - find a subnode based on substring 416 * @fdt: pointer to the device tree blob 417 * @parentoffset: structure block offset of a node 418 * @name: name of the subnode to locate 419 * @namelen: number of characters of name to consider 420 * 421 * Identical to fdt_subnode_offset(), but only examine the first 422 * namelen characters of name for matching the subnode name. This is 423 * useful for finding subnodes based on a portion of a larger string, 424 * such as a full path. 425 */ 426#ifndef SWIG /* Not available in Python */ 427int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, 428 const char *name, int namelen); 429#endif 430/** 431 * fdt_subnode_offset - find a subnode of a given node 432 * @fdt: pointer to the device tree blob 433 * @parentoffset: structure block offset of a node 434 * @name: name of the subnode to locate 435 * 436 * fdt_subnode_offset() finds a subnode of the node at structure block 437 * offset parentoffset with the given name. name may include a unit 438 * address, in which case fdt_subnode_offset() will find the subnode 439 * with that unit address, or the unit address may be omitted, in 440 * which case fdt_subnode_offset() will find an arbitrary subnode 441 * whose name excluding unit address matches the given name. 442 * 443 * returns: 444 * structure block offset of the requested subnode (>=0), on success 445 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist 446 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE 447 * tag 448 * -FDT_ERR_BADMAGIC, 449 * -FDT_ERR_BADVERSION, 450 * -FDT_ERR_BADSTATE, 451 * -FDT_ERR_BADSTRUCTURE, 452 * -FDT_ERR_TRUNCATED, standard meanings. 453 */ 454int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name); 455 456/** 457 * fdt_path_offset_namelen - find a tree node by its full path 458 * @fdt: pointer to the device tree blob 459 * @path: full path of the node to locate 460 * @namelen: number of characters of path to consider 461 * 462 * Identical to fdt_path_offset(), but only consider the first namelen 463 * characters of path as the path name. 464 */ 465#ifndef SWIG /* Not available in Python */ 466int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen); 467#endif 468 469/** 470 * fdt_path_offset - find a tree node by its full path 471 * @fdt: pointer to the device tree blob 472 * @path: full path of the node to locate 473 * 474 * fdt_path_offset() finds a node of a given path in the device tree. 475 * Each path component may omit the unit address portion, but the 476 * results of this are undefined if any such path component is 477 * ambiguous (that is if there are multiple nodes at the relevant 478 * level matching the given component, differentiated only by unit 479 * address). 480 * 481 * returns: 482 * structure block offset of the node with the requested path (>=0), on 483 * success 484 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid 485 * -FDT_ERR_NOTFOUND, if the requested node does not exist 486 * -FDT_ERR_BADMAGIC, 487 * -FDT_ERR_BADVERSION, 488 * -FDT_ERR_BADSTATE, 489 * -FDT_ERR_BADSTRUCTURE, 490 * -FDT_ERR_TRUNCATED, standard meanings. 491 */ 492int fdt_path_offset(const void *fdt, const char *path); 493 494/** 495 * fdt_get_name - retrieve the name of a given node 496 * @fdt: pointer to the device tree blob 497 * @nodeoffset: structure block offset of the starting node 498 * @lenp: pointer to an integer variable (will be overwritten) or NULL 499 * 500 * fdt_get_name() retrieves the name (including unit address) of the 501 * device tree node at structure block offset nodeoffset. If lenp is 502 * non-NULL, the length of this name is also returned, in the integer 503 * pointed to by lenp. 504 * 505 * returns: 506 * pointer to the node's name, on success 507 * If lenp is non-NULL, *lenp contains the length of that name 508 * (>=0) 509 * NULL, on error 510 * if lenp is non-NULL *lenp contains an error code (<0): 511 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE 512 * tag 513 * -FDT_ERR_BADMAGIC, 514 * -FDT_ERR_BADVERSION, 515 * -FDT_ERR_BADSTATE, standard meanings 516 */ 517const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp); 518 519/** 520 * fdt_first_property_offset - find the offset of a node's first property 521 * @fdt: pointer to the device tree blob 522 * @nodeoffset: structure block offset of a node 523 * 524 * fdt_first_property_offset() finds the first property of the node at 525 * the given structure block offset. 526 * 527 * returns: 528 * structure block offset of the property (>=0), on success 529 * -FDT_ERR_NOTFOUND, if the requested node has no properties 530 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag 531 * -FDT_ERR_BADMAGIC, 532 * -FDT_ERR_BADVERSION, 533 * -FDT_ERR_BADSTATE, 534 * -FDT_ERR_BADSTRUCTURE, 535 * -FDT_ERR_TRUNCATED, standard meanings. 536 */ 537int fdt_first_property_offset(const void *fdt, int nodeoffset); 538 539/** 540 * fdt_next_property_offset - step through a node's properties 541 * @fdt: pointer to the device tree blob 542 * @offset: structure block offset of a property 543 * 544 * fdt_next_property_offset() finds the property immediately after the 545 * one at the given structure block offset. This will be a property 546 * of the same node as the given property. 547 * 548 * returns: 549 * structure block offset of the next property (>=0), on success 550 * -FDT_ERR_NOTFOUND, if the given property is the last in its node 551 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag 552 * -FDT_ERR_BADMAGIC, 553 * -FDT_ERR_BADVERSION, 554 * -FDT_ERR_BADSTATE, 555 * -FDT_ERR_BADSTRUCTURE, 556 * -FDT_ERR_TRUNCATED, standard meanings. 557 */ 558int fdt_next_property_offset(const void *fdt, int offset); 559 560/** 561 * fdt_for_each_property_offset - iterate over all properties of a node 562 * 563 * @property_offset: property offset (int, lvalue) 564 * @fdt: FDT blob (const void *) 565 * @node: node offset (int) 566 * 567 * This is actually a wrapper around a for loop and would be used like so: 568 * 569 * fdt_for_each_property_offset(property, fdt, node) { 570 * Use property 571 * ... 572 * } 573 * 574 * if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) { 575 * Error handling 576 * } 577 * 578 * Note that this is implemented as a macro and property is used as 579 * iterator in the loop. The node variable can be constant or even a 580 * literal. 581 */ 582#define fdt_for_each_property_offset(property, fdt, node) \ 583 for (property = fdt_first_property_offset(fdt, node); \ 584 property >= 0; \ 585 property = fdt_next_property_offset(fdt, property)) 586 587/** 588 * fdt_get_property_by_offset - retrieve the property at a given offset 589 * @fdt: pointer to the device tree blob 590 * @offset: offset of the property to retrieve 591 * @lenp: pointer to an integer variable (will be overwritten) or NULL 592 * 593 * fdt_get_property_by_offset() retrieves a pointer to the 594 * fdt_property structure within the device tree blob at the given 595 * offset. If lenp is non-NULL, the length of the property value is 596 * also returned, in the integer pointed to by lenp. 597 * 598 * Note that this code only works on device tree versions >= 16. fdt_getprop() 599 * works on all versions. 600 * 601 * returns: 602 * pointer to the structure representing the property 603 * if lenp is non-NULL, *lenp contains the length of the property 604 * value (>=0) 605 * NULL, on error 606 * if lenp is non-NULL, *lenp contains an error code (<0): 607 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag 608 * -FDT_ERR_BADMAGIC, 609 * -FDT_ERR_BADVERSION, 610 * -FDT_ERR_BADSTATE, 611 * -FDT_ERR_BADSTRUCTURE, 612 * -FDT_ERR_TRUNCATED, standard meanings 613 */ 614const struct fdt_property *fdt_get_property_by_offset(const void *fdt, 615 int offset, 616 int *lenp); 617 618/** 619 * fdt_get_property_namelen - find a property based on substring 620 * @fdt: pointer to the device tree blob 621 * @nodeoffset: offset of the node whose property to find 622 * @name: name of the property to find 623 * @namelen: number of characters of name to consider 624 * @lenp: pointer to an integer variable (will be overwritten) or NULL 625 * 626 * Identical to fdt_get_property(), but only examine the first namelen 627 * characters of name for matching the property name. 628 */ 629#ifndef SWIG /* Not available in Python */ 630const struct fdt_property *fdt_get_property_namelen(const void *fdt, 631 int nodeoffset, 632 const char *name, 633 int namelen, int *lenp); 634#endif 635 636/** 637 * fdt_get_property - find a given property in a given node 638 * @fdt: pointer to the device tree blob 639 * @nodeoffset: offset of the node whose property to find 640 * @name: name of the property to find 641 * @lenp: pointer to an integer variable (will be overwritten) or NULL 642 * 643 * fdt_get_property() retrieves a pointer to the fdt_property 644 * structure within the device tree blob corresponding to the property 645 * named 'name' of the node at offset nodeoffset. If lenp is 646 * non-NULL, the length of the property value is also returned, in the 647 * integer pointed to by lenp. 648 * 649 * returns: 650 * pointer to the structure representing the property 651 * if lenp is non-NULL, *lenp contains the length of the property 652 * value (>=0) 653 * NULL, on error 654 * if lenp is non-NULL, *lenp contains an error code (<0): 655 * -FDT_ERR_NOTFOUND, node does not have named property 656 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE 657 * tag 658 * -FDT_ERR_BADMAGIC, 659 * -FDT_ERR_BADVERSION, 660 * -FDT_ERR_BADSTATE, 661 * -FDT_ERR_BADSTRUCTURE, 662 * -FDT_ERR_TRUNCATED, standard meanings 663 */ 664const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset, 665 const char *name, int *lenp); 666static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset, 667 const char *name, 668 int *lenp) 669{ 670 return (struct fdt_property *)(uintptr_t) 671 fdt_get_property(fdt, nodeoffset, name, lenp); 672} 673 674/** 675 * fdt_getprop_by_offset - retrieve the value of a property at a given offset 676 * @fdt: pointer to the device tree blob 677 * @ffset: offset of the property to read 678 * @namep: pointer to a string variable (will be overwritten) or NULL 679 * @lenp: pointer to an integer variable (will be overwritten) or NULL 680 * 681 * fdt_getprop_by_offset() retrieves a pointer to the value of the 682 * property at structure block offset 'offset' (this will be a pointer 683 * to within the device blob itself, not a copy of the value). If 684 * lenp is non-NULL, the length of the property value is also 685 * returned, in the integer pointed to by lenp. If namep is non-NULL, 686 * the property's namne will also be returned in the char * pointed to 687 * by namep (this will be a pointer to within the device tree's string 688 * block, not a new copy of the name). 689 * 690 * returns: 691 * pointer to the property's value 692 * if lenp is non-NULL, *lenp contains the length of the property 693 * value (>=0) 694 * if namep is non-NULL *namep contiains a pointer to the property 695 * name. 696 * NULL, on error 697 * if lenp is non-NULL, *lenp contains an error code (<0): 698 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag 699 * -FDT_ERR_BADMAGIC, 700 * -FDT_ERR_BADVERSION, 701 * -FDT_ERR_BADSTATE, 702 * -FDT_ERR_BADSTRUCTURE, 703 * -FDT_ERR_TRUNCATED, standard meanings 704 */ 705#ifndef SWIG /* This function is not useful in Python */ 706const void *fdt_getprop_by_offset(const void *fdt, int offset, 707 const char **namep, int *lenp); 708#endif 709 710/** 711 * fdt_getprop_namelen - get property value based on substring 712 * @fdt: pointer to the device tree blob 713 * @nodeoffset: offset of the node whose property to find 714 * @name: name of the property to find 715 * @namelen: number of characters of name to consider 716 * @lenp: pointer to an integer variable (will be overwritten) or NULL 717 * 718 * Identical to fdt_getprop(), but only examine the first namelen 719 * characters of name for matching the property name. 720 */ 721#ifndef SWIG /* Not available in Python */ 722const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, 723 const char *name, int namelen, int *lenp); 724static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset, 725 const char *name, int namelen, 726 int *lenp) 727{ 728 return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name, 729 namelen, lenp); 730} 731#endif 732 733/** 734 * fdt_getprop - retrieve the value of a given property 735 * @fdt: pointer to the device tree blob 736 * @nodeoffset: offset of the node whose property to find 737 * @name: name of the property to find 738 * @lenp: pointer to an integer variable (will be overwritten) or NULL 739 * 740 * fdt_getprop() retrieves a pointer to the value of the property 741 * named 'name' of the node at offset nodeoffset (this will be a 742 * pointer to within the device blob itself, not a copy of the value). 743 * If lenp is non-NULL, the length of the property value is also 744 * returned, in the integer pointed to by lenp. 745 * 746 * returns: 747 * pointer to the property's value 748 * if lenp is non-NULL, *lenp contains the length of the property 749 * value (>=0) 750 * NULL, on error 751 * if lenp is non-NULL, *lenp contains an error code (<0): 752 * -FDT_ERR_NOTFOUND, node does not have named property 753 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE 754 * tag 755 * -FDT_ERR_BADMAGIC, 756 * -FDT_ERR_BADVERSION, 757 * -FDT_ERR_BADSTATE, 758 * -FDT_ERR_BADSTRUCTURE, 759 * -FDT_ERR_TRUNCATED, standard meanings 760 */ 761const void *fdt_getprop(const void *fdt, int nodeoffset, 762 const char *name, int *lenp); 763static inline void *fdt_getprop_w(void *fdt, int nodeoffset, 764 const char *name, int *lenp) 765{ 766 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp); 767} 768 769/** 770 * fdt_get_phandle - retrieve the phandle of a given node 771 * @fdt: pointer to the device tree blob 772 * @nodeoffset: structure block offset of the node 773 * 774 * fdt_get_phandle() retrieves the phandle of the device tree node at 775 * structure block offset nodeoffset. 776 * 777 * returns: 778 * the phandle of the node at nodeoffset, on success (!= 0, != -1) 779 * 0, if the node has no phandle, or another error occurs 780 */ 781uint32_t fdt_get_phandle(const void *fdt, int nodeoffset); 782 783/** 784 * fdt_get_alias_namelen - get alias based on substring 785 * @fdt: pointer to the device tree blob 786 * @name: name of the alias th look up 787 * @namelen: number of characters of name to consider 788 * 789 * Identical to fdt_get_alias(), but only examine the first namelen 790 * characters of name for matching the alias name. 791 */ 792#ifndef SWIG /* Not available in Python */ 793const char *fdt_get_alias_namelen(const void *fdt, 794 const char *name, int namelen); 795#endif 796 797/** 798 * fdt_get_alias - retrieve the path referenced by a given alias 799 * @fdt: pointer to the device tree blob 800 * @name: name of the alias th look up 801 * 802 * fdt_get_alias() retrieves the value of a given alias. That is, the 803 * value of the property named 'name' in the node /aliases. 804 * 805 * returns: 806 * a pointer to the expansion of the alias named 'name', if it exists 807 * NULL, if the given alias or the /aliases node does not exist 808 */ 809const char *fdt_get_alias(const void *fdt, const char *name); 810 811/** 812 * fdt_get_path - determine the full path of a node 813 * @fdt: pointer to the device tree blob 814 * @nodeoffset: offset of the node whose path to find 815 * @buf: character buffer to contain the returned path (will be overwritten) 816 * @buflen: size of the character buffer at buf 817 * 818 * fdt_get_path() computes the full path of the node at offset 819 * nodeoffset, and records that path in the buffer at buf. 820 * 821 * NOTE: This function is expensive, as it must scan the device tree 822 * structure from the start to nodeoffset. 823 * 824 * returns: 825 * 0, on success 826 * buf contains the absolute path of the node at 827 * nodeoffset, as a NUL-terminated string. 828 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag 829 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1) 830 * characters and will not fit in the given buffer. 831 * -FDT_ERR_BADMAGIC, 832 * -FDT_ERR_BADVERSION, 833 * -FDT_ERR_BADSTATE, 834 * -FDT_ERR_BADSTRUCTURE, standard meanings 835 */ 836int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen); 837 838/** 839 * fdt_supernode_atdepth_offset - find a specific ancestor of a node 840 * @fdt: pointer to the device tree blob 841 * @nodeoffset: offset of the node whose parent to find 842 * @supernodedepth: depth of the ancestor to find 843 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL 844 * 845 * fdt_supernode_atdepth_offset() finds an ancestor of the given node 846 * at a specific depth from the root (where the root itself has depth 847 * 0, its immediate subnodes depth 1 and so forth). So 848 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL); 849 * will always return 0, the offset of the root node. If the node at 850 * nodeoffset has depth D, then: 851 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL); 852 * will return nodeoffset itself. 853 * 854 * NOTE: This function is expensive, as it must scan the device tree 855 * structure from the start to nodeoffset. 856 * 857 * returns: 858 * structure block offset of the node at node offset's ancestor 859 * of depth supernodedepth (>=0), on success 860 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag 861 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of 862 * nodeoffset 863 * -FDT_ERR_BADMAGIC, 864 * -FDT_ERR_BADVERSION, 865 * -FDT_ERR_BADSTATE, 866 * -FDT_ERR_BADSTRUCTURE, standard meanings 867 */ 868int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, 869 int supernodedepth, int *nodedepth); 870 871/** 872 * fdt_node_depth - find the depth of a given node 873 * @fdt: pointer to the device tree blob 874 * @nodeoffset: offset of the node whose parent to find 875 * 876 * fdt_node_depth() finds the depth of a given node. The root node 877 * has depth 0, its immediate subnodes depth 1 and so forth. 878 * 879 * NOTE: This function is expensive, as it must scan the device tree 880 * structure from the start to nodeoffset. 881 * 882 * returns: 883 * depth of the node at nodeoffset (>=0), on success 884 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag 885 * -FDT_ERR_BADMAGIC, 886 * -FDT_ERR_BADVERSION, 887 * -FDT_ERR_BADSTATE, 888 * -FDT_ERR_BADSTRUCTURE, standard meanings 889 */ 890int fdt_node_depth(const void *fdt, int nodeoffset); 891 892/** 893 * fdt_parent_offset - find the parent of a given node 894 * @fdt: pointer to the device tree blob 895 * @nodeoffset: offset of the node whose parent to find 896 * 897 * fdt_parent_offset() locates the parent node of a given node (that 898 * is, it finds the offset of the node which contains the node at 899 * nodeoffset as a subnode). 900 * 901 * NOTE: This function is expensive, as it must scan the device tree 902 * structure from the start to nodeoffset, *twice*. 903 * 904 * returns: 905 * structure block offset of the parent of the node at nodeoffset 906 * (>=0), on success 907 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag 908 * -FDT_ERR_BADMAGIC, 909 * -FDT_ERR_BADVERSION, 910 * -FDT_ERR_BADSTATE, 911 * -FDT_ERR_BADSTRUCTURE, standard meanings 912 */ 913int fdt_parent_offset(const void *fdt, int nodeoffset); 914 915/** 916 * fdt_node_offset_by_prop_value - find nodes with a given property value 917 * @fdt: pointer to the device tree blob 918 * @startoffset: only find nodes after this offset 919 * @propname: property name to check 920 * @propval: property value to search for 921 * @proplen: length of the value in propval 922 * 923 * fdt_node_offset_by_prop_value() returns the offset of the first 924 * node after startoffset, which has a property named propname whose 925 * value is of length proplen and has value equal to propval; or if 926 * startoffset is -1, the very first such node in the tree. 927 * 928 * To iterate through all nodes matching the criterion, the following 929 * idiom can be used: 930 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname, 931 * propval, proplen); 932 * while (offset != -FDT_ERR_NOTFOUND) { 933 * // other code here 934 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname, 935 * propval, proplen); 936 * } 937 * 938 * Note the -1 in the first call to the function, if 0 is used here 939 * instead, the function will never locate the root node, even if it 940 * matches the criterion. 941 * 942 * returns: 943 * structure block offset of the located node (>= 0, >startoffset), 944 * on success 945 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the 946 * tree after startoffset 947 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag 948 * -FDT_ERR_BADMAGIC, 949 * -FDT_ERR_BADVERSION, 950 * -FDT_ERR_BADSTATE, 951 * -FDT_ERR_BADSTRUCTURE, standard meanings 952 */ 953int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, 954 const char *propname, 955 const void *propval, int proplen); 956 957/** 958 * fdt_node_offset_by_phandle - find the node with a given phandle 959 * @fdt: pointer to the device tree blob 960 * @phandle: phandle value 961 * 962 * fdt_node_offset_by_phandle() returns the offset of the node 963 * which has the given phandle value. If there is more than one node 964 * in the tree with the given phandle (an invalid tree), results are 965 * undefined. 966 * 967 * returns: 968 * structure block offset of the located node (>= 0), on success 969 * -FDT_ERR_NOTFOUND, no node with that phandle exists 970 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1) 971 * -FDT_ERR_BADMAGIC, 972 * -FDT_ERR_BADVERSION, 973 * -FDT_ERR_BADSTATE, 974 * -FDT_ERR_BADSTRUCTURE, standard meanings 975 */ 976int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle); 977 978/** 979 * fdt_node_check_compatible: check a node's compatible property 980 * @fdt: pointer to the device tree blob 981 * @nodeoffset: offset of a tree node 982 * @compatible: string to match against 983 * 984 * 985 * fdt_node_check_compatible() returns 0 if the given node contains a 986 * 'compatible' property with the given string as one of its elements, 987 * it returns non-zero otherwise, or on error. 988 * 989 * returns: 990 * 0, if the node has a 'compatible' property listing the given string 991 * 1, if the node has a 'compatible' property, but it does not list 992 * the given string 993 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property 994 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag 995 * -FDT_ERR_BADMAGIC, 996 * -FDT_ERR_BADVERSION, 997 * -FDT_ERR_BADSTATE, 998 * -FDT_ERR_BADSTRUCTURE, standard meanings 999 */ 1000int fdt_node_check_compatible(const void *fdt, int nodeoffset, 1001 const char *compatible); 1002 1003/** 1004 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value 1005 * @fdt: pointer to the device tree blob 1006 * @startoffset: only find nodes after this offset 1007 * @compatible: 'compatible' string to match against 1008 * 1009 * fdt_node_offset_by_compatible() returns the offset of the first 1010 * node after startoffset, which has a 'compatible' property which 1011 * lists the given compatible string; or if startoffset is -1, the 1012 * very first such node in the tree. 1013 * 1014 * To iterate through all nodes matching the criterion, the following 1015 * idiom can be used: 1016 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible); 1017 * while (offset != -FDT_ERR_NOTFOUND) { 1018 * // other code here 1019 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible); 1020 * } 1021 * 1022 * Note the -1 in the first call to the function, if 0 is used here 1023 * instead, the function will never locate the root node, even if it 1024 * matches the criterion. 1025 * 1026 * returns: 1027 * structure block offset of the located node (>= 0, >startoffset), 1028 * on success 1029 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the 1030 * tree after startoffset 1031 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag 1032 * -FDT_ERR_BADMAGIC, 1033 * -FDT_ERR_BADVERSION, 1034 * -FDT_ERR_BADSTATE, 1035 * -FDT_ERR_BADSTRUCTURE, standard meanings 1036 */ 1037int fdt_node_offset_by_compatible(const void *fdt, int startoffset, 1038 const char *compatible); 1039 1040/** 1041 * fdt_stringlist_contains - check a string list property for a string 1042 * @strlist: Property containing a list of strings to check 1043 * @listlen: Length of property 1044 * @str: String to search for 1045 * 1046 * This is a utility function provided for convenience. The list contains 1047 * one or more strings, each terminated by \0, as is found in a device tree 1048 * "compatible" property. 1049 * 1050 * @return: 1 if the string is found in the list, 0 not found, or invalid list 1051 */ 1052int fdt_stringlist_contains(const char *strlist, int listlen, const char *str); 1053 1054/** 1055 * fdt_stringlist_count - count the number of strings in a string list 1056 * @fdt: pointer to the device tree blob 1057 * @nodeoffset: offset of a tree node 1058 * @property: name of the property containing the string list 1059 * @return: 1060 * the number of strings in the given property 1061 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated 1062 * -FDT_ERR_NOTFOUND if the property does not exist 1063 */ 1064int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property); 1065 1066/** 1067 * fdt_stringlist_search - find a string in a string list and return its index 1068 * @fdt: pointer to the device tree blob 1069 * @nodeoffset: offset of a tree node 1070 * @property: name of the property containing the string list 1071 * @string: string to look up in the string list 1072 * 1073 * Note that it is possible for this function to succeed on property values 1074 * that are not NUL-terminated. That's because the function will stop after 1075 * finding the first occurrence of @string. This can for example happen with 1076 * small-valued cell properties, such as #address-cells, when searching for 1077 * the empty string. 1078 * 1079 * @return: 1080 * the index of the string in the list of strings 1081 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated 1082 * -FDT_ERR_NOTFOUND if the property does not exist or does not contain 1083 * the given string 1084 */ 1085int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property, 1086 const char *string); 1087 1088/** 1089 * fdt_stringlist_get() - obtain the string at a given index in a string list 1090 * @fdt: pointer to the device tree blob 1091 * @nodeoffset: offset of a tree node 1092 * @property: name of the property containing the string list 1093 * @index: index of the string to return 1094 * @lenp: return location for the string length or an error code on failure 1095 * 1096 * Note that this will successfully extract strings from properties with 1097 * non-NUL-terminated values. For example on small-valued cell properties 1098 * this function will return the empty string. 1099 * 1100 * If non-NULL, the length of the string (on success) or a negative error-code 1101 * (on failure) will be stored in the integer pointer to by lenp. 1102 * 1103 * @return: 1104 * A pointer to the string at the given index in the string list or NULL on 1105 * failure. On success the length of the string will be stored in the memory 1106 * location pointed to by the lenp parameter, if non-NULL. On failure one of 1107 * the following negative error codes will be returned in the lenp parameter 1108 * (if non-NULL): 1109 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated 1110 * -FDT_ERR_NOTFOUND if the property does not exist 1111 */ 1112const char *fdt_stringlist_get(const void *fdt, int nodeoffset, 1113 const char *property, int index, 1114 int *lenp); 1115 1116/**********************************************************************/ 1117/* Read-only functions (addressing related) */ 1118/**********************************************************************/ 1119 1120/** 1121 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells 1122 * 1123 * This is the maximum value for #address-cells, #size-cells and 1124 * similar properties that will be processed by libfdt. IEE1275 1125 * requires that OF implementations handle values up to 4. 1126 * Implementations may support larger values, but in practice higher 1127 * values aren't used. 1128 */ 1129#define FDT_MAX_NCELLS 4 1130 1131/** 1132 * fdt_address_cells - retrieve address size for a bus represented in the tree 1133 * @fdt: pointer to the device tree blob 1134 * @nodeoffset: offset of the node to find the address size for 1135 * 1136 * When the node has a valid #address-cells property, returns its value. 1137 * 1138 * returns: 1139 * 0 <= n < FDT_MAX_NCELLS, on success 1140 * 2, if the node has no #address-cells property 1141 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid 1142 * #address-cells property 1143 * -FDT_ERR_BADMAGIC, 1144 * -FDT_ERR_BADVERSION, 1145 * -FDT_ERR_BADSTATE, 1146 * -FDT_ERR_BADSTRUCTURE, 1147 * -FDT_ERR_TRUNCATED, standard meanings 1148 */ 1149int fdt_address_cells(const void *fdt, int nodeoffset); 1150 1151/** 1152 * fdt_size_cells - retrieve address range size for a bus represented in the 1153 * tree 1154 * @fdt: pointer to the device tree blob 1155 * @nodeoffset: offset of the node to find the address range size for 1156 * 1157 * When the node has a valid #size-cells property, returns its value. 1158 * 1159 * returns: 1160 * 0 <= n < FDT_MAX_NCELLS, on success 1161 * 2, if the node has no #size-cells property 1162 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid 1163 * #size-cells property 1164 * -FDT_ERR_BADMAGIC, 1165 * -FDT_ERR_BADVERSION, 1166 * -FDT_ERR_BADSTATE, 1167 * -FDT_ERR_BADSTRUCTURE, 1168 * -FDT_ERR_TRUNCATED, standard meanings 1169 */ 1170int fdt_size_cells(const void *fdt, int nodeoffset); 1171 1172 1173/**********************************************************************/ 1174/* Write-in-place functions */ 1175/**********************************************************************/ 1176 1177/** 1178 * fdt_setprop_inplace_namelen_partial - change a property's value, 1179 * but not its size 1180 * @fdt: pointer to the device tree blob 1181 * @nodeoffset: offset of the node whose property to change 1182 * @name: name of the property to change 1183 * @namelen: number of characters of name to consider 1184 * @idx: index of the property to change in the array 1185 * @val: pointer to data to replace the property value with 1186 * @len: length of the property value 1187 * 1188 * Identical to fdt_setprop_inplace(), but modifies the given property 1189 * starting from the given index, and using only the first characters 1190 * of the name. It is useful when you want to manipulate only one value of 1191 * an array and you have a string that doesn't end with \0. 1192 */ 1193#ifndef SWIG /* Not available in Python */ 1194int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, 1195 const char *name, int namelen, 1196 uint32_t idx, const void *val, 1197 int len); 1198#endif 1199 1200/** 1201 * fdt_setprop_inplace - change a property's value, but not its size 1202 * @fdt: pointer to the device tree blob 1203 * @nodeoffset: offset of the node whose property to change 1204 * @name: name of the property to change 1205 * @val: pointer to data to replace the property value with 1206 * @len: length of the property value 1207 * 1208 * fdt_setprop_inplace() replaces the value of a given property with 1209 * the data in val, of length len. This function cannot change the 1210 * size of a property, and so will only work if len is equal to the 1211 * current length of the property. 1212 * 1213 * This function will alter only the bytes in the blob which contain 1214 * the given property value, and will not alter or move any other part 1215 * of the tree. 1216 * 1217 * returns: 1218 * 0, on success 1219 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length 1220 * -FDT_ERR_NOTFOUND, node does not have the named property 1221 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1222 * -FDT_ERR_BADMAGIC, 1223 * -FDT_ERR_BADVERSION, 1224 * -FDT_ERR_BADSTATE, 1225 * -FDT_ERR_BADSTRUCTURE, 1226 * -FDT_ERR_TRUNCATED, standard meanings 1227 */ 1228#ifndef SWIG /* Not available in Python */ 1229int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, 1230 const void *val, int len); 1231#endif 1232 1233/** 1234 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property 1235 * @fdt: pointer to the device tree blob 1236 * @nodeoffset: offset of the node whose property to change 1237 * @name: name of the property to change 1238 * @val: 32-bit integer value to replace the property with 1239 * 1240 * fdt_setprop_inplace_u32() replaces the value of a given property 1241 * with the 32-bit integer value in val, converting val to big-endian 1242 * if necessary. This function cannot change the size of a property, 1243 * and so will only work if the property already exists and has length 1244 * 4. 1245 * 1246 * This function will alter only the bytes in the blob which contain 1247 * the given property value, and will not alter or move any other part 1248 * of the tree. 1249 * 1250 * returns: 1251 * 0, on success 1252 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4 1253 * -FDT_ERR_NOTFOUND, node does not have the named property 1254 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1255 * -FDT_ERR_BADMAGIC, 1256 * -FDT_ERR_BADVERSION, 1257 * -FDT_ERR_BADSTATE, 1258 * -FDT_ERR_BADSTRUCTURE, 1259 * -FDT_ERR_TRUNCATED, standard meanings 1260 */ 1261static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset, 1262 const char *name, uint32_t val) 1263{ 1264 fdt32_t tmp = cpu_to_fdt32(val); 1265 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp)); 1266} 1267 1268/** 1269 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property 1270 * @fdt: pointer to the device tree blob 1271 * @nodeoffset: offset of the node whose property to change 1272 * @name: name of the property to change 1273 * @val: 64-bit integer value to replace the property with 1274 * 1275 * fdt_setprop_inplace_u64() replaces the value of a given property 1276 * with the 64-bit integer value in val, converting val to big-endian 1277 * if necessary. This function cannot change the size of a property, 1278 * and so will only work if the property already exists and has length 1279 * 8. 1280 * 1281 * This function will alter only the bytes in the blob which contain 1282 * the given property value, and will not alter or move any other part 1283 * of the tree. 1284 * 1285 * returns: 1286 * 0, on success 1287 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8 1288 * -FDT_ERR_NOTFOUND, node does not have the named property 1289 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1290 * -FDT_ERR_BADMAGIC, 1291 * -FDT_ERR_BADVERSION, 1292 * -FDT_ERR_BADSTATE, 1293 * -FDT_ERR_BADSTRUCTURE, 1294 * -FDT_ERR_TRUNCATED, standard meanings 1295 */ 1296static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset, 1297 const char *name, uint64_t val) 1298{ 1299 fdt64_t tmp = cpu_to_fdt64(val); 1300 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp)); 1301} 1302 1303/** 1304 * fdt_setprop_inplace_cell - change the value of a single-cell property 1305 * 1306 * This is an alternative name for fdt_setprop_inplace_u32() 1307 */ 1308static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset, 1309 const char *name, uint32_t val) 1310{ 1311 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val); 1312} 1313 1314/** 1315 * fdt_nop_property - replace a property with nop tags 1316 * @fdt: pointer to the device tree blob 1317 * @nodeoffset: offset of the node whose property to nop 1318 * @name: name of the property to nop 1319 * 1320 * fdt_nop_property() will replace a given property's representation 1321 * in the blob with FDT_NOP tags, effectively removing it from the 1322 * tree. 1323 * 1324 * This function will alter only the bytes in the blob which contain 1325 * the property, and will not alter or move any other part of the 1326 * tree. 1327 * 1328 * returns: 1329 * 0, on success 1330 * -FDT_ERR_NOTFOUND, node does not have the named property 1331 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1332 * -FDT_ERR_BADMAGIC, 1333 * -FDT_ERR_BADVERSION, 1334 * -FDT_ERR_BADSTATE, 1335 * -FDT_ERR_BADSTRUCTURE, 1336 * -FDT_ERR_TRUNCATED, standard meanings 1337 */ 1338int fdt_nop_property(void *fdt, int nodeoffset, const char *name); 1339 1340/** 1341 * fdt_nop_node - replace a node (subtree) with nop tags 1342 * @fdt: pointer to the device tree blob 1343 * @nodeoffset: offset of the node to nop 1344 * 1345 * fdt_nop_node() will replace a given node's representation in the 1346 * blob, including all its subnodes, if any, with FDT_NOP tags, 1347 * effectively removing it from the tree. 1348 * 1349 * This function will alter only the bytes in the blob which contain 1350 * the node and its properties and subnodes, and will not alter or 1351 * move any other part of the tree. 1352 * 1353 * returns: 1354 * 0, on success 1355 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1356 * -FDT_ERR_BADMAGIC, 1357 * -FDT_ERR_BADVERSION, 1358 * -FDT_ERR_BADSTATE, 1359 * -FDT_ERR_BADSTRUCTURE, 1360 * -FDT_ERR_TRUNCATED, standard meanings 1361 */ 1362int fdt_nop_node(void *fdt, int nodeoffset); 1363 1364/**********************************************************************/ 1365/* Sequential write functions */ 1366/**********************************************************************/ 1367 1368int fdt_create(void *buf, int bufsize); 1369int fdt_resize(void *fdt, void *buf, int bufsize); 1370int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size); 1371int fdt_finish_reservemap(void *fdt); 1372int fdt_begin_node(void *fdt, const char *name); 1373int fdt_property(void *fdt, const char *name, const void *val, int len); 1374static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val) 1375{ 1376 fdt32_t tmp = cpu_to_fdt32(val); 1377 return fdt_property(fdt, name, &tmp, sizeof(tmp)); 1378} 1379static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val) 1380{ 1381 fdt64_t tmp = cpu_to_fdt64(val); 1382 return fdt_property(fdt, name, &tmp, sizeof(tmp)); 1383} 1384 1385#ifndef SWIG /* Not available in Python */ 1386static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val) 1387{ 1388 return fdt_property_u32(fdt, name, val); 1389} 1390#endif 1391 1392/** 1393 * fdt_property_placeholder - add a new property and return a ptr to its value 1394 * 1395 * @fdt: pointer to the device tree blob 1396 * @name: name of property to add 1397 * @len: length of property value in bytes 1398 * @valp: returns a pointer to where where the value should be placed 1399 * 1400 * returns: 1401 * 0, on success 1402 * -FDT_ERR_BADMAGIC, 1403 * -FDT_ERR_NOSPACE, standard meanings 1404 */ 1405int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp); 1406 1407#define fdt_property_string(fdt, name, str) \ 1408 fdt_property(fdt, name, str, strlen(str)+1) 1409int fdt_end_node(void *fdt); 1410int fdt_finish(void *fdt); 1411 1412/**********************************************************************/ 1413/* Read-write functions */ 1414/**********************************************************************/ 1415 1416int fdt_create_empty_tree(void *buf, int bufsize); 1417int fdt_open_into(const void *fdt, void *buf, int bufsize); 1418int fdt_pack(void *fdt); 1419 1420/** 1421 * fdt_add_mem_rsv - add one memory reserve map entry 1422 * @fdt: pointer to the device tree blob 1423 * @address, @size: 64-bit values (native endian) 1424 * 1425 * Adds a reserve map entry to the given blob reserving a region at 1426 * address address of length size. 1427 * 1428 * This function will insert data into the reserve map and will 1429 * therefore change the indexes of some entries in the table. 1430 * 1431 * returns: 1432 * 0, on success 1433 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1434 * contain the new reservation entry 1435 * -FDT_ERR_BADMAGIC, 1436 * -FDT_ERR_BADVERSION, 1437 * -FDT_ERR_BADSTATE, 1438 * -FDT_ERR_BADSTRUCTURE, 1439 * -FDT_ERR_BADLAYOUT, 1440 * -FDT_ERR_TRUNCATED, standard meanings 1441 */ 1442int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size); 1443 1444/** 1445 * fdt_del_mem_rsv - remove a memory reserve map entry 1446 * @fdt: pointer to the device tree blob 1447 * @n: entry to remove 1448 * 1449 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from 1450 * the blob. 1451 * 1452 * This function will delete data from the reservation table and will 1453 * therefore change the indexes of some entries in the table. 1454 * 1455 * returns: 1456 * 0, on success 1457 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there 1458 * are less than n+1 reserve map entries) 1459 * -FDT_ERR_BADMAGIC, 1460 * -FDT_ERR_BADVERSION, 1461 * -FDT_ERR_BADSTATE, 1462 * -FDT_ERR_BADSTRUCTURE, 1463 * -FDT_ERR_BADLAYOUT, 1464 * -FDT_ERR_TRUNCATED, standard meanings 1465 */ 1466int fdt_del_mem_rsv(void *fdt, int n); 1467 1468/** 1469 * fdt_set_name - change the name of a given node 1470 * @fdt: pointer to the device tree blob 1471 * @nodeoffset: structure block offset of a node 1472 * @name: name to give the node 1473 * 1474 * fdt_set_name() replaces the name (including unit address, if any) 1475 * of the given node with the given string. NOTE: this function can't 1476 * efficiently check if the new name is unique amongst the given 1477 * node's siblings; results are undefined if this function is invoked 1478 * with a name equal to one of the given node's siblings. 1479 * 1480 * This function may insert or delete data from the blob, and will 1481 * therefore change the offsets of some existing nodes. 1482 * 1483 * returns: 1484 * 0, on success 1485 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob 1486 * to contain the new name 1487 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1488 * -FDT_ERR_BADMAGIC, 1489 * -FDT_ERR_BADVERSION, 1490 * -FDT_ERR_BADSTATE, standard meanings 1491 */ 1492int fdt_set_name(void *fdt, int nodeoffset, const char *name); 1493 1494/** 1495 * fdt_setprop - create or change a property 1496 * @fdt: pointer to the device tree blob 1497 * @nodeoffset: offset of the node whose property to change 1498 * @name: name of the property to change 1499 * @val: pointer to data to set the property value to 1500 * @len: length of the property value 1501 * 1502 * fdt_setprop() sets the value of the named property in the given 1503 * node to the given value and length, creating the property if it 1504 * does not already exist. 1505 * 1506 * This function may insert or delete data from the blob, and will 1507 * therefore change the offsets of some existing nodes. 1508 * 1509 * returns: 1510 * 0, on success 1511 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1512 * contain the new property value 1513 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1514 * -FDT_ERR_BADLAYOUT, 1515 * -FDT_ERR_BADMAGIC, 1516 * -FDT_ERR_BADVERSION, 1517 * -FDT_ERR_BADSTATE, 1518 * -FDT_ERR_BADSTRUCTURE, 1519 * -FDT_ERR_BADLAYOUT, 1520 * -FDT_ERR_TRUNCATED, standard meanings 1521 */ 1522int fdt_setprop(void *fdt, int nodeoffset, const char *name, 1523 const void *val, int len); 1524 1525/** 1526 * fdt_setprop_placeholder - allocate space for a property 1527 * @fdt: pointer to the device tree blob 1528 * @nodeoffset: offset of the node whose property to change 1529 * @name: name of the property to change 1530 * @len: length of the property value 1531 * @prop_data: return pointer to property data 1532 * 1533 * fdt_setprop_placeholer() allocates the named property in the given node. 1534 * If the property exists it is resized. In either case a pointer to the 1535 * property data is returned. 1536 * 1537 * This function may insert or delete data from the blob, and will 1538 * therefore change the offsets of some existing nodes. 1539 * 1540 * returns: 1541 * 0, on success 1542 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1543 * contain the new property value 1544 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1545 * -FDT_ERR_BADLAYOUT, 1546 * -FDT_ERR_BADMAGIC, 1547 * -FDT_ERR_BADVERSION, 1548 * -FDT_ERR_BADSTATE, 1549 * -FDT_ERR_BADSTRUCTURE, 1550 * -FDT_ERR_BADLAYOUT, 1551 * -FDT_ERR_TRUNCATED, standard meanings 1552 */ 1553int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name, 1554 int len, void **prop_data); 1555 1556/** 1557 * fdt_setprop_u32 - set a property to a 32-bit integer 1558 * @fdt: pointer to the device tree blob 1559 * @nodeoffset: offset of the node whose property to change 1560 * @name: name of the property to change 1561 * @val: 32-bit integer value for the property (native endian) 1562 * 1563 * fdt_setprop_u32() sets the value of the named property in the given 1564 * node to the given 32-bit integer value (converting to big-endian if 1565 * necessary), or creates a new property with that value if it does 1566 * not already exist. 1567 * 1568 * This function may insert or delete data from the blob, and will 1569 * therefore change the offsets of some existing nodes. 1570 * 1571 * returns: 1572 * 0, on success 1573 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1574 * contain the new property value 1575 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1576 * -FDT_ERR_BADLAYOUT, 1577 * -FDT_ERR_BADMAGIC, 1578 * -FDT_ERR_BADVERSION, 1579 * -FDT_ERR_BADSTATE, 1580 * -FDT_ERR_BADSTRUCTURE, 1581 * -FDT_ERR_BADLAYOUT, 1582 * -FDT_ERR_TRUNCATED, standard meanings 1583 */ 1584static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name, 1585 uint32_t val) 1586{ 1587 fdt32_t tmp = cpu_to_fdt32(val); 1588 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp)); 1589} 1590 1591/** 1592 * fdt_setprop_u64 - set a property to a 64-bit integer 1593 * @fdt: pointer to the device tree blob 1594 * @nodeoffset: offset of the node whose property to change 1595 * @name: name of the property to change 1596 * @val: 64-bit integer value for the property (native endian) 1597 * 1598 * fdt_setprop_u64() sets the value of the named property in the given 1599 * node to the given 64-bit integer value (converting to big-endian if 1600 * necessary), or creates a new property with that value if it does 1601 * not already exist. 1602 * 1603 * This function may insert or delete data from the blob, and will 1604 * therefore change the offsets of some existing nodes. 1605 * 1606 * returns: 1607 * 0, on success 1608 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1609 * contain the new property value 1610 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1611 * -FDT_ERR_BADLAYOUT, 1612 * -FDT_ERR_BADMAGIC, 1613 * -FDT_ERR_BADVERSION, 1614 * -FDT_ERR_BADSTATE, 1615 * -FDT_ERR_BADSTRUCTURE, 1616 * -FDT_ERR_BADLAYOUT, 1617 * -FDT_ERR_TRUNCATED, standard meanings 1618 */ 1619static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name, 1620 uint64_t val) 1621{ 1622 fdt64_t tmp = cpu_to_fdt64(val); 1623 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp)); 1624} 1625 1626/** 1627 * fdt_setprop_cell - set a property to a single cell value 1628 * 1629 * This is an alternative name for fdt_setprop_u32() 1630 */ 1631static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name, 1632 uint32_t val) 1633{ 1634 return fdt_setprop_u32(fdt, nodeoffset, name, val); 1635} 1636 1637/** 1638 * fdt_setprop_string - set a property to a string value 1639 * @fdt: pointer to the device tree blob 1640 * @nodeoffset: offset of the node whose property to change 1641 * @name: name of the property to change 1642 * @str: string value for the property 1643 * 1644 * fdt_setprop_string() sets the value of the named property in the 1645 * given node to the given string value (using the length of the 1646 * string to determine the new length of the property), or creates a 1647 * new property with that value if it does not already exist. 1648 * 1649 * This function may insert or delete data from the blob, and will 1650 * therefore change the offsets of some existing nodes. 1651 * 1652 * returns: 1653 * 0, on success 1654 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1655 * contain the new property value 1656 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1657 * -FDT_ERR_BADLAYOUT, 1658 * -FDT_ERR_BADMAGIC, 1659 * -FDT_ERR_BADVERSION, 1660 * -FDT_ERR_BADSTATE, 1661 * -FDT_ERR_BADSTRUCTURE, 1662 * -FDT_ERR_BADLAYOUT, 1663 * -FDT_ERR_TRUNCATED, standard meanings 1664 */ 1665#define fdt_setprop_string(fdt, nodeoffset, name, str) \ 1666 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1) 1667 1668 1669/** 1670 * fdt_setprop_empty - set a property to an empty value 1671 * @fdt: pointer to the device tree blob 1672 * @nodeoffset: offset of the node whose property to change 1673 * @name: name of the property to change 1674 * 1675 * fdt_setprop_empty() sets the value of the named property in the 1676 * given node to an empty (zero length) value, or creates a new empty 1677 * property if it does not already exist. 1678 * 1679 * This function may insert or delete data from the blob, and will 1680 * therefore change the offsets of some existing nodes. 1681 * 1682 * returns: 1683 * 0, on success 1684 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1685 * contain the new property value 1686 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1687 * -FDT_ERR_BADLAYOUT, 1688 * -FDT_ERR_BADMAGIC, 1689 * -FDT_ERR_BADVERSION, 1690 * -FDT_ERR_BADSTATE, 1691 * -FDT_ERR_BADSTRUCTURE, 1692 * -FDT_ERR_BADLAYOUT, 1693 * -FDT_ERR_TRUNCATED, standard meanings 1694 */ 1695#define fdt_setprop_empty(fdt, nodeoffset, name) \ 1696 fdt_setprop((fdt), (nodeoffset), (name), NULL, 0) 1697 1698/** 1699 * fdt_appendprop - append to or create a property 1700 * @fdt: pointer to the device tree blob 1701 * @nodeoffset: offset of the node whose property to change 1702 * @name: name of the property to append to 1703 * @val: pointer to data to append to the property value 1704 * @len: length of the data to append to the property value 1705 * 1706 * fdt_appendprop() appends the value to the named property in the 1707 * given node, creating the property if it does not already exist. 1708 * 1709 * This function may insert data into the blob, and will therefore 1710 * change the offsets of some existing nodes. 1711 * 1712 * returns: 1713 * 0, on success 1714 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1715 * contain the new property value 1716 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1717 * -FDT_ERR_BADLAYOUT, 1718 * -FDT_ERR_BADMAGIC, 1719 * -FDT_ERR_BADVERSION, 1720 * -FDT_ERR_BADSTATE, 1721 * -FDT_ERR_BADSTRUCTURE, 1722 * -FDT_ERR_BADLAYOUT, 1723 * -FDT_ERR_TRUNCATED, standard meanings 1724 */ 1725int fdt_appendprop(void *fdt, int nodeoffset, const char *name, 1726 const void *val, int len); 1727 1728/** 1729 * fdt_appendprop_u32 - append a 32-bit integer value to a property 1730 * @fdt: pointer to the device tree blob 1731 * @nodeoffset: offset of the node whose property to change 1732 * @name: name of the property to change 1733 * @val: 32-bit integer value to append to the property (native endian) 1734 * 1735 * fdt_appendprop_u32() appends the given 32-bit integer value 1736 * (converting to big-endian if necessary) to the value of the named 1737 * property in the given node, or creates a new property with that 1738 * value if it does not already exist. 1739 * 1740 * This function may insert data into the blob, and will therefore 1741 * change the offsets of some existing nodes. 1742 * 1743 * returns: 1744 * 0, on success 1745 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1746 * contain the new property value 1747 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1748 * -FDT_ERR_BADLAYOUT, 1749 * -FDT_ERR_BADMAGIC, 1750 * -FDT_ERR_BADVERSION, 1751 * -FDT_ERR_BADSTATE, 1752 * -FDT_ERR_BADSTRUCTURE, 1753 * -FDT_ERR_BADLAYOUT, 1754 * -FDT_ERR_TRUNCATED, standard meanings 1755 */ 1756static inline int fdt_appendprop_u32(void *fdt, int nodeoffset, 1757 const char *name, uint32_t val) 1758{ 1759 fdt32_t tmp = cpu_to_fdt32(val); 1760 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp)); 1761} 1762 1763/** 1764 * fdt_appendprop_u64 - append a 64-bit integer value to a property 1765 * @fdt: pointer to the device tree blob 1766 * @nodeoffset: offset of the node whose property to change 1767 * @name: name of the property to change 1768 * @val: 64-bit integer value to append to the property (native endian) 1769 * 1770 * fdt_appendprop_u64() appends the given 64-bit integer value 1771 * (converting to big-endian if necessary) to the value of the named 1772 * property in the given node, or creates a new property with that 1773 * value if it does not already exist. 1774 * 1775 * This function may insert data into the blob, and will therefore 1776 * change the offsets of some existing nodes. 1777 * 1778 * returns: 1779 * 0, on success 1780 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1781 * contain the new property value 1782 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1783 * -FDT_ERR_BADLAYOUT, 1784 * -FDT_ERR_BADMAGIC, 1785 * -FDT_ERR_BADVERSION, 1786 * -FDT_ERR_BADSTATE, 1787 * -FDT_ERR_BADSTRUCTURE, 1788 * -FDT_ERR_BADLAYOUT, 1789 * -FDT_ERR_TRUNCATED, standard meanings 1790 */ 1791static inline int fdt_appendprop_u64(void *fdt, int nodeoffset, 1792 const char *name, uint64_t val) 1793{ 1794 fdt64_t tmp = cpu_to_fdt64(val); 1795 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp)); 1796} 1797 1798/** 1799 * fdt_appendprop_cell - append a single cell value to a property 1800 * 1801 * This is an alternative name for fdt_appendprop_u32() 1802 */ 1803static inline int fdt_appendprop_cell(void *fdt, int nodeoffset, 1804 const char *name, uint32_t val) 1805{ 1806 return fdt_appendprop_u32(fdt, nodeoffset, name, val); 1807} 1808 1809/** 1810 * fdt_appendprop_string - append a string to a property 1811 * @fdt: pointer to the device tree blob 1812 * @nodeoffset: offset of the node whose property to change 1813 * @name: name of the property to change 1814 * @str: string value to append to the property 1815 * 1816 * fdt_appendprop_string() appends the given string to the value of 1817 * the named property in the given node, or creates a new property 1818 * with that value if it does not already exist. 1819 * 1820 * This function may insert data into the blob, and will therefore 1821 * change the offsets of some existing nodes. 1822 * 1823 * returns: 1824 * 0, on success 1825 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to 1826 * contain the new property value 1827 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1828 * -FDT_ERR_BADLAYOUT, 1829 * -FDT_ERR_BADMAGIC, 1830 * -FDT_ERR_BADVERSION, 1831 * -FDT_ERR_BADSTATE, 1832 * -FDT_ERR_BADSTRUCTURE, 1833 * -FDT_ERR_BADLAYOUT, 1834 * -FDT_ERR_TRUNCATED, standard meanings 1835 */ 1836#define fdt_appendprop_string(fdt, nodeoffset, name, str) \ 1837 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1) 1838 1839/** 1840 * fdt_delprop - delete a property 1841 * @fdt: pointer to the device tree blob 1842 * @nodeoffset: offset of the node whose property to nop 1843 * @name: name of the property to nop 1844 * 1845 * fdt_del_property() will delete the given property. 1846 * 1847 * This function will delete data from the blob, and will therefore 1848 * change the offsets of some existing nodes. 1849 * 1850 * returns: 1851 * 0, on success 1852 * -FDT_ERR_NOTFOUND, node does not have the named property 1853 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1854 * -FDT_ERR_BADLAYOUT, 1855 * -FDT_ERR_BADMAGIC, 1856 * -FDT_ERR_BADVERSION, 1857 * -FDT_ERR_BADSTATE, 1858 * -FDT_ERR_BADSTRUCTURE, 1859 * -FDT_ERR_TRUNCATED, standard meanings 1860 */ 1861int fdt_delprop(void *fdt, int nodeoffset, const char *name); 1862 1863/** 1864 * fdt_add_subnode_namelen - creates a new node based on substring 1865 * @fdt: pointer to the device tree blob 1866 * @parentoffset: structure block offset of a node 1867 * @name: name of the subnode to locate 1868 * @namelen: number of characters of name to consider 1869 * 1870 * Identical to fdt_add_subnode(), but use only the first namelen 1871 * characters of name as the name of the new node. This is useful for 1872 * creating subnodes based on a portion of a larger string, such as a 1873 * full path. 1874 */ 1875#ifndef SWIG /* Not available in Python */ 1876int fdt_add_subnode_namelen(void *fdt, int parentoffset, 1877 const char *name, int namelen); 1878#endif 1879 1880/** 1881 * fdt_add_subnode - creates a new node 1882 * @fdt: pointer to the device tree blob 1883 * @parentoffset: structure block offset of a node 1884 * @name: name of the subnode to locate 1885 * 1886 * fdt_add_subnode() creates a new node as a subnode of the node at 1887 * structure block offset parentoffset, with the given name (which 1888 * should include the unit address, if any). 1889 * 1890 * This function will insert data into the blob, and will therefore 1891 * change the offsets of some existing nodes. 1892 1893 * returns: 1894 * structure block offset of the created nodeequested subnode (>=0), on 1895 * success 1896 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist 1897 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE 1898 * tag 1899 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of 1900 * the given name 1901 * -FDT_ERR_NOSPACE, if there is insufficient free space in the 1902 * blob to contain the new node 1903 * -FDT_ERR_NOSPACE 1904 * -FDT_ERR_BADLAYOUT 1905 * -FDT_ERR_BADMAGIC, 1906 * -FDT_ERR_BADVERSION, 1907 * -FDT_ERR_BADSTATE, 1908 * -FDT_ERR_BADSTRUCTURE, 1909 * -FDT_ERR_TRUNCATED, standard meanings. 1910 */ 1911int fdt_add_subnode(void *fdt, int parentoffset, const char *name); 1912 1913/** 1914 * fdt_del_node - delete a node (subtree) 1915 * @fdt: pointer to the device tree blob 1916 * @nodeoffset: offset of the node to nop 1917 * 1918 * fdt_del_node() will remove the given node, including all its 1919 * subnodes if any, from the blob. 1920 * 1921 * This function will delete data from the blob, and will therefore 1922 * change the offsets of some existing nodes. 1923 * 1924 * returns: 1925 * 0, on success 1926 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag 1927 * -FDT_ERR_BADLAYOUT, 1928 * -FDT_ERR_BADMAGIC, 1929 * -FDT_ERR_BADVERSION, 1930 * -FDT_ERR_BADSTATE, 1931 * -FDT_ERR_BADSTRUCTURE, 1932 * -FDT_ERR_TRUNCATED, standard meanings 1933 */ 1934int fdt_del_node(void *fdt, int nodeoffset); 1935 1936/** 1937 * fdt_overlay_apply - Applies a DT overlay on a base DT 1938 * @fdt: pointer to the base device tree blob 1939 * @fdto: pointer to the device tree overlay blob 1940 * 1941 * fdt_overlay_apply() will apply the given device tree overlay on the 1942 * given base device tree. 1943 * 1944 * Expect the base device tree to be modified, even if the function 1945 * returns an error. 1946 * 1947 * returns: 1948 * 0, on success 1949 * -FDT_ERR_NOSPACE, there's not enough space in the base device tree 1950 * -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or 1951 * properties in the base DT 1952 * -FDT_ERR_BADPHANDLE, 1953 * -FDT_ERR_BADOVERLAY, 1954 * -FDT_ERR_NOPHANDLES, 1955 * -FDT_ERR_INTERNAL, 1956 * -FDT_ERR_BADLAYOUT, 1957 * -FDT_ERR_BADMAGIC, 1958 * -FDT_ERR_BADOFFSET, 1959 * -FDT_ERR_BADPATH, 1960 * -FDT_ERR_BADVERSION, 1961 * -FDT_ERR_BADSTRUCTURE, 1962 * -FDT_ERR_BADSTATE, 1963 * -FDT_ERR_TRUNCATED, standard meanings 1964 */ 1965int fdt_overlay_apply(void *fdt, void *fdto); 1966 1967/**********************************************************************/ 1968/* Debugging / informational functions */ 1969/**********************************************************************/ 1970 1971const char *fdt_strerror(int errval); 1972 1973#endif /* LIBFDT_H */ 1974