]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/open-amp.git/blob - obsolete/system/zynq7/linux/src/libfdt/libfdt.h
Move libs/ to obsolete/
[processor-sdk/open-amp.git] / obsolete / system / zynq7 / linux / src / 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  */
54 #include "types.h"
55 #include "fdt.h"
57 #define FDT_FIRST_SUPPORTED_VERSION     0x10
58 #define FDT_LAST_SUPPORTED_VERSION      0x11
60 /* Error codes: informative error codes */
61 #define FDT_ERR_NOTFOUND        1
62         /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
63 #define FDT_ERR_EXISTS          2
64         /* FDT_ERR_EXISTS: Attemped to create a node or property which
65          * already exists */
66 #define FDT_ERR_NOSPACE         3
67         /* FDT_ERR_NOSPACE: Operation needed to expand the device
68          * tree, but its buffer did not have sufficient space to
69          * contain the expanded tree. Use fdt_open_into() to move the
70          * device tree to a buffer with more space. */
72 /* Error codes: codes for bad parameters */
73 #define FDT_ERR_BADOFFSET       4
74         /* FDT_ERR_BADOFFSET: Function was passed a structure block
75          * offset which is out-of-bounds, or which points to an
76          * unsuitable part of the structure for the operation. */
77 #define FDT_ERR_BADPATH         5
78         /* FDT_ERR_BADPATH: Function was passed a badly formatted path
79          * (e.g. missing a leading / for a function which requires an
80          * absolute path) */
81 #define FDT_ERR_BADPHANDLE      6
82         /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle
83          * value.  phandle values of 0 and -1 are not permitted. */
84 #define FDT_ERR_BADSTATE        7
85         /* FDT_ERR_BADSTATE: Function was passed an incomplete device
86          * tree created by the sequential-write functions, which is
87          * not sufficiently complete for the requested operation. */
89 /* Error codes: codes for bad device tree blobs */
90 #define FDT_ERR_TRUNCATED       8
91         /* FDT_ERR_TRUNCATED: Structure block of the given device tree
92          * ends without an FDT_END tag. */
93 #define FDT_ERR_BADMAGIC        9
94         /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
95          * device tree at all - it is missing the flattened device
96          * tree magic number. */
97 #define FDT_ERR_BADVERSION      10
98         /* FDT_ERR_BADVERSION: Given device tree has a version which
99          * can't be handled by the requested operation.  For
100          * read-write functions, this may mean that fdt_open_into() is
101          * required to convert the tree to the expected version. */
102 #define FDT_ERR_BADSTRUCTURE    11
103         /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
104          * structure block or other serious error (e.g. misnested
105          * nodes, or subnodes preceding properties). */
106 #define FDT_ERR_BADLAYOUT       12
107         /* FDT_ERR_BADLAYOUT: For read-write functions, the given
108          * device tree has it's sub-blocks in an order that the
109          * function can't handle (memory reserve map, then structure,
110          * then strings).  Use fdt_open_into() to reorganize the tree
111          * into a form suitable for the read-write operations. */
113 /* "Can't happen" error indicating a bug in libfdt */
114 #define FDT_ERR_INTERNAL        13
115         /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
116          * Should never be returned, if it is, it indicates a bug in
117          * libfdt itself. */
119 #define FDT_ERR_MAX             13
121 /**********************************************************************/
122 /* Low-level functions (you probably don't need these)                */
123 /**********************************************************************/
125 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
126 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
128         return (void *)(uintptr_t) fdt_offset_ptr(fdt, offset, checklen);
131 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
133 /**********************************************************************/
134 /* Traversal functions                                                */
135 /**********************************************************************/
137 int fdt_next_node(const void *fdt, int offset, int *depth);
139 /**
140  * fdt_first_subnode() - get offset of first direct subnode
141  *
142  * @fdt:        FDT blob
143  * @offset:     Offset of node to check
144  * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
145  */
146 int fdt_first_subnode(const void *fdt, int offset);
148 /**
149  * fdt_next_subnode() - get offset of next direct subnode
150  *
151  * After first calling fdt_first_subnode(), call this function repeatedly to
152  * get direct subnodes of a parent node.
153  *
154  * @fdt:        FDT blob
155  * @offset:     Offset of previous subnode
156  * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
157  * subnodes
158  */
159 int fdt_next_subnode(const void *fdt, int offset);
161 /**********************************************************************/
162 /* General functions                                                  */
163 /**********************************************************************/
165 #define fdt_get_header(fdt, field) \
166         (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
167 #define fdt_magic(fdt)                  (fdt_get_header(fdt, magic))
168 #define fdt_totalsize(fdt)              (fdt_get_header(fdt, totalsize))
169 #define fdt_off_dt_struct(fdt)          (fdt_get_header(fdt, off_dt_struct))
170 #define fdt_off_dt_strings(fdt)         (fdt_get_header(fdt, off_dt_strings))
171 #define fdt_off_mem_rsvmap(fdt)         (fdt_get_header(fdt, off_mem_rsvmap))
172 #define fdt_version(fdt)                (fdt_get_header(fdt, version))
173 #define fdt_last_comp_version(fdt)      (fdt_get_header(fdt, last_comp_version))
174 #define fdt_boot_cpuid_phys(fdt)        (fdt_get_header(fdt, boot_cpuid_phys))
175 #define fdt_size_dt_strings(fdt)        (fdt_get_header(fdt, size_dt_strings))
176 #define fdt_size_dt_struct(fdt)         (fdt_get_header(fdt, size_dt_struct))
178 #define __fdt_set_hdr(name) \
179         static inline void fdt_set_##name(void *fdt, uint32_t val) \
180         { \
181                 struct fdt_header *fdth = (struct fdt_header*)fdt; \
182                 fdth->name = cpu_to_fdt32(val); \
183         }
184 __fdt_set_hdr(magic);
185 __fdt_set_hdr(totalsize);
186 __fdt_set_hdr(off_dt_struct);
187 __fdt_set_hdr(off_dt_strings);
188 __fdt_set_hdr(off_mem_rsvmap);
189 __fdt_set_hdr(version);
190 __fdt_set_hdr(last_comp_version);
191 __fdt_set_hdr(boot_cpuid_phys);
192 __fdt_set_hdr(size_dt_strings);
193 __fdt_set_hdr(size_dt_struct);
194 #undef __fdt_set_hdr
196 /**
197  * fdt_check_header - sanity check a device tree or possible device tree
198  * @fdt: pointer to data which might be a flattened device tree
199  *
200  * fdt_check_header() checks that the given buffer contains what
201  * appears to be a flattened device tree with sane information in its
202  * header.
203  *
204  * returns:
205  *     0, if the buffer appears to contain a valid device tree
206  *     -FDT_ERR_BADMAGIC,
207  *     -FDT_ERR_BADVERSION,
208  *     -FDT_ERR_BADSTATE, standard meanings, as above
209  */
210 int fdt_check_header(const void *fdt);
212 /**
213  * fdt_move - move a device tree around in memory
214  * @fdt: pointer to the device tree to move
215  * @buf: pointer to memory where the device is to be moved
216  * @bufsize: size of the memory space at buf
217  *
218  * fdt_move() relocates, if possible, the device tree blob located at
219  * fdt to the buffer at buf of size bufsize.  The buffer may overlap
220  * with the existing device tree blob at fdt.  Therefore,
221  *     fdt_move(fdt, fdt, fdt_totalsize(fdt))
222  * should always succeed.
223  *
224  * returns:
225  *     0, on success
226  *     -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
227  *     -FDT_ERR_BADMAGIC,
228  *     -FDT_ERR_BADVERSION,
229  *     -FDT_ERR_BADSTATE, standard meanings
230  */
231 int fdt_move(const void *fdt, void *buf, int bufsize);
233 /**********************************************************************/
234 /* Read-only functions                                                */
235 /**********************************************************************/
237 /**
238  * fdt_string - retrieve a string from the strings block of a device tree
239  * @fdt: pointer to the device tree blob
240  * @stroffset: offset of the string within the strings block (native endian)
241  *
242  * fdt_string() retrieves a pointer to a single string from the
243  * strings block of the device tree blob at fdt.
244  *
245  * returns:
246  *     a pointer to the string, on success
247  *     NULL, if stroffset is out of bounds
248  */
249 const char *fdt_string(const void *fdt, int stroffset);
251 /**
252  * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
253  * @fdt: pointer to the device tree blob
254  *
255  * Returns the number of entries in the device tree blob's memory
256  * reservation map.  This does not include the terminating 0,0 entry
257  * or any other (0,0) entries reserved for expansion.
258  *
259  * returns:
260  *     the number of entries
261  */
262 int fdt_num_mem_rsv(const void *fdt);
264 /**
265  * fdt_get_mem_rsv - retrieve one memory reserve map entry
266  * @fdt: pointer to the device tree blob
267  * @address, @size: pointers to 64-bit variables
268  *
269  * On success, *address and *size will contain the address and size of
270  * the n-th reserve map entry from the device tree blob, in
271  * native-endian format.
272  *
273  * returns:
274  *     0, on success
275  *     -FDT_ERR_BADMAGIC,
276  *     -FDT_ERR_BADVERSION,
277  *     -FDT_ERR_BADSTATE, standard meanings
278  */
279 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t * address,
280                     uint64_t * size);
282 /**
283  * fdt_subnode_offset_namelen - find a subnode based on substring
284  * @fdt: pointer to the device tree blob
285  * @parentoffset: structure block offset of a node
286  * @name: name of the subnode to locate
287  * @namelen: number of characters of name to consider
288  *
289  * Identical to fdt_subnode_offset(), but only examine the first
290  * namelen characters of name for matching the subnode name.  This is
291  * useful for finding subnodes based on a portion of a larger string,
292  * such as a full path.
293  */
294 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
295                                const char *name, int namelen);
296 /**
297  * fdt_subnode_offset - find a subnode of a given node
298  * @fdt: pointer to the device tree blob
299  * @parentoffset: structure block offset of a node
300  * @name: name of the subnode to locate
301  *
302  * fdt_subnode_offset() finds a subnode of the node at structure block
303  * offset parentoffset with the given name.  name may include a unit
304  * address, in which case fdt_subnode_offset() will find the subnode
305  * with that unit address, or the unit address may be omitted, in
306  * which case fdt_subnode_offset() will find an arbitrary subnode
307  * whose name excluding unit address matches the given name.
308  *
309  * returns:
310  *      structure block offset of the requested subnode (>=0), on success
311  *      -FDT_ERR_NOTFOUND, if the requested subnode does not exist
312  *      -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
313  *      -FDT_ERR_BADMAGIC,
314  *      -FDT_ERR_BADVERSION,
315  *      -FDT_ERR_BADSTATE,
316  *      -FDT_ERR_BADSTRUCTURE,
317  *      -FDT_ERR_TRUNCATED, standard meanings.
318  */
319 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
321 /**
322  * fdt_path_offset - find a tree node by its full path
323  * @fdt: pointer to the device tree blob
324  * @path: full path of the node to locate
325  *
326  * fdt_path_offset() finds a node of a given path in the device tree.
327  * Each path component may omit the unit address portion, but the
328  * results of this are undefined if any such path component is
329  * ambiguous (that is if there are multiple nodes at the relevant
330  * level matching the given component, differentiated only by unit
331  * address).
332  *
333  * returns:
334  *      structure block offset of the node with the requested path (>=0), on success
335  *      -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
336  *      -FDT_ERR_NOTFOUND, if the requested node does not exist
337  *      -FDT_ERR_BADMAGIC,
338  *      -FDT_ERR_BADVERSION,
339  *      -FDT_ERR_BADSTATE,
340  *      -FDT_ERR_BADSTRUCTURE,
341  *      -FDT_ERR_TRUNCATED, standard meanings.
342  */
343 int fdt_path_offset(const void *fdt, const char *path);
345 /**
346  * fdt_get_name - retrieve the name of a given node
347  * @fdt: pointer to the device tree blob
348  * @nodeoffset: structure block offset of the starting node
349  * @lenp: pointer to an integer variable (will be overwritten) or NULL
350  *
351  * fdt_get_name() retrieves the name (including unit address) of the
352  * device tree node at structure block offset nodeoffset.  If lenp is
353  * non-NULL, the length of this name is also returned, in the integer
354  * pointed to by lenp.
355  *
356  * returns:
357  *      pointer to the node's name, on success
358  *              If lenp is non-NULL, *lenp contains the length of that name (>=0)
359  *      NULL, on error
360  *              if lenp is non-NULL *lenp contains an error code (<0):
361  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
362  *              -FDT_ERR_BADMAGIC,
363  *              -FDT_ERR_BADVERSION,
364  *              -FDT_ERR_BADSTATE, standard meanings
365  */
366 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
368 /**
369  * fdt_first_property_offset - find the offset of a node's first property
370  * @fdt: pointer to the device tree blob
371  * @nodeoffset: structure block offset of a node
372  *
373  * fdt_first_property_offset() finds the first property of the node at
374  * the given structure block offset.
375  *
376  * returns:
377  *      structure block offset of the property (>=0), on success
378  *      -FDT_ERR_NOTFOUND, if the requested node has no properties
379  *      -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
380  *      -FDT_ERR_BADMAGIC,
381  *      -FDT_ERR_BADVERSION,
382  *      -FDT_ERR_BADSTATE,
383  *      -FDT_ERR_BADSTRUCTURE,
384  *      -FDT_ERR_TRUNCATED, standard meanings.
385  */
386 int fdt_first_property_offset(const void *fdt, int nodeoffset);
388 /**
389  * fdt_next_property_offset - step through a node's properties
390  * @fdt: pointer to the device tree blob
391  * @offset: structure block offset of a property
392  *
393  * fdt_next_property_offset() finds the property immediately after the
394  * one at the given structure block offset.  This will be a property
395  * of the same node as the given property.
396  *
397  * returns:
398  *      structure block offset of the next property (>=0), on success
399  *      -FDT_ERR_NOTFOUND, if the given property is the last in its node
400  *      -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
401  *      -FDT_ERR_BADMAGIC,
402  *      -FDT_ERR_BADVERSION,
403  *      -FDT_ERR_BADSTATE,
404  *      -FDT_ERR_BADSTRUCTURE,
405  *      -FDT_ERR_TRUNCATED, standard meanings.
406  */
407 int fdt_next_property_offset(const void *fdt, int offset);
409 /**
410  * fdt_get_property_by_offset - retrieve the property at a given offset
411  * @fdt: pointer to the device tree blob
412  * @offset: offset of the property to retrieve
413  * @lenp: pointer to an integer variable (will be overwritten) or NULL
414  *
415  * fdt_get_property_by_offset() retrieves a pointer to the
416  * fdt_property structure within the device tree blob at the given
417  * offset.  If lenp is non-NULL, the length of the property value is
418  * also returned, in the integer pointed to by lenp.
419  *
420  * returns:
421  *      pointer to the structure representing the property
422  *              if lenp is non-NULL, *lenp contains the length of the property
423  *              value (>=0)
424  *      NULL, on error
425  *              if lenp is non-NULL, *lenp contains an error code (<0):
426  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
427  *              -FDT_ERR_BADMAGIC,
428  *              -FDT_ERR_BADVERSION,
429  *              -FDT_ERR_BADSTATE,
430  *              -FDT_ERR_BADSTRUCTURE,
431  *              -FDT_ERR_TRUNCATED, standard meanings
432  */
433 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
434                                                       int offset, int *lenp);
436 /**
437  * fdt_get_property_namelen - find a property based on substring
438  * @fdt: pointer to the device tree blob
439  * @nodeoffset: offset of the node whose property to find
440  * @name: name of the property to find
441  * @namelen: number of characters of name to consider
442  * @lenp: pointer to an integer variable (will be overwritten) or NULL
443  *
444  * Identical to fdt_get_property_namelen(), but only examine the first
445  * namelen characters of name for matching the property name.
446  */
447 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
448                                                     int nodeoffset,
449                                                     const char *name,
450                                                     int namelen, int *lenp);
452 /**
453  * fdt_get_property - find a given property in a given node
454  * @fdt: pointer to the device tree blob
455  * @nodeoffset: offset of the node whose property to find
456  * @name: name of the property to find
457  * @lenp: pointer to an integer variable (will be overwritten) or NULL
458  *
459  * fdt_get_property() retrieves a pointer to the fdt_property
460  * structure within the device tree blob corresponding to the property
461  * named 'name' of the node at offset nodeoffset.  If lenp is
462  * non-NULL, the length of the property value is also returned, in the
463  * integer pointed to by lenp.
464  *
465  * returns:
466  *      pointer to the structure representing the property
467  *              if lenp is non-NULL, *lenp contains the length of the property
468  *              value (>=0)
469  *      NULL, on error
470  *              if lenp is non-NULL, *lenp contains an error code (<0):
471  *              -FDT_ERR_NOTFOUND, node does not have named property
472  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
473  *              -FDT_ERR_BADMAGIC,
474  *              -FDT_ERR_BADVERSION,
475  *              -FDT_ERR_BADSTATE,
476  *              -FDT_ERR_BADSTRUCTURE,
477  *              -FDT_ERR_TRUNCATED, standard meanings
478  */
479 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
480                                             const char *name, int *lenp);
481 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
482                                                       const char *name,
483                                                       int *lenp)
485         return (struct fdt_property *)(uintptr_t)
486             fdt_get_property(fdt, nodeoffset, name, lenp);
489 /**
490  * fdt_getprop_by_offset - retrieve the value of a property at a given offset
491  * @fdt: pointer to the device tree blob
492  * @ffset: offset of the property to read
493  * @namep: pointer to a string variable (will be overwritten) or NULL
494  * @lenp: pointer to an integer variable (will be overwritten) or NULL
495  *
496  * fdt_getprop_by_offset() retrieves a pointer to the value of the
497  * property at structure block offset 'offset' (this will be a pointer
498  * to within the device blob itself, not a copy of the value).  If
499  * lenp is non-NULL, the length of the property value is also
500  * returned, in the integer pointed to by lenp.  If namep is non-NULL,
501  * the property's namne will also be returned in the char * pointed to
502  * by namep (this will be a pointer to within the device tree's string
503  * block, not a new copy of the name).
504  *
505  * returns:
506  *      pointer to the property's value
507  *              if lenp is non-NULL, *lenp contains the length of the property
508  *              value (>=0)
509  *              if namep is non-NULL *namep contiains a pointer to the property
510  *              name.
511  *      NULL, on error
512  *              if lenp is non-NULL, *lenp contains an error code (<0):
513  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
514  *              -FDT_ERR_BADMAGIC,
515  *              -FDT_ERR_BADVERSION,
516  *              -FDT_ERR_BADSTATE,
517  *              -FDT_ERR_BADSTRUCTURE,
518  *              -FDT_ERR_TRUNCATED, standard meanings
519  */
520 const void *fdt_getprop_by_offset(const void *fdt, int offset,
521                                   const char **namep, int *lenp);
523 /**
524  * fdt_getprop_namelen - get property value based on substring
525  * @fdt: pointer to the device tree blob
526  * @nodeoffset: offset of the node whose property to find
527  * @name: name of the property to find
528  * @namelen: number of characters of name to consider
529  * @lenp: pointer to an integer variable (will be overwritten) or NULL
530  *
531  * Identical to fdt_getprop(), but only examine the first namelen
532  * characters of name for matching the property name.
533  */
534 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
535                                 const char *name, int namelen, int *lenp);
537 /**
538  * fdt_getprop - retrieve the value of a given property
539  * @fdt: pointer to the device tree blob
540  * @nodeoffset: offset of the node whose property to find
541  * @name: name of the property to find
542  * @lenp: pointer to an integer variable (will be overwritten) or NULL
543  *
544  * fdt_getprop() retrieves a pointer to the value of the property
545  * named 'name' of the node at offset nodeoffset (this will be a
546  * pointer to within the device blob itself, not a copy of the value).
547  * If lenp is non-NULL, the length of the property value is also
548  * returned, in the integer pointed to by lenp.
549  *
550  * returns:
551  *      pointer to the property's value
552  *              if lenp is non-NULL, *lenp contains the length of the property
553  *              value (>=0)
554  *      NULL, on error
555  *              if lenp is non-NULL, *lenp contains an error code (<0):
556  *              -FDT_ERR_NOTFOUND, node does not have named property
557  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
558  *              -FDT_ERR_BADMAGIC,
559  *              -FDT_ERR_BADVERSION,
560  *              -FDT_ERR_BADSTATE,
561  *              -FDT_ERR_BADSTRUCTURE,
562  *              -FDT_ERR_TRUNCATED, standard meanings
563  */
564 const void *fdt_getprop(const void *fdt, int nodeoffset,
565                         const char *name, int *lenp);
566 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
567                                   const char *name, int *lenp)
569         return (void *)(uintptr_t) fdt_getprop(fdt, nodeoffset, name, lenp);
572 /**
573  * fdt_get_phandle - retrieve the phandle of a given node
574  * @fdt: pointer to the device tree blob
575  * @nodeoffset: structure block offset of the node
576  *
577  * fdt_get_phandle() retrieves the phandle of the device tree node at
578  * structure block offset nodeoffset.
579  *
580  * returns:
581  *      the phandle of the node at nodeoffset, on success (!= 0, != -1)
582  *      0, if the node has no phandle, or another error occurs
583  */
584 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
586 /**
587  * fdt_get_alias_namelen - get alias based on substring
588  * @fdt: pointer to the device tree blob
589  * @name: name of the alias th look up
590  * @namelen: number of characters of name to consider
591  *
592  * Identical to fdt_get_alias(), but only examine the first namelen
593  * characters of name for matching the alias name.
594  */
595 const char *fdt_get_alias_namelen(const void *fdt,
596                                   const char *name, int namelen);
598 /**
599  * fdt_get_alias - retreive the path referenced by a given alias
600  * @fdt: pointer to the device tree blob
601  * @name: name of the alias th look up
602  *
603  * fdt_get_alias() retrieves the value of a given alias.  That is, the
604  * value of the property named 'name' in the node /aliases.
605  *
606  * returns:
607  *      a pointer to the expansion of the alias named 'name', if it exists
608  *      NULL, if the given alias or the /aliases node does not exist
609  */
610 const char *fdt_get_alias(const void *fdt, const char *name);
612 /**
613  * fdt_get_path - determine the full path of a node
614  * @fdt: pointer to the device tree blob
615  * @nodeoffset: offset of the node whose path to find
616  * @buf: character buffer to contain the returned path (will be overwritten)
617  * @buflen: size of the character buffer at buf
618  *
619  * fdt_get_path() computes the full path of the node at offset
620  * nodeoffset, and records that path in the buffer at buf.
621  *
622  * NOTE: This function is expensive, as it must scan the device tree
623  * structure from the start to nodeoffset.
624  *
625  * returns:
626  *      0, on success
627  *              buf contains the absolute path of the node at
628  *              nodeoffset, as a NUL-terminated string.
629  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
630  *      -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
631  *              characters and will not fit in the given buffer.
632  *      -FDT_ERR_BADMAGIC,
633  *      -FDT_ERR_BADVERSION,
634  *      -FDT_ERR_BADSTATE,
635  *      -FDT_ERR_BADSTRUCTURE, standard meanings
636  */
637 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
639 /**
640  * fdt_supernode_atdepth_offset - find a specific ancestor of a node
641  * @fdt: pointer to the device tree blob
642  * @nodeoffset: offset of the node whose parent to find
643  * @supernodedepth: depth of the ancestor to find
644  * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
645  *
646  * fdt_supernode_atdepth_offset() finds an ancestor of the given node
647  * at a specific depth from the root (where the root itself has depth
648  * 0, its immediate subnodes depth 1 and so forth).  So
649  *      fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
650  * will always return 0, the offset of the root node.  If the node at
651  * nodeoffset has depth D, then:
652  *      fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
653  * will return nodeoffset itself.
654  *
655  * NOTE: This function is expensive, as it must scan the device tree
656  * structure from the start to nodeoffset.
657  *
658  * returns:
660  *      structure block offset of the node at node offset's ancestor
661  *              of depth supernodedepth (>=0), on success
662  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
663 *       -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
664  *      -FDT_ERR_BADMAGIC,
665  *      -FDT_ERR_BADVERSION,
666  *      -FDT_ERR_BADSTATE,
667  *      -FDT_ERR_BADSTRUCTURE, standard meanings
668  */
669 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
670                                  int supernodedepth, int *nodedepth);
672 /**
673  * fdt_node_depth - find the depth of a given node
674  * @fdt: pointer to the device tree blob
675  * @nodeoffset: offset of the node whose parent to find
676  *
677  * fdt_node_depth() finds the depth of a given node.  The root node
678  * has depth 0, its immediate subnodes depth 1 and so forth.
679  *
680  * NOTE: This function is expensive, as it must scan the device tree
681  * structure from the start to nodeoffset.
682  *
683  * returns:
684  *      depth of the node at nodeoffset (>=0), on success
685  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
686  *      -FDT_ERR_BADMAGIC,
687  *      -FDT_ERR_BADVERSION,
688  *      -FDT_ERR_BADSTATE,
689  *      -FDT_ERR_BADSTRUCTURE, standard meanings
690  */
691 int fdt_node_depth(const void *fdt, int nodeoffset);
693 /**
694  * fdt_parent_offset - find the parent of a given node
695  * @fdt: pointer to the device tree blob
696  * @nodeoffset: offset of the node whose parent to find
697  *
698  * fdt_parent_offset() locates the parent node of a given node (that
699  * is, it finds the offset of the node which contains the node at
700  * nodeoffset as a subnode).
701  *
702  * NOTE: This function is expensive, as it must scan the device tree
703  * structure from the start to nodeoffset, *twice*.
704  *
705  * returns:
706  *      structure block offset of the parent of the node at nodeoffset
707  *              (>=0), on success
708  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
709  *      -FDT_ERR_BADMAGIC,
710  *      -FDT_ERR_BADVERSION,
711  *      -FDT_ERR_BADSTATE,
712  *      -FDT_ERR_BADSTRUCTURE, standard meanings
713  */
714 int fdt_parent_offset(const void *fdt, int nodeoffset);
716 /**
717  * fdt_node_offset_by_prop_value - find nodes with a given property value
718  * @fdt: pointer to the device tree blob
719  * @startoffset: only find nodes after this offset
720  * @propname: property name to check
721  * @propval: property value to search for
722  * @proplen: length of the value in propval
723  *
724  * fdt_node_offset_by_prop_value() returns the offset of the first
725  * node after startoffset, which has a property named propname whose
726  * value is of length proplen and has value equal to propval; or if
727  * startoffset is -1, the very first such node in the tree.
728  *
729  * To iterate through all nodes matching the criterion, the following
730  * idiom can be used:
731  *      offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
732  *                                             propval, proplen);
733  *      while (offset != -FDT_ERR_NOTFOUND) {
734  *              ... other code here ...
735  *              offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
736  *                                                     propval, proplen);
737  *      }
738  *
739  * Note the -1 in the first call to the function, if 0 is used here
740  * instead, the function will never locate the root node, even if it
741  * matches the criterion.
742  *
743  * returns:
744  *      structure block offset of the located node (>= 0, >startoffset),
745  *               on success
746  *      -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
747  *              tree after startoffset
748  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
749  *      -FDT_ERR_BADMAGIC,
750  *      -FDT_ERR_BADVERSION,
751  *      -FDT_ERR_BADSTATE,
752  *      -FDT_ERR_BADSTRUCTURE, standard meanings
753  */
754 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
755                                   const char *propname,
756                                   const void *propval, int proplen);
758 /**
759  * fdt_node_offset_by_phandle - find the node with a given phandle
760  * @fdt: pointer to the device tree blob
761  * @phandle: phandle value
762  *
763  * fdt_node_offset_by_phandle() returns the offset of the node
764  * which has the given phandle value.  If there is more than one node
765  * in the tree with the given phandle (an invalid tree), results are
766  * undefined.
767  *
768  * returns:
769  *      structure block offset of the located node (>= 0), on success
770  *      -FDT_ERR_NOTFOUND, no node with that phandle exists
771  *      -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
772  *      -FDT_ERR_BADMAGIC,
773  *      -FDT_ERR_BADVERSION,
774  *      -FDT_ERR_BADSTATE,
775  *      -FDT_ERR_BADSTRUCTURE, standard meanings
776  */
777 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
779 /**
780  * fdt_node_check_compatible: check a node's compatible property
781  * @fdt: pointer to the device tree blob
782  * @nodeoffset: offset of a tree node
783  * @compatible: string to match against
784  *
785  *
786  * fdt_node_check_compatible() returns 0 if the given node contains a
787  * 'compatible' property with the given string as one of its elements,
788  * it returns non-zero otherwise, or on error.
789  *
790  * returns:
791  *      0, if the node has a 'compatible' property listing the given string
792  *      1, if the node has a 'compatible' property, but it does not list
793  *              the given string
794  *      -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
795  *      -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
796  *      -FDT_ERR_BADMAGIC,
797  *      -FDT_ERR_BADVERSION,
798  *      -FDT_ERR_BADSTATE,
799  *      -FDT_ERR_BADSTRUCTURE, standard meanings
800  */
801 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
802                               const char *compatible);
804 /**
805  * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
806  * @fdt: pointer to the device tree blob
807  * @startoffset: only find nodes after this offset
808  * @compatible: 'compatible' string to match against
809  *
810  * fdt_node_offset_by_compatible() returns the offset of the first
811  * node after startoffset, which has a 'compatible' property which
812  * lists the given compatible string; or if startoffset is -1, the
813  * very first such node in the tree.
814  *
815  * To iterate through all nodes matching the criterion, the following
816  * idiom can be used:
817  *      offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
818  *      while (offset != -FDT_ERR_NOTFOUND) {
819  *              ... other code here ...
820  *              offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
821  *      }
822  *
823  * Note the -1 in the first call to the function, if 0 is used here
824  * instead, the function will never locate the root node, even if it
825  * matches the criterion.
826  *
827  * returns:
828  *      structure block offset of the located node (>= 0, >startoffset),
829  *               on success
830  *      -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
831  *              tree after startoffset
832  *      -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
833  *      -FDT_ERR_BADMAGIC,
834  *      -FDT_ERR_BADVERSION,
835  *      -FDT_ERR_BADSTATE,
836  *      -FDT_ERR_BADSTRUCTURE, standard meanings
837  */
838 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
839                                   const char *compatible);
841 /**
842  * fdt_stringlist_contains - check a string list property for a string
843  * @strlist: Property containing a list of strings to check
844  * @listlen: Length of property
845  * @str: String to search for
846  *
847  * This is a utility function provided for convenience. The list contains
848  * one or more strings, each terminated by \0, as is found in a device tree
849  * "compatible" property.
850  *
851  * @return: 1 if the string is found in the list, 0 not found, or invalid list
852  */
853 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
855 /**********************************************************************/
856 /* Write-in-place functions                                           */
857 /**********************************************************************/
859 /**
860  * fdt_setprop_inplace - change a property's value, but not its size
861  * @fdt: pointer to the device tree blob
862  * @nodeoffset: offset of the node whose property to change
863  * @name: name of the property to change
864  * @val: pointer to data to replace the property value with
865  * @len: length of the property value
866  *
867  * fdt_setprop_inplace() replaces the value of a given property with
868  * the data in val, of length len.  This function cannot change the
869  * size of a property, and so will only work if len is equal to the
870  * current length of the property.
871  *
872  * This function will alter only the bytes in the blob which contain
873  * the given property value, and will not alter or move any other part
874  * of the tree.
875  *
876  * returns:
877  *      0, on success
878  *      -FDT_ERR_NOSPACE, if len is not equal to the property's current length
879  *      -FDT_ERR_NOTFOUND, node does not have the named property
880  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
881  *      -FDT_ERR_BADMAGIC,
882  *      -FDT_ERR_BADVERSION,
883  *      -FDT_ERR_BADSTATE,
884  *      -FDT_ERR_BADSTRUCTURE,
885  *      -FDT_ERR_TRUNCATED, standard meanings
886  */
887 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
888                         const void *val, int len);
890 /**
891  * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
892  * @fdt: pointer to the device tree blob
893  * @nodeoffset: offset of the node whose property to change
894  * @name: name of the property to change
895  * @val: 32-bit integer value to replace the property with
896  *
897  * fdt_setprop_inplace_u32() replaces the value of a given property
898  * with the 32-bit integer value in val, converting val to big-endian
899  * if necessary.  This function cannot change the size of a property,
900  * and so will only work if the property already exists and has length
901  * 4.
902  *
903  * This function will alter only the bytes in the blob which contain
904  * the given property value, and will not alter or move any other part
905  * of the tree.
906  *
907  * returns:
908  *      0, on success
909  *      -FDT_ERR_NOSPACE, if the property's length is not equal to 4
910  *      -FDT_ERR_NOTFOUND, node does not have the named property
911  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
912  *      -FDT_ERR_BADMAGIC,
913  *      -FDT_ERR_BADVERSION,
914  *      -FDT_ERR_BADSTATE,
915  *      -FDT_ERR_BADSTRUCTURE,
916  *      -FDT_ERR_TRUNCATED, standard meanings
917  */
918 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
919                                           const char *name, uint32_t val)
921         fdt32_t tmp = cpu_to_fdt32(val);
922         return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
925 /**
926  * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
927  * @fdt: pointer to the device tree blob
928  * @nodeoffset: offset of the node whose property to change
929  * @name: name of the property to change
930  * @val: 64-bit integer value to replace the property with
931  *
932  * fdt_setprop_inplace_u64() replaces the value of a given property
933  * with the 64-bit integer value in val, converting val to big-endian
934  * if necessary.  This function cannot change the size of a property,
935  * and so will only work if the property already exists and has length
936  * 8.
937  *
938  * This function will alter only the bytes in the blob which contain
939  * the given property value, and will not alter or move any other part
940  * of the tree.
941  *
942  * returns:
943  *      0, on success
944  *      -FDT_ERR_NOSPACE, if the property's length is not equal to 8
945  *      -FDT_ERR_NOTFOUND, node does not have the named property
946  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
947  *      -FDT_ERR_BADMAGIC,
948  *      -FDT_ERR_BADVERSION,
949  *      -FDT_ERR_BADSTATE,
950  *      -FDT_ERR_BADSTRUCTURE,
951  *      -FDT_ERR_TRUNCATED, standard meanings
952  */
953 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
954                                           const char *name, uint64_t val)
956         fdt64_t tmp = cpu_to_fdt64(val);
957         return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
960 /**
961  * fdt_setprop_inplace_cell - change the value of a single-cell property
962  *
963  * This is an alternative name for fdt_setprop_inplace_u32()
964  */
965 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
966                                            const char *name, uint32_t val)
968         return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
971 /**
972  * fdt_nop_property - replace a property with nop tags
973  * @fdt: pointer to the device tree blob
974  * @nodeoffset: offset of the node whose property to nop
975  * @name: name of the property to nop
976  *
977  * fdt_nop_property() will replace a given property's representation
978  * in the blob with FDT_NOP tags, effectively removing it from the
979  * tree.
980  *
981  * This function will alter only the bytes in the blob which contain
982  * the property, and will not alter or move any other part of the
983  * tree.
984  *
985  * returns:
986  *      0, on success
987  *      -FDT_ERR_NOTFOUND, node does not have the named property
988  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
989  *      -FDT_ERR_BADMAGIC,
990  *      -FDT_ERR_BADVERSION,
991  *      -FDT_ERR_BADSTATE,
992  *      -FDT_ERR_BADSTRUCTURE,
993  *      -FDT_ERR_TRUNCATED, standard meanings
994  */
995 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
997 /**
998  * fdt_nop_node - replace a node (subtree) with nop tags
999  * @fdt: pointer to the device tree blob
1000  * @nodeoffset: offset of the node to nop
1001  *
1002  * fdt_nop_node() will replace a given node's representation in the
1003  * blob, including all its subnodes, if any, with FDT_NOP tags,
1004  * effectively removing it from the tree.
1005  *
1006  * This function will alter only the bytes in the blob which contain
1007  * the node and its properties and subnodes, and will not alter or
1008  * move any other part of the tree.
1009  *
1010  * returns:
1011  *      0, on success
1012  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1013  *      -FDT_ERR_BADMAGIC,
1014  *      -FDT_ERR_BADVERSION,
1015  *      -FDT_ERR_BADSTATE,
1016  *      -FDT_ERR_BADSTRUCTURE,
1017  *      -FDT_ERR_TRUNCATED, standard meanings
1018  */
1019 int fdt_nop_node(void *fdt, int nodeoffset);
1021 /**********************************************************************/
1022 /* Sequential write functions                                         */
1023 /**********************************************************************/
1025 int fdt_create(void *buf, int bufsize);
1026 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1027 int fdt_finish_reservemap(void *fdt);
1028 int fdt_begin_node(void *fdt, const char *name);
1029 int fdt_property(void *fdt, const char *name, const void *val, int len);
1030 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1032         fdt32_t tmp = cpu_to_fdt32(val);
1033         return fdt_property(fdt, name, &tmp, sizeof(tmp));
1036 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1038         fdt64_t tmp = cpu_to_fdt64(val);
1039         return fdt_property(fdt, name, &tmp, sizeof(tmp));
1042 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1044         return fdt_property_u32(fdt, name, val);
1047 #define fdt_property_string(fdt, name, str) \
1048         fdt_property(fdt, name, str, strlen(str)+1)
1049 int fdt_end_node(void *fdt);
1050 int fdt_finish(void *fdt);
1052 /**********************************************************************/
1053 /* Read-write functions                                               */
1054 /**********************************************************************/
1056 int fdt_create_empty_tree(void *buf, int bufsize);
1057 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1058 int fdt_pack(void *fdt);
1060 /**
1061  * fdt_add_mem_rsv - add one memory reserve map entry
1062  * @fdt: pointer to the device tree blob
1063  * @address, @size: 64-bit values (native endian)
1064  *
1065  * Adds a reserve map entry to the given blob reserving a region at
1066  * address address of length size.
1067  *
1068  * This function will insert data into the reserve map and will
1069  * therefore change the indexes of some entries in the table.
1070  *
1071  * returns:
1072  *      0, on success
1073  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1074  *              contain the new reservation entry
1075  *      -FDT_ERR_BADMAGIC,
1076  *      -FDT_ERR_BADVERSION,
1077  *      -FDT_ERR_BADSTATE,
1078  *      -FDT_ERR_BADSTRUCTURE,
1079  *      -FDT_ERR_BADLAYOUT,
1080  *      -FDT_ERR_TRUNCATED, standard meanings
1081  */
1082 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1084 /**
1085  * fdt_del_mem_rsv - remove a memory reserve map entry
1086  * @fdt: pointer to the device tree blob
1087  * @n: entry to remove
1088  *
1089  * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1090  * the blob.
1091  *
1092  * This function will delete data from the reservation table and will
1093  * therefore change the indexes of some entries in the table.
1094  *
1095  * returns:
1096  *      0, on success
1097  *      -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1098  *              are less than n+1 reserve map entries)
1099  *      -FDT_ERR_BADMAGIC,
1100  *      -FDT_ERR_BADVERSION,
1101  *      -FDT_ERR_BADSTATE,
1102  *      -FDT_ERR_BADSTRUCTURE,
1103  *      -FDT_ERR_BADLAYOUT,
1104  *      -FDT_ERR_TRUNCATED, standard meanings
1105  */
1106 int fdt_del_mem_rsv(void *fdt, int n);
1108 /**
1109  * fdt_set_name - change the name of a given node
1110  * @fdt: pointer to the device tree blob
1111  * @nodeoffset: structure block offset of a node
1112  * @name: name to give the node
1113  *
1114  * fdt_set_name() replaces the name (including unit address, if any)
1115  * of the given node with the given string.  NOTE: this function can't
1116  * efficiently check if the new name is unique amongst the given
1117  * node's siblings; results are undefined if this function is invoked
1118  * with a name equal to one of the given node's siblings.
1119  *
1120  * This function may insert or delete data from the blob, and will
1121  * therefore change the offsets of some existing nodes.
1122  *
1123  * returns:
1124  *      0, on success
1125  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1126  *              to contain the new name
1127  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1128  *      -FDT_ERR_BADMAGIC,
1129  *      -FDT_ERR_BADVERSION,
1130  *      -FDT_ERR_BADSTATE, standard meanings
1131  */
1132 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1134 /**
1135  * fdt_setprop - create or change a property
1136  * @fdt: pointer to the device tree blob
1137  * @nodeoffset: offset of the node whose property to change
1138  * @name: name of the property to change
1139  * @val: pointer to data to set the property value to
1140  * @len: length of the property value
1141  *
1142  * fdt_setprop() sets the value of the named property in the given
1143  * node to the given value and length, creating the property if it
1144  * does not already exist.
1145  *
1146  * This function may insert or delete data from the blob, and will
1147  * therefore change the offsets of some existing nodes.
1148  *
1149  * returns:
1150  *      0, on success
1151  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1152  *              contain the new property value
1153  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1154  *      -FDT_ERR_BADLAYOUT,
1155  *      -FDT_ERR_BADMAGIC,
1156  *      -FDT_ERR_BADVERSION,
1157  *      -FDT_ERR_BADSTATE,
1158  *      -FDT_ERR_BADSTRUCTURE,
1159  *      -FDT_ERR_BADLAYOUT,
1160  *      -FDT_ERR_TRUNCATED, standard meanings
1161  */
1162 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1163                 const void *val, int len);
1165 /**
1166  * fdt_setprop_u32 - set a property to a 32-bit integer
1167  * @fdt: pointer to the device tree blob
1168  * @nodeoffset: offset of the node whose property to change
1169  * @name: name of the property to change
1170  * @val: 32-bit integer value for the property (native endian)
1171  *
1172  * fdt_setprop_u32() sets the value of the named property in the given
1173  * node to the given 32-bit integer value (converting to big-endian if
1174  * necessary), or creates a new property with that value if it does
1175  * not already exist.
1176  *
1177  * This function may insert or delete data from the blob, and will
1178  * therefore change the offsets of some existing nodes.
1179  *
1180  * returns:
1181  *      0, on success
1182  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1183  *              contain the new property value
1184  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1185  *      -FDT_ERR_BADLAYOUT,
1186  *      -FDT_ERR_BADMAGIC,
1187  *      -FDT_ERR_BADVERSION,
1188  *      -FDT_ERR_BADSTATE,
1189  *      -FDT_ERR_BADSTRUCTURE,
1190  *      -FDT_ERR_BADLAYOUT,
1191  *      -FDT_ERR_TRUNCATED, standard meanings
1192  */
1193 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1194                                   uint32_t val)
1196         fdt32_t tmp = cpu_to_fdt32(val);
1197         return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1200 /**
1201  * fdt_setprop_u64 - set a property to a 64-bit integer
1202  * @fdt: pointer to the device tree blob
1203  * @nodeoffset: offset of the node whose property to change
1204  * @name: name of the property to change
1205  * @val: 64-bit integer value for the property (native endian)
1206  *
1207  * fdt_setprop_u64() sets the value of the named property in the given
1208  * node to the given 64-bit integer value (converting to big-endian if
1209  * necessary), or creates a new property with that value if it does
1210  * not already exist.
1211  *
1212  * This function may insert or delete data from the blob, and will
1213  * therefore change the offsets of some existing nodes.
1214  *
1215  * returns:
1216  *      0, on success
1217  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1218  *              contain the new property value
1219  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1220  *      -FDT_ERR_BADLAYOUT,
1221  *      -FDT_ERR_BADMAGIC,
1222  *      -FDT_ERR_BADVERSION,
1223  *      -FDT_ERR_BADSTATE,
1224  *      -FDT_ERR_BADSTRUCTURE,
1225  *      -FDT_ERR_BADLAYOUT,
1226  *      -FDT_ERR_TRUNCATED, standard meanings
1227  */
1228 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1229                                   uint64_t val)
1231         fdt64_t tmp = cpu_to_fdt64(val);
1232         return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1235 /**
1236  * fdt_setprop_cell - set a property to a single cell value
1237  *
1238  * This is an alternative name for fdt_setprop_u32()
1239  */
1240 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1241                                    uint32_t val)
1243         return fdt_setprop_u32(fdt, nodeoffset, name, val);
1246 /**
1247  * fdt_setprop_string - set a property to a string value
1248  * @fdt: pointer to the device tree blob
1249  * @nodeoffset: offset of the node whose property to change
1250  * @name: name of the property to change
1251  * @str: string value for the property
1252  *
1253  * fdt_setprop_string() sets the value of the named property in the
1254  * given node to the given string value (using the length of the
1255  * string to determine the new length of the property), or creates a
1256  * new property with that value if it does not already exist.
1257  *
1258  * This function may insert or delete data from the blob, and will
1259  * therefore change the offsets of some existing nodes.
1260  *
1261  * returns:
1262  *      0, on success
1263  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1264  *              contain the new property value
1265  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1266  *      -FDT_ERR_BADLAYOUT,
1267  *      -FDT_ERR_BADMAGIC,
1268  *      -FDT_ERR_BADVERSION,
1269  *      -FDT_ERR_BADSTATE,
1270  *      -FDT_ERR_BADSTRUCTURE,
1271  *      -FDT_ERR_BADLAYOUT,
1272  *      -FDT_ERR_TRUNCATED, standard meanings
1273  */
1274 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1275         fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1277 /**
1278  * fdt_appendprop - append to or create a property
1279  * @fdt: pointer to the device tree blob
1280  * @nodeoffset: offset of the node whose property to change
1281  * @name: name of the property to append to
1282  * @val: pointer to data to append to the property value
1283  * @len: length of the data to append to the property value
1284  *
1285  * fdt_appendprop() appends the value to the named property in the
1286  * given node, creating the property if it does not already exist.
1287  *
1288  * This function may insert data into the blob, and will therefore
1289  * change the offsets of some existing nodes.
1290  *
1291  * returns:
1292  *      0, on success
1293  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1294  *              contain the new property value
1295  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1296  *      -FDT_ERR_BADLAYOUT,
1297  *      -FDT_ERR_BADMAGIC,
1298  *      -FDT_ERR_BADVERSION,
1299  *      -FDT_ERR_BADSTATE,
1300  *      -FDT_ERR_BADSTRUCTURE,
1301  *      -FDT_ERR_BADLAYOUT,
1302  *      -FDT_ERR_TRUNCATED, standard meanings
1303  */
1304 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1305                    const void *val, int len);
1307 /**
1308  * fdt_appendprop_u32 - append a 32-bit integer value to a property
1309  * @fdt: pointer to the device tree blob
1310  * @nodeoffset: offset of the node whose property to change
1311  * @name: name of the property to change
1312  * @val: 32-bit integer value to append to the property (native endian)
1313  *
1314  * fdt_appendprop_u32() appends the given 32-bit integer value
1315  * (converting to big-endian if necessary) to the value of the named
1316  * property in the given node, or creates a new property with that
1317  * value if it does not already exist.
1318  *
1319  * This function may insert data into the blob, and will therefore
1320  * change the offsets of some existing nodes.
1321  *
1322  * returns:
1323  *      0, on success
1324  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1325  *              contain the new property value
1326  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1327  *      -FDT_ERR_BADLAYOUT,
1328  *      -FDT_ERR_BADMAGIC,
1329  *      -FDT_ERR_BADVERSION,
1330  *      -FDT_ERR_BADSTATE,
1331  *      -FDT_ERR_BADSTRUCTURE,
1332  *      -FDT_ERR_BADLAYOUT,
1333  *      -FDT_ERR_TRUNCATED, standard meanings
1334  */
1335 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1336                                      const char *name, uint32_t val)
1338         fdt32_t tmp = cpu_to_fdt32(val);
1339         return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1342 /**
1343  * fdt_appendprop_u64 - append a 64-bit integer value to a property
1344  * @fdt: pointer to the device tree blob
1345  * @nodeoffset: offset of the node whose property to change
1346  * @name: name of the property to change
1347  * @val: 64-bit integer value to append to the property (native endian)
1348  *
1349  * fdt_appendprop_u64() appends the given 64-bit integer value
1350  * (converting to big-endian if necessary) to the value of the named
1351  * property in the given node, or creates a new property with that
1352  * value if it does not already exist.
1353  *
1354  * This function may insert data into the blob, and will therefore
1355  * change the offsets of some existing nodes.
1356  *
1357  * returns:
1358  *      0, on success
1359  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1360  *              contain the new property value
1361  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1362  *      -FDT_ERR_BADLAYOUT,
1363  *      -FDT_ERR_BADMAGIC,
1364  *      -FDT_ERR_BADVERSION,
1365  *      -FDT_ERR_BADSTATE,
1366  *      -FDT_ERR_BADSTRUCTURE,
1367  *      -FDT_ERR_BADLAYOUT,
1368  *      -FDT_ERR_TRUNCATED, standard meanings
1369  */
1370 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1371                                      const char *name, uint64_t val)
1373         fdt64_t tmp = cpu_to_fdt64(val);
1374         return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1377 /**
1378  * fdt_appendprop_cell - append a single cell value to a property
1379  *
1380  * This is an alternative name for fdt_appendprop_u32()
1381  */
1382 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1383                                       const char *name, uint32_t val)
1385         return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1388 /**
1389  * fdt_appendprop_string - append a string to a property
1390  * @fdt: pointer to the device tree blob
1391  * @nodeoffset: offset of the node whose property to change
1392  * @name: name of the property to change
1393  * @str: string value to append to the property
1394  *
1395  * fdt_appendprop_string() appends the given string to the value of
1396  * the named property in the given node, or creates a new property
1397  * with that value if it does not already exist.
1398  *
1399  * This function may insert data into the blob, and will therefore
1400  * change the offsets of some existing nodes.
1401  *
1402  * returns:
1403  *      0, on success
1404  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1405  *              contain the new property value
1406  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1407  *      -FDT_ERR_BADLAYOUT,
1408  *      -FDT_ERR_BADMAGIC,
1409  *      -FDT_ERR_BADVERSION,
1410  *      -FDT_ERR_BADSTATE,
1411  *      -FDT_ERR_BADSTRUCTURE,
1412  *      -FDT_ERR_BADLAYOUT,
1413  *      -FDT_ERR_TRUNCATED, standard meanings
1414  */
1415 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1416         fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1418 /**
1419  * fdt_delprop - delete a property
1420  * @fdt: pointer to the device tree blob
1421  * @nodeoffset: offset of the node whose property to nop
1422  * @name: name of the property to nop
1423  *
1424  * fdt_del_property() will delete the given property.
1425  *
1426  * This function will delete data from the blob, and will therefore
1427  * change the offsets of some existing nodes.
1428  *
1429  * returns:
1430  *      0, on success
1431  *      -FDT_ERR_NOTFOUND, node does not have the named property
1432  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1433  *      -FDT_ERR_BADLAYOUT,
1434  *      -FDT_ERR_BADMAGIC,
1435  *      -FDT_ERR_BADVERSION,
1436  *      -FDT_ERR_BADSTATE,
1437  *      -FDT_ERR_BADSTRUCTURE,
1438  *      -FDT_ERR_TRUNCATED, standard meanings
1439  */
1440 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1442 /**
1443  * fdt_add_subnode_namelen - creates a new node based on substring
1444  * @fdt: pointer to the device tree blob
1445  * @parentoffset: structure block offset of a node
1446  * @name: name of the subnode to locate
1447  * @namelen: number of characters of name to consider
1448  *
1449  * Identical to fdt_add_subnode(), but use only the first namelen
1450  * characters of name as the name of the new node.  This is useful for
1451  * creating subnodes based on a portion of a larger string, such as a
1452  * full path.
1453  */
1454 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1455                             const char *name, int namelen);
1457 /**
1458  * fdt_add_subnode - creates a new node
1459  * @fdt: pointer to the device tree blob
1460  * @parentoffset: structure block offset of a node
1461  * @name: name of the subnode to locate
1462  *
1463  * fdt_add_subnode() creates a new node as a subnode of the node at
1464  * structure block offset parentoffset, with the given name (which
1465  * should include the unit address, if any).
1466  *
1467  * This function will insert data into the blob, and will therefore
1468  * change the offsets of some existing nodes.
1470  * returns:
1471  *      structure block offset of the created nodeequested subnode (>=0), on success
1472  *      -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1473  *      -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1474  *      -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1475  *              the given name
1476  *      -FDT_ERR_NOSPACE, if there is insufficient free space in the
1477  *              blob to contain the new node
1478  *      -FDT_ERR_NOSPACE
1479  *      -FDT_ERR_BADLAYOUT
1480  *      -FDT_ERR_BADMAGIC,
1481  *      -FDT_ERR_BADVERSION,
1482  *      -FDT_ERR_BADSTATE,
1483  *      -FDT_ERR_BADSTRUCTURE,
1484  *      -FDT_ERR_TRUNCATED, standard meanings.
1485  */
1486 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1488 /**
1489  * fdt_del_node - delete a node (subtree)
1490  * @fdt: pointer to the device tree blob
1491  * @nodeoffset: offset of the node to nop
1492  *
1493  * fdt_del_node() will remove the given node, including all its
1494  * subnodes if any, from the blob.
1495  *
1496  * This function will delete data from the blob, and will therefore
1497  * change the offsets of some existing nodes.
1498  *
1499  * returns:
1500  *      0, on success
1501  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1502  *      -FDT_ERR_BADLAYOUT,
1503  *      -FDT_ERR_BADMAGIC,
1504  *      -FDT_ERR_BADVERSION,
1505  *      -FDT_ERR_BADSTATE,
1506  *      -FDT_ERR_BADSTRUCTURE,
1507  *      -FDT_ERR_TRUNCATED, standard meanings
1508  */
1509 int fdt_del_node(void *fdt, int nodeoffset);
1511 /**********************************************************************/
1512 /* Debugging / informational functions                                */
1513 /**********************************************************************/
1515 const char *fdt_strerror(int errval);
1517 struct fdt_region {
1518         int offset;
1519         int size;
1520 };
1522 /**
1523  * fdt_find_regions() - find regions in device tree
1524  *
1525  * Given a list of nodes to include and properties to exclude, find
1526  * the regions of the device tree which describe those included parts.
1527  *
1528  * The intent is to get a list of regions which will be invariant provided
1529  * those parts are invariant. For example, if you request a list of regions
1530  * for all nodes but exclude the property "data", then you will get the
1531  * same region contents regardless of any change to "data" properties.
1532  *
1533  * This function can be used to produce a byte-stream to send to a hashing
1534  * function to verify that critical parts of the FDT have not changed.
1535  *
1536  * Nodes which are given in 'inc' are included in the region list, as
1537  * are the names of the immediate subnodes nodes (but not the properties
1538  * or subnodes of those subnodes).
1539  *
1540  * For eaxample "/" means to include the root node, all root properties
1541  * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1542  * ensures that we capture the names of the subnodes. In a hashing situation
1543  * it prevents the root node from changing at all Any change to non-excluded
1544  * properties, names of subnodes or number of subnodes would be detected.
1545  *
1546  * When used with FITs this provides the ability to hash and sign parts of
1547  * the FIT based on different configurations in the FIT. Then it is
1548  * impossible to change anything about that configuration (include images
1549  * attached to the configuration), but it may be possible to add new
1550  * configurations, new images or new signatures within the existing
1551  * framework.
1552  *
1553  * Adding new properties to a device tree may result in the string table
1554  * being extended (if the new property names are different from those
1555  * already added). This function can optionally include a region for
1556  * the string table so that this can be part of the hash too.
1557  *
1558  * The device tree header is not included in the list.
1559  *
1560  * @fdt:        Device tree to check
1561  * @inc:        List of node paths to included
1562  * @inc_count:  Number of node paths in list
1563  * @exc_prop:   List of properties names to exclude
1564  * @exc_prop_count:     Number of properties in exclude list
1565  * @region:     Returns list of regions
1566  * @max_region: Maximum length of region list
1567  * @path:       Pointer to a temporary string for the function to use for
1568  *              building path names
1569  * @path_len:   Length of path, must be large enough to hold the longest
1570  *              path in the tree
1571  * @add_string_tab:     1 to add a region for the string table
1572  * @return number of regions in list. If this is >max_regions then the
1573  * region array was exhausted. You should increase max_regions and try
1574  * the call again.
1575  */
1576 int fdt_find_regions(const void *fdt, char *const inc[], int inc_count,
1577                      char *const exc_prop[], int exc_prop_count,
1578                      struct fdt_region region[], int max_regions,
1579                      char *path, int path_len, int add_string_tab);
1581 #endif                          /* _LIBFDT_H */