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