Skip to contentSkip to conversation

add dumpdts ability to convert dtb to dts

PATCH In review john.liuli 3 participants 10 messages Aug 25, 2014 – Aug 26, 2014

lore
Share & tools
lore

john.liuli

author
#0 lore raw

cover add dumpdts ability to convert dtb to dts

From: Li Liu <[email protected]>

This patchset let qemu can convert dtb file to dts for two demands:

Some archtectures may generate the dtb file dynamically through qemu device tree functions. So this let it's possiable to dump final dtb to dts and save it as a reference.

For novices to debugging the issues caused by wrong dtb parameters. It will be easy to check the dts directly without copying the dtb which may be generated by 'dumpdtb' to the PC and dtc or fdtdump it.

The outputed dts format is compatile with 'dtc -I dtb -O dts xxx.dtb'. There's a new parameter 'dumpdts' which is similar to 'dumpdtb'. so try it like '-machine dumpdts=/tmp/xxx.dts'.

Li Liu (6):
device_tree.c: Introduce a function to check multiple strings for dts device_tree.c: dump three kind data types of dts to a file desciptor device_tree.c: Format a blob in memory as dts stream and dump to a
file.
device_tree.c: add the main function to analyse the parameter
'dumpdts'
machine: add 'dumpdts' parameter to 'machine' optslist.
arm: Allow dumping generated dtb to dts file

 device_tree.c                |  172 ++++++++++++++++++++++++++++++++++++++++++
 hw/arm/boot.c                |    1 +
 include/sysemu/device_tree.h |    1 +
 vl.c                         |    4 +
 4 files changed, 178 insertions(+)
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/6 device_tree.c: Introduce a function to check multiple strings for dts

From: Li Liu <[email protected]>

dts property's value can be multiple strings. So introduce a function to check a data with a given length to see if it is all printable and has a valid terminator. It can contain either a single string, or multiple strings each of non-zero length.

---
 device_tree.c |   32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
device_tree.c +32 -0
index ca83504..29d9acc 100644
@@ -345,3 +345,35 @@ int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
     return qemu_fdt_setprop(fdt, node_path, property, propcells,
                             cellnum * sizeof(uint32_t));
 }
+
+/*
+ * Check a data of a given length to see if it is all printable and
+ * has a valid terminator. The data can contain either a single string,
+ * or multiple strings each of non-zero length.
+ */
+static int dts_is_printable_strings(const void *data, int len)
+{
+    const char *str = (char *)data;
+    int substrs = 0;
+    int substr_len = 0;
+    int i;
+
+    for (i = 0; i < len; i++) {
+        if (str[i] == '\0') {
+            if (substr_len) {
+                substr_len = 0;
+                substrs++;
+                continue;
+            } else {
+                /* substr is empty */
+                return 0;
+            }
+        } else if (isprint(str[i])) {
+            substr_len++;
+        } else {
+            return 0;
+        }
+    }
+
+    return substrs && !substr_len;
+}
Signature · 1 line
1.7.9.5
author
#2 lore raw

PATCH 2/6 device_tree.c: dump three kind data types of dts to a file desciptor

In reply to john.liuli

From: Li Liu <[email protected]>

dump three kind data types 'strings', 'cell' and 'bytes' of dts to a file desciptor.

---
 device_tree.c |   39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
device_tree.c +39 -0
index 29d9acc..14d4015 100644
@@ -377,3 +377,42 @@ static int dts_is_printable_strings(const void *data, int len)
 
     return substrs && !substr_len;
 }
+
+static void dts_write_data(FILE *fp, const char *data, int len)
+{
+    int i;
+    const char *p = data;
+    const char *s;
+
+    if (len == 0) {
+        return;
+    }
+
+    if (dts_is_printable_strings(data, len)) {
+        fprintf(fp, " = ");
+
+        s = data;
+        do {
+            fprintf(fp, "\"%s\"", s);
+            s += strlen(s) + 1;
+            if (s < data + len) {
+                fprintf(fp, ", ");
+            }
+        } while (s < data + len);
+    } else if ((len % 4) == 0) {
+        const uint32_t *cell = (const uint32_t *)data;
+
+        fprintf(fp, " = <");
+        for (i = 0; i < len; i += 4) {
+            fprintf(fp, "0x%x%s", fdt32_to_cpu(cell[i / 4]),
+                    i < (len - 4) ? " " : "");
+        }
+        fprintf(fp, ">");
+    } else {
+        fprintf(fp, " = [");
+        for (i = 0; i < len; i++) {
+            fprintf(fp, "%02hhx%s", *p++, i < len - 1 ? " " : "");
+        }
+        fprintf(fp, "]");
+    }
+}
Signature · 1 line
1.7.9.5
author
#3 lore raw

PATCH 3/6 device_tree.c: Format a blob in memory as dts stream and dump to a file.

In reply to john.liuli

From: Li Liu <[email protected]>

The outputed stream format is compatile with
'dtc -I dtb -O dts xxx.dtb'.

---
 device_tree.c |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
