Skip to contentSkip to conversation

core/loader: capture Error from load_image_targphys

PATCH Superseded Vishal Chourasia 5 participants 26 messages Oct 24, 2025

Subsystems (2)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v9 cover core/loader: capture Error from load_image_targphys

Currently, when QEMU fails to load images (kernel, BIOS, initrd) via load_image_targphys(), the error messages are not descriptive enough for users to understand why the loading failed. This series adds the ability to add Error information through the loader functions to provide more detailed error reporting.

v9:
- Split Patch 2 from v8 into two patches.
- Included RB from Aditya Gupta.

v8:
- Rebased on latest master.
- Modified commit message for Patch 5.

v7:
- Dropped the patch [hw/ppc/spapr: Rename resize_hpt_err to errp]. already merged. Rebased on latest master.

v6:
- Pass error_fatal instead of errp where caller exits right after load_image_targphys() fails.
- Include filename in error messages.

v5:
- Moved the patch which replaces open() with qemu_open() earlier in the series.
- Incorporated minor changes in the error messages requested by Balaton.
- Moved check for size zero in load_image_targphys_as() into it's separate patch.

v4:
- Reject empty files (size == 0) with an appropriate error. [1/5]
- Populate error when rom_add_file_fixed_as() fails. [1/5]
- Add missing check in hw/ppc/virtex_ml507.c [5/5]
- Fixed checkpatch warnings [5/5]
- Check errp for failures from load_image_targphys/load_image_targphys_as [3,5/5]
- Remove unused variables [5/5]

v3:
- Use qemu_open() instead of open() in get_image_size() (danpb)
- Remove redundant error_setg_file_open() call (danpb)
- Use error_prepend()/error_reportf_err() to preserve underlying error details (danpb)

[v8]: https://lore.kernel.org/all/[email protected]
[v7]: https://lore.kernel.org/all/[email protected]
[v6]: https://lore.kernel.org/qemu-devel/[email protected]
[v5]: https://lore.kernel.org/all/[email protected]
[v4]: https://lore.kernel.org/all/[email protected]
[v3]: https://lore.kernel.org/all/[email protected]
[v2]: https://lore.kernel.org/all/[email protected]
[v1]: https://lore.kernel.org/all/[email protected]

Vishal Chourasia (6):
core/loader: Use qemu_open() instead of open() in get_image_size() core/loader: capture Error from load_image_targphys core/loader: split error handling in image loading functions core/loader: add check for zero size in load_image_targphys_as
core: Pass errp to load_image_targphys_as()
ppc: Pass error_fatal to load_image_targphys()

 hw/alpha/dp264.c         |  4 ++--
 hw/arm/armv7m.c          |  2 +-
 hw/arm/boot.c            |  5 +++--
 hw/arm/digic_boards.c    |  2 +-
 hw/arm/highbank.c        |  3 ++-
 hw/arm/raspi.c           |  2 +-
 hw/arm/vexpress.c        |  2 +-
 hw/core/generic-loader.c |  8 +++++---
 hw/core/guest-loader.c   |  7 ++++---
 hw/core/loader.c         | 37 +++++++++++++++++++++++++++++--------
 hw/hppa/machine.c        |  5 +++--
 hw/i386/multiboot.c      |  2 +-
 hw/i386/x86-common.c     |  4 ++--
 hw/ipmi/ipmi_bmc_sim.c   |  2 +-
 hw/loongarch/boot.c      |  5 ++---
 hw/m68k/an5206.c         |  2 +-
 hw/m68k/mcf5208.c        |  4 ++--
 hw/m68k/next-cube.c      |  2 +-
 hw/m68k/q800.c           |  7 ++++---
 hw/m68k/virt.c           |  4 ++--
 hw/microblaze/boot.c     |  5 +++--
 hw/mips/boston.c         |  2 +-
 hw/mips/fuloong2e.c      |  9 +++++----
 hw/mips/jazz.c           |  2 +-
 hw/mips/loongson3_virt.c | 10 ++++++----
 hw/mips/malta.c          |  9 +++++----
 hw/nubus/nubus-device.c  |  2 +-
 hw/openrisc/boot.c       |  5 +++--
 hw/pci/pci.c             |  2 +-
 hw/ppc/amigaone.c        | 13 ++-----------
 hw/ppc/e500.c            | 20 ++++----------------
 hw/ppc/mac_newworld.c    | 19 ++++++-------------
 hw/ppc/mac_oldworld.c    | 19 ++++++-------------
 hw/ppc/pegasos2.c        | 10 +++-------
 hw/ppc/pnv.c             | 27 ++++++---------------------
 hw/ppc/ppc440_bamboo.c   |  9 ++-------
 hw/ppc/prep.c            | 19 ++++++-------------
 hw/ppc/sam460ex.c        |  8 ++------
 hw/ppc/spapr.c           | 17 ++++-------------
 hw/ppc/virtex_ml507.c    | 11 +++--------
 hw/riscv/boot.c          |  7 ++++---
 hw/rx/rx-gdbsim.c        |  2 +-
 hw/s390x/ipl.c           |  8 +++++---
 hw/sh4/r2d.c             |  8 +++++---
 hw/smbios/smbios.c       |  2 +-
 hw/sparc/leon3.c         |  4 ++--
 hw/sparc/sun4m.c         |  8 +++++---
 hw/sparc64/sun4u.c       |  7 ++++---
 hw/xtensa/xtfpga.c       |  3 ++-
 include/hw/loader.h      |  8 +++++---
 system/device_tree.c     |  2 +-
 51 files changed, 173 insertions(+), 213 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v9 1/6 core/loader: Use qemu_open() instead of open() in get_image_size()

Replace open() with qemu_open() which provides better error handling via the Error object, automatically sets O_CLOEXEC, and supports FD passing with /dev/fdset.

Currently pass errp argument as NULL.

---
 hw/core/loader.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/core/loader.c +1 -1
