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