libfdt: Add and use a node iteration helper function.
This patch adds an fdt_next_node() function which can be used to iterate through nodes of the tree while keeping track of depth. This function is used to simplify the iteration code in a lot of other functions, and is also exported for use by library users. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
		
							parent
							
								
									8cf30809a8
								
							
						
					
					
						commit
						d1cc52879c
					
				|  | @ -130,6 +130,12 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) | ||||||
| 
 | 
 | ||||||
| uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset); | uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset); | ||||||
| 
 | 
 | ||||||
|  | /**********************************************************************/ | ||||||
|  | /* Traversal functions                                                */ | ||||||
|  | /**********************************************************************/ | ||||||
|  | 
 | ||||||
|  | int fdt_next_node(const void *fdt, int offset, int *depth); | ||||||
|  | 
 | ||||||
| /**********************************************************************/ | /**********************************************************************/ | ||||||
| /* General functions                                                  */ | /* General functions                                                  */ | ||||||
| /**********************************************************************/ | /**********************************************************************/ | ||||||
|  |  | ||||||
							
								
								
									
										41
									
								
								libfdt/fdt.c
								
								
								
								
							
							
						
						
									
										41
									
								
								libfdt/fdt.c
								
								
								
								
							|  | @ -133,6 +133,47 @@ uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset) | ||||||
