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