index 477661a025..c9782d67e8 100644
@@ -74,7 +74,7 @@ int64_t get_image_size(const char *filename)
 {
     int fd;
     int64_t size;
-    fd = open(filename, O_RDONLY | O_BINARY);
+    fd = qemu_open(filename, O_RDONLY | O_BINARY, NULL);
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
Signature · 1 line
2.51.0
Reviewed-by on v9 1/6
#2 lore raw

Re: [Patch v9 1/6] core/loader: Use qemu_open() instead of open() in get_image_size()

Quoted 11 lines · Vishal Chourasia

Replace open() with qemu_open() which provides better error handling via the Error object, automatically sets O_CLOEXEC, and supports FD passing with /dev/fdset.

Currently pass errp argument as NULL.

---
  hw/core/loader.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw

PATCH v9 2/6 core/loader: capture Error from load_image_targphys

In reply to Vishal Chourasia

Add Error **errp parameter to load_image_targphys(), load_image_targphys_as(), and get_image_size() to enable better error reporting when image loading fails.

Pass NULL for errp in all existing call sites to maintain current behavior. No functional change intended in this patch.

---
 hw/alpha/dp264.c         |  4 ++--
 hw/arm/armv7m.c          |  2 +-
 hw/arm/boot.c            |  5 +++--
 hw/arm/digic_boards.c    |  2 +-
 hw/arm/highbank.c        |  3 ++-
 hw/arm/raspi.c           |  2 +-
 hw/arm/vexpress.c        |  2 +-
 hw/core/generic-loader.c |  3 ++-
 hw/core/guest-loader.c   |  2 +-
 hw/core/loader.c         | 16 +++++++++-------
 hw/hppa/machine.c        |  5 +++--
 hw/i386/multiboot.c      |  2 +-
 hw/i386/x86-common.c     |  4 ++--
 hw/ipmi/ipmi_bmc_sim.c   |  2 +-
 hw/loongarch/boot.c      |  5 ++---
 hw/m68k/an5206.c         |  2 +-
 hw/m68k/mcf5208.c        |  4 ++--
 hw/m68k/next-cube.c      |  2 +-
 hw/m68k/q800.c           |  7 ++++---
 hw/m68k/virt.c           |  4 ++--
 hw/microblaze/boot.c     |  5 +++--
 hw/mips/boston.c         |  2 +-
 hw/mips/fuloong2e.c      |  9 +++++----
 hw/mips/jazz.c           |  2 +-
 hw/mips/loongson3_virt.c | 10 ++++++----
 hw/mips/malta.c          |  9 +++++----
 hw/nubus/nubus-device.c  |  2 +-
 hw/openrisc/boot.c       |  5 +++--
 hw/pci/pci.c             |  2 +-
 hw/ppc/amigaone.c        |  4 ++--
 hw/ppc/e500.c            |  5 +++--
 hw/ppc/mac_newworld.c    |  9 ++++++---
 hw/ppc/mac_oldworld.c    |  9 ++++++---
 hw/ppc/pegasos2.c        |  5 +++--
 hw/ppc/pnv.c             |  9 ++++++---
 hw/ppc/ppc440_bamboo.c   |  3 ++-
 hw/ppc/prep.c            |  8 +++++---
 hw/ppc/sam460ex.c        |  3 ++-
 hw/ppc/spapr.c           |  8 ++++----
 hw/ppc/virtex_ml507.c    |  5 +++--
 hw/riscv/boot.c          |  7 ++++---
 hw/rx/rx-gdbsim.c        |  2 +-
 hw/s390x/ipl.c           |  8 +++++---
 hw/sh4/r2d.c             |  8 +++++---
 hw/smbios/smbios.c       |  2 +-
 hw/sparc/leon3.c         |  4 ++--
 hw/sparc/sun4m.c         |  8 +++++---
 hw/sparc64/sun4u.c       |  7 ++++---
 hw/xtensa/xtfpga.c       |  3 ++-
 include/hw/loader.h      |  8 +++++---
 system/device_tree.c     |  2 +-
 51 files changed, 145 insertions(+), 106 deletions(-)
hw/alpha/dp264.c +2 -2
index 19562b5967..b6155646ef 100644
@@ -182,7 +182,7 @@ static void clipper_init(MachineState *machine)
             long initrd_base;
             int64_t initrd_size;
 
-            initrd_size = get_image_size(initrd_filename);
+            initrd_size = get_image_size(initrd_filename, NULL);
             if (initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              initrd_filename);
@@ -192,7 +192,7 @@ static void clipper_init(MachineState *machine)
             /* Put the initrd image as high in memory as possible.  */
             initrd_base = (ram_size - initrd_size) & TARGET_PAGE_MASK;
             load_image_targphys(initrd_filename, initrd_base,
-                                ram_size - initrd_base);
+                                ram_size - initrd_base, NULL);
 
             address_space_stq(&address_space_memory, param_offset + 0x100,
                               initrd_base + 0xfffffc0000000000ULL,
hw/arm/armv7m.c +1 -1
index cea3eb49ee..c4a9c3ac52 100644
@@ -611,7 +611,7 @@ void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename,
                                  NULL, ELFDATA2LSB, EM_ARM, 1, 0, as);
         if (image_size < 0) {
             image_size = load_image_targphys_as(kernel_filename, mem_base,
-                                                mem_size, as);
+                                                mem_size, as, NULL);
         }
         if (image_size < 0) {
             error_report("Could not load kernel '%s'", kernel_filename);
hw/arm/boot.c +3 -2
index e77d8679d8..b91660208f 100644
@@ -964,7 +964,8 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu,
         /* 32-bit ARM */
         entry = info->loader_start + KERNEL_LOAD_ADDR;
         kernel_size = load_image_targphys_as(info->kernel_filename, entry,
-                                             ram_end - KERNEL_LOAD_ADDR, as);
+                                             ram_end - KERNEL_LOAD_ADDR, as,
+                                             NULL);
         is_linux = 1;
         if (kernel_size >= 0) {
             image_low_addr = entry;
@@ -1025,7 +1026,7 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu,
                                                      info->initrd_start,
                                                      ram_end -
                                                      info->initrd_start,
-                                                     as);
+                                                     as, NULL);
             }
             if (initrd_size < 0) {
                 error_report("could not load initrd '%s'",
hw/arm/digic_boards.c +1 -1
index 466b8b84c0..de56991bac 100644
@@ -97,7 +97,7 @@ static void digic_load_rom(DigicState *s, hwaddr addr,
             exit(1);
         }
 
-        rom_size = load_image_targphys(fn, addr, max_size);
+        rom_size = load_image_targphys(fn, addr, max_size, NULL);
         if (rom_size < 0 || rom_size > max_size) {
             error_report("Couldn't load rom image '%s'.", filename);
             exit(1);
hw/arm/highbank.c +2 -1
index 165c0b741a..ff987467a9 100644
@@ -235,7 +235,8 @@ static void calxeda_init(MachineState *machine, enum cxmachines machine_id)
     if (machine->firmware != NULL) {
         sysboot_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware);
         if (sysboot_filename != NULL) {
-            if (load_image_targphys(sysboot_filename, 0xfff88000, 0x8000) < 0) {
+            if (load_image_targphys(sysboot_filename, 0xfff88000, 0x8000,
+                                    NULL) < 0) {
                 error_report("Unable to load %s", machine->firmware);
                 exit(1);
             }
hw/arm/raspi.c +1 -1
index 9d9af63d65..fbf3e09c03 100644
@@ -230,7 +230,7 @@ static void setup_boot(MachineState *machine, ARMCPU *cpu,
                              ? FIRMWARE_ADDR_2 : FIRMWARE_ADDR_3;
         /* load the firmware image (typically kernel.img) */
         r = load_image_targphys(machine->firmware, firmware_addr,
-                                ram_size - firmware_addr);
+                                ram_size - firmware_addr, NULL);
         if (r < 0) {
             error_report("Failed to load firmware from %s", machine->firmware);
             exit(1);
hw/arm/vexpress.c +1 -1
index 35f8d05ea1..60cd375fe7 100644
@@ -578,7 +578,7 @@ static void vexpress_common_init(MachineState *machine)
             exit(1);
         }
         image_size = load_image_targphys(fn, map[VE_NORFLASH0],
-                                         VEXPRESS_FLASH_SIZE);
+                                         VEXPRESS_FLASH_SIZE, NULL);
         g_free(fn);
         if (image_size < 0) {
             error_report("Could not load ROM image '%s'", machine->firmware);
hw/core/generic-loader.c +2 -1
index e72bbde2a2..6689847c33 100644
@@ -148,7 +148,8 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
 
         if (size < 0 || s->force_raw) {
             /* Default to the maximum size being the machine's ram size */
-            size = load_image_targphys_as(s->file, s->addr, current_machine->ram_size, as);
+            size = load_image_targphys_as(s->file, s->addr,
+                    current_machine->ram_size, as, NULL);
         } else {
             s->addr = entry;
         }
hw/core/guest-loader.c +1 -1
index 3db89d7a2e..59f325ad9c 100644
@@ -101,7 +101,7 @@ static void guest_loader_realize(DeviceState *dev, Error **errp)
 
     /* Default to the maximum size being the machine's ram size */
     size = load_image_targphys_as(file, s->addr, current_machine->ram_size,
-                                  NULL);
+                                  NULL, NULL);
     if (size < 0) {
         error_setg(errp, "Cannot load specified image %s", file);
         return;
hw/core/loader.c +9 -7
index c9782d67e8..7aca4989ef 100644
@@ -48,6 +48,7 @@
 #include "qapi/error.h"
 #include "qapi/qapi-commands-machine.h"
 #include "qapi/type-helpers.h"
+#include "qemu/units.h"
 #include "trace.h"
 #include "hw/hw.h"
 #include "disas/disas.h"
@@ -70,11 +71,11 @@
 static int roms_loaded;
 
 /* return the size or -1 if error */
-int64_t get_image_size(const char *filename)
+int64_t get_image_size(const char *filename, Error **errp)
 {
     int fd;
     int64_t size;
-    fd = qemu_open(filename, O_RDONLY | O_BINARY, NULL);
+    fd = qemu_open(filename, O_RDONLY | O_BINARY, errp);
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
@@ -118,18 +119,19 @@ ssize_t read_targphys(const char *name,
 }
 
 ssize_t load_image_targphys(const char *filename,
-                            hwaddr addr, uint64_t max_sz)
+                            hwaddr addr, uint64_t max_sz, Error **errp)
 {
-    return load_image_targphys_as(filename, addr, max_sz, NULL);
+    return load_image_targphys_as(filename, addr, max_sz, NULL, errp);
 }
 
 /* return the size or -1 if error */
 ssize_t load_image_targphys_as(const char *filename,
-                               hwaddr addr, uint64_t max_sz, AddressSpace *as)
+                               hwaddr addr, uint64_t max_sz, AddressSpace *as,
+                               Error **errp)
 {
     ssize_t size;
 
-    size = get_image_size(filename);
+    size = get_image_size(filename, errp);
     if (size < 0 || size > max_sz) {
         return -1;
     }
@@ -150,7 +152,7 @@ ssize_t load_image_mr(const char *filename, MemoryRegion *mr)
         return -1;
     }
 
-    size = get_image_size(filename);
+    size = get_image_size(filename, NULL);
 
     if (size < 0 || size > memory_region_size(mr)) {
         return -1;
hw/hppa/machine.c +3 -2
index cddca69b93..ba894f8b1e 100644
@@ -495,7 +495,7 @@ static void machine_HP_common_init_tail(MachineState *machine, PCIBus *pci_bus,
             ram_addr_t initrd_base;
             int64_t initrd_size;
 
-            initrd_size = get_image_size(initrd_filename);
+            initrd_size = get_image_size(initrd_filename, NULL);
             if (initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              initrd_filename);
@@ -516,7 +516,8 @@ static void machine_HP_common_init_tail(MachineState *machine, PCIBus *pci_bus,
                 exit(1);
             }
 
-            load_image_targphys(initrd_filename, initrd_base, initrd_size);
+            load_image_targphys(initrd_filename, initrd_base, initrd_size,
+                                NULL);
             cpu[0]->env.initrd_base = initrd_base;
             cpu[0]->env.initrd_end  = initrd_base + initrd_size;
         }
hw/i386/multiboot.c +1 -1
index 6e6b96bc34..78690781b7 100644
@@ -337,7 +337,7 @@ int load_multiboot(X86MachineState *x86ms,
                 *next_space = '\0';
             }
             mb_debug("multiboot loading module: %s", one_file);
-            mb_mod_length = get_image_size(one_file);
+            mb_mod_length = get_image_size(one_file, NULL);
             if (mb_mod_length < 0) {
                 error_report("Failed to open file '%s'", one_file);
                 exit(1);
hw/i386/x86-common.c +2 -2
index 7512be64d6..6e78f3d07d 100644
@@ -924,7 +924,7 @@ void x86_load_linux(X86MachineState *x86ms,
             exit(1);
         }
 
-        dtb_size = get_image_size(dtb_filename);
+        dtb_size = get_image_size(dtb_filename, NULL);
         if (dtb_size <= 0) {
             fprintf(stderr, "qemu: error reading dtb %s: %s\n",
                     dtb_filename, strerror(errno));
@@ -1025,7 +1025,7 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
     bios_name = MACHINE(x86ms)->firmware ?: default_firmware;
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
     if (filename) {
-        bios_size = get_image_size(filename);
+        bios_size = get_image_size(filename, NULL);
     } else {
         bios_size = -1;
     }
hw/ipmi/ipmi_bmc_sim.c +1 -1
index 04e1dcd0e7..4604d632b1 100644
@@ -2211,7 +2211,7 @@ static void ipmi_fru_init(IPMIFru *fru)
         goto out;
     }
 
-    fsize = get_image_size(fru->filename);
+    fsize = get_image_size(fru->filename, NULL);
     if (fsize > 0) {
         size = QEMU_ALIGN_UP(fsize, fru->areasize);
         fru->data = g_malloc0(size);
hw/loongarch/boot.c +2 -3
index 3dd48cb8aa..8857a04998 100644
@@ -328,14 +328,13 @@ static int64_t load_kernel_info(struct loongarch_boot_info *info)
     }
 
     if (info->initrd_filename) {
-        ssize_t initrd_size = get_image_size(info->initrd_filename);
-
+        ssize_t initrd_size = get_image_size(info->initrd_filename, NULL);
         if (initrd_size > 0) {
             initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB);
             initrd_offset = alloc_initrd_memory(info, initrd_offset,
                                                 initrd_size);
             initrd_size = load_image_targphys(info->initrd_filename,
-                                              initrd_offset, initrd_size);
+                                              initrd_offset, initrd_size, NULL);
         }
 
         if (initrd_size == -1) {
hw/m68k/an5206.c +1 -1
index d97399b882..ff21d7779e 100644
@@ -82,7 +82,7 @@ static void an5206_init(MachineState *machine)
     }
     if (kernel_size < 0) {
         kernel_size = load_image_targphys(kernel_filename, KERNEL_LOAD_ADDR,
-                                          ram_size - KERNEL_LOAD_ADDR);
+                                          ram_size - KERNEL_LOAD_ADDR, NULL);
         entry = KERNEL_LOAD_ADDR;
     }
     if (kernel_size < 0) {
hw/m68k/mcf5208.c +2 -2
index 75cc076f78..4a585b231d 100644
@@ -351,7 +351,7 @@ static void mcf5208evb_init(MachineState *machine)
             error_report("Could not find ROM image '%s'", machine->firmware);
             exit(1);
         }
-        if (load_image_targphys(fn, 0x0, ROM_SIZE) < 8) {
+        if (load_image_targphys(fn, 0x0, ROM_SIZE, NULL) < 8) {
             error_report("Could not load ROM image '%s'", machine->firmware);
             exit(1);
         }
@@ -380,7 +380,7 @@ static void mcf5208evb_init(MachineState *machine)
     }
     if (kernel_size < 0) {
         kernel_size = load_image_targphys(kernel_filename, 0x40000000,
-                                          ram_size);
+                                          ram_size, NULL);
         entry = 0x40000000;
     }
     if (kernel_size < 0) {
hw/m68k/next-cube.c +1 -1
index 957644b2d1..cfb2b319e5 100644
@@ -1325,7 +1325,7 @@ static void next_cube_init(MachineState *machine)
     memory_region_init_alias(&m->rom2, NULL, "next.rom2", &m->rom, 0x0,
                              0x20000);
     memory_region_add_subregion(sysmem, 0x0, &m->rom2);
-    if (load_image_targphys(bios_name, 0x01000000, 0x20000) < 8) {
+    if (load_image_targphys(bios_name, 0x01000000, 0x20000, NULL) < 8) {
         if (!qtest_enabled()) {
             error_report("Failed to load firmware '%s'.", bios_name);
         }
hw/m68k/q800.c +4 -3
index 793b23f815..b8676feb41 100644
@@ -629,7 +629,7 @@ static void q800_machine_init(MachineState *machine)
 
         /* load initrd */
         if (initrd_filename) {
-            initrd_size = get_image_size(initrd_filename);
+            initrd_size = get_image_size(initrd_filename, NULL);
             if (initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              initrd_filename);
@@ -638,7 +638,7 @@ static void q800_machine_init(MachineState *machine)
 
             initrd_base = (ram_size - initrd_size) & TARGET_PAGE_MASK;
             load_image_targphys(initrd_filename, initrd_base,
-                                ram_size - initrd_base);
+                                ram_size - initrd_base, NULL);
             BOOTINFO2(param_ptr, BI_RAMDISK, initrd_base,
                       initrd_size);
         } else {
@@ -668,7 +668,8 @@ static void q800_machine_init(MachineState *machine)
 
         /* Load MacROM binary */
         if (filename) {
-            bios_size = load_image_targphys(filename, MACROM_ADDR, MACROM_SIZE);
+            bios_size = load_image_targphys(filename, MACROM_ADDR, MACROM_SIZE,
+                                            NULL);
             g_free(filename);
         } else {
             bios_size = -1;
hw/m68k/virt.c +2 -2
index 98cfe43c73..3f65d91556 100644
@@ -283,7 +283,7 @@ static void virt_init(MachineState *machine)
 
         /* load initrd */
         if (initrd_filename) {
-            initrd_size = get_image_size(initrd_filename);
+            initrd_size = get_image_size(initrd_filename, NULL);
             if (initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              initrd_filename);
@@ -292,7 +292,7 @@ static void virt_init(MachineState *machine)
 
             initrd_base = (ram_size - initrd_size) & TARGET_PAGE_MASK;
             load_image_targphys(initrd_filename, initrd_base,
-                                ram_size - initrd_base);
+                                ram_size - initrd_base, NULL);
             BOOTINFO2(param_ptr, BI_RAMDISK, initrd_base,
                       initrd_size);
         } else {
hw/microblaze/boot.c +3 -2
index 4a9c9df318..ec38107f50 100644
@@ -170,7 +170,7 @@ void microblaze_load_kernel(MicroBlazeCPU *cpu, bool is_little_endian,
         /* Not an ELF image nor an u-boot image, try a RAW image.  */
         if (kernel_size < 0) {
             kernel_size = load_image_targphys(kernel_filename, ddr_base,
-                                              ramsize);
+                                              ramsize, NULL);
             boot_info.bootstrap_pc = ddr_base;
             high = (ddr_base + kernel_size + 3) & ~3;
         }
@@ -189,7 +189,8 @@ void microblaze_load_kernel(MicroBlazeCPU *cpu, bool is_little_endian,
             if (initrd_size < 0) {
                 initrd_size = load_image_targphys(initrd_filename,
                                                   boot_info.initrd_start,
-                                                  ramsize - initrd_offset);
+                                                  ramsize - initrd_offset,
+                                                  NULL);
             }
             if (initrd_size < 0) {
                 error_report("could not load initrd '%s'",
hw/mips/boston.c +1 -1
index 149a263bd5..0a3a827b5f 100644
@@ -778,7 +778,7 @@ static void boston_mach_init(MachineState *machine)
 
     if (machine->firmware) {
         fw_size = load_image_targphys(machine->firmware,
-                                      0x1fc00000, 4 * MiB);
+                                      0x1fc00000, 4 * MiB, NULL);
         if (fw_size == -1) {
             error_report("unable to load firmware image '%s'",
                           machine->firmware);
hw/mips/fuloong2e.c +5 -4
index 2a8507b8b0..5fe40eee65 100644
@@ -118,7 +118,7 @@ static uint64_t load_kernel(MIPSCPU *cpu)
     initrd_size = 0;
     initrd_offset = 0;
     if (loaderparams.initrd_filename) {
-        initrd_size = get_image_size(loaderparams.initrd_filename);
+        initrd_size = get_image_size(loaderparams.initrd_filename, NULL);
         if (initrd_size > 0) {
             initrd_offset = ROUND_UP(kernel_high, INITRD_PAGE_SIZE);
             if (initrd_offset + initrd_size > loaderparams.ram_size) {
@@ -127,8 +127,9 @@ static uint64_t load_kernel(MIPSCPU *cpu)
                 exit(1);
             }
             initrd_size = load_image_targphys(loaderparams.initrd_filename,
-                                              initrd_offset,
-                                              loaderparams.ram_size - initrd_offset);
+                                         initrd_offset,
+                                         loaderparams.ram_size - initrd_offset,
+                                         NULL);
         }
         if (initrd_size == (target_ulong) -1) {
             error_report("could not load initial ram disk '%s'",
@@ -264,7 +265,7 @@ static void mips_fuloong2e_init(MachineState *machine)
                                   machine->firmware ?: FULOONG_BIOSNAME);
         if (filename) {
             bios_size = load_image_targphys(filename, 0x1fc00000LL,
-                                            BIOS_SIZE);
+                                            BIOS_SIZE, NULL);
             g_free(filename);
         } else {
             bios_size = -1;
hw/mips/jazz.c +1 -1
index 7fb0b97a38..1d6bdc0091 100644
@@ -245,7 +245,7 @@ static void mips_jazz_init(MachineState *machine,
                               machine->firmware ?: bios_name);
     if (filename) {
         bios_size = load_image_targphys(filename, 0xfff00000LL,
-                                        MAGNUM_BIOS_SIZE);
+                                        MAGNUM_BIOS_SIZE, NULL);
         g_free(filename);
     } else {
         bios_size = -1;
hw/mips/loongson3_virt.c +6 -4
index 672083dec9..77dc895648 100644
@@ -371,7 +371,7 @@ static uint64_t load_kernel(CPUMIPSState *env)
     initrd_size = 0;
     initrd_offset = 0;
     if (loaderparams.initrd_filename) {
-        initrd_size = get_image_size(loaderparams.initrd_filename);
+        initrd_size = get_image_size(loaderparams.initrd_filename, NULL);
         if (initrd_size > 0) {
             initrd_offset = MAX(loader_memmap[LOADER_INITRD].base,
                                 ROUND_UP(kernel_high, INITRD_PAGE_SIZE));
@@ -383,8 +383,9 @@ static uint64_t load_kernel(CPUMIPSState *env)
             }
 
             initrd_size = load_image_targphys(loaderparams.initrd_filename,
-                                              initrd_offset,
-                                              loaderparams.ram_size - initrd_offset);
+                                        initrd_offset,
+                                        loaderparams.ram_size - initrd_offset,
+                                        NULL);
         }
 
         if (initrd_size == (target_ulong) -1) {
@@ -650,7 +651,8 @@ static void mips_loongson3_virt_init(MachineState *machine)
         if (filename) {
             bios_size = load_image_targphys(filename,
                                             virt_memmap[VIRT_BIOS_ROM].base,
-                                            virt_memmap[VIRT_BIOS_ROM].size);
+                                            virt_memmap[VIRT_BIOS_ROM].size,
+                                            NULL);
             g_free(filename);
         } else {
             bios_size = -1;
hw/mips/malta.c +5 -4
index 02da629b5a..806e8d5078 100644
@@ -892,7 +892,7 @@ static uint64_t load_kernel(void)
     initrd_size = 0;
     initrd_offset = 0;
     if (loaderparams.initrd_filename) {
-        initrd_size = get_image_size(loaderparams.initrd_filename);
+        initrd_size = get_image_size(loaderparams.initrd_filename, NULL);
         if (initrd_size > 0) {
             /*
              * The kernel allocates the bootmap memory in the low memory after
@@ -908,8 +908,9 @@ static uint64_t load_kernel(void)
                 exit(1);
             }
             initrd_size = load_image_targphys(loaderparams.initrd_filename,
-                                              initrd_offset,
-                                              loaderparams.ram_size - initrd_offset);
+                                        initrd_offset,
+                                        loaderparams.ram_size - initrd_offset,
+                                        NULL);
         }
         if (initrd_size == (target_ulong) -1) {
             error_report("could not load initial ram disk '%s'",
@@ -1176,7 +1177,7 @@ void mips_malta_init(MachineState *machine)
                                       machine->firmware ?: bios_name);
             if (filename) {
                 bios_size = load_image_targphys(filename, FLASH_ADDRESS,
-                                                BIOS_SIZE);
+                                                BIOS_SIZE, NULL);
                 g_free(filename);
             } else {
                 bios_size = -1;
hw/nubus/nubus-device.c +1 -1
index 7797e61c7f..0d4d6c0d87 100644
@@ -68,7 +68,7 @@ static void nubus_device_realize(DeviceState *dev, Error **errp)
             path = g_strdup(nd->romfile);
         }
 
-        size = get_image_size(path);
+        size = get_image_size(path, NULL);
         if (size < 0) {
             error_setg(errp, "failed to find romfile \"%s\"", nd->romfile);
             g_free(path);
hw/openrisc/boot.c +3 -2
index c81efe8138..db6fea071e 100644
@@ -44,7 +44,8 @@ hwaddr openrisc_load_kernel(ram_addr_t ram_size,
         if (kernel_size < 0) {
             kernel_size = load_image_targphys(kernel_filename,
                                               KERNEL_LOAD_ADDR,
-                                              ram_size - KERNEL_LOAD_ADDR);
+                                              ram_size - KERNEL_LOAD_ADDR,
+                                              NULL);
             high_addr = KERNEL_LOAD_ADDR + kernel_size;
         }
 
@@ -74,7 +75,7 @@ hwaddr openrisc_load_initrd(void *fdt, const char *filename,
 
     size = load_ramdisk(filename, start, mem_size - start);
     if (size < 0) {
-        size = load_image_targphys(filename, start, mem_size - start);
+        size = load_image_targphys(filename, start, mem_size - start, NULL);
         if (size < 0) {
             error_report("could not load ramdisk '%s'", filename);
             exit(1);
hw/pci/pci.c +1 -1
index acc03fd470..738b0c41ae 100644
@@ -2557,7 +2557,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
             path = g_strdup(pdev->romfile);
         }
 
-        size = get_image_size(path);
+        size = get_image_size(path, NULL);
         if (size < 0) {
             error_setg(errp, "failed to find romfile \"%s\"", pdev->romfile);
             return;
hw/ppc/amigaone.c +2 -2
index 47fb016b4a..192474e0ae 100644
@@ -324,7 +324,7 @@ static void amigaone_init(MachineState *machine)
             error_report("Could not find firmware '%s'", machine->firmware);
             exit(1);
         }
-        sz = load_image_targphys(filename, PROM_ADDR, PROM_SIZE);
+        sz = load_image_targphys(filename, PROM_ADDR, PROM_SIZE, NULL);
         if (sz <= 0 || sz > PROM_SIZE) {
             error_report("Could not load firmware '%s'", filename);
             exit(1);
@@ -413,7 +413,7 @@ static void amigaone_init(MachineState *machine)
         loadaddr = ROUND_UP(loadaddr + 4 * MiB, 4 * KiB);
         loadaddr = MAX(loadaddr, INITRD_MIN_ADDR);
         sz = load_image_targphys(machine->initrd_filename, loadaddr,
-                                 bi->bd_info - loadaddr);
+                                 bi->bd_info - loadaddr, NULL);
         if (sz <= 0) {
             error_report("Could not load initrd '%s'",
                          machine->initrd_filename);
hw/ppc/e500.c +3 -2
index 3d69428f31..418e1bb2fb 100644
@@ -1227,7 +1227,7 @@ void ppce500_init(MachineState *machine)
         kernel_base = cur_base;
         kernel_size = load_image_targphys(machine->kernel_filename,
                                           cur_base,
-                                          machine->ram_size - cur_base);
+                                          machine->ram_size - cur_base, NULL);
         if (kernel_size < 0) {
             error_report("could not load kernel '%s'",
                          machine->kernel_filename);
@@ -1241,7 +1241,8 @@ void ppce500_init(MachineState *machine)
     if (machine->initrd_filename) {
         initrd_base = (cur_base + INITRD_LOAD_PAD) & ~INITRD_PAD_MASK;
         initrd_size = load_image_targphys(machine->initrd_filename, initrd_base,
-                                          machine->ram_size - initrd_base);
+                                          machine->ram_size - initrd_base,
+                                          NULL);
 
         if (initrd_size < 0) {
             error_report("could not load initial ram disk '%s'",
hw/ppc/mac_newworld.c +6 -3
index 0b6e096116..004efc6b97 100644
@@ -188,7 +188,8 @@ static void ppc_core99_init(MachineState *machine)
 
         if (bios_size <= 0) {
             /* or load binary ROM image */
-            bios_size = load_image_targphys(filename, PROM_BASE, PROM_SIZE);
+            bios_size = load_image_targphys(filename, PROM_BASE, PROM_SIZE,
+                                            NULL);
         }
         g_free(filename);
     }
@@ -210,7 +211,8 @@ static void ppc_core99_init(MachineState *machine)
         if (kernel_size < 0) {
             kernel_size = load_image_targphys(machine->kernel_filename,
                                               kernel_base,
-                                              machine->ram_size - kernel_base);
+                                              machine->ram_size - kernel_base,
+                                              NULL);
         }
         if (kernel_size < 0) {
             error_report("could not load kernel '%s'",
@@ -222,7 +224,8 @@ static void ppc_core99_init(MachineState *machine)
             initrd_base = TARGET_PAGE_ALIGN(kernel_base + kernel_size + KERNEL_GAP);
             initrd_size = load_image_targphys(machine->initrd_filename,
                                               initrd_base,
-                                              machine->ram_size - initrd_base);
+                                              machine->ram_size - initrd_base,
+                                              NULL);
             if (initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              machine->initrd_filename);
hw/ppc/mac_oldworld.c +6 -3
index 40ae936ad8..c7e44d49b0 100644
@@ -143,7 +143,8 @@ static void ppc_heathrow_init(MachineState *machine)
 
         if (bios_size <= 0) {
             /* or if could not load ELF try loading a binary ROM image */
-            bios_size = load_image_targphys(filename, PROM_BASE, PROM_SIZE);
+            bios_size = load_image_targphys(filename, PROM_BASE, PROM_SIZE,
+                                            NULL);
             bios_addr = PROM_BASE;
         }
         g_free(filename);
@@ -166,7 +167,8 @@ static void ppc_heathrow_init(MachineState *machine)
         if (kernel_size < 0) {
             kernel_size = load_image_targphys(machine->kernel_filename,
                                               kernel_base,
-                                              machine->ram_size - kernel_base);
+                                              machine->ram_size - kernel_base,
+                                              NULL);
         }
         if (kernel_size < 0) {
             error_report("could not load kernel '%s'",
@@ -179,7 +181,8 @@ static void ppc_heathrow_init(MachineState *machine)
                                             KERNEL_GAP);
             initrd_size = load_image_targphys(machine->initrd_filename,
                                               initrd_base,
-                                              machine->ram_size - initrd_base);
+                                              machine->ram_size - initrd_base,
+                                              NULL);
             if (initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              machine->initrd_filename);
hw/ppc/pegasos2.c +3 -2
index 93696ed381..a9e706644c 100644
@@ -197,7 +197,8 @@ static void pegasos_init(MachineState *machine)
     sz = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                   ELFDATA2MSB, PPC_ELF_MACHINE, 0, 0);
     if (sz <= 0) {
-        sz = load_image_targphys(filename, pm->vof ? 0 : prom_addr, PROM_SIZE);
+        sz = load_image_targphys(filename, pm->vof ? 0 : prom_addr, PROM_SIZE,
+                                 NULL);
     }
     if (sz <= 0 || sz > PROM_SIZE) {
         error_report("Could not load firmware '%s'", filename);
@@ -301,7 +302,7 @@ static void pegasos_init(MachineState *machine)
         pm->initrd_addr = ROUND_UP(pm->initrd_addr, 4);
         pm->initrd_addr = MAX(pm->initrd_addr, INITRD_MIN_ADDR);
         sz = load_image_targphys(machine->initrd_filename, pm->initrd_addr,
-                                 machine->ram_size - pm->initrd_addr);
+                                 machine->ram_size - pm->initrd_addr, NULL);
         if (sz <= 0) {
             error_report("Could not load initrd '%s'",
                          machine->initrd_filename);
hw/ppc/pnv.c +6 -3
index f0469cdb8b..1c0dadda87 100644
@@ -1068,7 +1068,8 @@ static void pnv_init(MachineState *machine)
         exit(1);
     }
 
-    fw_size = load_image_targphys(fw_filename, pnv->fw_load_addr, FW_MAX_SIZE);
+    fw_size = load_image_targphys(fw_filename, pnv->fw_load_addr, FW_MAX_SIZE,
+                                  NULL);
     if (fw_size < 0) {
         error_report("Could not load OPAL firmware '%s'", fw_filename);
         exit(1);
@@ -1080,7 +1081,8 @@ static void pnv_init(MachineState *machine)
         long kernel_size;
 
         kernel_size = load_image_targphys(machine->kernel_filename,
-                                          KERNEL_LOAD_ADDR, KERNEL_MAX_SIZE);
+                                          KERNEL_LOAD_ADDR, KERNEL_MAX_SIZE,
+                                          NULL);
         if (kernel_size < 0) {
             error_report("Could not load kernel '%s'",
                          machine->kernel_filename);
@@ -1092,7 +1094,8 @@ static void pnv_init(MachineState *machine)
     if (machine->initrd_filename) {
         pnv->initrd_base = INITRD_LOAD_ADDR;
         pnv->initrd_size = load_image_targphys(machine->initrd_filename,
-                                  pnv->initrd_base, INITRD_MAX_SIZE);
+                                               pnv->initrd_base,
+                                               INITRD_MAX_SIZE, NULL);
         if (pnv->initrd_size < 0) {
             error_report("Could not load initial ram disk '%s'",
                          machine->initrd_filename);
hw/ppc/ppc440_bamboo.c +2 -1
index 6fff0d8afb..7c66912c10 100644
@@ -242,7 +242,8 @@ static void bamboo_init(MachineState *machine)
     /* Load initrd. */
     if (initrd_filename) {
         initrd_size = load_image_targphys(initrd_filename, RAMDISK_ADDR,
-                                          machine->ram_size - RAMDISK_ADDR);
+                                          machine->ram_size - RAMDISK_ADDR,
+                                          NULL);
 
         if (initrd_size < 0) {
             error_report("could not load ram disk '%s' at %x",
hw/ppc/prep.c +5 -3
index c730cb3429..0759e95cb6 100644
@@ -280,7 +280,7 @@ static void ibm_40p_init(MachineState *machine)
     bios_size = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                          ELFDATA2MSB, PPC_ELF_MACHINE, 0, 0);
     if (bios_size < 0) {
-        bios_size = load_image_targphys(filename, BIOS_ADDR, BIOS_SIZE);
+        bios_size = load_image_targphys(filename, BIOS_ADDR, BIOS_SIZE, NULL);
     }
     if (bios_size < 0 || bios_size > BIOS_SIZE) {
         error_report("Could not load bios image '%s'", filename);
@@ -379,7 +379,8 @@ static void ibm_40p_init(MachineState *machine)
         kernel_base = KERNEL_LOAD_ADDR;
         kernel_size = load_image_targphys(machine->kernel_filename,
                                           kernel_base,
-                                          machine->ram_size - kernel_base);
+                                          machine->ram_size - kernel_base,
+                                          NULL);
         if (kernel_size < 0) {
             error_report("could not load kernel '%s'",
                          machine->kernel_filename);
@@ -392,7 +393,8 @@ static void ibm_40p_init(MachineState *machine)
             initrd_base = INITRD_LOAD_ADDR;
             initrd_size = load_image_targphys(machine->initrd_filename,
                                               initrd_base,
-                                              machine->ram_size - initrd_base);
+                                              machine->ram_size - initrd_base,
+                                              NULL);
             if (initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              machine->initrd_filename);
hw/ppc/sam460ex.c +2 -1
index ee31bd8f34..68d3eacbff 100644
@@ -494,7 +494,8 @@ static void sam460ex_init(MachineState *machine)
     if (machine->initrd_filename) {
         initrd_size = load_image_targphys(machine->initrd_filename,
                                           RAMDISK_ADDR,
-                                          machine->ram_size - RAMDISK_ADDR);
+                                          machine->ram_size - RAMDISK_ADDR,
+                                          NULL);
         if (initrd_size < 0) {
             error_report("could not load ram disk '%s' at %x",
                     machine->initrd_filename, RAMDISK_ADDR);
hw/ppc/spapr.c +4 -4
index 6764730beb..cdf2fdeadc 100644
@@ -2854,7 +2854,7 @@ static void spapr_machine_init(MachineState *machine)
         error_report("Could not find LPAR firmware '%s'", bios_name);
         exit(1);
     }
-    fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
+    fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE, NULL);
     if (fw_size <= 0) {
         error_report("Could not load LPAR firmware '%s'", filename);
         exit(1);
@@ -3115,9 +3115,9 @@ static void spapr_machine_init(MachineState *machine)
             spapr->initrd_base = (spapr->kernel_addr + spapr->kernel_size
                                   + 0x1ffff) & ~0xffff;
             spapr->initrd_size = load_image_targphys(initrd_filename,
-                                                     spapr->initrd_base,
-                                                     load_limit
-                                                     - spapr->initrd_base);
+                                                spapr->initrd_base,
+                                                load_limit - spapr->initrd_base,
+                                                NULL);
             if (spapr->initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              initrd_filename);
hw/ppc/virtex_ml507.c +3 -2
index c9969ae48a..00d9ab7509 100644
@@ -253,7 +253,7 @@ static void virtex_init(MachineState *machine)
             /* If we failed loading ELF's try a raw image.  */
             kernel_size = load_image_targphys(kernel_filename,
                                               boot_offset,
-                                              machine->ram_size);
+                                              machine->ram_size, NULL);
             boot_info.bootstrap_pc = boot_offset;
             high = boot_info.bootstrap_pc + kernel_size + 8192;
         }
@@ -264,7 +264,8 @@ static void virtex_init(MachineState *machine)
         if (machine->initrd_filename) {
             initrd_base = high = ROUND_UP(high, 4);
             initrd_size = load_image_targphys(machine->initrd_filename,
-                                              high, machine->ram_size - high);
+                                              high, machine->ram_size - high,
+                                              NULL);
 
             if (initrd_size < 0) {
                 error_report("couldn't load ram disk '%s'",
hw/riscv/boot.c +4 -3
index 828a867be3..f38078c8c2 100644
@@ -172,7 +172,8 @@ target_ulong riscv_load_firmware(const char *firmware_filename,
 
     firmware_size = load_image_targphys_as(firmware_filename,
                                            *firmware_load_addr,
-                                           current_machine->ram_size, NULL);
+                                           current_machine->ram_size, NULL,
+                                           NULL);
 
     if (firmware_size > 0) {
         return *firmware_load_addr + firmware_size;
@@ -207,7 +208,7 @@ static void riscv_load_initrd(MachineState *machine, RISCVBootInfo *info)
 
     size = load_ramdisk(filename, start, mem_size - start);
     if (size == -1) {
-        size = load_image_targphys(filename, start, mem_size - start);
+        size = load_image_targphys(filename, start, mem_size - start, NULL);
         if (size == -1) {
             error_report("could not load ramdisk '%s'", filename);
             exit(1);
@@ -262,7 +263,7 @@ void riscv_load_kernel(MachineState *machine,
     }
 
     kernel_size = load_image_targphys_as(kernel_filename, kernel_start_addr,
-                                         current_machine->ram_size, NULL);
+                                         current_machine->ram_size, NULL, NULL);
     if (kernel_size > 0) {
         info->kernel_size = kernel_size;
         info->image_low_addr = kernel_start_addr;
hw/rx/rx-gdbsim.c +1 -1
index 5b9004e9e1..ed91c63178 100644
@@ -63,7 +63,7 @@ static void rx_load_image(RXCPU *cpu, const char *filename,
     long kernel_size;
     int i;
 
-    kernel_size = load_image_targphys(filename, start, size);
+    kernel_size = load_image_targphys(filename, start, size, NULL);
     if (kernel_size < 0) {
         fprintf(stderr, "qemu: could not load kernel '%s'\n", filename);
         exit(1);
hw/s390x/ipl.c +5 -3
index 2f082396c7..3843d2a850 100644
@@ -169,7 +169,7 @@ static void s390_ipl_realize(DeviceState *dev, Error **errp)
         } else {
             /* Try to load non-ELF file */
             bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
-                                            4096);
+                                            4096, NULL);
             ipl->bios_start_addr = ZIPL_IMAGE_START;
         }
         g_free(bios_filename);
@@ -188,7 +188,8 @@ static void s390_ipl_realize(DeviceState *dev, Error **errp)
                                &pentry, NULL,
                                NULL, NULL, ELFDATA2MSB, EM_S390, 0, 0);
         if (kernel_size < 0) {
-            kernel_size = load_image_targphys(ipl->kernel, 0, ms->ram_size);
+            kernel_size = load_image_targphys(ipl->kernel, 0, ms->ram_size,
+                                              NULL);
             if (kernel_size < 0) {
                 error_setg(errp, "could not load kernel '%s'", ipl->kernel);
                 return;
@@ -247,7 +248,8 @@ static void s390_ipl_realize(DeviceState *dev, Error **errp)
                 initrd_offset += 0x100000;
             }
             initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
-                                              ms->ram_size - initrd_offset);
+                                              ms->ram_size - initrd_offset,
+                                              NULL);
             if (initrd_size == -1) {
                 error_setg(errp, "could not load initrd '%s'", ipl->initrd);
                 return;
hw/sh4/r2d.c +5 -3
index d68c94e82e..010be6d539 100644
@@ -329,8 +329,9 @@ static void r2d_init(MachineState *machine)
         int kernel_size;
 
         kernel_size = load_image_targphys(kernel_filename,
-                                          SDRAM_BASE + LINUX_LOAD_OFFSET,
-                                          INITRD_LOAD_OFFSET - LINUX_LOAD_OFFSET);
+                                        SDRAM_BASE + LINUX_LOAD_OFFSET,
+                                        INITRD_LOAD_OFFSET - LINUX_LOAD_OFFSET,
+                                        NULL);
         if (kernel_size < 0) {
             error_report("qemu: could not load kernel '%s'", kernel_filename);
             exit(1);
@@ -350,7 +351,8 @@ static void r2d_init(MachineState *machine)
 
         initrd_size = load_image_targphys(initrd_filename,
                                           SDRAM_BASE + INITRD_LOAD_OFFSET,
-                                          SDRAM_SIZE - INITRD_LOAD_OFFSET);
+                                          SDRAM_SIZE - INITRD_LOAD_OFFSET,
+                                          NULL);
 
         if (initrd_size < 0) {
             error_report("qemu: could not load initrd '%s'", initrd_filename);
hw/smbios/smbios.c +1 -1
index 13e21a9c43..7558b2ad83 100644
@@ -1340,7 +1340,7 @@ void smbios_entry_add(QemuOpts *opts, Error **errp)
             return;
         }
 
-        size = get_image_size(val);
+        size = get_image_size(val, NULL);
         if (size == -1 || size < sizeof(struct smbios_structure_header)) {
             error_setg(errp, "Cannot read SMBIOS file %s", val);
             return;
hw/sparc/leon3.c +2 -2
index 09d2cec488..631c6113b5 100644
@@ -349,7 +349,7 @@ static void leon3_generic_hw_init(MachineState *machine)
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
 
     if (filename) {
-        bios_size = get_image_size(filename);
+        bios_size = get_image_size(filename, NULL);
     } else {
         bios_size = -1;
     }
@@ -360,7 +360,7 @@ static void leon3_generic_hw_init(MachineState *machine)
     }
 
     if (bios_size > 0) {
-        ret = load_image_targphys(filename, LEON3_PROM_OFFSET, bios_size);
+        ret = load_image_targphys(filename, LEON3_PROM_OFFSET, bios_size, NULL);
         if (ret < 0 || ret > prom_size) {
             error_report("could not load prom '%s'", filename);
             exit(1);
hw/sparc/sun4m.c +5 -3
index 8ac7e625ef..53d7ae08ae 100644
@@ -245,7 +245,8 @@ static unsigned long sun4m_load_kernel(const char *kernel_filename,
         if (kernel_size < 0)
             kernel_size = load_image_targphys(kernel_filename,
                                               KERNEL_LOAD_ADDR,
-                                              RAM_size - KERNEL_LOAD_ADDR);
+                                              RAM_size - KERNEL_LOAD_ADDR,
+                                              NULL);
         if (kernel_size < 0) {
             error_report("could not load kernel '%s'", kernel_filename);
             exit(1);
@@ -256,7 +257,8 @@ static unsigned long sun4m_load_kernel(const char *kernel_filename,
         if (initrd_filename) {
             *initrd_size = load_image_targphys(initrd_filename,
                                                INITRD_LOAD_ADDR,
-                                               RAM_size - INITRD_LOAD_ADDR);
+                                               RAM_size - INITRD_LOAD_ADDR,
+                                               NULL);
             if ((int)*initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              initrd_filename);
@@ -700,7 +702,7 @@ static void prom_init(hwaddr addr, const char *bios_name)
                        translate_prom_address, &addr, NULL,
                        NULL, NULL, NULL, ELFDATA2MSB, EM_SPARC, 0, 0);
         if (ret < 0 || ret > PROM_SIZE_MAX) {
-            ret = load_image_targphys(filename, addr, PROM_SIZE_MAX);
+            ret = load_image_targphys(filename, addr, PROM_SIZE_MAX, NULL);
         }
         g_free(filename);
     } else {
hw/sparc64/sun4u.c +4 -3
index e9f9b0a4cb..82c3e7c855 100644
@@ -182,7 +182,8 @@ static uint64_t sun4u_load_kernel(const char *kernel_filename,
         if (kernel_size < 0) {
             kernel_size = load_image_targphys(kernel_filename,
                                               KERNEL_LOAD_ADDR,
-                                              RAM_size - KERNEL_LOAD_ADDR);
+                                              RAM_size - KERNEL_LOAD_ADDR,
+                                              NULL);
         }
         if (kernel_size < 0) {
             error_report("could not load kernel '%s'", kernel_filename);
@@ -195,7 +196,7 @@ static uint64_t sun4u_load_kernel(const char *kernel_filename,
 
             *initrd_size = load_image_targphys(initrd_filename,
                                                *initrd_addr,
-                                               RAM_size - *initrd_addr);
+                                               RAM_size - *initrd_addr, NULL);
             if ((int)*initrd_size < 0) {
                 error_report("could not load initial ram disk '%s'",
                              initrd_filename);
@@ -437,7 +438,7 @@ static void prom_init(hwaddr addr, const char *bios_name)
         ret = load_elf(filename, NULL, translate_prom_address, &addr,
                        NULL, NULL, NULL, NULL, ELFDATA2MSB, EM_SPARCV9, 0, 0);
         if (ret < 0 || ret > PROM_SIZE_MAX) {
-            ret = load_image_targphys(filename, addr, PROM_SIZE_MAX);
+            ret = load_image_targphys(filename, addr, PROM_SIZE_MAX, NULL);
         }
         g_free(filename);
     } else {
hw/xtensa/xtfpga.c +2 -1
index 55de1a7a07..db3b015549 100644
@@ -381,7 +381,8 @@ static void xtfpga_init(const XtfpgaBoardDesc *board, MachineState *machine)
             if (initrd_size < 0) {
                 initrd_size = load_image_targphys(initrd_filename,
                                                   cur_lowmem,
-                                                  lowmem_end - cur_lowmem);
+                                                  lowmem_end - cur_lowmem,
+                                                  NULL);
             }
             if (initrd_size < 0) {
                 error_report("could not load initrd '%s'", initrd_filename);
include/hw/loader.h +5 -3
index c96b5e141c..d035e72748 100644
@@ -1,6 +1,7 @@
 #ifndef LOADER_H
 #define LOADER_H
 #include "hw/nvram/fw_cfg.h"
+#include "qemu/typedefs.h"
 
 /* loader.c */
 /**
@@ -10,7 +11,7 @@
  * Returns the size of the image file on success, -1 otherwise.
  * On error, errno is also set as appropriate.
  */
-int64_t get_image_size(const char *filename);
+int64_t get_image_size(const char *filename, Error **errp);
 /**
  * load_image_size: load an image file into specified buffer
  * @filename: Path to the image file
@@ -41,7 +42,8 @@ ssize_t load_image_size(const char *filename, void *addr, size_t size);
  * Returns the size of the loaded image on success, -1 otherwise.
  */
 ssize_t load_image_targphys_as(const char *filename,
-                               hwaddr addr, uint64_t max_sz, AddressSpace *as);
+                               hwaddr addr, uint64_t max_sz, AddressSpace *as,
+                               Error **errp);
 
 /**load_targphys_hex_as:
  * @filename: Path to the .hex file
@@ -61,7 +63,7 @@ ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
  * an AddressSpace.
  */
 ssize_t load_image_targphys(const char *filename, hwaddr,
-                            uint64_t max_sz);
+                            uint64_t max_sz, Error **errp);
 
 /**
  * load_image_mr: load an image into a memory region
system/device_tree.c +1 -1
index aa3fe9516f..7850b90fa7 100644
@@ -83,7 +83,7 @@ void *load_device_tree(const char *filename_path, int *sizep)
     void *fdt = NULL;
 
     *sizep = 0;
-    dt_size = get_image_size(filename_path);
+    dt_size = get_image_size(filename_path, NULL);
     if (dt_size < 0) {
         error_report("Unable to get size of device tree file '%s'",
                      filename_path);
Signature · 1 line
2.51.0
Reviewed-by on v9 2/6
#4 lore raw
Quoted 9 lines · Vishal Chourasia

Add Error **errp parameter to load_image_targphys(), load_image_targphys_as(), and get_image_size() to enable better error reporting when image loading fails.

Pass NULL for errp in all existing call sites to maintain current behavior. No functional change intended in this patch.

All tests pass for me, and changes look good.

Thanks,
- Aditya G

Tested-by Aditya Gupta
#5 lore raw
Quoted 5 lines · Aditya Gupta
Quoted 4 lines · Vishal Chourasia

Add Error **errp parameter to load_image_targphys(), load_image_targphys_as(), and get_image_size() to enable better error reporting when image loading fails.

Quoted 7 lines
Quoted 5 lines

Pass NULL for errp in all existing call sites to maintain current behavior. No functional change intended in this patch.

All tests pass for me, and changes look good.

Does that imply:

?

Quoted 7 lines

Thanks,
- Aditya G

Tested-by
#6 lore raw
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Aditya Gupta
Quoted 9 lines · Vishal Chourasia

Add Error **errp parameter to load_image_targphys(), load_image_targphys_as(), and get_image_size() to enable better error reporting when image loading fails.

Pass NULL for errp in all existing call sites to maintain current behavior. No functional change intended in this patch.

Quoted 7 lines
Quoted 1 line

All tests pass for me, and changes look good.

Does that imply:

?

Yes Philippe, can add Tested-by.

These are the tests I ran on the series:

* PowerPC tests (manually starting a VM + check-functional-ppc64)
* Build test all patches, so no patch introduces a compile error
* Gitlab pipeline passing

Didn't test all archs, but i don't see much functional changes to any arch other than ppc with the series.

Thanks,

- Aditya G

Quoted 9 lines
Quoted 8 lines

Thanks,
- Aditya G

Reviewed-by on v9 2/6
#7 lore raw
In reply to Vishal Chourasia
Quoted 54 lines · Vishal Chourasia

Add Error **errp parameter to load_image_targphys(), load_image_targphys_as(), and get_image_size() to enable better error reporting when image loading fails.

Pass NULL for errp in all existing call sites to maintain current behavior. No functional change intended in this patch.

---
  hw/alpha/dp264.c         |  4 ++--
  hw/arm/armv7m.c          |  2 +-
  hw/arm/boot.c            |  5 +++--
  hw/arm/digic_boards.c    |  2 +-
  hw/arm/highbank.c        |  3 ++-
  hw/arm/raspi.c           |  2 +-
  hw/arm/vexpress.c        |  2 +-
  hw/core/generic-loader.c |  3 ++-
  hw/core/guest-loader.c   |  2 +-
  hw/core/loader.c         | 16 +++++++++-------
  hw/hppa/machine.c        |  5 +++--
  hw/i386/multiboot.c      |  2 +-
  hw/i386/x86-common.c     |  4 ++--
  hw/ipmi/ipmi_bmc_sim.c   |  2 +-
  hw/loongarch/boot.c      |  5 ++---
  hw/m68k/an5206.c         |  2 +-
  hw/m68k/mcf5208.c        |  4 ++--
  hw/m68k/next-cube.c      |  2 +-
  hw/m68k/q800.c           |  7 ++++---
  hw/m68k/virt.c           |  4 ++--
  hw/microblaze/boot.c     |  5 +++--
  hw/mips/boston.c         |  2 +-
  hw/mips/fuloong2e.c      |  9 +++++----
  hw/mips/jazz.c           |  2 +-
  hw/mips/loongson3_virt.c | 10 ++++++----
  hw/mips/malta.c          |  9 +++++----
  hw/nubus/nubus-device.c  |  2 +-
  hw/openrisc/boot.c       |  5 +++--
  hw/pci/pci.c             |  2 +-
  hw/ppc/amigaone.c        |  4 ++--
  hw/ppc/e500.c            |  5 +++--
  hw/ppc/mac_newworld.c    |  9 ++++++---
  hw/ppc/mac_oldworld.c    |  9 ++++++---
  hw/ppc/pegasos2.c        |  5 +++--
  hw/ppc/pnv.c             |  9 ++++++---
  hw/ppc/ppc440_bamboo.c   |  3 ++-
  hw/ppc/prep.c            |  8 +++++---
  hw/ppc/sam460ex.c        |  3 ++-
  hw/ppc/spapr.c           |  8 ++++----
  hw/ppc/virtex_ml507.c    |  5 +++--
  hw/riscv/boot.c          |  7 ++++---
  hw/rx/rx-gdbsim.c        |  2 +-
  hw/s390x/ipl.c           |  8 +++++---
  hw/sh4/r2d.c             |  8 +++++---
Quoted 8 lines
  hw/smbios/smbios.c       |  2 +-
  hw/sparc/leon3.c         |  4 ++--
  hw/sparc/sun4m.c         |  8 +++++---
  hw/sparc64/sun4u.c       |  7 ++++---
  hw/xtensa/xtfpga.c       |  3 ++-
  include/hw/loader.h      |  8 +++++---
  system/device_tree.c     |  2 +-
  51 files changed, 145 insertions(+), 106 deletions(-)
#8 lore raw

PATCH v9 3/6 core/loader: improve error handling in image loading functions

In reply to Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

---
 hw/core/loader.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
hw/core/loader.c +15 -1
index 7aca4989ef..48dd4e7b33 100644
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, Error **errp)
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", filename);
+        return -1;
+    }
     close(fd);
     return size;
 }
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char *filename,
                                hwaddr addr, uint64_t max_sz, AddressSpace *as,
                                Error **errp)
 {
+    ERRP_GUARD();
     ssize_t size;
 
     size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (*errp) {
+        return -1;
+    }
+
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 " MiB)",
+                   filename, max_sz / MiB);
         return -1;
     }
+
     if (size > 0) {
         if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
+            error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
+                       filename, addr);
             return -1;
         }
     }
Signature · 1 line
2.51.0
Reviewed-by on v9 3/6
#10 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

In reply to Vishal Chourasia
Quoted 24 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

---
  hw/core/loader.c | 16 +++++++++++++++-
  1 file changed, 15 insertions(+), 1 deletion(-)
hw/core/loader.c +4 -0
index 7aca4989ef..48dd4e7b33 100644
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, Error **errp)
      if (fd < 0)
          return -1;
      size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", filename);
+        return -1;
+    }
      close(fd);
      return size;
  }

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 140

Quoted 9 lines
hw/core/loader.c +2 -1
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char *filename,
                                 hwaddr addr, uint64_t max_sz, AddressSpace *as,
                                 Error **errp)
  {
+    ERRP_GUARD();
      ssize_t size;
 
      size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (*errp) {

Although safe with ERRP_GUARD(), we try to avoid checking *errp as a pattern. I'll update (no need to repost) as:

      if (size < 0) {

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 142

Quoted 2 lines
hw/core/loader.c +2 -0
+        return -1;
+    }

else

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 148

Quoted 5 lines
hw/core/loader.c +3 -0
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 " MiB)",
+                   filename, max_sz / MiB);
          return -1;
      }

else

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 156

Quoted 7 lines
hw/core/loader.c +2 -0
      if (size > 0) {
          if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
+            error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
+                       filename, addr);
              return -1;
          }
      }
#11 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

In reply to Vishal Chourasia
Quoted 29 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

---
hw/core/loader.c | 16 +++++++++++++++-

1 file changed, 15 insertions(+), 1 deletion(-)
hw/core/loader.c +5 -0
index 7aca4989ef..48dd4e7b33 100644
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, Error **errp)
    if (fd < 0)
        return -1;
    size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", filename);
+        return -1;
+    }
    close(fd);
    return size;
 }
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char *filename,
                               hwaddr addr, uint64_t max_sz, AddressSpace *as,
                               Error **errp)
 {
+    ERRP_GUARD();
    ssize_t size;

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 146

Quoted 10 lines
hw/core/loader.c +7 -1
 
    size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (*errp) {
+        return -1;
+    }
+
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 " MiB)",
+                   filename, max_sz / MiB);

MiB is arbitrary here. This function is used to load all kinds of images such as ROMs which may be 64k-2MB or even executables in generic loader that can be a few kilobytes. This might result in errors saying max size is 0 MiB if the allowed size is less than a MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division discards fractions. Do we have a function to pretty print sizes or maybe this should be left as bytes or at most kilobytes?

Regards,
BALATON Zoltan

Quoted 11 lines
hw/core/loader.c +3 -0
        return -1;
    }
+
    if (size > 0) {
        if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
+            error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
+                       filename, addr);
            return -1;
        }
    }
#12 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

Hi Balaton, Philippe

Quoted 33 lines · BALATON Zoltan
Quoted 32 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

---
hw/core/loader.c | 16 +++++++++++++++-

1 file changed, 15 insertions(+), 1 deletion(-)
hw/core/loader.c +4 -0
index 7aca4989ef..48dd4e7b33 100644
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, Error 
 **errp)
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", filename);
+        return -1;
+    }
     close(fd);
     return size;

}

hw/core/loader.c +1 -0
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char 
 *filename,
                                hwaddr addr, uint64_t max_sz, AddressSpace *as,
                                Error **errp)
 {
+    ERRP_GUARD();
     ssize_t size;
 
Quoted 13 lines

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 146

Quoted 10 lines
hw/core/loader.c +7 -1
     size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (*errp) {
+        return -1;
+    }
+
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 " 
 MiB)",
+                   filename, max_sz / MiB);

MiB is arbitrary here. This function is used to load all kinds of images such as ROMs which may be 64k-2MB or even executables in generic loader that can be a few kilobytes. This might result in errors saying max size is 0 MiB if the allowed size is less than a MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division discards fractions. Do we have a function to pretty print sizes or maybe this should be left as bytes or at most kilobytes?

Yes, for images sizes less than a megabyte.
Perhaps we can leave it at Kilobytes

I will send out another version.
I will also address the *errp vs size<0 comment from Philippe.

Thanks,
vishalc

Quoted 14 lines

Regards,
BALATON Zoltan

Quoted 11 lines
hw/core/loader.c +3 -0
         return -1;
     }
+
     if (size > 0) {
         if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
+            error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
+                       filename, addr);
             return -1;

        }
    }

#13 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

Quoted 49 lines · Vishal Chourasia

Hi Balaton, Philippe

Quoted 33 lines · BALATON Zoltan
Quoted 32 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

---
hw/core/loader.c | 16 +++++++++++++++-

1 file changed, 15 insertions(+), 1 deletion(-)
hw/core/loader.c +4 -0
index 7aca4989ef..48dd4e7b33 100644
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, Error 
 **errp)
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", filename);
+        return -1;
+    }
     close(fd);
     return size;

}

hw/core/loader.c +1 -0
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char 
 *filename,
                                hwaddr addr, uint64_t max_sz, AddressSpace *as,
                                Error **errp)
 {
+    ERRP_GUARD();
     ssize_t size;
 
Quoted 13 lines

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 146

Quoted 10 lines
hw/core/loader.c +7 -1
     size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (*errp) {
+        return -1;
+    }
+
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 " 
 MiB)",
+                   filename, max_sz / MiB);

MiB is arbitrary here. This function is used to load all kinds of images such as ROMs which may be 64k-2MB or even executables in generic loader that can be a few kilobytes. This might result in errors saying max size is 0 MiB if the allowed size is less than a MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division discards fractions. Do we have a function to pretty print sizes or maybe this should be left as bytes or at most kilobytes?

Quoted 4 lines

Yes, for images sizes less than a megabyte.
Perhaps we can leave it at Kilobytes

I will send out another version.

If so, then please use qemu_strtosz().

Quoted 19 lines

I will also address the *errp vs size<0 comment from Philippe.

Thanks,
vishalc

Quoted 14 lines

Regards,
BALATON Zoltan

Quoted 11 lines
hw/core/loader.c +3 -0
         return -1;
     }
+
     if (size > 0) {
         if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
+            error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
+                       filename, addr);
             return -1;

        }
    }

#14 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

Hi Philippe,

Quoted 50 lines · Philippe Mathieu-Daudé
Quoted 49 lines · Vishal Chourasia

Hi Balaton, Philippe

Quoted 46 lines · BALATON Zoltan
Quoted 32 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

---
hw/core/loader.c | 16 +++++++++++++++-

1 file changed, 15 insertions(+), 1 deletion(-)
hw/core/loader.c +4 -0
index 7aca4989ef..48dd4e7b33 100644
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, 
 Error **errp)
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", filename);
+        return -1;
+    }
     close(fd);
     return size;

}

hw/core/loader.c +1 -0
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char 
 *filename,
                                hwaddr addr, uint64_t max_sz, AddressSpace *as,
                                Error **errp)
 {
+    ERRP_GUARD();
     ssize_t size;
 

hw/core/loader.c ssize_t load_image_targphys_as(const char · line 148

Quoted 10 lines
hw/core/loader.c +7 -1
     size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (*errp) {
+        return -1;
+    }
+
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 
 " MiB)",
+                   filename, max_sz / MiB);

MiB is arbitrary here. This function is used to load all kinds of images such as ROMs which may be 64k-2MB or even executables in generic loader that can be a few kilobytes. This might result in errors saying max size is 0 MiB if the allowed size is less than a MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division discards fractions. Do we have a function to pretty print sizes or maybe this should be left as bytes or at most kilobytes?

Quoted 6 lines
Quoted 4 lines

Yes, for images sizes less than a megabyte.
Perhaps we can leave it at Kilobytes

I will send out another version.

If so, then please use qemu_strtosz().

There is another function size_to_str() suggested by Peter I was planning to use it, as it was doing the expected job.

It seems qemu_strtosz() converts size strings to bytes.

Thanks, vishalc

Quoted 21 lines
Quoted 20 lines

I will also address the *errp vs size<0 comment from Philippe.

Thanks,
vishalc

Quoted 14 lines

Regards,
BALATON Zoltan

Quoted 11 lines
hw/core/loader.c +3 -0
         return -1;
     }
+
     if (size > 0) {
         if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
+            error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
+                       filename, addr);
             return -1;

        }
    }

#15 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

Quoted 56 lines · Vishal Chourasia

Hi Philippe,

Quoted 53 lines · Philippe Mathieu-Daudé
Quoted 52 lines · Vishal Chourasia

Hi Balaton, Philippe

Quoted 33 lines · BALATON Zoltan
Quoted 32 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

---
hw/core/loader.c | 16 +++++++++++++++-

1 file changed, 15 insertions(+), 1 deletion(-)
hw/core/loader.c +4 -0
index 7aca4989ef..48dd4e7b33 100644
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, 
 Error **errp)
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", filename);
+        return -1;
+    }
     close(fd);
     return size;

}

hw/core/loader.c +1 -0
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char 
 *filename,
                                hwaddr addr, uint64_t max_sz, AddressSpace *as,
                                Error **errp)
 {
+    ERRP_GUARD();
     ssize_t size;
 
Quoted 13 lines

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 146

Quoted 10 lines
hw/core/loader.c +7 -1
     size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (*errp) {
+        return -1;
+    }
+
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 
 " MiB)",
+                   filename, max_sz / MiB);

MiB is arbitrary here. This function is used to load all kinds of images such as ROMs which may be 64k-2MB or even executables in generic loader that can be a few kilobytes. This might result in errors saying max size is 0 MiB if the allowed size is less than a MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division discards fractions. Do we have a function to pretty print sizes or maybe this should be left as bytes or at most kilobytes?

Yes, for images sizes less than a megabyte.
Perhaps we can leave it at Kilobytes

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I will send out another version.

If so, then please use qemu_strtosz().

There is another function size_to_str() suggested by Peter I was planning to use it, as it was doing the expected job.

It seems qemu_strtosz() converts size strings to bytes.

Yes, they are the opposite ;)

#16 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

Quoted 60 lines · Philippe Mathieu-Daudé
Quoted 59 lines · Vishal Chourasia

Hi Philippe,

Quoted 56 lines · Philippe Mathieu-Daudé
Quoted 54 lines · Vishal Chourasia

Hi Balaton, Philippe

Quoted 34 lines · BALATON Zoltan
Quoted 33 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

---
hw/core/loader.c | 16 +++++++++++++++-

1 file changed, 15 insertions(+), 1 deletion(-)
hw/core/loader.c +4 -0
index 7aca4989ef..48dd4e7b33 100644
@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, 
 Error **errp)
     if (fd < 0)
         return -1;
     size = lseek(fd, 0, SEEK_END);
+    if (size < 0) {
+        error_setg_errno(errp, errno, "lseek failure: %s", 
 filename);
+        return -1;
+    }
     close(fd);

    return size;
}

hw/core/loader.c +1 -0
@@ -129,14 +133,24 @@ ssize_t load_image_targphys_as(const char 
 *filename,
                                hwaddr addr, uint64_t max_sz, AddressSpace *as,
                                Error **errp)
 {
+    ERRP_GUARD();
     ssize_t size;
 
Quoted 13 lines

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 146

Quoted 10 lines
hw/core/loader.c +7 -1
     size = get_image_size(filename, errp);
-    if (size < 0 || size > max_sz) {
+    if (*errp) {
+        return -1;
+    }
+
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" 
 PRIu64 " MiB)",
+                   filename, max_sz / MiB);

MiB is arbitrary here. This function is used to load all kinds of images such as ROMs which may be 64k-2MB or even executables in generic loader that can be a few kilobytes. This might result in errors saying max size is 0 MiB if the allowed size is less than a MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division discards fractions. Do we have a function to pretty print sizes or maybe this should be left as bytes or at most kilobytes?

Yes, for images sizes less than a megabyte.
Perhaps we can leave it at Kilobytes

I will send out another version.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

If so, then please use qemu_strtosz().

There is another function size_to_str() suggested by Peter I was planning to use it, as it was doing the expected job.

It seems qemu_strtosz() converts size strings to bytes.

Yes, they are the opposite ;)

:)

Hi Philippe,

I have sent out v10 in which I have used size_to_str() to pretty print size information. I have retained your RB, but now I am unsure if i should have not.

Can you review again perhaps

Thanks,
Vishal

#17 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

In reply to BALATON Zoltan
Quoted 11 lines · BALATON Zoltan

hw/core/loader.c ssize_t load_image_targphys_as(const char *filename, · line 146

Quoted 8 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

hw/core/loader.c +3 -0
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 " MiB)",
+                   filename, max_sz / MiB);
Quoted 7 lines

MiB is arbitrary here. This function is used to load all kinds of images such as ROMs which may be 64k-2MB or even executables in generic loader that can be a few kilobytes. This might result in errors saying max size is 0 MiB if the allowed size is less than a MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division discards fractions. Do we have a function to pretty print sizes or maybe this should be left as bytes or at most kilobytes?

Yes, we have size_to_str() for this purpose.

thanks
-- PMM

#18 lore raw

Re: [Patch v9 3/6] core/loader: improve error handling in image loading functions

Quoted 9 lines · Peter Maydell
Quoted 8 lines · BALATON Zoltan
Quoted 7 lines · Vishal Chourasia

Add error checking for lseek() failure and provide better error messages when image loading fails, including filenames and addresses.

hw/core/loader.c +3 -0
+    if (size > max_sz) {
+        error_setg(errp, "%s exceeds maximum image size (%" PRIu64 " MiB)",
+                   filename, max_sz / MiB);
Quoted 8 lines
Quoted 7 lines

MiB is arbitrary here. This function is used to load all kinds of images such as ROMs which may be 64k-2MB or even executables in generic loader that can be a few kilobytes. This might result in errors saying max size is 0 MiB if the allowed size is less than a MiB (e.g. amigaone PROM_SIZE = 512 KiB) and integer division discards fractions. Do we have a function to pretty print sizes or maybe this should be left as bytes or at most kilobytes?

Yes, we have size_to_str() for this purpose.

Thanks Peter, I will add it in next version.

vishalc

#19 lore raw

PATCH v9 4/6 core/loader: add check for zero size in load_image_targphys_as

In reply to Vishal Chourasia

Currently load_image_targphys_as() returns -1 on file open failure or when max size is exceeded. Add an explicit check for zero-sized files to catch this error early, since some callers check for size <= 0.

---
 hw/core/loader.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/core/loader.c +5 -0
index 48dd4e7b33..6717324527 100644
@@ -141,6 +141,11 @@ ssize_t load_image_targphys_as(const char *filename,
         return -1;
     }
 
+    if (size == 0) {
+        error_setg(errp, "empty file: %s", filename);
+        return -1;
+    }
+
     if (size > max_sz) {
         error_setg(errp, "%s exceeds maximum image size (%" PRIu64 " MiB)",
                    filename, max_sz / MiB);
Signature · 1 line
2.51.0
Reviewed-by on v9 4/6
#20 lore raw

Re: [Patch v9 4/6] core/loader: add check for zero size in load_image_targphys_as

Quoted 8 lines · Vishal Chourasia

Currently load_image_targphys_as() returns -1 on file open failure or when max size is exceeded. Add an explicit check for zero-sized files to catch this error early, since some callers check for size <= 0.

---
  hw/core/loader.c | 5 +++++
  1 file changed, 5 insertions(+)
#21 lore raw

PATCH v9 5/6 core: Pass errp to load_image_targphys_as()

In reply to Vishal Chourasia

Pass errp to load_image_targphys_as() in generic-loader and guest-loader to capture detailed error information from the loader functions.

Use error_prepend() instead of error_setg() to preserve the underlying error details while adding context about which image failed to load.

---
 hw/core/generic-loader.c | 7 ++++---
 hw/core/guest-loader.c   | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)
hw/core/generic-loader.c +4 -3
index 6689847c33..35d4e5f4ea 100644
@@ -60,6 +60,7 @@ static void generic_loader_reset(void *opaque)
 
 static void generic_loader_realize(DeviceState *dev, Error **errp)
 {
+    ERRP_GUARD();
     GenericLoaderState *s = GENERIC_LOADER(dev);
     hwaddr entry;
     ssize_t size = 0;
@@ -149,13 +150,13 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
         if (size < 0 || s->force_raw) {
             /* Default to the maximum size being the machine's ram size */
             size = load_image_targphys_as(s->file, s->addr,
-                    current_machine->ram_size, as, NULL);
+                    current_machine->ram_size, as, errp);
         } else {
             s->addr = entry;
         }
 
-        if (size < 0) {
-            error_setg(errp, "Cannot load specified image %s", s->file);
+        if (*errp) {
+            error_prepend(errp, "Cannot load specified image %s: ", s->file);
             return;
         }
     }
hw/core/guest-loader.c +4 -3
index 59f325ad9c..dcbe8e4520 100644
@@ -81,6 +81,7 @@ static void loader_insert_platform_data(GuestLoaderState *s, int size,
 
 static void guest_loader_realize(DeviceState *dev, Error **errp)
 {
+    ERRP_GUARD();
     GuestLoaderState *s = GUEST_LOADER(dev);
     char *file = s->kernel ? s->kernel : s->initrd;
     int size = 0;
@@ -101,9 +102,9 @@ static void guest_loader_realize(DeviceState *dev, Error **errp)
 
     /* Default to the maximum size being the machine's ram size */
     size = load_image_targphys_as(file, s->addr, current_machine->ram_size,
-                                  NULL, NULL);
-    if (size < 0) {
-        error_setg(errp, "Cannot load specified image %s", file);
+                                  NULL, errp);
+    if (*errp) {
+        error_prepend(errp, "Cannot load specified image %s: ", file);
         return;
     }
 
Signature · 1 line
2.51.0
Reviewed-by on v9 5/6
#22 lore raw

Re: [Patch v9 5/6] core: Pass errp to load_image_targphys_as()

Quoted 29 lines · Vishal Chourasia

Pass errp to load_image_targphys_as() in generic-loader and guest-loader to capture detailed error information from the loader functions.

Use error_prepend() instead of error_setg() to preserve the underlying error details while adding context about which image failed to load.

---
  hw/core/generic-loader.c | 7 ++++---
  hw/core/guest-loader.c   | 7 ++++---
  2 files changed, 8 insertions(+), 6 deletions(-)
hw/core/generic-loader.c +1 -0
index 6689847c33..35d4e5f4ea 100644
@@ -60,6 +60,7 @@ static void generic_loader_reset(void *opaque)
 
  static void generic_loader_realize(DeviceState *dev, Error **errp)
  {
+    ERRP_GUARD();
      GenericLoaderState *s = GENERIC_LOADER(dev);
      hwaddr entry;
      ssize_t size = 0;
@@ -149,13 +150,13 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
          if (size < 0 || s->force_raw) {
              /* Default to the maximum size being the machine's ram size */

hw/core/generic-loader.c static void generic_loader_realize(DeviceState *dev, Error **errp) · line 158

Quoted 10 lines
hw/core/generic-loader.c +2 -3
              size = load_image_targphys_as(s->file, s->addr,
-                    current_machine->ram_size, as, NULL);
+                    current_machine->ram_size, as, errp);
          } else {
              s->addr = entry;
          }
 
-        if (size < 0) {
-            error_setg(errp, "Cannot load specified image %s", s->file);
+        if (*errp) {

Again I'd keep the 'if (size < 0)' check to avoid *errp.

Quoted 17 lines
hw/core/generic-loader.c +1 -0
+            error_prepend(errp, "Cannot load specified image %s: ", s->file);
              return;
          }
      }
hw/core/guest-loader.c +1 -0
index 59f325ad9c..dcbe8e4520 100644
@@ -81,6 +81,7 @@ static void loader_insert_platform_data(GuestLoaderState *s, int size,
 
  static void guest_loader_realize(DeviceState *dev, Error **errp)
  {
+    ERRP_GUARD();
      GuestLoaderState *s = GUEST_LOADER(dev);
      char *file = s->kernel ? s->kernel : s->initrd;
      int size = 0;

hw/core/guest-loader.c static void guest_loader_realize(DeviceState *dev, Error **errp) · line 106

Quoted 8 lines
hw/core/guest-loader.c +2 -3
@@ -101,9 +102,9 @@ static void guest_loader_realize(DeviceState *dev, Error **errp)
 
      /* Default to the maximum size being the machine's ram size */
      size = load_image_targphys_as(file, s->addr, current_machine->ram_size,
-                                  NULL, NULL);
-    if (size < 0) {
-        error_setg(errp, "Cannot load specified image %s", file);
+                                  NULL, errp);
+    if (*errp) {

Ditto, otherwise:

Quoted 4 lines
hw/core/guest-loader.c +1 -0
+        error_prepend(errp, "Cannot load specified image %s: ", file);
          return;
      }
 
#23 lore raw

PATCH v9 6/6 ppc: Pass error_fatal to load_image_targphys()

In reply to Vishal Chourasia

Pass error_fatal to load_image_targphys() calls in ppc machine initialization to capture detailed error information when loading firmware, kernel, and initrd images.

Passing error_fatal automatically reports detailed error messages and exits immediately on failure. Eliminating redundant exit(1) calls, as error_fatal handles termination

The behavior remains functionally identical, but error messages now come directly from the loader function with more context about the failure cause.

---
 hw/ppc/amigaone.c      | 13 ++-----------
 hw/ppc/e500.c          | 19 +++----------------
 hw/ppc/mac_newworld.c  | 16 +++-------------
 hw/ppc/mac_oldworld.c  | 16 +++-------------
 hw/ppc/pegasos2.c      |  9 ++-------
 hw/ppc/pnv.c           | 28 +++++-----------------------
 hw/ppc/ppc440_bamboo.c |  8 +-------
 hw/ppc/prep.c          | 17 ++++-------------
 hw/ppc/sam460ex.c      |  7 +------
 hw/ppc/spapr.c         | 13 ++-----------
 hw/ppc/virtex_ml507.c  | 10 ++--------
 11 files changed, 28 insertions(+), 128 deletions(-)
hw/ppc/amigaone.c +2 -11
index 192474e0ae..74a1fa3b63 100644
@@ -324,11 +324,7 @@ static void amigaone_init(MachineState *machine)
             error_report("Could not find firmware '%s'", machine->firmware);
             exit(1);
         }
-        sz = load_image_targphys(filename, PROM_ADDR, PROM_SIZE, NULL);
-        if (sz <= 0 || sz > PROM_SIZE) {
-            error_report("Could not load firmware '%s'", filename);
-            exit(1);
-        }
+        sz = load_image_targphys(filename, PROM_ADDR, PROM_SIZE, &error_fatal);
     }
 
     /* Articia S */
@@ -413,12 +409,7 @@ static void amigaone_init(MachineState *machine)
         loadaddr = ROUND_UP(loadaddr + 4 * MiB, 4 * KiB);
         loadaddr = MAX(loadaddr, INITRD_MIN_ADDR);
         sz = load_image_targphys(machine->initrd_filename, loadaddr,
-                                 bi->bd_info - loadaddr, NULL);
-        if (sz <= 0) {
-            error_report("Could not load initrd '%s'",
-                         machine->initrd_filename);
-            exit(1);
-        }
+                                 bi->bd_info - loadaddr, &error_fatal);
         bi->initrd_start = loadaddr;
         bi->initrd_end = loadaddr + sz;
     }
hw/ppc/e500.c +3 -16
index 418e1bb2fb..8842f7f6b8 100644
@@ -1226,14 +1226,8 @@ void ppce500_init(MachineState *machine)
     if (machine->kernel_filename && !kernel_as_payload) {
         kernel_base = cur_base;
         kernel_size = load_image_targphys(machine->kernel_filename,
-                                          cur_base,
-                                          machine->ram_size - cur_base, NULL);
-        if (kernel_size < 0) {
-            error_report("could not load kernel '%s'",
-                         machine->kernel_filename);
-            exit(1);
-        }
-
+                                      cur_base, machine->ram_size - cur_base,
+                                      &error_fatal);
         cur_base += kernel_size;
     }
 
@@ -1242,14 +1236,7 @@ void ppce500_init(MachineState *machine)
         initrd_base = (cur_base + INITRD_LOAD_PAD) & ~INITRD_PAD_MASK;
         initrd_size = load_image_targphys(machine->initrd_filename, initrd_base,
                                           machine->ram_size - initrd_base,
-                                          NULL);
-
-        if (initrd_size < 0) {
-            error_report("could not load initial ram disk '%s'",
-                         machine->initrd_filename);
-            exit(1);
-        }
-
+                                          &error_fatal);
         cur_base = initrd_base + initrd_size;
     }
 
hw/ppc/mac_newworld.c +3 -13
index 004efc6b97..951de4bae4 100644
@@ -189,7 +189,7 @@ static void ppc_core99_init(MachineState *machine)
         if (bios_size <= 0) {
             /* or load binary ROM image */
             bios_size = load_image_targphys(filename, PROM_BASE, PROM_SIZE,
-                                            NULL);
+                                            &error_fatal);
         }
         g_free(filename);
     }
@@ -212,12 +212,7 @@ static void ppc_core99_init(MachineState *machine)
             kernel_size = load_image_targphys(machine->kernel_filename,
                                               kernel_base,
                                               machine->ram_size - kernel_base,
-                                              NULL);
-        }
-        if (kernel_size < 0) {
-            error_report("could not load kernel '%s'",
-                         machine->kernel_filename);
-            exit(1);
+                                              &error_fatal);
         }
         /* load initrd */
         if (machine->initrd_filename) {
@@ -225,12 +220,7 @@ static void ppc_core99_init(MachineState *machine)
             initrd_size = load_image_targphys(machine->initrd_filename,
                                               initrd_base,
                                               machine->ram_size - initrd_base,
-                                              NULL);
-            if (initrd_size < 0) {
-                error_report("could not load initial ram disk '%s'",
-                             machine->initrd_filename);
-                exit(1);
-            }
+                                              &error_fatal);
             cmdline_base = TARGET_PAGE_ALIGN(initrd_base + initrd_size);
         } else {
             cmdline_base = TARGET_PAGE_ALIGN(kernel_base + kernel_size + KERNEL_GAP);
hw/ppc/mac_oldworld.c +3 -13
index c7e44d49b0..cd2bb46442 100644
@@ -144,7 +144,7 @@ static void ppc_heathrow_init(MachineState *machine)
         if (bios_size <= 0) {
             /* or if could not load ELF try loading a binary ROM image */
             bios_size = load_image_targphys(filename, PROM_BASE, PROM_SIZE,
-                                            NULL);
+                                            &error_fatal);
             bios_addr = PROM_BASE;
         }
         g_free(filename);
@@ -168,12 +168,7 @@ static void ppc_heathrow_init(MachineState *machine)
             kernel_size = load_image_targphys(machine->kernel_filename,
                                               kernel_base,
                                               machine->ram_size - kernel_base,
-                                              NULL);
-        }
-        if (kernel_size < 0) {
-            error_report("could not load kernel '%s'",
-                         machine->kernel_filename);
-            exit(1);
+                                              &error_fatal);
         }
         /* load initrd */
         if (machine->initrd_filename) {
@@ -182,12 +177,7 @@ static void ppc_heathrow_init(MachineState *machine)
             initrd_size = load_image_targphys(machine->initrd_filename,
                                               initrd_base,
                                               machine->ram_size - initrd_base,
-                                              NULL);
-            if (initrd_size < 0) {
-                error_report("could not load initial ram disk '%s'",
-                             machine->initrd_filename);
-                exit(1);
-            }
+                                              &error_fatal);
             cmdline_base = TARGET_PAGE_ALIGN(initrd_base + initrd_size);
         } else {
             cmdline_base = TARGET_PAGE_ALIGN(kernel_base + kernel_size + KERNEL_GAP);
hw/ppc/pegasos2.c +2 -7
index a9e706644c..3c02c53c3a 100644
@@ -198,7 +198,7 @@ static void pegasos_init(MachineState *machine)
                   ELFDATA2MSB, PPC_ELF_MACHINE, 0, 0);
     if (sz <= 0) {
         sz = load_image_targphys(filename, pm->vof ? 0 : prom_addr, PROM_SIZE,
-                                 NULL);
+                                 &error_fatal);
     }
     if (sz <= 0 || sz > PROM_SIZE) {
         error_report("Could not load firmware '%s'", filename);
@@ -302,12 +302,7 @@ static void pegasos_init(MachineState *machine)
         pm->initrd_addr = ROUND_UP(pm->initrd_addr, 4);
         pm->initrd_addr = MAX(pm->initrd_addr, INITRD_MIN_ADDR);
         sz = load_image_targphys(machine->initrd_filename, pm->initrd_addr,
-                                 machine->ram_size - pm->initrd_addr, NULL);
-        if (sz <= 0) {
-            error_report("Could not load initrd '%s'",
-                         machine->initrd_filename);
-            exit(1);
-        }
+                            machine->ram_size - pm->initrd_addr, &error_fatal);
         pm->initrd_size = sz;
     }
 
