coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
device_tree.h
Go to the documentation of this file.
1 /* Taken from depthcharge: src/base/device_tree.h */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 
4 #ifndef __DEVICE_TREE_H__
5 #define __DEVICE_TREE_H__
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <list.h>
10 
11 /*
12  * Flattened device tree structures/constants.
13  */
14 
15 struct fdt_header {
21 
24 
26 
29 };
30 
31 #define FDT_HEADER_MAGIC 0xd00dfeed
32 #define FDT_SUPPORTED_VERSION 17
33 #define FDT_TOKEN_BEGIN_NODE 1
34 #define FDT_TOKEN_END_NODE 2
35 #define FDT_TOKEN_PROPERTY 3
36 #define FDT_TOKEN_END 9
37 #define FDT_PHANDLE_ILLEGAL 0xdeadbeef
38 
40 {
41  const char *name;
42  void *data;
44 };
45 
46 /*
47  * Unflattened device tree structures.
48  */
49 
51 {
52  struct fdt_property prop;
53 
54  struct list_node list_node;
55 };
56 
58 {
59  const char *name;
61 
62  /* List of struct device_tree_property-s. */
63  struct list_node properties;
64  /* List of struct device_tree_nodes. */
65  struct list_node children;
66 
67  struct list_node list_node;
68 };
69 
71 {
74 
75  struct list_node list_node;
76 };
77 
79 {
80  const void *header;
83 
84  struct list_node reserve_map;
85 
87 };
88 
89 /*
90  * Flattened device tree functions. These generally return the number of bytes
91  * which were consumed reading the requested value.
92  */
93 
94 /* Read the property, if any, at offset offset. */
95 int fdt_next_property(const void *blob, uint32_t offset,
96  struct fdt_property *prop);
97 /* Read the name of the node, if any, at offset offset. */
98 int fdt_node_name(const void *blob, uint32_t offset, const char **name);
99 
100 void fdt_print_node(const void *blob, uint32_t offset);
101 int fdt_skip_node(const void *blob, uint32_t offset);
102 
103 /* Read a flattened device tree into a hierarchical structure which refers to
104  the contents of the flattened tree in place. Modifying the flat tree
105  invalidates the unflattened one. */
106 struct device_tree *fdt_unflatten(const void *blob);
107 
108 /*
109  * Unflattened device tree functions.
110  */
111 
112 /* Figure out how big a device tree would be if it were flattened. */
113 uint32_t dt_flat_size(const struct device_tree *tree);
114 /* Flatten a device tree into the buffer pointed to by dest. */
115 void dt_flatten(const struct device_tree *tree, void *dest);
116 void dt_print_node(const struct device_tree_node *node);
117 /* Read #address-cells and #size-cells properties from a node. */
118 void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
119  u32 *sizecp);
120 /* Look up or create a node relative to a parent node, through its path
121  represented as an array of strings. */
122 struct device_tree_node *dt_find_node(struct device_tree_node *parent, const char **path,
123  u32 *addrcp, u32 *sizecp, int create);
125  uint32_t phandle);
126 /* Look up or create a node in the tree, through its path
127  represented as a string of '/' separated node names. */
129  const char *path, u32 *addrcp, u32 *sizecp, int create);
130 /* Look up a node through an alias. */
132  const char *alias);
133 /* Look up a node relative to a parent node, through its compatible string. */
134 struct device_tree_node *dt_find_compat(struct device_tree_node *parent, const char *compatible);
135 /* Look up the next child of a parent node, through its compatible string. It
136  uses child pointer as the marker to find next. */
138  struct device_tree_node *child,
139  const char *compat);
140 /* Look up a node relative to a parent node, through its property value. */
141 struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent, const char *name,
142  void *data, size_t size);
143 /* Write src into *dest as a 'length'-byte big-endian integer. */
144 void dt_write_int(u8 *dest, u64 src, size_t length);
145 /* Delete a property */
146 void dt_delete_prop(struct device_tree_node *node, const char *name);
147 /* Add different kinds of properties to a node, or update existing ones. */
148 void dt_add_bin_prop(struct device_tree_node *node, const char *name,
149  void *data, size_t size);
150 void dt_add_string_prop(struct device_tree_node *node, const char *name,
151  const char *str);
152 void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val);
153 void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val);
154 void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
155  int count, u32 addr_cells, u32 size_cells);
156 int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
157  void *data, size_t size, int create);
158 
159 void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
160  const void **data, size_t *size);
161 const char *dt_find_string_prop(const struct device_tree_node *node,
162  const char *name);
163 
164 /* Apply an overlay to a base device tree. Ownership of the overlay data passes
165  to the newly combined base tree -- do not free() or access it afterwards! */
166 int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay);
167 
168 /*
169  * Fixups to apply to a kernel's device tree before booting it.
170  */
171 
173 {
174  /**
175  * The function which does the fixing.
176  * 0 on success, non-zero on error.
177  */
178  int (*fixup)(struct device_tree_fixup *fixup,
179  struct device_tree *tree);
180 
181  struct list_node list_node;
182 };
183 
184 extern struct list_node device_tree_fixups;
185 
186 /**
187  * Function to apply fixups.
188  * 0 on success, non-zero on error.
189  */
190 int dt_apply_fixups(struct device_tree *tree);
191 
192 /*
193  * Init/retrieve the /reserved-memory/ node.
194  */
196 
197 #endif /* __DEVICE_TREE_H__ */
const char * name
Definition: mmu.c:92
int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
Definition: device_tree.c:1493
struct device_tree_node * dt_find_node_by_phandle(struct device_tree_node *root, uint32_t phandle)
Definition: device_tree.c:684
void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Definition: device_tree.c:957
int dt_apply_fixups(struct device_tree *tree)
Function to apply fixups.
Definition: device_tree.c:1012
void dt_print_node(const struct device_tree_node *node)
Definition: device_tree.c:488
void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp, u32 *sizecp)
Definition: device_tree.c:506
struct device_tree_node * dt_find_node(struct device_tree_node *parent, const char **path, u32 *addrcp, u32 *sizecp, int create)
Definition: device_tree.c:532
void fdt_print_node(const void *blob, uint32_t offset)
Definition: device_tree.c:134
struct device_tree_node * dt_find_next_compat_child(struct device_tree_node *parent, struct device_tree_node *child, const char *compat)
Definition: device_tree.c:772
void dt_add_bin_prop(struct device_tree_node *node, const char *name, void *data, size_t size)
Definition: device_tree.c:874
void dt_delete_prop(struct device_tree_node *node, const char *name)
Definition: device_tree.c:854
int fdt_next_property(const void *blob, uint32_t offset, struct fdt_property *prop)
Definition: device_tree.c:19
int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Definition: device_tree.c:44
struct device_tree_node * dt_init_reserved_memory_node(struct device_tree *tree)
Definition: device_tree.c:1071
uint32_t dt_flat_size(const struct device_tree *tree)
Definition: device_tree.c:332
struct device_tree_node * dt_find_prop_value(struct device_tree_node *parent, const char *name, void *data, size_t size)
Definition: device_tree.c:804
void dt_find_bin_prop(const struct device_tree_node *node, const char *name, const void **data, size_t *size)
Definition: device_tree.c:920
int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path, void *data, size_t size, int create)
Definition: device_tree.c:1023
void dt_add_string_prop(struct device_tree_node *node, const char *name, const char *str)
Definition: device_tree.c:944
void dt_flatten(const struct device_tree *tree, void *dest)
Definition: device_tree.c:424
struct device_tree_node * dt_find_compat(struct device_tree_node *parent, const char *compatible)
Definition: device_tree.c:743
struct device_tree_node * dt_find_node_by_path(struct device_tree *tree, const char *path, u32 *addrcp, u32 *sizecp, int create)
Definition: device_tree.c:586
struct device_tree * fdt_unflatten(const void *blob)
Definition: device_tree.c:237
struct device_tree_node * dt_find_node_by_alias(struct device_tree *tree, const char *alias)
Definition: device_tree.c:667
void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes, int count, u32 addr_cells, u32 size_cells)
Definition: device_tree.c:988
int fdt_skip_node(const void *blob, uint32_t offset)
Definition: device_tree.c:145
const char * dt_find_string_prop(const struct device_tree_node *node, const char *name)
Definition: device_tree.c:901
void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
Definition: device_tree.c:971
struct list_node device_tree_fixups
Definition: device_tree.c:1010
void dt_write_int(u8 *dest, u64 src, size_t length)
Definition: device_tree.c:840
static size_t offset
Definition: flashconsole.c:16
uint64_t length
Definition: fw_cfg_if.h:1
uint64_t u64
Definition: stdint.h:54
unsigned int uint32_t
Definition: stdint.h:14
uint32_t u32
Definition: stdint.h:51
unsigned long long uint64_t
Definition: stdint.h:17
uint8_t u8
Definition: stdint.h:45
int(* fixup)(struct device_tree_fixup *fixup, struct device_tree *tree)
The function which does the fixing.
Definition: device_tree.h:178
struct list_node children
Definition: device_tree.h:65
const char * name
Definition: device_tree.h:59
uint32_t phandle
Definition: device_tree.h:60
struct list_node properties
Definition: device_tree.h:63
struct fdt_property prop
Definition: device_tree.h:52
Definition: device_tree.h:71
uint64_t size
Definition: device_tree.h:73
uint64_t start
Definition: device_tree.h:72
struct list_node reserve_map
Definition: device_tree.h:84
uint32_t max_phandle
Definition: device_tree.h:82
uint32_t header_size
Definition: device_tree.h:81
const void * header
Definition: device_tree.h:80
struct device_tree_node * root
Definition: device_tree.h:86
uint32_t strings_offset
Definition: device_tree.h:19
uint32_t magic
Definition: device_tree.h:16
uint32_t reserve_map_offset
Definition: device_tree.h:20
uint32_t last_comp_version
Definition: device_tree.h:23
uint32_t strings_size
Definition: device_tree.h:27
uint32_t structure_size
Definition: device_tree.h:28
uint32_t structure_offset
Definition: device_tree.h:18
uint32_t version
Definition: device_tree.h:22
uint32_t totalsize
Definition: device_tree.h:17
uint32_t boot_cpuid_phys
Definition: device_tree.h:25
void * data
Definition: device_tree.h:42
uint32_t size
Definition: device_tree.h:43
const char * name
Definition: device_tree.h:41
Definition: list.h:7
u8 val
Definition: sys.c:300
#define count