blob: 9eefaaf5f87a7de869f78a05c67bed09d56202bd [file] [log] [blame]
Kumar Gala8d04f022007-10-24 11:04:22 -05001#ifndef _LIBFDT_H
2#define _LIBFDT_H
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04003/*
4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 *
Kumar Gala8d04f022007-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.
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04009 *
Kumar Gala8d04f022007-10-24 11:04:22 -050010 * 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.
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040014 *
Kumar Gala8d04f022007-10-24 11:04:22 -050015 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this library; if not, write to the Free
22 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 * MA 02110-1301 USA
24 *
25 * Alternatively,
26 *
27 * b) Redistribution and use in source and binary forms, with or
28 * without modification, are permitted provided that the following
29 * conditions are met:
30 *
31 * 1. Redistributions of source code must retain the above
32 * copyright notice, this list of conditions and the following
33 * disclaimer.
34 * 2. Redistributions in binary form must reproduce the above
35 * copyright notice, this list of conditions and the following
36 * disclaimer in the documentation and/or other materials
37 * provided with the distribution.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
40 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
44 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
50 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
51 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040052 */
53
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040054#include <libfdt_env.h>
Kumar Gala8d04f022007-10-24 11:04:22 -050055#include <fdt.h>
Gerald Van Baren7cd5da02007-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 Gala8d04f022007-10-24 11:04:22 -050062 /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040063#define FDT_ERR_EXISTS 2
Kumar Gala8d04f022007-10-24 11:04:22 -050064 /* FDT_ERR_EXISTS: Attemped to create a node or property which
65 * already exists */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040066#define FDT_ERR_NOSPACE 3
Kumar Gala8d04f022007-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 Baren7cd5da02007-03-31 11:59:59 -040071
72/* Error codes: codes for bad parameters */
73#define FDT_ERR_BADOFFSET 4
Kumar Gala8d04f022007-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 Baren7cd5da02007-03-31 11:59:59 -040077#define FDT_ERR_BADPATH 5
Kumar Gala8d04f022007-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 Baren7cd5da02007-03-31 11:59:59 -040088
89/* Error codes: codes for bad device tree blobs */
Kumar Gala8d04f022007-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. */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400112
Kumar Gala8d04f022007-10-24 11:04:22 -0500113/* "Can't happen" error indicating a bug in libfdt */
114#define FDT_ERR_INTERNAL 13
115 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
116 * Should never be returned, if it is, it indicates a bug in
117 * libfdt itself. */
118
119#define FDT_ERR_MAX 13
120
121/**********************************************************************/
122/* Low-level functions (you probably don't need these) */
123/**********************************************************************/
124
Jon Loeliger741a6d02008-09-25 11:02:17 -0500125const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
Kumar Gala8d04f022007-10-24 11:04:22 -0500126static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
127{
David Gibsonc6683022008-07-07 10:14:15 +1000128 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
Kumar Gala8d04f022007-10-24 11:04:22 -0500129}
130
131uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
132
133/**********************************************************************/
David Gibsonae0b5902008-02-12 11:58:31 +1100134/* Traversal functions */
135/**********************************************************************/
136
137int fdt_next_node(const void *fdt, int offset, int *depth);
138
Simon Glass88f95bb2013-05-07 06:11:50 +0000139/**
140 * fdt_first_subnode() - get offset of first direct subnode
141 *
142 * @fdt: FDT blob
143 * @offset: Offset of node to check
144 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
145 */
146int fdt_first_subnode(const void *fdt, int offset);
147
148/**
149 * fdt_next_subnode() - get offset of next direct subnode
150 *
151 * After first calling fdt_first_subnode(), call this function repeatedly to
152 * get direct subnodes of a parent node.
153 *
154 * @fdt: FDT blob
155 * @offset: Offset of previous subnode
156 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
157 * subnodes
158 */
159int fdt_next_subnode(const void *fdt, int offset);
160
David Gibsonae0b5902008-02-12 11:58:31 +1100161/**********************************************************************/
Kumar Gala8d04f022007-10-24 11:04:22 -0500162/* General functions */
163/**********************************************************************/
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400164
165#define fdt_get_header(fdt, field) \
Kumar Gala8d04f022007-10-24 11:04:22 -0500166 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200167#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400168#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
169#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
170#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
171#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
172#define fdt_version(fdt) (fdt_get_header(fdt, version))
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200173#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
174#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
175#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400176#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
177
Kumar Gala8d04f022007-10-24 11:04:22 -0500178#define __fdt_set_hdr(name) \
179 static inline void fdt_set_##name(void *fdt, uint32_t val) \
180 { \
Laurent Gregoire67b89c72009-03-03 14:23:59 +0100181 struct fdt_header *fdth = (struct fdt_header*)fdt; \
Kumar Gala8d04f022007-10-24 11:04:22 -0500182 fdth->name = cpu_to_fdt32(val); \
183 }
184__fdt_set_hdr(magic);
185__fdt_set_hdr(totalsize);
186__fdt_set_hdr(off_dt_struct);
187__fdt_set_hdr(off_dt_strings);
188__fdt_set_hdr(off_mem_rsvmap);
189__fdt_set_hdr(version);
190__fdt_set_hdr(last_comp_version);
191__fdt_set_hdr(boot_cpuid_phys);
192__fdt_set_hdr(size_dt_strings);
193__fdt_set_hdr(size_dt_struct);
194#undef __fdt_set_hdr
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400195
Kumar Gala8d04f022007-10-24 11:04:22 -0500196/**
197 * fdt_check_header - sanity check a device tree or possible device tree
198 * @fdt: pointer to data which might be a flattened device tree
199 *
200 * fdt_check_header() checks that the given buffer contains what
201 * appears to be a flattened device tree with sane information in its
202 * header.
203 *
204 * returns:
205 * 0, if the buffer appears to contain a valid device tree
206 * -FDT_ERR_BADMAGIC,
207 * -FDT_ERR_BADVERSION,
208 * -FDT_ERR_BADSTATE, standard meanings, as above
209 */
Gerald Van Baren6679f922007-04-06 14:17:14 -0400210int fdt_check_header(const void *fdt);
211
Kumar Gala8d04f022007-10-24 11:04:22 -0500212/**
213 * fdt_move - move a device tree around in memory
214 * @fdt: pointer to the device tree to move
215 * @buf: pointer to memory where the device is to be moved
216 * @bufsize: size of the memory space at buf
217 *
218 * fdt_move() relocates, if possible, the device tree blob located at
219 * fdt to the buffer at buf of size bufsize. The buffer may overlap
220 * with the existing device tree blob at fdt. Therefore,
221 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
222 * should always succeed.
223 *
224 * returns:
225 * 0, on success
226 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
227 * -FDT_ERR_BADMAGIC,
228 * -FDT_ERR_BADVERSION,
229 * -FDT_ERR_BADSTATE, standard meanings
230 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400231int fdt_move(const void *fdt, void *buf, int bufsize);
232
Kumar Gala8d04f022007-10-24 11:04:22 -0500233/**********************************************************************/
234/* Read-only functions */
235/**********************************************************************/
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400236
Kumar Gala8d04f022007-10-24 11:04:22 -0500237/**
Wolfram Sang46390da2008-07-09 11:22:44 +0200238 * fdt_string - retrieve a string from the strings block of a device tree
Kumar Gala8d04f022007-10-24 11:04:22 -0500239 * @fdt: pointer to the device tree blob
240 * @stroffset: offset of the string within the strings block (native endian)
241 *
242 * fdt_string() retrieves a pointer to a single string from the
243 * strings block of the device tree blob at fdt.
244 *
245 * returns:
246 * a pointer to the string, on success
247 * NULL, if stroffset is out of bounds
248 */
249const char *fdt_string(const void *fdt, int stroffset);
250
251/**
Wolfram Sang46390da2008-07-09 11:22:44 +0200252 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
Kumar Gala8d04f022007-10-24 11:04:22 -0500253 * @fdt: pointer to the device tree blob
254 *
255 * Returns the number of entries in the device tree blob's memory
256 * reservation map. This does not include the terminating 0,0 entry
257 * or any other (0,0) entries reserved for expansion.
258 *
259 * returns:
260 * the number of entries
261 */
262int fdt_num_mem_rsv(const void *fdt);
263
264/**
Wolfram Sang46390da2008-07-09 11:22:44 +0200265 * fdt_get_mem_rsv - retrieve one memory reserve map entry
Kumar Gala8d04f022007-10-24 11:04:22 -0500266 * @fdt: pointer to the device tree blob
267 * @address, @size: pointers to 64-bit variables
268 *
269 * On success, *address and *size will contain the address and size of
270 * the n-th reserve map entry from the device tree blob, in
271 * native-endian format.
272 *
273 * returns:
274 * 0, on success
275 * -FDT_ERR_BADMAGIC,
276 * -FDT_ERR_BADVERSION,
277 * -FDT_ERR_BADSTATE, standard meanings
278 */
279int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
280
281/**
282 * fdt_subnode_offset_namelen - find a subnode based on substring
283 * @fdt: pointer to the device tree blob
284 * @parentoffset: structure block offset of a node
285 * @name: name of the subnode to locate
286 * @namelen: number of characters of name to consider
287 *
288 * Identical to fdt_subnode_offset(), but only examine the first
289 * namelen characters of name for matching the subnode name. This is
290 * useful for finding subnodes based on a portion of a larger string,
291 * such as a full path.
292 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400293int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
294 const char *name, int namelen);
Kumar Gala8d04f022007-10-24 11:04:22 -0500295/**
296 * fdt_subnode_offset - find a subnode of a given node
297 * @fdt: pointer to the device tree blob
298 * @parentoffset: structure block offset of a node
299 * @name: name of the subnode to locate
300 *
301 * fdt_subnode_offset() finds a subnode of the node at structure block
302 * offset parentoffset with the given name. name may include a unit
303 * address, in which case fdt_subnode_offset() will find the subnode
304 * with that unit address, or the unit address may be omitted, in
305 * which case fdt_subnode_offset() will find an arbitrary subnode
306 * whose name excluding unit address matches the given name.
307 *
308 * returns:
309 * structure block offset of the requested subnode (>=0), on success
310 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
311 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
312 * -FDT_ERR_BADMAGIC,
313 * -FDT_ERR_BADVERSION,
314 * -FDT_ERR_BADSTATE,
315 * -FDT_ERR_BADSTRUCTURE,
316 * -FDT_ERR_TRUNCATED, standard meanings.
317 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400318int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
319
Kumar Gala8d04f022007-10-24 11:04:22 -0500320/**
321 * fdt_path_offset - find a tree node by its full path
322 * @fdt: pointer to the device tree blob
323 * @path: full path of the node to locate
324 *
325 * fdt_path_offset() finds a node of a given path in the device tree.
326 * Each path component may omit the unit address portion, but the
327 * results of this are undefined if any such path component is
328 * ambiguous (that is if there are multiple nodes at the relevant
329 * level matching the given component, differentiated only by unit
330 * address).
331 *
332 * returns:
333 * structure block offset of the node with the requested path (>=0), on success
334 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
335 * -FDT_ERR_NOTFOUND, if the requested node does not exist
336 * -FDT_ERR_BADMAGIC,
337 * -FDT_ERR_BADVERSION,
338 * -FDT_ERR_BADSTATE,
339 * -FDT_ERR_BADSTRUCTURE,
340 * -FDT_ERR_TRUNCATED, standard meanings.
341 */
342int fdt_path_offset(const void *fdt, const char *path);
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400343
Kumar Gala8d04f022007-10-24 11:04:22 -0500344/**
Wolfram Sang46390da2008-07-09 11:22:44 +0200345 * fdt_get_name - retrieve the name of a given node
Kumar Gala8d04f022007-10-24 11:04:22 -0500346 * @fdt: pointer to the device tree blob
347 * @nodeoffset: structure block offset of the starting node
348 * @lenp: pointer to an integer variable (will be overwritten) or NULL
349 *
350 * fdt_get_name() retrieves the name (including unit address) of the
351 * device tree node at structure block offset nodeoffset. If lenp is
352 * non-NULL, the length of this name is also returned, in the integer
353 * pointed to by lenp.
354 *
355 * returns:
356 * pointer to the node's name, on success
357 * If lenp is non-NULL, *lenp contains the length of that name (>=0)
358 * NULL, on error
359 * if lenp is non-NULL *lenp contains an error code (<0):
360 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
361 * -FDT_ERR_BADMAGIC,
362 * -FDT_ERR_BADVERSION,
363 * -FDT_ERR_BADSTATE, standard meanings
364 */
365const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400366
Kumar Gala8d04f022007-10-24 11:04:22 -0500367/**
David Gibsond1c63142010-03-09 17:39:14 +1100368 * fdt_first_property_offset - find the offset of a node's first property
369 * @fdt: pointer to the device tree blob
370 * @nodeoffset: structure block offset of a node
371 *
372 * fdt_first_property_offset() finds the first property of the node at
373 * the given structure block offset.
374 *
375 * returns:
376 * structure block offset of the property (>=0), on success
377 * -FDT_ERR_NOTFOUND, if the requested node has no properties
378 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
379 * -FDT_ERR_BADMAGIC,
380 * -FDT_ERR_BADVERSION,
381 * -FDT_ERR_BADSTATE,
382 * -FDT_ERR_BADSTRUCTURE,
383 * -FDT_ERR_TRUNCATED, standard meanings.
384 */
385int fdt_first_property_offset(const void *fdt, int nodeoffset);
386
387/**
388 * fdt_next_property_offset - step through a node's properties
389 * @fdt: pointer to the device tree blob
390 * @offset: structure block offset of a property
391 *
392 * fdt_next_property_offset() finds the property immediately after the
393 * one at the given structure block offset. This will be a property
394 * of the same node as the given property.
395 *
396 * returns:
397 * structure block offset of the next property (>=0), on success
398 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
399 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
400 * -FDT_ERR_BADMAGIC,
401 * -FDT_ERR_BADVERSION,
402 * -FDT_ERR_BADSTATE,
403 * -FDT_ERR_BADSTRUCTURE,
404 * -FDT_ERR_TRUNCATED, standard meanings.
405 */
406int fdt_next_property_offset(const void *fdt, int offset);
407
408/**
409 * fdt_get_property_by_offset - retrieve the property at a given offset
410 * @fdt: pointer to the device tree blob
411 * @offset: offset of the property to retrieve
412 * @lenp: pointer to an integer variable (will be overwritten) or NULL
413 *
414 * fdt_get_property_by_offset() retrieves a pointer to the
415 * fdt_property structure within the device tree blob at the given
416 * offset. If lenp is non-NULL, the length of the property value is
417 * also returned, in the integer pointed to by lenp.
418 *
419 * returns:
420 * pointer to the structure representing the property
421 * if lenp is non-NULL, *lenp contains the length of the property
422 * value (>=0)
423 * NULL, on error
424 * if lenp is non-NULL, *lenp contains an error code (<0):
425 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
426 * -FDT_ERR_BADMAGIC,
427 * -FDT_ERR_BADVERSION,
428 * -FDT_ERR_BADSTATE,
429 * -FDT_ERR_BADSTRUCTURE,
430 * -FDT_ERR_TRUNCATED, standard meanings
431 */
432const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
433 int offset,
434 int *lenp);
435
436/**
David Gibson02193992008-08-06 14:50:49 +1000437 * fdt_get_property_namelen - find a property based on substring
438 * @fdt: pointer to the device tree blob
439 * @nodeoffset: offset of the node whose property to find
440 * @name: name of the property to find
441 * @namelen: number of characters of name to consider
442 * @lenp: pointer to an integer variable (will be overwritten) or NULL
443 *
444 * Identical to fdt_get_property_namelen(), but only examine the first
445 * namelen characters of name for matching the property name.
446 */
447const struct fdt_property *fdt_get_property_namelen(const void *fdt,
448 int nodeoffset,
449 const char *name,
450 int namelen, int *lenp);
451
452/**
Kumar Gala8d04f022007-10-24 11:04:22 -0500453 * fdt_get_property - find a given property in a given node
454 * @fdt: pointer to the device tree blob
455 * @nodeoffset: offset of the node whose property to find
456 * @name: name of the property to find
457 * @lenp: pointer to an integer variable (will be overwritten) or NULL
458 *
459 * fdt_get_property() retrieves a pointer to the fdt_property
460 * structure within the device tree blob corresponding to the property
461 * named 'name' of the node at offset nodeoffset. If lenp is
Wolfram Sang46390da2008-07-09 11:22:44 +0200462 * non-NULL, the length of the property value is also returned, in the
Kumar Gala8d04f022007-10-24 11:04:22 -0500463 * integer pointed to by lenp.
464 *
465 * returns:
466 * pointer to the structure representing the property
467 * if lenp is non-NULL, *lenp contains the length of the property
468 * value (>=0)
469 * NULL, on error
470 * if lenp is non-NULL, *lenp contains an error code (<0):
471 * -FDT_ERR_NOTFOUND, node does not have named property
472 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
473 * -FDT_ERR_BADMAGIC,
474 * -FDT_ERR_BADVERSION,
475 * -FDT_ERR_BADSTATE,
476 * -FDT_ERR_BADSTRUCTURE,
477 * -FDT_ERR_TRUNCATED, standard meanings
478 */
479const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
480 const char *name, int *lenp);
481static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
482 const char *name,
483 int *lenp)
484{
David Gibsonc6683022008-07-07 10:14:15 +1000485 return (struct fdt_property *)(uintptr_t)
486 fdt_get_property(fdt, nodeoffset, name, lenp);
Kumar Gala8d04f022007-10-24 11:04:22 -0500487}
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400488
Kumar Gala8d04f022007-10-24 11:04:22 -0500489/**
David Gibsond1c63142010-03-09 17:39:14 +1100490 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
491 * @fdt: pointer to the device tree blob
492 * @ffset: offset of the property to read
493 * @namep: pointer to a string variable (will be overwritten) or NULL
494 * @lenp: pointer to an integer variable (will be overwritten) or NULL
495 *
496 * fdt_getprop_by_offset() retrieves a pointer to the value of the
497 * property at structure block offset 'offset' (this will be a pointer
498 * to within the device blob itself, not a copy of the value). If
499 * lenp is non-NULL, the length of the property value is also
500 * returned, in the integer pointed to by lenp. If namep is non-NULL,
501 * the property's namne will also be returned in the char * pointed to
502 * by namep (this will be a pointer to within the device tree's string
503 * block, not a new copy of the name).
504 *
505 * returns:
506 * pointer to the property's value
507 * if lenp is non-NULL, *lenp contains the length of the property
508 * value (>=0)
509 * if namep is non-NULL *namep contiains a pointer to the property
510 * name.
511 * NULL, on error
512 * if lenp is non-NULL, *lenp contains an error code (<0):
513 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
514 * -FDT_ERR_BADMAGIC,
515 * -FDT_ERR_BADVERSION,
516 * -FDT_ERR_BADSTATE,
517 * -FDT_ERR_BADSTRUCTURE,
518 * -FDT_ERR_TRUNCATED, standard meanings
519 */
520const void *fdt_getprop_by_offset(const void *fdt, int offset,
521 const char **namep, int *lenp);
522
523/**
David Gibson02193992008-08-06 14:50:49 +1000524 * fdt_getprop_namelen - get property value based on substring
525 * @fdt: pointer to the device tree blob
526 * @nodeoffset: offset of the node whose property to find
527 * @name: name of the property to find
528 * @namelen: number of characters of name to consider
529 * @lenp: pointer to an integer variable (will be overwritten) or NULL
530 *
531 * Identical to fdt_getprop(), but only examine the first namelen
532 * characters of name for matching the property name.
533 */
534const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
535 const char *name, int namelen, int *lenp);
536
537/**
Kumar Gala8d04f022007-10-24 11:04:22 -0500538 * fdt_getprop - retrieve the value of a given property
539 * @fdt: pointer to the device tree blob
540 * @nodeoffset: offset of the node whose property to find
541 * @name: name of the property to find
542 * @lenp: pointer to an integer variable (will be overwritten) or NULL
543 *
544 * fdt_getprop() retrieves a pointer to the value of the property
545 * named 'name' of the node at offset nodeoffset (this will be a
546 * pointer to within the device blob itself, not a copy of the value).
Wolfram Sang46390da2008-07-09 11:22:44 +0200547 * If lenp is non-NULL, the length of the property value is also
Kumar Gala8d04f022007-10-24 11:04:22 -0500548 * returned, in the integer pointed to by lenp.
549 *
550 * returns:
551 * pointer to the property's value
552 * if lenp is non-NULL, *lenp contains the length of the property
553 * value (>=0)
554 * NULL, on error
555 * if lenp is non-NULL, *lenp contains an error code (<0):
556 * -FDT_ERR_NOTFOUND, node does not have named property
557 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
558 * -FDT_ERR_BADMAGIC,
559 * -FDT_ERR_BADVERSION,
560 * -FDT_ERR_BADSTATE,
561 * -FDT_ERR_BADSTRUCTURE,
562 * -FDT_ERR_TRUNCATED, standard meanings
563 */
564const void *fdt_getprop(const void *fdt, int nodeoffset,
565 const char *name, int *lenp);
566static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
567 const char *name, int *lenp)
568{
David Gibsonc6683022008-07-07 10:14:15 +1000569 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
Kumar Gala8d04f022007-10-24 11:04:22 -0500570}
571
572/**
Wolfram Sang46390da2008-07-09 11:22:44 +0200573 * fdt_get_phandle - retrieve the phandle of a given node
Kumar Gala8d04f022007-10-24 11:04:22 -0500574 * @fdt: pointer to the device tree blob
575 * @nodeoffset: structure block offset of the node
576 *
577 * fdt_get_phandle() retrieves the phandle of the device tree node at
578 * structure block offset nodeoffset.
579 *
580 * returns:
Wolfram Sang46390da2008-07-09 11:22:44 +0200581 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
Kumar Gala8d04f022007-10-24 11:04:22 -0500582 * 0, if the node has no phandle, or another error occurs
583 */
584uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
585
586/**
Gerald Van Barencd3cb0d2008-10-04 07:56:06 -0400587 * fdt_get_alias_namelen - get alias based on substring
David Gibson9a6cf732008-08-20 16:55:14 +1000588 * @fdt: pointer to the device tree blob
589 * @name: name of the alias th look up
590 * @namelen: number of characters of name to consider
591 *
592 * Identical to fdt_get_alias(), but only examine the first namelen
593 * characters of name for matching the alias name.
594 */
595const char *fdt_get_alias_namelen(const void *fdt,
596 const char *name, int namelen);
597
598/**
Robert P. J. Day1f8b5462013-08-21 11:39:19 -0400599 * fdt_get_alias - retrieve the path referenced by a given alias
David Gibson9a6cf732008-08-20 16:55:14 +1000600 * @fdt: pointer to the device tree blob
Robert P. J. Day1f8b5462013-08-21 11:39:19 -0400601 * @name: name of the alias to look up
David Gibson9a6cf732008-08-20 16:55:14 +1000602 *
603 * fdt_get_alias() retrieves the value of a given alias. That is, the
604 * value of the property named 'name' in the node /aliases.
605 *
606 * returns:
François Revolc73bd492013-02-03 00:52:21 +0100607 * a pointer to the expansion of the alias named 'name', if it exists
David Gibson9a6cf732008-08-20 16:55:14 +1000608 * NULL, if the given alias or the /aliases node does not exist
609 */
610const char *fdt_get_alias(const void *fdt, const char *name);
611
612/**
Kumar Gala8d04f022007-10-24 11:04:22 -0500613 * fdt_get_path - determine the full path of a node
614 * @fdt: pointer to the device tree blob
615 * @nodeoffset: offset of the node whose path to find
616 * @buf: character buffer to contain the returned path (will be overwritten)
617 * @buflen: size of the character buffer at buf
618 *
619 * fdt_get_path() computes the full path of the node at offset
620 * nodeoffset, and records that path in the buffer at buf.
621 *
622 * NOTE: This function is expensive, as it must scan the device tree
623 * structure from the start to nodeoffset.
624 *
625 * returns:
626 * 0, on success
627 * buf contains the absolute path of the node at
628 * nodeoffset, as a NUL-terminated string.
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200629 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Gala8d04f022007-10-24 11:04:22 -0500630 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
631 * characters and will not fit in the given buffer.
632 * -FDT_ERR_BADMAGIC,
633 * -FDT_ERR_BADVERSION,
634 * -FDT_ERR_BADSTATE,
635 * -FDT_ERR_BADSTRUCTURE, standard meanings
636 */
637int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
638
639/**
640 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
641 * @fdt: pointer to the device tree blob
642 * @nodeoffset: offset of the node whose parent to find
643 * @supernodedepth: depth of the ancestor to find
644 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
645 *
646 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
647 * at a specific depth from the root (where the root itself has depth
648 * 0, its immediate subnodes depth 1 and so forth). So
649 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
650 * will always return 0, the offset of the root node. If the node at
651 * nodeoffset has depth D, then:
652 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
653 * will return nodeoffset itself.
654 *
655 * NOTE: This function is expensive, as it must scan the device tree
656 * structure from the start to nodeoffset.
657 *
658 * returns:
659
660 * structure block offset of the node at node offset's ancestor
661 * of depth supernodedepth (>=0), on success
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200662 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Gala8d04f022007-10-24 11:04:22 -0500663* -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
664 * -FDT_ERR_BADMAGIC,
665 * -FDT_ERR_BADVERSION,
666 * -FDT_ERR_BADSTATE,
667 * -FDT_ERR_BADSTRUCTURE, standard meanings
668 */
669int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
670 int supernodedepth, int *nodedepth);
671
672/**
673 * fdt_node_depth - find the depth of a given node
674 * @fdt: pointer to the device tree blob
675 * @nodeoffset: offset of the node whose parent to find
676 *
677 * fdt_node_depth() finds the depth of a given node. The root node
678 * has depth 0, its immediate subnodes depth 1 and so forth.
679 *
680 * NOTE: This function is expensive, as it must scan the device tree
681 * structure from the start to nodeoffset.
682 *
683 * returns:
684 * depth of the node at nodeoffset (>=0), on success
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200685 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Gala8d04f022007-10-24 11:04:22 -0500686 * -FDT_ERR_BADMAGIC,
687 * -FDT_ERR_BADVERSION,
688 * -FDT_ERR_BADSTATE,
689 * -FDT_ERR_BADSTRUCTURE, standard meanings
690 */
691int fdt_node_depth(const void *fdt, int nodeoffset);
692
693/**
694 * fdt_parent_offset - find the parent of a given node
695 * @fdt: pointer to the device tree blob
696 * @nodeoffset: offset of the node whose parent to find
697 *
698 * fdt_parent_offset() locates the parent node of a given node (that
699 * is, it finds the offset of the node which contains the node at
700 * nodeoffset as a subnode).
701 *
702 * NOTE: This function is expensive, as it must scan the device tree
703 * structure from the start to nodeoffset, *twice*.
704 *
705 * returns:
Wolfram Sang46390da2008-07-09 11:22:44 +0200706 * structure block offset of the parent of the node at nodeoffset
Kumar Gala8d04f022007-10-24 11:04:22 -0500707 * (>=0), on success
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200708 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Gala8d04f022007-10-24 11:04:22 -0500709 * -FDT_ERR_BADMAGIC,
710 * -FDT_ERR_BADVERSION,
711 * -FDT_ERR_BADSTATE,
712 * -FDT_ERR_BADSTRUCTURE, standard meanings
713 */
714int fdt_parent_offset(const void *fdt, int nodeoffset);
715
716/**
717 * fdt_node_offset_by_prop_value - find nodes with a given property value
718 * @fdt: pointer to the device tree blob
719 * @startoffset: only find nodes after this offset
720 * @propname: property name to check
721 * @propval: property value to search for
722 * @proplen: length of the value in propval
723 *
724 * fdt_node_offset_by_prop_value() returns the offset of the first
725 * node after startoffset, which has a property named propname whose
726 * value is of length proplen and has value equal to propval; or if
727 * startoffset is -1, the very first such node in the tree.
728 *
729 * To iterate through all nodes matching the criterion, the following
730 * idiom can be used:
731 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
732 * propval, proplen);
733 * while (offset != -FDT_ERR_NOTFOUND) {
Wolfgang Denk1182e9f2008-01-02 15:58:44 +0100734 * ... other code here ...
Kumar Gala8d04f022007-10-24 11:04:22 -0500735 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
736 * propval, proplen);
737 * }
738 *
739 * Note the -1 in the first call to the function, if 0 is used here
740 * instead, the function will never locate the root node, even if it
741 * matches the criterion.
742 *
743 * returns:
744 * structure block offset of the located node (>= 0, >startoffset),
745 * on success
746 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
747 * tree after startoffset
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200748 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Gala8d04f022007-10-24 11:04:22 -0500749 * -FDT_ERR_BADMAGIC,
750 * -FDT_ERR_BADVERSION,
751 * -FDT_ERR_BADSTATE,
752 * -FDT_ERR_BADSTRUCTURE, standard meanings
753 */
754int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
755 const char *propname,
756 const void *propval, int proplen);
757
758/**
759 * fdt_node_offset_by_phandle - find the node with a given phandle
760 * @fdt: pointer to the device tree blob
761 * @phandle: phandle value
762 *
Wolfram Sang46390da2008-07-09 11:22:44 +0200763 * fdt_node_offset_by_phandle() returns the offset of the node
Kumar Gala8d04f022007-10-24 11:04:22 -0500764 * which has the given phandle value. If there is more than one node
765 * in the tree with the given phandle (an invalid tree), results are
766 * undefined.
767 *
768 * returns:
769 * structure block offset of the located node (>= 0), on success
770 * -FDT_ERR_NOTFOUND, no node with that phandle exists
771 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
772 * -FDT_ERR_BADMAGIC,
773 * -FDT_ERR_BADVERSION,
774 * -FDT_ERR_BADSTATE,
775 * -FDT_ERR_BADSTRUCTURE, standard meanings
776 */
777int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
778
779/**
780 * fdt_node_check_compatible: check a node's compatible property
781 * @fdt: pointer to the device tree blob
782 * @nodeoffset: offset of a tree node
783 * @compatible: string to match against
784 *
785 *
786 * fdt_node_check_compatible() returns 0 if the given node contains a
787 * 'compatible' property with the given string as one of its elements,
788 * it returns non-zero otherwise, or on error.
789 *
790 * returns:
791 * 0, if the node has a 'compatible' property listing the given string
792 * 1, if the node has a 'compatible' property, but it does not list
793 * the given string
794 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200795 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
Kumar Gala8d04f022007-10-24 11:04:22 -0500796 * -FDT_ERR_BADMAGIC,
797 * -FDT_ERR_BADVERSION,
798 * -FDT_ERR_BADSTATE,
799 * -FDT_ERR_BADSTRUCTURE, standard meanings
800 */
801int fdt_node_check_compatible(const void *fdt, int nodeoffset,
802 const char *compatible);
803
804/**
805 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
806 * @fdt: pointer to the device tree blob
807 * @startoffset: only find nodes after this offset
808 * @compatible: 'compatible' string to match against
809 *
810 * fdt_node_offset_by_compatible() returns the offset of the first
811 * node after startoffset, which has a 'compatible' property which
812 * lists the given compatible string; or if startoffset is -1, the
813 * very first such node in the tree.
814 *
815 * To iterate through all nodes matching the criterion, the following
816 * idiom can be used:
817 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
818 * while (offset != -FDT_ERR_NOTFOUND) {
Wolfgang Denk1182e9f2008-01-02 15:58:44 +0100819 * ... other code here ...
Kumar Gala8d04f022007-10-24 11:04:22 -0500820 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
821 * }
822 *
823 * Note the -1 in the first call to the function, if 0 is used here
824 * instead, the function will never locate the root node, even if it
825 * matches the criterion.
826 *
827 * returns:
828 * structure block offset of the located node (>= 0, >startoffset),
829 * on success
830 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
831 * tree after startoffset
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200832 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
Kumar Gala8d04f022007-10-24 11:04:22 -0500833 * -FDT_ERR_BADMAGIC,
834 * -FDT_ERR_BADVERSION,
835 * -FDT_ERR_BADSTATE,
836 * -FDT_ERR_BADSTRUCTURE, standard meanings
837 */
838int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
839 const char *compatible);
840
Simon Glasse853b322013-01-21 12:59:18 -0800841/**
842 * fdt_stringlist_contains - check a string list property for a string
843 * @strlist: Property containing a list of strings to check
844 * @listlen: Length of property
845 * @str: String to search for
846 *
847 * This is a utility function provided for convenience. The list contains
848 * one or more strings, each terminated by \0, as is found in a device tree
849 * "compatible" property.
850 *
851 * @return: 1 if the string is found in the list, 0 not found, or invalid list
852 */
853int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
854
Kumar Gala8d04f022007-10-24 11:04:22 -0500855/**********************************************************************/
856/* Write-in-place functions */
857/**********************************************************************/
858
David Gibson9d4450b2007-12-17 14:41:52 +1100859/**
860 * fdt_setprop_inplace - change a property's value, but not its size
861 * @fdt: pointer to the device tree blob
862 * @nodeoffset: offset of the node whose property to change
863 * @name: name of the property to change
864 * @val: pointer to data to replace the property value with
865 * @len: length of the property value
866 *
867 * fdt_setprop_inplace() replaces the value of a given property with
868 * the data in val, of length len. This function cannot change the
869 * size of a property, and so will only work if len is equal to the
870 * current length of the property.
871 *
872 * This function will alter only the bytes in the blob which contain
873 * the given property value, and will not alter or move any other part
874 * of the tree.
875 *
876 * returns:
877 * 0, on success
878 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
879 * -FDT_ERR_NOTFOUND, node does not have the named property
880 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
881 * -FDT_ERR_BADMAGIC,
882 * -FDT_ERR_BADVERSION,
883 * -FDT_ERR_BADSTATE,
884 * -FDT_ERR_BADSTRUCTURE,
885 * -FDT_ERR_TRUNCATED, standard meanings
886 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400887int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
888 const void *val, int len);
David Gibson9d4450b2007-12-17 14:41:52 +1100889
890/**
David Gibson24fa0e52012-08-27 19:39:00 -0500891 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
David Gibson9d4450b2007-12-17 14:41:52 +1100892 * @fdt: pointer to the device tree blob
893 * @nodeoffset: offset of the node whose property to change
894 * @name: name of the property to change
David Gibson24fa0e52012-08-27 19:39:00 -0500895 * @val: 32-bit integer value to replace the property with
David Gibson9d4450b2007-12-17 14:41:52 +1100896 *
David Gibson24fa0e52012-08-27 19:39:00 -0500897 * fdt_setprop_inplace_u32() replaces the value of a given property
898 * with the 32-bit integer value in val, converting val to big-endian
899 * if necessary. This function cannot change the size of a property,
900 * and so will only work if the property already exists and has length
901 * 4.
David Gibson9d4450b2007-12-17 14:41:52 +1100902 *
903 * This function will alter only the bytes in the blob which contain
904 * the given property value, and will not alter or move any other part
905 * of the tree.
906 *
907 * returns:
908 * 0, on success
909 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
David Gibson24fa0e52012-08-27 19:39:00 -0500910 * -FDT_ERR_NOTFOUND, node does not have the named property
David Gibson9d4450b2007-12-17 14:41:52 +1100911 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
912 * -FDT_ERR_BADMAGIC,
913 * -FDT_ERR_BADVERSION,
914 * -FDT_ERR_BADSTATE,
915 * -FDT_ERR_BADSTRUCTURE,
916 * -FDT_ERR_TRUNCATED, standard meanings
917 */
David Gibson24fa0e52012-08-27 19:39:00 -0500918static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
919 const char *name, uint32_t val)
Kumar Gala8d04f022007-10-24 11:04:22 -0500920{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +0000921 fdt32_t tmp = cpu_to_fdt32(val);
922 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
Kumar Gala8d04f022007-10-24 11:04:22 -0500923}
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400924
David Gibson9d4450b2007-12-17 14:41:52 +1100925/**
David Gibson24fa0e52012-08-27 19:39:00 -0500926 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
927 * @fdt: pointer to the device tree blob
928 * @nodeoffset: offset of the node whose property to change
929 * @name: name of the property to change
930 * @val: 64-bit integer value to replace the property with
931 *
932 * fdt_setprop_inplace_u64() replaces the value of a given property
933 * with the 64-bit integer value in val, converting val to big-endian
934 * if necessary. This function cannot change the size of a property,
935 * and so will only work if the property already exists and has length
936 * 8.
937 *
938 * This function will alter only the bytes in the blob which contain
939 * the given property value, and will not alter or move any other part
940 * of the tree.
941 *
942 * returns:
943 * 0, on success
944 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
945 * -FDT_ERR_NOTFOUND, node does not have the named property
946 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
947 * -FDT_ERR_BADMAGIC,
948 * -FDT_ERR_BADVERSION,
949 * -FDT_ERR_BADSTATE,
950 * -FDT_ERR_BADSTRUCTURE,
951 * -FDT_ERR_TRUNCATED, standard meanings
952 */
953static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
954 const char *name, uint64_t val)
955{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +0000956 fdt64_t tmp = cpu_to_fdt64(val);
957 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
David Gibson24fa0e52012-08-27 19:39:00 -0500958}
959
960/**
961 * fdt_setprop_inplace_cell - change the value of a single-cell property
962 *
963 * This is an alternative name for fdt_setprop_inplace_u32()
964 */
965static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
966 const char *name, uint32_t val)
967{
968 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
969}
970
971/**
David Gibson9d4450b2007-12-17 14:41:52 +1100972 * fdt_nop_property - replace a property with nop tags
973 * @fdt: pointer to the device tree blob
974 * @nodeoffset: offset of the node whose property to nop
975 * @name: name of the property to nop
976 *
977 * fdt_nop_property() will replace a given property's representation
978 * in the blob with FDT_NOP tags, effectively removing it from the
979 * tree.
980 *
981 * This function will alter only the bytes in the blob which contain
982 * the property, and will not alter or move any other part of the
983 * tree.
984 *
985 * returns:
986 * 0, on success
987 * -FDT_ERR_NOTFOUND, node does not have the named property
988 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
989 * -FDT_ERR_BADMAGIC,
990 * -FDT_ERR_BADVERSION,
991 * -FDT_ERR_BADSTATE,
992 * -FDT_ERR_BADSTRUCTURE,
993 * -FDT_ERR_TRUNCATED, standard meanings
994 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400995int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
David Gibson9d4450b2007-12-17 14:41:52 +1100996
997/**
998 * fdt_nop_node - replace a node (subtree) with nop tags
999 * @fdt: pointer to the device tree blob
1000 * @nodeoffset: offset of the node to nop
1001 *
1002 * fdt_nop_node() will replace a given node's representation in the
1003 * blob, including all its subnodes, if any, with FDT_NOP tags,
1004 * effectively removing it from the tree.
1005 *
1006 * This function will alter only the bytes in the blob which contain
1007 * the node and its properties and subnodes, and will not alter or
1008 * move any other part of the tree.
1009 *
1010 * returns:
1011 * 0, on success
1012 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1013 * -FDT_ERR_BADMAGIC,
1014 * -FDT_ERR_BADVERSION,
1015 * -FDT_ERR_BADSTATE,
1016 * -FDT_ERR_BADSTRUCTURE,
1017 * -FDT_ERR_TRUNCATED, standard meanings
1018 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001019int fdt_nop_node(void *fdt, int nodeoffset);
Gerald Van Baren3f9f08c2007-04-14 22:46:41 -04001020
Kumar Gala8d04f022007-10-24 11:04:22 -05001021/**********************************************************************/
1022/* Sequential write functions */
1023/**********************************************************************/
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001024
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001025int fdt_create(void *buf, int bufsize);
1026int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1027int fdt_finish_reservemap(void *fdt);
1028int fdt_begin_node(void *fdt, const char *name);
1029int fdt_property(void *fdt, const char *name, const void *val, int len);
David Gibson24fa0e52012-08-27 19:39:00 -05001030static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
Kumar Gala8d04f022007-10-24 11:04:22 -05001031{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +00001032 fdt32_t tmp = cpu_to_fdt32(val);
1033 return fdt_property(fdt, name, &tmp, sizeof(tmp));
Kumar Gala8d04f022007-10-24 11:04:22 -05001034}
David Gibson24fa0e52012-08-27 19:39:00 -05001035static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1036{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +00001037 fdt64_t tmp = cpu_to_fdt64(val);
1038 return fdt_property(fdt, name, &tmp, sizeof(tmp));
David Gibson24fa0e52012-08-27 19:39:00 -05001039}
1040static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1041{
1042 return fdt_property_u32(fdt, name, val);
1043}
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001044#define fdt_property_string(fdt, name, str) \
1045 fdt_property(fdt, name, str, strlen(str)+1)
1046int fdt_end_node(void *fdt);
1047int fdt_finish(void *fdt);
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001048
Kumar Gala8d04f022007-10-24 11:04:22 -05001049/**********************************************************************/
1050/* Read-write functions */
1051/**********************************************************************/
1052
Gerald Van Barenc71b64f2012-10-15 22:24:36 -04001053int fdt_create_empty_tree(void *buf, int bufsize);
Kumar Gala8d04f022007-10-24 11:04:22 -05001054int fdt_open_into(const void *fdt, void *buf, int bufsize);
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001055int fdt_pack(void *fdt);
1056
David Gibsonaec71352007-12-17 14:42:07 +11001057/**
1058 * fdt_add_mem_rsv - add one memory reserve map entry
1059 * @fdt: pointer to the device tree blob
Wolfram Sang46390da2008-07-09 11:22:44 +02001060 * @address, @size: 64-bit values (native endian)
David Gibsonaec71352007-12-17 14:42:07 +11001061 *
1062 * Adds a reserve map entry to the given blob reserving a region at
1063 * address address of length size.
1064 *
1065 * This function will insert data into the reserve map and will
Wolfram Sang46390da2008-07-09 11:22:44 +02001066 * therefore change the indexes of some entries in the table.
David Gibsonaec71352007-12-17 14:42:07 +11001067 *
1068 * returns:
1069 * 0, on success
1070 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1071 * contain the new reservation entry
1072 * -FDT_ERR_BADMAGIC,
1073 * -FDT_ERR_BADVERSION,
1074 * -FDT_ERR_BADSTATE,
1075 * -FDT_ERR_BADSTRUCTURE,
1076 * -FDT_ERR_BADLAYOUT,
1077 * -FDT_ERR_TRUNCATED, standard meanings
1078 */
Kumar Gala8d04f022007-10-24 11:04:22 -05001079int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
David Gibsonaec71352007-12-17 14:42:07 +11001080
1081/**
1082 * fdt_del_mem_rsv - remove a memory reserve map entry
1083 * @fdt: pointer to the device tree blob
1084 * @n: entry to remove
1085 *
1086 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1087 * the blob.
1088 *
1089 * This function will delete data from the reservation table and will
Wolfram Sang46390da2008-07-09 11:22:44 +02001090 * therefore change the indexes of some entries in the table.
David Gibsonaec71352007-12-17 14:42:07 +11001091 *
1092 * returns:
1093 * 0, on success
1094 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1095 * are less than n+1 reserve map entries)
1096 * -FDT_ERR_BADMAGIC,
1097 * -FDT_ERR_BADVERSION,
1098 * -FDT_ERR_BADSTATE,
1099 * -FDT_ERR_BADSTRUCTURE,
1100 * -FDT_ERR_BADLAYOUT,
1101 * -FDT_ERR_TRUNCATED, standard meanings
1102 */
Kumar Gala8d04f022007-10-24 11:04:22 -05001103int fdt_del_mem_rsv(void *fdt, int n);
1104
David Gibsonaec71352007-12-17 14:42:07 +11001105/**
David Gibson9eaeb072008-01-11 14:55:05 +11001106 * fdt_set_name - change the name of a given node
1107 * @fdt: pointer to the device tree blob
1108 * @nodeoffset: structure block offset of a node
1109 * @name: name to give the node
1110 *
1111 * fdt_set_name() replaces the name (including unit address, if any)
1112 * of the given node with the given string. NOTE: this function can't
1113 * efficiently check if the new name is unique amongst the given
1114 * node's siblings; results are undefined if this function is invoked
1115 * with a name equal to one of the given node's siblings.
1116 *
1117 * This function may insert or delete data from the blob, and will
1118 * therefore change the offsets of some existing nodes.
1119 *
1120 * returns:
1121 * 0, on success
1122 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1123 * to contain the new name
1124 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1125 * -FDT_ERR_BADMAGIC,
1126 * -FDT_ERR_BADVERSION,
1127 * -FDT_ERR_BADSTATE, standard meanings
1128 */
1129int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1130
1131/**
David Gibsonaec71352007-12-17 14:42:07 +11001132 * fdt_setprop - create or change a property
1133 * @fdt: pointer to the device tree blob
1134 * @nodeoffset: offset of the node whose property to change
1135 * @name: name of the property to change
1136 * @val: pointer to data to set the property value to
1137 * @len: length of the property value
1138 *
1139 * fdt_setprop() sets the value of the named property in the given
Wolfram Sang46390da2008-07-09 11:22:44 +02001140 * node to the given value and length, creating the property if it
David Gibsonaec71352007-12-17 14:42:07 +11001141 * does not already exist.
1142 *
1143 * This function may insert or delete data from the blob, and will
1144 * therefore change the offsets of some existing nodes.
1145 *
1146 * returns:
1147 * 0, on success
1148 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1149 * contain the new property value
1150 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1151 * -FDT_ERR_BADLAYOUT,
1152 * -FDT_ERR_BADMAGIC,
1153 * -FDT_ERR_BADVERSION,
1154 * -FDT_ERR_BADSTATE,
1155 * -FDT_ERR_BADSTRUCTURE,
1156 * -FDT_ERR_BADLAYOUT,
1157 * -FDT_ERR_TRUNCATED, standard meanings
1158 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001159int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1160 const void *val, int len);
David Gibsonaec71352007-12-17 14:42:07 +11001161
1162/**
David Gibson24fa0e52012-08-27 19:39:00 -05001163 * fdt_setprop_u32 - set a property to a 32-bit integer
David Gibsonaec71352007-12-17 14:42:07 +11001164 * @fdt: pointer to the device tree blob
1165 * @nodeoffset: offset of the node whose property to change
1166 * @name: name of the property to change
1167 * @val: 32-bit integer value for the property (native endian)
1168 *
David Gibson24fa0e52012-08-27 19:39:00 -05001169 * fdt_setprop_u32() sets the value of the named property in the given
1170 * node to the given 32-bit integer value (converting to big-endian if
David Gibsonaec71352007-12-17 14:42:07 +11001171 * necessary), or creates a new property with that value if it does
1172 * not already exist.
1173 *
1174 * This function may insert or delete data from the blob, and will
1175 * therefore change the offsets of some existing nodes.
1176 *
1177 * returns:
1178 * 0, on success
1179 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1180 * contain the new property value
1181 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1182 * -FDT_ERR_BADLAYOUT,
1183 * -FDT_ERR_BADMAGIC,
1184 * -FDT_ERR_BADVERSION,
1185 * -FDT_ERR_BADSTATE,
1186 * -FDT_ERR_BADSTRUCTURE,
1187 * -FDT_ERR_BADLAYOUT,
1188 * -FDT_ERR_TRUNCATED, standard meanings
1189 */
David Gibson24fa0e52012-08-27 19:39:00 -05001190static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1191 uint32_t val)
Kumar Gala8d04f022007-10-24 11:04:22 -05001192{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +00001193 fdt32_t tmp = cpu_to_fdt32(val);
1194 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
Kumar Gala8d04f022007-10-24 11:04:22 -05001195}
David Gibsonaec71352007-12-17 14:42:07 +11001196
1197/**
David Gibson24fa0e52012-08-27 19:39:00 -05001198 * fdt_setprop_u64 - set a property to a 64-bit integer
1199 * @fdt: pointer to the device tree blob
1200 * @nodeoffset: offset of the node whose property to change
1201 * @name: name of the property to change
1202 * @val: 64-bit integer value for the property (native endian)
1203 *
1204 * fdt_setprop_u64() sets the value of the named property in the given
1205 * node to the given 64-bit integer value (converting to big-endian if
1206 * necessary), or creates a new property with that value if it does
1207 * not already exist.
1208 *
1209 * This function may insert or delete data from the blob, and will
1210 * therefore change the offsets of some existing nodes.
1211 *
1212 * returns:
1213 * 0, on success
1214 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1215 * contain the new property value
1216 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1217 * -FDT_ERR_BADLAYOUT,
1218 * -FDT_ERR_BADMAGIC,
1219 * -FDT_ERR_BADVERSION,
1220 * -FDT_ERR_BADSTATE,
1221 * -FDT_ERR_BADSTRUCTURE,
1222 * -FDT_ERR_BADLAYOUT,
1223 * -FDT_ERR_TRUNCATED, standard meanings
1224 */
1225static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1226 uint64_t val)
1227{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +00001228 fdt64_t tmp = cpu_to_fdt64(val);
1229 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
David Gibson24fa0e52012-08-27 19:39:00 -05001230}
1231
1232/**
1233 * fdt_setprop_cell - set a property to a single cell value
1234 *
1235 * This is an alternative name for fdt_setprop_u32()
1236 */
1237static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1238 uint32_t val)
1239{
1240 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1241}
1242
1243/**
David Gibsonaec71352007-12-17 14:42:07 +11001244 * fdt_setprop_string - set a property to a string value
1245 * @fdt: pointer to the device tree blob
1246 * @nodeoffset: offset of the node whose property to change
1247 * @name: name of the property to change
1248 * @str: string value for the property
1249 *
1250 * fdt_setprop_string() sets the value of the named property in the
1251 * given node to the given string value (using the length of the
1252 * string to determine the new length of the property), or creates a
1253 * new property with that value if it does not already exist.
1254 *
1255 * This function may insert or delete data from the blob, and will
1256 * therefore change the offsets of some existing nodes.
1257 *
1258 * returns:
1259 * 0, on success
1260 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1261 * contain the new property value
1262 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1263 * -FDT_ERR_BADLAYOUT,
1264 * -FDT_ERR_BADMAGIC,
1265 * -FDT_ERR_BADVERSION,
1266 * -FDT_ERR_BADSTATE,
1267 * -FDT_ERR_BADSTRUCTURE,
1268 * -FDT_ERR_BADLAYOUT,
1269 * -FDT_ERR_TRUNCATED, standard meanings
1270 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001271#define fdt_setprop_string(fdt, nodeoffset, name, str) \
1272 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
David Gibsonaec71352007-12-17 14:42:07 +11001273
1274/**
Minghuan Lian36ad18a2012-08-27 19:38:59 -05001275 * fdt_appendprop - append to or create a property
1276 * @fdt: pointer to the device tree blob
1277 * @nodeoffset: offset of the node whose property to change
1278 * @name: name of the property to append to
1279 * @val: pointer to data to append to the property value
1280 * @len: length of the data to append to the property value
1281 *
1282 * fdt_appendprop() appends the value to the named property in the
1283 * given node, creating the property if it does not already exist.
1284 *
1285 * This function may insert data into the blob, and will therefore
1286 * change the offsets of some existing nodes.
1287 *
1288 * returns:
1289 * 0, on success
1290 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1291 * contain the new property value
1292 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1293 * -FDT_ERR_BADLAYOUT,
1294 * -FDT_ERR_BADMAGIC,
1295 * -FDT_ERR_BADVERSION,
1296 * -FDT_ERR_BADSTATE,
1297 * -FDT_ERR_BADSTRUCTURE,
1298 * -FDT_ERR_BADLAYOUT,
1299 * -FDT_ERR_TRUNCATED, standard meanings
1300 */
1301int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1302 const void *val, int len);
1303
1304/**
David Gibson24fa0e52012-08-27 19:39:00 -05001305 * fdt_appendprop_u32 - append a 32-bit integer value to a property
Minghuan Lian36ad18a2012-08-27 19:38:59 -05001306 * @fdt: pointer to the device tree blob
1307 * @nodeoffset: offset of the node whose property to change
1308 * @name: name of the property to change
1309 * @val: 32-bit integer value to append to the property (native endian)
1310 *
David Gibson24fa0e52012-08-27 19:39:00 -05001311 * fdt_appendprop_u32() appends the given 32-bit integer value
1312 * (converting to big-endian if necessary) to the value of the named
1313 * property in the given node, or creates a new property with that
1314 * value if it does not already exist.
Minghuan Lian36ad18a2012-08-27 19:38:59 -05001315 *
1316 * This function may insert data into the blob, and will therefore
1317 * change the offsets of some existing nodes.
1318 *
1319 * returns:
1320 * 0, on success
1321 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1322 * contain the new property value
1323 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1324 * -FDT_ERR_BADLAYOUT,
1325 * -FDT_ERR_BADMAGIC,
1326 * -FDT_ERR_BADVERSION,
1327 * -FDT_ERR_BADSTATE,
1328 * -FDT_ERR_BADSTRUCTURE,
1329 * -FDT_ERR_BADLAYOUT,
1330 * -FDT_ERR_TRUNCATED, standard meanings
1331 */
David Gibson24fa0e52012-08-27 19:39:00 -05001332static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1333 const char *name, uint32_t val)
Minghuan Lian36ad18a2012-08-27 19:38:59 -05001334{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +00001335 fdt32_t tmp = cpu_to_fdt32(val);
1336 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
Minghuan Lian36ad18a2012-08-27 19:38:59 -05001337}
1338
1339/**
David Gibson24fa0e52012-08-27 19:39:00 -05001340 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1341 * @fdt: pointer to the device tree blob
1342 * @nodeoffset: offset of the node whose property to change
1343 * @name: name of the property to change
1344 * @val: 64-bit integer value to append to the property (native endian)
1345 *
1346 * fdt_appendprop_u64() appends the given 64-bit integer value
1347 * (converting to big-endian if necessary) to the value of the named
1348 * property in the given node, or creates a new property with that
1349 * value if it does not already exist.
1350 *
1351 * This function may insert data into the blob, and will therefore
1352 * change the offsets of some existing nodes.
1353 *
1354 * returns:
1355 * 0, on success
1356 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1357 * contain the new property value
1358 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1359 * -FDT_ERR_BADLAYOUT,
1360 * -FDT_ERR_BADMAGIC,
1361 * -FDT_ERR_BADVERSION,
1362 * -FDT_ERR_BADSTATE,
1363 * -FDT_ERR_BADSTRUCTURE,
1364 * -FDT_ERR_BADLAYOUT,
1365 * -FDT_ERR_TRUNCATED, standard meanings
1366 */
1367static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1368 const char *name, uint64_t val)
1369{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +00001370 fdt64_t tmp = cpu_to_fdt64(val);
1371 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
David Gibson24fa0e52012-08-27 19:39:00 -05001372}
1373
1374/**
1375 * fdt_appendprop_cell - append a single cell value to a property
1376 *
1377 * This is an alternative name for fdt_appendprop_u32()
1378 */
1379static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1380 const char *name, uint32_t val)
1381{
1382 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1383}
1384
1385/**
Minghuan Lian36ad18a2012-08-27 19:38:59 -05001386 * fdt_appendprop_string - append a string to a property
1387 * @fdt: pointer to the device tree blob
1388 * @nodeoffset: offset of the node whose property to change
1389 * @name: name of the property to change
1390 * @str: string value to append to the property
1391 *
1392 * fdt_appendprop_string() appends the given string to the value of
1393 * the named property in the given node, or creates a new property
1394 * with that value if it does not already exist.
1395 *
1396 * This function may insert data into the blob, and will therefore
1397 * change the offsets of some existing nodes.
1398 *
1399 * returns:
1400 * 0, on success
1401 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1402 * contain the new property value
1403 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1404 * -FDT_ERR_BADLAYOUT,
1405 * -FDT_ERR_BADMAGIC,
1406 * -FDT_ERR_BADVERSION,
1407 * -FDT_ERR_BADSTATE,
1408 * -FDT_ERR_BADSTRUCTURE,
1409 * -FDT_ERR_BADLAYOUT,
1410 * -FDT_ERR_TRUNCATED, standard meanings
1411 */
1412#define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1413 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1414
1415/**
David Gibsonaec71352007-12-17 14:42:07 +11001416 * fdt_delprop - delete a property
1417 * @fdt: pointer to the device tree blob
1418 * @nodeoffset: offset of the node whose property to nop
1419 * @name: name of the property to nop
1420 *
1421 * fdt_del_property() will delete the given property.
1422 *
1423 * This function will delete data from the blob, and will therefore
1424 * change the offsets of some existing nodes.
1425 *
1426 * returns:
1427 * 0, on success
1428 * -FDT_ERR_NOTFOUND, node does not have the named property
1429 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1430 * -FDT_ERR_BADLAYOUT,
1431 * -FDT_ERR_BADMAGIC,
1432 * -FDT_ERR_BADVERSION,
1433 * -FDT_ERR_BADSTATE,
1434 * -FDT_ERR_BADSTRUCTURE,
1435 * -FDT_ERR_TRUNCATED, standard meanings
1436 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001437int fdt_delprop(void *fdt, int nodeoffset, const char *name);
David Gibsonaec71352007-12-17 14:42:07 +11001438
1439/**
1440 * fdt_add_subnode_namelen - creates a new node based on substring
1441 * @fdt: pointer to the device tree blob
1442 * @parentoffset: structure block offset of a node
1443 * @name: name of the subnode to locate
1444 * @namelen: number of characters of name to consider
1445 *
1446 * Identical to fdt_add_subnode(), but use only the first namelen
1447 * characters of name as the name of the new node. This is useful for
1448 * creating subnodes based on a portion of a larger string, such as a
1449 * full path.
1450 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001451int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1452 const char *name, int namelen);
David Gibsonaec71352007-12-17 14:42:07 +11001453
1454/**
1455 * fdt_add_subnode - creates a new node
1456 * @fdt: pointer to the device tree blob
1457 * @parentoffset: structure block offset of a node
1458 * @name: name of the subnode to locate
1459 *
1460 * fdt_add_subnode() creates a new node as a subnode of the node at
1461 * structure block offset parentoffset, with the given name (which
1462 * should include the unit address, if any).
1463 *
1464 * This function will insert data into the blob, and will therefore
1465 * change the offsets of some existing nodes.
1466
1467 * returns:
1468 * structure block offset of the created nodeequested subnode (>=0), on success
1469 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1470 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1471 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1472 * the given name
1473 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1474 * blob to contain the new node
1475 * -FDT_ERR_NOSPACE
1476 * -FDT_ERR_BADLAYOUT
1477 * -FDT_ERR_BADMAGIC,
1478 * -FDT_ERR_BADVERSION,
1479 * -FDT_ERR_BADSTATE,
1480 * -FDT_ERR_BADSTRUCTURE,
1481 * -FDT_ERR_TRUNCATED, standard meanings.
1482 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001483int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
David Gibsonaec71352007-12-17 14:42:07 +11001484
1485/**
1486 * fdt_del_node - delete a node (subtree)
1487 * @fdt: pointer to the device tree blob
1488 * @nodeoffset: offset of the node to nop
1489 *
1490 * fdt_del_node() will remove the given node, including all its
1491 * subnodes if any, from the blob.
1492 *
1493 * This function will delete data from the blob, and will therefore
1494 * change the offsets of some existing nodes.
1495 *
1496 * returns:
1497 * 0, on success
1498 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1499 * -FDT_ERR_BADLAYOUT,
1500 * -FDT_ERR_BADMAGIC,
1501 * -FDT_ERR_BADVERSION,
1502 * -FDT_ERR_BADSTATE,
1503 * -FDT_ERR_BADSTRUCTURE,
1504 * -FDT_ERR_TRUNCATED, standard meanings
1505 */
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001506int fdt_del_node(void *fdt, int nodeoffset);
1507
Kumar Gala8d04f022007-10-24 11:04:22 -05001508/**********************************************************************/
1509/* Debugging / informational functions */
1510/**********************************************************************/
1511
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001512const char *fdt_strerror(int errval);
1513
Simon Glass3e06cd12013-06-13 15:10:08 -07001514struct fdt_region {
1515 int offset;
1516 int size;
1517};
1518
1519/**
1520 * fdt_find_regions() - find regions in device tree
1521 *
1522 * Given a list of nodes to include and properties to exclude, find
1523 * the regions of the device tree which describe those included parts.
1524 *
1525 * The intent is to get a list of regions which will be invariant provided
1526 * those parts are invariant. For example, if you request a list of regions
1527 * for all nodes but exclude the property "data", then you will get the
1528 * same region contents regardless of any change to "data" properties.
1529 *
1530 * This function can be used to produce a byte-stream to send to a hashing
1531 * function to verify that critical parts of the FDT have not changed.
1532 *
1533 * Nodes which are given in 'inc' are included in the region list, as
1534 * are the names of the immediate subnodes nodes (but not the properties
1535 * or subnodes of those subnodes).
1536 *
1537 * For eaxample "/" means to include the root node, all root properties
1538 * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1539 * ensures that we capture the names of the subnodes. In a hashing situation
1540 * it prevents the root node from changing at all Any change to non-excluded
1541 * properties, names of subnodes or number of subnodes would be detected.
1542 *
1543 * When used with FITs this provides the ability to hash and sign parts of
1544 * the FIT based on different configurations in the FIT. Then it is
1545 * impossible to change anything about that configuration (include images
1546 * attached to the configuration), but it may be possible to add new
1547 * configurations, new images or new signatures within the existing
1548 * framework.
1549 *
1550 * Adding new properties to a device tree may result in the string table
1551 * being extended (if the new property names are different from those
1552 * already added). This function can optionally include a region for
1553 * the string table so that this can be part of the hash too.
1554 *
1555 * The device tree header is not included in the list.
1556 *
1557 * @fdt: Device tree to check
1558 * @inc: List of node paths to included
1559 * @inc_count: Number of node paths in list
1560 * @exc_prop: List of properties names to exclude
1561 * @exc_prop_count: Number of properties in exclude list
1562 * @region: Returns list of regions
1563 * @max_region: Maximum length of region list
1564 * @path: Pointer to a temporary string for the function to use for
1565 * building path names
1566 * @path_len: Length of path, must be large enough to hold the longest
1567 * path in the tree
1568 * @add_string_tab: 1 to add a region for the string table
1569 * @return number of regions in list. If this is >max_regions then the
1570 * region array was exhausted. You should increase max_regions and try
1571 * the call again.
1572 */
1573int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
1574 char * const exc_prop[], int exc_prop_count,
1575 struct fdt_region region[], int max_regions,
1576 char *path, int path_len, int add_string_tab);
1577
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001578#endif /* _LIBFDT_H */