mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master v2 1/2] of: implement of_reparent_node helper
Date: Thu, 29 Jun 2023 08:56:12 +0200	[thread overview]
Message-ID: <20230629065612.1119622-1-a.fatoum@pengutronix.de> (raw)

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

Signed-off-by: Ahmad Fatoum <a.fatoum@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




             reply	other threads:[~2023-06-29  6:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-29  6:56 Ahmad Fatoum [this message]
2023-06-29  6:56 ` [PATCH master v2 2/2] boards: qemu-virt: support older QEMU with /soc/flash Ahmad Fatoum
2023-07-03  8:54 ` [PATCH master v2 1/2] of: implement of_reparent_node helper Sascha Hauer
2023-07-03  8:57   ` Ahmad Fatoum

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=20230629065612.1119622-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@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