Add a couple of string-related functions
cover Add a couple of string-related functions
From: Thierry Reding <[email protected]>
These three patches add a couple of string functions that have proven useful in U-Boot's copy of libfdt, so they are likely to be useful for other users as well.
Patch 1 adds a function to count the number of strings in a property's value. This also adds a new DTS sample along with a small test program to validate the implemented functions.
Patch 2 adds a function to retrieve the index of a given string in any given property's value. This adds code to the test program introduced in the previous patch to exercise the new functionality.
Patch 3 adds a function to retrieve a string by index from a property's value along with a shortcut for index 0. This extends the test program introduced in patch 1 to validate the new functionality.
Thierry
Thierry Reding (3):
fdt: Add a function to count strings
fdt: Add a function to get the index of a string
fdt: Add functions to retrieve strings
libfdt/fdt_ro.c | 76 +++++++++++++++++++++++++++++++++++ libfdt/libfdt.h | 47 ++++++++++++++++++++++ tests/.gitignore | 1 + tests/Makefile.tests | 1 + tests/run_tests.sh | 3 ++ tests/strings.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/strings.dts | 10 +++++ 7 files changed, 248 insertions(+) create mode 100644 tests/strings.c create mode 100644 tests/strings.dts
Signature · 1 line
2.4.5
A message that is not in the archive (sent off-list or not yet received): 1436958839-14793-1-git-send-email-thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
PATCH 1/3 fdt: Add a function to count strings
From: Thierry Reding <[email protected]>
Given a device tree node and a property name, the fdt_count_strings() function counts the number of strings found in the property value.
--- libfdt/fdt_ro.c | 20 ++++++++++++++++ libfdt/libfdt.h | 9 ++++++++ tests/.gitignore | 1 + tests/Makefile.tests | 1 + tests/run_tests.sh | 3 +++ tests/strings.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/strings.dts | 10 ++++++++ 7 files changed, 108 insertions(+) create mode 100644 tests/strings.c create mode 100644 tests/strings.dts
libfdt/fdt_ro.c +20 -0
@@ -538,6 +538,26 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)return 0 ; } +int fdt_count_strings (const void *fdt,int nodeoffset,const char *property) +{ +int length, i, count =0 ; +const char *list; + + list =fdt_getprop (fdt, nodeoffset, property, &length); +if (!list) +return -length; + +for (i =0 ; i < length; i++) { +int len =strlen (list); + + list += len +1 ; + i += len; + count++; + } + +return count; +} +int fdt_node_check_compatible (const void *fdt,int nodeoffset,const char *compatible) {
libfdt/libfdt.h +9 -0
@@ -868,6 +868,15 @@ int fdt_node_offset_by_compatible(const void *fdt, int startoffset,*/ int fdt_stringlist_contains (const char *strlist,int listlen,const char *str); +/** +* fdt_count_strings - count the number of strings in a string list +* @fdt: pointer to the device tree blob +* @nodeoffset: offset of a tree node +* @property: name of the property containing the string list +* @return: the number of strings in the given property +*/ +int fdt_count_strings (const void *fdt,int nodeoffset,const char *property); +/**********************************************************************/ /* Read-only functions (addressing related) */ /**********************************************************************/
tests/.gitignore +1 -0
@@ -49,6 +49,7 @@ tmp.* /setprop_inplace /sized_cells /string_escapes +/strings /subnode_iterate /subnode_offset /supernode_atdepth_offset
tests/Makefile.tests +1 -0
@@ -9,6 +9,7 @@ LIB_TESTS_L = get_mem_rsv \ sized_cells \ notfound \ addr_size_cells \ + strings \ setprop_inplace nop_property nop_node \ sw_tree1 \ move_and_save mangle-layout nopulate \
tests/run_tests.sh +3 -0
@@ -198,6 +198,9 @@ libfdt_tests () { run_dtc_test -I dts -O dtb -o addresses.test.dtb addresses.dts run_test addr_size_cells addresses.test.dtb + run_dtc_test -I dts -O dtb -o strings.test.dtb strings.dts + run_test strings strings.test.dtb + # Sequential write tests run_test sw_tree1 tree1_tests sw_tree1.test.dtb
tests/strings.c new file +64 -0
64 lines not on this page · raw message
tests/strings.dts new file +10 -0
@@ -0,0 +1,10 @@ +/dts-v1/; + +/ { + compatible = "foo"; + + device { + compatible = "bar", "baz"; + big-endian; + }; +};
Signature · 1 line
2.4.5
A message that is not in the archive (sent off-list or not yet received): 1436958839-14793-2-git-send-email-thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Re: [PATCH 1/3] fdt: Add a function to count strings
Quoted 27 lines · Thierry Reding
From: Thierry Reding <[email protected]>
Given a device tree node and a property name, the
fdt_count_strings()function counts the number of strings found in the property value.Signed-off-by Thierry Reding <[email protected]>--- libfdt/fdt_ro.c | 20 ++++++++++++++++ libfdt/libfdt.h | 9 ++++++++ tests/.gitignore | 1 + tests/Makefile.tests | 1 + tests/run_tests.sh | 3 +++ tests/strings.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/strings.dts | 10 ++++++++ 7 files changed, 108 insertions(+) create mode 100644 tests/strings.c create mode 100644 tests/strings.dtslibfdt/fdt_ro.c +1
-0@@ -538,6 +538,26 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)return 0 ; } +int fdt_count_strings (const void *fdt,int nodeoffset,const char *property)
libfdt/fdt_ro.c int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) · line 551
Quoted 10 lines
libfdt/fdt_ro.c +10
-0+{ +int length, i, count =0 ; +const char *list; + + list =fdt_getprop (fdt, nodeoffset, property, &length); +if (!list) +return -length; + +for (i =0 ; i < length; i++) { +int len =strlen (list);
I like the concept of these patches, but this implementation is unsafe if the given property does not, in fact, contain a list of \0 terminated strings.
Quoted 155 lines
libfdt/fdt_ro.c +8
-0+ list += len +1 ; + i += len; + count++; + } + +return count; +} +int fdt_node_check_compatible (const void *fdt,int nodeoffset,const char *compatible) {libfdt/libfdt.h +9
-0@@ -868,6 +868,15 @@ int fdt_node_offset_by_compatible(const void *fdt, int startoffset,*/ int fdt_stringlist_contains (const char *strlist,int listlen,const char *str); +/** +* fdt_count_strings - count the number of strings in a string list +* @fdt: pointer to the device tree blob +* @nodeoffset: offset of a tree node +* @property: name of the property containing the string list +* @return: the number of strings in the given property +*/ +int fdt_count_strings (const void *fdt,int nodeoffset,const char *property); +/**********************************************************************/ /* Read-only functions (addressing related) */ /**********************************************************************/ tests/.gitignore +1
-0@@ -49,6 +49,7 @@ tmp.* /setprop_inplace /sized_cells /string_escapes +/strings /subnode_iterate /subnode_offset /supernode_atdepth_offsettests/Makefile.tests +1
-0@@ -9,6 +9,7 @@ LIB_TESTS_L = get_mem_rsv \ sized_cells \ notfound \ addr_size_cells \ + strings \ setprop_inplace nop_property nop_node \ sw_tree1 \ move_and_save mangle-layout nopulate \tests/run_tests.sh +3
-0@@ -198,6 +198,9 @@ libfdt_tests () { run_dtc_test -I dts -O dtb -o addresses.test.dtb addresses.dts run_test addr_size_cells addresses.test.dtb + run_dtc_test -I dts -O dtb -o strings.test.dtb strings.dts + run_test strings strings.test.dtb + # Sequential write tests run_test sw_tree1 tree1_tests sw_tree1.test.dtbtests/strings.c new file +64
-0@@ -0,0 +1,64 @@ +/* +* libfdt - Flat Device Tree manipulation +* Testcase for string handling +* Copyright (C) 2015 NVIDIA Corporation +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public License +* as published by the Free Software Foundation; either version 2.1 of +* the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stdint.h> + +#include <libfdt.h> + +#include "tests.h" +#include "testdata.h" + +static void check_string_count (const void *fdt,const char *path, +const char *property,int count) +{ +int offset, err; + + offset =fdt_path_offset (fdt, path); +if (offset <0 ) +FAIL ("Couldn't find path %s" , path); + + err =fdt_count_strings (fdt, offset, property); +if (err <0 ) +FAIL ("Failed to count strings: %d\n" , err); + +if (err != count) +FAIL ("String count for %s is %d instead of %d\n" , + path, err, count); +} + +int main (int argc,char *argv[]) +{ +void *fdt; + +if (argc !=2 ) +CONFIG ("Usage: %s <dtb file>\n" , argv[0 ]); + +test_init (argc, argv); + fdt =load_blob (argv[1 ]); + +check_string_count (fdt,"/" ,"compatible" ,1 ); +check_string_count (fdt,"/device" ,"compatible" ,2 ); +check_string_count (fdt,"/device" ,"big-endian" ,0 ); + +PASS (); +}tests/strings.dts new file +10
-0@@ -0,0 +1,10 @@ +/dts-v1/; + +/ { + compatible = "foo"; + + device { + compatible = "bar", "baz"; + big-endian; + }; +};
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 1/3] fdt: Add a function to count strings
Quoted 27 lines · David Gibson
Quoted 26 lines · Thierry Reding
From: Thierry Reding <[email protected]>
Given a device tree node and a property name, the
fdt_count_strings()function counts the number of strings found in the property value.Signed-off-by Thierry Reding <[email protected]>--- libfdt/fdt_ro.c | 20 ++++++++++++++++ libfdt/libfdt.h | 9 ++++++++ tests/.gitignore | 1 + tests/Makefile.tests | 1 + tests/run_tests.sh | 3 +++ tests/strings.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/strings.dts | 10 ++++++++ 7 files changed, 108 insertions(+) create mode 100644 tests/strings.c create mode 100644 tests/strings.dtslibfdt/fdt_ro.c +0
-0@@ -538,6 +538,26 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)return 0 ; }
Quoted 15 lines
libfdt/fdt_ro.c int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) · line 551
Quoted 11 lines
libfdt/fdt_ro.c +11
-0+int fdt_count_strings (const void *fdt,int nodeoffset,const char *property) +{ +int length, i, count =0 ; +const char *list; + + list =fdt_getprop (fdt, nodeoffset, property, &length); +if (!list) +return -length; + +for (i =0 ; i < length; i++) { +int len =strlen (list);I like the concept of these patches, but this implementation is unsafe if the given property does not, in fact, contain a list of \0 terminated strings.
This should be fixed in v2 of the patches. This isn't quite as simple as using strnlen() instead of strlen() because it also means we need extra handling for the case where a NUL byte isn't found within the value.
There is also a bit of an inconsistency for the fdt_find_string() and fdt_get_string() functions because they can successfully work with non- NUL-terminated values. I've gone into some detail about that in the v2 cover letter, so feedback there would be most welcome.
Thierry
Re: [PATCH 1/3] fdt: Add a function to count strings
Quoted 44 lines · Thierry Reding
Quoted 27 lines · David Gibson
Quoted 26 lines · Thierry Reding
From: Thierry Reding <[email protected]>
Given a device tree node and a property name, the
fdt_count_strings()function counts the number of strings found in the property value.Signed-off-by Thierry Reding <[email protected]>--- libfdt/fdt_ro.c | 20 ++++++++++++++++ libfdt/libfdt.h | 9 ++++++++ tests/.gitignore | 1 + tests/Makefile.tests | 1 + tests/run_tests.sh | 3 +++ tests/strings.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/strings.dts | 10 ++++++++ 7 files changed, 108 insertions(+) create mode 100644 tests/strings.c create mode 100644 tests/strings.dtslibfdt/fdt_ro.c +0
-0@@ -538,6 +538,26 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)return 0 ; }Quoted 15 lines
libfdt/fdt_ro.c int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) · line 551
Quoted 11 lines
libfdt/fdt_ro.c +11
-0+int fdt_count_strings (const void *fdt,int nodeoffset,const char *property) +{ +int length, i, count =0 ; +const char *list; + + list =fdt_getprop (fdt, nodeoffset, property, &length); +if (!list) +return -length; + +for (i =0 ; i < length; i++) { +int len =strlen (list);I like the concept of these patches, but this implementation is unsafe if the given property does not, in fact, contain a list of \0 terminated strings.
Quoted 3 lines
This should be fixed in v2 of the patches. This isn't quite as simple as using
strnlen()instead ofstrlen()because it also means we need extra handling for the case where a NUL byte isn't found within the value.
Yes, I suspect it will actually be easier to use memchr().
Quoted 6 lines
There is also a bit of an inconsistency for the
fdt_find_string()andfdt_get_string()functions because they can successfully work with non- NUL-terminated values. I've gone into some detail about that in the v2 cover letter, so feedback there would be most welcome.Thierry
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 1/3] fdt: Add a function to count strings
Quoted 45 lines · David Gibson
Quoted 44 lines · Thierry Reding
Quoted 42 lines · David Gibson
Quoted 27 lines · Thierry Reding
From: Thierry Reding <[email protected]>
Given a device tree node and a property name, the
fdt_count_strings()function counts the number of strings found in the property value.Signed-off-by Thierry Reding <[email protected]>--- libfdt/fdt_ro.c | 20 ++++++++++++++++ libfdt/libfdt.h | 9 ++++++++ tests/.gitignore | 1 + tests/Makefile.tests | 1 + tests/run_tests.sh | 3 +++ tests/strings.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/strings.dts | 10 ++++++++ 7 files changed, 108 insertions(+) create mode 100644 tests/strings.c create mode 100644 tests/strings.dtslibfdt/fdt_ro.c +1
-0@@ -538,6 +538,26 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)return 0 ; } +int fdt_count_strings (const void *fdt,int nodeoffset,const char *property)libfdt/fdt_ro.c int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) · line 551
Quoted 10 lines
libfdt/fdt_ro.c +10
-0+{ +int length, i, count =0 ; +const char *list; + + list =fdt_getprop (fdt, nodeoffset, property, &length); +if (!list) +return -length; + +for (i =0 ; i < length; i++) { +int len =strlen (list);I like the concept of these patches, but this implementation is unsafe if the given property does not, in fact, contain a list of \0 terminated strings.
Quoted 5 lines
Quoted 3 lines
This should be fixed in v2 of the patches. This isn't quite as simple as using
strnlen()instead ofstrlen()because it also means we need extra handling for the case where a NUL byte isn't found within the value.Yes, I suspect it will actually be easier to use
memchr().
I don't see how that would make it easier. The problematic bit isn't about determining whether or not the NUL byte is there. The problematic bit is determining what to do about it. For example we could enforce that these functions only work on well-defined properties, that is, properties that end with a NUL-byte. Currently we don't do that in fdt_find_string() and fdt_get_string() for performance reasons. For fdt_count_strings() the check is implicit because the last string would not be bounded by the property value if it wasn't terminated with a NUL byte.
It would be easy to add this additional check as a way to short-circuit implementations and abort early on malformed property values, but that would be additional work that most users may not care about.
Thierry
Re: [PATCH 1/3] fdt: Add a function to count strings
Quoted 52 lines · Thierry Reding
Quoted 45 lines · David Gibson
Quoted 44 lines · Thierry Reding
Quoted 42 lines · David Gibson
Quoted 27 lines · Thierry Reding
From: Thierry Reding <[email protected]>
Given a device tree node and a property name, the
fdt_count_strings()function counts the number of strings found in the property value.Signed-off-by Thierry Reding <[email protected]>--- libfdt/fdt_ro.c | 20 ++++++++++++++++ libfdt/libfdt.h | 9 ++++++++ tests/.gitignore | 1 + tests/Makefile.tests | 1 + tests/run_tests.sh | 3 +++ tests/strings.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/strings.dts | 10 ++++++++ 7 files changed, 108 insertions(+) create mode 100644 tests/strings.c create mode 100644 tests/strings.dtslibfdt/fdt_ro.c +1
-0@@ -538,6 +538,26 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)return 0 ; } +int fdt_count_strings (const void *fdt,int nodeoffset,const char *property)libfdt/fdt_ro.c int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) · line 551
Quoted 10 lines
libfdt/fdt_ro.c +10
-0+{ +int length, i, count =0 ; +const char *list; + + list =fdt_getprop (fdt, nodeoffset, property, &length); +if (!list) +return -length; + +for (i =0 ; i < length; i++) { +int len =strlen (list);I like the concept of these patches, but this implementation is unsafe if the given property does not, in fact, contain a list of \0 terminated strings.
Quoted 5 lines
Quoted 3 lines
This should be fixed in v2 of the patches. This isn't quite as simple as using
strnlen()instead ofstrlen()because it also means we need extra handling for the case where a NUL byte isn't found within the value.Yes, I suspect it will actually be easier to use
memchr().
Quoted 9 lines
I don't see how that would make it easier. The problematic bit isn't about determining whether or not the NUL byte is there. The problematic bit is determining what to do about it. For example we could enforce that these functions only work on well-defined properties, that is, properties that end with a NUL-byte. Currently we don't do that in
fdt_find_string()andfdt_get_string()for performance reasons. Forfdt_count_strings()the check is implicit because the last string would not be bounded by the property value if it wasn't terminated with a NUL byte.
Yeah, true; memchr() was just the first implementation that popped into my head.
So, actually checking for a final \0 in advance isn't a real performance cost - getprop already gives you the total property length, so you can check if you have a valid stringlist in O(1).
Quoted 3 lines
It would be easy to add this additional check as a way to short-circuit implementations and abort early on malformed property values, but that would be additional work that most users may not care about.
The implementation you have now seems fine to me - only thing I'm still thinking about is whether I like the function names or not.
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
PATCH 2/3 fdt: Add a function to get the index of a string
In reply to a message not in the archiveFrom: Thierry Reding <[email protected]>
Given a device tree node and a property name, the new fdt_find_string() function will look up a given string in the string list contained in the property's value and return its index.
--- libfdt/fdt_ro.c | 26 ++++++++++++++++++++++++++ libfdt/libfdt.h | 11 +++++++++++ tests/strings.c | 22 ++++++++++++++++++++++ 3 files changed, 59 insertions(+)
libfdt/fdt_ro.c +26 -0
@@ -558,6 +558,32 @@ int fdt_count_strings(const void *fdt, int nodeoffset, const char *property)return count; } +int fdt_find_string (const void *fdt,int nodeoffset,const char *property, +const char *string) +{ +const char *list, *end; +int len, index =0 ; + + list =fdt_getprop (fdt, nodeoffset, property, &len); +if (!list) +return len; + + end = list + len; + len =strlen (string); + +while (list < end) { +int l =strlen (list); + +if (l == len &&memcmp (list, string, len) ==0 ) +return index; + + list += l +1 ; + index++; + } + +return -FDT_ERR_NOTFOUND ; +} +int fdt_node_check_compatible (const void *fdt,int nodeoffset,const char *compatible) {
libfdt/libfdt.h +11 -0
@@ -877,6 +877,17 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);*/ int fdt_count_strings (const void *fdt,int nodeoffset,const char *property); +/** +* fdt_find_string - find a string in a string list and return its index +* @fdt: pointer to the device tree blob +* @nodeoffset: offset of a tree node +* @property: name of the property containing the string list +* @string: string to look up in the string list +* @return: the index of the string or negative on error +*/ +int fdt_find_string (const void *fdt,int nodeoffset,const char *property, +const char *string); +/**********************************************************************/ /* Read-only functions (addressing related) */ /**********************************************************************/
tests/strings.c +22 -0
@@ -46,6 +46,23 @@ static void check_string_count(const void *fdt, const char *path, path, err, count); } +static void check_string_index (const void *fdt,const char *path, +const char *property,const char *string, +int index) +{ +int offset, err; + + offset =fdt_path_offset (fdt, path); +if (offset <0 ) +FAIL ("Couldn't find path %s" , path); + + err =fdt_find_string (fdt, offset, property, string); + +if (err != index) +FAIL ("Index of string %s is %d instead of %d\n" , + path, err, index); +} +int main (int argc,char *argv[]) {void *fdt;
@@ -60,5 +77,10 @@ int main(int argc, char *argv[])check_string_count (fdt,"/device" ,"compatible" ,2 );check_string_count (fdt,"/device" ,"big-endian" ,0 ); +check_string_index (fdt,"/" ,"compatible" ,"foo" ,0 ); +check_string_index (fdt,"/device" ,"compatible" ,"bar" ,0 ); +check_string_index (fdt,"/device" ,"compatible" ,"baz" ,1 ); +check_string_index (fdt,"/device" ,"big-endian" ,"bL" ,-1 ); +PASS (); }
Signature · 1 line
2.4.5
PATCH 3/3 fdt: Add functions to retrieve strings
In reply to a message not in the archiveFrom: Thierry Reding <[email protected]>
Given a device tree node, a property name and an index, the new function fdt_get_string_index() will return in an output argument a pointer to the index'th string in the property's value.
The fdt_get_string() is a shortcut for the above with the index being 0.
--- libfdt/fdt_ro.c | 30 ++++++++++++++++++++++++++++++ libfdt/libfdt.h | 27 +++++++++++++++++++++++++++ tests/strings.c | 24 ++++++++++++++++++++++++ 3 files changed, 81 insertions(+)
libfdt/fdt_ro.c +30 -0
@@ -584,6 +584,36 @@ int fdt_find_string(const void *fdt, int nodeoffset, const char *property,return -FDT_ERR_NOTFOUND ; } +int fdt_get_string_index (const void *fdt,int nodeoffset,const char *property, +int index,const char **output) +{ +const char *list; +int length, i; + + list =fdt_getprop (fdt, nodeoffset, property, &length); + +for (i =0 ; i < length; i++) { +int len =strlen (list); + +if (index ==0 ) { + *output = list; +return 0 ; + } + + list += len +1 ; + i += len; + index--; + } + +return FDT_ERR_NOTFOUND ; +} + +int fdt_get_string (const void *fdt,int nodeoffset,const char *property, +const char **output) +{ +return fdt_get_string_index (fdt, nodeoffset, property,0 , output); +} +int fdt_node_check_compatible (const void *fdt,int nodeoffset,const char *compatible) {
libfdt/libfdt.h +27 -0
@@ -888,6 +888,33 @@ int fdt_count_strings(const void *fdt, int nodeoffset, const char *property);int fdt_find_string (const void *fdt,int nodeoffset,const char *property,const char *string); +/** +* fdt_get_string_index() - obtain the string at a given index in a string list +* @fdt: pointer to the device tree blob +* @nodeoffset: offset of a tree node +* @property: name of the property containing the string list +* @index: index of the string to return +* @output: return location for the string +* @return: 0 if the string was found or a negative error code otherwise +*/ +int fdt_get_string_index (const void *fdt,int nodeoffset,const char *property, +int index,const char **output); + +/** +* fdt_get_string() - obtain the first string in a string list +* @fdt: pointer to the device tree blob +* @nodeoffset: offset of a tree node +* @property: name of the property containing the string list +* @output: return location for the string +* @return: 0 if the string was found or a negative error code otherwise +* +* This is a shortcut for: +* +* fdt_get_string_index(fdt, node, property, 0, output). +*/ +int fdt_get_string (const void *fdt,int nodeoffset,const char *property, +const char **output); +/**********************************************************************/ /* Read-only functions (addressing related) */ /**********************************************************************/
tests/strings.c +24 -0
@@ -63,6 +63,26 @@ static void check_string_index(const void *fdt, const char *path, path, err, index); } +static void check_string (const void *fdt,const char *path, +const char *property,int index, +const char *string) +{ +const char *result; +int offset, err; + + offset =fdt_path_offset (fdt, path); +if (offset <0 ) +FAIL ("Couldn't find path %s" , path); + + err =fdt_get_string_index (fdt, offset, property, index, &result); +if (err <0 ) +FAIL ("Couldn't extract string: %d\n" , err); + +if (strcmp (string, result) !=0 ) +FAIL ("String %d is %s instead of %s\n" , + index, result, string); +} +int main (int argc,char *argv[]) {void *fdt;
@@ -82,5 +102,9 @@ int main(int argc, char *argv[])check_string_index (fdt,"/device" ,"compatible" ,"baz" ,1 );check_string_index (fdt,"/device" ,"big-endian" ,"bL" ,-1 ); +check_string (fdt,"/" ,"compatible" ,0 ,"foo" ); +check_string (fdt,"/device" ,"compatible" ,0 ,"bar" ); +check_string (fdt,"/device" ,"compatible" ,1 ,"baz" ); +PASS (); }
Signature · 1 line
2.4.5
Quoted 14 lines · Thierry Reding
From: Thierry Reding <[email protected]>
These three patches add a couple of string functions that have proven useful in U-Boot's copy of libfdt, so they are likely to be useful for other users as well.
Patch 1 adds a function to count the number of strings in a property's value. This also adds a new DTS sample along with a small test program to validate the implemented functions.
Patch 2 adds a function to retrieve the index of a given string in any given property's value. This adds code to the test program introduced in the previous patch to exercise the new functionality.
Quoted 5 lines
Patch 3 adds a function to retrieve a string by index from a property's value along with a shortcut for index 0. This extends the test program introduced in patch 1 to validate the new functionality.
Thierry
Hi Thierry,
While I am generally fine with this patch set, I have a large-scope question. Is there a larger plan to consolidate or unify the U-Boot and DTC libfdts?
Thanks,
jdl
A message that is not in the archive (sent off-list or not yet received): E1ZFMkd-0002ir-O5-h9b8CULRd+FWk0Htik3J/[email protected]
Hi Jon,
Quoted 20 lines · Jon Loeliger
Quoted 18 lines · Thierry Reding
From: Thierry Reding <[email protected]>
These three patches add a couple of string functions that have proven useful in U-Boot's copy of libfdt, so they are likely to be useful for other users as well.
Patch 1 adds a function to count the number of strings in a property's value. This also adds a new DTS sample along with a small test program to validate the implemented functions.
Patch 2 adds a function to retrieve the index of a given string in any given property's value. This adds code to the test program introduced in the previous patch to exercise the new functionality.
Patch 3 adds a function to retrieve a string by index from a property's value along with a shortcut for index 0. This extends the test program introduced in patch 1 to validate the new functionality.
Quoted 8 lines
Quoted 1 line
Thierry
Hi Thierry,
While I am generally fine with this patch set, I have a large-scope question. Is there a larger plan to consolidate or unify the U-Boot and DTC libfdts?
I maintain the fdt tree for U-Boot at present. About once a quarter I check what has changed and do a bit of a sync. But there are things that libfdt upstream has not accepted - e.g. the grep functionality used by verified boot hashing stuff. I wish we could figure that out. Perhaps a cut-down fdtgrep tool would meet with favour. We're using it even more now since SPL (the minimal U-Boot loader) wants to run with a subset of the full board FDT for SRAM space reasons.
I do ask people to send things upstream, and if rejected we then have to work out what to do...there are recent U-Boot mailing list threads on this.
Regards,
Simon
A message that is not in the archive (sent off-list or not yet received): CAPnjgZ12VVSDkALDDg31Sgwo1PpFPfE10GFtrseHWpRX1re-HQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 32 lines · Simon Glass
Hi Jon,
Quoted 20 lines · Jon Loeliger
Quoted 18 lines · Thierry Reding
From: Thierry Reding <[email protected]>
These three patches add a couple of string functions that have proven useful in U-Boot's copy of libfdt, so they are likely to be useful for other users as well.
Patch 1 adds a function to count the number of strings in a property's value. This also adds a new DTS sample along with a small test program to validate the implemented functions.
Patch 2 adds a function to retrieve the index of a given string in any given property's value. This adds code to the test program introduced in the previous patch to exercise the new functionality.
Patch 3 adds a function to retrieve a string by index from a property's value along with a shortcut for index 0. This extends the test program introduced in patch 1 to validate the new functionality.
Quoted 8 lines
Quoted 1 line
Thierry
Hi Thierry,
While I am generally fine with this patch set, I have a large-scope question. Is there a larger plan to consolidate or unify the U-Boot and DTC libfdts?
Quoted 7 lines
I maintain the fdt tree for U-Boot at present. About once a quarter I check what has changed and do a bit of a sync. But there are things that libfdt upstream has not accepted - e.g. the grep functionality used by verified boot hashing stuff. I wish we could figure that out. Perhaps a cut-down fdtgrep tool would meet with favour. We're using it even more now since SPL (the minimal U-Boot loader) wants to run with a subset of the full board FDT for SRAM space reasons.
So, short-term: there's no reason your fdtgrep stuff needs to be considered as part of your version of libfdt - it could just as easily be an add-on sitting alongside libfdt - then you could share the core libfdt code at least.
Longer term, my main sticking point on the fdtgrep stuff was entry points whose semantics don't make me go cross-eyed (includes these nodes, but not those nodes, and might include children if this flag is set, but not that one and the operator's shoe size matches some other property...). I'm not sure if that's a question of redesigning the interface, or just of describing it better.
Quoted 6 lines
I do ask people to send things upstream, and if rejected we then have to work out what to do...there are recent U-Boot mailing list threads on this.
Regards,
Simon
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi David,
Quoted 41 lines · David Gibson
Quoted 32 lines · Simon Glass
Hi Jon,
Quoted 20 lines · Jon Loeliger
Quoted 18 lines · Thierry Reding
From: Thierry Reding <[email protected]>
These three patches add a couple of string functions that have proven useful in U-Boot's copy of libfdt, so they are likely to be useful for other users as well.
Patch 1 adds a function to count the number of strings in a property's value. This also adds a new DTS sample along with a small test program to validate the implemented functions.
Patch 2 adds a function to retrieve the index of a given string in any given property's value. This adds code to the test program introduced in the previous patch to exercise the new functionality.
Patch 3 adds a function to retrieve a string by index from a property's value along with a shortcut for index 0. This extends the test program introduced in patch 1 to validate the new functionality.
Quoted 8 lines
Quoted 1 line
Thierry
Hi Thierry,
While I am generally fine with this patch set, I have a large-scope question. Is there a larger plan to consolidate or unify the U-Boot and DTC libfdts?
Quoted 7 lines
I maintain the fdt tree for U-Boot at present. About once a quarter I check what has changed and do a bit of a sync. But there are things that libfdt upstream has not accepted - e.g. the grep functionality used by verified boot hashing stuff. I wish we could figure that out. Perhaps a cut-down fdtgrep tool would meet with favour. We're using it even more now since SPL (the minimal U-Boot loader) wants to run with a subset of the full board FDT for SRAM space reasons.
Quoted 4 lines
So, short-term: there's no reason your fdtgrep stuff needs to be considered as part of your version of libfdt - it could just as easily be an add-on sitting alongside libfdt - then you could share the core libfdt code at least.
That's how it is today, yes.
Quoted 7 lines
Longer term, my main sticking point on the fdtgrep stuff was entry points whose semantics don't make me go cross-eyed (includes these nodes, but not those nodes, and might include children if this flag is set, but not that one and the operator's shoe size matches some other property...). I'm not sure if that's a question of redesigning the interface, or just of describing it better.
Neither am I, but perhaps if I cut down the fdtgrep options so that it only does a few basic things that would help? The full feature set would still be in the implementation, but it would reduce confusion on that side.
Quoted 5 lines
Quoted 5 lines
I do ask people to send things upstream, and if rejected we then have to work out what to do...there are recent U-Boot mailing list threads on this.
Quoted 8 lines
Quoted 2 lines
Regards,
Simon--
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_!
Regards,
Simon
A message that is not in the archive (sent off-list or not yet received): CAPnjgZ0FQvjkC=LUWeCgeDw+x+DePyaNQ=CiC+ht86U2KY_BMA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 59 lines · Simon Glass
Hi David,
Quoted 41 lines · David Gibson
Quoted 32 lines · Simon Glass
Hi Jon,
Quoted 20 lines · Jon Loeliger
Quoted 18 lines · Thierry Reding
From: Thierry Reding <[email protected]>
These three patches add a couple of string functions that have proven useful in U-Boot's copy of libfdt, so they are likely to be useful for other users as well.
Patch 1 adds a function to count the number of strings in a property's value. This also adds a new DTS sample along with a small test program to validate the implemented functions.
Patch 2 adds a function to retrieve the index of a given string in any given property's value. This adds code to the test program introduced in the previous patch to exercise the new functionality.
Patch 3 adds a function to retrieve a string by index from a property's value along with a shortcut for index 0. This extends the test program introduced in patch 1 to validate the new functionality.
Quoted 8 lines
Quoted 1 line
Thierry
Hi Thierry,
While I am generally fine with this patch set, I have a large-scope question. Is there a larger plan to consolidate or unify the U-Boot and DTC libfdts?
Quoted 7 lines
I maintain the fdt tree for U-Boot at present. About once a quarter I check what has changed and do a bit of a sync. But there are things that libfdt upstream has not accepted - e.g. the grep functionality used by verified boot hashing stuff. I wish we could figure that out. Perhaps a cut-down fdtgrep tool would meet with favour. We're using it even more now since SPL (the minimal U-Boot loader) wants to run with a subset of the full board FDT for SRAM space reasons.
Quoted 4 lines
So, short-term: there's no reason your fdtgrep stuff needs to be considered as part of your version of libfdt - it could just as easily be an add-on sitting alongside libfdt - then you could share the core libfdt code at least.
That's how it is today, yes.
Quoted 7 lines
Longer term, my main sticking point on the fdtgrep stuff was entry points whose semantics don't make me go cross-eyed (includes these nodes, but not those nodes, and might include children if this flag is set, but not that one and the operator's shoe size matches some other property...). I'm not sure if that's a question of redesigning the interface, or just of describing it better.
Quoted 4 lines
Neither am I, but perhaps if I cut down the fdtgrep options so that it only does a few basic things that would help? The full feature set would still be in the implementation, but it would reduce confusion on that side.
It's not really the fdtgrep tool which bothers me, I'm much more concerned with the semantics of the libfdt function it uses to do its work.
Quoted 10 lines
Quoted 7 lines
Quoted 6 lines
I do ask people to send things upstream, and if rejected we then have to work out what to do...there are recent U-Boot mailing list threads on this.
Regards,
SimonRegards,
Simon
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi David,
Quoted 60 lines · David Gibson
Quoted 59 lines · Simon Glass
Hi David,
Quoted 41 lines · David Gibson
Quoted 32 lines · Simon Glass
Hi Jon,
Quoted 20 lines · Jon Loeliger
Quoted 18 lines · Thierry Reding
From: Thierry Reding <[email protected]>
These three patches add a couple of string functions that have proven useful in U-Boot's copy of libfdt, so they are likely to be useful for other users as well.
Patch 1 adds a function to count the number of strings in a property's value. This also adds a new DTS sample along with a small test program to validate the implemented functions.
Patch 2 adds a function to retrieve the index of a given string in any given property's value. This adds code to the test program introduced in the previous patch to exercise the new functionality.
Patch 3 adds a function to retrieve a string by index from a property's value along with a shortcut for index 0. This extends the test program introduced in patch 1 to validate the new functionality.
Quoted 8 lines
Quoted 1 line
Thierry
Hi Thierry,
While I am generally fine with this patch set, I have a large-scope question. Is there a larger plan to consolidate or unify the U-Boot and DTC libfdts?
Quoted 7 lines
I maintain the fdt tree for U-Boot at present. About once a quarter I check what has changed and do a bit of a sync. But there are things that libfdt upstream has not accepted - e.g. the grep functionality used by verified boot hashing stuff. I wish we could figure that out. Perhaps a cut-down fdtgrep tool would meet with favour. We're using it even more now since SPL (the minimal U-Boot loader) wants to run with a subset of the full board FDT for SRAM space reasons.
Quoted 4 lines
So, short-term: there's no reason your fdtgrep stuff needs to be considered as part of your version of libfdt - it could just as easily be an add-on sitting alongside libfdt - then you could share the core libfdt code at least.
That's how it is today, yes.
Quoted 7 lines
Longer term, my main sticking point on the fdtgrep stuff was entry points whose semantics don't make me go cross-eyed (includes these nodes, but not those nodes, and might include children if this flag is set, but not that one and the operator's shoe size matches some other property...). I'm not sure if that's a question of redesigning the interface, or just of describing it better.
Quoted 8 lines
Quoted 4 lines
Neither am I, but perhaps if I cut down the fdtgrep options so that it only does a few basic things that would help? The full feature set would still be in the implementation, but it would reduce confusion on that side.
It's not really the fdtgrep tool which bothers me, I'm much more concerned with the semantics of the libfdt function it uses to do its work.
Yes that's the tricky bit. I'll revisit it again with fresh eyes (it has been a while) and see if there is something I can do. Fundamentally it is not complex, but there are quite a few combinations to deal with and I think that is the tricky part.
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
Quoted 7 lines
I do ask people to send things upstream, and if rejected we then have to work out what to do...there are recent U-Boot mailing list threads on this.
Regards,
SimonRegards,
Simon
Regards,
Simon