All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Henrique Barboza <danielhb413@gmail.com>
To: qemu-devel@nongnu.org
Cc: qemu-ppc@nongnu.org, clg@kaod.org, david@gibson.dropbear.id.au,
	alistair.francis@wdc.com,
	Daniel Henrique Barboza <danielhb413@gmail.com>
Subject: [PATCH for-7.2 v4 21/21] qmp/hmp, device_tree.c: add textformat dumpdtb option
Date: Fri, 26 Aug 2022 11:11:50 -0300	[thread overview]
Message-ID: <20220826141150.7201-22-danielhb413@gmail.com> (raw)
In-Reply-To: <20220826141150.7201-1-danielhb413@gmail.com>

The QMP/HMP 'dumpdtb' command is saving the FDT blob to be decoded using
'dtc' like the '-machine dumpdtb' always did. However, after adding
support for the 'info fdt' command, we're now able to format a full FDT
in text format - which is what 'info fdt /' already does.

Let's extend the 'dumpdtb' QMP/HMP command with the capability of saving
the FDT in plain text format. A new textformat '-t' option was added to
it. With this option, qemu_fdt_qmp_dumpdtb() will call
qemu_fdt_qmp_query_fdt() to retrieve the human string that represents
the output of 'info fdt /' and write it to the file.

This will allow users to dump the FDT in a text file and immediately
open it to see its contents, without the need of an extra step to decode
a dtb blob with 'dtc'. Here's an example:

(qemu) dumpdtb fdt.dtb

$ file fdt.dtb
fdt.dtb: Device Tree Blob version 17, size=15458, boot CPU=0, string block
size=2318, DT structure block size=13084

(qemu) dumpdtb -t fdt.txt

$ file fdt.txt
fdt.txt: ASCII text, with very long lines (4746)

$ grep -A 3 'persistent-memory' fdt.txt
    ibm,persistent-memory {
        device_type = "ibm,persistent-memory";
        #size-cells = <0x0>;
        #address-cells = <0x1>;
    };

Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
---
 hmp-commands.hx              |  7 ++++---
 include/sysemu/device_tree.h |  3 ++-
 monitor/hmp-cmds.c           |  3 ++-
 monitor/qmp-cmds.c           |  8 +++++---
 qapi/machine.json            |  2 +-
 softmmu/device_tree.c        | 25 ++++++++++++++++++++++---
 6 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/hmp-commands.hx b/hmp-commands.hx
index 2dd737078e..8a9595cc26 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -1808,8 +1808,9 @@ SRST
 ERST
     {
         .name       = "dumpdtb",
-        .args_type  = "filename:F",
-        .params     = "filename",
-        .help       = "save the FDT in the 'filename' file to be decoded using dtc",
+        .args_type  = "textformat:-t,filename:F",
+        .params     = "[-t] filename",
+        .help       = "save the FDT in the 'filename' file to be decoded using dtc."
+                       "Use '-t' to save the file in text (dts) format.",
         .cmd        = hmp_dumpdtb,
     },
diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h
index 551a02dee2..082ff69751 100644
--- a/include/sysemu/device_tree.h
+++ b/include/sysemu/device_tree.h
@@ -138,7 +138,8 @@ int qemu_fdt_add_path(void *fdt, const char *path);
     } while (0)
 
 void qemu_fdt_dumpdtb(void *fdt, int size);
