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