4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
7 * libfdt is dual licensed: you can use it either under the terms of
8 * the GPL, or the BSD license, at your option.
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.
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.
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,
27 * b) Redistribution and use in source and binary forms, with or
28 * without modification, are permitted provided that the following
31 * 1. Redistributions of source code must retain the above
32 * copyright notice, this list of conditions and the following
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.
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.
54 #include "libfdt_env.h"
57 #define FDT_FIRST_SUPPORTED_VERSION 0x02
58 #define FDT_LAST_SUPPORTED_VERSION 0x11
60 /* Error codes: informative error codes */
61 #define FDT_ERR_NOTFOUND 1
62 /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
63 #define FDT_ERR_EXISTS 2
64 /* FDT_ERR_EXISTS: Attempted to create a node or property which
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. */
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
81 #define FDT_ERR_BADPHANDLE 6
82 /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle.
83 * This can be caused either by an invalid phandle property
84 * length, or the phandle value was either 0 or -1, which are
86 #define FDT_ERR_BADSTATE 7
87 /* FDT_ERR_BADSTATE: Function was passed an incomplete device
88 * tree created by the sequential-write functions, which is
89 * not sufficiently complete for the requested operation. */
91 /* Error codes: codes for bad device tree blobs */
92 #define FDT_ERR_TRUNCATED 8
93 /* FDT_ERR_TRUNCATED: FDT or a sub-block is improperly
94 * terminated (overflows, goes outside allowed bounds, or
95 * isn't properly terminated). */
96 #define FDT_ERR_BADMAGIC 9
97 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
98 * device tree at all - it is missing the flattened device
99 * tree magic number. */
100 #define FDT_ERR_BADVERSION 10
101 /* FDT_ERR_BADVERSION: Given device tree has a version which
102 * can't be handled by the requested operation. For
103 * read-write functions, this may mean that fdt_open_into() is
104 * required to convert the tree to the expected version. */
105 #define FDT_ERR_BADSTRUCTURE 11
106 /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
107 * structure block or other serious error (e.g. misnested
108 * nodes, or subnodes preceding properties). */
109 #define FDT_ERR_BADLAYOUT 12
110 /* FDT_ERR_BADLAYOUT: For read-write functions, the given
111 * device tree has it's sub-blocks in an order that the
112 * function can't handle (memory reserve map, then structure,
113 * then strings). Use fdt_open_into() to reorganize the tree
114 * into a form suitable for the read-write operations. */
116 /* "Can't happen" error indicating a bug in libfdt */
117 #define FDT_ERR_INTERNAL 13
118 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
119 * Should never be returned, if it is, it indicates a bug in
122 /* Errors in device tree content */
123 #define FDT_ERR_BADNCELLS 14
124 /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
125 * or similar property with a bad format or value */
127 #define FDT_ERR_BADVALUE 15
128 /* FDT_ERR_BADVALUE: Device tree has a property with an unexpected
129 * value. For example: a property expected to contain a string list
130 * is not NUL-terminated within the length of its value. */
132 #define FDT_ERR_BADOVERLAY 16
133 /* FDT_ERR_BADOVERLAY: The device tree overlay, while
134 * correctly structured, cannot be applied due to some
135 * unexpected or missing value, property or node. */
137 #define FDT_ERR_NOPHANDLES 17
138 /* FDT_ERR_NOPHANDLES: The device tree doesn't have any
139 * phandle available anymore without causing an overflow */
141 #define FDT_ERR_MAX 17
143 /**********************************************************************/
144 /* Low-level functions (you probably don't need these) */
145 /**********************************************************************/
147 #ifndef SWIG /* This function is not useful in Python */
148 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
150 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
152 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
155 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
159 * These helpers access words from a device tree blob. They're
160 * built to work even with unaligned pointers on platforms (ike
161 * ARM) that don't like unaligned loads and stores
164 static inline uint32_t fdt32_ld(const fdt32_t *p)
168 memcpy(&v, p, sizeof(v));
169 return fdt32_to_cpu(v);
172 static inline uint64_t fdt64_ld(const fdt64_t *p)
176 memcpy(&v, p, sizeof(v));
177 return fdt64_to_cpu(v);
180 /**********************************************************************/
181 /* Traversal functions */
182 /**********************************************************************/
184 int fdt_next_node(const void *fdt, int offset, int *depth);
187 * fdt_first_subnode() - get offset of first direct subnode
190 * @offset: Offset of node to check
191 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
193 int fdt_first_subnode(const void *fdt, int offset);
196 * fdt_next_subnode() - get offset of next direct subnode
198 * After first calling fdt_first_subnode(), call this function repeatedly to
199 * get direct subnodes of a parent node.
202 * @offset: Offset of previous subnode
203 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
206 int fdt_next_subnode(const void *fdt, int offset);
209 * fdt_for_each_subnode - iterate over all subnodes of a parent
211 * @node: child node (int, lvalue)
212 * @fdt: FDT blob (const void *)
213 * @parent: parent node (int)
215 * This is actually a wrapper around a for loop and would be used like so:
217 * fdt_for_each_subnode(node, fdt, parent) {
222 * if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) {
226 * Note that this is implemented as a macro and @node is used as
227 * iterator in the loop. The parent variable be constant or even a
231 #define fdt_for_each_subnode(node, fdt, parent) \
232 for (node = fdt_first_subnode(fdt, parent); \
234 node = fdt_next_subnode(fdt, node))
236 /**********************************************************************/
237 /* General functions */
238 /**********************************************************************/
239 #define fdt_get_header(fdt, field) \
240 (fdt32_ld(&((const struct fdt_header *)(fdt))->field))
241 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
242 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
243 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
244 #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
245 #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
246 #define fdt_version(fdt) (fdt_get_header(fdt, version))
247 #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
248 #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
249 #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
250 #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
252 #define fdt_set_hdr_(name) \
253 static inline void fdt_set_##name(void *fdt, uint32_t val) \
255 struct fdt_header *fdth = (struct fdt_header *)fdt; \
256 fdth->name = cpu_to_fdt32(val); \
259 fdt_set_hdr_(totalsize);
260 fdt_set_hdr_(off_dt_struct);
261 fdt_set_hdr_(off_dt_strings);
262 fdt_set_hdr_(off_mem_rsvmap);
263 fdt_set_hdr_(version);
264 fdt_set_hdr_(last_comp_version);
265 fdt_set_hdr_(boot_cpuid_phys);
266 fdt_set_hdr_(size_dt_strings);
267 fdt_set_hdr_(size_dt_struct);
271 * fdt_header_size - return the size of the tree's header
272 * @fdt: pointer to a flattened device tree
274 size_t fdt_header_size_(uint32_t version);
275 static inline size_t fdt_header_size(const void *fdt)
277 return fdt_header_size_(fdt_version(fdt));
281 * fdt_check_header - sanity check a device tree header
283 * @fdt: pointer to data which might be a flattened device tree
285 * fdt_check_header() checks that the given buffer contains what
286 * appears to be a flattened device tree, and that the header contains
287 * valid information (to the extent that can be determined from the
291 * 0, if the buffer appears to contain a valid device tree
293 * -FDT_ERR_BADVERSION,
295 * -FDT_ERR_TRUNCATED, standard meanings, as above
297 int fdt_check_header(const void *fdt);
300 * fdt_move - move a device tree around in memory
301 * @fdt: pointer to the device tree to move
302 * @buf: pointer to memory where the device is to be moved
303 * @bufsize: size of the memory space at buf
305 * fdt_move() relocates, if possible, the device tree blob located at
306 * fdt to the buffer at buf of size bufsize. The buffer may overlap
307 * with the existing device tree blob at fdt. Therefore,
308 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
309 * should always succeed.
313 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
315 * -FDT_ERR_BADVERSION,
316 * -FDT_ERR_BADSTATE, standard meanings
318 int fdt_move(const void *fdt, void *buf, int bufsize);
320 /**********************************************************************/
321 /* Read-only functions */
322 /**********************************************************************/
324 int fdt_check_full(const void *fdt, size_t bufsize);
327 * fdt_get_string - retrieve a string from the strings block of a device tree
328 * @fdt: pointer to the device tree blob
329 * @stroffset: offset of the string within the strings block (native endian)
330 * @lenp: optional pointer to return the string's length
332 * fdt_get_string() retrieves a pointer to a single string from the
333 * strings block of the device tree blob at fdt, and optionally also
334 * returns the string's length in *lenp.
337 * a pointer to the string, on success
338 * NULL, if stroffset is out of bounds, or doesn't point to a valid string
340 const char *fdt_get_string(const void *fdt, int stroffset, int *lenp);
343 * fdt_string - retrieve a string from the strings block of a device tree
344 * @fdt: pointer to the device tree blob
345 * @stroffset: offset of the string within the strings block (native endian)
347 * fdt_string() retrieves a pointer to a single string from the
348 * strings block of the device tree blob at fdt.
351 * a pointer to the string, on success
352 * NULL, if stroffset is out of bounds, or doesn't point to a valid string
354 const char *fdt_string(const void *fdt, int stroffset);
357 * fdt_get_max_phandle - retrieves the highest phandle in a tree
358 * @fdt: pointer to the device tree blob
360 * fdt_get_max_phandle retrieves the highest phandle in the given
361 * device tree. This will ignore badly formatted phandles, or phandles
362 * with a value of 0 or -1.
365 * the highest phandle on success
366 * 0, if no phandle was found in the device tree
367 * -1, if an error occurred
369 uint32_t fdt_get_max_phandle(const void *fdt);
372 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
373 * @fdt: pointer to the device tree blob
375 * Returns the number of entries in the device tree blob's memory
376 * reservation map. This does not include the terminating 0,0 entry
377 * or any other (0,0) entries reserved for expansion.
380 * the number of entries
382 int fdt_num_mem_rsv(const void *fdt);
385 * fdt_get_mem_rsv - retrieve one memory reserve map entry
386 * @fdt: pointer to the device tree blob
387 * @address, @size: pointers to 64-bit variables
389 * On success, *address and *size will contain the address and size of
390 * the n-th reserve map entry from the device tree blob, in
391 * native-endian format.
396 * -FDT_ERR_BADVERSION,
397 * -FDT_ERR_BADSTATE, standard meanings
399 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
402 * fdt_subnode_offset_namelen - find a subnode based on substring
403 * @fdt: pointer to the device tree blob
404 * @parentoffset: structure block offset of a node
405 * @name: name of the subnode to locate
406 * @namelen: number of characters of name to consider
408 * Identical to fdt_subnode_offset(), but only examine the first
409 * namelen characters of name for matching the subnode name. This is
410 * useful for finding subnodes based on a portion of a larger string,
411 * such as a full path.
413 #ifndef SWIG /* Not available in Python */
414 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
415 const char *name, int namelen);
418 * fdt_subnode_offset - find a subnode of a given node
419 * @fdt: pointer to the device tree blob
420 * @parentoffset: structure block offset of a node
421 * @name: name of the subnode to locate
423 * fdt_subnode_offset() finds a subnode of the node at structure block
424 * offset parentoffset with the given name. name may include a unit
425 * address, in which case fdt_subnode_offset() will find the subnode
426 * with that unit address, or the unit address may be omitted, in
427 * which case fdt_subnode_offset() will find an arbitrary subnode
428 * whose name excluding unit address matches the given name.
431 * structure block offset of the requested subnode (>=0), on success
432 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
433 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
436 * -FDT_ERR_BADVERSION,
438 * -FDT_ERR_BADSTRUCTURE,
439 * -FDT_ERR_TRUNCATED, standard meanings.
441 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
444 * fdt_path_offset_namelen - find a tree node by its full path
445 * @fdt: pointer to the device tree blob
446 * @path: full path of the node to locate
447 * @namelen: number of characters of path to consider
449 * Identical to fdt_path_offset(), but only consider the first namelen
450 * characters of path as the path name.
452 #ifndef SWIG /* Not available in Python */
453 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
457 * fdt_path_offset - find a tree node by its full path
458 * @fdt: pointer to the device tree blob
459 * @path: full path of the node to locate
461 * fdt_path_offset() finds a node of a given path in the device tree.
462 * Each path component may omit the unit address portion, but the
463 * results of this are undefined if any such path component is
464 * ambiguous (that is if there are multiple nodes at the relevant
465 * level matching the given component, differentiated only by unit
469 * structure block offset of the node with the requested path (>=0), on
471 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
472 * -FDT_ERR_NOTFOUND, if the requested node does not exist
474 * -FDT_ERR_BADVERSION,
476 * -FDT_ERR_BADSTRUCTURE,
477 * -FDT_ERR_TRUNCATED, standard meanings.
479 int fdt_path_offset(const void *fdt, const char *path);
482 * fdt_get_name - retrieve the name of a given node
483 * @fdt: pointer to the device tree blob
484 * @nodeoffset: structure block offset of the starting node
485 * @lenp: pointer to an integer variable (will be overwritten) or NULL
487 * fdt_get_name() retrieves the name (including unit address) of the
488 * device tree node at structure block offset nodeoffset. If lenp is
489 * non-NULL, the length of this name is also returned, in the integer
490 * pointed to by lenp.
493 * pointer to the node's name, on success
494 * If lenp is non-NULL, *lenp contains the length of that name
497 * if lenp is non-NULL *lenp contains an error code (<0):
498 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
501 * -FDT_ERR_BADVERSION,
502 * -FDT_ERR_BADSTATE, standard meanings
504 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
507 * fdt_first_property_offset - find the offset of a node's first property
508 * @fdt: pointer to the device tree blob
509 * @nodeoffset: structure block offset of a node
511 * fdt_first_property_offset() finds the first property of the node at
512 * the given structure block offset.
515 * structure block offset of the property (>=0), on success
516 * -FDT_ERR_NOTFOUND, if the requested node has no properties
517 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
519 * -FDT_ERR_BADVERSION,
521 * -FDT_ERR_BADSTRUCTURE,
522 * -FDT_ERR_TRUNCATED, standard meanings.
524 int fdt_first_property_offset(const void *fdt, int nodeoffset);
527 * fdt_next_property_offset - step through a node's properties
528 * @fdt: pointer to the device tree blob
529 * @offset: structure block offset of a property
531 * fdt_next_property_offset() finds the property immediately after the
532 * one at the given structure block offset. This will be a property
533 * of the same node as the given property.
536 * structure block offset of the next property (>=0), on success
537 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
538 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
540 * -FDT_ERR_BADVERSION,
542 * -FDT_ERR_BADSTRUCTURE,
543 * -FDT_ERR_TRUNCATED, standard meanings.
545 int fdt_next_property_offset(const void *fdt, int offset);
548 * fdt_for_each_property_offset - iterate over all properties of a node
550 * @property_offset: property offset (int, lvalue)
551 * @fdt: FDT blob (const void *)
552 * @node: node offset (int)
554 * This is actually a wrapper around a for loop and would be used like so:
556 * fdt_for_each_property_offset(property, fdt, node) {
561 * if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) {
565 * Note that this is implemented as a macro and property is used as
566 * iterator in the loop. The node variable can be constant or even a
569 #define fdt_for_each_property_offset(property, fdt, node) \
570 for (property = fdt_first_property_offset(fdt, node); \
572 property = fdt_next_property_offset(fdt, property))
575 * fdt_get_property_by_offset - retrieve the property at a given offset
576 * @fdt: pointer to the device tree blob
577 * @offset: offset of the property to retrieve
578 * @lenp: pointer to an integer variable (will be overwritten) or NULL
580 * fdt_get_property_by_offset() retrieves a pointer to the
581 * fdt_property structure within the device tree blob at the given
582 * offset. If lenp is non-NULL, the length of the property value is
583 * also returned, in the integer pointed to by lenp.
585 * Note that this code only works on device tree versions >= 16. fdt_getprop()
586 * works on all versions.
589 * pointer to the structure representing the property
590 * if lenp is non-NULL, *lenp contains the length of the property
593 * if lenp is non-NULL, *lenp contains an error code (<0):
594 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
596 * -FDT_ERR_BADVERSION,
598 * -FDT_ERR_BADSTRUCTURE,
599 * -FDT_ERR_TRUNCATED, standard meanings
601 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
606 * fdt_get_property_namelen - find a property based on substring
607 * @fdt: pointer to the device tree blob
608 * @nodeoffset: offset of the node whose property to find
609 * @name: name of the property to find
610 * @namelen: number of characters of name to consider
611 * @lenp: pointer to an integer variable (will be overwritten) or NULL
613 * Identical to fdt_get_property(), but only examine the first namelen
614 * characters of name for matching the property name.
616 #ifndef SWIG /* Not available in Python */
617 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
620 int namelen, int *lenp);
624 * fdt_get_property - find a given property in a given node
625 * @fdt: pointer to the device tree blob
626 * @nodeoffset: offset of the node whose property to find
627 * @name: name of the property to find
628 * @lenp: pointer to an integer variable (will be overwritten) or NULL
630 * fdt_get_property() retrieves a pointer to the fdt_property
631 * structure within the device tree blob corresponding to the property
632 * named 'name' of the node at offset nodeoffset. If lenp is
633 * non-NULL, the length of the property value is also returned, in the
634 * integer pointed to by lenp.
637 * pointer to the structure representing the property
638 * if lenp is non-NULL, *lenp contains the length of the property
641 * if lenp is non-NULL, *lenp contains an error code (<0):
642 * -FDT_ERR_NOTFOUND, node does not have named property
643 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
646 * -FDT_ERR_BADVERSION,
648 * -FDT_ERR_BADSTRUCTURE,
649 * -FDT_ERR_TRUNCATED, standard meanings
651 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
652 const char *name, int *lenp);
653 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
657 return (struct fdt_property *)(uintptr_t)
658 fdt_get_property(fdt, nodeoffset, name, lenp);
662 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
663 * @fdt: pointer to the device tree blob
664 * @ffset: offset of the property to read
665 * @namep: pointer to a string variable (will be overwritten) or NULL
666 * @lenp: pointer to an integer variable (will be overwritten) or NULL
668 * fdt_getprop_by_offset() retrieves a pointer to the value of the
669 * property at structure block offset 'offset' (this will be a pointer
670 * to within the device blob itself, not a copy of the value). If
671 * lenp is non-NULL, the length of the property value is also
672 * returned, in the integer pointed to by lenp. If namep is non-NULL,
673 * the property's namne will also be returned in the char * pointed to
674 * by namep (this will be a pointer to within the device tree's string
675 * block, not a new copy of the name).
678 * pointer to the property's value
679 * if lenp is non-NULL, *lenp contains the length of the property
681 * if namep is non-NULL *namep contiains a pointer to the property
684 * if lenp is non-NULL, *lenp contains an error code (<0):
685 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
687 * -FDT_ERR_BADVERSION,
689 * -FDT_ERR_BADSTRUCTURE,
690 * -FDT_ERR_TRUNCATED, standard meanings
692 #ifndef SWIG /* This function is not useful in Python */
693 const void *fdt_getprop_by_offset(const void *fdt, int offset,
694 const char **namep, int *lenp);
698 * fdt_getprop_namelen - get property value based on substring
699 * @fdt: pointer to the device tree blob
700 * @nodeoffset: offset of the node whose property to find
701 * @name: name of the property to find
702 * @namelen: number of characters of name to consider
703 * @lenp: pointer to an integer variable (will be overwritten) or NULL
705 * Identical to fdt_getprop(), but only examine the first namelen
706 * characters of name for matching the property name.
708 #ifndef SWIG /* Not available in Python */
709 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
710 const char *name, int namelen, int *lenp);
711 static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
712 const char *name, int namelen,
715 return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
721 * fdt_getprop - retrieve the value of a given property
722 * @fdt: pointer to the device tree blob
723 * @nodeoffset: offset of the node whose property to find
724 * @name: name of the property to find
725 * @lenp: pointer to an integer variable (will be overwritten) or NULL
727 * fdt_getprop() retrieves a pointer to the value of the property
728 * named 'name' of the node at offset nodeoffset (this will be a
729 * pointer to within the device blob itself, not a copy of the value).
730 * If lenp is non-NULL, the length of the property value is also
731 * returned, in the integer pointed to by lenp.
734 * pointer to the property's value
735 * if lenp is non-NULL, *lenp contains the length of the property
738 * if lenp is non-NULL, *lenp contains an error code (<0):
739 * -FDT_ERR_NOTFOUND, node does not have named property
740 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
743 * -FDT_ERR_BADVERSION,
745 * -FDT_ERR_BADSTRUCTURE,
746 * -FDT_ERR_TRUNCATED, standard meanings
748 const void *fdt_getprop(const void *fdt, int nodeoffset,
749 const char *name, int *lenp);
750 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
751 const char *name, int *lenp)
753 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
757 * fdt_get_phandle - retrieve the phandle of a given node
758 * @fdt: pointer to the device tree blob
759 * @nodeoffset: structure block offset of the node
761 * fdt_get_phandle() retrieves the phandle of the device tree node at
762 * structure block offset nodeoffset.
765 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
766 * 0, if the node has no phandle, or another error occurs
768 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
771 * fdt_get_alias_namelen - get alias based on substring
772 * @fdt: pointer to the device tree blob
773 * @name: name of the alias th look up
774 * @namelen: number of characters of name to consider
776 * Identical to fdt_get_alias(), but only examine the first namelen
777 * characters of name for matching the alias name.
779 #ifndef SWIG /* Not available in Python */
780 const char *fdt_get_alias_namelen(const void *fdt,
781 const char *name, int namelen);
785 * fdt_get_alias - retrieve the path referenced by a given alias
786 * @fdt: pointer to the device tree blob
787 * @name: name of the alias th look up
789 * fdt_get_alias() retrieves the value of a given alias. That is, the
790 * value of the property named 'name' in the node /aliases.
793 * a pointer to the expansion of the alias named 'name', if it exists
794 * NULL, if the given alias or the /aliases node does not exist
796 const char *fdt_get_alias(const void *fdt, const char *name);
799 * fdt_get_path - determine the full path of a node
800 * @fdt: pointer to the device tree blob
801 * @nodeoffset: offset of the node whose path to find
802 * @buf: character buffer to contain the returned path (will be overwritten)
803 * @buflen: size of the character buffer at buf
805 * fdt_get_path() computes the full path of the node at offset
806 * nodeoffset, and records that path in the buffer at buf.
808 * NOTE: This function is expensive, as it must scan the device tree
809 * structure from the start to nodeoffset.
813 * buf contains the absolute path of the node at
814 * nodeoffset, as a NUL-terminated string.
815 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
816 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
817 * characters and will not fit in the given buffer.
819 * -FDT_ERR_BADVERSION,
821 * -FDT_ERR_BADSTRUCTURE, standard meanings
823 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
826 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
827 * @fdt: pointer to the device tree blob
828 * @nodeoffset: offset of the node whose parent to find
829 * @supernodedepth: depth of the ancestor to find
830 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
832 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
833 * at a specific depth from the root (where the root itself has depth
834 * 0, its immediate subnodes depth 1 and so forth). So
835 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
836 * will always return 0, the offset of the root node. If the node at
837 * nodeoffset has depth D, then:
838 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
839 * will return nodeoffset itself.
841 * NOTE: This function is expensive, as it must scan the device tree
842 * structure from the start to nodeoffset.
845 * structure block offset of the node at node offset's ancestor
846 * of depth supernodedepth (>=0), on success
847 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
848 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
851 * -FDT_ERR_BADVERSION,
853 * -FDT_ERR_BADSTRUCTURE, standard meanings
855 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
856 int supernodedepth, int *nodedepth);
859 * fdt_node_depth - find the depth of a given node
860 * @fdt: pointer to the device tree blob
861 * @nodeoffset: offset of the node whose parent to find
863 * fdt_node_depth() finds the depth of a given node. The root node
864 * has depth 0, its immediate subnodes depth 1 and so forth.
866 * NOTE: This function is expensive, as it must scan the device tree
867 * structure from the start to nodeoffset.
870 * depth of the node at nodeoffset (>=0), on success
871 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
873 * -FDT_ERR_BADVERSION,
875 * -FDT_ERR_BADSTRUCTURE, standard meanings
877 int fdt_node_depth(const void *fdt, int nodeoffset);
880 * fdt_parent_offset - find the parent of a given node
881 * @fdt: pointer to the device tree blob
882 * @nodeoffset: offset of the node whose parent to find
884 * fdt_parent_offset() locates the parent node of a given node (that
885 * is, it finds the offset of the node which contains the node at
886 * nodeoffset as a subnode).
888 * NOTE: This function is expensive, as it must scan the device tree
889 * structure from the start to nodeoffset, *twice*.
892 * structure block offset of the parent of the node at nodeoffset
894 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
896 * -FDT_ERR_BADVERSION,
898 * -FDT_ERR_BADSTRUCTURE, standard meanings
900 int fdt_parent_offset(const void *fdt, int nodeoffset);
903 * fdt_node_offset_by_prop_value - find nodes with a given property value
904 * @fdt: pointer to the device tree blob
905 * @startoffset: only find nodes after this offset
906 * @propname: property name to check
907 * @propval: property value to search for
908 * @proplen: length of the value in propval
910 * fdt_node_offset_by_prop_value() returns the offset of the first
911 * node after startoffset, which has a property named propname whose
912 * value is of length proplen and has value equal to propval; or if
913 * startoffset is -1, the very first such node in the tree.
915 * To iterate through all nodes matching the criterion, the following
917 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
919 * while (offset != -FDT_ERR_NOTFOUND) {
921 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
925 * Note the -1 in the first call to the function, if 0 is used here
926 * instead, the function will never locate the root node, even if it
927 * matches the criterion.
930 * structure block offset of the located node (>= 0, >startoffset),
932 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
933 * tree after startoffset
934 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
936 * -FDT_ERR_BADVERSION,
938 * -FDT_ERR_BADSTRUCTURE, standard meanings
940 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
941 const char *propname,
942 const void *propval, int proplen);
945 * fdt_node_offset_by_phandle - find the node with a given phandle
946 * @fdt: pointer to the device tree blob
947 * @phandle: phandle value
949 * fdt_node_offset_by_phandle() returns the offset of the node
950 * which has the given phandle value. If there is more than one node
951 * in the tree with the given phandle (an invalid tree), results are
955 * structure block offset of the located node (>= 0), on success
956 * -FDT_ERR_NOTFOUND, no node with that phandle exists
957 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
959 * -FDT_ERR_BADVERSION,
961 * -FDT_ERR_BADSTRUCTURE, standard meanings
963 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
966 * fdt_node_check_compatible: check a node's compatible property
967 * @fdt: pointer to the device tree blob
968 * @nodeoffset: offset of a tree node
969 * @compatible: string to match against
972 * fdt_node_check_compatible() returns 0 if the given node contains a
973 * 'compatible' property with the given string as one of its elements,
974 * it returns non-zero otherwise, or on error.
977 * 0, if the node has a 'compatible' property listing the given string
978 * 1, if the node has a 'compatible' property, but it does not list
980 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
981 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
983 * -FDT_ERR_BADVERSION,
985 * -FDT_ERR_BADSTRUCTURE, standard meanings
987 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
988 const char *compatible);
991 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
992 * @fdt: pointer to the device tree blob
993 * @startoffset: only find nodes after this offset
994 * @compatible: 'compatible' string to match against
996 * fdt_node_offset_by_compatible() returns the offset of the first
997 * node after startoffset, which has a 'compatible' property which
998 * lists the given compatible string; or if startoffset is -1, the
999 * very first such node in the tree.
1001 * To iterate through all nodes matching the criterion, the following
1002 * idiom can be used:
1003 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
1004 * while (offset != -FDT_ERR_NOTFOUND) {
1005 * // other code here
1006 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
1009 * Note the -1 in the first call to the function, if 0 is used here
1010 * instead, the function will never locate the root node, even if it
1011 * matches the criterion.
1014 * structure block offset of the located node (>= 0, >startoffset),
1016 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
1017 * tree after startoffset
1018 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
1019 * -FDT_ERR_BADMAGIC,
1020 * -FDT_ERR_BADVERSION,
1021 * -FDT_ERR_BADSTATE,
1022 * -FDT_ERR_BADSTRUCTURE, standard meanings
1024 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
1025 const char *compatible);
1028 * fdt_stringlist_contains - check a string list property for a string
1029 * @strlist: Property containing a list of strings to check
1030 * @listlen: Length of property
1031 * @str: String to search for
1033 * This is a utility function provided for convenience. The list contains
1034 * one or more strings, each terminated by \0, as is found in a device tree
1035 * "compatible" property.
1037 * @return: 1 if the string is found in the list, 0 not found, or invalid list
1039 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
1042 * fdt_stringlist_count - count the number of strings in a string list
1043 * @fdt: pointer to the device tree blob
1044 * @nodeoffset: offset of a tree node
1045 * @property: name of the property containing the string list
1047 * the number of strings in the given property
1048 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1049 * -FDT_ERR_NOTFOUND if the property does not exist
1051 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
1054 * fdt_stringlist_search - find a string in a string list and return its index
1055 * @fdt: pointer to the device tree blob
1056 * @nodeoffset: offset of a tree node
1057 * @property: name of the property containing the string list
1058 * @string: string to look up in the string list
1060 * Note that it is possible for this function to succeed on property values
1061 * that are not NUL-terminated. That's because the function will stop after
1062 * finding the first occurrence of @string. This can for example happen with
1063 * small-valued cell properties, such as #address-cells, when searching for
1067 * the index of the string in the list of strings
1068 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1069 * -FDT_ERR_NOTFOUND if the property does not exist or does not contain
1072 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
1073 const char *string);
1076 * fdt_stringlist_get() - obtain the string at a given index in a string list
1077 * @fdt: pointer to the device tree blob
1078 * @nodeoffset: offset of a tree node
1079 * @property: name of the property containing the string list
1080 * @index: index of the string to return
1081 * @lenp: return location for the string length or an error code on failure
1083 * Note that this will successfully extract strings from properties with
1084 * non-NUL-terminated values. For example on small-valued cell properties
1085 * this function will return the empty string.
1087 * If non-NULL, the length of the string (on success) or a negative error-code
1088 * (on failure) will be stored in the integer pointer to by lenp.
1091 * A pointer to the string at the given index in the string list or NULL on
1092 * failure. On success the length of the string will be stored in the memory
1093 * location pointed to by the lenp parameter, if non-NULL. On failure one of
1094 * the following negative error codes will be returned in the lenp parameter
1096 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1097 * -FDT_ERR_NOTFOUND if the property does not exist
1099 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
1100 const char *property, int index,
1103 /**********************************************************************/
1104 /* Read-only functions (addressing related) */
1105 /**********************************************************************/
1108 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
1110 * This is the maximum value for #address-cells, #size-cells and
1111 * similar properties that will be processed by libfdt. IEE1275
1112 * requires that OF implementations handle values up to 4.
1113 * Implementations may support larger values, but in practice higher
1114 * values aren't used.
1116 #define FDT_MAX_NCELLS 4
1119 * fdt_address_cells - retrieve address size for a bus represented in the tree
1120 * @fdt: pointer to the device tree blob
1121 * @nodeoffset: offset of the node to find the address size for
1123 * When the node has a valid #address-cells property, returns its value.
1126 * 0 <= n < FDT_MAX_NCELLS, on success
1127 * 2, if the node has no #address-cells property
1128 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1129 * #address-cells property
1130 * -FDT_ERR_BADMAGIC,
1131 * -FDT_ERR_BADVERSION,
1132 * -FDT_ERR_BADSTATE,
1133 * -FDT_ERR_BADSTRUCTURE,
1134 * -FDT_ERR_TRUNCATED, standard meanings
1136 int fdt_address_cells(const void *fdt, int nodeoffset);
1139 * fdt_size_cells - retrieve address range size for a bus represented in the
1141 * @fdt: pointer to the device tree blob
1142 * @nodeoffset: offset of the node to find the address range size for
1144 * When the node has a valid #size-cells property, returns its value.
1147 * 0 <= n < FDT_MAX_NCELLS, on success
1148 * 2, if the node has no #size-cells property
1149 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1150 * #size-cells property
1151 * -FDT_ERR_BADMAGIC,
1152 * -FDT_ERR_BADVERSION,
1153 * -FDT_ERR_BADSTATE,
1154 * -FDT_ERR_BADSTRUCTURE,
1155 * -FDT_ERR_TRUNCATED, standard meanings
1157 int fdt_size_cells(const void *fdt, int nodeoffset);
1160 /**********************************************************************/
1161 /* Write-in-place functions */
1162 /**********************************************************************/
1165 * fdt_setprop_inplace_namelen_partial - change a property's value,
1167 * @fdt: pointer to the device tree blob
1168 * @nodeoffset: offset of the node whose property to change
1169 * @name: name of the property to change
1170 * @namelen: number of characters of name to consider
1171 * @idx: index of the property to change in the array
1172 * @val: pointer to data to replace the property value with
1173 * @len: length of the property value
1175 * Identical to fdt_setprop_inplace(), but modifies the given property
1176 * starting from the given index, and using only the first characters
1177 * of the name. It is useful when you want to manipulate only one value of
1178 * an array and you have a string that doesn't end with \0.
1180 #ifndef SWIG /* Not available in Python */
1181 int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
1182 const char *name, int namelen,
1183 uint32_t idx, const void *val,
1188 * fdt_setprop_inplace - change a property's value, but not its size
1189 * @fdt: pointer to the device tree blob
1190 * @nodeoffset: offset of the node whose property to change
1191 * @name: name of the property to change
1192 * @val: pointer to data to replace the property value with
1193 * @len: length of the property value
1195 * fdt_setprop_inplace() replaces the value of a given property with
1196 * the data in val, of length len. This function cannot change the
1197 * size of a property, and so will only work if len is equal to the
1198 * current length of the property.
1200 * This function will alter only the bytes in the blob which contain
1201 * the given property value, and will not alter or move any other part
1206 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1207 * -FDT_ERR_NOTFOUND, node does not have the named property
1208 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1209 * -FDT_ERR_BADMAGIC,
1210 * -FDT_ERR_BADVERSION,
1211 * -FDT_ERR_BADSTATE,
1212 * -FDT_ERR_BADSTRUCTURE,
1213 * -FDT_ERR_TRUNCATED, standard meanings
1215 #ifndef SWIG /* Not available in Python */
1216 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1217 const void *val, int len);
1221 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
1222 * @fdt: pointer to the device tree blob
1223 * @nodeoffset: offset of the node whose property to change
1224 * @name: name of the property to change
1225 * @val: 32-bit integer value to replace the property with
1227 * fdt_setprop_inplace_u32() replaces the value of a given property
1228 * with the 32-bit integer value in val, converting val to big-endian
1229 * if necessary. This function cannot change the size of a property,
1230 * and so will only work if the property already exists and has length
1233 * This function will alter only the bytes in the blob which contain
1234 * the given property value, and will not alter or move any other part
1239 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
1240 * -FDT_ERR_NOTFOUND, node does not have the named property
1241 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1242 * -FDT_ERR_BADMAGIC,
1243 * -FDT_ERR_BADVERSION,
1244 * -FDT_ERR_BADSTATE,
1245 * -FDT_ERR_BADSTRUCTURE,
1246 * -FDT_ERR_TRUNCATED, standard meanings
1248 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1249 const char *name, uint32_t val)
1251 fdt32_t tmp = cpu_to_fdt32(val);
1252 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1256 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1257 * @fdt: pointer to the device tree blob
1258 * @nodeoffset: offset of the node whose property to change
1259 * @name: name of the property to change
1260 * @val: 64-bit integer value to replace the property with
1262 * fdt_setprop_inplace_u64() replaces the value of a given property
1263 * with the 64-bit integer value in val, converting val to big-endian
1264 * if necessary. This function cannot change the size of a property,
1265 * and so will only work if the property already exists and has length
1268 * This function will alter only the bytes in the blob which contain
1269 * the given property value, and will not alter or move any other part
1274 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1275 * -FDT_ERR_NOTFOUND, node does not have the named property
1276 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1277 * -FDT_ERR_BADMAGIC,
1278 * -FDT_ERR_BADVERSION,
1279 * -FDT_ERR_BADSTATE,
1280 * -FDT_ERR_BADSTRUCTURE,
1281 * -FDT_ERR_TRUNCATED, standard meanings
1283 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1284 const char *name, uint64_t val)
1286 fdt64_t tmp = cpu_to_fdt64(val);
1287 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1291 * fdt_setprop_inplace_cell - change the value of a single-cell property
1293 * This is an alternative name for fdt_setprop_inplace_u32()
1295 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1296 const char *name, uint32_t val)
1298 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1302 * fdt_nop_property - replace a property with nop tags
1303 * @fdt: pointer to the device tree blob
1304 * @nodeoffset: offset of the node whose property to nop
1305 * @name: name of the property to nop
1307 * fdt_nop_property() will replace a given property's representation
1308 * in the blob with FDT_NOP tags, effectively removing it from the
1311 * This function will alter only the bytes in the blob which contain
1312 * the property, and will not alter or move any other part of the
1317 * -FDT_ERR_NOTFOUND, node does not have the named property
1318 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1319 * -FDT_ERR_BADMAGIC,
1320 * -FDT_ERR_BADVERSION,
1321 * -FDT_ERR_BADSTATE,
1322 * -FDT_ERR_BADSTRUCTURE,
1323 * -FDT_ERR_TRUNCATED, standard meanings
1325 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1328 * fdt_nop_node - replace a node (subtree) with nop tags
1329 * @fdt: pointer to the device tree blob
1330 * @nodeoffset: offset of the node to nop
1332 * fdt_nop_node() will replace a given node's representation in the
1333 * blob, including all its subnodes, if any, with FDT_NOP tags,
1334 * effectively removing it from the tree.
1336 * This function will alter only the bytes in the blob which contain
1337 * the node and its properties and subnodes, and will not alter or
1338 * move any other part of the tree.
1342 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1343 * -FDT_ERR_BADMAGIC,
1344 * -FDT_ERR_BADVERSION,
1345 * -FDT_ERR_BADSTATE,
1346 * -FDT_ERR_BADSTRUCTURE,
1347 * -FDT_ERR_TRUNCATED, standard meanings
1349 int fdt_nop_node(void *fdt, int nodeoffset);
1351 /**********************************************************************/
1352 /* Sequential write functions */
1353 /**********************************************************************/
1355 int fdt_create(void *buf, int bufsize);
1356 int fdt_resize(void *fdt, void *buf, int bufsize);
1357 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1358 int fdt_finish_reservemap(void *fdt);
1359 int fdt_begin_node(void *fdt, const char *name);
1360 int fdt_property(void *fdt, const char *name, const void *val, int len);
1361 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1363 fdt32_t tmp = cpu_to_fdt32(val);
1364 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1366 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1368 fdt64_t tmp = cpu_to_fdt64(val);
1369 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1372 #ifndef SWIG /* Not available in Python */
1373 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1375 return fdt_property_u32(fdt, name, val);
1380 * fdt_property_placeholder - add a new property and return a ptr to its value
1382 * @fdt: pointer to the device tree blob
1383 * @name: name of property to add
1384 * @len: length of property value in bytes
1385 * @valp: returns a pointer to where where the value should be placed
1389 * -FDT_ERR_BADMAGIC,
1390 * -FDT_ERR_NOSPACE, standard meanings
1392 int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
1394 #define fdt_property_string(fdt, name, str) \
1395 fdt_property(fdt, name, str, strlen(str)+1)
1396 int fdt_end_node(void *fdt);
1397 int fdt_finish(void *fdt);
1399 /**********************************************************************/
1400 /* Read-write functions */
1401 /**********************************************************************/
1403 int fdt_create_empty_tree(void *buf, int bufsize);
1404 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1405 int fdt_pack(void *fdt);
1408 * fdt_add_mem_rsv - add one memory reserve map entry
1409 * @fdt: pointer to the device tree blob
1410 * @address, @size: 64-bit values (native endian)
1412 * Adds a reserve map entry to the given blob reserving a region at
1413 * address address of length size.
1415 * This function will insert data into the reserve map and will
1416 * therefore change the indexes of some entries in the table.
1420 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1421 * contain the new reservation entry
1422 * -FDT_ERR_BADMAGIC,
1423 * -FDT_ERR_BADVERSION,
1424 * -FDT_ERR_BADSTATE,
1425 * -FDT_ERR_BADSTRUCTURE,
1426 * -FDT_ERR_BADLAYOUT,
1427 * -FDT_ERR_TRUNCATED, standard meanings
1429 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1432 * fdt_del_mem_rsv - remove a memory reserve map entry
1433 * @fdt: pointer to the device tree blob
1434 * @n: entry to remove
1436 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1439 * This function will delete data from the reservation table and will
1440 * therefore change the indexes of some entries in the table.
1444 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1445 * are less than n+1 reserve map entries)
1446 * -FDT_ERR_BADMAGIC,
1447 * -FDT_ERR_BADVERSION,
1448 * -FDT_ERR_BADSTATE,
1449 * -FDT_ERR_BADSTRUCTURE,
1450 * -FDT_ERR_BADLAYOUT,
1451 * -FDT_ERR_TRUNCATED, standard meanings
1453 int fdt_del_mem_rsv(void *fdt, int n);
1456 * fdt_set_name - change the name of a given node
1457 * @fdt: pointer to the device tree blob
1458 * @nodeoffset: structure block offset of a node
1459 * @name: name to give the node
1461 * fdt_set_name() replaces the name (including unit address, if any)
1462 * of the given node with the given string. NOTE: this function can't
1463 * efficiently check if the new name is unique amongst the given
1464 * node's siblings; results are undefined if this function is invoked
1465 * with a name equal to one of the given node's siblings.
1467 * This function may insert or delete data from the blob, and will
1468 * therefore change the offsets of some existing nodes.
1472 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1473 * to contain the new name
1474 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1475 * -FDT_ERR_BADMAGIC,
1476 * -FDT_ERR_BADVERSION,
1477 * -FDT_ERR_BADSTATE, standard meanings
1479 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1482 * fdt_setprop - create or change a property
1483 * @fdt: pointer to the device tree blob
1484 * @nodeoffset: offset of the node whose property to change
1485 * @name: name of the property to change
1486 * @val: pointer to data to set the property value to
1487 * @len: length of the property value
1489 * fdt_setprop() sets the value of the named property in the given
1490 * node to the given value and length, creating the property if it
1491 * does not already exist.
1493 * This function may insert or delete data from the blob, and will
1494 * therefore change the offsets of some existing nodes.
1498 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1499 * contain the new property value
1500 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1501 * -FDT_ERR_BADLAYOUT,
1502 * -FDT_ERR_BADMAGIC,
1503 * -FDT_ERR_BADVERSION,
1504 * -FDT_ERR_BADSTATE,
1505 * -FDT_ERR_BADSTRUCTURE,
1506 * -FDT_ERR_BADLAYOUT,
1507 * -FDT_ERR_TRUNCATED, standard meanings
1509 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1510 const void *val, int len);
1513 * fdt_setprop_placeholder - allocate space for a property
1514 * @fdt: pointer to the device tree blob
1515 * @nodeoffset: offset of the node whose property to change
1516 * @name: name of the property to change
1517 * @len: length of the property value
1518 * @prop_data: return pointer to property data
1520 * fdt_setprop_placeholer() allocates the named property in the given node.
1521 * If the property exists it is resized. In either case a pointer to the
1522 * property data is returned.
1524 * This function may insert or delete data from the blob, and will
1525 * therefore change the offsets of some existing nodes.
1529 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1530 * contain the new property value
1531 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1532 * -FDT_ERR_BADLAYOUT,
1533 * -FDT_ERR_BADMAGIC,
1534 * -FDT_ERR_BADVERSION,
1535 * -FDT_ERR_BADSTATE,
1536 * -FDT_ERR_BADSTRUCTURE,
1537 * -FDT_ERR_BADLAYOUT,
1538 * -FDT_ERR_TRUNCATED, standard meanings
1540 int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
1541 int len, void **prop_data);
1544 * fdt_setprop_u32 - set a property to a 32-bit integer
1545 * @fdt: pointer to the device tree blob
1546 * @nodeoffset: offset of the node whose property to change
1547 * @name: name of the property to change
1548 * @val: 32-bit integer value for the property (native endian)
1550 * fdt_setprop_u32() sets the value of the named property in the given
1551 * node to the given 32-bit integer value (converting to big-endian if
1552 * necessary), or creates a new property with that value if it does
1553 * not already exist.
1555 * This function may insert or delete data from the blob, and will
1556 * therefore change the offsets of some existing nodes.
1560 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1561 * contain the new property value
1562 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1563 * -FDT_ERR_BADLAYOUT,
1564 * -FDT_ERR_BADMAGIC,
1565 * -FDT_ERR_BADVERSION,
1566 * -FDT_ERR_BADSTATE,
1567 * -FDT_ERR_BADSTRUCTURE,
1568 * -FDT_ERR_BADLAYOUT,
1569 * -FDT_ERR_TRUNCATED, standard meanings
1571 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1574 fdt32_t tmp = cpu_to_fdt32(val);
1575 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1579 * fdt_setprop_u64 - set a property to a 64-bit integer
1580 * @fdt: pointer to the device tree blob
1581 * @nodeoffset: offset of the node whose property to change
1582 * @name: name of the property to change
1583 * @val: 64-bit integer value for the property (native endian)
1585 * fdt_setprop_u64() sets the value of the named property in the given
1586 * node to the given 64-bit integer value (converting to big-endian if
1587 * necessary), or creates a new property with that value if it does
1588 * not already exist.
1590 * This function may insert or delete data from the blob, and will
1591 * therefore change the offsets of some existing nodes.
1595 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1596 * contain the new property value
1597 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1598 * -FDT_ERR_BADLAYOUT,
1599 * -FDT_ERR_BADMAGIC,
1600 * -FDT_ERR_BADVERSION,
1601 * -FDT_ERR_BADSTATE,
1602 * -FDT_ERR_BADSTRUCTURE,
1603 * -FDT_ERR_BADLAYOUT,
1604 * -FDT_ERR_TRUNCATED, standard meanings
1606 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1609 fdt64_t tmp = cpu_to_fdt64(val);
1610 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1614 * fdt_setprop_cell - set a property to a single cell value
1616 * This is an alternative name for fdt_setprop_u32()
1618 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1621 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1625 * fdt_setprop_string - set a property to a string value
1626 * @fdt: pointer to the device tree blob
1627 * @nodeoffset: offset of the node whose property to change
1628 * @name: name of the property to change
1629 * @str: string value for the property
1631 * fdt_setprop_string() sets the value of the named property in the
1632 * given node to the given string value (using the length of the
1633 * string to determine the new length of the property), or creates a
1634 * new property with that value if it does not already exist.
1636 * This function may insert or delete data from the blob, and will
1637 * therefore change the offsets of some existing nodes.
1641 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1642 * contain the new property value
1643 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1644 * -FDT_ERR_BADLAYOUT,
1645 * -FDT_ERR_BADMAGIC,
1646 * -FDT_ERR_BADVERSION,
1647 * -FDT_ERR_BADSTATE,
1648 * -FDT_ERR_BADSTRUCTURE,
1649 * -FDT_ERR_BADLAYOUT,
1650 * -FDT_ERR_TRUNCATED, standard meanings
1652 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1653 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1657 * fdt_setprop_empty - set a property to an empty value
1658 * @fdt: pointer to the device tree blob
1659 * @nodeoffset: offset of the node whose property to change
1660 * @name: name of the property to change
1662 * fdt_setprop_empty() sets the value of the named property in the
1663 * given node to an empty (zero length) value, or creates a new empty
1664 * property if it does not already exist.
1666 * This function may insert or delete data from the blob, and will
1667 * therefore change the offsets of some existing nodes.
1671 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1672 * contain the new property value
1673 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1674 * -FDT_ERR_BADLAYOUT,
1675 * -FDT_ERR_BADMAGIC,
1676 * -FDT_ERR_BADVERSION,
1677 * -FDT_ERR_BADSTATE,
1678 * -FDT_ERR_BADSTRUCTURE,
1679 * -FDT_ERR_BADLAYOUT,
1680 * -FDT_ERR_TRUNCATED, standard meanings
1682 #define fdt_setprop_empty(fdt, nodeoffset, name) \
1683 fdt_setprop((fdt), (nodeoffset), (name), NULL, 0)
1686 * fdt_appendprop - append to or create a property
1687 * @fdt: pointer to the device tree blob
1688 * @nodeoffset: offset of the node whose property to change
1689 * @name: name of the property to append to
1690 * @val: pointer to data to append to the property value
1691 * @len: length of the data to append to the property value
1693 * fdt_appendprop() appends the value to the named property in the
1694 * given node, creating the property if it does not already exist.
1696 * This function may insert data into the blob, and will therefore
1697 * change the offsets of some existing nodes.
1701 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1702 * contain the new property value
1703 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1704 * -FDT_ERR_BADLAYOUT,
1705 * -FDT_ERR_BADMAGIC,
1706 * -FDT_ERR_BADVERSION,
1707 * -FDT_ERR_BADSTATE,
1708 * -FDT_ERR_BADSTRUCTURE,
1709 * -FDT_ERR_BADLAYOUT,
1710 * -FDT_ERR_TRUNCATED, standard meanings
1712 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1713 const void *val, int len);
1716 * fdt_appendprop_u32 - append a 32-bit integer value to a property
1717 * @fdt: pointer to the device tree blob
1718 * @nodeoffset: offset of the node whose property to change
1719 * @name: name of the property to change
1720 * @val: 32-bit integer value to append to the property (native endian)
1722 * fdt_appendprop_u32() appends the given 32-bit integer value
1723 * (converting to big-endian if necessary) to the value of the named
1724 * property in the given node, or creates a new property with that
1725 * value if it does not already exist.
1727 * This function may insert data into the blob, and will therefore
1728 * change the offsets of some existing nodes.
1732 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1733 * contain the new property value
1734 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1735 * -FDT_ERR_BADLAYOUT,
1736 * -FDT_ERR_BADMAGIC,
1737 * -FDT_ERR_BADVERSION,
1738 * -FDT_ERR_BADSTATE,
1739 * -FDT_ERR_BADSTRUCTURE,
1740 * -FDT_ERR_BADLAYOUT,
1741 * -FDT_ERR_TRUNCATED, standard meanings
1743 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1744 const char *name, uint32_t val)
1746 fdt32_t tmp = cpu_to_fdt32(val);
1747 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1751 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1752 * @fdt: pointer to the device tree blob
1753 * @nodeoffset: offset of the node whose property to change
1754 * @name: name of the property to change
1755 * @val: 64-bit integer value to append to the property (native endian)
1757 * fdt_appendprop_u64() appends the given 64-bit integer value
1758 * (converting to big-endian if necessary) to the value of the named
1759 * property in the given node, or creates a new property with that
1760 * value if it does not already exist.
1762 * This function may insert data into the blob, and will therefore
1763 * change the offsets of some existing nodes.
1767 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1768 * contain the new property value
1769 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1770 * -FDT_ERR_BADLAYOUT,
1771 * -FDT_ERR_BADMAGIC,
1772 * -FDT_ERR_BADVERSION,
1773 * -FDT_ERR_BADSTATE,
1774 * -FDT_ERR_BADSTRUCTURE,
1775 * -FDT_ERR_BADLAYOUT,
1776 * -FDT_ERR_TRUNCATED, standard meanings
1778 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1779 const char *name, uint64_t val)
1781 fdt64_t tmp = cpu_to_fdt64(val);
1782 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1786 * fdt_appendprop_cell - append a single cell value to a property
1788 * This is an alternative name for fdt_appendprop_u32()
1790 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1791 const char *name, uint32_t val)
1793 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1797 * fdt_appendprop_string - append a string to a property
1798 * @fdt: pointer to the device tree blob
1799 * @nodeoffset: offset of the node whose property to change
1800 * @name: name of the property to change
1801 * @str: string value to append to the property
1803 * fdt_appendprop_string() appends the given string to the value of
1804 * the named property in the given node, or creates a new property
1805 * with that value if it does not already exist.
1807 * This function may insert data into the blob, and will therefore
1808 * change the offsets of some existing nodes.
1812 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1813 * contain the new property value
1814 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1815 * -FDT_ERR_BADLAYOUT,
1816 * -FDT_ERR_BADMAGIC,
1817 * -FDT_ERR_BADVERSION,
1818 * -FDT_ERR_BADSTATE,
1819 * -FDT_ERR_BADSTRUCTURE,
1820 * -FDT_ERR_BADLAYOUT,
1821 * -FDT_ERR_TRUNCATED, standard meanings
1823 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1824 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1827 * fdt_delprop - delete a property
1828 * @fdt: pointer to the device tree blob
1829 * @nodeoffset: offset of the node whose property to nop
1830 * @name: name of the property to nop
1832 * fdt_del_property() will delete the given property.
1834 * This function will delete data from the blob, and will therefore
1835 * change the offsets of some existing nodes.
1839 * -FDT_ERR_NOTFOUND, node does not have the named property
1840 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1841 * -FDT_ERR_BADLAYOUT,
1842 * -FDT_ERR_BADMAGIC,
1843 * -FDT_ERR_BADVERSION,
1844 * -FDT_ERR_BADSTATE,
1845 * -FDT_ERR_BADSTRUCTURE,
1846 * -FDT_ERR_TRUNCATED, standard meanings
1848 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1851 * fdt_add_subnode_namelen - creates a new node based on substring
1852 * @fdt: pointer to the device tree blob
1853 * @parentoffset: structure block offset of a node
1854 * @name: name of the subnode to locate
1855 * @namelen: number of characters of name to consider
1857 * Identical to fdt_add_subnode(), but use only the first namelen
1858 * characters of name as the name of the new node. This is useful for
1859 * creating subnodes based on a portion of a larger string, such as a
1862 #ifndef SWIG /* Not available in Python */
1863 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1864 const char *name, int namelen);
1868 * fdt_add_subnode - creates a new node
1869 * @fdt: pointer to the device tree blob
1870 * @parentoffset: structure block offset of a node
1871 * @name: name of the subnode to locate
1873 * fdt_add_subnode() creates a new node as a subnode of the node at
1874 * structure block offset parentoffset, with the given name (which
1875 * should include the unit address, if any).
1877 * This function will insert data into the blob, and will therefore
1878 * change the offsets of some existing nodes.
1881 * structure block offset of the created nodeequested subnode (>=0), on
1883 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1884 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
1886 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1888 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1889 * blob to contain the new node
1891 * -FDT_ERR_BADLAYOUT
1892 * -FDT_ERR_BADMAGIC,
1893 * -FDT_ERR_BADVERSION,
1894 * -FDT_ERR_BADSTATE,
1895 * -FDT_ERR_BADSTRUCTURE,
1896 * -FDT_ERR_TRUNCATED, standard meanings.
1898 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1901 * fdt_del_node - delete a node (subtree)
1902 * @fdt: pointer to the device tree blob
1903 * @nodeoffset: offset of the node to nop
1905 * fdt_del_node() will remove the given node, including all its
1906 * subnodes if any, from the blob.
1908 * This function will delete data from the blob, and will therefore
1909 * change the offsets of some existing nodes.
1913 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1914 * -FDT_ERR_BADLAYOUT,
1915 * -FDT_ERR_BADMAGIC,
1916 * -FDT_ERR_BADVERSION,
1917 * -FDT_ERR_BADSTATE,
1918 * -FDT_ERR_BADSTRUCTURE,
1919 * -FDT_ERR_TRUNCATED, standard meanings
1921 int fdt_del_node(void *fdt, int nodeoffset);
1924 * fdt_overlay_apply - Applies a DT overlay on a base DT
1925 * @fdt: pointer to the base device tree blob
1926 * @fdto: pointer to the device tree overlay blob
1928 * fdt_overlay_apply() will apply the given device tree overlay on the
1929 * given base device tree.
1931 * Expect the base device tree to be modified, even if the function
1936 * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
1937 * -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or
1938 * properties in the base DT
1939 * -FDT_ERR_BADPHANDLE,
1940 * -FDT_ERR_BADOVERLAY,
1941 * -FDT_ERR_NOPHANDLES,
1942 * -FDT_ERR_INTERNAL,
1943 * -FDT_ERR_BADLAYOUT,
1944 * -FDT_ERR_BADMAGIC,
1945 * -FDT_ERR_BADOFFSET,
1947 * -FDT_ERR_BADVERSION,
1948 * -FDT_ERR_BADSTRUCTURE,
1949 * -FDT_ERR_BADSTATE,
1950 * -FDT_ERR_TRUNCATED, standard meanings
1952 int fdt_overlay_apply(void *fdt, void *fdto);
1954 /**********************************************************************/
1955 /* Debugging / informational functions */
1956 /**********************************************************************/
1958 const char *fdt_strerror(int errval);
1960 #endif /* LIBFDT_H */