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