blob: 8746790a25352fe297bb1b60f1bc3cd6e8485f06 [file] [log] [blame]
Kumar Galac8ab7052007-10-24 11:04:22 -05001#ifndef _LIBFDT_H
2#define _LIBFDT_H
Gerald Van Barenc0577a62007-03-31 11:59:59 -04003/*
4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 *
Kumar Galac8ab7052007-10-24 11:04:22 -05007 * 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.
Gerald Van Barenc0577a62007-03-31 11:59:59 -040019 *
Kumar Galac8ab7052007-10-24 11:04:22 -050020 * 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
Gerald Van Barenc0577a62007-03-31 11:59:59 -040024 *
Kumar Galac8ab7052007-10-24 11:04:22 -050025 * 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.
Gerald Van Barenc0577a62007-03-31 11:59:59 -040052 */
53
Gerald Van Barenc0577a62007-03-31 11:59:59 -040054#include <libfdt_env.h>
Kumar Galac8ab7052007-10-24 11:04:22 -050055#include <fdt.h>
Gerald Van Barenc0577a62007-03-31 11:59:59 -040056
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
Kumar Galac8ab7052007-10-24 11:04:22 -050062 /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
Gerald Van Barenc0577a62007-03-31 11:59:59 -040063#define FDT_ERR_EXISTS 2
Simon Glassdfdcd6f2016-10-02 17:59:27 -060064 /* FDT_ERR_EXISTS: Attempted to create a node or property which
Kumar Galac8ab7052007-10-24 11:04:22 -050065 * already exists */
Gerald Van Barenc0577a62007-03-31 11:59:59 -040066#define FDT_ERR_NOSPACE 3
Kumar Galac8ab7052007-10-24 11:04:22 -050067 /* 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. */
Gerald Van Barenc0577a62007-03-31 11:59:59 -040071
72/* Error codes: codes for bad parameters */
73#define FDT_ERR_BADOFFSET 4
Kumar Galac8ab7052007-10-24 11:04:22 -050074 /* 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. */
Gerald Van Barenc0577a62007-03-31 11:59:59 -040077#define FDT_ERR_BADPATH 5
Kumar Galac8ab7052007-10-24 11:04:22 -050078 /* 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. */
Gerald Van Barenc0577a62007-03-31 11:59:59 -040088
89/* Error codes: codes for bad device tree blobs */
Kumar Galac8ab7052007-10-24 11:04:22 -050090#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. */
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400118
Simon Glass536774d2014-07-30 03:59:03 -0600119/* 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
Simon Glassb0ea7402016-10-02 17:59:28 -0600124#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_TOODEEP 16
Simon Glass35b21522015-06-23 15:38:27 -0600130 /* FDT_ERR_TOODEEP: The depth of a node has exceeded the internal
131 * libfdt limit. This can happen if you have more than
132 * FDT_MAX_DEPTH nested nodes. */
133
Simon Glassb0ea7402016-10-02 17:59:28 -0600134#define FDT_ERR_MAX 16
Kumar Galac8ab7052007-10-24 11:04:22 -0500135
136/**********************************************************************/
137/* Low-level functions (you probably don't need these) */
138/**********************************************************************/
139
Jon Loeligerd94d0f12008-09-25 11:02:17 -0500140const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
Kumar Galac8ab7052007-10-24 11:04:22 -0500141static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
142{
David Gibson870fa472008-07-07 10:14:15 +1000143 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
Kumar Galac8ab7052007-10-24 11:04:22 -0500144}
145
146uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
147
148/**********************************************************************/
David Gibsond9d793c2008-02-12 11:58:31 +1100149/* Traversal functions */
150/**********************************************************************/
151
152int fdt_next_node(const void *fdt, int offset, int *depth);
153
Simon Glass3a4de332013-05-07 06:11:50 +0000154/**
155 * fdt_first_subnode() - get offset of first direct subnode
156 *
157 * @fdt: FDT blob
158 * @offset: Offset of node to check
159 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
160 */
161int fdt_first_subnode(const void *fdt, int offset);
162
163/**
164 * fdt_next_subnode() - get offset of next direct subnode
165 *
166 * After first calling fdt_first_subnode(), call this function repeatedly to
167 * get direct subnodes of a parent node.
168 *
169 * @fdt: FDT blob
170 * @offset: Offset of previous subnode
171 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
172 * subnodes
173 */
174int fdt_next_subnode(const void *fdt, int offset);
175
Thierry Redingc03264f2014-08-26 17:33:55 +0200176/**
177 * fdt_for_each_subnode - iterate over all subnodes of a parent
178 *
Simon Glass499c29e2016-10-02 17:59:29 -0600179 * @node: child node (int, lvalue)
180 * @fdt: FDT blob (const void *)
181 * @parent: parent node (int)
182 *
Thierry Redingc03264f2014-08-26 17:33:55 +0200183 * This is actually a wrapper around a for loop and would be used like so:
184 *
Simon Glass499c29e2016-10-02 17:59:29 -0600185 * fdt_for_each_subnode(node, fdt, parent) {
186 * Use node
Thierry Redingc03264f2014-08-26 17:33:55 +0200187 * ...
188 * }
189 *
Simon Glass499c29e2016-10-02 17:59:29 -0600190 * if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) {
191 * Error handling
192 * }
Thierry Redingc03264f2014-08-26 17:33:55 +0200193 *
Simon Glass499c29e2016-10-02 17:59:29 -0600194 * Note that this is implemented as a macro and @node is used as
195 * iterator in the loop. The parent variable be constant or even a
196 * literal.
197 *
Thierry Redingc03264f2014-08-26 17:33:55 +0200198 */
Simon Glass499c29e2016-10-02 17:59:29 -0600199#define fdt_for_each_subnode(node, fdt, parent) \
Thierry Redingc03264f2014-08-26 17:33:55 +0200200 for (node = fdt_first_subnode(fdt, parent); \
201 node >= 0; \
202 node = fdt_next_subnode(fdt, node))
203
David Gibsond9d793c2008-02-12 11:58:31 +1100204/**********************************************************************/
Kumar Galac8ab7052007-10-24 11:04:22 -0500205/* General functions */
206/**********************************************************************/
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400207
208#define fdt_get_header(fdt, field) \
Kumar Galac8ab7052007-10-24 11:04:22 -0500209 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
Wolfgang Denka1be4762008-05-20 16:00:29 +0200210#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400211#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
212#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
213#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
214#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
215#define fdt_version(fdt) (fdt_get_header(fdt, version))
Wolfgang Denka1be4762008-05-20 16:00:29 +0200216#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
217#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
218#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400219#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
220
Kumar Galac8ab7052007-10-24 11:04:22 -0500221#define __fdt_set_hdr(name) \
222 static inline void fdt_set_##name(void *fdt, uint32_t val) \
223 { \
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600224 struct fdt_header *fdth = (struct fdt_header *)fdt; \
Kumar Galac8ab7052007-10-24 11:04:22 -0500225 fdth->name = cpu_to_fdt32(val); \
226 }
227__fdt_set_hdr(magic);
228__fdt_set_hdr(totalsize);
229__fdt_set_hdr(off_dt_struct);
230__fdt_set_hdr(off_dt_strings);
231__fdt_set_hdr(off_mem_rsvmap);
232__fdt_set_hdr(version);
233__fdt_set_hdr(last_comp_version);
234__fdt_set_hdr(boot_cpuid_phys);
235__fdt_set_hdr(size_dt_strings);
236__fdt_set_hdr(size_dt_struct);
237#undef __fdt_set_hdr
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400238
Kumar Galac8ab7052007-10-24 11:04:22 -0500239/**
240 * fdt_check_header - sanity check a device tree or possible device tree
241 * @fdt: pointer to data which might be a flattened device tree
242 *
243 * fdt_check_header() checks that the given buffer contains what
244 * appears to be a flattened device tree with sane information in its
245 * header.
246 *
247 * returns:
248 * 0, if the buffer appears to contain a valid device tree
249 * -FDT_ERR_BADMAGIC,
250 * -FDT_ERR_BADVERSION,
251 * -FDT_ERR_BADSTATE, standard meanings, as above
252 */
Gerald Van Baren9f0f3582007-04-06 14:17:14 -0400253int fdt_check_header(const void *fdt);
254
Kumar Galac8ab7052007-10-24 11:04:22 -0500255/**
256 * fdt_move - move a device tree around in memory
257 * @fdt: pointer to the device tree to move
258 * @buf: pointer to memory where the device is to be moved
259 * @bufsize: size of the memory space at buf
260 *
261 * fdt_move() relocates, if possible, the device tree blob located at
262 * fdt to the buffer at buf of size bufsize. The buffer may overlap
263 * with the existing device tree blob at fdt. Therefore,
264 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
265 * should always succeed.
266 *
267 * returns:
268 * 0, on success
269 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
270 * -FDT_ERR_BADMAGIC,
271 * -FDT_ERR_BADVERSION,
272 * -FDT_ERR_BADSTATE, standard meanings
273 */
274int fdt_move(const void *fdt, void *buf, int bufsize);
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400275
Kumar Galac8ab7052007-10-24 11:04:22 -0500276/**********************************************************************/
277/* Read-only functions */
278/**********************************************************************/
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400279
Kumar Galac8ab7052007-10-24 11:04:22 -0500280/**
Wolfram Sang9a36da92008-07-09 11:22:44 +0200281 * fdt_string - retrieve a string from the strings block of a device tree
Kumar Galac8ab7052007-10-24 11:04:22 -0500282 * @fdt: pointer to the device tree blob
283 * @stroffset: offset of the string within the strings block (native endian)
284 *
285 * fdt_string() retrieves a pointer to a single string from the
286 * strings block of the device tree blob at fdt.
287 *
288 * returns:
289 * a pointer to the string, on success
290 * NULL, if stroffset is out of bounds
291 */
292const char *fdt_string(const void *fdt, int stroffset);
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400293
Kumar Galac8ab7052007-10-24 11:04:22 -0500294/**
Maxime Ripard2125ca52016-07-05 10:26:39 +0200295 * fdt_get_max_phandle - retrieves the highest phandle in a tree
296 * @fdt: pointer to the device tree blob
297 *
298 * fdt_get_max_phandle retrieves the highest phandle in the given
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600299 * device tree. This will ignore badly formatted phandles, or phandles
300 * with a value of 0 or -1.
Maxime Ripard2125ca52016-07-05 10:26:39 +0200301 *
302 * returns:
303 * the highest phandle on success
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600304 * 0, if no phandle was found in the device tree
305 * -1, if an error occurred
Maxime Ripard2125ca52016-07-05 10:26:39 +0200306 */
307uint32_t fdt_get_max_phandle(const void *fdt);
308
309/**
Wolfram Sang9a36da92008-07-09 11:22:44 +0200310 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
Kumar Galac8ab7052007-10-24 11:04:22 -0500311 * @fdt: pointer to the device tree blob
312 *
313 * Returns the number of entries in the device tree blob's memory
314 * reservation map. This does not include the terminating 0,0 entry
315 * or any other (0,0) entries reserved for expansion.
316 *
317 * returns:
318 * the number of entries
319 */
320int fdt_num_mem_rsv(const void *fdt);
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400321
Kumar Galac8ab7052007-10-24 11:04:22 -0500322/**
Wolfram Sang9a36da92008-07-09 11:22:44 +0200323 * fdt_get_mem_rsv - retrieve one memory reserve map entry
Kumar Galac8ab7052007-10-24 11:04:22 -0500324 * @fdt: pointer to the device tree blob
325 * @address, @size: pointers to 64-bit variables
326 *
327 * On success, *address and *size will contain the address and size of
328 * the n-th reserve map entry from the device tree blob, in
329 * native-endian format.
330 *
331 * returns:
332 * 0, on success
333 * -FDT_ERR_BADMAGIC,
334 * -FDT_ERR_BADVERSION,
335 * -FDT_ERR_BADSTATE, standard meanings
336 */
337int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
338
339/**
340 * fdt_subnode_offset_namelen - find a subnode based on substring
341 * @fdt: pointer to the device tree blob
342 * @parentoffset: structure block offset of a node
343 * @name: name of the subnode to locate
344 * @namelen: number of characters of name to consider
345 *
346 * Identical to fdt_subnode_offset(), but only examine the first
347 * namelen characters of name for matching the subnode name. This is
348 * useful for finding subnodes based on a portion of a larger string,
349 * such as a full path.
350 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400351int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
352 const char *name, int namelen);
Kumar Galac8ab7052007-10-24 11:04:22 -0500353/**
354 * fdt_subnode_offset - find a subnode of a given node
355 * @fdt: pointer to the device tree blob
356 * @parentoffset: structure block offset of a node
357 * @name: name of the subnode to locate
358 *
359 * fdt_subnode_offset() finds a subnode of the node at structure block
360 * offset parentoffset with the given name. name may include a unit
361 * address, in which case fdt_subnode_offset() will find the subnode
362 * with that unit address, or the unit address may be omitted, in
363 * which case fdt_subnode_offset() will find an arbitrary subnode
364 * whose name excluding unit address matches the given name.
365 *
366 * returns:
367 * structure block offset of the requested subnode (>=0), on success
368 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600369 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
370 * tag
371 * -FDT_ERR_BADMAGIC,
Kumar Galac8ab7052007-10-24 11:04:22 -0500372 * -FDT_ERR_BADVERSION,
373 * -FDT_ERR_BADSTATE,
374 * -FDT_ERR_BADSTRUCTURE,
375 * -FDT_ERR_TRUNCATED, standard meanings.
376 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400377int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
378
Kumar Galac8ab7052007-10-24 11:04:22 -0500379/**
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600380 * fdt_path_offset_namelen - find a tree node by its full path
Maxime Ripard87390e82016-07-05 10:26:41 +0200381 * @fdt: pointer to the device tree blob
382 * @path: full path of the node to locate
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600383 * @namelen: number of characters of path to consider
Maxime Ripard87390e82016-07-05 10:26:41 +0200384 *
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600385 * Identical to fdt_path_offset(), but only consider the first namelen
386 * characters of path as the path name.
Maxime Ripard87390e82016-07-05 10:26:41 +0200387 */
388int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
389
390/**
Kumar Galac8ab7052007-10-24 11:04:22 -0500391 * fdt_path_offset - find a tree node by its full path
392 * @fdt: pointer to the device tree blob
393 * @path: full path of the node to locate
394 *
395 * fdt_path_offset() finds a node of a given path in the device tree.
396 * Each path component may omit the unit address portion, but the
397 * results of this are undefined if any such path component is
398 * ambiguous (that is if there are multiple nodes at the relevant
399 * level matching the given component, differentiated only by unit
400 * address).
401 *
402 * returns:
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600403 * structure block offset of the node with the requested path (>=0), on
404 * success
Kumar Galac8ab7052007-10-24 11:04:22 -0500405 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
406 * -FDT_ERR_NOTFOUND, if the requested node does not exist
407 * -FDT_ERR_BADMAGIC,
408 * -FDT_ERR_BADVERSION,
409 * -FDT_ERR_BADSTATE,
410 * -FDT_ERR_BADSTRUCTURE,
411 * -FDT_ERR_TRUNCATED, standard meanings.
412 */
Simon Glassa2235e42016-10-02 17:59:30 -0600413int fdt_path_offset(const void *fdt, const char *path);
Kumar Galac8ab7052007-10-24 11:04:22 -0500414
415/**
Wolfram Sang9a36da92008-07-09 11:22:44 +0200416 * fdt_get_name - retrieve the name of a given node
Kumar Galac8ab7052007-10-24 11:04:22 -0500417 * @fdt: pointer to the device tree blob
418 * @nodeoffset: structure block offset of the starting node
419 * @lenp: pointer to an integer variable (will be overwritten) or NULL
420 *
421 * fdt_get_name() retrieves the name (including unit address) of the
422 * device tree node at structure block offset nodeoffset. If lenp is
423 * non-NULL, the length of this name is also returned, in the integer
424 * pointed to by lenp.
425 *
426 * returns:
427 * pointer to the node's name, on success
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600428 * If lenp is non-NULL, *lenp contains the length of that name
429 * (>=0)
Kumar Galac8ab7052007-10-24 11:04:22 -0500430 * NULL, on error
431 * if lenp is non-NULL *lenp contains an error code (<0):
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600432 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
433 * tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500434 * -FDT_ERR_BADMAGIC,
435 * -FDT_ERR_BADVERSION,
436 * -FDT_ERR_BADSTATE, standard meanings
437 */
438const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
439
440/**
David Gibson7b64da12010-03-09 17:39:14 +1100441 * fdt_first_property_offset - find the offset of a node's first property
442 * @fdt: pointer to the device tree blob
443 * @nodeoffset: structure block offset of a node
444 *
445 * fdt_first_property_offset() finds the first property of the node at
446 * the given structure block offset.
447 *
448 * returns:
449 * structure block offset of the property (>=0), on success
450 * -FDT_ERR_NOTFOUND, if the requested node has no properties
451 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
452 * -FDT_ERR_BADMAGIC,
453 * -FDT_ERR_BADVERSION,
454 * -FDT_ERR_BADSTATE,
455 * -FDT_ERR_BADSTRUCTURE,
456 * -FDT_ERR_TRUNCATED, standard meanings.
457 */
458int fdt_first_property_offset(const void *fdt, int nodeoffset);
459
460/**
461 * fdt_next_property_offset - step through a node's properties
462 * @fdt: pointer to the device tree blob
463 * @offset: structure block offset of a property
464 *
465 * fdt_next_property_offset() finds the property immediately after the
466 * one at the given structure block offset. This will be a property
467 * of the same node as the given property.
468 *
469 * returns:
470 * structure block offset of the next property (>=0), on success
471 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
472 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
473 * -FDT_ERR_BADMAGIC,
474 * -FDT_ERR_BADVERSION,
475 * -FDT_ERR_BADSTATE,
476 * -FDT_ERR_BADSTRUCTURE,
477 * -FDT_ERR_TRUNCATED, standard meanings.
478 */
479int fdt_next_property_offset(const void *fdt, int offset);
480
481/**
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600482 * fdt_for_each_property_offset - iterate over all properties of a node
483 *
484 * @property_offset: property offset (int, lvalue)
Maxime Riparde26d5042016-07-05 10:26:38 +0200485 * @fdt: FDT blob (const void *)
486 * @node: node offset (int)
487 *
488 * This is actually a wrapper around a for loop and would be used like so:
489 *
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600490 * fdt_for_each_property_offset(property, fdt, node) {
491 * Use property
Maxime Riparde26d5042016-07-05 10:26:38 +0200492 * ...
493 * }
494 *
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600495 * if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) {
496 * Error handling
497 * }
498 *
Maxime Riparde26d5042016-07-05 10:26:38 +0200499 * Note that this is implemented as a macro and property is used as
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600500 * iterator in the loop. The node variable can be constant or even a
501 * literal.
Maxime Riparde26d5042016-07-05 10:26:38 +0200502 */
503#define fdt_for_each_property_offset(property, fdt, node) \
504 for (property = fdt_first_property_offset(fdt, node); \
505 property >= 0; \
506 property = fdt_next_property_offset(fdt, property))
507
508/**
David Gibson7b64da12010-03-09 17:39:14 +1100509 * fdt_get_property_by_offset - retrieve the property at a given offset
510 * @fdt: pointer to the device tree blob
511 * @offset: offset of the property to retrieve
512 * @lenp: pointer to an integer variable (will be overwritten) or NULL
513 *
514 * fdt_get_property_by_offset() retrieves a pointer to the
515 * fdt_property structure within the device tree blob at the given
516 * offset. If lenp is non-NULL, the length of the property value is
517 * also returned, in the integer pointed to by lenp.
518 *
519 * returns:
520 * pointer to the structure representing the property
521 * if lenp is non-NULL, *lenp contains the length of the property
522 * value (>=0)
523 * NULL, on error
524 * if lenp is non-NULL, *lenp contains an error code (<0):
525 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
526 * -FDT_ERR_BADMAGIC,
527 * -FDT_ERR_BADVERSION,
528 * -FDT_ERR_BADSTATE,
529 * -FDT_ERR_BADSTRUCTURE,
530 * -FDT_ERR_TRUNCATED, standard meanings
531 */
532const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
533 int offset,
534 int *lenp);
535
536/**
David Gibsonc5b130e2008-08-06 14:50:49 +1000537 * fdt_get_property_namelen - find a property based on substring
538 * @fdt: pointer to the device tree blob
539 * @nodeoffset: offset of the node whose property to find
540 * @name: name of the property to find
541 * @namelen: number of characters of name to consider
542 * @lenp: pointer to an integer variable (will be overwritten) or NULL
543 *
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600544 * Identical to fdt_get_property(), but only examine the first namelen
545 * characters of name for matching the property name.
David Gibsonc5b130e2008-08-06 14:50:49 +1000546 */
547const struct fdt_property *fdt_get_property_namelen(const void *fdt,
548 int nodeoffset,
549 const char *name,
550 int namelen, int *lenp);
551
552/**
Kumar Galac8ab7052007-10-24 11:04:22 -0500553 * fdt_get_property - find a given property in a given node
554 * @fdt: pointer to the device tree blob
555 * @nodeoffset: offset of the node whose property to find
556 * @name: name of the property to find
557 * @lenp: pointer to an integer variable (will be overwritten) or NULL
558 *
559 * fdt_get_property() retrieves a pointer to the fdt_property
560 * structure within the device tree blob corresponding to the property
561 * named 'name' of the node at offset nodeoffset. If lenp is
Wolfram Sang9a36da92008-07-09 11:22:44 +0200562 * non-NULL, the length of the property value is also returned, in the
Kumar Galac8ab7052007-10-24 11:04:22 -0500563 * integer pointed to by lenp.
564 *
565 * returns:
566 * pointer to the structure representing the property
567 * if lenp is non-NULL, *lenp contains the length of the property
568 * value (>=0)
569 * NULL, on error
570 * if lenp is non-NULL, *lenp contains an error code (<0):
571 * -FDT_ERR_NOTFOUND, node does not have named property
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600572 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
573 * tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500574 * -FDT_ERR_BADMAGIC,
575 * -FDT_ERR_BADVERSION,
576 * -FDT_ERR_BADSTATE,
577 * -FDT_ERR_BADSTRUCTURE,
578 * -FDT_ERR_TRUNCATED, standard meanings
579 */
580const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
581 const char *name, int *lenp);
582static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
583 const char *name,
584 int *lenp)
585{
David Gibson870fa472008-07-07 10:14:15 +1000586 return (struct fdt_property *)(uintptr_t)
587 fdt_get_property(fdt, nodeoffset, name, lenp);
Kumar Galac8ab7052007-10-24 11:04:22 -0500588}
589
590/**
David Gibson7b64da12010-03-09 17:39:14 +1100591 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
592 * @fdt: pointer to the device tree blob
593 * @ffset: offset of the property to read
594 * @namep: pointer to a string variable (will be overwritten) or NULL
595 * @lenp: pointer to an integer variable (will be overwritten) or NULL
596 *
597 * fdt_getprop_by_offset() retrieves a pointer to the value of the
598 * property at structure block offset 'offset' (this will be a pointer
599 * to within the device blob itself, not a copy of the value). If
600 * lenp is non-NULL, the length of the property value is also
601 * returned, in the integer pointed to by lenp. If namep is non-NULL,
602 * the property's namne will also be returned in the char * pointed to
603 * by namep (this will be a pointer to within the device tree's string
604 * block, not a new copy of the name).
605 *
606 * returns:
607 * pointer to the property's value
608 * if lenp is non-NULL, *lenp contains the length of the property
609 * value (>=0)
610 * if namep is non-NULL *namep contiains a pointer to the property
611 * name.
612 * NULL, on error
613 * if lenp is non-NULL, *lenp contains an error code (<0):
614 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
615 * -FDT_ERR_BADMAGIC,
616 * -FDT_ERR_BADVERSION,
617 * -FDT_ERR_BADSTATE,
618 * -FDT_ERR_BADSTRUCTURE,
619 * -FDT_ERR_TRUNCATED, standard meanings
620 */
621const void *fdt_getprop_by_offset(const void *fdt, int offset,
622 const char **namep, int *lenp);
623
624/**
David Gibsonc5b130e2008-08-06 14:50:49 +1000625 * fdt_getprop_namelen - get property value based on substring
626 * @fdt: pointer to the device tree blob
627 * @nodeoffset: offset of the node whose property to find
628 * @name: name of the property to find
629 * @namelen: number of characters of name to consider
630 * @lenp: pointer to an integer variable (will be overwritten) or NULL
631 *
632 * Identical to fdt_getprop(), but only examine the first namelen
633 * characters of name for matching the property name.
634 */
635const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
636 const char *name, int namelen, int *lenp);
Maxime Ripard7bb42402016-07-05 10:26:42 +0200637static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
638 const char *name, int namelen,
639 int *lenp)
640{
641 return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
642 namelen, lenp);
643}
David Gibsonc5b130e2008-08-06 14:50:49 +1000644
645/**
Kumar Galac8ab7052007-10-24 11:04:22 -0500646 * fdt_getprop - retrieve the value of a given property
647 * @fdt: pointer to the device tree blob
648 * @nodeoffset: offset of the node whose property to find
649 * @name: name of the property to find
650 * @lenp: pointer to an integer variable (will be overwritten) or NULL
651 *
652 * fdt_getprop() retrieves a pointer to the value of the property
653 * named 'name' of the node at offset nodeoffset (this will be a
654 * pointer to within the device blob itself, not a copy of the value).
Wolfram Sang9a36da92008-07-09 11:22:44 +0200655 * If lenp is non-NULL, the length of the property value is also
Kumar Galac8ab7052007-10-24 11:04:22 -0500656 * returned, in the integer pointed to by lenp.
657 *
658 * returns:
659 * pointer to the property's value
660 * if lenp is non-NULL, *lenp contains the length of the property
661 * value (>=0)
662 * NULL, on error
663 * if lenp is non-NULL, *lenp contains an error code (<0):
664 * -FDT_ERR_NOTFOUND, node does not have named property
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600665 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
666 * tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500667 * -FDT_ERR_BADMAGIC,
668 * -FDT_ERR_BADVERSION,
669 * -FDT_ERR_BADSTATE,
670 * -FDT_ERR_BADSTRUCTURE,
671 * -FDT_ERR_TRUNCATED, standard meanings
672 */
673const void *fdt_getprop(const void *fdt, int nodeoffset,
674 const char *name, int *lenp);
675static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
676 const char *name, int *lenp)
677{
David Gibson870fa472008-07-07 10:14:15 +1000678 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
Kumar Galac8ab7052007-10-24 11:04:22 -0500679}
680
681/**
Wolfram Sang9a36da92008-07-09 11:22:44 +0200682 * fdt_get_phandle - retrieve the phandle of a given node
Kumar Galac8ab7052007-10-24 11:04:22 -0500683 * @fdt: pointer to the device tree blob
684 * @nodeoffset: structure block offset of the node
685 *
686 * fdt_get_phandle() retrieves the phandle of the device tree node at
687 * structure block offset nodeoffset.
688 *
689 * returns:
Wolfram Sang9a36da92008-07-09 11:22:44 +0200690 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
Kumar Galac8ab7052007-10-24 11:04:22 -0500691 * 0, if the node has no phandle, or another error occurs
692 */
693uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
694
695/**
Gerald Van Baren9f45b902008-10-04 07:56:06 -0400696 * fdt_get_alias_namelen - get alias based on substring
David Gibsond6656ea2008-08-20 16:55:14 +1000697 * @fdt: pointer to the device tree blob
698 * @name: name of the alias th look up
699 * @namelen: number of characters of name to consider
700 *
701 * Identical to fdt_get_alias(), but only examine the first namelen
702 * characters of name for matching the alias name.
703 */
704const char *fdt_get_alias_namelen(const void *fdt,
705 const char *name, int namelen);
706
707/**
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600708 * fdt_get_alias - retrieve the path referenced by a given alias
David Gibsond6656ea2008-08-20 16:55:14 +1000709 * @fdt: pointer to the device tree blob
Simon Glass536774d2014-07-30 03:59:03 -0600710 * @name: name of the alias th look up
David Gibsond6656ea2008-08-20 16:55:14 +1000711 *
712 * fdt_get_alias() retrieves the value of a given alias. That is, the
713 * value of the property named 'name' in the node /aliases.
714 *
715 * returns:
François Revol0f0ad8c2013-02-03 00:52:21 +0100716 * a pointer to the expansion of the alias named 'name', if it exists
David Gibsond6656ea2008-08-20 16:55:14 +1000717 * NULL, if the given alias or the /aliases node does not exist
718 */
719const char *fdt_get_alias(const void *fdt, const char *name);
720
721/**
Kumar Galac8ab7052007-10-24 11:04:22 -0500722 * fdt_get_path - determine the full path of a node
723 * @fdt: pointer to the device tree blob
724 * @nodeoffset: offset of the node whose path to find
725 * @buf: character buffer to contain the returned path (will be overwritten)
726 * @buflen: size of the character buffer at buf
727 *
728 * fdt_get_path() computes the full path of the node at offset
729 * nodeoffset, and records that path in the buffer at buf.
730 *
731 * NOTE: This function is expensive, as it must scan the device tree
732 * structure from the start to nodeoffset.
733 *
734 * returns:
735 * 0, on success
736 * buf contains the absolute path of the node at
737 * nodeoffset, as a NUL-terminated string.
Wolfgang Denka1be4762008-05-20 16:00:29 +0200738 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500739 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
740 * characters and will not fit in the given buffer.
741 * -FDT_ERR_BADMAGIC,
742 * -FDT_ERR_BADVERSION,
743 * -FDT_ERR_BADSTATE,
744 * -FDT_ERR_BADSTRUCTURE, standard meanings
745 */
746int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
747
748/**
749 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
750 * @fdt: pointer to the device tree blob
751 * @nodeoffset: offset of the node whose parent to find
752 * @supernodedepth: depth of the ancestor to find
753 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
754 *
755 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
756 * at a specific depth from the root (where the root itself has depth
757 * 0, its immediate subnodes depth 1 and so forth). So
758 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
759 * will always return 0, the offset of the root node. If the node at
760 * nodeoffset has depth D, then:
761 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
762 * will return nodeoffset itself.
763 *
764 * NOTE: This function is expensive, as it must scan the device tree
765 * structure from the start to nodeoffset.
766 *
767 * returns:
Kumar Galac8ab7052007-10-24 11:04:22 -0500768 * structure block offset of the node at node offset's ancestor
769 * of depth supernodedepth (>=0), on success
Wolfgang Denka1be4762008-05-20 16:00:29 +0200770 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600771 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
772 * nodeoffset
Kumar Galac8ab7052007-10-24 11:04:22 -0500773 * -FDT_ERR_BADMAGIC,
774 * -FDT_ERR_BADVERSION,
775 * -FDT_ERR_BADSTATE,
776 * -FDT_ERR_BADSTRUCTURE, standard meanings
777 */
778int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
779 int supernodedepth, int *nodedepth);
780
781/**
782 * fdt_node_depth - find the depth of a given node
783 * @fdt: pointer to the device tree blob
784 * @nodeoffset: offset of the node whose parent to find
785 *
786 * fdt_node_depth() finds the depth of a given node. The root node
787 * has depth 0, its immediate subnodes depth 1 and so forth.
788 *
789 * NOTE: This function is expensive, as it must scan the device tree
790 * structure from the start to nodeoffset.
791 *
792 * returns:
793 * depth of the node at nodeoffset (>=0), on success
Wolfgang Denka1be4762008-05-20 16:00:29 +0200794 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500795 * -FDT_ERR_BADMAGIC,
796 * -FDT_ERR_BADVERSION,
797 * -FDT_ERR_BADSTATE,
798 * -FDT_ERR_BADSTRUCTURE, standard meanings
799 */
800int fdt_node_depth(const void *fdt, int nodeoffset);
801
802/**
803 * fdt_parent_offset - find the parent of a given node
804 * @fdt: pointer to the device tree blob
805 * @nodeoffset: offset of the node whose parent to find
806 *
807 * fdt_parent_offset() locates the parent node of a given node (that
808 * is, it finds the offset of the node which contains the node at
809 * nodeoffset as a subnode).
810 *
811 * NOTE: This function is expensive, as it must scan the device tree
812 * structure from the start to nodeoffset, *twice*.
813 *
814 * returns:
Wolfram Sang9a36da92008-07-09 11:22:44 +0200815 * structure block offset of the parent of the node at nodeoffset
Kumar Galac8ab7052007-10-24 11:04:22 -0500816 * (>=0), on success
Wolfgang Denka1be4762008-05-20 16:00:29 +0200817 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500818 * -FDT_ERR_BADMAGIC,
819 * -FDT_ERR_BADVERSION,
820 * -FDT_ERR_BADSTATE,
821 * -FDT_ERR_BADSTRUCTURE, standard meanings
822 */
823int fdt_parent_offset(const void *fdt, int nodeoffset);
824
825/**
826 * fdt_node_offset_by_prop_value - find nodes with a given property value
827 * @fdt: pointer to the device tree blob
828 * @startoffset: only find nodes after this offset
829 * @propname: property name to check
830 * @propval: property value to search for
831 * @proplen: length of the value in propval
832 *
833 * fdt_node_offset_by_prop_value() returns the offset of the first
834 * node after startoffset, which has a property named propname whose
835 * value is of length proplen and has value equal to propval; or if
836 * startoffset is -1, the very first such node in the tree.
837 *
838 * To iterate through all nodes matching the criterion, the following
839 * idiom can be used:
840 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
841 * propval, proplen);
842 * while (offset != -FDT_ERR_NOTFOUND) {
Simon Glass536774d2014-07-30 03:59:03 -0600843 * // other code here
Kumar Galac8ab7052007-10-24 11:04:22 -0500844 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
845 * propval, proplen);
846 * }
847 *
848 * Note the -1 in the first call to the function, if 0 is used here
849 * instead, the function will never locate the root node, even if it
850 * matches the criterion.
851 *
852 * returns:
853 * structure block offset of the located node (>= 0, >startoffset),
854 * on success
855 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
856 * tree after startoffset
Wolfgang Denka1be4762008-05-20 16:00:29 +0200857 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500858 * -FDT_ERR_BADMAGIC,
859 * -FDT_ERR_BADVERSION,
860 * -FDT_ERR_BADSTATE,
861 * -FDT_ERR_BADSTRUCTURE, standard meanings
862 */
863int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
864 const char *propname,
865 const void *propval, int proplen);
866
867/**
868 * fdt_node_offset_by_phandle - find the node with a given phandle
869 * @fdt: pointer to the device tree blob
870 * @phandle: phandle value
871 *
Wolfram Sang9a36da92008-07-09 11:22:44 +0200872 * fdt_node_offset_by_phandle() returns the offset of the node
Kumar Galac8ab7052007-10-24 11:04:22 -0500873 * which has the given phandle value. If there is more than one node
874 * in the tree with the given phandle (an invalid tree), results are
875 * undefined.
876 *
877 * returns:
878 * structure block offset of the located node (>= 0), on success
879 * -FDT_ERR_NOTFOUND, no node with that phandle exists
880 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
881 * -FDT_ERR_BADMAGIC,
882 * -FDT_ERR_BADVERSION,
883 * -FDT_ERR_BADSTATE,
884 * -FDT_ERR_BADSTRUCTURE, standard meanings
885 */
886int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
887
888/**
889 * fdt_node_check_compatible: check a node's compatible property
890 * @fdt: pointer to the device tree blob
891 * @nodeoffset: offset of a tree node
892 * @compatible: string to match against
893 *
894 *
895 * fdt_node_check_compatible() returns 0 if the given node contains a
896 * 'compatible' property with the given string as one of its elements,
897 * it returns non-zero otherwise, or on error.
898 *
899 * returns:
900 * 0, if the node has a 'compatible' property listing the given string
901 * 1, if the node has a 'compatible' property, but it does not list
902 * the given string
903 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
Wolfgang Denka1be4762008-05-20 16:00:29 +0200904 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500905 * -FDT_ERR_BADMAGIC,
906 * -FDT_ERR_BADVERSION,
907 * -FDT_ERR_BADSTATE,
908 * -FDT_ERR_BADSTRUCTURE, standard meanings
909 */
910int fdt_node_check_compatible(const void *fdt, int nodeoffset,
911 const char *compatible);
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400912
Kumar Galac8ab7052007-10-24 11:04:22 -0500913/**
914 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
915 * @fdt: pointer to the device tree blob
916 * @startoffset: only find nodes after this offset
917 * @compatible: 'compatible' string to match against
918 *
919 * fdt_node_offset_by_compatible() returns the offset of the first
920 * node after startoffset, which has a 'compatible' property which
921 * lists the given compatible string; or if startoffset is -1, the
922 * very first such node in the tree.
923 *
924 * To iterate through all nodes matching the criterion, the following
925 * idiom can be used:
926 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
927 * while (offset != -FDT_ERR_NOTFOUND) {
Simon Glass536774d2014-07-30 03:59:03 -0600928 * // other code here
Kumar Galac8ab7052007-10-24 11:04:22 -0500929 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
930 * }
931 *
932 * Note the -1 in the first call to the function, if 0 is used here
933 * instead, the function will never locate the root node, even if it
934 * matches the criterion.
935 *
936 * returns:
937 * structure block offset of the located node (>= 0, >startoffset),
938 * on success
939 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
940 * tree after startoffset
Wolfgang Denka1be4762008-05-20 16:00:29 +0200941 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Galac8ab7052007-10-24 11:04:22 -0500942 * -FDT_ERR_BADMAGIC,
943 * -FDT_ERR_BADVERSION,
944 * -FDT_ERR_BADSTATE,
945 * -FDT_ERR_BADSTRUCTURE, standard meanings
946 */
947int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
948 const char *compatible);
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400949
Simon Glass73b47562013-01-21 12:59:18 -0800950/**
951 * fdt_stringlist_contains - check a string list property for a string
952 * @strlist: Property containing a list of strings to check
953 * @listlen: Length of property
954 * @str: String to search for
955 *
956 * This is a utility function provided for convenience. The list contains
957 * one or more strings, each terminated by \0, as is found in a device tree
958 * "compatible" property.
959 *
960 * @return: 1 if the string is found in the list, 0 not found, or invalid list
961 */
962int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
963
Thierry Reding92ba09e2014-08-26 17:33:50 +0200964/**
Simon Glassb0ea7402016-10-02 17:59:28 -0600965 * fdt_stringlist_count - count the number of strings in a string list
Thierry Reding92ba09e2014-08-26 17:33:50 +0200966 * @fdt: pointer to the device tree blob
Simon Glassb0ea7402016-10-02 17:59:28 -0600967 * @nodeoffset: offset of a tree node
Thierry Reding92ba09e2014-08-26 17:33:50 +0200968 * @property: name of the property containing the string list
Simon Glassb0ea7402016-10-02 17:59:28 -0600969 * @return:
970 * the number of strings in the given property
971 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
972 * -FDT_ERR_NOTFOUND if the property does not exist
Thierry Reding92ba09e2014-08-26 17:33:50 +0200973 */
Simon Glassb0ea7402016-10-02 17:59:28 -0600974int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
Thierry Reding92ba09e2014-08-26 17:33:50 +0200975
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200976/**
Simon Glassb0ea7402016-10-02 17:59:28 -0600977 * fdt_stringlist_search - find a string in a string list and return its index
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200978 * @fdt: pointer to the device tree blob
Simon Glassb0ea7402016-10-02 17:59:28 -0600979 * @nodeoffset: offset of a tree node
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200980 * @property: name of the property containing the string list
981 * @string: string to look up in the string list
Simon Glassb0ea7402016-10-02 17:59:28 -0600982 *
983 * Note that it is possible for this function to succeed on property values
984 * that are not NUL-terminated. That's because the function will stop after
985 * finding the first occurrence of @string. This can for example happen with
986 * small-valued cell properties, such as #address-cells, when searching for
987 * the empty string.
988 *
989 * @return:
990 * the index of the string in the list of strings
991 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
992 * -FDT_ERR_NOTFOUND if the property does not exist or does not contain
993 * the given string
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200994 */
Simon Glassb0ea7402016-10-02 17:59:28 -0600995int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
996 const char *string);
Thierry Redinga24f1ac2014-08-26 17:33:51 +0200997
Thierry Reding5504b802014-08-26 17:33:52 +0200998/**
Simon Glassb0ea7402016-10-02 17:59:28 -0600999 * fdt_stringlist_get() - obtain the string at a given index in a string list
Thierry Reding5504b802014-08-26 17:33:52 +02001000 * @fdt: pointer to the device tree blob
Simon Glassb0ea7402016-10-02 17:59:28 -06001001 * @nodeoffset: offset of a tree node
Thierry Reding5504b802014-08-26 17:33:52 +02001002 * @property: name of the property containing the string list
1003 * @index: index of the string to return
Simon Glassb0ea7402016-10-02 17:59:28 -06001004 * @lenp: return location for the string length or an error code on failure
Thierry Reding5504b802014-08-26 17:33:52 +02001005 *
Simon Glassb0ea7402016-10-02 17:59:28 -06001006 * Note that this will successfully extract strings from properties with
1007 * non-NUL-terminated values. For example on small-valued cell properties
1008 * this function will return the empty string.
Thierry Reding5504b802014-08-26 17:33:52 +02001009 *
Simon Glassb0ea7402016-10-02 17:59:28 -06001010 * If non-NULL, the length of the string (on success) or a negative error-code
1011 * (on failure) will be stored in the integer pointer to by lenp.
1012 *
1013 * @return:
1014 * A pointer to the string at the given index in the string list or NULL on
1015 * failure. On success the length of the string will be stored in the memory
1016 * location pointed to by the lenp parameter, if non-NULL. On failure one of
1017 * the following negative error codes will be returned in the lenp parameter
1018 * (if non-NULL):
1019 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1020 * -FDT_ERR_NOTFOUND if the property does not exist
Thierry Reding5504b802014-08-26 17:33:52 +02001021 */
Simon Glassb0ea7402016-10-02 17:59:28 -06001022const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
1023 const char *property, int index,
1024 int *lenp);
Thierry Reding5504b802014-08-26 17:33:52 +02001025
Kumar Galac8ab7052007-10-24 11:04:22 -05001026/**********************************************************************/
Simon Glass536774d2014-07-30 03:59:03 -06001027/* Read-only functions (addressing related) */
1028/**********************************************************************/
1029
1030/**
1031 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
1032 *
1033 * This is the maximum value for #address-cells, #size-cells and
1034 * similar properties that will be processed by libfdt. IEE1275
1035 * requires that OF implementations handle values up to 4.
1036 * Implementations may support larger values, but in practice higher
1037 * values aren't used.
1038 */
1039#define FDT_MAX_NCELLS 4
1040
1041/**
1042 * fdt_address_cells - retrieve address size for a bus represented in the tree
1043 * @fdt: pointer to the device tree blob
1044 * @nodeoffset: offset of the node to find the address size for
1045 *
1046 * When the node has a valid #address-cells property, returns its value.
1047 *
1048 * returns:
1049 * 0 <= n < FDT_MAX_NCELLS, on success
1050 * 2, if the node has no #address-cells property
1051 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1052 * #address-cells property
1053 * -FDT_ERR_BADMAGIC,
1054 * -FDT_ERR_BADVERSION,
1055 * -FDT_ERR_BADSTATE,
1056 * -FDT_ERR_BADSTRUCTURE,
1057 * -FDT_ERR_TRUNCATED, standard meanings
1058 */
1059int fdt_address_cells(const void *fdt, int nodeoffset);
1060
1061/**
1062 * fdt_size_cells - retrieve address range size for a bus represented in the
1063 * tree
1064 * @fdt: pointer to the device tree blob
1065 * @nodeoffset: offset of the node to find the address range size for
1066 *
1067 * When the node has a valid #size-cells property, returns its value.
1068 *
1069 * returns:
1070 * 0 <= n < FDT_MAX_NCELLS, on success
1071 * 2, if the node has no #address-cells property
1072 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1073 * #size-cells property
1074 * -FDT_ERR_BADMAGIC,
1075 * -FDT_ERR_BADVERSION,
1076 * -FDT_ERR_BADSTATE,
1077 * -FDT_ERR_BADSTRUCTURE,
1078 * -FDT_ERR_TRUNCATED, standard meanings
1079 */
1080int fdt_size_cells(const void *fdt, int nodeoffset);
1081
1082
1083/**********************************************************************/
Kumar Galac8ab7052007-10-24 11:04:22 -05001084/* Write-in-place functions */
1085/**********************************************************************/
Gerald Van Barenca8d8a62007-03-31 12:13:43 -04001086
David Gibsone85cae12007-12-17 14:41:52 +11001087/**
Maxime Ripard3559c792016-07-05 10:26:43 +02001088 * fdt_setprop_inplace_namelen_partial - change a property's value,
1089 * but not its size
1090 * @fdt: pointer to the device tree blob
1091 * @nodeoffset: offset of the node whose property to change
1092 * @name: name of the property to change
1093 * @namelen: number of characters of name to consider
Simon Glassdfdcd6f2016-10-02 17:59:27 -06001094 * @idx: index of the property to change in the array
Maxime Ripard3559c792016-07-05 10:26:43 +02001095 * @val: pointer to data to replace the property value with
1096 * @len: length of the property value
1097 *
1098 * Identical to fdt_setprop_inplace(), but modifies the given property
1099 * starting from the given index, and using only the first characters
1100 * of the name. It is useful when you want to manipulate only one value of
1101 * an array and you have a string that doesn't end with \0.
1102 */
1103int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
1104 const char *name, int namelen,
Simon Glassdfdcd6f2016-10-02 17:59:27 -06001105 uint32_t idx, const void *val,
Maxime Ripard3559c792016-07-05 10:26:43 +02001106 int len);
1107
1108/**
David Gibsone85cae12007-12-17 14:41:52 +11001109 * fdt_setprop_inplace - change a property's value, but not its size
1110 * @fdt: pointer to the device tree blob
1111 * @nodeoffset: offset of the node whose property to change
1112 * @name: name of the property to change
1113 * @val: pointer to data to replace the property value with
1114 * @len: length of the property value
1115 *
1116 * fdt_setprop_inplace() replaces the value of a given property with
1117 * the data in val, of length len. This function cannot change the
1118 * size of a property, and so will only work if len is equal to the
1119 * current length of the property.
1120 *
1121 * This function will alter only the bytes in the blob which contain
1122 * the given property value, and will not alter or move any other part
1123 * of the tree.
1124 *
1125 * returns:
1126 * 0, on success
1127 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1128 * -FDT_ERR_NOTFOUND, node does not have the named property
1129 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1130 * -FDT_ERR_BADMAGIC,
1131 * -FDT_ERR_BADVERSION,
1132 * -FDT_ERR_BADSTATE,
1133 * -FDT_ERR_BADSTRUCTURE,
1134 * -FDT_ERR_TRUNCATED, standard meanings
1135 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001136int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1137 const void *val, int len);
David Gibsone85cae12007-12-17 14:41:52 +11001138
1139/**
David Gibsonb4afa9a2012-08-27 19:39:00 -05001140 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
David Gibsone85cae12007-12-17 14:41:52 +11001141 * @fdt: pointer to the device tree blob
1142 * @nodeoffset: offset of the node whose property to change
1143 * @name: name of the property to change
David Gibsonb4afa9a2012-08-27 19:39:00 -05001144 * @val: 32-bit integer value to replace the property with
David Gibsone85cae12007-12-17 14:41:52 +11001145 *
David Gibsonb4afa9a2012-08-27 19:39:00 -05001146 * fdt_setprop_inplace_u32() replaces the value of a given property
1147 * with the 32-bit integer value in val, converting val to big-endian
1148 * if necessary. This function cannot change the size of a property,
1149 * and so will only work if the property already exists and has length
1150 * 4.
David Gibsone85cae12007-12-17 14:41:52 +11001151 *
1152 * This function will alter only the bytes in the blob which contain
1153 * the given property value, and will not alter or move any other part
1154 * of the tree.
1155 *
1156 * returns:
1157 * 0, on success
1158 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
David Gibsonb4afa9a2012-08-27 19:39:00 -05001159 * -FDT_ERR_NOTFOUND, node does not have the named property
David Gibsone85cae12007-12-17 14:41:52 +11001160 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1161 * -FDT_ERR_BADMAGIC,
1162 * -FDT_ERR_BADVERSION,
1163 * -FDT_ERR_BADSTATE,
1164 * -FDT_ERR_BADSTRUCTURE,
1165 * -FDT_ERR_TRUNCATED, standard meanings
1166 */
David Gibsonb4afa9a2012-08-27 19:39:00 -05001167static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1168 const char *name, uint32_t val)
Kumar Galac8ab7052007-10-24 11:04:22 -05001169{
Kim Phillips38c38152013-01-16 13:59:50 +00001170 fdt32_t tmp = cpu_to_fdt32(val);
1171 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
Kumar Galac8ab7052007-10-24 11:04:22 -05001172}
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001173
David Gibsone85cae12007-12-17 14:41:52 +11001174/**
David Gibsonb4afa9a2012-08-27 19:39:00 -05001175 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1176 * @fdt: pointer to the device tree blob
1177 * @nodeoffset: offset of the node whose property to change
1178 * @name: name of the property to change
1179 * @val: 64-bit integer value to replace the property with
1180 *
1181 * fdt_setprop_inplace_u64() replaces the value of a given property
1182 * with the 64-bit integer value in val, converting val to big-endian
1183 * if necessary. This function cannot change the size of a property,
1184 * and so will only work if the property already exists and has length
1185 * 8.
1186 *
1187 * This function will alter only the bytes in the blob which contain
1188 * the given property value, and will not alter or move any other part
1189 * of the tree.
1190 *
1191 * returns:
1192 * 0, on success
1193 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1194 * -FDT_ERR_NOTFOUND, node does not have the named property
1195 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1196 * -FDT_ERR_BADMAGIC,
1197 * -FDT_ERR_BADVERSION,
1198 * -FDT_ERR_BADSTATE,
1199 * -FDT_ERR_BADSTRUCTURE,
1200 * -FDT_ERR_TRUNCATED, standard meanings
1201 */
1202static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1203 const char *name, uint64_t val)
1204{
Kim Phillips38c38152013-01-16 13:59:50 +00001205 fdt64_t tmp = cpu_to_fdt64(val);
1206 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
David Gibsonb4afa9a2012-08-27 19:39:00 -05001207}
1208
1209/**
1210 * fdt_setprop_inplace_cell - change the value of a single-cell property
1211 *
1212 * This is an alternative name for fdt_setprop_inplace_u32()
1213 */
1214static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1215 const char *name, uint32_t val)
1216{
1217 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1218}
1219
1220/**
David Gibsone85cae12007-12-17 14:41:52 +11001221 * fdt_nop_property - replace a property with nop tags
1222 * @fdt: pointer to the device tree blob
1223 * @nodeoffset: offset of the node whose property to nop
1224 * @name: name of the property to nop
1225 *
1226 * fdt_nop_property() will replace a given property's representation
1227 * in the blob with FDT_NOP tags, effectively removing it from the
1228 * tree.
1229 *
1230 * This function will alter only the bytes in the blob which contain
1231 * the property, and will not alter or move any other part of the
1232 * tree.
1233 *
1234 * returns:
1235 * 0, on success
1236 * -FDT_ERR_NOTFOUND, node does not have the named property
1237 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1238 * -FDT_ERR_BADMAGIC,
1239 * -FDT_ERR_BADVERSION,
1240 * -FDT_ERR_BADSTATE,
1241 * -FDT_ERR_BADSTRUCTURE,
1242 * -FDT_ERR_TRUNCATED, standard meanings
1243 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001244int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
David Gibsone85cae12007-12-17 14:41:52 +11001245
1246/**
1247 * fdt_nop_node - replace a node (subtree) with nop tags
1248 * @fdt: pointer to the device tree blob
1249 * @nodeoffset: offset of the node to nop
1250 *
1251 * fdt_nop_node() will replace a given node's representation in the
1252 * blob, including all its subnodes, if any, with FDT_NOP tags,
1253 * effectively removing it from the tree.
1254 *
1255 * This function will alter only the bytes in the blob which contain
1256 * the node and its properties and subnodes, and will not alter or
1257 * move any other part of the tree.
1258 *
1259 * returns:
1260 * 0, on success
1261 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1262 * -FDT_ERR_BADMAGIC,
1263 * -FDT_ERR_BADVERSION,
1264 * -FDT_ERR_BADSTATE,
1265 * -FDT_ERR_BADSTRUCTURE,
1266 * -FDT_ERR_TRUNCATED, standard meanings
1267 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001268int fdt_nop_node(void *fdt, int nodeoffset);
Gerald Van Barenc077a132007-04-14 22:46:41 -04001269
Kumar Galac8ab7052007-10-24 11:04:22 -05001270/**********************************************************************/
1271/* Sequential write functions */
1272/**********************************************************************/
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001273
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001274int fdt_create(void *buf, int bufsize);
Simon Glass536774d2014-07-30 03:59:03 -06001275int fdt_resize(void *fdt, void *buf, int bufsize);
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001276int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1277int fdt_finish_reservemap(void *fdt);
1278int fdt_begin_node(void *fdt, const char *name);
1279int fdt_property(void *fdt, const char *name, const void *val, int len);
David Gibsonb4afa9a2012-08-27 19:39:00 -05001280static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
Kumar Galac8ab7052007-10-24 11:04:22 -05001281{
Kim Phillips38c38152013-01-16 13:59:50 +00001282 fdt32_t tmp = cpu_to_fdt32(val);
1283 return fdt_property(fdt, name, &tmp, sizeof(tmp));
Kumar Galac8ab7052007-10-24 11:04:22 -05001284}
David Gibsonb4afa9a2012-08-27 19:39:00 -05001285static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1286{
Kim Phillips38c38152013-01-16 13:59:50 +00001287 fdt64_t tmp = cpu_to_fdt64(val);
1288 return fdt_property(fdt, name, &tmp, sizeof(tmp));
David Gibsonb4afa9a2012-08-27 19:39:00 -05001289}
1290static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1291{
1292 return fdt_property_u32(fdt, name, val);
1293}
Simon Glass4973bf52016-02-22 22:55:39 -07001294
1295/**
1296 * fdt_property_placeholder - add a new property and return a ptr to its value
1297 *
1298 * @fdt: pointer to the device tree blob
1299 * @name: name of property to add
1300 * @len: length of property value in bytes
1301 * @valp: returns a pointer to where where the value should be placed
1302 *
1303 * returns:
1304 * 0, on success
1305 * -FDT_ERR_BADMAGIC,
1306 * -FDT_ERR_NOSPACE, standard meanings
1307 */
1308int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
1309
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001310#define fdt_property_string(fdt, name, str) \
1311 fdt_property(fdt, name, str, strlen(str)+1)
1312int fdt_end_node(void *fdt);
1313int fdt_finish(void *fdt);
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001314
Kumar Galac8ab7052007-10-24 11:04:22 -05001315/**********************************************************************/
1316/* Read-write functions */
1317/**********************************************************************/
1318
Gerald Van Barencb17cfa2012-10-15 22:24:36 -04001319int fdt_create_empty_tree(void *buf, int bufsize);
Kumar Galac8ab7052007-10-24 11:04:22 -05001320int fdt_open_into(const void *fdt, void *buf, int bufsize);
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001321int fdt_pack(void *fdt);
1322
David Gibson24d828f2007-12-17 14:42:07 +11001323/**
1324 * fdt_add_mem_rsv - add one memory reserve map entry
1325 * @fdt: pointer to the device tree blob
Wolfram Sang9a36da92008-07-09 11:22:44 +02001326 * @address, @size: 64-bit values (native endian)
David Gibson24d828f2007-12-17 14:42:07 +11001327 *
1328 * Adds a reserve map entry to the given blob reserving a region at
1329 * address address of length size.
1330 *
1331 * This function will insert data into the reserve map and will
Wolfram Sang9a36da92008-07-09 11:22:44 +02001332 * therefore change the indexes of some entries in the table.
David Gibson24d828f2007-12-17 14:42:07 +11001333 *
1334 * returns:
1335 * 0, on success
1336 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1337 * contain the new reservation entry
1338 * -FDT_ERR_BADMAGIC,
1339 * -FDT_ERR_BADVERSION,
1340 * -FDT_ERR_BADSTATE,
1341 * -FDT_ERR_BADSTRUCTURE,
1342 * -FDT_ERR_BADLAYOUT,
1343 * -FDT_ERR_TRUNCATED, standard meanings
1344 */
Kumar Galac8ab7052007-10-24 11:04:22 -05001345int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
David Gibson24d828f2007-12-17 14:42:07 +11001346
1347/**
1348 * fdt_del_mem_rsv - remove a memory reserve map entry
1349 * @fdt: pointer to the device tree blob
1350 * @n: entry to remove
1351 *
1352 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1353 * the blob.
1354 *
1355 * This function will delete data from the reservation table and will
Wolfram Sang9a36da92008-07-09 11:22:44 +02001356 * therefore change the indexes of some entries in the table.
David Gibson24d828f2007-12-17 14:42:07 +11001357 *
1358 * returns:
1359 * 0, on success
1360 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1361 * are less than n+1 reserve map entries)
1362 * -FDT_ERR_BADMAGIC,
1363 * -FDT_ERR_BADVERSION,
1364 * -FDT_ERR_BADSTATE,
1365 * -FDT_ERR_BADSTRUCTURE,
1366 * -FDT_ERR_BADLAYOUT,
1367 * -FDT_ERR_TRUNCATED, standard meanings
1368 */
Kumar Galac8ab7052007-10-24 11:04:22 -05001369int fdt_del_mem_rsv(void *fdt, int n);
1370
David Gibson24d828f2007-12-17 14:42:07 +11001371/**
David Gibsonfd1ea8f2008-01-11 14:55:05 +11001372 * fdt_set_name - change the name of a given node
1373 * @fdt: pointer to the device tree blob
1374 * @nodeoffset: structure block offset of a node
1375 * @name: name to give the node
1376 *
1377 * fdt_set_name() replaces the name (including unit address, if any)
1378 * of the given node with the given string. NOTE: this function can't
1379 * efficiently check if the new name is unique amongst the given
1380 * node's siblings; results are undefined if this function is invoked
1381 * with a name equal to one of the given node's siblings.
1382 *
1383 * This function may insert or delete data from the blob, and will
1384 * therefore change the offsets of some existing nodes.
1385 *
1386 * returns:
1387 * 0, on success
1388 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1389 * to contain the new name
1390 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1391 * -FDT_ERR_BADMAGIC,
1392 * -FDT_ERR_BADVERSION,
1393 * -FDT_ERR_BADSTATE, standard meanings
1394 */
1395int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1396
1397/**
David Gibson24d828f2007-12-17 14:42:07 +11001398 * fdt_setprop - create or change a property
1399 * @fdt: pointer to the device tree blob
1400 * @nodeoffset: offset of the node whose property to change
1401 * @name: name of the property to change
1402 * @val: pointer to data to set the property value to
1403 * @len: length of the property value
1404 *
1405 * fdt_setprop() sets the value of the named property in the given
Wolfram Sang9a36da92008-07-09 11:22:44 +02001406 * node to the given value and length, creating the property if it
David Gibson24d828f2007-12-17 14:42:07 +11001407 * does not already exist.
1408 *
1409 * This function may insert or delete data from the blob, and will
1410 * therefore change the offsets of some existing nodes.
1411 *
1412 * returns:
1413 * 0, on success
1414 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1415 * contain the new property value
1416 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1417 * -FDT_ERR_BADLAYOUT,
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 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001425int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1426 const void *val, int len);
David Gibson24d828f2007-12-17 14:42:07 +11001427
1428/**
David Gibsonb4afa9a2012-08-27 19:39:00 -05001429 * fdt_setprop_u32 - set a property to a 32-bit integer
David Gibson24d828f2007-12-17 14:42:07 +11001430 * @fdt: pointer to the device tree blob
1431 * @nodeoffset: offset of the node whose property to change
1432 * @name: name of the property to change
1433 * @val: 32-bit integer value for the property (native endian)
1434 *
David Gibsonb4afa9a2012-08-27 19:39:00 -05001435 * fdt_setprop_u32() sets the value of the named property in the given
1436 * node to the given 32-bit integer value (converting to big-endian if
David Gibson24d828f2007-12-17 14:42:07 +11001437 * necessary), or creates a new property with that value if it does
1438 * not already exist.
1439 *
1440 * This function may insert or delete data from the blob, and will
1441 * therefore change the offsets of some existing nodes.
1442 *
1443 * returns:
1444 * 0, on success
1445 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1446 * contain the new property value
1447 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1448 * -FDT_ERR_BADLAYOUT,
1449 * -FDT_ERR_BADMAGIC,
1450 * -FDT_ERR_BADVERSION,
1451 * -FDT_ERR_BADSTATE,
1452 * -FDT_ERR_BADSTRUCTURE,
1453 * -FDT_ERR_BADLAYOUT,
1454 * -FDT_ERR_TRUNCATED, standard meanings
1455 */
David Gibsonb4afa9a2012-08-27 19:39:00 -05001456static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1457 uint32_t val)
Kumar Galac8ab7052007-10-24 11:04:22 -05001458{
Kim Phillips38c38152013-01-16 13:59:50 +00001459 fdt32_t tmp = cpu_to_fdt32(val);
1460 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
Kumar Galac8ab7052007-10-24 11:04:22 -05001461}
David Gibson24d828f2007-12-17 14:42:07 +11001462
1463/**
David Gibsonb4afa9a2012-08-27 19:39:00 -05001464 * fdt_setprop_u64 - set a property to a 64-bit integer
1465 * @fdt: pointer to the device tree blob
1466 * @nodeoffset: offset of the node whose property to change
1467 * @name: name of the property to change
1468 * @val: 64-bit integer value for the property (native endian)
1469 *
1470 * fdt_setprop_u64() sets the value of the named property in the given
1471 * node to the given 64-bit integer value (converting to big-endian if
1472 * necessary), or creates a new property with that value if it does
1473 * not already exist.
1474 *
1475 * This function may insert or delete data from the blob, and will
1476 * therefore change the offsets of some existing nodes.
1477 *
1478 * returns:
1479 * 0, on success
1480 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1481 * contain the new property value
1482 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1483 * -FDT_ERR_BADLAYOUT,
1484 * -FDT_ERR_BADMAGIC,
1485 * -FDT_ERR_BADVERSION,
1486 * -FDT_ERR_BADSTATE,
1487 * -FDT_ERR_BADSTRUCTURE,
1488 * -FDT_ERR_BADLAYOUT,
1489 * -FDT_ERR_TRUNCATED, standard meanings
1490 */
1491static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1492 uint64_t val)
1493{
Kim Phillips38c38152013-01-16 13:59:50 +00001494 fdt64_t tmp = cpu_to_fdt64(val);
1495 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
David Gibsonb4afa9a2012-08-27 19:39:00 -05001496}
1497
1498/**
1499 * fdt_setprop_cell - set a property to a single cell value
1500 *
1501 * This is an alternative name for fdt_setprop_u32()
1502 */
1503static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1504 uint32_t val)
1505{
1506 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1507}
1508
1509/**
David Gibson24d828f2007-12-17 14:42:07 +11001510 * fdt_setprop_string - set a property to a string value
1511 * @fdt: pointer to the device tree blob
1512 * @nodeoffset: offset of the node whose property to change
1513 * @name: name of the property to change
1514 * @str: string value for the property
1515 *
1516 * fdt_setprop_string() sets the value of the named property in the
1517 * given node to the given string value (using the length of the
1518 * string to determine the new length of the property), or creates a
1519 * new property with that value if it does not already exist.
1520 *
1521 * This function may insert or delete data from the blob, and will
1522 * therefore change the offsets of some existing nodes.
1523 *
1524 * returns:
1525 * 0, on success
1526 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1527 * contain the new property value
1528 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1529 * -FDT_ERR_BADLAYOUT,
1530 * -FDT_ERR_BADMAGIC,
1531 * -FDT_ERR_BADVERSION,
1532 * -FDT_ERR_BADSTATE,
1533 * -FDT_ERR_BADSTRUCTURE,
1534 * -FDT_ERR_BADLAYOUT,
1535 * -FDT_ERR_TRUNCATED, standard meanings
1536 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001537#define fdt_setprop_string(fdt, nodeoffset, name, str) \
1538 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
David Gibson24d828f2007-12-17 14:42:07 +11001539
1540/**
Minghuan Lian30648272012-08-27 19:38:59 -05001541 * fdt_appendprop - append to or create a property
1542 * @fdt: pointer to the device tree blob
1543 * @nodeoffset: offset of the node whose property to change
1544 * @name: name of the property to append to
1545 * @val: pointer to data to append to the property value
1546 * @len: length of the data to append to the property value
1547 *
1548 * fdt_appendprop() appends the value to the named property in the
1549 * given node, creating the property if it does not already exist.
1550 *
1551 * This function may insert data into the blob, and will therefore
1552 * change the offsets of some existing nodes.
1553 *
1554 * returns:
1555 * 0, on success
1556 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1557 * contain the new property value
1558 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1559 * -FDT_ERR_BADLAYOUT,
1560 * -FDT_ERR_BADMAGIC,
1561 * -FDT_ERR_BADVERSION,
1562 * -FDT_ERR_BADSTATE,
1563 * -FDT_ERR_BADSTRUCTURE,
1564 * -FDT_ERR_BADLAYOUT,
1565 * -FDT_ERR_TRUNCATED, standard meanings
1566 */
1567int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1568 const void *val, int len);
1569
1570/**
David Gibsonb4afa9a2012-08-27 19:39:00 -05001571 * fdt_appendprop_u32 - append a 32-bit integer value to a property
Minghuan Lian30648272012-08-27 19:38:59 -05001572 * @fdt: pointer to the device tree blob
1573 * @nodeoffset: offset of the node whose property to change
1574 * @name: name of the property to change
1575 * @val: 32-bit integer value to append to the property (native endian)
1576 *
David Gibsonb4afa9a2012-08-27 19:39:00 -05001577 * fdt_appendprop_u32() appends the given 32-bit integer value
1578 * (converting to big-endian if necessary) to the value of the named
1579 * property in the given node, or creates a new property with that
1580 * value if it does not already exist.
Minghuan Lian30648272012-08-27 19:38:59 -05001581 *
1582 * This function may insert data into the blob, and will therefore
1583 * change the offsets of some existing nodes.
1584 *
1585 * returns:
1586 * 0, on success
1587 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1588 * contain the new property value
1589 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1590 * -FDT_ERR_BADLAYOUT,
1591 * -FDT_ERR_BADMAGIC,
1592 * -FDT_ERR_BADVERSION,
1593 * -FDT_ERR_BADSTATE,
1594 * -FDT_ERR_BADSTRUCTURE,
1595 * -FDT_ERR_BADLAYOUT,
1596 * -FDT_ERR_TRUNCATED, standard meanings
1597 */
David Gibsonb4afa9a2012-08-27 19:39:00 -05001598static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1599 const char *name, uint32_t val)
Minghuan Lian30648272012-08-27 19:38:59 -05001600{
Kim Phillips38c38152013-01-16 13:59:50 +00001601 fdt32_t tmp = cpu_to_fdt32(val);
1602 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
Minghuan Lian30648272012-08-27 19:38:59 -05001603}
1604
1605/**
David Gibsonb4afa9a2012-08-27 19:39:00 -05001606 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1607 * @fdt: pointer to the device tree blob
1608 * @nodeoffset: offset of the node whose property to change
1609 * @name: name of the property to change
1610 * @val: 64-bit integer value to append to the property (native endian)
1611 *
1612 * fdt_appendprop_u64() appends the given 64-bit integer value
1613 * (converting to big-endian if necessary) to the value of the named
1614 * property in the given node, or creates a new property with that
1615 * value if it does not already exist.
1616 *
1617 * This function may insert data into the blob, and will therefore
1618 * change the offsets of some existing nodes.
1619 *
1620 * returns:
1621 * 0, on success
1622 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1623 * contain the new property value
1624 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1625 * -FDT_ERR_BADLAYOUT,
1626 * -FDT_ERR_BADMAGIC,
1627 * -FDT_ERR_BADVERSION,
1628 * -FDT_ERR_BADSTATE,
1629 * -FDT_ERR_BADSTRUCTURE,
1630 * -FDT_ERR_BADLAYOUT,
1631 * -FDT_ERR_TRUNCATED, standard meanings
1632 */
1633static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1634 const char *name, uint64_t val)
1635{
Kim Phillips38c38152013-01-16 13:59:50 +00001636 fdt64_t tmp = cpu_to_fdt64(val);
1637 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
David Gibsonb4afa9a2012-08-27 19:39:00 -05001638}
1639
1640/**
1641 * fdt_appendprop_cell - append a single cell value to a property
1642 *
1643 * This is an alternative name for fdt_appendprop_u32()
1644 */
1645static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1646 const char *name, uint32_t val)
1647{
1648 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1649}
1650
1651/**
Minghuan Lian30648272012-08-27 19:38:59 -05001652 * fdt_appendprop_string - append a string to a property
1653 * @fdt: pointer to the device tree blob
1654 * @nodeoffset: offset of the node whose property to change
1655 * @name: name of the property to change
1656 * @str: string value to append to the property
1657 *
1658 * fdt_appendprop_string() appends the given string to the value of
1659 * the named property in the given node, or creates a new property
1660 * with that value if it does not already exist.
1661 *
1662 * This function may insert data into the blob, and will therefore
1663 * change the offsets of some existing nodes.
1664 *
1665 * returns:
1666 * 0, on success
1667 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1668 * contain the new property value
1669 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1670 * -FDT_ERR_BADLAYOUT,
1671 * -FDT_ERR_BADMAGIC,
1672 * -FDT_ERR_BADVERSION,
1673 * -FDT_ERR_BADSTATE,
1674 * -FDT_ERR_BADSTRUCTURE,
1675 * -FDT_ERR_BADLAYOUT,
1676 * -FDT_ERR_TRUNCATED, standard meanings
1677 */
1678#define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1679 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1680
1681/**
David Gibson24d828f2007-12-17 14:42:07 +11001682 * fdt_delprop - delete a property
1683 * @fdt: pointer to the device tree blob
1684 * @nodeoffset: offset of the node whose property to nop
1685 * @name: name of the property to nop
1686 *
1687 * fdt_del_property() will delete the given property.
1688 *
1689 * This function will delete data from the blob, and will therefore
1690 * change the offsets of some existing nodes.
1691 *
1692 * returns:
1693 * 0, on success
1694 * -FDT_ERR_NOTFOUND, node does not have the named property
1695 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1696 * -FDT_ERR_BADLAYOUT,
1697 * -FDT_ERR_BADMAGIC,
1698 * -FDT_ERR_BADVERSION,
1699 * -FDT_ERR_BADSTATE,
1700 * -FDT_ERR_BADSTRUCTURE,
1701 * -FDT_ERR_TRUNCATED, standard meanings
1702 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001703int fdt_delprop(void *fdt, int nodeoffset, const char *name);
David Gibson24d828f2007-12-17 14:42:07 +11001704
1705/**
1706 * fdt_add_subnode_namelen - creates a new node based on substring
1707 * @fdt: pointer to the device tree blob
1708 * @parentoffset: structure block offset of a node
1709 * @name: name of the subnode to locate
1710 * @namelen: number of characters of name to consider
1711 *
1712 * Identical to fdt_add_subnode(), but use only the first namelen
1713 * characters of name as the name of the new node. This is useful for
1714 * creating subnodes based on a portion of a larger string, such as a
1715 * full path.
1716 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001717int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1718 const char *name, int namelen);
David Gibson24d828f2007-12-17 14:42:07 +11001719
1720/**
1721 * fdt_add_subnode - creates a new node
1722 * @fdt: pointer to the device tree blob
1723 * @parentoffset: structure block offset of a node
1724 * @name: name of the subnode to locate
1725 *
1726 * fdt_add_subnode() creates a new node as a subnode of the node at
1727 * structure block offset parentoffset, with the given name (which
1728 * should include the unit address, if any).
1729 *
1730 * This function will insert data into the blob, and will therefore
1731 * change the offsets of some existing nodes.
1732
1733 * returns:
Simon Glassdfdcd6f2016-10-02 17:59:27 -06001734 * structure block offset of the created nodeequested subnode (>=0), on
1735 * success
David Gibson24d828f2007-12-17 14:42:07 +11001736 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
Simon Glassdfdcd6f2016-10-02 17:59:27 -06001737 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
1738 * tag
David Gibson24d828f2007-12-17 14:42:07 +11001739 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1740 * the given name
1741 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1742 * blob to contain the new node
1743 * -FDT_ERR_NOSPACE
1744 * -FDT_ERR_BADLAYOUT
1745 * -FDT_ERR_BADMAGIC,
1746 * -FDT_ERR_BADVERSION,
1747 * -FDT_ERR_BADSTATE,
1748 * -FDT_ERR_BADSTRUCTURE,
1749 * -FDT_ERR_TRUNCATED, standard meanings.
1750 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001751int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
David Gibson24d828f2007-12-17 14:42:07 +11001752
1753/**
1754 * fdt_del_node - delete a node (subtree)
1755 * @fdt: pointer to the device tree blob
1756 * @nodeoffset: offset of the node to nop
1757 *
1758 * fdt_del_node() will remove the given node, including all its
1759 * subnodes if any, from the blob.
1760 *
1761 * This function will delete data from the blob, and will therefore
1762 * change the offsets of some existing nodes.
1763 *
1764 * returns:
1765 * 0, on success
1766 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1767 * -FDT_ERR_BADLAYOUT,
1768 * -FDT_ERR_BADMAGIC,
1769 * -FDT_ERR_BADVERSION,
1770 * -FDT_ERR_BADSTATE,
1771 * -FDT_ERR_BADSTRUCTURE,
1772 * -FDT_ERR_TRUNCATED, standard meanings
1773 */
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001774int fdt_del_node(void *fdt, int nodeoffset);
1775
Maxime Ripard9e901ce2016-07-05 10:26:44 +02001776/**
1777 * fdt_overlay_apply - Applies a DT overlay on a base DT
1778 * @fdt: pointer to the base device tree blob
1779 * @fdto: pointer to the device tree overlay blob
1780 *
1781 * fdt_overlay_apply() will apply the given device tree overlay on the
1782 * given base device tree.
1783 *
1784 * Expect the base device tree to be modified, even if the function
1785 * returns an error.
1786 *
1787 * returns:
1788 * 0, on success
1789 * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
1790 * -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or
1791 * properties in the base DT
1792 * -FDT_ERR_BADPHANDLE, the phandles in the overlay do not have the right
1793 * magic
1794 * -FDT_ERR_INTERNAL,
1795 * -FDT_ERR_BADLAYOUT,
1796 * -FDT_ERR_BADMAGIC,
1797 * -FDT_ERR_BADOFFSET,
1798 * -FDT_ERR_BADPATH,
1799 * -FDT_ERR_BADVERSION,
1800 * -FDT_ERR_BADSTRUCTURE,
1801 * -FDT_ERR_BADSTATE,
1802 * -FDT_ERR_TRUNCATED, standard meanings
1803 */
1804int fdt_overlay_apply(void *fdt, void *fdto);
1805
Kumar Galac8ab7052007-10-24 11:04:22 -05001806/**********************************************************************/
1807/* Debugging / informational functions */
1808/**********************************************************************/
1809
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001810const char *fdt_strerror(int errval);
1811
Simon Glasse2976fe2015-06-23 15:38:26 -06001812/**
1813 * fdt_remove_unused_strings() - Remove any unused strings from an FDT
1814 *
1815 * This creates a new device tree in @new with unused strings removed. The
1816 * called can then use fdt_pack() to minimise the space consumed.
1817 *
1818 * @old: Old device tree blog
1819 * @new: Place to put new device tree blob, which must be as large as
1820 * @old
1821 * @return
1822 * 0, on success
1823 * -FDT_ERR_BADOFFSET, corrupt device tree
1824 * -FDT_ERR_NOSPACE, out of space, which should not happen unless there
1825 * is something very wrong with the device tree input
1826 */
1827int fdt_remove_unused_strings(const void *old, void *new);
1828
Simon Glassb0167022013-06-13 15:10:08 -07001829struct fdt_region {
1830 int offset;
1831 int size;
1832};
1833
Simon Glass35b21522015-06-23 15:38:27 -06001834/*
1835 * Flags for fdt_find_regions()
1836 *
1837 * Add a region for the string table (always the last region)
1838 */
1839#define FDT_REG_ADD_STRING_TAB (1 << 0)
1840
1841/*
1842 * Add all supernodes of a matching node/property, useful for creating a
1843 * valid subset tree
1844 */
1845#define FDT_REG_SUPERNODES (1 << 1)
1846
1847/* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
1848#define FDT_REG_DIRECT_SUBNODES (1 << 2)
1849
1850/* Add all subnodes of a matching node */
1851#define FDT_REG_ALL_SUBNODES (1 << 3)
1852
1853/* Add a region for the mem_rsvmap table (always the first region) */
1854#define FDT_REG_ADD_MEM_RSVMAP (1 << 4)
1855
1856/* Indicates what an fdt part is (node, property, value) */
1857#define FDT_IS_NODE (1 << 0)
1858#define FDT_IS_PROP (1 << 1)
1859#define FDT_IS_VALUE (1 << 2) /* not supported */
1860#define FDT_IS_COMPAT (1 << 3) /* used internally */
1861#define FDT_NODE_HAS_PROP (1 << 4) /* node contains prop */
1862
1863#define FDT_ANY_GLOBAL (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
1864 FDT_IS_COMPAT)
1865#define FDT_IS_ANY 0x1f /* all the above */
1866
1867/* We set a reasonable limit on the number of nested nodes */
1868#define FDT_MAX_DEPTH 32
1869
1870/* Decribes what we want to include from the current tag */
1871enum want_t {
1872 WANT_NOTHING,
1873 WANT_NODES_ONLY, /* No properties */
1874 WANT_NODES_AND_PROPS, /* Everything for one level */
1875 WANT_ALL_NODES_AND_PROPS /* Everything for all levels */
1876};
1877
1878/* Keeps track of the state at parent nodes */
1879struct fdt_subnode_stack {
1880 int offset; /* Offset of node */
1881 enum want_t want; /* The 'want' value here */
1882 int included; /* 1 if we included this node, 0 if not */
1883};
1884
1885struct fdt_region_ptrs {
1886 int depth; /* Current tree depth */
1887 int done; /* What we have completed scanning */
1888 enum want_t want; /* What we are currently including */
1889 char *end; /* Pointer to end of full node path */
1890 int nextoffset; /* Next node offset to check */
1891};
1892
1893/* The state of our finding algortihm */
1894struct fdt_region_state {
1895 struct fdt_subnode_stack stack[FDT_MAX_DEPTH]; /* node stack */
1896 struct fdt_region *region; /* Contains list of regions found */
1897 int count; /* Numnber of regions found */
1898 const void *fdt; /* FDT blob */
1899 int max_regions; /* Maximum regions to find */
1900 int can_merge; /* 1 if we can merge with previous region */
1901 int start; /* Start position of current region */
1902 struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */
1903};
1904
Simon Glassb0167022013-06-13 15:10:08 -07001905/**
1906 * fdt_find_regions() - find regions in device tree
1907 *
1908 * Given a list of nodes to include and properties to exclude, find
1909 * the regions of the device tree which describe those included parts.
1910 *
1911 * The intent is to get a list of regions which will be invariant provided
1912 * those parts are invariant. For example, if you request a list of regions
1913 * for all nodes but exclude the property "data", then you will get the
1914 * same region contents regardless of any change to "data" properties.
1915 *
1916 * This function can be used to produce a byte-stream to send to a hashing
1917 * function to verify that critical parts of the FDT have not changed.
1918 *
1919 * Nodes which are given in 'inc' are included in the region list, as
1920 * are the names of the immediate subnodes nodes (but not the properties
1921 * or subnodes of those subnodes).
1922 *
1923 * For eaxample "/" means to include the root node, all root properties
1924 * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1925 * ensures that we capture the names of the subnodes. In a hashing situation
1926 * it prevents the root node from changing at all Any change to non-excluded
1927 * properties, names of subnodes or number of subnodes would be detected.
1928 *
1929 * When used with FITs this provides the ability to hash and sign parts of
1930 * the FIT based on different configurations in the FIT. Then it is
1931 * impossible to change anything about that configuration (include images
1932 * attached to the configuration), but it may be possible to add new
1933 * configurations, new images or new signatures within the existing
1934 * framework.
1935 *
1936 * Adding new properties to a device tree may result in the string table
1937 * being extended (if the new property names are different from those
1938 * already added). This function can optionally include a region for
1939 * the string table so that this can be part of the hash too.
1940 *
1941 * The device tree header is not included in the list.
1942 *
1943 * @fdt: Device tree to check
1944 * @inc: List of node paths to included
1945 * @inc_count: Number of node paths in list
1946 * @exc_prop: List of properties names to exclude
1947 * @exc_prop_count: Number of properties in exclude list
1948 * @region: Returns list of regions
1949 * @max_region: Maximum length of region list
1950 * @path: Pointer to a temporary string for the function to use for
1951 * building path names
1952 * @path_len: Length of path, must be large enough to hold the longest
1953 * path in the tree
1954 * @add_string_tab: 1 to add a region for the string table
1955 * @return number of regions in list. If this is >max_regions then the
1956 * region array was exhausted. You should increase max_regions and try
1957 * the call again.
1958 */
1959int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
1960 char * const exc_prop[], int exc_prop_count,
1961 struct fdt_region region[], int max_regions,
1962 char *path, int path_len, int add_string_tab);
1963
Simon Glass35b21522015-06-23 15:38:27 -06001964/**
1965 * fdt_first_region() - find regions in device tree
1966 *
1967 * Given a nodes and properties to include and properties to exclude, find
1968 * the regions of the device tree which describe those included parts.
1969 *
1970 * The use for this function is twofold. Firstly it provides a convenient
1971 * way of performing a structure-aware grep of the tree. For example it is
1972 * possible to grep for a node and get all the properties associated with
1973 * that node. Trees can be subsetted easily, by specifying the nodes that
1974 * are required, and then writing out the regions returned by this function.
1975 * This is useful for small resource-constrained systems, such as boot
1976 * loaders, which want to use an FDT but do not need to know about all of
1977 * it.
1978 *
1979 * Secondly it makes it easy to hash parts of the tree and detect changes.
1980 * The intent is to get a list of regions which will be invariant provided
1981 * those parts are invariant. For example, if you request a list of regions
1982 * for all nodes but exclude the property "data", then you will get the
1983 * same region contents regardless of any change to "data" properties.
1984 *
1985 * This function can be used to produce a byte-stream to send to a hashing
1986 * function to verify that critical parts of the FDT have not changed.
1987 * Note that semantically null changes in order could still cause false
1988 * hash misses. Such reordering might happen if the tree is regenerated
1989 * from source, and nodes are reordered (the bytes-stream will be emitted
1990 * in a different order and mnay hash functions will detect this). However
1991 * if an existing tree is modified using libfdt functions, such as
1992 * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
1993 *
1994 * The nodes/properties to include/exclude are defined by a function
1995 * provided by the caller. This function is called for each node and
1996 * property, and must return:
1997 *
1998 * 0 - to exclude this part
1999 * 1 - to include this part
2000 * -1 - for FDT_IS_PROP only: no information is available, so include
2001 * if its containing node is included
2002 *
2003 * The last case is only used to deal with properties. Often a property is
2004 * included if its containing node is included - this is the case where
2005 * -1 is returned.. However if the property is specifically required to be
2006 * included/excluded, then 0 or 1 can be returned. Note that including a
2007 * property when the FDT_REG_SUPERNODES flag is given will force its
2008 * containing node to be included since it is not valid to have a property
2009 * that is not in a node.
2010 *
2011 * Using the information provided, the inclusion of a node can be controlled
2012 * either by a node name or its compatible string, or any other property
2013 * that the function can determine.
2014 *
2015 * As an example, including node "/" means to include the root node and all
2016 * root properties. A flag provides a way of also including supernodes (of
2017 * which there is none for the root node), and another flag includes
2018 * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
2019 * FDT_END_NODE of all subnodes of /.
2020 *
2021 * The subnode feature helps in a hashing situation since it prevents the
2022 * root node from changing at all. Any change to non-excluded properties,
2023 * names of subnodes or number of subnodes would be detected.
2024 *
2025 * When used with FITs this provides the ability to hash and sign parts of
2026 * the FIT based on different configurations in the FIT. Then it is
2027 * impossible to change anything about that configuration (include images
2028 * attached to the configuration), but it may be possible to add new
2029 * configurations, new images or new signatures within the existing
2030 * framework.
2031 *
2032 * Adding new properties to a device tree may result in the string table
2033 * being extended (if the new property names are different from those
2034 * already added). This function can optionally include a region for
2035 * the string table so that this can be part of the hash too. This is always
2036 * the last region.
2037 *
2038 * The FDT also has a mem_rsvmap table which can also be included, and is
2039 * always the first region if so.
2040 *
2041 * The device tree header is not included in the region list. Since the
2042 * contents of the FDT are changing (shrinking, often), the caller will need
2043 * to regenerate the header anyway.
2044 *
2045 * @fdt: Device tree to check
2046 * @h_include: Function to call to determine whether to include a part or
2047 * not:
2048 *
2049 * @priv: Private pointer as passed to fdt_find_regions()
2050 * @fdt: Pointer to FDT blob
2051 * @offset: Offset of this node / property
2052 * @type: Type of this part, FDT_IS_...
2053 * @data: Pointer to data (node name, property name, compatible
2054 * string, value (not yet supported)
2055 * @size: Size of data, or 0 if none
2056 * @return 0 to exclude, 1 to include, -1 if no information is
2057 * available
2058 * @priv: Private pointer passed to h_include
2059 * @region: Returns list of regions, sorted by offset
2060 * @max_regions: Maximum length of region list
2061 * @path: Pointer to a temporary string for the function to use for
2062 * building path names
2063 * @path_len: Length of path, must be large enough to hold the longest
2064 * path in the tree
2065 * @flags: Various flags that control the region algortihm, see
2066 * FDT_REG_...
2067 * @return number of regions in list. If this is >max_regions then the
2068 * region array was exhausted. You should increase max_regions and try
2069 * the call again. Only the first max_regions elements are available in the
2070 * array.
2071 *
2072 * On error a -ve value is return, which can be:
2073 *
2074 * -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
2075 * -FDT_ERR_BADLAYOUT
2076 * -FDT_ERR_NOSPACE (path area is too small)
2077 */
2078int fdt_first_region(const void *fdt,
2079 int (*h_include)(void *priv, const void *fdt, int offset,
2080 int type, const char *data, int size),
2081 void *priv, struct fdt_region *region,
2082 char *path, int path_len, int flags,
2083 struct fdt_region_state *info);
2084
2085/** fdt_next_region() - find next region
2086 *
2087 * See fdt_first_region() for full description. This function finds the
2088 * next region according to the provided parameters, which must be the same
2089 * as passed to fdt_first_region().
2090 *
2091 * This function can additionally return -FDT_ERR_NOTFOUND when there are no
2092 * more regions
2093 */
2094int fdt_next_region(const void *fdt,
2095 int (*h_include)(void *priv, const void *fdt, int offset,
2096 int type, const char *data, int size),
2097 void *priv, struct fdt_region *region,
2098 char *path, int path_len, int flags,
2099 struct fdt_region_state *info);
2100
2101/**
2102 * fdt_add_alias_regions() - find aliases that point to existing regions
2103 *
2104 * Once a device tree grep is complete some of the nodes will be present
2105 * and some will have been dropped. This function checks all the alias nodes
2106 * to figure out which points point to nodes which are still present. These
2107 * aliases need to be kept, along with the nodes they reference.
2108 *
2109 * Given a list of regions function finds the aliases that still apply and
2110 * adds more regions to the list for these. This function is called after
2111 * fdt_next_region() has finished returning regions and requires the same
2112 * state.
2113 *
2114 * @fdt: Device tree file to reference
2115 * @region: List of regions that will be kept
2116 * @count: Number of regions
2117 * @max_regions: Number of entries that can fit in @region
2118 * @info: Region state as returned from fdt_next_region()
2119 * @return new number of regions in @region (i.e. count + the number added)
2120 * or -FDT_ERR_NOSPACE if there was not enough space.
2121 */
2122int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
2123 int max_regions, struct fdt_region_state *info);
2124
Gerald Van Barenc0577a62007-03-31 11:59:59 -04002125#endif /* _LIBFDT_H */