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