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