[U-Boot] [PATCH v2 03/40] fdt: Add a function to get the index of a string

Thierry Reding thierry.reding at gmail.com
Tue Aug 26 17:33:51 CEST 2014


From: Thierry Reding <treding at nvidia.com>

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.

Signed-off-by: Thierry Reding <treding at nvidia.com>
---
Changes in v2:
- rename to fdt_find_string() to remove naming conflicts with other new
  functions
- correctly increment string list pointer

 include/libfdt.h    | 11 +++++++++++
 lib/libfdt/fdt_ro.c | 26 ++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/include/libfdt.h b/include/libfdt.h
index cf97bf0b3b52..d0dea668eea2 100644
--- a/include/libfdt.h
+++ b/include/libfdt.h
@@ -866,6 +866,17 @@ int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
  */
 int fdt_count_strings(const void *fdt, int node, const char *property);
 
+/**
+ * fdt_find_string - find a string in a string list and return its index
+ * @fdt: pointer to the device tree blob
+ * @node: offset of the 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 node, const char *property,
+		    const char *string);
+
 /**********************************************************************/
 /* Read-only functions (addressing related)                           */
 /**********************************************************************/
diff --git a/lib/libfdt/fdt_ro.c b/lib/libfdt/fdt_ro.c
index cb06a9b50d85..fec4a0a141fd 100644
--- a/lib/libfdt/fdt_ro.c
+++ b/lib/libfdt/fdt_ro.c
@@ -511,6 +511,32 @@ int fdt_count_strings(const void *fdt, int node, const char *property)
 	return count;
 }
 
+int fdt_find_string(const void *fdt, int node, const char *property,
+		    const char *string)
+{
+	const char *list, *end;
+	int len, index = 0;
+
+	list = fdt_getprop(fdt, node, 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)
 {
-- 
2.0.4



More information about the U-Boot mailing list