[PATCH 18/27] hw/core/machine: add '-hw-dtb' option for machine

Ruslan Ruslichenko posted 27 patches 1 week, 4 days ago
Maintainers: Peter Maydell <peter.maydell@linaro.org>, Alistair Francis <alistair@alistair23.me>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, David Gibson <david@gibson.dropbear.id.au>, Peter Xu <peterx@redhat.com>
[PATCH 18/27] hw/core/machine: add '-hw-dtb' option for machine
Posted by Ruslan Ruslichenko 1 week, 4 days ago
From: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>

Add new '-hw-dtb' command-line option and corresponding
MachineState property.
The 'hw-dtb' option allows to specify a Device tree
binary with hardware description which Qemu should
emulate.

Signed-off-by: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>
---
 hw/core/machine.c        | 19 +++++++++++++++++++
 include/hw/core/boards.h |  1 +
 qemu-options.hx          |  9 +++++++++
 system/vl.c              |  3 +++
 4 files changed, 32 insertions(+)

diff --git a/hw/core/machine.c b/hw/core/machine.c
index 6411e68856..fc04cf75c0 100644
--- a/hw/core/machine.c
+++ b/hw/core/machine.c
@@ -378,6 +378,20 @@ static void machine_set_dtb(Object *obj, const char *value, Error **errp)
     ms->dtb = g_strdup(value);
 }
 
+static char *machine_get_hw_dtb(Object *obj, Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    return g_strdup(ms->hw_dtb);
+}
+
+static void machine_set_hw_dtb(Object *obj, const char *value, Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    ms->hw_dtb = g_strdup(value);
+}
+
 static char *machine_get_dumpdtb(Object *obj, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
@@ -1287,6 +1301,11 @@ static void machine_initfn(Object *obj)
     ms->ram_size = mc->default_ram_size;
     ms->maxram_size = mc->default_ram_size;
 
+    object_property_add_str(obj, "hw-dtb",
+                            machine_get_hw_dtb, machine_set_hw_dtb);
+    object_property_set_description(obj, "hw-dtb",
+                    "A device tree used to describe the hardware to QEMU.");
+
     if (mc->nvdimm_supported) {
         ms->nvdimms_state = g_new0(NVDIMMState, 1);
         object_property_add_bool(obj, "nvdimm",
diff --git a/include/hw/core/boards.h b/include/hw/core/boards.h
index 07f8938752..c28c505bb6 100644
--- a/include/hw/core/boards.h
+++ b/include/hw/core/boards.h
@@ -402,6 +402,7 @@ struct MachineState {
 
     void *fdt;
     char *dtb;
+    char *hw_dtb;
     char *dumpdtb;
     int phandle_start;
     char *dt_compatible;
diff --git a/qemu-options.hx b/qemu-options.hx
index bd014a3244..6dd3e04e38 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -4492,6 +4492,15 @@ SRST(initrd)
 
 ERST
 
+DEF("hw-dtb", HAS_ARG, QEMU_OPTION_hw_dtb, \
+    "-hw-dtb file    use 'file' as device tree image\n", QEMU_ARCH_ALL)
+SRST
+``-hw-dtb file``
+    Use <file> as a device tree binary (dtb) image used to create the
+    emulated machine. This dtb will not be passed to the kernel, use -dtb
+    for that.
+ERST
+
 DEF("dtb", HAS_ARG, QEMU_OPTION_dtb, \
     "-dtb    file    use 'file' as device tree image\n", QEMU_ARCH_ALL)
 SRST
diff --git a/system/vl.c b/system/vl.c
index aa9a155041..c890da586c 100644
--- a/system/vl.c
+++ b/system/vl.c
@@ -3020,6 +3020,9 @@ void qemu_init(int argc, char **argv)
             case QEMU_OPTION_dtb:
                 qdict_put_str(machine_opts_dict, "dtb", optarg);
                 break;
+            case QEMU_OPTION_hw_dtb:
+                qdict_put_str(machine_opts_dict, "hw-dtb", optarg);
+                break;
             case QEMU_OPTION_cdrom:
                 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
                 break;
-- 
2.43.0
Re: [PATCH 18/27] hw/core/machine: add '-hw-dtb' option for machine
Posted by Zhao Liu 1 week, 3 days ago
>  static char *machine_get_dumpdtb(Object *obj, Error **errp)
>  {
>      MachineState *ms = MACHINE(obj);
> @@ -1287,6 +1301,11 @@ static void machine_initfn(Object *obj)
>      ms->ram_size = mc->default_ram_size;
>      ms->maxram_size = mc->default_ram_size;
>  
> +    object_property_add_str(obj, "hw-dtb",
> +                            machine_get_hw_dtb, machine_set_hw_dtb);

What about object_class_property_add_str()? It seems this property
doesn't depend on any other state, so I think it could be at class
level, just like "dtb" did.

> +    object_property_set_description(obj, "hw-dtb",
> +                    "A device tree used to describe the hardware to QEMU.");

maybe "A device tree binary used to describe the hardware to QEMU"?

and none of the other machine property descriptions have 'periods'.

> +
>      if (mc->nvdimm_supported) {
>          ms->nvdimms_state = g_new0(NVDIMMState, 1);
>          object_property_add_bool(obj, "nvdimm",
Re: [PATCH 18/27] hw/core/machine: add '-hw-dtb' option for machine
Posted by Ruslan Ruslichenko 1 week, 3 days ago
On Tue, Jan 27, 2026 at 9:15 AM Zhao Liu <zhao1.liu@intel.com> wrote:
>
> >  static char *machine_get_dumpdtb(Object *obj, Error **errp)
> >  {
> >      MachineState *ms = MACHINE(obj);
> > @@ -1287,6 +1301,11 @@ static void machine_initfn(Object *obj)
> >      ms->ram_size = mc->default_ram_size;
> >      ms->maxram_size = mc->default_ram_size;
> >
> > +    object_property_add_str(obj, "hw-dtb",
> > +                            machine_get_hw_dtb, machine_set_hw_dtb);
>
> What about object_class_property_add_str()? It seems this property
> doesn't depend on any other state, so I think it could be at class
> level, just like "dtb" did.
>
Yes, it could be done by machine_class_init() indeed.

> > +    object_property_set_description(obj, "hw-dtb",
> > +                    "A device tree used to describe the hardware to QEMU.");
>
> maybe "A device tree binary used to describe the hardware to QEMU"?
>
> and none of the other machine property descriptions have 'periods'.
>
Thanks, I will update it in the v2 patch series.

> > +
> >      if (mc->nvdimm_supported) {
> >          ms->nvdimms_state = g_new0(NVDIMMState, 1);
> >          object_property_add_bool(obj, "nvdimm",