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