[PATCH 4/4] hw/arm: expose Error * to arm_load_dtb

Alex Bennée posted 4 patches 3 weeks, 6 days ago
There is a newer version of this series
[PATCH 4/4] hw/arm: expose Error * to arm_load_dtb
Posted by Alex Bennée 3 weeks, 6 days ago
Currently all calls to arm_load_dtb will result in an exit if we fail.
By passing Error * we can use &error_fatal and properly set the error
report.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 include/hw/arm/boot.h |  3 ++-
 hw/arm/boot.c         | 35 +++++++++++++++--------------------
 hw/arm/virt.c         |  6 +++---
 3 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/include/hw/arm/boot.h b/include/hw/arm/boot.h
index a2e22bda8a5..fdb99c0c1ee 100644
--- a/include/hw/arm/boot.h
+++ b/include/hw/arm/boot.h
@@ -164,6 +164,7 @@ AddressSpace *arm_boot_address_space(ARMCPU *cpu,
  * @addr_limit: upper limit of the available memory area at @addr
  * @as:         address space to load image to
  * @cpu:        ARM CPU object
+ * @errp:       Error object, often &error_fatal
  *
  * Load a device tree supplied by the machine or by the user  with the
  * '-dtb' command line option, and put it at offset @addr in target
@@ -181,7 +182,7 @@ AddressSpace *arm_boot_address_space(ARMCPU *cpu,
  */
 int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
                  hwaddr addr_limit, AddressSpace *as, MachineState *ms,
-                 ARMCPU *cpu);
+                 ARMCPU *cpu, Error **errp);
 
 /* Write a secure board setup routine with a dummy handler for SMCs */
 void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
diff --git a/hw/arm/boot.c b/hw/arm/boot.c
index f9d0bc7011e..d28ae8b86ab 100644
--- a/hw/arm/boot.c
+++ b/hw/arm/boot.c
@@ -517,7 +517,7 @@ static void fdt_add_psci_node(void *fdt, ARMCPU *armcpu)
 
 int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
                  hwaddr addr_limit, AddressSpace *as, MachineState *ms,
-                 ARMCPU *cpu)
+                 ARMCPU *cpu, Error **errp)
 {
     g_autofree void *fdt = NULL;
     g_auto(GStrv) node_path = NULL;
@@ -525,25 +525,24 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
     uint32_t acells, scells;
     unsigned int i;
     hwaddr mem_base, mem_len;
-    Error *err = NULL;
 
     if (binfo->dtb_filename) {
         g_autofree char *filename = qemu_find_file(QEMU_FILE_TYPE_DTB,
                                                    binfo->dtb_filename);
         if (!filename) {
-            fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
+            error_setg(errp, "Couldn't open dtb file %s", binfo->dtb_filename);
             return -1;
         }
 
         fdt = load_device_tree(filename, &size);
         if (!fdt) {
-            fprintf(stderr, "Couldn't open dtb file %s\n", filename);
+            error_setg(errp, "Couldn't open dtb file %s", filename);
             return -1;
         }
     } else {
         fdt = binfo->get_dtb(binfo, &size);
         if (!fdt) {
-            fprintf(stderr, "Board was unable to create a dtb blob\n");
+            error_setg(errp, "Board was unable to create a dtb blob");
             return -1;
         }
     }
@@ -561,7 +560,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
     scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
                                    NULL, &error_fatal);
     if (acells == 0 || scells == 0) {
-        fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
+        error_setg(errp, "dtb file invalid (#address-cells or #size-cells 0)");
         return -1;
     }
 
@@ -569,15 +568,13 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
         /* This is user error so deserves a friendlier error message
          * than the failure of setprop_sized_cells would provide
          */
-        fprintf(stderr, "qemu: dtb file not compatible with "
-                "RAM size > 4GB\n");
+        error_setg(errp, "qemu: dtb file not compatible with RAM size > 4GB");
         return -1;
     }
 
     /* nop all root nodes matching /memory or /memory@unit-address */
-    node_path = qemu_fdt_node_unit_path(fdt, "memory", &err);
-    if (err) {
-        error_report_err(err);
+    node_path = qemu_fdt_node_unit_path(fdt, "memory", errp);
+    if (!node_path) {
         return -1;
     }
     while (node_path[n]) {
@@ -607,7 +604,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
             rc = fdt_add_memory_node(fdt, acells, mem_base,
                                      scells, mem_len, i);
             if (rc < 0) {
-                fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
+                error_setg(errp, "couldn't add /memory@%"PRIx64" node",
                         mem_base);
                 return -1;
             }
@@ -618,7 +615,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
         rc = fdt_add_memory_node(fdt, acells, binfo->loader_start,
                                  scells, binfo->ram_size, -1);
         if (rc < 0) {
-            fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
+            error_setg(errp, "couldn't add /memory@%"PRIx64" node",
                     binfo->loader_start);
             return -1;
         }
@@ -633,7 +630,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
         rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
                                      ms->kernel_cmdline);
         if (rc < 0) {
-            fprintf(stderr, "couldn't set /chosen/bootargs\n");
+            error_setg(errp, "couldn't set /chosen/bootargs");
             return -1;
         }
     }
