xref: /openbmc/u-boot/include/linux/libfdt.h (revision fabbeb33)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _INCLUDE_LIBFDT_H_
3 #define _INCLUDE_LIBFDT_H_
4 
5 #ifndef USE_HOSTCC
6 #include <linux/libfdt_env.h>
7 #endif
8 #include "../../scripts/dtc/libfdt/libfdt.h"
9 
10 /* U-Boot local hacks */
11 
12 #ifndef SWIG /* Not available in Python */
13 struct fdt_region {
14 	int offset;
15 	int size;
16 };
17 
18 /*
19  * Flags for fdt_find_regions()
20  *
21  * Add a region for the string table (always the last region)
22  */
23 #define FDT_REG_ADD_STRING_TAB		(1 << 0)
24 
25 /*
26  * Add all supernodes of a matching node/property, useful for creating a
27  * valid subset tree
28  */
29 #define FDT_REG_SUPERNODES		(1 << 1)
30 
31 /* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
32 #define FDT_REG_DIRECT_SUBNODES	(1 << 2)
33 
34 /* Add all subnodes of a matching node */
35 #define FDT_REG_ALL_SUBNODES		(1 << 3)
36 
37 /* Add a region for the mem_rsvmap table (always the first region) */
38 #define FDT_REG_ADD_MEM_RSVMAP		(1 << 4)
39 
40 /* Indicates what an fdt part is (node, property, value) */
41 #define FDT_IS_NODE			(1 << 0)
42 #define FDT_IS_PROP			(1 << 1)
43 #define FDT_IS_VALUE			(1 << 2)	/* not supported */
44 #define FDT_IS_COMPAT			(1 << 3)	/* used internally */
45 #define FDT_NODE_HAS_PROP		(1 << 4)	/* node contains prop */
46 
47 #define FDT_ANY_GLOBAL		(FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
48 					FDT_IS_COMPAT)
49 #define FDT_IS_ANY			0x1f		/* all the above */
50 
51 /* We set a reasonable limit on the number of nested nodes */
52 #define FDT_MAX_DEPTH			32
53 
54 /* Decribes what we want to include from the current tag */
55 enum want_t {
56 	WANT_NOTHING,
57 	WANT_NODES_ONLY,		/* No properties */
58 	WANT_NODES_AND_PROPS,		/* Everything for one level */
59 	WANT_ALL_NODES_AND_PROPS	/* Everything for all levels */
60 };
61 
62 /* Keeps track of the state at parent nodes */
63 struct fdt_subnode_stack {
64 	int offset;		/* Offset of node */
65 	enum want_t want;	/* The 'want' value here */
66 	int included;		/* 1 if we included this node, 0 if not */
67 };
68 
69 struct fdt_region_ptrs {
70 	int depth;			/* Current tree depth */
71 	int done;			/* What we have completed scanning */
72 	enum want_t want;		/* What we are currently including */
73 	char *end;			/* Pointer to end of full node path */
74 	int nextoffset;			/* Next node offset to check */
75 };
76 
77 /* The state of our finding algortihm */
78 struct fdt_region_state {
79 	struct fdt_subnode_stack stack[FDT_MAX_DEPTH];	/* node stack */
80 	struct fdt_region *region;	/* Contains list of regions found */
81 	int count;			/* Numnber of regions found */
82 	const void *fdt;		/* FDT blob */
83 	int max_regions;		/* Maximum regions to find */
84 	int can_merge;		/* 1 if we can merge with previous region */
85 	int start;			/* Start position of current region */
86 	struct fdt_region_ptrs ptrs;	/* Pointers for what we are up to */
87 };
88 
89 /**
90  * fdt_find_regions() - find regions in device tree
91  *
92  * Given a list of nodes to include and properties to exclude, find
93  * the regions of the device tree which describe those included parts.
94  *
95  * The intent is to get a list of regions which will be invariant provided
96  * those parts are invariant. For example, if you request a list of regions
97  * for all nodes but exclude the property "data", then you will get the
98  * same region contents regardless of any change to "data" properties.
99  *
100  * This function can be used to produce a byte-stream to send to a hashing
101  * function to verify that critical parts of the FDT have not changed.
102  *
103  * Nodes which are given in 'inc' are included in the region list, as
104  * are the names of the immediate subnodes nodes (but not the properties
105  * or subnodes of those subnodes).
106  *
107  * For eaxample "/" means to include the root node, all root properties
108  * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
109  * ensures that we capture the names of the subnodes. In a hashing situation
110  * it prevents the root node from changing at all Any change to non-excluded
111  * properties, names of subnodes or number of subnodes would be detected.
112  *
113  * When used with FITs this provides the ability to hash and sign parts of
114  * the FIT based on different configurations in the FIT. Then it is
115  * impossible to change anything about that configuration (include images
116  * attached to the configuration), but it may be possible to add new
117  * configurations, new images or new signatures within the existing
118  * framework.
119  *
120  * Adding new properties to a device tree may result in the string table
121  * being extended (if the new property names are different from those
122  * already added). This function can optionally include a region for
123  * the string table so that this can be part of the hash too.
124  *
125  * The device tree header is not included in the list.
126  *
127  * @fdt:	Device tree to check
128  * @inc:	List of node paths to included
129  * @inc_count:	Number of node paths in list
130  * @exc_prop:	List of properties names to exclude
131  * @exc_prop_count:	Number of properties in exclude list
132  * @region:	Returns list of regions
133  * @max_region:	Maximum length of region list
134  * @path:	Pointer to a temporary string for the function to use for
135  *		building path names
136  * @path_len:	Length of path, must be large enough to hold the longest
137  *		path in the tree
138  * @add_string_tab:	1 to add a region for the string table
139  * @return number of regions in list. If this is >max_regions then the
140  * region array was exhausted. You should increase max_regions and try
141  * the call again.
142  */
143 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
144 		     char * const exc_prop[], int exc_prop_count,
145 		     struct fdt_region region[], int max_regions,
146 		     char *path, int path_len, int add_string_tab);
147 
148 /**
149  * fdt_first_region() - find regions in device tree
150  *
151  * Given a nodes and properties to include and properties to exclude, find
152  * the regions of the device tree which describe those included parts.
153  *
154  * The use for this function is twofold. Firstly it provides a convenient
155  * way of performing a structure-aware grep of the tree. For example it is
156  * possible to grep for a node and get all the properties associated with
157  * that node. Trees can be subsetted easily, by specifying the nodes that
158  * are required, and then writing out the regions returned by this function.
159  * This is useful for small resource-constrained systems, such as boot
160  * loaders, which want to use an FDT but do not need to know about all of
161  * it.
162  *
163  * Secondly it makes it easy to hash parts of the tree and detect changes.
164  * The intent is to get a list of regions which will be invariant provided
165  * those parts are invariant. For example, if you request a list of regions
166  * for all nodes but exclude the property "data", then you will get the
167  * same region contents regardless of any change to "data" properties.
168  *
169  * This function can be used to produce a byte-stream to send to a hashing
170  * function to verify that critical parts of the FDT have not changed.
171  * Note that semantically null changes in order could still cause false
172  * hash misses. Such reordering might happen if the tree is regenerated
173  * from source, and nodes are reordered (the bytes-stream will be emitted
174  * in a different order and many hash functions will detect this). However
175  * if an existing tree is modified using libfdt functions, such as
176  * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
177  *
178  * The nodes/properties to include/exclude are defined by a function
179  * provided by the caller. This function is called for each node and
180  * property, and must return:
181  *
182  *    0 - to exclude this part
183  *    1 - to include this part
184  *   -1 - for FDT_IS_PROP only: no information is available, so include
185  *		if its containing node is included
186  *
187  * The last case is only used to deal with properties. Often a property is
188  * included if its containing node is included - this is the case where
189  * -1 is returned.. However if the property is specifically required to be
190  * included/excluded, then 0 or 1 can be returned. Note that including a
191  * property when the FDT_REG_SUPERNODES flag is given will force its
192  * containing node to be included since it is not valid to have a property
193  * that is not in a node.
194  *
195  * Using the information provided, the inclusion of a node can be controlled
196  * either by a node name or its compatible string, or any other property
197  * that the function can determine.
198  *
199  * As an example, including node "/" means to include the root node and all
200  * root properties. A flag provides a way of also including supernodes (of
201  * which there is none for the root node), and another flag includes
202  * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
203  * FDT_END_NODE of all subnodes of /.
204  *
205  * The subnode feature helps in a hashing situation since it prevents the
206  * root node from changing at all. Any change to non-excluded properties,
207  * names of subnodes or number of subnodes would be detected.
208  *
209  * When used with FITs this provides the ability to hash and sign parts of
210  * the FIT based on different configurations in the FIT. Then it is
211  * impossible to change anything about that configuration (include images
212  * attached to the configuration), but it may be possible to add new
213  * configurations, new images or new signatures within the existing
214  * framework.
215  *
216  * Adding new properties to a device tree may result in the string table
217  * being extended (if the new property names are different from those
218  * already added). This function can optionally include a region for
219  * the string table so that this can be part of the hash too. This is always
220  * the last region.
221  *
222  * The FDT also has a mem_rsvmap table which can also be included, and is
223  * always the first region if so.
224  *
225  * The device tree header is not included in the region list. Since the
226  * contents of the FDT are changing (shrinking, often), the caller will need
227  * to regenerate the header anyway.
228  *
229  * @fdt:	Device tree to check
230  * @h_include:	Function to call to determine whether to include a part or
231  *		not:
232  *
233  *		@priv: Private pointer as passed to fdt_find_regions()
234  *		@fdt: Pointer to FDT blob
235  *		@offset: Offset of this node / property
236  *		@type: Type of this part, FDT_IS_...
237  *		@data: Pointer to data (node name, property name, compatible
238  *			string, value (not yet supported)
239  *		@size: Size of data, or 0 if none
240  *		@return 0 to exclude, 1 to include, -1 if no information is
241  *		available
242  * @priv:	Private pointer passed to h_include
243  * @region:	Returns list of regions, sorted by offset
244  * @max_regions: Maximum length of region list
245  * @path:	Pointer to a temporary string for the function to use for
246  *		building path names
247  * @path_len:	Length of path, must be large enough to hold the longest
248  *		path in the tree
249  * @flags:	Various flags that control the region algortihm, see
250  *		FDT_REG_...
251  * @return number of regions in list. If this is >max_regions then the
252  * region array was exhausted. You should increase max_regions and try
253  * the call again. Only the first max_regions elements are available in the
254  * array.
255  *
256  * On error a -ve value is return, which can be:
257  *
258  *	-FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
259  *	-FDT_ERR_BADLAYOUT
260  *	-FDT_ERR_NOSPACE (path area is too small)
261  */
262 int fdt_first_region(const void *fdt,
263 		     int (*h_include)(void *priv, const void *fdt, int offset,
264 				      int type, const char *data, int size),
265 		     void *priv, struct fdt_region *region,
266 		     char *path, int path_len, int flags,
267 		     struct fdt_region_state *info);
268 
269 /** fdt_next_region() - find next region
270  *
271  * See fdt_first_region() for full description. This function finds the
272  * next region according to the provided parameters, which must be the same
273  * as passed to fdt_first_region().
274  *
275  * This function can additionally return -FDT_ERR_NOTFOUND when there are no
276  * more regions
277  */
278 int fdt_next_region(const void *fdt,
279 		    int (*h_include)(void *priv, const void *fdt, int offset,
280 				     int type, const char *data, int size),
281 		    void *priv, struct fdt_region *region,
282 		    char *path, int path_len, int flags,
283 		    struct fdt_region_state *info);
284 
285 /**
286  * fdt_add_alias_regions() - find aliases that point to existing regions
287  *
288  * Once a device tree grep is complete some of the nodes will be present
289  * and some will have been dropped. This function checks all the alias nodes
290  * to figure out which points point to nodes which are still present. These
291  * aliases need to be kept, along with the nodes they reference.
292  *
293  * Given a list of regions function finds the aliases that still apply and
294  * adds more regions to the list for these. This function is called after
295  * fdt_next_region() has finished returning regions and requires the same
296  * state.
297  *
298  * @fdt:	Device tree file to reference
299  * @region:	List of regions that will be kept
300  * @count:	Number of regions
301  * @max_regions: Number of entries that can fit in @region
302  * @info:	Region state as returned from fdt_next_region()
303  * @return new number of regions in @region (i.e. count + the number added)
304  * or -FDT_ERR_NOSPACE if there was not enough space.
305  */
306 int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
307 			  int max_regions, struct fdt_region_state *info);
308 #endif /* SWIG */
309 
310 extern struct fdt_header *working_fdt;  /* Pointer to the working fdt */
311 
312 #endif /* _INCLUDE_LIBFDT_H_ */
313