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