device_tree.c +80 -0
index 14d4015..1e407d2 100644
@@ -416,3 +416,83 @@ static void dts_write_data(FILE *fp, const char *data, int len)
         fprintf(fp, "]");
     }
 }
+
+static void dts_write_prefix(FILE *fp, int depth)
+{
+    int i;
+
+    for (i = 0; i < depth; i++) {
+        fprintf(fp, "%c", '\t');
+    }
+}
+
+#define PALIGN(p, a) ((void *)(QEMU_ALIGN_UP((unsigned long)(p), (a))))
+#define GET_CELL(p)  (p += 4, *((const uint32_t *)(p - 4)))
+
+static void qemu_write_dts(FILE *fp, void *fdt)
+{
+    const char *p, *name, *data;
+    uint32_t tag;
+    int len, num, depth = 0;
+    int i;
+
+    const char *fdt_struct = (const char *)fdt + fdt_off_dt_struct(fdt);
+    const char *fdt_strings = (const char *)fdt + fdt_off_dt_strings(fdt);
+
+    fprintf(fp, "/dts-v1/;\n");
+
+    num = fdt_num_mem_rsv(fdt);
+    fprintf(fp, "%s", num ? "\n" : "");
+    for (i = 0; i < num; i++) {
+        uint64_t addr, size;
+
+        fdt_get_mem_rsv(fdt, i, &addr, &size);
+        fprintf(fp, "/memreserve/\t0x%016llx 0x%016llx;\n",
+                (unsigned long long)addr, (unsigned long long)size);
+    }
+
+    p = fdt_struct;
+    while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
+        if (tag == FDT_BEGIN_NODE) {
+            name = p;
+            p = PALIGN(p + strlen(name) + 1, 4);
+            if (*name == '\0') {
+                name = "/";
+            }
+
+            fprintf(fp, "\n");
+            dts_write_prefix(fp, depth);
+            fprintf(fp, "%s {\n", name);
+
+            depth++;
+            continue;
+        }
+
+        if (tag == FDT_END_NODE) {
+            depth--;
+
+            dts_write_prefix(fp, depth);
+            fprintf(fp, "};\n");
+            continue;
+        }
+
+        if (tag == FDT_NOP) {
+            continue;
+        }
+
+        if (tag != FDT_PROP) {
+            fprintf(stderr, "FDT: Unknown tag 0x%08x\n", tag);
+            break;
+        }
+
+        len = fdt32_to_cpu(GET_CELL(p));
+        name = fdt_strings + fdt32_to_cpu(GET_CELL(p));
+        data = p;
+        p = PALIGN(p + len, 4);
+
+        dts_write_prefix(fp, depth);
+        fprintf(fp, "%s", name);
+        dts_write_data(fp, data, len);
+        fprintf(fp, ";\n");
+    }
+}
Signature · 1 line
1.7.9.5
author
#4 lore raw

PATCH 4/6 device_tree.c: add the main function to analyse the parameter 'dumpdts'

In reply to john.liuli

From: Li Liu <[email protected]>

add the main function to analyse the parameter 'dumpdts' as a filename the dts will be dumped to.

---
 device_tree.c                |   21 +++++++++++++++++++++
 include/sysemu/device_tree.h |    1 +
 2 files changed, 22 insertions(+)
device_tree.c +21 -0
index 1e407d2..b9a924f 100644
@@ -496,3 +496,24 @@ static void qemu_write_dts(FILE *fp, void *fdt)
         fprintf(fp, ";\n");
     }
 }
+
+void qemu_fdt_dumpdts(void *fdt)
+{
+    FILE *fp;
+    const char *filename = qemu_opt_get(qemu_get_machine_opts(), "dumpdts");
+
+    if (!filename || !fdt) {
+        return;
+    }
+
+    fp = fopen(filename, "w");
+    if (!fp) {
+        fprintf(stderr, "Failed to open file '%s'\n", filename);
+        goto ret;
+    }
+
+    qemu_write_dts(fp, fdt);
+
+ret:
+    fclose(fp);
+}
include/sysemu/device_tree.h +1 -0
index 899f05c..27145fb 100644
@@ -50,6 +50,7 @@ int qemu_fdt_add_subnode(void *fdt, const char *name);
     } while (0)
 
 void qemu_fdt_dumpdtb(void *fdt, int size);
