[Skiboot] [PATCH 2/7 v3] core: Add dt_copy

Jeremy Kerr jk at ozlabs.org
Mon Aug 17 16:49:53 AEST 2015


This change adds a new function to copy a device tree node to a new
parent.

Signed-off-by: Jeremy Kerr <jk at ozlabs.org>

---
 core/device.c    |   38 ++++++++++++++++++++++++++++++++++++++
 include/device.h |    3 +++
 2 files changed, 41 insertions(+)

diff --git a/core/device.c b/core/device.c
index 807764c..483baec 100644
--- a/core/device.c
+++ b/core/device.c
@@ -152,6 +152,44 @@ struct dt_node *dt_new_2addr(struct dt_node *parent, const char *name,
 	return new;
 }
 
+static struct dt_node *__dt_copy(struct dt_node *node, struct dt_node *parent,
+		bool root)
+{
+	struct dt_property *prop, *new_prop;
+	struct dt_node *new_node, *child;
+
+	new_node = dt_new(parent, node->name);
+	if (!new_node)
+		return NULL;
+
+	list_for_each(&node->properties, prop, list) {
+		new_prop = dt_add_property(new_node, prop->name, prop->prop,
+				prop->len);
+		if (!new_prop)
+			goto fail;
+	}
+
+	list_for_each(&node->children, child, list) {
+		child = __dt_copy(child, new_node, false);
+		if (!child)
+			goto fail;
+	}
+
+	return new_node;
+
+fail:
+	/* dt_free will recurse for us, so only free when we unwind to the
+	 * top-level failure */
+	if (root)
+		dt_free(new_node);
+	return NULL;
+}
+
+struct dt_node *dt_copy(struct dt_node *node, struct dt_node *parent)
+{
+	return __dt_copy(node, parent, true);
+}
+
 char *dt_get_path(const struct dt_node *node)
 {
 	unsigned int len = 0;
diff --git a/include/device.h b/include/device.h
index b301958..a0fc280 100644
--- a/include/device.h
+++ b/include/device.h
@@ -68,6 +68,9 @@ struct dt_node *dt_new_addr(struct dt_node *parent, const char *name,
 struct dt_node *dt_new_2addr(struct dt_node *parent, const char *name,
 			     uint64_t unit_addr0, uint64_t unit_addr1);
 
+/* Copy node to new parent, including properties and subnodes */
+struct dt_node *dt_copy(struct dt_node *node, struct dt_node *parent);
+
 /* Add a property node, various forms. */
 struct dt_property *dt_add_property(struct dt_node *node,
 				    const char *name,


More information about the Skiboot mailing list