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