Skip to contentSkip to conversation

Hybrid approach for DT schema checking

RFC PATCH In review Tomasz Figa 2 participants 11 messages Feb 20, 2014 – May 13, 2014 4 messages not in the archive Kbuild

lore
Share & tools
lore
#0 lore raw

cover Hybrid approach for DT schema checking

This series adds a proof of concept framework to implement schema checker using a combined C and DTSS based approach. Several example bindings are also implemented using C and DTSS[1].

Complex and generic bindings can be implemented directly in C and then instantiated from simple device-specific bindings using DTS-like DTSS language.

A quick description of C part:

A new check is registered in dtc checks framework to perform schema checking of each node. Checking is done by searching specified schema set for matching schemas (by compatible, device_type or absolute path) and applying matched schemas to the node.

Schemas for complex generic bindings (such as interrupts, gpios, clocks, i2c, mmio-bus, etc.) can be implemented directly in C and instantiated from DTSS schemas of particular devices. An example C schema may look like:

static void generic_checkfn_xxx_yyy(const struct generic_schema *schema,
struct node *root, struct node *node, struct node *params, bool required)
{
/*
* Get necessary schema arguments from DTSS schema by looking
* at properties of @params node.
*
* Check whether @node node matches the schema.
*
* The @required argument may be used to check whether some error
* conditions should be ignored (e.g. unspecified interrupt). */
}
GENERIC_SCHEMA("xxx-yyy", xxx_yyy);

A quick description of DTSS part:

* DTSS is a DTS-like language for specification of simple bindings, e.g. bindings of particular devices. The basic syntax is very similar to DTS, with main elements being nodes and properties. At root level a series of nodes should be specified representing particular bindings:

	/dtss-v1/;

	binding1 {
		/* Definition of binding 1 */
	};

	binding2 {
		/* Definition of binding 2 */
	};

* Matching key for each binding can be specified using /match/ keyword:

	root-node {
		/match/ path = "/";
	};

	wlf,wm8903 {
		/match/ compatible = "wlf,wm8903";
	};

	pci-bus {
		/match/ device_type = "memory";
	};

Currently supported matches: path, compatible, device_type.

* Bindings can be specified either by listing properties they require (or can use) directly or by instantiating generic C-based bindings.

	binding {
		required-property;

		/optional/ optional-property;

		/require/ required-generic-schema;

		/use/ optional-generic-schema {
			schema-argument = <1>;
		};
	};

Generic schemas are implemented in C, as described above, and can use arguments specified in DTSS as properties. /require/ calls the schema with required=true, while /use/ with required=false.

This is based on Stephen Warren's C based DT schema checker proof of concept patch adding C-based validation[2].

TODO:
- specification of subnodes directly from DTSS,
- specification of simple property values from DTSS (cells, strings, phandles),
- reporting of unrecognized properties,
- probably many more...

[1] Device Tree Schema Source
[2] http://thread.gmane.org/gmane.linux.ports.arm.kernel/275896

Tomasz Figa (5):
dtc: Add helpers for various message levels
dtc: livetree: Add more tree parsing helpers
Another try of DT schema checker using hybrid C and DTSS based
approach
Add sample C-based generic bindings
Add sample DTS and DTSS schema

 Makefile                                  |   2 +-
 Makefile.dtc                              |  10 +-
 checks.c                                  |  15 +
 dtc.c                                     |  17 +-
 dtc.h                                     |  57 ++++
 dtss-lexer.l                              | 291 +++++++++++++++++++
 dtss-parser.y                             | 341 ++++++++++++++++++++++
 livetree.c                                | 230 +++++++++++++++
 sample.dts                                |  70 +++++
 schema.dtss                               |  86 ++++++
 schemas/clock/clock.c                     |  77 +++++
 schemas/gpio/gpio.c                       |  93 ++++++
 schemas/i2c/i2c.c                         |  42 +++
 schemas/interrupt-controller/interrupts.c | 452 ++++++++++++++++++++++++++++++
 schemas/mmio-bus.c                        |  97 +++++++
 schemas/schema.c                          | 311 ++++++++++++++++++++
 schemas/schema.h                          |  89 ++++++
 srcpos.h                                  |   2 +
 treesource.c                              |  22 ++
 19 files changed, 2300 insertions(+), 4 deletions(-)
 create mode 100644 dtss-lexer.l
 create mode 100644 dtss-parser.y
 create mode 100644 sample.dts
 create mode 100644 schema.dtss
 create mode 100644 schemas/clock/clock.c
 create mode 100644 schemas/gpio/gpio.c
 create mode 100644 schemas/i2c/i2c.c
 create mode 100644 schemas/interrupt-controller/interrupts.c
 create mode 100644 schemas/mmio-bus.c
 create mode 100644 schemas/schema.c
 create mode 100644 schemas/schema.h
Signature · 6 lines
1.8.5.2

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw

RFC PATCH 1/5 dtc: Add helpers for various message levels

In reply to a message not in the archive

This patch adds three helper macros to print errors, warnings and informational messages using standard format.

---
 dtc.h | 3 +++
 1 file changed, 3 insertions(+)
dtc.h +3 -0
index 20de073..e95bed7 100644
@@ -43,6 +43,9 @@
 #define debug(fmt,args...)
 #endif
 
+#define pr_err(...)	fprintf (stderr, "ERROR: " __VA_ARGS__)
+#define pr_warn(...)	fprintf (stderr, "WARNING: " __VA_ARGS__)
+#define pr_info(...)	fprintf (stderr, "INFO: " __VA_ARGS__)
 
 #define DEFAULT_FDT_VERSION	17
 
Signature · 6 lines
1.8.5.2

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw

RFC PATCH 2/5 dtc: livetree: Add more tree parsing helpers

In reply to a message not in the archive

This patch extends the set of parsing helpers available in dtc with string and phandle parsing.

---
 dtc.h      |  28 ++++++++
 livetree.c | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 258 insertions(+)
dtc.h +28 -0
index e95bed7..9ce9d12 100644
@@ -163,6 +163,13 @@ struct node {
 	struct label *labels;
 };
 
+#define MAX_PHANDLE_ARGS 16
+struct of_phandle_args {
+	struct node *np;
+	int args_count;
+	uint32_t args[MAX_PHANDLE_ARGS];
+};
+
 #define for_each_label_withdel(l0, l) \
 	for ((l) = (l0); (l); (l) = (l)->next)
 
