hw/core/loader: Make load_elf_hdr() return bool, simplify caller

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-ID: <20251119130855.105479-2-armbru@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
This commit is contained in:
Markus Armbruster
2025-11-19 14:08:51 +01:00
parent 0fc482b73d
commit 7bfd6c0e09
4 changed files with 11 additions and 17 deletions

View File

@@ -766,16 +766,12 @@ static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
int data_swab = 0;
int elf_data_order;
ssize_t ret;
Error *err = NULL;
load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
if (err) {
if (!load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, NULL)) {
/*
* If the file is not an ELF file we silently return.
* The caller will fall back to try other formats.
*/
error_free(err);
return -1;
}

View File

@@ -364,8 +364,9 @@ const char *load_elf_strerror(ssize_t error)
}
}
void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
bool load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
{
bool ok = false;
int fd;
uint8_t e_ident_local[EI_NIDENT];
uint8_t *e_ident;
@@ -380,7 +381,7 @@ void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
fd = open(filename, O_RDONLY | O_BINARY);
if (fd < 0) {
error_setg_errno(errp, errno, "Failed to open file: %s", filename);
return;
return false;
}
if (read(fd, hdr, EI_NIDENT) != EI_NIDENT) {
error_setg_errno(errp, errno, "Failed to read file: %s", filename);
@@ -415,8 +416,11 @@ void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
off += br;
}
ok = true;
fail:
close(fd);
return ok;
}
/* return < 0 if error, otherwise the number of bytes loaded in memory */

View File

@@ -180,15 +180,7 @@ static void create_fdt(SpikeState *s, const MemMapEntry *memmap,
static bool spike_test_elf_image(char *filename)
{
Error *err = NULL;
load_elf_hdr(filename, NULL, NULL, &err);
if (err) {
error_free(err);
return false;
} else {
return true;
}
return load_elf_hdr(filename, NULL, NULL, NULL);
}
static void spike_board_init(MachineState *machine)

View File

@@ -188,8 +188,10 @@ ssize_t load_elf(const char *filename,
*
* Inspect an ELF file's header. Read its full header contents into a
* buffer and/or determine if the ELF is 64bit.
*
* Returns true on success, false on failure.
*/
void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp);
bool load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp);
ssize_t load_aout(const char *filename, hwaddr addr, int max_sz,
bool big_endian, hwaddr target_page_size);