libfdt: Add fdt_node_offset_by_prop_value()
This patch adds a function to libfdt to locate nodes containing a property with a specific value.
Signed-off-by David Gibson <[email protected]>
libfdt/fdt_ro.c +51 -0
@@ -407,3 +407,54 @@ int fdt_parent_offset(const void *fdt, ireturn 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
@@ -146,6 +146,10 @@ int fdt_supernode_atdepth_offset(const vint 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
@@ -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
@@ -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
Jon Loeliger
AppliedSo, 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.
Signed-off-by David Gibson <[email protected]>
Applied.
jdl