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