hw/ppc/pnv.c +5 -23
index 1c0dadda87..895132da91 100644
@@ -1009,7 +1009,6 @@ static void pnv_init(MachineState *machine)
     PnvMachineClass *pmc = PNV_MACHINE_GET_CLASS(machine);
     int max_smt_threads = pmc->max_smt_threads;
     char *fw_filename;
-    long fw_size;
     uint64_t chip_ram_start = 0;
     int i;
     char *chip_typename;
@@ -1068,26 +1067,14 @@ static void pnv_init(MachineState *machine)
         exit(1);
     }
 
-    fw_size = load_image_targphys(fw_filename, pnv->fw_load_addr, FW_MAX_SIZE,
-                                  NULL);
-    if (fw_size < 0) {
-        error_report("Could not load OPAL firmware '%s'", fw_filename);
-        exit(1);
-    }
+    load_image_targphys(fw_filename, pnv->fw_load_addr, FW_MAX_SIZE,
+                        &error_fatal);
     g_free(fw_filename);
 
     /* load kernel */
     if (machine->kernel_filename) {
-        long kernel_size;
-
-        kernel_size = load_image_targphys(machine->kernel_filename,
-                                          KERNEL_LOAD_ADDR, KERNEL_MAX_SIZE,
-                                          NULL);
-        if (kernel_size < 0) {
-            error_report("Could not load kernel '%s'",
-                         machine->kernel_filename);
-            exit(1);
-        }
+        load_image_targphys(machine->kernel_filename,
+                            KERNEL_LOAD_ADDR, KERNEL_MAX_SIZE, &error_fatal);
     }
 
     /* load initrd */
