Skip to contentSkip to conversation

libfdt: Several new functions

PATCH New David Gibson 2 participants 5 messages Aug 30, 2007

lore
Share & tools
lore
#0 lore raw

This series of patches adds several new functions to libfdt. These are all read-only functions related to determining a given nodes node and ancestry.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#1 lore raw

PATCH 1/3 libfdt: Add fdt_get_name() to retrieve a node's name

This patch adds a new fdt_get_name() function to libfdt which will return a node's name string (including unit address, if any).

---
 libfdt/fdt_ro.c      |   24 ++++++++++++++
 libfdt/libfdt.h      |    2 +
 tests/Makefile.tests |    3 +
 tests/get_name.c     |   83 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/run_tests.sh   |    1 
 5 files changed, 112 insertions(+), 1 deletion(-)
libfdt/fdt_ro.c +24 -0
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -169,6 +169,30 @@
 	return offset;	
 }
 
+const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
+{
+	const struct fdt_node_header *nh;
+	int err;
+
+	if ((err = _fdt_check_header(fdt)) != 0)
+		goto fail;
+
+	err = -FDT_ERR_BADOFFSET;
+	nh = fdt_offset_ptr(fdt, nodeoffset, sizeof(*nh));
+	if (!nh || (fdt32_to_cpu(nh->tag) != FDT_BEGIN_NODE))
+		goto fail;
+
+	if (len)
+		*len = strlen(nh->name);
+
+	return nh->name;
+
+ fail:
+	if (len)
+		*len = err;
+	return NULL;
+}
+
 const struct fdt_property *fdt_get_property(const void *fdt,
 					    int nodeoffset,
 					    const char *name, int *lenp)
libfdt/libfdt.h +2 -0
Index: dtc/libfdt/libfdt.h
===================================================================
@@ -115,6 +115,8 @@
 
 int fdt_path_offset(const void *fdt, const char *path);
 
+const char *fdt_get_name(const void *fdt, int nodeoffset, int *baselen);
+
 const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
 					    const char *name, int *lenp);
 
tests/Makefile.tests +2 -1
Index: dtc/tests/Makefile.tests
===================================================================
@@ -1,4 +1,5 @@
-LIB_TESTS_L = root_node find_property subnode_offset path_offset getprop \
+LIB_TESTS_L = root_node find_property subnode_offset path_offset \
+	get_name getprop \
 	notfound \
 	setprop_inplace nop_property nop_node \
 	sw_tree1 \
tests/run_tests.sh +1 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -35,6 +35,7 @@
     run_test find_property $TREE
     run_test subnode_offset $TREE
     run_test path_offset $TREE
+    run_test get_name $TREE
     run_test getprop $TREE
     run_test notfound $TREE
 
tests/get_name.c new file +83 -0

83 lines not on this page · raw message

#2 lore raw

PATCH 2/3 libfdt: Add fdt_get_path() function

In reply to David Gibson

This patch adds an fdt_get_path() function to libfdt, which returns the full path of a given node in a caller supplied buffer.

---
 libfdt/fdt_ro.c      |   66 ++++++++++++++++++++++++++++++++++++
 libfdt/libfdt.h      |    2 +
 tests/Makefile.tests |    2 -
 tests/get_path.c     |   91 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/run_tests.sh   |    1 
 5 files changed, 161 insertions(+), 1 deletion(-)
libfdt/fdt_ro.c +66 -0
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -283,3 +283,69 @@
 
 	return prop->data;
 }
+
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
+{
+	uint32_t tag;
+	int p = 0, overflow = 0;
+	int offset, nextoffset, namelen;
+	const char *name;
+
+	CHECK_HEADER(fdt);
+
+	tag = _fdt_next_tag(fdt, 0, &nextoffset);
+	if (tag != FDT_BEGIN_NODE)
+		return -FDT_ERR_BADSTRUCTURE;
+
+	if (buflen < 2)
+		return -FDT_ERR_NOSPACE;
+	buf[0] = '/';
+	p = 1;
+
+	while (offset < nodeoffset) {
+		offset = nextoffset;
+		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		switch (tag) {
+		case FDT_END:
+			return -FDT_ERR_BADOFFSET;
+
+		case FDT_BEGIN_NODE:
+			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 {
+				p--;
+			} while  (buf[p-1] != '/');
+			break;
+
+		case FDT_PROP:
+		case FDT_NOP:
+			break;
+
+		default:
+			return -FDT_ERR_BADSTRUCTURE;
+		}
+	}
+
+	if (overflow)
+		return -FDT_ERR_NOSPACE;
+
+	if (p > 1) /* special case so that root path is "/", not "" */
+		p--;
+	buf[p] = '\0';
+	return p;
+}
tests/get_path.c new file +91 -0

91 lines not on this page · raw message

tests/Makefile.tests +1 -1
Index: dtc/tests/Makefile.tests
===================================================================
@@ -1,5 +1,5 @@
 LIB_TESTS_L = root_node find_property subnode_offset path_offset \
-	get_name getprop \
+	get_name getprop get_path \
 	notfound \
 	setprop_inplace nop_property nop_node \
 	sw_tree1 \
