]> git.ipfire.org Git - people/ms/u-boot.git/blob - include/libfdt.h
libfdt: Add fdt_path_offset_namelen
[people/ms/u-boot.git] / include / libfdt.h
1 #ifndef _LIBFDT_H
2 #define _LIBFDT_H
3 /*
4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 *
7 * libfdt is dual licensed: you can use it either under the terms of
8 * the GPL, or the BSD license, at your option.
9 *
10 * a) This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this library; if not, write to the Free
22 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 * MA 02110-1301 USA
24 *
25 * Alternatively,
26 *
27 * b) Redistribution and use in source and binary forms, with or
28 * without modification, are permitted provided that the following
29 * conditions are met:
30 *
31 * 1. Redistributions of source code must retain the above
32 * copyright notice, this list of conditions and the following
33 * disclaimer.
34 * 2. Redistributions in binary form must reproduce the above
35 * copyright notice, this list of conditions and the following
36 * disclaimer in the documentation and/or other materials
37 * provided with the distribution.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
40 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
44 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
50 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
51 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54 #include <libfdt_env.h>
55 #include <fdt.h>
56
57 #define FDT_FIRST_SUPPORTED_VERSION 0x10
58 #define FDT_LAST_SUPPORTED_VERSION 0x11
59
60 /* Error codes: informative error codes */
61 #define FDT_ERR_NOTFOUND 1
62 /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
63 #define FDT_ERR_EXISTS 2
64 /* FDT_ERR_EXISTS: Attemped to create a node or property which
65 * already exists */
66 #define FDT_ERR_NOSPACE 3
67 /* FDT_ERR_NOSPACE: Operation needed to expand the device
68 * tree, but its buffer did not have sufficient space to
69 * contain the expanded tree. Use fdt_open_into() to move the
70 * device tree to a buffer with more space. */
71
72 /* Error codes: codes for bad parameters */
73 #define FDT_ERR_BADOFFSET 4
74 /* FDT_ERR_BADOFFSET: Function was passed a structure block
75 * offset which is out-of-bounds, or which points to an
76 * unsuitable part of the structure for the operation. */
77 #define FDT_ERR_BADPATH 5
78 /* FDT_ERR_BADPATH: Function was passed a badly formatted path
79 * (e.g. missing a leading / for a function which requires an
80 * absolute path) */
81 #define FDT_ERR_BADPHANDLE 6
82 /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle
83 * value. phandle values of 0 and -1 are not permitted. */
84 #define FDT_ERR_BADSTATE 7
85 /* FDT_ERR_BADSTATE: Function was passed an incomplete device
86 * tree created by the sequential-write functions, which is
87 * not sufficiently complete for the requested operation. */
88
89 /* Error codes: codes for bad device tree blobs */
90 #define FDT_ERR_TRUNCATED 8
91 /* FDT_ERR_TRUNCATED: Structure block of the given device tree
92 * ends without an FDT_END tag. */
93 #define FDT_ERR_BADMAGIC 9
94 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
95 * device tree at all - it is missing the flattened device
96 * tree magic number. */
97 #define FDT_ERR_BADVERSION 10
98 /* FDT_ERR_BADVERSION: Given device tree has a version which
99 * can't be handled by the requested operation. For
100 * read-write functions, this may mean that fdt_open_into() is
101 * required to convert the tree to the expected version. */
102 #define FDT_ERR_BADSTRUCTURE 11
103 /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
104 * structure block or other serious error (e.g. misnested
105 * nodes, or subnodes preceding properties). */
106 #define FDT_ERR_BADLAYOUT 12
107 /* FDT_ERR_BADLAYOUT: For read-write functions, the given
108 * device tree has it's sub-blocks in an order that the
109 * function can't handle (memory reserve map, then structure,
110 * then strings). Use fdt_open_into() to reorganize the tree
111 * into a form suitable for the read-write operations. */
112
113 /* "Can't happen" error indicating a bug in libfdt */
114 #define FDT_ERR_INTERNAL 13
115 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
116 * Should never be returned, if it is, it indicates a bug in
117 * libfdt itself. */
118
119 /* Errors in device tree content */
120 #define FDT_ERR_BADNCELLS 14
121 /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
122 * or similar property with a bad format or value */
123
124 #define FDT_ERR_TOODEEP 15
125 /* FDT_ERR_TOODEEP: The depth of a node has exceeded the internal
126 * libfdt limit. This can happen if you have more than
127 * FDT_MAX_DEPTH nested nodes. */
128
129 #define FDT_ERR_MAX 15
130
131 /**********************************************************************/
132 /* Low-level functions (you probably don't need these) */
133 /**********************************************************************/
134
135 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
136 static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
137 {
138 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
139 }
140
141 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
142
143 /**********************************************************************/
144 /* Traversal functions */
145 /**********************************************************************/
146
147 int fdt_next_node(const void *fdt, int offset, int *depth);
148
149 /**
150 * fdt_first_subnode() - get offset of first direct subnode
151 *
152 * @fdt: FDT blob
153 * @offset: Offset of node to check
154 * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
155 */
156 int fdt_first_subnode(const void *fdt, int offset);
157
158 /**
159 * fdt_next_subnode() - get offset of next direct subnode
160 *
161 * After first calling fdt_first_subnode(), call this function repeatedly to
162 * get direct subnodes of a parent node.
163 *
164 * @fdt: FDT blob
165 * @offset: Offset of previous subnode
166 * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
167 * subnodes
168 */
169 int fdt_next_subnode(const void *fdt, int offset);
170
171 /**
172 * fdt_for_each_subnode - iterate over all subnodes of a parent
173 *
174 * This is actually a wrapper around a for loop and would be used like so:
175 *
176 * fdt_for_each_subnode(fdt, node, parent) {
177 * ...
178 * use node
179 * ...
180 * }
181 *
182 * Note that this is implemented as a macro and node is used as iterator in
183 * the loop. It should therefore be a locally allocated variable. The parent
184 * variable on the other hand is never modified, so it can be constant or
185 * even a literal.
186 *
187 * @fdt: FDT blob (const void *)
188 * @node: child node (int)
189 * @parent: parent node (int)
190 */
191 #define fdt_for_each_subnode(fdt, node, parent) \
192 for (node = fdt_first_subnode(fdt, parent); \
193 node >= 0; \
194 node = fdt_next_subnode(fdt, node))
195
196 /**********************************************************************/
197 /* General functions */
198 /**********************************************************************/
199
200 #define fdt_get_header(fdt, field) \
201 (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
202 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
203 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
204 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
205 #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
206 #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
207 #define fdt_version(fdt) (fdt_get_header(fdt, version))
208 #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
209 #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
210 #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
211 #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
212
213 #define __fdt_set_hdr(name) \
214 static inline void fdt_set_##name(void *fdt, uint32_t val) \
215 { \
216 struct fdt_header *fdth = (struct fdt_header*)fdt; \
217 fdth->name = cpu_to_fdt32(val); \
218 }
219 __fdt_set_hdr(magic);
220 __fdt_set_hdr(totalsize);
221 __fdt_set_hdr(off_dt_struct);
222 __fdt_set_hdr(off_dt_strings);
223 __fdt_set_hdr(off_mem_rsvmap);
224 __fdt_set_hdr(version);
225 __fdt_set_hdr(last_comp_version);
226 __fdt_set_hdr(boot_cpuid_phys);
227 __fdt_set_hdr(size_dt_strings);
228 __fdt_set_hdr(size_dt_struct);
229 #undef __fdt_set_hdr
230
231 /**
232 * fdt_check_header - sanity check a device tree or possible device tree
233 * @fdt: pointer to data which might be a flattened device tree
234 *
235 * fdt_check_header() checks that the given buffer contains what
236 * appears to be a flattened device tree with sane information in its
237 * header.
238 *
239 * returns:
240 * 0, if the buffer appears to contain a valid device tree
241 * -FDT_ERR_BADMAGIC,
242 * -FDT_ERR_BADVERSION,
243 * -FDT_ERR_BADSTATE, standard meanings, as above
244 */
245 int fdt_check_header(const void *fdt);
246
247 /**
248 * fdt_move - move a device tree around in memory
249 * @fdt: pointer to the device tree to move
250 * @buf: pointer to memory where the device is to be moved
251 * @bufsize: size of the memory space at buf
252 *
253 * fdt_move() relocates, if possible, the device tree blob located at
254 * fdt to the buffer at buf of size bufsize. The buffer may overlap
255 * with the existing device tree blob at fdt. Therefore,
256 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
257 * should always succeed.
258 *
259 * returns:
260 * 0, on success
261 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
262 * -FDT_ERR_BADMAGIC,
263 * -FDT_ERR_BADVERSION,
264 * -FDT_ERR_BADSTATE, standard meanings
265 */
266 int fdt_move(const void *fdt, void *buf, int bufsize);
267
268 /**********************************************************************/
269 /* Read-only functions */
270 /**********************************************************************/
271
272 /**
273 * fdt_string - retrieve a string from the strings block of a device tree
274 * @fdt: pointer to the device tree blob
275 * @stroffset: offset of the string within the strings block (native endian)
276 *
277 * fdt_string() retrieves a pointer to a single string from the
278 * strings block of the device tree blob at fdt.
279 *
280 * returns:
281 * a pointer to the string, on success
282 * NULL, if stroffset is out of bounds
283 */
284 const char *fdt_string(const void *fdt, int stroffset);
285
286 /**
287 * fdt_get_max_phandle - retrieves the highest phandle in a tree
288 * @fdt: pointer to the device tree blob
289 *
290 * fdt_get_max_phandle retrieves the highest phandle in the given
291 * device tree
292 *
293 * returns:
294 * the highest phandle on success
295 * 0, if an error occurred
296 */
297 uint32_t fdt_get_max_phandle(const void *fdt);
298
299 /**
300 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
301 * @fdt: pointer to the device tree blob
302 *
303 * Returns the number of entries in the device tree blob's memory
304 * reservation map. This does not include the terminating 0,0 entry
305 * or any other (0,0) entries reserved for expansion.
306 *
307 * returns:
308 * the number of entries
309 */
310 int fdt_num_mem_rsv(const void *fdt);
311
312 /**
313 * fdt_get_mem_rsv - retrieve one memory reserve map entry
314 * @fdt: pointer to the device tree blob
315 * @address, @size: pointers to 64-bit variables
316 *
317 * On success, *address and *size will contain the address and size of
318 * the n-th reserve map entry from the device tree blob, in
319 * native-endian format.
320 *
321 * returns:
322 * 0, on success
323 * -FDT_ERR_BADMAGIC,
324 * -FDT_ERR_BADVERSION,
325 * -FDT_ERR_BADSTATE, standard meanings
326 */
327 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
328
329 /**
330 * fdt_subnode_offset_namelen - find a subnode based on substring
331 * @fdt: pointer to the device tree blob
332 * @parentoffset: structure block offset of a node
333 * @name: name of the subnode to locate
334 * @namelen: number of characters of name to consider
335 *
336 * Identical to fdt_subnode_offset(), but only examine the first
337 * namelen characters of name for matching the subnode name. This is
338 * useful for finding subnodes based on a portion of a larger string,
339 * such as a full path.
340 */
341 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
342 const char *name, int namelen);
343 /**
344 * fdt_subnode_offset - find a subnode of a given node
345 * @fdt: pointer to the device tree blob
346 * @parentoffset: structure block offset of a node
347 * @name: name of the subnode to locate
348 *
349 * fdt_subnode_offset() finds a subnode of the node at structure block
350 * offset parentoffset with the given name. name may include a unit
351 * address, in which case fdt_subnode_offset() will find the subnode
352 * with that unit address, or the unit address may be omitted, in
353 * which case fdt_subnode_offset() will find an arbitrary subnode
354 * whose name excluding unit address matches the given name.
355 *
356 * returns:
357 * structure block offset of the requested subnode (>=0), on success
358 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
359 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
360 * -FDT_ERR_BADMAGIC,
361 * -FDT_ERR_BADVERSION,
362 * -FDT_ERR_BADSTATE,
363 * -FDT_ERR_BADSTRUCTURE,
364 * -FDT_ERR_TRUNCATED, standard meanings.
365 */
366 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
367
368 /**
369 * fdt_path_offset_namelen - find a tree node based on substring
370 * @fdt: pointer to the device tree blob
371 * @path: full path of the node to locate
372 * @namelen: number of characters of name to consider
373 *
374 * Identical to fdt_path_offset(), but only examine the first
375 * namelen characters of path for matching the node path.
376 */
377 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
378
379 /**
380 * fdt_path_offset - find a tree node by its full path
381 * @fdt: pointer to the device tree blob
382 * @path: full path of the node to locate
383 *
384 * fdt_path_offset() finds a node of a given path in the device tree.
385 * Each path component may omit the unit address portion, but the
386 * results of this are undefined if any such path component is
387 * ambiguous (that is if there are multiple nodes at the relevant
388 * level matching the given component, differentiated only by unit
389 * address).
390 *
391 * returns:
392 * structure block offset of the node with the requested path (>=0), on success
393 * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
394 * -FDT_ERR_NOTFOUND, if the requested node does not exist
395 * -FDT_ERR_BADMAGIC,
396 * -FDT_ERR_BADVERSION,
397 * -FDT_ERR_BADSTATE,
398 * -FDT_ERR_BADSTRUCTURE,
399 * -FDT_ERR_TRUNCATED, standard meanings.
400 */
401 static inline int fdt_path_offset(const void *fdt, const char *path)
402 {
403 return fdt_path_offset_namelen(fdt, path, strlen(path));
404 }
405
406 /**
407 * fdt_get_name - retrieve the name of a given node
408 * @fdt: pointer to the device tree blob
409 * @nodeoffset: structure block offset of the starting node
410 * @lenp: pointer to an integer variable (will be overwritten) or NULL
411 *
412 * fdt_get_name() retrieves the name (including unit address) of the
413 * device tree node at structure block offset nodeoffset. If lenp is
414 * non-NULL, the length of this name is also returned, in the integer
415 * pointed to by lenp.
416 *
417 * returns:
418 * pointer to the node's name, on success
419 * If lenp is non-NULL, *lenp contains the length of that name (>=0)
420 * NULL, on error
421 * if lenp is non-NULL *lenp contains an error code (<0):
422 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
423 * -FDT_ERR_BADMAGIC,
424 * -FDT_ERR_BADVERSION,
425 * -FDT_ERR_BADSTATE, standard meanings
426 */
427 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
428
429 /**
430 * fdt_first_property_offset - find the offset of a node's first property
431 * @fdt: pointer to the device tree blob
432 * @nodeoffset: structure block offset of a node
433 *
434 * fdt_first_property_offset() finds the first property of the node at
435 * the given structure block offset.
436 *
437 * returns:
438 * structure block offset of the property (>=0), on success
439 * -FDT_ERR_NOTFOUND, if the requested node has no properties
440 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
441 * -FDT_ERR_BADMAGIC,
442 * -FDT_ERR_BADVERSION,
443 * -FDT_ERR_BADSTATE,
444 * -FDT_ERR_BADSTRUCTURE,
445 * -FDT_ERR_TRUNCATED, standard meanings.
446 */
447 int fdt_first_property_offset(const void *fdt, int nodeoffset);
448
449 /**
450 * fdt_next_property_offset - step through a node's properties
451 * @fdt: pointer to the device tree blob
452 * @offset: structure block offset of a property
453 *
454 * fdt_next_property_offset() finds the property immediately after the
455 * one at the given structure block offset. This will be a property
456 * of the same node as the given property.
457 *
458 * returns:
459 * structure block offset of the next property (>=0), on success
460 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
461 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
462 * -FDT_ERR_BADMAGIC,
463 * -FDT_ERR_BADVERSION,
464 * -FDT_ERR_BADSTATE,
465 * -FDT_ERR_BADSTRUCTURE,
466 * -FDT_ERR_TRUNCATED, standard meanings.
467 */
468 int fdt_next_property_offset(const void *fdt, int offset);
469
470 /**
471 * fdt_for_each_property - iterate over all properties of a node
472 * @property_offset: property offset (int)
473 * @fdt: FDT blob (const void *)
474 * @node: node offset (int)
475 *
476 * This is actually a wrapper around a for loop and would be used like so:
477 *
478 * fdt_for_each_property(fdt, node, property) {
479 * ...
480 * use property
481 * ...
482 * }
483 *
484 * Note that this is implemented as a macro and property is used as
485 * iterator in the loop. It should therefore be a locally allocated
486 * variable. The node variable on the other hand is never modified, so
487 * it can be constant or even a literal.
488 */
489 #define fdt_for_each_property_offset(property, fdt, node) \
490 for (property = fdt_first_property_offset(fdt, node); \
491 property >= 0; \
492 property = fdt_next_property_offset(fdt, property))
493
494 /**
495 * fdt_get_property_by_offset - retrieve the property at a given offset
496 * @fdt: pointer to the device tree blob
497 * @offset: offset of the property to retrieve
498 * @lenp: pointer to an integer variable (will be overwritten) or NULL
499 *
500 * fdt_get_property_by_offset() retrieves a pointer to the
501 * fdt_property structure within the device tree blob at the given
502 * offset. If lenp is non-NULL, the length of the property value is
503 * also returned, in the integer pointed to by lenp.
504 *
505 * returns:
506 * pointer to the structure representing the property
507 * if lenp is non-NULL, *lenp contains the length of the property
508 * value (>=0)
509 * NULL, on error
510 * if lenp is non-NULL, *lenp contains an error code (<0):
511 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
512 * -FDT_ERR_BADMAGIC,
513 * -FDT_ERR_BADVERSION,
514 * -FDT_ERR_BADSTATE,
515 * -FDT_ERR_BADSTRUCTURE,
516 * -FDT_ERR_TRUNCATED, standard meanings
517 */
518 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
519 int offset,
520 int *lenp);
521
522 /**
523 * fdt_get_property_namelen - find a property based on substring
524 * @fdt: pointer to the device tree blob
525 * @nodeoffset: offset of the node whose property to find
526 * @name: name of the property to find
527 * @namelen: number of characters of name to consider
528 * @lenp: pointer to an integer variable (will be overwritten) or NULL
529 *
530 * Identical to fdt_get_property_namelen(), but only examine the first
531 * namelen characters of name for matching the property name.
532 */
533 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
534 int nodeoffset,
535 const char *name,
536 int namelen, int *lenp);
537
538 /**
539 * fdt_get_property - find a given property in a given node
540 * @fdt: pointer to the device tree blob
541 * @nodeoffset: offset of the node whose property to find
542 * @name: name of the property to find
543 * @lenp: pointer to an integer variable (will be overwritten) or NULL
544 *
545 * fdt_get_property() retrieves a pointer to the fdt_property
546 * structure within the device tree blob corresponding to the property
547 * named 'name' of the node at offset nodeoffset. If lenp is
548 * non-NULL, the length of the property value is also returned, in the
549 * integer pointed to by lenp.
550 *
551 * returns:
552 * pointer to the structure representing the property
553 * if lenp is non-NULL, *lenp contains the length of the property
554 * value (>=0)
555 * NULL, on error
556 * if lenp is non-NULL, *lenp contains an error code (<0):
557 * -FDT_ERR_NOTFOUND, node does not have named property
558 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
559 * -FDT_ERR_BADMAGIC,
560 * -FDT_ERR_BADVERSION,
561 * -FDT_ERR_BADSTATE,
562 * -FDT_ERR_BADSTRUCTURE,
563 * -FDT_ERR_TRUNCATED, standard meanings
564 */
565 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
566 const char *name, int *lenp);
567 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
568 const char *name,
569 int *lenp)
570 {
571 return (struct fdt_property *)(uintptr_t)
572 fdt_get_property(fdt, nodeoffset, name, lenp);
573 }
574
575 /**
576 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
577 * @fdt: pointer to the device tree blob
578 * @ffset: offset of the property to read
579 * @namep: pointer to a string variable (will be overwritten) or NULL
580 * @lenp: pointer to an integer variable (will be overwritten) or NULL
581 *
582 * fdt_getprop_by_offset() retrieves a pointer to the value of the
583 * property at structure block offset 'offset' (this will be a pointer
584 * to within the device blob itself, not a copy of the value). If
585 * lenp is non-NULL, the length of the property value is also
586 * returned, in the integer pointed to by lenp. If namep is non-NULL,
587 * the property's namne will also be returned in the char * pointed to
588 * by namep (this will be a pointer to within the device tree's string
589 * block, not a new copy of the name).
590 *
591 * returns:
592 * pointer to the property's value
593 * if lenp is non-NULL, *lenp contains the length of the property
594 * value (>=0)
595 * if namep is non-NULL *namep contiains a pointer to the property
596 * name.
597 * NULL, on error
598 * if lenp is non-NULL, *lenp contains an error code (<0):
599 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
600 * -FDT_ERR_BADMAGIC,
601 * -FDT_ERR_BADVERSION,
602 * -FDT_ERR_BADSTATE,
603 * -FDT_ERR_BADSTRUCTURE,
604 * -FDT_ERR_TRUNCATED, standard meanings
605 */
606 const void *fdt_getprop_by_offset(const void *fdt, int offset,
607 const char **namep, int *lenp);
608
609 /**
610 * fdt_getprop_namelen - get property value based on substring
611 * @fdt: pointer to the device tree blob
612 * @nodeoffset: offset of the node whose property to find
613 * @name: name of the property to find
614 * @namelen: number of characters of name to consider
615 * @lenp: pointer to an integer variable (will be overwritten) or NULL
616 *
617 * Identical to fdt_getprop(), but only examine the first namelen
618 * characters of name for matching the property name.
619 */
620 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
621 const char *name, int namelen, int *lenp);
622
623 /**
624 * fdt_getprop - retrieve the value of a given property
625 * @fdt: pointer to the device tree blob
626 * @nodeoffset: offset of the node whose property to find
627 * @name: name of the property to find
628 * @lenp: pointer to an integer variable (will be overwritten) or NULL
629 *
630 * fdt_getprop() retrieves a pointer to the value of the property
631 * named 'name' of the node at offset nodeoffset (this will be a
632 * pointer to within the device blob itself, not a copy of the value).
633 * If lenp is non-NULL, the length of the property value is also
634 * returned, in the integer pointed to by lenp.
635 *
636 * returns:
637 * pointer to the property's value
638 * if lenp is non-NULL, *lenp contains the length of the property
639 * value (>=0)
640 * NULL, on error
641 * if lenp is non-NULL, *lenp contains an error code (<0):
642 * -FDT_ERR_NOTFOUND, node does not have named property
643 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
644 * -FDT_ERR_BADMAGIC,
645 * -FDT_ERR_BADVERSION,
646 * -FDT_ERR_BADSTATE,
647 * -FDT_ERR_BADSTRUCTURE,
648 * -FDT_ERR_TRUNCATED, standard meanings
649 */
650 const void *fdt_getprop(const void *fdt, int nodeoffset,
651 const char *name, int *lenp);
652 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
653 const char *name, int *lenp)
654 {
655 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
656 }
657
658 /**
659 * fdt_get_phandle - retrieve the phandle of a given node
660 * @fdt: pointer to the device tree blob
661 * @nodeoffset: structure block offset of the node
662 *
663 * fdt_get_phandle() retrieves the phandle of the device tree node at
664 * structure block offset nodeoffset.
665 *
666 * returns:
667 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
668 * 0, if the node has no phandle, or another error occurs
669 */
670 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
671
672 /**
673 * fdt_get_alias_namelen - get alias based on substring
674 * @fdt: pointer to the device tree blob
675 * @name: name of the alias th look up
676 * @namelen: number of characters of name to consider
677 *
678 * Identical to fdt_get_alias(), but only examine the first namelen
679 * characters of name for matching the alias name.
680 */
681 const char *fdt_get_alias_namelen(const void *fdt,
682 const char *name, int namelen);
683
684 /**
685 * fdt_get_alias - retreive the path referenced by a given alias
686 * @fdt: pointer to the device tree blob
687 * @name: name of the alias th look up
688 *
689 * fdt_get_alias() retrieves the value of a given alias. That is, the
690 * value of the property named 'name' in the node /aliases.
691 *
692 * returns:
693 * a pointer to the expansion of the alias named 'name', if it exists
694 * NULL, if the given alias or the /aliases node does not exist
695 */
696 const char *fdt_get_alias(const void *fdt, const char *name);
697
698 /**
699 * fdt_get_path - determine the full path of a node
700 * @fdt: pointer to the device tree blob
701 * @nodeoffset: offset of the node whose path to find
702 * @buf: character buffer to contain the returned path (will be overwritten)
703 * @buflen: size of the character buffer at buf
704 *
705 * fdt_get_path() computes the full path of the node at offset
706 * nodeoffset, and records that path in the buffer at buf.
707 *
708 * NOTE: This function is expensive, as it must scan the device tree
709 * structure from the start to nodeoffset.
710 *
711 * returns:
712 * 0, on success
713 * buf contains the absolute path of the node at
714 * nodeoffset, as a NUL-terminated string.
715 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
716 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
717 * characters and will not fit in the given buffer.
718 * -FDT_ERR_BADMAGIC,
719 * -FDT_ERR_BADVERSION,
720 * -FDT_ERR_BADSTATE,
721 * -FDT_ERR_BADSTRUCTURE, standard meanings
722 */
723 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
724
725 /**
726 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
727 * @fdt: pointer to the device tree blob
728 * @nodeoffset: offset of the node whose parent to find
729 * @supernodedepth: depth of the ancestor to find
730 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
731 *
732 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
733 * at a specific depth from the root (where the root itself has depth
734 * 0, its immediate subnodes depth 1 and so forth). So
735 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
736 * will always return 0, the offset of the root node. If the node at
737 * nodeoffset has depth D, then:
738 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
739 * will return nodeoffset itself.
740 *
741 * NOTE: This function is expensive, as it must scan the device tree
742 * structure from the start to nodeoffset.
743 *
744 * returns:
745
746 * structure block offset of the node at node offset's ancestor
747 * of depth supernodedepth (>=0), on success
748 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
749 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
750 * -FDT_ERR_BADMAGIC,
751 * -FDT_ERR_BADVERSION,
752 * -FDT_ERR_BADSTATE,
753 * -FDT_ERR_BADSTRUCTURE, standard meanings
754 */
755 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
756 int supernodedepth, int *nodedepth);
757
758 /**
759 * fdt_node_depth - find the depth of a given node
760 * @fdt: pointer to the device tree blob
761 * @nodeoffset: offset of the node whose parent to find
762 *
763 * fdt_node_depth() finds the depth of a given node. The root node
764 * has depth 0, its immediate subnodes depth 1 and so forth.
765 *
766 * NOTE: This function is expensive, as it must scan the device tree
767 * structure from the start to nodeoffset.
768 *
769 * returns:
770 * depth of the node at nodeoffset (>=0), on success
771 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
772 * -FDT_ERR_BADMAGIC,
773 * -FDT_ERR_BADVERSION,
774 * -FDT_ERR_BADSTATE,
775 * -FDT_ERR_BADSTRUCTURE, standard meanings
776 */
777 int fdt_node_depth(const void *fdt, int nodeoffset);
778
779 /**
780 * fdt_parent_offset - find the parent of a given node
781 * @fdt: pointer to the device tree blob
782 * @nodeoffset: offset of the node whose parent to find
783 *
784 * fdt_parent_offset() locates the parent node of a given node (that
785 * is, it finds the offset of the node which contains the node at
786 * nodeoffset as a subnode).
787 *
788 * NOTE: This function is expensive, as it must scan the device tree
789 * structure from the start to nodeoffset, *twice*.
790 *
791 * returns:
792 * structure block offset of the parent of the node at nodeoffset
793 * (>=0), on success
794 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
795 * -FDT_ERR_BADMAGIC,
796 * -FDT_ERR_BADVERSION,
797 * -FDT_ERR_BADSTATE,
798 * -FDT_ERR_BADSTRUCTURE, standard meanings
799 */
800 int fdt_parent_offset(const void *fdt, int nodeoffset);
801
802 /**
803 * fdt_node_offset_by_prop_value - find nodes with a given property value
804 * @fdt: pointer to the device tree blob
805 * @startoffset: only find nodes after this offset
806 * @propname: property name to check
807 * @propval: property value to search for
808 * @proplen: length of the value in propval
809 *
810 * fdt_node_offset_by_prop_value() returns the offset of the first
811 * node after startoffset, which has a property named propname whose
812 * value is of length proplen and has value equal to propval; or if
813 * startoffset is -1, the 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_prop_value(fdt, -1, propname,
818 * propval, proplen);
819 * while (offset != -FDT_ERR_NOTFOUND) {
820 * // other code here
821 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
822 * propval, proplen);
823 * }
824 *
825 * Note the -1 in the first call to the function, if 0 is used here
826 * instead, the function will never locate the root node, even if it
827 * matches the criterion.
828 *
829 * returns:
830 * structure block offset of the located node (>= 0, >startoffset),
831 * on success
832 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
833 * tree after startoffset
834 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
835 * -FDT_ERR_BADMAGIC,
836 * -FDT_ERR_BADVERSION,
837 * -FDT_ERR_BADSTATE,
838 * -FDT_ERR_BADSTRUCTURE, standard meanings
839 */
840 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
841 const char *propname,
842 const void *propval, int proplen);
843
844 /**
845 * fdt_node_offset_by_phandle - find the node with a given phandle
846 * @fdt: pointer to the device tree blob
847 * @phandle: phandle value
848 *
849 * fdt_node_offset_by_phandle() returns the offset of the node
850 * which has the given phandle value. If there is more than one node
851 * in the tree with the given phandle (an invalid tree), results are
852 * undefined.
853 *
854 * returns:
855 * structure block offset of the located node (>= 0), on success
856 * -FDT_ERR_NOTFOUND, no node with that phandle exists
857 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
858 * -FDT_ERR_BADMAGIC,
859 * -FDT_ERR_BADVERSION,
860 * -FDT_ERR_BADSTATE,
861 * -FDT_ERR_BADSTRUCTURE, standard meanings
862 */
863 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
864
865 /**
866 * fdt_node_check_compatible: check a node's compatible property
867 * @fdt: pointer to the device tree blob
868 * @nodeoffset: offset of a tree node
869 * @compatible: string to match against
870 *
871 *
872 * fdt_node_check_compatible() returns 0 if the given node contains a
873 * 'compatible' property with the given string as one of its elements,
874 * it returns non-zero otherwise, or on error.
875 *
876 * returns:
877 * 0, if the node has a 'compatible' property listing the given string
878 * 1, if the node has a 'compatible' property, but it does not list
879 * the given string
880 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
881 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
882 * -FDT_ERR_BADMAGIC,
883 * -FDT_ERR_BADVERSION,
884 * -FDT_ERR_BADSTATE,
885 * -FDT_ERR_BADSTRUCTURE, standard meanings
886 */
887 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
888 const char *compatible);
889
890 /**
891 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
892 * @fdt: pointer to the device tree blob
893 * @startoffset: only find nodes after this offset
894 * @compatible: 'compatible' string to match against
895 *
896 * fdt_node_offset_by_compatible() returns the offset of the first
897 * node after startoffset, which has a 'compatible' property which
898 * lists the given compatible string; or if startoffset is -1, the
899 * very first such node in the tree.
900 *
901 * To iterate through all nodes matching the criterion, the following
902 * idiom can be used:
903 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
904 * while (offset != -FDT_ERR_NOTFOUND) {
905 * // other code here
906 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
907 * }
908 *
909 * Note the -1 in the first call to the function, if 0 is used here
910 * instead, the function will never locate the root node, even if it
911 * matches the criterion.
912 *
913 * returns:
914 * structure block offset of the located node (>= 0, >startoffset),
915 * on success
916 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
917 * tree after startoffset
918 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
919 * -FDT_ERR_BADMAGIC,
920 * -FDT_ERR_BADVERSION,
921 * -FDT_ERR_BADSTATE,
922 * -FDT_ERR_BADSTRUCTURE, standard meanings
923 */
924 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
925 const char *compatible);
926
927 /**
928 * fdt_stringlist_contains - check a string list property for a string
929 * @strlist: Property containing a list of strings to check
930 * @listlen: Length of property
931 * @str: String to search for
932 *
933 * This is a utility function provided for convenience. The list contains
934 * one or more strings, each terminated by \0, as is found in a device tree
935 * "compatible" property.
936 *
937 * @return: 1 if the string is found in the list, 0 not found, or invalid list
938 */
939 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
940
941 /**
942 * fdt_count_strings - count the number of strings in a string list
943 * @fdt: pointer to the device tree blob
944 * @node: offset of the node
945 * @property: name of the property containing the string list
946 * @return: the number of strings in the given property
947 */
948 int fdt_count_strings(const void *fdt, int node, const char *property);
949
950 /**
951 * fdt_find_string - find a string in a string list and return its index
952 * @fdt: pointer to the device tree blob
953 * @node: offset of the node
954 * @property: name of the property containing the string list
955 * @string: string to look up in the string list
956 * @return: the index of the string or negative on error
957 */
958 int fdt_find_string(const void *fdt, int node, const char *property,
959 const char *string);
960
961 /**
962 * fdt_get_string_index() - obtain the string at a given index in a string list
963 * @fdt: pointer to the device tree blob
964 * @node: offset of the node
965 * @property: name of the property containing the string list
966 * @index: index of the string to return
967 * @output: return location for the string
968 * @return: 0 if the string was found or a negative error code otherwise
969 */
970 int fdt_get_string_index(const void *fdt, int node, const char *property,
971 int index, const char **output);
972
973 /**
974 * fdt_get_string() - obtain the first string in a string list
975 * @fdt: pointer to the device tree blob
976 * @node: offset of the node
977 * @property: name of the property containing the string list
978 * @output: return location for the string
979 * @return: 0 if the string was found or a negative error code otherwise
980 *
981 * This is a shortcut for:
982 *
983 * fdt_get_string_index(fdt, node, property, 0, output).
984 */
985 int fdt_get_string(const void *fdt, int node, const char *property,
986 const char **output);
987
988 /**********************************************************************/
989 /* Read-only functions (addressing related) */
990 /**********************************************************************/
991
992 /**
993 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
994 *
995 * This is the maximum value for #address-cells, #size-cells and
996 * similar properties that will be processed by libfdt. IEE1275
997 * requires that OF implementations handle values up to 4.
998 * Implementations may support larger values, but in practice higher
999 * values aren't used.
1000 */
1001 #define FDT_MAX_NCELLS 4
1002
1003 /**
1004 * fdt_address_cells - retrieve address size for a bus represented in the tree
1005 * @fdt: pointer to the device tree blob
1006 * @nodeoffset: offset of the node to find the address size for
1007 *
1008 * When the node has a valid #address-cells property, returns its value.
1009 *
1010 * returns:
1011 * 0 <= n < FDT_MAX_NCELLS, on success
1012 * 2, if the node has no #address-cells property
1013 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1014 * #address-cells property
1015 * -FDT_ERR_BADMAGIC,
1016 * -FDT_ERR_BADVERSION,
1017 * -FDT_ERR_BADSTATE,
1018 * -FDT_ERR_BADSTRUCTURE,
1019 * -FDT_ERR_TRUNCATED, standard meanings
1020 */
1021 int fdt_address_cells(const void *fdt, int nodeoffset);
1022
1023 /**
1024 * fdt_size_cells - retrieve address range size for a bus represented in the
1025 * tree
1026 * @fdt: pointer to the device tree blob
1027 * @nodeoffset: offset of the node to find the address range size for
1028 *
1029 * When the node has a valid #size-cells property, returns its value.
1030 *
1031 * returns:
1032 * 0 <= n < FDT_MAX_NCELLS, on success
1033 * 2, if the node has no #address-cells property
1034 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1035 * #size-cells property
1036 * -FDT_ERR_BADMAGIC,
1037 * -FDT_ERR_BADVERSION,
1038 * -FDT_ERR_BADSTATE,
1039 * -FDT_ERR_BADSTRUCTURE,
1040 * -FDT_ERR_TRUNCATED, standard meanings
1041 */
1042 int fdt_size_cells(const void *fdt, int nodeoffset);
1043
1044
1045 /**********************************************************************/
1046 /* Write-in-place functions */
1047 /**********************************************************************/
1048
1049 /**
1050 * fdt_setprop_inplace - change a property's value, but not its size
1051 * @fdt: pointer to the device tree blob
1052 * @nodeoffset: offset of the node whose property to change
1053 * @name: name of the property to change
1054 * @val: pointer to data to replace the property value with
1055 * @len: length of the property value
1056 *
1057 * fdt_setprop_inplace() replaces the value of a given property with
1058 * the data in val, of length len. This function cannot change the
1059 * size of a property, and so will only work if len is equal to the
1060 * current length of the property.
1061 *
1062 * This function will alter only the bytes in the blob which contain
1063 * the given property value, and will not alter or move any other part
1064 * of the tree.
1065 *
1066 * returns:
1067 * 0, on success
1068 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1069 * -FDT_ERR_NOTFOUND, node does not have the named property
1070 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1071 * -FDT_ERR_BADMAGIC,
1072 * -FDT_ERR_BADVERSION,
1073 * -FDT_ERR_BADSTATE,
1074 * -FDT_ERR_BADSTRUCTURE,
1075 * -FDT_ERR_TRUNCATED, standard meanings
1076 */
1077 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1078 const void *val, int len);
1079
1080 /**
1081 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
1082 * @fdt: pointer to the device tree blob
1083 * @nodeoffset: offset of the node whose property to change
1084 * @name: name of the property to change
1085 * @val: 32-bit integer value to replace the property with
1086 *
1087 * fdt_setprop_inplace_u32() replaces the value of a given property
1088 * with the 32-bit integer value in val, converting val to big-endian
1089 * if necessary. This function cannot change the size of a property,
1090 * and so will only work if the property already exists and has length
1091 * 4.
1092 *
1093 * This function will alter only the bytes in the blob which contain
1094 * the given property value, and will not alter or move any other part
1095 * of the tree.
1096 *
1097 * returns:
1098 * 0, on success
1099 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
1100 * -FDT_ERR_NOTFOUND, node does not have the named property
1101 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1102 * -FDT_ERR_BADMAGIC,
1103 * -FDT_ERR_BADVERSION,
1104 * -FDT_ERR_BADSTATE,
1105 * -FDT_ERR_BADSTRUCTURE,
1106 * -FDT_ERR_TRUNCATED, standard meanings
1107 */
1108 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1109 const char *name, uint32_t val)
1110 {
1111 fdt32_t tmp = cpu_to_fdt32(val);
1112 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1113 }
1114
1115 /**
1116 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1117 * @fdt: pointer to the device tree blob
1118 * @nodeoffset: offset of the node whose property to change
1119 * @name: name of the property to change
1120 * @val: 64-bit integer value to replace the property with
1121 *
1122 * fdt_setprop_inplace_u64() replaces the value of a given property
1123 * with the 64-bit integer value in val, converting val to big-endian
1124 * if necessary. This function cannot change the size of a property,
1125 * and so will only work if the property already exists and has length
1126 * 8.
1127 *
1128 * This function will alter only the bytes in the blob which contain
1129 * the given property value, and will not alter or move any other part
1130 * of the tree.
1131 *
1132 * returns:
1133 * 0, on success
1134 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1135 * -FDT_ERR_NOTFOUND, node does not have the named property
1136 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1137 * -FDT_ERR_BADMAGIC,
1138 * -FDT_ERR_BADVERSION,
1139 * -FDT_ERR_BADSTATE,
1140 * -FDT_ERR_BADSTRUCTURE,
1141 * -FDT_ERR_TRUNCATED, standard meanings
1142 */
1143 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1144 const char *name, uint64_t val)
1145 {
1146 fdt64_t tmp = cpu_to_fdt64(val);
1147 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1148 }
1149
1150 /**
1151 * fdt_setprop_inplace_cell - change the value of a single-cell property
1152 *
1153 * This is an alternative name for fdt_setprop_inplace_u32()
1154 */
1155 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1156 const char *name, uint32_t val)
1157 {
1158 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1159 }
1160
1161 /**
1162 * fdt_nop_property - replace a property with nop tags
1163 * @fdt: pointer to the device tree blob
1164 * @nodeoffset: offset of the node whose property to nop
1165 * @name: name of the property to nop
1166 *
1167 * fdt_nop_property() will replace a given property's representation
1168 * in the blob with FDT_NOP tags, effectively removing it from the
1169 * tree.
1170 *
1171 * This function will alter only the bytes in the blob which contain
1172 * the property, and will not alter or move any other part of the
1173 * tree.
1174 *
1175 * returns:
1176 * 0, on success
1177 * -FDT_ERR_NOTFOUND, node does not have the named property
1178 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1179 * -FDT_ERR_BADMAGIC,
1180 * -FDT_ERR_BADVERSION,
1181 * -FDT_ERR_BADSTATE,
1182 * -FDT_ERR_BADSTRUCTURE,
1183 * -FDT_ERR_TRUNCATED, standard meanings
1184 */
1185 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1186
1187 /**
1188 * fdt_nop_node - replace a node (subtree) with nop tags
1189 * @fdt: pointer to the device tree blob
1190 * @nodeoffset: offset of the node to nop
1191 *
1192 * fdt_nop_node() will replace a given node's representation in the
1193 * blob, including all its subnodes, if any, with FDT_NOP tags,
1194 * effectively removing it from the tree.
1195 *
1196 * This function will alter only the bytes in the blob which contain
1197 * the node and its properties and subnodes, and will not alter or
1198 * move any other part of the tree.
1199 *
1200 * returns:
1201 * 0, on success
1202 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1203 * -FDT_ERR_BADMAGIC,
1204 * -FDT_ERR_BADVERSION,
1205 * -FDT_ERR_BADSTATE,
1206 * -FDT_ERR_BADSTRUCTURE,
1207 * -FDT_ERR_TRUNCATED, standard meanings
1208 */
1209 int fdt_nop_node(void *fdt, int nodeoffset);
1210
1211 /**********************************************************************/
1212 /* Sequential write functions */
1213 /**********************************************************************/
1214
1215 int fdt_create(void *buf, int bufsize);
1216 int fdt_resize(void *fdt, void *buf, int bufsize);
1217 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1218 int fdt_finish_reservemap(void *fdt);
1219 int fdt_begin_node(void *fdt, const char *name);
1220 int fdt_property(void *fdt, const char *name, const void *val, int len);
1221 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1222 {
1223 fdt32_t tmp = cpu_to_fdt32(val);
1224 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1225 }
1226 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1227 {
1228 fdt64_t tmp = cpu_to_fdt64(val);
1229 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1230 }
1231 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1232 {
1233 return fdt_property_u32(fdt, name, val);
1234 }
1235
1236 /**
1237 * fdt_property_placeholder - add a new property and return a ptr to its value
1238 *
1239 * @fdt: pointer to the device tree blob
1240 * @name: name of property to add
1241 * @len: length of property value in bytes
1242 * @valp: returns a pointer to where where the value should be placed
1243 *
1244 * returns:
1245 * 0, on success
1246 * -FDT_ERR_BADMAGIC,
1247 * -FDT_ERR_NOSPACE, standard meanings
1248 */
1249 int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
1250
1251 #define fdt_property_string(fdt, name, str) \
1252 fdt_property(fdt, name, str, strlen(str)+1)
1253 int fdt_end_node(void *fdt);
1254 int fdt_finish(void *fdt);
1255
1256 /**********************************************************************/
1257 /* Read-write functions */
1258 /**********************************************************************/
1259
1260 int fdt_create_empty_tree(void *buf, int bufsize);
1261 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1262 int fdt_pack(void *fdt);
1263
1264 /**
1265 * fdt_add_mem_rsv - add one memory reserve map entry
1266 * @fdt: pointer to the device tree blob
1267 * @address, @size: 64-bit values (native endian)
1268 *
1269 * Adds a reserve map entry to the given blob reserving a region at
1270 * address address of length size.
1271 *
1272 * This function will insert data into the reserve map and will
1273 * therefore change the indexes of some entries in the table.
1274 *
1275 * returns:
1276 * 0, on success
1277 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1278 * contain the new reservation entry
1279 * -FDT_ERR_BADMAGIC,
1280 * -FDT_ERR_BADVERSION,
1281 * -FDT_ERR_BADSTATE,
1282 * -FDT_ERR_BADSTRUCTURE,
1283 * -FDT_ERR_BADLAYOUT,
1284 * -FDT_ERR_TRUNCATED, standard meanings
1285 */
1286 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1287
1288 /**
1289 * fdt_del_mem_rsv - remove a memory reserve map entry
1290 * @fdt: pointer to the device tree blob
1291 * @n: entry to remove
1292 *
1293 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1294 * the blob.
1295 *
1296 * This function will delete data from the reservation table and will
1297 * therefore change the indexes of some entries in the table.
1298 *
1299 * returns:
1300 * 0, on success
1301 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1302 * are less than n+1 reserve map entries)
1303 * -FDT_ERR_BADMAGIC,
1304 * -FDT_ERR_BADVERSION,
1305 * -FDT_ERR_BADSTATE,
1306 * -FDT_ERR_BADSTRUCTURE,
1307 * -FDT_ERR_BADLAYOUT,
1308 * -FDT_ERR_TRUNCATED, standard meanings
1309 */
1310 int fdt_del_mem_rsv(void *fdt, int n);
1311
1312 /**
1313 * fdt_set_name - change the name of a given node
1314 * @fdt: pointer to the device tree blob
1315 * @nodeoffset: structure block offset of a node
1316 * @name: name to give the node
1317 *
1318 * fdt_set_name() replaces the name (including unit address, if any)
1319 * of the given node with the given string. NOTE: this function can't
1320 * efficiently check if the new name is unique amongst the given
1321 * node's siblings; results are undefined if this function is invoked
1322 * with a name equal to one of the given node's siblings.
1323 *
1324 * This function may insert or delete data from the blob, and will
1325 * therefore change the offsets of some existing nodes.
1326 *
1327 * returns:
1328 * 0, on success
1329 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1330 * to contain the new name
1331 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1332 * -FDT_ERR_BADMAGIC,
1333 * -FDT_ERR_BADVERSION,
1334 * -FDT_ERR_BADSTATE, standard meanings
1335 */
1336 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1337
1338 /**
1339 * fdt_setprop - create or change a property
1340 * @fdt: pointer to the device tree blob
1341 * @nodeoffset: offset of the node whose property to change
1342 * @name: name of the property to change
1343 * @val: pointer to data to set the property value to
1344 * @len: length of the property value
1345 *
1346 * fdt_setprop() sets the value of the named property in the given
1347 * node to the given value and length, creating the property if it
1348 * does not already exist.
1349 *
1350 * This function may insert or delete data from the blob, and will
1351 * therefore change the offsets of some existing nodes.
1352 *
1353 * returns:
1354 * 0, on success
1355 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1356 * contain the new property value
1357 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1358 * -FDT_ERR_BADLAYOUT,
1359 * -FDT_ERR_BADMAGIC,
1360 * -FDT_ERR_BADVERSION,
1361 * -FDT_ERR_BADSTATE,
1362 * -FDT_ERR_BADSTRUCTURE,
1363 * -FDT_ERR_BADLAYOUT,
1364 * -FDT_ERR_TRUNCATED, standard meanings
1365 */
1366 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1367 const void *val, int len);
1368
1369 /**
1370 * fdt_setprop_u32 - set a property to a 32-bit integer
1371 * @fdt: pointer to the device tree blob
1372 * @nodeoffset: offset of the node whose property to change
1373 * @name: name of the property to change
1374 * @val: 32-bit integer value for the property (native endian)
1375 *
1376 * fdt_setprop_u32() sets the value of the named property in the given
1377 * node to the given 32-bit integer value (converting to big-endian if
1378 * necessary), or creates a new property with that value if it does
1379 * not already exist.
1380 *
1381 * This function may insert or delete data from the blob, and will
1382 * therefore change the offsets of some existing nodes.
1383 *
1384 * returns:
1385 * 0, on success
1386 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1387 * contain the new property value
1388 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1389 * -FDT_ERR_BADLAYOUT,
1390 * -FDT_ERR_BADMAGIC,
1391 * -FDT_ERR_BADVERSION,
1392 * -FDT_ERR_BADSTATE,
1393 * -FDT_ERR_BADSTRUCTURE,
1394 * -FDT_ERR_BADLAYOUT,
1395 * -FDT_ERR_TRUNCATED, standard meanings
1396 */
1397 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1398 uint32_t val)
1399 {
1400 fdt32_t tmp = cpu_to_fdt32(val);
1401 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1402 }
1403
1404 /**
1405 * fdt_setprop_u64 - set a property to a 64-bit integer
1406 * @fdt: pointer to the device tree blob
1407 * @nodeoffset: offset of the node whose property to change
1408 * @name: name of the property to change
1409 * @val: 64-bit integer value for the property (native endian)
1410 *
1411 * fdt_setprop_u64() sets the value of the named property in the given
1412 * node to the given 64-bit integer value (converting to big-endian if
1413 * necessary), or creates a new property with that value if it does
1414 * not already exist.
1415 *
1416 * This function may insert or delete data from the blob, and will
1417 * therefore change the offsets of some existing nodes.
1418 *
1419 * returns:
1420 * 0, on success
1421 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1422 * contain the new property value
1423 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1424 * -FDT_ERR_BADLAYOUT,
1425 * -FDT_ERR_BADMAGIC,
1426 * -FDT_ERR_BADVERSION,
1427 * -FDT_ERR_BADSTATE,
1428 * -FDT_ERR_BADSTRUCTURE,
1429 * -FDT_ERR_BADLAYOUT,
1430 * -FDT_ERR_TRUNCATED, standard meanings
1431 */
1432 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1433 uint64_t val)
1434 {
1435 fdt64_t tmp = cpu_to_fdt64(val);
1436 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1437 }
1438
1439 /**
1440 * fdt_setprop_cell - set a property to a single cell value
1441 *
1442 * This is an alternative name for fdt_setprop_u32()
1443 */
1444 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1445 uint32_t val)
1446 {
1447 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1448 }
1449
1450 /**
1451 * fdt_setprop_string - set a property to a string value
1452 * @fdt: pointer to the device tree blob
1453 * @nodeoffset: offset of the node whose property to change
1454 * @name: name of the property to change
1455 * @str: string value for the property
1456 *
1457 * fdt_setprop_string() sets the value of the named property in the
1458 * given node to the given string value (using the length of the
1459 * string to determine the new length of the property), or creates a
1460 * new property with that value if it does not already exist.
1461 *
1462 * This function may insert or delete data from the blob, and will
1463 * therefore change the offsets of some existing nodes.
1464 *
1465 * returns:
1466 * 0, on success
1467 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1468 * contain the new property value
1469 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1470 * -FDT_ERR_BADLAYOUT,
1471 * -FDT_ERR_BADMAGIC,
1472 * -FDT_ERR_BADVERSION,
1473 * -FDT_ERR_BADSTATE,
1474 * -FDT_ERR_BADSTRUCTURE,
1475 * -FDT_ERR_BADLAYOUT,
1476 * -FDT_ERR_TRUNCATED, standard meanings
1477 */
1478 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
1479 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1480
1481 /**
1482 * fdt_appendprop - append to or create a property
1483 * @fdt: pointer to the device tree blob
1484 * @nodeoffset: offset of the node whose property to change
1485 * @name: name of the property to append to
1486 * @val: pointer to data to append to the property value
1487 * @len: length of the data to append to the property value
1488 *
1489 * fdt_appendprop() appends the value to the named property in the
1490 * given node, creating the property if it does not already exist.
1491 *
1492 * This function may insert data into the blob, and will therefore
1493 * change the offsets of some existing nodes.
1494 *
1495 * returns:
1496 * 0, on success
1497 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1498 * contain the new property value
1499 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1500 * -FDT_ERR_BADLAYOUT,
1501 * -FDT_ERR_BADMAGIC,
1502 * -FDT_ERR_BADVERSION,
1503 * -FDT_ERR_BADSTATE,
1504 * -FDT_ERR_BADSTRUCTURE,
1505 * -FDT_ERR_BADLAYOUT,
1506 * -FDT_ERR_TRUNCATED, standard meanings
1507 */
1508 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1509 const void *val, int len);
1510
1511 /**
1512 * fdt_appendprop_u32 - append a 32-bit integer value to a property
1513 * @fdt: pointer to the device tree blob
1514 * @nodeoffset: offset of the node whose property to change
1515 * @name: name of the property to change
1516 * @val: 32-bit integer value to append to the property (native endian)
1517 *
1518 * fdt_appendprop_u32() appends the given 32-bit integer value
1519 * (converting to big-endian if necessary) to the value of the named
1520 * property in the given node, or creates a new property with that
1521 * value if it does not already exist.
1522 *
1523 * This function may insert data into the blob, and will therefore
1524 * change the offsets of some existing nodes.
1525 *
1526 * returns:
1527 * 0, on success
1528 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1529 * contain the new property value
1530 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1531 * -FDT_ERR_BADLAYOUT,
1532 * -FDT_ERR_BADMAGIC,
1533 * -FDT_ERR_BADVERSION,
1534 * -FDT_ERR_BADSTATE,
1535 * -FDT_ERR_BADSTRUCTURE,
1536 * -FDT_ERR_BADLAYOUT,
1537 * -FDT_ERR_TRUNCATED, standard meanings
1538 */
1539 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1540 const char *name, uint32_t val)
1541 {
1542 fdt32_t tmp = cpu_to_fdt32(val);
1543 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1544 }
1545
1546 /**
1547 * fdt_appendprop_u64 - append a 64-bit integer value to a property
1548 * @fdt: pointer to the device tree blob
1549 * @nodeoffset: offset of the node whose property to change
1550 * @name: name of the property to change
1551 * @val: 64-bit integer value to append to the property (native endian)
1552 *
1553 * fdt_appendprop_u64() appends the given 64-bit integer value
1554 * (converting to big-endian if necessary) to the value of the named
1555 * property in the given node, or creates a new property with that
1556 * value if it does not already exist.
1557 *
1558 * This function may insert data into the blob, and will therefore
1559 * change the offsets of some existing nodes.
1560 *
1561 * returns:
1562 * 0, on success
1563 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1564 * contain the new property value
1565 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1566 * -FDT_ERR_BADLAYOUT,
1567 * -FDT_ERR_BADMAGIC,
1568 * -FDT_ERR_BADVERSION,
1569 * -FDT_ERR_BADSTATE,
1570 * -FDT_ERR_BADSTRUCTURE,
1571 * -FDT_ERR_BADLAYOUT,
1572 * -FDT_ERR_TRUNCATED, standard meanings
1573 */
1574 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1575 const char *name, uint64_t val)
1576 {
1577 fdt64_t tmp = cpu_to_fdt64(val);
1578 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1579 }
1580
1581 /**
1582 * fdt_appendprop_cell - append a single cell value to a property
1583 *
1584 * This is an alternative name for fdt_appendprop_u32()
1585 */
1586 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1587 const char *name, uint32_t val)
1588 {
1589 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1590 }
1591
1592 /**
1593 * fdt_appendprop_string - append a string to a property
1594 * @fdt: pointer to the device tree blob
1595 * @nodeoffset: offset of the node whose property to change
1596 * @name: name of the property to change
1597 * @str: string value to append to the property
1598 *
1599 * fdt_appendprop_string() appends the given string to the value of
1600 * the named property in the given node, or creates a new property
1601 * with that value if it does not already exist.
1602 *
1603 * This function may insert data into the blob, and will therefore
1604 * change the offsets of some existing nodes.
1605 *
1606 * returns:
1607 * 0, on success
1608 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1609 * contain the new property value
1610 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1611 * -FDT_ERR_BADLAYOUT,
1612 * -FDT_ERR_BADMAGIC,
1613 * -FDT_ERR_BADVERSION,
1614 * -FDT_ERR_BADSTATE,
1615 * -FDT_ERR_BADSTRUCTURE,
1616 * -FDT_ERR_BADLAYOUT,
1617 * -FDT_ERR_TRUNCATED, standard meanings
1618 */
1619 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1620 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1621
1622 /**
1623 * fdt_delprop - delete a property
1624 * @fdt: pointer to the device tree blob
1625 * @nodeoffset: offset of the node whose property to nop
1626 * @name: name of the property to nop
1627 *
1628 * fdt_del_property() will delete the given property.
1629 *
1630 * This function will delete data from the blob, and will therefore
1631 * change the offsets of some existing nodes.
1632 *
1633 * returns:
1634 * 0, on success
1635 * -FDT_ERR_NOTFOUND, node does not have the named property
1636 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1637 * -FDT_ERR_BADLAYOUT,
1638 * -FDT_ERR_BADMAGIC,
1639 * -FDT_ERR_BADVERSION,
1640 * -FDT_ERR_BADSTATE,
1641 * -FDT_ERR_BADSTRUCTURE,
1642 * -FDT_ERR_TRUNCATED, standard meanings
1643 */
1644 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
1645
1646 /**
1647 * fdt_add_subnode_namelen - creates a new node based on substring
1648 * @fdt: pointer to the device tree blob
1649 * @parentoffset: structure block offset of a node
1650 * @name: name of the subnode to locate
1651 * @namelen: number of characters of name to consider
1652 *
1653 * Identical to fdt_add_subnode(), but use only the first namelen
1654 * characters of name as the name of the new node. This is useful for
1655 * creating subnodes based on a portion of a larger string, such as a
1656 * full path.
1657 */
1658 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
1659 const char *name, int namelen);
1660
1661 /**
1662 * fdt_add_subnode - creates a new node
1663 * @fdt: pointer to the device tree blob
1664 * @parentoffset: structure block offset of a node
1665 * @name: name of the subnode to locate
1666 *
1667 * fdt_add_subnode() creates a new node as a subnode of the node at
1668 * structure block offset parentoffset, with the given name (which
1669 * should include the unit address, if any).
1670 *
1671 * This function will insert data into the blob, and will therefore
1672 * change the offsets of some existing nodes.
1673
1674 * returns:
1675 * structure block offset of the created nodeequested subnode (>=0), on success
1676 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
1677 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
1678 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
1679 * the given name
1680 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
1681 * blob to contain the new node
1682 * -FDT_ERR_NOSPACE
1683 * -FDT_ERR_BADLAYOUT
1684 * -FDT_ERR_BADMAGIC,
1685 * -FDT_ERR_BADVERSION,
1686 * -FDT_ERR_BADSTATE,
1687 * -FDT_ERR_BADSTRUCTURE,
1688 * -FDT_ERR_TRUNCATED, standard meanings.
1689 */
1690 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
1691
1692 /**
1693 * fdt_del_node - delete a node (subtree)
1694 * @fdt: pointer to the device tree blob
1695 * @nodeoffset: offset of the node to nop
1696 *
1697 * fdt_del_node() will remove the given node, including all its
1698 * subnodes if any, from the blob.
1699 *
1700 * This function will delete data from the blob, and will therefore
1701 * change the offsets of some existing nodes.
1702 *
1703 * returns:
1704 * 0, on success
1705 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1706 * -FDT_ERR_BADLAYOUT,
1707 * -FDT_ERR_BADMAGIC,
1708 * -FDT_ERR_BADVERSION,
1709 * -FDT_ERR_BADSTATE,
1710 * -FDT_ERR_BADSTRUCTURE,
1711 * -FDT_ERR_TRUNCATED, standard meanings
1712 */
1713 int fdt_del_node(void *fdt, int nodeoffset);
1714
1715 /**********************************************************************/
1716 /* Debugging / informational functions */
1717 /**********************************************************************/
1718
1719 const char *fdt_strerror(int errval);
1720
1721 /**
1722 * fdt_remove_unused_strings() - Remove any unused strings from an FDT
1723 *
1724 * This creates a new device tree in @new with unused strings removed. The
1725 * called can then use fdt_pack() to minimise the space consumed.
1726 *
1727 * @old: Old device tree blog
1728 * @new: Place to put new device tree blob, which must be as large as
1729 * @old
1730 * @return
1731 * 0, on success
1732 * -FDT_ERR_BADOFFSET, corrupt device tree
1733 * -FDT_ERR_NOSPACE, out of space, which should not happen unless there
1734 * is something very wrong with the device tree input
1735 */
1736 int fdt_remove_unused_strings(const void *old, void *new);
1737
1738 struct fdt_region {
1739 int offset;
1740 int size;
1741 };
1742
1743 /*
1744 * Flags for fdt_find_regions()
1745 *
1746 * Add a region for the string table (always the last region)
1747 */
1748 #define FDT_REG_ADD_STRING_TAB (1 << 0)
1749
1750 /*
1751 * Add all supernodes of a matching node/property, useful for creating a
1752 * valid subset tree
1753 */
1754 #define FDT_REG_SUPERNODES (1 << 1)
1755
1756 /* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
1757 #define FDT_REG_DIRECT_SUBNODES (1 << 2)
1758
1759 /* Add all subnodes of a matching node */
1760 #define FDT_REG_ALL_SUBNODES (1 << 3)
1761
1762 /* Add a region for the mem_rsvmap table (always the first region) */
1763 #define FDT_REG_ADD_MEM_RSVMAP (1 << 4)
1764
1765 /* Indicates what an fdt part is (node, property, value) */
1766 #define FDT_IS_NODE (1 << 0)
1767 #define FDT_IS_PROP (1 << 1)
1768 #define FDT_IS_VALUE (1 << 2) /* not supported */
1769 #define FDT_IS_COMPAT (1 << 3) /* used internally */
1770 #define FDT_NODE_HAS_PROP (1 << 4) /* node contains prop */
1771
1772 #define FDT_ANY_GLOBAL (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
1773 FDT_IS_COMPAT)
1774 #define FDT_IS_ANY 0x1f /* all the above */
1775
1776 /* We set a reasonable limit on the number of nested nodes */
1777 #define FDT_MAX_DEPTH 32
1778
1779 /* Decribes what we want to include from the current tag */
1780 enum want_t {
1781 WANT_NOTHING,
1782 WANT_NODES_ONLY, /* No properties */
1783 WANT_NODES_AND_PROPS, /* Everything for one level */
1784 WANT_ALL_NODES_AND_PROPS /* Everything for all levels */
1785 };
1786
1787 /* Keeps track of the state at parent nodes */
1788 struct fdt_subnode_stack {
1789 int offset; /* Offset of node */
1790 enum want_t want; /* The 'want' value here */
1791 int included; /* 1 if we included this node, 0 if not */
1792 };
1793
1794 struct fdt_region_ptrs {
1795 int depth; /* Current tree depth */
1796 int done; /* What we have completed scanning */
1797 enum want_t want; /* What we are currently including */
1798 char *end; /* Pointer to end of full node path */
1799 int nextoffset; /* Next node offset to check */
1800 };
1801
1802 /* The state of our finding algortihm */
1803 struct fdt_region_state {
1804 struct fdt_subnode_stack stack[FDT_MAX_DEPTH]; /* node stack */
1805 struct fdt_region *region; /* Contains list of regions found */
1806 int count; /* Numnber of regions found */
1807 const void *fdt; /* FDT blob */
1808 int max_regions; /* Maximum regions to find */
1809 int can_merge; /* 1 if we can merge with previous region */
1810 int start; /* Start position of current region */
1811 struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */
1812 };
1813
1814 /**
1815 * fdt_find_regions() - find regions in device tree
1816 *
1817 * Given a list of nodes to include and properties to exclude, find
1818 * the regions of the device tree which describe those included parts.
1819 *
1820 * The intent is to get a list of regions which will be invariant provided
1821 * those parts are invariant. For example, if you request a list of regions
1822 * for all nodes but exclude the property "data", then you will get the
1823 * same region contents regardless of any change to "data" properties.
1824 *
1825 * This function can be used to produce a byte-stream to send to a hashing
1826 * function to verify that critical parts of the FDT have not changed.
1827 *
1828 * Nodes which are given in 'inc' are included in the region list, as
1829 * are the names of the immediate subnodes nodes (but not the properties
1830 * or subnodes of those subnodes).
1831 *
1832 * For eaxample "/" means to include the root node, all root properties
1833 * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
1834 * ensures that we capture the names of the subnodes. In a hashing situation
1835 * it prevents the root node from changing at all Any change to non-excluded
1836 * properties, names of subnodes or number of subnodes would be detected.
1837 *
1838 * When used with FITs this provides the ability to hash and sign parts of
1839 * the FIT based on different configurations in the FIT. Then it is
1840 * impossible to change anything about that configuration (include images
1841 * attached to the configuration), but it may be possible to add new
1842 * configurations, new images or new signatures within the existing
1843 * framework.
1844 *
1845 * Adding new properties to a device tree may result in the string table
1846 * being extended (if the new property names are different from those
1847 * already added). This function can optionally include a region for
1848 * the string table so that this can be part of the hash too.
1849 *
1850 * The device tree header is not included in the list.
1851 *
1852 * @fdt: Device tree to check
1853 * @inc: List of node paths to included
1854 * @inc_count: Number of node paths in list
1855 * @exc_prop: List of properties names to exclude
1856 * @exc_prop_count: Number of properties in exclude list
1857 * @region: Returns list of regions
1858 * @max_region: Maximum length of region list
1859 * @path: Pointer to a temporary string for the function to use for
1860 * building path names
1861 * @path_len: Length of path, must be large enough to hold the longest
1862 * path in the tree
1863 * @add_string_tab: 1 to add a region for the string table
1864 * @return number of regions in list. If this is >max_regions then the
1865 * region array was exhausted. You should increase max_regions and try
1866 * the call again.
1867 */
1868 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
1869 char * const exc_prop[], int exc_prop_count,
1870 struct fdt_region region[], int max_regions,
1871 char *path, int path_len, int add_string_tab);
1872
1873 /**
1874 * fdt_first_region() - find regions in device tree
1875 *
1876 * Given a nodes and properties to include and properties to exclude, find
1877 * the regions of the device tree which describe those included parts.
1878 *
1879 * The use for this function is twofold. Firstly it provides a convenient
1880 * way of performing a structure-aware grep of the tree. For example it is
1881 * possible to grep for a node and get all the properties associated with
1882 * that node. Trees can be subsetted easily, by specifying the nodes that
1883 * are required, and then writing out the regions returned by this function.
1884 * This is useful for small resource-constrained systems, such as boot
1885 * loaders, which want to use an FDT but do not need to know about all of
1886 * it.
1887 *
1888 * Secondly it makes it easy to hash parts of the tree and detect changes.
1889 * The intent is to get a list of regions which will be invariant provided
1890 * those parts are invariant. For example, if you request a list of regions
1891 * for all nodes but exclude the property "data", then you will get the
1892 * same region contents regardless of any change to "data" properties.
1893 *
1894 * This function can be used to produce a byte-stream to send to a hashing
1895 * function to verify that critical parts of the FDT have not changed.
1896 * Note that semantically null changes in order could still cause false
1897 * hash misses. Such reordering might happen if the tree is regenerated
1898 * from source, and nodes are reordered (the bytes-stream will be emitted
1899 * in a different order and mnay hash functions will detect this). However
1900 * if an existing tree is modified using libfdt functions, such as
1901 * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
1902 *
1903 * The nodes/properties to include/exclude are defined by a function
1904 * provided by the caller. This function is called for each node and
1905 * property, and must return:
1906 *
1907 * 0 - to exclude this part
1908 * 1 - to include this part
1909 * -1 - for FDT_IS_PROP only: no information is available, so include
1910 * if its containing node is included
1911 *
1912 * The last case is only used to deal with properties. Often a property is
1913 * included if its containing node is included - this is the case where
1914 * -1 is returned.. However if the property is specifically required to be
1915 * included/excluded, then 0 or 1 can be returned. Note that including a
1916 * property when the FDT_REG_SUPERNODES flag is given will force its
1917 * containing node to be included since it is not valid to have a property
1918 * that is not in a node.
1919 *
1920 * Using the information provided, the inclusion of a node can be controlled
1921 * either by a node name or its compatible string, or any other property
1922 * that the function can determine.
1923 *
1924 * As an example, including node "/" means to include the root node and all
1925 * root properties. A flag provides a way of also including supernodes (of
1926 * which there is none for the root node), and another flag includes
1927 * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
1928 * FDT_END_NODE of all subnodes of /.
1929 *
1930 * The subnode feature helps in a hashing situation since it prevents the
1931 * root node from changing at all. Any change to non-excluded properties,
1932 * names of subnodes or number of subnodes would be detected.
1933 *
1934 * When used with FITs this provides the ability to hash and sign parts of
1935 * the FIT based on different configurations in the FIT. Then it is
1936 * impossible to change anything about that configuration (include images
1937 * attached to the configuration), but it may be possible to add new
1938 * configurations, new images or new signatures within the existing
1939 * framework.
1940 *
1941 * Adding new properties to a device tree may result in the string table
1942 * being extended (if the new property names are different from those
1943 * already added). This function can optionally include a region for
1944 * the string table so that this can be part of the hash too. This is always
1945 * the last region.
1946 *
1947 * The FDT also has a mem_rsvmap table which can also be included, and is
1948 * always the first region if so.
1949 *
1950 * The device tree header is not included in the region list. Since the
1951 * contents of the FDT are changing (shrinking, often), the caller will need
1952 * to regenerate the header anyway.
1953 *
1954 * @fdt: Device tree to check
1955 * @h_include: Function to call to determine whether to include a part or
1956 * not:
1957 *
1958 * @priv: Private pointer as passed to fdt_find_regions()
1959 * @fdt: Pointer to FDT blob
1960 * @offset: Offset of this node / property
1961 * @type: Type of this part, FDT_IS_...
1962 * @data: Pointer to data (node name, property name, compatible
1963 * string, value (not yet supported)
1964 * @size: Size of data, or 0 if none
1965 * @return 0 to exclude, 1 to include, -1 if no information is
1966 * available
1967 * @priv: Private pointer passed to h_include
1968 * @region: Returns list of regions, sorted by offset
1969 * @max_regions: Maximum length of region list
1970 * @path: Pointer to a temporary string for the function to use for
1971 * building path names
1972 * @path_len: Length of path, must be large enough to hold the longest
1973 * path in the tree
1974 * @flags: Various flags that control the region algortihm, see
1975 * FDT_REG_...
1976 * @return number of regions in list. If this is >max_regions then the
1977 * region array was exhausted. You should increase max_regions and try
1978 * the call again. Only the first max_regions elements are available in the
1979 * array.
1980 *
1981 * On error a -ve value is return, which can be:
1982 *
1983 * -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
1984 * -FDT_ERR_BADLAYOUT
1985 * -FDT_ERR_NOSPACE (path area is too small)
1986 */
1987 int fdt_first_region(const void *fdt,
1988 int (*h_include)(void *priv, const void *fdt, int offset,
1989 int type, const char *data, int size),
1990 void *priv, struct fdt_region *region,
1991 char *path, int path_len, int flags,
1992 struct fdt_region_state *info);
1993
1994 /** fdt_next_region() - find next region
1995 *
1996 * See fdt_first_region() for full description. This function finds the
1997 * next region according to the provided parameters, which must be the same
1998 * as passed to fdt_first_region().
1999 *
2000 * This function can additionally return -FDT_ERR_NOTFOUND when there are no
2001 * more regions
2002 */
2003 int fdt_next_region(const void *fdt,
2004 int (*h_include)(void *priv, const void *fdt, int offset,
2005 int type, const char *data, int size),
2006 void *priv, struct fdt_region *region,
2007 char *path, int path_len, int flags,
2008 struct fdt_region_state *info);
2009
2010 /**
2011 * fdt_add_alias_regions() - find aliases that point to existing regions
2012 *
2013 * Once a device tree grep is complete some of the nodes will be present
2014 * and some will have been dropped. This function checks all the alias nodes
2015 * to figure out which points point to nodes which are still present. These
2016 * aliases need to be kept, along with the nodes they reference.
2017 *
2018 * Given a list of regions function finds the aliases that still apply and
2019 * adds more regions to the list for these. This function is called after
2020 * fdt_next_region() has finished returning regions and requires the same
2021 * state.
2022 *
2023 * @fdt: Device tree file to reference
2024 * @region: List of regions that will be kept
2025 * @count: Number of regions
2026 * @max_regions: Number of entries that can fit in @region
2027 * @info: Region state as returned from fdt_next_region()
2028 * @return new number of regions in @region (i.e. count + the number added)
2029 * or -FDT_ERR_NOSPACE if there was not enough space.
2030 */
2031 int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
2032 int max_regions, struct fdt_region_state *info);
2033
2034 #endif /* _LIBFDT_H */