Patchwork [1/2] fdt: move dumpdtb interpretation code to device_tree.c

login
register
mail settings
Submitter Alexander Graf
Date Sept. 23, 2012, 6:41 a.m.
Message ID <1348382519-2167-1-git-send-email-agraf@suse.de>
Download mbox | patch
Permalink /patch/186489/
State New
Headers show

Comments

Alexander Graf - Sept. 23, 2012, 6:41 a.m.
The dumpdtb code can be useful in more places than just for e500. Move it
to a generic place.

Signed-off-by: Alexander Graf <agraf@suse.de>
---
 device_tree.c |   22 ++++++++++++++++++++++
 device_tree.h |    2 ++
 hw/ppc/e500.c |   15 +--------------
 3 files changed, 25 insertions(+), 14 deletions(-)
Peter Maydell - Sept. 24, 2012, 6:02 p.m.
On 23 September 2012 07:41, Alexander Graf <agraf@suse.de> wrote:
> +void qemu_devtree_dumpdtb(void *fdt, int size)
> +{
> +    QemuOpts *machine_opts;
> +
> +    machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
> +    if (machine_opts) {
> +        const char *dumpdtb = qemu_opt_get(machine_opts, "dumpdtb");

...maybe we should have a utility function for
   QemuOpts *opts = qemu_opts_find(qemu_find_opts("machine"), 0);
   if (!opts) {
       return NULL;
   }
   return qemu_opt_get(opts, optname);

because it seems to be quite a common thing and it's a bit dull to have
to do that null-pointer check in every place that wants to read a
machine option.

> +        if (dumpdtb) {
> +            /* Dump the dtb to a file and quit */
> +            FILE *f = fopen(dumpdtb, "wb");

...not checking return value from fopen().

> +            size_t len;
> +            len = fwrite(fdt, size, 1, f);
> +            fclose(f);

...not checking return value from fclose() (important as it's where
we're likely to do the actual writing!).

> +            if (len != size) {
> +                exit(1);
> +            }
> +            exit(0);

Or we could let glib do the heavy lifting:

    exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);

-- PMM

Patch

diff --git a/device_tree.c b/device_tree.c
index d7a9b6b..69ca953 100644
--- a/device_tree.c
+++ b/device_tree.c
@@ -304,3 +304,25 @@  int qemu_devtree_add_subnode(void *fdt, const char *name)
     g_free(dupname);
     return retval;
 }
+
+void qemu_devtree_dumpdtb(void *fdt, int size)
+{
+    QemuOpts *machine_opts;
+
+    machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
+    if (machine_opts) {
+        const char *dumpdtb = qemu_opt_get(machine_opts, "dumpdtb");
+        if (dumpdtb) {
+            /* Dump the dtb to a file and quit */
+            FILE *f = fopen(dumpdtb, "wb");
+            size_t len;
+            len = fwrite(fdt, size, 1, f);
+            fclose(f);
+            if (len != size) {
+                exit(1);
+            }
+            exit(0);
+        }
+    }
+
+}
diff --git a/device_tree.h b/device_tree.h
index f7a3e6c..f0b3f35 100644
--- a/device_tree.h
+++ b/device_tree.h
@@ -49,4 +49,6 @@  int qemu_devtree_add_subnode(void *fdt, const char *name);
                              sizeof(qdt_tmp));                                \
     } while (0)
 
+void qemu_devtree_dumpdtb(void *fdt, int size);
+
 #endif /* __DEVICE_TREE_H__ */
diff --git a/hw/ppc/e500.c b/hw/ppc/e500.c
index 6f0de6d..5bab340 100644
--- a/hw/ppc/e500.c
+++ b/hw/ppc/e500.c
@@ -139,12 +139,10 @@  static int ppce500_load_device_tree(CPUPPCState *env,
             0x0, 0x10000,
         };
     QemuOpts *machine_opts;
-    const char *dumpdtb = NULL;
     const char *dtb_file = NULL;
 
     machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
     if (machine_opts) {
-        dumpdtb = qemu_opt_get(machine_opts, "dumpdtb");
         dtb_file = qemu_opt_get(machine_opts, "dtb");
         toplevel_compat = qemu_opt_get(machine_opts, "dt_compatible");
     }
@@ -334,18 +332,7 @@  static int ppce500_load_device_tree(CPUPPCState *env,
     }
 
 done:
-    if (dumpdtb) {
-        /* Dump the dtb to a file and quit */
-        FILE *f = fopen(dumpdtb, "wb");
-        size_t len;
-        len = fwrite(fdt, fdt_size, 1, f);
-        fclose(f);
-        if (len != fdt_size) {
-            exit(1);
-        }
-        exit(0);
-    }
-
+    qemu_devtree_dumpdtb(fdt, fdt_size);
     ret = rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
     if (ret < 0) {
         goto out;