@@ -1095,12 +1082,7 @@ static void pnv_init(MachineState *machine)
         pnv->initrd_base = INITRD_LOAD_ADDR;
         pnv->initrd_size = load_image_targphys(machine->initrd_filename,
                                                pnv->initrd_base,
-                                               INITRD_MAX_SIZE, NULL);
-        if (pnv->initrd_size < 0) {
-            error_report("Could not load initial ram disk '%s'",
-                         machine->initrd_filename);
-            exit(1);
-        }
+                                               INITRD_MAX_SIZE, &error_fatal);
     }
 
     /* load dtb if passed */
hw/ppc/ppc440_bamboo.c +1 -7
index 7c66912c10..7e739a2114 100644
@@ -243,13 +243,7 @@ static void bamboo_init(MachineState *machine)
     if (initrd_filename) {
         initrd_size = load_image_targphys(initrd_filename, RAMDISK_ADDR,
                                           machine->ram_size - RAMDISK_ADDR,
-                                          NULL);
-
-        if (initrd_size < 0) {
-            error_report("could not load ram disk '%s' at %x",
-                         initrd_filename, RAMDISK_ADDR);
-            exit(1);
-        }
+                                          &error_fatal);
     }
 
     /* If we're loading a kernel directly, we must load the device tree too. */
