devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benoit Cousson <bcousson@baylibre.com>
To: olof@lixom.net, devicetree@vger.kernel.org,
	tomasz.figa@gmail.com, swarren@wwwdotorg.org,
	grant.likely@secretlab.ca, rob.herring@calxeda.com
Cc: khilman@linaro.org, linux-omap@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org, fparent@baylibre.com,
	Benoit Cousson <bcousson@baylibre.com>
Subject: [RFC 11/15] scripts/dtc: check for children nodes
Date: Tue, 24 Sep 2013 18:52:17 +0200	[thread overview]
Message-ID: <1380041541-17529-12-git-send-email-bcousson@baylibre.com> (raw)
In-Reply-To: <1380041541-17529-1-git-send-email-bcousson@baylibre.com>

From: Fabien Parent <fparent@baylibre.com>

Add the ability to check if a node has some required children nodes.
Add as well two test files for this feature.

node {
    compatible = "comp";

    subnode1 {
    };

    subnode2 {
    };

    abc {
    };
};

One can check if 'node' has the following subnode 'subnode1', 'subnode2', and
'abc' with the schema below:

/dts-v1/;
/ {
    compatible = "comp";
    children = "abc", "subnode[0-9]";
};

Signed-off-by: Fabien Parent <fparent@baylibre.com>
Signed-off-by: Benoit Cousson <bcousson@baylibre.com>
---
 scripts/dtc/schema-test.c                         |  6 +++
 scripts/dtc/schema.c                              | 47 +++++++++++++++++++++++
 scripts/dtc/tests/schemas/children-nodes-1.schema |  5 +++
 scripts/dtc/tests/schemas/children-nodes-2.schema |  5 +++
 scripts/dtc/tests/test1.dts                       |  4 ++
 5 files changed, 67 insertions(+)
 create mode 100644 scripts/dtc/tests/schemas/children-nodes-1.schema
 create mode 100644 scripts/dtc/tests/schemas/children-nodes-2.schema

diff --git a/scripts/dtc/schema-test.c b/scripts/dtc/schema-test.c
index 128a265..9f1ce31 100644
--- a/scripts/dtc/schema-test.c
+++ b/scripts/dtc/schema-test.c
@@ -75,6 +75,12 @@ static struct schema_test tests[] = {
 	 "tests/schemas/nodes-count-3.schema", 0},
 	{"Nodes Count #4", "tests/test1.dts",
 	 "tests/schemas/nodes-count-4.schema", 0},
+
+	/* Children nodes */
+	{"Children Nodes #1", "tests/test1.dts",
+	 "tests/schemas/children-nodes-1.schema", 1},
+	{"Children Nodes #2", "tests/test1.dts",
+	 "tests/schemas/children-nodes-2.schema", 0},
 };
 
 int main(void)
diff --git a/scripts/dtc/schema.c b/scripts/dtc/schema.c
index b7cfb37..a454a19 100644
--- a/scripts/dtc/schema.c
+++ b/scripts/dtc/schema.c
@@ -47,6 +47,11 @@ static const char *const SCHEMA_EXT = ".schema";
 static const char *const VALUE_PROPNAME = "value";
 static int exit_on_failure = 0;
 
