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