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 0x10
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: Structure block of the given device tree
94 * ends without an FDT_END tag. */
95 #define FDT_ERR_BADMAGIC 9
96 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
97 * device tree at all - it is missing the flattened device
98 * tree magic number. */
99 #define FDT_ERR_BADVERSION 10
100 /* FDT_ERR_BADVERSION: Given device tree has a version which
101 * can't be handled by the requested operation. For
102 * read-write functions, this may mean that fdt_open_into() is
103 * required to convert the tree to the expected version. */
104 #define FDT_ERR_BADSTRUCTURE 11
105 /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
106 * structure block or other serious error (e.g. misnested
107 * nodes, or subnodes preceding properties). */
108 #define FDT_ERR_BADLAYOUT 12
109 /* FDT_ERR_BADLAYOUT: For read-write functions, the given
110 * device tree has it's sub-blocks in an order that the
111 * function can't handle (memory reserve map, then structure,
112 * then strings). Use fdt_open_into() to reorganize the tree
113 * into a form suitable for the read-write operations. */
115 /* "Can't happen" error indicating a bug in libfdt */
116 #define FDT_ERR_INTERNAL 13
117 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
118 * Should never be returned, if it is, it indicates a bug in
121 /* Errors in device tree content */
122 #define FDT_ERR_BADNCELLS 14
123 /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
124 * or similar property with a bad format or value */
126 #define FDT_ERR_BADVALUE 15
127 /* FDT_ERR_BADVALUE: Device tree has a property with an unexpected
128 * value. For example: a property expected to contain a string list
129 * is not NUL-terminated within the length of its value. */
131 #define FDT_ERR_BADOVERLAY 16
132 /* FDT_ERR_BADOVERLAY: The device tree overlay, while
133 * correctly structured, cannot be applied due to some
134 * unexpected or missing value, property or node. */
136 #define FDT_ERR_NOPHANDLES 17
137 /* FDT_ERR_NOPHANDLES: The device tree doesn't have any
138 * phandle available anymore without causing an overflow */
140 #define FDT_ERR_MAX 17
142 /**********************************************************************/
143 /* Low-level functions (you probably don't need these) */
144 /**********************************************************************/
146 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
147 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
149 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
152 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
154 /**********************************************************************/
155 /* Traversal functions */
156 /**********************************************************************/
158 int fdt_next_node(const void *fdt, int offset, int *depth);
161 * fdt_first_subnode() - get offset of first direct subnode
164 * @offset: Offset of node to check
165 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
167 int fdt_first_subnode(const void *fdt, int offset);
170 * fdt_next_subnode() - get offset of next direct subnode
172 * After first calling fdt_first_subnode(), call this function repeatedly to
173 * get direct subnodes of a parent node.
176 * @offset: Offset of previous subnode
177 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
180 int fdt_next_subnode(const void *fdt, int offset);
183 * fdt_for_each_subnode - iterate over all subnodes of a parent
185 * @node: child node (int, lvalue)
186 * @fdt: FDT blob (const void *)
187 * @parent: parent node (int)
189 * This is actually a wrapper around a for loop and would be used like so:
191 * fdt_for_each_subnode(node, fdt, parent) {
196 * if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) {
200 * Note that this is implemented as a macro and @node is used as
201 * iterator in the loop. The parent variable be constant or even a
205 #define fdt_for_each_subnode(node, fdt, parent) \
206 for (node = fdt_first_subnode(fdt, parent); \
208 node = fdt_next_subnode(fdt, node))
210 /**********************************************************************/
211 /* General functions */
212 /**********************************************************************/
214 #define fdt_get_header(fdt, field) \
215 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
216 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
217 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
218 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
219 #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
220 #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
221 #define fdt_version(fdt) (fdt_get_header(fdt, version))
222 #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
223 #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
224 #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
225 #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
227 #define __fdt_set_hdr(name) \
228 static inline void fdt_set_##name(void *fdt, uint32_t val) \
230 struct fdt_header *fdth = (struct fdt_header *)fdt; \
231 fdth->name = cpu_to_fdt32(val); \
233 __fdt_set_hdr(magic);
234 __fdt_set_hdr(totalsize);
235 __fdt_set_hdr(off_dt_struct);
236 __fdt_set_hdr(off_dt_strings);
237 __fdt_set_hdr(off_mem_rsvmap);
238 __fdt_set_hdr(version);
239 __fdt_set_hdr(last_comp_version);
240 __fdt_set_hdr(boot_cpuid_phys);
241 __fdt_set_hdr(size_dt_strings);
242 __fdt_set_hdr(size_dt_struct);
246 * fdt_check_header - sanity check a device tree or possible device tree
247 * @fdt: pointer to data which might be a flattened device tree
249 * fdt_check_header() checks that the given buffer contains what
250 * appears to be a flattened device tree with sane information in its
254 * 0, if the buffer appears to contain a valid device tree
256 * -FDT_ERR_BADVERSION,
257 * -FDT_ERR_BADSTATE, standard meanings, as above
259 int fdt_check_header(const void *fdt);
262 * fdt_move - move a device tree around in memory
263 * @fdt: pointer to the device tree to move
264 * @buf: pointer to memory where the device is to be moved
265 * @bufsize: size of the memory space at buf
267 * fdt_move() relocates, if possible, the device tree blob located at
268 * fdt to the buffer at buf of size bufsize. The buffer may overlap
269 * with the existing device tree blob at fdt. Therefore,
270 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
271 * should always succeed.
275 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
277 * -FDT_ERR_BADVERSION,
278 * -FDT_ERR_BADSTATE, standard meanings
280 int fdt_move(const void *fdt, void *buf, int bufsize);
282 /**********************************************************************/
283 /* Read-only functions */
284 /**********************************************************************/
287 * fdt_string - retrieve a string from the strings block of a device tree
288 * @fdt: pointer to the device tree blob
289 * @stroffset: offset of the string within the strings block (native endian)
291 * fdt_string() retrieves a pointer to a single string from the
292 * strings block of the device tree blob at fdt.
295 * a pointer to the string, on success
296 * NULL, if stroffset is out of bounds
298 const char *fdt_string(const void *fdt, int stroffset);
301 * fdt_get_max_phandle - retrieves the highest phandle in a tree
302 * @fdt: pointer to the device tree blob
304 * fdt_get_max_phandle retrieves the highest phandle in the given
305 * device tree. This will ignore badly formatted phandles, or phandles
306 * with a value of 0 or -1.
309 * the highest phandle on success
310 * 0, if no phandle was found in the device tree
311 * -1, if an error occurred
313 uint32_t fdt_get_max_phandle(const void *fdt);
316 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
317 * @fdt: pointer to the device tree blob
319 * Returns the number of entries in the device tree blob's memory
320 * reservation map. This does not include the terminating 0,0 entry
321 * or any other (0,0) entries reserved for expansion.
324 * the number of entries
326 int fdt_num_mem_rsv(const void *fdt);
329 * fdt_get_mem_rsv - retrieve one memory reserve map entry
330 * @fdt: pointer to the device tree blob
331 * @address, @size: pointers to 64-bit variables
333 * On success, *address and *size will contain the address and size of
334 * the n-th reserve map entry from the device tree blob, in
335 * native-endian format.
340 * -FDT_ERR_BADVERSION,
341 * -FDT_ERR_BADSTATE, standard meanings
343 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
346 * fdt_subnode_offset_namelen - find a subnode based on substring
347 * @fdt: pointer to the device tree blob
348 * @parentoffset: structure block offset of a node
349 * @name: name of the subnode to locate
350 * @namelen: number of characters of name to consider
352 * Identical to fdt_subnode_offset(), but only examine the first
353 * namelen characters of name for matching the subnode name. This is
354 * useful for finding subnodes based on a portion of a larger string,
355 * such as a full path.
357 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
358 const char *name, int namelen);
360 * fdt_subnode_offset - find a subnode of a given node
361 * @fdt: pointer to the device tree blob
362 * @parentoffset: structure block offset of a node
363 * @name: name of the subnode to locate
365 * fdt_subnode_offset() finds a subnode of the node at structure block
366 * offset parentoffset with the given name. name may include a unit
367 * address, in which case fdt_subnode_offset() will find the subnode
368 * with that unit address, or the unit address may be omitted, in
369 * which case fdt_subnode_offset() will find an arbitrary subnode
370 * whose name excluding unit address matches the given name.
373 * structure block offset of the requested subnode (>=0), on success
374 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
375 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
378 * -FDT_ERR_BADVERSION,
380 * -FDT_ERR_BADSTRUCTURE,
381 * -FDT_ERR_TRUNCATED, standard meanings.
383 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
386 * fdt_path_offset_namelen - find a tree node by its full path
387 * @fdt: pointer to the device tree blob
388 * @path: full path of the node to locate
389 * @namelen: number of characters of path to consider
391 * Identical to fdt_path_offset(), but only consider the first namelen
392 * characters of path as the path name.
394 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
397 * fdt_path_offset - find a tree node by its full path
398 * @fdt: pointer to the device tree blob
399 * @path: full path of the node to locate
401 * fdt_path_offset() finds a node of a given path in the device tree.
402 * Each path component may omit the unit address portion, but the
403 * results of this are undefined if any such path component is
404 * ambiguous (that is if there are multiple nodes at the relevant
405 * level matching the given component, differentiated only by unit
409 * structure block offset of the node with the requested path (>=0), on
411 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
412 * -FDT_ERR_NOTFOUND, if the requested node does not exist
414 * -FDT_ERR_BADVERSION,
416 * -FDT_ERR_BADSTRUCTURE,
417 * -FDT_ERR_TRUNCATED, standard meanings.
419 int fdt_path_offset(const void *fdt, const char *path);
422 * fdt_get_name - retrieve the name of a given node
423 * @fdt: pointer to the device tree blob
424 * @nodeoffset: structure block offset of the starting node
425 * @lenp: pointer to an integer variable (will be overwritten) or NULL
427 * fdt_get_name() retrieves the name (including unit address) of the
428 * device tree node at structure block offset nodeoffset. If lenp is
429 * non-NULL, the length of this name is also returned, in the integer
430 * pointed to by lenp.
433 * pointer to the node's name, on success
434 * If lenp is non-NULL, *lenp contains the length of that name
437 * if lenp is non-NULL *lenp contains an error code (<0):
438 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
441 * -FDT_ERR_BADVERSION,
442 * -FDT_ERR_BADSTATE, standard meanings
444 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
447 * fdt_first_property_offset - find the offset of a node's first property
448 * @fdt: pointer to the device tree blob
449 * @nodeoffset: structure block offset of a node
451 * fdt_first_property_offset() finds the first property of the node at
452 * the given structure block offset.
455 * structure block offset of the property (>=0), on success
456 * -FDT_ERR_NOTFOUND, if the requested node has no properties
457 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
459 * -FDT_ERR_BADVERSION,
461 * -FDT_ERR_BADSTRUCTURE,
462 * -FDT_ERR_TRUNCATED, standard meanings.
464 int fdt_first_property_offset(const void *fdt, int nodeoffset);
467 * fdt_next_property_offset - step through a node's properties
468 * @fdt: pointer to the device tree blob
469 * @offset: structure block offset of a property
471 * fdt_next_property_offset() finds the property immediately after the
472 * one at the given structure block offset. This will be a property
473 * of the same node as the given property.
476 * structure block offset of the next property (>=0), on success
477 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
478 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
480 * -FDT_ERR_BADVERSION,
482 * -FDT_ERR_BADSTRUCTURE,
483 * -FDT_ERR_TRUNCATED, standard meanings.
485 int fdt_next_property_offset(const void *fdt, int offset);
488 * fdt_for_each_property_offset - iterate over all properties of a node
490 * @property_offset: property offset (int, lvalue)
491 * @fdt: FDT blob (const void *)
492 * @node: node offset (int)
494 * This is actually a wrapper around a for loop and would be used like so:
496 * fdt_for_each_property_offset(property, fdt, node) {
501 * if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) {
505 * Note that this is implemented as a macro and property is used as
506 * iterator in the loop. The node variable can be constant or even a
509 #define fdt_for_each_property_offset(property, fdt, node) \
510 for (property = fdt_first_property_offset(fdt, node); \
512 property = fdt_next_property_offset(fdt, property))
515 * fdt_get_property_by_offset - retrieve the property at a given offset
516 * @fdt: pointer to the device tree blob
517 * @offset: offset of the property to retrieve
518 * @lenp: pointer to an integer variable (will be overwritten) or NULL
520 * fdt_get_property_by_offset() retrieves a pointer to the
521 * fdt_property structure within the device tree blob at the given
522 * offset. If lenp is non-NULL, the length of the property value is
523 * also returned, in the integer pointed to by lenp.
526 * pointer to the structure representing the property
527 * if lenp is non-NULL, *lenp contains the length of the property
530 * if lenp is non-NULL, *lenp contains an error code (<0):
531 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
533 * -FDT_ERR_BADVERSION,
535 * -FDT_ERR_BADSTRUCTURE,
536 * -FDT_ERR_TRUNCATED, standard meanings
538 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
543 * fdt_get_property_namelen - find a property based on substring
544 * @fdt: pointer to the device tree blob
545 * @nodeoffset: offset of the node whose property to find
546 * @name: name of the property to find
547 * @namelen: number of characters of name to consider
548 * @lenp: pointer to an integer variable (will be overwritten) or NULL
550 * Identical to fdt_get_property(), but only examine the first namelen
551 * characters of name for matching the property name.
553 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
556 int namelen, int *lenp);
559 * fdt_get_property - find a given property in a given node
560 * @fdt: pointer to the device tree blob
561 * @nodeoffset: offset of the node whose property to find
562 * @name: name of the property to find
563 * @lenp: pointer to an integer variable (will be overwritten) or NULL
565 * fdt_get_property() retrieves a pointer to the fdt_property
566 * structure within the device tree blob corresponding to the property
567 * named 'name' of the node at offset nodeoffset. If lenp is
568 * non-NULL, the length of the property value is also returned, in the
569 * integer pointed to by lenp.
572 * pointer to the structure representing the property
573 * if lenp is non-NULL, *lenp contains the length of the property
576 * if lenp is non-NULL, *lenp contains an error code (<0):
577 * -FDT_ERR_NOTFOUND, node does not have named property
578 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
581 * -FDT_ERR_BADVERSION,
583 * -FDT_ERR_BADSTRUCTURE,
584 * -FDT_ERR_TRUNCATED, standard meanings
586 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
587 const char *name, int *lenp);
588 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
592 return (struct fdt_property *)(uintptr_t)
593 fdt_get_property(fdt, nodeoffset, name, lenp);
597 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
598 * @fdt: pointer to the device tree blob
599 * @ffset: offset of the property to read
600 * @namep: pointer to a string variable (will be overwritten) or NULL
601 * @lenp: pointer to an integer variable (will be overwritten) or NULL
603 * fdt_getprop_by_offset() retrieves a pointer to the value of the
604 * property at structure block offset 'offset' (this will be a pointer
605 * to within the device blob itself, not a copy of the value). If
606 * lenp is non-NULL, the length of the property value is also
607 * returned, in the integer pointed to by lenp. If namep is non-NULL,
608 * the property's namne will also be returned in the char * pointed to
609 * by namep (this will be a pointer to within the device tree's string
610 * block, not a new copy of the name).
613 * pointer to the property's value
614 * if lenp is non-NULL, *lenp contains the length of the property
616 * if namep is non-NULL *namep contiains a pointer to the property
619 * if lenp is non-NULL, *lenp contains an error code (<0):
620 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
622 * -FDT_ERR_BADVERSION,
624 * -FDT_ERR_BADSTRUCTURE,
625 * -FDT_ERR_TRUNCATED, standard meanings
627 const void *fdt_getprop_by_offset(const void *fdt, int offset,
628 const char **namep, int *lenp);
631 * fdt_getprop_namelen - get property value based on substring
632 * @fdt: pointer to the device tree blob
633 * @nodeoffset: offset of the node whose property to find
634 * @name: name of the property to find
635 * @namelen: number of characters of name to consider
636 * @lenp: pointer to an integer variable (will be overwritten) or NULL
638 * Identical to fdt_getprop(), but only examine the first namelen
639 * characters of name for matching the property name.
641 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
642 const char *name, int namelen, int *lenp);
643 static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
644 const char *name, int namelen,
647 return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
652 * fdt_getprop - retrieve the value of a given property
653 * @fdt: pointer to the device tree blob
654 * @nodeoffset: offset of the node whose property to find
655 * @name: name of the property to find
656 * @lenp: pointer to an integer variable (will be overwritten) or NULL
658 * fdt_getprop() retrieves a pointer to the value of the property
659 * named 'name' of the node at offset nodeoffset (this will be a
660 * pointer to within the device blob itself, not a copy of the value).
661 * If lenp is non-NULL, the length of the property value is also
662 * returned, in the integer pointed to by lenp.
665 * pointer to the property's value
666 * if lenp is non-NULL, *lenp contains the length of the property
669 * if lenp is non-NULL, *lenp contains an error code (<0):
670 * -FDT_ERR_NOTFOUND, node does not have named property
671 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
674 * -FDT_ERR_BADVERSION,
676 * -FDT_ERR_BADSTRUCTURE,
677 * -FDT_ERR_TRUNCATED, standard meanings
679 const void *fdt_getprop(const void *fdt, int nodeoffset,
680 const char *name, int *lenp);
681 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
682 const char *name, int *lenp)
684 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
688 * fdt_get_phandle - retrieve the phandle of a given node
689 * @fdt: pointer to the device tree blob
690 * @nodeoffset: structure block offset of the node
692 * fdt_get_phandle() retrieves the phandle of the device tree node at
693 * structure block offset nodeoffset.
696 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
697 * 0, if the node has no phandle, or another error occurs
699 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
702 * fdt_get_alias_namelen - get alias based on substring
703 * @fdt: pointer to the device tree blob
704 * @name: name of the alias th look up
705 * @namelen: number of characters of name to consider
707 * Identical to fdt_get_alias(), but only examine the first namelen
708 * characters of name for matching the alias name.
710 const char *fdt_get_alias_namelen(const void *fdt,
711 const char *name, int namelen);
714 * fdt_get_alias - retrieve the path referenced by a given alias
715 * @fdt: pointer to the device tree blob
716 * @name: name of the alias th look up
718 * fdt_get_alias() retrieves the value of a given alias. That is, the
719 * value of the property named 'name' in the node /aliases.
722 * a pointer to the expansion of the alias named 'name', if it exists
723 * NULL, if the given alias or the /aliases node does not exist
725 const char *fdt_get_alias(const void *fdt, const char *name);
728 * fdt_get_path - determine the full path of a node
729 * @fdt: pointer to the device tree blob
730 * @nodeoffset: offset of the node whose path to find
731 * @buf: character buffer to contain the returned path (will be overwritten)
732 * @buflen: size of the character buffer at buf
734 * fdt_get_path() computes the full path of the node at offset
735 * nodeoffset, and records that path in the buffer at buf.
737 * NOTE: This function is expensive, as it must scan the device tree
738 * structure from the start to nodeoffset.
742 * buf contains the absolute path of the node at
743 * nodeoffset, as a NUL-terminated string.
744 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
745 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
746 * characters and will not fit in the given buffer.
748 * -FDT_ERR_BADVERSION,
750 * -FDT_ERR_BADSTRUCTURE, standard meanings
752 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
755 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
756 * @fdt: pointer to the device tree blob
757 * @nodeoffset: offset of the node whose parent to find
758 * @supernodedepth: depth of the ancestor to find
759 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
761 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
762 * at a specific depth from the root (where the root itself has depth
763 * 0, its immediate subnodes depth 1 and so forth). So
764 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
765 * will always return 0, the offset of the root node. If the node at
766 * nodeoffset has depth D, then:
767 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
768 * will return nodeoffset itself.
770 * NOTE: This function is expensive, as it must scan the device tree
771 * structure from the start to nodeoffset.
774 * structure block offset of the node at node offset's ancestor
775 * of depth supernodedepth (>=0), on success
776 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
777 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
780 * -FDT_ERR_BADVERSION,
782 * -FDT_ERR_BADSTRUCTURE, standard meanings
784 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
785 int supernodedepth, int *nodedepth);
788 * fdt_node_depth - find the depth of a given node
789 * @fdt: pointer to the device tree blob
790 * @nodeoffset: offset of the node whose parent to find
792 * fdt_node_depth() finds the depth of a given node. The root node
793 * has depth 0, its immediate subnodes depth 1 and so forth.
795 * NOTE: This function is expensive, as it must scan the device tree
796 * structure from the start to nodeoffset.
799 * depth of the node at nodeoffset (>=0), on success
800 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
802 * -FDT_ERR_BADVERSION,
804 * -FDT_ERR_BADSTRUCTURE, standard meanings
806 int fdt_node_depth(const void *fdt, int nodeoffset);
809 * fdt_parent_offset - find the parent of a given node
810 * @fdt: pointer to the device tree blob
811 * @nodeoffset: offset of the node whose parent to find
813 * fdt_parent_offset() locates the parent node of a given node (that
814 * is, it finds the offset of the node which contains the node at
815 * nodeoffset as a subnode).
817 * NOTE: This function is expensive, as it must scan the device tree
818 * structure from the start to nodeoffset, *twice*.
821 * structure block offset of the parent of the node at nodeoffset
823 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
825 * -FDT_ERR_BADVERSION,
827 * -FDT_ERR_BADSTRUCTURE, standard meanings
829 int fdt_parent_offset(const void *fdt, int nodeoffset);
832 * fdt_node_offset_by_prop_value - find nodes with a given property value
833 * @fdt: pointer to the device tree blob
834 * @startoffset: only find nodes after this offset
835 * @propname: property name to check
836 * @propval: property value to search for
837 * @proplen: length of the value in propval
839 * fdt_node_offset_by_prop_value() returns the offset of the first
840 * node after startoffset, which has a property named propname whose
841 * value is of length proplen and has value equal to propval; or if
842 * startoffset is -1, the very first such node in the tree.
844 * To iterate through all nodes matching the criterion, the following
846 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
848 * while (offset != -FDT_ERR_NOTFOUND) {
850 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
854 * Note the -1 in the first call to the function, if 0 is used here
855 * instead, the function will never locate the root node, even if it
856 * matches the criterion.
859 * structure block offset of the located node (>= 0, >startoffset),
861 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
862 * tree after startoffset
863 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
865 * -FDT_ERR_BADVERSION,
867 * -FDT_ERR_BADSTRUCTURE, standard meanings
869 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
870 const char *propname,
871 const void *propval, int proplen);
874 * fdt_node_offset_by_phandle - find the node with a given phandle
875 * @fdt: pointer to the device tree blob
876 * @phandle: phandle value
878 * fdt_node_offset_by_phandle() returns the offset of the node
879 * which has the given phandle value. If there is more than one node
880 * in the tree with the given phandle (an invalid tree), results are
884 * structure block offset of the located node (>= 0), on success
885 * -FDT_ERR_NOTFOUND, no node with that phandle exists
886 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
888 * -FDT_ERR_BADVERSION,
890 * -FDT_ERR_BADSTRUCTURE, standard meanings
892 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
895 * fdt_node_check_compatible: check a node's compatible property
896 * @fdt: pointer to the device tree blob
897 * @nodeoffset: offset of a tree node
898 * @compatible: string to match against
901 * fdt_node_check_compatible() returns 0 if the given node contains a
902 * 'compatible' property with the given string as one of its elements,
903 * it returns non-zero otherwise, or on error.
906 * 0, if the node has a 'compatible' property listing the given string
907 * 1, if the node has a 'compatible' property, but it does not list
909 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
910 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
912 * -FDT_ERR_BADVERSION,
914 * -FDT_ERR_BADSTRUCTURE, standard meanings
916 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
917 const char *compatible);
920 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
921 * @fdt: pointer to the device tree blob
922 * @startoffset: only find nodes after this offset
923 * @compatible: 'compatible' string to match against
925 * fdt_node_offset_by_compatible() returns the offset of the first
926 * node after startoffset, which has a 'compatible' property which
927 * lists the given compatible string; or if startoffset is -1, the
928 * very first such node in the tree.
930 * To iterate through all nodes matching the criterion, the following
932 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
933 * while (offset != -FDT_ERR_NOTFOUND) {
935 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
938 * Note the -1 in the first call to the function, if 0 is used here
939 * instead, the function will never locate the root node, even if it
940 * matches the criterion.
943 * structure block offset of the located node (>= 0, >startoffset),
945 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
946 * tree after startoffset
947 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
949 * -FDT_ERR_BADVERSION,
951 * -FDT_ERR_BADSTRUCTURE, standard meanings
953 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
954 const char *compatible);
957 * fdt_stringlist_contains - check a string list property for a string
958 * @strlist: Property containing a list of strings to check
959 * @listlen: Length of property
960 * @str: String to search for
962 * This is a utility function provided for convenience. The list contains
963 * one or more strings, each terminated by \0, as is found in a device tree
964 * "compatible" property.
966 * @return: 1 if the string is found in the list, 0 not found, or invalid list
968 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
971 * fdt_stringlist_count - count the number of strings in a string list
972 * @fdt: pointer to the device tree blob
973 * @nodeoffset: offset of a tree node
974 * @property: name of the property containing the string list
976 * the number of strings in the given property
977 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
978 * -FDT_ERR_NOTFOUND if the property does not exist
980 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
983 * fdt_stringlist_search - find a string in a string list and return its index
984 * @fdt: pointer to the device tree blob
985 * @nodeoffset: offset of a tree node
986 * @property: name of the property containing the string list
987 * @string: string to look up in the string list
989 * Note that it is possible for this function to succeed on property values
990 * that are not NUL-terminated. That's because the function will stop after
991 * finding the first occurrence of @string. This can for example happen with
992 * small-valued cell properties, such as #address-cells, when searching for
996 * the index of the string in the list of strings
997 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
998 * -FDT_ERR_NOTFOUND if the property does not exist or does not contain
1001 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
1002 const char *string);
1005 * fdt_stringlist_get() - obtain the string at a given index in a string list
1006 * @fdt: pointer to the device tree blob
1007 * @nodeoffset: offset of a tree node
1008 * @property: name of the property containing the string list
1009 * @index: index of the string to return
1010 * @lenp: return location for the string length or an error code on failure
1012 * Note that this will successfully extract strings from properties with
1013 * non-NUL-terminated values. For example on small-valued cell properties
1014 * this function will return the empty string.
1016 * If non-NULL, the length of the string (on success) or a negative error-code
1017 * (on failure) will be stored in the integer pointer to by lenp.
1020 * A pointer to the string at the given index in the string list or NULL on
1021 * failure. On success the length of the string will be stored in the memory
1022 * location pointed to by the lenp parameter, if non-NULL. On failure one of
1023 * the following negative error codes will be returned in the lenp parameter
1025 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1026 * -FDT_ERR_NOTFOUND if the property does not exist
1028 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
1029 const char *property, int index,
1032 /**********************************************************************/
1033 /* Read-only functions (addressing related) */
1034 /**********************************************************************/
1037 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
1039 * This is the maximum value for #address-cells, #size-cells and
1040 * similar properties that will be processed by libfdt. IEE1275
1041 * requires that OF implementations handle values up to 4.
1042 * Implementations may support larger values, but in practice higher
1043 * values aren't used.
1045 #define FDT_MAX_NCELLS 4
1048 * fdt_address_cells - retrieve address size for a bus represented in the tree
1049 * @fdt: pointer to the device tree blob
1050 * @nodeoffset: offset of the node to find the address size for
1052 * When the node has a valid #address-cells property, returns its value.
1055 * 0 <= n < FDT_MAX_NCELLS, on success
1056 * 2, if the node has no #address-cells property
1057 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1058 * #address-cells property
1059 * -FDT_ERR_BADMAGIC,
1060 * -FDT_ERR_BADVERSION,
1061 * -FDT_ERR_BADSTATE,
1062 * -FDT_ERR_BADSTRUCTURE,
1063 * -FDT_ERR_TRUNCATED, standard meanings
1065 int fdt_address_cells(const void *fdt, int nodeoffset);
1068 * fdt_size_cells - retrieve address range size for a bus represented in the
1070 * @fdt: pointer to the device tree blob
1071 * @nodeoffset: offset of the node to find the address range size for
1073 * When the node has a valid #size-cells property, returns its value.
1076 * 0 <= n < FDT_MAX_NCELLS, on success
1077 * 2, if the node has no #address-cells property
1078 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1079 * #size-cells property
1080 * -FDT_ERR_BADMAGIC,
1081 * -FDT_ERR_BADVERSION,
1082 * -FDT_ERR_BADSTATE,
1083 * -FDT_ERR_BADSTRUCTURE,
1084 * -FDT_ERR_TRUNCATED, standard meanings
1086 int fdt_size_cells(const void *fdt, int nodeoffset);
1089 /**********************************************************************/
1090 /* Write-in-place functions */
1091 /**********************************************************************/
1094 * fdt_setprop_inplace_namelen_partial - change a property's value,
1096 * @fdt: pointer to the device tree blob
1097 * @nodeoffset: offset of the node whose property to change
1098 * @name: name of the property to change
1099 * @namelen: number of characters of name to consider
1100 * @idx: index of the property to change in the array
1101 * @val: pointer to data to replace the property value with
1102 * @len: length of the property value
1104 * Identical to fdt_setprop_inplace(), but modifies the given property
1105 * starting from the given index, and using only the first characters
1106 * of the name. It is useful when you want to manipulate only one value of
1107 * an array and you have a string that doesn't end with \0.
1109 int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
1110 const char *name, int namelen,
1111 uint32_t idx, const void *val,
1115 * fdt_setprop_inplace - change a property's value, but not its size
1116 * @fdt: pointer to the device tree blob
1117 * @nodeoffset: offset of the node whose property to change
1118 * @name: name of the property to change
1119 * @val: pointer to data to replace the property value with
1120 * @len: length of the property value
1122 * fdt_setprop_inplace() replaces the value of a given property with
1123 * the data in val, of length len. This function cannot change the
1124 * size of a property, and so will only work if len is equal to the
1125 * current length of the property.
1127 * This function will alter only the bytes in the blob which contain
1128 * the given property value, and will not alter or move any other part
1133 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1134 * -FDT_ERR_NOTFOUND, node does not have the named property
1135 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1136 * -FDT_ERR_BADMAGIC,
1137 * -FDT_ERR_BADVERSION,
1138 * -FDT_ERR_BADSTATE,
1139 * -FDT_ERR_BADSTRUCTURE,
1140 * -FDT_ERR_TRUNCATED, standard meanings
1142 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1143 const void *val, int len);
1146 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
1147 * @fdt: pointer to the device tree blob
1148 * @nodeoffset: offset of the node whose property to change
1149 * @name: name of the property to change
1150 * @val: 32-bit integer value to replace the property with
1152 * fdt_setprop_inplace_u32() replaces the value of a given property
1153 * with the 32-bit integer value in val, converting val to big-endian
1154 * if necessary. This function cannot change the size of a property,
1155 * and so will only work if the property already exists and has length
1158 * This function will alter only the bytes in the blob which contain
1159 * the given property value, and will not alter or move any other part
1164 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
1165 * -FDT_ERR_NOTFOUND, node does not have the named property
1166 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1167 * -FDT_ERR_BADMAGIC,
1168 * -FDT_ERR_BADVERSION,
1169 * -FDT_ERR_BADSTATE,
1170 * -FDT_ERR_BADSTRUCTURE,
1171 * -FDT_ERR_TRUNCATED, standard meanings
1173 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1174 const char *name, uint32_t val)
1176 fdt32_t tmp = cpu_to_fdt32(val);
1177 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1181 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1182 * @fdt: pointer to the device tree blob
1183 * @nodeoffset: offset of the node whose property to change
1184 * @name: name of the property to change
1185 * @val: 64-bit integer value to replace the property with
1187 * fdt_setprop_inplace_u64() replaces the value of a given property
1188 * with the 64-bit integer value in val, converting val to big-endian
1189 * if necessary. This function cannot change the size of a property,
1190 * and so will only work if the property already exists and has length
1193 * This function will alter only the bytes in the blob which contain
1194 * the given property value, and will not alter or move any other part
1199 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1200 * -FDT_ERR_NOTFOUND, node does not have the named property
1201 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1202 * -FDT_ERR_BADMAGIC,
1203 * -FDT_ERR_BADVERSION,
1204 * -FDT_ERR_BADSTATE,
1205 * -FDT_ERR_BADSTRUCTURE,
1206 * -FDT_ERR_TRUNCATED, standard meanings
1208 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1209 const char *name, uint64_t val)
1211 fdt64_t tmp = cpu_to_fdt64(val);
1212 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1216 * fdt_setprop_inplace_cell - change the value of a single-cell property
1218 * This is an alternative name for fdt_setprop_inplace_u32()
1220 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1221 const char *name, uint32_t val)
1223 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1227 * fdt_nop_property - replace a property with nop tags
1228 * @fdt: pointer to the device tree blob
1229 * @nodeoffset: offset of the node whose property to nop
1230 * @name: name of the property to nop
1232 * fdt_nop_property() will replace a given property's representation
1233 * in the blob with FDT_NOP tags, effectively removing it from the
1236 * This function will alter only the bytes in the blob which contain
1237 * the property, and will not alter or move any other part of the
1242 * -FDT_ERR_NOTFOUND, node does not have the named property
1243 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1244 * -FDT_ERR_BADMAGIC,
1245 * -FDT_ERR_BADVERSION,
1246 * -FDT_ERR_BADSTATE,
1247 * -FDT_ERR_BADSTRUCTURE,
1248 * -FDT_ERR_TRUNCATED, standard meanings
1250 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1253 * fdt_nop_node - replace a node (subtree) with nop tags
1254 * @fdt: pointer to the device tree blob
1255 * @nodeoffset: offset of the node to nop
1257 * fdt_nop_node() will replace a given node's representation in the
1258 * blob, including all its subnodes, if any, with FDT_NOP tags,
1259 * effectively removing it from the tree.
1261 * This function will alter only the bytes in the blob which contain
1262 * the node and its properties and subnodes, and will not alter or
1263 * move any other part of the tree.
1267 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1268 * -FDT_ERR_BADMAGIC,
1269 * -FDT_ERR_BADVERSION,
1270 * -FDT_ERR_BADSTATE,
1271 * -FDT_ERR_BADSTRUCTURE,
1272 * -FDT_ERR_TRUNCATED, standard meanings
1274 int fdt_nop_node(void *fdt, int nodeoffset);
1276 /**********************************************************************/
1277 /* Sequential write functions */
1278 /**********************************************************************/
1280 int fdt_create(void *buf, int bufsize);
1281 int fdt_resize(void *fdt, void *buf, int bufsize);
1282 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1283 int fdt_finish_reservemap(void *fdt);
1284 int fdt_begin_node(void *fdt, const char *name);
1285 int fdt_property(void *fdt, const char *name, const void *val, int len);
1286 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1288 fdt32_t tmp = cpu_to_fdt32(val);
1289 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1291 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1293 fdt64_t tmp = cpu_to_fdt64(val);
1294 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1296 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1298 return fdt_property_u32(fdt, name, val);
1300 #define fdt_property_string(fdt, name, str) \
1301 fdt_property(fdt, name, str, strlen(str)+1)
1302 int fdt_end_node(void *fdt);
1303 int fdt_finish(void *fdt);
1305 /**********************************************************************/
1306 /* Read-write functions */
1307 /**********************************************************************/
1309 int fdt_create_empty_tree(void *buf, int bufsize);
1310 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1311 int fdt_pack(void *fdt);
1314 * fdt_add_mem_rsv - add one memory reserve map entry
1315 * @fdt: pointer to the device tree blob
1316 * @address, @size: 64-bit values (native endian)
1318 * Adds a reserve map entry to the given blob reserving a region at
1319 * address address of length size.
1321 * This function will insert data into the reserve map and will
1322 * therefore change the indexes of some entries in the table.
1326 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1327 * contain the new reservation entry
1328 * -FDT_ERR_BADMAGIC,
1329 * -FDT_ERR_BADVERSION,
1330 * -FDT_ERR_BADSTATE,
1331 * -FDT_ERR_BADSTRUCTURE,
1332 * -FDT_ERR_BADLAYOUT,
1333 * -FDT_ERR_TRUNCATED, standard meanings
1335 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1338 * fdt_del_mem_rsv - remove a memory reserve map entry
1339 * @fdt: pointer to the device tree blob
1340 * @n: entry to remove
1342 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1345 * This function will delete data from the reservation table and will
1346 * therefore change the indexes of some entries in the table.
1350 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1351 * are less than n+1 reserve map entries)
1352 * -FDT_ERR_BADMAGIC,
1353 * -FDT_ERR_BADVERSION,
1354 * -FDT_ERR_BADSTATE,
1355 * -FDT_ERR_BADSTRUCTURE,
1356 * -FDT_ERR_BADLAYOUT,
1357 * -FDT_ERR_TRUNCATED, standard meanings
1359 int fdt_del_mem_rsv(void *fdt, int n);
1362 * fdt_set_name - change the name of a given node
1363 * @fdt: pointer to the device tree blob
1364 * @nodeoffset: structure block offset of a node
1365 * @name: name to give the node
1367 * fdt_set_name() replaces the name (including unit address, if any)
1368 * of the given node with the given string. NOTE: this function can't
1369 * efficiently check if the new name is unique amongst the given
1370 * node's siblings; results are undefined if this function is invoked
1371 * with a name equal to one of the given node's siblings.
1373 * This function may insert or delete data from the blob, and will
1374 * therefore change the offsets of some existing nodes.
1378 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1379 * to contain the new name
1380 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1381 * -FDT_ERR_BADMAGIC,
1382 * -FDT_ERR_BADVERSION,
1383 * -FDT_ERR_BADSTATE, standard meanings
1385 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1388 * fdt_setprop - create or change a property
1389 * @fdt: pointer to the device tree blob
1390 * @nodeoffset: offset of the node whose property to change
1391 * @name: name of the property to change
1392 * @val: pointer to data to set the property value to
1393 * @len: length of the property value
1395 * fdt_setprop() sets the value of the named property in the given
1396 * node to the given value and length, creating the property if it
1397 * does not already exist.
1399 * This function may insert or delete data from the blob, and will
1400 * therefore change the offsets of some existing nodes.
1404 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1405 * contain the new property value
1406 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1407 * -FDT_ERR_BADLAYOUT,
1408 * -FDT_ERR_BADMAGIC,
1409 * -FDT_ERR_BADVERSION,
1410 * -FDT_ERR_BADSTATE,
1411 * -FDT_ERR_BADSTRUCTURE,
1412 * -FDT_ERR_BADLAYOUT,
1413 * -FDT_ERR_TRUNCATED, standard meanings
1415 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1416 const void *val, int len);
1419 * fdt_setprop_u32 - set a property to a 32-bit integer
1420 * @fdt: pointer to the device tree blob
1421 * @nodeoffset: offset of the node whose property to change
1422 * @name: name of the property to change
1423 * @val: 32-bit integer value for the property (native endian)
1425 * fdt_setprop_u32() sets the value of the named property in the given
1426 * node to the given 32-bit integer value (converting to big-endian if
1427 * necessary), or creates a new property with that value if it does
1428 * not already exist.
1430 * This function may insert or delete data from the blob, and will
1431 * therefore change the offsets of some existing nodes.
1435 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1436 * contain the new property value
1437 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1438 * -FDT_ERR_BADLAYOUT,
1439 * -FDT_ERR_BADMAGIC,
1440 * -FDT_ERR_BADVERSION,
1441 * -FDT_ERR_BADSTATE,
1442 * -FDT_ERR_BADSTRUCTURE,
1443 * -FDT_ERR_BADLAYOUT,
1444 * -FDT_ERR_TRUNCATED, standard meanings
1446 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1449 fdt32_t tmp = cpu_to_fdt32(val);
1450 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1454 * fdt_setprop_u64 - set a property to a 64-bit integer
1455 * @fdt: pointer to the device tree blob
1456 * @nodeoffset: offset of the node whose property to change
1457 * @name: name of the property to change
1458 * @val: 64-bit integer value for the property (native endian)
1460 * fdt_setprop_u64() sets the value of the named property in the given
1461 * node to the given 64-bit integer value (converting to big-endian if
1462 * necessary), or creates a new property with that value if it does
1463 * not already exist.
1465 * This function may insert or delete data from the blob, and will
1466 * therefore change the offsets of some existing nodes.
1470 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1471 * contain the new property value
1472 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1473 * -FDT_ERR_BADLAYOUT,
1474 * -FDT_ERR_BADMAGIC,
1475 * -FDT_ERR_BADVERSION,
1476 * -FDT_ERR_BADSTATE,
1477 * -FDT_ERR_BADSTRUCTURE,
1478 * -FDT_ERR_BADLAYOUT,
1479 * -FDT_ERR_TRUNCATED, standard meanings
1481 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1484 fdt64_t tmp = cpu_to_fdt64(val);
1485 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1489 * fdt_setprop_cell - set a property to a single cell value
1491 * This is an alternative name for fdt_setprop_u32()
1493 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1496 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1500 * fdt_setprop_string - set a property to a string value
1501 * @fdt: pointer to the device tree blob
1502 * @nodeoffset: offset of the node whose property to change
1503 * @name: name of the property to change
1504 * @str: string value for the property
1506 * fdt_setprop_string() sets the value of the named property in the
1507 * given node to the given string value (using the length of the
1508 * string to determine the new length of the property), or creates a
1509 * new property with that value if it does not already exist.
1511 * This function may insert or delete data from the blob, and will
1512 * therefore change the offsets of some existing nodes.
1516 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1517 * contain the new property value
1518 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1519 * -FDT_ERR_BADLAYOUT,
1520 * -FDT_ERR_BADMAGIC,
1521 * -FDT_ERR_BADVERSION,
1522 * -FDT_ERR_BADSTATE,
1523 * -FDT_ERR_BADSTRUCTURE,
1524 * -FDT_ERR_BADLAYOUT,
1525 * -FDT_ERR_TRUNCATED, standard meanings
1527 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1528 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1532 * fdt_setprop_empty - set a property to an empty value
1533 * @fdt: pointer to the device tree blob
1534 * @nodeoffset: offset of the node whose property to change
1535 * @name: name of the property to change
1537 * fdt_setprop_empty() sets the value of the named property in the
1538 * given node to an empty (zero length) value, or creates a new empty
1539 * property if it does not already exist.
1541 * This function may insert or delete data from the blob, and will
1542 * therefore change the offsets of some existing nodes.
1546 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1547 * contain the new property value
1548 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1549 * -FDT_ERR_BADLAYOUT,
1550 * -FDT_ERR_BADMAGIC,
1551 * -FDT_ERR_BADVERSION,
1552 * -FDT_ERR_BADSTATE,
1553 * -FDT_ERR_BADSTRUCTURE,
1554 * -FDT_ERR_BADLAYOUT,
1555 * -FDT_ERR_TRUNCATED, standard meanings
1557 #define fdt_setprop_empty(fdt, nodeoffset, name) \
1558 fdt_setprop((fdt), (nodeoffset), (name), NULL, 0)
1561 * fdt_appendprop - append to or create a property
1562 * @fdt: pointer to the device tree blob
1563 * @nodeoffset: offset of the node whose property to change
1564 * @name: name of the property to append to
1565 * @val: pointer to data to append to the property value
1566 * @len: length of the data to append to the property value
1568 * fdt_appendprop() appends the value to the named property in the
1569 * given node, creating the property if it does not already exist.
1571 * This function may insert data into the blob, and will therefore
1572 * change the offsets of some existing nodes.
1576 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1577 * contain the new property value
1578 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1579 * -FDT_ERR_BADLAYOUT,
1580 * -FDT_ERR_BADMAGIC,
1581 * -FDT_ERR_BADVERSION,
1582 * -FDT_ERR_BADSTATE,
1583 * -FDT_ERR_BADSTRUCTURE,
1584 * -FDT_ERR_BADLAYOUT,
1585 * -FDT_ERR_TRUNCATED, standard meanings
1587 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1588 const void *val, int len);
1591 * fdt_appendprop_u32 - append a 32-bit integer value to a property
1592 * @fdt: pointer to the device tree blob
1593 * @nodeoffset: offset of the node whose property to change
1594 * @name: name of the property to change
1595 * @val: 32-bit integer value to append to the property (native endian)
1597 * fdt_appendprop_u32() appends the given 32-bit integer value
1598 * (converting to big-endian if necessary) to the value of the named
1599 * property in the given node, or creates a new property with that
1600 * value if it does not already exist.
1602 * This function may insert data into the blob, and will therefore
1603 * change the offsets of some existing nodes.
1607 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1608 * contain the new property value
1609 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1610 * -FDT_ERR_BADLAYOUT,
1611 * -FDT_ERR_BADMAGIC,
1612 * -FDT_ERR_BADVERSION,
1613 * -FDT_ERR_BADSTATE,
1614 * -FDT_ERR_BADSTRUCTURE,
1615 * -FDT_ERR_BADLAYOUT,
1616 * -FDT_ERR_TRUNCATED, standard meanings
1618 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1619 const char *name, uint32_t val)
1621 fdt32_t tmp = cpu_to_fdt32(val);
1622 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1626 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1627 * @fdt: pointer to the device tree blob
1628 * @nodeoffset: offset of the node whose property to change
1629 * @name: name of the property to change
1630 * @val: 64-bit integer value to append to the property (native endian)
1632 * fdt_appendprop_u64() appends the given 64-bit integer value
1633 * (converting to big-endian if necessary) to the value of the named
1634 * property in the given node, or creates a new property with that
1635 * value if it does not already exist.
1637 * This function may insert data into the blob, and will therefore
1638 * change the offsets of some existing nodes.
1642 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1643 * contain the new property value
1644 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1645 * -FDT_ERR_BADLAYOUT,
1646 * -FDT_ERR_BADMAGIC,
1647 * -FDT_ERR_BADVERSION,
1648 * -FDT_ERR_BADSTATE,
1649 * -FDT_ERR_BADSTRUCTURE,
1650 * -FDT_ERR_BADLAYOUT,
1651 * -FDT_ERR_TRUNCATED, standard meanings
1653 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1654 const char *name, uint64_t val)
1656 fdt64_t tmp = cpu_to_fdt64(val);
1657 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1661 * fdt_appendprop_cell - append a single cell value to a property
1663 * This is an alternative name for fdt_appendprop_u32()
1665 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1666 const char *name, uint32_t val)
1668 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1672 * fdt_appendprop_string - append a string to a property
1673 * @fdt: pointer to the device tree blob
1674 * @nodeoffset: offset of the node whose property to change
1675 * @name: name of the property to change
1676 * @str: string value to append to the property
1678 * fdt_appendprop_string() appends the given string to the value of
1679 * the named property in the given node, or creates a new property
1680 * with that value if it does not already exist.
1682 * This function may insert data into the blob, and will therefore
1683 * change the offsets of some existing nodes.
1687 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1688 * contain the new property value
1689 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1690 * -FDT_ERR_BADLAYOUT,
1691 * -FDT_ERR_BADMAGIC,
1692 * -FDT_ERR_BADVERSION,
1693 * -FDT_ERR_BADSTATE,
1694 * -FDT_ERR_BADSTRUCTURE,
1695 * -FDT_ERR_BADLAYOUT,
1696 * -FDT_ERR_TRUNCATED, standard meanings
1698 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1699 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1702 * fdt_delprop - delete a property
1703 * @fdt: pointer to the device tree blob
1704 * @nodeoffset: offset of the node whose property to nop
1705 * @name: name of the property to nop
1707 * fdt_del_property() will delete the given property.
1709 * This function will delete data from the blob, and will therefore
1710 * change the offsets of some existing nodes.
1714 * -FDT_ERR_NOTFOUND, node does not have the named property
1715 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1716 * -FDT_ERR_BADLAYOUT,
1717 * -FDT_ERR_BADMAGIC,
1718 * -FDT_ERR_BADVERSION,
1719 * -FDT_ERR_BADSTATE,
1720 * -FDT_ERR_BADSTRUCTURE,
1721 * -FDT_ERR_TRUNCATED, standard meanings
1723 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1726 * fdt_add_subnode_namelen - creates a new node based on substring
1727 * @fdt: pointer to the device tree blob
1728 * @parentoffset: structure block offset of a node
1729 * @name: name of the subnode to locate
1730 * @namelen: number of characters of name to consider
1732 * Identical to fdt_add_subnode(), but use only the first namelen
1733 * characters of name as the name of the new node. This is useful for
1734 * creating subnodes based on a portion of a larger string, such as a
1737 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1738 const char *name, int namelen);
1741 * fdt_add_subnode - creates a new node
1742 * @fdt: pointer to the device tree blob
1743 * @parentoffset: structure block offset of a node
1744 * @name: name of the subnode to locate
1746 * fdt_add_subnode() creates a new node as a subnode of the node at
1747 * structure block offset parentoffset, with the given name (which
1748 * should include the unit address, if any).
1750 * This function will insert data into the blob, and will therefore
1751 * change the offsets of some existing nodes.
1754 * structure block offset of the created nodeequested subnode (>=0), on
1756 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1757 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
1759 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1761 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1762 * blob to contain the new node
1764 * -FDT_ERR_BADLAYOUT
1765 * -FDT_ERR_BADMAGIC,
1766 * -FDT_ERR_BADVERSION,
1767 * -FDT_ERR_BADSTATE,
1768 * -FDT_ERR_BADSTRUCTURE,
1769 * -FDT_ERR_TRUNCATED, standard meanings.
1771 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1774 * fdt_del_node - delete a node (subtree)
1775 * @fdt: pointer to the device tree blob
1776 * @nodeoffset: offset of the node to nop
1778 * fdt_del_node() will remove the given node, including all its
1779 * subnodes if any, from the blob.
1781 * This function will delete data from the blob, and will therefore
1782 * change the offsets of some existing nodes.
1786 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1787 * -FDT_ERR_BADLAYOUT,
1788 * -FDT_ERR_BADMAGIC,
1789 * -FDT_ERR_BADVERSION,
1790 * -FDT_ERR_BADSTATE,
1791 * -FDT_ERR_BADSTRUCTURE,
1792 * -FDT_ERR_TRUNCATED, standard meanings
1794 int fdt_del_node(void *fdt, int nodeoffset);
1797 * fdt_overlay_apply - Applies a DT overlay on a base DT
1798 * @fdt: pointer to the base device tree blob
1799 * @fdto: pointer to the device tree overlay blob
1801 * fdt_overlay_apply() will apply the given device tree overlay on the
1802 * given base device tree.
1804 * Expect the base device tree to be modified, even if the function
1809 * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
1810 * -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or
1811 * properties in the base DT
1812 * -FDT_ERR_BADPHANDLE,
1813 * -FDT_ERR_BADOVERLAY,
1814 * -FDT_ERR_NOPHANDLES,
1815 * -FDT_ERR_INTERNAL,
1816 * -FDT_ERR_BADLAYOUT,
1817 * -FDT_ERR_BADMAGIC,
1818 * -FDT_ERR_BADOFFSET,
1820 * -FDT_ERR_BADVERSION,
1821 * -FDT_ERR_BADSTRUCTURE,
1822 * -FDT_ERR_BADSTATE,
1823 * -FDT_ERR_TRUNCATED, standard meanings
1825 int fdt_overlay_apply(void *fdt, void *fdto);
1827 /**********************************************************************/
1828 /* Debugging / informational functions */
1829 /**********************************************************************/
1831 const char *fdt_strerror(int errval);
1833 #endif /* _LIBFDT_H */