[PATCH 02/18] of: Add function to duplicate a device tree

Sascha Hauer s.hauer at pengutronix.de
Thu Jun 24 01:52:07 PDT 2021


This adds of_dup() to duplicate a device tree. Previously of_copy_node()
was used for this, but of_copy_node() has issues with potentially
duplicated phandle values when the new tree is inserted to an existing
tree, that is when the parent argument of of_copy_node() is non NULL.
All users of of_copy_node() with a NULL parent argument are converted
to of_dup() which is safe to use leaving only the problematic users
of of_copy_node().

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/of_diff.c | 4 ++--
 common/oftree.c    | 2 +-
 drivers/of/base.c  | 5 +++++
 include/of.h       | 1 +
 4 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/commands/of_diff.c b/commands/of_diff.c
index ec039776cf..d9d84fd4bf 100644
--- a/commands/of_diff.c
+++ b/commands/of_diff.c
@@ -25,7 +25,7 @@ static struct device_node *get_tree(const char *filename, struct device_node *ro
 		if (!node)
 			return ERR_PTR(-ENOENT);
 
-		return of_copy_node(NULL, node);
+		return of_dup(node);
 	}
 
 	if (!strcmp(filename, "+")) {
@@ -33,7 +33,7 @@ static struct device_node *get_tree(const char *filename, struct device_node *ro
 		if (!node)
 			return ERR_PTR(-ENOENT);
 
-		node = of_copy_node(NULL, root);
+		node = of_dup(root);
 
 		of_fix_tree(node);
 
diff --git a/common/oftree.c b/common/oftree.c
index 5eaa63ad7e..da8043809b 100644
--- a/common/oftree.c
+++ b/common/oftree.c
@@ -353,7 +353,7 @@ struct fdt_header *of_get_fixed_tree(struct device_node *node)
 		if (!node)
 			return NULL;
 
-		freenp = node = of_copy_node(NULL, node);
+		freenp = node = of_dup(node);
 		if (!node)
 			return NULL;
 	}
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 1434f39ad1..5d93750aec 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2420,6 +2420,11 @@ struct device_node *of_copy_node(struct device_node *parent, const struct device
 	return np;
 }
 
+struct device_node *of_dup(const struct device_node *root)
+{
+	return of_copy_node(NULL, root);
+}
+
 void of_delete_node(struct device_node *node)
 {
 	struct device_node *n, *nt;
diff --git a/include/of.h b/include/of.h
index d67a40bd19..94eb71190d 100644
--- a/include/of.h
+++ b/include/of.h
@@ -162,6 +162,7 @@ extern struct device_node *of_create_node(struct device_node *root,
 					const char *path);
 extern struct device_node *of_copy_node(struct device_node *parent,
 				const struct device_node *other);
+extern struct device_node *of_dup(const struct device_node *root);
 extern void of_delete_node(struct device_node *node);
 
 extern const char *of_get_machine_compatible(void);
-- 
2.29.2




More information about the barebox mailing list