RTEMS 6.1-rc2
Loading...
Searching...
No Matches
libfdt.h
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
13extern "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#ifndef SWIG /* This function is not useful in Python */
120const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
121#endif
122static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
123{
124 return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
125}
126
127uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
128
129/*
130 * External helpers to access words from a device tree blob. They're built
131 * to work even with unaligned pointers on platforms (such as ARMv5) that don't
132 * like unaligned loads and stores.
133 */
134static inline uint16_t fdt16_ld(const fdt16_t *p)
135{
136 const uint8_t *bp = (const uint8_t *)p;
137
138 return ((uint16_t)bp[0] << 8) | bp[1];
139}
140
141static inline uint32_t fdt32_ld(const fdt32_t *p)
142{
143 const uint8_t *bp = (const uint8_t *)p;
144
145 return ((uint32_t)bp[0] << 24)
146 | ((uint32_t)bp[1] << 16)
147 | ((uint32_t)bp[2] << 8)
148 | bp[3];
149}
150
151static inline void fdt32_st(void *property, uint32_t value)
152{
153 uint8_t *bp = (uint8_t *)property;
154
155 bp[0] = value >> 24;
156 bp[1] = (value >> 16) & 0xff;
157 bp[2] = (value >> 8) & 0xff;
158 bp[3] = value & 0xff;
159}
160
161static inline uint64_t fdt64_ld(const fdt64_t *p)
162{
163 const uint8_t *bp = (const uint8_t *)p;
164
165 return ((uint64_t)bp[0] << 56)
166 | ((uint64_t)bp[1] << 48)
167 | ((uint64_t)bp[2] << 40)
168 | ((uint64_t)bp[3] << 32)
169 | ((uint64_t)bp[4] << 24)
170 | ((uint64_t)bp[5] << 16)
171 | ((uint64_t)bp[6] << 8)
172 | bp[7];
173}
174
175static inline void fdt64_st(void *property, uint64_t value)
176{
177 uint8_t *bp = (uint8_t *)property;
178
179 bp[0] = value >> 56;
180 bp[1] = (value >> 48) & 0xff;
181 bp[2] = (value >> 40) & 0xff;
182 bp[3] = (value >> 32) & 0xff;
183 bp[4] = (value >> 24) & 0xff;
184 bp[5] = (value >> 16) & 0xff;
185 bp[6] = (value >> 8) & 0xff;
186 bp[7] = value & 0xff;
187}
188
189/**********************************************************************/
190/* Traversal functions */
191/**********************************************************************/
192
193int fdt_next_node(const void *fdt, int offset, int *depth);
194
202int fdt_first_subnode(const void *fdt, int offset);
203
215int fdt_next_subnode(const void *fdt, int offset);
216
239#define fdt_for_each_subnode(node, fdt, parent) \
240 for (node = fdt_first_subnode(fdt, parent); \
241 node >= 0; \
242 node = fdt_next_subnode(fdt, node))
243
244/**********************************************************************/
245/* General functions */
246/**********************************************************************/
247#define fdt_get_header(fdt, field) \
248 (fdt32_ld(&((const struct fdt_header *)(fdt))->field))
249#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
250#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
251#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
252#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
253#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
254#define fdt_version(fdt) (fdt_get_header(fdt, version))
255#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
256#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
257#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
258#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
259
260#define fdt_set_hdr_(name) \
261 static inline void fdt_set_##name(void *fdt, uint32_t val) \
262 { \
263 struct fdt_header *fdth = (struct fdt_header *)fdt; \
264 fdth->name = cpu_to_fdt32(val); \
265 }
266fdt_set_hdr_(magic);
267fdt_set_hdr_(totalsize);
268fdt_set_hdr_(off_dt_struct);
269fdt_set_hdr_(off_dt_strings);
270fdt_set_hdr_(off_mem_rsvmap);
271fdt_set_hdr_(version);
272fdt_set_hdr_(last_comp_version);
273fdt_set_hdr_(boot_cpuid_phys);
274fdt_set_hdr_(size_dt_strings);
275fdt_set_hdr_(size_dt_struct);
276#undef fdt_set_hdr_
277
284size_t fdt_header_size(const void *fdt);
285
292size_t fdt_header_size_(uint32_t version);
293
310int fdt_check_header(const void *fdt);
311
331int fdt_move(const void *fdt, void *buf, int bufsize);
332
333/**********************************************************************/
334/* Read-only functions */
335/**********************************************************************/
336
337int fdt_check_full(const void *fdt, size_t bufsize);
338
353const char *fdt_get_string(const void *fdt, int stroffset, int *lenp);
354
367const char *fdt_string(const void *fdt, int stroffset);
368
381int fdt_find_max_phandle(const void *fdt, uint32_t *phandle);
382
398static inline uint32_t fdt_get_max_phandle(const void *fdt)
399{
400 uint32_t phandle;
401 int err;
402
403 err = fdt_find_max_phandle(fdt, &phandle);
404 if (err < 0)
405 return (uint32_t)-1;
406
407 return phandle;
408}
409
422int fdt_generate_phandle(const void *fdt, uint32_t *phandle);
423
435int fdt_num_mem_rsv(const void *fdt);
436
454int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
455
470#ifndef SWIG /* Not available in Python */
471int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
472 const char *name, int namelen);
473#endif
498int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
499
511#ifndef SWIG /* Not available in Python */
512int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
513#endif
514
538int fdt_path_offset(const void *fdt, const char *path);
539
563const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
564
583int fdt_first_property_offset(const void *fdt, int nodeoffset);
584
604int fdt_next_property_offset(const void *fdt, int offset);
605
628#define fdt_for_each_property_offset(property, fdt, node) \
629 for (property = fdt_first_property_offset(fdt, node); \
630 property >= 0; \
631 property = fdt_next_property_offset(fdt, property))
632
660const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
661 int offset,
662 int *lenp);
663static inline struct fdt_property *fdt_get_property_by_offset_w(void *fdt,
664 int offset,
665 int *lenp)
666{
667 return (struct fdt_property *)(uintptr_t)
668 fdt_get_property_by_offset(fdt, offset, lenp);
669}
670
685#ifndef SWIG /* Not available in Python */
686const struct fdt_property *fdt_get_property_namelen(const void *fdt,
687 int nodeoffset,
688 const char *name,
689 int namelen, int *lenp);
690#endif
691
720const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
721 const char *name, int *lenp);
722static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
723 const char *name,
724 int *lenp)
725{
726 return (struct fdt_property *)(uintptr_t)
727 fdt_get_property(fdt, nodeoffset, name, lenp);
728}
729
761#ifndef SWIG /* This function is not useful in Python */
762const void *fdt_getprop_by_offset(const void *fdt, int offset,
763 const char **namep, int *lenp);
764#endif
765
779#ifndef SWIG /* Not available in Python */
780const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
781 const char *name, int namelen, int *lenp);
782static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
783 const char *name, int namelen,
784 int *lenp)
785{
786 return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
787 namelen, lenp);
788}
789#endif
790
819const void *fdt_getprop(const void *fdt, int nodeoffset,
820 const char *name, int *lenp);
821static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
822 const char *name, int *lenp)
823{
824 return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
825}
826
839uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
840
853#ifndef SWIG /* Not available in Python */
854const char *fdt_get_alias_namelen(const void *fdt,
855 const char *name, int namelen);
856#endif
857
870const char *fdt_get_alias(const void *fdt, const char *name);
871
897int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
898
929int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
930 int supernodedepth, int *nodedepth);
931
951int fdt_node_depth(const void *fdt, int nodeoffset);
952
974int fdt_parent_offset(const void *fdt, int nodeoffset);
975
1014int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
1015 const char *propname,
1016 const void *propval, int proplen);
1017
1037int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
1038
1060int fdt_node_check_compatible(const void *fdt, int nodeoffset,
1061 const char *compatible);
1062
1097int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
1098 const char *compatible);
1099
1112int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
1113
1125int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
1126
1146int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
1147 const char *string);
1148
1173const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
1174 const char *property, int index,
1175 int *lenp);
1176
1177/**********************************************************************/
1178/* Read-only functions (addressing related) */
1179/**********************************************************************/
1180
1190#define FDT_MAX_NCELLS 4
1191
1210int fdt_address_cells(const void *fdt, int nodeoffset);
1211
1231int fdt_size_cells(const void *fdt, int nodeoffset);
1232
1233
1234/**********************************************************************/
1235/* Write-in-place functions */
1236/**********************************************************************/
1237
1256#ifndef SWIG /* Not available in Python */
1257int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
1258 const char *name, int namelen,
1259 uint32_t idx, const void *val,
1260 int len);
1261#endif
1262
1291#ifndef SWIG /* Not available in Python */
1292int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
1293 const void *val, int len);
1294#endif
1295
1324static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
1325 const char *name, uint32_t val)
1326{
1327 fdt32_t tmp = cpu_to_fdt32(val);
1328 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1329}
1330
1359static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
1360 const char *name, uint64_t val)
1361{
1362 fdt64_t tmp = cpu_to_fdt64(val);
1363 return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1364}
1365
1376static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
1377 const char *name, uint32_t val)
1378{
1379 return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
1380}
1381
1406int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
1407
1430int fdt_nop_node(void *fdt, int nodeoffset);
1431
1432/**********************************************************************/
1433/* Sequential write functions */
1434/**********************************************************************/
1435
1436/* fdt_create_with_flags flags */
1437#define FDT_CREATE_FLAG_NO_NAME_DEDUP 0x1
1438 /* FDT_CREATE_FLAG_NO_NAME_DEDUP: Do not try to de-duplicate property
1439 * names in the fdt. This can result in faster creation times, but
1440 * a larger fdt. */
1441
1442#define FDT_CREATE_FLAGS_ALL (FDT_CREATE_FLAG_NO_NAME_DEDUP)
1443
1460int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags);
1461
1473int fdt_create(void *buf, int bufsize);
1474
1475int fdt_resize(void *fdt, void *buf, int bufsize);
1476int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
1477int fdt_finish_reservemap(void *fdt);
1478int fdt_begin_node(void *fdt, const char *name);
1479int fdt_property(void *fdt, const char *name, const void *val, int len);
1480static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
1481{
1482 fdt32_t tmp = cpu_to_fdt32(val);
1483 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1484}
1485static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
1486{
1487 fdt64_t tmp = cpu_to_fdt64(val);
1488 return fdt_property(fdt, name, &tmp, sizeof(tmp));
1489}
1490
1491#ifndef SWIG /* Not available in Python */
1492static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
1493{
1494 return fdt_property_u32(fdt, name, val);
1495}
1496#endif
1497
1511int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
1512
1513#define fdt_property_string(fdt, name, str) \
1514 fdt_property(fdt, name, str, strlen(str)+1)
1515int fdt_end_node(void *fdt);
1516int fdt_finish(void *fdt);
1517
1518/**********************************************************************/
1519/* Read-write functions */
1520/**********************************************************************/
1521
1522int fdt_create_empty_tree(void *buf, int bufsize);
1523int fdt_open_into(const void *fdt, void *buf, int bufsize);
1524int fdt_pack(void *fdt);
1525
1549int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
1550
1573int fdt_del_mem_rsv(void *fdt, int n);
1574
1599int fdt_set_name(void *fdt, int nodeoffset, const char *name);
1600
1629int fdt_setprop(void *fdt, int nodeoffset, const char *name,
1630 const void *val, int len);
1631
1660int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
1661 int len, void **prop_data);
1662
1691static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
1692 uint32_t val)
1693{
1694 fdt32_t tmp = cpu_to_fdt32(val);
1695 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1696}
1697
1726static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
1727 uint64_t val)
1728{
1729 fdt64_t tmp = cpu_to_fdt64(val);
1730 return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1731}
1732
1744static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
1745 uint32_t val)
1746{
1747 return fdt_setprop_u32(fdt, nodeoffset, name, val);
1748}
1749
1778#define fdt_setprop_string(fdt, nodeoffset, name, str) \
1779 fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1780
1781
1808#define fdt_setprop_empty(fdt, nodeoffset, name) \
1809 fdt_setprop((fdt), (nodeoffset), (name), NULL, 0)
1810
1838int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
1839 const void *val, int len);
1840
1869static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
1870 const char *name, uint32_t val)
1871{
1872 fdt32_t tmp = cpu_to_fdt32(val);
1873 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1874}
1875
1904static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
1905 const char *name, uint64_t val)
1906{
1907 fdt64_t tmp = cpu_to_fdt64(val);
1908 return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
1909}
1910
1922static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
1923 const char *name, uint32_t val)
1924{
1925 return fdt_appendprop_u32(fdt, nodeoffset, name, val);
1926}
1927
1955#define fdt_appendprop_string(fdt, nodeoffset, name, str) \
1956 fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
1957
1992int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
1993 const char *name, uint64_t addr, uint64_t size);
1994
2017int fdt_delprop(void *fdt, int nodeoffset, const char *name);
2018
2034#ifndef SWIG /* Not available in Python */
2035int fdt_add_subnode_namelen(void *fdt, int parentoffset,
2036 const char *name, int namelen);
2037#endif
2038
2070int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
2071
2093int fdt_del_node(void *fdt, int nodeoffset);
2094
2124int fdt_overlay_apply(void *fdt, void *fdto);
2125
2141int fdt_overlay_target_offset(const void *fdt, const void *fdto,
2142 int fragment_offset, char const **pathp);
2143
2144/**********************************************************************/
2145/* Debugging / informational functions */
2146/**********************************************************************/
2147
2148const char *fdt_strerror(int errval);
2149
2150#ifdef __cplusplus
2151}
2152#endif
2153
2154#endif /* LIBFDT_H */
Definition: fdt.h:41
unsigned p
Definition: tte.h:17