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