qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, hreitz@redhat.com, mst@redhat.com,
	imammedo@redhat.com, anisinha@redhat.com, gengdongjiu1@gmail.com,
	peter.maydell@linaro.org, alistair@alistair23.me,
	edgar.iglesias@gmail.com, npiggin@gmail.com,
	harshpb@linux.ibm.com, palmer@dabbelt.com, liwei1518@gmail.com,
	dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com,
	sstabellini@kernel.org, anthony@xenproject.org, paul@xen.org,
	berrange@redhat.com, peterx@redhat.com, farosas@suse.de,
	eblake@redhat.com, vsementsov@yandex-team.ru,
	eduardo@habkost.net, marcel.apfelbaum@gmail.com,
	philmd@linaro.org, wangyanan55@huawei.com, zhao1.liu@intel.com,
	qemu-block@nongnu.org, qemu-arm@nongnu.org, qemu-ppc@nongnu.org,
	qemu-riscv@nongnu.org, xen-devel@lists.xenproject.org
Subject: [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller
Date: Wed, 19 Nov 2025 14:08:51 +0100	[thread overview]
Message-ID: <20251119130855.105479-2-armbru@redhat.com> (raw)
In-Reply-To: <20251119130855.105479-1-armbru@redhat.com>

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 include/hw/loader.h |  4 +++-
 hw/arm/boot.c       |  6 +-----
 hw/core/loader.c    |  8 ++++++--
 hw/riscv/spike.c    | 10 +---------
 4 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/include/hw/loader.h b/include/hw/loader.h
index d035e72748..6f91703503 100644
--- a/include/hw/loader.h
+++ b/include/hw/loader.h
@@ -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);
diff --git a/hw/arm/boot.c b/hw/arm/boot.c
index b91660208f..06b303aab8 100644
--- a/hw/arm/boot.c
+++ b/hw/arm/boot.c
@@ -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;
     }
 
diff --git a/hw/core/loader.c b/hw/core/loader.c
index 590c5b02aa..10687fe1c8 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -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 */
diff --git a/hw/riscv/spike.c b/hw/riscv/spike.c
index b0bab3fe00..8531e1d121 100644
--- a/hw/riscv/spike.c
+++ b/hw/riscv/spike.c
@@ -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)
-- 
2.49.0



  reply	other threads:[~2025-11-19 13:11 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-19 13:08 [PATCH 0/5] A bit of cleanup around Error Markus Armbruster
2025-11-19 13:08 ` Markus Armbruster [this message]
2025-11-19 15:37   ` [PATCH 1/5] hw/core/loader: Make load_elf_hdr() return bool, simplify caller Richard Henderson
2025-11-19 16:34   ` Vladimir Sementsov-Ogievskiy
2025-11-19 19:12     ` Markus Armbruster
2025-11-20 10:51       ` Philippe Mathieu-Daudé
2025-11-19 18:06   ` Peter Xu
2025-11-20 12:04   ` Daniel Henrique Barboza
2025-11-20 12:55     ` BALATON Zoltan
2025-11-20 19:12       ` Markus Armbruster
2025-11-20 14:53   ` Zhao Liu
2025-11-19 13:08 ` [PATCH 2/5] hw/nvram/xlnx-bbram: More idiomatic and simpler error reporting Markus Armbruster
2025-11-19 16:39   ` Vladimir Sementsov-Ogievskiy
2025-11-19 19:10     ` Markus Armbruster
2025-11-19 18:09   ` Peter Xu
2025-11-20  8:10   ` Luc Michel
2025-11-20 14:54   ` Zhao Liu
2025-11-19 13:08 ` [PATCH 3/5] nbd/client-connection: Replace error_propagate() by assignment Markus Armbruster
2025-11-19 16:43   ` Vladimir Sementsov-Ogievskiy
2025-11-19 18:09   ` Peter Xu
2025-11-20 14:55   ` Zhao Liu
2025-11-19 13:08 ` [PATCH 4/5] error: error_free(NULL) is safe, drop unnecessary conditionals Markus Armbruster
2025-11-19 15:38   ` Richard Henderson
2025-11-19 16:44   ` Vladimir Sementsov-Ogievskiy
2025-11-19 18:04   ` Peter Xu
2025-11-20 14:56   ` Zhao Liu
2025-11-19 13:08 ` [PATCH 5/5] error: Consistently name Error * objects err, and not errp Markus Armbruster
2025-11-19 13:22   ` Andrew Cooper
2025-11-19 13:35     ` Daniel P. Berrangé
2025-11-19 13:48       ` Markus Armbruster
2025-11-20 14:57   ` Zhao Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251119130855.105479-2-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=alistair@alistair23.me \
    --cc=anisinha@redhat.com \
    --cc=anthony@xenproject.org \
    --cc=berrange@redhat.com \
    --cc=dbarboza@ventanamicro.com \
    --cc=eblake@redhat.com \
    --cc=edgar.iglesias@gmail.com \
    --cc=eduardo@habkost.net \
    --cc=farosas@suse.de \
    --cc=gengdongjiu1@gmail.com \
    --cc=harshpb@linux.ibm.com \
    --cc=hreitz@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=liwei1518@gmail.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=npiggin@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=paul@xen.org \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=sstabellini@kernel.org \
    --cc=vsementsov@yandex-team.ru \
    --cc=wangyanan55@huawei.com \
    --cc=xen-devel@lists.xenproject.org \
    --cc=zhao1.liu@intel.com \
    --cc=zhiwei_liu@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).