+void qemu_fdt_dumpdts(void *fdt);
 
 /**
  * qemu_fdt_setprop_sized_cells_from_array:
Signature · 1 line
1.7.9.5
author
#5 lore raw

PATCH 5/6 machine: add 'dumpdts' parameter to 'machine' optslist.

In reply to john.liuli

From: Li Liu <[email protected]>

---
 vl.c |    4 ++++
 1 file changed, 4 insertions(+)
vl.c +4 -0
index b796c67..853f748 100644
@@ -357,6 +357,10 @@ static QemuOptsList qemu_machine_opts = {
             .type = QEMU_OPT_STRING,
             .help = "Dump current dtb to a file and quit",
         }, {
+            .name = "dumpdts",
+            .type = QEMU_OPT_STRING,
+            .help = "Convert current dtb to dts and dump to a file",
+        },{
             .name = "phandle_start",
             .type = QEMU_OPT_NUMBER,
             .help = "The first phandle ID we may generate dynamically",
Signature · 1 line
1.7.9.5
author
#6 lore raw

PATCH 6/6 arm: Allow dumping generated dtb to dts file

In reply to john.liuli

From: Li Liu <[email protected]>

By calling qemu_fdt_dumdts before qemu_fdt_dumpdtb.

---
 hw/arm/boot.c |    1 +
 1 file changed, 1 insertion(+)
hw/arm/boot.c +1 -0
index e32f2f4..18cc8ac 100644
@@ -394,6 +394,7 @@ static int load_dtb(hwaddr addr, const struct arm_boot_info *binfo)
         binfo->modify_dtb(binfo, fdt);
     }
 
+    qemu_fdt_dumpdts(fdt);
     qemu_fdt_dumpdtb(fdt, size);
 
     cpu_physical_memory_write(addr, fdt, size);
Signature · 1 line
1.7.9.5
#7 lore raw
In reply to john.liuli
Quoted 8 lines · john.liuli

From: Li Liu <[email protected]>

This patchset let qemu can convert dtb file to dts for two demands:

Some archtectures may generate the dtb file dynamically through qemu device tree functions. So this let it's possiable to dump final dtb to dts and save it as a reference.

Quoted 8 lines

For novices to debugging the issues caused by wrong dtb parameters. It will be easy to check the dts directly without copying the dtb which may be generated by 'dumpdtb' to the PC and dtc or fdtdump it.

The outputed dts format is compatile with 'dtc -I dtb -O dts xxx.dtb'. There's a new parameter 'dumpdts' which is similar to 'dumpdtb'. so try it like '-machine dumpdts=/tmp/xxx.dts'.

Hi. Thanks for this patchset, but I'm afraid this doesn't seem to me like something that should be in QEMU.
As you say, you can easily turn the dtb blob into a source file with dtc. That gets you a definitely-correct disassembly of the blob, and we don't need to maintain a possibly-buggy reimplementation of the dtb disassembler in QEMU.

thanks
-- PMM

author
#8 lore raw
Quoted 25 lines · Peter Maydell
Quoted 8 lines · john.liuli

From: Li Liu <[email protected]>

This patchset let qemu can convert dtb file to dts for two demands:

Some archtectures may generate the dtb file dynamically through qemu device tree functions. So this let it's possiable to dump final dtb to dts and save it as a reference.

Quoted 8 lines

For novices to debugging the issues caused by wrong dtb parameters. It will be easy to check the dts directly without copying the dtb which may be generated by 'dumpdtb' to the PC and dtc or fdtdump it.

The outputed dts format is compatile with 'dtc -I dtb -O dts xxx.dtb'. There's a new parameter 'dumpdts' which is similar to 'dumpdtb'. so try it like '-machine dumpdts=/tmp/xxx.dts'.

Hi. Thanks for this patchset, but I'm afraid this doesn't seem to me like something that should be in QEMU.
As you say, you can easily turn the dtb blob into a source file with dtc. That gets you a definitely-correct disassembly of the blob, and we don't need to maintain a possibly-buggy reimplementation of the dtb disassembler in QEMU.

Quoted 3 lines

thanks
-- PMM

That makes sense. It's mostly used for debugging.

Best regards
Li.

Quoted 1 line
#9 lore raw
Quoted 21 lines · Li Liu
Quoted 18 lines · Peter Maydell
Quoted 16 lines · john.liuli

From: Li Liu <[email protected]>

This patchset let qemu can convert dtb file to dts for two demands:

Some archtectures may generate the dtb file dynamically through qemu device tree functions. So this let it's possiable to dump final dtb to dts and save it as a reference.

For novices to debugging the issues caused by wrong dtb parameters. It will be easy to check the dts directly without copying the dtb which may be generated by 'dumpdtb' to the PC and dtc or fdtdump it.

The outputed dts format is compatile with 'dtc -I dtb -O dts xxx.dtb'. There's a new parameter 'dumpdts' which is similar to 'dumpdtb'. so try it like '-machine dumpdts=/tmp/xxx.dts'.

Quoted 7 lines
Quoted 7 lines

Hi. Thanks for this patchset, but I'm afraid this doesn't seem to me like something that should be in QEMU.
As you say, you can easily turn the dtb blob into a source file with dtc. That gets you a definitely-correct disassembly of the blob, and we don't need to maintain a possibly-buggy reimplementation of the dtb disassembler in QEMU.

+1. With DTC source on-hand (in the submodule) it might be possible to link the actual dtc dissassembler into QEMU, but I think external scripting of the dtc tool is easiest.

Regards,
Peter

Quoted 12 lines
Quoted 3 lines

thanks
-- PMM

That makes sense. It's mostly used for debugging.

Best regards
Li.

Quoted 1 line