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