From: Thierry Reding <thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: David Gibson
<david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>,
Jon Loeliger <jdl-CYoMK+44s/E@public.gmane.org>
Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>,
Masahiro Yamada
<yamada.masahiro-uWyLwvC0a2jby3iVrkZq2A@public.gmane.org>
Subject: [PATCH 1/3] fdt: Add a function to count strings
Date: Wed, 15 Jul 2015 13:13:57 +0200 [thread overview]
Message-ID: <1436958839-14793-2-git-send-email-thierry.reding@gmail.com> (raw)
In-Reply-To: <1436958839-14793-1-git-send-email-thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
From: Thierry Reding <treding-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
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 <treding-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
---
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
diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index a65e4b5b72b6..874975a0d8ad 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -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)
{
diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
index 1054512814bc..93deec13655b 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -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) */
/**********************************************************************/
diff --git a/tests/.gitignore b/tests/.gitignore
index 5656555f9cbc..09a1d3b84f6f 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -49,6 +49,7 @@ tmp.*
/setprop_inplace
/sized_cells
/string_escapes
+/strings
/subnode_iterate
/subnode_offset
/supernode_atdepth_offset
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index 9adedecdff3d..415e4d670f2c 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -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 \
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 5268293434af..92ee96340c29 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -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
diff --git a/tests/strings.c b/tests/strings.c
new file mode 100644
index 000000000000..642057d1c8d9
--- /dev/null
+++ b/tests/strings.c
@@ -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();
+}
diff --git a/tests/strings.dts b/tests/strings.dts
new file mode 100644
index 000000000000..04c2c202ec77
--- /dev/null
+++ b/tests/strings.dts
@@ -0,0 +1,10 @@
+/dts-v1/;
+
+/ {
+ compatible = "foo";
+
+ device {
+ compatible = "bar", "baz";
+ big-endian;
+ };
+};
--
2.4.5
next prev parent reply other threads:[~2015-07-15 11:13 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-15 11:13 [PATCH 0/3] Add a couple of string-related functions Thierry Reding
[not found] ` <1436958839-14793-1-git-send-email-thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-07-15 11:13 ` Thierry Reding [this message]
[not found] ` <1436958839-14793-2-git-send-email-thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-07-15 13:41 ` [PATCH 1/3] fdt: Add a function to count strings David Gibson
[not found] ` <20150715134130.GA1567-RXTfZT5YzpxwFLYp8hBm2A@public.gmane.org>
2015-07-16 11:13 ` Thierry Reding
2015-07-16 12:29 ` David Gibson
[not found] ` <20150716122935.GD25179-1s0os16eZneny3qCrzbmXA@public.gmane.org>
2015-07-17 9:10 ` Thierry Reding
2015-07-17 13:44 ` David Gibson
2015-07-15 11:13 ` [PATCH 2/3] fdt: Add a function to get the index of a string Thierry Reding
2015-07-15 11:13 ` [PATCH 3/3] fdt: Add functions to retrieve strings Thierry Reding
2015-07-15 13:29 ` [PATCH 0/3] Add a couple of string-related functions Jon Loeliger
[not found] ` <E1ZFMkd-0002ir-O5-h9b8CULRd+FWk0Htik3J/w@public.gmane.org>
2015-07-15 21:45 ` Simon Glass
[not found] ` <CAPnjgZ12VVSDkALDDg31Sgwo1PpFPfE10GFtrseHWpRX1re-HQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-16 5:27 ` David Gibson
[not found] ` <20150716052716.GB25179-1s0os16eZneny3qCrzbmXA@public.gmane.org>
2015-07-16 16:56 ` Simon Glass
[not found] ` <CAPnjgZ0FQvjkC=LUWeCgeDw+x+DePyaNQ=CiC+ht86U2KY_BMA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-07-16 23:40 ` David Gibson
[not found] ` <20150716234007.GE25179-1s0os16eZneny3qCrzbmXA@public.gmane.org>
2015-07-20 2:19 ` Simon Glass
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1436958839-14793-2-git-send-email-thierry.reding@gmail.com \
--to=thierry.reding-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
--cc=david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org \
--cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=jdl-CYoMK+44s/E@public.gmane.org \
--cc=sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org \
--cc=yamada.masahiro-uWyLwvC0a2jby3iVrkZq2A@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).