From: Vishal Chourasia <vishalc@linux.ibm.com>
To: Alistair Francis <alistair23@gmail.com>
Cc: adityag@linux.ibm.com, harshpb@linux.ibm.com,
milesg@linux.ibm.com, npiggin@gmail.com,
peter.maydell@linaro.org, qemu-devel@nongnu.org,
qemu-ppc@nongnu.org, berrange@redhat.com,
richard.henderson@linaro.org, alistair@alistair23.me,
alex.bennee@linaro.org, deller@gmx.de, pbonzini@redhat.com,
eduardo@habkost.net, minyard@acm.org, gaosong@loongson.cn,
maobibo@loongson.cn, laurent@vivier.eu, edgar.iglesias@gmail.com,
hpoussin@reactos.org, balaton@eik.bme.hu, david@redhat.com,
chigot@adacore.com, konrad.frederic@yahoo.fr,
atar4qemu@gmail.com, jcmvbkbc@gmail.com
Subject: Re: [PATCH 1/5] hw/core/loader: capture Error from load_image_targphys
Date: Fri, 17 Oct 2025 11:19:07 +0530 [thread overview]
Message-ID: <aPHY0828TU5hl5Tg@linux.ibm.com> (raw)
In-Reply-To: <CAKmqyKOJS+rY0NpTpv76SJqZmgjoLnvQX8wiKgxgUP6k=+fpnQ@mail.gmail.com>
Hi Alistair,
On Fri, Oct 17, 2025 at 08:58:58AM +1000, Alistair Francis wrote:
> On Fri, Oct 17, 2025 at 4:01 AM Vishal Chourasia <vishalc@linux.ibm.com> wrote:
> >
> > 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>
> > Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>
>
> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
>
> Alistair
>
> > ---
> > hw/core/loader.c | 33 +++++++++++++++++++++++++--------
<snipped>
> > diff --git a/hw/core/loader.c b/hw/core/loader.c
> > index 477661a025..63bb0578b1 100644
> > --- a/hw/core/loader.c
> > +++ b/hw/core/loader.c
> > @@ -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"
> > @@ -61,23 +62,31 @@
> > #include "hw/nvram/fw_cfg.h"
> > #include "system/memory.h"
> > #include "hw/boards.h"
> > +#include "qapi/error.h"
> > #include "qemu/cutils.h"
> > #include "system/runstate.h"
> > #include "tcg/debuginfo.h"
> >
> > +#include <errno.h>
> > #include <zlib.h>
> >
> > 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 = open(filename, O_RDONLY | O_BINARY);
> > - if (fd < 0)
> > + if (fd < 0) {
> > + error_setg_file_open(errp, errno, filename);
> > 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;
> > }
> > @@ -118,21 +127,29 @@ 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)
> > {
> > + ERRP_GUARD();
> > ssize_t size;
> >
> > - size = get_image_size(filename);
> > - if (size < 0 || size > max_sz) {
> > + size = get_image_size(filename, errp);
> > + if (*errp) {
> > return -1;
> > }
> > +
> > + if (size > max_sz) {
> > + error_setg(errp, "%s exceeds maximum image size (%lu MiB)", filename, max_sz / MiB);
> > + return -1;
> > + }
> > +
> > if (size > 0) {
> > if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
> > return -1;
There was a case where load_image_targphys_as() can return -1 but errp
was not set. Following change addresses this issue.
I will incorporate this in the next version (v4).
/* 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)
{
+ ERRP_GUARD();
ssize_t size;
- size = get_image_size(filename);
- if (size < 0 || size > max_sz) {
+ size = get_image_size(filename, errp);
+ if (*errp) {
return -1;
}
+
+ if (size > max_sz) {
+ error_setg(errp, "%s exceeds maximum image size (%lu MiB)",
+ filename, max_sz / MiB);
+ return -1;
+ }
+
if (size > 0) {
if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
+ error_setg(errp, "failed to add file as ROM");
return -1;
}
}
Thanks,
vishalc
next prev parent reply other threads:[~2025-10-17 5:51 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-16 17:34 [PATCH v3 0/5] hw/core/loader: Add Error parameter for better error reporting Vishal Chourasia
2025-10-16 17:34 ` [PATCH 1/5] hw/core/loader: capture Error from load_image_targphys Vishal Chourasia
2025-10-16 22:58 ` Alistair Francis
2025-10-17 5:49 ` Vishal Chourasia [this message]
2025-10-16 17:34 ` [PATCH 2/5] hw/core/loader: Use qemu_open() instead of open() in get_image_size() Vishal Chourasia
2025-10-16 17:40 ` Daniel P. Berrangé
2025-10-16 17:35 ` [PATCH 3/5] hw/core: Pass errp to load_image_targphys_as() Vishal Chourasia
2025-10-16 22:59 ` Alistair Francis
2025-10-17 4:52 ` Vishal Chourasia
2025-10-16 17:35 ` [PATCH 4/5] hw/ppc/spapr: Rename resize_hpt_err to errp Vishal Chourasia
2025-10-16 17:35 ` [PATCH 5/5] hw/ppc: Pass errp to load_image_targphys() and report errors Vishal Chourasia
2025-10-16 19:27 ` BALATON Zoltan
2025-10-16 20:26 ` Vishal Chourasia
2025-10-17 6:08 ` Vishal Chourasia
2025-10-17 4:54 ` Aditya Gupta
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=aPHY0828TU5hl5Tg@linux.ibm.com \
--to=vishalc@linux.ibm.com \
--cc=adityag@linux.ibm.com \
--cc=alex.bennee@linaro.org \
--cc=alistair23@gmail.com \
--cc=alistair@alistair23.me \
--cc=atar4qemu@gmail.com \
--cc=balaton@eik.bme.hu \
--cc=berrange@redhat.com \
--cc=chigot@adacore.com \
--cc=david@redhat.com \
--cc=deller@gmx.de \
--cc=edgar.iglesias@gmail.com \
--cc=eduardo@habkost.net \
--cc=gaosong@loongson.cn \
--cc=harshpb@linux.ibm.com \
--cc=hpoussin@reactos.org \
--cc=jcmvbkbc@gmail.com \
--cc=konrad.frederic@yahoo.fr \
--cc=laurent@vivier.eu \
--cc=maobibo@loongson.cn \
--cc=milesg@linux.ibm.com \
--cc=minyard@acm.org \
--cc=npiggin@gmail.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
--cc=richard.henderson@linaro.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.