mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 2/3] of: add of_diff()
Date: Thu, 12 Sep 2019 10:35:04 +0200	[thread overview]
Message-ID: <20190912083505.4162-2-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20190912083505.4162-1-s.hauer@pengutronix.de>

of_diff compares two device trees against each other and prints a
diff-like result.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/of/base.c | 141 ++++++++++++++++++++++++++++++++++++++++++----
 include/of.h      |   1 +
 2 files changed, 132 insertions(+), 10 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 22077fa397..63e0879f06 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -1776,24 +1776,21 @@ struct device_node *of_get_child_by_name(const struct device_node *node,
 }
 EXPORT_SYMBOL(of_get_child_by_name);
 
-void of_print_nodes(struct device_node *node, int indent)
+static void __of_print_nodes(struct device_node *node, int indent, const char *prefix)
 {
 	struct device_node *n;
 	struct property *p;
-	int i;
 
 	if (!node)
 		return;
 
-	for (i = 0; i < indent; i++)
-		printf("\t");
+	if (!prefix)
+		prefix = "";
 
-	printf("%s%s\n", node->name, node->name ? " {" : "{");
+	printf("%s%*s%s%s\n", prefix, indent * 8, "", node->name, node->name ? " {" : "{");
 
 	list_for_each_entry(p, &node->properties, list) {
-		for (i = 0; i < indent + 1; i++)
-			printf("\t");
-		printf("%s", p->name);
+		printf("%s%*s%s", prefix, (indent + 1) * 8, "", p->name);
 		if (p->length) {
 			printf(" = ");
 			of_print_property(of_property_get_value(p), p->length);
@@ -1802,12 +1799,136 @@ void of_print_nodes(struct device_node *node, int indent)
 	}
 
 	list_for_each_entry(n, &node->children, parent_list) {
-		of_print_nodes(n, indent + 1);
+		__of_print_nodes(n, indent + 1, prefix);
+	}
+
+	printf("%s%*s};\n", prefix, indent * 8, "");
+}
+
+void of_print_nodes(struct device_node *node, int indent)
+{
+	__of_print_nodes(node, indent, NULL);
+}
+
+static void __of_print_property(struct property *p, int indent)
+{
+	int i;
+
+	for (i = 0; i < indent; i++)
+		printf("\t");
+
+	printf("%s", p->name);
+	if (p->length) {
+		printf(" = ");
+		of_print_property(of_property_get_value(p), p->length);
 	}
+	printf(";\n");
+}
+
+static int __of_print_parents(struct device_node *node)
+{
+	int indent, i;
+
+	if (!node->parent)
+		return 0;
+
+	indent = __of_print_parents(node->parent);
 
 	for (i = 0; i < indent; i++)
 		printf("\t");
-	printf("};\n");
+
+	printf("%s {\n", node->name);
+
+	return indent + 1;
+}
+
+static void of_print_parents(struct device_node *node, int *printed)
+{
+	if (*printed)
+		return;
+
+	__of_print_parents(node);
+
+	*printed = 1;
+}
+
+static void of_print_close(struct device_node *node, int *printed)
+{
+	int depth = 0, i, j;
+
+	if (!*printed)
+		return;
+
+	while ((node = node->parent))
+		depth++;
+
+	for (i = depth; i > 0; i--) {
+		for (j = 0; j + 1 < i; j++)
+			printf("\t");
+		printf("};\n");
+	}
+}
+
+/**
+ * of_diff - compare two device trees against each other
+ * @a: The first device tree
+ * @b: The second device tree
+ * @indent: The initial indentation level when printing
+ *
+ * This function compares two device trees against each other and prints
+ * a diff-like result.
+ */
+void of_diff(struct device_node *a, struct device_node *b, int indent)
+{
+	struct property *ap, *bp;
+	struct device_node *ca, *cb;
+	int printed = 0;
+
+	list_for_each_entry(ap, &a->properties, list) {
+		bp = of_find_property(b, ap->name, NULL);
+		if (!bp) {
+			of_print_parents(a, &printed);
+			printf("- ");
+			__of_print_property(ap, indent);
+			continue;
+		}
+
+		if (ap->length != bp->length || memcmp(of_property_get_value(ap), of_property_get_value(bp), bp->length)) {
+			of_print_parents(a, &printed);
+			printf("- ");
+			__of_print_property(ap, indent);
+			printf("+ ");
+			__of_print_property(bp, indent);
+		}
+	}
+
+	list_for_each_entry(bp, &b->properties, list) {
+		ap = of_find_property(a, bp->name, NULL);
+		if (!ap) {
+			of_print_parents(a, &printed);
+			printf("+ ");
+			__of_print_property(bp, indent);
+		}
+	}
+
+	for_each_child_of_node(a, ca) {
+		cb = of_get_child_by_name(b, ca->name);
+		if (cb) {
+			of_diff(ca, cb, indent + 1);
+		} else {
+			of_print_parents(a, &printed);
+			__of_print_nodes(ca, indent, "-");
+		}
+	}
+
+	for_each_child_of_node(b, cb) {
+		if (!of_get_child_by_name(a, cb->name)) {
+			of_print_parents(a, &printed);
+			__of_print_nodes(cb, indent, "+");
+		}
+	}
+
+	of_print_close(a, &printed);
 }
 
 struct device_node *of_new_node(struct device_node *parent, const char *name)
diff --git a/include/of.h b/include/of.h
index b5f54dd4e5..c8275e169b 100644
--- a/include/of.h
+++ b/include/of.h
@@ -104,6 +104,7 @@ void of_print_property(const void *data, int len);
 void of_print_cmdline(struct device_node *root);
 
 void of_print_nodes(struct device_node *node, int indent);
+void of_diff(struct device_node *a, struct device_node *b, int indent);
 int of_probe(void);
 int of_parse_dtb(struct fdt_header *fdt);
 struct device_node *of_unflatten_dtb(const void *fdt);
-- 
2.23.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2019-09-12  8:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-12  8:35 [PATCH 1/3] of: Fix memory hole in of_find_node_by_reproducible_name() Sascha Hauer
2019-09-12  8:35 ` Sascha Hauer [this message]
2019-09-12  8:35 ` [PATCH 3/3] add of_diff command Sascha Hauer

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=20190912083505.4162-2-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.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