-void qemu_fdt_qmp_dumpdtb(const char *filename, Error **errp);
+void qemu_fdt_qmp_dumpdtb(const char *filename, bool textformat,
+                          Error **errp);
 HumanReadableText *qemu_fdt_qmp_query_fdt(const char *nodepath,
                                           bool has_propname,
                                           const char *propname,
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index 320204e982..ffcb9ffb67 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -2476,9 +2476,10 @@ exit_no_print:
 void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
 {
     const char *filename = qdict_get_str(qdict, "filename");
+    bool textformat = qdict_get_try_bool(qdict, "textformat", false);
     Error *local_err = NULL;
 
-    qmp_dumpdtb(filename, &local_err);
+    qmp_dumpdtb(true, textformat, filename, &local_err);
 
     if (local_err) {
         hmp_handle_error(mon, local_err);
diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c
index ca2a96cdf7..8d625e5e7d 100644
--- a/monitor/qmp-cmds.c
+++ b/monitor/qmp-cmds.c
@@ -599,9 +599,10 @@ bool apply_str_list_filter(const char *string, strList *list)
 }
 
 #ifdef CONFIG_FDT
-void qmp_dumpdtb(const char *filename, Error **errp)
+void qmp_dumpdtb(bool has_textformat, bool textformat,
+                 const char *filename, Error **errp)
 {
-    return qemu_fdt_qmp_dumpdtb(filename, errp);
+    return qemu_fdt_qmp_dumpdtb(filename, textformat, errp);
 }
 
 HumanReadableText *qmp_x_query_fdt(const char *nodepath, bool has_propname,
@@ -610,7 +611,8 @@ HumanReadableText *qmp_x_query_fdt(const char *nodepath, bool has_propname,
     return qemu_fdt_qmp_query_fdt(nodepath, has_propname, propname, errp);
 }
 #else
-void qmp_dumpdtb(const char *filename, Error **errp)
+void qmp_dumpdtb(bool has_textformat, bool textformat,
+                 const char *filename, Error **errp)
 {
     error_setg(errp, "dumpdtb requires libfdt");
 }
diff --git a/qapi/machine.json b/qapi/machine.json
index c15ce60f46..8573f96da8 100644
--- a/qapi/machine.json
+++ b/qapi/machine.json
@@ -1680,7 +1680,7 @@
 #
 ##
 { 'command': 'dumpdtb',
-  'data': { 'filename': 'str' } }
+  'data': { '*textformat':'bool', 'filename': 'str' } }
 
 ##
 # @x-query-fdt:
diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c
index ad2386295b..34af31552d 100644
--- a/softmmu/device_tree.c
+++ b/softmmu/device_tree.c
@@ -645,8 +645,10 @@ out:
     return ret;
 }
 
-void qemu_fdt_qmp_dumpdtb(const char *filename, Error **errp)
+void qemu_fdt_qmp_dumpdtb(const char *filename, bool textformat, Error **errp)
 {
+    g_autoptr(HumanReadableText) txt = NULL;
+    void *contents = NULL;
     int size;
 
     if (!current_machine->fdt) {
@@ -654,9 +656,26 @@ void qemu_fdt_qmp_dumpdtb(const char *filename, Error **errp)
         return;
     }
 
-    size = fdt_totalsize(current_machine->fdt);
+    if (textformat) {
+        /*
+         * 'info fdt /' returns all the FDT in text format, formatted
+         * with a style close to what 'dtc' uses to decode the blob
+         * to a .dts.
+         */
+        txt = qemu_fdt_qmp_query_fdt("/", false, NULL, errp);
+
+        if (!txt) {
+            return;
+        }
+
+        contents = txt->human_readable_text;
+        size = strlen(txt->human_readable_text);
+    } else {
+        contents = current_machine->fdt;
+        size = fdt_totalsize(current_machine->fdt);
+    }
 
-    if (g_file_set_contents(filename, current_machine->fdt, size, NULL)) {
+    if (g_file_set_contents(filename, contents, size, NULL)) {
         return;
     }
 
-- 
2.37.2



      parent reply	other threads:[~2022-08-26 14:36 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26 14:11 [PATCH for-7.2 v4 00/21] QMP/HMP: add 'dumpdtb' and 'info fdt' commands Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 01/21] hw/arm: do not free machine->fdt in arm_load_dtb() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 02/21] hw/microblaze: set machine->fdt in microblaze_load_dtb() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 03/21] hw/nios2: set machine->fdt in nios2_load_dtb() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 04/21] hw/ppc: set machine->fdt in ppce500_load_device_tree() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 05/21] hw/ppc: set machine->fdt in bamboo_load_device_tree() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 06/21] hw/ppc: set machine->fdt in sam460ex_load_device_tree() Daniel Henrique Barboza
2022-08-26 18:56   ` BALATON Zoltan
2022-08-26 20:34     ` Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 07/21] hw/ppc: set machine->fdt in xilinx_load_device_tree() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 08/21] hw/ppc: set machine->fdt in pegasos2_machine_reset() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 09/21] hw/ppc: set machine->fdt in pnv_reset() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 10/21] hw/ppc: set machine->fdt in spapr machine Daniel Henrique Barboza
2022-08-29  3:29   ` David Gibson
2022-08-26 14:11 ` [PATCH for-7.2 v4 11/21] hw/riscv: set machine->fdt in sifive_u_machine_init() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 12/21] hw/riscv: set machine->fdt in spike_board_init() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 13/21] hw/xtensa: set machine->fdt in xtfpga_init() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 14/21] qmp/hmp, device_tree.c: introduce dumpdtb Daniel Henrique Barboza
2022-08-30 10:40   ` Markus Armbruster
2022-08-26 14:11 ` [PATCH for-7.2 v4 15/21] qmp/hmp, device_tree.c: introduce 'info fdt' command Daniel Henrique Barboza
2022-08-29  3:34   ` David Gibson
2022-08-29 22:00     ` Daniel Henrique Barboza
2022-08-30  1:50       ` David Gibson
2022-08-30  9:59         ` Daniel Henrique Barboza
2022-08-30 10:43         ` Markus Armbruster
2022-09-01  2:10           ` David Gibson
2022-08-30 10:41   ` Markus Armbruster
2022-08-26 14:11 ` [PATCH for-7.2 v4 16/21] device_tree.c: support string array prop in fdt_format_node() Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 17/21] device_tree.c: support remaining FDT prop types Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 18/21] device_node.c: enable 'info fdt' to print subnodes Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 19/21] device_tree.c: add fdt_format_property() helper Daniel Henrique Barboza
2022-08-26 14:11 ` [PATCH for-7.2 v4 20/21] hmp, device_tree.c: add 'info fdt <property>' support Daniel Henrique Barboza
2022-08-26 14:11 ` Daniel Henrique Barboza [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220826141150.7201-22-danielhb413@gmail.com \
    --to=danielhb413@gmail.com \
    --cc=alistair.francis@wdc.com \
    --cc=clg@kaod.org \
    --cc=david@gibson.dropbear.id.au \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.