1 /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
2 #ifndef LIBFDT_H
3 #define LIBFDT_H
4 /*
5 * libfdt - Flat Device Tree manipulation
6 * Copyright (C) 2006 David Gibson, IBM Corporation.
7 */
8
9 #include "libfdt_env.h"
10 #include "fdt.h"
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 #define FDT_FIRST_SUPPORTED_VERSION 0x02
17 #define FDT_LAST_COMPATIBLE_VERSION 0x10
18 #define FDT_LAST_SUPPORTED_VERSION 0x11
19
20 /* Error codes: informative error codes */
21 #define FDT_ERR_NOTFOUND 1
22 /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
23 #define FDT_ERR_EXISTS 2
24 /* FDT_ERR_EXISTS: Attempted to create a node or property which
25 * already exists */
26 #define FDT_ERR_NOSPACE 3
27 /* FDT_ERR_NOSPACE: Operation needed to expand the device
28 * tree, but its buffer did not have sufficient space to
29 * contain the expanded tree. Use fdt_open_into() to move the
30 * device tree to a buffer with more space. */
31
32 /* Error codes: codes for bad parameters */
33 #define FDT_ERR_BADOFFSET 4
34 /* FDT_ERR_BADOFFSET: Function was passed a structure block
35 * offset which is out-of-bounds, or which points to an
36 * unsuitable part of the structure for the operation. */
37 #define FDT_ERR_BADPATH 5
38 /* FDT_ERR_BADPATH: Function was passed a badly formatted path
39 * (e.g. missing a leading / for a function which requires an
40 * absolute path) */
41 #define FDT_ERR_BADPHANDLE 6
42 /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle.
43 * This can be caused either by an invalid phandle property
44 * length, or the phandle value was either 0 or -1, which are
45 * not permitted. */
46 #define FDT_ERR_BADSTATE 7
47 /* FDT_ERR_BADSTATE: Function was passed an incomplete device
48 * tree created by the sequential-write functions, which is
49 * not sufficiently complete for the requested operation. */
50
51 /* Error codes: codes for bad device tree blobs */
52 #define FDT_ERR_TRUNCATED 8
53 /* FDT_ERR_TRUNCATED: FDT or a sub-block is improperly
54 * terminated (overflows, goes outside allowed bounds, or
55 * isn't properly terminated). */
56 #define FDT_ERR_BADMAGIC 9
57 /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
58 * device tree at all - it is missing the flattened device
59 * tree magic number. */
60 #define FDT_ERR_BADVERSION 10
61 /* FDT_ERR_BADVERSION: Given device tree has a version which
62 * can't be handled by the requested operation. For
63 * read-write functions, this may mean that fdt_open_into() is
64 * required to convert the tree to the expected version. */
65 #define FDT_ERR_BADSTRUCTURE 11
66 /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
67 * structure block or other serious error (e.g. misnested
68 * nodes, or subnodes preceding properties). */
69 #define FDT_ERR_BADLAYOUT 12
70 /* FDT_ERR_BADLAYOUT: For read-write functions, the given
71 * device tree has it's sub-blocks in an order that the
72 * function can't handle (memory reserve map, then structure,
73 * then strings). Use fdt_open_into() to reorganize the tree
74 * into a form suitable for the read-write operations. */
75
76 /* "Can't happen" error indicating a bug in libfdt */
77 #define FDT_ERR_INTERNAL 13
78 /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
79 * Should never be returned, if it is, it indicates a bug in
80 * libfdt itself. */
81
82 /* Errors in device tree content */
83 #define FDT_ERR_BADNCELLS 14
84 /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
85 * or similar property with a bad format or value */
86
87 #define FDT_ERR_BADVALUE 15
88 /* FDT_ERR_BADVALUE: Device tree has a property with an unexpected
89 * value. For example: a property expected to contain a string list
90 * is not NUL-terminated within the length of its value. */
91
92 #define FDT_ERR_BADOVERLAY 16
93 /* FDT_ERR_BADOVERLAY: The device tree overlay, while
94 * correctly structured, cannot be applied due to some
95 * unexpected or missing value, property or node. */
96
97 #define FDT_ERR_NOPHANDLES 17
98 /* FDT_ERR_NOPHANDLES: The device tree doesn't have any
99 * phandle available anymore without causing an overflow */
100
101 #define FDT_ERR_BADFLAGS 18
102 /* FDT_ERR_BADFLAGS: The function was passed a flags field that
103 * contains invalid flags or an invalid combination of flags. */
104
105 #define FDT_ERR_ALIGNMENT 19
106 /* FDT_ERR_ALIGNMENT: The device tree base address is not 8-byte
107 * aligned. */
108
109 #define FDT_ERR_MAX 19
110
111 /* constants */
112 #define FDT_MAX_PHANDLE 0xfffffffe
113 /* Valid values for phandles range from 1 to 2^32-2. */
114
115 /**********************************************************************/
116 /* Low-level functions (you probably don't need these) */
117 /**********************************************************************/
118
119 /**
120 * fdt_offset_ptr - safely get a byte range within the device tree blob
121 * @fdt: Pointer to the device tree blob
122 * @offset: Offset within the blob to the desired byte range
123 * @checklen: Required length of the byte range
124 *
125 * fdt_offset_ptr() returns a pointer to the byte range of length @checklen at
126 * the given @offset within the device tree blob, after verifying that the byte
127 * range fits entirely within the blob and does not overflow.
128 *
129 * returns:
130 * pointer to the byte range, on success
131 * NULL, if the requested range does not fit within the blob
132 */
133 #ifndef SWIG /* This function is not useful in Python */
134 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
135 #endif
fdt_offset_ptr_w(void * fdt,int offset,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 /**
142 * fdt_next_tag - get next tag in the device tree
143 * @fdt: Pointer to the device tree blob
144 * @offset: Offset within the blob to start searching
145 * @nextoffset: Pointer to variable to store the offset of the next tag
146 *
147 * fdt_next_tag() returns the tag type of the next tag in the device tree
148 * blob starting from the given @offset. If @nextoffset is non-NULL, it will
149 * be set to the offset immediately following the tag.
150 *
151 * returns:
152 * the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END),
153 * FDT_END, if offset is out of bounds
154 */
155 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
156
157 /*
158 * External helpers to access words from a device tree blob. They're built
159 * to work even with unaligned pointers on platforms (such as ARMv5) that don't
160 * like unaligned loads and stores.
161 */
fdt16_ld(const fdt16_t * p)162 static inline uint16_t fdt16_ld(const fdt16_t *p)
163 {
164 const uint8_t *bp = (const uint8_t *)p;
165
166 return ((uint16_t)bp[0] << 8) | bp[1];
167 }
168
fdt32_ld(const fdt32_t * p)169 static inline uint32_t fdt32_ld(const fdt32_t *p)
170 {
171 const uint8_t *bp = (const uint8_t *)p;
172
173 return ((uint32_t)bp[0] << 24)
174 | ((uint32_t)bp[1] << 16)
175 | ((uint32_t)bp[2] << 8)
176 | bp[3];
177 }
178
fdt32_st(void * property,uint32_t value)179 static inline void fdt32_st(void *property, uint32_t value)
180 {
181 uint8_t *bp = (uint8_t *)property;
182
183 bp[0] = value >> 24;
184 bp[1] = (value >> 16) & 0xff;
185 bp[2] = (value >> 8) & 0xff;
186 bp[3] = value & 0xff;
187 }
188
fdt64_ld(const fdt64_t * p)189 static inline uint64_t fdt64_ld(const fdt64_t *p)
190 {
191 const uint8_t *bp = (const uint8_t *)p;
192
193 return ((uint64_t)bp[0] << 56)
194 | ((uint64_t)bp[1] << 48)
195 | ((uint64_t)bp[2] << 40)
196 | ((uint64_t)bp[3] << 32)
197 | ((uint64_t)bp[4] << 24)
198 | ((uint64_t)bp[5] << 16)
199 | ((uint64_t)bp[6] << 8)
200 | bp[7];
201 }
202
fdt64_st(void * property,uint64_t value)203 static inline void fdt64_st(void *property, uint64_t value)
204 {
205 uint8_t *bp = (uint8_t *)property;
206
207 bp[0] = value >> 56;
208 bp[1] = (value >> 48) & 0xff;
209 bp[2] = (value >> 40) & 0xff;
210 bp[3] = (value >> 32) & 0xff;
211 bp[4] = (value >> 24) & 0xff;
212 bp[5] = (value >> 16) & 0xff;
213 bp[6] = (value >> 8) & 0xff;
214 bp[7] = value & 0xff;
215 }
216
217 /**********************************************************************/
218 /* Traversal functions */
219 /**********************************************************************/
220
221 int fdt_next_node(const void *fdt, int offset, int *depth);
222
223 /**
224 * fdt_first_subnode() - get offset of first direct subnode
225 * @fdt: FDT blob
226 * @offset: Offset of node to check
227 *
228 * Return: offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
229 */
230 int fdt_first_subnode(const void *fdt, int offset);
231
232 /**
233 * fdt_next_subnode() - get offset of next direct subnode
234 * @fdt: FDT blob
235 * @offset: Offset of previous subnode
236 *
237 * After first calling fdt_first_subnode(), call this function repeatedly to
238 * get direct subnodes of a parent node.
239 *
240 * Return: offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
241 * subnodes
242 */
243 int fdt_next_subnode(const void *fdt, int offset);
244
245 /**
246 * fdt_for_each_subnode - iterate over all subnodes of a parent
247 *
248 * @node: child node (int, lvalue)
249 * @fdt: FDT blob (const void *)
250 * @parent: parent node (int)
251 *
252 * This is actually a wrapper around a for loop and would be used like so:
253 *
254 * fdt_for_each_subnode(node, fdt, parent) {
255 * Use node
256 * ...
257 * }
258 *
259 * if ((node < 0) && (node != -FDT_ERR_NOTFOUND)) {
260 * Error handling
261 * }
262 *
263 * Note that this is implemented as a macro and @node is used as
264 * iterator in the loop. The parent variable be constant or even a
265 * literal.
266 */
267 #define fdt_for_each_subnode(node, fdt, parent) \
268 for (node = fdt_first_subnode(fdt, parent); \
269 node >= 0; \
270 node = fdt_next_subnode(fdt, node))
271
272 /**********************************************************************/
273 /* General functions */
274 /**********************************************************************/
275 #define fdt_get_header(fdt, field) \
276 (fdt32_ld(&((const struct fdt_header *)(fdt))->field))
277 #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
278 #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
279 #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
280 #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
281 #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
282 #define fdt_version(fdt) (fdt_get_header(fdt, version))
283 #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
284 #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
285 #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
286 #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
287
288 #define fdt_set_hdr_(name) \
289 static inline void fdt_set_##name(void *fdt, uint32_t val) \
290 { \
291 struct fdt_header *fdth = (struct fdt_header *)fdt; \
292 fdth->name = cpu_to_fdt32(val); \
293 }
294 fdt_set_hdr_(magic)
295 fdt_set_hdr_(totalsize)
296 fdt_set_hdr_(off_dt_struct)
297 fdt_set_hdr_(off_dt_strings)
298 fdt_set_hdr_(off_mem_rsvmap)
299 fdt_set_hdr_(version)
300 fdt_set_hdr_(last_comp_version)
301 fdt_set_hdr_(boot_cpuid_phys)
302 fdt_set_hdr_(size_dt_strings)
303 fdt_set_hdr_(size_dt_struct)
304 #undef fdt_set_hdr_
305
306 /**
307 * fdt_header_size - return the size of the tree's header
308 * @fdt: pointer to a flattened device tree
309 *
310 * Return: size of DTB header in bytes
311 */
312 size_t fdt_header_size(const void *fdt);
313
314 /**
315 * fdt_header_size_ - internal function to get header size from a version number
316 * @version: device tree version number
317 *
318 * Return: size of DTB header in bytes
319 */
320 size_t fdt_header_size_(uint32_t version);
321
322 /**
323 * fdt_check_header - sanity check a device tree header
324 * @fdt: pointer to data which might be a flattened device tree
325 *
326 * fdt_check_header() checks that the given buffer contains what
327 * appears to be a flattened device tree, and that the header contains
328 * valid information (to the extent that can be determined from the
329 * header alone).
330 *
331 * returns:
332 * 0, if the buffer appears to contain a valid device tree
333 * -FDT_ERR_BADMAGIC,
334 * -FDT_ERR_BADVERSION,
335 * -FDT_ERR_BADSTATE,
336 * -FDT_ERR_TRUNCATED, standard meanings, as above
337 */
338 int fdt_check_header(const void *fdt);
339
340 /**
341 * fdt_move - move a device tree around in memory
342 * @fdt: pointer to the device tree to move
343 * @buf: pointer to memory where the device is to be moved
344 * @bufsize: size of the memory space at buf
345 *
346 * fdt_move() relocates, if possible, the device tree blob located at
347 * fdt to the buffer at buf of size bufsize. The buffer may overlap
348 * with the existing device tree blob at fdt. Therefore,
349 * fdt_move(fdt, fdt, fdt_totalsize(fdt))
350 * should always succeed.
351 *
352 * returns:
353 * 0, on success
354 * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
355 * -FDT_ERR_BADMAGIC,
356 * -FDT_ERR_BADVERSION,
357 * -FDT_ERR_BADSTATE, standard meanings
358 */
359 int fdt_move(const void *fdt, void *buf, int bufsize);
360
361 /**********************************************************************/
362 /* Read-only functions */
363 /**********************************************************************/
364
365 /**
366 * fdt_check_full - check device tree validity
367 * @fdt: pointer to the device tree blob
368 * @bufsize: size of the buffer containing the device tree
369 *
370 * fdt_check_full() checks that the given buffer contains a valid
371 * flattened device tree and that the tree structure is internally
372 * consistent. This is a more thorough check than fdt_check_header().
373 *
374 * returns:
375 * 0, on success
376 * -FDT_ERR_BADMAGIC,
377 * -FDT_ERR_BADVERSION,
378 * -FDT_ERR_BADSTATE,
379 * -FDT_ERR_BADSTRUCTURE,
380 * -FDT_ERR_TRUNCATED, standard meanings
381 */
382 int fdt_check_full(const void *fdt, size_t bufsize);
383
384 /**
385 * fdt_get_string - retrieve a string from the strings block of a device tree
386 * @fdt: pointer to the device tree blob
387 * @stroffset: offset of the string within the strings block (native endian)
388 * @lenp: optional pointer to return the string's length
389 *
390 * fdt_get_string() retrieves a pointer to a single string from the
391 * strings block of the device tree blob at fdt, and optionally also
392 * returns the string's length in *lenp.
393 *
394 * returns:
395 * a pointer to the string, on success
396 * NULL, if stroffset is out of bounds, or doesn't point to a valid string
397 */
398 const char *fdt_get_string(const void *fdt, int stroffset, int *lenp);
399
400 /**
401 * fdt_string - retrieve a string from the strings block of a device tree
402 * @fdt: pointer to the device tree blob
403 * @stroffset: offset of the string within the strings block (native endian)
404 *
405 * fdt_string() retrieves a pointer to a single string from the
406 * strings block of the device tree blob at fdt.
407 *
408 * returns:
409 * a pointer to the string, on success
410 * NULL, if stroffset is out of bounds, or doesn't point to a valid string
411 */
412 const char *fdt_string(const void *fdt, int stroffset);
413
414 /**
415 * fdt_find_max_phandle - find and return the highest phandle in a tree
416 * @fdt: pointer to the device tree blob
417 * @phandle: return location for the highest phandle value found in the tree
418 *
419 * fdt_find_max_phandle() finds the highest phandle value in the given device
420 * tree. The value returned in @phandle is only valid if the function returns
421 * success.
422 *
423 * returns:
424 * 0 on success or a negative error code on failure
425 */
426 int fdt_find_max_phandle(const void *fdt, uint32_t *phandle);
427
428 /**
429 * fdt_get_max_phandle - retrieves the highest phandle in a tree
430 * @fdt: pointer to the device tree blob
431 *
432 * fdt_get_max_phandle retrieves the highest phandle in the given
433 * device tree. This will ignore badly formatted phandles, or phandles
434 * with a value of 0 or -1.
435 *
436 * This function is deprecated in favour of fdt_find_max_phandle().
437 *
438 * returns:
439 * the highest phandle on success
440 * 0, if no phandle was found in the device tree
441 * -1, if an error occurred
442 */
fdt_get_max_phandle(const void * fdt)443 static inline uint32_t fdt_get_max_phandle(const void *fdt)
444 {
445 uint32_t phandle;
446 int err;
447
448 err = fdt_find_max_phandle(fdt, &phandle);
449 if (err < 0)
450 return (uint32_t)-1;
451
452 return phandle;
453 }
454
455 /**
456 * fdt_generate_phandle - return a new, unused phandle for a device tree blob
457 * @fdt: pointer to the device tree blob
458 * @phandle: return location for the new phandle
459 *
460 * Walks the device tree blob and looks for the highest phandle value. On
461 * success, the new, unused phandle value (one higher than the previously
462 * highest phandle value in the device tree blob) will be returned in the
463 * @phandle parameter.
464 *
465 * Return: 0 on success or a negative error-code on failure
466 */
467 int fdt_generate_phandle(const void *fdt, uint32_t *phandle);
468
469 /**
470 * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
471 * @fdt: pointer to the device tree blob
472 *
473 * Returns the number of entries in the device tree blob's memory
474 * reservation map. This does not include the terminating 0,0 entry
475 * or any other (0,0) entries reserved for expansion.
476 *
477 * returns:
478 * the number of entries
479 */
480 int fdt_num_mem_rsv(const void *fdt);
481
482 /**
483 * fdt_get_mem_rsv - retrieve one memory reserve map entry
484 * @fdt: pointer to the device tree blob
485 * @n: index of reserve map entry
486 * @address: pointer to 64-bit variable to hold the start address
487 * @size: pointer to 64-bit variable to hold the size of the entry
488 *
489 * On success, @address and @size will contain the address and size of
490 * the n-th reserve map entry from the device tree blob, in
491 * native-endian format.
492 *
493 * returns:
494 * 0, on success
495 * -FDT_ERR_BADMAGIC,
496 * -FDT_ERR_BADVERSION,
497 * -FDT_ERR_BADSTATE, standard meanings
498 */
499 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
500
501 /**
502 * fdt_subnode_offset_namelen - find a subnode based on substring
503 * @fdt: pointer to the device tree blob
504 * @parentoffset: structure block offset of a node
505 * @name: name of the subnode to locate
506 * @namelen: number of characters of name to consider
507 *
508 * Identical to fdt_subnode_offset(), but only examine the first
509 * namelen characters of name for matching the subnode name. This is
510 * useful for finding subnodes based on a portion of a larger string,
511 * such as a full path.
512 *
513 * Return: offset of the subnode or -FDT_ERR_NOTFOUND if name not found.
514 */
515 #ifndef SWIG /* Not available in Python */
516 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
517 const char *name, int namelen);
518 #endif
519 /**
520 * fdt_subnode_offset - find a subnode of a given node
521 * @fdt: pointer to the device tree blob
522 * @parentoffset: structure block offset of a node
523 * @name: name of the subnode to locate
524 *
525 * fdt_subnode_offset() finds a subnode of the node at structure block
526 * offset parentoffset with the given name. name may include a unit
527 * address, in which case fdt_subnode_offset() will find the subnode
528 * with that unit address, or the unit address may be omitted, in
529 * which case fdt_subnode_offset() will find an arbitrary subnode
530 * whose name excluding unit address matches the given name.
531 *
532 * returns:
533 * structure block offset of the requested subnode (>=0), on success
534 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
535 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
536 * tag
537 * -FDT_ERR_BADMAGIC,
538 * -FDT_ERR_BADVERSION,
539 * -FDT_ERR_BADSTATE,
540 * -FDT_ERR_BADSTRUCTURE,
541 * -FDT_ERR_TRUNCATED, standard meanings.
542 */
543 int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
544
545 /**
546 * fdt_path_offset_namelen - find a tree node by its full path
547 * @fdt: pointer to the device tree blob
548 * @path: full path of the node to locate
549 * @namelen: number of characters of path to consider
550 *
551 * Identical to fdt_path_offset(), but only consider the first namelen
552 * characters of path as the path name.
553 *
554 * Return: offset of the node or negative libfdt error value otherwise
555 */
556 #ifndef SWIG /* Not available in Python */
557 int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
558 #endif
559
560 /**
561 * fdt_path_offset - find a tree node by its full path
562 * @fdt: pointer to the device tree blob
563 * @path: full path of the node to locate
564 *
565 * fdt_path_offset() finds a node of a given path in the device tree.
566 * Each path component may omit the unit address portion, but the
567 * results of this are undefined if any such path component is
568 * ambiguous (that is if there are multiple nodes at the relevant
569 * level matching the given component, differentiated only by unit
570 * address).
571 *
572 * If the path is not absolute (i.e. does not begin with '/'), the
573 * first component is treated as an alias. That is, the property by
574 * that name is looked up in the /aliases node, and the value of that
575 * property used in place of that first component.
576 *
577 * For example, for this small fragment
578 *
579 * / {
580 * aliases {
581 * i2c2 = &foo; // RHS compiles to "/soc@0/i2c@30a40000/eeprom@52"
582 * };
583 * soc@0 {
584 * foo: i2c@30a40000 {
585 * bar: eeprom@52 {
586 * };
587 * };
588 * };
589 * };
590 *
591 * these would be equivalent:
592 *
593 * /soc@0/i2c@30a40000/eeprom@52
594 * i2c2/eeprom@52
595 *
596 * returns:
597 * structure block offset of the node with the requested path (>=0), on
598 * success
599 * -FDT_ERR_BADPATH, given path does not begin with '/' and the first
600 * component is not a valid alias
601 * -FDT_ERR_NOTFOUND, if the requested node does not exist
602 * -FDT_ERR_BADMAGIC,
603 * -FDT_ERR_BADVERSION,
604 * -FDT_ERR_BADSTATE,
605 * -FDT_ERR_BADSTRUCTURE,
606 * -FDT_ERR_TRUNCATED, standard meanings.
607 */
608 int fdt_path_offset(const void *fdt, const char *path);
609
610 /**
611 * fdt_get_name - retrieve the name of a given node
612 * @fdt: pointer to the device tree blob
613 * @nodeoffset: structure block offset of the starting node
614 * @lenp: pointer to an integer variable (will be overwritten) or NULL
615 *
616 * fdt_get_name() retrieves the name (including unit address) of the
617 * device tree node at structure block offset nodeoffset. If lenp is
618 * non-NULL, the length of this name is also returned, in the integer
619 * pointed to by lenp.
620 *
621 * returns:
622 * pointer to the node's name, on success
623 * If lenp is non-NULL, *lenp contains the length of that name
624 * (>=0)
625 * NULL, on error
626 * if lenp is non-NULL *lenp contains an error code (<0):
627 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
628 * tag
629 * -FDT_ERR_BADMAGIC,
630 * -FDT_ERR_BADVERSION,
631 * -FDT_ERR_BADSTATE, standard meanings
632 */
633 const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
634
635 /**
636 * fdt_first_property_offset - find the offset of a node's first property
637 * @fdt: pointer to the device tree blob
638 * @nodeoffset: structure block offset of a node
639 *
640 * fdt_first_property_offset() finds the first property of the node at
641 * the given structure block offset.
642 *
643 * returns:
644 * structure block offset of the property (>=0), on success
645 * -FDT_ERR_NOTFOUND, if the requested node has no properties
646 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
647 * -FDT_ERR_BADMAGIC,
648 * -FDT_ERR_BADVERSION,
649 * -FDT_ERR_BADSTATE,
650 * -FDT_ERR_BADSTRUCTURE,
651 * -FDT_ERR_TRUNCATED, standard meanings.
652 */
653 int fdt_first_property_offset(const void *fdt, int nodeoffset);
654
655 /**
656 * fdt_next_property_offset - step through a node's properties
657 * @fdt: pointer to the device tree blob
658 * @offset: structure block offset of a property
659 *
660 * fdt_next_property_offset() finds the property immediately after the
661 * one at the given structure block offset. This will be a property
662 * of the same node as the given property.
663 *
664 * returns:
665 * structure block offset of the next property (>=0), on success
666 * -FDT_ERR_NOTFOUND, if the given property is the last in its node
667 * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
668 * -FDT_ERR_BADMAGIC,
669 * -FDT_ERR_BADVERSION,
670 * -FDT_ERR_BADSTATE,
671 * -FDT_ERR_BADSTRUCTURE,
672 * -FDT_ERR_TRUNCATED, standard meanings.
673 */
674 int fdt_next_property_offset(const void *fdt, int offset);
675
676 /**
677 * fdt_for_each_property_offset - iterate over all properties of a node
678 *
679 * @property: property offset (int, lvalue)
680 * @fdt: FDT blob (const void *)
681 * @node: node offset (int)
682 *
683 * This is actually a wrapper around a for loop and would be used like so:
684 *
685 * fdt_for_each_property_offset(property, fdt, node) {
686 * Use property
687 * ...
688 * }
689 *
690 * if ((property < 0) && (property != -FDT_ERR_NOTFOUND)) {
691 * Error handling
692 * }
693 *
694 * Note that this is implemented as a macro and property is used as
695 * iterator in the loop. The node variable can be constant or even a
696 * literal.
697 */
698 #define fdt_for_each_property_offset(property, fdt, node) \
699 for (property = fdt_first_property_offset(fdt, node); \
700 property >= 0; \
701 property = fdt_next_property_offset(fdt, property))
702
703 /**
704 * fdt_get_property_by_offset - retrieve the property at a given offset
705 * @fdt: pointer to the device tree blob
706 * @offset: offset of the property to retrieve
707 * @lenp: pointer to an integer variable (will be overwritten) or NULL
708 *
709 * fdt_get_property_by_offset() retrieves a pointer to the
710 * fdt_property structure within the device tree blob at the given
711 * offset. If lenp is non-NULL, the length of the property value is
712 * also returned, in the integer pointed to by lenp.
713 *
714 * Note that this code only works on device tree versions >= 16. fdt_getprop()
715 * works on all versions.
716 *
717 * returns:
718 * pointer to the structure representing the property
719 * if lenp is non-NULL, *lenp contains the length of the property
720 * value (>=0)
721 * NULL, on error
722 * if lenp is non-NULL, *lenp contains an error code (<0):
723 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
724 * -FDT_ERR_BADMAGIC,
725 * -FDT_ERR_BADVERSION,
726 * -FDT_ERR_BADSTATE,
727 * -FDT_ERR_BADSTRUCTURE,
728 * -FDT_ERR_TRUNCATED, standard meanings
729 */
730 const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
731 int offset,
732 int *lenp);
fdt_get_property_by_offset_w(void * fdt,int offset,int * lenp)733 static inline struct fdt_property *fdt_get_property_by_offset_w(void *fdt,
734 int offset,
735 int *lenp)
736 {
737 return (struct fdt_property *)(uintptr_t)
738 fdt_get_property_by_offset(fdt, offset, lenp);
739 }
740
741 /**
742 * fdt_get_property_namelen - find a property based on substring
743 * @fdt: pointer to the device tree blob
744 * @nodeoffset: offset of the node whose property to find
745 * @name: name of the property to find
746 * @namelen: number of characters of name to consider
747 * @lenp: pointer to an integer variable (will be overwritten) or NULL
748 *
749 * Identical to fdt_get_property(), but only examine the first namelen
750 * characters of name for matching the property name.
751 *
752 * Return: pointer to the structure representing the property, or NULL
753 * if not found
754 */
755 #ifndef SWIG /* Not available in Python */
756 const struct fdt_property *fdt_get_property_namelen(const void *fdt,
757 int nodeoffset,
758 const char *name,
759 int namelen, int *lenp);
760 static inline struct fdt_property *
fdt_get_property_namelen_w(void * fdt,int nodeoffset,const char * name,int namelen,int * lenp)761 fdt_get_property_namelen_w(void *fdt, int nodeoffset, const char *name,
762 int namelen, int *lenp)
763 {
764 return (struct fdt_property *)(uintptr_t)fdt_get_property_namelen(
765 fdt, nodeoffset, name, namelen, lenp);
766 }
767 #endif
768
769 /**
770 * fdt_get_property - find a given property in a given node
771 * @fdt: pointer to the device tree blob
772 * @nodeoffset: offset of the node whose property to find
773 * @name: name of the property to find
774 * @lenp: pointer to an integer variable (will be overwritten) or NULL
775 *
776 * fdt_get_property() retrieves a pointer to the fdt_property
777 * structure within the device tree blob corresponding to the property
778 * named 'name' of the node at offset nodeoffset. If lenp is
779 * non-NULL, the length of the property value is also returned, in the
780 * integer pointed to by lenp.
781 *
782 * returns:
783 * pointer to the structure representing the property
784 * if lenp is non-NULL, *lenp contains the length of the property
785 * value (>=0)
786 * NULL, on error
787 * if lenp is non-NULL, *lenp contains an error code (<0):
788 * -FDT_ERR_NOTFOUND, node does not have named property
789 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
790 * tag
791 * -FDT_ERR_BADMAGIC,
792 * -FDT_ERR_BADVERSION,
793 * -FDT_ERR_BADSTATE,
794 * -FDT_ERR_BADSTRUCTURE,
795 * -FDT_ERR_TRUNCATED, standard meanings
796 */
797 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
798 const char *name, int *lenp);
fdt_get_property_w(void * fdt,int nodeoffset,const char * name,int * lenp)799 static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
800 const char *name,
801 int *lenp)
802 {
803 return (struct fdt_property *)(uintptr_t)
804 fdt_get_property(fdt, nodeoffset, name, lenp);
805 }
806
807 /**
808 * fdt_getprop_by_offset - retrieve the value of a property at a given offset
809 * @fdt: pointer to the device tree blob
810 * @offset: offset of the property to read
811 * @namep: pointer to a string variable (will be overwritten) or NULL
812 * @lenp: pointer to an integer variable (will be overwritten) or NULL
813 *
814 * fdt_getprop_by_offset() retrieves a pointer to the value of the
815 * property at structure block offset 'offset' (this will be a pointer
816 * to within the device blob itself, not a copy of the value). If
817 * lenp is non-NULL, the length of the property value is also
818 * returned, in the integer pointed to by lenp. If namep is non-NULL,
819 * the property's name will also be returned in the char * pointed to
820 * by namep (this will be a pointer to within the device tree's string
821 * block, not a new copy of the name).
822 *
823 * returns:
824 * pointer to the property's value
825 * if lenp is non-NULL, *lenp contains the length of the property
826 * value (>=0)
827 * if namep is non-NULL *namep contains a pointer to the property
828 * name.
829 * NULL, on error
830 * if lenp is non-NULL, *lenp contains an error code (<0):
831 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
832 * -FDT_ERR_BADMAGIC,
833 * -FDT_ERR_BADVERSION,
834 * -FDT_ERR_BADSTATE,
835 * -FDT_ERR_BADSTRUCTURE,
836 * -FDT_ERR_TRUNCATED, standard meanings
837 */
838 #ifndef SWIG /* This function is not useful in Python */
839 const void *fdt_getprop_by_offset(const void *fdt, int offset,
840 const char **namep, int *lenp);
841 #endif
842
843 /**
844 * fdt_getprop_namelen - get property value based on substring
845 * @fdt: pointer to the device tree blob
846 * @nodeoffset: offset of the node whose property to find
847 * @name: name of the property to find
848 * @namelen: number of characters of name to consider
849 * @lenp: pointer to an integer variable (will be overwritten) or NULL
850 *
851 * Identical to fdt_getprop(), but only examine the first namelen
852 * characters of name for matching the property name.
853 *
854 * Return: pointer to the property's value or NULL on error
855 */
856 #ifndef SWIG /* Not available in Python */
857 const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
858 const char *name, int namelen, int *lenp);
fdt_getprop_namelen_w(void * fdt,int nodeoffset,const char * name,int namelen,int * lenp)859 static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
860 const char *name, int namelen,
861 int *lenp)
862 {
863 return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
864 namelen, lenp);
865 }
866 #endif
867
868 /**
869 * fdt_getprop - retrieve the value of a given property
870 * @fdt: pointer to the device tree blob
871 * @nodeoffset: offset of the node whose property to find
872 * @name: name of the property to find
873 * @lenp: pointer to an integer variable (will be overwritten) or NULL
874 *
875 * fdt_getprop() retrieves a pointer to the value of the property
876 * named @name of the node at offset @nodeoffset (this will be a
877 * pointer to within the device blob itself, not a copy of the value).
878 * If @lenp is non-NULL, the length of the property value is also
879 * returned, in the integer pointed to by @lenp.
880 *
881 * returns:
882 * pointer to the property's value
883 * if lenp is non-NULL, *lenp contains the length of the property
884 * value (>=0)
885 * NULL, on error
886 * if lenp is non-NULL, *lenp contains an error code (<0):
887 * -FDT_ERR_NOTFOUND, node does not have named property
888 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
889 * tag
890 * -FDT_ERR_BADMAGIC,
891 * -FDT_ERR_BADVERSION,
892 * -FDT_ERR_BADSTATE,
893 * -FDT_ERR_BADSTRUCTURE,
894 * -FDT_ERR_TRUNCATED, standard meanings
895 */
896 const void *fdt_getprop(const void *fdt, int nodeoffset,
897 const char *name, int *lenp);
fdt_getprop_w(void * fdt,int nodeoffset,const char * name,int * lenp)898 static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
899 const char *name, int *lenp)
900 {
901 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
902 }
903
904 /**
905 * fdt_get_phandle - retrieve the phandle of a given node
906 * @fdt: pointer to the device tree blob
907 * @nodeoffset: structure block offset of the node
908 *
909 * fdt_get_phandle() retrieves the phandle of the device tree node at
910 * structure block offset nodeoffset.
911 *
912 * returns:
913 * the phandle of the node at nodeoffset, on success (!= 0, != -1)
914 * 0, if the node has no phandle, or another error occurs
915 */
916 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
917
918 /**
919 * fdt_get_alias_namelen - get alias based on substring
920 * @fdt: pointer to the device tree blob
921 * @name: name of the alias to look up
922 * @namelen: number of characters of name to consider
923 *
924 * Identical to fdt_get_alias(), but only examine the first @namelen
925 * characters of @name for matching the alias name.
926 *
927 * Return: a pointer to the expansion of the alias named @name, if it exists,
928 * NULL otherwise
929 */
930 #ifndef SWIG /* Not available in Python */
931 const char *fdt_get_alias_namelen(const void *fdt,
932 const char *name, int namelen);
933 #endif
934
935 /**
936 * fdt_get_alias - retrieve the path referenced by a given alias
937 * @fdt: pointer to the device tree blob
938 * @name: name of the alias to look up
939 *
940 * fdt_get_alias() retrieves the value of a given alias. That is, the
941 * value of the property named @name in the node /aliases.
942 *
943 * returns:
944 * a pointer to the expansion of the alias named 'name', if it exists
945 * NULL, if the given alias or the /aliases node does not exist
946 */
947 const char *fdt_get_alias(const void *fdt, const char *name);
948
949 /**
950 * fdt_get_symbol_namelen - get symbol based on substring
951 * @fdt: pointer to the device tree blob
952 * @name: name of the symbol to look up
953 * @namelen: number of characters of name to consider
954 *
955 * Identical to fdt_get_symbol(), but only examine the first @namelen
956 * characters of @name for matching the symbol name.
957 *
958 * Return: a pointer to the expansion of the symbol named @name, if it exists,
959 * NULL otherwise
960 */
961 #ifndef SWIG /* Not available in Python */
962 const char *fdt_get_symbol_namelen(const void *fdt,
963 const char *name, int namelen);
964 #endif
965
966 /**
967 * fdt_get_symbol - retrieve the path referenced by a given symbol
968 * @fdt: pointer to the device tree blob
969 * @name: name of the symbol to look up
970 *
971 * fdt_get_symbol() retrieves the value of a given symbol. That is,
972 * the value of the property named @name in the node
973 * /__symbols__. Such a node exists only for a device tree blob that
974 * has been compiled with the -@ dtc option. Each property corresponds
975 * to a label appearing in the device tree source, with the name of
976 * the property being the label and the value being the full path of
977 * the node it is attached to.
978 *
979 * returns:
980 * a pointer to the expansion of the symbol named 'name', if it exists
981 * NULL, if the given symbol or the /__symbols__ node does not exist
982 */
983 const char *fdt_get_symbol(const void *fdt, const char *name);
984
985 /**
986 * fdt_get_path - determine the full path of a node
987 * @fdt: pointer to the device tree blob
988 * @nodeoffset: offset of the node whose path to find
989 * @buf: character buffer to contain the returned path (will be overwritten)
990 * @buflen: size of the character buffer at buf
991 *
992 * fdt_get_path() computes the full path of the node at offset
993 * nodeoffset, and records that path in the buffer at buf.
994 *
995 * NOTE: This function is expensive, as it must scan the device tree
996 * structure from the start to nodeoffset.
997 *
998 * returns:
999 * 0, on success
1000 * buf contains the absolute path of the node at
1001 * nodeoffset, as a NUL-terminated string.
1002 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
1003 * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
1004 * characters and will not fit in the given buffer.
1005 * -FDT_ERR_BADMAGIC,
1006 * -FDT_ERR_BADVERSION,
1007 * -FDT_ERR_BADSTATE,
1008 * -FDT_ERR_BADSTRUCTURE, standard meanings
1009 */
1010 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
1011
1012 /**
1013 * fdt_supernode_atdepth_offset - find a specific ancestor of a node
1014 * @fdt: pointer to the device tree blob
1015 * @nodeoffset: offset of the node whose parent to find
1016 * @supernodedepth: depth of the ancestor to find
1017 * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
1018 *
1019 * fdt_supernode_atdepth_offset() finds an ancestor of the given node
1020 * at a specific depth from the root (where the root itself has depth
1021 * 0, its immediate subnodes depth 1 and so forth). So
1022 * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
1023 * will always return 0, the offset of the root node. If the node at
1024 * nodeoffset has depth D, then:
1025 * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
1026 * will return nodeoffset itself.
1027 *
1028 * NOTE: This function is expensive, as it must scan the device tree
1029 * structure from the start to nodeoffset.
1030 *
1031 * returns:
1032 * structure block offset of the node at node offset's ancestor
1033 * of depth supernodedepth (>=0), on success
1034 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
1035 * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
1036 * nodeoffset
1037 * -FDT_ERR_BADMAGIC,
1038 * -FDT_ERR_BADVERSION,
1039 * -FDT_ERR_BADSTATE,
1040 * -FDT_ERR_BADSTRUCTURE, standard meanings
1041 */
1042 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
1043 int supernodedepth, int *nodedepth);
1044
1045 /**
1046 * fdt_node_depth - find the depth of a given node
1047 * @fdt: pointer to the device tree blob
1048 * @nodeoffset: offset of the node whose parent to find
1049 *
1050 * fdt_node_depth() finds the depth of a given node. The root node
1051 * has depth 0, its immediate subnodes depth 1 and so forth.
1052 *
1053 * NOTE: This function is expensive, as it must scan the device tree
1054 * structure from the start to nodeoffset.
1055 *
1056 * returns:
1057 * depth of the node at nodeoffset (>=0), on success
1058 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
1059 * -FDT_ERR_BADMAGIC,
1060 * -FDT_ERR_BADVERSION,
1061 * -FDT_ERR_BADSTATE,
1062 * -FDT_ERR_BADSTRUCTURE, standard meanings
1063 */
1064 int fdt_node_depth(const void *fdt, int nodeoffset);
1065
1066 /**
1067 * fdt_parent_offset - find the parent of a given node
1068 * @fdt: pointer to the device tree blob
1069 * @nodeoffset: offset of the node whose parent to find
1070 *
1071 * fdt_parent_offset() locates the parent node of a given node (that
1072 * is, it finds the offset of the node which contains the node at
1073 * nodeoffset as a subnode).
1074 *
1075 * NOTE: This function is expensive, as it must scan the device tree
1076 * structure from the start to nodeoffset, *twice*.
1077 *
1078 * returns:
1079 * structure block offset of the parent of the node at nodeoffset
1080 * (>=0), on success
1081 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
1082 * -FDT_ERR_BADMAGIC,
1083 * -FDT_ERR_BADVERSION,
1084 * -FDT_ERR_BADSTATE,
1085 * -FDT_ERR_BADSTRUCTURE, standard meanings
1086 */
1087 int fdt_parent_offset(const void *fdt, int nodeoffset);
1088
1089 /**
1090 * fdt_node_offset_by_prop_value - find nodes with a given property value
1091 * @fdt: pointer to the device tree blob
1092 * @startoffset: only find nodes after this offset
1093 * @propname: property name to check
1094 * @propval: property value to search for
1095 * @proplen: length of the value in propval
1096 *
1097 * fdt_node_offset_by_prop_value() returns the offset of the first
1098 * node after startoffset, which has a property named propname whose
1099 * value is of length proplen and has value equal to propval; or if
1100 * startoffset is -1, the very first such node in the tree.
1101 *
1102 * To iterate through all nodes matching the criterion, the following
1103 * idiom can be used:
1104 * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
1105 * propval, proplen);
1106 * while (offset != -FDT_ERR_NOTFOUND) {
1107 * // other code here
1108 * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
1109 * propval, proplen);
1110 * }
1111 *
1112 * Note the -1 in the first call to the function, if 0 is used here
1113 * instead, the function will never locate the root node, even if it
1114 * matches the criterion.
1115 *
1116 * returns:
1117 * structure block offset of the located node (>= 0, >startoffset),
1118 * on success
1119 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
1120 * tree after startoffset
1121 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
1122 * -FDT_ERR_BADMAGIC,
1123 * -FDT_ERR_BADVERSION,
1124 * -FDT_ERR_BADSTATE,
1125 * -FDT_ERR_BADSTRUCTURE, standard meanings
1126 */
1127 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
1128 const char *propname,
1129 const void *propval, int proplen);
1130
1131 /**
1132 * fdt_node_offset_by_phandle - find the node with a given phandle
1133 * @fdt: pointer to the device tree blob
1134 * @phandle: phandle value
1135 *
1136 * fdt_node_offset_by_phandle() returns the offset of the node
1137 * which has the given phandle value. If there is more than one node
1138 * in the tree with the given phandle (an invalid tree), results are
1139 * undefined.
1140 *
1141 * returns:
1142 * structure block offset of the located node (>= 0), on success
1143 * -FDT_ERR_NOTFOUND, no node with that phandle exists
1144 * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
1145 * -FDT_ERR_BADMAGIC,
1146 * -FDT_ERR_BADVERSION,
1147 * -FDT_ERR_BADSTATE,
1148 * -FDT_ERR_BADSTRUCTURE, standard meanings
1149 */
1150 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
1151
1152 /**
1153 * fdt_node_check_compatible - check a node's compatible property
1154 * @fdt: pointer to the device tree blob
1155 * @nodeoffset: offset of a tree node
1156 * @compatible: string to match against
1157 *
1158 * fdt_node_check_compatible() returns 0 if the given node contains a
1159 * @compatible property with the given string as one of its elements,
1160 * it returns non-zero otherwise, or on error.
1161 *
1162 * returns:
1163 * 0, if the node has a 'compatible' property listing the given string
1164 * 1, if the node has a 'compatible' property, but it does not list
1165 * the given string
1166 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
1167 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
1168 * -FDT_ERR_BADMAGIC,
1169 * -FDT_ERR_BADVERSION,
1170 * -FDT_ERR_BADSTATE,
1171 * -FDT_ERR_BADSTRUCTURE, standard meanings
1172 */
1173 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
1174 const char *compatible);
1175
1176 /**
1177 * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
1178 * @fdt: pointer to the device tree blob
1179 * @startoffset: only find nodes after this offset
1180 * @compatible: 'compatible' string to match against
1181 *
1182 * fdt_node_offset_by_compatible() returns the offset of the first
1183 * node after startoffset, which has a 'compatible' property which
1184 * lists the given compatible string; or if startoffset is -1, the
1185 * very first such node in the tree.
1186 *
1187 * To iterate through all nodes matching the criterion, the following
1188 * idiom can be used:
1189 * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
1190 * while (offset != -FDT_ERR_NOTFOUND) {
1191 * // other code here
1192 * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
1193 * }
1194 *
1195 * Note the -1 in the first call to the function, if 0 is used here
1196 * instead, the function will never locate the root node, even if it
1197 * matches the criterion.
1198 *
1199 * returns:
1200 * structure block offset of the located node (>= 0, >startoffset),
1201 * on success
1202 * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
1203 * tree after startoffset
1204 * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
1205 * -FDT_ERR_BADMAGIC,
1206 * -FDT_ERR_BADVERSION,
1207 * -FDT_ERR_BADSTATE,
1208 * -FDT_ERR_BADSTRUCTURE, standard meanings
1209 */
1210 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
1211 const char *compatible);
1212
1213 /**
1214 * fdt_stringlist_contains - check a string list property for a string
1215 * @strlist: Property containing a list of strings to check
1216 * @listlen: Length of property
1217 * @str: String to search for
1218 *
1219 * This is a utility function provided for convenience. The list contains
1220 * one or more strings, each terminated by \0, as is found in a device tree
1221 * "compatible" property.
1222 *
1223 * Return: 1 if the string is found in the list, 0 not found, or invalid list
1224 */
1225 int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
1226
1227 /**
1228 * fdt_stringlist_count - count the number of strings in a string list
1229 * @fdt: pointer to the device tree blob
1230 * @nodeoffset: offset of a tree node
1231 * @property: name of the property containing the string list
1232 *
1233 * Return:
1234 * the number of strings in the given property
1235 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1236 * -FDT_ERR_NOTFOUND if the property does not exist
1237 */
1238 int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
1239
1240 /**
1241 * fdt_stringlist_search - find a string in a string list and return its index
1242 * @fdt: pointer to the device tree blob
1243 * @nodeoffset: offset of a tree node
1244 * @property: name of the property containing the string list
1245 * @string: string to look up in the string list
1246 *
1247 * Note that it is possible for this function to succeed on property values
1248 * that are not NUL-terminated. That's because the function will stop after
1249 * finding the first occurrence of @string. This can for example happen with
1250 * small-valued cell properties, such as #address-cells, when searching for
1251 * the empty string.
1252 *
1253 * return:
1254 * the index of the string in the list of strings
1255 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1256 * -FDT_ERR_NOTFOUND if the property does not exist or does not contain
1257 * the given string
1258 */
1259 int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
1260 const char *string);
1261
1262 /**
1263 * fdt_stringlist_get() - obtain the string at a given index in a string list
1264 * @fdt: pointer to the device tree blob
1265 * @nodeoffset: offset of a tree node
1266 * @property: name of the property containing the string list
1267 * @index: index of the string to return
1268 * @lenp: return location for the string length or an error code on failure
1269 *
1270 * Note that this will successfully extract strings from properties with
1271 * non-NUL-terminated values. For example on small-valued cell properties
1272 * this function will return the empty string.
1273 *
1274 * If non-NULL, the length of the string (on success) or a negative error-code
1275 * (on failure) will be stored in the integer pointer to by lenp.
1276 *
1277 * Return:
1278 * A pointer to the string at the given index in the string list or NULL on
1279 * failure. On success the length of the string will be stored in the memory
1280 * location pointed to by the lenp parameter, if non-NULL. On failure one of
1281 * the following negative error codes will be returned in the lenp parameter
1282 * (if non-NULL):
1283 * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
1284 * -FDT_ERR_NOTFOUND if the property does not exist
1285 */
1286 const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
1287 const char *property, int index,
1288 int *lenp);
1289
1290 /**********************************************************************/
1291 /* Read-only functions (addressing related) */
1292 /**********************************************************************/
1293
1294 /**
1295 * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
1296 *
1297 * This is the maximum value for #address-cells, #size-cells and
1298 * similar properties that will be processed by libfdt. IEE1275
1299 * requires that OF implementations handle values up to 4.
1300 * Implementations may support larger values, but in practice higher
1301 * values aren't used.
1302 */
1303 #define FDT_MAX_NCELLS 4
1304
1305 /**
1306 * fdt_address_cells - retrieve address size for a bus represented in the tree
1307 * @fdt: pointer to the device tree blob
1308 * @nodeoffset: offset of the node to find the address size for
1309 *
1310 * When the node has a valid #address-cells property, returns its value.
1311 *
1312 * returns:
1313 * 0 <= n < FDT_MAX_NCELLS, on success
1314 * 2, if the node has no #address-cells property
1315 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1316 * #address-cells property
1317 * -FDT_ERR_BADMAGIC,
1318 * -FDT_ERR_BADVERSION,
1319 * -FDT_ERR_BADSTATE,
1320 * -FDT_ERR_BADSTRUCTURE,
1321 * -FDT_ERR_TRUNCATED, standard meanings
1322 */
1323 int fdt_address_cells(const void *fdt, int nodeoffset);
1324
1325 /**
1326 * fdt_size_cells - retrieve address range size for a bus represented in the
1327 * tree
1328 * @fdt: pointer to the device tree blob
1329 * @nodeoffset: offset of the node to find the address range size for
1330 *
1331 * When the node has a valid #size-cells property, returns its value.
1332 *
1333 * returns:
1334 * 0 <= n < FDT_MAX_NCELLS, on success
1335 * 1, if the node has no #size-cells property
1336 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
1337 * #size-cells property
1338 * -FDT_ERR_BADMAGIC,
1339 * -FDT_ERR_BADVERSION,
1340 * -FDT_ERR_BADSTATE,
1341 * -FDT_ERR_BADSTRUCTURE,
1342 * -FDT_ERR_TRUNCATED, standard meanings
1343 */
1344 int fdt_size_cells(const void *fdt, int nodeoffset);
1345
1346
1347 /**********************************************************************/
1348 /* Write-in-place functions */
1349 /**********************************************************************/
1350
1351 /**
1352 * fdt_setprop_inplace_namelen_partial - change a property's value,
1353 * but not its size
1354 * @fdt: pointer to the device tree blob
1355 * @nodeoffset: offset of the node whose property to change
1356 * @name: name of the property to change
1357 * @namelen: number of characters of name to consider
1358 * @idx: index of the property to change in the array
1359 * @val: pointer to data to replace the property value with
1360 * @len: length of the property value
1361 *
1362 * Identical to fdt_setprop_inplace(), but modifies the given property
1363 * starting from the given index, and using only the first characters
1364 * of the name. It is useful when you want to manipulate only one value of
1365 * an array and you have a string that doesn't end with \0.
1366 *
1367 * Return: 0 on success, negative libfdt error value otherwise
1368 */
1369 #ifndef SWIG /* Not available in Python */
1370 int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
1371 const char *name, int namelen,
1372 uint32_t idx, const void *val,
1373 int len);
1374 #endif
1375
1376 /**
1377 * fdt_setprop_inplace - change a property's value, but not its size
1378 * @fdt: pointer to the device tree blob
1379 * @nodeoffset: offset of the node whose property to change
1380 * @name: name of the property to change
1381 * @val: pointer to data to replace the property value with
1382 * @len: length of the property value
1383 *
1384 * fdt_setprop_inplace() replaces the value of a given property with
1385 * the data in val, of length len. This function cannot change the
1386 * size of a property, and so will only work if len is equal to the
1387 * current length of the property.
1388 *
1389 * This function will alter only the bytes in the blob which contain
1390 * the given property value, and will not alter or move any other part
1391 * of the tree.
1392 *
1393 * returns:
1394 * 0, on success
1395 * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
1396 * -FDT_ERR_NOTFOUND, node does not have the named property
1397 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1398 * -FDT_ERR_BADMAGIC,
1399 * -FDT_ERR_BADVERSION,
1400 * -FDT_ERR_BADSTATE,
1401 * -FDT_ERR_BADSTRUCTURE,
1402 * -FDT_ERR_TRUNCATED, standard meanings
1403 */
1404 #ifndef SWIG /* Not available in Python */
1405 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1406 const void *val, int len);
1407 #endif
1408
1409 /**
1410 * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
1411 * @fdt: pointer to the device tree blob
1412 * @nodeoffset: offset of the node whose property to change
1413 * @name: name of the property to change
1414 * @val: 32-bit integer value to replace the property with
1415 *
1416 * fdt_setprop_inplace_u32() replaces the value of a given property
1417 * with the 32-bit integer value in val, converting val to big-endian
1418 * if necessary. This function cannot change the size of a property,
1419 * and so will only work if the property already exists and has length
1420 * 4.
1421 *
1422 * This function will alter only the bytes in the blob which contain
1423 * the given property value, and will not alter or move any other part
1424 * of the tree.
1425 *
1426 * returns:
1427 * 0, on success
1428 * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
1429 * -FDT_ERR_NOTFOUND, node does not have the named property
1430 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1431 * -FDT_ERR_BADMAGIC,
1432 * -FDT_ERR_BADVERSION,
1433 * -FDT_ERR_BADSTATE,
1434 * -FDT_ERR_BADSTRUCTURE,
1435 * -FDT_ERR_TRUNCATED, standard meanings
1436 */
fdt_setprop_inplace_u32(void * fdt,int nodeoffset,const char * name,uint32_t val)1437 static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1438 const char *name, uint32_t val)
1439 {
1440 fdt32_t tmp = cpu_to_fdt32(val);
1441 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1442 }
1443
1444 /**
1445 * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
1446 * @fdt: pointer to the device tree blob
1447 * @nodeoffset: offset of the node whose property to change
1448 * @name: name of the property to change
1449 * @val: 64-bit integer value to replace the property with
1450 *
1451 * fdt_setprop_inplace_u64() replaces the value of a given property
1452 * with the 64-bit integer value in val, converting val to big-endian
1453 * if necessary. This function cannot change the size of a property,
1454 * and so will only work if the property already exists and has length
1455 * 8.
1456 *
1457 * This function will alter only the bytes in the blob which contain
1458 * the given property value, and will not alter or move any other part
1459 * of the tree.
1460 *
1461 * returns:
1462 * 0, on success
1463 * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
1464 * -FDT_ERR_NOTFOUND, node does not have the named property
1465 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1466 * -FDT_ERR_BADMAGIC,
1467 * -FDT_ERR_BADVERSION,
1468 * -FDT_ERR_BADSTATE,
1469 * -FDT_ERR_BADSTRUCTURE,
1470 * -FDT_ERR_TRUNCATED, standard meanings
1471 */
fdt_setprop_inplace_u64(void * fdt,int nodeoffset,const char * name,uint64_t val)1472 static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1473 const char *name, uint64_t val)
1474 {
1475 fdt64_t tmp = cpu_to_fdt64(val);
1476 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1477 }
1478
1479 /**
1480 * fdt_setprop_inplace_cell - change the value of a single-cell property
1481 * @fdt: pointer to the device tree blob
1482 * @nodeoffset: offset of the node containing the property
1483 * @name: name of the property to change the value of
1484 * @val: new value of the 32-bit cell
1485 *
1486 * This is an alternative name for fdt_setprop_inplace_u32()
1487 * Return: 0 on success, negative libfdt error number otherwise.
1488 */
fdt_setprop_inplace_cell(void * fdt,int nodeoffset,const char * name,uint32_t val)1489 static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1490 const char *name, uint32_t val)
1491 {
1492 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1493 }
1494
1495 /**
1496 * fdt_nop_property - replace a property with nop tags
1497 * @fdt: pointer to the device tree blob
1498 * @nodeoffset: offset of the node whose property to nop
1499 * @name: name of the property to nop
1500 *
1501 * fdt_nop_property() will replace a given property's representation
1502 * in the blob with FDT_NOP tags, effectively removing it from the
1503 * tree.
1504 *
1505 * This function will alter only the bytes in the blob which contain
1506 * the property, and will not alter or move any other part of the
1507 * tree.
1508 *
1509 * returns:
1510 * 0, on success
1511 * -FDT_ERR_NOTFOUND, node does not have the named property
1512 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1513 * -FDT_ERR_BADMAGIC,
1514 * -FDT_ERR_BADVERSION,
1515 * -FDT_ERR_BADSTATE,
1516 * -FDT_ERR_BADSTRUCTURE,
1517 * -FDT_ERR_TRUNCATED, standard meanings
1518 */
1519 int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1520
1521 /**
1522 * fdt_nop_node - replace a node (subtree) with nop tags
1523 * @fdt: pointer to the device tree blob
1524 * @nodeoffset: offset of the node to nop
1525 *
1526 * fdt_nop_node() will replace a given node's representation in the
1527 * blob, including all its subnodes, if any, with FDT_NOP tags,
1528 * effectively removing it from the tree.
1529 *
1530 * This function will alter only the bytes in the blob which contain
1531 * the node and its properties and subnodes, and will not alter or
1532 * move any other part of the tree.
1533 *
1534 * returns:
1535 * 0, on success
1536 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1537 * -FDT_ERR_BADMAGIC,
1538 * -FDT_ERR_BADVERSION,
1539 * -FDT_ERR_BADSTATE,
1540 * -FDT_ERR_BADSTRUCTURE,
1541 * -FDT_ERR_TRUNCATED, standard meanings
1542 */
1543 int fdt_nop_node(void *fdt, int nodeoffset);
1544
1545 /**********************************************************************/
1546 /* Sequential write functions */
1547 /**********************************************************************/
1548
1549 /* fdt_create_with_flags flags */
1550 #define FDT_CREATE_FLAG_NO_NAME_DEDUP 0x1
1551 /* FDT_CREATE_FLAG_NO_NAME_DEDUP: Do not try to de-duplicate property
1552 * names in the fdt. This can result in faster creation times, but
1553 * a larger fdt. */
1554
1555 #define FDT_CREATE_FLAGS_ALL (FDT_CREATE_FLAG_NO_NAME_DEDUP)
1556
1557 /**
1558 * fdt_create_with_flags - begin creation of a new fdt
1559 * @buf: pointer to memory allocated where fdt will be created
1560 * @bufsize: size of the memory space at fdt
1561 * @flags: a valid combination of FDT_CREATE_FLAG_ flags, or 0.
1562 *
1563 * fdt_create_with_flags() begins the process of creating a new fdt with
1564 * the sequential write interface.
1565 *
1566 * fdt creation process must end with fdt_finish() to produce a valid fdt.
1567 *
1568 * returns:
1569 * 0, on success
1570 * -FDT_ERR_NOSPACE, bufsize is insufficient for a minimal fdt
1571 * -FDT_ERR_BADFLAGS, flags is not valid
1572 */
1573 int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags);
1574
1575 /**
1576 * fdt_create - begin creation of a new fdt
1577 * @buf: pointer to memory allocated where fdt will be created
1578 * @bufsize: size of the memory space at fdt
1579 *
1580 * fdt_create() is equivalent to fdt_create_with_flags() with flags=0.
1581 *
1582 * returns:
1583 * 0, on success
1584 * -FDT_ERR_NOSPACE, bufsize is insufficient for a minimal fdt
1585 */
1586 int fdt_create(void *buf, int bufsize);
1587
1588 /**
1589 * fdt_resize - move and resize a device tree in sequential write state
1590 * @fdt: Pointer to the device tree to resize
1591 * @buf: Buffer where resized tree should be placed
1592 * @bufsize: Size of the buffer at @buf
1593 *
1594 * fdt_resize() moves the device tree blob from @fdt to @buf and
1595 * resizes it to fit in the new buffer size.
1596 *
1597 * returns:
1598 * 0, on success
1599 * -FDT_ERR_NOSPACE, if @bufsize is too small
1600 * -FDT_ERR_BADMAGIC,
1601 * -FDT_ERR_BADVERSION,
1602 * -FDT_ERR_BADSTATE, standard meanings
1603 */
1604 int fdt_resize(void *fdt, void *buf, int bufsize);
1605
1606 /**
1607 * fdt_add_reservemap_entry - add an entry to the memory reserve map
1608 * @fdt: Pointer to the device tree blob
1609 * @addr: Start address of the reserve map entry
1610 * @size: Size of the reserved region
1611 *
1612 * fdt_add_reservemap_entry() adds a memory reserve map entry to the
1613 * device tree blob during the sequential write process. This function
1614 * can only be called after fdt_create() and before fdt_finish_reservemap().
1615 *
1616 * returns:
1617 * 0, on success
1618 * -FDT_ERR_NOSPACE, if there is insufficient space in the blob
1619 * -FDT_ERR_BADSTATE, if not in the correct sequential write state
1620 */
1621 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1622
1623 /**
1624 * fdt_finish_reservemap - complete the memory reserve map
1625 * @fdt: Pointer to the device tree blob
1626 *
1627 * fdt_finish_reservemap() completes the memory reserve map section
1628 * of the device tree blob during sequential write. After calling this
1629 * function, no more reserve map entries can be added and the blob
1630 * moves to the structure creation phase.
1631 *
1632 * returns:
1633 * 0, on success
1634 * -FDT_ERR_BADSTATE, if not in the correct sequential write state
1635 */
1636 int fdt_finish_reservemap(void *fdt);
1637
1638 /**
1639 * fdt_begin_node - start creation of a new node
1640 * @fdt: Pointer to the device tree blob
1641 * @name: Name of the node to create
1642 *
1643 * fdt_begin_node() starts the creation of a new node with the given
1644 * @name during sequential write. After calling this function, properties
1645 * can be added with fdt_property() and subnodes can be created with
1646 * additional fdt_begin_node() calls. The node must be completed with
1647 * fdt_end_node().
1648 *
1649 * returns:
1650 * 0, on success
1651 * -FDT_ERR_NOSPACE, if there is insufficient space in the blob
1652 * -FDT_ERR_BADSTATE, if not in the correct sequential write state
1653 */
1654 int fdt_begin_node(void *fdt, const char *name);
1655
1656 /**
1657 * fdt_property - add a property to the current node
1658 * @fdt: Pointer to the device tree blob
1659 * @name: Name of the property to add
1660 * @val: Pointer to the property value
1661 * @len: Length of the property value in bytes
1662 *
1663 * fdt_property() adds a property with the given @name and value to
1664 * the current node during sequential write. This function can only
1665 * be called between fdt_begin_node() and fdt_end_node().
1666 *
1667 * returns:
1668 * 0, on success
1669 * -FDT_ERR_NOSPACE, if there is insufficient space in the blob
1670 * -FDT_ERR_BADSTATE, if not currently within a node
1671 */
1672 int fdt_property(void *fdt, const char *name, const void *val, int len);
fdt_property_u32(void * fdt,const char * name,uint32_t val)1673 static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1674 {
1675 fdt32_t tmp = cpu_to_fdt32(val);
1676 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1677 }
fdt_property_u64(void * fdt,const char * name,uint64_t val)1678 static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1679 {
1680 fdt64_t tmp = cpu_to_fdt64(val);
1681 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1682 }
1683
1684 #ifndef SWIG /* Not available in Python */
fdt_property_cell(void * fdt,const char * name,uint32_t val)1685 static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1686 {
1687 return fdt_property_u32(fdt, name, val);
1688 }
1689 #endif
1690
1691 /**
1692 * fdt_property_placeholder - add a new property and return a ptr to its value
1693 *
1694 * @fdt: pointer to the device tree blob
1695 * @name: name of property to add
1696 * @len: length of property value in bytes
1697 * @valp: returns a pointer to where the value should be placed
1698 *
1699 * returns:
1700 * 0, on success
1701 * -FDT_ERR_BADMAGIC,
1702 * -FDT_ERR_NOSPACE, standard meanings
1703 */
1704 int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
1705
1706 #define fdt_property_string(fdt, name, str) \
1707 fdt_property(fdt, name, str, strlen(str)+1)
1708
1709 /**
1710 * fdt_end_node - complete the current node
1711 * @fdt: Pointer to the device tree blob
1712 *
1713 * fdt_end_node() completes the current node during sequential write. This
1714 * function must be called to close each node started with
1715 * fdt_begin_node(). After calling this function, no more properties or subnodes
1716 * can be added to the node.
1717 *
1718 * returns:
1719 * 0, on success
1720 * -FDT_ERR_BADSTATE, if not currently within a node
1721 */
1722 int fdt_end_node(void *fdt);
1723
1724 /**
1725 * fdt_finish - complete device tree creation
1726 * @fdt: Pointer to the device tree blob
1727 *
1728 * fdt_finish() completes the device tree creation process started with
1729 * fdt_create(). This function finalizes the device tree blob and makes it ready
1730 * for use. After calling this function, the blob is complete and can be used
1731 * with libfdt read-only and read-write functions, but not with sequential write
1732 * functions.
1733 *
1734 * returns:
1735 * 0, on success
1736 * -FDT_ERR_BADSTATE, if the sequential write process is incomplete
1737 */
1738 int fdt_finish(void *fdt);
1739
1740 /**********************************************************************/
1741 /* Read-write functions */
1742 /**********************************************************************/
1743
1744 /**
1745 * fdt_create_empty_tree - create an empty device tree
1746 * @buf: Buffer where the empty tree should be created
1747 * @bufsize: Size of the buffer at @buf
1748 *
1749 * fdt_create_empty_tree() creates a minimal empty device tree blob
1750 * in the given buffer. The tree contains only a root node with no
1751 * properties or subnodes.
1752 *
1753 * returns:
1754 * 0, on success
1755 * -FDT_ERR_NOSPACE, if @bufsize is too small for even an empty tree
1756 */
1757 int fdt_create_empty_tree(void *buf, int bufsize);
1758
1759 /**
1760 * fdt_open_into - move a device tree into a new buffer and make editable
1761 * @fdt: Pointer to the device tree to move
1762 * @buf: Buffer where the editable tree should be placed
1763 * @bufsize: Size of the buffer at @buf
1764 *
1765 * fdt_open_into() moves and reorganizes the device tree blob from @fdt
1766 * into @buf, converting it to a format suitable for read-write operations.
1767 * The new buffer should allow space for modifications.
1768 *
1769 * returns:
1770 * 0, on success
1771 * -FDT_ERR_NOSPACE, if @bufsize is too small
1772 * -FDT_ERR_BADMAGIC,
1773 * -FDT_ERR_BADVERSION,
1774 * -FDT_ERR_BADSTATE,
1775 * -FDT_ERR_BADSTRUCTURE,
1776 * -FDT_ERR_TRUNCATED, standard meanings
1777 */
1778 int fdt_open_into(const void *fdt, void *buf, int bufsize);
1779
1780 /**
1781 * fdt_pack - pack a device tree blob
1782 * @fdt: Pointer to the device tree blob
1783 *
1784 * fdt_pack() reorganizes the device tree blob to eliminate any free space
1785 * and pack it into the minimum possible size. This is useful after making
1786 * modifications that might have left gaps in the blob.
1787 *
1788 * returns:
1789 * 0, on success
1790 * -FDT_ERR_BADMAGIC,
1791 * -FDT_ERR_BADVERSION,
1792 * -FDT_ERR_BADSTATE,
1793 * -FDT_ERR_BADSTRUCTURE,
1794 * -FDT_ERR_BADLAYOUT, standard meanings
1795 */
1796 int fdt_pack(void *fdt);
1797
1798 /**
1799 * fdt_add_mem_rsv - add one memory reserve map entry
1800 * @fdt: pointer to the device tree blob
1801 * @address: 64-bit start address of the reserve map entry
1802 * @size: 64-bit size of the reserved region
1803 *
1804 * Adds a reserve map entry to the given blob reserving a region at
1805 * address address of length size.
1806 *
1807 * This function will insert data into the reserve map and will
1808 * therefore change the indexes of some entries in the table.
1809 *
1810 * returns:
1811 * 0, on success
1812 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1813 * contain the new reservation entry
1814 * -FDT_ERR_BADMAGIC,
1815 * -FDT_ERR_BADVERSION,
1816 * -FDT_ERR_BADSTATE,
1817 * -FDT_ERR_BADSTRUCTURE,
1818 * -FDT_ERR_BADLAYOUT,
1819 * -FDT_ERR_TRUNCATED, standard meanings
1820 */
1821 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1822
1823 /**
1824 * fdt_del_mem_rsv - remove a memory reserve map entry
1825 * @fdt: pointer to the device tree blob
1826 * @n: entry to remove
1827 *
1828 * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
1829 * the blob.
1830 *
1831 * This function will delete data from the reservation table and will
1832 * therefore change the indexes of some entries in the table.
1833 *
1834 * returns:
1835 * 0, on success
1836 * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
1837 * are less than n+1 reserve map entries)
1838 * -FDT_ERR_BADMAGIC,
1839 * -FDT_ERR_BADVERSION,
1840 * -FDT_ERR_BADSTATE,
1841 * -FDT_ERR_BADSTRUCTURE,
1842 * -FDT_ERR_BADLAYOUT,
1843 * -FDT_ERR_TRUNCATED, standard meanings
1844 */
1845 int fdt_del_mem_rsv(void *fdt, int n);
1846
1847 /**
1848 * fdt_set_name - change the name of a given node
1849 * @fdt: pointer to the device tree blob
1850 * @nodeoffset: structure block offset of a node
1851 * @name: name to give the node
1852 *
1853 * fdt_set_name() replaces the name (including unit address, if any)
1854 * of the given node with the given string. NOTE: this function can't
1855 * efficiently check if the new name is unique amongst the given
1856 * node's siblings; results are undefined if this function is invoked
1857 * with a name equal to one of the given node's siblings.
1858 *
1859 * This function may insert or delete data from the blob, and will
1860 * therefore change the offsets of some existing nodes.
1861 *
1862 * returns:
1863 * 0, on success
1864 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
1865 * to contain the new name
1866 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1867 * -FDT_ERR_BADMAGIC,
1868 * -FDT_ERR_BADVERSION,
1869 * -FDT_ERR_BADSTATE, standard meanings
1870 */
1871 int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1872
1873 /**
1874 * fdt_setprop_namelen - create or change a property
1875 * @fdt: pointer to the device tree blob
1876 * @nodeoffset: offset of the node whose property to change
1877 * @name: name of the property to change
1878 * @namelen: length of the name
1879 * @val: pointer to data to set the property value to
1880 * @len: length of the property value
1881 *
1882 * fdt_setprop_namelen() sets the value of the named property in the given
1883 * node to the given value and length, creating the property if it
1884 * does not already exist.
1885 *
1886 * This function may insert or delete data from the blob, and will
1887 * therefore change the offsets of some existing nodes.
1888 *
1889 * returns:
1890 * 0, on success
1891 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1892 * contain the new property value
1893 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1894 * -FDT_ERR_BADLAYOUT,
1895 * -FDT_ERR_BADMAGIC,
1896 * -FDT_ERR_BADVERSION,
1897 * -FDT_ERR_BADSTATE,
1898 * -FDT_ERR_BADSTRUCTURE,
1899 * -FDT_ERR_BADLAYOUT,
1900 * -FDT_ERR_TRUNCATED, standard meanings
1901 */
1902 int fdt_setprop_namelen(void *fdt, int nodeoffset, const char *name,
1903 int namelen, const void *val, int len);
1904
1905 /**
1906 * fdt_setprop - create or change a property
1907 * @fdt: pointer to the device tree blob
1908 * @nodeoffset: offset of the node whose property to change
1909 * @name: name of the property to change
1910 * @val: pointer to data to set the property value to
1911 * @len: length of the property value
1912 *
1913 * fdt_setprop() sets the value of the named property in the given
1914 * node to the given value and length, creating the property if it
1915 * does not already exist.
1916 *
1917 * This function may insert or delete data from the blob, and will
1918 * therefore change the offsets of some existing nodes.
1919 *
1920 * returns:
1921 * 0, on success
1922 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1923 * contain the new property value
1924 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1925 * -FDT_ERR_BADLAYOUT,
1926 * -FDT_ERR_BADMAGIC,
1927 * -FDT_ERR_BADVERSION,
1928 * -FDT_ERR_BADSTATE,
1929 * -FDT_ERR_BADSTRUCTURE,
1930 * -FDT_ERR_BADLAYOUT,
1931 * -FDT_ERR_TRUNCATED, standard meanings
1932 */
fdt_setprop(void * fdt,int nodeoffset,const char * name,const void * val,int len)1933 static inline int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1934 const void *val, int len)
1935 {
1936 return fdt_setprop_namelen(fdt, nodeoffset, name, strlen(name), val,
1937 len);
1938 }
1939
1940 /**
1941 * fdt_setprop_placeholder_namelen - allocate space for a property
1942 * @fdt: pointer to the device tree blob
1943 * @nodeoffset: offset of the node whose property to change
1944 * @name: name of the property to change
1945 * @namelen: length of the name
1946 * @len: length of the property value
1947 * @prop_data: return pointer to property data
1948 *
1949 * fdt_setprop_placeholder_namelen() allocates the named property in the given node.
1950 * If the property exists it is resized. In either case a pointer to the
1951 * property data is returned.
1952 *
1953 * This function may insert or delete data from the blob, and will
1954 * therefore change the offsets of some existing nodes.
1955 *
1956 * returns:
1957 * 0, on success
1958 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1959 * contain the new property value
1960 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1961 * -FDT_ERR_BADLAYOUT,
1962 * -FDT_ERR_BADMAGIC,
1963 * -FDT_ERR_BADVERSION,
1964 * -FDT_ERR_BADSTATE,
1965 * -FDT_ERR_BADSTRUCTURE,
1966 * -FDT_ERR_BADLAYOUT,
1967 * -FDT_ERR_TRUNCATED, standard meanings
1968 */
1969 int fdt_setprop_placeholder_namelen(void *fdt, int nodeoffset, const char *name,
1970 int namelen, int len, void **prop_data);
1971
1972 /**
1973 * fdt_setprop_placeholder - allocate space for a property
1974 * @fdt: pointer to the device tree blob
1975 * @nodeoffset: offset of the node whose property to change
1976 * @name: name of the property to change
1977 * @len: length of the property value
1978 * @prop_data: return pointer to property data
1979 *
1980 * fdt_setprop_placeholder() allocates the named property in the given node.
1981 * If the property exists it is resized. In either case a pointer to the
1982 * property data is returned.
1983 *
1984 * This function may insert or delete data from the blob, and will
1985 * therefore change the offsets of some existing nodes.
1986 *
1987 * returns:
1988 * 0, on success
1989 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
1990 * contain the new property value
1991 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
1992 * -FDT_ERR_BADLAYOUT,
1993 * -FDT_ERR_BADMAGIC,
1994 * -FDT_ERR_BADVERSION,
1995 * -FDT_ERR_BADSTATE,
1996 * -FDT_ERR_BADSTRUCTURE,
1997 * -FDT_ERR_BADLAYOUT,
1998 * -FDT_ERR_TRUNCATED, standard meanings
1999 */
fdt_setprop_placeholder(void * fdt,int nodeoffset,const char * name,int len,void ** prop_data)2000 static inline int fdt_setprop_placeholder(void *fdt, int nodeoffset,
2001 const char *name, int len,
2002 void **prop_data)
2003 {
2004 return fdt_setprop_placeholder_namelen(fdt, nodeoffset, name,
2005 strlen(name), len, prop_data);
2006 }
2007
2008 /**
2009 * fdt_setprop_u32 - set a property to a 32-bit integer
2010 * @fdt: pointer to the device tree blob
2011 * @nodeoffset: offset of the node whose property to change
2012 * @name: name of the property to change
2013 * @val: 32-bit integer value for the property (native endian)
2014 *
2015 * fdt_setprop_u32() sets the value of the named property in the given
2016 * node to the given 32-bit integer value (converting to big-endian if
2017 * necessary), or creates a new property with that value if it does
2018 * not already exist.
2019 *
2020 * This function may insert or delete data from the blob, and will
2021 * therefore change the offsets of some existing nodes.
2022 *
2023 * returns:
2024 * 0, on success
2025 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2026 * contain the new property value
2027 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2028 * -FDT_ERR_BADLAYOUT,
2029 * -FDT_ERR_BADMAGIC,
2030 * -FDT_ERR_BADVERSION,
2031 * -FDT_ERR_BADSTATE,
2032 * -FDT_ERR_BADSTRUCTURE,
2033 * -FDT_ERR_BADLAYOUT,
2034 * -FDT_ERR_TRUNCATED, standard meanings
2035 */
fdt_setprop_u32(void * fdt,int nodeoffset,const char * name,uint32_t val)2036 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
2037 uint32_t val)
2038 {
2039 fdt32_t tmp = cpu_to_fdt32(val);
2040 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
2041 }
2042
2043 /**
2044 * fdt_setprop_u64 - set a property to a 64-bit integer
2045 * @fdt: pointer to the device tree blob
2046 * @nodeoffset: offset of the node whose property to change
2047 * @name: name of the property to change
2048 * @val: 64-bit integer value for the property (native endian)
2049 *
2050 * fdt_setprop_u64() sets the value of the named property in the given
2051 * node to the given 64-bit integer value (converting to big-endian if
2052 * necessary), or creates a new property with that value if it does
2053 * not already exist.
2054 *
2055 * This function may insert or delete data from the blob, and will
2056 * therefore change the offsets of some existing nodes.
2057 *
2058 * returns:
2059 * 0, on success
2060 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2061 * contain the new property value
2062 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2063 * -FDT_ERR_BADLAYOUT,
2064 * -FDT_ERR_BADMAGIC,
2065 * -FDT_ERR_BADVERSION,
2066 * -FDT_ERR_BADSTATE,
2067 * -FDT_ERR_BADSTRUCTURE,
2068 * -FDT_ERR_BADLAYOUT,
2069 * -FDT_ERR_TRUNCATED, standard meanings
2070 */
fdt_setprop_u64(void * fdt,int nodeoffset,const char * name,uint64_t val)2071 static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
2072 uint64_t val)
2073 {
2074 fdt64_t tmp = cpu_to_fdt64(val);
2075 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
2076 }
2077
2078 /**
2079 * fdt_setprop_cell - set a property to a single cell value
2080 * @fdt: pointer to the device tree blob
2081 * @nodeoffset: offset of the node whose property to change
2082 * @name: name of the property to change
2083 * @val: 32-bit integer value for the property (native endian)
2084 *
2085 * This is an alternative name for fdt_setprop_u32()
2086 *
2087 * Return: 0 on success, negative libfdt error value otherwise.
2088 */
fdt_setprop_cell(void * fdt,int nodeoffset,const char * name,uint32_t val)2089 static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
2090 uint32_t val)
2091 {
2092 return fdt_setprop_u32(fdt, nodeoffset, name, val);
2093 }
2094
2095 /**
2096 * fdt_setprop_string - set a property to a string value
2097 * @fdt: pointer to the device tree blob
2098 * @nodeoffset: offset of the node whose property to change
2099 * @name: name of the property to change
2100 * @str: string value for the property
2101 *
2102 * fdt_setprop_string() sets the value of the named property in the
2103 * given node to the given string value (using the length of the
2104 * string to determine the new length of the property), or creates a
2105 * new property with that value if it does not already exist.
2106 *
2107 * This function may insert or delete data from the blob, and will
2108 * therefore change the offsets of some existing nodes.
2109 *
2110 * returns:
2111 * 0, on success
2112 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2113 * contain the new property value
2114 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2115 * -FDT_ERR_BADLAYOUT,
2116 * -FDT_ERR_BADMAGIC,
2117 * -FDT_ERR_BADVERSION,
2118 * -FDT_ERR_BADSTATE,
2119 * -FDT_ERR_BADSTRUCTURE,
2120 * -FDT_ERR_BADLAYOUT,
2121 * -FDT_ERR_TRUNCATED, standard meanings
2122 */
2123 #define fdt_setprop_string(fdt, nodeoffset, name, str) \
2124 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
2125
2126 /**
2127 * fdt_setprop_namelen_string - set a property to a string value
2128 * @fdt: pointer to the device tree blob
2129 * @nodeoffset: offset of the node whose property to change
2130 * @name: name of the property to change
2131 * @namelen: number of characters of name to consider
2132 * @str: string value for the property
2133 *
2134 * fdt_setprop_namelen_string() sets the value of the named property in the
2135 * given node to the given string value (using the length of the
2136 * string to determine the new length of the property), or creates a
2137 * new property with that value if it does not already exist.
2138 *
2139 * This function may insert or delete data from the blob, and will
2140 * therefore change the offsets of some existing nodes.
2141 *
2142 * returns:
2143 * 0, on success
2144 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2145 * contain the new property value
2146 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2147 * -FDT_ERR_BADLAYOUT,
2148 * -FDT_ERR_BADMAGIC,
2149 * -FDT_ERR_BADVERSION,
2150 * -FDT_ERR_BADSTATE,
2151 * -FDT_ERR_BADSTRUCTURE,
2152 * -FDT_ERR_BADLAYOUT,
2153 * -FDT_ERR_TRUNCATED, standard meanings
2154 */
2155 #define fdt_setprop_namelen_string(fdt, nodeoffset, name, namelen, str) \
2156 fdt_setprop_namelen((fdt), (nodeoffset), (name), (namelen), (str), \
2157 strlen(str) + 1)
2158
2159 /**
2160 * fdt_setprop_empty - set a property to an empty value
2161 * @fdt: pointer to the device tree blob
2162 * @nodeoffset: offset of the node whose property to change
2163 * @name: name of the property to change
2164 *
2165 * fdt_setprop_empty() sets the value of the named property in the
2166 * given node to an empty (zero length) value, or creates a new empty
2167 * property if it does not already exist.
2168 *
2169 * This function may insert or delete data from the blob, and will
2170 * therefore change the offsets of some existing nodes.
2171 *
2172 * returns:
2173 * 0, on success
2174 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2175 * contain the new property value
2176 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2177 * -FDT_ERR_BADLAYOUT,
2178 * -FDT_ERR_BADMAGIC,
2179 * -FDT_ERR_BADVERSION,
2180 * -FDT_ERR_BADSTATE,
2181 * -FDT_ERR_BADSTRUCTURE,
2182 * -FDT_ERR_BADLAYOUT,
2183 * -FDT_ERR_TRUNCATED, standard meanings
2184 */
2185 #define fdt_setprop_empty(fdt, nodeoffset, name) \
2186 fdt_setprop((fdt), (nodeoffset), (name), NULL, 0)
2187
2188 /**
2189 * fdt_appendprop - append to or create a property
2190 * @fdt: pointer to the device tree blob
2191 * @nodeoffset: offset of the node whose property to change
2192 * @name: name of the property to append to
2193 * @val: pointer to data to append to the property value
2194 * @len: length of the data to append to the property value
2195 *
2196 * fdt_appendprop() appends the value to the named property in the
2197 * given node, creating the property if it does not already exist.
2198 *
2199 * This function may insert data into the blob, and will therefore
2200 * change the offsets of some existing nodes.
2201 *
2202 * returns:
2203 * 0, on success
2204 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2205 * contain the new property value
2206 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2207 * -FDT_ERR_BADLAYOUT,
2208 * -FDT_ERR_BADMAGIC,
2209 * -FDT_ERR_BADVERSION,
2210 * -FDT_ERR_BADSTATE,
2211 * -FDT_ERR_BADSTRUCTURE,
2212 * -FDT_ERR_BADLAYOUT,
2213 * -FDT_ERR_TRUNCATED, standard meanings
2214 */
2215 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
2216 const void *val, int len);
2217
2218 /**
2219 * fdt_appendprop_u32 - append a 32-bit integer value to a property
2220 * @fdt: pointer to the device tree blob
2221 * @nodeoffset: offset of the node whose property to change
2222 * @name: name of the property to change
2223 * @val: 32-bit integer value to append to the property (native endian)
2224 *
2225 * fdt_appendprop_u32() appends the given 32-bit integer value
2226 * (converting to big-endian if necessary) to the value of the named
2227 * property in the given node, or creates a new property with that
2228 * value if it does not already exist.
2229 *
2230 * This function may insert data into the blob, and will therefore
2231 * change the offsets of some existing nodes.
2232 *
2233 * returns:
2234 * 0, on success
2235 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2236 * contain the new property value
2237 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2238 * -FDT_ERR_BADLAYOUT,
2239 * -FDT_ERR_BADMAGIC,
2240 * -FDT_ERR_BADVERSION,
2241 * -FDT_ERR_BADSTATE,
2242 * -FDT_ERR_BADSTRUCTURE,
2243 * -FDT_ERR_BADLAYOUT,
2244 * -FDT_ERR_TRUNCATED, standard meanings
2245 */
fdt_appendprop_u32(void * fdt,int nodeoffset,const char * name,uint32_t val)2246 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
2247 const char *name, uint32_t val)
2248 {
2249 fdt32_t tmp = cpu_to_fdt32(val);
2250 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
2251 }
2252
2253 /**
2254 * fdt_appendprop_u64 - append a 64-bit integer value to a property
2255 * @fdt: pointer to the device tree blob
2256 * @nodeoffset: offset of the node whose property to change
2257 * @name: name of the property to change
2258 * @val: 64-bit integer value to append to the property (native endian)
2259 *
2260 * fdt_appendprop_u64() appends the given 64-bit integer value
2261 * (converting to big-endian if necessary) to the value of the named
2262 * property in the given node, or creates a new property with that
2263 * value if it does not already exist.
2264 *
2265 * This function may insert data into the blob, and will therefore
2266 * change the offsets of some existing nodes.
2267 *
2268 * returns:
2269 * 0, on success
2270 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2271 * contain the new property value
2272 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2273 * -FDT_ERR_BADLAYOUT,
2274 * -FDT_ERR_BADMAGIC,
2275 * -FDT_ERR_BADVERSION,
2276 * -FDT_ERR_BADSTATE,
2277 * -FDT_ERR_BADSTRUCTURE,
2278 * -FDT_ERR_BADLAYOUT,
2279 * -FDT_ERR_TRUNCATED, standard meanings
2280 */
fdt_appendprop_u64(void * fdt,int nodeoffset,const char * name,uint64_t val)2281 static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
2282 const char *name, uint64_t val)
2283 {
2284 fdt64_t tmp = cpu_to_fdt64(val);
2285 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
2286 }
2287
2288 /**
2289 * fdt_appendprop_cell - append a single cell value to a property
2290 * @fdt: pointer to the device tree blob
2291 * @nodeoffset: offset of the node whose property to change
2292 * @name: name of the property to change
2293 * @val: 32-bit integer value to append to the property (native endian)
2294 *
2295 * This is an alternative name for fdt_appendprop_u32()
2296 *
2297 * Return: 0 on success, negative libfdt error value otherwise.
2298 */
fdt_appendprop_cell(void * fdt,int nodeoffset,const char * name,uint32_t val)2299 static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
2300 const char *name, uint32_t val)
2301 {
2302 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
2303 }
2304
2305 /**
2306 * fdt_appendprop_string - append a string to a property
2307 * @fdt: pointer to the device tree blob
2308 * @nodeoffset: offset of the node whose property to change
2309 * @name: name of the property to change
2310 * @str: string value to append to the property
2311 *
2312 * fdt_appendprop_string() appends the given string to the value of
2313 * the named property in the given node, or creates a new property
2314 * with that value if it does not already exist.
2315 *
2316 * This function may insert data into the blob, and will therefore
2317 * change the offsets of some existing nodes.
2318 *
2319 * returns:
2320 * 0, on success
2321 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2322 * contain the new property value
2323 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2324 * -FDT_ERR_BADLAYOUT,
2325 * -FDT_ERR_BADMAGIC,
2326 * -FDT_ERR_BADVERSION,
2327 * -FDT_ERR_BADSTATE,
2328 * -FDT_ERR_BADSTRUCTURE,
2329 * -FDT_ERR_BADLAYOUT,
2330 * -FDT_ERR_TRUNCATED, standard meanings
2331 */
2332 #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
2333 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
2334
2335 /**
2336 * fdt_appendprop_addrrange - append a address range property
2337 * @fdt: pointer to the device tree blob
2338 * @parent: offset of the parent node
2339 * @nodeoffset: offset of the node to add a property at
2340 * @name: name of property
2341 * @addr: start address of a given range
2342 * @size: size of a given range
2343 *
2344 * fdt_appendprop_addrrange() appends an address range value (start
2345 * address and size) to the value of the named property in the given
2346 * node, or creates a new property with that value if it does not
2347 * already exist.
2348 *
2349 * Cell sizes are determined by parent's #address-cells and #size-cells.
2350 *
2351 * This function may insert data into the blob, and will therefore
2352 * change the offsets of some existing nodes.
2353 *
2354 * returns:
2355 * 0, on success
2356 * -FDT_ERR_BADLAYOUT,
2357 * -FDT_ERR_BADMAGIC,
2358 * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
2359 * #address-cells property
2360 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2361 * -FDT_ERR_BADSTATE,
2362 * -FDT_ERR_BADSTRUCTURE,
2363 * -FDT_ERR_BADVERSION,
2364 * -FDT_ERR_BADVALUE, addr or size doesn't fit to respective cells size
2365 * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
2366 * contain a new property
2367 * -FDT_ERR_TRUNCATED, standard meanings
2368 */
2369 int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
2370 const char *name, uint64_t addr, uint64_t size);
2371
2372 /**
2373 * fdt_delprop - delete a property
2374 * @fdt: pointer to the device tree blob
2375 * @nodeoffset: offset of the node whose property to nop
2376 * @name: name of the property to nop
2377 *
2378 * fdt_delprop() will delete the given property.
2379 *
2380 * This function will delete data from the blob, and will therefore
2381 * change the offsets of some existing nodes.
2382 *
2383 * returns:
2384 * 0, on success
2385 * -FDT_ERR_NOTFOUND, node does not have the named property
2386 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2387 * -FDT_ERR_BADLAYOUT,
2388 * -FDT_ERR_BADMAGIC,
2389 * -FDT_ERR_BADVERSION,
2390 * -FDT_ERR_BADSTATE,
2391 * -FDT_ERR_BADSTRUCTURE,
2392 * -FDT_ERR_TRUNCATED, standard meanings
2393 */
2394 int fdt_delprop(void *fdt, int nodeoffset, const char *name);
2395
2396 /**
2397 * fdt_add_subnode_namelen - creates a new node based on substring
2398 * @fdt: pointer to the device tree blob
2399 * @parentoffset: structure block offset of a node
2400 * @name: name of the subnode to create
2401 * @namelen: number of characters of name to consider
2402 *
2403 * Identical to fdt_add_subnode(), but use only the first @namelen
2404 * characters of @name as the name of the new node. This is useful for
2405 * creating subnodes based on a portion of a larger string, such as a
2406 * full path.
2407 *
2408 * Return: structure block offset of the created subnode (>=0),
2409 * negative libfdt error value otherwise
2410 */
2411 #ifndef SWIG /* Not available in Python */
2412 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
2413 const char *name, int namelen);
2414 #endif
2415
2416 /**
2417 * fdt_add_subnode - creates a new node
2418 * @fdt: pointer to the device tree blob
2419 * @parentoffset: structure block offset of a node
2420 * @name: name of the subnode to locate
2421 *
2422 * fdt_add_subnode() creates a new node as a subnode of the node at
2423 * structure block offset parentoffset, with the given name (which
2424 * should include the unit address, if any).
2425 *
2426 * This function will insert data into the blob, and will therefore
2427 * change the offsets of some existing nodes.
2428 *
2429 * returns:
2430 * structure block offset of the created subnode (>=0), on success
2431 * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
2432 * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
2433 * tag
2434 * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
2435 * the given name
2436 * -FDT_ERR_NOSPACE, if there is insufficient free space in the
2437 * blob to contain the new node
2438 * -FDT_ERR_NOSPACE
2439 * -FDT_ERR_BADLAYOUT
2440 * -FDT_ERR_BADMAGIC,
2441 * -FDT_ERR_BADVERSION,
2442 * -FDT_ERR_BADSTATE,
2443 * -FDT_ERR_BADSTRUCTURE,
2444 * -FDT_ERR_TRUNCATED, standard meanings.
2445 */
2446 int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
2447
2448 /**
2449 * fdt_del_node - delete a node (subtree)
2450 * @fdt: pointer to the device tree blob
2451 * @nodeoffset: offset of the node to nop
2452 *
2453 * fdt_del_node() will remove the given node, including all its
2454 * subnodes if any, from the blob.
2455 *
2456 * This function will delete data from the blob, and will therefore
2457 * change the offsets of some existing nodes.
2458 *
2459 * returns:
2460 * 0, on success
2461 * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
2462 * -FDT_ERR_BADLAYOUT,
2463 * -FDT_ERR_BADMAGIC,
2464 * -FDT_ERR_BADVERSION,
2465 * -FDT_ERR_BADSTATE,
2466 * -FDT_ERR_BADSTRUCTURE,
2467 * -FDT_ERR_TRUNCATED, standard meanings
2468 */
2469 int fdt_del_node(void *fdt, int nodeoffset);
2470
2471 /**
2472 * fdt_overlay_apply - Applies a DT overlay on a base DT
2473 * @fdt: pointer to the base device tree blob
2474 * @fdto: pointer to the device tree overlay blob
2475 *
2476 * fdt_overlay_apply() will apply the given device tree overlay on the
2477 * given base device tree.
2478 *
2479 * Expect the base device tree to be modified, even if the function
2480 * returns an error.
2481 *
2482 * returns:
2483 * 0, on success
2484 * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
2485 * -FDT_ERR_NOTFOUND, the overlay points to some nonexistent nodes or
2486 * properties in the base DT
2487 * -FDT_ERR_BADPHANDLE,
2488 * -FDT_ERR_BADOVERLAY,
2489 * -FDT_ERR_NOPHANDLES,
2490 * -FDT_ERR_INTERNAL,
2491 * -FDT_ERR_BADLAYOUT,
2492 * -FDT_ERR_BADMAGIC,
2493 * -FDT_ERR_BADOFFSET,
2494 * -FDT_ERR_BADPATH,
2495 * -FDT_ERR_BADVERSION,
2496 * -FDT_ERR_BADSTRUCTURE,
2497 * -FDT_ERR_BADSTATE,
2498 * -FDT_ERR_TRUNCATED, standard meanings
2499 */
2500 int fdt_overlay_apply(void *fdt, void *fdto);
2501
2502 /**
2503 * fdt_overlay_target_offset - retrieves the offset of a fragment's target
2504 * @fdt: Base device tree blob
2505 * @fdto: Device tree overlay blob
2506 * @fragment_offset: node offset of the fragment in the overlay
2507 * @pathp: pointer which receives the path of the target (or NULL)
2508 *
2509 * fdt_overlay_target_offset() retrieves the target offset in the base
2510 * device tree of a fragment, no matter how the actual targeting is
2511 * done (through a phandle or a path)
2512 *
2513 * returns:
2514 * the targeted node offset in the base device tree
2515 * Negative error code on error
2516 */
2517 int fdt_overlay_target_offset(const void *fdt, const void *fdto,
2518 int fragment_offset, char const **pathp);
2519
2520 /**********************************************************************/
2521 /* Debugging / informational functions */
2522 /**********************************************************************/
2523
2524 /**
2525 * fdt_strerror - return string description of error code
2526 * @errval: Error code returned by a libfdt function
2527 *
2528 * fdt_strerror() returns a string description of the error code passed
2529 * in @errval.
2530 *
2531 * returns:
2532 * pointer to a string describing the error code
2533 */
2534 const char *fdt_strerror(int errval);
2535
2536 #ifdef __cplusplus
2537 }
2538 #endif
2539
2540 #endif /* LIBFDT_H */
2541