source: rtems/cpukit/include/libfdt.h @ 9a7de8e

5
Last change on this file since 9a7de8e was 9a7de8e, checked in by David Gibson <david@…>, on Mar 16, 2018 at 11:40:26 AM

libfdt: Add fdt_header_size()

We have a couple of places within libfdt and its tests where we need to
find the size of the header, based on the version. Add a helper function
for it.

Signed-off-by: David Gibson <david@…>
Reviewed-by: Simon Glass <sjg@…>
Tested-by: Alexey Kardashevskiy <aik@…>
Reviewed-by: Alexey Kardashevskiy <aik@…>

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