@@ -184,6 +191,11 @@ struct node {
 	for_each_child_withdel(n, c) \
 		if (!(c)->deleted)
 
+#define for_each_propval_string(p, s)	\
+	for (s = propval_next_string(p, NULL);		\
+		s;					\
+		s = propval_next_string(p, s))
+
 void add_label(struct label **labels, char *label);
 void delete_labels(struct label **labels);
 
@@ -208,6 +220,22 @@ void delete_node(struct node *node);
 const char *get_unitname(struct node *node);
 struct property *get_property(struct node *node, const char *propname);
 cell_t propval_cell(struct property *prop);
+int propval_string_count(struct node *np, struct property *prop);
+const char *propval_next_string(struct property *prop, const char *cur);
+int propval_match_string(struct property *prop, const char *string);
+struct node *propval_parse_phandle(struct node *root,
+				     struct property *prop, int index);
+int propval_parse_phandle_with_args(struct node *root,
+				    struct property *prop,
+				    const char *cells_name, int index,
+				    struct of_phandle_args *out_args);
+int propval_parse_phandle_with_fixed_args(struct node *root,
+					  struct property *prop, int cell_count,
+					  int index,
+					  struct of_phandle_args *out_args);
+int propval_count_phandle_with_args(struct node *root,
+				    struct property *prop,
+				    const char *cells_name);
 struct property *get_property_by_label(struct node *tree, const char *label,
 				       struct node **node);
 struct marker *get_marker_label(struct node *tree, const char *label,
livetree.c +230 -0

242 lines not on this page · raw message

Signature · 6 lines
1.8.5.2

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#8 lore raw

RFC PATCH 3/5 Implement DT schema checker using hybrid approach

In reply to a message not in the archive

This patch adds a proof of concept framework to implement schema checker using a combined C and DTSS based approach.

Complex and generic bindings can be implemented directly in C and then instantiated from simple device-specific bindings using DTS-like DTSS language.

This is based on Stephen Warren's C based DT schema checker proof of concept patch.

[original C based DT schema checker proof of concept]
[further development into hybrid solution]
---
 Makefile         |   2 +-
 Makefile.dtc     |   5 +-
 checks.c         |  15 +++
 dtc.c            |  17 ++-
 dtc.h            |  26 +++++
 dtss-lexer.l     | 291 +++++++++++++++++++++++++++++++++++++++++++++++
 dtss-parser.y    | 341 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 schemas/schema.c | 311 ++++++++++++++++++++++++++++++++++++++++++++++++++
 schemas/schema.h |  89 +++++++++++++++
 srcpos.h         |   2 +
 treesource.c     |  22 ++++
 11 files changed, 1117 insertions(+), 4 deletions(-)
 create mode 100644 dtss-lexer.l
 create mode 100644 dtss-parser.y
 create mode 100644 schemas/schema.c
 create mode 100644 schemas/schema.h
Makefile +1 -1
index 86f5ab3..0625fb8 100644
@@ -15,7 +15,7 @@ EXTRAVERSION =
 LOCAL_VERSION =
 CONFIG_LOCALVERSION =
 
-CPPFLAGS = -I libfdt -I .
+CPPFLAGS = -I libfdt -I . -I schemas
 WARNINGS = -Werror -Wall -Wpointer-arith -Wcast-qual -Wnested-externs \
 	-Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -Wshadow
 CFLAGS = -g -Os -fPIC -Werror $(WARNINGS)
Makefile.dtc +3 -2
index bece49b..bf19564 100644
@@ -12,7 +12,8 @@ DTC_SRCS = \
 	livetree.c \
 	srcpos.c \
 	treesource.c \
-	util.c
+	util.c \
+	schemas/schema.c
 
-DTC_GEN_SRCS = dtc-lexer.lex.c dtc-parser.tab.c
+DTC_GEN_SRCS = dtc-lexer.lex.c dtc-parser.tab.c dtss-lexer.lex.c dtss-parser.tab.c
 DTC_OBJS = $(DTC_SRCS:%.c=%.o) $(DTC_GEN_SRCS:%.c=%.o)
checks.c +15 -0
index 47eda65..7c85fcf 100644
@@ -19,6 +19,7 @@
  */
 
 #include "dtc.h"
+#include "schemas/schema.h"
 
 #ifdef TRACE_CHECKS
 #define TRACE(c, ...) \
@@ -651,6 +652,18 @@ static void check_obsolete_chosen_interrupt_controller(struct check *c,
 }
 TREE_WARNING(obsolete_chosen_interrupt_controller, NULL);
 
+/*
+ * Schema checks
+ */
+
+static void check_schema(struct check *c, struct node *dt,
+				       struct node *node)
+{
+	if (schema_check_node(dt, node))
+		FAIL(c, "Schema check failed for %s", node->fullpath);
+}
+NODE_ERROR(schema, NULL);
+
 static struct check *check_table[] = {
 	&duplicate_node_names, &duplicate_property_names,
 	&node_name_chars, &node_name_format, &property_name_chars,
@@ -669,6 +682,8 @@ static struct check *check_table[] = {
 	&avoid_default_addr_size,
 	&obsolete_chosen_interrupt_controller,
 
+	&schema,
+
 	&always_fail,
 };
 
dtc.c +16 -1
index d36ccdc..1a5913b 100644
@@ -20,6 +20,7 @@
 
 #include "dtc.h"
 #include "srcpos.h"
+#include "schemas/schema.h"
 
 /*
  * Command line options
@@ -49,7 +50,7 @@ static void fill_fullpaths(struct node *tree, const char *prefix)
 
 /* Usage related data. */
 static const char usage_synopsis[] = "dtc [options] <input file>";
-static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:fb:i:H:sW:E:hv";
+static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:fb:i:H:sW:E:x:hv";
 static struct option const usage_long_opts[] = {
 	{"quiet",            no_argument, NULL, 'q'},
 	{"in-format",         a_argument, NULL, 'I'},
@@ -67,6 +68,7 @@ static struct option const usage_long_opts[] = {
 	{"phandle",           a_argument, NULL, 'H'},
 	{"warning",           a_argument, NULL, 'W'},
 	{"error",             a_argument, NULL, 'E'},
+	{"schema",            a_argument, NULL, 'x'},
 	{"help",             no_argument, NULL, 'h'},
 	{"version",          no_argument, NULL, 'v'},
 	{NULL,               no_argument, NULL, 0x0},
@@ -97,6 +99,7 @@ static const char * const usage_opts_help[] = {
 	 "\t\tboth   - Both \"linux,phandle\" and \"phandle\" properties",
 	"\n\tEnable/disable warnings (prefix with \"no-\")",
 	"\n\tEnable/disable errors (prefix with \"no-\")",
+	"\n\tUse schema file"
 	"\n\tPrint this help and exit",
 	"\n\tPrint version and exit",
 	NULL,
@@ -105,10 +108,12 @@ static const char * const usage_opts_help[] = {
 int main(int argc, char *argv[])
 {
 	struct boot_info *bi;
+	struct boot_info *bi_schema;
 	const char *inform = "dts";
 	const char *outform = "dts";
 	const char *outname = "-";
 	const char *depname = NULL;
+	const char *schema = NULL;
 	bool force = false, sort = false;
 	const char *arg;
 	int opt;
@@ -185,6 +190,10 @@ int main(int argc, char *argv[])
 			parse_checks_option(false, true, optarg);
 			break;
 
+		case 'x':
+			schema = optarg;
+			break;
+
 		case 'h':
 			usage(NULL);
 		default:
@@ -220,6 +229,12 @@ int main(int argc, char *argv[])
 	else
 		die("Unknown input format \"%s\"\n", inform);
 
+	if (schema) {
+		bi_schema = schema_from_source(schema);
+		//dt_to_source(stdout, bi_schema);
+		build_schema_list(bi_schema);
+	}
+
 	if (depfile) {
 		fputc('\n', depfile);
 		fclose(depfile);
dtc.h +26 -0
index 9ce9d12..19d2d24 100644
@@ -135,21 +135,43 @@ struct label {
 	struct label *next;
 };
 
+enum {
+	PROPERTY_DATA,
+	PROPERTY_USE,
+	PROPERTY_REQUIRE,
+	PROPERTY_MATCH,
+};
+
+#define PROPERTY_FLAG_OPTIONAL	(1 << 0)
+
 struct property {
 	bool deleted;
 	char *name;
 	struct data val;
+	unsigned int type;
+	unsigned int flags;
 
 	struct property *next;
 
 	struct label *labels;
 };
 
+enum {
+	NODE_DATA,
+	NODE_USE,
+	NODE_REQUIRE,
+};
+
+#define NODE_FLAG_OPTIONAL	(1 << 0)
+#define NODE_FLAG_INCOMPLETE	(1 << 1)
+
 struct node {
 	bool deleted;
 	char *name;
 	struct property *proplist;
 	struct node *children;
+	int type;
+	unsigned int flags;
 
 	struct node *parent;
 	struct node *next_sibling;
@@ -297,4 +319,8 @@ struct boot_info *dt_from_source(const char *f);
 
 struct boot_info *dt_from_fs(const char *dirname);
 
+/* Schema source */
+
+struct boot_info *schema_from_source(const char *fname);
+
 #endif /* _DTC_H */
dtss-lexer.l new file +291 -0

291 lines not on this page · raw message

dtss-parser.y new file +341 -0

341 lines not on this page · raw message

schemas/schema.c new file +311 -0

311 lines not on this page · raw message

schemas/schema.h new file +89 -0

89 lines not on this page · raw message

srcpos.h +2 -0
index f81827b..b761522 100644
@@ -75,7 +75,9 @@ struct srcpos {
     struct srcfile_state *file;
 };
 
+#ifndef YYLTYPE
 #define YYLTYPE struct srcpos
+#endif
 
 #define YYLLOC_DEFAULT(Current, Rhs, N)						\
 	do {									\
treesource.c +22 -0
index bf7a626..e50285c 100644
@@ -25,6 +25,10 @@ extern FILE *yyin;
 extern int yyparse(void);
 extern YYLTYPE yylloc;
 
+extern FILE *dtss_yyin;
+extern int dtss_yyparse(void);
+extern YYLTYPE dtss_yylloc;
+
 struct boot_info *the_boot_info;
 bool treesource_error;
 
@@ -46,6 +50,24 @@ struct boot_info *dt_from_source(const char *fname)
 	return the_boot_info;
 }
 
+struct boot_info *schema_from_source(const char *fname)
+{
+	the_boot_info = NULL;
+	treesource_error = false;
+
+	srcfile_push(fname);
+	dtss_yyin = current_srcfile->f;
+	dtss_yylloc.file = current_srcfile;
+
+	if (dtss_yyparse() != 0)
+		die("Unable to parse input tree\n");
+
+	if (treesource_error)
+		die("Syntax error parsing input tree\n");
+
+	return the_boot_info;
+}
+
 static void write_prefix(FILE *f, int level)
 {
 	int i;
Signature · 6 lines
1.8.5.2

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#11 lore raw

RFC PATCH 4/5 Add sample C-based generic bindings

In reply to a message not in the archive
---
 Makefile.dtc                              |   7 +-
 schemas/clock/clock.c                     |  77 +++++
 schemas/gpio/gpio.c                       |  93 ++++++
 schemas/i2c/i2c.c                         |  42 +++
 schemas/interrupt-controller/interrupts.c | 452 ++++++++++++++++++++++++++++++
 schemas/mmio-bus.c                        |  97 +++++++
 6 files changed, 767 insertions(+), 1 deletion(-)
 create mode 100644 schemas/clock/clock.c
 create mode 100644 schemas/gpio/gpio.c
 create mode 100644 schemas/i2c/i2c.c
 create mode 100644 schemas/interrupt-controller/interrupts.c
 create mode 100644 schemas/mmio-bus.c
Makefile.dtc +6 -1
index bf19564..b75da69 100644
@@ -13,7 +13,12 @@ DTC_SRCS = \
 	srcpos.c \
 	treesource.c \
 	util.c \
-	schemas/schema.c
+	schemas/mmio-bus.c \
+	schemas/schema.c \
+	schemas/clock/clock.c \
+	schemas/gpio/gpio.c \
+	schemas/i2c/i2c.c \
+	schemas/interrupt-controller/interrupts.c \
 
 DTC_GEN_SRCS = dtc-lexer.lex.c dtc-parser.tab.c dtss-lexer.lex.c dtss-parser.tab.c
 DTC_OBJS = $(DTC_SRCS:%.c=%.o) $(DTC_GEN_SRCS:%.c=%.o)
schemas/clock/clock.c new file +77 -0

77 lines not on this page · raw message

schemas/gpio/gpio.c new file +93 -0

93 lines not on this page · raw message

schemas/i2c/i2c.c new file +42 -0

42 lines not on this page · raw message

schemas/interrupt-controller/interrupts.c new file +452 -0

452 lines not on this page · raw message

schemas/mmio-bus.c new file +97 -0

97 lines not on this page · raw message

Signature · 6 lines
1.8.5.2

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#12 lore raw

RFC PATCH 5/5 Add sample DTS and DTSS schema

In reply to a message not in the archive
---
 sample.dts  | 70 +++++++++++++++++++++++++++++++++++++++++++++++++
 schema.dtss | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 156 insertions(+)
 create mode 100644 sample.dts
 create mode 100644 schema.dtss
sample.dts new file +70 -0
new file mode 100644
index 0000000..f962051
@@ -0,0 +1,70 @@
+/dts-v1/;
+
+/ {
+	#address-cells = <1>;
+	#size-cells = <1>;
+	interrupt-parent = <&gic>;
+	compatible = "foo,bar";
+	model = "Foo Bar board";
+
+	chosen {
+		bootargs = "console=ttySAC2,115200N8";
+	};
+
+	aliases {
+		i2c0 = &i2c_0;
+	};
+
+	memory {
+		device_type = "memory";
+		reg = <0x40000000 0x40000000>;
+	};
+
+	soc {
+		compatible = "simple-bus";
+		#address-cells = <1>;
+		#size-cells = <1>;
+		ranges;
+
+		gic: interrupt-controller@10490000 {
+			compatible = "arm,cortex-a9-gic";
+			#interrupt-cells = <3>;
+			interrupt-controller;
+			reg = <0x10490000 0x1000>, <0x10480000 0x100>;
+			cpu-offset = <0x4000>;
+		};
+
+		i2c_0: i2c@13860000 {
+			compatible = "nvidia,tegra20-i2c";
+			reg = <0x13860000 0x100>;
+			interrupts = <0 57 0>;
+			clocks = <&clocks 43>, <&clocks 28>;
+			clock-names = "div-clk", "fast-clk";
+			#address-cells = <1>;
+			#size-cells = <0>;
+
+			audio-codec@33 {
+				compatible = "wlf,wm8903";
+				reg = <0x33>;
+				gpio-controller;
+				#gpio-cells = <1>;
+			};
+		};
+
+		gpio: pinctrl@11400000 {
+			compatible = "foo,bar-gpio";
+			reg = <0x11400000 0x1000>;
+			interrupts = <0 47 0>;
+			gpio-controller;
+			#gpio-cells = <2>;
+			interrupt-controller;
+			#interrupt-cells = <2>;
+		};
+
+		clocks: clock-controller@15440000 {
+			compatible = "foo,bar-clocks";
+			reg = <0x15440000 0x1000>;
+			#clock-cells = <1>;
+		};
+	};
+};
schema.dtss new file +86 -0

86 lines not on this page · raw message

Signature · 6 lines
1.8.5.2

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw

Re: [PATCH RFC 1/5] dtc: Add helpers for various message levels

In reply to a message not in the archive
Quoted 13 lines · Tomasz Figa

This patch adds three helper macros to print errors, warnings and informational messages using standard format.

---
 dtc.h | 3 +++
 1 file changed, 3 insertions(+)
dtc.h +0 -0
index 20de073..e95bed7 100644

dtc.h line 48

Quoted 6 lines
dtc.h +3 -0
@@ -43,6 +43,9 @@
 #define debug(fmt,args...)
 #endif
 
+#define pr_err(...)	fprintf (stderr, "ERROR: " __VA_ARGS__)
+#define pr_warn(...)	fprintf (stderr, "WARNING: " __VA_ARGS__)
+#define pr_info(...)	fprintf (stderr, "INFO: " __VA_ARGS__)

So, there are already several message helper functions in dtc. They're not all that consistently applied, so I can see an argument for cleaning things up, and replacing them with ones modelled on the kernel versions like these.

But.. this patch doesn't do that, it just adds yet another variant to the mix.

In addition, what we want for messages in dtc is a bit different from the kernel - the kernel's expexted to keep running, and the messages log events of interest. For dtc, for each warning/error you should also be deciding whether that should terminate the program or not - these helpers don't address that.

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
#7 lore raw

Re: [PATCH RFC 2/5] dtc: livetree: Add more tree parsing helpers

In reply to a message not in the archive
Quoted 68 lines · Tomasz Figa

This patch extends the set of parsing helpers available in dtc with string and phandle parsing.

---
 dtc.h      |  28 ++++++++
 livetree.c | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 258 insertions(+)
dtc.h +28 -0
index e95bed7..9ce9d12 100644
@@ -163,6 +163,13 @@ struct node {
 	struct label *labels;
 };
 
+#define MAX_PHANDLE_ARGS 16
+struct of_phandle_args {
+	struct node *np;
+	int args_count;
+	uint32_t args[MAX_PHANDLE_ARGS];
+};
+
 #define for_each_label_withdel(l0, l) \
 	for ((l) = (l0); (l); (l) = (l)->next)
 
@@ -184,6 +191,11 @@ struct node {
 	for_each_child_withdel(n, c) \
 		if (!(c)->deleted)
 
+#define for_each_propval_string(p, s)	\
+	for (s = propval_next_string(p, NULL);		\
+		s;					\
+		s = propval_next_string(p, s))
+
 void add_label(struct label **labels, char *label);
 void delete_labels(struct label **labels);
 
@@ -208,6 +220,22 @@ void delete_node(struct node *node);
 const char *get_unitname(struct node *node);
 struct property *get_property(struct node *node, const char *propname);
 cell_t propval_cell(struct property *prop);
+int propval_string_count(struct node *np, struct property *prop);
+const char *propval_next_string(struct property *prop, const char *cur);
+int propval_match_string(struct property *prop, const char *string);
+struct node *propval_parse_phandle(struct node *root,
+				     struct property *prop, int index);
+int propval_parse_phandle_with_args(struct node *root,
+				    struct property *prop,
+				    const char *cells_name, int index,
+				    struct of_phandle_args *out_args);
+int propval_parse_phandle_with_fixed_args(struct node *root,
+					  struct property *prop, int cell_count,
+					  int index,
+					  struct of_phandle_args *out_args);
+int propval_count_phandle_with_args(struct node *root,
+				    struct property *prop,
+				    const char *cells_name);
 struct property *get_property_by_label(struct node *tree, const char *label,
 				       struct node **node);
 struct marker *get_marker_label(struct node *tree, const char *label,
livetree.c +0 -0
index b61465f..015ed06 100644
@@ -377,6 +377,230 @@ cell_t propval_cell(struct property *prop)
 	return fdt32_to_cpu(*((cell_t *)prop->val.val));

livetree.c cell_t propval_cell(struct property *prop) · line 387

Quoted 10 lines
livetree.c +8 -0
 }
 
+int propval_string_count(struct node *np, struct property *prop)
+{
+	int i = 0;
+	size_t l = 0, total = 0;
+	const char *p;
+
+	if (!prop)
+		return -EINVAL;

1) using Unix error codes as return values is a convention which is not currently used anywhere in dtc. Introducing it in the middle of other changes is not a good idea.

2) Surely calling this on a NULL property is a bug in the caller, so it should be an assert().

livetree.c cell_t propval_cell(struct property *prop) · line 389

Quoted 2 lines
livetree.c +2 -0
+	if (!prop->val.val)
+		return -ENODATA;

An empty property is a valid stringlist with 0 entries, so this should return 0 not an error.

Quoted 72 lines
livetree.c +72 -0
+	if (strnlen(prop->val.val, prop->val.len) >= prop->val.len)
+		return -EILSEQ;
+
+	p = prop->val.val;
+
+	for (i = 0; total < prop->val.len; total += l, p += l, i++)
+		l = strlen(p) + 1;
+
+	return i;
+}
+
+const char *propval_next_string(struct property *prop, const char *cur)
+{
+	const char *curv = cur;
+
+	if (!prop)
+		return NULL;
+
+	if (!cur)
+		return prop->val.val;
+
+	curv += strlen(cur) + 1;
+	if (curv >= prop->val.val + prop->val.len)
+		return NULL;
+
+	return curv;
+}
+
+int propval_match_string(struct property *prop, const char *string)
+{
+	size_t l;
+	int i;
+	const char *p, *end;
+
+	if (!prop)
+		return -EINVAL;
+	if (!prop->val.val)
+		return -ENODATA;
+
+	p = prop->val.val;
+	end = p + prop->val.len;
+
+	for (i = 0; p < end; i++, p += l) {
+		l = strlen(p) + 1;
+		if (p + l > end)
+			return -EILSEQ;
+		if (strcmp(string, p) == 0)
+			return i; /* Found it; return index */
+	}
+	return -ENODATA;
+}
+
+static int parse_phandle_with_args(struct node *root,
+					struct property *prop,
+					const char *cells_name,
+					int cell_count, int index,
+					struct of_phandle_args *out_args)
+{
+	const cell_t *list, *list_end;
+	int rc = 0, size, cur_index = 0;
+	uint32_t count = 0;
+	struct node *node = NULL;
+	cell_t phandle;
+
+	/* Retrieve the phandle list property */
+	size = prop->val.len;
+	list = (const cell_t *)prop->val.val;
+	if (!list)
+		return -ENOENT;
+	list_end = list + size / sizeof(*list);
+
+	/* Loop over the phandles until all the requested entry is found */

livetree.c cell_t propval_cell(struct property *prop) · line 471

Quoted 10 lines
livetree.c +10 -0
+	while (list < list_end) {
+		rc = -EINVAL;
+		count = 0;
+
+		/*
+		 * If phandle is 0, then it is an empty entry with no
+		 * arguments.  Skip forward to the next entry.
+		 */
+		phandle = fdt32_to_cpu(*list++);
+		if (phandle) {

Using continue would avoid extraneous indentation and make this easier to read.

Quoted 50 lines
livetree.c +50 -0
+			/*
+			 * Find the provider node and parse the #*-cells
+			 * property to determine the argument length.
+			 *
+			 * This is not needed if the cell count is hard-coded
+			 * (i.e. cells_name not set, but cell_count is set),
+			 * except when we're going to return the found node
+			 * below.
+			 */
+			if (cells_name || cur_index == index) {
+				node = get_node_by_phandle(root, phandle);
+				if (!node) {
+					pr_err("could not find phandle %u\n",
+						phandle);
+					goto err;
+				}
+			}
+
+			if (cells_name) {
+				struct property *cells_prop;
+
+				cells_prop = get_property(node, cells_name);
+				if (!cells_prop) {
+					pr_err("could not get %s for %s\n",
+						cells_name,
+						node->fullpath);
+					goto err;
+				}
+
+				count = propval_cell(cells_prop);
+			} else {
+				count = cell_count;
+			}
+
+			/*
+			 * Make sure that the arguments actually fit in the
+			 * remaining property data length
+			 */
+			if (list + count > list_end) {
+				pr_err("arguments longer than property\n");
+				goto err;
+			}
+		}
+
+		/*
+		 * All of the error cases above bail out of the loop, so at
+		 * this point, the parsing is successful. If the requested
+		 * index matches, then fill the out_args structure and return,
+		 * or return -ENOENT for an empty entry.
+		 */

livetree.c cell_t propval_cell(struct property *prop) · line 531

Quoted 10 lines
livetree.c +10 -0
+		rc = -ENOENT;
+		if (cur_index == index) {
+			if (!phandle)
+				goto err;
+
+			if (out_args) {
+				int i;
+				if (count > MAX_PHANDLE_ARGS) {
+					pr_warn("argument count higher than MAX_PHANDLE_ARGS\n");
+					count = MAX_PHANDLE_ARGS;

That's more than a warn. If you've run out of space here that pretty much has to be a fatal error.

Quoted 8 lines
livetree.c +8 -0
+				}
+				out_args->np = node;
+				out_args->args_count = count;
+				for (i = 0; i < count; i++)
+					out_args->args[i] = fdt32_to_cpu(*list++);
+			}
+
+			/* Found it! return success */

livetree.c cell_t propval_cell(struct property *prop) · line 549

Quoted 10 lines
livetree.c +10 -0
+			return 0;
+		}
+
+		node = NULL;
+		list += count;
+		cur_index++;
+	}
+
+	/*
+	 * Unlock node before returning result; will be one of:

Hrm... looks like a comment suffering from copy/paste without updating for the new context.

Quoted 6 lines
livetree.c +6 -0
+	 * -ENOENT : index is for empty phandle
+	 * -EINVAL : parsing error on data
+	 * [1..n]  : Number of phandle (count mode; when index = -1)
+	 */
+	rc = index < 0 ? cur_index : -ENOENT;
+ err:

livetree.c cell_t propval_cell(struct property *prop) · line 565

Quoted 10 lines
livetree.c +10 -0
+	return rc;
+}
+
+struct node *propval_parse_phandle(struct node *root,
+				     struct property *prop, int index)
+{
+	struct of_phandle_args args;
+
+	if (index < 0)
+		return NULL;

Again, this is a bug in the caller; should be an assert().

Quoted 42 lines
livetree.c +38 -0
+
+	if (parse_phandle_with_args(root, prop, NULL, 0,
+					 index, &args))
+		return NULL;
+
+	return args.np;
+}
+
+int propval_parse_phandle_with_args(struct node *root,
+				    struct property *prop,
+				    const char *cells_name, int index,
+				    struct of_phandle_args *out_args)
+{
+	if (index < 0)
+		return -EINVAL;
+	return parse_phandle_with_args(root, prop, cells_name, 0,
+						index, out_args);
+}
+
+int propval_parse_phandle_with_fixed_args(struct node *root,
+					  struct property *prop, int cell_count,
+					  int index,
+					  struct of_phandle_args *out_args)
+{
+	if (index < 0)
+		return -EINVAL;
+	return parse_phandle_with_args(root, prop, NULL, cell_count,
+					   index, out_args);
+}
+
+int propval_count_phandle_with_args(struct node *root,
+				    struct property *prop,
+				    const char *cells_name)
+{
+	return parse_phandle_with_args(root, prop, cells_name, 0, -1,
+						NULL);
+}
+
 struct property *get_property_by_label(struct node *tree, const char *label,
 				       struct node **node)
 {

livetree.c struct node *get_node_by_path(struct node *tree, const char *path) · line 687

Quoted 8 lines
livetree.c +5 -0
@@ -456,6 +680,12 @@ struct node *get_node_by_path(struct node *tree, const char *path)
 	while (path[0] == '/')
 		path++;
 
+	if (!(*path)) {
+		if (tree->deleted)
+			return NULL;
+		return tree;
+	}

This change needs a justification.

Quoted 3 lines
livetree.c +0 -0
 	p = strchr(path, '/');
 
 	for_each_child(tree, child) {
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
#10 lore raw

Re: [PATCH RFC 3/5] Implement DT schema checker using hybrid approach

In reply to a message not in the archive
Quoted 92 lines · Tomasz Figa

This patch adds a proof of concept framework to implement schema checker using a combined C and DTSS based approach.

Complex and generic bindings can be implemented directly in C and then instantiated from simple device-specific bindings using DTS-like DTSS language.

This is based on Stephen Warren's C based DT schema checker proof of concept patch.

[original C based DT schema checker proof of concept]
[further development into hybrid solution]
---
 Makefile         |   2 +-
 Makefile.dtc     |   5 +-
 checks.c         |  15 +++
 dtc.c            |  17 ++-
 dtc.h            |  26 +++++
 dtss-lexer.l     | 291 +++++++++++++++++++++++++++++++++++++++++++++++
 dtss-parser.y    | 341 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 schemas/schema.c | 311 ++++++++++++++++++++++++++++++++++++++++++++++++++
 schemas/schema.h |  89 +++++++++++++++
 srcpos.h         |   2 +
 treesource.c     |  22 ++++
 11 files changed, 1117 insertions(+), 4 deletions(-)
 create mode 100644 dtss-lexer.l
 create mode 100644 dtss-parser.y
 create mode 100644 schemas/schema.c
 create mode 100644 schemas/schema.h
Makefile +1 -1
index 86f5ab3..0625fb8 100644
@@ -15,7 +15,7 @@ EXTRAVERSION =
 LOCAL_VERSION =
 CONFIG_LOCALVERSION =
 
-CPPFLAGS = -I libfdt -I .
+CPPFLAGS = -I libfdt -I . -I schemas
 WARNINGS = -Werror -Wall -Wpointer-arith -Wcast-qual -Wnested-externs \
 	-Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -Wshadow
 CFLAGS = -g -Os -fPIC -Werror $(WARNINGS)
Makefile.dtc +3 -2
index bece49b..bf19564 100644
@@ -12,7 +12,8 @@ DTC_SRCS = \
 	livetree.c \
 	srcpos.c \
 	treesource.c \
-	util.c
+	util.c \
+	schemas/schema.c
 
-DTC_GEN_SRCS = dtc-lexer.lex.c dtc-parser.tab.c
+DTC_GEN_SRCS = dtc-lexer.lex.c dtc-parser.tab.c dtss-lexer.lex.c dtss-parser.tab.c
 DTC_OBJS = $(DTC_SRCS:%.c=%.o) $(DTC_GEN_SRCS:%.c=%.o)
checks.c +13 -0
index 47eda65..7c85fcf 100644
@@ -19,6 +19,7 @@
  */
 
 #include "dtc.h"
+#include "schemas/schema.h"
 
 #ifdef TRACE_CHECKS
 #define TRACE(c, ...) \
@@ -651,6 +652,18 @@ static void check_obsolete_chosen_interrupt_controller(struct check *c,
 }
 TREE_WARNING(obsolete_chosen_interrupt_controller, NULL);
 
+/*
+ * Schema checks
+ */
+
+static void check_schema(struct check *c, struct node *dt,
+				       struct node *node)
+{
+	if (schema_check_node(dt, node))
+		FAIL(c, "Schema check failed for %s", node->fullpath);
+}
+NODE_ERROR(schema, NULL);
+
 static struct check *check_table[] = {
 	&duplicate_node_names, &duplicate_property_names,
 	&node_name_chars, &node_name_format, &property_name_chars,

checks.c static struct check *check_table[] = { · line 688

Quoted 7 lines
checks.c +2 -0
@@ -669,6 +682,8 @@ static struct check *check_table[] = {
 	&avoid_default_addr_size,
 	&obsolete_chosen_interrupt_controller,
 
+	&schema,
+
 	&always_fail,
 };

Blech. The whole point of the checks infrastructure is to keep track of checks for different possible problems in the tree. It has stuff to handle possible interdependencies, and to keep checking what it can, even if some checks have already failed.

And you throw that all away by handling all the schemas in a single check. Instead, each schema should be loaded as a separate check. This also makes the C/schema hybrid approach much more natural, as there's no artificial distinction between a schema/check implemented directly in C and one loaded from a script/schema parser.

Quoted 38 lines
dtc.c +4 -1
index d36ccdc..1a5913b 100644
@@ -20,6 +20,7 @@
 
 #include "dtc.h"
 #include "srcpos.h"
+#include "schemas/schema.h"
 
 /*
  * Command line options
@@ -49,7 +50,7 @@ static void fill_fullpaths(struct node *tree, const char *prefix)
 
 /* Usage related data. */
 static const char usage_synopsis[] = "dtc [options] <input file>";
-static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:fb:i:H:sW:E:hv";
+static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:fb:i:H:sW:E:x:hv";
 static struct option const usage_long_opts[] = {
 	{"quiet",            no_argument, NULL, 'q'},
 	{"in-format",         a_argument, NULL, 'I'},
@@ -67,6 +68,7 @@ static struct option const usage_long_opts[] = {
 	{"phandle",           a_argument, NULL, 'H'},
 	{"warning",           a_argument, NULL, 'W'},
 	{"error",             a_argument, NULL, 'E'},
+	{"schema",            a_argument, NULL, 'x'},
 	{"help",             no_argument, NULL, 'h'},
 	{"version",          no_argument, NULL, 'v'},
 	{NULL,               no_argument, NULL, 0x0},
@@ -97,6 +99,7 @@ static const char * const usage_opts_help[] = {
 	 "\t\tboth   - Both \"linux,phandle\" and \"phandle\" properties",
 	"\n\tEnable/disable warnings (prefix with \"no-\")",
 	"\n\tEnable/disable errors (prefix with \"no-\")",
+	"\n\tUse schema file"
 	"\n\tPrint this help and exit",
 	"\n\tPrint version and exit",
 	NULL,

dtc.c static const char * const usage_opts_help[] = { · line 111

Quoted 4 lines
dtc.c +1 -0
@@ -105,10 +108,12 @@ static const char * const usage_opts_help[] = {
 int main(int argc, char *argv[])
 {
 	struct boot_info *bi;
+	struct boot_info *bi_schema;

Really? Most of the boot_info structure makes no sense when parsing schemas rather than an actual device tree. Even if you're trying to use a common parser (which I think is a mistake), then at least implement a different wrapper which uses an appropriate output type.

Quoted 94 lines
dtc.c +11 -0
 	const char *inform = "dts";
 	const char *outform = "dts";
 	const char *outname = "-";
 	const char *depname = NULL;
+	const char *schema = NULL;
 	bool force = false, sort = false;
 	const char *arg;
 	int opt;
@@ -185,6 +190,10 @@ int main(int argc, char *argv[])
 			parse_checks_option(false, true, optarg);
 			break;
 
+		case 'x':
+			schema = optarg;
+			break;
+
 		case 'h':
 			usage(NULL);
 		default:
@@ -220,6 +229,12 @@ int main(int argc, char *argv[])
 	else
 		die("Unknown input format \"%s\"\n", inform);
 
+	if (schema) {
+		bi_schema = schema_from_source(schema);
+		//dt_to_source(stdout, bi_schema);
+		build_schema_list(bi_schema);
+	}
+
 	if (depfile) {
 		fputc('\n', depfile);
 		fclose(depfile);
dtc.h +26 -0
index 9ce9d12..19d2d24 100644
@@ -135,21 +135,43 @@ struct label {
 	struct label *next;
 };
 
+enum {
+	PROPERTY_DATA,
+	PROPERTY_USE,
+	PROPERTY_REQUIRE,
+	PROPERTY_MATCH,
+};
+
+#define PROPERTY_FLAG_OPTIONAL	(1 << 0)
+
 struct property {
 	bool deleted;
 	char *name;
 	struct data val;
+	unsigned int type;
+	unsigned int flags;
 
 	struct property *next;
 
 	struct label *labels;
 };
 
+enum {
+	NODE_DATA,
+	NODE_USE,
+	NODE_REQUIRE,
+};
+
+#define NODE_FLAG_OPTIONAL	(1 << 0)
+#define NODE_FLAG_INCOMPLETE	(1 << 1)
+
 struct node {
 	bool deleted;
 	char *name;
 	struct property *proplist;
 	struct node *children;
+	int type;
+	unsigned int flags;
 
 	struct node *parent;
 	struct node *next_sibling;
@@ -297,4 +319,8 @@ struct boot_info *dt_from_source(const char *f);
 
 struct boot_info *dt_from_fs(const char *dirname);
 
+/* Schema source */
+
+struct boot_info *schema_from_source(const char *fname);
+
 #endif /* _DTC_H */
dtss-lexer.l new file +0 -0
new file mode 100644
index 0000000..aee41f6

[snip]

dtss-parser.y

Quoted 5 lines
dtss-parser.y new file +0 -0
new file mode 100644
index 0000000..1c807da

[snip]

My mistake, there is a new lexer and parser, so there's no excuse for the duplicated boot_info structure above. Or for the tortured schema syntax to make it look kind of like a device tree itself.

Quoted 343 lines
schemas/schema.c new file +311 -0
new file mode 100644
index 0000000..e5258cf
@@ -0,0 +1,311 @@
+/*
+ * Copyright (C) 2013 Stephen Warren <[email protected]>
+ *
+ * Copyright (C) 2014 Samsung Electronics Co., Ltd.
+ *	Tomasz Figa <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ *  This program 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
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+ *                                                                   USA
+ */
+
+#include "schema.h"
+
+static struct schema_checker schema_list = {
+	.next = &schema_list,
+};
+
+int schema_check_node(struct node *root, struct node *node)
+{
+	const struct schema_checker *checker;
+	int match;
+	int checked = 0;
+
+	checker = schema_list.next;
+	for (; checker != &schema_list; checker = checker->next) {
+		match = checker->matchfn(node, checker);
+		if (!match)
+			continue;
+
+		pr_info("Node %s matches checker %s at level %d\n",
+			node->fullpath, checker->name, match);
+
+		checker->checkfn(root, node, checker);
+		checked = 1;
+	}
+
+	/*
+	 * FIXME: this is too noisy right now. Make it optional until schemas
+	 * for most bindings are implemented.
+	 */
+	if (!checked) {
+		pr_warn("no schema for node %s\n", node->fullpath);
+		return 0;
+	}
+
+	/*
+	 * FIXME: grab validation state from global somewhere.
+	 * Using global state avoids having check return values after every
+	 * function call, thus making the code less verbose and appear more
+	 * assertion-based.
+	 */
+	return 0;
+}
+
+int schema_match_path(struct node *node, const struct schema_checker *checker)
+{
+	return !strcmp(node->fullpath, checker->u.path.path);
+}
+
+int schema_match_compatible(struct node *node,
+				const struct schema_checker *checker)
+{
+	struct property *compat_prop;
+	int index;
+	const char *node_compat;
+	const char **test_compats;
+
+	compat_prop = get_property(node, "compatible");
+	if (!compat_prop)
+		return 0;
+
+	/*
+	 * Match with any compatible value of schema with any compatible
+	 * value of node being verified.
+	 */
+	for (node_compat = compat_prop->val.val, index = 0;
+			*node_compat;
+			node_compat += strlen(node_compat) + 1, index++) {
+		for (test_compats = checker->u.compatible.compats;
+				*test_compats; test_compats++) {
+			if (!strcmp(node_compat, *test_compats))
+				return 1;
+		}
+	}
+
+	return 0;
+}
+
+struct property *schema_get_param(struct node *params, const char *name)
+{
+	if (!params)
+		return NULL;
+
+	return get_property(params, name);
+}
+
+int schema_get_param_cell(struct node *params, const char *name, cell_t *val)
+{
+	struct property *prop;
+
+	prop = schema_get_param(params, name);
+	if (!prop)
+		return -ENOENT;
+
+	if (!prop->val.len)
+		return -EINVAL;
+
+	*val = propval_cell(prop);
+	return 0;
+}
+
+struct property *require_property(struct node *node, const char *name)
+{
+	struct property *prop;
+
+	prop = get_property(node, name);
+	if (!prop) {
+		/*
+		 * FIXME: set global error state. The same comment applies
+		 * everywhere.
+		 */
+		pr_err("node '%s' missing '%s' property\n",
+			node->fullpath, name);
+	}
+
+	return prop;
+}
+
+static void check_required_property(struct node *node, struct property *schema)
+{
+	struct property *prop;
+
+	prop = require_property(node, schema->name);
+	if (!prop)
+		return;
+
+	if (schema->val.len
+	    && (schema->val.len != prop->val.len
+	    || memcmp(schema->val.val, prop->val.val, prop->val.len)))
+		pr_err("node %s with wrong constant value of property %s\n",
+			node->fullpath, schema->name);
+}
+
+static void check_optional_property(struct node *node, struct property *schema)
+{
+	struct property *prop;
+
+	prop = get_property(node, schema->name);
+	if (!prop)
+		return;
+
+	check_required_property(node, schema);
+}
+
+/*
+ * FIXME: Use a more generic solution, which does not rely on linker
+ * specific features.
+ */
+extern const struct generic_schema __start_generic_schemas;
+extern const struct generic_schema __stop_generic_schemas;
+
+static void check_generic_schema(struct node *root, struct node *node,
+					const char *name,
+					struct node *schema_node,
+					bool required)
+{
+	const struct generic_schema *gs;
+	int i;
+	bool checked = false;
+	unsigned int count = &__stop_generic_schemas - &__start_generic_schemas;
+
+	pr_info("running schema \"%s\"\n", name);
+
+	gs = &__start_generic_schemas;
+	for (i = 0; i < count; ++i, ++gs) {
+		if (strcmp(gs->name, name))
+			continue;
+
+		gs->checkfn(gs, root, node, schema_node, required);
+
+		checked = true;
+	}
+
+	if (!checked)
+		pr_err("schema \"%s\" not found\n", name);
+}
+
+static void check_dtss_schema(struct node *root, struct node *node,
+			      const struct schema_checker *checker)
+{
+	struct property *prop_schema;
+	struct node *schema = checker->node, *node_schema;
+
+	for_each_property(schema, prop_schema) {
+		if (!strcmp(prop_schema->name, "compatible")
+		    || !strcmp(prop_schema->name, "device_type"))
+			continue;
+
+		switch (prop_schema->type) {
+		case PROPERTY_DATA:
+			if (prop_schema->flags & PROPERTY_FLAG_OPTIONAL)
+				check_optional_property(node, prop_schema);
+			else
+				check_required_property(node, prop_schema);
+			break;
+
+		case PROPERTY_REQUIRE:
+			check_generic_schema(root, node, prop_schema->name,
+						NULL, true);
+			break;
+
+		case PROPERTY_USE:
+			check_generic_schema(root, node, prop_schema->name,
+						NULL, false);
+			break;
+		}
+	}
+
+	for_each_child(schema, node_schema) {
+		switch (node_schema->type) {
+		case NODE_DATA:
+			/* TODO: verify subnodes */
+			break;
+
+		case NODE_REQUIRE:
+			check_generic_schema(root, node, node_schema->name,
+						node_schema, true);
+			break;
+
+		case NODE_USE:
+			check_generic_schema(root, node, node_schema->name,
+						node_schema, false);
+			break;
+		}
+	}
+
+	/* TODO: detect unknown properties */
+}
+
+void build_schema_list(struct boot_info *schema_tree)
+{
+	struct node *root, *schema;
+
+	root = get_node_by_path(schema_tree->dt, "/");
+	if (!root) {
+		pr_err("schema file missing / node\n");
+		return;
+	}
+
+	for_each_child(root, schema) {
+		struct schema_checker *sc = xmalloc(sizeof(*sc));
+		struct property *prop;
+
+		sc->node = schema;
+		sc->checkfn = check_dtss_schema;
+		sc->name = schema->name;
+
+		for_each_property(schema, prop)
+			if (prop->type == PROPERTY_MATCH)
+				goto found_match;
+
+		pr_err("schema '%s' without matching key\n", sc->name);
+		free(sc);
+		continue;
+
+found_match:
+		if (!strcmp(prop->name, "compatible")) {
+			int count;
+			const char **compats;
+			const char *compat;
+
+			count = propval_string_count(schema, prop) + 1;
+			compats = xmalloc(count * sizeof(*compats));
+
+			sc->u.compatible.compats = compats;
+
+			while ((compat = propval_next_string(prop, compat))) {
+				*compats = compat;
+				++compats;
+			}
+			*compats = NULL;
+
+			sc->matchfn = schema_match_compatible;
+			sc->next = schema_list.next;
+			schema_list.next = sc;
+		} else if (!strcmp(prop->name, "device_type")) {
+			sc->u.type.type = propval_next_string(prop, NULL);
+			sc->next = schema_list.next;
+			schema_list.next = sc;
+		} else if (!strcmp(prop->name, "path")) {
+			sc->u.path.path = propval_next_string(prop, NULL);
+			sc->matchfn = schema_match_path;
+			sc->next = schema_list.next;
+			schema_list.next = sc;
+		} else {
+			pr_err("wrong schema key type\n");
+			free(sc);
+		}
+	}
+}
schemas/schema.h new file +20 -0
new file mode 100644
index 0000000..9972a17
@@ -0,0 +1,89 @@
+#ifndef _SCHEMAS_SCHEMA_H
+#define _SCHEMAS_SCHEMA_H
+
+#include "dtc.h"
+
+struct schema_checker;
+
+typedef int (schema_matcher_func)(struct node *node,
+					const struct schema_checker *checker);
+typedef void (schema_checker_func)(struct node *root, struct node *node,
+					const struct schema_checker *checker);
+
+struct schema_checker {
+	const char *name;
+	schema_matcher_func *matchfn;
+	schema_checker_func *checkfn;
+	struct node *node;
+	union {
+		struct {
+			const char *path;

schemas/schema.h line 30

Quoted 10 lines
schemas/schema.h new file +10 -0
+		} path;
+		struct {
+			const char **compats;
+		} compatible;
+		struct {
+			const char *type;
+		} type;
+	} u;
+	struct schema_checker *next;
+};

AFAICT all the above is only used internally in schema.c so there's no need for it to be in a header.

Quoted 113 lines
schemas/schema.h new file +59 -0
+
+int schema_check_node(struct node *root, struct node *node);
+
+int schema_match_path(struct node *node, const struct schema_checker *checker);
+int schema_match_compatible(struct node *node,
+				const struct schema_checker *checker);
+
+#define SCHEMA_MATCH_PATH(_name_, _path_) \
+	struct schema_checker schema_checker_##_name_ = { \
+		.name = #_name_, \
+		.matchfn = schema_match_path, \
+		.checkfn = checkfn_##_name_, \
+		.u.path.path = _path_, \
+	};
+
+#define SCHEMA_MATCH_COMPATIBLE(_name_) \
+	struct schema_checker schema_checker_##_name_ = { \
+		.name = #_name_, \
+		.matchfn = schema_match_compatible, \
+		.checkfn = checkfn_##_name_, \
+		.u.compatible.compats = compats_##_name_, \
+	};
+
+struct generic_schema;
+
+typedef void (generic_schema_checker_func)(const struct generic_schema *schema,
+					   struct node *root, struct node *node,
+					   struct node *params, bool required);
+
+struct generic_schema {
+	const char *name;
+	generic_schema_checker_func *checkfn;
+};
+
+#define __used		__attribute__((__used__))
+#define __section(S)	__attribute__ ((__section__(#S)))
+
+#define GENERIC_SCHEMA(_dt_name_, _name_)				\
+	static const struct generic_schema generic_schema_##_name_	\
+		__used __section(generic_schemas) = {			\
+			.name = _dt_name_,				\
+			.checkfn = generic_checkfn_##_name_,		\
+	};
+
+struct property *require_property(struct node *node, const char *propname);
+struct property *schema_get_param(struct node *params, const char *name);
+int schema_get_param_cell(struct node *params, const char *name, cell_t *val);
+
+void build_schema_list(struct boot_info *schema_tree);
+
+#define schema_err(s,fmt,args...)	pr_err("%s: " fmt, (s)->name, ##args)
+#define schema_warn(s,fmt,args...)	pr_warn("%s: " fmt, (s)->name, ##args)
+#define schema_info(s,fmt,args...)	pr_info("%s: " fmt, (s)->name, ##args)
+
+#define node_err(n,fmt,args...)		pr_err("%s: " fmt, (n)->fullpath, ##args)
+#define node_warn(n,fmt,args...)	pr_warn("%s: " fmt, (n)->fullpath, ##args)
+#define node_info(n,fmt,args...)	pr_info("%s: " fmt, (n)->fullpath, ##args)
+
+#endif
srcpos.h +2 -0
index f81827b..b761522 100644
@@ -75,7 +75,9 @@ struct srcpos {
     struct srcfile_state *file;
 };
 
+#ifndef YYLTYPE
 #define YYLTYPE struct srcpos
+#endif
 
 #define YYLLOC_DEFAULT(Current, Rhs, N)						\
 	do {									\
treesource.c +22 -0
index bf7a626..e50285c 100644
@@ -25,6 +25,10 @@ extern FILE *yyin;
 extern int yyparse(void);
 extern YYLTYPE yylloc;
 
+extern FILE *dtss_yyin;
+extern int dtss_yyparse(void);
+extern YYLTYPE dtss_yylloc;
+
 struct boot_info *the_boot_info;
 bool treesource_error;
 
@@ -46,6 +50,24 @@ struct boot_info *dt_from_source(const char *fname)
 	return the_boot_info;
 }
 
+struct boot_info *schema_from_source(const char *fname)
+{
+	the_boot_info = NULL;
+	treesource_error = false;
+
+	srcfile_push(fname);
+	dtss_yyin = current_srcfile->f;
+	dtss_yylloc.file = current_srcfile;
+
+	if (dtss_yyparse() != 0)
+		die("Unable to parse input tree\n");
+
+	if (treesource_error)
+		die("Syntax error parsing input tree\n");
+
+	return the_boot_info;
+}
+
 static void write_prefix(FILE *f, int level)
 {
 	int i;
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
#13 lore raw
In reply to a message not in the archive
Quoted 8 lines · Tomasz Figa

This series adds a proof of concept framework to implement schema checker using a combined C and DTSS based approach. Several example bindings are also implemented using C and DTSS[1].

Complex and generic bindings can be implemented directly in C and then instantiated from simple device-specific bindings using DTS-like DTSS language.

Quoted 6 lines

A quick description of C part:

A new check is registered in dtc checks framework to perform schema checking of each node. Checking is done by searching specified schema set for matching schemas (by compatible, device_type or absolute path) and applying matched schemas to the node.

We already have an infrastructure for handling C schemas - the checks infrastructure itself. By all means extend/fix it where it needs it, but implementing another layer for "schemas" as a special kind of checks is silly.

[snip]

Quoted 105 lines

A quick description of DTSS part:

* DTSS is a DTS-like language for specification of simple bindings, e.g. bindings of particular devices. The basic syntax is very similar to DTS, with main elements being nodes and properties. At root level a series of nodes should be specified representing particular bindings:

	/dtss-v1/;

	binding1 {
		/* Definition of binding 1 */
	};

	binding2 {
		/* Definition of binding 2 */
	};

* Matching key for each binding can be specified using /match/ keyword:

	root-node {
		/match/ path = "/";
	};

	wlf,wm8903 {
		/match/ compatible = "wlf,wm8903";
	};

	pci-bus {
		/match/ device_type = "memory";
	};

Currently supported matches: path, compatible, device_type.

* Bindings can be specified either by listing properties they require (or can use) directly or by instantiating generic C-based bindings.

	binding {
		required-property;

		/optional/ optional-property;

		/require/ required-generic-schema;

		/use/ optional-generic-schema {
			schema-argument = <1>;
		};
	};

Generic schemas are implemented in C, as described above, and can use arguments specified in DTSS as properties. /require/ calls the schema with required=true, while /use/ with required=false.

This is based on Stephen Warren's C based DT schema checker proof of concept patch adding C-based validation[2].

TODO:
- specification of subnodes directly from DTSS,
- specification of simple property values from DTSS (cells, strings, phandles),
- reporting of unrecognized properties,
- probably many more...

[1] Device Tree Schema Source
[2] http://thread.gmane.org/gmane.linux.ports.arm.kernel/275896

Tomasz Figa (5):
dtc: Add helpers for various message levels
dtc: livetree: Add more tree parsing helpers
Another try of DT schema checker using hybrid C and DTSS based
approach
Add sample C-based generic bindings
Add sample DTS and DTSS schema

 Makefile                                  |   2 +-
 Makefile.dtc                              |  10 +-
 checks.c                                  |  15 +
 dtc.c                                     |  17 +-
 dtc.h                                     |  57 ++++
 dtss-lexer.l                              | 291 +++++++++++++++++++
 dtss-parser.y                             | 341 ++++++++++++++++++++++
 livetree.c                                | 230 +++++++++++++++
 sample.dts                                |  70 +++++
 schema.dtss                               |  86 ++++++
 schemas/clock/clock.c                     |  77 +++++
 schemas/gpio/gpio.c                       |  93 ++++++
 schemas/i2c/i2c.c                         |  42 +++
 schemas/interrupt-controller/interrupts.c | 452 ++++++++++++++++++++++++++++++
 schemas/mmio-bus.c                        |  97 +++++++
 schemas/schema.c                          | 311 ++++++++++++++++++++
 schemas/schema.h                          |  89 ++++++
 srcpos.h                                  |   2 +
 treesource.c                              |  22 ++
 19 files changed, 2300 insertions(+), 4 deletions(-)
 create mode 100644 dtss-lexer.l
 create mode 100644 dtss-parser.y
 create mode 100644 sample.dts
 create mode 100644 schema.dtss
 create mode 100644 schemas/clock/clock.c
 create mode 100644 schemas/gpio/gpio.c
 create mode 100644 schemas/i2c/i2c.c
 create mode 100644 schemas/interrupt-controller/interrupts.c
 create mode 100644 schemas/mmio-bus.c
 create mode 100644 schemas/schema.c
 create mode 100644 schemas/schema.h
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
#14 lore raw
In reply to a message not in the archive

Hi,

Just wanted to add that you can also find the code in my dtc tree on kernel.org:

git://git.kernel.org/pub/scm/linux/kernel/git/tfiga/dtc.git

or if you prefer web interface to quickly look through the changes:

https://git.kernel.org/cgit/linux/kernel/git/tfiga/dtc.git/

Best regards,
Tomasz

Quoted 131 lines · Tomasz Figa

This series adds a proof of concept framework to implement schema checker using a combined C and DTSS based approach. Several example bindings are also implemented using C and DTSS[1].

Complex and generic bindings can be implemented directly in C and then instantiated from simple device-specific bindings using DTS-like DTSS language.

A quick description of C part:

A new check is registered in dtc checks framework to perform schema checking of each node. Checking is done by searching specified schema set for matching schemas (by compatible, device_type or absolute path) and applying matched schemas to the node.

Schemas for complex generic bindings (such as interrupts, gpios, clocks, i2c, mmio-bus, etc.) can be implemented directly in C and instantiated from DTSS schemas of particular devices. An example C schema may look like:

static void generic_checkfn_xxx_yyy(const struct generic_schema *schema,
struct node *root, struct node *node, struct node *params, bool required)
{
/*
* Get necessary schema arguments from DTSS schema by looking
* at properties of @params node.
*
* Check whether @node node matches the schema.
*
* The @required argument may be used to check whether some error
* conditions should be ignored (e.g. unspecified interrupt). */
}
GENERIC_SCHEMA("xxx-yyy", xxx_yyy);

A quick description of DTSS part:

* DTSS is a DTS-like language for specification of simple bindings, e.g. bindings of particular devices. The basic syntax is very similar to DTS, with main elements being nodes and properties. At root level a series of nodes should be specified representing particular bindings:

	/dtss-v1/;

	binding1 {
		/* Definition of binding 1 */
	};

	binding2 {
		/* Definition of binding 2 */
	};

* Matching key for each binding can be specified using /match/ keyword:

	root-node {
		/match/ path = "/";
	};

	wlf,wm8903 {
		/match/ compatible = "wlf,wm8903";
	};

	pci-bus {
		/match/ device_type = "memory";
	};

Currently supported matches: path, compatible, device_type.

* Bindings can be specified either by listing properties they require (or can use) directly or by instantiating generic C-based bindings.

	binding {
		required-property;

		/optional/ optional-property;

		/require/ required-generic-schema;

		/use/ optional-generic-schema {
			schema-argument = <1>;
		};
	};

Generic schemas are implemented in C, as described above, and can use arguments specified in DTSS as properties. /require/ calls the schema with required=true, while /use/ with required=false.

This is based on Stephen Warren's C based DT schema checker proof of concept patch adding C-based validation[2].

TODO:
- specification of subnodes directly from DTSS,
- specification of simple property values from DTSS (cells, strings, phandles),
- reporting of unrecognized properties,
- probably many more...

[1] Device Tree Schema Source
[2] http://thread.gmane.org/gmane.linux.ports.arm.kernel/275896

Tomasz Figa (5):
dtc: Add helpers for various message levels
dtc: livetree: Add more tree parsing helpers
Another try of DT schema checker using hybrid C and DTSS based
approach
Add sample C-based generic bindings
Add sample DTS and DTSS schema

 Makefile                                  |   2 +-
 Makefile.dtc                              |  10 +-
 checks.c                                  |  15 +
 dtc.c                                     |  17 +-
 dtc.h                                     |  57 ++++
 dtss-lexer.l                              | 291 +++++++++++++++++++
 dtss-parser.y                             | 341 ++++++++++++++++++++++
 livetree.c                                | 230 +++++++++++++++
 sample.dts                                |  70 +++++
 schema.dtss                               |  86 ++++++
 schemas/clock/clock.c                     |  77 +++++
 schemas/gpio/gpio.c                       |  93 ++++++
 schemas/i2c/i2c.c                         |  42 +++
 schemas/interrupt-controller/interrupts.c | 452 ++++++++++++++++++++++++++++++
 schemas/mmio-bus.c                        |  97 +++++++
 schemas/schema.c                          | 311 ++++++++++++++++++++
 schemas/schema.h                          |  89 ++++++
 srcpos.h                                  |   2 +
 treesource.c                              |  22 ++
 19 files changed, 2300 insertions(+), 4 deletions(-)
 create mode 100644 dtss-lexer.l
 create mode 100644 dtss-parser.y
 create mode 100644 sample.dts
Quoted 9 lines
 create mode 100644 schema.dtss
 create mode 100644 schemas/clock/clock.c
 create mode 100644 schemas/gpio/gpio.c
 create mode 100644 schemas/i2c/i2c.c
 create mode 100644 schemas/interrupt-controller/interrupts.c
 create mode 100644 schemas/mmio-bus.c
 create mode 100644 schemas/schema.c
 create mode 100644 schemas/schema.h

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