@@ -642,7 +639,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
         rc = qemu_fdt_setprop_sized_cells(fdt, "/chosen", "linux,initrd-start",
                                           acells, binfo->initrd_start);
         if (rc < 0) {
-            fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
+            error_setg(errp, "couldn't set /chosen/linux,initrd-start");
             return -1;
         }
 
@@ -651,7 +648,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
                                           binfo->initrd_start +
                                           binfo->initrd_size);
         if (rc < 0) {
-            fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
+            error_setg(errp, "couldn't set /chosen/linux,initrd-end");
             return -1;
         }
     }
@@ -1321,10 +1318,8 @@ void arm_load_kernel(ARMCPU *cpu, MachineState *ms, struct arm_boot_info *info)
      * decided whether to enable PSCI and set the psci-conduit CPU properties.
      */
     if (!info->skip_dtb_autoload && have_dtb(info)) {
-        if (arm_load_dtb(info->dtb_start, info, info->dtb_limit,
-                         as, ms, cpu) < 0) {
-            exit(1);
-        }
+        arm_load_dtb(info->dtb_start, info, info->dtb_limit,
+                     as, ms, cpu, &error_fatal);
     }
 }
 
diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 9326cfc895f..6061e0ddb50 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -1803,9 +1803,9 @@ void virt_machine_done(Notifier *notifier, void *data)
                                        vms->memmap[VIRT_PLATFORM_BUS].size,
                                        vms->irqmap[VIRT_PLATFORM_BUS]);
     }
-    if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms, cpu) < 0) {
-        exit(1);
-    }
+
+    arm_load_dtb(info->dtb_start, info, info->dtb_limit,
+                 as, ms, cpu, &error_fatal);
 
     pci_bus_add_fw_cfg_extra_pci_roots(vms->fw_cfg, vms->bus,
                                        &error_abort);
-- 
2.47.2


Re: [PATCH 4/4] hw/arm: expose Error * to arm_load_dtb
Posted by Manos Pitsidianakis 3 weeks, 6 days ago
On Mon, Sep 1, 2025 at 3:54 PM Alex Bennée <alex.bennee@linaro.org> wrote:
>
> Currently all calls to arm_load_dtb will result in an exit if we fail.
> By passing Error * we can use &error_fatal and properly set the error
> report.
>
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>


