xref: /optee_os/core/lib/libfdt/include/libfdt.h (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1 /* SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0+) */
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  * libfdt is dual licensed: you can use it either under the terms of
9  * the GPL, or the BSD license, at your option.
10  *
11  *  a) This library is free software; you can redistribute it and/or
12  *     modify it under the terms of the GNU General Public License as
13  *     published by the Free Software Foundation; either version 2 of the
14  *     License, or (at your option) any later version.
15  *
16  *     This library is distributed in the hope that it will be useful,
17  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *     GNU General Public License for more details.
20  *
21  *     You should have received a copy of the GNU General Public
22  *     License along with this library; if not, write to the Free
23  *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24  *     MA 02110-1301 USA
25  *
26  * Alternatively,
27  *
28  *  b) Redistribution and use in source and binary forms, with or
29  *     without modification, are permitted provided that the following
30  *     conditions are met:
31  *
32  *     1. Redistributions of source code must retain the above
33  *        copyright notice, this list of conditions and the following
34  *        disclaimer.
35  *     2. Redistributions in binary form must reproduce the above
36  *        copyright notice, this list of conditions and the following
37  *        disclaimer in the documentation and/or other materials
38  *        provided with the distribution.
39  *
40  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
41  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
42  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
45  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
51  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
52  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  */
54 
55 #include <libfdt_env.h>
56 #include <fdt.h>
57 
58 #define FDT_FIRST_SUPPORTED_VERSION	0x10
59 #define FDT_LAST_SUPPORTED_VERSION	0x11
60 
61 /* Error codes: informative error codes */
62 #define FDT_ERR_NOTFOUND	1
63 	/* FDT_ERR_NOTFOUND: The requested node or property does not exist */
64 #define FDT_ERR_EXISTS		2
65 	/* FDT_ERR_EXISTS: Attemped to create a node or property which
66 	 * already exists */
67 #define FDT_ERR_NOSPACE		3
68 	/* FDT_ERR_NOSPACE: Operation needed to expand the device
69 	 * tree, but its buffer did not have sufficient space to
70 	 * contain the expanded tree. Use fdt_open_into() to move the
71 	 * device tree to a buffer with more space. */
72 
73 /* Error codes: codes for bad parameters */
74 #define FDT_ERR_BADOFFSET	4
75 	/* FDT_ERR_BADOFFSET: Function was passed a structure block
76 	 * offset which is out-of-bounds, or which points to an
77 	 * unsuitable part of the structure for the operation. */
78 #define FDT_ERR_BADPATH		5
79 	/* FDT_ERR_BADPATH: Function was passed a badly formatted path
80 	 * (e.g. missing a leading / for a function which requires an
81 	 * absolute path) */
82 #define FDT_ERR_BADPHANDLE	6
83 	/* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle
84 	 * value.  phandle values of 0 and -1 are not permitted. */
85 #define FDT_ERR_BADSTATE	7
86 	/* FDT_ERR_BADSTATE: Function was passed an incomplete device
87 	 * tree created by the sequential-write functions, which is
88 	 * not sufficiently complete for the requested operation. */
89 
90 /* Error codes: codes for bad device tree blobs */
91 #define FDT_ERR_TRUNCATED	8
92 	/* FDT_ERR_TRUNCATED: Structure block of the given device tree
93 	 * ends without an FDT_END tag. */
94 #define FDT_ERR_BADMAGIC	9
95 	/* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
96 	 * device tree at all - it is missing the flattened device
97 	 * tree magic number. */
98 #define FDT_ERR_BADVERSION	10
99 	/* FDT_ERR_BADVERSION: Given device tree has a version which
100 	 * can't be handled by the requested operation.  For
101 	 * read-write functions, this may mean that fdt_open_into() is
102 	 * required to convert the tree to the expected version. */
103 #define FDT_ERR_BADSTRUCTURE	11
104 	/* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
105 	 * structure block or other serious error (e.g. misnested
106 	 * nodes, or subnodes preceding properties). */
107 #define FDT_ERR_BADLAYOUT	12
108 	/* FDT_ERR_BADLAYOUT: For read-write functions, the given
109 	 * device tree has it's sub-blocks in an order that the
110 	 * function can't handle (memory reserve map, then structure,
111 	 * then strings).  Use fdt_open_into() to reorganize the tree
112 	 * into a form suitable for the read-write operations. */
113 
114 /* "Can't happen" error indicating a bug in libfdt */
115 #define FDT_ERR_INTERNAL	13
116 	/* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
117 	 * Should never be returned, if it is, it indicates a bug in
118 	 * libfdt itself. */
119 
120 /* Errors in device tree content */
121 #define FDT_ERR_BADNCELLS	14
122 	/* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
123 	 * or similar property with a bad format or value */
124 
125 #define FDT_ERR_MAX		14
126 
127 /**********************************************************************/
128 /* Low-level functions (you probably don't need these)                */
129 /**********************************************************************/
130 
131 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
132 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
133 {
134 	return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
135 }
136 
137 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
138 
139 /**********************************************************************/
140 /* Traversal functions                                                */
141 /**********************************************************************/
142 
143 int fdt_next_node(const void *fdt, int offset, int *depth);
144 
145 /**
146  * fdt_first_subnode() - get offset of first direct subnode
147  *
148  * @fdt:	FDT blob
149  * @offset:	Offset of node to check
150  * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
151  */
152 int fdt_first_subnode(const void *fdt, int offset);
153 
154 /**
155  * fdt_next_subnode() - get offset of next direct subnode
156  *
157  * After first calling fdt_first_subnode(), call this function repeatedly to
158  * get direct subnodes of a parent node.
159  *
160  * @fdt:	FDT blob
161  * @offset:	Offset of previous subnode
162  * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
163  * subnodes
164  */
165 int fdt_next_subnode(const void *fdt, int offset);
166 
167 /**********************************************************************/
168 /* General functions                                                  */
169 /**********************************************************************/
170 
171 #define fdt_get_header(fdt, field) \
172 	(fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
173 #define fdt_magic(fdt) 			(fdt_get_header(fdt, magic))
174 #define fdt_totalsize(fdt)		(fdt_get_header(fdt, totalsize))
175 #define fdt_off_dt_struct(fdt)		(fdt_get_header(fdt, off_dt_struct))
176 #define fdt_off_dt_strings(fdt)		(fdt_get_header(fdt, off_dt_strings))
177 #define fdt_off_mem_rsvmap(fdt)		(fdt_get_header(fdt, off_mem_rsvmap))
178 #define fdt_version(fdt)		(fdt_get_header(fdt, version))
179 #define fdt_last_comp_version(fdt) 	(fdt_get_header(fdt, last_comp_version))
180 #define fdt_boot_cpuid_phys(fdt) 	(fdt_get_header(fdt, boot_cpuid_phys))
181 #define fdt_size_dt_strings(fdt) 	(fdt_get_header(fdt, size_dt_strings))
182 #define fdt_size_dt_struct(fdt)		(fdt_get_header(fdt, size_dt_struct))
183 
184 #define __fdt_set_hdr(name) \
185 	static inline void fdt_set_##name(void *fdt, uint32_t val) \
186 	{ \
187 		struct fdt_header *fdth = (struct fdt_header*)fdt; \
188 		fdth->name = cpu_to_fdt32(val); \
189 	}
190 __fdt_set_hdr(magic)
191 __fdt_set_hdr(totalsize)
192 __fdt_set_hdr(off_dt_struct)
193 __fdt_set_hdr(off_dt_strings)
194 __fdt_set_hdr(off_mem_rsvmap)
195 __fdt_set_hdr(version)
196 __fdt_set_hdr(last_comp_version)
197 __fdt_set_hdr(boot_cpuid_phys)
198 __fdt_set_hdr(size_dt_strings)
199 __fdt_set_hdr(size_dt_struct)
200 #undef __fdt_set_hdr
201 
202 /**
203  * fdt_check_header - sanity check a device tree or possible device tree
204  * @fdt: pointer to data which might be a flattened device tree
205  *
206  * fdt_check_header() checks that the given buffer contains what
207  * appears to be a flattened device tree with sane information in its
208  * header.
209  *
210  * returns:
211  *     0, if the buffer appears to contain a valid device tree
212  *     -FDT_ERR_BADMAGIC,
213  *     -FDT_ERR_BADVERSION,
214  *     -FDT_ERR_BADSTATE, standard meanings, as above
215  */
216 int fdt_check_header(const void *fdt);
217 
218 /**
219  * fdt_move - move a device tree around in memory
220  * @fdt: pointer to the device tree to move
221  * @buf: pointer to memory where the device is to be moved
222  * @bufsize: size of the memory space at buf
223  *
224  * fdt_move() relocates, if possible, the device tree blob located at
225  * fdt to the buffer at buf of size bufsize.  The buffer may overlap
226  * with the existing device tree blob at fdt.  Therefore,
227  *     fdt_move(fdt, fdt, fdt_totalsize(fdt))
228  * should always succeed.
229  *
230  * returns:
231  *     0, on success
232  *     -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
233  *     -FDT_ERR_BADMAGIC,
234  *     -FDT_ERR_BADVERSION,
235  *     -FDT_ERR_BADSTATE, standard meanings
236  */
237 int fdt_move(const void *fdt, void *buf, int bufsize);
238 
239 /**********************************************************************/
240 /* Read-only functions                                                */
241 /**********************************************************************/
242 
243 /**
244  * fdt_string - retrieve a string from the strings block of a device tree
245  * @fdt: pointer to the device tree blob
246  * @stroffset: offset of the string within the strings block (native endian)
247  *
248  * fdt_string() retrieves a pointer to a single string from the
249  * strings block of the device tree blob at fdt.
250  *
251  * returns:
252  *     a pointer to the string, on success
253  *     NULL, if stroffset is out of bounds
254  */
255 const char *fdt_string(const void *fdt, int stroffset);
256 
257 /**
258  * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
259  * @fdt: pointer to the device tree blob
260  *
261  * Returns the number of entries in the device tree blob's memory
262  * reservation map.  This does not include the terminating 0,0 entry
263  * or any other (0,0) entries reserved for expansion.
264  *
265  * returns:
266  *     the number of entries
267  */
268 int fdt_num_mem_rsv(const void *fdt);
269 
270 /**
271  * fdt_get_mem_rsv - retrieve one memory reserve map entry
272  * @fdt: pointer to the device tree blob
273  * @address, @size: pointers to 64-bit variables
274  *
275  * On success, *address and *size will contain the address and size of
276  * the n-th reserve map entry from the device tree blob, in
277  * native-endian format.
278  *
279  * returns:
280  *     0, on success
281  *     -FDT_ERR_BADMAGIC,
282  *     -FDT_ERR_BADVERSION,
283  *     -FDT_ERR_BADSTATE, standard meanings
284  */
285 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
286 
287 /**
288  * fdt_subnode_offset_namelen - find a subnode based on substring
289  * @fdt: pointer to the device tree blob
290  * @parentoffset: structure block offset of a node
291  * @name: name of the subnode to locate
292  * @namelen: number of characters of name to consider
293  *
294  * Identical to fdt_subnode_offset(), but only examine the first
295  * namelen characters of name for matching the subnode name.  This is
296  * useful for finding subnodes based on a portion of a larger string,
297  * such as a full path.
298  */
299 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
300 			       const char *name, int namelen);
301 /**
302  * fdt_subnode_offset - find a subnode of a given node
303  * @fdt: pointer to the device tree blob
304  * @parentoffset: structure block offset of a node
305  * @name: name of the subnode to locate
306  *
307  * fdt_subnode_offset() finds a subnode of the node at structure block
308  * offset parentoffset with the given name.  name may include a unit
309  * address, in which case fdt_subnode_offset() will find the subnode
310  * with that unit address, or the unit address may be omitted, in
311  * which case fdt_subnode_offset() will find an arbitrary subnode
312  * whose name excluding unit address matches the given name.
313  *
314  * returns:
315  *	structure block offset of the requested subnode (>=0), on success
316  *	-FDT_ERR_NOTFOUND, if the requested subnode does not exist
317  *	-FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
318  *      -FDT_ERR_BADMAGIC,
319  *	-FDT_ERR_BADVERSION,
320  *	-FDT_ERR_BADSTATE,
321  *	-FDT_ERR_BADSTRUCTURE,
322  *	-FDT_ERR_TRUNCATED, standard meanings.
323  */
324 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
325 
326 /**
327  * fdt_path_offset - find a tree node by its full path
328  * @fdt: pointer to the device tree blob
329  * @path: full path of the node to locate
330  *
331  * fdt_path_offset() finds a node of a given path in the device tree.
332  * Each path component may omit the unit address portion, but the
333  * results of this are undefined if any such path component is
334  * ambiguous (that is if there are multiple nodes at the relevant
335  * level matching the given component, differentiated only by unit
336  * address).
337  *
338  * returns:
339  *	structure block offset of the node with the requested path (>=0), on success
340  *	-FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
341  *	-FDT_ERR_NOTFOUND, if the requested node does not exist
342  *      -FDT_ERR_BADMAGIC,
343  *	-FDT_ERR_BADVERSION,
344  *	-FDT_ERR_BADSTATE,
345  *	-FDT_ERR_BADSTRUCTURE,
346  *	-FDT_ERR_TRUNCATED, standard meanings.
347  */
348 int fdt_path_offset(const void *fdt, const char *path);
349 
350 /**
351  * fdt_get_name - retrieve the name of a given node
352  * @fdt: pointer to the device tree blob
353  * @nodeoffset: structure block offset of the starting node
354  * @lenp: pointer to an integer variable (will be overwritten) or NULL
355  *
356  * fdt_get_name() retrieves the name (including unit address) of the
357  * device tree node at structure block offset nodeoffset.  If lenp is
358  * non-NULL, the length of this name is also returned, in the integer
359  * pointed to by lenp.
360  *
361  * returns:
362  *	pointer to the node's name, on success
363  *		If lenp is non-NULL, *lenp contains the length of that name (>=0)
364  *	NULL, on error
365  *		if lenp is non-NULL *lenp contains an error code (<0):
366  *		-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
367  *		-FDT_ERR_BADMAGIC,
368  *		-FDT_ERR_BADVERSION,
369  *		-FDT_ERR_BADSTATE, standard meanings
370  */
371 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
372 
373 /**
374  * fdt_first_property_offset - find the offset of a node's first property
375  * @fdt: pointer to the device tree blob
376  * @nodeoffset: structure block offset of a node
377  *
378  * fdt_first_property_offset() finds the first property of the node at
379  * the given structure block offset.
380  *
381  * returns:
382  *	structure block offset of the property (>=0), on success
383  *	-FDT_ERR_NOTFOUND, if the requested node has no properties
384  *	-FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
385  *      -FDT_ERR_BADMAGIC,
386  *	-FDT_ERR_BADVERSION,
387  *	-FDT_ERR_BADSTATE,
388  *	-FDT_ERR_BADSTRUCTURE,
389  *	-FDT_ERR_TRUNCATED, standard meanings.
390  */
391 int fdt_first_property_offset(const void *fdt, int nodeoffset);
392 
393 /**
394  * fdt_next_property_offset - step through a node's properties
395  * @fdt: pointer to the device tree blob
396  * @offset: structure block offset of a property
397  *
398  * fdt_next_property_offset() finds the property immediately after the
399  * one at the given structure block offset.  This will be a property
400  * of the same node as the given property.
401  *
402  * returns:
403  *	structure block offset of the next property (>=0), on success
404  *	-FDT_ERR_NOTFOUND, if the given property is the last in its node
405  *	-FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
406  *      -FDT_ERR_BADMAGIC,
407  *	-FDT_ERR_BADVERSION,
408  *	-FDT_ERR_BADSTATE,
409  *	-FDT_ERR_BADSTRUCTURE,
410  *	-FDT_ERR_TRUNCATED, standard meanings.
411  */
412 int fdt_next_property_offset(const void *fdt, int offset);
413 
414 /**
415  * fdt_get_property_by_offset - retrieve the property at a given offset
416  * @fdt: pointer to the device tree blob
417  * @offset: offset of the property to retrieve
418  * @lenp: pointer to an integer variable (will be overwritten) or NULL
419  *
420  * fdt_get_property_by_offset() retrieves a pointer to the
421  * fdt_property structure within the device tree blob at the given
422  * offset.  If lenp is non-NULL, the length of the property value is
423  * also returned, in the integer pointed to by lenp.
424  *
425  * returns:
426  *	pointer to the structure representing the property
427  *		if lenp is non-NULL, *lenp contains the length of the property
428  *		value (>=0)
429  *	NULL, on error
430  *		if lenp is non-NULL, *lenp contains an error code (<0):
431  *		-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
432  *		-FDT_ERR_BADMAGIC,
433  *		-FDT_ERR_BADVERSION,
434  *		-FDT_ERR_BADSTATE,
435  *		-FDT_ERR_BADSTRUCTURE,
436  *		-FDT_ERR_TRUNCATED, standard meanings
437  */
438 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
439 						      int offset,
440 						      int *lenp);
441 
442 /**
443  * fdt_get_property_namelen - find a property based on substring
444  * @fdt: pointer to the device tree blob
445  * @nodeoffset: offset of the node whose property to find
446  * @name: name of the property to find
447  * @namelen: number of characters of name to consider
448  * @lenp: pointer to an integer variable (will be overwritten) or NULL
449  *
450  * Identical to fdt_get_property_namelen(), but only examine the first
451  * namelen characters of name for matching the property name.
452  */
453 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
454 						    int nodeoffset,
455 						    const char *name,
456 						    int namelen, int *lenp);
457 
458 /**
459  * fdt_get_property - find a given property in a given node
460  * @fdt: pointer to the device tree blob
461  * @nodeoffset: offset of the node whose property to find
462  * @name: name of the property to find
463  * @lenp: pointer to an integer variable (will be overwritten) or NULL
464  *
465  * fdt_get_property() retrieves a pointer to the fdt_property
466  * structure within the device tree blob corresponding to the property
467  * named 'name' of the node at offset nodeoffset.  If lenp is
468  * non-NULL, the length of the property value is also returned, in the
469  * integer pointed to by lenp.
470  *
471  * returns:
472  *	pointer to the structure representing the property
473  *		if lenp is non-NULL, *lenp contains the length of the property
474  *		value (>=0)
475  *	NULL, on error
476  *		if lenp is non-NULL, *lenp contains an error code (<0):
477  *		-FDT_ERR_NOTFOUND, node does not have named property
478  *		-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
479  *		-FDT_ERR_BADMAGIC,
480  *		-FDT_ERR_BADVERSION,
481  *		-FDT_ERR_BADSTATE,
482  *		-FDT_ERR_BADSTRUCTURE,
483  *		-FDT_ERR_TRUNCATED, standard meanings
484  */
485 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
486 					    const char *name, int *lenp);
487 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
488 						      const char *name,
489 						      int *lenp)
490 {
491 	return (struct fdt_property *)(uintptr_t)
492 		fdt_get_property(fdt, nodeoffset, name, lenp);
493 }
494 
495 /**
496  * fdt_getprop_by_offset - retrieve the value of a property at a given offset
497  * @fdt: pointer to the device tree blob
498  * @ffset: offset of the property to read
499  * @namep: pointer to a string variable (will be overwritten) or NULL
500  * @lenp: pointer to an integer variable (will be overwritten) or NULL
501  *
502  * fdt_getprop_by_offset() retrieves a pointer to the value of the
503  * property at structure block offset 'offset' (this will be a pointer
504  * to within the device blob itself, not a copy of the value).  If
505  * lenp is non-NULL, the length of the property value is also
506  * returned, in the integer pointed to by lenp.  If namep is non-NULL,
507  * the property's namne will also be returned in the char * pointed to
508  * by namep (this will be a pointer to within the device tree's string
509  * block, not a new copy of the name).
510  *
511  * returns:
512  *	pointer to the property's value
513  *		if lenp is non-NULL, *lenp contains the length of the property
514  *		value (>=0)
515  *		if namep is non-NULL *namep contiains a pointer to the property
516  *		name.
517  *	NULL, on error
518  *		if lenp is non-NULL, *lenp contains an error code (<0):
519  *		-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
520  *		-FDT_ERR_BADMAGIC,
521  *		-FDT_ERR_BADVERSION,
522  *		-FDT_ERR_BADSTATE,
523  *		-FDT_ERR_BADSTRUCTURE,
524  *		-FDT_ERR_TRUNCATED, standard meanings
525  */
526 const void *fdt_getprop_by_offset(const void *fdt, int offset,
527 				  const char **namep, int *lenp);
528 
529 /**
530  * fdt_getprop_namelen - get property value based on substring
531  * @fdt: pointer to the device tree blob
532  * @nodeoffset: offset of the node whose property to find
533  * @name: name of the property to find
534  * @namelen: number of characters of name to consider
535  * @lenp: pointer to an integer variable (will be overwritten) or NULL
536  *
537  * Identical to fdt_getprop(), but only examine the first namelen
538  * characters of name for matching the property name.
539  */
540 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
541 				const char *name, int namelen, int *lenp);
542 
543 /**
544  * fdt_getprop - retrieve the value of a given property
545  * @fdt: pointer to the device tree blob
546  * @nodeoffset: offset of the node whose property to find
547  * @name: name of the property to find
548  * @lenp: pointer to an integer variable (will be overwritten) or NULL
549  *
550  * fdt_getprop() retrieves a pointer to the value of the property
551  * named 'name' of the node at offset nodeoffset (this will be a
552  * pointer to within the device blob itself, not a copy of the value).
553  * If lenp is non-NULL, the length of the property value is also
554  * returned, in the integer pointed to by lenp.
555  *
556  * returns:
557  *	pointer to the property's value
558  *		if lenp is non-NULL, *lenp contains the length of the property
559  *		value (>=0)
560  *	NULL, on error
561  *		if lenp is non-NULL, *lenp contains an error code (<0):
562  *		-FDT_ERR_NOTFOUND, node does not have named property
563  *		-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
564  *		-FDT_ERR_BADMAGIC,
565  *		-FDT_ERR_BADVERSION,
566  *		-FDT_ERR_BADSTATE,
567  *		-FDT_ERR_BADSTRUCTURE,
568  *		-FDT_ERR_TRUNCATED, standard meanings
569  */
570 const void *fdt_getprop(const void *fdt, int nodeoffset,
571 			const char *name, int *lenp);
572 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
573 				  const char *name, int *lenp)
574 {
575 	return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
576 }
577 
578 /**
579  * fdt_get_phandle - retrieve the phandle of a given node
580  * @fdt: pointer to the device tree blob
581  * @nodeoffset: structure block offset of the node
582  *
583  * fdt_get_phandle() retrieves the phandle of the device tree node at
584  * structure block offset nodeoffset.
585  *
586  * returns:
587  *	the phandle of the node at nodeoffset, on success (!= 0, != -1)
588  *	0, if the node has no phandle, or another error occurs
589  */
590 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
591 
592 /**
593  * fdt_get_alias_namelen - get alias based on substring
594  * @fdt: pointer to the device tree blob
595  * @name: name of the alias th look up
596  * @namelen: number of characters of name to consider
597  *
598  * Identical to fdt_get_alias(), but only examine the first namelen
599  * characters of name for matching the alias name.
600  */
601 const char *fdt_get_alias_namelen(const void *fdt,
602 				  const char *name, int namelen);
603 
604 /**
605  * fdt_get_alias - retreive the path referenced by a given alias
606  * @fdt: pointer to the device tree blob
607  * @name: name of the alias th look up
608  *
609  * fdt_get_alias() retrieves the value of a given alias.  That is, the
610  * value of the property named 'name' in the node /aliases.
611  *
612  * returns:
613  *	a pointer to the expansion of the alias named 'name', if it exists
614  *	NULL, if the given alias or the /aliases node does not exist
615  */
616 const char *fdt_get_alias(const void *fdt, const char *name);
617 
618 /**
619  * fdt_get_path - determine the full path of a node
620  * @fdt: pointer to the device tree blob
621  * @nodeoffset: offset of the node whose path to find
622  * @buf: character buffer to contain the returned path (will be overwritten)
623  * @buflen: size of the character buffer at buf
624  *
625  * fdt_get_path() computes the full path of the node at offset
626  * nodeoffset, and records that path in the buffer at buf.
627  *
628  * NOTE: This function is expensive, as it must scan the device tree
629  * structure from the start to nodeoffset.
630  *
631  * returns:
632  *	0, on success
633  *		buf contains the absolute path of the node at
634  *		nodeoffset, as a NUL-terminated string.
635  * 	-FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
636  *	-FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
637  *		characters and will not fit in the given buffer.
638  *	-FDT_ERR_BADMAGIC,
639  *	-FDT_ERR_BADVERSION,
640  *	-FDT_ERR_BADSTATE,
641  *	-FDT_ERR_BADSTRUCTURE, standard meanings
642  */
643 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
644 
645 /**
646  * fdt_supernode_atdepth_offset - find a specific ancestor of a node
647  * @fdt: pointer to the device tree blob
648  * @nodeoffset: offset of the node whose parent to find
649  * @supernodedepth: depth of the ancestor to find
650  * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
651  *
652  * fdt_supernode_atdepth_offset() finds an ancestor of the given node
653  * at a specific depth from the root (where the root itself has depth
654  * 0, its immediate subnodes depth 1 and so forth).  So
655  *	fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
656  * will always return 0, the offset of the root node.  If the node at
657  * nodeoffset has depth D, then:
658  *	fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
659  * will return nodeoffset itself.
660  *
661  * NOTE: This function is expensive, as it must scan the device tree
662  * structure from the start to nodeoffset.
663  *
664  * returns:
665 
666  *	structure block offset of the node at node offset's ancestor
667  *		of depth supernodedepth (>=0), on success
668  * 	-FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
669 *	-FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
670  *	-FDT_ERR_BADMAGIC,
671  *	-FDT_ERR_BADVERSION,
672  *	-FDT_ERR_BADSTATE,
673  *	-FDT_ERR_BADSTRUCTURE, standard meanings
674  */
675 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
676 				 int supernodedepth, int *nodedepth);
677 
678 /**
679  * fdt_node_depth - find the depth of a given node
680  * @fdt: pointer to the device tree blob
681  * @nodeoffset: offset of the node whose parent to find
682  *
683  * fdt_node_depth() finds the depth of a given node.  The root node
684  * has depth 0, its immediate subnodes depth 1 and so forth.
685  *
686  * NOTE: This function is expensive, as it must scan the device tree
687  * structure from the start to nodeoffset.
688  *
689  * returns:
690  *	depth of the node at nodeoffset (>=0), on success
691  * 	-FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
692  *	-FDT_ERR_BADMAGIC,
693  *	-FDT_ERR_BADVERSION,
694  *	-FDT_ERR_BADSTATE,
695  *	-FDT_ERR_BADSTRUCTURE, standard meanings
696  */
697 int fdt_node_depth(const void *fdt, int nodeoffset);
698 
699 /**
700  * fdt_parent_offset - find the parent of a given node
701  * @fdt: pointer to the device tree blob
702  * @nodeoffset: offset of the node whose parent to find
703  *
704  * fdt_parent_offset() locates the parent node of a given node (that
705  * is, it finds the offset of the node which contains the node at
706  * nodeoffset as a subnode).
707  *
708  * NOTE: This function is expensive, as it must scan the device tree
709  * structure from the start to nodeoffset, *twice*.
710  *
711  * returns:
712  *	structure block offset of the parent of the node at nodeoffset
713  *		(>=0), on success
714  * 	-FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
715  *	-FDT_ERR_BADMAGIC,
716  *	-FDT_ERR_BADVERSION,
717  *	-FDT_ERR_BADSTATE,
718  *	-FDT_ERR_BADSTRUCTURE, standard meanings
719  */
720 int fdt_parent_offset(const void *fdt, int nodeoffset);
721 
722 /**
723  * fdt_node_offset_by_prop_value - find nodes with a given property value
724  * @fdt: pointer to the device tree blob
725  * @startoffset: only find nodes after this offset
726  * @propname: property name to check
727  * @propval: property value to search for
728  * @proplen: length of the value in propval
729  *
730  * fdt_node_offset_by_prop_value() returns the offset of the first
731  * node after startoffset, which has a property named propname whose
732  * value is of length proplen and has value equal to propval; or if
733  * startoffset is -1, the very first such node in the tree.
734  *
735  * To iterate through all nodes matching the criterion, the following
736  * idiom can be used:
737  *	offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
738  *					       propval, proplen);
739  *	while (offset != -FDT_ERR_NOTFOUND) {
740  *		// other code here
741  *		offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
742  *						       propval, proplen);
743  *	}
744  *
745  * Note the -1 in the first call to the function, if 0 is used here
746  * instead, the function will never locate the root node, even if it
747  * matches the criterion.
748  *
749  * returns:
750  *	structure block offset of the located node (>= 0, >startoffset),
751  *		 on success
752  *	-FDT_ERR_NOTFOUND, no node matching the criterion exists in the
753  *		tree after startoffset
754  * 	-FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
755  *	-FDT_ERR_BADMAGIC,
756  *	-FDT_ERR_BADVERSION,
757  *	-FDT_ERR_BADSTATE,
758  *	-FDT_ERR_BADSTRUCTURE, standard meanings
759  */
760 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
761 				  const char *propname,
762 				  const void *propval, int proplen);
763 
764 /**
765  * fdt_node_offset_by_phandle - find the node with a given phandle
766  * @fdt: pointer to the device tree blob
767  * @phandle: phandle value
768  *
769  * fdt_node_offset_by_phandle() returns the offset of the node
770  * which has the given phandle value.  If there is more than one node
771  * in the tree with the given phandle (an invalid tree), results are
772  * undefined.
773  *
774  * returns:
775  *	structure block offset of the located node (>= 0), on success
776  *	-FDT_ERR_NOTFOUND, no node with that phandle exists
777  *	-FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
778  *	-FDT_ERR_BADMAGIC,
779  *	-FDT_ERR_BADVERSION,
780  *	-FDT_ERR_BADSTATE,
781  *	-FDT_ERR_BADSTRUCTURE, standard meanings
782  */
783 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
784 
785 /**
786  * fdt_node_check_compatible: check a node's compatible property
787  * @fdt: pointer to the device tree blob
788  * @nodeoffset: offset of a tree node
789  * @compatible: string to match against
790  *
791  *
792  * fdt_node_check_compatible() returns 0 if the given node contains a
793  * 'compatible' property with the given string as one of its elements,
794  * it returns non-zero otherwise, or on error.
795  *
796  * returns:
797  *	0, if the node has a 'compatible' property listing the given string
798  *	1, if the node has a 'compatible' property, but it does not list
799  *		the given string
800  *	-FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
801  * 	-FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
802  *	-FDT_ERR_BADMAGIC,
803  *	-FDT_ERR_BADVERSION,
804  *	-FDT_ERR_BADSTATE,
805  *	-FDT_ERR_BADSTRUCTURE, standard meanings
806  */
807 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
808 			      const char *compatible);
809 
810 /**
811  * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
812  * @fdt: pointer to the device tree blob
813  * @startoffset: only find nodes after this offset
814  * @compatible: 'compatible' string to match against
815  *
816  * fdt_node_offset_by_compatible() returns the offset of the first
817  * node after startoffset, which has a 'compatible' property which
818  * lists the given compatible string; or if startoffset is -1, the
819  * very first such node in the tree.
820  *
821  * To iterate through all nodes matching the criterion, the following
822  * idiom can be used:
823  *	offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
824  *	while (offset != -FDT_ERR_NOTFOUND) {
825  *		// other code here
826  *		offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
827  *	}
828  *
829  * Note the -1 in the first call to the function, if 0 is used here
830  * instead, the function will never locate the root node, even if it
831  * matches the criterion.
832  *
833  * returns:
834  *	structure block offset of the located node (>= 0, >startoffset),
835  *		 on success
836  *	-FDT_ERR_NOTFOUND, no node matching the criterion exists in the
837  *		tree after startoffset
838  * 	-FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
839  *	-FDT_ERR_BADMAGIC,
840  *	-FDT_ERR_BADVERSION,
841  *	-FDT_ERR_BADSTATE,
842  *	-FDT_ERR_BADSTRUCTURE, standard meanings
843  */
844 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
845 				  const char *compatible);
846 
847 /**
848  * fdt_stringlist_contains - check a string list property for a string
849  * @strlist: Property containing a list of strings to check
850  * @listlen: Length of property
851  * @str: String to search for
852  *
853  * This is a utility function provided for convenience. The list contains
854  * one or more strings, each terminated by \0, as is found in a device tree
855  * "compatible" property.
856  *
857  * @return: 1 if the string is found in the list, 0 not found, or invalid list
858  */
859 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
860 
861 /**********************************************************************/
862 /* Read-only functions (addressing related)                           */
863 /**********************************************************************/
864 
865 /**
866  * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
867  *
868  * This is the maximum value for #address-cells, #size-cells and
869  * similar properties that will be processed by libfdt.  IEE1275
870  * requires that OF implementations handle values up to 4.
871  * Implementations may support larger values, but in practice higher
872  * values aren't used.
873  */
874 #define FDT_MAX_NCELLS		4
875 
876 /**
877  * fdt_address_cells - retrieve address size for a bus represented in the tree
878  * @fdt: pointer to the device tree blob
879  * @nodeoffset: offset of the node to find the address size for
880  *
881  * When the node has a valid #address-cells property, returns its value.
882  *
883  * returns:
884  *	0 <= n < FDT_MAX_NCELLS, on success
885  *      2, if the node has no #address-cells property
886  *      -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid #address-cells property
887  *	-FDT_ERR_BADMAGIC,
888  *	-FDT_ERR_BADVERSION,
889  *	-FDT_ERR_BADSTATE,
890  *	-FDT_ERR_BADSTRUCTURE,
891  *	-FDT_ERR_TRUNCATED, standard meanings
892  */
893 int fdt_address_cells(const void *fdt, int nodeoffset);
894 
895 /**
896  * fdt_size_cells - retrieve address range size for a bus represented in the
897  *                  tree
898  * @fdt: pointer to the device tree blob
899  * @nodeoffset: offset of the node to find the address range size for
900  *
901  * When the node has a valid #size-cells property, returns its value.
902  *
903  * returns:
904  *	0 <= n < FDT_MAX_NCELLS, on success
905  *      2, if the node has no #address-cells property
906  *      -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid #size-cells property
907  *	-FDT_ERR_BADMAGIC,
908  *	-FDT_ERR_BADVERSION,
909  *	-FDT_ERR_BADSTATE,
910  *	-FDT_ERR_BADSTRUCTURE,
911  *	-FDT_ERR_TRUNCATED, standard meanings
912  */
913 int fdt_size_cells(const void *fdt, int nodeoffset);
914 
915 
916 /**********************************************************************/
917 /* Write-in-place functions                                           */
918 /**********************************************************************/
919 
920 /**
921  * fdt_setprop_inplace - change a property's value, but not its size
922  * @fdt: pointer to the device tree blob
923  * @nodeoffset: offset of the node whose property to change
924  * @name: name of the property to change
925  * @val: pointer to data to replace the property value with
926  * @len: length of the property value
927  *
928  * fdt_setprop_inplace() replaces the value of a given property with
929  * the data in val, of length len.  This function cannot change the
930  * size of a property, and so will only work if len is equal to the
931  * current length of the property.
932  *
933  * This function will alter only the bytes in the blob which contain
934  * the given property value, and will not alter or move any other part
935  * of the tree.
936  *
937  * returns:
938  *	0, on success
939  *	-FDT_ERR_NOSPACE, if len is not equal to the property's current length
940  *	-FDT_ERR_NOTFOUND, node does not have the named property
941  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
942  *	-FDT_ERR_BADMAGIC,
943  *	-FDT_ERR_BADVERSION,
944  *	-FDT_ERR_BADSTATE,
945  *	-FDT_ERR_BADSTRUCTURE,
946  *	-FDT_ERR_TRUNCATED, standard meanings
947  */
948 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
949 			const void *val, int len);
950 
951 /**
952  * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
953  * @fdt: pointer to the device tree blob
954  * @nodeoffset: offset of the node whose property to change
955  * @name: name of the property to change
956  * @val: 32-bit integer value to replace the property with
957  *
958  * fdt_setprop_inplace_u32() replaces the value of a given property
959  * with the 32-bit integer value in val, converting val to big-endian
960  * if necessary.  This function cannot change the size of a property,
961  * and so will only work if the property already exists and has length
962  * 4.
963  *
964  * This function will alter only the bytes in the blob which contain
965  * the given property value, and will not alter or move any other part
966  * of the tree.
967  *
968  * returns:
969  *	0, on success
970  *	-FDT_ERR_NOSPACE, if the property's length is not equal to 4
971  *	-FDT_ERR_NOTFOUND, node does not have the named property
972  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
973  *	-FDT_ERR_BADMAGIC,
974  *	-FDT_ERR_BADVERSION,
975  *	-FDT_ERR_BADSTATE,
976  *	-FDT_ERR_BADSTRUCTURE,
977  *	-FDT_ERR_TRUNCATED, standard meanings
978  */
979 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
980 					  const char *name, uint32_t val)
981 {
982 	fdt32_t tmp = cpu_to_fdt32(val);
983 	return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
984 }
985 
986 /**
987  * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
988  * @fdt: pointer to the device tree blob
989  * @nodeoffset: offset of the node whose property to change
990  * @name: name of the property to change
991  * @val: 64-bit integer value to replace the property with
992  *
993  * fdt_setprop_inplace_u64() replaces the value of a given property
994  * with the 64-bit integer value in val, converting val to big-endian
995  * if necessary.  This function cannot change the size of a property,
996  * and so will only work if the property already exists and has length
997  * 8.
998  *
999  * This function will alter only the bytes in the blob which contain
1000  * the given property value, and will not alter or move any other part
1001  * of the tree.
1002  *
1003  * returns:
1004  *	0, on success
1005  *	-FDT_ERR_NOSPACE, if the property's length is not equal to 8
1006  *	-FDT_ERR_NOTFOUND, node does not have the named property
1007  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1008  *	-FDT_ERR_BADMAGIC,
1009  *	-FDT_ERR_BADVERSION,
1010  *	-FDT_ERR_BADSTATE,
1011  *	-FDT_ERR_BADSTRUCTURE,
1012  *	-FDT_ERR_TRUNCATED, standard meanings
1013  */
1014 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1015 					  const char *name, uint64_t val)
1016 {
1017 	fdt64_t tmp = cpu_to_fdt64(val);
1018 	return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1019 }
1020 
1021 /**
1022  * fdt_setprop_inplace_cell - change the value of a single-cell property
1023  *
1024  * This is an alternative name for fdt_setprop_inplace_u32()
1025  */
1026 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1027 					   const char *name, uint32_t val)
1028 {
1029 	return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1030 }
1031 
1032 /**
1033  * fdt_nop_property - replace a property with nop tags
1034  * @fdt: pointer to the device tree blob
1035  * @nodeoffset: offset of the node whose property to nop
1036  * @name: name of the property to nop
1037  *
1038  * fdt_nop_property() will replace a given property's representation
1039  * in the blob with FDT_NOP tags, effectively removing it from the
1040  * tree.
1041  *
1042  * This function will alter only the bytes in the blob which contain
1043  * the property, and will not alter or move any other part of the
1044  * tree.
1045  *
1046  * returns:
1047  *	0, on success
1048  *	-FDT_ERR_NOTFOUND, node does not have the named property
1049  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1050  *	-FDT_ERR_BADMAGIC,
1051  *	-FDT_ERR_BADVERSION,
1052  *	-FDT_ERR_BADSTATE,
1053  *	-FDT_ERR_BADSTRUCTURE,
1054  *	-FDT_ERR_TRUNCATED, standard meanings
1055  */
1056 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1057 
1058 /**
1059  * fdt_nop_node - replace a node (subtree) with nop tags
1060  * @fdt: pointer to the device tree blob
1061  * @nodeoffset: offset of the node to nop
1062  *
1063  * fdt_nop_node() will replace a given node's representation in the
1064  * blob, including all its subnodes, if any, with FDT_NOP tags,
1065  * effectively removing it from the tree.
1066  *
1067  * This function will alter only the bytes in the blob which contain
1068  * the node and its properties and subnodes, and will not alter or
1069  * move any other part of the tree.
1070  *
1071  * returns:
1072  *	0, on success
1073  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1074  *	-FDT_ERR_BADMAGIC,
1075  *	-FDT_ERR_BADVERSION,
1076  *	-FDT_ERR_BADSTATE,
1077  *	-FDT_ERR_BADSTRUCTURE,
1078  *	-FDT_ERR_TRUNCATED, standard meanings
1079  */
1080 int fdt_nop_node(void *fdt, int nodeoffset);
1081 
1082 /**********************************************************************/
1083 /* Sequential write functions                                         */
1084 /**********************************************************************/
1085 
1086 int fdt_create(void *buf, int bufsize);
1087 int fdt_resize(void *fdt, void *buf, int bufsize);
1088 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1089 int fdt_finish_reservemap(void *fdt);
1090 int fdt_begin_node(void *fdt, const char *name);
1091 int fdt_property(void *fdt, const char *name, const void *val, int len);
1092 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1093 {
1094 	fdt32_t tmp = cpu_to_fdt32(val);
1095 	return fdt_property(fdt, name, &tmp, sizeof(tmp));
1096 }
1097 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1098 {
1099 	fdt64_t tmp = cpu_to_fdt64(val);
1100 	return fdt_property(fdt, name, &tmp, sizeof(tmp));
1101 }
1102 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1103 {
1104 	return fdt_property_u32(fdt, name, val);
1105 }
1106 #define fdt_property_string(fdt, name, str) \
1107 	fdt_property(fdt, name, str, strlen(str)+1)
1108 int fdt_end_node(void *fdt);
1109 int fdt_finish(void *fdt);
1110 
1111 /**********************************************************************/
1112 /* Read-write functions                                               */
1113 /**********************************************************************/
1114 
1115 int fdt_create_empty_tree(void *buf, int bufsize);
1116 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1117 int fdt_pack(void *fdt);
1118 
1119 /**
1120  * fdt_add_mem_rsv - add one memory reserve map entry
1121  * @fdt: pointer to the device tree blob
1122  * @address, @size: 64-bit values (native endian)
1123  *
1124  * Adds a reserve map entry to the given blob reserving a region at
1125  * address address of length size.
1126  *
1127  * This function will insert data into the reserve map and will
1128  * therefore change the indexes of some entries in the table.
1129  *
1130  * returns:
1131  *	0, on success
1132  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1133  *		contain the new reservation entry
1134  *	-FDT_ERR_BADMAGIC,
1135  *	-FDT_ERR_BADVERSION,
1136  *	-FDT_ERR_BADSTATE,
1137  *	-FDT_ERR_BADSTRUCTURE,
1138  *	-FDT_ERR_BADLAYOUT,
1139  *	-FDT_ERR_TRUNCATED, standard meanings
1140  */
1141 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1142 
1143 /**
1144  * fdt_del_mem_rsv - remove a memory reserve map entry
1145  * @fdt: pointer to the device tree blob
1146  * @n: entry to remove
1147  *
1148  * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1149  * the blob.
1150  *
1151  * This function will delete data from the reservation table and will
1152  * therefore change the indexes of some entries in the table.
1153  *
1154  * returns:
1155  *	0, on success
1156  *	-FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1157  *		are less than n+1 reserve map entries)
1158  *	-FDT_ERR_BADMAGIC,
1159  *	-FDT_ERR_BADVERSION,
1160  *	-FDT_ERR_BADSTATE,
1161  *	-FDT_ERR_BADSTRUCTURE,
1162  *	-FDT_ERR_BADLAYOUT,
1163  *	-FDT_ERR_TRUNCATED, standard meanings
1164  */
1165 int fdt_del_mem_rsv(void *fdt, int n);
1166 
1167 /**
1168  * fdt_set_name - change the name of a given node
1169  * @fdt: pointer to the device tree blob
1170  * @nodeoffset: structure block offset of a node
1171  * @name: name to give the node
1172  *
1173  * fdt_set_name() replaces the name (including unit address, if any)
1174  * of the given node with the given string.  NOTE: this function can't
1175  * efficiently check if the new name is unique amongst the given
1176  * node's siblings; results are undefined if this function is invoked
1177  * with a name equal to one of the given node's siblings.
1178  *
1179  * This function may insert or delete data from the blob, and will
1180  * therefore change the offsets of some existing nodes.
1181  *
1182  * returns:
1183  *	0, on success
1184  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob
1185  *		to contain the new name
1186  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1187  *	-FDT_ERR_BADMAGIC,
1188  *	-FDT_ERR_BADVERSION,
1189  *	-FDT_ERR_BADSTATE, standard meanings
1190  */
1191 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1192 
1193 /**
1194  * fdt_setprop - create or change a property
1195  * @fdt: pointer to the device tree blob
1196  * @nodeoffset: offset of the node whose property to change
1197  * @name: name of the property to change
1198  * @val: pointer to data to set the property value to
1199  * @len: length of the property value
1200  *
1201  * fdt_setprop() sets the value of the named property in the given
1202  * node to the given value and length, creating the property if it
1203  * does not already exist.
1204  *
1205  * This function may insert or delete data from the blob, and will
1206  * therefore change the offsets of some existing nodes.
1207  *
1208  * returns:
1209  *	0, on success
1210  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1211  *		contain the new property value
1212  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1213  *	-FDT_ERR_BADLAYOUT,
1214  *	-FDT_ERR_BADMAGIC,
1215  *	-FDT_ERR_BADVERSION,
1216  *	-FDT_ERR_BADSTATE,
1217  *	-FDT_ERR_BADSTRUCTURE,
1218  *	-FDT_ERR_BADLAYOUT,
1219  *	-FDT_ERR_TRUNCATED, standard meanings
1220  */
1221 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1222 		const void *val, int len);
1223 
1224 /**
1225  * fdt_setprop_u32 - set a property to a 32-bit integer
1226  * @fdt: pointer to the device tree blob
1227  * @nodeoffset: offset of the node whose property to change
1228  * @name: name of the property to change
1229  * @val: 32-bit integer value for the property (native endian)
1230  *
1231  * fdt_setprop_u32() sets the value of the named property in the given
1232  * node to the given 32-bit integer value (converting to big-endian if
1233  * necessary), or creates a new property with that value if it does
1234  * not already exist.
1235  *
1236  * This function may insert or delete data from the blob, and will
1237  * therefore change the offsets of some existing nodes.
1238  *
1239  * returns:
1240  *	0, on success
1241  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1242  *		contain the new property value
1243  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1244  *	-FDT_ERR_BADLAYOUT,
1245  *	-FDT_ERR_BADMAGIC,
1246  *	-FDT_ERR_BADVERSION,
1247  *	-FDT_ERR_BADSTATE,
1248  *	-FDT_ERR_BADSTRUCTURE,
1249  *	-FDT_ERR_BADLAYOUT,
1250  *	-FDT_ERR_TRUNCATED, standard meanings
1251  */
1252 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1253 				  uint32_t val)
1254 {
1255 	fdt32_t tmp = cpu_to_fdt32(val);
1256 	return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1257 }
1258 
1259 /**
1260  * fdt_setprop_u64 - set a property to a 64-bit integer
1261  * @fdt: pointer to the device tree blob
1262  * @nodeoffset: offset of the node whose property to change
1263  * @name: name of the property to change
1264  * @val: 64-bit integer value for the property (native endian)
1265  *
1266  * fdt_setprop_u64() sets the value of the named property in the given
1267  * node to the given 64-bit integer value (converting to big-endian if
1268  * necessary), or creates a new property with that value if it does
1269  * not already exist.
1270  *
1271  * This function may insert or delete data from the blob, and will
1272  * therefore change the offsets of some existing nodes.
1273  *
1274  * returns:
1275  *	0, on success
1276  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1277  *		contain the new property value
1278  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1279  *	-FDT_ERR_BADLAYOUT,
1280  *	-FDT_ERR_BADMAGIC,
1281  *	-FDT_ERR_BADVERSION,
1282  *	-FDT_ERR_BADSTATE,
1283  *	-FDT_ERR_BADSTRUCTURE,
1284  *	-FDT_ERR_BADLAYOUT,
1285  *	-FDT_ERR_TRUNCATED, standard meanings
1286  */
1287 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1288 				  uint64_t val)
1289 {
1290 	fdt64_t tmp = cpu_to_fdt64(val);
1291 	return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1292 }
1293 
1294 /**
1295  * fdt_setprop_cell - set a property to a single cell value
1296  *
1297  * This is an alternative name for fdt_setprop_u32()
1298  */
1299 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1300 				   uint32_t val)
1301 {
1302 	return fdt_setprop_u32(fdt, nodeoffset, name, val);
1303 }
1304 
1305 /**
1306  * fdt_setprop_string - set a property to a string value
1307  * @fdt: pointer to the device tree blob
1308  * @nodeoffset: offset of the node whose property to change
1309  * @name: name of the property to change
1310  * @str: string value for the property
1311  *
1312  * fdt_setprop_string() sets the value of the named property in the
1313  * given node to the given string value (using the length of the
1314  * string to determine the new length of the property), or creates a
1315  * new property with that value if it does not already exist.
1316  *
1317  * This function may insert or delete data from the blob, and will
1318  * therefore change the offsets of some existing nodes.
1319  *
1320  * returns:
1321  *	0, on success
1322  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1323  *		contain the new property value
1324  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1325  *	-FDT_ERR_BADLAYOUT,
1326  *	-FDT_ERR_BADMAGIC,
1327  *	-FDT_ERR_BADVERSION,
1328  *	-FDT_ERR_BADSTATE,
1329  *	-FDT_ERR_BADSTRUCTURE,
1330  *	-FDT_ERR_BADLAYOUT,
1331  *	-FDT_ERR_TRUNCATED, standard meanings
1332  */
1333 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1334 	fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1335 
1336 /**
1337  * fdt_appendprop - append to or create a property
1338  * @fdt: pointer to the device tree blob
1339  * @nodeoffset: offset of the node whose property to change
1340  * @name: name of the property to append to
1341  * @val: pointer to data to append to the property value
1342  * @len: length of the data to append to the property value
1343  *
1344  * fdt_appendprop() appends the value to the named property in the
1345  * given node, creating the property if it does not already exist.
1346  *
1347  * This function may insert data into the blob, and will therefore
1348  * change the offsets of some existing nodes.
1349  *
1350  * returns:
1351  *	0, on success
1352  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1353  *		contain the new property value
1354  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1355  *	-FDT_ERR_BADLAYOUT,
1356  *	-FDT_ERR_BADMAGIC,
1357  *	-FDT_ERR_BADVERSION,
1358  *	-FDT_ERR_BADSTATE,
1359  *	-FDT_ERR_BADSTRUCTURE,
1360  *	-FDT_ERR_BADLAYOUT,
1361  *	-FDT_ERR_TRUNCATED, standard meanings
1362  */
1363 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1364 		   const void *val, int len);
1365 
1366 /**
1367  * fdt_appendprop_u32 - append a 32-bit integer value to a property
1368  * @fdt: pointer to the device tree blob
1369  * @nodeoffset: offset of the node whose property to change
1370  * @name: name of the property to change
1371  * @val: 32-bit integer value to append to the property (native endian)
1372  *
1373  * fdt_appendprop_u32() appends the given 32-bit integer value
1374  * (converting to big-endian if necessary) to the value of the named
1375  * property in the given node, or creates a new property with that
1376  * value if it does not already exist.
1377  *
1378  * This function may insert data into the blob, and will therefore
1379  * change the offsets of some existing nodes.
1380  *
1381  * returns:
1382  *	0, on success
1383  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1384  *		contain the new property value
1385  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1386  *	-FDT_ERR_BADLAYOUT,
1387  *	-FDT_ERR_BADMAGIC,
1388  *	-FDT_ERR_BADVERSION,
1389  *	-FDT_ERR_BADSTATE,
1390  *	-FDT_ERR_BADSTRUCTURE,
1391  *	-FDT_ERR_BADLAYOUT,
1392  *	-FDT_ERR_TRUNCATED, standard meanings
1393  */
1394 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1395 				     const char *name, uint32_t val)
1396 {
1397 	fdt32_t tmp = cpu_to_fdt32(val);
1398 	return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1399 }
1400 
1401 /**
1402  * fdt_appendprop_u64 - append a 64-bit integer value to a property
1403  * @fdt: pointer to the device tree blob
1404  * @nodeoffset: offset of the node whose property to change
1405  * @name: name of the property to change
1406  * @val: 64-bit integer value to append to the property (native endian)
1407  *
1408  * fdt_appendprop_u64() appends the given 64-bit integer value
1409  * (converting to big-endian if necessary) to the value of the named
1410  * property in the given node, or creates a new property with that
1411  * value if it does not already exist.
1412  *
1413  * This function may insert data into the blob, and will therefore
1414  * change the offsets of some existing nodes.
1415  *
1416  * returns:
1417  *	0, on success
1418  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1419  *		contain the new property value
1420  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1421  *	-FDT_ERR_BADLAYOUT,
1422  *	-FDT_ERR_BADMAGIC,
1423  *	-FDT_ERR_BADVERSION,
1424  *	-FDT_ERR_BADSTATE,
1425  *	-FDT_ERR_BADSTRUCTURE,
1426  *	-FDT_ERR_BADLAYOUT,
1427  *	-FDT_ERR_TRUNCATED, standard meanings
1428  */
1429 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1430 				     const char *name, uint64_t val)
1431 {
1432 	fdt64_t tmp = cpu_to_fdt64(val);
1433 	return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1434 }
1435 
1436 /**
1437  * fdt_appendprop_cell - append a single cell value to a property
1438  *
1439  * This is an alternative name for fdt_appendprop_u32()
1440  */
1441 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1442 				      const char *name, uint32_t val)
1443 {
1444 	return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1445 }
1446 
1447 /**
1448  * fdt_appendprop_string - append a string to a property
1449  * @fdt: pointer to the device tree blob
1450  * @nodeoffset: offset of the node whose property to change
1451  * @name: name of the property to change
1452  * @str: string value to append to the property
1453  *
1454  * fdt_appendprop_string() appends the given string to the value of
1455  * the named property in the given node, or creates a new property
1456  * with that value if it does not already exist.
1457  *
1458  * This function may insert data into the blob, and will therefore
1459  * change the offsets of some existing nodes.
1460  *
1461  * returns:
1462  *	0, on success
1463  *	-FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1464  *		contain the new property value
1465  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1466  *	-FDT_ERR_BADLAYOUT,
1467  *	-FDT_ERR_BADMAGIC,
1468  *	-FDT_ERR_BADVERSION,
1469  *	-FDT_ERR_BADSTATE,
1470  *	-FDT_ERR_BADSTRUCTURE,
1471  *	-FDT_ERR_BADLAYOUT,
1472  *	-FDT_ERR_TRUNCATED, standard meanings
1473  */
1474 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1475 	fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1476 
1477 /**
1478  * fdt_delprop - delete a property
1479  * @fdt: pointer to the device tree blob
1480  * @nodeoffset: offset of the node whose property to nop
1481  * @name: name of the property to nop
1482  *
1483  * fdt_del_property() will delete the given property.
1484  *
1485  * This function will delete data from the blob, and will therefore
1486  * change the offsets of some existing nodes.
1487  *
1488  * returns:
1489  *	0, on success
1490  *	-FDT_ERR_NOTFOUND, node does not have the named property
1491  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1492  *	-FDT_ERR_BADLAYOUT,
1493  *	-FDT_ERR_BADMAGIC,
1494  *	-FDT_ERR_BADVERSION,
1495  *	-FDT_ERR_BADSTATE,
1496  *	-FDT_ERR_BADSTRUCTURE,
1497  *	-FDT_ERR_TRUNCATED, standard meanings
1498  */
1499 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1500 
1501 /**
1502  * fdt_add_subnode_namelen - creates a new node based on substring
1503  * @fdt: pointer to the device tree blob
1504  * @parentoffset: structure block offset of a node
1505  * @name: name of the subnode to locate
1506  * @namelen: number of characters of name to consider
1507  *
1508  * Identical to fdt_add_subnode(), but use only the first namelen
1509  * characters of name as the name of the new node.  This is useful for
1510  * creating subnodes based on a portion of a larger string, such as a
1511  * full path.
1512  */
1513 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1514 			    const char *name, int namelen);
1515 
1516 /**
1517  * fdt_add_subnode - creates a new node
1518  * @fdt: pointer to the device tree blob
1519  * @parentoffset: structure block offset of a node
1520  * @name: name of the subnode to locate
1521  *
1522  * fdt_add_subnode() creates a new node as a subnode of the node at
1523  * structure block offset parentoffset, with the given name (which
1524  * should include the unit address, if any).
1525  *
1526  * This function will insert data into the blob, and will therefore
1527  * change the offsets of some existing nodes.
1528 
1529  * returns:
1530  *	structure block offset of the created nodeequested subnode (>=0), on success
1531  *	-FDT_ERR_NOTFOUND, if the requested subnode does not exist
1532  *	-FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1533  *	-FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1534  *		the given name
1535  *	-FDT_ERR_NOSPACE, if there is insufficient free space in the
1536  *		blob to contain the new node
1537  *	-FDT_ERR_NOSPACE
1538  *	-FDT_ERR_BADLAYOUT
1539  *      -FDT_ERR_BADMAGIC,
1540  *	-FDT_ERR_BADVERSION,
1541  *	-FDT_ERR_BADSTATE,
1542  *	-FDT_ERR_BADSTRUCTURE,
1543  *	-FDT_ERR_TRUNCATED, standard meanings.
1544  */
1545 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1546 
1547 /**
1548  * fdt_del_node - delete a node (subtree)
1549  * @fdt: pointer to the device tree blob
1550  * @nodeoffset: offset of the node to nop
1551  *
1552  * fdt_del_node() will remove the given node, including all its
1553  * subnodes if any, from the blob.
1554  *
1555  * This function will delete data from the blob, and will therefore
1556  * change the offsets of some existing nodes.
1557  *
1558  * returns:
1559  *	0, on success
1560  *	-FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1561  *	-FDT_ERR_BADLAYOUT,
1562  *	-FDT_ERR_BADMAGIC,
1563  *	-FDT_ERR_BADVERSION,
1564  *	-FDT_ERR_BADSTATE,
1565  *	-FDT_ERR_BADSTRUCTURE,
1566  *	-FDT_ERR_TRUNCATED, standard meanings
1567  */
1568 int fdt_del_node(void *fdt, int nodeoffset);
1569 
1570 /**********************************************************************/
1571 /* Debugging / informational functions                                */
1572 /**********************************************************************/
1573 
1574 const char *fdt_strerror(int errval);
1575 
1576 #endif /* _LIBFDT_H */
1577