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