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