xref: /openbmc/u-boot/lib/libfdt/fdt_region.c (revision 0c4b382f9041f9f2f00246c8a0ece90dae5451be)
14549e789STom Rini // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
2c3c4c005SSimon Glass /*
3c3c4c005SSimon Glass  * libfdt - Flat Device Tree manipulation
4c3c4c005SSimon Glass  * Copyright (C) 2013 Google, Inc
5c3c4c005SSimon Glass  * Written by Simon Glass <sjg@chromium.org>
6c3c4c005SSimon Glass  */
7c3c4c005SSimon Glass 
8b08c8c48SMasahiro Yamada #include <linux/libfdt_env.h>
9c3c4c005SSimon Glass 
10c3c4c005SSimon Glass #ifndef USE_HOSTCC
11c3c4c005SSimon Glass #include <fdt.h>
12b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
13c3c4c005SSimon Glass #else
14c3c4c005SSimon Glass #include "fdt_host.h"
15c3c4c005SSimon Glass #endif
16c3c4c005SSimon Glass 
1787be1e9fSMasahiro Yamada #define FDT_MAX_DEPTH	32
1887be1e9fSMasahiro Yamada 
str_in_list(const char * str,char * const list[],int count)1987be1e9fSMasahiro Yamada static int str_in_list(const char *str, char * const list[], int count)
2087be1e9fSMasahiro Yamada {
2187be1e9fSMasahiro Yamada 	int i;
2287be1e9fSMasahiro Yamada 
2387be1e9fSMasahiro Yamada 	for (i = 0; i < count; i++)
2487be1e9fSMasahiro Yamada 		if (!strcmp(list[i], str))
2587be1e9fSMasahiro Yamada 			return 1;
2687be1e9fSMasahiro Yamada 
2787be1e9fSMasahiro Yamada 	return 0;
2887be1e9fSMasahiro Yamada }
2987be1e9fSMasahiro Yamada 
fdt_find_regions(const void * fdt,char * const inc[],int inc_count,char * const exc_prop[],int exc_prop_count,struct fdt_region region[],int max_regions,char * path,int path_len,int add_string_tab)3087be1e9fSMasahiro Yamada int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
3187be1e9fSMasahiro Yamada 		     char * const exc_prop[], int exc_prop_count,
3287be1e9fSMasahiro Yamada 		     struct fdt_region region[], int max_regions,
3387be1e9fSMasahiro Yamada 		     char *path, int path_len, int add_string_tab)
3487be1e9fSMasahiro Yamada {
3587be1e9fSMasahiro Yamada 	int stack[FDT_MAX_DEPTH] = { 0 };
3687be1e9fSMasahiro Yamada 	char *end;
3787be1e9fSMasahiro Yamada 	int nextoffset = 0;
3887be1e9fSMasahiro Yamada 	uint32_t tag;
3987be1e9fSMasahiro Yamada 	int count = 0;
4087be1e9fSMasahiro Yamada 	int start = -1;
4187be1e9fSMasahiro Yamada 	int depth = -1;
4287be1e9fSMasahiro Yamada 	int want = 0;
4387be1e9fSMasahiro Yamada 	int base = fdt_off_dt_struct(fdt);
4487be1e9fSMasahiro Yamada 
4587be1e9fSMasahiro Yamada 	end = path;
4687be1e9fSMasahiro Yamada 	*end = '\0';
4787be1e9fSMasahiro Yamada 	do {
4887be1e9fSMasahiro Yamada 		const struct fdt_property *prop;
4987be1e9fSMasahiro Yamada 		const char *name;
5087be1e9fSMasahiro Yamada 		const char *str;
5187be1e9fSMasahiro Yamada 		int include = 0;
5287be1e9fSMasahiro Yamada 		int stop_at = 0;
5387be1e9fSMasahiro Yamada 		int offset;
5487be1e9fSMasahiro Yamada 		int len;
5587be1e9fSMasahiro Yamada 
5687be1e9fSMasahiro Yamada 		offset = nextoffset;
5787be1e9fSMasahiro Yamada 		tag = fdt_next_tag(fdt, offset, &nextoffset);
5887be1e9fSMasahiro Yamada 		stop_at = nextoffset;
5987be1e9fSMasahiro Yamada 
6087be1e9fSMasahiro Yamada 		switch (tag) {
6187be1e9fSMasahiro Yamada 		case FDT_PROP:
6287be1e9fSMasahiro Yamada 			include = want >= 2;
6387be1e9fSMasahiro Yamada 			stop_at = offset;
6487be1e9fSMasahiro Yamada 			prop = fdt_get_property_by_offset(fdt, offset, NULL);
6587be1e9fSMasahiro Yamada 			str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
6687be1e9fSMasahiro Yamada 			if (str_in_list(str, exc_prop, exc_prop_count))
6787be1e9fSMasahiro Yamada 				include = 0;
6887be1e9fSMasahiro Yamada 			break;
6987be1e9fSMasahiro Yamada 
7087be1e9fSMasahiro Yamada 		case FDT_NOP:
7187be1e9fSMasahiro Yamada 			include = want >= 2;
7287be1e9fSMasahiro Yamada 			stop_at = offset;
7387be1e9fSMasahiro Yamada 			break;
7487be1e9fSMasahiro Yamada 
7587be1e9fSMasahiro Yamada 		case FDT_BEGIN_NODE:
7687be1e9fSMasahiro Yamada 			depth++;
7787be1e9fSMasahiro Yamada 			if (depth == FDT_MAX_DEPTH)
7887be1e9fSMasahiro Yamada 				return -FDT_ERR_BADSTRUCTURE;
7987be1e9fSMasahiro Yamada 			name = fdt_get_name(fdt, offset, &len);
8087be1e9fSMasahiro Yamada 			if (end - path + 2 + len >= path_len)
8187be1e9fSMasahiro Yamada 				return -FDT_ERR_NOSPACE;
8287be1e9fSMasahiro Yamada 			if (end != path + 1)
8387be1e9fSMasahiro Yamada 				*end++ = '/';
8487be1e9fSMasahiro Yamada 			strcpy(end, name);
8587be1e9fSMasahiro Yamada 			end += len;
8687be1e9fSMasahiro Yamada 			stack[depth] = want;
8787be1e9fSMasahiro Yamada 			if (want == 1)
8887be1e9fSMasahiro Yamada 				stop_at = offset;
8987be1e9fSMasahiro Yamada 			if (str_in_list(path, inc, inc_count))
9087be1e9fSMasahiro Yamada 				want = 2;
9187be1e9fSMasahiro Yamada 			else if (want)
9287be1e9fSMasahiro Yamada 				want--;
9387be1e9fSMasahiro Yamada 			else
9487be1e9fSMasahiro Yamada 				stop_at = offset;
9587be1e9fSMasahiro Yamada 			include = want;
9687be1e9fSMasahiro Yamada 			break;
9787be1e9fSMasahiro Yamada 
9887be1e9fSMasahiro Yamada 		case FDT_END_NODE:
99*21ebf2adSKonrad Beckmann 			/* Depth must never go below -1 */
100*21ebf2adSKonrad Beckmann 			if (depth < 0)
101*21ebf2adSKonrad Beckmann 				return -FDT_ERR_BADSTRUCTURE;
10287be1e9fSMasahiro Yamada 			include = want;
10387be1e9fSMasahiro Yamada 			want = stack[depth--];
10487be1e9fSMasahiro Yamada 			while (end > path && *--end != '/')
10587be1e9fSMasahiro Yamada 				;
10687be1e9fSMasahiro Yamada 			*end = '\0';
10787be1e9fSMasahiro Yamada 			break;
10887be1e9fSMasahiro Yamada 
10987be1e9fSMasahiro Yamada 		case FDT_END:
11087be1e9fSMasahiro Yamada 			include = 1;
11187be1e9fSMasahiro Yamada 			break;
11287be1e9fSMasahiro Yamada 		}
11387be1e9fSMasahiro Yamada 
11487be1e9fSMasahiro Yamada 		if (include && start == -1) {
11587be1e9fSMasahiro Yamada 			/* Should we merge with previous? */
11687be1e9fSMasahiro Yamada 			if (count && count <= max_regions &&
11787be1e9fSMasahiro Yamada 			    offset == region[count - 1].offset +
11887be1e9fSMasahiro Yamada 					region[count - 1].size - base)
11987be1e9fSMasahiro Yamada 				start = region[--count].offset - base;
12087be1e9fSMasahiro Yamada 			else
12187be1e9fSMasahiro Yamada 				start = offset;
12287be1e9fSMasahiro Yamada 		}
12387be1e9fSMasahiro Yamada 
12487be1e9fSMasahiro Yamada 		if (!include && start != -1) {
12587be1e9fSMasahiro Yamada 			if (count < max_regions) {
12687be1e9fSMasahiro Yamada 				region[count].offset = base + start;
12787be1e9fSMasahiro Yamada 				region[count].size = stop_at - start;
12887be1e9fSMasahiro Yamada 			}
12987be1e9fSMasahiro Yamada 			count++;
13087be1e9fSMasahiro Yamada 			start = -1;
13187be1e9fSMasahiro Yamada 		}
13287be1e9fSMasahiro Yamada 	} while (tag != FDT_END);
13387be1e9fSMasahiro Yamada 
13487be1e9fSMasahiro Yamada 	if (nextoffset != fdt_size_dt_struct(fdt))
13587be1e9fSMasahiro Yamada 		return -FDT_ERR_BADLAYOUT;
13687be1e9fSMasahiro Yamada 
13787be1e9fSMasahiro Yamada 	/* Add a region for the END tag and the string table */
13887be1e9fSMasahiro Yamada 	if (count < max_regions) {
13987be1e9fSMasahiro Yamada 		region[count].offset = base + start;
14087be1e9fSMasahiro Yamada 		region[count].size = nextoffset - start;
14187be1e9fSMasahiro Yamada 		if (add_string_tab)
14287be1e9fSMasahiro Yamada 			region[count].size += fdt_size_dt_strings(fdt);
14387be1e9fSMasahiro Yamada 	}
14487be1e9fSMasahiro Yamada 	count++;
14587be1e9fSMasahiro Yamada 
14687be1e9fSMasahiro Yamada 	return count;
14787be1e9fSMasahiro Yamada }
14887be1e9fSMasahiro Yamada 
149c3c4c005SSimon Glass /**
150c3c4c005SSimon Glass  * fdt_add_region() - Add a new region to our list
1516feed2a5SRobert P. J. Day  * @info:	State information
1526feed2a5SRobert P. J. Day  * @offset:	Start offset of region
1536feed2a5SRobert P. J. Day  * @size:	Size of region
154c3c4c005SSimon Glass  *
155c3c4c005SSimon Glass  * The region is added if there is space, but in any case we increment the
156c3c4c005SSimon Glass  * count. If permitted, and the new region overlaps the last one, we merge
157c3c4c005SSimon Glass  * them.
158c3c4c005SSimon Glass  */
fdt_add_region(struct fdt_region_state * info,int offset,int size)159c3c4c005SSimon Glass static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
160c3c4c005SSimon Glass {
161c3c4c005SSimon Glass 	struct fdt_region *reg;
162c3c4c005SSimon Glass 
163c3c4c005SSimon Glass 	reg = info->region ? &info->region[info->count - 1] : NULL;
164c3c4c005SSimon Glass 	if (info->can_merge && info->count &&
165c3c4c005SSimon Glass 	    info->count <= info->max_regions &&
166c3c4c005SSimon Glass 	    reg && offset <= reg->offset + reg->size) {
167c3c4c005SSimon Glass 		reg->size = offset + size - reg->offset;
168c3c4c005SSimon Glass 	} else if (info->count++ < info->max_regions) {
169c3c4c005SSimon Glass 		if (reg) {
170c3c4c005SSimon Glass 			reg++;
171c3c4c005SSimon Glass 			reg->offset = offset;
172c3c4c005SSimon Glass 			reg->size = size;
173c3c4c005SSimon Glass 		}
174c3c4c005SSimon Glass 	} else {
175c3c4c005SSimon Glass 		return -1;
176c3c4c005SSimon Glass 	}
177c3c4c005SSimon Glass 
178c3c4c005SSimon Glass 	return 0;
179c3c4c005SSimon Glass }
180c3c4c005SSimon Glass 
region_list_contains_offset(struct fdt_region_state * info,const void * fdt,int target)181c3c4c005SSimon Glass static int region_list_contains_offset(struct fdt_region_state *info,
182c3c4c005SSimon Glass 				       const void *fdt, int target)
183c3c4c005SSimon Glass {
184c3c4c005SSimon Glass 	struct fdt_region *reg;
185c3c4c005SSimon Glass 	int num;
186c3c4c005SSimon Glass 
187c3c4c005SSimon Glass 	target += fdt_off_dt_struct(fdt);
188c3c4c005SSimon Glass 	for (reg = info->region, num = 0; num < info->count; reg++, num++) {
189c3c4c005SSimon Glass 		if (target >= reg->offset && target < reg->offset + reg->size)
190c3c4c005SSimon Glass 			return 1;
191c3c4c005SSimon Glass 	}
192c3c4c005SSimon Glass 
193c3c4c005SSimon Glass 	return 0;
194c3c4c005SSimon Glass }
195c3c4c005SSimon Glass 
196c47a38b4SSimon Glass /**
197c47a38b4SSimon Glass  * fdt_add_alias_regions() - Add regions covering the aliases that we want
198c47a38b4SSimon Glass  *
199c47a38b4SSimon Glass  * The /aliases node is not automatically included by fdtgrep unless the
200c47a38b4SSimon Glass  * command-line arguments cause to be included (or not excluded). However
201c47a38b4SSimon Glass  * aliases are special in that we generally want to include those which
202c47a38b4SSimon Glass  * reference a node that fdtgrep includes.
203c47a38b4SSimon Glass  *
204c47a38b4SSimon Glass  * In fact we want to include only aliases for those nodes still included in
205c47a38b4SSimon Glass  * the fdt, and drop the other aliases since they point to nodes that will not
206c47a38b4SSimon Glass  * be present.
207c47a38b4SSimon Glass  *
208c47a38b4SSimon Glass  * This function scans the aliases and adds regions for those which we want
209c47a38b4SSimon Glass  * to keep.
210c47a38b4SSimon Glass  *
211c47a38b4SSimon Glass  * @fdt: Device tree to scan
212c47a38b4SSimon Glass  * @region: List of regions
213c47a38b4SSimon Glass  * @count: Number of regions in the list so far (i.e. starting point for this
214c47a38b4SSimon Glass  *	function)
215c47a38b4SSimon Glass  * @max_regions: Maximum number of regions in @region list
216c47a38b4SSimon Glass  * @info: Place to put the region state
217c47a38b4SSimon Glass  * @return number of regions after processing, or -FDT_ERR_NOSPACE if we did
218c47a38b4SSimon Glass  * not have enough room in the regions table for the regions we wanted to add.
219c47a38b4SSimon Glass  */
fdt_add_alias_regions(const void * fdt,struct fdt_region * region,int count,int max_regions,struct fdt_region_state * info)220c3c4c005SSimon Glass int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
221c3c4c005SSimon Glass 			  int max_regions, struct fdt_region_state *info)
222c3c4c005SSimon Glass {
223c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(fdt);
224c3c4c005SSimon Glass 	int node, node_end, offset;
225c3c4c005SSimon Glass 	int did_alias_header;
226c3c4c005SSimon Glass 
227c3c4c005SSimon Glass 	node = fdt_subnode_offset(fdt, 0, "aliases");
228c3c4c005SSimon Glass 	if (node < 0)
229c3c4c005SSimon Glass 		return -FDT_ERR_NOTFOUND;
230c3c4c005SSimon Glass 
231c47a38b4SSimon Glass 	/*
232c47a38b4SSimon Glass 	 * Find the next node so that we know where the /aliases node ends. We
233c47a38b4SSimon Glass 	 * need special handling if /aliases is the last node.
234c47a38b4SSimon Glass 	 */
235c3c4c005SSimon Glass 	node_end = fdt_next_subnode(fdt, node);
236c47a38b4SSimon Glass 	if (node_end == -FDT_ERR_NOTFOUND)
237c47a38b4SSimon Glass 		/* Move back to the FDT_END_NODE tag of '/' */
238c47a38b4SSimon Glass 		node_end = fdt_size_dt_struct(fdt) - sizeof(fdt32_t) * 2;
239c47a38b4SSimon Glass 	else if (node_end < 0) /* other error */
240c47a38b4SSimon Glass 		return node_end;
241c47a38b4SSimon Glass 	node_end -= sizeof(fdt32_t);  /* Move to FDT_END_NODE tag of /aliases */
242c3c4c005SSimon Glass 
243c3c4c005SSimon Glass 	did_alias_header = 0;
244c3c4c005SSimon Glass 	info->region = region;
245c3c4c005SSimon Glass 	info->count = count;
246c3c4c005SSimon Glass 	info->can_merge = 0;
247c3c4c005SSimon Glass 	info->max_regions = max_regions;
248c3c4c005SSimon Glass 
249c3c4c005SSimon Glass 	for (offset = fdt_first_property_offset(fdt, node);
250c3c4c005SSimon Glass 	     offset >= 0;
251c3c4c005SSimon Glass 	     offset = fdt_next_property_offset(fdt, offset)) {
252c3c4c005SSimon Glass 		const struct fdt_property *prop;
253c3c4c005SSimon Glass 		const char *name;
254c3c4c005SSimon Glass 		int target, next;
255c3c4c005SSimon Glass 
256c3c4c005SSimon Glass 		prop = fdt_get_property_by_offset(fdt, offset, NULL);
257c3c4c005SSimon Glass 		name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
258c3c4c005SSimon Glass 		target = fdt_path_offset(fdt, name);
259c3c4c005SSimon Glass 		if (!region_list_contains_offset(info, fdt, target))
260c3c4c005SSimon Glass 			continue;
261c3c4c005SSimon Glass 		next = fdt_next_property_offset(fdt, offset);
262c3c4c005SSimon Glass 		if (next < 0)
2639d8ac956SSimon Glass 			next = node_end;
264c3c4c005SSimon Glass 
265c3c4c005SSimon Glass 		if (!did_alias_header) {
266c3c4c005SSimon Glass 			fdt_add_region(info, base + node, 12);
267c3c4c005SSimon Glass 			did_alias_header = 1;
268c3c4c005SSimon Glass 		}
269c3c4c005SSimon Glass 		fdt_add_region(info, base + offset, next - offset);
270c3c4c005SSimon Glass 	}
271c3c4c005SSimon Glass 
272c47a38b4SSimon Glass 	/* Add the FDT_END_NODE tag */
273c3c4c005SSimon Glass 	if (did_alias_header)
274c3c4c005SSimon Glass 		fdt_add_region(info, base + node_end, sizeof(fdt32_t));
275c3c4c005SSimon Glass 
276c3c4c005SSimon Glass 	return info->count < max_regions ? info->count : -FDT_ERR_NOSPACE;
277c3c4c005SSimon Glass }
278c3c4c005SSimon Glass 
279c3c4c005SSimon Glass /**
280c3c4c005SSimon Glass  * fdt_include_supernodes() - Include supernodes required by this node
2816feed2a5SRobert P. J. Day  * @info:	State information
2826feed2a5SRobert P. J. Day  * @depth:	Current stack depth
283c3c4c005SSimon Glass  *
284c3c4c005SSimon Glass  * When we decided to include a node or property which is not at the top
285c3c4c005SSimon Glass  * level, this function forces the inclusion of higher level nodes. For
286c3c4c005SSimon Glass  * example, given this tree:
287c3c4c005SSimon Glass  *
288c3c4c005SSimon Glass  * / {
289c3c4c005SSimon Glass  *     testing {
290c3c4c005SSimon Glass  *     }
291c3c4c005SSimon Glass  * }
292c3c4c005SSimon Glass  *
293c3c4c005SSimon Glass  * If we decide to include testing then we need the root node to have a valid
294c3c4c005SSimon Glass  * tree. This function adds those regions.
295c3c4c005SSimon Glass  */
fdt_include_supernodes(struct fdt_region_state * info,int depth)296c3c4c005SSimon Glass static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
297c3c4c005SSimon Glass {
298c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(info->fdt);
299c3c4c005SSimon Glass 	int start, stop_at;
300c3c4c005SSimon Glass 	int i;
301c3c4c005SSimon Glass 
302c3c4c005SSimon Glass 	/*
303c3c4c005SSimon Glass 	 * Work down the stack looking for supernodes that we didn't include.
304c3c4c005SSimon Glass 	 * The algortihm here is actually pretty simple, since we know that
305c3c4c005SSimon Glass 	 * no previous subnode had to include these nodes, or if it did, we
306c3c4c005SSimon Glass 	 * marked them as included (on the stack) already.
307c3c4c005SSimon Glass 	 */
308c3c4c005SSimon Glass 	for (i = 0; i <= depth; i++) {
309c3c4c005SSimon Glass 		if (!info->stack[i].included) {
310c3c4c005SSimon Glass 			start = info->stack[i].offset;
311c3c4c005SSimon Glass 
312c3c4c005SSimon Glass 			/* Add the FDT_BEGIN_NODE tag of this supernode */
313c3c4c005SSimon Glass 			fdt_next_tag(info->fdt, start, &stop_at);
314c3c4c005SSimon Glass 			if (fdt_add_region(info, base + start, stop_at - start))
315c3c4c005SSimon Glass 				return -1;
316c3c4c005SSimon Glass 
317c3c4c005SSimon Glass 			/* Remember that this supernode is now included */
318c3c4c005SSimon Glass 			info->stack[i].included = 1;
319c3c4c005SSimon Glass 			info->can_merge = 1;
320c3c4c005SSimon Glass 		}
321c3c4c005SSimon Glass 
322c3c4c005SSimon Glass 		/* Force (later) generation of the FDT_END_NODE tag */
323c3c4c005SSimon Glass 		if (!info->stack[i].want)
324c3c4c005SSimon Glass 			info->stack[i].want = WANT_NODES_ONLY;
325c3c4c005SSimon Glass 	}
326c3c4c005SSimon Glass 
327c3c4c005SSimon Glass 	return 0;
328c3c4c005SSimon Glass }
329c3c4c005SSimon Glass 
330c3c4c005SSimon Glass enum {
331c3c4c005SSimon Glass 	FDT_DONE_NOTHING,
332c3c4c005SSimon Glass 	FDT_DONE_MEM_RSVMAP,
333c3c4c005SSimon Glass 	FDT_DONE_STRUCT,
334c3c4c005SSimon Glass 	FDT_DONE_END,
335c3c4c005SSimon Glass 	FDT_DONE_STRINGS,
336c3c4c005SSimon Glass 	FDT_DONE_ALL,
337c3c4c005SSimon Glass };
338c3c4c005SSimon Glass 
fdt_first_region(const void * fdt,int (* h_include)(void * priv,const void * fdt,int offset,int type,const char * data,int size),void * priv,struct fdt_region * region,char * path,int path_len,int flags,struct fdt_region_state * info)339c3c4c005SSimon Glass int fdt_first_region(const void *fdt,
340c3c4c005SSimon Glass 		int (*h_include)(void *priv, const void *fdt, int offset,
341c3c4c005SSimon Glass 				 int type, const char *data, int size),
342c3c4c005SSimon Glass 		void *priv, struct fdt_region *region,
343c3c4c005SSimon Glass 		char *path, int path_len, int flags,
344c3c4c005SSimon Glass 		struct fdt_region_state *info)
345c3c4c005SSimon Glass {
346c3c4c005SSimon Glass 	struct fdt_region_ptrs *p = &info->ptrs;
347c3c4c005SSimon Glass 
348c3c4c005SSimon Glass 	/* Set up our state */
349c3c4c005SSimon Glass 	info->fdt = fdt;
350c3c4c005SSimon Glass 	info->can_merge = 1;
351c3c4c005SSimon Glass 	info->max_regions = 1;
352c3c4c005SSimon Glass 	info->start = -1;
353c3c4c005SSimon Glass 	p->want = WANT_NOTHING;
354c3c4c005SSimon Glass 	p->end = path;
355c3c4c005SSimon Glass 	*p->end = '\0';
356c3c4c005SSimon Glass 	p->nextoffset = 0;
357c3c4c005SSimon Glass 	p->depth = -1;
358c3c4c005SSimon Glass 	p->done = FDT_DONE_NOTHING;
359c3c4c005SSimon Glass 
360c3c4c005SSimon Glass 	return fdt_next_region(fdt, h_include, priv, region,
361c3c4c005SSimon Glass 			       path, path_len, flags, info);
362c3c4c005SSimon Glass }
363c3c4c005SSimon Glass 
3646feed2a5SRobert P. J. Day /***********************************************************************
3656feed2a5SRobert P. J. Day  *
366c3c4c005SSimon Glass  *	Theory of operation
367c3c4c005SSimon Glass  *
3686feed2a5SRobert P. J. Day  * Note: in this description 'included' means that a node (or other part
3696feed2a5SRobert P. J. Day  * of the tree) should be included in the region list, i.e. it will have
3706feed2a5SRobert P. J. Day  * a region which covers its part of the tree.
371c3c4c005SSimon Glass  *
3726feed2a5SRobert P. J. Day  * This function maintains some state from the last time it is called.
3736feed2a5SRobert P. J. Day  * It checks the next part of the tree that it is supposed to look at
3746feed2a5SRobert P. J. Day  * (p.nextoffset) to see if that should be included or not. When it
3756feed2a5SRobert P. J. Day  * finds something to include, it sets info->start to its offset. This
3766feed2a5SRobert P. J. Day  * marks the start of the region we want to include.
377c3c4c005SSimon Glass  *
3786feed2a5SRobert P. J. Day  * Once info->start is set to the start (i.e. not -1), we continue
3796feed2a5SRobert P. J. Day  * scanning until we find something that we don't want included. This
3806feed2a5SRobert P. J. Day  * will be the end of a region. At this point we can close off the
3816feed2a5SRobert P. J. Day  * region and add it to the list. So we do so, and reset info->start
3826feed2a5SRobert P. J. Day  * to -1.
3836feed2a5SRobert P. J. Day  *
3846feed2a5SRobert P. J. Day  * One complication here is that we want to merge regions. So when we
3856feed2a5SRobert P. J. Day  * come to add another region later, we may in fact merge it with the
3866feed2a5SRobert P. J. Day  * previous one if one ends where the other starts.
3876feed2a5SRobert P. J. Day  *
3886feed2a5SRobert P. J. Day  * The function fdt_add_region() will return -1 if it fails to add the
3896feed2a5SRobert P. J. Day  * region, because we already have a region ready to be returned, and
3906feed2a5SRobert P. J. Day  * the new one cannot be merged in with it. In this case, we must return
3916feed2a5SRobert P. J. Day  * the region we found, and wait for another call to this function.
3926feed2a5SRobert P. J. Day  * When it comes, we will repeat the processing of the tag and again
3936feed2a5SRobert P. J. Day  * try to add a region. This time it will succeed.
3946feed2a5SRobert P. J. Day  *
3956feed2a5SRobert P. J. Day  * The current state of the pointers (stack, offset, etc.) is maintained
3966feed2a5SRobert P. J. Day  * in a ptrs member. At the start of every loop iteration we make a copy
3976feed2a5SRobert P. J. Day  * of it.  The copy is then updated as the tag is processed. Only if we
3986feed2a5SRobert P. J. Day  * get to the end of the loop iteration (and successfully call
3996feed2a5SRobert P. J. Day  * fdt_add_region() if we need to) can we commit the changes we have
4006feed2a5SRobert P. J. Day  * made to these pointers. For example, if we see an FDT_END_NODE tag,
4016feed2a5SRobert P. J. Day  * we will decrement the depth value. But if we need to add a region
4026feed2a5SRobert P. J. Day  * for this tag (let's say because the previous tag is included and this
4036feed2a5SRobert P. J. Day  * FDT_END_NODE tag is not included) then we will only commit the result
4046feed2a5SRobert P. J. Day  * if we were able to add the region. That allows us to retry again next
4056feed2a5SRobert P. J. Day  * time.
4066feed2a5SRobert P. J. Day  *
4076feed2a5SRobert P. J. Day  * We keep track of a variable called 'want' which tells us what we want
4086feed2a5SRobert P. J. Day  * to include when there is no specific information provided by the
4096feed2a5SRobert P. J. Day  * h_include function for a particular property. This basically handles
4106feed2a5SRobert P. J. Day  * the inclusion of properties which are pulled in by virtue of the node
4116feed2a5SRobert P. J. Day  * they are in. So if you include a node, its properties are also
4126feed2a5SRobert P. J. Day  * included.  In this case 'want' will be WANT_NODES_AND_PROPS. The
4136feed2a5SRobert P. J. Day  * FDT_REG_DIRECT_SUBNODES feature also makes use of 'want'. While we
4146feed2a5SRobert P. J. Day  * are inside the subnode, 'want' will be set to WANT_NODES_ONLY, so
4156feed2a5SRobert P. J. Day  * that only the subnode's FDT_BEGIN_NODE and FDT_END_NODE tags will be
4166feed2a5SRobert P. J. Day  * included, and properties will be skipped. If WANT_NOTHING is
4176feed2a5SRobert P. J. Day  * selected, then we will just rely on what the h_include() function
4186feed2a5SRobert P. J. Day  * tells us.
4196feed2a5SRobert P. J. Day  *
4206feed2a5SRobert P. J. Day  * Using 'want' we work out 'include', which tells us whether this
4216feed2a5SRobert P. J. Day  * current tag should be included or not. As you can imagine, if the
4226feed2a5SRobert P. J. Day  * value of 'include' changes, that means we are on a boundary between
4236feed2a5SRobert P. J. Day  * nodes to include and nodes to exclude. At this point we either close
4246feed2a5SRobert P. J. Day  * off a previous region and add it to the list, or mark the start of a
4256feed2a5SRobert P. J. Day  * new region.
4266feed2a5SRobert P. J. Day  *
4276feed2a5SRobert P. J. Day  * Apart from the nodes, we have mem_rsvmap, the FDT_END tag and the
4286feed2a5SRobert P. J. Day  * string list. Each of these dealt with as a whole (i.e. we create a
4296feed2a5SRobert P. J. Day  * region for each if it is to be included). For mem_rsvmap we don't
4306feed2a5SRobert P. J. Day  * allow it to merge with the first struct region. For the stringlist,
4316feed2a5SRobert P. J. Day  * we don't allow it to merge with the last struct region (which
4326feed2a5SRobert P. J. Day  * contains at minimum the FDT_END tag).
4336feed2a5SRobert P. J. Day  *
4346feed2a5SRobert P. J. Day  *********************************************************************/
435c3c4c005SSimon Glass 
fdt_next_region(const void * fdt,int (* h_include)(void * priv,const void * fdt,int offset,int type,const char * data,int size),void * priv,struct fdt_region * region,char * path,int path_len,int flags,struct fdt_region_state * info)436c3c4c005SSimon Glass int fdt_next_region(const void *fdt,
437c3c4c005SSimon Glass 		int (*h_include)(void *priv, const void *fdt, int offset,
438c3c4c005SSimon Glass 				 int type, const char *data, int size),
439c3c4c005SSimon Glass 		void *priv, struct fdt_region *region,
440c3c4c005SSimon Glass 		char *path, int path_len, int flags,
441c3c4c005SSimon Glass 		struct fdt_region_state *info)
442c3c4c005SSimon Glass {
443c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(fdt);
444c3c4c005SSimon Glass 	int last_node = 0;
445c3c4c005SSimon Glass 	const char *str;
446c3c4c005SSimon Glass 
447c3c4c005SSimon Glass 	info->region = region;
448c3c4c005SSimon Glass 	info->count = 0;
449c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_MEM_RSVMAP &&
450c3c4c005SSimon Glass 	    (flags & FDT_REG_ADD_MEM_RSVMAP)) {
451c3c4c005SSimon Glass 		/* Add the memory reserve map into its own region */
452c3c4c005SSimon Glass 		if (fdt_add_region(info, fdt_off_mem_rsvmap(fdt),
453c3c4c005SSimon Glass 				   fdt_off_dt_struct(fdt) -
454c3c4c005SSimon Glass 				   fdt_off_mem_rsvmap(fdt)))
455c3c4c005SSimon Glass 			return 0;
456c3c4c005SSimon Glass 		info->can_merge = 0;	/* Don't allow merging with this */
457c3c4c005SSimon Glass 		info->ptrs.done = FDT_DONE_MEM_RSVMAP;
458c3c4c005SSimon Glass 	}
459c3c4c005SSimon Glass 
460c3c4c005SSimon Glass 	/*
461c3c4c005SSimon Glass 	 * Work through the tags one by one, deciding whether each needs to
462c3c4c005SSimon Glass 	 * be included or not. We set the variable 'include' to indicate our
463c3c4c005SSimon Glass 	 * decision. 'want' is used to track what we want to include - it
464c3c4c005SSimon Glass 	 * allows us to pick up all the properties (and/or subnode tags) of
465c3c4c005SSimon Glass 	 * a node.
466c3c4c005SSimon Glass 	 */
467c3c4c005SSimon Glass 	while (info->ptrs.done < FDT_DONE_STRUCT) {
468c3c4c005SSimon Glass 		const struct fdt_property *prop;
469c3c4c005SSimon Glass 		struct fdt_region_ptrs p;
470c3c4c005SSimon Glass 		const char *name;
471c3c4c005SSimon Glass 		int include = 0;
472c3c4c005SSimon Glass 		int stop_at = 0;
473c3c4c005SSimon Glass 		uint32_t tag;
474c3c4c005SSimon Glass 		int offset;
475c3c4c005SSimon Glass 		int val;
476c3c4c005SSimon Glass 		int len;
477c3c4c005SSimon Glass 
478c3c4c005SSimon Glass 		/*
479c3c4c005SSimon Glass 		 * Make a copy of our pointers. If we make it to the end of
480c3c4c005SSimon Glass 		 * this block then we will commit them back to info->ptrs.
481c3c4c005SSimon Glass 		 * Otherwise we can try again from the same starting state
482c3c4c005SSimon Glass 		 * next time we are called.
483c3c4c005SSimon Glass 		 */
484c3c4c005SSimon Glass 		p = info->ptrs;
485c3c4c005SSimon Glass 
486c3c4c005SSimon Glass 		/*
487c3c4c005SSimon Glass 		 * Find the tag, and the offset of the next one. If we need to
488c3c4c005SSimon Glass 		 * stop including tags, then by default we stop *after*
489c3c4c005SSimon Glass 		 * including the current tag
490c3c4c005SSimon Glass 		 */
491c3c4c005SSimon Glass 		offset = p.nextoffset;
492c3c4c005SSimon Glass 		tag = fdt_next_tag(fdt, offset, &p.nextoffset);
493c3c4c005SSimon Glass 		stop_at = p.nextoffset;
494c3c4c005SSimon Glass 
495c3c4c005SSimon Glass 		switch (tag) {
496c3c4c005SSimon Glass 		case FDT_PROP:
497c3c4c005SSimon Glass 			stop_at = offset;
498c3c4c005SSimon Glass 			prop = fdt_get_property_by_offset(fdt, offset, NULL);
499c3c4c005SSimon Glass 			str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
500c3c4c005SSimon Glass 			val = h_include(priv, fdt, last_node, FDT_IS_PROP, str,
501c3c4c005SSimon Glass 					    strlen(str) + 1);
502c3c4c005SSimon Glass 			if (val == -1) {
503c3c4c005SSimon Glass 				include = p.want >= WANT_NODES_AND_PROPS;
504c3c4c005SSimon Glass 			} else {
505c3c4c005SSimon Glass 				include = val;
506c3c4c005SSimon Glass 				/*
507c3c4c005SSimon Glass 				 * Make sure we include the } for this block.
508c3c4c005SSimon Glass 				 * It might be more correct to have this done
509c3c4c005SSimon Glass 				 * by the call to fdt_include_supernodes() in
510c3c4c005SSimon Glass 				 * the case where it adds the node we are
511c3c4c005SSimon Glass 				 * currently in, but this is equivalent.
512c3c4c005SSimon Glass 				 */
513c3c4c005SSimon Glass 				if ((flags & FDT_REG_SUPERNODES) && val &&
514c3c4c005SSimon Glass 				    !p.want)
515c3c4c005SSimon Glass 					p.want = WANT_NODES_ONLY;
516c3c4c005SSimon Glass 			}
517c3c4c005SSimon Glass 
518c3c4c005SSimon Glass 			/* Value grepping is not yet supported */
519c3c4c005SSimon Glass 			break;
520c3c4c005SSimon Glass 
521c3c4c005SSimon Glass 		case FDT_NOP:
522c3c4c005SSimon Glass 			include = p.want >= WANT_NODES_AND_PROPS;
523c3c4c005SSimon Glass 			stop_at = offset;
524c3c4c005SSimon Glass 			break;
525c3c4c005SSimon Glass 
526c3c4c005SSimon Glass 		case FDT_BEGIN_NODE:
527c3c4c005SSimon Glass 			last_node = offset;
528c3c4c005SSimon Glass 			p.depth++;
529c3c4c005SSimon Glass 			if (p.depth == FDT_MAX_DEPTH)
5302bf94120SSimon Glass 				return -FDT_ERR_BADSTRUCTURE;
531c3c4c005SSimon Glass 			name = fdt_get_name(fdt, offset, &len);
532c3c4c005SSimon Glass 			if (p.end - path + 2 + len >= path_len)
533c3c4c005SSimon Glass 				return -FDT_ERR_NOSPACE;
534c3c4c005SSimon Glass 
535c3c4c005SSimon Glass 			/* Build the full path of this node */
536c3c4c005SSimon Glass 			if (p.end != path + 1)
537c3c4c005SSimon Glass 				*p.end++ = '/';
538c3c4c005SSimon Glass 			strcpy(p.end, name);
539c3c4c005SSimon Glass 			p.end += len;
540c3c4c005SSimon Glass 			info->stack[p.depth].want = p.want;
541c3c4c005SSimon Glass 			info->stack[p.depth].offset = offset;
542c3c4c005SSimon Glass 
543c3c4c005SSimon Glass 			/*
544c3c4c005SSimon Glass 			 * If we are not intending to include this node unless
545c3c4c005SSimon Glass 			 * it matches, make sure we stop *before* its tag.
546c3c4c005SSimon Glass 			 */
547c3c4c005SSimon Glass 			if (p.want == WANT_NODES_ONLY ||
548c3c4c005SSimon Glass 			    !(flags & (FDT_REG_DIRECT_SUBNODES |
549c3c4c005SSimon Glass 				       FDT_REG_ALL_SUBNODES))) {
550c3c4c005SSimon Glass 				stop_at = offset;
551c3c4c005SSimon Glass 				p.want = WANT_NOTHING;
552c3c4c005SSimon Glass 			}
553c3c4c005SSimon Glass 			val = h_include(priv, fdt, offset, FDT_IS_NODE, path,
554c3c4c005SSimon Glass 					p.end - path + 1);
555c3c4c005SSimon Glass 
556c3c4c005SSimon Glass 			/* Include this if requested */
557c3c4c005SSimon Glass 			if (val) {
558c3c4c005SSimon Glass 				p.want = (flags & FDT_REG_ALL_SUBNODES) ?
559c3c4c005SSimon Glass 					WANT_ALL_NODES_AND_PROPS :
560c3c4c005SSimon Glass 					WANT_NODES_AND_PROPS;
561c3c4c005SSimon Glass 			}
562c3c4c005SSimon Glass 
563c3c4c005SSimon Glass 			/* If not requested, decay our 'p.want' value */
564c3c4c005SSimon Glass 			else if (p.want) {
565c3c4c005SSimon Glass 				if (p.want != WANT_ALL_NODES_AND_PROPS)
566c3c4c005SSimon Glass 					p.want--;
567c3c4c005SSimon Glass 
568c3c4c005SSimon Glass 			/* Not including this tag, so stop now */
569c3c4c005SSimon Glass 			} else {
570c3c4c005SSimon Glass 				stop_at = offset;
571c3c4c005SSimon Glass 			}
572c3c4c005SSimon Glass 
573c3c4c005SSimon Glass 			/*
574c3c4c005SSimon Glass 			 * Decide whether to include this tag, and update our
575c3c4c005SSimon Glass 			 * stack with the state for this node
576c3c4c005SSimon Glass 			 */
577c3c4c005SSimon Glass 			include = p.want;
578c3c4c005SSimon Glass 			info->stack[p.depth].included = include;
579c3c4c005SSimon Glass 			break;
580c3c4c005SSimon Glass 
581c3c4c005SSimon Glass 		case FDT_END_NODE:
582c3c4c005SSimon Glass 			include = p.want;
583c3c4c005SSimon Glass 			if (p.depth < 0)
584c3c4c005SSimon Glass 				return -FDT_ERR_BADSTRUCTURE;
585c3c4c005SSimon Glass 
586c3c4c005SSimon Glass 			/*
587c3c4c005SSimon Glass 			 * If we don't want this node, stop right away, unless
588c3c4c005SSimon Glass 			 * we are including subnodes
589c3c4c005SSimon Glass 			 */
590c3c4c005SSimon Glass 			if (!p.want && !(flags & FDT_REG_DIRECT_SUBNODES))
591c3c4c005SSimon Glass 				stop_at = offset;
592c3c4c005SSimon Glass 			p.want = info->stack[p.depth].want;
593c3c4c005SSimon Glass 			p.depth--;
594c3c4c005SSimon Glass 			while (p.end > path && *--p.end != '/')
595c3c4c005SSimon Glass 				;
596c3c4c005SSimon Glass 			*p.end = '\0';
597c3c4c005SSimon Glass 			break;
598c3c4c005SSimon Glass 
599c3c4c005SSimon Glass 		case FDT_END:
600c3c4c005SSimon Glass 			/* We always include the end tag */
601c3c4c005SSimon Glass 			include = 1;
602c3c4c005SSimon Glass 			p.done = FDT_DONE_STRUCT;
603c3c4c005SSimon Glass 			break;
604c3c4c005SSimon Glass 		}
605c3c4c005SSimon Glass 
606c3c4c005SSimon Glass 		/* If this tag is to be included, mark it as region start */
607c3c4c005SSimon Glass 		if (include && info->start == -1) {
608c3c4c005SSimon Glass 			/* Include any supernodes required by this one */
609c3c4c005SSimon Glass 			if (flags & FDT_REG_SUPERNODES) {
610c3c4c005SSimon Glass 				if (fdt_include_supernodes(info, p.depth))
611c3c4c005SSimon Glass 					return 0;
612c3c4c005SSimon Glass 			}
613c3c4c005SSimon Glass 			info->start = offset;
614c3c4c005SSimon Glass 		}
615c3c4c005SSimon Glass 
616c3c4c005SSimon Glass 		/*
617c3c4c005SSimon Glass 		 * If this tag is not to be included, finish up the current
618c3c4c005SSimon Glass 		 * region.
619c3c4c005SSimon Glass 		 */
620c3c4c005SSimon Glass 		if (!include && info->start != -1) {
621c3c4c005SSimon Glass 			if (fdt_add_region(info, base + info->start,
622c3c4c005SSimon Glass 					   stop_at - info->start))
623c3c4c005SSimon Glass 				return 0;
624c3c4c005SSimon Glass 			info->start = -1;
625c3c4c005SSimon Glass 			info->can_merge = 1;
626c3c4c005SSimon Glass 		}
627c3c4c005SSimon Glass 
628c3c4c005SSimon Glass 		/* If we have made it this far, we can commit our pointers */
629c3c4c005SSimon Glass 		info->ptrs = p;
630c3c4c005SSimon Glass 	}
631c3c4c005SSimon Glass 
632c3c4c005SSimon Glass 	/* Add a region for the END tag and a separate one for string table */
633c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_END) {
634c3c4c005SSimon Glass 		if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
635c3c4c005SSimon Glass 			return -FDT_ERR_BADSTRUCTURE;
636c3c4c005SSimon Glass 
637c3c4c005SSimon Glass 		if (fdt_add_region(info, base + info->start,
638c3c4c005SSimon Glass 				   info->ptrs.nextoffset - info->start))
639c3c4c005SSimon Glass 			return 0;
640c3c4c005SSimon Glass 		info->ptrs.done++;
641c3c4c005SSimon Glass 	}
642c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_STRINGS) {
643c3c4c005SSimon Glass 		if (flags & FDT_REG_ADD_STRING_TAB) {
644c3c4c005SSimon Glass 			info->can_merge = 0;
645c3c4c005SSimon Glass 			if (fdt_off_dt_strings(fdt) <
646c3c4c005SSimon Glass 			    base + info->ptrs.nextoffset)
647c3c4c005SSimon Glass 				return -FDT_ERR_BADLAYOUT;
648c3c4c005SSimon Glass 			if (fdt_add_region(info, fdt_off_dt_strings(fdt),
649c3c4c005SSimon Glass 					   fdt_size_dt_strings(fdt)))
650c3c4c005SSimon Glass 				return 0;
651c3c4c005SSimon Glass 		}
652c3c4c005SSimon Glass 		info->ptrs.done++;
653c3c4c005SSimon Glass 	}
654c3c4c005SSimon Glass 
655c3c4c005SSimon Glass 	return info->count > 0 ? 0 : -FDT_ERR_NOTFOUND;
656c3c4c005SSimon Glass }
657