hw/ppc/prep.c +4 -13
index 0759e95cb6..c2fe16e985 100644
@@ -280,7 +280,8 @@ static void ibm_40p_init(MachineState *machine)
     bios_size = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                          ELFDATA2MSB, PPC_ELF_MACHINE, 0, 0);
     if (bios_size < 0) {
-        bios_size = load_image_targphys(filename, BIOS_ADDR, BIOS_SIZE, NULL);
+        bios_size = load_image_targphys(filename, BIOS_ADDR, BIOS_SIZE,
+                                        &error_fatal);
     }
     if (bios_size < 0 || bios_size > BIOS_SIZE) {
         error_report("Could not load bios image '%s'", filename);
@@ -380,12 +381,7 @@ static void ibm_40p_init(MachineState *machine)
         kernel_size = load_image_targphys(machine->kernel_filename,
                                           kernel_base,
                                           machine->ram_size - kernel_base,
-                                          NULL);
-        if (kernel_size < 0) {
-            error_report("could not load kernel '%s'",
-                         machine->kernel_filename);
-            exit(1);
-        }
+                                          &error_fatal);
         fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_base);
         fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
         /* load initrd */
@@ -394,12 +390,7 @@ static void ibm_40p_init(MachineState *machine)
             initrd_size = load_image_targphys(machine->initrd_filename,
                                               initrd_base,
                                               machine->ram_size - initrd_base,
-                                              NULL);
-            if (initrd_size < 0) {
-                error_report("could not load initial ram disk '%s'",
-                             machine->initrd_filename);
-                exit(1);
-            }
+                                              &error_fatal);
             fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_base);
             fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
         }
hw/ppc/sam460ex.c +1 -6
index 68d3eacbff..258d43f8d2 100644
@@ -495,12 +495,7 @@ static void sam460ex_init(MachineState *machine)
         initrd_size = load_image_targphys(machine->initrd_filename,
                                           RAMDISK_ADDR,
                                           machine->ram_size - RAMDISK_ADDR,
-                                          NULL);
-        if (initrd_size < 0) {
-            error_report("could not load ram disk '%s' at %x",
-                    machine->initrd_filename, RAMDISK_ADDR);
-            exit(1);
-        }
+                                          &error_fatal);
     }
 
     /* If we're loading a kernel directly, we must load the device tree too. */
hw/ppc/spapr.c +2 -11
index cdf2fdeadc..99b843ba2f 100644
@@ -2854,11 +2854,7 @@ static void spapr_machine_init(MachineState *machine)
         error_report("Could not find LPAR firmware '%s'", bios_name);
         exit(1);
     }
-    fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE, NULL);
-    if (fw_size <= 0) {
-        error_report("Could not load LPAR firmware '%s'", filename);
-        exit(1);
-    }
+    fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE, &error_fatal);
 
     /*
      * if Secure VM (PEF) support is configured, then initialize it
@@ -3117,12 +3113,7 @@ static void spapr_machine_init(MachineState *machine)
             spapr->initrd_size = load_image_targphys(initrd_filename,
                                                 spapr->initrd_base,
                                                 load_limit - spapr->initrd_base,
-                                                NULL);
-            if (spapr->initrd_size < 0) {
-                error_report("could not load initial ram disk '%s'",
-                             initrd_filename);
-                exit(1);
-            }
+                                                &error_fatal);
         }
     }
 
hw/ppc/virtex_ml507.c +2 -8
index 00d9ab7509..43a6d505a8 100644
@@ -253,7 +253,7 @@ static void virtex_init(MachineState *machine)
             /* If we failed loading ELF's try a raw image.  */
             kernel_size = load_image_targphys(kernel_filename,
                                               boot_offset,
-                                              machine->ram_size, NULL);
+                                              machine->ram_size, &error_fatal);
             boot_info.bootstrap_pc = boot_offset;
             high = boot_info.bootstrap_pc + kernel_size + 8192;
         }
