source: rtems/cpukit/include/libfdt.h @ 8f6fb28c

5
Last change on this file since 8f6fb28c was 8f6fb28c, checked in by Simon Glass <sjg@…>, on 03/07/16 at 03:12:47

Correct line lengths in libfdt.h

There are a few lines that are over 80 columns. Fix these.

Signed-off-by: Simon Glass <sjg@…>
Signed-off-by: David Gibson <david@…>

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