4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
7 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
10 #include <libfdt_env.h>
13 #define FDT_FIRST_SUPPORTED_VERSION 0x10
14 #define FDT_LAST_SUPPORTED_VERSION 0x11
16 /* Error codes: informative error codes */
17 #define FDT_ERR_NOTFOUND 1
18 /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
19 #define FDT_ERR_EXISTS 2
20 /* FDT_ERR_EXISTS: Attempted to create a node or property which
22 #define FDT_ERR_NOSPACE 3
23 /* FDT_ERR_NOSPACE: Operation needed to expand the device
24 * tree, but its buffer did not have sufficient space to
25 * contain the expanded tree. Use fdt_open_into() to move the
26 * device tree to a buffer with more space. */
28 /* Error codes: codes for bad parameters */
29 #define FDT_ERR_BADOFFSET 4
30 /* FDT_ERR_BADOFFSET: Function was passed a structure block
31 * offset which is out-of-bounds, or which points to an
32 * unsuitable part of the structure for the operation. */
33 #define FDT_ERR_BADPATH 5
34 /* FDT_ERR_BADPATH: Function was passed a badly formatted path
35 * (e.g. missing a leading / for a function which requires an
37 #define FDT_ERR_BADPHANDLE 6
38 /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle.
39 * This can be caused either by an invalid phandle property
40 * length, or the phandle value was either 0 or -1, which are
42 #define FDT_ERR_BADSTATE 7
43 /* FDT_ERR_BADSTATE: Function was passed an incomplete device
44 * tree created by the sequential-write functions, which is
45 * not sufficiently complete for the requested operation. */
47 /* Error codes: codes for bad device tree blobs */
48 #define FDT_ERR_TRUNCATED 8
49 /* FDT_ERR_TRUNCATED: Structure block of the given device tree
50 * ends without an FDT_END tag. */
51 #define FDT_ERR_BADMAGIC 9
52 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
53 * device tree at all - it is missing the flattened device
54 * tree magic number. */
55 #define FDT_ERR_BADVERSION 10
56 /* FDT_ERR_BADVERSION: Given device tree has a version which
57 * can't be handled by the requested operation. For
58 * read-write functions, this may mean that fdt_open_into() is
59 * required to convert the tree to the expected version. */
60 #define FDT_ERR_BADSTRUCTURE 11
61 /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
62 * structure block or other serious error (e.g. misnested
63 * nodes, or subnodes preceding properties). */
64 #define FDT_ERR_BADLAYOUT 12
65 /* FDT_ERR_BADLAYOUT: For read-write functions, the given
66 * device tree has it's sub-blocks in an order that the
67 * function can't handle (memory reserve map, then structure,
68 * then strings). Use fdt_open_into() to reorganize the tree
69 * into a form suitable for the read-write operations. */
71 /* "Can't happen" error indicating a bug in libfdt */
72 #define FDT_ERR_INTERNAL 13
73 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
74 * Should never be returned, if it is, it indicates a bug in
77 /* Errors in device tree content */
78 #define FDT_ERR_BADNCELLS 14
79 /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
80 * or similar property with a bad format or value */
82 #define FDT_ERR_BADVALUE 15
83 /* FDT_ERR_BADVALUE: Device tree has a property with an unexpected
84 * value. For example: a property expected to contain a string list
85 * is not NUL-terminated within the length of its value. */
87 #define FDT_ERR_BADOVERLAY 16
88 /* FDT_ERR_BADOVERLAY: The device tree overlay, while
89 * correctly structured, cannot be applied due to some
90 * unexpected or missing value, property or node. */
92 #define FDT_ERR_NOPHANDLES 17
93 /* FDT_ERR_NOPHANDLES: The device tree doesn't have any
94 * phandle available anymore without causing an overflow */
96 #define FDT_ERR_MAX 17
98 /**********************************************************************/
99 /* Low-level functions (you probably don't need these) */
100 /**********************************************************************/
102 #ifndef SWIG /* This function is not useful in Python */
103 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
105 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
107 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
110 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
112 /**********************************************************************/
113 /* Traversal functions */
114 /**********************************************************************/
116 int fdt_next_node(const void *fdt, int offset, int *depth);
119 * fdt_first_subnode() - get offset of first direct subnode
122 * @offset: Offset of node to check
123 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
125 int fdt_first_subnode(const void *fdt, int offset);
128 * fdt_next_subnode() - get offset of next direct subnode
130 * After first calling fdt_first_subnode(), call this function repeatedly to
131 * get direct subnodes of a parent node.
134 * @offset: Offset of previous subnode
135 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
138 int fdt_next_subnode(const void *fdt, int offset);
141 * fdt_for_each_subnode - iterate over all subnodes of a parent
143 * @node: child node (int, lvalue)
144 * @fdt: FDT blob (const void *)
145 * @parent: parent node (int)
147 * This is actually a wrapper around a for loop and would be used like so:
149 * fdt_for_each_subnode(node, fdt, parent) {
154 * if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) {
158 * Note that this is implemented as a macro and @node is used as
159 * iterator in the loop. The parent variable be constant or even a
163 #define fdt_for_each_subnode(node, fdt, parent) \
164 for (node = fdt_first_subnode(fdt, parent); \
166 node = fdt_next_subnode(fdt, node))
168 /**********************************************************************/
169 /* General functions */
170 /**********************************************************************/
171 #define fdt_get_header(fdt, field) \
172 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
173 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
174 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
175 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
176 #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
177 #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
178 #define fdt_version(fdt) (fdt_get_header(fdt, version))
179 #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
180 #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
181 #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
182 #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
184 #define __fdt_set_hdr(name) \
185 static inline void fdt_set_##name(void *fdt, uint32_t val) \
187 struct fdt_header *fdth = (struct fdt_header *)fdt; \
188 fdth->name = cpu_to_fdt32(val); \
190 __fdt_set_hdr(magic);
191 __fdt_set_hdr(totalsize);
192 __fdt_set_hdr(off_dt_struct);
193 __fdt_set_hdr(off_dt_strings);
194 __fdt_set_hdr(off_mem_rsvmap);
195 __fdt_set_hdr(version);
196 __fdt_set_hdr(last_comp_version);
197 __fdt_set_hdr(boot_cpuid_phys);
198 __fdt_set_hdr(size_dt_strings);
199 __fdt_set_hdr(size_dt_struct);
203 * fdt_check_header - sanity check a device tree or possible device tree
204 * @fdt: pointer to data which might be a flattened device tree
206 * fdt_check_header() checks that the given buffer contains what
207 * appears to be a flattened device tree with sane information in its
211 * 0, if the buffer appears to contain a valid device tree
213 * -FDT_ERR_BADVERSION,
214 * -FDT_ERR_BADSTATE, standard meanings, as above
216 int fdt_check_header(const void *fdt);
219 * fdt_move - move a device tree around in memory
220 * @fdt: pointer to the device tree to move
221 * @buf: pointer to memory where the device is to be moved
222 * @bufsize: size of the memory space at buf
224 * fdt_move() relocates, if possible, the device tree blob located at
225 * fdt to the buffer at buf of size bufsize. The buffer may overlap
226 * with the existing device tree blob at fdt. Therefore,
227 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
228 * should always succeed.
232 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
234 * -FDT_ERR_BADVERSION,
235 * -FDT_ERR_BADSTATE, standard meanings
237 int fdt_move(const void *fdt, void *buf, int bufsize);
239 /**********************************************************************/
240 /* Read-only functions */
241 /**********************************************************************/
244 * fdt_string - retrieve a string from the strings block of a device tree
245 * @fdt: pointer to the device tree blob
246 * @stroffset: offset of the string within the strings block (native endian)
248 * fdt_string() retrieves a pointer to a single string from the
249 * strings block of the device tree blob at fdt.
252 * a pointer to the string, on success
253 * NULL, if stroffset is out of bounds
255 const char *fdt_string(const void *fdt, int stroffset);
258 * fdt_get_max_phandle - retrieves the highest phandle in a tree
259 * @fdt: pointer to the device tree blob
261 * fdt_get_max_phandle retrieves the highest phandle in the given
262 * device tree. This will ignore badly formatted phandles, or phandles
263 * with a value of 0 or -1.
266 * the highest phandle on success
267 * 0, if no phandle was found in the device tree
268 * -1, if an error occurred
270 uint32_t fdt_get_max_phandle(const void *fdt);
273 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
274 * @fdt: pointer to the device tree blob
276 * Returns the number of entries in the device tree blob's memory
277 * reservation map. This does not include the terminating 0,0 entry
278 * or any other (0,0) entries reserved for expansion.
281 * the number of entries
283 int fdt_num_mem_rsv(const void *fdt);
286 * fdt_get_mem_rsv - retrieve one memory reserve map entry
287 * @fdt: pointer to the device tree blob
288 * @address, @size: pointers to 64-bit variables
290 * On success, *address and *size will contain the address and size of
291 * the n-th reserve map entry from the device tree blob, in
292 * native-endian format.
297 * -FDT_ERR_BADVERSION,
298 * -FDT_ERR_BADSTATE, standard meanings
300 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
303 * fdt_subnode_offset_namelen - find a subnode based on substring
304 * @fdt: pointer to the device tree blob
305 * @parentoffset: structure block offset of a node
306 * @name: name of the subnode to locate
307 * @namelen: number of characters of name to consider
309 * Identical to fdt_subnode_offset(), but only examine the first
310 * namelen characters of name for matching the subnode name. This is
311 * useful for finding subnodes based on a portion of a larger string,
312 * such as a full path.
314 #ifndef SWIG /* Not available in Python */
315 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
316 const char *name, int namelen);
319 * fdt_subnode_offset - find a subnode of a given node
320 * @fdt: pointer to the device tree blob
321 * @parentoffset: structure block offset of a node
322 * @name: name of the subnode to locate
324 * fdt_subnode_offset() finds a subnode of the node at structure block
325 * offset parentoffset with the given name. name may include a unit
326 * address, in which case fdt_subnode_offset() will find the subnode
327 * with that unit address, or the unit address may be omitted, in
328 * which case fdt_subnode_offset() will find an arbitrary subnode
329 * whose name excluding unit address matches the given name.
332 * structure block offset of the requested subnode (>=0), on success
333 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
334 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
337 * -FDT_ERR_BADVERSION,
339 * -FDT_ERR_BADSTRUCTURE,
340 * -FDT_ERR_TRUNCATED, standard meanings.
342 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
345 * fdt_path_offset_namelen - find a tree node by its full path
346 * @fdt: pointer to the device tree blob
347 * @path: full path of the node to locate
348 * @namelen: number of characters of path to consider
350 * Identical to fdt_path_offset(), but only consider the first namelen
351 * characters of path as the path name.
353 #ifndef SWIG /* Not available in Python */
354 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
358 * fdt_path_offset - find a tree node by its full path
359 * @fdt: pointer to the device tree blob
360 * @path: full path of the node to locate
362 * fdt_path_offset() finds a node of a given path in the device tree.
363 * Each path component may omit the unit address portion, but the
364 * results of this are undefined if any such path component is
365 * ambiguous (that is if there are multiple nodes at the relevant
366 * level matching the given component, differentiated only by unit
370 * structure block offset of the node with the requested path (>=0), on
372 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
373 * -FDT_ERR_NOTFOUND, if the requested node does not exist
375 * -FDT_ERR_BADVERSION,
377 * -FDT_ERR_BADSTRUCTURE,
378 * -FDT_ERR_TRUNCATED, standard meanings.
380 int fdt_path_offset(const void *fdt, const char *path);
383 * fdt_get_name - retrieve the name of a given node
384 * @fdt: pointer to the device tree blob
385 * @nodeoffset: structure block offset of the starting node
386 * @lenp: pointer to an integer variable (will be overwritten) or NULL
388 * fdt_get_name() retrieves the name (including unit address) of the
389 * device tree node at structure block offset nodeoffset. If lenp is
390 * non-NULL, the length of this name is also returned, in the integer
391 * pointed to by lenp.
394 * pointer to the node's name, on success
395 * If lenp is non-NULL, *lenp contains the length of that name
398 * if lenp is non-NULL *lenp contains an error code (<0):
399 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
402 * -FDT_ERR_BADVERSION,
403 * -FDT_ERR_BADSTATE, standard meanings
405 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
408 * fdt_first_property_offset - find the offset of a node's first property
409 * @fdt: pointer to the device tree blob
410 * @nodeoffset: structure block offset of a node
412 * fdt_first_property_offset() finds the first property of the node at
413 * the given structure block offset.
416 * structure block offset of the property (>=0), on success
417 * -FDT_ERR_NOTFOUND, if the requested node has no properties
418 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
420 * -FDT_ERR_BADVERSION,
422 * -FDT_ERR_BADSTRUCTURE,
423 * -FDT_ERR_TRUNCATED, standard meanings.
425 int fdt_first_property_offset(const void *fdt, int nodeoffset);
428 * fdt_next_property_offset - step through a node's properties
429 * @fdt: pointer to the device tree blob
430 * @offset: structure block offset of a property
432 * fdt_next_property_offset() finds the property immediately after the
433 * one at the given structure block offset. This will be a property
434 * of the same node as the given property.
437 * structure block offset of the next property (>=0), on success
438 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
439 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
441 * -FDT_ERR_BADVERSION,
443 * -FDT_ERR_BADSTRUCTURE,
444 * -FDT_ERR_TRUNCATED, standard meanings.
446 int fdt_next_property_offset(const void *fdt, int offset);
449 * fdt_for_each_property_offset - iterate over all properties of a node
451 * @property_offset: property offset (int, lvalue)
452 * @fdt: FDT blob (const void *)
453 * @node: node offset (int)
455 * This is actually a wrapper around a for loop and would be used like so:
457 * fdt_for_each_property_offset(property, fdt, node) {
462 * if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) {
466 * Note that this is implemented as a macro and property is used as
467 * iterator in the loop. The node variable can be constant or even a
470 #define fdt_for_each_property_offset(property, fdt, node) \
471 for (property = fdt_first_property_offset(fdt, node); \
473 property = fdt_next_property_offset(fdt, property))
476 * fdt_get_property_by_offset - retrieve the property at a given offset
477 * @fdt: pointer to the device tree blob
478 * @offset: offset of the property to retrieve
479 * @lenp: pointer to an integer variable (will be overwritten) or NULL
481 * fdt_get_property_by_offset() retrieves a pointer to the
482 * fdt_property structure within the device tree blob at the given
483 * offset. If lenp is non-NULL, the length of the property value is
484 * also returned, in the integer pointed to by lenp.
487 * pointer to the structure representing the property
488 * if lenp is non-NULL, *lenp contains the length of the property
491 * if lenp is non-NULL, *lenp contains an error code (<0):
492 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
494 * -FDT_ERR_BADVERSION,
496 * -FDT_ERR_BADSTRUCTURE,
497 * -FDT_ERR_TRUNCATED, standard meanings
499 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
504 * fdt_get_property_namelen - find a property based on substring
505 * @fdt: pointer to the device tree blob
506 * @nodeoffset: offset of the node whose property to find
507 * @name: name of the property to find
508 * @namelen: number of characters of name to consider
509 * @lenp: pointer to an integer variable (will be overwritten) or NULL
511 * Identical to fdt_get_property(), but only examine the first namelen
512 * characters of name for matching the property name.
514 #ifndef SWIG /* Not available in Python */
515 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
518 int namelen, int *lenp);
522 * fdt_get_property - find a given property in a given node
523 * @fdt: pointer to the device tree blob
524 * @nodeoffset: offset of the node whose property to find
525 * @name: name of the property to find
526 * @lenp: pointer to an integer variable (will be overwritten) or NULL
528 * fdt_get_property() retrieves a pointer to the fdt_property
529 * structure within the device tree blob corresponding to the property
530 * named 'name' of the node at offset nodeoffset. If lenp is
531 * non-NULL, the length of the property value is also returned, in the
532 * integer pointed to by lenp.
535 * pointer to the structure representing the property
536 * if lenp is non-NULL, *lenp contains the length of the property
539 * if lenp is non-NULL, *lenp contains an error code (<0):
540 * -FDT_ERR_NOTFOUND, node does not have named property
541 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
544 * -FDT_ERR_BADVERSION,
546 * -FDT_ERR_BADSTRUCTURE,
547 * -FDT_ERR_TRUNCATED, standard meanings
549 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
550 const char *name, int *lenp);
551 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
555 return (struct fdt_property *)(uintptr_t)
556 fdt_get_property(fdt, nodeoffset, name, lenp);
560 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
561 * @fdt: pointer to the device tree blob
562 * @ffset: offset of the property to read
563 * @namep: pointer to a string variable (will be overwritten) or NULL
564 * @lenp: pointer to an integer variable (will be overwritten) or NULL
566 * fdt_getprop_by_offset() retrieves a pointer to the value of the
567 * property at structure block offset 'offset' (this will be a pointer
568 * to within the device blob itself, not a copy of the value). If
569 * lenp is non-NULL, the length of the property value is also
570 * returned, in the integer pointed to by lenp. If namep is non-NULL,
571 * the property's namne will also be returned in the char * pointed to
572 * by namep (this will be a pointer to within the device tree's string
573 * block, not a new copy of the name).
576 * pointer to the property's value
577 * if lenp is non-NULL, *lenp contains the length of the property
579 * if namep is non-NULL *namep contiains a pointer to the property
582 * if lenp is non-NULL, *lenp contains an error code (<0):
583 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
585 * -FDT_ERR_BADVERSION,
587 * -FDT_ERR_BADSTRUCTURE,
588 * -FDT_ERR_TRUNCATED, standard meanings
590 #ifndef SWIG /* This function is not useful in Python */
591 const void *fdt_getprop_by_offset(const void *fdt, int offset,
592 const char **namep, int *lenp);
596 * fdt_getprop_namelen - get property value based on substring
597 * @fdt: pointer to the device tree blob
598 * @nodeoffset: offset of the node whose property to find
599 * @name: name of the property to find
600 * @namelen: number of characters of name to consider
601 * @lenp: pointer to an integer variable (will be overwritten) or NULL
603 * Identical to fdt_getprop(), but only examine the first namelen
604 * characters of name for matching the property name.
606 #ifndef SWIG /* Not available in Python */
607 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
608 const char *name, int namelen, int *lenp);
609 static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
610 const char *name, int namelen,
613 return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
619 * fdt_getprop - retrieve the value of a given property
620 * @fdt: pointer to the device tree blob
621 * @nodeoffset: offset of the node whose property to find
622 * @name: name of the property to find
623 * @lenp: pointer to an integer variable (will be overwritten) or NULL
625 * fdt_getprop() retrieves a pointer to the value of the property
626 * named 'name' of the node at offset nodeoffset (this will be a
627 * pointer to within the device blob itself, not a copy of the value).
628 * If lenp is non-NULL, the length of the property value is also
629 * returned, in the integer pointed to by lenp.
632 * pointer to the property's value
633 * if lenp is non-NULL, *lenp contains the length of the property
636 * if lenp is non-NULL, *lenp contains an error code (<0):
637 * -FDT_ERR_NOTFOUND, node does not have named property
638 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
641 * -FDT_ERR_BADVERSION,
643 * -FDT_ERR_BADSTRUCTURE,
644 * -FDT_ERR_TRUNCATED, standard meanings
646 const void *fdt_getprop(const void *fdt, int nodeoffset,
647 const char *name, int *lenp);
648 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
649 const char *name, int *lenp)
651 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
655 * fdt_get_phandle - retrieve the phandle of a given node
656 * @fdt: pointer to the device tree blob
657 * @nodeoffset: structure block offset of the node
659 * fdt_get_phandle() retrieves the phandle of the device tree node at
660 * structure block offset nodeoffset.
663 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
664 * 0, if the node has no phandle, or another error occurs
666 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
669 * fdt_get_alias_namelen - get alias based on substring
670 * @fdt: pointer to the device tree blob
671 * @name: name of the alias th look up
672 * @namelen: number of characters of name to consider
674 * Identical to fdt_get_alias(), but only examine the first namelen
675 * characters of name for matching the alias name.
677 #ifndef SWIG /* Not available in Python */
678 const char *fdt_get_alias_namelen(const void *fdt,
679 const char *name, int namelen);
683 * fdt_get_alias - retrieve the path referenced by a given alias
684 * @fdt: pointer to the device tree blob
685 * @name: name of the alias th look up
687 * fdt_get_alias() retrieves the value of a given alias. That is, the
688 * value of the property named 'name' in the node /aliases.
691 * a pointer to the expansion of the alias named 'name', if it exists
692 * NULL, if the given alias or the /aliases node does not exist
694 const char *fdt_get_alias(const void *fdt, const char *name);
697 * fdt_get_path - determine the full path of a node
698 * @fdt: pointer to the device tree blob
699 * @nodeoffset: offset of the node whose path to find
700 * @buf: character buffer to contain the returned path (will be overwritten)
701 * @buflen: size of the character buffer at buf
703 * fdt_get_path() computes the full path of the node at offset
704 * nodeoffset, and records that path in the buffer at buf.
706 * NOTE: This function is expensive, as it must scan the device tree
707 * structure from the start to nodeoffset.
711 * buf contains the absolute path of the node at
712 * nodeoffset, as a NUL-terminated string.
713 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
714 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
715 * characters and will not fit in the given buffer.
717 * -FDT_ERR_BADVERSION,
719 * -FDT_ERR_BADSTRUCTURE, standard meanings
721 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
724 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
725 * @fdt: pointer to the device tree blob
726 * @nodeoffset: offset of the node whose parent to find
727 * @supernodedepth: depth of the ancestor to find
728 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
730 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
731 * at a specific depth from the root (where the root itself has depth
732 * 0, its immediate subnodes depth 1 and so forth). So
733 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
734 * will always return 0, the offset of the root node. If the node at
735 * nodeoffset has depth D, then:
736 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
737 * will return nodeoffset itself.
739 * NOTE: This function is expensive, as it must scan the device tree
740 * structure from the start to nodeoffset.
743 * structure block offset of the node at node offset's ancestor
744 * of depth supernodedepth (>=0), on success
745 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
746 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
749 * -FDT_ERR_BADVERSION,
751 * -FDT_ERR_BADSTRUCTURE, standard meanings
753 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
754 int supernodedepth, int *nodedepth);
757 * fdt_node_depth - find the depth of a given node
758 * @fdt: pointer to the device tree blob
759 * @nodeoffset: offset of the node whose parent to find
761 * fdt_node_depth() finds the depth of a given node. The root node
762 * has depth 0, its immediate subnodes depth 1 and so forth.
764 * NOTE: This function is expensive, as it must scan the device tree
765 * structure from the start to nodeoffset.
768 * depth of the node at nodeoffset (>=0), on success
769 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
771 * -FDT_ERR_BADVERSION,
773 * -FDT_ERR_BADSTRUCTURE, standard meanings
775 int fdt_node_depth(const void *fdt, int nodeoffset);
778 * fdt_parent_offset - find the parent of a given node
779 * @fdt: pointer to the device tree blob
780 * @nodeoffset: offset of the node whose parent to find
782 * fdt_parent_offset() locates the parent node of a given node (that
783 * is, it finds the offset of the node which contains the node at
784 * nodeoffset as a subnode).
786 * NOTE: This function is expensive, as it must scan the device tree
787 * structure from the start to nodeoffset, *twice*.
790 * structure block offset of the parent of the node at nodeoffset
792 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
794 * -FDT_ERR_BADVERSION,
796 * -FDT_ERR_BADSTRUCTURE, standard meanings
798 int fdt_parent_offset(const void *fdt, int nodeoffset);
801 * fdt_node_offset_by_prop_value - find nodes with a given property value
802 * @fdt: pointer to the device tree blob
803 * @startoffset: only find nodes after this offset
804 * @propname: property name to check
805 * @propval: property value to search for
806 * @proplen: length of the value in propval
808 * fdt_node_offset_by_prop_value() returns the offset of the first
809 * node after startoffset, which has a property named propname whose
810 * value is of length proplen and has value equal to propval; or if
811 * startoffset is -1, the very first such node in the tree.
813 * To iterate through all nodes matching the criterion, the following
815 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
817 * while (offset != -FDT_ERR_NOTFOUND) {
819 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
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_prop_value(const void *fdt, int startoffset,
839 const char *propname,
840 const void *propval, int proplen);
843 * fdt_node_offset_by_phandle - find the node with a given phandle
844 * @fdt: pointer to the device tree blob
845 * @phandle: phandle value
847 * fdt_node_offset_by_phandle() returns the offset of the node
848 * which has the given phandle value. If there is more than one node
849 * in the tree with the given phandle (an invalid tree), results are
853 * structure block offset of the located node (>= 0), on success
854 * -FDT_ERR_NOTFOUND, no node with that phandle exists
855 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
857 * -FDT_ERR_BADVERSION,
859 * -FDT_ERR_BADSTRUCTURE, standard meanings
861 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
864 * fdt_node_check_compatible: check a node's compatible property
865 * @fdt: pointer to the device tree blob
866 * @nodeoffset: offset of a tree node
867 * @compatible: string to match against
870 * fdt_node_check_compatible() returns 0 if the given node contains a
871 * 'compatible' property with the given string as one of its elements,
872 * it returns non-zero otherwise, or on error.
875 * 0, if the node has a 'compatible' property listing the given string
876 * 1, if the node has a 'compatible' property, but it does not list
878 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
879 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
881 * -FDT_ERR_BADVERSION,
883 * -FDT_ERR_BADSTRUCTURE, standard meanings
885 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
886 const char *compatible);
889 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
890 * @fdt: pointer to the device tree blob
891 * @startoffset: only find nodes after this offset
892 * @compatible: 'compatible' string to match against
894 * fdt_node_offset_by_compatible() returns the offset of the first
895 * node after startoffset, which has a 'compatible' property which
896 * lists the given compatible string; or if startoffset is -1, the
897 * very first such node in the tree.
899 * To iterate through all nodes matching the criterion, the following
901 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
902 * while (offset != -FDT_ERR_NOTFOUND) {
904 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
907 * Note the -1 in the first call to the function, if 0 is used here
908 * instead, the function will never locate the root node, even if it
909 * matches the criterion.
912 * structure block offset of the located node (>= 0, >startoffset),
914 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
915 * tree after startoffset
916 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
918 * -FDT_ERR_BADVERSION,
920 * -FDT_ERR_BADSTRUCTURE, standard meanings
922 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
923 const char *compatible);
926 * fdt_stringlist_contains - check a string list property for a string
927 * @strlist: Property containing a list of strings to check
928 * @listlen: Length of property
929 * @str: String to search for
931 * This is a utility function provided for convenience. The list contains
932 * one or more strings, each terminated by \0, as is found in a device tree
933 * "compatible" property.
935 * @return: 1 if the string is found in the list, 0 not found, or invalid list
937 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
940 * fdt_stringlist_count - count the number of strings in a string list
941 * @fdt: pointer to the device tree blob
942 * @nodeoffset: offset of a tree node
943 * @property: name of the property containing the string list
945 * the number of strings in the given property
946 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
947 * -FDT_ERR_NOTFOUND if the property does not exist
949 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
952 * fdt_stringlist_search - find a string in a string list and return its index
953 * @fdt: pointer to the device tree blob
954 * @nodeoffset: offset of a tree node
955 * @property: name of the property containing the string list
956 * @string: string to look up in the string list
958 * Note that it is possible for this function to succeed on property values
959 * that are not NUL-terminated. That's because the function will stop after
960 * finding the first occurrence of @string. This can for example happen with
961 * small-valued cell properties, such as #address-cells, when searching for
965 * the index of the string in the list of strings
966 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
967 * -FDT_ERR_NOTFOUND if the property does not exist or does not contain
970 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
974 * fdt_stringlist_get() - obtain the string at a given index in a string list
975 * @fdt: pointer to the device tree blob
976 * @nodeoffset: offset of a tree node
977 * @property: name of the property containing the string list
978 * @index: index of the string to return
979 * @lenp: return location for the string length or an error code on failure
981 * Note that this will successfully extract strings from properties with
982 * non-NUL-terminated values. For example on small-valued cell properties
983 * this function will return the empty string.
985 * If non-NULL, the length of the string (on success) or a negative error-code
986 * (on failure) will be stored in the integer pointer to by lenp.
989 * A pointer to the string at the given index in the string list or NULL on
990 * failure. On success the length of the string will be stored in the memory
991 * location pointed to by the lenp parameter, if non-NULL. On failure one of
992 * the following negative error codes will be returned in the lenp parameter
994 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
995 * -FDT_ERR_NOTFOUND if the property does not exist
997 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
998 const char *property, int index,
1001 /**********************************************************************/
1002 /* Read-only functions (addressing related) */
1003 /**********************************************************************/
1006 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
1008 * This is the maximum value for #address-cells, #size-cells and
1009 * similar properties that will be processed by libfdt. IEE1275
1010 * requires that OF implementations handle values up to 4.
1011 * Implementations may support larger values, but in practice higher
1012 * values aren't used.
1014 #define FDT_MAX_NCELLS 4
1017 * fdt_address_cells - retrieve address size for a bus represented in the tree
1018 * @fdt: pointer to the device tree blob
1019 * @nodeoffset: offset of the node to find the address size for
1021 * When the node has a valid #address-cells property, returns its value.
1024 * 0 <= n < FDT_MAX_NCELLS, on success
1025 * 2, if the node has no #address-cells property
1026 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1027 * #address-cells property
1028 * -FDT_ERR_BADMAGIC,
1029 * -FDT_ERR_BADVERSION,
1030 * -FDT_ERR_BADSTATE,
1031 * -FDT_ERR_BADSTRUCTURE,
1032 * -FDT_ERR_TRUNCATED, standard meanings
1034 int fdt_address_cells(const void *fdt, int nodeoffset);
1037 * fdt_size_cells - retrieve address range size for a bus represented in the
1039 * @fdt: pointer to the device tree blob
1040 * @nodeoffset: offset of the node to find the address range size for
1042 * When the node has a valid #size-cells property, returns its value.
1045 * 0 <= n < FDT_MAX_NCELLS, on success
1046 * 2, if the node has no #address-cells property
1047 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1048 * #size-cells property
1049 * -FDT_ERR_BADMAGIC,
1050 * -FDT_ERR_BADVERSION,
1051 * -FDT_ERR_BADSTATE,
1052 * -FDT_ERR_BADSTRUCTURE,
1053 * -FDT_ERR_TRUNCATED, standard meanings
1055 int fdt_size_cells(const void *fdt, int nodeoffset);
1058 /**********************************************************************/
1059 /* Write-in-place functions */
1060 /**********************************************************************/
1063 * fdt_setprop_inplace_namelen_partial - change a property's value,
1065 * @fdt: pointer to the device tree blob
1066 * @nodeoffset: offset of the node whose property to change
1067 * @name: name of the property to change
1068 * @namelen: number of characters of name to consider
1069 * @idx: index of the property to change in the array
1070 * @val: pointer to data to replace the property value with
1071 * @len: length of the property value
1073 * Identical to fdt_setprop_inplace(), but modifies the given property
1074 * starting from the given index, and using only the first characters
1075 * of the name. It is useful when you want to manipulate only one value of
1076 * an array and you have a string that doesn't end with \0.
1078 #ifndef SWIG /* Not available in Python */
1079 int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
1080 const char *name, int namelen,
1081 uint32_t idx, const void *val,
1086 * fdt_setprop_inplace - change a property's value, but not its size
1087 * @fdt: pointer to the device tree blob
1088 * @nodeoffset: offset of the node whose property to change
1089 * @name: name of the property to change
1090 * @val: pointer to data to replace the property value with
1091 * @len: length of the property value
1093 * fdt_setprop_inplace() replaces the value of a given property with
1094 * the data in val, of length len. This function cannot change the
1095 * size of a property, and so will only work if len is equal to the
1096 * current length of the property.
1098 * This function will alter only the bytes in the blob which contain
1099 * the given property value, and will not alter or move any other part
1104 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1105 * -FDT_ERR_NOTFOUND, node does not have the named property
1106 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1107 * -FDT_ERR_BADMAGIC,
1108 * -FDT_ERR_BADVERSION,
1109 * -FDT_ERR_BADSTATE,
1110 * -FDT_ERR_BADSTRUCTURE,
1111 * -FDT_ERR_TRUNCATED, standard meanings
1113 #ifndef SWIG /* Not available in Python */
1114 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1115 const void *val, int len);
1119 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
1120 * @fdt: pointer to the device tree blob
1121 * @nodeoffset: offset of the node whose property to change
1122 * @name: name of the property to change
1123 * @val: 32-bit integer value to replace the property with
1125 * fdt_setprop_inplace_u32() replaces the value of a given property
1126 * with the 32-bit integer value in val, converting val to big-endian
1127 * if necessary. This function cannot change the size of a property,
1128 * and so will only work if the property already exists and has length
1131 * This function will alter only the bytes in the blob which contain
1132 * the given property value, and will not alter or move any other part
1137 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
1138 * -FDT_ERR_NOTFOUND, node does not have the named property
1139 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1140 * -FDT_ERR_BADMAGIC,
1141 * -FDT_ERR_BADVERSION,
1142 * -FDT_ERR_BADSTATE,
1143 * -FDT_ERR_BADSTRUCTURE,
1144 * -FDT_ERR_TRUNCATED, standard meanings
1146 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1147 const char *name, uint32_t val)
1149 fdt32_t tmp = cpu_to_fdt32(val);
1150 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1154 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1155 * @fdt: pointer to the device tree blob
1156 * @nodeoffset: offset of the node whose property to change
1157 * @name: name of the property to change
1158 * @val: 64-bit integer value to replace the property with
1160 * fdt_setprop_inplace_u64() replaces the value of a given property
1161 * with the 64-bit integer value in val, converting val to big-endian
1162 * if necessary. This function cannot change the size of a property,
1163 * and so will only work if the property already exists and has length
1166 * This function will alter only the bytes in the blob which contain
1167 * the given property value, and will not alter or move any other part
1172 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1173 * -FDT_ERR_NOTFOUND, node does not have the named property
1174 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1175 * -FDT_ERR_BADMAGIC,
1176 * -FDT_ERR_BADVERSION,
1177 * -FDT_ERR_BADSTATE,
1178 * -FDT_ERR_BADSTRUCTURE,
1179 * -FDT_ERR_TRUNCATED, standard meanings
1181 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1182 const char *name, uint64_t val)
1184 fdt64_t tmp = cpu_to_fdt64(val);
1185 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1189 * fdt_setprop_inplace_cell - change the value of a single-cell property
1191 * This is an alternative name for fdt_setprop_inplace_u32()
1193 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1194 const char *name, uint32_t val)
1196 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1200 * fdt_nop_property - replace a property with nop tags
1201 * @fdt: pointer to the device tree blob
1202 * @nodeoffset: offset of the node whose property to nop
1203 * @name: name of the property to nop
1205 * fdt_nop_property() will replace a given property's representation
1206 * in the blob with FDT_NOP tags, effectively removing it from the
1209 * This function will alter only the bytes in the blob which contain
1210 * the property, and will not alter or move any other part of the
1215 * -FDT_ERR_NOTFOUND, node does not have the named property
1216 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1217 * -FDT_ERR_BADMAGIC,
1218 * -FDT_ERR_BADVERSION,
1219 * -FDT_ERR_BADSTATE,
1220 * -FDT_ERR_BADSTRUCTURE,
1221 * -FDT_ERR_TRUNCATED, standard meanings
1223 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1226 * fdt_nop_node - replace a node (subtree) with nop tags
1227 * @fdt: pointer to the device tree blob
1228 * @nodeoffset: offset of the node to nop
1230 * fdt_nop_node() will replace a given node's representation in the
1231 * blob, including all its subnodes, if any, with FDT_NOP tags,
1232 * effectively removing it from the tree.
1234 * This function will alter only the bytes in the blob which contain
1235 * the node and its properties and subnodes, and will not alter or
1236 * move any other part of the tree.
1240 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1241 * -FDT_ERR_BADMAGIC,
1242 * -FDT_ERR_BADVERSION,
1243 * -FDT_ERR_BADSTATE,
1244 * -FDT_ERR_BADSTRUCTURE,
1245 * -FDT_ERR_TRUNCATED, standard meanings
1247 int fdt_nop_node(void *fdt, int nodeoffset);
1249 /**********************************************************************/
1250 /* Sequential write functions */
1251 /**********************************************************************/
1253 int fdt_create(void *buf, int bufsize);
1254 int fdt_resize(void *fdt, void *buf, int bufsize);
1255 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1256 int fdt_finish_reservemap(void *fdt);
1257 int fdt_begin_node(void *fdt, const char *name);
1258 int fdt_property(void *fdt, const char *name, const void *val, int len);
1259 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1261 fdt32_t tmp = cpu_to_fdt32(val);
1262 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1264 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1266 fdt64_t tmp = cpu_to_fdt64(val);
1267 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1269 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1271 return fdt_property_u32(fdt, name, val);
1275 * fdt_property_placeholder - add a new property and return a ptr to its value
1277 * @fdt: pointer to the device tree blob
1278 * @name: name of property to add
1279 * @len: length of property value in bytes
1280 * @valp: returns a pointer to where where the value should be placed
1284 * -FDT_ERR_BADMAGIC,
1285 * -FDT_ERR_NOSPACE, standard meanings
1287 int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
1289 #define fdt_property_string(fdt, name, str) \
1290 fdt_property(fdt, name, str, strlen(str)+1)
1291 int fdt_end_node(void *fdt);
1292 int fdt_finish(void *fdt);
1294 /**********************************************************************/
1295 /* Read-write functions */
1296 /**********************************************************************/
1298 int fdt_create_empty_tree(void *buf, int bufsize);
1299 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1300 int fdt_pack(void *fdt);
1303 * fdt_add_mem_rsv - add one memory reserve map entry
1304 * @fdt: pointer to the device tree blob
1305 * @address, @size: 64-bit values (native endian)
1307 * Adds a reserve map entry to the given blob reserving a region at
1308 * address address of length size.
1310 * This function will insert data into the reserve map and will
1311 * therefore change the indexes of some entries in the table.
1315 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1316 * contain the new reservation entry
1317 * -FDT_ERR_BADMAGIC,
1318 * -FDT_ERR_BADVERSION,
1319 * -FDT_ERR_BADSTATE,
1320 * -FDT_ERR_BADSTRUCTURE,
1321 * -FDT_ERR_BADLAYOUT,
1322 * -FDT_ERR_TRUNCATED, standard meanings
1324 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1327 * fdt_del_mem_rsv - remove a memory reserve map entry
1328 * @fdt: pointer to the device tree blob
1329 * @n: entry to remove
1331 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1334 * This function will delete data from the reservation table and will
1335 * therefore change the indexes of some entries in the table.
1339 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1340 * are less than n+1 reserve map entries)
1341 * -FDT_ERR_BADMAGIC,
1342 * -FDT_ERR_BADVERSION,
1343 * -FDT_ERR_BADSTATE,
1344 * -FDT_ERR_BADSTRUCTURE,
1345 * -FDT_ERR_BADLAYOUT,
1346 * -FDT_ERR_TRUNCATED, standard meanings
1348 int fdt_del_mem_rsv(void *fdt, int n);
1351 * fdt_set_name - change the name of a given node
1352 * @fdt: pointer to the device tree blob
1353 * @nodeoffset: structure block offset of a node
1354 * @name: name to give the node
1356 * fdt_set_name() replaces the name (including unit address, if any)
1357 * of the given node with the given string. NOTE: this function can't
1358 * efficiently check if the new name is unique amongst the given
1359 * node's siblings; results are undefined if this function is invoked
1360 * with a name equal to one of the given node's siblings.
1362 * This function may insert or delete data from the blob, and will
1363 * therefore change the offsets of some existing nodes.
1367 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1368 * to contain the new name
1369 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1370 * -FDT_ERR_BADMAGIC,
1371 * -FDT_ERR_BADVERSION,
1372 * -FDT_ERR_BADSTATE, standard meanings
1374 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1377 * fdt_setprop - create or change a property
1378 * @fdt: pointer to the device tree blob
1379 * @nodeoffset: offset of the node whose property to change
1380 * @name: name of the property to change
1381 * @val: pointer to data to set the property value to
1382 * @len: length of the property value
1384 * fdt_setprop() sets the value of the named property in the given
1385 * node to the given value and length, creating the property if it
1386 * does not already exist.
1388 * This function may insert or delete data from the blob, and will
1389 * therefore change the offsets of some existing nodes.
1393 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1394 * contain the new property value
1395 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1396 * -FDT_ERR_BADLAYOUT,
1397 * -FDT_ERR_BADMAGIC,
1398 * -FDT_ERR_BADVERSION,
1399 * -FDT_ERR_BADSTATE,
1400 * -FDT_ERR_BADSTRUCTURE,
1401 * -FDT_ERR_BADLAYOUT,
1402 * -FDT_ERR_TRUNCATED, standard meanings
1404 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1405 const void *val, int len);
1408 * fdt_setprop _placeholder - allocate space for a property
1409 * @fdt: pointer to the device tree blob
1410 * @nodeoffset: offset of the node whose property to change
1411 * @name: name of the property to change
1412 * @len: length of the property value
1413 * @prop_data: return pointer to property data
1415 * fdt_setprop_placeholer() allocates the named property in the given node.
1416 * If the property exists it is resized. In either case a pointer to the
1417 * property data is returned.
1419 * This function may insert or delete data from the blob, and will
1420 * therefore change the offsets of some existing nodes.
1424 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1425 * contain the new property value
1426 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1427 * -FDT_ERR_BADLAYOUT,
1428 * -FDT_ERR_BADMAGIC,
1429 * -FDT_ERR_BADVERSION,
1430 * -FDT_ERR_BADSTATE,
1431 * -FDT_ERR_BADSTRUCTURE,
1432 * -FDT_ERR_BADLAYOUT,
1433 * -FDT_ERR_TRUNCATED, standard meanings
1435 int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
1436 int len, void **prop_data);
1439 * fdt_setprop_u32 - set a property to a 32-bit integer
1440 * @fdt: pointer to the device tree blob
1441 * @nodeoffset: offset of the node whose property to change
1442 * @name: name of the property to change
1443 * @val: 32-bit integer value for the property (native endian)
1445 * fdt_setprop_u32() sets the value of the named property in the given
1446 * node to the given 32-bit integer value (converting to big-endian if
1447 * necessary), or creates a new property with that value if it does
1448 * not already exist.
1450 * This function may insert or delete data from the blob, and will
1451 * therefore change the offsets of some existing nodes.
1455 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1456 * contain the new property value
1457 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1458 * -FDT_ERR_BADLAYOUT,
1459 * -FDT_ERR_BADMAGIC,
1460 * -FDT_ERR_BADVERSION,
1461 * -FDT_ERR_BADSTATE,
1462 * -FDT_ERR_BADSTRUCTURE,
1463 * -FDT_ERR_BADLAYOUT,
1464 * -FDT_ERR_TRUNCATED, standard meanings
1466 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1469 fdt32_t tmp = cpu_to_fdt32(val);
1470 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1474 * fdt_setprop_u64 - set a property to a 64-bit integer
1475 * @fdt: pointer to the device tree blob
1476 * @nodeoffset: offset of the node whose property to change
1477 * @name: name of the property to change
1478 * @val: 64-bit integer value for the property (native endian)
1480 * fdt_setprop_u64() sets the value of the named property in the given
1481 * node to the given 64-bit integer value (converting to big-endian if
1482 * necessary), or creates a new property with that value if it does
1483 * not already exist.
1485 * This function may insert or delete data from the blob, and will
1486 * therefore change the offsets of some existing nodes.
1490 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1491 * contain the new property value
1492 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1493 * -FDT_ERR_BADLAYOUT,
1494 * -FDT_ERR_BADMAGIC,
1495 * -FDT_ERR_BADVERSION,
1496 * -FDT_ERR_BADSTATE,
1497 * -FDT_ERR_BADSTRUCTURE,
1498 * -FDT_ERR_BADLAYOUT,
1499 * -FDT_ERR_TRUNCATED, standard meanings
1501 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1504 fdt64_t tmp = cpu_to_fdt64(val);
1505 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1509 * fdt_setprop_cell - set a property to a single cell value
1511 * This is an alternative name for fdt_setprop_u32()
1513 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1516 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1520 * fdt_setprop_string - set a property to a string value
1521 * @fdt: pointer to the device tree blob
1522 * @nodeoffset: offset of the node whose property to change
1523 * @name: name of the property to change
1524 * @str: string value for the property
1526 * fdt_setprop_string() sets the value of the named property in the
1527 * given node to the given string value (using the length of the
1528 * string to determine the new length of the property), or creates a
1529 * new property with that value if it does not already exist.
1531 * This function may insert or delete data from the blob, and will
1532 * therefore 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_setprop_string(fdt, nodeoffset, name, str) \
1548 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1552 * fdt_setprop_empty - set a property to an empty value
1553 * @fdt: pointer to the device tree blob
1554 * @nodeoffset: offset of the node whose property to change
1555 * @name: name of the property to change
1557 * fdt_setprop_empty() sets the value of the named property in the
1558 * given node to an empty (zero length) value, or creates a new empty
1559 * property if it does not already exist.
1561 * This function may insert or delete data from the blob, and will
1562 * therefore change the offsets of some existing nodes.
1566 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1567 * contain the new property value
1568 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1569 * -FDT_ERR_BADLAYOUT,
1570 * -FDT_ERR_BADMAGIC,
1571 * -FDT_ERR_BADVERSION,
1572 * -FDT_ERR_BADSTATE,
1573 * -FDT_ERR_BADSTRUCTURE,
1574 * -FDT_ERR_BADLAYOUT,
1575 * -FDT_ERR_TRUNCATED, standard meanings
1577 #define fdt_setprop_empty(fdt, nodeoffset, name) \
1578 fdt_setprop((fdt), (nodeoffset), (name), NULL, 0)
1581 * fdt_appendprop - append to or create a property
1582 * @fdt: pointer to the device tree blob
1583 * @nodeoffset: offset of the node whose property to change
1584 * @name: name of the property to append to
1585 * @val: pointer to data to append to the property value
1586 * @len: length of the data to append to the property value
1588 * fdt_appendprop() appends the value to the named property in the
1589 * given node, creating the property if it does not already exist.
1591 * This function may insert data into the blob, and will therefore
1592 * change the offsets of some existing nodes.
1596 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1597 * contain the new property value
1598 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1599 * -FDT_ERR_BADLAYOUT,
1600 * -FDT_ERR_BADMAGIC,
1601 * -FDT_ERR_BADVERSION,
1602 * -FDT_ERR_BADSTATE,
1603 * -FDT_ERR_BADSTRUCTURE,
1604 * -FDT_ERR_BADLAYOUT,
1605 * -FDT_ERR_TRUNCATED, standard meanings
1607 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1608 const void *val, int len);
1611 * fdt_appendprop_u32 - append a 32-bit integer value to a property
1612 * @fdt: pointer to the device tree blob
1613 * @nodeoffset: offset of the node whose property to change
1614 * @name: name of the property to change
1615 * @val: 32-bit integer value to append to the property (native endian)
1617 * fdt_appendprop_u32() appends the given 32-bit integer value
1618 * (converting to big-endian if necessary) to the value of the named
1619 * property in the given node, or creates a new property with that
1620 * value if it does not already exist.
1622 * This function may insert data into the blob, and will therefore
1623 * change the offsets of some existing nodes.
1627 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1628 * contain the new property value
1629 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1630 * -FDT_ERR_BADLAYOUT,
1631 * -FDT_ERR_BADMAGIC,
1632 * -FDT_ERR_BADVERSION,
1633 * -FDT_ERR_BADSTATE,
1634 * -FDT_ERR_BADSTRUCTURE,
1635 * -FDT_ERR_BADLAYOUT,
1636 * -FDT_ERR_TRUNCATED, standard meanings
1638 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1639 const char *name, uint32_t val)
1641 fdt32_t tmp = cpu_to_fdt32(val);
1642 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1646 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1647 * @fdt: pointer to the device tree blob
1648 * @nodeoffset: offset of the node whose property to change
1649 * @name: name of the property to change
1650 * @val: 64-bit integer value to append to the property (native endian)
1652 * fdt_appendprop_u64() appends the given 64-bit integer value
1653 * (converting to big-endian if necessary) to the value of the named
1654 * property in the given node, or creates a new property with that
1655 * value if it does not already exist.
1657 * This function may insert data into the blob, and will therefore
1658 * change the offsets of some existing nodes.
1662 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1663 * contain the new property value
1664 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1665 * -FDT_ERR_BADLAYOUT,
1666 * -FDT_ERR_BADMAGIC,
1667 * -FDT_ERR_BADVERSION,
1668 * -FDT_ERR_BADSTATE,
1669 * -FDT_ERR_BADSTRUCTURE,
1670 * -FDT_ERR_BADLAYOUT,
1671 * -FDT_ERR_TRUNCATED, standard meanings
1673 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1674 const char *name, uint64_t val)
1676 fdt64_t tmp = cpu_to_fdt64(val);
1677 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1681 * fdt_appendprop_cell - append a single cell value to a property
1683 * This is an alternative name for fdt_appendprop_u32()
1685 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1686 const char *name, uint32_t val)
1688 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1692 * fdt_appendprop_string - append a string to a property
1693 * @fdt: pointer to the device tree blob
1694 * @nodeoffset: offset of the node whose property to change
1695 * @name: name of the property to change
1696 * @str: string value to append to the property
1698 * fdt_appendprop_string() appends the given string to the value of
1699 * the named property in the given node, or creates a new property
1700 * with that value if it does not already exist.
1702 * This function may insert data into the blob, and will therefore
1703 * change the offsets of some existing nodes.
1707 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1708 * contain the new property value
1709 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1710 * -FDT_ERR_BADLAYOUT,
1711 * -FDT_ERR_BADMAGIC,
1712 * -FDT_ERR_BADVERSION,
1713 * -FDT_ERR_BADSTATE,
1714 * -FDT_ERR_BADSTRUCTURE,
1715 * -FDT_ERR_BADLAYOUT,
1716 * -FDT_ERR_TRUNCATED, standard meanings
1718 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1719 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1722 * fdt_delprop - delete a property
1723 * @fdt: pointer to the device tree blob
1724 * @nodeoffset: offset of the node whose property to nop
1725 * @name: name of the property to nop
1727 * fdt_del_property() will delete the given property.
1729 * This function will delete data from the blob, and will therefore
1730 * change the offsets of some existing nodes.
1734 * -FDT_ERR_NOTFOUND, node does not have the named property
1735 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1736 * -FDT_ERR_BADLAYOUT,
1737 * -FDT_ERR_BADMAGIC,
1738 * -FDT_ERR_BADVERSION,
1739 * -FDT_ERR_BADSTATE,
1740 * -FDT_ERR_BADSTRUCTURE,
1741 * -FDT_ERR_TRUNCATED, standard meanings
1743 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1746 * fdt_add_subnode_namelen - creates a new node based on substring
1747 * @fdt: pointer to the device tree blob
1748 * @parentoffset: structure block offset of a node
1749 * @name: name of the subnode to locate
1750 * @namelen: number of characters of name to consider
1752 * Identical to fdt_add_subnode(), but use only the first namelen
1753 * characters of name as the name of the new node. This is useful for
1754 * creating subnodes based on a portion of a larger string, such as a
1757 #ifndef SWIG /* Not available in Python */
1758 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1759 const char *name, int namelen);
1763 * fdt_add_subnode - creates a new node
1764 * @fdt: pointer to the device tree blob
1765 * @parentoffset: structure block offset of a node
1766 * @name: name of the subnode to locate
1768 * fdt_add_subnode() creates a new node as a subnode of the node at
1769 * structure block offset parentoffset, with the given name (which
1770 * should include the unit address, if any).
1772 * This function will insert data into the blob, and will therefore
1773 * change the offsets of some existing nodes.
1776 * structure block offset of the created nodeequested subnode (>=0), on
1778 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1779 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
1781 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1783 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1784 * blob to contain the new node
1786 * -FDT_ERR_BADLAYOUT
1787 * -FDT_ERR_BADMAGIC,
1788 * -FDT_ERR_BADVERSION,
1789 * -FDT_ERR_BADSTATE,
1790 * -FDT_ERR_BADSTRUCTURE,
1791 * -FDT_ERR_TRUNCATED, standard meanings.
1793 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1796 * fdt_del_node - delete a node (subtree)
1797 * @fdt: pointer to the device tree blob
1798 * @nodeoffset: offset of the node to nop
1800 * fdt_del_node() will remove the given node, including all its
1801 * subnodes if any, from the blob.
1803 * This function will delete data from the blob, and will therefore
1804 * change the offsets of some existing nodes.
1808 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1809 * -FDT_ERR_BADLAYOUT,
1810 * -FDT_ERR_BADMAGIC,
1811 * -FDT_ERR_BADVERSION,
1812 * -FDT_ERR_BADSTATE,
1813 * -FDT_ERR_BADSTRUCTURE,
1814 * -FDT_ERR_TRUNCATED, standard meanings
1816 int fdt_del_node(void *fdt, int nodeoffset);
1819 * fdt_overlay_apply - Applies a DT overlay on a base DT
1820 * @fdt: pointer to the base device tree blob
1821 * @fdto: pointer to the device tree overlay blob
1823 * fdt_overlay_apply() will apply the given device tree overlay on the
1824 * given base device tree.
1826 * Expect the base device tree to be modified, even if the function
1831 * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
1832 * -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or
1833 * properties in the base DT
1834 * -FDT_ERR_BADPHANDLE,
1835 * -FDT_ERR_BADOVERLAY,
1836 * -FDT_ERR_NOPHANDLES,
1837 * -FDT_ERR_INTERNAL,
1838 * -FDT_ERR_BADLAYOUT,
1839 * -FDT_ERR_BADMAGIC,
1840 * -FDT_ERR_BADOFFSET,
1842 * -FDT_ERR_BADVERSION,
1843 * -FDT_ERR_BADSTRUCTURE,
1844 * -FDT_ERR_BADSTATE,
1845 * -FDT_ERR_TRUNCATED, standard meanings
1847 int fdt_overlay_apply(void *fdt, void *fdto);
1849 /**********************************************************************/
1850 /* Debugging / informational functions */
1851 /**********************************************************************/
1853 #ifndef SWIG /* Not available in Python */
1854 const char *fdt_strerror(int errval);
1857 * fdt_remove_unused_strings() - Remove any unused strings from an FDT
1859 * This creates a new device tree in @new with unused strings removed. The
1860 * called can then use fdt_pack() to minimise the space consumed.
1862 * @old: Old device tree blog
1863 * @new: Place to put new device tree blob, which must be as large as
1867 * -FDT_ERR_BADOFFSET, corrupt device tree
1868 * -FDT_ERR_NOSPACE, out of space, which should not happen unless there
1869 * is something very wrong with the device tree input
1871 int fdt_remove_unused_strings(const void *old, void *new);
1879 * Flags for fdt_find_regions()
1881 * Add a region for the string table (always the last region)
1883 #define FDT_REG_ADD_STRING_TAB (1 << 0)
1886 * Add all supernodes of a matching node/property, useful for creating a
1889 #define FDT_REG_SUPERNODES (1 << 1)
1891 /* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
1892 #define FDT_REG_DIRECT_SUBNODES (1 << 2)
1894 /* Add all subnodes of a matching node */
1895 #define FDT_REG_ALL_SUBNODES (1 << 3)
1897 /* Add a region for the mem_rsvmap table (always the first region) */
1898 #define FDT_REG_ADD_MEM_RSVMAP (1 << 4)
1900 /* Indicates what an fdt part is (node, property, value) */
1901 #define FDT_IS_NODE (1 << 0)
1902 #define FDT_IS_PROP (1 << 1)
1903 #define FDT_IS_VALUE (1 << 2) /* not supported */
1904 #define FDT_IS_COMPAT (1 << 3) /* used internally */
1905 #define FDT_NODE_HAS_PROP (1 << 4) /* node contains prop */
1907 #define FDT_ANY_GLOBAL (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
1909 #define FDT_IS_ANY 0x1f /* all the above */
1911 /* We set a reasonable limit on the number of nested nodes */
1912 #define FDT_MAX_DEPTH 32
1914 /* Decribes what we want to include from the current tag */
1917 WANT_NODES_ONLY, /* No properties */
1918 WANT_NODES_AND_PROPS, /* Everything for one level */
1919 WANT_ALL_NODES_AND_PROPS /* Everything for all levels */
1922 /* Keeps track of the state at parent nodes */
1923 struct fdt_subnode_stack {
1924 int offset; /* Offset of node */
1925 enum want_t want; /* The 'want' value here */
1926 int included; /* 1 if we included this node, 0 if not */
1929 struct fdt_region_ptrs {
1930 int depth; /* Current tree depth */
1931 int done; /* What we have completed scanning */
1932 enum want_t want; /* What we are currently including */
1933 char *end; /* Pointer to end of full node path */
1934 int nextoffset; /* Next node offset to check */
1937 /* The state of our finding algortihm */
1938 struct fdt_region_state {
1939 struct fdt_subnode_stack stack[FDT_MAX_DEPTH]; /* node stack */
1940 struct fdt_region *region; /* Contains list of regions found */
1941 int count; /* Numnber of regions found */
1942 const void *fdt; /* FDT blob */
1943 int max_regions; /* Maximum regions to find */
1944 int can_merge; /* 1 if we can merge with previous region */
1945 int start; /* Start position of current region */
1946 struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */
1950 * fdt_find_regions() - find regions in device tree
1952 * Given a list of nodes to include and properties to exclude, find
1953 * the regions of the device tree which describe those included parts.
1955 * The intent is to get a list of regions which will be invariant provided
1956 * those parts are invariant. For example, if you request a list of regions
1957 * for all nodes but exclude the property "data", then you will get the
1958 * same region contents regardless of any change to "data" properties.
1960 * This function can be used to produce a byte-stream to send to a hashing
1961 * function to verify that critical parts of the FDT have not changed.
1963 * Nodes which are given in 'inc' are included in the region list, as
1964 * are the names of the immediate subnodes nodes (but not the properties
1965 * or subnodes of those subnodes).
1967 * For eaxample "/" means to include the root node, all root properties
1968 * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1969 * ensures that we capture the names of the subnodes. In a hashing situation
1970 * it prevents the root node from changing at all Any change to non-excluded
1971 * properties, names of subnodes or number of subnodes would be detected.
1973 * When used with FITs this provides the ability to hash and sign parts of
1974 * the FIT based on different configurations in the FIT. Then it is
1975 * impossible to change anything about that configuration (include images
1976 * attached to the configuration), but it may be possible to add new
1977 * configurations, new images or new signatures within the existing
1980 * Adding new properties to a device tree may result in the string table
1981 * being extended (if the new property names are different from those
1982 * already added). This function can optionally include a region for
1983 * the string table so that this can be part of the hash too.
1985 * The device tree header is not included in the list.
1987 * @fdt: Device tree to check
1988 * @inc: List of node paths to included
1989 * @inc_count: Number of node paths in list
1990 * @exc_prop: List of properties names to exclude
1991 * @exc_prop_count: Number of properties in exclude list
1992 * @region: Returns list of regions
1993 * @max_region: Maximum length of region list
1994 * @path: Pointer to a temporary string for the function to use for
1995 * building path names
1996 * @path_len: Length of path, must be large enough to hold the longest
1998 * @add_string_tab: 1 to add a region for the string table
1999 * @return number of regions in list. If this is >max_regions then the
2000 * region array was exhausted. You should increase max_regions and try
2003 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
2004 char * const exc_prop[], int exc_prop_count,
2005 struct fdt_region region[], int max_regions,
2006 char *path, int path_len, int add_string_tab);
2009 * fdt_first_region() - find regions in device tree
2011 * Given a nodes and properties to include and properties to exclude, find
2012 * the regions of the device tree which describe those included parts.
2014 * The use for this function is twofold. Firstly it provides a convenient
2015 * way of performing a structure-aware grep of the tree. For example it is
2016 * possible to grep for a node and get all the properties associated with
2017 * that node. Trees can be subsetted easily, by specifying the nodes that
2018 * are required, and then writing out the regions returned by this function.
2019 * This is useful for small resource-constrained systems, such as boot
2020 * loaders, which want to use an FDT but do not need to know about all of
2023 * Secondly it makes it easy to hash parts of the tree and detect changes.
2024 * The intent is to get a list of regions which will be invariant provided
2025 * those parts are invariant. For example, if you request a list of regions
2026 * for all nodes but exclude the property "data", then you will get the
2027 * same region contents regardless of any change to "data" properties.
2029 * This function can be used to produce a byte-stream to send to a hashing
2030 * function to verify that critical parts of the FDT have not changed.
2031 * Note that semantically null changes in order could still cause false
2032 * hash misses. Such reordering might happen if the tree is regenerated
2033 * from source, and nodes are reordered (the bytes-stream will be emitted
2034 * in a different order and mnay hash functions will detect this). However
2035 * if an existing tree is modified using libfdt functions, such as
2036 * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
2038 * The nodes/properties to include/exclude are defined by a function
2039 * provided by the caller. This function is called for each node and
2040 * property, and must return:
2042 * 0 - to exclude this part
2043 * 1 - to include this part
2044 * -1 - for FDT_IS_PROP only: no information is available, so include
2045 * if its containing node is included
2047 * The last case is only used to deal with properties. Often a property is
2048 * included if its containing node is included - this is the case where
2049 * -1 is returned.. However if the property is specifically required to be
2050 * included/excluded, then 0 or 1 can be returned. Note that including a
2051 * property when the FDT_REG_SUPERNODES flag is given will force its
2052 * containing node to be included since it is not valid to have a property
2053 * that is not in a node.
2055 * Using the information provided, the inclusion of a node can be controlled
2056 * either by a node name or its compatible string, or any other property
2057 * that the function can determine.
2059 * As an example, including node "/" means to include the root node and all
2060 * root properties. A flag provides a way of also including supernodes (of
2061 * which there is none for the root node), and another flag includes
2062 * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
2063 * FDT_END_NODE of all subnodes of /.
2065 * The subnode feature helps in a hashing situation since it prevents the
2066 * root node from changing at all. Any change to non-excluded properties,
2067 * names of subnodes or number of subnodes would be detected.
2069 * When used with FITs this provides the ability to hash and sign parts of
2070 * the FIT based on different configurations in the FIT. Then it is
2071 * impossible to change anything about that configuration (include images
2072 * attached to the configuration), but it may be possible to add new
2073 * configurations, new images or new signatures within the existing
2076 * Adding new properties to a device tree may result in the string table
2077 * being extended (if the new property names are different from those
2078 * already added). This function can optionally include a region for
2079 * the string table so that this can be part of the hash too. This is always
2082 * The FDT also has a mem_rsvmap table which can also be included, and is
2083 * always the first region if so.
2085 * The device tree header is not included in the region list. Since the
2086 * contents of the FDT are changing (shrinking, often), the caller will need
2087 * to regenerate the header anyway.
2089 * @fdt: Device tree to check
2090 * @h_include: Function to call to determine whether to include a part or
2093 * @priv: Private pointer as passed to fdt_find_regions()
2094 * @fdt: Pointer to FDT blob
2095 * @offset: Offset of this node / property
2096 * @type: Type of this part, FDT_IS_...
2097 * @data: Pointer to data (node name, property name, compatible
2098 * string, value (not yet supported)
2099 * @size: Size of data, or 0 if none
2100 * @return 0 to exclude, 1 to include, -1 if no information is
2102 * @priv: Private pointer passed to h_include
2103 * @region: Returns list of regions, sorted by offset
2104 * @max_regions: Maximum length of region list
2105 * @path: Pointer to a temporary string for the function to use for
2106 * building path names
2107 * @path_len: Length of path, must be large enough to hold the longest
2109 * @flags: Various flags that control the region algortihm, see
2111 * @return number of regions in list. If this is >max_regions then the
2112 * region array was exhausted. You should increase max_regions and try
2113 * the call again. Only the first max_regions elements are available in the
2116 * On error a -ve value is return, which can be:
2118 * -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
2119 * -FDT_ERR_BADLAYOUT
2120 * -FDT_ERR_NOSPACE (path area is too small)
2122 int fdt_first_region(const void *fdt,
2123 int (*h_include)(void *priv, const void *fdt, int offset,
2124 int type, const char *data, int size),
2125 void *priv, struct fdt_region *region,
2126 char *path, int path_len, int flags,
2127 struct fdt_region_state *info);
2129 /** fdt_next_region() - find next region
2131 * See fdt_first_region() for full description. This function finds the
2132 * next region according to the provided parameters, which must be the same
2133 * as passed to fdt_first_region().
2135 * This function can additionally return -FDT_ERR_NOTFOUND when there are no
2138 int fdt_next_region(const void *fdt,
2139 int (*h_include)(void *priv, const void *fdt, int offset,
2140 int type, const char *data, int size),
2141 void *priv, struct fdt_region *region,
2142 char *path, int path_len, int flags,
2143 struct fdt_region_state *info);
2146 * fdt_add_alias_regions() - find aliases that point to existing regions
2148 * Once a device tree grep is complete some of the nodes will be present
2149 * and some will have been dropped. This function checks all the alias nodes
2150 * to figure out which points point to nodes which are still present. These
2151 * aliases need to be kept, along with the nodes they reference.
2153 * Given a list of regions function finds the aliases that still apply and
2154 * adds more regions to the list for these. This function is called after
2155 * fdt_next_region() has finished returning regions and requires the same
2158 * @fdt: Device tree file to reference
2159 * @region: List of regions that will be kept
2160 * @count: Number of regions
2161 * @max_regions: Number of entries that can fit in @region
2162 * @info: Region state as returned from fdt_next_region()
2163 * @return new number of regions in @region (i.e. count + the number added)
2164 * or -FDT_ERR_NOSPACE if there was not enough space.
2166 int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
2167 int max_regions, struct fdt_region_state *info);
2170 #endif /* _LIBFDT_H */