| 	return tag; | 	return tag; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int fdt_next_node(const void *fdt, int offset, int *depth) | ||||||
|  | { | ||||||
|  | 	int nextoffset = 0; | ||||||
|  | 	uint32_t tag; | ||||||
|  | 
 | ||||||
|  | 	if (offset >= 0) { | ||||||
|  | 		tag = fdt_next_tag(fdt, offset, &nextoffset); | ||||||
|  | 		if (tag != FDT_BEGIN_NODE) | ||||||
|  | 			return -FDT_ERR_BADOFFSET; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	do { | ||||||
|  | 		offset = nextoffset; | ||||||
|  | 		tag = fdt_next_tag(fdt, offset, &nextoffset); | ||||||
|  | 
 | ||||||
|  | 		switch (tag) { | ||||||
|  | 		case FDT_PROP: | ||||||
|  | 		case FDT_NOP: | ||||||
|  | 			break; | ||||||
|  | 
 | ||||||
|  | 		case FDT_BEGIN_NODE: | ||||||
|  | 			if (depth) | ||||||
|  | 				(*depth)++; | ||||||
|  | 			break; | ||||||
|  | 
 | ||||||
|  | 		case FDT_END_NODE: | ||||||
|  | 			if (depth) | ||||||
|  | 				(*depth)--; | ||||||
|  | 			break; | ||||||
|  | 
 | ||||||
|  | 		case FDT_END: | ||||||
|  | 			return -FDT_ERR_NOTFOUND; | ||||||
|  | 
 | ||||||
|  | 		default: | ||||||
|  | 			return -FDT_ERR_BADSTRUCTURE; | ||||||
|  | 		} | ||||||
|  | 	} while (tag != FDT_BEGIN_NODE); | ||||||
|  | 
 | ||||||
|  | 	return offset; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) | const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) | ||||||
| { | { | ||||||
| 	int len = strlen(s) + 1; | 	int len = strlen(s) + 1; | ||||||
|  |  | ||||||
							
								
								
									
										236
									
								
								libfdt/fdt_ro.c
								
								
								
								
							
							
						
						
									
										236
									
								
								libfdt/fdt_ro.c
								
								
								
								
							|  | @ -69,7 +69,7 @@ | ||||||
| static int nodename_eq(const void *fdt, int offset, | static int nodename_eq(const void *fdt, int offset, | ||||||
| 		       const char *s, int len) | 		       const char *s, int len) | ||||||
| { | { | ||||||
| 	const char *p = fdt_offset_ptr(fdt, offset, len+1); | 	const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1); | ||||||
| 
 | 
 | ||||||
| 	if (! p) | 	if (! p) | ||||||
| 		/* short match */ | 		/* short match */ | ||||||
|  | @ -108,50 +108,24 @@ int fdt_num_mem_rsv(const void *fdt) | ||||||
| 	return i; | 	return i; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, | int fdt_subnode_offset_namelen(const void *fdt, int offset, | ||||||
| 			       const char *name, int namelen) | 			       const char *name, int namelen) | ||||||
| { | { | ||||||
| 	int level = 0; | 	int depth; | ||||||
| 	uint32_t tag; |  | ||||||
| 	int offset, nextoffset; |  | ||||||
| 
 | 
 | ||||||
| 	CHECK_HEADER(fdt); | 	CHECK_HEADER(fdt); | ||||||
| 
 | 
 | ||||||
| 	tag = fdt_next_tag(fdt, parentoffset, &nextoffset); | 	for (depth = 0; | ||||||
| 	if (tag != FDT_BEGIN_NODE) | 	     offset >= 0; | ||||||
| 		return -FDT_ERR_BADOFFSET; | 	     offset = fdt_next_node(fdt, offset, &depth)) { | ||||||
| 
 | 		if (depth < 0) | ||||||
| 	do { |  | ||||||
| 		offset = nextoffset; |  | ||||||
| 		tag = fdt_next_tag(fdt, offset, &nextoffset); |  | ||||||
| 
 |  | ||||||
| 		switch (tag) { |  | ||||||
| 		case FDT_END: |  | ||||||
| 			return -FDT_ERR_TRUNCATED; |  | ||||||
| 
 |  | ||||||
| 		case FDT_BEGIN_NODE: |  | ||||||
| 			level++; |  | ||||||
| 			if (level != 1) |  | ||||||
| 				continue; |  | ||||||
| 			if (nodename_eq(fdt, offset+FDT_TAGSIZE, name, namelen)) |  | ||||||
| 				/* Found it! */ |  | ||||||
| 				return offset; |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		case FDT_END_NODE: |  | ||||||
| 			level--; |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		case FDT_PROP: |  | ||||||
| 		case FDT_NOP: |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		default: |  | ||||||
| 			return -FDT_ERR_BADSTRUCTURE; |  | ||||||
| 		} |  | ||||||
| 	} while (level >= 0); |  | ||||||
| 
 |  | ||||||
| 			return -FDT_ERR_NOTFOUND; | 			return -FDT_ERR_NOTFOUND; | ||||||
|  | 		else if ((depth == 1) | ||||||
|  | 			 && nodename_eq(fdt, offset, name, namelen)) | ||||||
|  | 			return offset; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return offset; /* error */ | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int fdt_subnode_offset(const void *fdt, int parentoffset, | int fdt_subnode_offset(const void *fdt, int parentoffset, | ||||||
|  | @ -311,62 +285,40 @@ uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) | ||||||
| 
 | 
 | ||||||
| int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) | ||||||
| { | { | ||||||
| 	uint32_t tag; | 	int pdepth = 0, p = 0; | ||||||
| 	int p = 0, overflow = 0; | 	int offset, depth, namelen; | ||||||
| 	int offset, nextoffset, namelen; |  | ||||||
| 	const char *name; | 	const char *name; | ||||||
| 
 | 
 | ||||||
| 	CHECK_HEADER(fdt); | 	CHECK_HEADER(fdt); | ||||||
| 
 | 
 | ||||||
| 	tag = fdt_next_tag(fdt, 0, &nextoffset); |  | ||||||
| 	if (tag != FDT_BEGIN_NODE) |  | ||||||
| 		return -FDT_ERR_BADSTRUCTURE; |  | ||||||
| 
 |  | ||||||
| 	if (buflen < 2) | 	if (buflen < 2) | ||||||
| 		return -FDT_ERR_NOSPACE; | 		return -FDT_ERR_NOSPACE; | ||||||
| 	buf[0] = '/'; |  | ||||||
| 	p = 1; |  | ||||||
| 
 | 
 | ||||||
| 	while (nextoffset <= nodeoffset) { | 	for (offset = 0, depth = 0; | ||||||
| 		offset = nextoffset; | 	     (offset >= 0) && (offset <= nodeoffset); | ||||||
| 		tag = fdt_next_tag(fdt, offset, &nextoffset); | 	     offset = fdt_next_node(fdt, offset, &depth)) { | ||||||
| 		switch (tag) { | 		if (pdepth < depth) | ||||||
| 		case FDT_END: | 			continue; /* overflowed buffer */ | ||||||
| 			return -FDT_ERR_BADOFFSET; |  | ||||||
| 
 | 
 | ||||||
| 		case FDT_BEGIN_NODE: | 		while (pdepth > depth) { | ||||||
| 			name = fdt_get_name(fdt, offset, &namelen); |  | ||||||
| 			if (!name) |  | ||||||
| 				return namelen; |  | ||||||
| 			if (overflow || ((p + namelen + 1) > buflen)) { |  | ||||||
| 				overflow++; |  | ||||||
| 				break; |  | ||||||
| 			} |  | ||||||
| 			memcpy(buf + p, name, namelen); |  | ||||||
| 			p += namelen; |  | ||||||
| 			buf[p++] = '/'; |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		case FDT_END_NODE: |  | ||||||
| 			if (overflow) { |  | ||||||
| 				overflow--; |  | ||||||
| 				break; |  | ||||||
| 			} |  | ||||||
| 			do { | 			do { | ||||||
| 				p--; | 				p--; | ||||||
| 			} while (buf[p-1] != '/'); | 			} while (buf[p-1] != '/'); | ||||||
| 			break; | 			pdepth--; | ||||||
| 
 |  | ||||||
| 		case FDT_PROP: |  | ||||||
| 		case FDT_NOP: |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		default: |  | ||||||
| 			return -FDT_ERR_BADSTRUCTURE; |  | ||||||
| 		} |  | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 	if (overflow) | 		name = fdt_get_name(fdt, offset, &namelen); | ||||||
|  | 		if (!name) | ||||||
|  | 			return namelen; | ||||||
|  | 		if ((p + namelen + 1) <= buflen) { | ||||||
|  | 			memcpy(buf + p, name, namelen); | ||||||
|  | 			p += namelen; | ||||||
|  | 			buf[p++] = '/'; | ||||||
|  | 			pdepth++; | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if (offset == nodeoffset) { | ||||||
|  | 			if (pdepth < (depth + 1)) | ||||||
| 				return -FDT_ERR_NOSPACE; | 				return -FDT_ERR_NOSPACE; | ||||||
| 
 | 
 | ||||||
| 			if (p > 1) /* special case so that root path is "/", not "" */ | 			if (p > 1) /* special case so that root path is "/", not "" */ | ||||||
|  | @ -374,13 +326,20 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) | ||||||
| 			buf[p] = '\0'; | 			buf[p] = '\0'; | ||||||
| 			return p; | 			return p; | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) | ||||||
|  | 		return -FDT_ERR_BADOFFSET; | ||||||
|  | 	else if (offset == -FDT_ERR_BADOFFSET) | ||||||
|  | 		return -FDT_ERR_BADSTRUCTURE; | ||||||
|  | 
 | ||||||
|  | 	return offset; /* error from fdt_next_node() */ | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, | ||||||
| 				 int supernodedepth, int *nodedepth) | 				 int supernodedepth, int *nodedepth) | ||||||
| { | { | ||||||
| 	int level = -1; | 	int offset, depth; | ||||||
| 	uint32_t tag; |  | ||||||
| 	int offset, nextoffset = 0; |  | ||||||
| 	int supernodeoffset = -FDT_ERR_INTERNAL; | 	int supernodeoffset = -FDT_ERR_INTERNAL; | ||||||
| 
 | 
 | ||||||
| 	CHECK_HEADER(fdt); | 	CHECK_HEADER(fdt); | ||||||
|  | @ -388,39 +347,30 @@ int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, | ||||||
| 	if (supernodedepth < 0) | 	if (supernodedepth < 0) | ||||||
| 		return -FDT_ERR_NOTFOUND; | 		return -FDT_ERR_NOTFOUND; | ||||||
| 
 | 
 | ||||||
| 	do { | 	for (offset = 0, depth = 0; | ||||||
| 		offset = nextoffset; | 	     (offset >= 0) && (offset <= nodeoffset); | ||||||
| 		tag = fdt_next_tag(fdt, offset, &nextoffset); | 	     offset = fdt_next_node(fdt, offset, &depth)) { | ||||||
| 		switch (tag) { | 		if (depth == supernodedepth) | ||||||
| 		case FDT_END: |  | ||||||
| 			return -FDT_ERR_BADOFFSET; |  | ||||||
| 
 |  | ||||||
| 		case FDT_BEGIN_NODE: |  | ||||||
| 			level++; |  | ||||||
| 			if (level == supernodedepth) |  | ||||||
| 			supernodeoffset = offset; | 			supernodeoffset = offset; | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		case FDT_END_NODE: |  | ||||||
| 			level--; |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		case FDT_PROP: |  | ||||||
| 		case FDT_NOP: |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		default: |  | ||||||
| 			return -FDT_ERR_BADSTRUCTURE; |  | ||||||
| 		} |  | ||||||
| 	} while (offset < nodeoffset); |  | ||||||
| 
 | 
 | ||||||
|  | 		if (offset == nodeoffset) { | ||||||
| 			if (nodedepth) | 			if (nodedepth) | ||||||
| 		*nodedepth = level; | 				*nodedepth = depth; | ||||||
| 
 | 
 | ||||||
| 	if (supernodedepth > level) | 			if (supernodedepth > depth) | ||||||
| 				return -FDT_ERR_NOTFOUND; | 				return -FDT_ERR_NOTFOUND; | ||||||
|  | 			else | ||||||
| 				return supernodeoffset; | 				return supernodeoffset; | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) | ||||||
|  | 		return -FDT_ERR_BADOFFSET; | ||||||
|  | 	else if (offset == -FDT_ERR_BADOFFSET) | ||||||
|  | 		return -FDT_ERR_BADSTRUCTURE; | ||||||
|  | 
 | ||||||
|  | 	return offset; /* error from fdt_next_node() */ | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| int fdt_node_depth(const void *fdt, int nodeoffset) | int fdt_node_depth(const void *fdt, int nodeoffset) | ||||||
| { | { | ||||||
|  | @ -447,51 +397,27 @@ int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, | ||||||
| 				  const char *propname, | 				  const char *propname, | ||||||
| 				  const void *propval, int proplen) | 				  const void *propval, int proplen) | ||||||
| { | { | ||||||
| 	uint32_t tag; | 	int offset; | ||||||
| 	int offset, nextoffset; |  | ||||||
| 	const void *val; | 	const void *val; | ||||||
| 	int len; | 	int len; | ||||||
| 
 | 
 | ||||||
| 	CHECK_HEADER(fdt); | 	CHECK_HEADER(fdt); | ||||||
| 
 | 
 | ||||||
| 	if (startoffset >= 0) { |  | ||||||
| 		tag = fdt_next_tag(fdt, startoffset, &nextoffset); |  | ||||||
| 		if (tag != FDT_BEGIN_NODE) |  | ||||||
| 			return -FDT_ERR_BADOFFSET; |  | ||||||
| 	} else { |  | ||||||
| 		nextoffset = 0; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	/* FIXME: The algorithm here is pretty horrible: we scan each
 | 	/* FIXME: The algorithm here is pretty horrible: we scan each
 | ||||||
| 	 * property of a node in fdt_getprop(), then if that didn't | 	 * property of a node in fdt_getprop(), then if that didn't | ||||||
| 	 * find what we want, we scan over them again making our way | 	 * find what we want, we scan over them again making our way | ||||||
| 	 * to the next node.  Still it's the easiest to implement | 	 * to the next node.  Still it's the easiest to implement | ||||||
| 	 * approach; performance can come later. */ | 	 * approach; performance can come later. */ | ||||||
| 	do { | 	for (offset = fdt_next_node(fdt, startoffset, NULL); | ||||||
| 		offset = nextoffset; | 	     offset >= 0; | ||||||
| 		tag = fdt_next_tag(fdt, offset, &nextoffset); | 	     offset = fdt_next_node(fdt, offset, NULL)) { | ||||||
| 
 |  | ||||||
| 		switch (tag) { |  | ||||||
| 		case FDT_BEGIN_NODE: |  | ||||||
| 		val = fdt_getprop(fdt, offset, propname, &len); | 		val = fdt_getprop(fdt, offset, propname, &len); | ||||||
| 			if (val | 		if (val && (len == proplen) | ||||||
| 			    && (len == proplen) |  | ||||||
| 		    && (memcmp(val, propval, len) == 0)) | 		    && (memcmp(val, propval, len) == 0)) | ||||||
| 			return offset; | 			return offset; | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		case FDT_PROP: |  | ||||||
| 		case FDT_END: |  | ||||||
| 		case FDT_END_NODE: |  | ||||||
| 		case FDT_NOP: |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		default: |  | ||||||
| 			return -FDT_ERR_BADSTRUCTURE; |  | ||||||
| 	} | 	} | ||||||
| 	} while (tag != FDT_END); |  | ||||||
| 
 | 
 | ||||||
| 	return -FDT_ERR_NOTFOUND; | 	return offset; /* error from fdt_next_node() */ | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) | int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) | ||||||
|  | @ -557,31 +483,15 @@ int fdt_node_offset_by_compatible(const void *fdt, int startoffset, | ||||||
| 	 * that didn't find what we want, we scan over them again | 	 * that didn't find what we want, we scan over them again | ||||||
| 	 * making our way to the next node.  Still it's the easiest to | 	 * making our way to the next node.  Still it's the easiest to | ||||||
| 	 * implement approach; performance can come later. */ | 	 * implement approach; performance can come later. */ | ||||||
| 	do { | 	for (offset = fdt_next_node(fdt, startoffset, NULL); | ||||||
| 		offset = nextoffset; | 	     offset >= 0; | ||||||
| 		tag = fdt_next_tag(fdt, offset, &nextoffset); | 	     offset = fdt_next_node(fdt, offset, NULL)) { | ||||||
| 
 | 		err = fdt_node_check_compatible(fdt, offset, compatible); | ||||||
| 		switch (tag) { | 		if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) | ||||||
| 		case FDT_BEGIN_NODE: |  | ||||||
| 			err = fdt_node_check_compatible(fdt, offset, |  | ||||||
| 							compatible); |  | ||||||
| 			if ((err < 0) |  | ||||||
| 			    && (err != -FDT_ERR_NOTFOUND)) |  | ||||||
| 			return err; | 			return err; | ||||||
| 		else if (err == 0) | 		else if (err == 0) | ||||||
| 			return offset; | 			return offset; | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		case FDT_PROP: |  | ||||||
| 		case FDT_END: |  | ||||||
| 		case FDT_END_NODE: |  | ||||||
| 		case FDT_NOP: |  | ||||||
| 			break; |  | ||||||
| 
 |  | ||||||
| 		default: |  | ||||||
| 			return -FDT_ERR_BADSTRUCTURE; |  | ||||||
| 	} | 	} | ||||||
| 	} while (tag != FDT_END); |  | ||||||
| 
 | 
 | ||||||
| 	return -FDT_ERR_NOTFOUND; | 	return offset; /* error from fdt_next_node() */ | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue