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