tests/run_tests.sh +1 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -37,6 +37,7 @@
     run_test path_offset $TREE
     run_test get_name $TREE
     run_test getprop $TREE
+    run_test get_path $TREE
     run_test notfound $TREE
 
     # Write-in-place tests
libfdt/libfdt.h +2 -0
Index: dtc/libfdt/libfdt.h
===================================================================
@@ -136,6 +136,8 @@
 	return (void *)fdt_getprop(fdt, nodeoffset, name, lenp);
 }
 
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
+
 /* Write-in-place functions */
 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
 			const void *val, int len);
#3 lore raw

PATCH 3/3 libfdt: Add fdt_parent_offset() and supporting functions

In reply to David Gibson

This patch adds an fdt_parent_offset() function which returns an offset to the parent node of a given node. It also adds two helper functions which are used to implement fdt_parent_offset() but are also
exported: fdt_supernode_atdepth_offset() which returns the ancestor of a given node at a specified depth from the root of the tree, and fdt_node_depth() which returns the depth of a given node.

---
 libfdt/fdt_ro.c                  |   68 ++++++++++++++++++
 libfdt/libfdt.h                  |   10 ++
 tests/Makefile.tests             |    2 
 tests/parent_offset.c            |   90 ++++++++++++++++++++++++
 tests/run_tests.sh               |    2 
 tests/supernode_atdepth_offset.c |  144 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 314 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +68 -0
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -349,3 +349,71 @@ int fdt_get_path(const void *fdt, int no
 	buf[p] = '\0';
 	return p;
 }
+
+int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
+				 int supernodedepth, int *nodedepth)
+{
+	int level = -1;
+	uint32_t tag;
+	int offset, nextoffset = 0;
+	int supernodeoffset = -FDT_ERR_INTERNAL;
+
+	CHECK_HEADER(fdt);
+
+	if (supernodedepth < 0)
+		return -FDT_ERR_NOTFOUND;
+
+	do {
+		offset = nextoffset;
+		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+		switch (tag) {
+		case FDT_END:
+			return -FDT_ERR_BADOFFSET;
+
+		case FDT_BEGIN_NODE:
+			level++;
+			if (level == supernodedepth)
+				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 (nodedepth)
+		*nodedepth = level;
+
+	if (supernodedepth > level)
+		return -FDT_ERR_NOTFOUND;
+	return supernodeoffset;
+}
+
+int fdt_node_depth(const void *fdt, int nodeoffset)
+{
+	int nodedepth;
+	int err;
+
+	err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
+	if (err)
+		return (err < 0) ? err : -FDT_ERR_INTERNAL;
+	return nodedepth;
+}
+
+int fdt_parent_offset(const void *fdt, int nodeoffset)
+{
+	int nodedepth = fdt_node_depth(fdt, nodeoffset);
+
+	if (nodedepth < 0)
+		return nodedepth;
+	return fdt_supernode_atdepth_offset(fdt, nodeoffset,
+					    nodedepth - 1, NULL);
+}
libfdt/libfdt.h +9 -1
Index: dtc/libfdt/libfdt.h
===================================================================
@@ -74,7 +74,10 @@
 #define FDT_ERR_BADSTRUCTURE	10
 #define FDT_ERR_BADLAYOUT	11
 
-#define FDT_ERR_MAX		11
+/* "Can't happen" error indicating a bug in libfdt */
+#define FDT_ERR_INTERNAL	12
+
+#define FDT_ERR_MAX		12
 
 #define fdt_get_header(fdt, field) \
 	(fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
@@ -138,6 +141,11 @@ static inline void *fdt_getprop_w(void *
 
 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
 
+int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
+				 int supernodedepth, int *nodedepth);
+int fdt_node_depth(const void *fdt, int nodeoffset);
+int fdt_parent_offset(const void *fdt, int nodeoffset);
+
 /* Write-in-place functions */
 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
 			const void *val, int len);
tests/Makefile.tests +1 -1
Index: dtc/tests/Makefile.tests
===================================================================
@@ -1,5 +1,5 @@
 LIB_TESTS_L = root_node find_property subnode_offset path_offset \
-	get_name getprop get_path \
+	get_name getprop get_path supernode_atdepth_offset parent_offset \
 	notfound \
 	setprop_inplace nop_property nop_node \
 	sw_tree1 \
tests/run_tests.sh +2 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -38,6 +38,8 @@ tree1_tests () {
     run_test get_name $TREE
     run_test getprop $TREE
     run_test get_path $TREE
+    run_test supernode_atdepth_offset $TREE
+    run_test parent_offset $TREE
     run_test notfound $TREE
 
     # Write-in-place tests
tests/supernode_atdepth_offset.c new file +144 -0

144 lines not on this page · raw message

tests/parent_offset.c new file +90 -0

90 lines not on this page · raw message

#4 lore raw
In reply to David Gibson

So, like, the other day David Gibson mumbled:

Quoted 3 lines

This series of patches adds several new functions to libfdt. These are all read-only functions related to determining a given nodes node and ancestry.

All three pplied.

Thanks,
jdl