[PATCH 08/16] of: Add support for converting the unflattened tree back to a dtb

Sascha Hauer s.hauer at pengutronix.de
Fri Jan 11 08:24:28 EST 2013


We already have support for unflattening the devicetree. This patch
adds support for converting it back to a dtb.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/of/base.c |   64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/of.h      |    1 +
 2 files changed, 65 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index cb1989d..3307f41 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -24,6 +24,7 @@
 #include <malloc.h>
 #include <init.h>
 #include <memory.h>
+#include <sizes.h>
 #include <linux/ctype.h>
 
 /**
@@ -909,6 +910,69 @@ int of_unflatten_dtb(struct fdt_header *fdt)
 	return 0;
 }
 
+static int __of_flatten_dtb(void *fdt, struct device_node *node)
+{
+	struct property *p;
+	struct device_node *n;
+	int ret;
+
+	ret = fdt_begin_node(fdt, node->name);
+	if (ret)
+		return ret;
+
+	list_for_each_entry(p, &node->properties, list) {
+		ret = fdt_property(fdt, p->name, p->value, p->length);
+		if (ret)
+			return ret;
+	}
+
+	list_for_each_entry(n, &node->children, parent_list) {
+		ret = __of_flatten_dtb(fdt, n);
+		if (ret)
+			return ret;
+	}
+
+	ret = fdt_end_node(fdt);
+
+	return ret;
+}
+
+#define DTB_SIZE	SZ_128K
+
+void *of_flatten_dtb(void)
+{
+	void *fdt;
+	int ret;
+
+	if (!root_node)
+		return NULL;
+
+	fdt = malloc(DTB_SIZE);
+	if (!fdt)
+		return NULL;
+
+	ret = fdt_create(fdt, DTB_SIZE);
+	if (ret)
+		goto out_free;
+
+	ret = fdt_finish_reservemap(fdt);
+	if (ret)
+		goto out_free;
+
+	ret = __of_flatten_dtb(fdt, root_node);
+	if (ret)
+		goto out_free;
+
+	fdt_finish(fdt);
+
+	return fdt;
+
+out_free:
+	free(fdt);
+
+	return NULL;
+}
+
 int of_device_is_stdout_path(struct device_d *dev)
 {
 	struct device_node *dn;
diff --git a/include/of.h b/include/of.h
index 7682503..80ec447 100644
--- a/include/of.h
+++ b/include/of.h
@@ -109,6 +109,7 @@ int of_probe(void);
 int of_parse_dtb(struct fdt_header *fdt);
 void of_free(struct device_node *node);
 int of_unflatten_dtb(struct fdt_header *fdt);
+void *of_flatten_dtb(void);
 
 int of_property_read_string(struct device_node *np, const char *propname,
 				const char **out_string);
-- 
1.7.10.4




More information about the barebox mailing list