>  include/hw/arm/boot.h |  3 ++-
>  hw/arm/boot.c         | 35 +++++++++++++++--------------------
>  hw/arm/virt.c         |  6 +++---
>  3 files changed, 20 insertions(+), 24 deletions(-)
>
> diff --git a/include/hw/arm/boot.h b/include/hw/arm/boot.h
> index a2e22bda8a5..fdb99c0c1ee 100644
> --- a/include/hw/arm/boot.h
> +++ b/include/hw/arm/boot.h
> @@ -164,6 +164,7 @@ AddressSpace *arm_boot_address_space(ARMCPU *cpu,
>   * @addr_limit: upper limit of the available memory area at @addr
>   * @as:         address space to load image to
>   * @cpu:        ARM CPU object
> + * @errp:       Error object, often &error_fatal
>   *
>   * Load a device tree supplied by the machine or by the user  with the
>   * '-dtb' command line option, and put it at offset @addr in target
> @@ -181,7 +182,7 @@ AddressSpace *arm_boot_address_space(ARMCPU *cpu,
>   */
>  int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>                   hwaddr addr_limit, AddressSpace *as, MachineState *ms,
> -                 ARMCPU *cpu);
> +                 ARMCPU *cpu, Error **errp);
>
>  /* Write a secure board setup routine with a dummy handler for SMCs */
>  void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
> diff --git a/hw/arm/boot.c b/hw/arm/boot.c
> index f9d0bc7011e..d28ae8b86ab 100644
> --- a/hw/arm/boot.c
> +++ b/hw/arm/boot.c
> @@ -517,7 +517,7 @@ static void fdt_add_psci_node(void *fdt, ARMCPU *armcpu)
>
>  int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>                   hwaddr addr_limit, AddressSpace *as, MachineState *ms,
> -                 ARMCPU *cpu)
> +                 ARMCPU *cpu, Error **errp)
>  {
>      g_autofree void *fdt = NULL;
>      g_auto(GStrv) node_path = NULL;
> @@ -525,25 +525,24 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>      uint32_t acells, scells;
>      unsigned int i;
>      hwaddr mem_base, mem_len;
> -    Error *err = NULL;
>
>      if (binfo->dtb_filename) {
>          g_autofree char *filename = qemu_find_file(QEMU_FILE_TYPE_DTB,
>                                                     binfo->dtb_filename);
>          if (!filename) {
> -            fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
> +            error_setg(errp, "Couldn't open dtb file %s", binfo->dtb_filename);
>              return -1;
>          }
>
>          fdt = load_device_tree(filename, &size);
>          if (!fdt) {
> -            fprintf(stderr, "Couldn't open dtb file %s\n", filename);
> +            error_setg(errp, "Couldn't open dtb file %s", filename);
>              return -1;
>          }
>      } else {
>          fdt = binfo->get_dtb(binfo, &size);
>          if (!fdt) {
> -            fprintf(stderr, "Board was unable to create a dtb blob\n");
> +            error_setg(errp, "Board was unable to create a dtb blob");
>              return -1;
>          }
>      }
> @@ -561,7 +560,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>      scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
>                                     NULL, &error_fatal);
>      if (acells == 0 || scells == 0) {
> -        fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
> +        error_setg(errp, "dtb file invalid (#address-cells or #size-cells 0)");
>          return -1;
>      }
>
> @@ -569,15 +568,13 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>          /* This is user error so deserves a friendlier error message
>           * than the failure of setprop_sized_cells would provide
>           */
> -        fprintf(stderr, "qemu: dtb file not compatible with "
> -                "RAM size > 4GB\n");
> +        error_setg(errp, "qemu: dtb file not compatible with RAM size > 4GB");
>          return -1;
>      }
>
>      /* nop all root nodes matching /memory or /memory@unit-address */
> -    node_path = qemu_fdt_node_unit_path(fdt, "memory", &err);
> -    if (err) {
> -        error_report_err(err);
> +    node_path = qemu_fdt_node_unit_path(fdt, "memory", errp);
> +    if (!node_path) {
>          return -1;
>      }
>      while (node_path[n]) {
> @@ -607,7 +604,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>              rc = fdt_add_memory_node(fdt, acells, mem_base,
>                                       scells, mem_len, i);
>              if (rc < 0) {
> -                fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
> +                error_setg(errp, "couldn't add /memory@%"PRIx64" node",
>                          mem_base);
>                  return -1;
>              }
> @@ -618,7 +615,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>          rc = fdt_add_memory_node(fdt, acells, binfo->loader_start,
>                                   scells, binfo->ram_size, -1);
>          if (rc < 0) {
> -            fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
> +            error_setg(errp, "couldn't add /memory@%"PRIx64" node",
>                      binfo->loader_start);
>              return -1;
>          }
> @@ -633,7 +630,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>          rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
>                                       ms->kernel_cmdline);
>          if (rc < 0) {
> -            fprintf(stderr, "couldn't set /chosen/bootargs\n");
> +            error_setg(errp, "couldn't set /chosen/bootargs");
>              return -1;
>          }
>      }
> @@ -642,7 +639,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>          rc = qemu_fdt_setprop_sized_cells(fdt, "/chosen", "linux,initrd-start",
>                                            acells, binfo->initrd_start);
>          if (rc < 0) {
> -            fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
> +            error_setg(errp, "couldn't set /chosen/linux,initrd-start");
>              return -1;
>          }
>
> @@ -651,7 +648,7 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>                                            binfo->initrd_start +
>                                            binfo->initrd_size);
>          if (rc < 0) {
> -            fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
> +            error_setg(errp, "couldn't set /chosen/linux,initrd-end");
>              return -1;
>          }
>      }
> @@ -1321,10 +1318,8 @@ void arm_load_kernel(ARMCPU *cpu, MachineState *ms, struct arm_boot_info *info)
>       * decided whether to enable PSCI and set the psci-conduit CPU properties.
>       */
>      if (!info->skip_dtb_autoload && have_dtb(info)) {
> -        if (arm_load_dtb(info->dtb_start, info, info->dtb_limit,
> -                         as, ms, cpu) < 0) {
> -            exit(1);
> -        }
> +        arm_load_dtb(info->dtb_start, info, info->dtb_limit,
> +                     as, ms, cpu, &error_fatal);
>      }
>  }
>
> diff --git a/hw/arm/virt.c b/hw/arm/virt.c
> index 9326cfc895f..6061e0ddb50 100644
> --- a/hw/arm/virt.c
> +++ b/hw/arm/virt.c
> @@ -1803,9 +1803,9 @@ void virt_machine_done(Notifier *notifier, void *data)
>                                         vms->memmap[VIRT_PLATFORM_BUS].size,
>                                         vms->irqmap[VIRT_PLATFORM_BUS]);
>      }
> -    if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms, cpu) < 0) {
> -        exit(1);
> -    }
> +
> +    arm_load_dtb(info->dtb_start, info, info->dtb_limit,
> +                 as, ms, cpu, &error_fatal);
>
>      pci_bus_add_fw_cfg_extra_pci_roots(vms->fw_cfg, vms->bus,
>                                         &error_abort);
> --
> 2.47.2
>
>