hw/core/loader: capture Error from load_image_targphys

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.

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Aditya Gupta <adityag@linux.ibm.com>
Tested-by: Aditya Gupta <adityag@linux.ibm.com>
Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>
Message-ID: <20251024130556.1942835-6-vishalc@linux.ibm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
This commit is contained in:
Vishal Chourasia
2025-10-28 08:19:18 +01:00
committed by Philippe Mathieu-Daudé
parent 77a5b02b5b
commit a9c608904f
51 changed files with 145 additions and 106 deletions
+2 -2
View File
@@ -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,
+1 -1
View File
@@ -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);
+3 -2
View File
@@ -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'",
+1 -1
View File
@@ -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);
+2 -1
View File
@@ -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);
}
+1 -1
View File
@@ -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);
+1 -1
View File
@@ -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);
+2 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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;
+9 -7
View File
@@ -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;
+3 -2
View File
@@ -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;
}
+1 -1
View File
@@ -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);
+2 -2
View File
@@ -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;
}
+1 -1
View File
@@ -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);
+2 -3
View File
@@ -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) {
+1 -1
View File
@@ -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) {
+2 -2
View File
@@ -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) {
+1 -1
View File
@@ -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);
}
+4 -3
View File
@@ -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;
+2 -2
View File
@@ -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 {

Some files were not shown because too many files have changed in this diff Show More