diff mbox

[06/25] dt: add helper for empty dt creation

Message ID 1338375646-15064-7-git-send-email-agraf@suse.de
State New
Headers show

Commit Message

Alexander Graf May 30, 2012, 11 a.m. UTC
We want to get rid of the concept of loading an external device tree and instead
generate our own. However, to do this we need to also create a device tree
template programatically.

This patch adds a helper to create an empty device tree in memory.

Signed-off-by: Alexander Graf <agraf@suse.de>
---
 device_tree.c |   37 +++++++++++++++++++++++++++++++++++++
 device_tree.h |    1 +
 2 files changed, 38 insertions(+), 0 deletions(-)

Comments

David Gibson May 31, 2012, 10:55 a.m. UTC | #1
On Wed, May 30, 2012 at 01:00:27PM +0200, Alexander Graf wrote:
> We want to get rid of the concept of loading an external device tree and instead
> generate our own. However, to do this we need to also create a device tree
> template programatically.
> 
> This patch adds a helper to create an empty device tree in memory.

Hm, this make-an-empty-template thing is common enough that I'll make
a helper for it in libfdt.
Alexander Graf June 5, 2012, 2:07 p.m. UTC | #2
On 31.05.2012, at 12:55, David Gibson wrote:

> On Wed, May 30, 2012 at 01:00:27PM +0200, Alexander Graf wrote:
>> We want to get rid of the concept of loading an external device tree and instead
>> generate our own. However, to do this we need to also create a device tree
>> template programatically.
>> 
>> This patch adds a helper to create an empty device tree in memory.
> 
> Hm, this make-an-empty-template thing is common enough that I'll make
> a helper for it in libfdt.

Very cool :). That would still mean that we need the in-QEMU helper for a while though, until we can reasonably expect people to have newer versions of libfdt on their systems, right? :)


Alex
David Gibson June 5, 2012, 5 p.m. UTC | #3
On Tue, Jun 05, 2012 at 04:07:23PM +0200, Alexander Graf wrote:
> 
> On 31.05.2012, at 12:55, David Gibson wrote:
> 
> > On Wed, May 30, 2012 at 01:00:27PM +0200, Alexander Graf wrote:
> >> We want to get rid of the concept of loading an external device tree and instead
> >> generate our own. However, to do this we need to also create a device tree
> >> template programatically.
> >> 
> >> This patch adds a helper to create an empty device tree in memory.
> > 
> > Hm, this make-an-empty-template thing is common enough that I'll make
> > a helper for it in libfdt.
> 
> Very cool :). That would still mean that we need the in-QEMU helper
> for a while though, until we can reasonably expect people to have
> newer versions of libfdt on their systems, right? :)

True.  The helper is in upstream libfdt git now, btw.
diff mbox

Patch

diff --git a/device_tree.c b/device_tree.c
index 6745d17..d4f1f0a 100644
--- a/device_tree.c
+++ b/device_tree.c
@@ -25,6 +25,43 @@ 
 
 #include <libfdt.h>
 
+#define FDT_MAX_SIZE  0x10000
+
+void *create_device_tree(int *sizep)
+{
+    void *fdt;
+    int ret;
+
+    *sizep = FDT_MAX_SIZE;
+    fdt = g_malloc0(FDT_MAX_SIZE);
+    ret = fdt_create(fdt, FDT_MAX_SIZE);
+    if (ret < 0) {
+        goto fail;
+    }
+    ret = fdt_begin_node(fdt, "");
+    if (ret < 0) {
+        goto fail;
+    }
+    ret = fdt_end_node(fdt);
+    if (ret < 0) {
+        goto fail;
+    }
+    ret = fdt_finish(fdt);
+    if (ret < 0) {
+        goto fail;
+    }
+    ret = fdt_open_into(fdt, fdt, *sizep);
+    if (ret) {
+        fprintf(stderr, "Unable to copy device tree in memory\n");
+        exit(1);
+    }
+
+    return fdt;
+fail:
+    fprintf(stderr, "%s Couldn't create dt: %s\n", __func__, fdt_strerror(ret));
+    exit(1);
+}
+
 void *load_device_tree(const char *filename_path, int *sizep)
 {
     int dt_size;
diff --git a/device_tree.h b/device_tree.h
index 376287a..5464dc7 100644
--- a/device_tree.h
+++ b/device_tree.h
@@ -14,6 +14,7 @@ 
 #ifndef __DEVICE_TREE_H__
 #define __DEVICE_TREE_H__
 
+void *create_device_tree(int *sizep);
 void *load_device_tree(const char *filename_path, int *sizep);
 
 int qemu_devtree_setprop(void *fdt, const char *node_path,