xref: /rk3399_rockchip-uboot/lib/libfdt/fdt_region.c (revision 2bf94120e9a1827ddaa1f11f4e9b909b2c03bc37)
1c3c4c005SSimon Glass /*
2c3c4c005SSimon Glass  * libfdt - Flat Device Tree manipulation
3c3c4c005SSimon Glass  * Copyright (C) 2013 Google, Inc
4c3c4c005SSimon Glass  * Written by Simon Glass <sjg@chromium.org>
5c3c4c005SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+ BSD-2-Clause
6c3c4c005SSimon Glass  */
7c3c4c005SSimon Glass 
86feed2a5SRobert P. J. Day #include <libfdt_env.h>
9c3c4c005SSimon Glass 
10c3c4c005SSimon Glass #ifndef USE_HOSTCC
11c3c4c005SSimon Glass #include <fdt.h>
12c3c4c005SSimon Glass #include <libfdt.h>
13c3c4c005SSimon Glass #else
14c3c4c005SSimon Glass #include "fdt_host.h"
15c3c4c005SSimon Glass #endif
16c3c4c005SSimon Glass 
17c3c4c005SSimon Glass #include "libfdt_internal.h"
18c3c4c005SSimon Glass 
19c3c4c005SSimon Glass /**
20c3c4c005SSimon Glass  * fdt_add_region() - Add a new region to our list
216feed2a5SRobert P. J. Day  * @info:	State information
226feed2a5SRobert P. J. Day  * @offset:	Start offset of region
236feed2a5SRobert P. J. Day  * @size:	Size of region
24c3c4c005SSimon Glass  *
25c3c4c005SSimon Glass  * The region is added if there is space, but in any case we increment the
26c3c4c005SSimon Glass  * count. If permitted, and the new region overlaps the last one, we merge
27c3c4c005SSimon Glass  * them.
28c3c4c005SSimon Glass  */
29c3c4c005SSimon Glass static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
30c3c4c005SSimon Glass {
31c3c4c005SSimon Glass 	struct fdt_region *reg;
32c3c4c005SSimon Glass 
33c3c4c005SSimon Glass 	reg = info->region ? &info->region[info->count - 1] : NULL;
34c3c4c005SSimon Glass 	if (info->can_merge && info->count &&
35c3c4c005SSimon Glass 	    info->count <= info->max_regions &&
36c3c4c005SSimon Glass 	    reg && offset <= reg->offset + reg->size) {
37c3c4c005SSimon Glass 		reg->size = offset + size - reg->offset;
38c3c4c005SSimon Glass 	} else if (info->count++ < info->max_regions) {
39c3c4c005SSimon Glass 		if (reg) {
40c3c4c005SSimon Glass 			reg++;
41c3c4c005SSimon Glass 			reg->offset = offset;
42c3c4c005SSimon Glass 			reg->size = size;
43c3c4c005SSimon Glass 		}
44c3c4c005SSimon Glass 	} else {
45c3c4c005SSimon Glass 		return -1;
46c3c4c005SSimon Glass 	}
47c3c4c005SSimon Glass 
48c3c4c005SSimon Glass 	return 0;
49c3c4c005SSimon Glass }
50c3c4c005SSimon Glass 
51c3c4c005SSimon Glass static int region_list_contains_offset(struct fdt_region_state *info,
52c3c4c005SSimon Glass 				       const void *fdt, int target)
53c3c4c005SSimon Glass {
54c3c4c005SSimon Glass 	struct fdt_region *reg;
55c3c4c005SSimon Glass 	int num;
56c3c4c005SSimon Glass 
57c3c4c005SSimon Glass 	target += fdt_off_dt_struct(fdt);
58c3c4c005SSimon Glass 	for (reg = info->region, num = 0; num < info->count; reg++, num++) {
59c3c4c005SSimon Glass 		if (target >= reg->offset && target < reg->offset + reg->size)
60c3c4c005SSimon Glass 			return 1;
61c3c4c005SSimon Glass 	}
62c3c4c005SSimon Glass 
63c3c4c005SSimon Glass 	return 0;
64c3c4c005SSimon Glass }
65c3c4c005SSimon Glass 
66c47a38b4SSimon Glass /**
67c47a38b4SSimon Glass  * fdt_add_alias_regions() - Add regions covering the aliases that we want
68c47a38b4SSimon Glass  *
69c47a38b4SSimon Glass  * The /aliases node is not automatically included by fdtgrep unless the
70c47a38b4SSimon Glass  * command-line arguments cause to be included (or not excluded). However
71c47a38b4SSimon Glass  * aliases are special in that we generally want to include those which
72c47a38b4SSimon Glass  * reference a node that fdtgrep includes.
73c47a38b4SSimon Glass  *
74c47a38b4SSimon Glass  * In fact we want to include only aliases for those nodes still included in
75c47a38b4SSimon Glass  * the fdt, and drop the other aliases since they point to nodes that will not
76c47a38b4SSimon Glass  * be present.
77c47a38b4SSimon Glass  *
78c47a38b4SSimon Glass  * This function scans the aliases and adds regions for those which we want
79c47a38b4SSimon Glass  * to keep.
80c47a38b4SSimon Glass  *
81c47a38b4SSimon Glass  * @fdt: Device tree to scan
82c47a38b4SSimon Glass  * @region: List of regions
83c47a38b4SSimon Glass  * @count: Number of regions in the list so far (i.e. starting point for this
84c47a38b4SSimon Glass  *	function)
85c47a38b4SSimon Glass  * @max_regions: Maximum number of regions in @region list
86c47a38b4SSimon Glass  * @info: Place to put the region state
87c47a38b4SSimon Glass  * @return number of regions after processing, or -FDT_ERR_NOSPACE if we did
88c47a38b4SSimon Glass  * not have enough room in the regions table for the regions we wanted to add.
89c47a38b4SSimon Glass  */
90c3c4c005SSimon Glass int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
91c3c4c005SSimon Glass 			  int max_regions, struct fdt_region_state *info)
92c3c4c005SSimon Glass {
93c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(fdt);
94c3c4c005SSimon Glass 	int node, node_end, offset;
95c3c4c005SSimon Glass 	int did_alias_header;
96c3c4c005SSimon Glass 
97c3c4c005SSimon Glass 	node = fdt_subnode_offset(fdt, 0, "aliases");
98c3c4c005SSimon Glass 	if (node < 0)
99c3c4c005SSimon Glass 		return -FDT_ERR_NOTFOUND;
100c3c4c005SSimon Glass 
101c47a38b4SSimon Glass 	/*
102c47a38b4SSimon Glass 	 * Find the next node so that we know where the /aliases node ends. We
103c47a38b4SSimon Glass 	 * need special handling if /aliases is the last node.
104c47a38b4SSimon Glass 	 */
105c3c4c005SSimon Glass 	node_end = fdt_next_subnode(fdt, node);
106c47a38b4SSimon Glass 	if (node_end == -FDT_ERR_NOTFOUND)
107c47a38b4SSimon Glass 		/* Move back to the FDT_END_NODE tag of '/' */
108c47a38b4SSimon Glass 		node_end = fdt_size_dt_struct(fdt) - sizeof(fdt32_t) * 2;
109c47a38b4SSimon Glass 	else if (node_end < 0) /* other error */
110c47a38b4SSimon Glass 		return node_end;
111c47a38b4SSimon Glass 	node_end -= sizeof(fdt32_t);  /* Move to FDT_END_NODE tag of /aliases */
112c3c4c005SSimon Glass 
113c3c4c005SSimon Glass 	did_alias_header = 0;
114c3c4c005SSimon Glass 	info->region = region;
115c3c4c005SSimon Glass 	info->count = count;
116c3c4c005SSimon Glass 	info->can_merge = 0;
117c3c4c005SSimon Glass 	info->max_regions = max_regions;
118c3c4c005SSimon Glass 
119c3c4c005SSimon Glass 	for (offset = fdt_first_property_offset(fdt, node);
120c3c4c005SSimon Glass 	     offset >= 0;
121c3c4c005SSimon Glass 	     offset = fdt_next_property_offset(fdt, offset)) {
122c3c4c005SSimon Glass 		const struct fdt_property *prop;
123c3c4c005SSimon Glass 		const char *name;
124c3c4c005SSimon Glass 		int target, next;
125c3c4c005SSimon Glass 
126c3c4c005SSimon Glass 		prop = fdt_get_property_by_offset(fdt, offset, NULL);
127c3c4c005SSimon Glass 		name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
128c3c4c005SSimon Glass 		target = fdt_path_offset(fdt, name);
129c3c4c005SSimon Glass 		if (!region_list_contains_offset(info, fdt, target))
130c3c4c005SSimon Glass 			continue;
131c3c4c005SSimon Glass 		next = fdt_next_property_offset(fdt, offset);
132c3c4c005SSimon Glass 		if (next < 0)
1339d8ac956SSimon Glass 			next = node_end;
134c3c4c005SSimon Glass 
135c3c4c005SSimon Glass 		if (!did_alias_header) {
136c3c4c005SSimon Glass 			fdt_add_region(info, base + node, 12);
137c3c4c005SSimon Glass 			did_alias_header = 1;
138c3c4c005SSimon Glass 		}
139c3c4c005SSimon Glass 		fdt_add_region(info, base + offset, next - offset);
140c3c4c005SSimon Glass 	}
141c3c4c005SSimon Glass 
142c47a38b4SSimon Glass 	/* Add the FDT_END_NODE tag */
143c3c4c005SSimon Glass 	if (did_alias_header)
144c3c4c005SSimon Glass 		fdt_add_region(info, base + node_end, sizeof(fdt32_t));
145c3c4c005SSimon Glass 
146c3c4c005SSimon Glass 	return info->count < max_regions ? info->count : -FDT_ERR_NOSPACE;
147c3c4c005SSimon Glass }
148c3c4c005SSimon Glass 
149c3c4c005SSimon Glass /**
150c3c4c005SSimon Glass  * fdt_include_supernodes() - Include supernodes required by this node
1516feed2a5SRobert P. J. Day  * @info:	State information
1526feed2a5SRobert P. J. Day  * @depth:	Current stack depth
153c3c4c005SSimon Glass  *
154c3c4c005SSimon Glass  * When we decided to include a node or property which is not at the top
155c3c4c005SSimon Glass  * level, this function forces the inclusion of higher level nodes. For
156c3c4c005SSimon Glass  * example, given this tree:
157c3c4c005SSimon Glass  *
158c3c4c005SSimon Glass  * / {
159c3c4c005SSimon Glass  *     testing {
160c3c4c005SSimon Glass  *     }
161c3c4c005SSimon Glass  * }
162c3c4c005SSimon Glass  *
163c3c4c005SSimon Glass  * If we decide to include testing then we need the root node to have a valid
164c3c4c005SSimon Glass  * tree. This function adds those regions.
165c3c4c005SSimon Glass  */
166c3c4c005SSimon Glass static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
167c3c4c005SSimon Glass {
168c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(info->fdt);
169c3c4c005SSimon Glass 	int start, stop_at;
170c3c4c005SSimon Glass 	int i;
171c3c4c005SSimon Glass 
172c3c4c005SSimon Glass 	/*
173c3c4c005SSimon Glass 	 * Work down the stack looking for supernodes that we didn't include.
174c3c4c005SSimon Glass 	 * The algortihm here is actually pretty simple, since we know that
175c3c4c005SSimon Glass 	 * no previous subnode had to include these nodes, or if it did, we
176c3c4c005SSimon Glass 	 * marked them as included (on the stack) already.
177c3c4c005SSimon Glass 	 */
178c3c4c005SSimon Glass 	for (i = 0; i <= depth; i++) {
179c3c4c005SSimon Glass 		if (!info->stack[i].included) {
180c3c4c005SSimon Glass 			start = info->stack[i].offset;
181c3c4c005SSimon Glass 
182c3c4c005SSimon Glass 			/* Add the FDT_BEGIN_NODE tag of this supernode */
183c3c4c005SSimon Glass 			fdt_next_tag(info->fdt, start, &stop_at);
184c3c4c005SSimon Glass 			if (fdt_add_region(info, base + start, stop_at - start))
185c3c4c005SSimon Glass 				return -1;
186c3c4c005SSimon Glass 
187c3c4c005SSimon Glass 			/* Remember that this supernode is now included */
188c3c4c005SSimon Glass 			info->stack[i].included = 1;
189c3c4c005SSimon Glass 			info->can_merge = 1;
190c3c4c005SSimon Glass 		}
191c3c4c005SSimon Glass 
192c3c4c005SSimon Glass 		/* Force (later) generation of the FDT_END_NODE tag */
193c3c4c005SSimon Glass 		if (!info->stack[i].want)
194c3c4c005SSimon Glass 			info->stack[i].want = WANT_NODES_ONLY;
195c3c4c005SSimon Glass 	}
196c3c4c005SSimon Glass 
197c3c4c005SSimon Glass 	return 0;
198c3c4c005SSimon Glass }
199c3c4c005SSimon Glass 
200c3c4c005SSimon Glass enum {
201c3c4c005SSimon Glass 	FDT_DONE_NOTHING,
202c3c4c005SSimon Glass 	FDT_DONE_MEM_RSVMAP,
203c3c4c005SSimon Glass 	FDT_DONE_STRUCT,
204c3c4c005SSimon Glass 	FDT_DONE_END,
205c3c4c005SSimon Glass 	FDT_DONE_STRINGS,
206c3c4c005SSimon Glass 	FDT_DONE_ALL,
207c3c4c005SSimon Glass };
208c3c4c005SSimon Glass 
209c3c4c005SSimon Glass int fdt_first_region(const void *fdt,
210c3c4c005SSimon Glass 		int (*h_include)(void *priv, const void *fdt, int offset,
211c3c4c005SSimon Glass 				 int type, const char *data, int size),
212c3c4c005SSimon Glass 		void *priv, struct fdt_region *region,
213c3c4c005SSimon Glass 		char *path, int path_len, int flags,
214c3c4c005SSimon Glass 		struct fdt_region_state *info)
215c3c4c005SSimon Glass {
216c3c4c005SSimon Glass 	struct fdt_region_ptrs *p = &info->ptrs;
217c3c4c005SSimon Glass 
218c3c4c005SSimon Glass 	/* Set up our state */
219c3c4c005SSimon Glass 	info->fdt = fdt;
220c3c4c005SSimon Glass 	info->can_merge = 1;
221c3c4c005SSimon Glass 	info->max_regions = 1;
222c3c4c005SSimon Glass 	info->start = -1;
223c3c4c005SSimon Glass 	p->want = WANT_NOTHING;
224c3c4c005SSimon Glass 	p->end = path;
225c3c4c005SSimon Glass 	*p->end = '\0';
226c3c4c005SSimon Glass 	p->nextoffset = 0;
227c3c4c005SSimon Glass 	p->depth = -1;
228c3c4c005SSimon Glass 	p->done = FDT_DONE_NOTHING;
229c3c4c005SSimon Glass 
230c3c4c005SSimon Glass 	return fdt_next_region(fdt, h_include, priv, region,
231c3c4c005SSimon Glass 			       path, path_len, flags, info);
232c3c4c005SSimon Glass }
233c3c4c005SSimon Glass 
2346feed2a5SRobert P. J. Day /***********************************************************************
2356feed2a5SRobert P. J. Day  *
236c3c4c005SSimon Glass  *	Theory of operation
237c3c4c005SSimon Glass  *
2386feed2a5SRobert P. J. Day  * Note: in this description 'included' means that a node (or other part
2396feed2a5SRobert P. J. Day  * of the tree) should be included in the region list, i.e. it will have
2406feed2a5SRobert P. J. Day  * a region which covers its part of the tree.
241c3c4c005SSimon Glass  *
2426feed2a5SRobert P. J. Day  * This function maintains some state from the last time it is called.
2436feed2a5SRobert P. J. Day  * It checks the next part of the tree that it is supposed to look at
2446feed2a5SRobert P. J. Day  * (p.nextoffset) to see if that should be included or not. When it
2456feed2a5SRobert P. J. Day  * finds something to include, it sets info->start to its offset. This
2466feed2a5SRobert P. J. Day  * marks the start of the region we want to include.
247c3c4c005SSimon Glass  *
2486feed2a5SRobert P. J. Day  * Once info->start is set to the start (i.e. not -1), we continue
2496feed2a5SRobert P. J. Day  * scanning until we find something that we don't want included. This
2506feed2a5SRobert P. J. Day  * will be the end of a region. At this point we can close off the
2516feed2a5SRobert P. J. Day  * region and add it to the list. So we do so, and reset info->start
2526feed2a5SRobert P. J. Day  * to -1.
2536feed2a5SRobert P. J. Day  *
2546feed2a5SRobert P. J. Day  * One complication here is that we want to merge regions. So when we
2556feed2a5SRobert P. J. Day  * come to add another region later, we may in fact merge it with the
2566feed2a5SRobert P. J. Day  * previous one if one ends where the other starts.
2576feed2a5SRobert P. J. Day  *
2586feed2a5SRobert P. J. Day  * The function fdt_add_region() will return -1 if it fails to add the
2596feed2a5SRobert P. J. Day  * region, because we already have a region ready to be returned, and
2606feed2a5SRobert P. J. Day  * the new one cannot be merged in with it. In this case, we must return
2616feed2a5SRobert P. J. Day  * the region we found, and wait for another call to this function.
2626feed2a5SRobert P. J. Day  * When it comes, we will repeat the processing of the tag and again
2636feed2a5SRobert P. J. Day  * try to add a region. This time it will succeed.
2646feed2a5SRobert P. J. Day  *
2656feed2a5SRobert P. J. Day  * The current state of the pointers (stack, offset, etc.) is maintained
2666feed2a5SRobert P. J. Day  * in a ptrs member. At the start of every loop iteration we make a copy
2676feed2a5SRobert P. J. Day  * of it.  The copy is then updated as the tag is processed. Only if we
2686feed2a5SRobert P. J. Day  * get to the end of the loop iteration (and successfully call
2696feed2a5SRobert P. J. Day  * fdt_add_region() if we need to) can we commit the changes we have
2706feed2a5SRobert P. J. Day  * made to these pointers. For example, if we see an FDT_END_NODE tag,
2716feed2a5SRobert P. J. Day  * we will decrement the depth value. But if we need to add a region
2726feed2a5SRobert P. J. Day  * for this tag (let's say because the previous tag is included and this
2736feed2a5SRobert P. J. Day  * FDT_END_NODE tag is not included) then we will only commit the result
2746feed2a5SRobert P. J. Day  * if we were able to add the region. That allows us to retry again next
2756feed2a5SRobert P. J. Day  * time.
2766feed2a5SRobert P. J. Day  *
2776feed2a5SRobert P. J. Day  * We keep track of a variable called 'want' which tells us what we want
2786feed2a5SRobert P. J. Day  * to include when there is no specific information provided by the
2796feed2a5SRobert P. J. Day  * h_include function for a particular property. This basically handles
2806feed2a5SRobert P. J. Day  * the inclusion of properties which are pulled in by virtue of the node
2816feed2a5SRobert P. J. Day  * they are in. So if you include a node, its properties are also
2826feed2a5SRobert P. J. Day  * included.  In this case 'want' will be WANT_NODES_AND_PROPS. The
2836feed2a5SRobert P. J. Day  * FDT_REG_DIRECT_SUBNODES feature also makes use of 'want'. While we
2846feed2a5SRobert P. J. Day  * are inside the subnode, 'want' will be set to WANT_NODES_ONLY, so
2856feed2a5SRobert P. J. Day  * that only the subnode's FDT_BEGIN_NODE and FDT_END_NODE tags will be
2866feed2a5SRobert P. J. Day  * included, and properties will be skipped. If WANT_NOTHING is
2876feed2a5SRobert P. J. Day  * selected, then we will just rely on what the h_include() function
2886feed2a5SRobert P. J. Day  * tells us.
2896feed2a5SRobert P. J. Day  *
2906feed2a5SRobert P. J. Day  * Using 'want' we work out 'include', which tells us whether this
2916feed2a5SRobert P. J. Day  * current tag should be included or not. As you can imagine, if the
2926feed2a5SRobert P. J. Day  * value of 'include' changes, that means we are on a boundary between
2936feed2a5SRobert P. J. Day  * nodes to include and nodes to exclude. At this point we either close
2946feed2a5SRobert P. J. Day  * off a previous region and add it to the list, or mark the start of a
2956feed2a5SRobert P. J. Day  * new region.
2966feed2a5SRobert P. J. Day  *
2976feed2a5SRobert P. J. Day  * Apart from the nodes, we have mem_rsvmap, the FDT_END tag and the
2986feed2a5SRobert P. J. Day  * string list. Each of these dealt with as a whole (i.e. we create a
2996feed2a5SRobert P. J. Day  * region for each if it is to be included). For mem_rsvmap we don't
3006feed2a5SRobert P. J. Day  * allow it to merge with the first struct region. For the stringlist,
3016feed2a5SRobert P. J. Day  * we don't allow it to merge with the last struct region (which
3026feed2a5SRobert P. J. Day  * contains at minimum the FDT_END tag).
3036feed2a5SRobert P. J. Day  *
3046feed2a5SRobert P. J. Day  *********************************************************************/
305c3c4c005SSimon Glass 
306c3c4c005SSimon Glass int fdt_next_region(const void *fdt,
307c3c4c005SSimon Glass 		int (*h_include)(void *priv, const void *fdt, int offset,
308c3c4c005SSimon Glass 				 int type, const char *data, int size),
309c3c4c005SSimon Glass 		void *priv, struct fdt_region *region,
310c3c4c005SSimon Glass 		char *path, int path_len, int flags,
311c3c4c005SSimon Glass 		struct fdt_region_state *info)
312c3c4c005SSimon Glass {
313c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(fdt);
314c3c4c005SSimon Glass 	int last_node = 0;
315c3c4c005SSimon Glass 	const char *str;
316c3c4c005SSimon Glass 
317c3c4c005SSimon Glass 	info->region = region;
318c3c4c005SSimon Glass 	info->count = 0;
319c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_MEM_RSVMAP &&
320c3c4c005SSimon Glass 	    (flags & FDT_REG_ADD_MEM_RSVMAP)) {
321c3c4c005SSimon Glass 		/* Add the memory reserve map into its own region */
322c3c4c005SSimon Glass 		if (fdt_add_region(info, fdt_off_mem_rsvmap(fdt),
323c3c4c005SSimon Glass 				   fdt_off_dt_struct(fdt) -
324c3c4c005SSimon Glass 				   fdt_off_mem_rsvmap(fdt)))
325c3c4c005SSimon Glass 			return 0;
326c3c4c005SSimon Glass 		info->can_merge = 0;	/* Don't allow merging with this */
327c3c4c005SSimon Glass 		info->ptrs.done = FDT_DONE_MEM_RSVMAP;
328c3c4c005SSimon Glass 	}
329c3c4c005SSimon Glass 
330c3c4c005SSimon Glass 	/*
331c3c4c005SSimon Glass 	 * Work through the tags one by one, deciding whether each needs to
332c3c4c005SSimon Glass 	 * be included or not. We set the variable 'include' to indicate our
333c3c4c005SSimon Glass 	 * decision. 'want' is used to track what we want to include - it
334c3c4c005SSimon Glass 	 * allows us to pick up all the properties (and/or subnode tags) of
335c3c4c005SSimon Glass 	 * a node.
336c3c4c005SSimon Glass 	 */
337c3c4c005SSimon Glass 	while (info->ptrs.done < FDT_DONE_STRUCT) {
338c3c4c005SSimon Glass 		const struct fdt_property *prop;
339c3c4c005SSimon Glass 		struct fdt_region_ptrs p;
340c3c4c005SSimon Glass 		const char *name;
341c3c4c005SSimon Glass 		int include = 0;
342c3c4c005SSimon Glass 		int stop_at = 0;
343c3c4c005SSimon Glass 		uint32_t tag;
344c3c4c005SSimon Glass 		int offset;
345c3c4c005SSimon Glass 		int val;
346c3c4c005SSimon Glass 		int len;
347c3c4c005SSimon Glass 
348c3c4c005SSimon Glass 		/*
349c3c4c005SSimon Glass 		 * Make a copy of our pointers. If we make it to the end of
350c3c4c005SSimon Glass 		 * this block then we will commit them back to info->ptrs.
351c3c4c005SSimon Glass 		 * Otherwise we can try again from the same starting state
352c3c4c005SSimon Glass 		 * next time we are called.
353c3c4c005SSimon Glass 		 */
354c3c4c005SSimon Glass 		p = info->ptrs;
355c3c4c005SSimon Glass 
356c3c4c005SSimon Glass 		/*
357c3c4c005SSimon Glass 		 * Find the tag, and the offset of the next one. If we need to
358c3c4c005SSimon Glass 		 * stop including tags, then by default we stop *after*
359c3c4c005SSimon Glass 		 * including the current tag
360c3c4c005SSimon Glass 		 */
361c3c4c005SSimon Glass 		offset = p.nextoffset;
362c3c4c005SSimon Glass 		tag = fdt_next_tag(fdt, offset, &p.nextoffset);
363c3c4c005SSimon Glass 		stop_at = p.nextoffset;
364c3c4c005SSimon Glass 
365c3c4c005SSimon Glass 		switch (tag) {
366c3c4c005SSimon Glass 		case FDT_PROP:
367c3c4c005SSimon Glass 			stop_at = offset;
368c3c4c005SSimon Glass 			prop = fdt_get_property_by_offset(fdt, offset, NULL);
369c3c4c005SSimon Glass 			str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
370c3c4c005SSimon Glass 			val = h_include(priv, fdt, last_node, FDT_IS_PROP, str,
371c3c4c005SSimon Glass 					    strlen(str) + 1);
372c3c4c005SSimon Glass 			if (val == -1) {
373c3c4c005SSimon Glass 				include = p.want >= WANT_NODES_AND_PROPS;
374c3c4c005SSimon Glass 			} else {
375c3c4c005SSimon Glass 				include = val;
376c3c4c005SSimon Glass 				/*
377c3c4c005SSimon Glass 				 * Make sure we include the } for this block.
378c3c4c005SSimon Glass 				 * It might be more correct to have this done
379c3c4c005SSimon Glass 				 * by the call to fdt_include_supernodes() in
380c3c4c005SSimon Glass 				 * the case where it adds the node we are
381c3c4c005SSimon Glass 				 * currently in, but this is equivalent.
382c3c4c005SSimon Glass 				 */
383c3c4c005SSimon Glass 				if ((flags & FDT_REG_SUPERNODES) && val &&
384c3c4c005SSimon Glass 				    !p.want)
385c3c4c005SSimon Glass 					p.want = WANT_NODES_ONLY;
386c3c4c005SSimon Glass 			}
387c3c4c005SSimon Glass 
388c3c4c005SSimon Glass 			/* Value grepping is not yet supported */
389c3c4c005SSimon Glass 			break;
390c3c4c005SSimon Glass 
391c3c4c005SSimon Glass 		case FDT_NOP:
392c3c4c005SSimon Glass 			include = p.want >= WANT_NODES_AND_PROPS;
393c3c4c005SSimon Glass 			stop_at = offset;
394c3c4c005SSimon Glass 			break;
395c3c4c005SSimon Glass 
396c3c4c005SSimon Glass 		case FDT_BEGIN_NODE:
397c3c4c005SSimon Glass 			last_node = offset;
398c3c4c005SSimon Glass 			p.depth++;
399c3c4c005SSimon Glass 			if (p.depth == FDT_MAX_DEPTH)
400*2bf94120SSimon Glass 				return -FDT_ERR_BADSTRUCTURE;
401c3c4c005SSimon Glass 			name = fdt_get_name(fdt, offset, &len);
402c3c4c005SSimon Glass 			if (p.end - path + 2 + len >= path_len)
403c3c4c005SSimon Glass 				return -FDT_ERR_NOSPACE;
404c3c4c005SSimon Glass 
405c3c4c005SSimon Glass 			/* Build the full path of this node */
406c3c4c005SSimon Glass 			if (p.end != path + 1)
407c3c4c005SSimon Glass 				*p.end++ = '/';
408c3c4c005SSimon Glass 			strcpy(p.end, name);
409c3c4c005SSimon Glass 			p.end += len;
410c3c4c005SSimon Glass 			info->stack[p.depth].want = p.want;
411c3c4c005SSimon Glass 			info->stack[p.depth].offset = offset;
412c3c4c005SSimon Glass 
413c3c4c005SSimon Glass 			/*
414c3c4c005SSimon Glass 			 * If we are not intending to include this node unless
415c3c4c005SSimon Glass 			 * it matches, make sure we stop *before* its tag.
416c3c4c005SSimon Glass 			 */
417c3c4c005SSimon Glass 			if (p.want == WANT_NODES_ONLY ||
418c3c4c005SSimon Glass 			    !(flags & (FDT_REG_DIRECT_SUBNODES |
419c3c4c005SSimon Glass 				       FDT_REG_ALL_SUBNODES))) {
420c3c4c005SSimon Glass 				stop_at = offset;
421c3c4c005SSimon Glass 				p.want = WANT_NOTHING;
422c3c4c005SSimon Glass 			}
423c3c4c005SSimon Glass 			val = h_include(priv, fdt, offset, FDT_IS_NODE, path,
424c3c4c005SSimon Glass 					p.end - path + 1);
425c3c4c005SSimon Glass 
426c3c4c005SSimon Glass 			/* Include this if requested */
427c3c4c005SSimon Glass 			if (val) {
428c3c4c005SSimon Glass 				p.want = (flags & FDT_REG_ALL_SUBNODES) ?
429c3c4c005SSimon Glass 					WANT_ALL_NODES_AND_PROPS :
430c3c4c005SSimon Glass 					WANT_NODES_AND_PROPS;
431c3c4c005SSimon Glass 			}
432c3c4c005SSimon Glass 
433c3c4c005SSimon Glass 			/* If not requested, decay our 'p.want' value */
434c3c4c005SSimon Glass 			else if (p.want) {
435c3c4c005SSimon Glass 				if (p.want != WANT_ALL_NODES_AND_PROPS)
436c3c4c005SSimon Glass 					p.want--;
437c3c4c005SSimon Glass 
438c3c4c005SSimon Glass 			/* Not including this tag, so stop now */
439c3c4c005SSimon Glass 			} else {
440c3c4c005SSimon Glass 				stop_at = offset;
441c3c4c005SSimon Glass 			}
442c3c4c005SSimon Glass 
443c3c4c005SSimon Glass 			/*
444c3c4c005SSimon Glass 			 * Decide whether to include this tag, and update our
445c3c4c005SSimon Glass 			 * stack with the state for this node
446c3c4c005SSimon Glass 			 */
447c3c4c005SSimon Glass 			include = p.want;
448c3c4c005SSimon Glass 			info->stack[p.depth].included = include;
449c3c4c005SSimon Glass 			break;
450c3c4c005SSimon Glass 
451c3c4c005SSimon Glass 		case FDT_END_NODE:
452c3c4c005SSimon Glass 			include = p.want;
453c3c4c005SSimon Glass 			if (p.depth < 0)
454c3c4c005SSimon Glass 				return -FDT_ERR_BADSTRUCTURE;
455c3c4c005SSimon Glass 
456c3c4c005SSimon Glass 			/*
457c3c4c005SSimon Glass 			 * If we don't want this node, stop right away, unless
458c3c4c005SSimon Glass 			 * we are including subnodes
459c3c4c005SSimon Glass 			 */
460c3c4c005SSimon Glass 			if (!p.want && !(flags & FDT_REG_DIRECT_SUBNODES))
461c3c4c005SSimon Glass 				stop_at = offset;
462c3c4c005SSimon Glass 			p.want = info->stack[p.depth].want;
463c3c4c005SSimon Glass 			p.depth--;
464c3c4c005SSimon Glass 			while (p.end > path && *--p.end != '/')
465c3c4c005SSimon Glass 				;
466c3c4c005SSimon Glass 			*p.end = '\0';
467c3c4c005SSimon Glass 			break;
468c3c4c005SSimon Glass 
469c3c4c005SSimon Glass 		case FDT_END:
470c3c4c005SSimon Glass 			/* We always include the end tag */
471c3c4c005SSimon Glass 			include = 1;
472c3c4c005SSimon Glass 			p.done = FDT_DONE_STRUCT;
473c3c4c005SSimon Glass 			break;
474c3c4c005SSimon Glass 		}
475c3c4c005SSimon Glass 
476c3c4c005SSimon Glass 		/* If this tag is to be included, mark it as region start */
477c3c4c005SSimon Glass 		if (include && info->start == -1) {
478c3c4c005SSimon Glass 			/* Include any supernodes required by this one */
479c3c4c005SSimon Glass 			if (flags & FDT_REG_SUPERNODES) {
480c3c4c005SSimon Glass 				if (fdt_include_supernodes(info, p.depth))
481c3c4c005SSimon Glass 					return 0;
482c3c4c005SSimon Glass 			}
483c3c4c005SSimon Glass 			info->start = offset;
484c3c4c005SSimon Glass 		}
485c3c4c005SSimon Glass 
486c3c4c005SSimon Glass 		/*
487c3c4c005SSimon Glass 		 * If this tag is not to be included, finish up the current
488c3c4c005SSimon Glass 		 * region.
489c3c4c005SSimon Glass 		 */
490c3c4c005SSimon Glass 		if (!include && info->start != -1) {
491c3c4c005SSimon Glass 			if (fdt_add_region(info, base + info->start,
492c3c4c005SSimon Glass 					   stop_at - info->start))
493c3c4c005SSimon Glass 				return 0;
494c3c4c005SSimon Glass 			info->start = -1;
495c3c4c005SSimon Glass 			info->can_merge = 1;
496c3c4c005SSimon Glass 		}
497c3c4c005SSimon Glass 
498c3c4c005SSimon Glass 		/* If we have made it this far, we can commit our pointers */
499c3c4c005SSimon Glass 		info->ptrs = p;
500c3c4c005SSimon Glass 	}
501c3c4c005SSimon Glass 
502c3c4c005SSimon Glass 	/* Add a region for the END tag and a separate one for string table */
503c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_END) {
504c3c4c005SSimon Glass 		if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
505c3c4c005SSimon Glass 			return -FDT_ERR_BADSTRUCTURE;
506c3c4c005SSimon Glass 
507c3c4c005SSimon Glass 		if (fdt_add_region(info, base + info->start,
508c3c4c005SSimon Glass 				   info->ptrs.nextoffset - info->start))
509c3c4c005SSimon Glass 			return 0;
510c3c4c005SSimon Glass 		info->ptrs.done++;
511c3c4c005SSimon Glass 	}
512c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_STRINGS) {
513c3c4c005SSimon Glass 		if (flags & FDT_REG_ADD_STRING_TAB) {
514c3c4c005SSimon Glass 			info->can_merge = 0;
515c3c4c005SSimon Glass 			if (fdt_off_dt_strings(fdt) <
516c3c4c005SSimon Glass 			    base + info->ptrs.nextoffset)
517c3c4c005SSimon Glass 				return -FDT_ERR_BADLAYOUT;
518c3c4c005SSimon Glass 			if (fdt_add_region(info, fdt_off_dt_strings(fdt),
519c3c4c005SSimon Glass 					   fdt_size_dt_strings(fdt)))
520c3c4c005SSimon Glass 				return 0;
521c3c4c005SSimon Glass 		}
522c3c4c005SSimon Glass 		info->ptrs.done++;
523c3c4c005SSimon Glass 	}
524c3c4c005SSimon Glass 
525c3c4c005SSimon Glass 	return info->count > 0 ? 0 : -FDT_ERR_NOTFOUND;
526c3c4c005SSimon Glass }
527