libfdt: Sync fdt_for_each_subnode() with upstream
[platform/kernel/u-boot.git] / include / libfdt.h
1 #ifndef _LIBFDT_H
2 #define _LIBFDT_H
3 /*
4  * libfdt - Flat Device Tree manipulation
5  * Copyright (C) 2006 David Gibson, IBM Corporation.
6  *
7  * libfdt is dual licensed: you can use it either under the terms of
8  * the GPL, or the BSD license, at your option.
9  *
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.
14  *
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.
19  *
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,
23  *     MA 02110-1301 USA
24  *
25  * Alternatively,
26  *
27  *  b) Redistribution and use in source and binary forms, with or
28  *     without modification, are permitted provided that the following
29  *     conditions are met:
30  *
31  *     1. Redistributions of source code must retain the above
32  *        copyright notice, this list of conditions and the following
33  *        disclaimer.
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.
38  *
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.
52  */
53
54 #include <libfdt_env.h>
55 #include <fdt.h>
56
57 #define FDT_FIRST_SUPPORTED_VERSION     0x10
58 #define FDT_LAST_SUPPORTED_VERSION      0x11
59
60 /* Error codes: informative error codes */
61 #define FDT_ERR_NOTFOUND        1
62         /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
63 #define FDT_ERR_EXISTS          2
64         /* FDT_ERR_EXISTS: Attempted to create a node or property which
65          * already exists */
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. */
71
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
80          * absolute path) */
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. */
88
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. */
112
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
117          * libfdt itself. */
118
119 /* Errors in device tree content */
120 #define FDT_ERR_BADNCELLS       14
121         /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
122          * or similar property with a bad format or value */
123
124 #define FDT_ERR_BADVALUE        15
125         /* FDT_ERR_BADVALUE: Device tree has a property with an unexpected
126          * value. For example: a property expected to contain a string list
127          * is not NUL-terminated within the length of its value. */
128
129 #define FDT_ERR_TOODEEP         16
130         /* FDT_ERR_TOODEEP: The depth of a node has exceeded the internal
131          * libfdt limit. This can happen if you have more than
132          * FDT_MAX_DEPTH nested nodes. */
133
134 #define FDT_ERR_MAX             16
135
136 /**********************************************************************/
137 /* Low-level functions (you probably don't need these)                */
138 /**********************************************************************/
139
140 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
141 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
142 {
143         return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
144 }
145
146 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
147
148 /**********************************************************************/
149 /* Traversal functions                                                */
150 /**********************************************************************/
151
152 int fdt_next_node(const void *fdt, int offset, int *depth);
153
154 /**
155  * fdt_first_subnode() - get offset of first direct subnode
156  *
157  * @fdt:        FDT blob
158  * @offset:     Offset of node to check
159  * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
160  */
161 int fdt_first_subnode(const void *fdt, int offset);
162
163 /**
164  * fdt_next_subnode() - get offset of next direct subnode
165  *
166  * After first calling fdt_first_subnode(), call this function repeatedly to
167  * get direct subnodes of a parent node.
168  *
169  * @fdt:        FDT blob
170  * @offset:     Offset of previous subnode
171  * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
172  * subnodes
173  */
174 int fdt_next_subnode(const void *fdt, int offset);
175
176 /**
177  * fdt_for_each_subnode - iterate over all subnodes of a parent
178  *
179  * @node:       child node (int, lvalue)
180  * @fdt:        FDT blob (const void *)
181  * @parent:     parent node (int)
182  *
183  * This is actually a wrapper around a for loop and would be used like so:
184  *
185  *      fdt_for_each_subnode(node, fdt, parent) {
186  *              Use node
187  *              ...
188  *      }
189  *
190  *      if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) {
191  *              Error handling
192  *      }
193  *
194  * Note that this is implemented as a macro and @node is used as
195  * iterator in the loop. The parent variable be constant or even a
196  * literal.
197  *
198  */
199 #define fdt_for_each_subnode(node, fdt, parent)         \
200         for (node = fdt_first_subnode(fdt, parent);     \
201              node >= 0;                                 \
202              node = fdt_next_subnode(fdt, node))
203
204 /**********************************************************************/
205 /* General functions                                                  */
206 /**********************************************************************/
207
208 #define fdt_get_header(fdt, field) \
209         (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
210 #define fdt_magic(fdt)                  (fdt_get_header(fdt, magic))
211 #define fdt_totalsize(fdt)              (fdt_get_header(fdt, totalsize))
212 #define fdt_off_dt_struct(fdt)          (fdt_get_header(fdt, off_dt_struct))
213 #define fdt_off_dt_strings(fdt)         (fdt_get_header(fdt, off_dt_strings))
214 #define fdt_off_mem_rsvmap(fdt)         (fdt_get_header(fdt, off_mem_rsvmap))
215 #define fdt_version(fdt)                (fdt_get_header(fdt, version))
216 #define fdt_last_comp_version(fdt)      (fdt_get_header(fdt, last_comp_version))
217 #define fdt_boot_cpuid_phys(fdt)        (fdt_get_header(fdt, boot_cpuid_phys))
218 #define fdt_size_dt_strings(fdt)        (fdt_get_header(fdt, size_dt_strings))
219 #define fdt_size_dt_struct(fdt)         (fdt_get_header(fdt, size_dt_struct))
220
221 #define __fdt_set_hdr(name) \
222         static inline void fdt_set_##name(void *fdt, uint32_t val) \
223         { \
224                 struct fdt_header *fdth = (struct fdt_header *)fdt; \
225                 fdth->name = cpu_to_fdt32(val); \
226         }
227 __fdt_set_hdr(magic);
228 __fdt_set_hdr(totalsize);
229 __fdt_set_hdr(off_dt_struct);
230 __fdt_set_hdr(off_dt_strings);
231 __fdt_set_hdr(off_mem_rsvmap);
232 __fdt_set_hdr(version);
233 __fdt_set_hdr(last_comp_version);
234 __fdt_set_hdr(boot_cpuid_phys);
235 __fdt_set_hdr(size_dt_strings);
236 __fdt_set_hdr(size_dt_struct);
237 #undef __fdt_set_hdr
238
239 /**
240  * fdt_check_header - sanity check a device tree or possible device tree
241  * @fdt: pointer to data which might be a flattened device tree
242  *
243  * fdt_check_header() checks that the given buffer contains what
244  * appears to be a flattened device tree with sane information in its
245  * header.
246  *
247  * returns:
248  *     0, if the buffer appears to contain a valid device tree
249  *     -FDT_ERR_BADMAGIC,
250  *     -FDT_ERR_BADVERSION,
251  *     -FDT_ERR_BADSTATE, standard meanings, as above
252  */
253 int fdt_check_header(const void *fdt);
254
255 /**
256  * fdt_move - move a device tree around in memory
257  * @fdt: pointer to the device tree to move
258  * @buf: pointer to memory where the device is to be moved
259  * @bufsize: size of the memory space at buf
260  *
261  * fdt_move() relocates, if possible, the device tree blob located at
262  * fdt to the buffer at buf of size bufsize.  The buffer may overlap
263  * with the existing device tree blob at fdt.  Therefore,
264  *     fdt_move(fdt, fdt, fdt_totalsize(fdt))
265  * should always succeed.
266  *
267  * returns:
268  *     0, on success
269  *     -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
270  *     -FDT_ERR_BADMAGIC,
271  *     -FDT_ERR_BADVERSION,
272  *     -FDT_ERR_BADSTATE, standard meanings
273  */
274 int fdt_move(const void *fdt, void *buf, int bufsize);
275
276 /**********************************************************************/
277 /* Read-only functions                                                */
278 /**********************************************************************/
279
280 /**
281  * fdt_string - retrieve a string from the strings block of a device tree
282  * @fdt: pointer to the device tree blob
283  * @stroffset: offset of the string within the strings block (native endian)
284  *
285  * fdt_string() retrieves a pointer to a single string from the
286  * strings block of the device tree blob at fdt.
287  *
288  * returns:
289  *     a pointer to the string, on success
290  *     NULL, if stroffset is out of bounds
291  */
292 const char *fdt_string(const void *fdt, int stroffset);
293
294 /**
295  * fdt_get_max_phandle - retrieves the highest phandle in a tree
296  * @fdt: pointer to the device tree blob
297  *
298  * fdt_get_max_phandle retrieves the highest phandle in the given
299  * device tree. This will ignore badly formatted phandles, or phandles
300  * with a value of 0 or -1.
301  *
302  * returns:
303  *      the highest phandle on success
304  *      0, if no phandle was found in the device tree
305  *      -1, if an error occurred
306  */
307 uint32_t fdt_get_max_phandle(const void *fdt);
308
309 /**
310  * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
311  * @fdt: pointer to the device tree blob
312  *
313  * Returns the number of entries in the device tree blob's memory
314  * reservation map.  This does not include the terminating 0,0 entry
315  * or any other (0,0) entries reserved for expansion.
316  *
317  * returns:
318  *     the number of entries
319  */
320 int fdt_num_mem_rsv(const void *fdt);
321
322 /**
323  * fdt_get_mem_rsv - retrieve one memory reserve map entry
324  * @fdt: pointer to the device tree blob
325  * @address, @size: pointers to 64-bit variables
326  *
327  * On success, *address and *size will contain the address and size of
328  * the n-th reserve map entry from the device tree blob, in
329  * native-endian format.
330  *
331  * returns:
332  *     0, on success
333  *     -FDT_ERR_BADMAGIC,
334  *     -FDT_ERR_BADVERSION,
335  *     -FDT_ERR_BADSTATE, standard meanings
336  */
337 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
338
339 /**
340  * fdt_subnode_offset_namelen - find a subnode based on substring
341  * @fdt: pointer to the device tree blob
342  * @parentoffset: structure block offset of a node
343  * @name: name of the subnode to locate
344  * @namelen: number of characters of name to consider
345  *
346  * Identical to fdt_subnode_offset(), but only examine the first
347  * namelen characters of name for matching the subnode name.  This is
348  * useful for finding subnodes based on a portion of a larger string,
349  * such as a full path.
350  */
351 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
352                                const char *name, int namelen);
353 /**
354  * fdt_subnode_offset - find a subnode of a given node
355  * @fdt: pointer to the device tree blob
356  * @parentoffset: structure block offset of a node
357  * @name: name of the subnode to locate
358  *
359  * fdt_subnode_offset() finds a subnode of the node at structure block
360  * offset parentoffset with the given name.  name may include a unit
361  * address, in which case fdt_subnode_offset() will find the subnode
362  * with that unit address, or the unit address may be omitted, in
363  * which case fdt_subnode_offset() will find an arbitrary subnode
364  * whose name excluding unit address matches the given name.
365  *
366  * returns:
367  *      structure block offset of the requested subnode (>=0), on success
368  *      -FDT_ERR_NOTFOUND, if the requested subnode does not exist
369  *      -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
370  *              tag
371  *      -FDT_ERR_BADMAGIC,
372  *      -FDT_ERR_BADVERSION,
373  *      -FDT_ERR_BADSTATE,
374  *      -FDT_ERR_BADSTRUCTURE,
375  *      -FDT_ERR_TRUNCATED, standard meanings.
376  */
377 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
378
379 /**
380  * fdt_path_offset_namelen - find a tree node by its full path
381  * @fdt: pointer to the device tree blob
382  * @path: full path of the node to locate
383  * @namelen: number of characters of path to consider
384  *
385  * Identical to fdt_path_offset(), but only consider the first namelen
386  * characters of path as the path name.
387  */
388 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
389
390 /**
391  * fdt_path_offset - find a tree node by its full path
392  * @fdt: pointer to the device tree blob
393  * @path: full path of the node to locate
394  *
395  * fdt_path_offset() finds a node of a given path in the device tree.
396  * Each path component may omit the unit address portion, but the
397  * results of this are undefined if any such path component is
398  * ambiguous (that is if there are multiple nodes at the relevant
399  * level matching the given component, differentiated only by unit
400  * address).
401  *
402  * returns:
403  *      structure block offset of the node with the requested path (>=0), on
404  *              success
405  *      -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
406  *      -FDT_ERR_NOTFOUND, if the requested node does not exist
407  *      -FDT_ERR_BADMAGIC,
408  *      -FDT_ERR_BADVERSION,
409  *      -FDT_ERR_BADSTATE,
410  *      -FDT_ERR_BADSTRUCTURE,
411  *      -FDT_ERR_TRUNCATED, standard meanings.
412  */
413 static inline int fdt_path_offset(const void *fdt, const char *path)
414 {
415         return fdt_path_offset_namelen(fdt, path, strlen(path));
416 }
417
418 /**
419  * fdt_get_name - retrieve the name of a given node
420  * @fdt: pointer to the device tree blob
421  * @nodeoffset: structure block offset of the starting node
422  * @lenp: pointer to an integer variable (will be overwritten) or NULL
423  *
424  * fdt_get_name() retrieves the name (including unit address) of the
425  * device tree node at structure block offset nodeoffset.  If lenp is
426  * non-NULL, the length of this name is also returned, in the integer
427  * pointed to by lenp.
428  *
429  * returns:
430  *      pointer to the node's name, on success
431  *              If lenp is non-NULL, *lenp contains the length of that name
432  *                      (>=0)
433  *      NULL, on error
434  *              if lenp is non-NULL *lenp contains an error code (<0):
435  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
436  *                      tag
437  *              -FDT_ERR_BADMAGIC,
438  *              -FDT_ERR_BADVERSION,
439  *              -FDT_ERR_BADSTATE, standard meanings
440  */
441 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
442
443 /**
444  * fdt_first_property_offset - find the offset of a node's first property
445  * @fdt: pointer to the device tree blob
446  * @nodeoffset: structure block offset of a node
447  *
448  * fdt_first_property_offset() finds the first property of the node at
449  * the given structure block offset.
450  *
451  * returns:
452  *      structure block offset of the property (>=0), on success
453  *      -FDT_ERR_NOTFOUND, if the requested node has no properties
454  *      -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
455  *      -FDT_ERR_BADMAGIC,
456  *      -FDT_ERR_BADVERSION,
457  *      -FDT_ERR_BADSTATE,
458  *      -FDT_ERR_BADSTRUCTURE,
459  *      -FDT_ERR_TRUNCATED, standard meanings.
460  */
461 int fdt_first_property_offset(const void *fdt, int nodeoffset);
462
463 /**
464  * fdt_next_property_offset - step through a node's properties
465  * @fdt: pointer to the device tree blob
466  * @offset: structure block offset of a property
467  *
468  * fdt_next_property_offset() finds the property immediately after the
469  * one at the given structure block offset.  This will be a property
470  * of the same node as the given property.
471  *
472  * returns:
473  *      structure block offset of the next property (>=0), on success
474  *      -FDT_ERR_NOTFOUND, if the given property is the last in its node
475  *      -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
476  *      -FDT_ERR_BADMAGIC,
477  *      -FDT_ERR_BADVERSION,
478  *      -FDT_ERR_BADSTATE,
479  *      -FDT_ERR_BADSTRUCTURE,
480  *      -FDT_ERR_TRUNCATED, standard meanings.
481  */
482 int fdt_next_property_offset(const void *fdt, int offset);
483
484 /**
485  * fdt_for_each_property_offset - iterate over all properties of a node
486  *
487  * @property_offset:    property offset (int, lvalue)
488  * @fdt:                FDT blob (const void *)
489  * @node:               node offset (int)
490  *
491  * This is actually a wrapper around a for loop and would be used like so:
492  *
493  *      fdt_for_each_property_offset(property, fdt, node) {
494  *              Use property
495  *              ...
496  *      }
497  *
498  *      if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) {
499  *              Error handling
500  *      }
501  *
502  * Note that this is implemented as a macro and property is used as
503  * iterator in the loop. The node variable can be constant or even a
504  * literal.
505  */
506 #define fdt_for_each_property_offset(property, fdt, node)       \
507         for (property = fdt_first_property_offset(fdt, node);   \
508              property >= 0;                                     \
509              property = fdt_next_property_offset(fdt, property))
510
511 /**
512  * fdt_get_property_by_offset - retrieve the property at a given offset
513  * @fdt: pointer to the device tree blob
514  * @offset: offset of the property to retrieve
515  * @lenp: pointer to an integer variable (will be overwritten) or NULL
516  *
517  * fdt_get_property_by_offset() retrieves a pointer to the
518  * fdt_property structure within the device tree blob at the given
519  * offset.  If lenp is non-NULL, the length of the property value is
520  * also returned, in the integer pointed to by lenp.
521  *
522  * returns:
523  *      pointer to the structure representing the property
524  *              if lenp is non-NULL, *lenp contains the length of the property
525  *              value (>=0)
526  *      NULL, on error
527  *              if lenp is non-NULL, *lenp contains an error code (<0):
528  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
529  *              -FDT_ERR_BADMAGIC,
530  *              -FDT_ERR_BADVERSION,
531  *              -FDT_ERR_BADSTATE,
532  *              -FDT_ERR_BADSTRUCTURE,
533  *              -FDT_ERR_TRUNCATED, standard meanings
534  */
535 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
536                                                       int offset,
537                                                       int *lenp);
538
539 /**
540  * fdt_get_property_namelen - find a property based on substring
541  * @fdt: pointer to the device tree blob
542  * @nodeoffset: offset of the node whose property to find
543  * @name: name of the property to find
544  * @namelen: number of characters of name to consider
545  * @lenp: pointer to an integer variable (will be overwritten) or NULL
546  *
547  * Identical to fdt_get_property(), but only examine the first namelen
548  * characters of name for matching the property name.
549  */
550 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
551                                                     int nodeoffset,
552                                                     const char *name,
553                                                     int namelen, int *lenp);
554
555 /**
556  * fdt_get_property - find a given property in a given node
557  * @fdt: pointer to the device tree blob
558  * @nodeoffset: offset of the node whose property to find
559  * @name: name of the property to find
560  * @lenp: pointer to an integer variable (will be overwritten) or NULL
561  *
562  * fdt_get_property() retrieves a pointer to the fdt_property
563  * structure within the device tree blob corresponding to the property
564  * named 'name' of the node at offset nodeoffset.  If lenp is
565  * non-NULL, the length of the property value is also returned, in the
566  * integer pointed to by lenp.
567  *
568  * returns:
569  *      pointer to the structure representing the property
570  *              if lenp is non-NULL, *lenp contains the length of the property
571  *              value (>=0)
572  *      NULL, on error
573  *              if lenp is non-NULL, *lenp contains an error code (<0):
574  *              -FDT_ERR_NOTFOUND, node does not have named property
575  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
576  *                      tag
577  *              -FDT_ERR_BADMAGIC,
578  *              -FDT_ERR_BADVERSION,
579  *              -FDT_ERR_BADSTATE,
580  *              -FDT_ERR_BADSTRUCTURE,
581  *              -FDT_ERR_TRUNCATED, standard meanings
582  */
583 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
584                                             const char *name, int *lenp);
585 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
586                                                       const char *name,
587                                                       int *lenp)
588 {
589         return (struct fdt_property *)(uintptr_t)
590                 fdt_get_property(fdt, nodeoffset, name, lenp);
591 }
592
593 /**
594  * fdt_getprop_by_offset - retrieve the value of a property at a given offset
595  * @fdt: pointer to the device tree blob
596  * @ffset: offset of the property to read
597  * @namep: pointer to a string variable (will be overwritten) or NULL
598  * @lenp: pointer to an integer variable (will be overwritten) or NULL
599  *
600  * fdt_getprop_by_offset() retrieves a pointer to the value of the
601  * property at structure block offset 'offset' (this will be a pointer
602  * to within the device blob itself, not a copy of the value).  If
603  * lenp is non-NULL, the length of the property value is also
604  * returned, in the integer pointed to by lenp.  If namep is non-NULL,
605  * the property's namne will also be returned in the char * pointed to
606  * by namep (this will be a pointer to within the device tree's string
607  * block, not a new copy of the name).
608  *
609  * returns:
610  *      pointer to the property's value
611  *              if lenp is non-NULL, *lenp contains the length of the property
612  *              value (>=0)
613  *              if namep is non-NULL *namep contiains a pointer to the property
614  *              name.
615  *      NULL, on error
616  *              if lenp is non-NULL, *lenp contains an error code (<0):
617  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
618  *              -FDT_ERR_BADMAGIC,
619  *              -FDT_ERR_BADVERSION,
620  *              -FDT_ERR_BADSTATE,
621  *              -FDT_ERR_BADSTRUCTURE,
622  *              -FDT_ERR_TRUNCATED, standard meanings
623  */
624 const void *fdt_getprop_by_offset(const void *fdt, int offset,
625                                   const char **namep, int *lenp);
626
627 /**
628  * fdt_getprop_namelen - get property value based on substring
629  * @fdt: pointer to the device tree blob
630  * @nodeoffset: offset of the node whose property to find
631  * @name: name of the property to find
632  * @namelen: number of characters of name to consider
633  * @lenp: pointer to an integer variable (will be overwritten) or NULL
634  *
635  * Identical to fdt_getprop(), but only examine the first namelen
636  * characters of name for matching the property name.
637  */
638 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
639                                 const char *name, int namelen, int *lenp);
640 static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
641                                           const char *name, int namelen,
642                                           int *lenp)
643 {
644         return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
645                                                       namelen, lenp);
646 }
647
648 /**
649  * fdt_getprop - retrieve the value of a given property
650  * @fdt: pointer to the device tree blob
651  * @nodeoffset: offset of the node whose property to find
652  * @name: name of the property to find
653  * @lenp: pointer to an integer variable (will be overwritten) or NULL
654  *
655  * fdt_getprop() retrieves a pointer to the value of the property
656  * named 'name' of the node at offset nodeoffset (this will be a
657  * pointer to within the device blob itself, not a copy of the value).
658  * If lenp is non-NULL, the length of the property value is also
659  * returned, in the integer pointed to by lenp.
660  *
661  * returns:
662  *      pointer to the property's value
663  *              if lenp is non-NULL, *lenp contains the length of the property
664  *              value (>=0)
665  *      NULL, on error
666  *              if lenp is non-NULL, *lenp contains an error code (<0):
667  *              -FDT_ERR_NOTFOUND, node does not have named property
668  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
669  *                      tag
670  *              -FDT_ERR_BADMAGIC,
671  *              -FDT_ERR_BADVERSION,
672  *              -FDT_ERR_BADSTATE,
673  *              -FDT_ERR_BADSTRUCTURE,
674  *              -FDT_ERR_TRUNCATED, standard meanings
675  */
676 const void *fdt_getprop(const void *fdt, int nodeoffset,
677                         const char *name, int *lenp);
678 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
679                                   const char *name, int *lenp)
680 {
681         return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
682 }
683
684 /**
685  * fdt_get_phandle - retrieve the phandle of a given node
686  * @fdt: pointer to the device tree blob
687  * @nodeoffset: structure block offset of the node
688  *
689  * fdt_get_phandle() retrieves the phandle of the device tree node at
690  * structure block offset nodeoffset.
691  *
692  * returns:
693  *      the phandle of the node at nodeoffset, on success (!= 0, != -1)
694  *      0, if the node has no phandle, or another error occurs
695  */
696 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
697
698 /**
699  * fdt_get_alias_namelen - get alias based on substring
700  * @fdt: pointer to the device tree blob
701  * @name: name of the alias th look up
702  * @namelen: number of characters of name to consider
703  *
704  * Identical to fdt_get_alias(), but only examine the first namelen
705  * characters of name for matching the alias name.
706  */
707 const char *fdt_get_alias_namelen(const void *fdt,
708                                   const char *name, int namelen);
709
710 /**
711  * fdt_get_alias - retrieve the path referenced by a given alias
712  * @fdt: pointer to the device tree blob
713  * @name: name of the alias th look up
714  *
715  * fdt_get_alias() retrieves the value of a given alias.  That is, the
716  * value of the property named 'name' in the node /aliases.
717  *
718  * returns:
719  *      a pointer to the expansion of the alias named 'name', if it exists
720  *      NULL, if the given alias or the /aliases node does not exist
721  */
722 const char *fdt_get_alias(const void *fdt, const char *name);
723
724 /**
725  * fdt_get_path - determine the full path of a node
726  * @fdt: pointer to the device tree blob
727  * @nodeoffset: offset of the node whose path to find
728  * @buf: character buffer to contain the returned path (will be overwritten)
729  * @buflen: size of the character buffer at buf
730  *
731  * fdt_get_path() computes the full path of the node at offset
732  * nodeoffset, and records that path in the buffer at buf.
733  *
734  * NOTE: This function is expensive, as it must scan the device tree
735  * structure from the start to nodeoffset.
736  *
737  * returns:
738  *      0, on success
739  *              buf contains the absolute path of the node at
740  *              nodeoffset, as a NUL-terminated string.
741  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
742  *      -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
743  *              characters and will not fit in the given buffer.
744  *      -FDT_ERR_BADMAGIC,
745  *      -FDT_ERR_BADVERSION,
746  *      -FDT_ERR_BADSTATE,
747  *      -FDT_ERR_BADSTRUCTURE, standard meanings
748  */
749 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
750
751 /**
752  * fdt_supernode_atdepth_offset - find a specific ancestor of a node
753  * @fdt: pointer to the device tree blob
754  * @nodeoffset: offset of the node whose parent to find
755  * @supernodedepth: depth of the ancestor to find
756  * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
757  *
758  * fdt_supernode_atdepth_offset() finds an ancestor of the given node
759  * at a specific depth from the root (where the root itself has depth
760  * 0, its immediate subnodes depth 1 and so forth).  So
761  *      fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
762  * will always return 0, the offset of the root node.  If the node at
763  * nodeoffset has depth D, then:
764  *      fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
765  * will return nodeoffset itself.
766  *
767  * NOTE: This function is expensive, as it must scan the device tree
768  * structure from the start to nodeoffset.
769  *
770  * returns:
771  *      structure block offset of the node at node offset's ancestor
772  *              of depth supernodedepth (>=0), on success
773  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
774  *      -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
775  *              nodeoffset
776  *      -FDT_ERR_BADMAGIC,
777  *      -FDT_ERR_BADVERSION,
778  *      -FDT_ERR_BADSTATE,
779  *      -FDT_ERR_BADSTRUCTURE, standard meanings
780  */
781 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
782                                  int supernodedepth, int *nodedepth);
783
784 /**
785  * fdt_node_depth - find the depth of a given node
786  * @fdt: pointer to the device tree blob
787  * @nodeoffset: offset of the node whose parent to find
788  *
789  * fdt_node_depth() finds the depth of a given node.  The root node
790  * has depth 0, its immediate subnodes depth 1 and so forth.
791  *
792  * NOTE: This function is expensive, as it must scan the device tree
793  * structure from the start to nodeoffset.
794  *
795  * returns:
796  *      depth of the node at nodeoffset (>=0), on success
797  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
798  *      -FDT_ERR_BADMAGIC,
799  *      -FDT_ERR_BADVERSION,
800  *      -FDT_ERR_BADSTATE,
801  *      -FDT_ERR_BADSTRUCTURE, standard meanings
802  */
803 int fdt_node_depth(const void *fdt, int nodeoffset);
804
805 /**
806  * fdt_parent_offset - find the parent of a given node
807  * @fdt: pointer to the device tree blob
808  * @nodeoffset: offset of the node whose parent to find
809  *
810  * fdt_parent_offset() locates the parent node of a given node (that
811  * is, it finds the offset of the node which contains the node at
812  * nodeoffset as a subnode).
813  *
814  * NOTE: This function is expensive, as it must scan the device tree
815  * structure from the start to nodeoffset, *twice*.
816  *
817  * returns:
818  *      structure block offset of the parent of the node at nodeoffset
819  *              (>=0), on success
820  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
821  *      -FDT_ERR_BADMAGIC,
822  *      -FDT_ERR_BADVERSION,
823  *      -FDT_ERR_BADSTATE,
824  *      -FDT_ERR_BADSTRUCTURE, standard meanings
825  */
826 int fdt_parent_offset(const void *fdt, int nodeoffset);
827
828 /**
829  * fdt_node_offset_by_prop_value - find nodes with a given property value
830  * @fdt: pointer to the device tree blob
831  * @startoffset: only find nodes after this offset
832  * @propname: property name to check
833  * @propval: property value to search for
834  * @proplen: length of the value in propval
835  *
836  * fdt_node_offset_by_prop_value() returns the offset of the first
837  * node after startoffset, which has a property named propname whose
838  * value is of length proplen and has value equal to propval; or if
839  * startoffset is -1, the very first such node in the tree.
840  *
841  * To iterate through all nodes matching the criterion, the following
842  * idiom can be used:
843  *      offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
844  *                                             propval, proplen);
845  *      while (offset != -FDT_ERR_NOTFOUND) {
846  *              // other code here
847  *              offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
848  *                                                     propval, proplen);
849  *      }
850  *
851  * Note the -1 in the first call to the function, if 0 is used here
852  * instead, the function will never locate the root node, even if it
853  * matches the criterion.
854  *
855  * returns:
856  *      structure block offset of the located node (>= 0, >startoffset),
857  *               on success
858  *      -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
859  *              tree after startoffset
860  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
861  *      -FDT_ERR_BADMAGIC,
862  *      -FDT_ERR_BADVERSION,
863  *      -FDT_ERR_BADSTATE,
864  *      -FDT_ERR_BADSTRUCTURE, standard meanings
865  */
866 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
867                                   const char *propname,
868                                   const void *propval, int proplen);
869
870 /**
871  * fdt_node_offset_by_phandle - find the node with a given phandle
872  * @fdt: pointer to the device tree blob
873  * @phandle: phandle value
874  *
875  * fdt_node_offset_by_phandle() returns the offset of the node
876  * which has the given phandle value.  If there is more than one node
877  * in the tree with the given phandle (an invalid tree), results are
878  * undefined.
879  *
880  * returns:
881  *      structure block offset of the located node (>= 0), on success
882  *      -FDT_ERR_NOTFOUND, no node with that phandle exists
883  *      -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
884  *      -FDT_ERR_BADMAGIC,
885  *      -FDT_ERR_BADVERSION,
886  *      -FDT_ERR_BADSTATE,
887  *      -FDT_ERR_BADSTRUCTURE, standard meanings
888  */
889 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
890
891 /**
892  * fdt_node_check_compatible: check a node's compatible property
893  * @fdt: pointer to the device tree blob
894  * @nodeoffset: offset of a tree node
895  * @compatible: string to match against
896  *
897  *
898  * fdt_node_check_compatible() returns 0 if the given node contains a
899  * 'compatible' property with the given string as one of its elements,
900  * it returns non-zero otherwise, or on error.
901  *
902  * returns:
903  *      0, if the node has a 'compatible' property listing the given string
904  *      1, if the node has a 'compatible' property, but it does not list
905  *              the given string
906  *      -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
907  *      -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
908  *      -FDT_ERR_BADMAGIC,
909  *      -FDT_ERR_BADVERSION,
910  *      -FDT_ERR_BADSTATE,
911  *      -FDT_ERR_BADSTRUCTURE, standard meanings
912  */
913 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
914                               const char *compatible);
915
916 /**
917  * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
918  * @fdt: pointer to the device tree blob
919  * @startoffset: only find nodes after this offset
920  * @compatible: 'compatible' string to match against
921  *
922  * fdt_node_offset_by_compatible() returns the offset of the first
923  * node after startoffset, which has a 'compatible' property which
924  * lists the given compatible string; or if startoffset is -1, the
925  * very first such node in the tree.
926  *
927  * To iterate through all nodes matching the criterion, the following
928  * idiom can be used:
929  *      offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
930  *      while (offset != -FDT_ERR_NOTFOUND) {
931  *              // other code here
932  *              offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
933  *      }
934  *
935  * Note the -1 in the first call to the function, if 0 is used here
936  * instead, the function will never locate the root node, even if it
937  * matches the criterion.
938  *
939  * returns:
940  *      structure block offset of the located node (>= 0, >startoffset),
941  *               on success
942  *      -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
943  *              tree after startoffset
944  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
945  *      -FDT_ERR_BADMAGIC,
946  *      -FDT_ERR_BADVERSION,
947  *      -FDT_ERR_BADSTATE,
948  *      -FDT_ERR_BADSTRUCTURE, standard meanings
949  */
950 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
951                                   const char *compatible);
952
953 /**
954  * fdt_stringlist_contains - check a string list property for a string
955  * @strlist: Property containing a list of strings to check
956  * @listlen: Length of property
957  * @str: String to search for
958  *
959  * This is a utility function provided for convenience. The list contains
960  * one or more strings, each terminated by \0, as is found in a device tree
961  * "compatible" property.
962  *
963  * @return: 1 if the string is found in the list, 0 not found, or invalid list
964  */
965 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
966
967 /**
968  * fdt_stringlist_count - count the number of strings in a string list
969  * @fdt: pointer to the device tree blob
970  * @nodeoffset: offset of a tree node
971  * @property: name of the property containing the string list
972  * @return:
973  *   the number of strings in the given property
974  *   -FDT_ERR_BADVALUE if the property value is not NUL-terminated
975  *   -FDT_ERR_NOTFOUND if the property does not exist
976  */
977 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
978
979 /**
980  * fdt_stringlist_search - find a string in a string list and return its index
981  * @fdt: pointer to the device tree blob
982  * @nodeoffset: offset of a tree node
983  * @property: name of the property containing the string list
984  * @string: string to look up in the string list
985  *
986  * Note that it is possible for this function to succeed on property values
987  * that are not NUL-terminated. That's because the function will stop after
988  * finding the first occurrence of @string. This can for example happen with
989  * small-valued cell properties, such as #address-cells, when searching for
990  * the empty string.
991  *
992  * @return:
993  *   the index of the string in the list of strings
994  *   -FDT_ERR_BADVALUE if the property value is not NUL-terminated
995  *   -FDT_ERR_NOTFOUND if the property does not exist or does not contain
996  *                     the given string
997  */
998 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
999                           const char *string);
1000
1001 /**
1002  * fdt_stringlist_get() - obtain the string at a given index in a string list
1003  * @fdt: pointer to the device tree blob
1004  * @nodeoffset: offset of a tree node
1005  * @property: name of the property containing the string list
1006  * @index: index of the string to return
1007  * @lenp: return location for the string length or an error code on failure
1008  *
1009  * Note that this will successfully extract strings from properties with
1010  * non-NUL-terminated values. For example on small-valued cell properties
1011  * this function will return the empty string.
1012  *
1013  * If non-NULL, the length of the string (on success) or a negative error-code
1014  * (on failure) will be stored in the integer pointer to by lenp.
1015  *
1016  * @return:
1017  *   A pointer to the string at the given index in the string list or NULL on
1018  *   failure. On success the length of the string will be stored in the memory
1019  *   location pointed to by the lenp parameter, if non-NULL. On failure one of
1020  *   the following negative error codes will be returned in the lenp parameter
1021  *   (if non-NULL):
1022  *     -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1023  *     -FDT_ERR_NOTFOUND if the property does not exist
1024  */
1025 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
1026                                const char *property, int index,
1027                                int *lenp);
1028
1029 /**********************************************************************/
1030 /* Read-only functions (addressing related)                           */
1031 /**********************************************************************/
1032
1033 /**
1034  * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
1035  *
1036  * This is the maximum value for #address-cells, #size-cells and
1037  * similar properties that will be processed by libfdt.  IEE1275
1038  * requires that OF implementations handle values up to 4.
1039  * Implementations may support larger values, but in practice higher
1040  * values aren't used.
1041  */
1042 #define FDT_MAX_NCELLS          4
1043
1044 /**
1045  * fdt_address_cells - retrieve address size for a bus represented in the tree
1046  * @fdt: pointer to the device tree blob
1047  * @nodeoffset: offset of the node to find the address size for
1048  *
1049  * When the node has a valid #address-cells property, returns its value.
1050  *
1051  * returns:
1052  *      0 <= n < FDT_MAX_NCELLS, on success
1053  *      2, if the node has no #address-cells property
1054  *      -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1055  *              #address-cells property
1056  *      -FDT_ERR_BADMAGIC,
1057  *      -FDT_ERR_BADVERSION,
1058  *      -FDT_ERR_BADSTATE,
1059  *      -FDT_ERR_BADSTRUCTURE,
1060  *      -FDT_ERR_TRUNCATED, standard meanings
1061  */
1062 int fdt_address_cells(const void *fdt, int nodeoffset);
1063
1064 /**
1065  * fdt_size_cells - retrieve address range size for a bus represented in the
1066  *                  tree
1067  * @fdt: pointer to the device tree blob
1068  * @nodeoffset: offset of the node to find the address range size for
1069  *
1070  * When the node has a valid #size-cells property, returns its value.
1071  *
1072  * returns:
1073  *      0 <= n < FDT_MAX_NCELLS, on success
1074  *      2, if the node has no #address-cells property
1075  *      -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1076  *              #size-cells property
1077  *      -FDT_ERR_BADMAGIC,
1078  *      -FDT_ERR_BADVERSION,
1079  *      -FDT_ERR_BADSTATE,
1080  *      -FDT_ERR_BADSTRUCTURE,
1081  *      -FDT_ERR_TRUNCATED, standard meanings
1082  */
1083 int fdt_size_cells(const void *fdt, int nodeoffset);
1084
1085
1086 /**********************************************************************/
1087 /* Write-in-place functions                                           */
1088 /**********************************************************************/
1089
1090 /**
1091  * fdt_setprop_inplace_namelen_partial - change a property's value,
1092  *                                       but not its size
1093  * @fdt: pointer to the device tree blob
1094  * @nodeoffset: offset of the node whose property to change
1095  * @name: name of the property to change
1096  * @namelen: number of characters of name to consider
1097  * @idx: index of the property to change in the array
1098  * @val: pointer to data to replace the property value with
1099  * @len: length of the property value
1100  *
1101  * Identical to fdt_setprop_inplace(), but modifies the given property
1102  * starting from the given index, and using only the first characters
1103  * of the name. It is useful when you want to manipulate only one value of
1104  * an array and you have a string that doesn't end with \0.
1105  */
1106 int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
1107                                         const char *name, int namelen,
1108                                         uint32_t idx, const void *val,
1109                                         int len);
1110
1111 /**
1112  * fdt_setprop_inplace - change a property's value, but not its size
1113  * @fdt: pointer to the device tree blob
1114  * @nodeoffset: offset of the node whose property to change
1115  * @name: name of the property to change
1116  * @val: pointer to data to replace the property value with
1117  * @len: length of the property value
1118  *
1119  * fdt_setprop_inplace() replaces the value of a given property with
1120  * the data in val, of length len.  This function cannot change the
1121  * size of a property, and so will only work if len is equal to the
1122  * current length of the property.
1123  *
1124  * This function will alter only the bytes in the blob which contain
1125  * the given property value, and will not alter or move any other part
1126  * of the tree.
1127  *
1128  * returns:
1129  *      0, on success
1130  *      -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1131  *      -FDT_ERR_NOTFOUND, node does not have the named property
1132  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1133  *      -FDT_ERR_BADMAGIC,
1134  *      -FDT_ERR_BADVERSION,
1135  *      -FDT_ERR_BADSTATE,
1136  *      -FDT_ERR_BADSTRUCTURE,
1137  *      -FDT_ERR_TRUNCATED, standard meanings
1138  */
1139 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1140                         const void *val, int len);
1141
1142 /**
1143  * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
1144  * @fdt: pointer to the device tree blob
1145  * @nodeoffset: offset of the node whose property to change
1146  * @name: name of the property to change
1147  * @val: 32-bit integer value to replace the property with
1148  *
1149  * fdt_setprop_inplace_u32() replaces the value of a given property
1150  * with the 32-bit integer value in val, converting val to big-endian
1151  * if necessary.  This function cannot change the size of a property,
1152  * and so will only work if the property already exists and has length
1153  * 4.
1154  *
1155  * This function will alter only the bytes in the blob which contain
1156  * the given property value, and will not alter or move any other part
1157  * of the tree.
1158  *
1159  * returns:
1160  *      0, on success
1161  *      -FDT_ERR_NOSPACE, if the property's length is not equal to 4
1162  *      -FDT_ERR_NOTFOUND, node does not have the named property
1163  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1164  *      -FDT_ERR_BADMAGIC,
1165  *      -FDT_ERR_BADVERSION,
1166  *      -FDT_ERR_BADSTATE,
1167  *      -FDT_ERR_BADSTRUCTURE,
1168  *      -FDT_ERR_TRUNCATED, standard meanings
1169  */
1170 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1171                                           const char *name, uint32_t val)
1172 {
1173         fdt32_t tmp = cpu_to_fdt32(val);
1174         return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1175 }
1176
1177 /**
1178  * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1179  * @fdt: pointer to the device tree blob
1180  * @nodeoffset: offset of the node whose property to change
1181  * @name: name of the property to change
1182  * @val: 64-bit integer value to replace the property with
1183  *
1184  * fdt_setprop_inplace_u64() replaces the value of a given property
1185  * with the 64-bit integer value in val, converting val to big-endian
1186  * if necessary.  This function cannot change the size of a property,
1187  * and so will only work if the property already exists and has length
1188  * 8.
1189  *
1190  * This function will alter only the bytes in the blob which contain
1191  * the given property value, and will not alter or move any other part
1192  * of the tree.
1193  *
1194  * returns:
1195  *      0, on success
1196  *      -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1197  *      -FDT_ERR_NOTFOUND, node does not have the named property
1198  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1199  *      -FDT_ERR_BADMAGIC,
1200  *      -FDT_ERR_BADVERSION,
1201  *      -FDT_ERR_BADSTATE,
1202  *      -FDT_ERR_BADSTRUCTURE,
1203  *      -FDT_ERR_TRUNCATED, standard meanings
1204  */
1205 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1206                                           const char *name, uint64_t val)
1207 {
1208         fdt64_t tmp = cpu_to_fdt64(val);
1209         return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1210 }
1211
1212 /**
1213  * fdt_setprop_inplace_cell - change the value of a single-cell property
1214  *
1215  * This is an alternative name for fdt_setprop_inplace_u32()
1216  */
1217 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1218                                            const char *name, uint32_t val)
1219 {
1220         return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1221 }
1222
1223 /**
1224  * fdt_nop_property - replace a property with nop tags
1225  * @fdt: pointer to the device tree blob
1226  * @nodeoffset: offset of the node whose property to nop
1227  * @name: name of the property to nop
1228  *
1229  * fdt_nop_property() will replace a given property's representation
1230  * in the blob with FDT_NOP tags, effectively removing it from the
1231  * tree.
1232  *
1233  * This function will alter only the bytes in the blob which contain
1234  * the property, and will not alter or move any other part of the
1235  * tree.
1236  *
1237  * returns:
1238  *      0, on success
1239  *      -FDT_ERR_NOTFOUND, node does not have the named property
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
1246  */
1247 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1248
1249 /**
1250  * fdt_nop_node - replace a node (subtree) with nop tags
1251  * @fdt: pointer to the device tree blob
1252  * @nodeoffset: offset of the node to nop
1253  *
1254  * fdt_nop_node() will replace a given node's representation in the
1255  * blob, including all its subnodes, if any, with FDT_NOP tags,
1256  * effectively removing it from the tree.
1257  *
1258  * This function will alter only the bytes in the blob which contain
1259  * the node and its properties and subnodes, and will not alter or
1260  * move any other part of the tree.
1261  *
1262  * returns:
1263  *      0, on success
1264  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1265  *      -FDT_ERR_BADMAGIC,
1266  *      -FDT_ERR_BADVERSION,
1267  *      -FDT_ERR_BADSTATE,
1268  *      -FDT_ERR_BADSTRUCTURE,
1269  *      -FDT_ERR_TRUNCATED, standard meanings
1270  */
1271 int fdt_nop_node(void *fdt, int nodeoffset);
1272
1273 /**********************************************************************/
1274 /* Sequential write functions                                         */
1275 /**********************************************************************/
1276
1277 int fdt_create(void *buf, int bufsize);
1278 int fdt_resize(void *fdt, void *buf, int bufsize);
1279 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1280 int fdt_finish_reservemap(void *fdt);
1281 int fdt_begin_node(void *fdt, const char *name);
1282 int fdt_property(void *fdt, const char *name, const void *val, int len);
1283 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1284 {
1285         fdt32_t tmp = cpu_to_fdt32(val);
1286         return fdt_property(fdt, name, &tmp, sizeof(tmp));
1287 }
1288 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1289 {
1290         fdt64_t tmp = cpu_to_fdt64(val);
1291         return fdt_property(fdt, name, &tmp, sizeof(tmp));
1292 }
1293 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1294 {
1295         return fdt_property_u32(fdt, name, val);
1296 }
1297
1298 /**
1299  * fdt_property_placeholder - add a new property and return a ptr to its value
1300  *
1301  * @fdt: pointer to the device tree blob
1302  * @name: name of property to add
1303  * @len: length of property value in bytes
1304  * @valp: returns a pointer to where where the value should be placed
1305  *
1306  * returns:
1307  *      0, on success
1308  *      -FDT_ERR_BADMAGIC,
1309  *      -FDT_ERR_NOSPACE, standard meanings
1310  */
1311 int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
1312
1313 #define fdt_property_string(fdt, name, str) \
1314         fdt_property(fdt, name, str, strlen(str)+1)
1315 int fdt_end_node(void *fdt);
1316 int fdt_finish(void *fdt);
1317
1318 /**********************************************************************/
1319 /* Read-write functions                                               */
1320 /**********************************************************************/
1321
1322 int fdt_create_empty_tree(void *buf, int bufsize);
1323 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1324 int fdt_pack(void *fdt);
1325
1326 /**
1327  * fdt_add_mem_rsv - add one memory reserve map entry
1328  * @fdt: pointer to the device tree blob
1329  * @address, @size: 64-bit values (native endian)
1330  *
1331  * Adds a reserve map entry to the given blob reserving a region at
1332  * address address of length size.
1333  *
1334  * This function will insert data into the reserve map and will
1335  * therefore change the indexes of some entries in the table.
1336  *
1337  * returns:
1338  *      0, on success
1339  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1340  *              contain the new reservation entry
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
1347  */
1348 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1349
1350 /**
1351  * fdt_del_mem_rsv - remove a memory reserve map entry
1352  * @fdt: pointer to the device tree blob
1353  * @n: entry to remove
1354  *
1355  * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1356  * the blob.
1357  *
1358  * This function will delete data from the reservation table and will
1359  * therefore change the indexes of some entries in the table.
1360  *
1361  * returns:
1362  *      0, on success
1363  *      -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1364  *              are less than n+1 reserve map entries)
1365  *      -FDT_ERR_BADMAGIC,
1366  *      -FDT_ERR_BADVERSION,
1367  *      -FDT_ERR_BADSTATE,
1368  *      -FDT_ERR_BADSTRUCTURE,
1369  *      -FDT_ERR_BADLAYOUT,
1370  *      -FDT_ERR_TRUNCATED, standard meanings
1371  */
1372 int fdt_del_mem_rsv(void *fdt, int n);
1373
1374 /**
1375  * fdt_set_name - change the name of a given node
1376  * @fdt: pointer to the device tree blob
1377  * @nodeoffset: structure block offset of a node
1378  * @name: name to give the node
1379  *
1380  * fdt_set_name() replaces the name (including unit address, if any)
1381  * of the given node with the given string.  NOTE: this function can't
1382  * efficiently check if the new name is unique amongst the given
1383  * node's siblings; results are undefined if this function is invoked
1384  * with a name equal to one of the given node's siblings.
1385  *
1386  * This function may insert or delete data from the blob, and will
1387  * therefore change the offsets of some existing nodes.
1388  *
1389  * returns:
1390  *      0, on success
1391  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1392  *              to contain the new name
1393  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1394  *      -FDT_ERR_BADMAGIC,
1395  *      -FDT_ERR_BADVERSION,
1396  *      -FDT_ERR_BADSTATE, standard meanings
1397  */
1398 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1399
1400 /**
1401  * fdt_setprop - create or change a property
1402  * @fdt: pointer to the device tree blob
1403  * @nodeoffset: offset of the node whose property to change
1404  * @name: name of the property to change
1405  * @val: pointer to data to set the property value to
1406  * @len: length of the property value
1407  *
1408  * fdt_setprop() sets the value of the named property in the given
1409  * node to the given value and length, creating the property if it
1410  * does not already exist.
1411  *
1412  * This function may insert or delete data from the blob, and will
1413  * therefore change the offsets of some existing nodes.
1414  *
1415  * returns:
1416  *      0, on success
1417  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1418  *              contain the new property value
1419  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1420  *      -FDT_ERR_BADLAYOUT,
1421  *      -FDT_ERR_BADMAGIC,
1422  *      -FDT_ERR_BADVERSION,
1423  *      -FDT_ERR_BADSTATE,
1424  *      -FDT_ERR_BADSTRUCTURE,
1425  *      -FDT_ERR_BADLAYOUT,
1426  *      -FDT_ERR_TRUNCATED, standard meanings
1427  */
1428 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1429                 const void *val, int len);
1430
1431 /**
1432  * fdt_setprop_u32 - set a property to a 32-bit integer
1433  * @fdt: pointer to the device tree blob
1434  * @nodeoffset: offset of the node whose property to change
1435  * @name: name of the property to change
1436  * @val: 32-bit integer value for the property (native endian)
1437  *
1438  * fdt_setprop_u32() sets the value of the named property in the given
1439  * node to the given 32-bit integer value (converting to big-endian if
1440  * necessary), or creates a new property with that value if it does
1441  * not already exist.
1442  *
1443  * This function may insert or delete data from the blob, and will
1444  * therefore change the offsets of some existing nodes.
1445  *
1446  * returns:
1447  *      0, on success
1448  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1449  *              contain the new property value
1450  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1451  *      -FDT_ERR_BADLAYOUT,
1452  *      -FDT_ERR_BADMAGIC,
1453  *      -FDT_ERR_BADVERSION,
1454  *      -FDT_ERR_BADSTATE,
1455  *      -FDT_ERR_BADSTRUCTURE,
1456  *      -FDT_ERR_BADLAYOUT,
1457  *      -FDT_ERR_TRUNCATED, standard meanings
1458  */
1459 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1460                                   uint32_t val)
1461 {
1462         fdt32_t tmp = cpu_to_fdt32(val);
1463         return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1464 }
1465
1466 /**
1467  * fdt_setprop_u64 - set a property to a 64-bit integer
1468  * @fdt: pointer to the device tree blob
1469  * @nodeoffset: offset of the node whose property to change
1470  * @name: name of the property to change
1471  * @val: 64-bit integer value for the property (native endian)
1472  *
1473  * fdt_setprop_u64() sets the value of the named property in the given
1474  * node to the given 64-bit integer value (converting to big-endian if
1475  * necessary), or creates a new property with that value if it does
1476  * not already exist.
1477  *
1478  * This function may insert or delete data from the blob, and will
1479  * therefore change the offsets of some existing nodes.
1480  *
1481  * returns:
1482  *      0, on success
1483  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1484  *              contain the new property value
1485  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1486  *      -FDT_ERR_BADLAYOUT,
1487  *      -FDT_ERR_BADMAGIC,
1488  *      -FDT_ERR_BADVERSION,
1489  *      -FDT_ERR_BADSTATE,
1490  *      -FDT_ERR_BADSTRUCTURE,
1491  *      -FDT_ERR_BADLAYOUT,
1492  *      -FDT_ERR_TRUNCATED, standard meanings
1493  */
1494 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1495                                   uint64_t val)
1496 {
1497         fdt64_t tmp = cpu_to_fdt64(val);
1498         return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1499 }
1500
1501 /**
1502  * fdt_setprop_cell - set a property to a single cell value
1503  *
1504  * This is an alternative name for fdt_setprop_u32()
1505  */
1506 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1507                                    uint32_t val)
1508 {
1509         return fdt_setprop_u32(fdt, nodeoffset, name, val);
1510 }
1511
1512 /**
1513  * fdt_setprop_string - set a property to a string value
1514  * @fdt: pointer to the device tree blob
1515  * @nodeoffset: offset of the node whose property to change
1516  * @name: name of the property to change
1517  * @str: string value for the property
1518  *
1519  * fdt_setprop_string() sets the value of the named property in the
1520  * given node to the given string value (using the length of the
1521  * string to determine the new length of the property), or creates a
1522  * new property with that value if it does not already exist.
1523  *
1524  * This function may insert or delete data from the blob, and will
1525  * therefore change the offsets of some existing nodes.
1526  *
1527  * returns:
1528  *      0, on success
1529  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1530  *              contain the new property value
1531  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1532  *      -FDT_ERR_BADLAYOUT,
1533  *      -FDT_ERR_BADMAGIC,
1534  *      -FDT_ERR_BADVERSION,
1535  *      -FDT_ERR_BADSTATE,
1536  *      -FDT_ERR_BADSTRUCTURE,
1537  *      -FDT_ERR_BADLAYOUT,
1538  *      -FDT_ERR_TRUNCATED, standard meanings
1539  */
1540 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1541         fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1542
1543 /**
1544  * fdt_appendprop - append to or create a property
1545  * @fdt: pointer to the device tree blob
1546  * @nodeoffset: offset of the node whose property to change
1547  * @name: name of the property to append to
1548  * @val: pointer to data to append to the property value
1549  * @len: length of the data to append to the property value
1550  *
1551  * fdt_appendprop() appends the value to the named property in the
1552  * given node, creating the property if it does not already exist.
1553  *
1554  * This function may insert data into the blob, and will therefore
1555  * change the offsets of some existing nodes.
1556  *
1557  * returns:
1558  *      0, on success
1559  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1560  *              contain the new property value
1561  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1562  *      -FDT_ERR_BADLAYOUT,
1563  *      -FDT_ERR_BADMAGIC,
1564  *      -FDT_ERR_BADVERSION,
1565  *      -FDT_ERR_BADSTATE,
1566  *      -FDT_ERR_BADSTRUCTURE,
1567  *      -FDT_ERR_BADLAYOUT,
1568  *      -FDT_ERR_TRUNCATED, standard meanings
1569  */
1570 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1571                    const void *val, int len);
1572
1573 /**
1574  * fdt_appendprop_u32 - append a 32-bit integer value to a property
1575  * @fdt: pointer to the device tree blob
1576  * @nodeoffset: offset of the node whose property to change
1577  * @name: name of the property to change
1578  * @val: 32-bit integer value to append to the property (native endian)
1579  *
1580  * fdt_appendprop_u32() appends the given 32-bit integer value
1581  * (converting to big-endian if necessary) to the value of the named
1582  * property in the given node, or creates a new property with that
1583  * value if it does not already exist.
1584  *
1585  * This function may insert data into the blob, and will therefore
1586  * change the offsets of some existing nodes.
1587  *
1588  * returns:
1589  *      0, on success
1590  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1591  *              contain the new property value
1592  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1593  *      -FDT_ERR_BADLAYOUT,
1594  *      -FDT_ERR_BADMAGIC,
1595  *      -FDT_ERR_BADVERSION,
1596  *      -FDT_ERR_BADSTATE,
1597  *      -FDT_ERR_BADSTRUCTURE,
1598  *      -FDT_ERR_BADLAYOUT,
1599  *      -FDT_ERR_TRUNCATED, standard meanings
1600  */
1601 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1602                                      const char *name, uint32_t val)
1603 {
1604         fdt32_t tmp = cpu_to_fdt32(val);
1605         return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1606 }
1607
1608 /**
1609  * fdt_appendprop_u64 - append a 64-bit integer value to a property
1610  * @fdt: pointer to the device tree blob
1611  * @nodeoffset: offset of the node whose property to change
1612  * @name: name of the property to change
1613  * @val: 64-bit integer value to append to the property (native endian)
1614  *
1615  * fdt_appendprop_u64() appends the given 64-bit integer value
1616  * (converting to big-endian if necessary) to the value of the named
1617  * property in the given node, or creates a new property with that
1618  * value if it does not already exist.
1619  *
1620  * This function may insert data into the blob, and will therefore
1621  * change the offsets of some existing nodes.
1622  *
1623  * returns:
1624  *      0, on success
1625  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1626  *              contain the new property value
1627  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1628  *      -FDT_ERR_BADLAYOUT,
1629  *      -FDT_ERR_BADMAGIC,
1630  *      -FDT_ERR_BADVERSION,
1631  *      -FDT_ERR_BADSTATE,
1632  *      -FDT_ERR_BADSTRUCTURE,
1633  *      -FDT_ERR_BADLAYOUT,
1634  *      -FDT_ERR_TRUNCATED, standard meanings
1635  */
1636 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1637                                      const char *name, uint64_t val)
1638 {
1639         fdt64_t tmp = cpu_to_fdt64(val);
1640         return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1641 }
1642
1643 /**
1644  * fdt_appendprop_cell - append a single cell value to a property
1645  *
1646  * This is an alternative name for fdt_appendprop_u32()
1647  */
1648 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1649                                       const char *name, uint32_t val)
1650 {
1651         return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1652 }
1653
1654 /**
1655  * fdt_appendprop_string - append a string to a property
1656  * @fdt: pointer to the device tree blob
1657  * @nodeoffset: offset of the node whose property to change
1658  * @name: name of the property to change
1659  * @str: string value to append to the property
1660  *
1661  * fdt_appendprop_string() appends the given string to the value of
1662  * the named property in the given node, or creates a new property
1663  * with that value if it does not already exist.
1664  *
1665  * This function may insert data into the blob, and will therefore
1666  * change the offsets of some existing nodes.
1667  *
1668  * returns:
1669  *      0, on success
1670  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1671  *              contain the new property value
1672  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1673  *      -FDT_ERR_BADLAYOUT,
1674  *      -FDT_ERR_BADMAGIC,
1675  *      -FDT_ERR_BADVERSION,
1676  *      -FDT_ERR_BADSTATE,
1677  *      -FDT_ERR_BADSTRUCTURE,
1678  *      -FDT_ERR_BADLAYOUT,
1679  *      -FDT_ERR_TRUNCATED, standard meanings
1680  */
1681 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1682         fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1683
1684 /**
1685  * fdt_delprop - delete a property
1686  * @fdt: pointer to the device tree blob
1687  * @nodeoffset: offset of the node whose property to nop
1688  * @name: name of the property to nop
1689  *
1690  * fdt_del_property() will delete the given property.
1691  *
1692  * This function will delete data from the blob, and will therefore
1693  * change the offsets of some existing nodes.
1694  *
1695  * returns:
1696  *      0, on success
1697  *      -FDT_ERR_NOTFOUND, node does not have the named property
1698  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1699  *      -FDT_ERR_BADLAYOUT,
1700  *      -FDT_ERR_BADMAGIC,
1701  *      -FDT_ERR_BADVERSION,
1702  *      -FDT_ERR_BADSTATE,
1703  *      -FDT_ERR_BADSTRUCTURE,
1704  *      -FDT_ERR_TRUNCATED, standard meanings
1705  */
1706 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1707
1708 /**
1709  * fdt_add_subnode_namelen - creates a new node based on substring
1710  * @fdt: pointer to the device tree blob
1711  * @parentoffset: structure block offset of a node
1712  * @name: name of the subnode to locate
1713  * @namelen: number of characters of name to consider
1714  *
1715  * Identical to fdt_add_subnode(), but use only the first namelen
1716  * characters of name as the name of the new node.  This is useful for
1717  * creating subnodes based on a portion of a larger string, such as a
1718  * full path.
1719  */
1720 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1721                             const char *name, int namelen);
1722
1723 /**
1724  * fdt_add_subnode - creates a new node
1725  * @fdt: pointer to the device tree blob
1726  * @parentoffset: structure block offset of a node
1727  * @name: name of the subnode to locate
1728  *
1729  * fdt_add_subnode() creates a new node as a subnode of the node at
1730  * structure block offset parentoffset, with the given name (which
1731  * should include the unit address, if any).
1732  *
1733  * This function will insert data into the blob, and will therefore
1734  * change the offsets of some existing nodes.
1735
1736  * returns:
1737  *      structure block offset of the created nodeequested subnode (>=0), on
1738  *              success
1739  *      -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1740  *      -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
1741  *              tag
1742  *      -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1743  *              the given name
1744  *      -FDT_ERR_NOSPACE, if there is insufficient free space in the
1745  *              blob to contain the new node
1746  *      -FDT_ERR_NOSPACE
1747  *      -FDT_ERR_BADLAYOUT
1748  *      -FDT_ERR_BADMAGIC,
1749  *      -FDT_ERR_BADVERSION,
1750  *      -FDT_ERR_BADSTATE,
1751  *      -FDT_ERR_BADSTRUCTURE,
1752  *      -FDT_ERR_TRUNCATED, standard meanings.
1753  */
1754 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1755
1756 /**
1757  * fdt_del_node - delete a node (subtree)
1758  * @fdt: pointer to the device tree blob
1759  * @nodeoffset: offset of the node to nop
1760  *
1761  * fdt_del_node() will remove the given node, including all its
1762  * subnodes if any, from the blob.
1763  *
1764  * This function will delete data from the blob, and will therefore
1765  * change the offsets of some existing nodes.
1766  *
1767  * returns:
1768  *      0, on success
1769  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1770  *      -FDT_ERR_BADLAYOUT,
1771  *      -FDT_ERR_BADMAGIC,
1772  *      -FDT_ERR_BADVERSION,
1773  *      -FDT_ERR_BADSTATE,
1774  *      -FDT_ERR_BADSTRUCTURE,
1775  *      -FDT_ERR_TRUNCATED, standard meanings
1776  */
1777 int fdt_del_node(void *fdt, int nodeoffset);
1778
1779 /**
1780  * fdt_overlay_apply - Applies a DT overlay on a base DT
1781  * @fdt: pointer to the base device tree blob
1782  * @fdto: pointer to the device tree overlay blob
1783  *
1784  * fdt_overlay_apply() will apply the given device tree overlay on the
1785  * given base device tree.
1786  *
1787  * Expect the base device tree to be modified, even if the function
1788  * returns an error.
1789  *
1790  * returns:
1791  *      0, on success
1792  *      -FDT_ERR_NOSPACE, there's not enough space in the base device tree
1793  *      -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or
1794  *              properties in the base DT
1795  *      -FDT_ERR_BADPHANDLE, the phandles in the overlay do not have the right
1796  *              magic
1797  *      -FDT_ERR_INTERNAL,
1798  *      -FDT_ERR_BADLAYOUT,
1799  *      -FDT_ERR_BADMAGIC,
1800  *      -FDT_ERR_BADOFFSET,
1801  *      -FDT_ERR_BADPATH,
1802  *      -FDT_ERR_BADVERSION,
1803  *      -FDT_ERR_BADSTRUCTURE,
1804  *      -FDT_ERR_BADSTATE,
1805  *      -FDT_ERR_TRUNCATED, standard meanings
1806  */
1807 int fdt_overlay_apply(void *fdt, void *fdto);
1808
1809 /**********************************************************************/
1810 /* Debugging / informational functions                                */
1811 /**********************************************************************/
1812
1813 const char *fdt_strerror(int errval);
1814
1815 /**
1816  * fdt_remove_unused_strings() - Remove any unused strings from an FDT
1817  *
1818  * This creates a new device tree in @new with unused strings removed. The
1819  * called can then use fdt_pack() to minimise the space consumed.
1820  *
1821  * @old:        Old device tree blog
1822  * @new:        Place to put new device tree blob, which must be as large as
1823  *              @old
1824  * @return
1825  *      0, on success
1826  *      -FDT_ERR_BADOFFSET, corrupt device tree
1827  *      -FDT_ERR_NOSPACE, out of space, which should not happen unless there
1828  *              is something very wrong with the device tree input
1829  */
1830 int fdt_remove_unused_strings(const void *old, void *new);
1831
1832 struct fdt_region {
1833         int offset;
1834         int size;
1835 };
1836
1837 /*
1838  * Flags for fdt_find_regions()
1839  *
1840  * Add a region for the string table (always the last region)
1841  */
1842 #define FDT_REG_ADD_STRING_TAB          (1 << 0)
1843
1844 /*
1845  * Add all supernodes of a matching node/property, useful for creating a
1846  * valid subset tree
1847  */
1848 #define FDT_REG_SUPERNODES              (1 << 1)
1849
1850 /* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
1851 #define FDT_REG_DIRECT_SUBNODES (1 << 2)
1852
1853 /* Add all subnodes of a matching node */
1854 #define FDT_REG_ALL_SUBNODES            (1 << 3)
1855
1856 /* Add a region for the mem_rsvmap table (always the first region) */
1857 #define FDT_REG_ADD_MEM_RSVMAP          (1 << 4)
1858
1859 /* Indicates what an fdt part is (node, property, value) */
1860 #define FDT_IS_NODE                     (1 << 0)
1861 #define FDT_IS_PROP                     (1 << 1)
1862 #define FDT_IS_VALUE                    (1 << 2)        /* not supported */
1863 #define FDT_IS_COMPAT                   (1 << 3)        /* used internally */
1864 #define FDT_NODE_HAS_PROP               (1 << 4)        /* node contains prop */
1865
1866 #define FDT_ANY_GLOBAL          (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
1867                                         FDT_IS_COMPAT)
1868 #define FDT_IS_ANY                      0x1f            /* all the above */
1869
1870 /* We set a reasonable limit on the number of nested nodes */
1871 #define FDT_MAX_DEPTH                   32
1872
1873 /* Decribes what we want to include from the current tag */
1874 enum want_t {
1875         WANT_NOTHING,
1876         WANT_NODES_ONLY,                /* No properties */
1877         WANT_NODES_AND_PROPS,           /* Everything for one level */
1878         WANT_ALL_NODES_AND_PROPS        /* Everything for all levels */
1879 };
1880
1881 /* Keeps track of the state at parent nodes */
1882 struct fdt_subnode_stack {
1883         int offset;             /* Offset of node */
1884         enum want_t want;       /* The 'want' value here */
1885         int included;           /* 1 if we included this node, 0 if not */
1886 };
1887
1888 struct fdt_region_ptrs {
1889         int depth;                      /* Current tree depth */
1890         int done;                       /* What we have completed scanning */
1891         enum want_t want;               /* What we are currently including */
1892         char *end;                      /* Pointer to end of full node path */
1893         int nextoffset;                 /* Next node offset to check */
1894 };
1895
1896 /* The state of our finding algortihm */
1897 struct fdt_region_state {
1898         struct fdt_subnode_stack stack[FDT_MAX_DEPTH];  /* node stack */
1899         struct fdt_region *region;      /* Contains list of regions found */
1900         int count;                      /* Numnber of regions found */
1901         const void *fdt;                /* FDT blob */
1902         int max_regions;                /* Maximum regions to find */
1903         int can_merge;          /* 1 if we can merge with previous region */
1904         int start;                      /* Start position of current region */
1905         struct fdt_region_ptrs ptrs;    /* Pointers for what we are up to */
1906 };
1907
1908 /**
1909  * fdt_find_regions() - find regions in device tree
1910  *
1911  * Given a list of nodes to include and properties to exclude, find
1912  * the regions of the device tree which describe those included parts.
1913  *
1914  * The intent is to get a list of regions which will be invariant provided
1915  * those parts are invariant. For example, if you request a list of regions
1916  * for all nodes but exclude the property "data", then you will get the
1917  * same region contents regardless of any change to "data" properties.
1918  *
1919  * This function can be used to produce a byte-stream to send to a hashing
1920  * function to verify that critical parts of the FDT have not changed.
1921  *
1922  * Nodes which are given in 'inc' are included in the region list, as
1923  * are the names of the immediate subnodes nodes (but not the properties
1924  * or subnodes of those subnodes).
1925  *
1926  * For eaxample "/" means to include the root node, all root properties
1927  * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1928  * ensures that we capture the names of the subnodes. In a hashing situation
1929  * it prevents the root node from changing at all Any change to non-excluded
1930  * properties, names of subnodes or number of subnodes would be detected.
1931  *
1932  * When used with FITs this provides the ability to hash and sign parts of
1933  * the FIT based on different configurations in the FIT. Then it is
1934  * impossible to change anything about that configuration (include images
1935  * attached to the configuration), but it may be possible to add new
1936  * configurations, new images or new signatures within the existing
1937  * framework.
1938  *
1939  * Adding new properties to a device tree may result in the string table
1940  * being extended (if the new property names are different from those
1941  * already added). This function can optionally include a region for
1942  * the string table so that this can be part of the hash too.
1943  *
1944  * The device tree header is not included in the list.
1945  *
1946  * @fdt:        Device tree to check
1947  * @inc:        List of node paths to included
1948  * @inc_count:  Number of node paths in list
1949  * @exc_prop:   List of properties names to exclude
1950  * @exc_prop_count:     Number of properties in exclude list
1951  * @region:     Returns list of regions
1952  * @max_region: Maximum length of region list
1953  * @path:       Pointer to a temporary string for the function to use for
1954  *              building path names
1955  * @path_len:   Length of path, must be large enough to hold the longest
1956  *              path in the tree
1957  * @add_string_tab:     1 to add a region for the string table
1958  * @return number of regions in list. If this is >max_regions then the
1959  * region array was exhausted. You should increase max_regions and try
1960  * the call again.
1961  */
1962 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
1963                      char * const exc_prop[], int exc_prop_count,
1964                      struct fdt_region region[], int max_regions,
1965                      char *path, int path_len, int add_string_tab);
1966
1967 /**
1968  * fdt_first_region() - find regions in device tree
1969  *
1970  * Given a nodes and properties to include and properties to exclude, find
1971  * the regions of the device tree which describe those included parts.
1972  *
1973  * The use for this function is twofold. Firstly it provides a convenient
1974  * way of performing a structure-aware grep of the tree. For example it is
1975  * possible to grep for a node and get all the properties associated with
1976  * that node. Trees can be subsetted easily, by specifying the nodes that
1977  * are required, and then writing out the regions returned by this function.
1978  * This is useful for small resource-constrained systems, such as boot
1979  * loaders, which want to use an FDT but do not need to know about all of
1980  * it.
1981  *
1982  * Secondly it makes it easy to hash parts of the tree and detect changes.
1983  * The intent is to get a list of regions which will be invariant provided
1984  * those parts are invariant. For example, if you request a list of regions
1985  * for all nodes but exclude the property "data", then you will get the
1986  * same region contents regardless of any change to "data" properties.
1987  *
1988  * This function can be used to produce a byte-stream to send to a hashing
1989  * function to verify that critical parts of the FDT have not changed.
1990  * Note that semantically null changes in order could still cause false
1991  * hash misses. Such reordering might happen if the tree is regenerated
1992  * from source, and nodes are reordered (the bytes-stream will be emitted
1993  * in a different order and mnay hash functions will detect this). However
1994  * if an existing tree is modified using libfdt functions, such as
1995  * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
1996  *
1997  * The nodes/properties to include/exclude are defined by a function
1998  * provided by the caller. This function is called for each node and
1999  * property, and must return:
2000  *
2001  *    0 - to exclude this part
2002  *    1 - to include this part
2003  *   -1 - for FDT_IS_PROP only: no information is available, so include
2004  *              if its containing node is included
2005  *
2006  * The last case is only used to deal with properties. Often a property is
2007  * included if its containing node is included - this is the case where
2008  * -1 is returned.. However if the property is specifically required to be
2009  * included/excluded, then 0 or 1 can be returned. Note that including a
2010  * property when the FDT_REG_SUPERNODES flag is given will force its
2011  * containing node to be included since it is not valid to have a property
2012  * that is not in a node.
2013  *
2014  * Using the information provided, the inclusion of a node can be controlled
2015  * either by a node name or its compatible string, or any other property
2016  * that the function can determine.
2017  *
2018  * As an example, including node "/" means to include the root node and all
2019  * root properties. A flag provides a way of also including supernodes (of
2020  * which there is none for the root node), and another flag includes
2021  * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
2022  * FDT_END_NODE of all subnodes of /.
2023  *
2024  * The subnode feature helps in a hashing situation since it prevents the
2025  * root node from changing at all. Any change to non-excluded properties,
2026  * names of subnodes or number of subnodes would be detected.
2027  *
2028  * When used with FITs this provides the ability to hash and sign parts of
2029  * the FIT based on different configurations in the FIT. Then it is
2030  * impossible to change anything about that configuration (include images
2031  * attached to the configuration), but it may be possible to add new
2032  * configurations, new images or new signatures within the existing
2033  * framework.
2034  *
2035  * Adding new properties to a device tree may result in the string table
2036  * being extended (if the new property names are different from those
2037  * already added). This function can optionally include a region for
2038  * the string table so that this can be part of the hash too. This is always
2039  * the last region.
2040  *
2041  * The FDT also has a mem_rsvmap table which can also be included, and is
2042  * always the first region if so.
2043  *
2044  * The device tree header is not included in the region list. Since the
2045  * contents of the FDT are changing (shrinking, often), the caller will need
2046  * to regenerate the header anyway.
2047  *
2048  * @fdt:        Device tree to check
2049  * @h_include:  Function to call to determine whether to include a part or
2050  *              not:
2051  *
2052  *              @priv: Private pointer as passed to fdt_find_regions()
2053  *              @fdt: Pointer to FDT blob
2054  *              @offset: Offset of this node / property
2055  *              @type: Type of this part, FDT_IS_...
2056  *              @data: Pointer to data (node name, property name, compatible
2057  *                      string, value (not yet supported)
2058  *              @size: Size of data, or 0 if none
2059  *              @return 0 to exclude, 1 to include, -1 if no information is
2060  *              available
2061  * @priv:       Private pointer passed to h_include
2062  * @region:     Returns list of regions, sorted by offset
2063  * @max_regions: Maximum length of region list
2064  * @path:       Pointer to a temporary string for the function to use for
2065  *              building path names
2066  * @path_len:   Length of path, must be large enough to hold the longest
2067  *              path in the tree
2068  * @flags:      Various flags that control the region algortihm, see
2069  *              FDT_REG_...
2070  * @return number of regions in list. If this is >max_regions then the
2071  * region array was exhausted. You should increase max_regions and try
2072  * the call again. Only the first max_regions elements are available in the
2073  * array.
2074  *
2075  * On error a -ve value is return, which can be:
2076  *
2077  *      -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
2078  *      -FDT_ERR_BADLAYOUT
2079  *      -FDT_ERR_NOSPACE (path area is too small)
2080  */
2081 int fdt_first_region(const void *fdt,
2082                 int (*h_include)(void *priv, const void *fdt, int offset,
2083                                  int type, const char *data, int size),
2084                 void *priv, struct fdt_region *region,
2085                 char *path, int path_len, int flags,
2086                 struct fdt_region_state *info);
2087
2088 /** fdt_next_region() - find next region
2089  *
2090  * See fdt_first_region() for full description. This function finds the
2091  * next region according to the provided parameters, which must be the same
2092  * as passed to fdt_first_region().
2093  *
2094  * This function can additionally return -FDT_ERR_NOTFOUND when there are no
2095  * more regions
2096  */
2097 int fdt_next_region(const void *fdt,
2098                 int (*h_include)(void *priv, const void *fdt, int offset,
2099                                  int type, const char *data, int size),
2100                 void *priv, struct fdt_region *region,
2101                 char *path, int path_len, int flags,
2102                 struct fdt_region_state *info);
2103
2104 /**
2105  * fdt_add_alias_regions() - find aliases that point to existing regions
2106  *
2107  * Once a device tree grep is complete some of the nodes will be present
2108  * and some will have been dropped. This function checks all the alias nodes
2109  * to figure out which points point to nodes which are still present. These
2110  * aliases need to be kept, along with the nodes they reference.
2111  *
2112  * Given a list of regions function finds the aliases that still apply and
2113  * adds more regions to the list for these. This function is called after
2114  * fdt_next_region() has finished returning regions and requires the same
2115  * state.
2116  *
2117  * @fdt:        Device tree file to reference
2118  * @region:     List of regions that will be kept
2119  * @count:      Number of regions
2120  * @max_regions: Number of entries that can fit in @region
2121  * @info:       Region state as returned from fdt_next_region()
2122  * @return new number of regions in @region (i.e. count + the number added)
2123  * or -FDT_ERR_NOSPACE if there was not enough space.
2124  */
2125 int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
2126                           int max_regions, struct fdt_region_state *info);
2127
2128 #endif /* _LIBFDT_H */