libfdt: Add functions for handling the "compatible" property
This patch adds functions for dealing with the compatible property. fdt_node_check_compatible() can be used to determine whether a node is compatible with a given string and fdt_node_offset_by_compatible() locates nodes with a given compatible string.
Testcases for these functions are also included.
Signed-off-by David Gibson <[email protected]>
libfdt/fdt_ro.c +83 -0
@@ -477,3 +477,86 @@ int fdt_node_offset_by_prop_value(constreturn -FDT_ERR_NOTFOUND ; } + +int _stringlist_contains (const void *strlist,int listlen,const char *str) +{ +int len =strlen (str); +const void *p; + +while (listlen >= len) { +if (memcmp (str, strlist, len+1 ) ==0 ) +return 1 ; + p =memchr (strlist,'\0' , listlen); +if (!p) +return 0 ;/* malformed strlist.. */ + listlen -= (p-strlist) +1 ; + strlist = p +1 ; + } +return 0 ; +} + +int fdt_node_check_compatible (const void *fdt,int nodeoffset, +const char *compatible) +{ +const void *prop; +int len; + + prop =fdt_getprop (fdt, nodeoffset,"compatible" , &len); +if (!prop) +return len; +if (_stringlist_contains (prop, len, compatible)) +return 0 ; +else +return 1 ; +} + +int fdt_node_offset_by_compatible (const void *fdt,int startoffset, +const char *compatible) +{ +uint32_t tag; +int offset, nextoffset; +int err; + +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_node_check_compatible(), 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 : + err =fdt_node_check_compatible (fdt, offset, + compatible); +if ((err <0 ) + && (err != -FDT_ERR_NOTFOUND )) +return err; +else if (err ==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 ; +}
tests/trees.S +5 -0
@@ -83,13 +83,16 @@ test_tree1_rsvmap: test_tree1_struct: BEGIN_NODE("") + PROP_STR(test_tree1, compatible, "test_tree1") PROP_INT(test_tree1, prop_int, TEST_VALUE_1) PROP_STR(test_tree1, prop_str, TEST_STRING_1) BEGIN_NODE("subnode@1") + PROP_STR(test_tree1, compatible, "subnode1") PROP_INT(test_tree1, prop_int, TEST_VALUE_1) BEGIN_NODE("subsubnode") + PROP_STR(test_tree1, compatible, "subsubnode1\0subsubnode") PROP_INT(test_tree1, prop_int, TEST_VALUE_1) END_NODE END_NODE
@@ -98,6 +101,7 @@ test_tree1_struct: PROP_INT(test_tree1, prop_int, TEST_VALUE_2) BEGIN_NODE("subsubnode@0") + PROP_STR(test_tree1, compatible, "subsubnode2\0subsubnode") PROP_INT(test_tree1, prop_int, TEST_VALUE_2) END_NODE END_NODE
@@ -106,6 +110,7 @@ test_tree1_struct: FDTLONG(FDT_END) test_tree1_strings: + STRING(test_tree1, compatible, "compatible") STRING(test_tree1, prop_int, "prop-int") STRING(test_tree1, prop_str, "prop-str") test_tree1_end:
tests/Makefile.tests +1 -0
@@ -2,6 +2,7 @@ LIB_TESTS_L = get_mem_rsv \ root_node find_property subnode_offset path_offset \ get_name getprop get_path supernode_atdepth_offset parent_offset \ node_offset_by_prop_value \ + node_check_compatible node_offset_by_compatible \ notfound \ setprop_inplace nop_property nop_node \ sw_tree1 \
tests/run_tests.sh +2 -0
@@ -42,6 +42,8 @@ tree1_tests () { run_test supernode_atdepth_offset $TREE run_test parent_offset $TREE run_test node_offset_by_prop_value $TREE + run_test node_check_compatible $TREE + run_test node_offset_by_compatible $TREE run_test notfound $TREE # Write-in-place tests
libfdt/libfdt.h +5 -0
@@ -153,6 +153,11 @@ int fdt_node_offset_by_prop_value(constconst char *propname, const void *propval, int proplen); +int fdt_node_check_compatible (const void *fdt,int nodeoffset, +const char *compatible); +int fdt_node_offset_by_compatible (const void *fdt,int startoffset, +const char *compatible); +/* Write-in-place functions */ int fdt_setprop_inplace (void *fdt,int nodeoffset,const char *name,const void *val,int len);
tests/rw_tree1.c +6 -1
@@ -72,18 +72,23 @@ int main(int argc, char *argv[])CHECK (fdt_add_mem_rsv (fdt,TEST_ADDR_1 ,TEST_SIZE_1 ));CHECK (fdt_add_mem_rsv (fdt,TEST_ADDR_2 ,TEST_SIZE_2 )); +CHECK (fdt_setprop_string (fdt,0 ,"compatible" ,"test_tree1" ));CHECK (fdt_setprop_typed (fdt,0 ,"prop-int" ,TEST_VALUE_1 ));CHECK (fdt_setprop_string (fdt,0 ,"prop-str" ,TEST_STRING_1 ));OFF_CHECK (offset,fdt_add_subnode (fdt,0 ,"subnode@1" )); +CHECK (fdt_setprop_string (fdt, offset,"compatible" ,"subnode1" ));CHECK (fdt_setprop_typed (fdt, offset,"prop-int" ,TEST_VALUE_1 ));OFF_CHECK (offset,fdt_add_subnode (fdt, offset,"subsubnode" )); +CHECK (fdt_setprop (fdt, offset,"compatible" , +"subsubnode1\0subsubnode" ,23 ));CHECK (fdt_setprop_typed (fdt, offset,"prop-int" ,TEST_VALUE_1 ));OFF_CHECK (offset,fdt_add_subnode (fdt,0 ,"subnode@2" ));CHECK (fdt_setprop_typed (fdt, offset,"prop-int" ,TEST_VALUE_2 ));OFF_CHECK (offset,fdt_add_subnode (fdt, offset,"subsubnode@0" )); - +CHECK (fdt_setprop (fdt, offset,"compatible" , +"subsubnode2\0subsubnode" ,23 ));CHECK (fdt_setprop_typed (fdt, offset,"prop-int" ,TEST_VALUE_2 ));CHECK (fdt_pack (fdt));
tests/sw_tree1.c +7 -0
@@ -54,12 +54,17 @@ int main(int argc, char *argv[])CHECK (fdt_finish_reservemap (fdt));CHECK (fdt_begin_node (fdt,"" )); +CHECK (fdt_property_string (fdt,"compatible" ,"test_tree1" ));CHECK (fdt_property_typed (fdt,"prop-int" ,TEST_VALUE_1 ));CHECK (fdt_property_string (fdt,"prop-str" ,TEST_STRING_1 ));CHECK (fdt_begin_node (fdt,"subnode@1" )); +CHECK (fdt_property_string (fdt,"compatible" ,"subnode1" ));CHECK (fdt_property_typed (fdt,"prop-int" ,TEST_VALUE_1 ));CHECK (fdt_begin_node (fdt,"subsubnode" )); +CHECK (fdt_property (fdt,"compatible" ,"subsubnode1\0subsubnode" , +23 )); +CHECK (fdt_property_string (fdt,"compatible" ,"subsubnode1\0" ));CHECK (fdt_property_typed (fdt,"prop-int" ,TEST_VALUE_1 ));CHECK (fdt_end_node (fdt));CHECK (fdt_end_node (fdt));
@@ -67,6 +72,8 @@ int main(int argc, char *argv[])CHECK (fdt_begin_node (fdt,"subnode@2" ));CHECK (fdt_property_typed (fdt,"prop-int" ,TEST_VALUE_2 ));CHECK (fdt_begin_node (fdt,"subsubnode@0" )); +CHECK (fdt_property (fdt,"compatible" ,"subsubnode2\0subsubnode" , +23 ));CHECK (fdt_property_typed (fdt,"prop-int" ,TEST_VALUE_2 ));CHECK (fdt_end_node (fdt));CHECK (fdt_end_node (fdt));
tests/test_tree1.dts +4 -0
@@ -2,13 +2,16 @@ /memreserve/ abcd1234 00001234; / { + compatible = "test_tree1"; prop-int = <deadbeef>; prop-str = "hello world"; subnode@1 { + compatible = "subnode1"; prop-int = <deadbeef>; subsubnode { + compatible = "subsubnode1", "subsubnode"; prop-int = <deadbeef>; }; };
@@ -17,6 +20,7 @@ prop-int = <abcd1234>; subsubnode@0 { + compatible = "subsubnode2", "subsubnode"; prop-int = <abcd1234>; }; };
tests/node_offset_by_compatible.c new file +86 -0
86 lines not on this page · raw message
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 8 lines
This patch adds functions for dealing with the compatible property.
fdt_node_check_compatible()can be used to determine whether a node is compatible with a given string andfdt_node_offset_by_compatible()locates nodes with a given compatible string.Testcases for these functions are also included.
Signed-off-by David Gibson <[email protected]>
Applied.
jdl