Skip to contentSkip to conversation

libfdt: Add fdt_node_offset_by_prop_value()

PATCH Applied David Gibson 2 participants 2 messages Sep 17, 2007

lore
Share & tools
lore
#0 lore raw

This patch adds a function to libfdt to locate nodes containing a property with a specific value.

libfdt/fdt_ro.c +51 -0
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -407,3 +407,54 @@ int fdt_parent_offset(const void *fdt, i
 	return fdt_supernode_atdepth_offset(fdt, nodeoffset,
 					    nodedepth - 1, NULL);
 }
+
+int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
+				  const char *propname,
+				  const void *propval, int proplen)
+{
+	uint32_t tag;
+	int offset, nextoffset;
+	const void *val;
+	int len;
+
+	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
+	 * property of a node in fdt_getprop(), then if 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 implement
+	 * approach; performance can come later. */
+	do {
+		offset = nextoffset;
+		tag = _fdt_next_tag(fdt, offset, &nextoffset);
+
+		switch (tag) {
+		case FDT_BEGIN_NODE:
+			val = fdt_getprop(fdt, offset, propname, &len);
+			if (val
+			    && (len == proplen)
+			    && (memcmp(val, propval, len) == 0))
+				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;
+}
libfdt/libfdt.h +4 -0
Index: dtc/libfdt/libfdt.h
===================================================================
@@ -146,6 +146,10 @@ int fdt_supernode_atdepth_offset(const v
 int fdt_node_depth(const void *fdt, int nodeoffset);
 int fdt_parent_offset(const void *fdt, int nodeoffset);
 
+int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
+				  const char *propname,
+				  const void *propval, int proplen);
+
 /* Write-in-place functions */
 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
 			const void *val, int len);
tests/Makefile.tests +1 -0
Index: dtc/tests/Makefile.tests
===================================================================
@@ -1,5 +1,6 @@
 LIB_TESTS_L = root_node find_property subnode_offset path_offset \
 	get_name getprop get_path supernode_atdepth_offset parent_offset \
+	node_offset_by_prop_value \
 	notfound \
 	setprop_inplace nop_property nop_node \
 	sw_tree1 \
tests/node_offset_by_prop_value.c new file +110 -0

110 lines not on this page · raw message

tests/run_tests.sh +1 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -40,6 +40,7 @@ tree1_tests () {
     run_test get_path $TREE
     run_test supernode_atdepth_offset $TREE
     run_test parent_offset $TREE
+    run_test node_offset_by_prop_value $TREE
     run_test notfound $TREE
 
     # Write-in-place tests
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
Applied
#1 lore raw

So, like, the other day David Gibson mumbled:

Quoted 4 lines

This patch adds a function to libfdt to locate nodes containing a property with a specific value.

Applied.

jdl