xref: /rk3399_rockchip-uboot/lib/libfdt/fdt_region.c (revision 0e00a84cdedf7a1949486746225b35984b351eca)
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 
8*0e00a84cSMasahiro Yamada #include <linux/libfdt_env.h>
9c3c4c005SSimon Glass 
10c3c4c005SSimon Glass #ifndef USE_HOSTCC
11c3c4c005SSimon Glass #include <fdt.h>
12*0e00a84cSMasahiro Yamada #include <linux/libfdt.h>
13c3c4c005SSimon Glass #else
14c3c4c005SSimon Glass #include "fdt_host.h"
15c3c4c005SSimon Glass #endif
16c3c4c005SSimon Glass 
17c3c4c005SSimon Glass #include "libfdt_internal.h"
18c3c4c005SSimon Glass 
19e1c37281SMasahiro Yamada #define FDT_MAX_DEPTH	32
20e1c37281SMasahiro Yamada 
str_in_list(const char * str,char * const list[],int count)21e1c37281SMasahiro Yamada static int str_in_list(const char *str, char * const list[], int count)
22e1c37281SMasahiro Yamada {
23e1c37281SMasahiro Yamada 	int i;
24e1c37281SMasahiro Yamada 
25e1c37281SMasahiro Yamada 	for (i = 0; i < count; i++)
26e1c37281SMasahiro Yamada 		if (!strcmp(list[i], str))
27e1c37281SMasahiro Yamada 			return 1;
28e1c37281SMasahiro Yamada 
29e1c37281SMasahiro Yamada 	return 0;
30e1c37281SMasahiro Yamada }
31e1c37281SMasahiro 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)32e1c37281SMasahiro Yamada int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
33e1c37281SMasahiro Yamada 		     char * const exc_prop[], int exc_prop_count,
34e1c37281SMasahiro Yamada 		     struct fdt_region region[], int max_regions,
35e1c37281SMasahiro Yamada 		     char *path, int path_len, int add_string_tab)
36e1c37281SMasahiro Yamada {
37e1c37281SMasahiro Yamada 	int stack[FDT_MAX_DEPTH] = { 0 };
38e1c37281SMasahiro Yamada 	char *end;
39e1c37281SMasahiro Yamada 	int nextoffset = 0;
40e1c37281SMasahiro Yamada 	uint32_t tag;
41e1c37281SMasahiro Yamada 	int count = 0;
42e1c37281SMasahiro Yamada 	int start = -1;
43e1c37281SMasahiro Yamada 	int depth = -1;
44e1c37281SMasahiro Yamada 	int want = 0;
45e1c37281SMasahiro Yamada 	int base = fdt_off_dt_struct(fdt);
46e1c37281SMasahiro Yamada 
47e1c37281SMasahiro Yamada 	end = path;
48e1c37281SMasahiro Yamada 	*end = '\0';
49e1c37281SMasahiro Yamada 	do {
50e1c37281SMasahiro Yamada 		const struct fdt_property *prop;
51e1c37281SMasahiro Yamada 		const char *name;
52e1c37281SMasahiro Yamada 		const char *str;
53e1c37281SMasahiro Yamada 		int include = 0;
54e1c37281SMasahiro Yamada 		int stop_at = 0;
55e1c37281SMasahiro Yamada 		int offset;
56e1c37281SMasahiro Yamada 		int len;
57e1c37281SMasahiro Yamada 
58e1c37281SMasahiro Yamada 		offset = nextoffset;
59e1c37281SMasahiro Yamada 		tag = fdt_next_tag(fdt, offset, &nextoffset);
60e1c37281SMasahiro Yamada 		stop_at = nextoffset;
61e1c37281SMasahiro Yamada 
62e1c37281SMasahiro Yamada 		switch (tag) {
63e1c37281SMasahiro Yamada 		case FDT_PROP:
64e1c37281SMasahiro Yamada 			include = want >= 2;
65e1c37281SMasahiro Yamada 			stop_at = offset;
66e1c37281SMasahiro Yamada 			prop = fdt_get_property_by_offset(fdt, offset, NULL);
67e1c37281SMasahiro Yamada 			str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
68e1c37281SMasahiro Yamada 			if (str_in_list(str, exc_prop, exc_prop_count))
69e1c37281SMasahiro Yamada 				include = 0;
70e1c37281SMasahiro Yamada 			break;
71e1c37281SMasahiro Yamada 
72e1c37281SMasahiro Yamada 		case FDT_NOP:
73e1c37281SMasahiro Yamada 			include = want >= 2;
74e1c37281SMasahiro Yamada 			stop_at = offset;
75e1c37281SMasahiro Yamada 			break;
76e1c37281SMasahiro Yamada 
77e1c37281SMasahiro Yamada 		case FDT_BEGIN_NODE:
78e1c37281SMasahiro Yamada 			depth++;
79e1c37281SMasahiro Yamada 			if (depth == FDT_MAX_DEPTH)
80e1c37281SMasahiro Yamada 				return -FDT_ERR_BADSTRUCTURE;
81e1c37281SMasahiro Yamada 			name = fdt_get_name(fdt, offset, &len);
82e1c37281SMasahiro Yamada 			if (end - path + 2 + len >= path_len)
83e1c37281SMasahiro Yamada 				return -FDT_ERR_NOSPACE;
84e1c37281SMasahiro Yamada 			if (end != path + 1)
85e1c37281SMasahiro Yamada 				*end++ = '/';
86e1c37281SMasahiro Yamada 			strcpy(end, name);
87e1c37281SMasahiro Yamada 			end += len;
88e1c37281SMasahiro Yamada 			stack[depth] = want;
89e1c37281SMasahiro Yamada 			if (want == 1)
90e1c37281SMasahiro Yamada 				stop_at = offset;
91e1c37281SMasahiro Yamada 			if (str_in_list(path, inc, inc_count))
92e1c37281SMasahiro Yamada 				want = 2;
93e1c37281SMasahiro Yamada 			else if (want)
94e1c37281SMasahiro Yamada 				want--;
95e1c37281SMasahiro Yamada 			else
96e1c37281SMasahiro Yamada 				stop_at = offset;
97e1c37281SMasahiro Yamada 			include = want;
98e1c37281SMasahiro Yamada 			break;
99e1c37281SMasahiro Yamada 
100e1c37281SMasahiro Yamada 		case FDT_END_NODE:
101e1c37281SMasahiro Yamada 			include = want;
102e1c37281SMasahiro Yamada 			want = stack[depth--];
103e1c37281SMasahiro Yamada 			while (end > path && *--end != '/')
104e1c37281SMasahiro Yamada 				;
105e1c37281SMasahiro Yamada 			*end = '\0';
106e1c37281SMasahiro Yamada 			break;
107e1c37281SMasahiro Yamada 
108e1c37281SMasahiro Yamada 		case FDT_END:
109e1c37281SMasahiro Yamada 			include = 1;
110e1c37281SMasahiro Yamada 			break;
111e1c37281SMasahiro Yamada 		}
112e1c37281SMasahiro Yamada 
113e1c37281SMasahiro Yamada 		if (include && start == -1) {
114e1c37281SMasahiro Yamada 			/* Should we merge with previous? */
115e1c37281SMasahiro Yamada 			if (count && count <= max_regions &&
116e1c37281SMasahiro Yamada 			    offset == region[count - 1].offset +
117e1c37281SMasahiro Yamada 					region[count - 1].size - base)
118e1c37281SMasahiro Yamada 				start = region[--count].offset - base;
119e1c37281SMasahiro Yamada 			else
120e1c37281SMasahiro Yamada 				start = offset;
121e1c37281SMasahiro Yamada 		}
122e1c37281SMasahiro Yamada 
123e1c37281SMasahiro Yamada 		if (!include && start != -1) {
124e1c37281SMasahiro Yamada 			if (count < max_regions) {
125e1c37281SMasahiro Yamada 				region[count].offset = base + start;
126e1c37281SMasahiro Yamada 				region[count].size = stop_at - start;
127e1c37281SMasahiro Yamada 			}
128e1c37281SMasahiro Yamada 			count++;
129e1c37281SMasahiro Yamada 			start = -1;
130e1c37281SMasahiro Yamada 		}
131e1c37281SMasahiro Yamada 	} while (tag != FDT_END);
132e1c37281SMasahiro Yamada 
133e1c37281SMasahiro Yamada 	if (nextoffset != fdt_size_dt_struct(fdt))
134e1c37281SMasahiro Yamada 		return -FDT_ERR_BADLAYOUT;
135e1c37281SMasahiro Yamada 
136e1c37281SMasahiro Yamada 	/* Add a region for the END tag and the string table */
137e1c37281SMasahiro Yamada 	if (count < max_regions) {
138e1c37281SMasahiro Yamada 		region[count].offset = base + start;
139e1c37281SMasahiro Yamada 		region[count].size = nextoffset - start;
140e1c37281SMasahiro Yamada 		if (add_string_tab)
141e1c37281SMasahiro Yamada 			region[count].size += fdt_size_dt_strings(fdt);
142e1c37281SMasahiro Yamada 	}
143e1c37281SMasahiro Yamada 	count++;
144e1c37281SMasahiro Yamada 
145e1c37281SMasahiro Yamada 	return count;
146e1c37281SMasahiro Yamada }
147e1c37281SMasahiro Yamada 
148c3c4c005SSimon Glass /**
149c3c4c005SSimon Glass  * fdt_add_region() - Add a new region to our list
1506feed2a5SRobert P. J. Day  * @info:	State information
1516feed2a5SRobert P. J. Day  * @offset:	Start offset of region
1526feed2a5SRobert P. J. Day  * @size:	Size of region
153c3c4c005SSimon Glass  *
154c3c4c005SSimon Glass  * The region is added if there is space, but in any case we increment the
155c3c4c005SSimon Glass  * count. If permitted, and the new region overlaps the last one, we merge
156c3c4c005SSimon Glass  * them.
157c3c4c005SSimon Glass  */
fdt_add_region(struct fdt_region_state * info,int offset,int size)158c3c4c005SSimon Glass static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
159c3c4c005SSimon Glass {
160c3c4c005SSimon Glass 	struct fdt_region *reg;
161c3c4c005SSimon Glass 
162c3c4c005SSimon Glass 	reg = info->region ? &info->region[info->count - 1] : NULL;
163c3c4c005SSimon Glass 	if (info->can_merge && info->count &&
164c3c4c005SSimon Glass 	    info->count <= info->max_regions &&
165c3c4c005SSimon Glass 	    reg && offset <= reg->offset + reg->size) {
166c3c4c005SSimon Glass 		reg->size = offset + size - reg->offset;
167c3c4c005SSimon Glass 	} else if (info->count++ < info->max_regions) {
168c3c4c005SSimon Glass 		if (reg) {
169c3c4c005SSimon Glass 			reg++;
170c3c4c005SSimon Glass 			reg->offset = offset;
171c3c4c005SSimon Glass 			reg->size = size;
172c3c4c005SSimon Glass 		}
173c3c4c005SSimon Glass 	} else {
174c3c4c005SSimon Glass 		return -1;
175c3c4c005SSimon Glass 	}
176c3c4c005SSimon Glass 
177c3c4c005SSimon Glass 	return 0;
178c3c4c005SSimon Glass }
179c3c4c005SSimon Glass 
region_list_contains_offset(struct fdt_region_state * info,const void * fdt,int target)180c3c4c005SSimon Glass static int region_list_contains_offset(struct fdt_region_state *info,
181c3c4c005SSimon Glass 				       const void *fdt, int target)
182c3c4c005SSimon Glass {
183c3c4c005SSimon Glass 	struct fdt_region *reg;
184c3c4c005SSimon Glass 	int num;
185c3c4c005SSimon Glass 
186c3c4c005SSimon Glass 	target += fdt_off_dt_struct(fdt);
187c3c4c005SSimon Glass 	for (reg = info->region, num = 0; num < info->count; reg++, num++) {
188c3c4c005SSimon Glass 		if (target >= reg->offset && target < reg->offset + reg->size)
189c3c4c005SSimon Glass 			return 1;
190c3c4c005SSimon Glass 	}
191c3c4c005SSimon Glass 
192c3c4c005SSimon Glass 	return 0;
193c3c4c005SSimon Glass }
194c3c4c005SSimon Glass 
195c47a38b4SSimon Glass /**
196c47a38b4SSimon Glass  * fdt_add_alias_regions() - Add regions covering the aliases that we want
197c47a38b4SSimon Glass  *
198c47a38b4SSimon Glass  * The /aliases node is not automatically included by fdtgrep unless the
199c47a38b4SSimon Glass  * command-line arguments cause to be included (or not excluded). However
200c47a38b4SSimon Glass  * aliases are special in that we generally want to include those which
201c47a38b4SSimon Glass  * reference a node that fdtgrep includes.
202c47a38b4SSimon Glass  *
203c47a38b4SSimon Glass  * In fact we want to include only aliases for those nodes still included in
204c47a38b4SSimon Glass  * the fdt, and drop the other aliases since they point to nodes that will not
205c47a38b4SSimon Glass  * be present.
206c47a38b4SSimon Glass  *
207c47a38b4SSimon Glass  * This function scans the aliases and adds regions for those which we want
208c47a38b4SSimon Glass  * to keep.
209c47a38b4SSimon Glass  *
210c47a38b4SSimon Glass  * @fdt: Device tree to scan
211c47a38b4SSimon Glass  * @region: List of regions
212c47a38b4SSimon Glass  * @count: Number of regions in the list so far (i.e. starting point for this
213c47a38b4SSimon Glass  *	function)
214c47a38b4SSimon Glass  * @max_regions: Maximum number of regions in @region list
215c47a38b4SSimon Glass  * @info: Place to put the region state
216c47a38b4SSimon Glass  * @return number of regions after processing, or -FDT_ERR_NOSPACE if we did
217c47a38b4SSimon Glass  * not have enough room in the regions table for the regions we wanted to add.
218c47a38b4SSimon Glass  */
fdt_add_alias_regions(const void * fdt,struct fdt_region * region,int count,int max_regions,struct fdt_region_state * info)219c3c4c005SSimon Glass int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
220c3c4c005SSimon Glass 			  int max_regions, struct fdt_region_state *info)
221c3c4c005SSimon Glass {
222c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(fdt);
223c3c4c005SSimon Glass 	int node, node_end, offset;
224c3c4c005SSimon Glass 	int did_alias_header;
225c3c4c005SSimon Glass 
226c3c4c005SSimon Glass 	node = fdt_subnode_offset(fdt, 0, "aliases");
227c3c4c005SSimon Glass 	if (node < 0)
228c3c4c005SSimon Glass 		return -FDT_ERR_NOTFOUND;
229c3c4c005SSimon Glass 
230c47a38b4SSimon Glass 	/*
231c47a38b4SSimon Glass 	 * Find the next node so that we know where the /aliases node ends. We
232c47a38b4SSimon Glass 	 * need special handling if /aliases is the last node.
233c47a38b4SSimon Glass 	 */
234c3c4c005SSimon Glass 	node_end = fdt_next_subnode(fdt, node);
235c47a38b4SSimon Glass 	if (node_end == -FDT_ERR_NOTFOUND)
236c47a38b4SSimon Glass 		/* Move back to the FDT_END_NODE tag of '/' */
237c47a38b4SSimon Glass 		node_end = fdt_size_dt_struct(fdt) - sizeof(fdt32_t) * 2;
238c47a38b4SSimon Glass 	else if (node_end < 0) /* other error */
239c47a38b4SSimon Glass 		return node_end;
240c47a38b4SSimon Glass 	node_end -= sizeof(fdt32_t);  /* Move to FDT_END_NODE tag of /aliases */
241c3c4c005SSimon Glass 
242c3c4c005SSimon Glass 	did_alias_header = 0;
243c3c4c005SSimon Glass 	info->region = region;
244c3c4c005SSimon Glass 	info->count = count;
245c3c4c005SSimon Glass 	info->can_merge = 0;
246c3c4c005SSimon Glass 	info->max_regions = max_regions;
247c3c4c005SSimon Glass 
248c3c4c005SSimon Glass 	for (offset = fdt_first_property_offset(fdt, node);
249c3c4c005SSimon Glass 	     offset >= 0;
250c3c4c005SSimon Glass 	     offset = fdt_next_property_offset(fdt, offset)) {
251c3c4c005SSimon Glass 		const struct fdt_property *prop;
252c3c4c005SSimon Glass 		const char *name;
253c3c4c005SSimon Glass 		int target, next;
254c3c4c005SSimon Glass 
255c3c4c005SSimon Glass 		prop = fdt_get_property_by_offset(fdt, offset, NULL);
256c3c4c005SSimon Glass 		name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
257c3c4c005SSimon Glass 		target = fdt_path_offset(fdt, name);
258c3c4c005SSimon Glass 		if (!region_list_contains_offset(info, fdt, target))
259c3c4c005SSimon Glass 			continue;
260c3c4c005SSimon Glass 		next = fdt_next_property_offset(fdt, offset);
261c3c4c005SSimon Glass 		if (next < 0)
2629d8ac956SSimon Glass 			next = node_end;
263c3c4c005SSimon Glass 
264c3c4c005SSimon Glass 		if (!did_alias_header) {
265c3c4c005SSimon Glass 			fdt_add_region(info, base + node, 12);
266c3c4c005SSimon Glass 			did_alias_header = 1;
267c3c4c005SSimon Glass 		}
268c3c4c005SSimon Glass 		fdt_add_region(info, base + offset, next - offset);
269c3c4c005SSimon Glass 	}
270c3c4c005SSimon Glass 
271c47a38b4SSimon Glass 	/* Add the FDT_END_NODE tag */
272c3c4c005SSimon Glass 	if (did_alias_header)
273c3c4c005SSimon Glass 		fdt_add_region(info, base + node_end, sizeof(fdt32_t));
274c3c4c005SSimon Glass 
275c3c4c005SSimon Glass 	return info->count < max_regions ? info->count : -FDT_ERR_NOSPACE;
276c3c4c005SSimon Glass }
277c3c4c005SSimon Glass 
278c3c4c005SSimon Glass /**
279c3c4c005SSimon Glass  * fdt_include_supernodes() - Include supernodes required by this node
2806feed2a5SRobert P. J. Day  * @info:	State information
2816feed2a5SRobert P. J. Day  * @depth:	Current stack depth
282c3c4c005SSimon Glass  *
283c3c4c005SSimon Glass  * When we decided to include a node or property which is not at the top
284c3c4c005SSimon Glass  * level, this function forces the inclusion of higher level nodes. For
285c3c4c005SSimon Glass  * example, given this tree:
286c3c4c005SSimon Glass  *
287c3c4c005SSimon Glass  * / {
288c3c4c005SSimon Glass  *     testing {
289c3c4c005SSimon Glass  *     }
290c3c4c005SSimon Glass  * }
291c3c4c005SSimon Glass  *
292c3c4c005SSimon Glass  * If we decide to include testing then we need the root node to have a valid
293c3c4c005SSimon Glass  * tree. This function adds those regions.
294c3c4c005SSimon Glass  */
fdt_include_supernodes(struct fdt_region_state * info,int depth)295c3c4c005SSimon Glass static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
296c3c4c005SSimon Glass {
297c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(info->fdt);
298c3c4c005SSimon Glass 	int start, stop_at;
299c3c4c005SSimon Glass 	int i;
300c3c4c005SSimon Glass 
301c3c4c005SSimon Glass 	/*
302c3c4c005SSimon Glass 	 * Work down the stack looking for supernodes that we didn't include.
303c3c4c005SSimon Glass 	 * The algortihm here is actually pretty simple, since we know that
304c3c4c005SSimon Glass 	 * no previous subnode had to include these nodes, or if it did, we
305c3c4c005SSimon Glass 	 * marked them as included (on the stack) already.
306c3c4c005SSimon Glass 	 */
307c3c4c005SSimon Glass 	for (i = 0; i <= depth; i++) {
308c3c4c005SSimon Glass 		if (!info->stack[i].included) {
309c3c4c005SSimon Glass 			start = info->stack[i].offset;
310c3c4c005SSimon Glass 
311c3c4c005SSimon Glass 			/* Add the FDT_BEGIN_NODE tag of this supernode */
312c3c4c005SSimon Glass 			fdt_next_tag(info->fdt, start, &stop_at);
313c3c4c005SSimon Glass 			if (fdt_add_region(info, base + start, stop_at - start))
314c3c4c005SSimon Glass 				return -1;
315c3c4c005SSimon Glass 
316c3c4c005SSimon Glass 			/* Remember that this supernode is now included */
317c3c4c005SSimon Glass 			info->stack[i].included = 1;
318c3c4c005SSimon Glass 			info->can_merge = 1;
319c3c4c005SSimon Glass 		}
320c3c4c005SSimon Glass 
321c3c4c005SSimon Glass 		/* Force (later) generation of the FDT_END_NODE tag */
322c3c4c005SSimon Glass 		if (!info->stack[i].want)
323c3c4c005SSimon Glass 			info->stack[i].want = WANT_NODES_ONLY;
324c3c4c005SSimon Glass 	}
325c3c4c005SSimon Glass 
326c3c4c005SSimon Glass 	return 0;
327c3c4c005SSimon Glass }
328c3c4c005SSimon Glass 
329c3c4c005SSimon Glass enum {
330c3c4c005SSimon Glass 	FDT_DONE_NOTHING,
331c3c4c005SSimon Glass 	FDT_DONE_MEM_RSVMAP,
332c3c4c005SSimon Glass 	FDT_DONE_STRUCT,
333c3c4c005SSimon Glass 	FDT_DONE_END,
334c3c4c005SSimon Glass 	FDT_DONE_STRINGS,
335c3c4c005SSimon Glass 	FDT_DONE_ALL,
336c3c4c005SSimon Glass };
337c3c4c005SSimon 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)338c3c4c005SSimon Glass int fdt_first_region(const void *fdt,
339c3c4c005SSimon Glass 		int (*h_include)(void *priv, const void *fdt, int offset,
340c3c4c005SSimon Glass 				 int type, const char *data, int size),
341c3c4c005SSimon Glass 		void *priv, struct fdt_region *region,
342c3c4c005SSimon Glass 		char *path, int path_len, int flags,
343c3c4c005SSimon Glass 		struct fdt_region_state *info)
344c3c4c005SSimon Glass {
345c3c4c005SSimon Glass 	struct fdt_region_ptrs *p = &info->ptrs;
346c3c4c005SSimon Glass 
347c3c4c005SSimon Glass 	/* Set up our state */
348c3c4c005SSimon Glass 	info->fdt = fdt;
349c3c4c005SSimon Glass 	info->can_merge = 1;
350c3c4c005SSimon Glass 	info->max_regions = 1;
351c3c4c005SSimon Glass 	info->start = -1;
352c3c4c005SSimon Glass 	p->want = WANT_NOTHING;
353c3c4c005SSimon Glass 	p->end = path;
354c3c4c005SSimon Glass 	*p->end = '\0';
355c3c4c005SSimon Glass 	p->nextoffset = 0;
356c3c4c005SSimon Glass 	p->depth = -1;
357c3c4c005SSimon Glass 	p->done = FDT_DONE_NOTHING;
358c3c4c005SSimon Glass 
359c3c4c005SSimon Glass 	return fdt_next_region(fdt, h_include, priv, region,
360c3c4c005SSimon Glass 			       path, path_len, flags, info);
361c3c4c005SSimon Glass }
362c3c4c005SSimon Glass 
3636feed2a5SRobert P. J. Day /***********************************************************************
3646feed2a5SRobert P. J. Day  *
365c3c4c005SSimon Glass  *	Theory of operation
366c3c4c005SSimon Glass  *
3676feed2a5SRobert P. J. Day  * Note: in this description 'included' means that a node (or other part
3686feed2a5SRobert P. J. Day  * of the tree) should be included in the region list, i.e. it will have
3696feed2a5SRobert P. J. Day  * a region which covers its part of the tree.
370c3c4c005SSimon Glass  *
3716feed2a5SRobert P. J. Day  * This function maintains some state from the last time it is called.
3726feed2a5SRobert P. J. Day  * It checks the next part of the tree that it is supposed to look at
3736feed2a5SRobert P. J. Day  * (p.nextoffset) to see if that should be included or not. When it
3746feed2a5SRobert P. J. Day  * finds something to include, it sets info->start to its offset. This
3756feed2a5SRobert P. J. Day  * marks the start of the region we want to include.
376c3c4c005SSimon Glass  *
3776feed2a5SRobert P. J. Day  * Once info->start is set to the start (i.e. not -1), we continue
3786feed2a5SRobert P. J. Day  * scanning until we find something that we don't want included. This
3796feed2a5SRobert P. J. Day  * will be the end of a region. At this point we can close off the
3806feed2a5SRobert P. J. Day  * region and add it to the list. So we do so, and reset info->start
3816feed2a5SRobert P. J. Day  * to -1.
3826feed2a5SRobert P. J. Day  *
3836feed2a5SRobert P. J. Day  * One complication here is that we want to merge regions. So when we
3846feed2a5SRobert P. J. Day  * come to add another region later, we may in fact merge it with the
3856feed2a5SRobert P. J. Day  * previous one if one ends where the other starts.
3866feed2a5SRobert P. J. Day  *
3876feed2a5SRobert P. J. Day  * The function fdt_add_region() will return -1 if it fails to add the
3886feed2a5SRobert P. J. Day  * region, because we already have a region ready to be returned, and
3896feed2a5SRobert P. J. Day  * the new one cannot be merged in with it. In this case, we must return
3906feed2a5SRobert P. J. Day  * the region we found, and wait for another call to this function.
3916feed2a5SRobert P. J. Day  * When it comes, we will repeat the processing of the tag and again
3926feed2a5SRobert P. J. Day  * try to add a region. This time it will succeed.
3936feed2a5SRobert P. J. Day  *
3946feed2a5SRobert P. J. Day  * The current state of the pointers (stack, offset, etc.) is maintained
3956feed2a5SRobert P. J. Day  * in a ptrs member. At the start of every loop iteration we make a copy
3966feed2a5SRobert P. J. Day  * of it.  The copy is then updated as the tag is processed. Only if we
3976feed2a5SRobert P. J. Day  * get to the end of the loop iteration (and successfully call
3986feed2a5SRobert P. J. Day  * fdt_add_region() if we need to) can we commit the changes we have
3996feed2a5SRobert P. J. Day  * made to these pointers. For example, if we see an FDT_END_NODE tag,
4006feed2a5SRobert P. J. Day  * we will decrement the depth value. But if we need to add a region
4016feed2a5SRobert P. J. Day  * for this tag (let's say because the previous tag is included and this
4026feed2a5SRobert P. J. Day  * FDT_END_NODE tag is not included) then we will only commit the result
4036feed2a5SRobert P. J. Day  * if we were able to add the region. That allows us to retry again next
4046feed2a5SRobert P. J. Day  * time.
4056feed2a5SRobert P. J. Day  *
4066feed2a5SRobert P. J. Day  * We keep track of a variable called 'want' which tells us what we want
4076feed2a5SRobert P. J. Day  * to include when there is no specific information provided by the
4086feed2a5SRobert P. J. Day  * h_include function for a particular property. This basically handles
4096feed2a5SRobert P. J. Day  * the inclusion of properties which are pulled in by virtue of the node
4106feed2a5SRobert P. J. Day  * they are in. So if you include a node, its properties are also
4116feed2a5SRobert P. J. Day  * included.  In this case 'want' will be WANT_NODES_AND_PROPS. The
4126feed2a5SRobert P. J. Day  * FDT_REG_DIRECT_SUBNODES feature also makes use of 'want'. While we
4136feed2a5SRobert P. J. Day  * are inside the subnode, 'want' will be set to WANT_NODES_ONLY, so
4146feed2a5SRobert P. J. Day  * that only the subnode's FDT_BEGIN_NODE and FDT_END_NODE tags will be
4156feed2a5SRobert P. J. Day  * included, and properties will be skipped. If WANT_NOTHING is
4166feed2a5SRobert P. J. Day  * selected, then we will just rely on what the h_include() function
4176feed2a5SRobert P. J. Day  * tells us.
4186feed2a5SRobert P. J. Day  *
4196feed2a5SRobert P. J. Day  * Using 'want' we work out 'include', which tells us whether this
4206feed2a5SRobert P. J. Day  * current tag should be included or not. As you can imagine, if the
4216feed2a5SRobert P. J. Day  * value of 'include' changes, that means we are on a boundary between
4226feed2a5SRobert P. J. Day  * nodes to include and nodes to exclude. At this point we either close
4236feed2a5SRobert P. J. Day  * off a previous region and add it to the list, or mark the start of a
4246feed2a5SRobert P. J. Day  * new region.
4256feed2a5SRobert P. J. Day  *
4266feed2a5SRobert P. J. Day  * Apart from the nodes, we have mem_rsvmap, the FDT_END tag and the
4276feed2a5SRobert P. J. Day  * string list. Each of these dealt with as a whole (i.e. we create a
4286feed2a5SRobert P. J. Day  * region for each if it is to be included). For mem_rsvmap we don't
4296feed2a5SRobert P. J. Day  * allow it to merge with the first struct region. For the stringlist,
4306feed2a5SRobert P. J. Day  * we don't allow it to merge with the last struct region (which
4316feed2a5SRobert P. J. Day  * contains at minimum the FDT_END tag).
4326feed2a5SRobert P. J. Day  *
4336feed2a5SRobert P. J. Day  *********************************************************************/
434c3c4c005SSimon 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)435c3c4c005SSimon Glass int fdt_next_region(const void *fdt,
436c3c4c005SSimon Glass 		int (*h_include)(void *priv, const void *fdt, int offset,
437c3c4c005SSimon Glass 				 int type, const char *data, int size),
438c3c4c005SSimon Glass 		void *priv, struct fdt_region *region,
439c3c4c005SSimon Glass 		char *path, int path_len, int flags,
440c3c4c005SSimon Glass 		struct fdt_region_state *info)
441c3c4c005SSimon Glass {
442c3c4c005SSimon Glass 	int base = fdt_off_dt_struct(fdt);
443c3c4c005SSimon Glass 	int last_node = 0;
444c3c4c005SSimon Glass 	const char *str;
445c3c4c005SSimon Glass 
446c3c4c005SSimon Glass 	info->region = region;
447c3c4c005SSimon Glass 	info->count = 0;
448c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_MEM_RSVMAP &&
449c3c4c005SSimon Glass 	    (flags & FDT_REG_ADD_MEM_RSVMAP)) {
450c3c4c005SSimon Glass 		/* Add the memory reserve map into its own region */
451c3c4c005SSimon Glass 		if (fdt_add_region(info, fdt_off_mem_rsvmap(fdt),
452c3c4c005SSimon Glass 				   fdt_off_dt_struct(fdt) -
453c3c4c005SSimon Glass 				   fdt_off_mem_rsvmap(fdt)))
454c3c4c005SSimon Glass 			return 0;
455c3c4c005SSimon Glass 		info->can_merge = 0;	/* Don't allow merging with this */
456c3c4c005SSimon Glass 		info->ptrs.done = FDT_DONE_MEM_RSVMAP;
457c3c4c005SSimon Glass 	}
458c3c4c005SSimon Glass 
459c3c4c005SSimon Glass 	/*
460c3c4c005SSimon Glass 	 * Work through the tags one by one, deciding whether each needs to
461c3c4c005SSimon Glass 	 * be included or not. We set the variable 'include' to indicate our
462c3c4c005SSimon Glass 	 * decision. 'want' is used to track what we want to include - it
463c3c4c005SSimon Glass 	 * allows us to pick up all the properties (and/or subnode tags) of
464c3c4c005SSimon Glass 	 * a node.
465c3c4c005SSimon Glass 	 */
466c3c4c005SSimon Glass 	while (info->ptrs.done < FDT_DONE_STRUCT) {
467c3c4c005SSimon Glass 		const struct fdt_property *prop;
468c3c4c005SSimon Glass 		struct fdt_region_ptrs p;
469c3c4c005SSimon Glass 		const char *name;
470c3c4c005SSimon Glass 		int include = 0;
471c3c4c005SSimon Glass 		int stop_at = 0;
472c3c4c005SSimon Glass 		uint32_t tag;
473c3c4c005SSimon Glass 		int offset;
474c3c4c005SSimon Glass 		int val;
475c3c4c005SSimon Glass 		int len;
476c3c4c005SSimon Glass 
477c3c4c005SSimon Glass 		/*
478c3c4c005SSimon Glass 		 * Make a copy of our pointers. If we make it to the end of
479c3c4c005SSimon Glass 		 * this block then we will commit them back to info->ptrs.
480c3c4c005SSimon Glass 		 * Otherwise we can try again from the same starting state
481c3c4c005SSimon Glass 		 * next time we are called.
482c3c4c005SSimon Glass 		 */
483c3c4c005SSimon Glass 		p = info->ptrs;
484c3c4c005SSimon Glass 
485c3c4c005SSimon Glass 		/*
486c3c4c005SSimon Glass 		 * Find the tag, and the offset of the next one. If we need to
487c3c4c005SSimon Glass 		 * stop including tags, then by default we stop *after*
488c3c4c005SSimon Glass 		 * including the current tag
489c3c4c005SSimon Glass 		 */
490c3c4c005SSimon Glass 		offset = p.nextoffset;
491c3c4c005SSimon Glass 		tag = fdt_next_tag(fdt, offset, &p.nextoffset);
492c3c4c005SSimon Glass 		stop_at = p.nextoffset;
493c3c4c005SSimon Glass 
494c3c4c005SSimon Glass 		switch (tag) {
495c3c4c005SSimon Glass 		case FDT_PROP:
496c3c4c005SSimon Glass 			stop_at = offset;
497c3c4c005SSimon Glass 			prop = fdt_get_property_by_offset(fdt, offset, NULL);
498c3c4c005SSimon Glass 			str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
499c3c4c005SSimon Glass 			val = h_include(priv, fdt, last_node, FDT_IS_PROP, str,
500c3c4c005SSimon Glass 					    strlen(str) + 1);
501c3c4c005SSimon Glass 			if (val == -1) {
502c3c4c005SSimon Glass 				include = p.want >= WANT_NODES_AND_PROPS;
503c3c4c005SSimon Glass 			} else {
504c3c4c005SSimon Glass 				include = val;
505c3c4c005SSimon Glass 				/*
506c3c4c005SSimon Glass 				 * Make sure we include the } for this block.
507c3c4c005SSimon Glass 				 * It might be more correct to have this done
508c3c4c005SSimon Glass 				 * by the call to fdt_include_supernodes() in
509c3c4c005SSimon Glass 				 * the case where it adds the node we are
510c3c4c005SSimon Glass 				 * currently in, but this is equivalent.
511c3c4c005SSimon Glass 				 */
512c3c4c005SSimon Glass 				if ((flags & FDT_REG_SUPERNODES) && val &&
513c3c4c005SSimon Glass 				    !p.want)
514c3c4c005SSimon Glass 					p.want = WANT_NODES_ONLY;
515c3c4c005SSimon Glass 			}
516c3c4c005SSimon Glass 
517c3c4c005SSimon Glass 			/* Value grepping is not yet supported */
518c3c4c005SSimon Glass 			break;
519c3c4c005SSimon Glass 
520c3c4c005SSimon Glass 		case FDT_NOP:
521c3c4c005SSimon Glass 			include = p.want >= WANT_NODES_AND_PROPS;
522c3c4c005SSimon Glass 			stop_at = offset;
523c3c4c005SSimon Glass 			break;
524c3c4c005SSimon Glass 
525c3c4c005SSimon Glass 		case FDT_BEGIN_NODE:
526c3c4c005SSimon Glass 			last_node = offset;
527c3c4c005SSimon Glass 			p.depth++;
528c3c4c005SSimon Glass 			if (p.depth == FDT_MAX_DEPTH)
5292bf94120SSimon Glass 				return -FDT_ERR_BADSTRUCTURE;
530c3c4c005SSimon Glass 			name = fdt_get_name(fdt, offset, &len);
531c3c4c005SSimon Glass 			if (p.end - path + 2 + len >= path_len)
532c3c4c005SSimon Glass 				return -FDT_ERR_NOSPACE;
533c3c4c005SSimon Glass 
534c3c4c005SSimon Glass 			/* Build the full path of this node */
535c3c4c005SSimon Glass 			if (p.end != path + 1)
536c3c4c005SSimon Glass 				*p.end++ = '/';
537c3c4c005SSimon Glass 			strcpy(p.end, name);
538c3c4c005SSimon Glass 			p.end += len;
539c3c4c005SSimon Glass 			info->stack[p.depth].want = p.want;
540c3c4c005SSimon Glass 			info->stack[p.depth].offset = offset;
541c3c4c005SSimon Glass 
542c3c4c005SSimon Glass 			/*
543c3c4c005SSimon Glass 			 * If we are not intending to include this node unless
544c3c4c005SSimon Glass 			 * it matches, make sure we stop *before* its tag.
545c3c4c005SSimon Glass 			 */
546c3c4c005SSimon Glass 			if (p.want == WANT_NODES_ONLY ||
547c3c4c005SSimon Glass 			    !(flags & (FDT_REG_DIRECT_SUBNODES |
548c3c4c005SSimon Glass 				       FDT_REG_ALL_SUBNODES))) {
549c3c4c005SSimon Glass 				stop_at = offset;
550c3c4c005SSimon Glass 				p.want = WANT_NOTHING;
551c3c4c005SSimon Glass 			}
552c3c4c005SSimon Glass 			val = h_include(priv, fdt, offset, FDT_IS_NODE, path,
553c3c4c005SSimon Glass 					p.end - path + 1);
554c3c4c005SSimon Glass 
555c3c4c005SSimon Glass 			/* Include this if requested */
556c3c4c005SSimon Glass 			if (val) {
557c3c4c005SSimon Glass 				p.want = (flags & FDT_REG_ALL_SUBNODES) ?
558c3c4c005SSimon Glass 					WANT_ALL_NODES_AND_PROPS :
559c3c4c005SSimon Glass 					WANT_NODES_AND_PROPS;
560c3c4c005SSimon Glass 			}
561c3c4c005SSimon Glass 
562c3c4c005SSimon Glass 			/* If not requested, decay our 'p.want' value */
563c3c4c005SSimon Glass 			else if (p.want) {
564c3c4c005SSimon Glass 				if (p.want != WANT_ALL_NODES_AND_PROPS)
565c3c4c005SSimon Glass 					p.want--;
566c3c4c005SSimon Glass 
567c3c4c005SSimon Glass 			/* Not including this tag, so stop now */
568c3c4c005SSimon Glass 			} else {
569c3c4c005SSimon Glass 				stop_at = offset;
570c3c4c005SSimon Glass 			}
571c3c4c005SSimon Glass 
572c3c4c005SSimon Glass 			/*
573c3c4c005SSimon Glass 			 * Decide whether to include this tag, and update our
574c3c4c005SSimon Glass 			 * stack with the state for this node
575c3c4c005SSimon Glass 			 */
576c3c4c005SSimon Glass 			include = p.want;
577c3c4c005SSimon Glass 			info->stack[p.depth].included = include;
578c3c4c005SSimon Glass 			break;
579c3c4c005SSimon Glass 
580c3c4c005SSimon Glass 		case FDT_END_NODE:
581c3c4c005SSimon Glass 			include = p.want;
582c3c4c005SSimon Glass 			if (p.depth < 0)
583c3c4c005SSimon Glass 				return -FDT_ERR_BADSTRUCTURE;
584c3c4c005SSimon Glass 
585c3c4c005SSimon Glass 			/*
586c3c4c005SSimon Glass 			 * If we don't want this node, stop right away, unless
587c3c4c005SSimon Glass 			 * we are including subnodes
588c3c4c005SSimon Glass 			 */
589c3c4c005SSimon Glass 			if (!p.want && !(flags & FDT_REG_DIRECT_SUBNODES))
590c3c4c005SSimon Glass 				stop_at = offset;
591c3c4c005SSimon Glass 			p.want = info->stack[p.depth].want;
592c3c4c005SSimon Glass 			p.depth--;
593c3c4c005SSimon Glass 			while (p.end > path && *--p.end != '/')
594c3c4c005SSimon Glass 				;
595c3c4c005SSimon Glass 			*p.end = '\0';
596c3c4c005SSimon Glass 			break;
597c3c4c005SSimon Glass 
598c3c4c005SSimon Glass 		case FDT_END:
599c3c4c005SSimon Glass 			/* We always include the end tag */
600c3c4c005SSimon Glass 			include = 1;
601c3c4c005SSimon Glass 			p.done = FDT_DONE_STRUCT;
602c3c4c005SSimon Glass 			break;
603c3c4c005SSimon Glass 		}
604c3c4c005SSimon Glass 
605c3c4c005SSimon Glass 		/* If this tag is to be included, mark it as region start */
606c3c4c005SSimon Glass 		if (include && info->start == -1) {
607c3c4c005SSimon Glass 			/* Include any supernodes required by this one */
608c3c4c005SSimon Glass 			if (flags & FDT_REG_SUPERNODES) {
609c3c4c005SSimon Glass 				if (fdt_include_supernodes(info, p.depth))
610c3c4c005SSimon Glass 					return 0;
611c3c4c005SSimon Glass 			}
612c3c4c005SSimon Glass 			info->start = offset;
613c3c4c005SSimon Glass 		}
614c3c4c005SSimon Glass 
615c3c4c005SSimon Glass 		/*
616c3c4c005SSimon Glass 		 * If this tag is not to be included, finish up the current
617c3c4c005SSimon Glass 		 * region.
618c3c4c005SSimon Glass 		 */
619c3c4c005SSimon Glass 		if (!include && info->start != -1) {
620c3c4c005SSimon Glass 			if (fdt_add_region(info, base + info->start,
621c3c4c005SSimon Glass 					   stop_at - info->start))
622c3c4c005SSimon Glass 				return 0;
623c3c4c005SSimon Glass 			info->start = -1;
624c3c4c005SSimon Glass 			info->can_merge = 1;
625c3c4c005SSimon Glass 		}
626c3c4c005SSimon Glass 
627c3c4c005SSimon Glass 		/* If we have made it this far, we can commit our pointers */
628c3c4c005SSimon Glass 		info->ptrs = p;
629c3c4c005SSimon Glass 	}
630c3c4c005SSimon Glass 
631c3c4c005SSimon Glass 	/* Add a region for the END tag and a separate one for string table */
632c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_END) {
633c3c4c005SSimon Glass 		if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
634c3c4c005SSimon Glass 			return -FDT_ERR_BADSTRUCTURE;
635c3c4c005SSimon Glass 
636c3c4c005SSimon Glass 		if (fdt_add_region(info, base + info->start,
637c3c4c005SSimon Glass 				   info->ptrs.nextoffset - info->start))
638c3c4c005SSimon Glass 			return 0;
639c3c4c005SSimon Glass 		info->ptrs.done++;
640c3c4c005SSimon Glass 	}
641c3c4c005SSimon Glass 	if (info->ptrs.done < FDT_DONE_STRINGS) {
642c3c4c005SSimon Glass 		if (flags & FDT_REG_ADD_STRING_TAB) {
643c3c4c005SSimon Glass 			info->can_merge = 0;
644c3c4c005SSimon Glass 			if (fdt_off_dt_strings(fdt) <
645c3c4c005SSimon Glass 			    base + info->ptrs.nextoffset)
646c3c4c005SSimon Glass 				return -FDT_ERR_BADLAYOUT;
647c3c4c005SSimon Glass 			if (fdt_add_region(info, fdt_off_dt_strings(fdt),
648c3c4c005SSimon Glass 					   fdt_size_dt_strings(fdt)))
649c3c4c005SSimon Glass 				return 0;
650c3c4c005SSimon Glass 		}
651c3c4c005SSimon Glass 		info->ptrs.done++;
652c3c4c005SSimon Glass 	}
653c3c4c005SSimon Glass 
654c3c4c005SSimon Glass 	return info->count > 0 ? 0 : -FDT_ERR_NOTFOUND;
655c3c4c005SSimon Glass }
656