[PATCH 2/3] fdt: Add a function to get the index of a string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



From: Thierry Reding <treding@xxxxxxxxxx>

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@xxxxxxxxxx>
---
 libfdt/fdt_ro.c | 26 ++++++++++++++++++++++++++
 libfdt/libfdt.h | 11 +++++++++++
 tests/strings.c | 22 ++++++++++++++++++++++
 3 files changed, 59 insertions(+)

diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index 874975a0d8ad..aab3d1b94509 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -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)
 {
diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
index 93deec13655b..34ab30dc3338 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -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)                           */
 /**********************************************************************/
diff --git a/tests/strings.c b/tests/strings.c
index 642057d1c8d9..050230567468 100644
--- a/tests/strings.c
+++ b/tests/strings.c
@@ -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();
 }
-- 
2.4.5

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Device Tree]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux