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 #define FDT_ERR_MAX 13
121 /**********************************************************************/
122 /* Low-level functions (you probably don't need these) */
123 /**********************************************************************/
125 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
126 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
128 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
131 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
133 /**********************************************************************/
134 /* Traversal functions */
135 /**********************************************************************/
137 int fdt_next_node(const void *fdt, int offset, int *depth);
140 * fdt_first_subnode() - get offset of first direct subnode
143 * @offset: Offset of node to check
144 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
146 int fdt_first_subnode(const void *fdt, int offset);
149 * fdt_next_subnode() - get offset of next direct subnode
151 * After first calling fdt_first_subnode(), call this function repeatedly to
152 * get direct subnodes of a parent node.
155 * @offset: Offset of previous subnode
156 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
159 int fdt_next_subnode(const void *fdt, int offset);
161 /**********************************************************************/
162 /* General functions */
163 /**********************************************************************/
165 #define fdt_get_header(fdt, field) \
166 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
167 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
168 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
169 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
170 #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
171 #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
172 #define fdt_version(fdt) (fdt_get_header(fdt, version))
173 #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
174 #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
175 #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
176 #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
178 #define __fdt_set_hdr(name) \
179 static inline void fdt_set_##name(void *fdt, uint32_t val) \
181 struct fdt_header *fdth = (struct fdt_header*)fdt; \
182 fdth->name = cpu_to_fdt32(val); \
184 __fdt_set_hdr(magic);
185 __fdt_set_hdr(totalsize);
186 __fdt_set_hdr(off_dt_struct);
187 __fdt_set_hdr(off_dt_strings);
188 __fdt_set_hdr(off_mem_rsvmap);
189 __fdt_set_hdr(version);
190 __fdt_set_hdr(last_comp_version);
191 __fdt_set_hdr(boot_cpuid_phys);
192 __fdt_set_hdr(size_dt_strings);
193 __fdt_set_hdr(size_dt_struct);
197 * fdt_check_header - sanity check a device tree or possible device tree
198 * @fdt: pointer to data which might be a flattened device tree
200 * fdt_check_header() checks that the given buffer contains what
201 * appears to be a flattened device tree with sane information in its
205 * 0, if the buffer appears to contain a valid device tree
207 * -FDT_ERR_BADVERSION,
208 * -FDT_ERR_BADSTATE, standard meanings, as above
210 int fdt_check_header(const void *fdt);
213 * fdt_move - move a device tree around in memory
214 * @fdt: pointer to the device tree to move
215 * @buf: pointer to memory where the device is to be moved
216 * @bufsize: size of the memory space at buf
218 * fdt_move() relocates, if possible, the device tree blob located at
219 * fdt to the buffer at buf of size bufsize. The buffer may overlap
220 * with the existing device tree blob at fdt. Therefore,
221 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
222 * should always succeed.
226 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
228 * -FDT_ERR_BADVERSION,
229 * -FDT_ERR_BADSTATE, standard meanings
231 int fdt_move(const void *fdt, void *buf, int bufsize);
233 /**********************************************************************/
234 /* Read-only functions */
235 /**********************************************************************/
238 * fdt_string - retrieve a string from the strings block of a device tree
239 * @fdt: pointer to the device tree blob
240 * @stroffset: offset of the string within the strings block (native endian)
242 * fdt_string() retrieves a pointer to a single string from the
243 * strings block of the device tree blob at fdt.
246 * a pointer to the string, on success
247 * NULL, if stroffset is out of bounds
249 const char *fdt_string(const void *fdt, int stroffset);
252 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
253 * @fdt: pointer to the device tree blob
255 * Returns the number of entries in the device tree blob's memory
256 * reservation map. This does not include the terminating 0,0 entry
257 * or any other (0,0) entries reserved for expansion.
260 * the number of entries
262 int fdt_num_mem_rsv(const void *fdt);
265 * fdt_get_mem_rsv - retrieve one memory reserve map entry
266 * @fdt: pointer to the device tree blob
267 * @address, @size: pointers to 64-bit variables
269 * On success, *address and *size will contain the address and size of
270 * the n-th reserve map entry from the device tree blob, in
271 * native-endian format.
276 * -FDT_ERR_BADVERSION,
277 * -FDT_ERR_BADSTATE, standard meanings
279 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
282 * fdt_subnode_offset_namelen - find a subnode based on substring
283 * @fdt: pointer to the device tree blob
284 * @parentoffset: structure block offset of a node
285 * @name: name of the subnode to locate
286 * @namelen: number of characters of name to consider
288 * Identical to fdt_subnode_offset(), but only examine the first
289 * namelen characters of name for matching the subnode name. This is
290 * useful for finding subnodes based on a portion of a larger string,
291 * such as a full path.
293 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
294 const char *name, int namelen);
296 * fdt_subnode_offset - find a subnode of a given node
297 * @fdt: pointer to the device tree blob
298 * @parentoffset: structure block offset of a node
299 * @name: name of the subnode to locate
301 * fdt_subnode_offset() finds a subnode of the node at structure block
302 * offset parentoffset with the given name. name may include a unit
303 * address, in which case fdt_subnode_offset() will find the subnode
304 * with that unit address, or the unit address may be omitted, in
305 * which case fdt_subnode_offset() will find an arbitrary subnode
306 * whose name excluding unit address matches the given name.
309 * structure block offset of the requested subnode (>=0), on success
310 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
311 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
313 * -FDT_ERR_BADVERSION,
315 * -FDT_ERR_BADSTRUCTURE,
316 * -FDT_ERR_TRUNCATED, standard meanings.
318 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
321 * fdt_path_offset - find a tree node by its full path
322 * @fdt: pointer to the device tree blob
323 * @path: full path of the node to locate
325 * fdt_path_offset() finds a node of a given path in the device tree.
326 * Each path component may omit the unit address portion, but the
327 * results of this are undefined if any such path component is
328 * ambiguous (that is if there are multiple nodes at the relevant
329 * level matching the given component, differentiated only by unit
333 * structure block offset of the node with the requested path (>=0), on success
334 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
335 * -FDT_ERR_NOTFOUND, if the requested node does not exist
337 * -FDT_ERR_BADVERSION,
339 * -FDT_ERR_BADSTRUCTURE,
340 * -FDT_ERR_TRUNCATED, standard meanings.
342 int fdt_path_offset(const void *fdt, const char *path);
345 * fdt_get_name - retrieve the name of a given node
346 * @fdt: pointer to the device tree blob
347 * @nodeoffset: structure block offset of the starting node
348 * @lenp: pointer to an integer variable (will be overwritten) or NULL
350 * fdt_get_name() retrieves the name (including unit address) of the
351 * device tree node at structure block offset nodeoffset. If lenp is
352 * non-NULL, the length of this name is also returned, in the integer
353 * pointed to by lenp.
356 * pointer to the node's name, on success
357 * If lenp is non-NULL, *lenp contains the length of that name (>=0)
359 * if lenp is non-NULL *lenp contains an error code (<0):
360 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
362 * -FDT_ERR_BADVERSION,
363 * -FDT_ERR_BADSTATE, standard meanings
365 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
368 * fdt_first_property_offset - find the offset of a node's first property
369 * @fdt: pointer to the device tree blob
370 * @nodeoffset: structure block offset of a node
372 * fdt_first_property_offset() finds the first property of the node at
373 * the given structure block offset.
376 * structure block offset of the property (>=0), on success
377 * -FDT_ERR_NOTFOUND, if the requested node has no properties
378 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
380 * -FDT_ERR_BADVERSION,
382 * -FDT_ERR_BADSTRUCTURE,
383 * -FDT_ERR_TRUNCATED, standard meanings.
385 int fdt_first_property_offset(const void *fdt, int nodeoffset);
388 * fdt_next_property_offset - step through a node's properties
389 * @fdt: pointer to the device tree blob
390 * @offset: structure block offset of a property
392 * fdt_next_property_offset() finds the property immediately after the
393 * one at the given structure block offset. This will be a property
394 * of the same node as the given property.
397 * structure block offset of the next property (>=0), on success
398 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
399 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
401 * -FDT_ERR_BADVERSION,
403 * -FDT_ERR_BADSTRUCTURE,
404 * -FDT_ERR_TRUNCATED, standard meanings.
406 int fdt_next_property_offset(const void *fdt, int offset);
409 * fdt_get_property_by_offset - retrieve the property at a given offset
410 * @fdt: pointer to the device tree blob
411 * @offset: offset of the property to retrieve
412 * @lenp: pointer to an integer variable (will be overwritten) or NULL
414 * fdt_get_property_by_offset() retrieves a pointer to the
415 * fdt_property structure within the device tree blob at the given
416 * offset. If lenp is non-NULL, the length of the property value is
417 * also returned, in the integer pointed to by lenp.
420 * pointer to the structure representing the property
421 * if lenp is non-NULL, *lenp contains the length of the property
424 * if lenp is non-NULL, *lenp contains an error code (<0):
425 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
427 * -FDT_ERR_BADVERSION,
429 * -FDT_ERR_BADSTRUCTURE,
430 * -FDT_ERR_TRUNCATED, standard meanings
432 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
437 * fdt_get_property_namelen - find a property based on substring
438 * @fdt: pointer to the device tree blob
439 * @nodeoffset: offset of the node whose property to find
440 * @name: name of the property to find
441 * @namelen: number of characters of name to consider
442 * @lenp: pointer to an integer variable (will be overwritten) or NULL
444 * Identical to fdt_get_property_namelen(), but only examine the first
445 * namelen characters of name for matching the property name.
447 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
450 int namelen, int *lenp);
453 * fdt_get_property - find a given property in a given node
454 * @fdt: pointer to the device tree blob
455 * @nodeoffset: offset of the node whose property to find
456 * @name: name of the property to find
457 * @lenp: pointer to an integer variable (will be overwritten) or NULL
459 * fdt_get_property() retrieves a pointer to the fdt_property
460 * structure within the device tree blob corresponding to the property
461 * named 'name' of the node at offset nodeoffset. If lenp is
462 * non-NULL, the length of the property value is also returned, in the
463 * integer pointed to by lenp.
466 * pointer to the structure representing the property
467 * if lenp is non-NULL, *lenp contains the length of the property
470 * if lenp is non-NULL, *lenp contains an error code (<0):
471 * -FDT_ERR_NOTFOUND, node does not have named property
472 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
474 * -FDT_ERR_BADVERSION,
476 * -FDT_ERR_BADSTRUCTURE,
477 * -FDT_ERR_TRUNCATED, standard meanings
479 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
480 const char *name, int *lenp);
481 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
485 return (struct fdt_property *)(uintptr_t)
486 fdt_get_property(fdt, nodeoffset, name, lenp);
490 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
491 * @fdt: pointer to the device tree blob
492 * @ffset: offset of the property to read
493 * @namep: pointer to a string variable (will be overwritten) or NULL
494 * @lenp: pointer to an integer variable (will be overwritten) or NULL
496 * fdt_getprop_by_offset() retrieves a pointer to the value of the
497 * property at structure block offset 'offset' (this will be a pointer
498 * to within the device blob itself, not a copy of the value). If
499 * lenp is non-NULL, the length of the property value is also
500 * returned, in the integer pointed to by lenp. If namep is non-NULL,
501 * the property's namne will also be returned in the char * pointed to
502 * by namep (this will be a pointer to within the device tree's string
503 * block, not a new copy of the name).
506 * pointer to the property's value
507 * if lenp is non-NULL, *lenp contains the length of the property
509 * if namep is non-NULL *namep contiains a pointer to the property
512 * if lenp is non-NULL, *lenp contains an error code (<0):
513 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
515 * -FDT_ERR_BADVERSION,
517 * -FDT_ERR_BADSTRUCTURE,
518 * -FDT_ERR_TRUNCATED, standard meanings
520 const void *fdt_getprop_by_offset(const void *fdt, int offset,
521 const char **namep, int *lenp);
524 * fdt_getprop_namelen - get property value based on substring
525 * @fdt: pointer to the device tree blob
526 * @nodeoffset: offset of the node whose property to find
527 * @name: name of the property to find
528 * @namelen: number of characters of name to consider
529 * @lenp: pointer to an integer variable (will be overwritten) or NULL
531 * Identical to fdt_getprop(), but only examine the first namelen
532 * characters of name for matching the property name.
534 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
535 const char *name, int namelen, int *lenp);
538 * fdt_getprop - retrieve the value of a given property
539 * @fdt: pointer to the device tree blob
540 * @nodeoffset: offset of the node whose property to find
541 * @name: name of the property to find
542 * @lenp: pointer to an integer variable (will be overwritten) or NULL
544 * fdt_getprop() retrieves a pointer to the value of the property
545 * named 'name' of the node at offset nodeoffset (this will be a
546 * pointer to within the device blob itself, not a copy of the value).
547 * If lenp is non-NULL, the length of the property value is also
548 * returned, in the integer pointed to by lenp.
551 * pointer to the property's value
552 * if lenp is non-NULL, *lenp contains the length of the property
555 * if lenp is non-NULL, *lenp contains an error code (<0):
556 * -FDT_ERR_NOTFOUND, node does not have named property
557 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
559 * -FDT_ERR_BADVERSION,
561 * -FDT_ERR_BADSTRUCTURE,
562 * -FDT_ERR_TRUNCATED, standard meanings
564 const void *fdt_getprop(const void *fdt, int nodeoffset,
565 const char *name, int *lenp);
566 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
567 const char *name, int *lenp)
569 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
573 * fdt_get_phandle - retrieve the phandle of a given node
574 * @fdt: pointer to the device tree blob
575 * @nodeoffset: structure block offset of the node
577 * fdt_get_phandle() retrieves the phandle of the device tree node at
578 * structure block offset nodeoffset.
581 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
582 * 0, if the node has no phandle, or another error occurs
584 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
587 * fdt_get_alias_namelen - get alias based on substring
588 * @fdt: pointer to the device tree blob
589 * @name: name of the alias th look up
590 * @namelen: number of characters of name to consider
592 * Identical to fdt_get_alias(), but only examine the first namelen
593 * characters of name for matching the alias name.
595 const char *fdt_get_alias_namelen(const void *fdt,
596 const char *name, int namelen);
599 * fdt_get_alias - retrieve the path referenced by a given alias
600 * @fdt: pointer to the device tree blob
601 * @name: name of the alias to look up
603 * fdt_get_alias() retrieves the value of a given alias. That is, the
604 * value of the property named 'name' in the node /aliases.
607 * a pointer to the expansion of the alias named 'name', if it exists
608 * NULL, if the given alias or the /aliases node does not exist
610 const char *fdt_get_alias(const void *fdt, const char *name);
613 * fdt_get_path - determine the full path of a node
614 * @fdt: pointer to the device tree blob
615 * @nodeoffset: offset of the node whose path to find
616 * @buf: character buffer to contain the returned path (will be overwritten)
617 * @buflen: size of the character buffer at buf
619 * fdt_get_path() computes the full path of the node at offset
620 * nodeoffset, and records that path in the buffer at buf.
622 * NOTE: This function is expensive, as it must scan the device tree
623 * structure from the start to nodeoffset.
627 * buf contains the absolute path of the node at
628 * nodeoffset, as a NUL-terminated string.
629 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
630 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
631 * characters and will not fit in the given buffer.
633 * -FDT_ERR_BADVERSION,
635 * -FDT_ERR_BADSTRUCTURE, standard meanings
637 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
640 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
641 * @fdt: pointer to the device tree blob
642 * @nodeoffset: offset of the node whose parent to find
643 * @supernodedepth: depth of the ancestor to find
644 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
646 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
647 * at a specific depth from the root (where the root itself has depth
648 * 0, its immediate subnodes depth 1 and so forth). So
649 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
650 * will always return 0, the offset of the root node. If the node at
651 * nodeoffset has depth D, then:
652 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
653 * will return nodeoffset itself.
655 * NOTE: This function is expensive, as it must scan the device tree
656 * structure from the start to nodeoffset.
660 * structure block offset of the node at node offset's ancestor
661 * of depth supernodedepth (>=0), on success
662 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
663 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
665 * -FDT_ERR_BADVERSION,
667 * -FDT_ERR_BADSTRUCTURE, standard meanings
669 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
670 int supernodedepth, int *nodedepth);
673 * fdt_node_depth - find the depth of a given node
674 * @fdt: pointer to the device tree blob
675 * @nodeoffset: offset of the node whose parent to find
677 * fdt_node_depth() finds the depth of a given node. The root node
678 * has depth 0, its immediate subnodes depth 1 and so forth.
680 * NOTE: This function is expensive, as it must scan the device tree
681 * structure from the start to nodeoffset.
684 * depth of the node at nodeoffset (>=0), on success
685 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
687 * -FDT_ERR_BADVERSION,
689 * -FDT_ERR_BADSTRUCTURE, standard meanings
691 int fdt_node_depth(const void *fdt, int nodeoffset);
694 * fdt_parent_offset - find the parent of a given node
695 * @fdt: pointer to the device tree blob
696 * @nodeoffset: offset of the node whose parent to find
698 * fdt_parent_offset() locates the parent node of a given node (that
699 * is, it finds the offset of the node which contains the node at
700 * nodeoffset as a subnode).
702 * NOTE: This function is expensive, as it must scan the device tree
703 * structure from the start to nodeoffset, *twice*.
706 * structure block offset of the parent of the node at nodeoffset
708 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
710 * -FDT_ERR_BADVERSION,
712 * -FDT_ERR_BADSTRUCTURE, standard meanings
714 int fdt_parent_offset(const void *fdt, int nodeoffset);
717 * fdt_node_offset_by_prop_value - find nodes with a given property value
718 * @fdt: pointer to the device tree blob
719 * @startoffset: only find nodes after this offset
720 * @propname: property name to check
721 * @propval: property value to search for
722 * @proplen: length of the value in propval
724 * fdt_node_offset_by_prop_value() returns the offset of the first
725 * node after startoffset, which has a property named propname whose
726 * value is of length proplen and has value equal to propval; or if
727 * startoffset is -1, the very first such node in the tree.
729 * To iterate through all nodes matching the criterion, the following
731 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
733 * while (offset != -FDT_ERR_NOTFOUND) {
734 * ... other code here ...
735 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
739 * Note the -1 in the first call to the function, if 0 is used here
740 * instead, the function will never locate the root node, even if it
741 * matches the criterion.
744 * structure block offset of the located node (>= 0, >startoffset),
746 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
747 * tree after startoffset
748 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
750 * -FDT_ERR_BADVERSION,
752 * -FDT_ERR_BADSTRUCTURE, standard meanings
754 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
755 const char *propname,
756 const void *propval, int proplen);
759 * fdt_node_offset_by_phandle - find the node with a given phandle
760 * @fdt: pointer to the device tree blob
761 * @phandle: phandle value
763 * fdt_node_offset_by_phandle() returns the offset of the node
764 * which has the given phandle value. If there is more than one node
765 * in the tree with the given phandle (an invalid tree), results are
769 * structure block offset of the located node (>= 0), on success
770 * -FDT_ERR_NOTFOUND, no node with that phandle exists
771 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
773 * -FDT_ERR_BADVERSION,
775 * -FDT_ERR_BADSTRUCTURE, standard meanings
777 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
780 * fdt_node_check_compatible: check a node's compatible property
781 * @fdt: pointer to the device tree blob
782 * @nodeoffset: offset of a tree node
783 * @compatible: string to match against
786 * fdt_node_check_compatible() returns 0 if the given node contains a
787 * 'compatible' property with the given string as one of its elements,
788 * it returns non-zero otherwise, or on error.
791 * 0, if the node has a 'compatible' property listing the given string
792 * 1, if the node has a 'compatible' property, but it does not list
794 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
795 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
797 * -FDT_ERR_BADVERSION,
799 * -FDT_ERR_BADSTRUCTURE, standard meanings
801 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
802 const char *compatible);
805 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
806 * @fdt: pointer to the device tree blob
807 * @startoffset: only find nodes after this offset
808 * @compatible: 'compatible' string to match against
810 * fdt_node_offset_by_compatible() returns the offset of the first
811 * node after startoffset, which has a 'compatible' property which
812 * lists the given compatible string; or if startoffset is -1, the
813 * very first such node in the tree.
815 * To iterate through all nodes matching the criterion, the following
817 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
818 * while (offset != -FDT_ERR_NOTFOUND) {
819 * ... other code here ...
820 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
823 * Note the -1 in the first call to the function, if 0 is used here
824 * instead, the function will never locate the root node, even if it
825 * matches the criterion.
828 * structure block offset of the located node (>= 0, >startoffset),
830 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
831 * tree after startoffset
832 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
834 * -FDT_ERR_BADVERSION,
836 * -FDT_ERR_BADSTRUCTURE, standard meanings
838 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
839 const char *compatible);
842 * fdt_stringlist_contains - check a string list property for a string
843 * @strlist: Property containing a list of strings to check
844 * @listlen: Length of property
845 * @str: String to search for
847 * This is a utility function provided for convenience. The list contains
848 * one or more strings, each terminated by \0, as is found in a device tree
849 * "compatible" property.
851 * @return: 1 if the string is found in the list, 0 not found, or invalid list
853 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
855 /**********************************************************************/
856 /* Write-in-place functions */
857 /**********************************************************************/
860 * fdt_setprop_inplace - change a property's value, but not its size
861 * @fdt: pointer to the device tree blob
862 * @nodeoffset: offset of the node whose property to change
863 * @name: name of the property to change
864 * @val: pointer to data to replace the property value with
865 * @len: length of the property value
867 * fdt_setprop_inplace() replaces the value of a given property with
868 * the data in val, of length len. This function cannot change the
869 * size of a property, and so will only work if len is equal to the
870 * current length of the property.
872 * This function will alter only the bytes in the blob which contain
873 * the given property value, and will not alter or move any other part
878 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
879 * -FDT_ERR_NOTFOUND, node does not have the named property
880 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
882 * -FDT_ERR_BADVERSION,
884 * -FDT_ERR_BADSTRUCTURE,
885 * -FDT_ERR_TRUNCATED, standard meanings
887 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
888 const void *val, int len);
891 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
892 * @fdt: pointer to the device tree blob
893 * @nodeoffset: offset of the node whose property to change
894 * @name: name of the property to change
895 * @val: 32-bit integer value to replace the property with
897 * fdt_setprop_inplace_u32() replaces the value of a given property
898 * with the 32-bit integer value in val, converting val to big-endian
899 * if necessary. This function cannot change the size of a property,
900 * and so will only work if the property already exists and has length
903 * This function will alter only the bytes in the blob which contain
904 * the given property value, and will not alter or move any other part
909 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
910 * -FDT_ERR_NOTFOUND, node does not have the named property
911 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
913 * -FDT_ERR_BADVERSION,
915 * -FDT_ERR_BADSTRUCTURE,
916 * -FDT_ERR_TRUNCATED, standard meanings
918 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
919 const char *name, uint32_t val)
921 fdt32_t tmp = cpu_to_fdt32(val);
922 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
926 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
927 * @fdt: pointer to the device tree blob
928 * @nodeoffset: offset of the node whose property to change
929 * @name: name of the property to change
930 * @val: 64-bit integer value to replace the property with
932 * fdt_setprop_inplace_u64() replaces the value of a given property
933 * with the 64-bit integer value in val, converting val to big-endian
934 * if necessary. This function cannot change the size of a property,
935 * and so will only work if the property already exists and has length
938 * This function will alter only the bytes in the blob which contain
939 * the given property value, and will not alter or move any other part
944 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
945 * -FDT_ERR_NOTFOUND, node does not have the named property
946 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
948 * -FDT_ERR_BADVERSION,
950 * -FDT_ERR_BADSTRUCTURE,
951 * -FDT_ERR_TRUNCATED, standard meanings
953 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
954 const char *name, uint64_t val)
956 fdt64_t tmp = cpu_to_fdt64(val);
957 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
961 * fdt_setprop_inplace_cell - change the value of a single-cell property
963 * This is an alternative name for fdt_setprop_inplace_u32()
965 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
966 const char *name, uint32_t val)
968 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
972 * fdt_nop_property - replace a property with nop tags
973 * @fdt: pointer to the device tree blob
974 * @nodeoffset: offset of the node whose property to nop
975 * @name: name of the property to nop
977 * fdt_nop_property() will replace a given property's representation
978 * in the blob with FDT_NOP tags, effectively removing it from the
981 * This function will alter only the bytes in the blob which contain
982 * the property, and will not alter or move any other part of the
987 * -FDT_ERR_NOTFOUND, node does not have the named property
988 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
990 * -FDT_ERR_BADVERSION,
992 * -FDT_ERR_BADSTRUCTURE,
993 * -FDT_ERR_TRUNCATED, standard meanings
995 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
998 * fdt_nop_node - replace a node (subtree) with nop tags
999 * @fdt: pointer to the device tree blob
1000 * @nodeoffset: offset of the node to nop
1002 * fdt_nop_node() will replace a given node's representation in the
1003 * blob, including all its subnodes, if any, with FDT_NOP tags,
1004 * effectively removing it from the tree.
1006 * This function will alter only the bytes in the blob which contain
1007 * the node and its properties and subnodes, and will not alter or
1008 * move any other part of the tree.
1012 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1013 * -FDT_ERR_BADMAGIC,
1014 * -FDT_ERR_BADVERSION,
1015 * -FDT_ERR_BADSTATE,
1016 * -FDT_ERR_BADSTRUCTURE,
1017 * -FDT_ERR_TRUNCATED, standard meanings
1019 int fdt_nop_node(void *fdt, int nodeoffset);
1021 /**********************************************************************/
1022 /* Sequential write functions */
1023 /**********************************************************************/
1025 int fdt_create(void *buf, int bufsize);
1026 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1027 int fdt_finish_reservemap(void *fdt);
1028 int fdt_begin_node(void *fdt, const char *name);
1029 int fdt_property(void *fdt, const char *name, const void *val, int len);
1030 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1032 fdt32_t tmp = cpu_to_fdt32(val);
1033 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1035 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1037 fdt64_t tmp = cpu_to_fdt64(val);
1038 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1040 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1042 return fdt_property_u32(fdt, name, val);
1044 #define fdt_property_string(fdt, name, str) \
1045 fdt_property(fdt, name, str, strlen(str)+1)
1046 int fdt_end_node(void *fdt);
1047 int fdt_finish(void *fdt);
1049 /**********************************************************************/
1050 /* Read-write functions */
1051 /**********************************************************************/
1053 int fdt_create_empty_tree(void *buf, int bufsize);
1054 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1055 int fdt_pack(void *fdt);
1058 * fdt_add_mem_rsv - add one memory reserve map entry
1059 * @fdt: pointer to the device tree blob
1060 * @address, @size: 64-bit values (native endian)
1062 * Adds a reserve map entry to the given blob reserving a region at
1063 * address address of length size.
1065 * This function will insert data into the reserve map and will
1066 * therefore change the indexes of some entries in the table.
1070 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1071 * contain the new reservation entry
1072 * -FDT_ERR_BADMAGIC,
1073 * -FDT_ERR_BADVERSION,
1074 * -FDT_ERR_BADSTATE,
1075 * -FDT_ERR_BADSTRUCTURE,
1076 * -FDT_ERR_BADLAYOUT,
1077 * -FDT_ERR_TRUNCATED, standard meanings
1079 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1082 * fdt_del_mem_rsv - remove a memory reserve map entry
1083 * @fdt: pointer to the device tree blob
1084 * @n: entry to remove
1086 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1089 * This function will delete data from the reservation table and will
1090 * therefore change the indexes of some entries in the table.
1094 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1095 * are less than n+1 reserve map entries)
1096 * -FDT_ERR_BADMAGIC,
1097 * -FDT_ERR_BADVERSION,
1098 * -FDT_ERR_BADSTATE,
1099 * -FDT_ERR_BADSTRUCTURE,
1100 * -FDT_ERR_BADLAYOUT,
1101 * -FDT_ERR_TRUNCATED, standard meanings
1103 int fdt_del_mem_rsv(void *fdt, int n);
1106 * fdt_set_name - change the name of a given node
1107 * @fdt: pointer to the device tree blob
1108 * @nodeoffset: structure block offset of a node
1109 * @name: name to give the node
1111 * fdt_set_name() replaces the name (including unit address, if any)
1112 * of the given node with the given string. NOTE: this function can't
1113 * efficiently check if the new name is unique amongst the given
1114 * node's siblings; results are undefined if this function is invoked
1115 * with a name equal to one of the given node's siblings.
1117 * This function may insert or delete data from the blob, and will
1118 * therefore change the offsets of some existing nodes.
1122 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1123 * to contain the new name
1124 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1125 * -FDT_ERR_BADMAGIC,
1126 * -FDT_ERR_BADVERSION,
1127 * -FDT_ERR_BADSTATE, standard meanings
1129 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1132 * fdt_setprop - create or change a property
1133 * @fdt: pointer to the device tree blob
1134 * @nodeoffset: offset of the node whose property to change
1135 * @name: name of the property to change
1136 * @val: pointer to data to set the property value to
1137 * @len: length of the property value
1139 * fdt_setprop() sets the value of the named property in the given
1140 * node to the given value and length, creating the property if it
1141 * does not already exist.
1143 * This function may insert or delete data from the blob, and will
1144 * therefore change the offsets of some existing nodes.
1148 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1149 * contain the new property value
1150 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1151 * -FDT_ERR_BADLAYOUT,
1152 * -FDT_ERR_BADMAGIC,
1153 * -FDT_ERR_BADVERSION,
1154 * -FDT_ERR_BADSTATE,
1155 * -FDT_ERR_BADSTRUCTURE,
1156 * -FDT_ERR_BADLAYOUT,
1157 * -FDT_ERR_TRUNCATED, standard meanings
1159 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1160 const void *val, int len);
1163 * fdt_setprop_u32 - set a property to a 32-bit integer
1164 * @fdt: pointer to the device tree blob
1165 * @nodeoffset: offset of the node whose property to change
1166 * @name: name of the property to change
1167 * @val: 32-bit integer value for the property (native endian)
1169 * fdt_setprop_u32() sets the value of the named property in the given
1170 * node to the given 32-bit integer value (converting to big-endian if
1171 * necessary), or creates a new property with that value if it does
1172 * not already exist.
1174 * This function may insert or delete data from the blob, and will
1175 * therefore change the offsets of some existing nodes.
1179 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1180 * contain the new property value
1181 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1182 * -FDT_ERR_BADLAYOUT,
1183 * -FDT_ERR_BADMAGIC,
1184 * -FDT_ERR_BADVERSION,
1185 * -FDT_ERR_BADSTATE,
1186 * -FDT_ERR_BADSTRUCTURE,
1187 * -FDT_ERR_BADLAYOUT,
1188 * -FDT_ERR_TRUNCATED, standard meanings
1190 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1193 fdt32_t tmp = cpu_to_fdt32(val);
1194 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1198 * fdt_setprop_u64 - set a property to a 64-bit integer
1199 * @fdt: pointer to the device tree blob
1200 * @nodeoffset: offset of the node whose property to change
1201 * @name: name of the property to change
1202 * @val: 64-bit integer value for the property (native endian)
1204 * fdt_setprop_u64() sets the value of the named property in the given
1205 * node to the given 64-bit integer value (converting to big-endian if
1206 * necessary), or creates a new property with that value if it does
1207 * not already exist.
1209 * This function may insert or delete data from the blob, and will
1210 * therefore change the offsets of some existing nodes.
1214 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1215 * contain the new property value
1216 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1217 * -FDT_ERR_BADLAYOUT,
1218 * -FDT_ERR_BADMAGIC,
1219 * -FDT_ERR_BADVERSION,
1220 * -FDT_ERR_BADSTATE,
1221 * -FDT_ERR_BADSTRUCTURE,
1222 * -FDT_ERR_BADLAYOUT,
1223 * -FDT_ERR_TRUNCATED, standard meanings
1225 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1228 fdt64_t tmp = cpu_to_fdt64(val);
1229 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1233 * fdt_setprop_cell - set a property to a single cell value
1235 * This is an alternative name for fdt_setprop_u32()
1237 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1240 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1244 * fdt_setprop_string - set a property to a string value
1245 * @fdt: pointer to the device tree blob
1246 * @nodeoffset: offset of the node whose property to change
1247 * @name: name of the property to change
1248 * @str: string value for the property
1250 * fdt_setprop_string() sets the value of the named property in the
1251 * given node to the given string value (using the length of the
1252 * string to determine the new length of the property), or creates a
1253 * new property with that value if it does not already exist.
1255 * This function may insert or delete data from the blob, and will
1256 * therefore change the offsets of some existing nodes.
1260 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1261 * contain the new property value
1262 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1263 * -FDT_ERR_BADLAYOUT,
1264 * -FDT_ERR_BADMAGIC,
1265 * -FDT_ERR_BADVERSION,
1266 * -FDT_ERR_BADSTATE,
1267 * -FDT_ERR_BADSTRUCTURE,
1268 * -FDT_ERR_BADLAYOUT,
1269 * -FDT_ERR_TRUNCATED, standard meanings
1271 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1272 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1275 * fdt_appendprop - append to or create a property
1276 * @fdt: pointer to the device tree blob
1277 * @nodeoffset: offset of the node whose property to change
1278 * @name: name of the property to append to
1279 * @val: pointer to data to append to the property value
1280 * @len: length of the data to append to the property value
1282 * fdt_appendprop() appends the value to the named property in the
1283 * given node, creating the property if it does not already exist.
1285 * This function may insert data into the blob, and will therefore
1286 * change the offsets of some existing nodes.
1290 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1291 * contain the new property value
1292 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1293 * -FDT_ERR_BADLAYOUT,
1294 * -FDT_ERR_BADMAGIC,
1295 * -FDT_ERR_BADVERSION,
1296 * -FDT_ERR_BADSTATE,
1297 * -FDT_ERR_BADSTRUCTURE,
1298 * -FDT_ERR_BADLAYOUT,
1299 * -FDT_ERR_TRUNCATED, standard meanings
1301 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1302 const void *val, int len);
1305 * fdt_appendprop_u32 - append a 32-bit integer value to a property
1306 * @fdt: pointer to the device tree blob
1307 * @nodeoffset: offset of the node whose property to change
1308 * @name: name of the property to change
1309 * @val: 32-bit integer value to append to the property (native endian)
1311 * fdt_appendprop_u32() appends the given 32-bit integer value
1312 * (converting to big-endian if necessary) to the value of the named
1313 * property in the given node, or creates a new property with that
1314 * value if it does not already exist.
1316 * This function may insert data into the blob, and will therefore
1317 * change the offsets of some existing nodes.
1321 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1322 * contain the new property value
1323 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1324 * -FDT_ERR_BADLAYOUT,
1325 * -FDT_ERR_BADMAGIC,
1326 * -FDT_ERR_BADVERSION,
1327 * -FDT_ERR_BADSTATE,
1328 * -FDT_ERR_BADSTRUCTURE,
1329 * -FDT_ERR_BADLAYOUT,
1330 * -FDT_ERR_TRUNCATED, standard meanings
1332 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1333 const char *name, uint32_t val)
1335 fdt32_t tmp = cpu_to_fdt32(val);
1336 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1340 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1341 * @fdt: pointer to the device tree blob
1342 * @nodeoffset: offset of the node whose property to change
1343 * @name: name of the property to change
1344 * @val: 64-bit integer value to append to the property (native endian)
1346 * fdt_appendprop_u64() appends the given 64-bit integer value
1347 * (converting to big-endian if necessary) to the value of the named
1348 * property in the given node, or creates a new property with that
1349 * value if it does not already exist.
1351 * This function may insert data into the blob, and will therefore
1352 * change the offsets of some existing nodes.
1356 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1357 * contain the new property value
1358 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1359 * -FDT_ERR_BADLAYOUT,
1360 * -FDT_ERR_BADMAGIC,
1361 * -FDT_ERR_BADVERSION,
1362 * -FDT_ERR_BADSTATE,
1363 * -FDT_ERR_BADSTRUCTURE,
1364 * -FDT_ERR_BADLAYOUT,
1365 * -FDT_ERR_TRUNCATED, standard meanings
1367 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1368 const char *name, uint64_t val)
1370 fdt64_t tmp = cpu_to_fdt64(val);
1371 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1375 * fdt_appendprop_cell - append a single cell value to a property
1377 * This is an alternative name for fdt_appendprop_u32()
1379 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1380 const char *name, uint32_t val)
1382 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1386 * fdt_appendprop_string - append a string to a property
1387 * @fdt: pointer to the device tree blob
1388 * @nodeoffset: offset of the node whose property to change
1389 * @name: name of the property to change
1390 * @str: string value to append to the property
1392 * fdt_appendprop_string() appends the given string to the value of
1393 * the named property in the given node, or creates a new property
1394 * with that value if it does not already exist.
1396 * This function may insert data into the blob, and will therefore
1397 * change the offsets of some existing nodes.
1401 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1402 * contain the new property value
1403 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1404 * -FDT_ERR_BADLAYOUT,
1405 * -FDT_ERR_BADMAGIC,
1406 * -FDT_ERR_BADVERSION,
1407 * -FDT_ERR_BADSTATE,
1408 * -FDT_ERR_BADSTRUCTURE,
1409 * -FDT_ERR_BADLAYOUT,
1410 * -FDT_ERR_TRUNCATED, standard meanings
1412 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1413 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1416 * fdt_delprop - delete a property
1417 * @fdt: pointer to the device tree blob
1418 * @nodeoffset: offset of the node whose property to nop
1419 * @name: name of the property to nop
1421 * fdt_del_property() will delete the given property.
1423 * This function will delete data from the blob, and will therefore
1424 * change the offsets of some existing nodes.
1428 * -FDT_ERR_NOTFOUND, node does not have the named property
1429 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1430 * -FDT_ERR_BADLAYOUT,
1431 * -FDT_ERR_BADMAGIC,
1432 * -FDT_ERR_BADVERSION,
1433 * -FDT_ERR_BADSTATE,
1434 * -FDT_ERR_BADSTRUCTURE,
1435 * -FDT_ERR_TRUNCATED, standard meanings
1437 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1440 * fdt_add_subnode_namelen - creates a new node based on substring
1441 * @fdt: pointer to the device tree blob
1442 * @parentoffset: structure block offset of a node
1443 * @name: name of the subnode to locate
1444 * @namelen: number of characters of name to consider
1446 * Identical to fdt_add_subnode(), but use only the first namelen
1447 * characters of name as the name of the new node. This is useful for
1448 * creating subnodes based on a portion of a larger string, such as a
1451 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1452 const char *name, int namelen);
1455 * fdt_add_subnode - creates a new node
1456 * @fdt: pointer to the device tree blob
1457 * @parentoffset: structure block offset of a node
1458 * @name: name of the subnode to locate
1460 * fdt_add_subnode() creates a new node as a subnode of the node at
1461 * structure block offset parentoffset, with the given name (which
1462 * should include the unit address, if any).
1464 * This function will insert data into the blob, and will therefore
1465 * change the offsets of some existing nodes.
1468 * structure block offset of the created nodeequested subnode (>=0), on success
1469 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1470 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1471 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1473 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1474 * blob to contain the new node
1476 * -FDT_ERR_BADLAYOUT
1477 * -FDT_ERR_BADMAGIC,
1478 * -FDT_ERR_BADVERSION,
1479 * -FDT_ERR_BADSTATE,
1480 * -FDT_ERR_BADSTRUCTURE,
1481 * -FDT_ERR_TRUNCATED, standard meanings.
1483 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1486 * fdt_del_node - delete a node (subtree)
1487 * @fdt: pointer to the device tree blob
1488 * @nodeoffset: offset of the node to nop
1490 * fdt_del_node() will remove the given node, including all its
1491 * subnodes if any, from the blob.
1493 * This function will delete data from the blob, and will therefore
1494 * change the offsets of some existing nodes.
1498 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1499 * -FDT_ERR_BADLAYOUT,
1500 * -FDT_ERR_BADMAGIC,
1501 * -FDT_ERR_BADVERSION,
1502 * -FDT_ERR_BADSTATE,
1503 * -FDT_ERR_BADSTRUCTURE,
1504 * -FDT_ERR_TRUNCATED, standard meanings
1506 int fdt_del_node(void *fdt, int nodeoffset);
1508 /**********************************************************************/
1509 /* Debugging / informational functions */
1510 /**********************************************************************/
1512 const char *fdt_strerror(int errval);
1520 * fdt_find_regions() - find regions in device tree
1522 * Given a list of nodes to include and properties to exclude, find
1523 * the regions of the device tree which describe those included parts.
1525 * The intent is to get a list of regions which will be invariant provided
1526 * those parts are invariant. For example, if you request a list of regions
1527 * for all nodes but exclude the property "data", then you will get the
1528 * same region contents regardless of any change to "data" properties.
1530 * This function can be used to produce a byte-stream to send to a hashing
1531 * function to verify that critical parts of the FDT have not changed.
1533 * Nodes which are given in 'inc' are included in the region list, as
1534 * are the names of the immediate subnodes nodes (but not the properties
1535 * or subnodes of those subnodes).
1537 * For eaxample "/" means to include the root node, all root properties
1538 * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1539 * ensures that we capture the names of the subnodes. In a hashing situation
1540 * it prevents the root node from changing at all Any change to non-excluded
1541 * properties, names of subnodes or number of subnodes would be detected.
1543 * When used with FITs this provides the ability to hash and sign parts of
1544 * the FIT based on different configurations in the FIT. Then it is
1545 * impossible to change anything about that configuration (include images
1546 * attached to the configuration), but it may be possible to add new
1547 * configurations, new images or new signatures within the existing
1550 * Adding new properties to a device tree may result in the string table
1551 * being extended (if the new property names are different from those
1552 * already added). This function can optionally include a region for
1553 * the string table so that this can be part of the hash too.
1555 * The device tree header is not included in the list.
1557 * @fdt: Device tree to check
1558 * @inc: List of node paths to included
1559 * @inc_count: Number of node paths in list
1560 * @exc_prop: List of properties names to exclude
1561 * @exc_prop_count: Number of properties in exclude list
1562 * @region: Returns list of regions
1563 * @max_region: Maximum length of region list
1564 * @path: Pointer to a temporary string for the function to use for
1565 * building path names
1566 * @path_len: Length of path, must be large enough to hold the longest
1568 * @add_string_tab: 1 to add a region for the string table
1569 * @return number of regions in list. If this is >max_regions then the
1570 * region array was exhausted. You should increase max_regions and try
1573 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
1574 char * const exc_prop[], int exc_prop_count,
1575 struct fdt_region region[], int max_regions,
1576 char *path, int path_len, int add_string_tab);
1578 #endif /* _LIBFDT_H */