@@ -265,13 +265,7 @@ static void virtex_init(MachineState *machine)
             initrd_base = high = ROUND_UP(high, 4);
             initrd_size = load_image_targphys(machine->initrd_filename,
                                               high, machine->ram_size - high,
-                                              NULL);
-
-            if (initrd_size < 0) {
-                error_report("couldn't load ram disk '%s'",
-                             machine->initrd_filename);
-                exit(1);
-            }
+                                              &error_fatal);
             high = ROUND_UP(high + initrd_size, 4);
         }
 
Signature · 1 line
2.51.0
Reviewed-by on v9 6/6
#24 lore raw

Re: [Patch v9 6/6] ppc: Pass error_fatal to load_image_targphys()

Quoted 14 lines · Vishal Chourasia

Pass error_fatal to load_image_targphys() calls in ppc machine initialization to capture detailed error information when loading firmware, kernel, and initrd images.

Passing error_fatal automatically reports detailed error messages and exits immediately on failure. Eliminating redundant exit(1) calls, as error_fatal handles termination

The behavior remains functionally identical, but error messages now come directly from the loader function with more context about the failure cause.

Quoted 13 lines
---
  hw/ppc/amigaone.c      | 13 ++-----------
  hw/ppc/e500.c          | 19 +++----------------
  hw/ppc/mac_newworld.c  | 16 +++-------------
  hw/ppc/mac_oldworld.c  | 16 +++-------------
  hw/ppc/pegasos2.c      |  9 ++-------
  hw/ppc/pnv.c           | 28 +++++-----------------------
  hw/ppc/ppc440_bamboo.c |  8 +-------
  hw/ppc/prep.c          | 17 ++++-------------
  hw/ppc/sam460ex.c      |  7 +------
  hw/ppc/spapr.c         | 13 ++-----------
  hw/ppc/virtex_ml507.c  | 10 ++--------
  11 files changed, 28 insertions(+), 128 deletions(-)
Reviewed-by on v9 6/6
#25 lore raw

Re: [Patch v9 6/6] ppc: Pass error_fatal to load_image_targphys()

In reply to Vishal Chourasia
Quoted 8 lines · Vishal Chourasia

Pass error_fatal to load_image_targphys() calls in ppc machine initialization to capture detailed error information when loading firmware, kernel, and initrd images.

Passing error_fatal automatically reports detailed error messages and exits immediately on failure. Eliminating redundant exit(1) calls, as error_fatal handles termination

Quoted 6 lines

The behavior remains functionally identical, but error messages now come directly from the loader function with more context about the failure cause.

Thank you for doing this. It's a nice clean up making these boards more concise and readable.

Regards,
BALATON Zoltan