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: Attemped 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 * value. phandle values of 0 and -1 are not permitted. */
84 #define FDT_ERR_BADSTATE 7
85 /* FDT_ERR_BADSTATE: Function was passed an incomplete device
86 * tree created by the sequential-write functions, which is
87 * not sufficiently complete for the requested operation. */
89 /* Error codes: codes for bad device tree blobs */
90 #define FDT_ERR_TRUNCATED 8
91 /* FDT_ERR_TRUNCATED: Structure block of the given device tree
92 * ends without an FDT_END tag. */
93 #define FDT_ERR_BADMAGIC 9
94 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
95 * device tree at all - it is missing the flattened device
96 * tree magic number. */
97 #define FDT_ERR_BADVERSION 10
98 /* FDT_ERR_BADVERSION: Given device tree has a version which
99 * can't be handled by the requested operation. For
100 * read-write functions, this may mean that fdt_open_into() is
101 * required to convert the tree to the expected version. */
102 #define FDT_ERR_BADSTRUCTURE 11
103 /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
104 * structure block or other serious error (e.g. misnested
105 * nodes, or subnodes preceding properties). */
106 #define FDT_ERR_BADLAYOUT 12
107 /* FDT_ERR_BADLAYOUT: For read-write functions, the given
108 * device tree has it's sub-blocks in an order that the
109 * function can't handle (memory reserve map, then structure,
110 * then strings). Use fdt_open_into() to reorganize the tree
111 * into a form suitable for the read-write operations. */
113 /* "Can't happen" error indicating a bug in libfdt */
114 #define FDT_ERR_INTERNAL 13
115 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
116 * Should never be returned, if it is, it indicates a bug in
119 /* Errors in device tree content */
120 #define FDT_ERR_BADNCELLS 14
121 /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
122 * or similar property with a bad format or value */
124 #define FDT_ERR_MAX 14
126 /**********************************************************************/
127 /* Low-level functions (you probably don't need these) */
128 /**********************************************************************/
130 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
131 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
133 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
136 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
138 /**********************************************************************/
139 /* Traversal functions */
140 /**********************************************************************/
142 int fdt_next_node(const void *fdt, int offset, int *depth);
145 * fdt_first_subnode() - get offset of first direct subnode
148 * @offset: Offset of node to check
149 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
151 int fdt_first_subnode(const void *fdt, int offset);
154 * fdt_next_subnode() - get offset of next direct subnode
156 * After first calling fdt_first_subnode(), call this function repeatedly to
157 * get direct subnodes of a parent node.
160 * @offset: Offset of previous subnode
161 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
164 int fdt_next_subnode(const void *fdt, int offset);
167 * fdt_for_each_subnode - iterate over all subnodes of a parent
169 * This is actually a wrapper around a for loop and would be used like so:
171 * fdt_for_each_subnode(fdt, node, parent) {
177 * Note that this is implemented as a macro and node is used as iterator in
178 * the loop. It should therefore be a locally allocated variable. The parent
179 * variable on the other hand is never modified, so it can be constant or
182 * @fdt: FDT blob (const void *)
183 * @node: child node (int)
184 * @parent: parent node (int)
186 #define fdt_for_each_subnode(fdt, node, parent) \
187 for (node = fdt_first_subnode(fdt, parent); \
189 node = fdt_next_subnode(fdt, node))
191 /**********************************************************************/
192 /* General functions */
193 /**********************************************************************/
195 #define fdt_get_header(fdt, field) \
196 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
197 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
198 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
199 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
200 #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
201 #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
202 #define fdt_version(fdt) (fdt_get_header(fdt, version))
203 #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
204 #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
205 #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
206 #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
208 #define __fdt_set_hdr(name) \
209 static inline void fdt_set_##name(void *fdt, uint32_t val) \
211 struct fdt_header *fdth = (struct fdt_header*)fdt; \
212 fdth->name = cpu_to_fdt32(val); \
214 __fdt_set_hdr(magic);
215 __fdt_set_hdr(totalsize);
216 __fdt_set_hdr(off_dt_struct);
217 __fdt_set_hdr(off_dt_strings);
218 __fdt_set_hdr(off_mem_rsvmap);
219 __fdt_set_hdr(version);
220 __fdt_set_hdr(last_comp_version);
221 __fdt_set_hdr(boot_cpuid_phys);
222 __fdt_set_hdr(size_dt_strings);
223 __fdt_set_hdr(size_dt_struct);
227 * fdt_check_header - sanity check a device tree or possible device tree
228 * @fdt: pointer to data which might be a flattened device tree
230 * fdt_check_header() checks that the given buffer contains what
231 * appears to be a flattened device tree with sane information in its
235 * 0, if the buffer appears to contain a valid device tree
237 * -FDT_ERR_BADVERSION,
238 * -FDT_ERR_BADSTATE, standard meanings, as above
240 int fdt_check_header(const void *fdt);
243 * fdt_move - move a device tree around in memory
244 * @fdt: pointer to the device tree to move
245 * @buf: pointer to memory where the device is to be moved
246 * @bufsize: size of the memory space at buf
248 * fdt_move() relocates, if possible, the device tree blob located at
249 * fdt to the buffer at buf of size bufsize. The buffer may overlap
250 * with the existing device tree blob at fdt. Therefore,
251 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
252 * should always succeed.
256 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
258 * -FDT_ERR_BADVERSION,
259 * -FDT_ERR_BADSTATE, standard meanings
261 int fdt_move(const void *fdt, void *buf, int bufsize);
263 /**********************************************************************/
264 /* Read-only functions */
265 /**********************************************************************/
268 * fdt_string - retrieve a string from the strings block of a device tree
269 * @fdt: pointer to the device tree blob
270 * @stroffset: offset of the string within the strings block (native endian)
272 * fdt_string() retrieves a pointer to a single string from the
273 * strings block of the device tree blob at fdt.
276 * a pointer to the string, on success
277 * NULL, if stroffset is out of bounds
279 const char *fdt_string(const void *fdt, int stroffset);
282 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
283 * @fdt: pointer to the device tree blob
285 * Returns the number of entries in the device tree blob's memory
286 * reservation map. This does not include the terminating 0,0 entry
287 * or any other (0,0) entries reserved for expansion.
290 * the number of entries
292 int fdt_num_mem_rsv(const void *fdt);
295 * fdt_get_mem_rsv - retrieve one memory reserve map entry
296 * @fdt: pointer to the device tree blob
297 * @address, @size: pointers to 64-bit variables
299 * On success, *address and *size will contain the address and size of
300 * the n-th reserve map entry from the device tree blob, in
301 * native-endian format.
306 * -FDT_ERR_BADVERSION,
307 * -FDT_ERR_BADSTATE, standard meanings
309 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
312 * fdt_subnode_offset_namelen - find a subnode based on substring
313 * @fdt: pointer to the device tree blob
314 * @parentoffset: structure block offset of a node
315 * @name: name of the subnode to locate
316 * @namelen: number of characters of name to consider
318 * Identical to fdt_subnode_offset(), but only examine the first
319 * namelen characters of name for matching the subnode name. This is
320 * useful for finding subnodes based on a portion of a larger string,
321 * such as a full path.
323 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
324 const char *name, int namelen);
326 * fdt_subnode_offset - find a subnode of a given node
327 * @fdt: pointer to the device tree blob
328 * @parentoffset: structure block offset of a node
329 * @name: name of the subnode to locate
331 * fdt_subnode_offset() finds a subnode of the node at structure block
332 * offset parentoffset with the given name. name may include a unit
333 * address, in which case fdt_subnode_offset() will find the subnode
334 * with that unit address, or the unit address may be omitted, in
335 * which case fdt_subnode_offset() will find an arbitrary subnode
336 * whose name excluding unit address matches the given name.
339 * structure block offset of the requested subnode (>=0), on success
340 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
341 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
343 * -FDT_ERR_BADVERSION,
345 * -FDT_ERR_BADSTRUCTURE,
346 * -FDT_ERR_TRUNCATED, standard meanings.
348 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
351 * fdt_path_offset - find a tree node by its full path
352 * @fdt: pointer to the device tree blob
353 * @path: full path of the node to locate
355 * fdt_path_offset() finds a node of a given path in the device tree.
356 * Each path component may omit the unit address portion, but the
357 * results of this are undefined if any such path component is
358 * ambiguous (that is if there are multiple nodes at the relevant
359 * level matching the given component, differentiated only by unit
363 * structure block offset of the node with the requested path (>=0), on success
364 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
365 * -FDT_ERR_NOTFOUND, if the requested node does not exist
367 * -FDT_ERR_BADVERSION,
369 * -FDT_ERR_BADSTRUCTURE,
370 * -FDT_ERR_TRUNCATED, standard meanings.
372 int fdt_path_offset(const void *fdt, const char *path);
375 * fdt_get_name - retrieve the name of a given node
376 * @fdt: pointer to the device tree blob
377 * @nodeoffset: structure block offset of the starting node
378 * @lenp: pointer to an integer variable (will be overwritten) or NULL
380 * fdt_get_name() retrieves the name (including unit address) of the
381 * device tree node at structure block offset nodeoffset. If lenp is
382 * non-NULL, the length of this name is also returned, in the integer
383 * pointed to by lenp.
386 * pointer to the node's name, on success
387 * If lenp is non-NULL, *lenp contains the length of that name (>=0)
389 * if lenp is non-NULL *lenp contains an error code (<0):
390 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
392 * -FDT_ERR_BADVERSION,
393 * -FDT_ERR_BADSTATE, standard meanings
395 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
398 * fdt_first_property_offset - find the offset of a node's first property
399 * @fdt: pointer to the device tree blob
400 * @nodeoffset: structure block offset of a node
402 * fdt_first_property_offset() finds the first property of the node at
403 * the given structure block offset.
406 * structure block offset of the property (>=0), on success
407 * -FDT_ERR_NOTFOUND, if the requested node has no properties
408 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
410 * -FDT_ERR_BADVERSION,
412 * -FDT_ERR_BADSTRUCTURE,
413 * -FDT_ERR_TRUNCATED, standard meanings.
415 int fdt_first_property_offset(const void *fdt, int nodeoffset);
418 * fdt_next_property_offset - step through a node's properties
419 * @fdt: pointer to the device tree blob
420 * @offset: structure block offset of a property
422 * fdt_next_property_offset() finds the property immediately after the
423 * one at the given structure block offset. This will be a property
424 * of the same node as the given property.
427 * structure block offset of the next property (>=0), on success
428 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
429 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
431 * -FDT_ERR_BADVERSION,
433 * -FDT_ERR_BADSTRUCTURE,
434 * -FDT_ERR_TRUNCATED, standard meanings.
436 int fdt_next_property_offset(const void *fdt, int offset);
439 * fdt_get_property_by_offset - retrieve the property at a given offset
440 * @fdt: pointer to the device tree blob
441 * @offset: offset of the property to retrieve
442 * @lenp: pointer to an integer variable (will be overwritten) or NULL
444 * fdt_get_property_by_offset() retrieves a pointer to the
445 * fdt_property structure within the device tree blob at the given
446 * offset. If lenp is non-NULL, the length of the property value is
447 * also returned, in the integer pointed to by lenp.
450 * pointer to the structure representing the property
451 * if lenp is non-NULL, *lenp contains the length of the property
454 * if lenp is non-NULL, *lenp contains an error code (<0):
455 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
457 * -FDT_ERR_BADVERSION,
459 * -FDT_ERR_BADSTRUCTURE,
460 * -FDT_ERR_TRUNCATED, standard meanings
462 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
467 * fdt_get_property_namelen - find a property based on substring
468 * @fdt: pointer to the device tree blob
469 * @nodeoffset: offset of the node whose property to find
470 * @name: name of the property to find
471 * @namelen: number of characters of name to consider
472 * @lenp: pointer to an integer variable (will be overwritten) or NULL
474 * Identical to fdt_get_property_namelen(), but only examine the first
475 * namelen characters of name for matching the property name.
477 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
480 int namelen, int *lenp);
483 * fdt_get_property - find a given property in a given node
484 * @fdt: pointer to the device tree blob
485 * @nodeoffset: offset of the node whose property to find
486 * @name: name of the property to find
487 * @lenp: pointer to an integer variable (will be overwritten) or NULL
489 * fdt_get_property() retrieves a pointer to the fdt_property
490 * structure within the device tree blob corresponding to the property
491 * named 'name' of the node at offset nodeoffset. If lenp is
492 * non-NULL, the length of the property value is also returned, in the
493 * integer pointed to by lenp.
496 * pointer to the structure representing the property
497 * if lenp is non-NULL, *lenp contains the length of the property
500 * if lenp is non-NULL, *lenp contains an error code (<0):
501 * -FDT_ERR_NOTFOUND, node does not have named property
502 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
504 * -FDT_ERR_BADVERSION,
506 * -FDT_ERR_BADSTRUCTURE,
507 * -FDT_ERR_TRUNCATED, standard meanings
509 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
510 const char *name, int *lenp);
511 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
515 return (struct fdt_property *)(uintptr_t)
516 fdt_get_property(fdt, nodeoffset, name, lenp);
520 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
521 * @fdt: pointer to the device tree blob
522 * @ffset: offset of the property to read
523 * @namep: pointer to a string variable (will be overwritten) or NULL
524 * @lenp: pointer to an integer variable (will be overwritten) or NULL
526 * fdt_getprop_by_offset() retrieves a pointer to the value of the
527 * property at structure block offset 'offset' (this will be a pointer
528 * to within the device blob itself, not a copy of the value). If
529 * lenp is non-NULL, the length of the property value is also
530 * returned, in the integer pointed to by lenp. If namep is non-NULL,
531 * the property's namne will also be returned in the char * pointed to
532 * by namep (this will be a pointer to within the device tree's string
533 * block, not a new copy of the name).
536 * pointer to the property's value
537 * if lenp is non-NULL, *lenp contains the length of the property
539 * if namep is non-NULL *namep contiains a pointer to the property
542 * if lenp is non-NULL, *lenp contains an error code (<0):
543 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
545 * -FDT_ERR_BADVERSION,
547 * -FDT_ERR_BADSTRUCTURE,
548 * -FDT_ERR_TRUNCATED, standard meanings
550 const void *fdt_getprop_by_offset(const void *fdt, int offset,
551 const char **namep, int *lenp);
554 * fdt_getprop_namelen - get property value based on substring
555 * @fdt: pointer to the device tree blob
556 * @nodeoffset: offset of the node whose property to find
557 * @name: name of the property to find
558 * @namelen: number of characters of name to consider
559 * @lenp: pointer to an integer variable (will be overwritten) or NULL
561 * Identical to fdt_getprop(), but only examine the first namelen
562 * characters of name for matching the property name.
564 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
565 const char *name, int namelen, int *lenp);
568 * fdt_getprop - retrieve the value of a given property
569 * @fdt: pointer to the device tree blob
570 * @nodeoffset: offset of the node whose property to find
571 * @name: name of the property to find
572 * @lenp: pointer to an integer variable (will be overwritten) or NULL
574 * fdt_getprop() retrieves a pointer to the value of the property
575 * named 'name' of the node at offset nodeoffset (this will be a
576 * pointer to within the device blob itself, not a copy of the value).
577 * If lenp is non-NULL, the length of the property value is also
578 * returned, in the integer pointed to by lenp.
581 * pointer to the property's value
582 * if lenp is non-NULL, *lenp contains the length of the property
585 * if lenp is non-NULL, *lenp contains an error code (<0):
586 * -FDT_ERR_NOTFOUND, node does not have named property
587 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
589 * -FDT_ERR_BADVERSION,
591 * -FDT_ERR_BADSTRUCTURE,
592 * -FDT_ERR_TRUNCATED, standard meanings
594 const void *fdt_getprop(const void *fdt, int nodeoffset,
595 const char *name, int *lenp);
596 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
597 const char *name, int *lenp)
599 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
603 * fdt_get_phandle - retrieve the phandle of a given node
604 * @fdt: pointer to the device tree blob
605 * @nodeoffset: structure block offset of the node
607 * fdt_get_phandle() retrieves the phandle of the device tree node at
608 * structure block offset nodeoffset.
611 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
612 * 0, if the node has no phandle, or another error occurs
614 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
617 * fdt_get_alias_namelen - get alias based on substring
618 * @fdt: pointer to the device tree blob
619 * @name: name of the alias th look up
620 * @namelen: number of characters of name to consider
622 * Identical to fdt_get_alias(), but only examine the first namelen
623 * characters of name for matching the alias name.
625 const char *fdt_get_alias_namelen(const void *fdt,
626 const char *name, int namelen);
629 * fdt_get_alias - retreive the path referenced by a given alias
630 * @fdt: pointer to the device tree blob
631 * @name: name of the alias th look up
633 * fdt_get_alias() retrieves the value of a given alias. That is, the
634 * value of the property named 'name' in the node /aliases.
637 * a pointer to the expansion of the alias named 'name', if it exists
638 * NULL, if the given alias or the /aliases node does not exist
640 const char *fdt_get_alias(const void *fdt, const char *name);
643 * fdt_get_path - determine the full path of a node
644 * @fdt: pointer to the device tree blob
645 * @nodeoffset: offset of the node whose path to find
646 * @buf: character buffer to contain the returned path (will be overwritten)
647 * @buflen: size of the character buffer at buf
649 * fdt_get_path() computes the full path of the node at offset
650 * nodeoffset, and records that path in the buffer at buf.
652 * NOTE: This function is expensive, as it must scan the device tree
653 * structure from the start to nodeoffset.
657 * buf contains the absolute path of the node at
658 * nodeoffset, as a NUL-terminated string.
659 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
660 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
661 * characters and will not fit in the given buffer.
663 * -FDT_ERR_BADVERSION,
665 * -FDT_ERR_BADSTRUCTURE, standard meanings
667 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
670 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
671 * @fdt: pointer to the device tree blob
672 * @nodeoffset: offset of the node whose parent to find
673 * @supernodedepth: depth of the ancestor to find
674 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
676 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
677 * at a specific depth from the root (where the root itself has depth
678 * 0, its immediate subnodes depth 1 and so forth). So
679 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
680 * will always return 0, the offset of the root node. If the node at
681 * nodeoffset has depth D, then:
682 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
683 * will return nodeoffset itself.
685 * NOTE: This function is expensive, as it must scan the device tree
686 * structure from the start to nodeoffset.
690 * structure block offset of the node at node offset's ancestor
691 * of depth supernodedepth (>=0), on success
692 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
693 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
695 * -FDT_ERR_BADVERSION,
697 * -FDT_ERR_BADSTRUCTURE, standard meanings
699 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
700 int supernodedepth, int *nodedepth);
703 * fdt_node_depth - find the depth of a given node
704 * @fdt: pointer to the device tree blob
705 * @nodeoffset: offset of the node whose parent to find
707 * fdt_node_depth() finds the depth of a given node. The root node
708 * has depth 0, its immediate subnodes depth 1 and so forth.
710 * NOTE: This function is expensive, as it must scan the device tree
711 * structure from the start to nodeoffset.
714 * depth of the node at nodeoffset (>=0), on success
715 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
717 * -FDT_ERR_BADVERSION,
719 * -FDT_ERR_BADSTRUCTURE, standard meanings
721 int fdt_node_depth(const void *fdt, int nodeoffset);
724 * fdt_parent_offset - find the parent of a given node
725 * @fdt: pointer to the device tree blob
726 * @nodeoffset: offset of the node whose parent to find
728 * fdt_parent_offset() locates the parent node of a given node (that
729 * is, it finds the offset of the node which contains the node at
730 * nodeoffset as a subnode).
732 * NOTE: This function is expensive, as it must scan the device tree
733 * structure from the start to nodeoffset, *twice*.
736 * structure block offset of the parent of the node at nodeoffset
738 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
740 * -FDT_ERR_BADVERSION,
742 * -FDT_ERR_BADSTRUCTURE, standard meanings
744 int fdt_parent_offset(const void *fdt, int nodeoffset);
747 * fdt_node_offset_by_prop_value - find nodes with a given property value
748 * @fdt: pointer to the device tree blob
749 * @startoffset: only find nodes after this offset
750 * @propname: property name to check
751 * @propval: property value to search for
752 * @proplen: length of the value in propval
754 * fdt_node_offset_by_prop_value() returns the offset of the first
755 * node after startoffset, which has a property named propname whose
756 * value is of length proplen and has value equal to propval; or if
757 * startoffset is -1, the very first such node in the tree.
759 * To iterate through all nodes matching the criterion, the following
761 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
763 * while (offset != -FDT_ERR_NOTFOUND) {
765 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
769 * Note the -1 in the first call to the function, if 0 is used here
770 * instead, the function will never locate the root node, even if it
771 * matches the criterion.
774 * structure block offset of the located node (>= 0, >startoffset),
776 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
777 * tree after startoffset
778 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
780 * -FDT_ERR_BADVERSION,
782 * -FDT_ERR_BADSTRUCTURE, standard meanings
784 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
785 const char *propname,
786 const void *propval, int proplen);
789 * fdt_node_offset_by_phandle - find the node with a given phandle
790 * @fdt: pointer to the device tree blob
791 * @phandle: phandle value
793 * fdt_node_offset_by_phandle() returns the offset of the node
794 * which has the given phandle value. If there is more than one node
795 * in the tree with the given phandle (an invalid tree), results are
799 * structure block offset of the located node (>= 0), on success
800 * -FDT_ERR_NOTFOUND, no node with that phandle exists
801 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
803 * -FDT_ERR_BADVERSION,
805 * -FDT_ERR_BADSTRUCTURE, standard meanings
807 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
810 * fdt_node_check_compatible: check a node's compatible property
811 * @fdt: pointer to the device tree blob
812 * @nodeoffset: offset of a tree node
813 * @compatible: string to match against
816 * fdt_node_check_compatible() returns 0 if the given node contains a
817 * 'compatible' property with the given string as one of its elements,
818 * it returns non-zero otherwise, or on error.
821 * 0, if the node has a 'compatible' property listing the given string
822 * 1, if the node has a 'compatible' property, but it does not list
824 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
825 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
827 * -FDT_ERR_BADVERSION,
829 * -FDT_ERR_BADSTRUCTURE, standard meanings
831 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
832 const char *compatible);
835 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
836 * @fdt: pointer to the device tree blob
837 * @startoffset: only find nodes after this offset
838 * @compatible: 'compatible' string to match against
840 * fdt_node_offset_by_compatible() returns the offset of the first
841 * node after startoffset, which has a 'compatible' property which
842 * lists the given compatible string; or if startoffset is -1, the
843 * very first such node in the tree.
845 * To iterate through all nodes matching the criterion, the following
847 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
848 * while (offset != -FDT_ERR_NOTFOUND) {
850 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
853 * Note the -1 in the first call to the function, if 0 is used here
854 * instead, the function will never locate the root node, even if it
855 * matches the criterion.
858 * structure block offset of the located node (>= 0, >startoffset),
860 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
861 * tree after startoffset
862 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
864 * -FDT_ERR_BADVERSION,
866 * -FDT_ERR_BADSTRUCTURE, standard meanings
868 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
869 const char *compatible);
872 * fdt_stringlist_contains - check a string list property for a string
873 * @strlist: Property containing a list of strings to check
874 * @listlen: Length of property
875 * @str: String to search for
877 * This is a utility function provided for convenience. The list contains
878 * one or more strings, each terminated by \0, as is found in a device tree
879 * "compatible" property.
881 * @return: 1 if the string is found in the list, 0 not found, or invalid list
883 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
886 * fdt_count_strings - count the number of strings in a string list
887 * @fdt: pointer to the device tree blob
888 * @node: offset of the node
889 * @property: name of the property containing the string list
890 * @return: the number of strings in the given property
892 int fdt_count_strings(const void *fdt, int node, const char *property);
895 * fdt_find_string - find a string in a string list and return its index
896 * @fdt: pointer to the device tree blob
897 * @node: offset of the node
898 * @property: name of the property containing the string list
899 * @string: string to look up in the string list
900 * @return: the index of the string or negative on error
902 int fdt_find_string(const void *fdt, int node, const char *property,
906 * fdt_get_string_index() - obtain the string at a given index in a string list
907 * @fdt: pointer to the device tree blob
908 * @node: offset of the node
909 * @property: name of the property containing the string list
910 * @index: index of the string to return
911 * @output: return location for the string
912 * @return: 0 if the string was found or a negative error code otherwise
914 int fdt_get_string_index(const void *fdt, int node, const char *property,
915 int index, const char **output);
918 * fdt_get_string() - obtain the first string in a string list
919 * @fdt: pointer to the device tree blob
920 * @node: offset of the node
921 * @property: name of the property containing the string list
922 * @output: return location for the string
923 * @return: 0 if the string was found or a negative error code otherwise
925 * This is a shortcut for:
927 * fdt_get_string_index(fdt, node, property, 0, output).
929 int fdt_get_string(const void *fdt, int node, const char *property,
930 const char **output);
932 /**********************************************************************/
933 /* Read-only functions (addressing related) */
934 /**********************************************************************/
937 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
939 * This is the maximum value for #address-cells, #size-cells and
940 * similar properties that will be processed by libfdt. IEE1275
941 * requires that OF implementations handle values up to 4.
942 * Implementations may support larger values, but in practice higher
943 * values aren't used.
945 #define FDT_MAX_NCELLS 4
948 * fdt_address_cells - retrieve address size for a bus represented in the tree
949 * @fdt: pointer to the device tree blob
950 * @nodeoffset: offset of the node to find the address size for
952 * When the node has a valid #address-cells property, returns its value.
955 * 0 <= n < FDT_MAX_NCELLS, on success
956 * 2, if the node has no #address-cells property
957 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
958 * #address-cells property
960 * -FDT_ERR_BADVERSION,
962 * -FDT_ERR_BADSTRUCTURE,
963 * -FDT_ERR_TRUNCATED, standard meanings
965 int fdt_address_cells(const void *fdt, int nodeoffset);
968 * fdt_size_cells - retrieve address range size for a bus represented in the
970 * @fdt: pointer to the device tree blob
971 * @nodeoffset: offset of the node to find the address range size for
973 * When the node has a valid #size-cells property, returns its value.
976 * 0 <= n < FDT_MAX_NCELLS, on success
977 * 2, if the node has no #address-cells property
978 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
979 * #size-cells property
981 * -FDT_ERR_BADVERSION,
983 * -FDT_ERR_BADSTRUCTURE,
984 * -FDT_ERR_TRUNCATED, standard meanings
986 int fdt_size_cells(const void *fdt, int nodeoffset);
989 /**********************************************************************/
990 /* Write-in-place functions */
991 /**********************************************************************/
994 * fdt_setprop_inplace - change a property's value, but not its size
995 * @fdt: pointer to the device tree blob
996 * @nodeoffset: offset of the node whose property to change
997 * @name: name of the property to change
998 * @val: pointer to data to replace the property value with
999 * @len: length of the property value
1001 * fdt_setprop_inplace() replaces the value of a given property with
1002 * the data in val, of length len. This function cannot change the
1003 * size of a property, and so will only work if len is equal to the
1004 * current length of the property.
1006 * This function will alter only the bytes in the blob which contain
1007 * the given property value, and will not alter or move any other part
1012 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1013 * -FDT_ERR_NOTFOUND, node does not have the named property
1014 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1015 * -FDT_ERR_BADMAGIC,
1016 * -FDT_ERR_BADVERSION,
1017 * -FDT_ERR_BADSTATE,
1018 * -FDT_ERR_BADSTRUCTURE,
1019 * -FDT_ERR_TRUNCATED, standard meanings
1021 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1022 const void *val, int len);
1025 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
1026 * @fdt: pointer to the device tree blob
1027 * @nodeoffset: offset of the node whose property to change
1028 * @name: name of the property to change
1029 * @val: 32-bit integer value to replace the property with
1031 * fdt_setprop_inplace_u32() replaces the value of a given property
1032 * with the 32-bit integer value in val, converting val to big-endian
1033 * if necessary. This function cannot change the size of a property,
1034 * and so will only work if the property already exists and has length
1037 * This function will alter only the bytes in the blob which contain
1038 * the given property value, and will not alter or move any other part
1043 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
1044 * -FDT_ERR_NOTFOUND, node does not have the named property
1045 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1046 * -FDT_ERR_BADMAGIC,
1047 * -FDT_ERR_BADVERSION,
1048 * -FDT_ERR_BADSTATE,
1049 * -FDT_ERR_BADSTRUCTURE,
1050 * -FDT_ERR_TRUNCATED, standard meanings
1052 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1053 const char *name, uint32_t val)
1055 fdt32_t tmp = cpu_to_fdt32(val);
1056 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1060 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1061 * @fdt: pointer to the device tree blob
1062 * @nodeoffset: offset of the node whose property to change
1063 * @name: name of the property to change
1064 * @val: 64-bit integer value to replace the property with
1066 * fdt_setprop_inplace_u64() replaces the value of a given property
1067 * with the 64-bit integer value in val, converting val to big-endian
1068 * if necessary. This function cannot change the size of a property,
1069 * and so will only work if the property already exists and has length
1072 * This function will alter only the bytes in the blob which contain
1073 * the given property value, and will not alter or move any other part
1078 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1079 * -FDT_ERR_NOTFOUND, node does not have the named property
1080 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1081 * -FDT_ERR_BADMAGIC,
1082 * -FDT_ERR_BADVERSION,
1083 * -FDT_ERR_BADSTATE,
1084 * -FDT_ERR_BADSTRUCTURE,
1085 * -FDT_ERR_TRUNCATED, standard meanings
1087 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1088 const char *name, uint64_t val)
1090 fdt64_t tmp = cpu_to_fdt64(val);
1091 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1095 * fdt_setprop_inplace_cell - change the value of a single-cell property
1097 * This is an alternative name for fdt_setprop_inplace_u32()
1099 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1100 const char *name, uint32_t val)
1102 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1106 * fdt_nop_property - replace a property with nop tags
1107 * @fdt: pointer to the device tree blob
1108 * @nodeoffset: offset of the node whose property to nop
1109 * @name: name of the property to nop
1111 * fdt_nop_property() will replace a given property's representation
1112 * in the blob with FDT_NOP tags, effectively removing it from the
1115 * This function will alter only the bytes in the blob which contain
1116 * the property, and will not alter or move any other part of the
1121 * -FDT_ERR_NOTFOUND, node does not have the named property
1122 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1123 * -FDT_ERR_BADMAGIC,
1124 * -FDT_ERR_BADVERSION,
1125 * -FDT_ERR_BADSTATE,
1126 * -FDT_ERR_BADSTRUCTURE,
1127 * -FDT_ERR_TRUNCATED, standard meanings
1129 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1132 * fdt_nop_node - replace a node (subtree) with nop tags
1133 * @fdt: pointer to the device tree blob
1134 * @nodeoffset: offset of the node to nop
1136 * fdt_nop_node() will replace a given node's representation in the
1137 * blob, including all its subnodes, if any, with FDT_NOP tags,
1138 * effectively removing it from the tree.
1140 * This function will alter only the bytes in the blob which contain
1141 * the node and its properties and subnodes, and will not alter or
1142 * move any other part of the tree.
1146 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1147 * -FDT_ERR_BADMAGIC,
1148 * -FDT_ERR_BADVERSION,
1149 * -FDT_ERR_BADSTATE,
1150 * -FDT_ERR_BADSTRUCTURE,
1151 * -FDT_ERR_TRUNCATED, standard meanings
1153 int fdt_nop_node(void *fdt, int nodeoffset);
1155 /**********************************************************************/
1156 /* Sequential write functions */
1157 /**********************************************************************/
1159 int fdt_create(void *buf, int bufsize);
1160 int fdt_resize(void *fdt, void *buf, int bufsize);
1161 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1162 int fdt_finish_reservemap(void *fdt);
1163 int fdt_begin_node(void *fdt, const char *name);
1164 int fdt_property(void *fdt, const char *name, const void *val, int len);
1165 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1167 fdt32_t tmp = cpu_to_fdt32(val);
1168 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1170 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1172 fdt64_t tmp = cpu_to_fdt64(val);
1173 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1175 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1177 return fdt_property_u32(fdt, name, val);
1179 #define fdt_property_string(fdt, name, str) \
1180 fdt_property(fdt, name, str, strlen(str)+1)
1181 int fdt_end_node(void *fdt);
1182 int fdt_finish(void *fdt);
1184 /**********************************************************************/
1185 /* Read-write functions */
1186 /**********************************************************************/
1188 int fdt_create_empty_tree(void *buf, int bufsize);
1189 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1190 int fdt_pack(void *fdt);
1193 * fdt_add_mem_rsv - add one memory reserve map entry
1194 * @fdt: pointer to the device tree blob
1195 * @address, @size: 64-bit values (native endian)
1197 * Adds a reserve map entry to the given blob reserving a region at
1198 * address address of length size.
1200 * This function will insert data into the reserve map and will
1201 * therefore change the indexes of some entries in the table.
1205 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1206 * contain the new reservation entry
1207 * -FDT_ERR_BADMAGIC,
1208 * -FDT_ERR_BADVERSION,
1209 * -FDT_ERR_BADSTATE,
1210 * -FDT_ERR_BADSTRUCTURE,
1211 * -FDT_ERR_BADLAYOUT,
1212 * -FDT_ERR_TRUNCATED, standard meanings
1214 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1217 * fdt_del_mem_rsv - remove a memory reserve map entry
1218 * @fdt: pointer to the device tree blob
1219 * @n: entry to remove
1221 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1224 * This function will delete data from the reservation table and will
1225 * therefore change the indexes of some entries in the table.
1229 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1230 * are less than n+1 reserve map entries)
1231 * -FDT_ERR_BADMAGIC,
1232 * -FDT_ERR_BADVERSION,
1233 * -FDT_ERR_BADSTATE,
1234 * -FDT_ERR_BADSTRUCTURE,
1235 * -FDT_ERR_BADLAYOUT,
1236 * -FDT_ERR_TRUNCATED, standard meanings
1238 int fdt_del_mem_rsv(void *fdt, int n);
1241 * fdt_set_name - change the name of a given node
1242 * @fdt: pointer to the device tree blob
1243 * @nodeoffset: structure block offset of a node
1244 * @name: name to give the node
1246 * fdt_set_name() replaces the name (including unit address, if any)
1247 * of the given node with the given string. NOTE: this function can't
1248 * efficiently check if the new name is unique amongst the given
1249 * node's siblings; results are undefined if this function is invoked
1250 * with a name equal to one of the given node's siblings.
1252 * This function may insert or delete data from the blob, and will
1253 * therefore change the offsets of some existing nodes.
1257 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1258 * to contain the new name
1259 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1260 * -FDT_ERR_BADMAGIC,
1261 * -FDT_ERR_BADVERSION,
1262 * -FDT_ERR_BADSTATE, standard meanings
1264 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1267 * fdt_setprop - create or change a property
1268 * @fdt: pointer to the device tree blob
1269 * @nodeoffset: offset of the node whose property to change
1270 * @name: name of the property to change
1271 * @val: pointer to data to set the property value to
1272 * @len: length of the property value
1274 * fdt_setprop() sets the value of the named property in the given
1275 * node to the given value and length, creating the property if it
1276 * does not already exist.
1278 * This function may insert or delete data from the blob, and will
1279 * therefore change the offsets of some existing nodes.
1283 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1284 * contain the new property value
1285 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1286 * -FDT_ERR_BADLAYOUT,
1287 * -FDT_ERR_BADMAGIC,
1288 * -FDT_ERR_BADVERSION,
1289 * -FDT_ERR_BADSTATE,
1290 * -FDT_ERR_BADSTRUCTURE,
1291 * -FDT_ERR_BADLAYOUT,
1292 * -FDT_ERR_TRUNCATED, standard meanings
1294 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1295 const void *val, int len);
1298 * fdt_setprop_u32 - set a property to a 32-bit integer
1299 * @fdt: pointer to the device tree blob
1300 * @nodeoffset: offset of the node whose property to change
1301 * @name: name of the property to change
1302 * @val: 32-bit integer value for the property (native endian)
1304 * fdt_setprop_u32() sets the value of the named property in the given
1305 * node to the given 32-bit integer value (converting to big-endian if
1306 * necessary), or creates a new property with that value if it does
1307 * not already exist.
1309 * This function may insert or delete data from the blob, and will
1310 * therefore change the offsets of some existing nodes.
1314 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1315 * contain the new property value
1316 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1317 * -FDT_ERR_BADLAYOUT,
1318 * -FDT_ERR_BADMAGIC,
1319 * -FDT_ERR_BADVERSION,
1320 * -FDT_ERR_BADSTATE,
1321 * -FDT_ERR_BADSTRUCTURE,
1322 * -FDT_ERR_BADLAYOUT,
1323 * -FDT_ERR_TRUNCATED, standard meanings
1325 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1328 fdt32_t tmp = cpu_to_fdt32(val);
1329 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1333 * fdt_setprop_u64 - set a property to a 64-bit integer
1334 * @fdt: pointer to the device tree blob
1335 * @nodeoffset: offset of the node whose property to change
1336 * @name: name of the property to change
1337 * @val: 64-bit integer value for the property (native endian)
1339 * fdt_setprop_u64() sets the value of the named property in the given
1340 * node to the given 64-bit integer value (converting to big-endian if
1341 * necessary), or creates a new property with that value if it does
1342 * not already exist.
1344 * This function may insert or delete data from the blob, and will
1345 * therefore change the offsets of some existing nodes.
1349 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1350 * contain the new property value
1351 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1352 * -FDT_ERR_BADLAYOUT,
1353 * -FDT_ERR_BADMAGIC,
1354 * -FDT_ERR_BADVERSION,
1355 * -FDT_ERR_BADSTATE,
1356 * -FDT_ERR_BADSTRUCTURE,
1357 * -FDT_ERR_BADLAYOUT,
1358 * -FDT_ERR_TRUNCATED, standard meanings
1360 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1363 fdt64_t tmp = cpu_to_fdt64(val);
1364 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1368 * fdt_setprop_cell - set a property to a single cell value
1370 * This is an alternative name for fdt_setprop_u32()
1372 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1375 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1379 * fdt_setprop_string - set a property to a string value
1380 * @fdt: pointer to the device tree blob
1381 * @nodeoffset: offset of the node whose property to change
1382 * @name: name of the property to change
1383 * @str: string value for the property
1385 * fdt_setprop_string() sets the value of the named property in the
1386 * given node to the given string value (using the length of the
1387 * string to determine the new length of the property), or creates a
1388 * new property with that value if it does not already exist.
1390 * This function may insert or delete data from the blob, and will
1391 * therefore change the offsets of some existing nodes.
1395 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1396 * contain the new property value
1397 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1398 * -FDT_ERR_BADLAYOUT,
1399 * -FDT_ERR_BADMAGIC,
1400 * -FDT_ERR_BADVERSION,
1401 * -FDT_ERR_BADSTATE,
1402 * -FDT_ERR_BADSTRUCTURE,
1403 * -FDT_ERR_BADLAYOUT,
1404 * -FDT_ERR_TRUNCATED, standard meanings
1406 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1407 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1410 * fdt_appendprop - append to or create a property
1411 * @fdt: pointer to the device tree blob
1412 * @nodeoffset: offset of the node whose property to change
1413 * @name: name of the property to append to
1414 * @val: pointer to data to append to the property value
1415 * @len: length of the data to append to the property value
1417 * fdt_appendprop() appends the value to the named property in the
1418 * given node, creating the property if it does not already exist.
1420 * This function may insert data into the blob, and will therefore
1421 * change the offsets of some existing nodes.
1425 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1426 * contain the new property value
1427 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1428 * -FDT_ERR_BADLAYOUT,
1429 * -FDT_ERR_BADMAGIC,
1430 * -FDT_ERR_BADVERSION,
1431 * -FDT_ERR_BADSTATE,
1432 * -FDT_ERR_BADSTRUCTURE,
1433 * -FDT_ERR_BADLAYOUT,
1434 * -FDT_ERR_TRUNCATED, standard meanings
1436 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1437 const void *val, int len);
1440 * fdt_appendprop_u32 - append a 32-bit integer value to a property
1441 * @fdt: pointer to the device tree blob
1442 * @nodeoffset: offset of the node whose property to change
1443 * @name: name of the property to change
1444 * @val: 32-bit integer value to append to the property (native endian)
1446 * fdt_appendprop_u32() appends the given 32-bit integer value
1447 * (converting to big-endian if necessary) to the value of the named
1448 * property in the given node, or creates a new property with that
1449 * value if it does not already exist.
1451 * This function may insert data into the blob, and will therefore
1452 * change the offsets of some existing nodes.
1456 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1457 * contain the new property value
1458 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1459 * -FDT_ERR_BADLAYOUT,
1460 * -FDT_ERR_BADMAGIC,
1461 * -FDT_ERR_BADVERSION,
1462 * -FDT_ERR_BADSTATE,
1463 * -FDT_ERR_BADSTRUCTURE,
1464 * -FDT_ERR_BADLAYOUT,
1465 * -FDT_ERR_TRUNCATED, standard meanings
1467 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1468 const char *name, uint32_t val)
1470 fdt32_t tmp = cpu_to_fdt32(val);
1471 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1475 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1476 * @fdt: pointer to the device tree blob
1477 * @nodeoffset: offset of the node whose property to change
1478 * @name: name of the property to change
1479 * @val: 64-bit integer value to append to the property (native endian)
1481 * fdt_appendprop_u64() appends the given 64-bit integer value
1482 * (converting to big-endian if necessary) to the value of the named
1483 * property in the given node, or creates a new property with that
1484 * value if it does not already exist.
1486 * This function may insert data into the blob, and will therefore
1487 * change the offsets of some existing nodes.
1491 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1492 * contain the new property value
1493 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1494 * -FDT_ERR_BADLAYOUT,
1495 * -FDT_ERR_BADMAGIC,
1496 * -FDT_ERR_BADVERSION,
1497 * -FDT_ERR_BADSTATE,
1498 * -FDT_ERR_BADSTRUCTURE,
1499 * -FDT_ERR_BADLAYOUT,
1500 * -FDT_ERR_TRUNCATED, standard meanings
1502 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1503 const char *name, uint64_t val)
1505 fdt64_t tmp = cpu_to_fdt64(val);
1506 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1510 * fdt_appendprop_cell - append a single cell value to a property
1512 * This is an alternative name for fdt_appendprop_u32()
1514 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1515 const char *name, uint32_t val)
1517 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1521 * fdt_appendprop_string - append a string to a property
1522 * @fdt: pointer to the device tree blob
1523 * @nodeoffset: offset of the node whose property to change
1524 * @name: name of the property to change
1525 * @str: string value to append to the property
1527 * fdt_appendprop_string() appends the given string to the value of
1528 * the named property in the given node, or creates a new property
1529 * with that value if it does not already exist.
1531 * This function may insert data into the blob, and will therefore
1532 * change the offsets of some existing nodes.
1536 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1537 * contain the new property value
1538 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1539 * -FDT_ERR_BADLAYOUT,
1540 * -FDT_ERR_BADMAGIC,
1541 * -FDT_ERR_BADVERSION,
1542 * -FDT_ERR_BADSTATE,
1543 * -FDT_ERR_BADSTRUCTURE,
1544 * -FDT_ERR_BADLAYOUT,
1545 * -FDT_ERR_TRUNCATED, standard meanings
1547 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1548 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1551 * fdt_delprop - delete a property
1552 * @fdt: pointer to the device tree blob
1553 * @nodeoffset: offset of the node whose property to nop
1554 * @name: name of the property to nop
1556 * fdt_del_property() will delete the given property.
1558 * This function will delete data from the blob, and will therefore
1559 * change the offsets of some existing nodes.
1563 * -FDT_ERR_NOTFOUND, node does not have the named property
1564 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1565 * -FDT_ERR_BADLAYOUT,
1566 * -FDT_ERR_BADMAGIC,
1567 * -FDT_ERR_BADVERSION,
1568 * -FDT_ERR_BADSTATE,
1569 * -FDT_ERR_BADSTRUCTURE,
1570 * -FDT_ERR_TRUNCATED, standard meanings
1572 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1575 * fdt_add_subnode_namelen - creates a new node based on substring
1576 * @fdt: pointer to the device tree blob
1577 * @parentoffset: structure block offset of a node
1578 * @name: name of the subnode to locate
1579 * @namelen: number of characters of name to consider
1581 * Identical to fdt_add_subnode(), but use only the first namelen
1582 * characters of name as the name of the new node. This is useful for
1583 * creating subnodes based on a portion of a larger string, such as a
1586 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1587 const char *name, int namelen);
1590 * fdt_add_subnode - creates a new node
1591 * @fdt: pointer to the device tree blob
1592 * @parentoffset: structure block offset of a node
1593 * @name: name of the subnode to locate
1595 * fdt_add_subnode() creates a new node as a subnode of the node at
1596 * structure block offset parentoffset, with the given name (which
1597 * should include the unit address, if any).
1599 * This function will insert data into the blob, and will therefore
1600 * change the offsets of some existing nodes.
1603 * structure block offset of the created nodeequested subnode (>=0), on success
1604 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1605 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1606 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1608 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1609 * blob to contain the new node
1611 * -FDT_ERR_BADLAYOUT
1612 * -FDT_ERR_BADMAGIC,
1613 * -FDT_ERR_BADVERSION,
1614 * -FDT_ERR_BADSTATE,
1615 * -FDT_ERR_BADSTRUCTURE,
1616 * -FDT_ERR_TRUNCATED, standard meanings.
1618 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1621 * fdt_del_node - delete a node (subtree)
1622 * @fdt: pointer to the device tree blob
1623 * @nodeoffset: offset of the node to nop
1625 * fdt_del_node() will remove the given node, including all its
1626 * subnodes if any, from the blob.
1628 * This function will delete data from the blob, and will therefore
1629 * change the offsets of some existing nodes.
1633 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1634 * -FDT_ERR_BADLAYOUT,
1635 * -FDT_ERR_BADMAGIC,
1636 * -FDT_ERR_BADVERSION,
1637 * -FDT_ERR_BADSTATE,
1638 * -FDT_ERR_BADSTRUCTURE,
1639 * -FDT_ERR_TRUNCATED, standard meanings
1641 int fdt_del_node(void *fdt, int nodeoffset);
1643 /**********************************************************************/
1644 /* Debugging / informational functions */
1645 /**********************************************************************/
1647 const char *fdt_strerror(int errval);
1655 * fdt_find_regions() - find regions in device tree
1657 * Given a list of nodes to include and properties to exclude, find
1658 * the regions of the device tree which describe those included parts.
1660 * The intent is to get a list of regions which will be invariant provided
1661 * those parts are invariant. For example, if you request a list of regions
1662 * for all nodes but exclude the property "data", then you will get the
1663 * same region contents regardless of any change to "data" properties.
1665 * This function can be used to produce a byte-stream to send to a hashing
1666 * function to verify that critical parts of the FDT have not changed.
1668 * Nodes which are given in 'inc' are included in the region list, as
1669 * are the names of the immediate subnodes nodes (but not the properties
1670 * or subnodes of those subnodes).
1672 * For eaxample "/" means to include the root node, all root properties
1673 * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1674 * ensures that we capture the names of the subnodes. In a hashing situation
1675 * it prevents the root node from changing at all Any change to non-excluded
1676 * properties, names of subnodes or number of subnodes would be detected.
1678 * When used with FITs this provides the ability to hash and sign parts of
1679 * the FIT based on different configurations in the FIT. Then it is
1680 * impossible to change anything about that configuration (include images
1681 * attached to the configuration), but it may be possible to add new
1682 * configurations, new images or new signatures within the existing
1685 * Adding new properties to a device tree may result in the string table
1686 * being extended (if the new property names are different from those
1687 * already added). This function can optionally include a region for
1688 * the string table so that this can be part of the hash too.
1690 * The device tree header is not included in the list.
1692 * @fdt: Device tree to check
1693 * @inc: List of node paths to included
1694 * @inc_count: Number of node paths in list
1695 * @exc_prop: List of properties names to exclude
1696 * @exc_prop_count: Number of properties in exclude list
1697 * @region: Returns list of regions
1698 * @max_region: Maximum length of region list
1699 * @path: Pointer to a temporary string for the function to use for
1700 * building path names
1701 * @path_len: Length of path, must be large enough to hold the longest
1703 * @add_string_tab: 1 to add a region for the string table
1704 * @return number of regions in list. If this is >max_regions then the
1705 * region array was exhausted. You should increase max_regions and try
1708 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
1709 char * const exc_prop[], int exc_prop_count,
1710 struct fdt_region region[], int max_regions,
1711 char *path, int path_len, int add_string_tab);
1713 #endif /* _LIBFDT_H */