]> git.ipfire.org Git - people/ms/u-boot.git/blame - include/libfdt.h
arm: mvebu: Move CONFIG_SYS_TEXT_BASE to an address < 16 MiB
[people/ms/u-boot.git] / include / libfdt.h
CommitLineData
8d04f02f
KG
1#ifndef _LIBFDT_H
2#define _LIBFDT_H
7cd5da0f
GVB
3/*
4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 *
8d04f02f
KG
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:
7cd5da0f 30 *
8d04f02f
KG
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.
7cd5da0f 38 *
8d04f02f
KG
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.
7cd5da0f
GVB
52 */
53
7cd5da0f 54#include <libfdt_env.h>
8d04f02f 55#include <fdt.h>
7cd5da0f
GVB
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
8d04f02f 62 /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
7cd5da0f 63#define FDT_ERR_EXISTS 2
8d04f02f
KG
64 /* FDT_ERR_EXISTS: Attemped to create a node or property which
65 * already exists */
7cd5da0f 66#define FDT_ERR_NOSPACE 3
8d04f02f
KG
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. */
7cd5da0f
GVB
71
72/* Error codes: codes for bad parameters */
73#define FDT_ERR_BADOFFSET 4
8d04f02f
KG
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. */
7cd5da0f 77#define FDT_ERR_BADPATH 5
8d04f02f
KG
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 * value. phandle values of 0 and -1 are not permitted. */
84#define FDT_ERR_BADSTATE 7
85 /* FDT_ERR_BADSTATE: Function was passed an incomplete device
86 * tree created by the sequential-write functions, which is
87 * not sufficiently complete for the requested operation. */
7cd5da0f
GVB
88
89/* Error codes: codes for bad device tree blobs */
8d04f02f
KG
90#define FDT_ERR_TRUNCATED 8
91 /* FDT_ERR_TRUNCATED: Structure block of the given device tree
92 * ends without an FDT_END tag. */
93#define FDT_ERR_BADMAGIC 9
94 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
95 * device tree at all - it is missing the flattened device
96 * tree magic number. */
97#define FDT_ERR_BADVERSION 10
98 /* FDT_ERR_BADVERSION: Given device tree has a version which
99 * can't be handled by the requested operation. For
100 * read-write functions, this may mean that fdt_open_into() is
101 * required to convert the tree to the expected version. */
102#define FDT_ERR_BADSTRUCTURE 11
103 /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
104 * structure block or other serious error (e.g. misnested
105 * nodes, or subnodes preceding properties). */
106#define FDT_ERR_BADLAYOUT 12
107 /* FDT_ERR_BADLAYOUT: For read-write functions, the given
108 * device tree has it's sub-blocks in an order that the
109 * function can't handle (memory reserve map, then structure,
110 * then strings). Use fdt_open_into() to reorganize the tree
111 * into a form suitable for the read-write operations. */
112
113/* "Can't happen" error indicating a bug in libfdt */
114#define FDT_ERR_INTERNAL 13
115 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
116 * Should never be returned, if it is, it indicates a bug in
117 * libfdt itself. */
7cd5da0f 118
0596d35d
SG
119/* Errors in device tree content */
120#define FDT_ERR_BADNCELLS 14
121 /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
122 * or similar property with a bad format or value */
123
c3c4c005
SG
124#define FDT_ERR_TOODEEP 15
125 /* FDT_ERR_TOODEEP: The depth of a node has exceeded the internal
126 * libfdt limit. This can happen if you have more than
127 * FDT_MAX_DEPTH nested nodes. */
128
129#define FDT_ERR_MAX 15
8d04f02f
KG
130
131/**********************************************************************/
132/* Low-level functions (you probably don't need these) */
133/**********************************************************************/
134
741a6d01 135const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
8d04f02f
KG
136static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
137{
c6683026 138 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
8d04f02f
KG
139}
140
141uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
142
ae0b5908
DG
143/**********************************************************************/
144/* Traversal functions */
145/**********************************************************************/
146
147int fdt_next_node(const void *fdt, int offset, int *depth);
148
88f95bba
SG
149/**
150 * fdt_first_subnode() - get offset of first direct subnode
151 *
152 * @fdt: FDT blob
153 * @offset: Offset of node to check
154 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
155 */
156int fdt_first_subnode(const void *fdt, int offset);
157
158/**
159 * fdt_next_subnode() - get offset of next direct subnode
160 *
161 * After first calling fdt_first_subnode(), call this function repeatedly to
162 * get direct subnodes of a parent node.
163 *
164 * @fdt: FDT blob
165 * @offset: Offset of previous subnode
166 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
167 * subnodes
168 */
169int fdt_next_subnode(const void *fdt, int offset);
170
2f376042
TR
171/**
172 * fdt_for_each_subnode - iterate over all subnodes of a parent
173 *
174 * This is actually a wrapper around a for loop and would be used like so:
175 *
176 * fdt_for_each_subnode(fdt, node, parent) {
177 * ...
178 * use node
179 * ...
180 * }
181 *
182 * Note that this is implemented as a macro and node is used as iterator in
183 * the loop. It should therefore be a locally allocated variable. The parent
184 * variable on the other hand is never modified, so it can be constant or
185 * even a literal.
186 *
187 * @fdt: FDT blob (const void *)
188 * @node: child node (int)
189 * @parent: parent node (int)
190 */
191#define fdt_for_each_subnode(fdt, node, parent) \
192 for (node = fdt_first_subnode(fdt, parent); \
193 node >= 0; \
194 node = fdt_next_subnode(fdt, node))
195
8d04f02f
KG
196/**********************************************************************/
197/* General functions */
198/**********************************************************************/
7cd5da0f
GVB
199
200#define fdt_get_header(fdt, field) \
8d04f02f 201 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
53677ef1 202#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
7cd5da0f
GVB
203#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
204#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
205#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
206#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
207#define fdt_version(fdt) (fdt_get_header(fdt, version))
53677ef1
WD
208#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
209#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
210#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
7cd5da0f
GVB
211#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
212
8d04f02f
KG
213#define __fdt_set_hdr(name) \
214 static inline void fdt_set_##name(void *fdt, uint32_t val) \
215 { \
67b89c79 216 struct fdt_header *fdth = (struct fdt_header*)fdt; \
8d04f02f
KG
217 fdth->name = cpu_to_fdt32(val); \
218 }
219__fdt_set_hdr(magic);
220__fdt_set_hdr(totalsize);
221__fdt_set_hdr(off_dt_struct);
222__fdt_set_hdr(off_dt_strings);
223__fdt_set_hdr(off_mem_rsvmap);
224__fdt_set_hdr(version);
225__fdt_set_hdr(last_comp_version);
226__fdt_set_hdr(boot_cpuid_phys);
227__fdt_set_hdr(size_dt_strings);
228__fdt_set_hdr(size_dt_struct);
229#undef __fdt_set_hdr
7cd5da0f 230
8d04f02f
KG
231/**
232 * fdt_check_header - sanity check a device tree or possible device tree
233 * @fdt: pointer to data which might be a flattened device tree
234 *
235 * fdt_check_header() checks that the given buffer contains what
236 * appears to be a flattened device tree with sane information in its
237 * header.
238 *
239 * returns:
240 * 0, if the buffer appears to contain a valid device tree
241 * -FDT_ERR_BADMAGIC,
242 * -FDT_ERR_BADVERSION,
243 * -FDT_ERR_BADSTATE, standard meanings, as above
244 */
6679f929
GVB
245int fdt_check_header(const void *fdt);
246
8d04f02f
KG
247/**
248 * fdt_move - move a device tree around in memory
249 * @fdt: pointer to the device tree to move
250 * @buf: pointer to memory where the device is to be moved
251 * @bufsize: size of the memory space at buf
252 *
253 * fdt_move() relocates, if possible, the device tree blob located at
254 * fdt to the buffer at buf of size bufsize. The buffer may overlap
255 * with the existing device tree blob at fdt. Therefore,
256 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
257 * should always succeed.
258 *
259 * returns:
260 * 0, on success
261 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
262 * -FDT_ERR_BADMAGIC,
263 * -FDT_ERR_BADVERSION,
264 * -FDT_ERR_BADSTATE, standard meanings
265 */
266int fdt_move(const void *fdt, void *buf, int bufsize);
7cd5da0f 267
8d04f02f
KG
268/**********************************************************************/
269/* Read-only functions */
270/**********************************************************************/
7cd5da0f 271
8d04f02f 272/**
46390da1 273 * fdt_string - retrieve a string from the strings block of a device tree
8d04f02f
KG
274 * @fdt: pointer to the device tree blob
275 * @stroffset: offset of the string within the strings block (native endian)
276 *
277 * fdt_string() retrieves a pointer to a single string from the
278 * strings block of the device tree blob at fdt.
279 *
280 * returns:
281 * a pointer to the string, on success
282 * NULL, if stroffset is out of bounds
283 */
284const char *fdt_string(const void *fdt, int stroffset);
7cd5da0f 285
8d04f02f 286/**
46390da1 287 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
8d04f02f
KG
288 * @fdt: pointer to the device tree blob
289 *
290 * Returns the number of entries in the device tree blob's memory
291 * reservation map. This does not include the terminating 0,0 entry
292 * or any other (0,0) entries reserved for expansion.
293 *
294 * returns:
295 * the number of entries
296 */
297int fdt_num_mem_rsv(const void *fdt);
7cd5da0f 298
8d04f02f 299/**
46390da1 300 * fdt_get_mem_rsv - retrieve one memory reserve map entry
8d04f02f
KG
301 * @fdt: pointer to the device tree blob
302 * @address, @size: pointers to 64-bit variables
303 *
304 * On success, *address and *size will contain the address and size of
305 * the n-th reserve map entry from the device tree blob, in
306 * native-endian format.
307 *
308 * returns:
309 * 0, on success
310 * -FDT_ERR_BADMAGIC,
311 * -FDT_ERR_BADVERSION,
312 * -FDT_ERR_BADSTATE, standard meanings
313 */
314int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
315
316/**
317 * fdt_subnode_offset_namelen - find a subnode based on substring
318 * @fdt: pointer to the device tree blob
319 * @parentoffset: structure block offset of a node
320 * @name: name of the subnode to locate
321 * @namelen: number of characters of name to consider
322 *
323 * Identical to fdt_subnode_offset(), but only examine the first
324 * namelen characters of name for matching the subnode name. This is
325 * useful for finding subnodes based on a portion of a larger string,
326 * such as a full path.
327 */
7cd5da0f
GVB
328int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
329 const char *name, int namelen);
8d04f02f
KG
330/**
331 * fdt_subnode_offset - find a subnode of a given node
332 * @fdt: pointer to the device tree blob
333 * @parentoffset: structure block offset of a node
334 * @name: name of the subnode to locate
335 *
336 * fdt_subnode_offset() finds a subnode of the node at structure block
337 * offset parentoffset with the given name. name may include a unit
338 * address, in which case fdt_subnode_offset() will find the subnode
339 * with that unit address, or the unit address may be omitted, in
340 * which case fdt_subnode_offset() will find an arbitrary subnode
341 * whose name excluding unit address matches the given name.
342 *
343 * returns:
344 * structure block offset of the requested subnode (>=0), on success
345 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
346 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
347 * -FDT_ERR_BADMAGIC,
348 * -FDT_ERR_BADVERSION,
349 * -FDT_ERR_BADSTATE,
350 * -FDT_ERR_BADSTRUCTURE,
351 * -FDT_ERR_TRUNCATED, standard meanings.
352 */
7cd5da0f
GVB
353int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
354
8d04f02f
KG
355/**
356 * fdt_path_offset - find a tree node by its full path
357 * @fdt: pointer to the device tree blob
358 * @path: full path of the node to locate
359 *
360 * fdt_path_offset() finds a node of a given path in the device tree.
361 * Each path component may omit the unit address portion, but the
362 * results of this are undefined if any such path component is
363 * ambiguous (that is if there are multiple nodes at the relevant
364 * level matching the given component, differentiated only by unit
365 * address).
366 *
367 * returns:
368 * structure block offset of the node with the requested path (>=0), on success
369 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
370 * -FDT_ERR_NOTFOUND, if the requested node does not exist
371 * -FDT_ERR_BADMAGIC,
372 * -FDT_ERR_BADVERSION,
373 * -FDT_ERR_BADSTATE,
374 * -FDT_ERR_BADSTRUCTURE,
375 * -FDT_ERR_TRUNCATED, standard meanings.
376 */
377int fdt_path_offset(const void *fdt, const char *path);
378
379/**
46390da1 380 * fdt_get_name - retrieve the name of a given node
8d04f02f
KG
381 * @fdt: pointer to the device tree blob
382 * @nodeoffset: structure block offset of the starting node
383 * @lenp: pointer to an integer variable (will be overwritten) or NULL
384 *
385 * fdt_get_name() retrieves the name (including unit address) of the
386 * device tree node at structure block offset nodeoffset. If lenp is
387 * non-NULL, the length of this name is also returned, in the integer
388 * pointed to by lenp.
389 *
390 * returns:
391 * pointer to the node's name, on success
392 * If lenp is non-NULL, *lenp contains the length of that name (>=0)
393 * NULL, on error
394 * if lenp is non-NULL *lenp contains an error code (<0):
395 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
396 * -FDT_ERR_BADMAGIC,
397 * -FDT_ERR_BADVERSION,
398 * -FDT_ERR_BADSTATE, standard meanings
399 */
400const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
401
d1c63148
DG
402/**
403 * fdt_first_property_offset - find the offset of a node's first property
404 * @fdt: pointer to the device tree blob
405 * @nodeoffset: structure block offset of a node
406 *
407 * fdt_first_property_offset() finds the first property of the node at
408 * the given structure block offset.
409 *
410 * returns:
411 * structure block offset of the property (>=0), on success
412 * -FDT_ERR_NOTFOUND, if the requested node has no properties
413 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
414 * -FDT_ERR_BADMAGIC,
415 * -FDT_ERR_BADVERSION,
416 * -FDT_ERR_BADSTATE,
417 * -FDT_ERR_BADSTRUCTURE,
418 * -FDT_ERR_TRUNCATED, standard meanings.
419 */
420int fdt_first_property_offset(const void *fdt, int nodeoffset);
421
422/**
423 * fdt_next_property_offset - step through a node's properties
424 * @fdt: pointer to the device tree blob
425 * @offset: structure block offset of a property
426 *
427 * fdt_next_property_offset() finds the property immediately after the
428 * one at the given structure block offset. This will be a property
429 * of the same node as the given property.
430 *
431 * returns:
432 * structure block offset of the next property (>=0), on success
433 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
434 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
435 * -FDT_ERR_BADMAGIC,
436 * -FDT_ERR_BADVERSION,
437 * -FDT_ERR_BADSTATE,
438 * -FDT_ERR_BADSTRUCTURE,
439 * -FDT_ERR_TRUNCATED, standard meanings.
440 */
441int fdt_next_property_offset(const void *fdt, int offset);
442
443/**
444 * fdt_get_property_by_offset - retrieve the property at a given offset
445 * @fdt: pointer to the device tree blob
446 * @offset: offset of the property to retrieve
447 * @lenp: pointer to an integer variable (will be overwritten) or NULL
448 *
449 * fdt_get_property_by_offset() retrieves a pointer to the
450 * fdt_property structure within the device tree blob at the given
451 * offset. If lenp is non-NULL, the length of the property value is
452 * also returned, in the integer pointed to by lenp.
453 *
454 * returns:
455 * pointer to the structure representing the property
456 * if lenp is non-NULL, *lenp contains the length of the property
457 * value (>=0)
458 * NULL, on error
459 * if lenp is non-NULL, *lenp contains an error code (<0):
460 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
461 * -FDT_ERR_BADMAGIC,
462 * -FDT_ERR_BADVERSION,
463 * -FDT_ERR_BADSTATE,
464 * -FDT_ERR_BADSTRUCTURE,
465 * -FDT_ERR_TRUNCATED, standard meanings
466 */
467const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
468 int offset,
469 int *lenp);
470
0219399a
DG
471/**
472 * fdt_get_property_namelen - find a property based on substring
473 * @fdt: pointer to the device tree blob
474 * @nodeoffset: offset of the node whose property to find
475 * @name: name of the property to find
476 * @namelen: number of characters of name to consider
477 * @lenp: pointer to an integer variable (will be overwritten) or NULL
478 *
479 * Identical to fdt_get_property_namelen(), but only examine the first
480 * namelen characters of name for matching the property name.
481 */
482const struct fdt_property *fdt_get_property_namelen(const void *fdt,
483 int nodeoffset,
484 const char *name,
485 int namelen, int *lenp);
486
8d04f02f
KG
487/**
488 * fdt_get_property - find a given property in a given node
489 * @fdt: pointer to the device tree blob
490 * @nodeoffset: offset of the node whose property to find
491 * @name: name of the property to find
492 * @lenp: pointer to an integer variable (will be overwritten) or NULL
493 *
494 * fdt_get_property() retrieves a pointer to the fdt_property
495 * structure within the device tree blob corresponding to the property
496 * named 'name' of the node at offset nodeoffset. If lenp is
46390da1 497 * non-NULL, the length of the property value is also returned, in the
8d04f02f
KG
498 * integer pointed to by lenp.
499 *
500 * returns:
501 * pointer to the structure representing the property
502 * if lenp is non-NULL, *lenp contains the length of the property
503 * value (>=0)
504 * NULL, on error
505 * if lenp is non-NULL, *lenp contains an error code (<0):
506 * -FDT_ERR_NOTFOUND, node does not have named property
507 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
508 * -FDT_ERR_BADMAGIC,
509 * -FDT_ERR_BADVERSION,
510 * -FDT_ERR_BADSTATE,
511 * -FDT_ERR_BADSTRUCTURE,
512 * -FDT_ERR_TRUNCATED, standard meanings
513 */
514const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
515 const char *name, int *lenp);
516static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
517 const char *name,
518 int *lenp)
519{
c6683026
DG
520 return (struct fdt_property *)(uintptr_t)
521 fdt_get_property(fdt, nodeoffset, name, lenp);
8d04f02f
KG
522}
523
d1c63148
DG
524/**
525 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
526 * @fdt: pointer to the device tree blob
527 * @ffset: offset of the property to read
528 * @namep: pointer to a string variable (will be overwritten) or NULL
529 * @lenp: pointer to an integer variable (will be overwritten) or NULL
530 *
531 * fdt_getprop_by_offset() retrieves a pointer to the value of the
532 * property at structure block offset 'offset' (this will be a pointer
533 * to within the device blob itself, not a copy of the value). If
534 * lenp is non-NULL, the length of the property value is also
535 * returned, in the integer pointed to by lenp. If namep is non-NULL,
536 * the property's namne will also be returned in the char * pointed to
537 * by namep (this will be a pointer to within the device tree's string
538 * block, not a new copy of the name).
539 *
540 * returns:
541 * pointer to the property's value
542 * if lenp is non-NULL, *lenp contains the length of the property
543 * value (>=0)
544 * if namep is non-NULL *namep contiains a pointer to the property
545 * name.
546 * NULL, on error
547 * if lenp is non-NULL, *lenp contains an error code (<0):
548 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
549 * -FDT_ERR_BADMAGIC,
550 * -FDT_ERR_BADVERSION,
551 * -FDT_ERR_BADSTATE,
552 * -FDT_ERR_BADSTRUCTURE,
553 * -FDT_ERR_TRUNCATED, standard meanings
554 */
555const void *fdt_getprop_by_offset(const void *fdt, int offset,
556 const char **namep, int *lenp);
557
0219399a
DG
558/**
559 * fdt_getprop_namelen - get property value based on substring
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 * @namelen: number of characters of name to consider
564 * @lenp: pointer to an integer variable (will be overwritten) or NULL
565 *
566 * Identical to fdt_getprop(), but only examine the first namelen
567 * characters of name for matching the property name.
568 */
569const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
570 const char *name, int namelen, int *lenp);
571
8d04f02f
KG
572/**
573 * fdt_getprop - retrieve the value of a given property
574 * @fdt: pointer to the device tree blob
575 * @nodeoffset: offset of the node whose property to find
576 * @name: name of the property to find
577 * @lenp: pointer to an integer variable (will be overwritten) or NULL
578 *
579 * fdt_getprop() retrieves a pointer to the value of the property
580 * named 'name' of the node at offset nodeoffset (this will be a
581 * pointer to within the device blob itself, not a copy of the value).
46390da1 582 * If lenp is non-NULL, the length of the property value is also
8d04f02f
KG
583 * returned, in the integer pointed to by lenp.
584 *
585 * returns:
586 * pointer to the property's value
587 * if lenp is non-NULL, *lenp contains the length of the property
588 * value (>=0)
589 * NULL, on error
590 * if lenp is non-NULL, *lenp contains an error code (<0):
591 * -FDT_ERR_NOTFOUND, node does not have named property
592 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
593 * -FDT_ERR_BADMAGIC,
594 * -FDT_ERR_BADVERSION,
595 * -FDT_ERR_BADSTATE,
596 * -FDT_ERR_BADSTRUCTURE,
597 * -FDT_ERR_TRUNCATED, standard meanings
598 */
599const void *fdt_getprop(const void *fdt, int nodeoffset,
600 const char *name, int *lenp);
601static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
602 const char *name, int *lenp)
603{
c6683026 604 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
8d04f02f
KG
605}
606
607/**
46390da1 608 * fdt_get_phandle - retrieve the phandle of a given node
8d04f02f
KG
609 * @fdt: pointer to the device tree blob
610 * @nodeoffset: structure block offset of the node
611 *
612 * fdt_get_phandle() retrieves the phandle of the device tree node at
613 * structure block offset nodeoffset.
614 *
615 * returns:
46390da1 616 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
8d04f02f
KG
617 * 0, if the node has no phandle, or another error occurs
618 */
619uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
620
9a6cf73a 621/**
cd3cb0d9 622 * fdt_get_alias_namelen - get alias based on substring
9a6cf73a
DG
623 * @fdt: pointer to the device tree blob
624 * @name: name of the alias th look up
625 * @namelen: number of characters of name to consider
626 *
627 * Identical to fdt_get_alias(), but only examine the first namelen
628 * characters of name for matching the alias name.
629 */
630const char *fdt_get_alias_namelen(const void *fdt,
631 const char *name, int namelen);
632
633/**
0596d35d 634 * fdt_get_alias - retreive the path referenced by a given alias
9a6cf73a 635 * @fdt: pointer to the device tree blob
0596d35d 636 * @name: name of the alias th look up
9a6cf73a
DG
637 *
638 * fdt_get_alias() retrieves the value of a given alias. That is, the
639 * value of the property named 'name' in the node /aliases.
640 *
641 * returns:
c73bd49d 642 * a pointer to the expansion of the alias named 'name', if it exists
9a6cf73a
DG
643 * NULL, if the given alias or the /aliases node does not exist
644 */
645const char *fdt_get_alias(const void *fdt, const char *name);
646
8d04f02f
KG
647/**
648 * fdt_get_path - determine the full path of a node
649 * @fdt: pointer to the device tree blob
650 * @nodeoffset: offset of the node whose path to find
651 * @buf: character buffer to contain the returned path (will be overwritten)
652 * @buflen: size of the character buffer at buf
653 *
654 * fdt_get_path() computes the full path of the node at offset
655 * nodeoffset, and records that path in the buffer at buf.
656 *
657 * NOTE: This function is expensive, as it must scan the device tree
658 * structure from the start to nodeoffset.
659 *
660 * returns:
661 * 0, on success
662 * buf contains the absolute path of the node at
663 * nodeoffset, as a NUL-terminated string.
53677ef1 664 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
665 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
666 * characters and will not fit in the given buffer.
667 * -FDT_ERR_BADMAGIC,
668 * -FDT_ERR_BADVERSION,
669 * -FDT_ERR_BADSTATE,
670 * -FDT_ERR_BADSTRUCTURE, standard meanings
671 */
672int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
673
674/**
675 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
676 * @fdt: pointer to the device tree blob
677 * @nodeoffset: offset of the node whose parent to find
678 * @supernodedepth: depth of the ancestor to find
679 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
680 *
681 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
682 * at a specific depth from the root (where the root itself has depth
683 * 0, its immediate subnodes depth 1 and so forth). So
684 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
685 * will always return 0, the offset of the root node. If the node at
686 * nodeoffset has depth D, then:
687 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
688 * will return nodeoffset itself.
689 *
690 * NOTE: This function is expensive, as it must scan the device tree
691 * structure from the start to nodeoffset.
692 *
693 * returns:
694
695 * structure block offset of the node at node offset's ancestor
696 * of depth supernodedepth (>=0), on success
53677ef1 697 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
698* -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
699 * -FDT_ERR_BADMAGIC,
700 * -FDT_ERR_BADVERSION,
701 * -FDT_ERR_BADSTATE,
702 * -FDT_ERR_BADSTRUCTURE, standard meanings
703 */
704int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
705 int supernodedepth, int *nodedepth);
706
707/**
708 * fdt_node_depth - find the depth of a given node
709 * @fdt: pointer to the device tree blob
710 * @nodeoffset: offset of the node whose parent to find
711 *
712 * fdt_node_depth() finds the depth of a given node. The root node
713 * has depth 0, its immediate subnodes depth 1 and so forth.
714 *
715 * NOTE: This function is expensive, as it must scan the device tree
716 * structure from the start to nodeoffset.
717 *
718 * returns:
719 * depth of the node at nodeoffset (>=0), on success
53677ef1 720 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
721 * -FDT_ERR_BADMAGIC,
722 * -FDT_ERR_BADVERSION,
723 * -FDT_ERR_BADSTATE,
724 * -FDT_ERR_BADSTRUCTURE, standard meanings
725 */
726int fdt_node_depth(const void *fdt, int nodeoffset);
727
728/**
729 * fdt_parent_offset - find the parent of a given node
730 * @fdt: pointer to the device tree blob
731 * @nodeoffset: offset of the node whose parent to find
732 *
733 * fdt_parent_offset() locates the parent node of a given node (that
734 * is, it finds the offset of the node which contains the node at
735 * nodeoffset as a subnode).
736 *
737 * NOTE: This function is expensive, as it must scan the device tree
738 * structure from the start to nodeoffset, *twice*.
739 *
740 * returns:
46390da1 741 * structure block offset of the parent of the node at nodeoffset
8d04f02f 742 * (>=0), on success
53677ef1 743 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
744 * -FDT_ERR_BADMAGIC,
745 * -FDT_ERR_BADVERSION,
746 * -FDT_ERR_BADSTATE,
747 * -FDT_ERR_BADSTRUCTURE, standard meanings
748 */
749int fdt_parent_offset(const void *fdt, int nodeoffset);
750
751/**
752 * fdt_node_offset_by_prop_value - find nodes with a given property value
753 * @fdt: pointer to the device tree blob
754 * @startoffset: only find nodes after this offset
755 * @propname: property name to check
756 * @propval: property value to search for
757 * @proplen: length of the value in propval
758 *
759 * fdt_node_offset_by_prop_value() returns the offset of the first
760 * node after startoffset, which has a property named propname whose
761 * value is of length proplen and has value equal to propval; or if
762 * startoffset is -1, the very first such node in the tree.
763 *
764 * To iterate through all nodes matching the criterion, the following
765 * idiom can be used:
766 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
767 * propval, proplen);
768 * while (offset != -FDT_ERR_NOTFOUND) {
0596d35d 769 * // other code here
8d04f02f
KG
770 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
771 * propval, proplen);
772 * }
773 *
774 * Note the -1 in the first call to the function, if 0 is used here
775 * instead, the function will never locate the root node, even if it
776 * matches the criterion.
777 *
778 * returns:
779 * structure block offset of the located node (>= 0, >startoffset),
780 * on success
781 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
782 * tree after startoffset
53677ef1 783 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
784 * -FDT_ERR_BADMAGIC,
785 * -FDT_ERR_BADVERSION,
786 * -FDT_ERR_BADSTATE,
787 * -FDT_ERR_BADSTRUCTURE, standard meanings
788 */
789int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
790 const char *propname,
791 const void *propval, int proplen);
792
793/**
794 * fdt_node_offset_by_phandle - find the node with a given phandle
795 * @fdt: pointer to the device tree blob
796 * @phandle: phandle value
797 *
46390da1 798 * fdt_node_offset_by_phandle() returns the offset of the node
8d04f02f
KG
799 * which has the given phandle value. If there is more than one node
800 * in the tree with the given phandle (an invalid tree), results are
801 * undefined.
802 *
803 * returns:
804 * structure block offset of the located node (>= 0), on success
805 * -FDT_ERR_NOTFOUND, no node with that phandle exists
806 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
807 * -FDT_ERR_BADMAGIC,
808 * -FDT_ERR_BADVERSION,
809 * -FDT_ERR_BADSTATE,
810 * -FDT_ERR_BADSTRUCTURE, standard meanings
811 */
812int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
813
814/**
815 * fdt_node_check_compatible: check a node's compatible property
816 * @fdt: pointer to the device tree blob
817 * @nodeoffset: offset of a tree node
818 * @compatible: string to match against
819 *
820 *
821 * fdt_node_check_compatible() returns 0 if the given node contains a
822 * 'compatible' property with the given string as one of its elements,
823 * it returns non-zero otherwise, or on error.
824 *
825 * returns:
826 * 0, if the node has a 'compatible' property listing the given string
827 * 1, if the node has a 'compatible' property, but it does not list
828 * the given string
829 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
53677ef1 830 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
831 * -FDT_ERR_BADMAGIC,
832 * -FDT_ERR_BADVERSION,
833 * -FDT_ERR_BADSTATE,
834 * -FDT_ERR_BADSTRUCTURE, standard meanings
835 */
836int fdt_node_check_compatible(const void *fdt, int nodeoffset,
837 const char *compatible);
7cd5da0f 838
8d04f02f
KG
839/**
840 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
841 * @fdt: pointer to the device tree blob
842 * @startoffset: only find nodes after this offset
843 * @compatible: 'compatible' string to match against
844 *
845 * fdt_node_offset_by_compatible() returns the offset of the first
846 * node after startoffset, which has a 'compatible' property which
847 * lists the given compatible string; or if startoffset is -1, the
848 * very first such node in the tree.
849 *
850 * To iterate through all nodes matching the criterion, the following
851 * idiom can be used:
852 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
853 * while (offset != -FDT_ERR_NOTFOUND) {
0596d35d 854 * // other code here
8d04f02f
KG
855 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
856 * }
857 *
858 * Note the -1 in the first call to the function, if 0 is used here
859 * instead, the function will never locate the root node, even if it
860 * matches the criterion.
861 *
862 * returns:
863 * structure block offset of the located node (>= 0, >startoffset),
864 * on success
865 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
866 * tree after startoffset
53677ef1 867 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
868 * -FDT_ERR_BADMAGIC,
869 * -FDT_ERR_BADVERSION,
870 * -FDT_ERR_BADSTATE,
871 * -FDT_ERR_BADSTRUCTURE, standard meanings
872 */
873int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
874 const char *compatible);
7cd5da0f 875
e853b324
SG
876/**
877 * fdt_stringlist_contains - check a string list property for a string
878 * @strlist: Property containing a list of strings to check
879 * @listlen: Length of property
880 * @str: String to search for
881 *
882 * This is a utility function provided for convenience. The list contains
883 * one or more strings, each terminated by \0, as is found in a device tree
884 * "compatible" property.
885 *
886 * @return: 1 if the string is found in the list, 0 not found, or invalid list
887 */
888int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
889
bc4147ab
TR
890/**
891 * fdt_count_strings - count the number of strings in a string list
892 * @fdt: pointer to the device tree blob
893 * @node: offset of the node
894 * @property: name of the property containing the string list
895 * @return: the number of strings in the given property
896 */
897int fdt_count_strings(const void *fdt, int node, const char *property);
898
fc503c17
TR
899/**
900 * fdt_find_string - find a string in a string list and return its index
901 * @fdt: pointer to the device tree blob
902 * @node: offset of the node
903 * @property: name of the property containing the string list
904 * @string: string to look up in the string list
905 * @return: the index of the string or negative on error
906 */
907int fdt_find_string(const void *fdt, int node, const char *property,
908 const char *string);
909
5094eb40
TR
910/**
911 * fdt_get_string_index() - obtain the string at a given index in a string list
912 * @fdt: pointer to the device tree blob
913 * @node: offset of the node
914 * @property: name of the property containing the string list
915 * @index: index of the string to return
916 * @output: return location for the string
917 * @return: 0 if the string was found or a negative error code otherwise
918 */
919int fdt_get_string_index(const void *fdt, int node, const char *property,
920 int index, const char **output);
921
922/**
965fab1a 923 * fdt_get_string() - obtain the first string in a string list
5094eb40
TR
924 * @fdt: pointer to the device tree blob
925 * @node: offset of the node
926 * @property: name of the property containing the string list
927 * @output: return location for the string
928 * @return: 0 if the string was found or a negative error code otherwise
929 *
930 * This is a shortcut for:
931 *
932 * fdt_get_string_index(fdt, node, property, 0, output).
933 */
934int fdt_get_string(const void *fdt, int node, const char *property,
935 const char **output);
936
0596d35d
SG
937/**********************************************************************/
938/* Read-only functions (addressing related) */
939/**********************************************************************/
940
941/**
942 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
943 *
944 * This is the maximum value for #address-cells, #size-cells and
945 * similar properties that will be processed by libfdt. IEE1275
946 * requires that OF implementations handle values up to 4.
947 * Implementations may support larger values, but in practice higher
948 * values aren't used.
949 */
950#define FDT_MAX_NCELLS 4
951
952/**
953 * fdt_address_cells - retrieve address size for a bus represented in the tree
954 * @fdt: pointer to the device tree blob
955 * @nodeoffset: offset of the node to find the address size for
956 *
957 * When the node has a valid #address-cells property, returns its value.
958 *
959 * returns:
960 * 0 <= n < FDT_MAX_NCELLS, on success
961 * 2, if the node has no #address-cells property
962 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
963 * #address-cells property
964 * -FDT_ERR_BADMAGIC,
965 * -FDT_ERR_BADVERSION,
966 * -FDT_ERR_BADSTATE,
967 * -FDT_ERR_BADSTRUCTURE,
968 * -FDT_ERR_TRUNCATED, standard meanings
969 */
970int fdt_address_cells(const void *fdt, int nodeoffset);
971
972/**
973 * fdt_size_cells - retrieve address range size for a bus represented in the
974 * tree
975 * @fdt: pointer to the device tree blob
976 * @nodeoffset: offset of the node to find the address range size for
977 *
978 * When the node has a valid #size-cells property, returns its value.
979 *
980 * returns:
981 * 0 <= n < FDT_MAX_NCELLS, on success
982 * 2, if the node has no #address-cells property
983 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
984 * #size-cells property
985 * -FDT_ERR_BADMAGIC,
986 * -FDT_ERR_BADVERSION,
987 * -FDT_ERR_BADSTATE,
988 * -FDT_ERR_BADSTRUCTURE,
989 * -FDT_ERR_TRUNCATED, standard meanings
990 */
991int fdt_size_cells(const void *fdt, int nodeoffset);
992
993
8d04f02f
KG
994/**********************************************************************/
995/* Write-in-place functions */
996/**********************************************************************/
3af0d587 997
9d4450b5
DG
998/**
999 * fdt_setprop_inplace - change a property's value, but not its size
1000 * @fdt: pointer to the device tree blob
1001 * @nodeoffset: offset of the node whose property to change
1002 * @name: name of the property to change
1003 * @val: pointer to data to replace the property value with
1004 * @len: length of the property value
1005 *
1006 * fdt_setprop_inplace() replaces the value of a given property with
1007 * the data in val, of length len. This function cannot change the
1008 * size of a property, and so will only work if len is equal to the
1009 * current length of the property.
1010 *
1011 * This function will alter only the bytes in the blob which contain
1012 * the given property value, and will not alter or move any other part
1013 * of the tree.
1014 *
1015 * returns:
1016 * 0, on success
1017 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1018 * -FDT_ERR_NOTFOUND, node does not have the named property
1019 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1020 * -FDT_ERR_BADMAGIC,
1021 * -FDT_ERR_BADVERSION,
1022 * -FDT_ERR_BADSTATE,
1023 * -FDT_ERR_BADSTRUCTURE,
1024 * -FDT_ERR_TRUNCATED, standard meanings
1025 */
7cd5da0f
GVB
1026int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1027 const void *val, int len);
9d4450b5
DG
1028
1029/**
24fa0e58 1030 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
9d4450b5
DG
1031 * @fdt: pointer to the device tree blob
1032 * @nodeoffset: offset of the node whose property to change
1033 * @name: name of the property to change
24fa0e58 1034 * @val: 32-bit integer value to replace the property with
9d4450b5 1035 *
24fa0e58
DG
1036 * fdt_setprop_inplace_u32() replaces the value of a given property
1037 * with the 32-bit integer value in val, converting val to big-endian
1038 * if necessary. This function cannot change the size of a property,
1039 * and so will only work if the property already exists and has length
1040 * 4.
9d4450b5
DG
1041 *
1042 * This function will alter only the bytes in the blob which contain
1043 * the given property value, and will not alter or move any other part
1044 * of the tree.
1045 *
1046 * returns:
1047 * 0, on success
1048 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
24fa0e58 1049 * -FDT_ERR_NOTFOUND, node does not have the named property
9d4450b5
DG
1050 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1051 * -FDT_ERR_BADMAGIC,
1052 * -FDT_ERR_BADVERSION,
1053 * -FDT_ERR_BADSTATE,
1054 * -FDT_ERR_BADSTRUCTURE,
1055 * -FDT_ERR_TRUNCATED, standard meanings
1056 */
24fa0e58
DG
1057static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1058 const char *name, uint32_t val)
8d04f02f 1059{
b2ba62a1
KP
1060 fdt32_t tmp = cpu_to_fdt32(val);
1061 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
8d04f02f 1062}
7cd5da0f 1063
24fa0e58
DG
1064/**
1065 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1066 * @fdt: pointer to the device tree blob
1067 * @nodeoffset: offset of the node whose property to change
1068 * @name: name of the property to change
1069 * @val: 64-bit integer value to replace the property with
1070 *
1071 * fdt_setprop_inplace_u64() replaces the value of a given property
1072 * with the 64-bit integer value in val, converting val to big-endian
1073 * if necessary. This function cannot change the size of a property,
1074 * and so will only work if the property already exists and has length
1075 * 8.
1076 *
1077 * This function will alter only the bytes in the blob which contain
1078 * the given property value, and will not alter or move any other part
1079 * of the tree.
1080 *
1081 * returns:
1082 * 0, on success
1083 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1084 * -FDT_ERR_NOTFOUND, node does not have the named property
1085 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1086 * -FDT_ERR_BADMAGIC,
1087 * -FDT_ERR_BADVERSION,
1088 * -FDT_ERR_BADSTATE,
1089 * -FDT_ERR_BADSTRUCTURE,
1090 * -FDT_ERR_TRUNCATED, standard meanings
1091 */
1092static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1093 const char *name, uint64_t val)
1094{
b2ba62a1
KP
1095 fdt64_t tmp = cpu_to_fdt64(val);
1096 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
24fa0e58
DG
1097}
1098
1099/**
1100 * fdt_setprop_inplace_cell - change the value of a single-cell property
1101 *
1102 * This is an alternative name for fdt_setprop_inplace_u32()
1103 */
1104static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1105 const char *name, uint32_t val)
1106{
1107 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1108}
1109
9d4450b5
DG
1110/**
1111 * fdt_nop_property - replace a property with nop tags
1112 * @fdt: pointer to the device tree blob
1113 * @nodeoffset: offset of the node whose property to nop
1114 * @name: name of the property to nop
1115 *
1116 * fdt_nop_property() will replace a given property's representation
1117 * in the blob with FDT_NOP tags, effectively removing it from the
1118 * tree.
1119 *
1120 * This function will alter only the bytes in the blob which contain
1121 * the property, and will not alter or move any other part of the
1122 * tree.
1123 *
1124 * returns:
1125 * 0, on success
1126 * -FDT_ERR_NOTFOUND, node does not have the named property
1127 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1128 * -FDT_ERR_BADMAGIC,
1129 * -FDT_ERR_BADVERSION,
1130 * -FDT_ERR_BADSTATE,
1131 * -FDT_ERR_BADSTRUCTURE,
1132 * -FDT_ERR_TRUNCATED, standard meanings
1133 */
7cd5da0f 1134int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
9d4450b5
DG
1135
1136/**
1137 * fdt_nop_node - replace a node (subtree) with nop tags
1138 * @fdt: pointer to the device tree blob
1139 * @nodeoffset: offset of the node to nop
1140 *
1141 * fdt_nop_node() will replace a given node's representation in the
1142 * blob, including all its subnodes, if any, with FDT_NOP tags,
1143 * effectively removing it from the tree.
1144 *
1145 * This function will alter only the bytes in the blob which contain
1146 * the node and its properties and subnodes, and will not alter or
1147 * move any other part of the tree.
1148 *
1149 * returns:
1150 * 0, on success
1151 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1152 * -FDT_ERR_BADMAGIC,
1153 * -FDT_ERR_BADVERSION,
1154 * -FDT_ERR_BADSTATE,
1155 * -FDT_ERR_BADSTRUCTURE,
1156 * -FDT_ERR_TRUNCATED, standard meanings
1157 */
7cd5da0f 1158int fdt_nop_node(void *fdt, int nodeoffset);
3f9f08cf 1159
8d04f02f
KG
1160/**********************************************************************/
1161/* Sequential write functions */
1162/**********************************************************************/
7cd5da0f 1163
7cd5da0f 1164int fdt_create(void *buf, int bufsize);
0596d35d 1165int fdt_resize(void *fdt, void *buf, int bufsize);
7cd5da0f
GVB
1166int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1167int fdt_finish_reservemap(void *fdt);
1168int fdt_begin_node(void *fdt, const char *name);
1169int fdt_property(void *fdt, const char *name, const void *val, int len);
24fa0e58 1170static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
8d04f02f 1171{
b2ba62a1
KP
1172 fdt32_t tmp = cpu_to_fdt32(val);
1173 return fdt_property(fdt, name, &tmp, sizeof(tmp));
8d04f02f 1174}
24fa0e58
DG
1175static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1176{
b2ba62a1
KP
1177 fdt64_t tmp = cpu_to_fdt64(val);
1178 return fdt_property(fdt, name, &tmp, sizeof(tmp));
24fa0e58
DG
1179}
1180static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1181{
1182 return fdt_property_u32(fdt, name, val);
1183}
7cd5da0f
GVB
1184#define fdt_property_string(fdt, name, str) \
1185 fdt_property(fdt, name, str, strlen(str)+1)
1186int fdt_end_node(void *fdt);
1187int fdt_finish(void *fdt);
1188
8d04f02f
KG
1189/**********************************************************************/
1190/* Read-write functions */
1191/**********************************************************************/
1192
c71b64f3 1193int fdt_create_empty_tree(void *buf, int bufsize);
8d04f02f 1194int fdt_open_into(const void *fdt, void *buf, int bufsize);
7cd5da0f
GVB
1195int fdt_pack(void *fdt);
1196
aec7135b
DG
1197/**
1198 * fdt_add_mem_rsv - add one memory reserve map entry
1199 * @fdt: pointer to the device tree blob
46390da1 1200 * @address, @size: 64-bit values (native endian)
aec7135b
DG
1201 *
1202 * Adds a reserve map entry to the given blob reserving a region at
1203 * address address of length size.
1204 *
1205 * This function will insert data into the reserve map and will
46390da1 1206 * therefore change the indexes of some entries in the table.
aec7135b
DG
1207 *
1208 * returns:
1209 * 0, on success
1210 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1211 * contain the new reservation entry
1212 * -FDT_ERR_BADMAGIC,
1213 * -FDT_ERR_BADVERSION,
1214 * -FDT_ERR_BADSTATE,
1215 * -FDT_ERR_BADSTRUCTURE,
1216 * -FDT_ERR_BADLAYOUT,
1217 * -FDT_ERR_TRUNCATED, standard meanings
1218 */
8d04f02f 1219int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
aec7135b
DG
1220
1221/**
1222 * fdt_del_mem_rsv - remove a memory reserve map entry
1223 * @fdt: pointer to the device tree blob
1224 * @n: entry to remove
1225 *
1226 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1227 * the blob.
1228 *
1229 * This function will delete data from the reservation table and will
46390da1 1230 * therefore change the indexes of some entries in the table.
aec7135b
DG
1231 *
1232 * returns:
1233 * 0, on success
1234 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1235 * are less than n+1 reserve map entries)
1236 * -FDT_ERR_BADMAGIC,
1237 * -FDT_ERR_BADVERSION,
1238 * -FDT_ERR_BADSTATE,
1239 * -FDT_ERR_BADSTRUCTURE,
1240 * -FDT_ERR_BADLAYOUT,
1241 * -FDT_ERR_TRUNCATED, standard meanings
1242 */
8d04f02f
KG
1243int fdt_del_mem_rsv(void *fdt, int n);
1244
9eaeb07a
DG
1245/**
1246 * fdt_set_name - change the name of a given node
1247 * @fdt: pointer to the device tree blob
1248 * @nodeoffset: structure block offset of a node
1249 * @name: name to give the node
1250 *
1251 * fdt_set_name() replaces the name (including unit address, if any)
1252 * of the given node with the given string. NOTE: this function can't
1253 * efficiently check if the new name is unique amongst the given
1254 * node's siblings; results are undefined if this function is invoked
1255 * with a name equal to one of the given node's siblings.
1256 *
1257 * This function may insert or delete data from the blob, and will
1258 * therefore change the offsets of some existing nodes.
1259 *
1260 * returns:
1261 * 0, on success
1262 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1263 * to contain the new name
1264 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1265 * -FDT_ERR_BADMAGIC,
1266 * -FDT_ERR_BADVERSION,
1267 * -FDT_ERR_BADSTATE, standard meanings
1268 */
1269int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1270
aec7135b
DG
1271/**
1272 * fdt_setprop - create or change a property
1273 * @fdt: pointer to the device tree blob
1274 * @nodeoffset: offset of the node whose property to change
1275 * @name: name of the property to change
1276 * @val: pointer to data to set the property value to
1277 * @len: length of the property value
1278 *
1279 * fdt_setprop() sets the value of the named property in the given
46390da1 1280 * node to the given value and length, creating the property if it
aec7135b
DG
1281 * does not already exist.
1282 *
1283 * This function may insert or delete data from the blob, and will
1284 * therefore change the offsets of some existing nodes.
1285 *
1286 * returns:
1287 * 0, on success
1288 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1289 * contain the new property value
1290 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1291 * -FDT_ERR_BADLAYOUT,
1292 * -FDT_ERR_BADMAGIC,
1293 * -FDT_ERR_BADVERSION,
1294 * -FDT_ERR_BADSTATE,
1295 * -FDT_ERR_BADSTRUCTURE,
1296 * -FDT_ERR_BADLAYOUT,
1297 * -FDT_ERR_TRUNCATED, standard meanings
1298 */
7cd5da0f
GVB
1299int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1300 const void *val, int len);
aec7135b
DG
1301
1302/**
24fa0e58 1303 * fdt_setprop_u32 - set a property to a 32-bit integer
aec7135b
DG
1304 * @fdt: pointer to the device tree blob
1305 * @nodeoffset: offset of the node whose property to change
1306 * @name: name of the property to change
1307 * @val: 32-bit integer value for the property (native endian)
1308 *
24fa0e58
DG
1309 * fdt_setprop_u32() sets the value of the named property in the given
1310 * node to the given 32-bit integer value (converting to big-endian if
aec7135b
DG
1311 * necessary), or creates a new property with that value if it does
1312 * not already exist.
1313 *
1314 * This function may insert or delete data from the blob, and will
1315 * therefore change the offsets of some existing nodes.
1316 *
1317 * returns:
1318 * 0, on success
1319 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1320 * contain the new property value
1321 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1322 * -FDT_ERR_BADLAYOUT,
1323 * -FDT_ERR_BADMAGIC,
1324 * -FDT_ERR_BADVERSION,
1325 * -FDT_ERR_BADSTATE,
1326 * -FDT_ERR_BADSTRUCTURE,
1327 * -FDT_ERR_BADLAYOUT,
1328 * -FDT_ERR_TRUNCATED, standard meanings
1329 */
24fa0e58
DG
1330static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1331 uint32_t val)
8d04f02f 1332{
b2ba62a1
KP
1333 fdt32_t tmp = cpu_to_fdt32(val);
1334 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
8d04f02f 1335}
aec7135b 1336
24fa0e58
DG
1337/**
1338 * fdt_setprop_u64 - set a property to a 64-bit integer
1339 * @fdt: pointer to the device tree blob
1340 * @nodeoffset: offset of the node whose property to change
1341 * @name: name of the property to change
1342 * @val: 64-bit integer value for the property (native endian)
1343 *
1344 * fdt_setprop_u64() sets the value of the named property in the given
1345 * node to the given 64-bit integer value (converting to big-endian if
1346 * necessary), or creates a new property with that value if it does
1347 * not already exist.
1348 *
1349 * This function may insert or delete data from the blob, and will
1350 * therefore change the offsets of some existing nodes.
1351 *
1352 * returns:
1353 * 0, on success
1354 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1355 * contain the new property value
1356 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1357 * -FDT_ERR_BADLAYOUT,
1358 * -FDT_ERR_BADMAGIC,
1359 * -FDT_ERR_BADVERSION,
1360 * -FDT_ERR_BADSTATE,
1361 * -FDT_ERR_BADSTRUCTURE,
1362 * -FDT_ERR_BADLAYOUT,
1363 * -FDT_ERR_TRUNCATED, standard meanings
1364 */
1365static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1366 uint64_t val)
1367{
b2ba62a1
KP
1368 fdt64_t tmp = cpu_to_fdt64(val);
1369 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
24fa0e58
DG
1370}
1371
1372/**
1373 * fdt_setprop_cell - set a property to a single cell value
1374 *
1375 * This is an alternative name for fdt_setprop_u32()
1376 */
1377static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1378 uint32_t val)
1379{
1380 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1381}
1382
aec7135b
DG
1383/**
1384 * fdt_setprop_string - set a property to a string value
1385 * @fdt: pointer to the device tree blob
1386 * @nodeoffset: offset of the node whose property to change
1387 * @name: name of the property to change
1388 * @str: string value for the property
1389 *
1390 * fdt_setprop_string() sets the value of the named property in the
1391 * given node to the given string value (using the length of the
1392 * string to determine the new length of the property), or creates a
1393 * new property with that value if it does not already exist.
1394 *
1395 * This function may insert or delete data from the blob, and will
1396 * therefore change the offsets of some existing nodes.
1397 *
1398 * returns:
1399 * 0, on success
1400 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1401 * contain the new property value
1402 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1403 * -FDT_ERR_BADLAYOUT,
1404 * -FDT_ERR_BADMAGIC,
1405 * -FDT_ERR_BADVERSION,
1406 * -FDT_ERR_BADSTATE,
1407 * -FDT_ERR_BADSTRUCTURE,
1408 * -FDT_ERR_BADLAYOUT,
1409 * -FDT_ERR_TRUNCATED, standard meanings
1410 */
7cd5da0f
GVB
1411#define fdt_setprop_string(fdt, nodeoffset, name, str) \
1412 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
aec7135b 1413
36ad18a6
ML
1414/**
1415 * fdt_appendprop - append to or create a property
1416 * @fdt: pointer to the device tree blob
1417 * @nodeoffset: offset of the node whose property to change
1418 * @name: name of the property to append to
1419 * @val: pointer to data to append to the property value
1420 * @len: length of the data to append to the property value
1421 *
1422 * fdt_appendprop() appends the value to the named property in the
1423 * given node, creating the property if it does not already exist.
1424 *
1425 * This function may insert data into the blob, and will therefore
1426 * change the offsets of some existing nodes.
1427 *
1428 * returns:
1429 * 0, on success
1430 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1431 * contain the new property value
1432 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1433 * -FDT_ERR_BADLAYOUT,
1434 * -FDT_ERR_BADMAGIC,
1435 * -FDT_ERR_BADVERSION,
1436 * -FDT_ERR_BADSTATE,
1437 * -FDT_ERR_BADSTRUCTURE,
1438 * -FDT_ERR_BADLAYOUT,
1439 * -FDT_ERR_TRUNCATED, standard meanings
1440 */
1441int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1442 const void *val, int len);
1443
1444/**
24fa0e58 1445 * fdt_appendprop_u32 - append a 32-bit integer value to a property
36ad18a6
ML
1446 * @fdt: pointer to the device tree blob
1447 * @nodeoffset: offset of the node whose property to change
1448 * @name: name of the property to change
1449 * @val: 32-bit integer value to append to the property (native endian)
1450 *
24fa0e58
DG
1451 * fdt_appendprop_u32() appends the given 32-bit integer value
1452 * (converting to big-endian if necessary) to the value of the named
1453 * property in the given node, or creates a new property with that
1454 * value if it does not already exist.
36ad18a6
ML
1455 *
1456 * This function may insert data into the blob, and will therefore
1457 * change the offsets of some existing nodes.
1458 *
1459 * returns:
1460 * 0, on success
1461 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1462 * contain the new property value
1463 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1464 * -FDT_ERR_BADLAYOUT,
1465 * -FDT_ERR_BADMAGIC,
1466 * -FDT_ERR_BADVERSION,
1467 * -FDT_ERR_BADSTATE,
1468 * -FDT_ERR_BADSTRUCTURE,
1469 * -FDT_ERR_BADLAYOUT,
1470 * -FDT_ERR_TRUNCATED, standard meanings
1471 */
24fa0e58
DG
1472static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1473 const char *name, uint32_t val)
36ad18a6 1474{
b2ba62a1
KP
1475 fdt32_t tmp = cpu_to_fdt32(val);
1476 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
36ad18a6
ML
1477}
1478
24fa0e58
DG
1479/**
1480 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1481 * @fdt: pointer to the device tree blob
1482 * @nodeoffset: offset of the node whose property to change
1483 * @name: name of the property to change
1484 * @val: 64-bit integer value to append to the property (native endian)
1485 *
1486 * fdt_appendprop_u64() appends the given 64-bit integer value
1487 * (converting to big-endian if necessary) to the value of the named
1488 * property in the given node, or creates a new property with that
1489 * value if it does not already exist.
1490 *
1491 * This function may insert data into the blob, and will therefore
1492 * change the offsets of some existing nodes.
1493 *
1494 * returns:
1495 * 0, on success
1496 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1497 * contain the new property value
1498 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1499 * -FDT_ERR_BADLAYOUT,
1500 * -FDT_ERR_BADMAGIC,
1501 * -FDT_ERR_BADVERSION,
1502 * -FDT_ERR_BADSTATE,
1503 * -FDT_ERR_BADSTRUCTURE,
1504 * -FDT_ERR_BADLAYOUT,
1505 * -FDT_ERR_TRUNCATED, standard meanings
1506 */
1507static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1508 const char *name, uint64_t val)
1509{
b2ba62a1
KP
1510 fdt64_t tmp = cpu_to_fdt64(val);
1511 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
24fa0e58
DG
1512}
1513
1514/**
1515 * fdt_appendprop_cell - append a single cell value to a property
1516 *
1517 * This is an alternative name for fdt_appendprop_u32()
1518 */
1519static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1520 const char *name, uint32_t val)
1521{
1522 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1523}
1524
36ad18a6
ML
1525/**
1526 * fdt_appendprop_string - append a string to a property
1527 * @fdt: pointer to the device tree blob
1528 * @nodeoffset: offset of the node whose property to change
1529 * @name: name of the property to change
1530 * @str: string value to append to the property
1531 *
1532 * fdt_appendprop_string() appends the given string to the value of
1533 * the named property in the given node, or creates a new property
1534 * with that value if it does not already exist.
1535 *
1536 * This function may insert data into the blob, and will therefore
1537 * change the offsets of some existing nodes.
1538 *
1539 * returns:
1540 * 0, on success
1541 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1542 * contain the new property value
1543 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1544 * -FDT_ERR_BADLAYOUT,
1545 * -FDT_ERR_BADMAGIC,
1546 * -FDT_ERR_BADVERSION,
1547 * -FDT_ERR_BADSTATE,
1548 * -FDT_ERR_BADSTRUCTURE,
1549 * -FDT_ERR_BADLAYOUT,
1550 * -FDT_ERR_TRUNCATED, standard meanings
1551 */
1552#define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1553 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1554
aec7135b
DG
1555/**
1556 * fdt_delprop - delete a property
1557 * @fdt: pointer to the device tree blob
1558 * @nodeoffset: offset of the node whose property to nop
1559 * @name: name of the property to nop
1560 *
1561 * fdt_del_property() will delete the given property.
1562 *
1563 * This function will delete data from the blob, and will therefore
1564 * change the offsets of some existing nodes.
1565 *
1566 * returns:
1567 * 0, on success
1568 * -FDT_ERR_NOTFOUND, node does not have the named property
1569 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1570 * -FDT_ERR_BADLAYOUT,
1571 * -FDT_ERR_BADMAGIC,
1572 * -FDT_ERR_BADVERSION,
1573 * -FDT_ERR_BADSTATE,
1574 * -FDT_ERR_BADSTRUCTURE,
1575 * -FDT_ERR_TRUNCATED, standard meanings
1576 */
7cd5da0f 1577int fdt_delprop(void *fdt, int nodeoffset, const char *name);
aec7135b
DG
1578
1579/**
1580 * fdt_add_subnode_namelen - creates a new node based on substring
1581 * @fdt: pointer to the device tree blob
1582 * @parentoffset: structure block offset of a node
1583 * @name: name of the subnode to locate
1584 * @namelen: number of characters of name to consider
1585 *
1586 * Identical to fdt_add_subnode(), but use only the first namelen
1587 * characters of name as the name of the new node. This is useful for
1588 * creating subnodes based on a portion of a larger string, such as a
1589 * full path.
1590 */
7cd5da0f
GVB
1591int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1592 const char *name, int namelen);
aec7135b
DG
1593
1594/**
1595 * fdt_add_subnode - creates a new node
1596 * @fdt: pointer to the device tree blob
1597 * @parentoffset: structure block offset of a node
1598 * @name: name of the subnode to locate
1599 *
1600 * fdt_add_subnode() creates a new node as a subnode of the node at
1601 * structure block offset parentoffset, with the given name (which
1602 * should include the unit address, if any).
1603 *
1604 * This function will insert data into the blob, and will therefore
1605 * change the offsets of some existing nodes.
1606
1607 * returns:
1608 * structure block offset of the created nodeequested subnode (>=0), on success
1609 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1610 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1611 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1612 * the given name
1613 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1614 * blob to contain the new node
1615 * -FDT_ERR_NOSPACE
1616 * -FDT_ERR_BADLAYOUT
1617 * -FDT_ERR_BADMAGIC,
1618 * -FDT_ERR_BADVERSION,
1619 * -FDT_ERR_BADSTATE,
1620 * -FDT_ERR_BADSTRUCTURE,
1621 * -FDT_ERR_TRUNCATED, standard meanings.
1622 */
7cd5da0f 1623int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
aec7135b
DG
1624
1625/**
1626 * fdt_del_node - delete a node (subtree)
1627 * @fdt: pointer to the device tree blob
1628 * @nodeoffset: offset of the node to nop
1629 *
1630 * fdt_del_node() will remove the given node, including all its
1631 * subnodes if any, from the blob.
1632 *
1633 * This function will delete data from the blob, and will therefore
1634 * change the offsets of some existing nodes.
1635 *
1636 * returns:
1637 * 0, on success
1638 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1639 * -FDT_ERR_BADLAYOUT,
1640 * -FDT_ERR_BADMAGIC,
1641 * -FDT_ERR_BADVERSION,
1642 * -FDT_ERR_BADSTATE,
1643 * -FDT_ERR_BADSTRUCTURE,
1644 * -FDT_ERR_TRUNCATED, standard meanings
1645 */
7cd5da0f
GVB
1646int fdt_del_node(void *fdt, int nodeoffset);
1647
8d04f02f
KG
1648/**********************************************************************/
1649/* Debugging / informational functions */
1650/**********************************************************************/
1651
7cd5da0f
GVB
1652const char *fdt_strerror(int errval);
1653
8f6e2e1e
SG
1654/**
1655 * fdt_remove_unused_strings() - Remove any unused strings from an FDT
1656 *
1657 * This creates a new device tree in @new with unused strings removed. The
1658 * called can then use fdt_pack() to minimise the space consumed.
1659 *
1660 * @old: Old device tree blog
1661 * @new: Place to put new device tree blob, which must be as large as
1662 * @old
1663 * @return
1664 * 0, on success
1665 * -FDT_ERR_BADOFFSET, corrupt device tree
1666 * -FDT_ERR_NOSPACE, out of space, which should not happen unless there
1667 * is something very wrong with the device tree input
1668 */
1669int fdt_remove_unused_strings(const void *old, void *new);
1670
3e06cd1f
SG
1671struct fdt_region {
1672 int offset;
1673 int size;
1674};
1675
c3c4c005
SG
1676/*
1677 * Flags for fdt_find_regions()
1678 *
1679 * Add a region for the string table (always the last region)
1680 */
1681#define FDT_REG_ADD_STRING_TAB (1 << 0)
1682
1683/*
1684 * Add all supernodes of a matching node/property, useful for creating a
1685 * valid subset tree
1686 */
1687#define FDT_REG_SUPERNODES (1 << 1)
1688
1689/* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
1690#define FDT_REG_DIRECT_SUBNODES (1 << 2)
1691
1692/* Add all subnodes of a matching node */
1693#define FDT_REG_ALL_SUBNODES (1 << 3)
1694
1695/* Add a region for the mem_rsvmap table (always the first region) */
1696#define FDT_REG_ADD_MEM_RSVMAP (1 << 4)
1697
1698/* Indicates what an fdt part is (node, property, value) */
1699#define FDT_IS_NODE (1 << 0)
1700#define FDT_IS_PROP (1 << 1)
1701#define FDT_IS_VALUE (1 << 2) /* not supported */
1702#define FDT_IS_COMPAT (1 << 3) /* used internally */
1703#define FDT_NODE_HAS_PROP (1 << 4) /* node contains prop */
1704
1705#define FDT_ANY_GLOBAL (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
1706 FDT_IS_COMPAT)
1707#define FDT_IS_ANY 0x1f /* all the above */
1708
1709/* We set a reasonable limit on the number of nested nodes */
1710#define FDT_MAX_DEPTH 32
1711
1712/* Decribes what we want to include from the current tag */
1713enum want_t {
1714 WANT_NOTHING,
1715 WANT_NODES_ONLY, /* No properties */
1716 WANT_NODES_AND_PROPS, /* Everything for one level */
1717 WANT_ALL_NODES_AND_PROPS /* Everything for all levels */
1718};
1719
1720/* Keeps track of the state at parent nodes */
1721struct fdt_subnode_stack {
1722 int offset; /* Offset of node */
1723 enum want_t want; /* The 'want' value here */
1724 int included; /* 1 if we included this node, 0 if not */
1725};
1726
1727struct fdt_region_ptrs {
1728 int depth; /* Current tree depth */
1729 int done; /* What we have completed scanning */
1730 enum want_t want; /* What we are currently including */
1731 char *end; /* Pointer to end of full node path */
1732 int nextoffset; /* Next node offset to check */
1733};
1734
1735/* The state of our finding algortihm */
1736struct fdt_region_state {
1737 struct fdt_subnode_stack stack[FDT_MAX_DEPTH]; /* node stack */
1738 struct fdt_region *region; /* Contains list of regions found */
1739 int count; /* Numnber of regions found */
1740 const void *fdt; /* FDT blob */
1741 int max_regions; /* Maximum regions to find */
1742 int can_merge; /* 1 if we can merge with previous region */
1743 int start; /* Start position of current region */
1744 struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */
1745};
1746
3e06cd1f
SG
1747/**
1748 * fdt_find_regions() - find regions in device tree
1749 *
1750 * Given a list of nodes to include and properties to exclude, find
1751 * the regions of the device tree which describe those included parts.
1752 *
1753 * The intent is to get a list of regions which will be invariant provided
1754 * those parts are invariant. For example, if you request a list of regions
1755 * for all nodes but exclude the property "data", then you will get the
1756 * same region contents regardless of any change to "data" properties.
1757 *
1758 * This function can be used to produce a byte-stream to send to a hashing
1759 * function to verify that critical parts of the FDT have not changed.
1760 *
1761 * Nodes which are given in 'inc' are included in the region list, as
1762 * are the names of the immediate subnodes nodes (but not the properties
1763 * or subnodes of those subnodes).
1764 *
1765 * For eaxample "/" means to include the root node, all root properties
1766 * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1767 * ensures that we capture the names of the subnodes. In a hashing situation
1768 * it prevents the root node from changing at all Any change to non-excluded
1769 * properties, names of subnodes or number of subnodes would be detected.
1770 *
1771 * When used with FITs this provides the ability to hash and sign parts of
1772 * the FIT based on different configurations in the FIT. Then it is
1773 * impossible to change anything about that configuration (include images
1774 * attached to the configuration), but it may be possible to add new
1775 * configurations, new images or new signatures within the existing
1776 * framework.
1777 *
1778 * Adding new properties to a device tree may result in the string table
1779 * being extended (if the new property names are different from those
1780 * already added). This function can optionally include a region for
1781 * the string table so that this can be part of the hash too.
1782 *
1783 * The device tree header is not included in the list.
1784 *
1785 * @fdt: Device tree to check
1786 * @inc: List of node paths to included
1787 * @inc_count: Number of node paths in list
1788 * @exc_prop: List of properties names to exclude
1789 * @exc_prop_count: Number of properties in exclude list
1790 * @region: Returns list of regions
1791 * @max_region: Maximum length of region list
1792 * @path: Pointer to a temporary string for the function to use for
1793 * building path names
1794 * @path_len: Length of path, must be large enough to hold the longest
1795 * path in the tree
1796 * @add_string_tab: 1 to add a region for the string table
1797 * @return number of regions in list. If this is >max_regions then the
1798 * region array was exhausted. You should increase max_regions and try
1799 * the call again.
1800 */
1801int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
1802 char * const exc_prop[], int exc_prop_count,
1803 struct fdt_region region[], int max_regions,
1804 char *path, int path_len, int add_string_tab);
1805
c3c4c005
SG
1806/**
1807 * fdt_first_region() - find regions in device tree
1808 *
1809 * Given a nodes and properties to include and properties to exclude, find
1810 * the regions of the device tree which describe those included parts.
1811 *
1812 * The use for this function is twofold. Firstly it provides a convenient
1813 * way of performing a structure-aware grep of the tree. For example it is
1814 * possible to grep for a node and get all the properties associated with
1815 * that node. Trees can be subsetted easily, by specifying the nodes that
1816 * are required, and then writing out the regions returned by this function.
1817 * This is useful for small resource-constrained systems, such as boot
1818 * loaders, which want to use an FDT but do not need to know about all of
1819 * it.
1820 *
1821 * Secondly it makes it easy to hash parts of the tree and detect changes.
1822 * The intent is to get a list of regions which will be invariant provided
1823 * those parts are invariant. For example, if you request a list of regions
1824 * for all nodes but exclude the property "data", then you will get the
1825 * same region contents regardless of any change to "data" properties.
1826 *
1827 * This function can be used to produce a byte-stream to send to a hashing
1828 * function to verify that critical parts of the FDT have not changed.
1829 * Note that semantically null changes in order could still cause false
1830 * hash misses. Such reordering might happen if the tree is regenerated
1831 * from source, and nodes are reordered (the bytes-stream will be emitted
1832 * in a different order and mnay hash functions will detect this). However
1833 * if an existing tree is modified using libfdt functions, such as
1834 * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
1835 *
1836 * The nodes/properties to include/exclude are defined by a function
1837 * provided by the caller. This function is called for each node and
1838 * property, and must return:
1839 *
1840 * 0 - to exclude this part
1841 * 1 - to include this part
1842 * -1 - for FDT_IS_PROP only: no information is available, so include
1843 * if its containing node is included
1844 *
1845 * The last case is only used to deal with properties. Often a property is
1846 * included if its containing node is included - this is the case where
1847 * -1 is returned.. However if the property is specifically required to be
1848 * included/excluded, then 0 or 1 can be returned. Note that including a
1849 * property when the FDT_REG_SUPERNODES flag is given will force its
1850 * containing node to be included since it is not valid to have a property
1851 * that is not in a node.
1852 *
1853 * Using the information provided, the inclusion of a node can be controlled
1854 * either by a node name or its compatible string, or any other property
1855 * that the function can determine.
1856 *
1857 * As an example, including node "/" means to include the root node and all
1858 * root properties. A flag provides a way of also including supernodes (of
1859 * which there is none for the root node), and another flag includes
1860 * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
1861 * FDT_END_NODE of all subnodes of /.
1862 *
1863 * The subnode feature helps in a hashing situation since it prevents the
1864 * root node from changing at all. Any change to non-excluded properties,
1865 * names of subnodes or number of subnodes would be detected.
1866 *
1867 * When used with FITs this provides the ability to hash and sign parts of
1868 * the FIT based on different configurations in the FIT. Then it is
1869 * impossible to change anything about that configuration (include images
1870 * attached to the configuration), but it may be possible to add new
1871 * configurations, new images or new signatures within the existing
1872 * framework.
1873 *
1874 * Adding new properties to a device tree may result in the string table
1875 * being extended (if the new property names are different from those
1876 * already added). This function can optionally include a region for
1877 * the string table so that this can be part of the hash too. This is always
1878 * the last region.
1879 *
1880 * The FDT also has a mem_rsvmap table which can also be included, and is
1881 * always the first region if so.
1882 *
1883 * The device tree header is not included in the region list. Since the
1884 * contents of the FDT are changing (shrinking, often), the caller will need
1885 * to regenerate the header anyway.
1886 *
1887 * @fdt: Device tree to check
1888 * @h_include: Function to call to determine whether to include a part or
1889 * not:
1890 *
1891 * @priv: Private pointer as passed to fdt_find_regions()
1892 * @fdt: Pointer to FDT blob
1893 * @offset: Offset of this node / property
1894 * @type: Type of this part, FDT_IS_...
1895 * @data: Pointer to data (node name, property name, compatible
1896 * string, value (not yet supported)
1897 * @size: Size of data, or 0 if none
1898 * @return 0 to exclude, 1 to include, -1 if no information is
1899 * available
1900 * @priv: Private pointer passed to h_include
1901 * @region: Returns list of regions, sorted by offset
1902 * @max_regions: Maximum length of region list
1903 * @path: Pointer to a temporary string for the function to use for
1904 * building path names
1905 * @path_len: Length of path, must be large enough to hold the longest
1906 * path in the tree
1907 * @flags: Various flags that control the region algortihm, see
1908 * FDT_REG_...
1909 * @return number of regions in list. If this is >max_regions then the
1910 * region array was exhausted. You should increase max_regions and try
1911 * the call again. Only the first max_regions elements are available in the
1912 * array.
1913 *
1914 * On error a -ve value is return, which can be:
1915 *
1916 * -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
1917 * -FDT_ERR_BADLAYOUT
1918 * -FDT_ERR_NOSPACE (path area is too small)
1919 */
1920int fdt_first_region(const void *fdt,
1921 int (*h_include)(void *priv, const void *fdt, int offset,
1922 int type, const char *data, int size),
1923 void *priv, struct fdt_region *region,
1924 char *path, int path_len, int flags,
1925 struct fdt_region_state *info);
1926
1927/** fdt_next_region() - find next region
1928 *
1929 * See fdt_first_region() for full description. This function finds the
1930 * next region according to the provided parameters, which must be the same
1931 * as passed to fdt_first_region().
1932 *
1933 * This function can additionally return -FDT_ERR_NOTFOUND when there are no
1934 * more regions
1935 */
1936int fdt_next_region(const void *fdt,
1937 int (*h_include)(void *priv, const void *fdt, int offset,
1938 int type, const char *data, int size),
1939 void *priv, struct fdt_region *region,
1940 char *path, int path_len, int flags,
1941 struct fdt_region_state *info);
1942
1943/**
1944 * fdt_add_alias_regions() - find aliases that point to existing regions
1945 *
1946 * Once a device tree grep is complete some of the nodes will be present
1947 * and some will have been dropped. This function checks all the alias nodes
1948 * to figure out which points point to nodes which are still present. These
1949 * aliases need to be kept, along with the nodes they reference.
1950 *
1951 * Given a list of regions function finds the aliases that still apply and
1952 * adds more regions to the list for these. This function is called after
1953 * fdt_next_region() has finished returning regions and requires the same
1954 * state.
1955 *
1956 * @fdt: Device tree file to reference
1957 * @region: List of regions that will be kept
1958 * @count: Number of regions
1959 * @max_regions: Number of entries that can fit in @region
1960 * @info: Region state as returned from fdt_next_region()
1961 * @return new number of regions in @region (i.e. count + the number added)
1962 * or -FDT_ERR_NOSPACE if there was not enough space.
1963 */
1964int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
1965 int max_regions, struct fdt_region_state *info);
1966
7cd5da0f 1967#endif /* _LIBFDT_H */