[PATCH master v2 1/2] of: implement of_reparent_node helper

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jun 28 23:56:12 PDT 2023


Reparenting nodes can be a useful thing to do in fixups. Add a helper
for that.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
v1 -> v2:
  - rename from of_move_node to of_reparent_node
  - ensure that there's a parent to avoid null pointer deref
    (Sascha)
  - set node->parent to new_parent (Sascha)
  - Remove broken condition on old parent (Sascha)
---
 drivers/of/base.c           | 26 ++++++++++++++++++++++++++
 include/of.h                |  1 +
 test/self/of_manipulation.c |  8 ++++++--
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 4dc1c76b136d..e1134e9c694b 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2697,6 +2697,32 @@ void of_delete_node(struct device_node *node)
 	free(node);
 }
 
+/*
+ * of_reparent_node - Move node from beneath one parent to another
+ * @new_parent: The new parent node
+ * @node        The node to be moved
+ */
+int of_reparent_node(struct device_node *new_parent, struct device_node *node)
+{
+	if (!node)
+		return 0;
+
+	if (!new_parent || !node->parent)
+		return -EINVAL;
+
+	list_del(&node->parent_list);
+	list_del(&node->list);
+
+	free(node->full_name);
+	node->full_name = basprintf("%s/%s", new_parent->full_name, node->name);
+
+	list_add(&node->list, &new_parent->list);
+	list_add_tail(&node->parent_list, &new_parent->children);
+	node->parent = new_parent;
+
+	return 0;
+}
+
 /*
  * of_find_node_by_chosen - Find a node given a chosen property pointing at it
  * @propname:   the name of the property containing a path or alias
diff --git a/include/of.h b/include/of.h
index 92a15f5c4a13..4cc26164a703 100644
--- a/include/of.h
+++ b/include/of.h
@@ -188,6 +188,7 @@ extern struct device_node *of_create_node(struct device_node *root,
 extern void of_merge_nodes(struct device_node *np, const struct device_node *other);
 extern struct device_node *of_copy_node(struct device_node *parent,
 				const struct device_node *other);
+int of_reparent_node(struct device_node *parent, struct device_node *node);
 extern struct device_node *of_dup(const struct device_node *root);
 extern void of_delete_node(struct device_node *node);
 
diff --git a/test/self/of_manipulation.c b/test/self/of_manipulation.c
index 64913ac1eab8..8843cfbe607e 100644
--- a/test/self/of_manipulation.c
+++ b/test/self/of_manipulation.c
@@ -37,7 +37,9 @@ static void test_of_basics(struct device_node *root)
 	struct device_node *node1, *node2, *node21;
 
 	node1 = of_new_node(root, "node1");
-	node2 = of_new_node(root, "node2");
+	node2 = of_new_node(node1, "node2");
+
+	of_reparent_node(root, node2);
 
 	assert_equal(node1, node2);
 
@@ -77,7 +79,9 @@ static void test_of_property_strings(struct device_node *root)
 	np1 = of_new_node(root, "np1");
 	np2 = of_new_node(root, "np2");
 	np3 = of_new_node(root, "np3");
-	np4 = of_new_node(root, "np4");
+	np4 = of_new_node(np1, "np4");
+
+	of_reparent_node(root, np4);
 
 	of_property_sprintf(np1, "property-single", "%c%c%c", 'a', 'y', 'y');
 
-- 
2.39.2




More information about the barebox mailing list