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