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