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