+struct str_list {
+	char *str;
+	struct str_list *next;
+};
+
 struct node_list {
 	struct node *n;
 	struct node_list *next;
@@ -92,6 +97,7 @@ struct node_constraints {
 	uint32_t count_requested;
 	uint32_t count;
 	uint32_t max_count;
+	struct str_list *children;
 };
 
 struct schema_db {
@@ -664,6 +670,8 @@ static void load_node_constraints(struct node_constraints *nc,
 				  struct node *n)
 {
 	struct property *p;
+	int i = 0;
+	struct str_list *iter;
 
 	assert(n);
 	assert(nc);
@@ -675,6 +683,15 @@ static void load_node_constraints(struct node_constraints *nc,
 
 	p = get_property(n, "max-count");
 	nc->max_count = p ? prop_val_to_uint32(p, 0) : ULONG_MAX;
+
+	p = get_property(n, "children");
+	while (p && i < p->val.len && i >= 0) {
+		iter = xmalloc(sizeof(*iter));
+		iter->str = xstrdup(p->val.val + i);
+		iter->next = nc->children;
+		nc->children = iter;
+		i = get_next_string_offset(p, i);
+	}
 }
 
 static int validate_node(struct node *n,
@@ -682,7 +699,10 @@ static int validate_node(struct node *n,
 			 struct node_list *path)
 {
 	struct node *iter;
+	struct str_list *pattern;
 	int ret = 1;
+	pcre *re;
+	int has_child;
 
 	assert(n);
 	assert(path);
@@ -692,6 +712,27 @@ static int validate_node(struct node *n,
 	for (iter = nc->dt->children; iter; iter = iter->next_sibling)
 		ret &= validate_properties(n, iter, path);
 
+	/* Check whether the node has all the required children nodes */
+	for (pattern = nc->children;
+	     pattern;
+	     pattern = pattern->next) {
+		re = compile_pattern(pattern->str);
+		if (!re)
+			die("Invalid pattern: %s\n", pattern->str);
+
+		has_child = 0;
+		for (iter = n->children; iter; iter = iter->next_sibling) {
+			has_child |= pcre_exec(re, 0, iter->name,
+					       strlen(iter->name), 0, 0,
+					       NULL, 0) >= 0;
+		}
+
+		pcre_free(re);
+		DT_ERROR_IF(!has_child, path, NULL,
+			    "Missing child node '%s'\n", pattern->str);
+	}
+
+end:
 	return ret;
 }
 
@@ -1021,9 +1062,15 @@ struct schema_db *build_schema_db(const char *dir)
 
 static void free_node_constraints(struct node_constraints *nc)
 {
+	struct str_list *iter, *iter_next;
+
 	if (!nc)
 		return;
 
+	for_each_safe(nc->children, iter, iter_next) {
+		free(iter->str);
+	}
+
 	pcre_free(nc->re_compat);
 	free_dt(nc->bi);
 	free(nc->filepath);
diff --git a/scripts/dtc/tests/schemas/children-nodes-1.schema b/scripts/dtc/tests/schemas/children-nodes-1.schema
new file mode 100644
index 0000000..8f1cf9a
--- /dev/null
+++ b/scripts/dtc/tests/schemas/children-nodes-1.schema
@@ -0,0 +1,5 @@
+/dts-v1/;
+/ {
+	compatible = "node";
+	children = "node1", "node2";
+};
diff --git a/scripts/dtc/tests/schemas/children-nodes-2.schema b/scripts/dtc/tests/schemas/children-nodes-2.schema
new file mode 100644
index 0000000..f0ee2f2
--- /dev/null
+++ b/scripts/dtc/tests/schemas/children-nodes-2.schema
@@ -0,0 +1,5 @@
+/dts-v1/;
+/ {
+	compatible = "node";
+	children = "node3";
+};
diff --git a/scripts/dtc/tests/test1.dts b/scripts/dtc/tests/test1.dts
index 7d8d745..c390050 100644
--- a/scripts/dtc/tests/test1.dts
+++ b/scripts/dtc/tests/test1.dts
@@ -15,4 +15,8 @@
 			compatible = "compat2";
 		};
 	};
+
+	node2 {
+		compatible = "compat3";
+	};
 };
-- 
1.8.1.2


  parent reply	other threads:[~2013-09-24 16:52 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-24 16:52 [RFC 00/15] Device Tree schemas and validation Benoit Cousson
2013-09-24 16:52 ` [RFC 01/15] scripts/dtc: fix most memory leaks in dtc Benoit Cousson
     [not found]   ` <1380041541-17529-2-git-send-email-bcousson-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
2013-10-02 12:59     ` David Gibson
     [not found]       ` <CAOwMV_zAZG3vvWS6pkyK-FbOEg_32KRO-k1SmFSh-pc9+0JiPA@mail.gmail.com>
2013-10-03 14:26         ` Fabien Parent
2013-09-24 16:52 ` [RFC 04/15] scripts/dtc: add procedure to handle dts errors Benoit Cousson
2013-09-24 16:52 ` [RFC 05/15] scripts/dtc: check type on properties Benoit Cousson
2013-09-24 16:52 ` [RFC 07/15] scripts/dtc: can inherit properties Benoit Cousson
     [not found] ` <1380041541-17529-1-git-send-email-bcousson-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
2013-09-24 16:52   ` [RFC 02/15] scripts/dtc: build schema index for dts validation Benoit Cousson
2013-09-24 16:52   ` [RFC 03/15] scripts/dtc: validate each nodes and properties Benoit Cousson
2013-09-24 16:52   ` [RFC 06/15] scripts/dtc: check for required properties Benoit Cousson
2013-09-24 16:52   ` [RFC 08/15] scripts/dtc: check array size Benoit Cousson
2013-09-24 16:52   ` [RFC 09/15] scripts/dtc: check value of properties Benoit Cousson
2013-09-24 16:52   ` [RFC 10/15] scripts/dtc: add count limit on nodes Benoit Cousson
2013-10-01 22:22   ` [RFC 00/15] Device Tree schemas and validation Stephen Warren
     [not found]     ` <524B4B20.4020002-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
2013-10-02 14:29       ` David Gibson
     [not found]         ` <20131002142914.GI6506-RXTfZT5YzpxwFLYp8hBm2A@public.gmane.org>
2013-10-03 13:53           ` Benoit Cousson
2013-10-06  3:02             ` Chaiken, Alison
2013-10-03 13:17     ` Benoit Cousson
2013-09-24 16:52 ` Benoit Cousson [this message]
2013-09-24 16:52 ` [RFC 12/15] scripts/dtc: check constraints on parents Benoit Cousson
2013-09-24 16:52 ` [RFC 13/15] bindings: OMAP: add new schema files Benoit Cousson
2013-09-24 16:52 ` [RFC 14/15] scripts/dtc: validate dts against schema bindings Benoit Cousson
2013-09-24 16:52 ` [RFC 15/15] scripts/dtc: add verbose options Benoit Cousson
2013-10-01  8:06 ` [RFC 00/15] Device Tree schemas and validation Benoit Cousson
     [not found]   ` <524A8289.3050107-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
2013-10-01 13:17     ` Rob Herring
     [not found]       ` <524ACB76.1010001-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2013-10-01 15:06         ` Benoit Cousson
2013-10-01 15:17           ` Jon Loeliger
2013-10-02  8:24             ` David Gibson
2013-10-02  9:25             ` Benoit Cousson
     [not found]               ` <524BE66D.7060308-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
2013-10-02 13:22                 ` Jon Loeliger
     [not found]           ` <524AE4FB.4080906-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
2013-10-01 20:54             ` Rob Herring
     [not found]               ` <CAL_JsqJ31TGFJCSeSOqgee=OLVfSUTAYdF4nSn7X2DiCequVAw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2013-10-02 13:54                 ` David Gibson
2013-10-02 18:08                   ` Mark Brown
2013-10-02 23:38                     ` David Gibson
2013-10-03  6:52                   ` Benoit Cousson
2013-10-02 13:52         ` David Gibson

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=1380041541-17529-12-git-send-email-bcousson@baylibre.com \
    --to=bcousson@baylibre.com \
    --cc=devicetree@vger.kernel.org \
    --cc=fparent@baylibre.com \
    --cc=grant.likely@secretlab.ca \
    --cc=khilman@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=olof@lixom.net \
    --cc=rob.herring@calxeda.com \
    --cc=swarren@wwwdotorg.org \
    --cc=tomasz.figa@gmail.com \
    /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).