]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/dtc/libfdt/libfdt.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / 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     0x10
58 #define FDT_LAST_SUPPORTED_VERSION      0x11
59
60 /* Error codes: informative error codes */
61 #define FDT_ERR_NOTFOUND        1
62         /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
63 #define FDT_ERR_EXISTS          2
64         /* FDT_ERR_EXISTS: Attemped to create a node or property which
65          * already exists */
66 #define FDT_ERR_NOSPACE         3
67         /* FDT_ERR_NOSPACE: Operation needed to expand the device
68          * tree, but its buffer did not have sufficient space to
69          * contain the expanded tree. Use fdt_open_into() to move the
70          * device tree to a buffer with more space. */
71
72 /* Error codes: codes for bad parameters */
73 #define FDT_ERR_BADOFFSET       4
74         /* FDT_ERR_BADOFFSET: Function was passed a structure block
75          * offset which is out-of-bounds, or which points to an
76          * unsuitable part of the structure for the operation. */
77 #define FDT_ERR_BADPATH         5
78         /* FDT_ERR_BADPATH: Function was passed a badly formatted path
79          * (e.g. missing a leading / for a function which requires an
80          * absolute path) */
81 #define FDT_ERR_BADPHANDLE      6
82         /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle
83          * value.  phandle values of 0 and -1 are not permitted. */
84 #define FDT_ERR_BADSTATE        7
85         /* FDT_ERR_BADSTATE: Function was passed an incomplete device
86          * tree created by the sequential-write functions, which is
87          * not sufficiently complete for the requested operation. */
88
89 /* Error codes: codes for bad device tree blobs */
90 #define FDT_ERR_TRUNCATED       8
91         /* FDT_ERR_TRUNCATED: Structure block of the given device tree
92          * ends without an FDT_END tag. */
93 #define FDT_ERR_BADMAGIC        9
94         /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
95          * device tree at all - it is missing the flattened device
96          * tree magic number. */
97 #define FDT_ERR_BADVERSION      10
98         /* FDT_ERR_BADVERSION: Given device tree has a version which
99          * can't be handled by the requested operation.  For
100          * read-write functions, this may mean that fdt_open_into() is
101          * required to convert the tree to the expected version. */
102 #define FDT_ERR_BADSTRUCTURE    11
103         /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
104          * structure block or other serious error (e.g. misnested
105          * nodes, or subnodes preceding properties). */
106 #define FDT_ERR_BADLAYOUT       12
107         /* FDT_ERR_BADLAYOUT: For read-write functions, the given
108          * device tree has it's sub-blocks in an order that the
109          * function can't handle (memory reserve map, then structure,
110          * then strings).  Use fdt_open_into() to reorganize the tree
111          * into a form suitable for the read-write operations. */
112
113 /* "Can't happen" error indicating a bug in libfdt */
114 #define FDT_ERR_INTERNAL        13
115         /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
116          * Should never be returned, if it is, it indicates a bug in
117          * libfdt itself. */
118
119 #define FDT_ERR_MAX             13
120
121 /**********************************************************************/
122 /* Low-level functions (you probably don't need these)                */
123 /**********************************************************************/
124
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)
127 {
128         return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
129 }
130
131 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
132
133 /**********************************************************************/
134 /* Traversal functions                                                */
135 /**********************************************************************/
136
137 int fdt_next_node(const void *fdt, int offset, int *depth);
138
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);
147
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);
160
161 /**********************************************************************/
162 /* General functions                                                  */
163 /**********************************************************************/
164
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))
177
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
195
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);
211
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);
232
233 /**********************************************************************/
234 /* Read-only functions                                                */
235 /**********************************************************************/
236
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);
250
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);
263
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, uint64_t *size);
280
281 /**
282  * fdt_subnode_offset_namelen - find a subnode based on substring
283  * @fdt: pointer to the device tree blob
284  * @parentoffset: structure block offset of a node
285  * @name: name of the subnode to locate
286  * @namelen: number of characters of name to consider
287  *
288  * Identical to fdt_subnode_offset(), but only examine the first
289  * namelen characters of name for matching the subnode name.  This is
290  * useful for finding subnodes based on a portion of a larger string,
291  * such as a full path.
292  */
293 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
294                                const char *name, int namelen);
295 /**
296  * fdt_subnode_offset - find a subnode of a given node
297  * @fdt: pointer to the device tree blob
298  * @parentoffset: structure block offset of a node
299  * @name: name of the subnode to locate
300  *
301  * fdt_subnode_offset() finds a subnode of the node at structure block
302  * offset parentoffset with the given name.  name may include a unit
303  * address, in which case fdt_subnode_offset() will find the subnode
304  * with that unit address, or the unit address may be omitted, in
305  * which case fdt_subnode_offset() will find an arbitrary subnode
306  * whose name excluding unit address matches the given name.
307  *
308  * returns:
309  *      structure block offset of the requested subnode (>=0), on success
310  *      -FDT_ERR_NOTFOUND, if the requested subnode does not exist
311  *      -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
312  *      -FDT_ERR_BADMAGIC,
313  *      -FDT_ERR_BADVERSION,
314  *      -FDT_ERR_BADSTATE,
315  *      -FDT_ERR_BADSTRUCTURE,
316  *      -FDT_ERR_TRUNCATED, standard meanings.
317  */
318 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
319
320 /**
321  * fdt_path_offset - find a tree node by its full path
322  * @fdt: pointer to the device tree blob
323  * @path: full path of the node to locate
324  *
325  * fdt_path_offset() finds a node of a given path in the device tree.
326  * Each path component may omit the unit address portion, but the
327  * results of this are undefined if any such path component is
328  * ambiguous (that is if there are multiple nodes at the relevant
329  * level matching the given component, differentiated only by unit
330  * address).
331  *
332  * returns:
333  *      structure block offset of the node with the requested path (>=0), on success
334  *      -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
335  *      -FDT_ERR_NOTFOUND, if the requested node does not exist
336  *      -FDT_ERR_BADMAGIC,
337  *      -FDT_ERR_BADVERSION,
338  *      -FDT_ERR_BADSTATE,
339  *      -FDT_ERR_BADSTRUCTURE,
340  *      -FDT_ERR_TRUNCATED, standard meanings.
341  */
342 int fdt_path_offset(const void *fdt, const char *path);
343
344 /**
345  * fdt_get_name - retrieve the name of a given node
346  * @fdt: pointer to the device tree blob
347  * @nodeoffset: structure block offset of the starting node
348  * @lenp: pointer to an integer variable (will be overwritten) or NULL
349  *
350  * fdt_get_name() retrieves the name (including unit address) of the
351  * device tree node at structure block offset nodeoffset.  If lenp is
352  * non-NULL, the length of this name is also returned, in the integer
353  * pointed to by lenp.
354  *
355  * returns:
356  *      pointer to the node's name, on success
357  *              If lenp is non-NULL, *lenp contains the length of that name (>=0)
358  *      NULL, on error
359  *              if lenp is non-NULL *lenp contains an error code (<0):
360  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
361  *              -FDT_ERR_BADMAGIC,
362  *              -FDT_ERR_BADVERSION,
363  *              -FDT_ERR_BADSTATE, standard meanings
364  */
365 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
366
367 /**
368  * fdt_first_property_offset - find the offset of a node's first property
369  * @fdt: pointer to the device tree blob
370  * @nodeoffset: structure block offset of a node
371  *
372  * fdt_first_property_offset() finds the first property of the node at
373  * the given structure block offset.
374  *
375  * returns:
376  *      structure block offset of the property (>=0), on success
377  *      -FDT_ERR_NOTFOUND, if the requested node has no properties
378  *      -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
379  *      -FDT_ERR_BADMAGIC,
380  *      -FDT_ERR_BADVERSION,
381  *      -FDT_ERR_BADSTATE,
382  *      -FDT_ERR_BADSTRUCTURE,
383  *      -FDT_ERR_TRUNCATED, standard meanings.
384  */
385 int fdt_first_property_offset(const void *fdt, int nodeoffset);
386
387 /**
388  * fdt_next_property_offset - step through a node's properties
389  * @fdt: pointer to the device tree blob
390  * @offset: structure block offset of a property
391  *
392  * fdt_next_property_offset() finds the property immediately after the
393  * one at the given structure block offset.  This will be a property
394  * of the same node as the given property.
395  *
396  * returns:
397  *      structure block offset of the next property (>=0), on success
398  *      -FDT_ERR_NOTFOUND, if the given property is the last in its node
399  *      -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
400  *      -FDT_ERR_BADMAGIC,
401  *      -FDT_ERR_BADVERSION,
402  *      -FDT_ERR_BADSTATE,
403  *      -FDT_ERR_BADSTRUCTURE,
404  *      -FDT_ERR_TRUNCATED, standard meanings.
405  */
406 int fdt_next_property_offset(const void *fdt, int offset);
407
408 /**
409  * fdt_get_property_by_offset - retrieve the property at a given offset
410  * @fdt: pointer to the device tree blob
411  * @offset: offset of the property to retrieve
412  * @lenp: pointer to an integer variable (will be overwritten) or NULL
413  *
414  * fdt_get_property_by_offset() retrieves a pointer to the
415  * fdt_property structure within the device tree blob at the given
416  * offset.  If lenp is non-NULL, the length of the property value is
417  * also returned, in the integer pointed to by lenp.
418  *
419  * returns:
420  *      pointer to the structure representing the property
421  *              if lenp is non-NULL, *lenp contains the length of the property
422  *              value (>=0)
423  *      NULL, on error
424  *              if lenp is non-NULL, *lenp contains an error code (<0):
425  *              -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
426  *              -FDT_ERR_BADMAGIC,
427  *              -FDT_ERR_BADVERSION,
428  *              -FDT_ERR_BADSTATE,
429  *              -FDT_ERR_BADSTRUCTURE,
430  *              -FDT_ERR_TRUNCATED, standard meanings
431  */
432 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
433                                                       int offset,
434                                                       int *lenp);
435
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);
451
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)
484 {
485         return (struct fdt_property *)(uintptr_t)
486                 fdt_get_property(fdt, nodeoffset, name, lenp);
487 }
488
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);
522
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);
536
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)
568 {
569         return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
570 }
571
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);
585
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);
597
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);
611
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);
638
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:
659
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);
671
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);
692
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);
715
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);
757
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);
778
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);
803
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);
840
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);
854
855 /**********************************************************************/
856 /* Write-in-place functions                                           */
857 /**********************************************************************/
858
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);
889
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)
920 {
921         fdt32_t tmp = cpu_to_fdt32(val);
922         return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
923 }
924
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)
955 {
956         fdt64_t tmp = cpu_to_fdt64(val);
957         return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
958 }
959
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)
967 {
968         return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
969 }
970
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);
996
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);
1020
1021 /**********************************************************************/
1022 /* Sequential write functions                                         */
1023 /**********************************************************************/
1024
1025 int fdt_create(void *buf, int bufsize);
1026 int fdt_resize(void *fdt, void *buf, int bufsize);
1027 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1028 int fdt_finish_reservemap(void *fdt);
1029 int fdt_begin_node(void *fdt, const char *name);
1030 int fdt_property(void *fdt, const char *name, const void *val, int len);
1031 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1032 {
1033         fdt32_t tmp = cpu_to_fdt32(val);
1034         return fdt_property(fdt, name, &tmp, sizeof(tmp));
1035 }
1036 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1037 {
1038         fdt64_t tmp = cpu_to_fdt64(val);
1039         return fdt_property(fdt, name, &tmp, sizeof(tmp));
1040 }
1041 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1042 {
1043         return fdt_property_u32(fdt, name, val);
1044 }
1045 #define fdt_property_string(fdt, name, str) \
1046         fdt_property(fdt, name, str, strlen(str)+1)
1047 int fdt_end_node(void *fdt);
1048 int fdt_finish(void *fdt);
1049
1050 /**********************************************************************/
1051 /* Read-write functions                                               */
1052 /**********************************************************************/
1053
1054 int fdt_create_empty_tree(void *buf, int bufsize);
1055 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1056 int fdt_pack(void *fdt);
1057
1058 /**
1059  * fdt_add_mem_rsv - add one memory reserve map entry
1060  * @fdt: pointer to the device tree blob
1061  * @address, @size: 64-bit values (native endian)
1062  *
1063  * Adds a reserve map entry to the given blob reserving a region at
1064  * address address of length size.
1065  *
1066  * This function will insert data into the reserve map and will
1067  * therefore change the indexes of some entries in the table.
1068  *
1069  * returns:
1070  *      0, on success
1071  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1072  *              contain the new reservation entry
1073  *      -FDT_ERR_BADMAGIC,
1074  *      -FDT_ERR_BADVERSION,
1075  *      -FDT_ERR_BADSTATE,
1076  *      -FDT_ERR_BADSTRUCTURE,
1077  *      -FDT_ERR_BADLAYOUT,
1078  *      -FDT_ERR_TRUNCATED, standard meanings
1079  */
1080 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1081
1082 /**
1083  * fdt_del_mem_rsv - remove a memory reserve map entry
1084  * @fdt: pointer to the device tree blob
1085  * @n: entry to remove
1086  *
1087  * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1088  * the blob.
1089  *
1090  * This function will delete data from the reservation table and will
1091  * therefore change the indexes of some entries in the table.
1092  *
1093  * returns:
1094  *      0, on success
1095  *      -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1096  *              are less than n+1 reserve map entries)
1097  *      -FDT_ERR_BADMAGIC,
1098  *      -FDT_ERR_BADVERSION,
1099  *      -FDT_ERR_BADSTATE,
1100  *      -FDT_ERR_BADSTRUCTURE,
1101  *      -FDT_ERR_BADLAYOUT,
1102  *      -FDT_ERR_TRUNCATED, standard meanings
1103  */
1104 int fdt_del_mem_rsv(void *fdt, int n);
1105
1106 /**
1107  * fdt_set_name - change the name of a given node
1108  * @fdt: pointer to the device tree blob
1109  * @nodeoffset: structure block offset of a node
1110  * @name: name to give the node
1111  *
1112  * fdt_set_name() replaces the name (including unit address, if any)
1113  * of the given node with the given string.  NOTE: this function can't
1114  * efficiently check if the new name is unique amongst the given
1115  * node's siblings; results are undefined if this function is invoked
1116  * with a name equal to one of the given node's siblings.
1117  *
1118  * This function may insert or delete data from the blob, and will
1119  * therefore change the offsets of some existing nodes.
1120  *
1121  * returns:
1122  *      0, on success
1123  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1124  *              to contain the new name
1125  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1126  *      -FDT_ERR_BADMAGIC,
1127  *      -FDT_ERR_BADVERSION,
1128  *      -FDT_ERR_BADSTATE, standard meanings
1129  */
1130 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1131
1132 /**
1133  * fdt_setprop - create or change a property
1134  * @fdt: pointer to the device tree blob
1135  * @nodeoffset: offset of the node whose property to change
1136  * @name: name of the property to change
1137  * @val: pointer to data to set the property value to
1138  * @len: length of the property value
1139  *
1140  * fdt_setprop() sets the value of the named property in the given
1141  * node to the given value and length, creating the property if it
1142  * does not already exist.
1143  *
1144  * This function may insert or delete data from the blob, and will
1145  * therefore change the offsets of some existing nodes.
1146  *
1147  * returns:
1148  *      0, on success
1149  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1150  *              contain the new property value
1151  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1152  *      -FDT_ERR_BADLAYOUT,
1153  *      -FDT_ERR_BADMAGIC,
1154  *      -FDT_ERR_BADVERSION,
1155  *      -FDT_ERR_BADSTATE,
1156  *      -FDT_ERR_BADSTRUCTURE,
1157  *      -FDT_ERR_BADLAYOUT,
1158  *      -FDT_ERR_TRUNCATED, standard meanings
1159  */
1160 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1161                 const void *val, int len);
1162
1163 /**
1164  * fdt_setprop_u32 - set a property to a 32-bit integer
1165  * @fdt: pointer to the device tree blob
1166  * @nodeoffset: offset of the node whose property to change
1167  * @name: name of the property to change
1168  * @val: 32-bit integer value for the property (native endian)
1169  *
1170  * fdt_setprop_u32() sets the value of the named property in the given
1171  * node to the given 32-bit integer value (converting to big-endian if
1172  * necessary), or creates a new property with that value if it does
1173  * not already exist.
1174  *
1175  * This function may insert or delete data from the blob, and will
1176  * therefore change the offsets of some existing nodes.
1177  *
1178  * returns:
1179  *      0, on success
1180  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1181  *              contain the new property value
1182  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1183  *      -FDT_ERR_BADLAYOUT,
1184  *      -FDT_ERR_BADMAGIC,
1185  *      -FDT_ERR_BADVERSION,
1186  *      -FDT_ERR_BADSTATE,
1187  *      -FDT_ERR_BADSTRUCTURE,
1188  *      -FDT_ERR_BADLAYOUT,
1189  *      -FDT_ERR_TRUNCATED, standard meanings
1190  */
1191 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1192                                   uint32_t val)
1193 {
1194         fdt32_t tmp = cpu_to_fdt32(val);
1195         return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1196 }
1197
1198 /**
1199  * fdt_setprop_u64 - set a property to a 64-bit integer
1200  * @fdt: pointer to the device tree blob
1201  * @nodeoffset: offset of the node whose property to change
1202  * @name: name of the property to change
1203  * @val: 64-bit integer value for the property (native endian)
1204  *
1205  * fdt_setprop_u64() sets the value of the named property in the given
1206  * node to the given 64-bit integer value (converting to big-endian if
1207  * necessary), or creates a new property with that value if it does
1208  * not already exist.
1209  *
1210  * This function may insert or delete data from the blob, and will
1211  * therefore change the offsets of some existing nodes.
1212  *
1213  * returns:
1214  *      0, on success
1215  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1216  *              contain the new property value
1217  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1218  *      -FDT_ERR_BADLAYOUT,
1219  *      -FDT_ERR_BADMAGIC,
1220  *      -FDT_ERR_BADVERSION,
1221  *      -FDT_ERR_BADSTATE,
1222  *      -FDT_ERR_BADSTRUCTURE,
1223  *      -FDT_ERR_BADLAYOUT,
1224  *      -FDT_ERR_TRUNCATED, standard meanings
1225  */
1226 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1227                                   uint64_t val)
1228 {
1229         fdt64_t tmp = cpu_to_fdt64(val);
1230         return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1231 }
1232
1233 /**
1234  * fdt_setprop_cell - set a property to a single cell value
1235  *
1236  * This is an alternative name for fdt_setprop_u32()
1237  */
1238 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1239                                    uint32_t val)
1240 {
1241         return fdt_setprop_u32(fdt, nodeoffset, name, val);
1242 }
1243
1244 /**
1245  * fdt_setprop_string - set a property to a string value
1246  * @fdt: pointer to the device tree blob
1247  * @nodeoffset: offset of the node whose property to change
1248  * @name: name of the property to change
1249  * @str: string value for the property
1250  *
1251  * fdt_setprop_string() sets the value of the named property in the
1252  * given node to the given string value (using the length of the
1253  * string to determine the new length of the property), or creates a
1254  * new property with that value if it does not already exist.
1255  *
1256  * This function may insert or delete data from the blob, and will
1257  * therefore change the offsets of some existing nodes.
1258  *
1259  * returns:
1260  *      0, on success
1261  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1262  *              contain the new property value
1263  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1264  *      -FDT_ERR_BADLAYOUT,
1265  *      -FDT_ERR_BADMAGIC,
1266  *      -FDT_ERR_BADVERSION,
1267  *      -FDT_ERR_BADSTATE,
1268  *      -FDT_ERR_BADSTRUCTURE,
1269  *      -FDT_ERR_BADLAYOUT,
1270  *      -FDT_ERR_TRUNCATED, standard meanings
1271  */
1272 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1273         fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1274
1275 /**
1276  * fdt_appendprop - append to or create a property
1277  * @fdt: pointer to the device tree blob
1278  * @nodeoffset: offset of the node whose property to change
1279  * @name: name of the property to append to
1280  * @val: pointer to data to append to the property value
1281  * @len: length of the data to append to the property value
1282  *
1283  * fdt_appendprop() appends the value to the named property in the
1284  * given node, creating the property if it does not already exist.
1285  *
1286  * This function may insert data into the blob, and will therefore
1287  * change the offsets of some existing nodes.
1288  *
1289  * returns:
1290  *      0, on success
1291  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1292  *              contain the new property value
1293  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1294  *      -FDT_ERR_BADLAYOUT,
1295  *      -FDT_ERR_BADMAGIC,
1296  *      -FDT_ERR_BADVERSION,
1297  *      -FDT_ERR_BADSTATE,
1298  *      -FDT_ERR_BADSTRUCTURE,
1299  *      -FDT_ERR_BADLAYOUT,
1300  *      -FDT_ERR_TRUNCATED, standard meanings
1301  */
1302 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1303                    const void *val, int len);
1304
1305 /**
1306  * fdt_appendprop_u32 - append a 32-bit integer value to a property
1307  * @fdt: pointer to the device tree blob
1308  * @nodeoffset: offset of the node whose property to change
1309  * @name: name of the property to change
1310  * @val: 32-bit integer value to append to the property (native endian)
1311  *
1312  * fdt_appendprop_u32() appends the given 32-bit integer value
1313  * (converting to big-endian if necessary) to the value of the named
1314  * property in the given node, or creates a new property with that
1315  * value if it does not already exist.
1316  *
1317  * This function may insert data into the blob, and will therefore
1318  * change the offsets of some existing nodes.
1319  *
1320  * returns:
1321  *      0, on success
1322  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1323  *              contain the new property value
1324  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1325  *      -FDT_ERR_BADLAYOUT,
1326  *      -FDT_ERR_BADMAGIC,
1327  *      -FDT_ERR_BADVERSION,
1328  *      -FDT_ERR_BADSTATE,
1329  *      -FDT_ERR_BADSTRUCTURE,
1330  *      -FDT_ERR_BADLAYOUT,
1331  *      -FDT_ERR_TRUNCATED, standard meanings
1332  */
1333 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1334                                      const char *name, uint32_t val)
1335 {
1336         fdt32_t tmp = cpu_to_fdt32(val);
1337         return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1338 }
1339
1340 /**
1341  * fdt_appendprop_u64 - append a 64-bit integer value to a property
1342  * @fdt: pointer to the device tree blob
1343  * @nodeoffset: offset of the node whose property to change
1344  * @name: name of the property to change
1345  * @val: 64-bit integer value to append to the property (native endian)
1346  *
1347  * fdt_appendprop_u64() appends the given 64-bit integer value
1348  * (converting to big-endian if necessary) to the value of the named
1349  * property in the given node, or creates a new property with that
1350  * value if it does not already exist.
1351  *
1352  * This function may insert data into the blob, and will therefore
1353  * change the offsets of some existing nodes.
1354  *
1355  * returns:
1356  *      0, on success
1357  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1358  *              contain the new property value
1359  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1360  *      -FDT_ERR_BADLAYOUT,
1361  *      -FDT_ERR_BADMAGIC,
1362  *      -FDT_ERR_BADVERSION,
1363  *      -FDT_ERR_BADSTATE,
1364  *      -FDT_ERR_BADSTRUCTURE,
1365  *      -FDT_ERR_BADLAYOUT,
1366  *      -FDT_ERR_TRUNCATED, standard meanings
1367  */
1368 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1369                                      const char *name, uint64_t val)
1370 {
1371         fdt64_t tmp = cpu_to_fdt64(val);
1372         return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1373 }
1374
1375 /**
1376  * fdt_appendprop_cell - append a single cell value to a property
1377  *
1378  * This is an alternative name for fdt_appendprop_u32()
1379  */
1380 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1381                                       const char *name, uint32_t val)
1382 {
1383         return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1384 }
1385
1386 /**
1387  * fdt_appendprop_string - append a string to a property
1388  * @fdt: pointer to the device tree blob
1389  * @nodeoffset: offset of the node whose property to change
1390  * @name: name of the property to change
1391  * @str: string value to append to the property
1392  *
1393  * fdt_appendprop_string() appends the given string to the value of
1394  * the named property in the given node, or creates a new property
1395  * with that value if it does not already exist.
1396  *
1397  * This function may insert data into the blob, and will therefore
1398  * change the offsets of some existing nodes.
1399  *
1400  * returns:
1401  *      0, on success
1402  *      -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1403  *              contain the new property value
1404  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1405  *      -FDT_ERR_BADLAYOUT,
1406  *      -FDT_ERR_BADMAGIC,
1407  *      -FDT_ERR_BADVERSION,
1408  *      -FDT_ERR_BADSTATE,
1409  *      -FDT_ERR_BADSTRUCTURE,
1410  *      -FDT_ERR_BADLAYOUT,
1411  *      -FDT_ERR_TRUNCATED, standard meanings
1412  */
1413 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1414         fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1415
1416 /**
1417  * fdt_delprop - delete a property
1418  * @fdt: pointer to the device tree blob
1419  * @nodeoffset: offset of the node whose property to nop
1420  * @name: name of the property to nop
1421  *
1422  * fdt_del_property() will delete the given property.
1423  *
1424  * This function will delete data from the blob, and will therefore
1425  * change the offsets of some existing nodes.
1426  *
1427  * returns:
1428  *      0, on success
1429  *      -FDT_ERR_NOTFOUND, node does not have the named property
1430  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1431  *      -FDT_ERR_BADLAYOUT,
1432  *      -FDT_ERR_BADMAGIC,
1433  *      -FDT_ERR_BADVERSION,
1434  *      -FDT_ERR_BADSTATE,
1435  *      -FDT_ERR_BADSTRUCTURE,
1436  *      -FDT_ERR_TRUNCATED, standard meanings
1437  */
1438 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1439
1440 /**
1441  * fdt_add_subnode_namelen - creates a new node based on substring
1442  * @fdt: pointer to the device tree blob
1443  * @parentoffset: structure block offset of a node
1444  * @name: name of the subnode to locate
1445  * @namelen: number of characters of name to consider
1446  *
1447  * Identical to fdt_add_subnode(), but use only the first namelen
1448  * characters of name as the name of the new node.  This is useful for
1449  * creating subnodes based on a portion of a larger string, such as a
1450  * full path.
1451  */
1452 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1453                             const char *name, int namelen);
1454
1455 /**
1456  * fdt_add_subnode - creates a new node
1457  * @fdt: pointer to the device tree blob
1458  * @parentoffset: structure block offset of a node
1459  * @name: name of the subnode to locate
1460  *
1461  * fdt_add_subnode() creates a new node as a subnode of the node at
1462  * structure block offset parentoffset, with the given name (which
1463  * should include the unit address, if any).
1464  *
1465  * This function will insert data into the blob, and will therefore
1466  * change the offsets of some existing nodes.
1467
1468  * returns:
1469  *      structure block offset of the created nodeequested subnode (>=0), on success
1470  *      -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1471  *      -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1472  *      -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1473  *              the given name
1474  *      -FDT_ERR_NOSPACE, if there is insufficient free space in the
1475  *              blob to contain the new node
1476  *      -FDT_ERR_NOSPACE
1477  *      -FDT_ERR_BADLAYOUT
1478  *      -FDT_ERR_BADMAGIC,
1479  *      -FDT_ERR_BADVERSION,
1480  *      -FDT_ERR_BADSTATE,
1481  *      -FDT_ERR_BADSTRUCTURE,
1482  *      -FDT_ERR_TRUNCATED, standard meanings.
1483  */
1484 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1485
1486 /**
1487  * fdt_del_node - delete a node (subtree)
1488  * @fdt: pointer to the device tree blob
1489  * @nodeoffset: offset of the node to nop
1490  *
1491  * fdt_del_node() will remove the given node, including all its
1492  * subnodes if any, from the blob.
1493  *
1494  * This function will delete data from the blob, and will therefore
1495  * change the offsets of some existing nodes.
1496  *
1497  * returns:
1498  *      0, on success
1499  *      -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1500  *      -FDT_ERR_BADLAYOUT,
1501  *      -FDT_ERR_BADMAGIC,
1502  *      -FDT_ERR_BADVERSION,
1503  *      -FDT_ERR_BADSTATE,
1504  *      -FDT_ERR_BADSTRUCTURE,
1505  *      -FDT_ERR_TRUNCATED, standard meanings
1506  */
1507 int fdt_del_node(void *fdt, int nodeoffset);
1508
1509 /**********************************************************************/
1510 /* Debugging / informational functions                                */
1511 /**********************************************************************/
1512
1513 const char *fdt_strerror(int errval);
1514
1515 #endif /* _LIBFDT_H */