]> git.ipfire.org Git - people/ms/u-boot.git/blame - include/libfdt.h
libfdt: Add iterator over properties
[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
67e610d9
MR
443/**
444 * fdt_for_each_property - iterate over all properties of a node
445 * @property_offset: property offset (int)
446 * @fdt: FDT blob (const void *)
447 * @node: node offset (int)
448 *
449 * This is actually a wrapper around a for loop and would be used like so:
450 *
451 * fdt_for_each_property(fdt, node, property) {
452 * ...
453 * use property
454 * ...
455 * }
456 *
457 * Note that this is implemented as a macro and property is used as
458 * iterator in the loop. It should therefore be a locally allocated
459 * variable. The node variable on the other hand is never modified, so
460 * it can be constant or even a literal.
461 */
462#define fdt_for_each_property_offset(property, fdt, node) \
463 for (property = fdt_first_property_offset(fdt, node); \
464 property >= 0; \
465 property = fdt_next_property_offset(fdt, property))
466
d1c63148
DG
467/**
468 * fdt_get_property_by_offset - retrieve the property at a given offset
469 * @fdt: pointer to the device tree blob
470 * @offset: offset of the property to retrieve
471 * @lenp: pointer to an integer variable (will be overwritten) or NULL
472 *
473 * fdt_get_property_by_offset() retrieves a pointer to the
474 * fdt_property structure within the device tree blob at the given
475 * offset. If lenp is non-NULL, the length of the property value is
476 * also returned, in the integer pointed to by lenp.
477 *
478 * returns:
479 * pointer to the structure representing the property
480 * if lenp is non-NULL, *lenp contains the length of the property
481 * value (>=0)
482 * NULL, on error
483 * if lenp is non-NULL, *lenp contains an error code (<0):
484 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
485 * -FDT_ERR_BADMAGIC,
486 * -FDT_ERR_BADVERSION,
487 * -FDT_ERR_BADSTATE,
488 * -FDT_ERR_BADSTRUCTURE,
489 * -FDT_ERR_TRUNCATED, standard meanings
490 */
491const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
492 int offset,
493 int *lenp);
494
0219399a
DG
495/**
496 * fdt_get_property_namelen - find a property based on substring
497 * @fdt: pointer to the device tree blob
498 * @nodeoffset: offset of the node whose property to find
499 * @name: name of the property to find
500 * @namelen: number of characters of name to consider
501 * @lenp: pointer to an integer variable (will be overwritten) or NULL
502 *
503 * Identical to fdt_get_property_namelen(), but only examine the first
504 * namelen characters of name for matching the property name.
505 */
506const struct fdt_property *fdt_get_property_namelen(const void *fdt,
507 int nodeoffset,
508 const char *name,
509 int namelen, int *lenp);
510
8d04f02f
KG
511/**
512 * fdt_get_property - find a given property in a given node
513 * @fdt: pointer to the device tree blob
514 * @nodeoffset: offset of the node whose property to find
515 * @name: name of the property to find
516 * @lenp: pointer to an integer variable (will be overwritten) or NULL
517 *
518 * fdt_get_property() retrieves a pointer to the fdt_property
519 * structure within the device tree blob corresponding to the property
520 * named 'name' of the node at offset nodeoffset. If lenp is
46390da1 521 * non-NULL, the length of the property value is also returned, in the
8d04f02f
KG
522 * integer pointed to by lenp.
523 *
524 * returns:
525 * pointer to the structure representing the property
526 * if lenp is non-NULL, *lenp contains the length of the property
527 * value (>=0)
528 * NULL, on error
529 * if lenp is non-NULL, *lenp contains an error code (<0):
530 * -FDT_ERR_NOTFOUND, node does not have named property
531 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
532 * -FDT_ERR_BADMAGIC,
533 * -FDT_ERR_BADVERSION,
534 * -FDT_ERR_BADSTATE,
535 * -FDT_ERR_BADSTRUCTURE,
536 * -FDT_ERR_TRUNCATED, standard meanings
537 */
538const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
539 const char *name, int *lenp);
540static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
541 const char *name,
542 int *lenp)
543{
c6683026
DG
544 return (struct fdt_property *)(uintptr_t)
545 fdt_get_property(fdt, nodeoffset, name, lenp);
8d04f02f
KG
546}
547
d1c63148
DG
548/**
549 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
550 * @fdt: pointer to the device tree blob
551 * @ffset: offset of the property to read
552 * @namep: pointer to a string variable (will be overwritten) or NULL
553 * @lenp: pointer to an integer variable (will be overwritten) or NULL
554 *
555 * fdt_getprop_by_offset() retrieves a pointer to the value of the
556 * property at structure block offset 'offset' (this will be a pointer
557 * to within the device blob itself, not a copy of the value). If
558 * lenp is non-NULL, the length of the property value is also
559 * returned, in the integer pointed to by lenp. If namep is non-NULL,
560 * the property's namne will also be returned in the char * pointed to
561 * by namep (this will be a pointer to within the device tree's string
562 * block, not a new copy of the name).
563 *
564 * returns:
565 * pointer to the property's value
566 * if lenp is non-NULL, *lenp contains the length of the property
567 * value (>=0)
568 * if namep is non-NULL *namep contiains a pointer to the property
569 * name.
570 * NULL, on error
571 * if lenp is non-NULL, *lenp contains an error code (<0):
572 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
573 * -FDT_ERR_BADMAGIC,
574 * -FDT_ERR_BADVERSION,
575 * -FDT_ERR_BADSTATE,
576 * -FDT_ERR_BADSTRUCTURE,
577 * -FDT_ERR_TRUNCATED, standard meanings
578 */
579const void *fdt_getprop_by_offset(const void *fdt, int offset,
580 const char **namep, int *lenp);
581
0219399a
DG
582/**
583 * fdt_getprop_namelen - get property value based on substring
584 * @fdt: pointer to the device tree blob
585 * @nodeoffset: offset of the node whose property to find
586 * @name: name of the property to find
587 * @namelen: number of characters of name to consider
588 * @lenp: pointer to an integer variable (will be overwritten) or NULL
589 *
590 * Identical to fdt_getprop(), but only examine the first namelen
591 * characters of name for matching the property name.
592 */
593const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
594 const char *name, int namelen, int *lenp);
595
8d04f02f
KG
596/**
597 * fdt_getprop - retrieve the value of a given property
598 * @fdt: pointer to the device tree blob
599 * @nodeoffset: offset of the node whose property to find
600 * @name: name of the property to find
601 * @lenp: pointer to an integer variable (will be overwritten) or NULL
602 *
603 * fdt_getprop() retrieves a pointer to the value of the property
604 * named 'name' of the node at offset nodeoffset (this will be a
605 * pointer to within the device blob itself, not a copy of the value).
46390da1 606 * If lenp is non-NULL, the length of the property value is also
8d04f02f
KG
607 * returned, in the integer pointed to by lenp.
608 *
609 * returns:
610 * pointer to the property's value
611 * if lenp is non-NULL, *lenp contains the length of the property
612 * value (>=0)
613 * NULL, on error
614 * if lenp is non-NULL, *lenp contains an error code (<0):
615 * -FDT_ERR_NOTFOUND, node does not have named property
616 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
617 * -FDT_ERR_BADMAGIC,
618 * -FDT_ERR_BADVERSION,
619 * -FDT_ERR_BADSTATE,
620 * -FDT_ERR_BADSTRUCTURE,
621 * -FDT_ERR_TRUNCATED, standard meanings
622 */
623const void *fdt_getprop(const void *fdt, int nodeoffset,
624 const char *name, int *lenp);
625static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
626 const char *name, int *lenp)
627{
c6683026 628 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
8d04f02f
KG
629}
630
631/**
46390da1 632 * fdt_get_phandle - retrieve the phandle of a given node
8d04f02f
KG
633 * @fdt: pointer to the device tree blob
634 * @nodeoffset: structure block offset of the node
635 *
636 * fdt_get_phandle() retrieves the phandle of the device tree node at
637 * structure block offset nodeoffset.
638 *
639 * returns:
46390da1 640 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
8d04f02f
KG
641 * 0, if the node has no phandle, or another error occurs
642 */
643uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
644
9a6cf73a 645/**
cd3cb0d9 646 * fdt_get_alias_namelen - get alias based on substring
9a6cf73a
DG
647 * @fdt: pointer to the device tree blob
648 * @name: name of the alias th look up
649 * @namelen: number of characters of name to consider
650 *
651 * Identical to fdt_get_alias(), but only examine the first namelen
652 * characters of name for matching the alias name.
653 */
654const char *fdt_get_alias_namelen(const void *fdt,
655 const char *name, int namelen);
656
657/**
0596d35d 658 * fdt_get_alias - retreive the path referenced by a given alias
9a6cf73a 659 * @fdt: pointer to the device tree blob
0596d35d 660 * @name: name of the alias th look up
9a6cf73a
DG
661 *
662 * fdt_get_alias() retrieves the value of a given alias. That is, the
663 * value of the property named 'name' in the node /aliases.
664 *
665 * returns:
c73bd49d 666 * a pointer to the expansion of the alias named 'name', if it exists
9a6cf73a
DG
667 * NULL, if the given alias or the /aliases node does not exist
668 */
669const char *fdt_get_alias(const void *fdt, const char *name);
670
8d04f02f
KG
671/**
672 * fdt_get_path - determine the full path of a node
673 * @fdt: pointer to the device tree blob
674 * @nodeoffset: offset of the node whose path to find
675 * @buf: character buffer to contain the returned path (will be overwritten)
676 * @buflen: size of the character buffer at buf
677 *
678 * fdt_get_path() computes the full path of the node at offset
679 * nodeoffset, and records that path in the buffer at buf.
680 *
681 * NOTE: This function is expensive, as it must scan the device tree
682 * structure from the start to nodeoffset.
683 *
684 * returns:
685 * 0, on success
686 * buf contains the absolute path of the node at
687 * nodeoffset, as a NUL-terminated string.
53677ef1 688 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
689 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
690 * characters and will not fit in the given buffer.
691 * -FDT_ERR_BADMAGIC,
692 * -FDT_ERR_BADVERSION,
693 * -FDT_ERR_BADSTATE,
694 * -FDT_ERR_BADSTRUCTURE, standard meanings
695 */
696int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
697
698/**
699 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
700 * @fdt: pointer to the device tree blob
701 * @nodeoffset: offset of the node whose parent to find
702 * @supernodedepth: depth of the ancestor to find
703 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
704 *
705 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
706 * at a specific depth from the root (where the root itself has depth
707 * 0, its immediate subnodes depth 1 and so forth). So
708 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
709 * will always return 0, the offset of the root node. If the node at
710 * nodeoffset has depth D, then:
711 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
712 * will return nodeoffset itself.
713 *
714 * NOTE: This function is expensive, as it must scan the device tree
715 * structure from the start to nodeoffset.
716 *
717 * returns:
718
719 * structure block offset of the node at node offset's ancestor
720 * of depth supernodedepth (>=0), on success
53677ef1 721 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
722* -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
723 * -FDT_ERR_BADMAGIC,
724 * -FDT_ERR_BADVERSION,
725 * -FDT_ERR_BADSTATE,
726 * -FDT_ERR_BADSTRUCTURE, standard meanings
727 */
728int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
729 int supernodedepth, int *nodedepth);
730
731/**
732 * fdt_node_depth - find the depth of a given node
733 * @fdt: pointer to the device tree blob
734 * @nodeoffset: offset of the node whose parent to find
735 *
736 * fdt_node_depth() finds the depth of a given node. The root node
737 * has depth 0, its immediate subnodes depth 1 and so forth.
738 *
739 * NOTE: This function is expensive, as it must scan the device tree
740 * structure from the start to nodeoffset.
741 *
742 * returns:
743 * depth of the node at nodeoffset (>=0), on success
53677ef1 744 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
745 * -FDT_ERR_BADMAGIC,
746 * -FDT_ERR_BADVERSION,
747 * -FDT_ERR_BADSTATE,
748 * -FDT_ERR_BADSTRUCTURE, standard meanings
749 */
750int fdt_node_depth(const void *fdt, int nodeoffset);
751
752/**
753 * fdt_parent_offset - find the parent of a given node
754 * @fdt: pointer to the device tree blob
755 * @nodeoffset: offset of the node whose parent to find
756 *
757 * fdt_parent_offset() locates the parent node of a given node (that
758 * is, it finds the offset of the node which contains the node at
759 * nodeoffset as a subnode).
760 *
761 * NOTE: This function is expensive, as it must scan the device tree
762 * structure from the start to nodeoffset, *twice*.
763 *
764 * returns:
46390da1 765 * structure block offset of the parent of the node at nodeoffset
8d04f02f 766 * (>=0), on success
53677ef1 767 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
768 * -FDT_ERR_BADMAGIC,
769 * -FDT_ERR_BADVERSION,
770 * -FDT_ERR_BADSTATE,
771 * -FDT_ERR_BADSTRUCTURE, standard meanings
772 */
773int fdt_parent_offset(const void *fdt, int nodeoffset);
774
775/**
776 * fdt_node_offset_by_prop_value - find nodes with a given property value
777 * @fdt: pointer to the device tree blob
778 * @startoffset: only find nodes after this offset
779 * @propname: property name to check
780 * @propval: property value to search for
781 * @proplen: length of the value in propval
782 *
783 * fdt_node_offset_by_prop_value() returns the offset of the first
784 * node after startoffset, which has a property named propname whose
785 * value is of length proplen and has value equal to propval; or if
786 * startoffset is -1, the very first such node in the tree.
787 *
788 * To iterate through all nodes matching the criterion, the following
789 * idiom can be used:
790 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
791 * propval, proplen);
792 * while (offset != -FDT_ERR_NOTFOUND) {
0596d35d 793 * // other code here
8d04f02f
KG
794 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
795 * propval, proplen);
796 * }
797 *
798 * Note the -1 in the first call to the function, if 0 is used here
799 * instead, the function will never locate the root node, even if it
800 * matches the criterion.
801 *
802 * returns:
803 * structure block offset of the located node (>= 0, >startoffset),
804 * on success
805 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
806 * tree after startoffset
53677ef1 807 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
808 * -FDT_ERR_BADMAGIC,
809 * -FDT_ERR_BADVERSION,
810 * -FDT_ERR_BADSTATE,
811 * -FDT_ERR_BADSTRUCTURE, standard meanings
812 */
813int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
814 const char *propname,
815 const void *propval, int proplen);
816
817/**
818 * fdt_node_offset_by_phandle - find the node with a given phandle
819 * @fdt: pointer to the device tree blob
820 * @phandle: phandle value
821 *
46390da1 822 * fdt_node_offset_by_phandle() returns the offset of the node
8d04f02f
KG
823 * which has the given phandle value. If there is more than one node
824 * in the tree with the given phandle (an invalid tree), results are
825 * undefined.
826 *
827 * returns:
828 * structure block offset of the located node (>= 0), on success
829 * -FDT_ERR_NOTFOUND, no node with that phandle exists
830 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
831 * -FDT_ERR_BADMAGIC,
832 * -FDT_ERR_BADVERSION,
833 * -FDT_ERR_BADSTATE,
834 * -FDT_ERR_BADSTRUCTURE, standard meanings
835 */
836int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
837
838/**
839 * fdt_node_check_compatible: check a node's compatible property
840 * @fdt: pointer to the device tree blob
841 * @nodeoffset: offset of a tree node
842 * @compatible: string to match against
843 *
844 *
845 * fdt_node_check_compatible() returns 0 if the given node contains a
846 * 'compatible' property with the given string as one of its elements,
847 * it returns non-zero otherwise, or on error.
848 *
849 * returns:
850 * 0, if the node has a 'compatible' property listing the given string
851 * 1, if the node has a 'compatible' property, but it does not list
852 * the given string
853 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
53677ef1 854 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
855 * -FDT_ERR_BADMAGIC,
856 * -FDT_ERR_BADVERSION,
857 * -FDT_ERR_BADSTATE,
858 * -FDT_ERR_BADSTRUCTURE, standard meanings
859 */
860int fdt_node_check_compatible(const void *fdt, int nodeoffset,
861 const char *compatible);
7cd5da0f 862
8d04f02f
KG
863/**
864 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
865 * @fdt: pointer to the device tree blob
866 * @startoffset: only find nodes after this offset
867 * @compatible: 'compatible' string to match against
868 *
869 * fdt_node_offset_by_compatible() returns the offset of the first
870 * node after startoffset, which has a 'compatible' property which
871 * lists the given compatible string; or if startoffset is -1, the
872 * very first such node in the tree.
873 *
874 * To iterate through all nodes matching the criterion, the following
875 * idiom can be used:
876 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
877 * while (offset != -FDT_ERR_NOTFOUND) {
0596d35d 878 * // other code here
8d04f02f
KG
879 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
880 * }
881 *
882 * Note the -1 in the first call to the function, if 0 is used here
883 * instead, the function will never locate the root node, even if it
884 * matches the criterion.
885 *
886 * returns:
887 * structure block offset of the located node (>= 0, >startoffset),
888 * on success
889 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
890 * tree after startoffset
53677ef1 891 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
8d04f02f
KG
892 * -FDT_ERR_BADMAGIC,
893 * -FDT_ERR_BADVERSION,
894 * -FDT_ERR_BADSTATE,
895 * -FDT_ERR_BADSTRUCTURE, standard meanings
896 */
897int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
898 const char *compatible);
7cd5da0f 899
e853b324
SG
900/**
901 * fdt_stringlist_contains - check a string list property for a string
902 * @strlist: Property containing a list of strings to check
903 * @listlen: Length of property
904 * @str: String to search for
905 *
906 * This is a utility function provided for convenience. The list contains
907 * one or more strings, each terminated by \0, as is found in a device tree
908 * "compatible" property.
909 *
910 * @return: 1 if the string is found in the list, 0 not found, or invalid list
911 */
912int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
913
bc4147ab
TR
914/**
915 * fdt_count_strings - count the number of strings in a string list
916 * @fdt: pointer to the device tree blob
917 * @node: offset of the node
918 * @property: name of the property containing the string list
919 * @return: the number of strings in the given property
920 */
921int fdt_count_strings(const void *fdt, int node, const char *property);
922
fc503c17
TR
923/**
924 * fdt_find_string - find a string in a string list and return its index
925 * @fdt: pointer to the device tree blob
926 * @node: offset of the node
927 * @property: name of the property containing the string list
928 * @string: string to look up in the string list
929 * @return: the index of the string or negative on error
930 */
931int fdt_find_string(const void *fdt, int node, const char *property,
932 const char *string);
933
5094eb40
TR
934/**
935 * fdt_get_string_index() - obtain the string at a given index in a string list
936 * @fdt: pointer to the device tree blob
937 * @node: offset of the node
938 * @property: name of the property containing the string list
939 * @index: index of the string to return
940 * @output: return location for the string
941 * @return: 0 if the string was found or a negative error code otherwise
942 */
943int fdt_get_string_index(const void *fdt, int node, const char *property,
944 int index, const char **output);
945
946/**
965fab1a 947 * fdt_get_string() - obtain the first string in a string list
5094eb40
TR
948 * @fdt: pointer to the device tree blob
949 * @node: offset of the node
950 * @property: name of the property containing the string list
951 * @output: return location for the string
952 * @return: 0 if the string was found or a negative error code otherwise
953 *
954 * This is a shortcut for:
955 *
956 * fdt_get_string_index(fdt, node, property, 0, output).
957 */
958int fdt_get_string(const void *fdt, int node, const char *property,
959 const char **output);
960
0596d35d
SG
961/**********************************************************************/
962/* Read-only functions (addressing related) */
963/**********************************************************************/
964
965/**
966 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
967 *
968 * This is the maximum value for #address-cells, #size-cells and
969 * similar properties that will be processed by libfdt. IEE1275
970 * requires that OF implementations handle values up to 4.
971 * Implementations may support larger values, but in practice higher
972 * values aren't used.
973 */
974#define FDT_MAX_NCELLS 4
975
976/**
977 * fdt_address_cells - retrieve address size for a bus represented in the tree
978 * @fdt: pointer to the device tree blob
979 * @nodeoffset: offset of the node to find the address size for
980 *
981 * When the node has a valid #address-cells property, returns its value.
982 *
983 * returns:
984 * 0 <= n < FDT_MAX_NCELLS, on success
985 * 2, if the node has no #address-cells property
986 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
987 * #address-cells property
988 * -FDT_ERR_BADMAGIC,
989 * -FDT_ERR_BADVERSION,
990 * -FDT_ERR_BADSTATE,
991 * -FDT_ERR_BADSTRUCTURE,
992 * -FDT_ERR_TRUNCATED, standard meanings
993 */
994int fdt_address_cells(const void *fdt, int nodeoffset);
995
996/**
997 * fdt_size_cells - retrieve address range size for a bus represented in the
998 * tree
999 * @fdt: pointer to the device tree blob
1000 * @nodeoffset: offset of the node to find the address range size for
1001 *
1002 * When the node has a valid #size-cells property, returns its value.
1003 *
1004 * returns:
1005 * 0 <= n < FDT_MAX_NCELLS, on success
1006 * 2, if the node has no #address-cells property
1007 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1008 * #size-cells property
1009 * -FDT_ERR_BADMAGIC,
1010 * -FDT_ERR_BADVERSION,
1011 * -FDT_ERR_BADSTATE,
1012 * -FDT_ERR_BADSTRUCTURE,
1013 * -FDT_ERR_TRUNCATED, standard meanings
1014 */
1015int fdt_size_cells(const void *fdt, int nodeoffset);
1016
1017
8d04f02f
KG
1018/**********************************************************************/
1019/* Write-in-place functions */
1020/**********************************************************************/
3af0d587 1021
9d4450b5
DG
1022/**
1023 * fdt_setprop_inplace - change a property's value, but not its size
1024 * @fdt: pointer to the device tree blob
1025 * @nodeoffset: offset of the node whose property to change
1026 * @name: name of the property to change
1027 * @val: pointer to data to replace the property value with
1028 * @len: length of the property value
1029 *
1030 * fdt_setprop_inplace() replaces the value of a given property with
1031 * the data in val, of length len. This function cannot change the
1032 * size of a property, and so will only work if len is equal to the
1033 * current length of the property.
1034 *
1035 * This function will alter only the bytes in the blob which contain
1036 * the given property value, and will not alter or move any other part
1037 * of the tree.
1038 *
1039 * returns:
1040 * 0, on success
1041 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1042 * -FDT_ERR_NOTFOUND, node does not have the named property
1043 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1044 * -FDT_ERR_BADMAGIC,
1045 * -FDT_ERR_BADVERSION,
1046 * -FDT_ERR_BADSTATE,
1047 * -FDT_ERR_BADSTRUCTURE,
1048 * -FDT_ERR_TRUNCATED, standard meanings
1049 */
7cd5da0f
GVB
1050int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1051 const void *val, int len);
9d4450b5
DG
1052
1053/**
24fa0e58 1054 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
9d4450b5
DG
1055 * @fdt: pointer to the device tree blob
1056 * @nodeoffset: offset of the node whose property to change
1057 * @name: name of the property to change
24fa0e58 1058 * @val: 32-bit integer value to replace the property with
9d4450b5 1059 *
24fa0e58
DG
1060 * fdt_setprop_inplace_u32() replaces the value of a given property
1061 * with the 32-bit integer value in val, converting val to big-endian
1062 * if necessary. This function cannot change the size of a property,
1063 * and so will only work if the property already exists and has length
1064 * 4.
9d4450b5
DG
1065 *
1066 * This function will alter only the bytes in the blob which contain
1067 * the given property value, and will not alter or move any other part
1068 * of the tree.
1069 *
1070 * returns:
1071 * 0, on success
1072 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
24fa0e58 1073 * -FDT_ERR_NOTFOUND, node does not have the named property
9d4450b5
DG
1074 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1075 * -FDT_ERR_BADMAGIC,
1076 * -FDT_ERR_BADVERSION,
1077 * -FDT_ERR_BADSTATE,
1078 * -FDT_ERR_BADSTRUCTURE,
1079 * -FDT_ERR_TRUNCATED, standard meanings
1080 */
24fa0e58
DG
1081static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1082 const char *name, uint32_t val)
8d04f02f 1083{
b2ba62a1
KP
1084 fdt32_t tmp = cpu_to_fdt32(val);
1085 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
8d04f02f 1086}
7cd5da0f 1087
24fa0e58
DG
1088/**
1089 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1090 * @fdt: pointer to the device tree blob
1091 * @nodeoffset: offset of the node whose property to change
1092 * @name: name of the property to change
1093 * @val: 64-bit integer value to replace the property with
1094 *
1095 * fdt_setprop_inplace_u64() replaces the value of a given property
1096 * with the 64-bit integer value in val, converting val to big-endian
1097 * if necessary. This function cannot change the size of a property,
1098 * and so will only work if the property already exists and has length
1099 * 8.
1100 *
1101 * This function will alter only the bytes in the blob which contain
1102 * the given property value, and will not alter or move any other part
1103 * of the tree.
1104 *
1105 * returns:
1106 * 0, on success
1107 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1108 * -FDT_ERR_NOTFOUND, node does not have the named property
1109 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1110 * -FDT_ERR_BADMAGIC,
1111 * -FDT_ERR_BADVERSION,
1112 * -FDT_ERR_BADSTATE,
1113 * -FDT_ERR_BADSTRUCTURE,
1114 * -FDT_ERR_TRUNCATED, standard meanings
1115 */
1116static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1117 const char *name, uint64_t val)
1118{
b2ba62a1
KP
1119 fdt64_t tmp = cpu_to_fdt64(val);
1120 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
24fa0e58
DG
1121}
1122
1123/**
1124 * fdt_setprop_inplace_cell - change the value of a single-cell property
1125 *
1126 * This is an alternative name for fdt_setprop_inplace_u32()
1127 */
1128static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1129 const char *name, uint32_t val)
1130{
1131 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1132}
1133
9d4450b5
DG
1134/**
1135 * fdt_nop_property - replace a property with nop tags
1136 * @fdt: pointer to the device tree blob
1137 * @nodeoffset: offset of the node whose property to nop
1138 * @name: name of the property to nop
1139 *
1140 * fdt_nop_property() will replace a given property's representation
1141 * in the blob with FDT_NOP tags, effectively removing it from the
1142 * tree.
1143 *
1144 * This function will alter only the bytes in the blob which contain
1145 * the property, and will not alter or move any other part of the
1146 * tree.
1147 *
1148 * returns:
1149 * 0, on success
1150 * -FDT_ERR_NOTFOUND, node does not have the named property
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_property(void *fdt, int nodeoffset, const char *name);
9d4450b5
DG
1159
1160/**
1161 * fdt_nop_node - replace a node (subtree) with nop tags
1162 * @fdt: pointer to the device tree blob
1163 * @nodeoffset: offset of the node to nop
1164 *
1165 * fdt_nop_node() will replace a given node's representation in the
1166 * blob, including all its subnodes, if any, with FDT_NOP tags,
1167 * effectively removing it from the tree.
1168 *
1169 * This function will alter only the bytes in the blob which contain
1170 * the node and its properties and subnodes, and will not alter or
1171 * move any other part of the tree.
1172 *
1173 * returns:
1174 * 0, on success
1175 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1176 * -FDT_ERR_BADMAGIC,
1177 * -FDT_ERR_BADVERSION,
1178 * -FDT_ERR_BADSTATE,
1179 * -FDT_ERR_BADSTRUCTURE,
1180 * -FDT_ERR_TRUNCATED, standard meanings
1181 */
7cd5da0f 1182int fdt_nop_node(void *fdt, int nodeoffset);
3f9f08cf 1183
8d04f02f
KG
1184/**********************************************************************/
1185/* Sequential write functions */
1186/**********************************************************************/
7cd5da0f 1187
7cd5da0f 1188int fdt_create(void *buf, int bufsize);
0596d35d 1189int fdt_resize(void *fdt, void *buf, int bufsize);
7cd5da0f
GVB
1190int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1191int fdt_finish_reservemap(void *fdt);
1192int fdt_begin_node(void *fdt, const char *name);
1193int fdt_property(void *fdt, const char *name, const void *val, int len);
24fa0e58 1194static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
8d04f02f 1195{
b2ba62a1
KP
1196 fdt32_t tmp = cpu_to_fdt32(val);
1197 return fdt_property(fdt, name, &tmp, sizeof(tmp));
8d04f02f 1198}
24fa0e58
DG
1199static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1200{
b2ba62a1
KP
1201 fdt64_t tmp = cpu_to_fdt64(val);
1202 return fdt_property(fdt, name, &tmp, sizeof(tmp));
24fa0e58
DG
1203}
1204static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1205{
1206 return fdt_property_u32(fdt, name, val);
1207}
f8a2d7a4
SG
1208
1209/**
1210 * fdt_property_placeholder - add a new property and return a ptr to its value
1211 *
1212 * @fdt: pointer to the device tree blob
1213 * @name: name of property to add
1214 * @len: length of property value in bytes
1215 * @valp: returns a pointer to where where the value should be placed
1216 *
1217 * returns:
1218 * 0, on success
1219 * -FDT_ERR_BADMAGIC,
1220 * -FDT_ERR_NOSPACE, standard meanings
1221 */
1222int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
1223
7cd5da0f
GVB
1224#define fdt_property_string(fdt, name, str) \
1225 fdt_property(fdt, name, str, strlen(str)+1)
1226int fdt_end_node(void *fdt);
1227int fdt_finish(void *fdt);
1228
8d04f02f
KG
1229/**********************************************************************/
1230/* Read-write functions */
1231/**********************************************************************/
1232
c71b64f3 1233int fdt_create_empty_tree(void *buf, int bufsize);
8d04f02f 1234int fdt_open_into(const void *fdt, void *buf, int bufsize);
7cd5da0f
GVB
1235int fdt_pack(void *fdt);
1236
aec7135b
DG
1237/**
1238 * fdt_add_mem_rsv - add one memory reserve map entry
1239 * @fdt: pointer to the device tree blob
46390da1 1240 * @address, @size: 64-bit values (native endian)
aec7135b
DG
1241 *
1242 * Adds a reserve map entry to the given blob reserving a region at
1243 * address address of length size.
1244 *
1245 * This function will insert data into the reserve map and will
46390da1 1246 * therefore change the indexes of some entries in the table.
aec7135b
DG
1247 *
1248 * returns:
1249 * 0, on success
1250 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1251 * contain the new reservation entry
1252 * -FDT_ERR_BADMAGIC,
1253 * -FDT_ERR_BADVERSION,
1254 * -FDT_ERR_BADSTATE,
1255 * -FDT_ERR_BADSTRUCTURE,
1256 * -FDT_ERR_BADLAYOUT,
1257 * -FDT_ERR_TRUNCATED, standard meanings
1258 */
8d04f02f 1259int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
aec7135b
DG
1260
1261/**
1262 * fdt_del_mem_rsv - remove a memory reserve map entry
1263 * @fdt: pointer to the device tree blob
1264 * @n: entry to remove
1265 *
1266 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1267 * the blob.
1268 *
1269 * This function will delete data from the reservation table and will
46390da1 1270 * therefore change the indexes of some entries in the table.
aec7135b
DG
1271 *
1272 * returns:
1273 * 0, on success
1274 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1275 * are less than n+1 reserve map entries)
1276 * -FDT_ERR_BADMAGIC,
1277 * -FDT_ERR_BADVERSION,
1278 * -FDT_ERR_BADSTATE,
1279 * -FDT_ERR_BADSTRUCTURE,
1280 * -FDT_ERR_BADLAYOUT,
1281 * -FDT_ERR_TRUNCATED, standard meanings
1282 */
8d04f02f
KG
1283int fdt_del_mem_rsv(void *fdt, int n);
1284
9eaeb07a
DG
1285/**
1286 * fdt_set_name - change the name of a given node
1287 * @fdt: pointer to the device tree blob
1288 * @nodeoffset: structure block offset of a node
1289 * @name: name to give the node
1290 *
1291 * fdt_set_name() replaces the name (including unit address, if any)
1292 * of the given node with the given string. NOTE: this function can't
1293 * efficiently check if the new name is unique amongst the given
1294 * node's siblings; results are undefined if this function is invoked
1295 * with a name equal to one of the given node's siblings.
1296 *
1297 * This function may insert or delete data from the blob, and will
1298 * therefore change the offsets of some existing nodes.
1299 *
1300 * returns:
1301 * 0, on success
1302 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1303 * to contain the new name
1304 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1305 * -FDT_ERR_BADMAGIC,
1306 * -FDT_ERR_BADVERSION,
1307 * -FDT_ERR_BADSTATE, standard meanings
1308 */
1309int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1310
aec7135b
DG
1311/**
1312 * fdt_setprop - create or change a property
1313 * @fdt: pointer to the device tree blob
1314 * @nodeoffset: offset of the node whose property to change
1315 * @name: name of the property to change
1316 * @val: pointer to data to set the property value to
1317 * @len: length of the property value
1318 *
1319 * fdt_setprop() sets the value of the named property in the given
46390da1 1320 * node to the given value and length, creating the property if it
aec7135b
DG
1321 * does not already exist.
1322 *
1323 * This function may insert or delete data from the blob, and will
1324 * therefore change the offsets of some existing nodes.
1325 *
1326 * returns:
1327 * 0, on success
1328 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1329 * contain the new property value
1330 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1331 * -FDT_ERR_BADLAYOUT,
1332 * -FDT_ERR_BADMAGIC,
1333 * -FDT_ERR_BADVERSION,
1334 * -FDT_ERR_BADSTATE,
1335 * -FDT_ERR_BADSTRUCTURE,
1336 * -FDT_ERR_BADLAYOUT,
1337 * -FDT_ERR_TRUNCATED, standard meanings
1338 */
7cd5da0f
GVB
1339int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1340 const void *val, int len);
aec7135b
DG
1341
1342/**
24fa0e58 1343 * fdt_setprop_u32 - set a property to a 32-bit integer
aec7135b
DG
1344 * @fdt: pointer to the device tree blob
1345 * @nodeoffset: offset of the node whose property to change
1346 * @name: name of the property to change
1347 * @val: 32-bit integer value for the property (native endian)
1348 *
24fa0e58
DG
1349 * fdt_setprop_u32() sets the value of the named property in the given
1350 * node to the given 32-bit integer value (converting to big-endian if
aec7135b
DG
1351 * necessary), or creates a new property with that value if it does
1352 * not already exist.
1353 *
1354 * This function may insert or delete data from the blob, and will
1355 * therefore change the offsets of some existing nodes.
1356 *
1357 * returns:
1358 * 0, on success
1359 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1360 * contain the new property value
1361 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1362 * -FDT_ERR_BADLAYOUT,
1363 * -FDT_ERR_BADMAGIC,
1364 * -FDT_ERR_BADVERSION,
1365 * -FDT_ERR_BADSTATE,
1366 * -FDT_ERR_BADSTRUCTURE,
1367 * -FDT_ERR_BADLAYOUT,
1368 * -FDT_ERR_TRUNCATED, standard meanings
1369 */
24fa0e58
DG
1370static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1371 uint32_t val)
8d04f02f 1372{
b2ba62a1
KP
1373 fdt32_t tmp = cpu_to_fdt32(val);
1374 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
8d04f02f 1375}
aec7135b 1376
24fa0e58
DG
1377/**
1378 * fdt_setprop_u64 - set a property to a 64-bit integer
1379 * @fdt: pointer to the device tree blob
1380 * @nodeoffset: offset of the node whose property to change
1381 * @name: name of the property to change
1382 * @val: 64-bit integer value for the property (native endian)
1383 *
1384 * fdt_setprop_u64() sets the value of the named property in the given
1385 * node to the given 64-bit integer value (converting to big-endian if
1386 * necessary), or creates a new property with that value if it does
1387 * not already exist.
1388 *
1389 * This function may insert or delete data from the blob, and will
1390 * therefore change the offsets of some existing nodes.
1391 *
1392 * returns:
1393 * 0, on success
1394 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1395 * contain the new property value
1396 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1397 * -FDT_ERR_BADLAYOUT,
1398 * -FDT_ERR_BADMAGIC,
1399 * -FDT_ERR_BADVERSION,
1400 * -FDT_ERR_BADSTATE,
1401 * -FDT_ERR_BADSTRUCTURE,
1402 * -FDT_ERR_BADLAYOUT,
1403 * -FDT_ERR_TRUNCATED, standard meanings
1404 */
1405static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1406 uint64_t val)
1407{
b2ba62a1
KP
1408 fdt64_t tmp = cpu_to_fdt64(val);
1409 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
24fa0e58
DG
1410}
1411
1412/**
1413 * fdt_setprop_cell - set a property to a single cell value
1414 *
1415 * This is an alternative name for fdt_setprop_u32()
1416 */
1417static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1418 uint32_t val)
1419{
1420 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1421}
1422
aec7135b
DG
1423/**
1424 * fdt_setprop_string - set a property to a string value
1425 * @fdt: pointer to the device tree blob
1426 * @nodeoffset: offset of the node whose property to change
1427 * @name: name of the property to change
1428 * @str: string value for the property
1429 *
1430 * fdt_setprop_string() sets the value of the named property in the
1431 * given node to the given string value (using the length of the
1432 * string to determine the new length of the property), or creates a
1433 * new property with that value if it does not already exist.
1434 *
1435 * This function may insert or delete data from the blob, and will
1436 * therefore change the offsets of some existing nodes.
1437 *
1438 * returns:
1439 * 0, on success
1440 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1441 * contain the new property value
1442 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1443 * -FDT_ERR_BADLAYOUT,
1444 * -FDT_ERR_BADMAGIC,
1445 * -FDT_ERR_BADVERSION,
1446 * -FDT_ERR_BADSTATE,
1447 * -FDT_ERR_BADSTRUCTURE,
1448 * -FDT_ERR_BADLAYOUT,
1449 * -FDT_ERR_TRUNCATED, standard meanings
1450 */
7cd5da0f
GVB
1451#define fdt_setprop_string(fdt, nodeoffset, name, str) \
1452 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
aec7135b 1453
36ad18a6
ML
1454/**
1455 * fdt_appendprop - append to or create a property
1456 * @fdt: pointer to the device tree blob
1457 * @nodeoffset: offset of the node whose property to change
1458 * @name: name of the property to append to
1459 * @val: pointer to data to append to the property value
1460 * @len: length of the data to append to the property value
1461 *
1462 * fdt_appendprop() appends the value to the named property in the
1463 * given node, creating the property if it does not already exist.
1464 *
1465 * This function may insert data into the blob, and will therefore
1466 * 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 */
1481int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1482 const void *val, int len);
1483
1484/**
24fa0e58 1485 * fdt_appendprop_u32 - append a 32-bit integer value to a property
36ad18a6
ML
1486 * @fdt: pointer to the device tree blob
1487 * @nodeoffset: offset of the node whose property to change
1488 * @name: name of the property to change
1489 * @val: 32-bit integer value to append to the property (native endian)
1490 *
24fa0e58
DG
1491 * fdt_appendprop_u32() appends the given 32-bit integer value
1492 * (converting to big-endian if necessary) to the value of the named
1493 * property in the given node, or creates a new property with that
1494 * value if it does not already exist.
36ad18a6
ML
1495 *
1496 * This function may insert data into the blob, and will therefore
1497 * change the offsets of some existing nodes.
1498 *
1499 * returns:
1500 * 0, on success
1501 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1502 * contain the new property value
1503 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1504 * -FDT_ERR_BADLAYOUT,
1505 * -FDT_ERR_BADMAGIC,
1506 * -FDT_ERR_BADVERSION,
1507 * -FDT_ERR_BADSTATE,
1508 * -FDT_ERR_BADSTRUCTURE,
1509 * -FDT_ERR_BADLAYOUT,
1510 * -FDT_ERR_TRUNCATED, standard meanings
1511 */
24fa0e58
DG
1512static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1513 const char *name, uint32_t val)
36ad18a6 1514{
b2ba62a1
KP
1515 fdt32_t tmp = cpu_to_fdt32(val);
1516 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
36ad18a6
ML
1517}
1518
24fa0e58
DG
1519/**
1520 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1521 * @fdt: pointer to the device tree blob
1522 * @nodeoffset: offset of the node whose property to change
1523 * @name: name of the property to change
1524 * @val: 64-bit integer value to append to the property (native endian)
1525 *
1526 * fdt_appendprop_u64() appends the given 64-bit integer value
1527 * (converting to big-endian if necessary) to the value of the named
1528 * property in the given node, or creates a new property with that
1529 * value if it does not already exist.
1530 *
1531 * This function may insert data into the blob, and will therefore
1532 * change the offsets of some existing nodes.
1533 *
1534 * returns:
1535 * 0, on success
1536 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1537 * contain the new property value
1538 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1539 * -FDT_ERR_BADLAYOUT,
1540 * -FDT_ERR_BADMAGIC,
1541 * -FDT_ERR_BADVERSION,
1542 * -FDT_ERR_BADSTATE,
1543 * -FDT_ERR_BADSTRUCTURE,
1544 * -FDT_ERR_BADLAYOUT,
1545 * -FDT_ERR_TRUNCATED, standard meanings
1546 */
1547static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1548 const char *name, uint64_t val)
1549{
b2ba62a1
KP
1550 fdt64_t tmp = cpu_to_fdt64(val);
1551 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
24fa0e58
DG
1552}
1553
1554/**
1555 * fdt_appendprop_cell - append a single cell value to a property
1556 *
1557 * This is an alternative name for fdt_appendprop_u32()
1558 */
1559static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1560 const char *name, uint32_t val)
1561{
1562 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1563}
1564
36ad18a6
ML
1565/**
1566 * fdt_appendprop_string - append a string to a property
1567 * @fdt: pointer to the device tree blob
1568 * @nodeoffset: offset of the node whose property to change
1569 * @name: name of the property to change
1570 * @str: string value to append to the property
1571 *
1572 * fdt_appendprop_string() appends the given string to the value of
1573 * the named property in the given node, or creates a new property
1574 * with that value if it does not already exist.
1575 *
1576 * This function may insert data into the blob, and will therefore
1577 * change the offsets of some existing nodes.
1578 *
1579 * returns:
1580 * 0, on success
1581 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1582 * contain the new property value
1583 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1584 * -FDT_ERR_BADLAYOUT,
1585 * -FDT_ERR_BADMAGIC,
1586 * -FDT_ERR_BADVERSION,
1587 * -FDT_ERR_BADSTATE,
1588 * -FDT_ERR_BADSTRUCTURE,
1589 * -FDT_ERR_BADLAYOUT,
1590 * -FDT_ERR_TRUNCATED, standard meanings
1591 */
1592#define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1593 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1594
aec7135b
DG
1595/**
1596 * fdt_delprop - delete a property
1597 * @fdt: pointer to the device tree blob
1598 * @nodeoffset: offset of the node whose property to nop
1599 * @name: name of the property to nop
1600 *
1601 * fdt_del_property() will delete the given property.
1602 *
1603 * This function will delete data from the blob, and will therefore
1604 * change the offsets of some existing nodes.
1605 *
1606 * returns:
1607 * 0, on success
1608 * -FDT_ERR_NOTFOUND, node does not have the named property
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_TRUNCATED, standard meanings
1616 */
7cd5da0f 1617int fdt_delprop(void *fdt, int nodeoffset, const char *name);
aec7135b
DG
1618
1619/**
1620 * fdt_add_subnode_namelen - creates a new node based on substring
1621 * @fdt: pointer to the device tree blob
1622 * @parentoffset: structure block offset of a node
1623 * @name: name of the subnode to locate
1624 * @namelen: number of characters of name to consider
1625 *
1626 * Identical to fdt_add_subnode(), but use only the first namelen
1627 * characters of name as the name of the new node. This is useful for
1628 * creating subnodes based on a portion of a larger string, such as a
1629 * full path.
1630 */
7cd5da0f
GVB
1631int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1632 const char *name, int namelen);
aec7135b
DG
1633
1634/**
1635 * fdt_add_subnode - creates a new node
1636 * @fdt: pointer to the device tree blob
1637 * @parentoffset: structure block offset of a node
1638 * @name: name of the subnode to locate
1639 *
1640 * fdt_add_subnode() creates a new node as a subnode of the node at
1641 * structure block offset parentoffset, with the given name (which
1642 * should include the unit address, if any).
1643 *
1644 * This function will insert data into the blob, and will therefore
1645 * change the offsets of some existing nodes.
1646
1647 * returns:
1648 * structure block offset of the created nodeequested subnode (>=0), on success
1649 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1650 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1651 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1652 * the given name
1653 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1654 * blob to contain the new node
1655 * -FDT_ERR_NOSPACE
1656 * -FDT_ERR_BADLAYOUT
1657 * -FDT_ERR_BADMAGIC,
1658 * -FDT_ERR_BADVERSION,
1659 * -FDT_ERR_BADSTATE,
1660 * -FDT_ERR_BADSTRUCTURE,
1661 * -FDT_ERR_TRUNCATED, standard meanings.
1662 */
7cd5da0f 1663int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
aec7135b
DG
1664
1665/**
1666 * fdt_del_node - delete a node (subtree)
1667 * @fdt: pointer to the device tree blob
1668 * @nodeoffset: offset of the node to nop
1669 *
1670 * fdt_del_node() will remove the given node, including all its
1671 * subnodes if any, from the blob.
1672 *
1673 * This function will delete data from the blob, and will therefore
1674 * change the offsets of some existing nodes.
1675 *
1676 * returns:
1677 * 0, on success
1678 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1679 * -FDT_ERR_BADLAYOUT,
1680 * -FDT_ERR_BADMAGIC,
1681 * -FDT_ERR_BADVERSION,
1682 * -FDT_ERR_BADSTATE,
1683 * -FDT_ERR_BADSTRUCTURE,
1684 * -FDT_ERR_TRUNCATED, standard meanings
1685 */
7cd5da0f
GVB
1686int fdt_del_node(void *fdt, int nodeoffset);
1687
8d04f02f
KG
1688/**********************************************************************/
1689/* Debugging / informational functions */
1690/**********************************************************************/
1691
7cd5da0f
GVB
1692const char *fdt_strerror(int errval);
1693
8f6e2e1e
SG
1694/**
1695 * fdt_remove_unused_strings() - Remove any unused strings from an FDT
1696 *
1697 * This creates a new device tree in @new with unused strings removed. The
1698 * called can then use fdt_pack() to minimise the space consumed.
1699 *
1700 * @old: Old device tree blog
1701 * @new: Place to put new device tree blob, which must be as large as
1702 * @old
1703 * @return
1704 * 0, on success
1705 * -FDT_ERR_BADOFFSET, corrupt device tree
1706 * -FDT_ERR_NOSPACE, out of space, which should not happen unless there
1707 * is something very wrong with the device tree input
1708 */
1709int fdt_remove_unused_strings(const void *old, void *new);
1710
3e06cd1f
SG
1711struct fdt_region {
1712 int offset;
1713 int size;
1714};
1715
c3c4c005
SG
1716/*
1717 * Flags for fdt_find_regions()
1718 *
1719 * Add a region for the string table (always the last region)
1720 */
1721#define FDT_REG_ADD_STRING_TAB (1 << 0)
1722
1723/*
1724 * Add all supernodes of a matching node/property, useful for creating a
1725 * valid subset tree
1726 */
1727#define FDT_REG_SUPERNODES (1 << 1)
1728
1729/* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
1730#define FDT_REG_DIRECT_SUBNODES (1 << 2)
1731
1732/* Add all subnodes of a matching node */
1733#define FDT_REG_ALL_SUBNODES (1 << 3)
1734
1735/* Add a region for the mem_rsvmap table (always the first region) */
1736#define FDT_REG_ADD_MEM_RSVMAP (1 << 4)
1737
1738/* Indicates what an fdt part is (node, property, value) */
1739#define FDT_IS_NODE (1 << 0)
1740#define FDT_IS_PROP (1 << 1)
1741#define FDT_IS_VALUE (1 << 2) /* not supported */
1742#define FDT_IS_COMPAT (1 << 3) /* used internally */
1743#define FDT_NODE_HAS_PROP (1 << 4) /* node contains prop */
1744
1745#define FDT_ANY_GLOBAL (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
1746 FDT_IS_COMPAT)
1747#define FDT_IS_ANY 0x1f /* all the above */
1748
1749/* We set a reasonable limit on the number of nested nodes */
1750#define FDT_MAX_DEPTH 32
1751
1752/* Decribes what we want to include from the current tag */
1753enum want_t {
1754 WANT_NOTHING,
1755 WANT_NODES_ONLY, /* No properties */
1756 WANT_NODES_AND_PROPS, /* Everything for one level */
1757 WANT_ALL_NODES_AND_PROPS /* Everything for all levels */
1758};
1759
1760/* Keeps track of the state at parent nodes */
1761struct fdt_subnode_stack {
1762 int offset; /* Offset of node */
1763 enum want_t want; /* The 'want' value here */
1764 int included; /* 1 if we included this node, 0 if not */
1765};
1766
1767struct fdt_region_ptrs {
1768 int depth; /* Current tree depth */
1769 int done; /* What we have completed scanning */
1770 enum want_t want; /* What we are currently including */
1771 char *end; /* Pointer to end of full node path */
1772 int nextoffset; /* Next node offset to check */
1773};
1774
1775/* The state of our finding algortihm */
1776struct fdt_region_state {
1777 struct fdt_subnode_stack stack[FDT_MAX_DEPTH]; /* node stack */
1778 struct fdt_region *region; /* Contains list of regions found */
1779 int count; /* Numnber of regions found */
1780 const void *fdt; /* FDT blob */
1781 int max_regions; /* Maximum regions to find */
1782 int can_merge; /* 1 if we can merge with previous region */
1783 int start; /* Start position of current region */
1784 struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */
1785};
1786
3e06cd1f
SG
1787/**
1788 * fdt_find_regions() - find regions in device tree
1789 *
1790 * Given a list of nodes to include and properties to exclude, find
1791 * the regions of the device tree which describe those included parts.
1792 *
1793 * The intent is to get a list of regions which will be invariant provided
1794 * those parts are invariant. For example, if you request a list of regions
1795 * for all nodes but exclude the property "data", then you will get the
1796 * same region contents regardless of any change to "data" properties.
1797 *
1798 * This function can be used to produce a byte-stream to send to a hashing
1799 * function to verify that critical parts of the FDT have not changed.
1800 *
1801 * Nodes which are given in 'inc' are included in the region list, as
1802 * are the names of the immediate subnodes nodes (but not the properties
1803 * or subnodes of those subnodes).
1804 *
1805 * For eaxample "/" means to include the root node, all root properties
1806 * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1807 * ensures that we capture the names of the subnodes. In a hashing situation
1808 * it prevents the root node from changing at all Any change to non-excluded
1809 * properties, names of subnodes or number of subnodes would be detected.
1810 *
1811 * When used with FITs this provides the ability to hash and sign parts of
1812 * the FIT based on different configurations in the FIT. Then it is
1813 * impossible to change anything about that configuration (include images
1814 * attached to the configuration), but it may be possible to add new
1815 * configurations, new images or new signatures within the existing
1816 * framework.
1817 *
1818 * Adding new properties to a device tree may result in the string table
1819 * being extended (if the new property names are different from those
1820 * already added). This function can optionally include a region for
1821 * the string table so that this can be part of the hash too.
1822 *
1823 * The device tree header is not included in the list.
1824 *
1825 * @fdt: Device tree to check
1826 * @inc: List of node paths to included
1827 * @inc_count: Number of node paths in list
1828 * @exc_prop: List of properties names to exclude
1829 * @exc_prop_count: Number of properties in exclude list
1830 * @region: Returns list of regions
1831 * @max_region: Maximum length of region list
1832 * @path: Pointer to a temporary string for the function to use for
1833 * building path names
1834 * @path_len: Length of path, must be large enough to hold the longest
1835 * path in the tree
1836 * @add_string_tab: 1 to add a region for the string table
1837 * @return number of regions in list. If this is >max_regions then the
1838 * region array was exhausted. You should increase max_regions and try
1839 * the call again.
1840 */
1841int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
1842 char * const exc_prop[], int exc_prop_count,
1843 struct fdt_region region[], int max_regions,
1844 char *path, int path_len, int add_string_tab);
1845
c3c4c005
SG
1846/**
1847 * fdt_first_region() - find regions in device tree
1848 *
1849 * Given a nodes and properties to include and properties to exclude, find
1850 * the regions of the device tree which describe those included parts.
1851 *
1852 * The use for this function is twofold. Firstly it provides a convenient
1853 * way of performing a structure-aware grep of the tree. For example it is
1854 * possible to grep for a node and get all the properties associated with
1855 * that node. Trees can be subsetted easily, by specifying the nodes that
1856 * are required, and then writing out the regions returned by this function.
1857 * This is useful for small resource-constrained systems, such as boot
1858 * loaders, which want to use an FDT but do not need to know about all of
1859 * it.
1860 *
1861 * Secondly it makes it easy to hash parts of the tree and detect changes.
1862 * The intent is to get a list of regions which will be invariant provided
1863 * those parts are invariant. For example, if you request a list of regions
1864 * for all nodes but exclude the property "data", then you will get the
1865 * same region contents regardless of any change to "data" properties.
1866 *
1867 * This function can be used to produce a byte-stream to send to a hashing
1868 * function to verify that critical parts of the FDT have not changed.
1869 * Note that semantically null changes in order could still cause false
1870 * hash misses. Such reordering might happen if the tree is regenerated
1871 * from source, and nodes are reordered (the bytes-stream will be emitted
1872 * in a different order and mnay hash functions will detect this). However
1873 * if an existing tree is modified using libfdt functions, such as
1874 * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
1875 *
1876 * The nodes/properties to include/exclude are defined by a function
1877 * provided by the caller. This function is called for each node and
1878 * property, and must return:
1879 *
1880 * 0 - to exclude this part
1881 * 1 - to include this part
1882 * -1 - for FDT_IS_PROP only: no information is available, so include
1883 * if its containing node is included
1884 *
1885 * The last case is only used to deal with properties. Often a property is
1886 * included if its containing node is included - this is the case where
1887 * -1 is returned.. However if the property is specifically required to be
1888 * included/excluded, then 0 or 1 can be returned. Note that including a
1889 * property when the FDT_REG_SUPERNODES flag is given will force its
1890 * containing node to be included since it is not valid to have a property
1891 * that is not in a node.
1892 *
1893 * Using the information provided, the inclusion of a node can be controlled
1894 * either by a node name or its compatible string, or any other property
1895 * that the function can determine.
1896 *
1897 * As an example, including node "/" means to include the root node and all
1898 * root properties. A flag provides a way of also including supernodes (of
1899 * which there is none for the root node), and another flag includes
1900 * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
1901 * FDT_END_NODE of all subnodes of /.
1902 *
1903 * The subnode feature helps in a hashing situation since it prevents the
1904 * root node from changing at all. Any change to non-excluded properties,
1905 * names of subnodes or number of subnodes would be detected.
1906 *
1907 * When used with FITs this provides the ability to hash and sign parts of
1908 * the FIT based on different configurations in the FIT. Then it is
1909 * impossible to change anything about that configuration (include images
1910 * attached to the configuration), but it may be possible to add new
1911 * configurations, new images or new signatures within the existing
1912 * framework.
1913 *
1914 * Adding new properties to a device tree may result in the string table
1915 * being extended (if the new property names are different from those
1916 * already added). This function can optionally include a region for
1917 * the string table so that this can be part of the hash too. This is always
1918 * the last region.
1919 *
1920 * The FDT also has a mem_rsvmap table which can also be included, and is
1921 * always the first region if so.
1922 *
1923 * The device tree header is not included in the region list. Since the
1924 * contents of the FDT are changing (shrinking, often), the caller will need
1925 * to regenerate the header anyway.
1926 *
1927 * @fdt: Device tree to check
1928 * @h_include: Function to call to determine whether to include a part or
1929 * not:
1930 *
1931 * @priv: Private pointer as passed to fdt_find_regions()
1932 * @fdt: Pointer to FDT blob
1933 * @offset: Offset of this node / property
1934 * @type: Type of this part, FDT_IS_...
1935 * @data: Pointer to data (node name, property name, compatible
1936 * string, value (not yet supported)
1937 * @size: Size of data, or 0 if none
1938 * @return 0 to exclude, 1 to include, -1 if no information is
1939 * available
1940 * @priv: Private pointer passed to h_include
1941 * @region: Returns list of regions, sorted by offset
1942 * @max_regions: Maximum length of region list
1943 * @path: Pointer to a temporary string for the function to use for
1944 * building path names
1945 * @path_len: Length of path, must be large enough to hold the longest
1946 * path in the tree
1947 * @flags: Various flags that control the region algortihm, see
1948 * FDT_REG_...
1949 * @return number of regions in list. If this is >max_regions then the
1950 * region array was exhausted. You should increase max_regions and try
1951 * the call again. Only the first max_regions elements are available in the
1952 * array.
1953 *
1954 * On error a -ve value is return, which can be:
1955 *
1956 * -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
1957 * -FDT_ERR_BADLAYOUT
1958 * -FDT_ERR_NOSPACE (path area is too small)
1959 */
1960int fdt_first_region(const void *fdt,
1961 int (*h_include)(void *priv, const void *fdt, int offset,
1962 int type, const char *data, int size),
1963 void *priv, struct fdt_region *region,
1964 char *path, int path_len, int flags,
1965 struct fdt_region_state *info);
1966
1967/** fdt_next_region() - find next region
1968 *
1969 * See fdt_first_region() for full description. This function finds the
1970 * next region according to the provided parameters, which must be the same
1971 * as passed to fdt_first_region().
1972 *
1973 * This function can additionally return -FDT_ERR_NOTFOUND when there are no
1974 * more regions
1975 */
1976int fdt_next_region(const void *fdt,
1977 int (*h_include)(void *priv, const void *fdt, int offset,
1978 int type, const char *data, int size),
1979 void *priv, struct fdt_region *region,
1980 char *path, int path_len, int flags,
1981 struct fdt_region_state *info);
1982
1983/**
1984 * fdt_add_alias_regions() - find aliases that point to existing regions
1985 *
1986 * Once a device tree grep is complete some of the nodes will be present
1987 * and some will have been dropped. This function checks all the alias nodes
1988 * to figure out which points point to nodes which are still present. These
1989 * aliases need to be kept, along with the nodes they reference.
1990 *
1991 * Given a list of regions function finds the aliases that still apply and
1992 * adds more regions to the list for these. This function is called after
1993 * fdt_next_region() has finished returning regions and requires the same
1994 * state.
1995 *
1996 * @fdt: Device tree file to reference
1997 * @region: List of regions that will be kept
1998 * @count: Number of regions
1999 * @max_regions: Number of entries that can fit in @region
2000 * @info: Region state as returned from fdt_next_region()
2001 * @return new number of regions in @region (i.e. count + the number added)
2002 * or -FDT_ERR_NOSPACE if there was not enough space.
2003 */
2004int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
2005 int max_regions, struct fdt_region_state *info);
2006
7cd5da0f 2007#endif /* _LIBFDT_H */