[PATCH 3/4] hw/riscv/boot: Warn if a ELF format file is loaded as a binary

Joel Stanley posted 4 patches 1 month ago
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <dbarboza@ventanamicro.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Vijai Kumar K <vijai@behindbytes.com>, Ran Wang <wangran@bosc.ac.cn>
[PATCH 3/4] hw/riscv/boot: Warn if a ELF format file is loaded as a binary
Posted by Joel Stanley 1 month ago
From: Nicholas Piggin <npiggin@gmail.com>

It is possible that an ELF file can not be loaded, in that
case the loader falls back to loading the file as a binary
blob. Print a warning in this case because it is likely that
it is not intended.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Joel Stanley <joel@jms.id.au>
---
 hw/riscv/boot.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/hw/riscv/boot.c b/hw/riscv/boot.c
index f3857e984240..3ea95c175c14 100644
--- a/hw/riscv/boot.c
+++ b/hw/riscv/boot.c
@@ -179,13 +179,27 @@ hwaddr riscv_load_firmware(MachineState *machine,
 
     g_assert(firmware_filename != NULL);
 
-    if (load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
-                         &firmware_entry, NULL, &firmware_end, NULL,
-                         0, EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
+    firmware_size = load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
+                                     &firmware_entry, NULL, &firmware_end,
+                                     NULL, 0, EM_RISCV, 1, 0, NULL, false,
+                                     sym_cb);
+    if (firmware_size > 0) {
         *firmware_load_addr = firmware_entry;
         return firmware_end;
     }
 
+    if (firmware_size != ELF_LOAD_NOT_ELF) {
+        /*
+         * If the user specified an ELF format firmware that could not be
+         * loaded as an ELF, it's possible that loading it as a binary is
+         * not what was intended.
+         */
+        warn_report("could not load ELF format firmware '%s' (%s). "
+                    "Attempting to load as binary.",
+                    firmware_filename,
+                    load_elf_strerror(firmware_size));
+    }
+
     firmware_size = load_image_targphys_as(firmware_filename,
                                            *firmware_load_addr,
                                            mem_size, NULL,
@@ -195,7 +209,8 @@ hwaddr riscv_load_firmware(MachineState *machine,
         return *firmware_load_addr + firmware_size;
     }
 
-    error_report("could not load firmware '%s'", firmware_filename);
+    error_report("could not load firmware '%s': %s", firmware_filename,
+                 load_elf_strerror(firmware_size));
     exit(1);
 }
 
-- 
2.47.3
Re: [PATCH 3/4] hw/riscv/boot: Warn if a ELF format file is loaded as a binary
Posted by Daniel Henrique Barboza 3 weeks, 4 days ago

On 1/9/2026 10:16 AM, Joel Stanley wrote:
> From: Nicholas Piggin <npiggin@gmail.com>
> 
> It is possible that an ELF file can not be loaded, in that
> case the loader falls back to loading the file as a binary
> blob. Print a warning in this case because it is likely that
> it is not intended.
> 
> Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
> Signed-off-by: Joel Stanley <joel@jms.id.au>
> ---


Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>

>   hw/riscv/boot.c | 23 +++++++++++++++++++----
>   1 file changed, 19 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/riscv/boot.c b/hw/riscv/boot.c
> index f3857e984240..3ea95c175c14 100644
> --- a/hw/riscv/boot.c
> +++ b/hw/riscv/boot.c
> @@ -179,13 +179,27 @@ hwaddr riscv_load_firmware(MachineState *machine,
>   
>       g_assert(firmware_filename != NULL);
>   
> -    if (load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
> -                         &firmware_entry, NULL, &firmware_end, NULL,
> -                         0, EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
> +    firmware_size = load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
> +                                     &firmware_entry, NULL, &firmware_end,
> +                                     NULL, 0, EM_RISCV, 1, 0, NULL, false,
> +                                     sym_cb);
> +    if (firmware_size > 0) {
>           *firmware_load_addr = firmware_entry;
>           return firmware_end;
>       }
>   
> +    if (firmware_size != ELF_LOAD_NOT_ELF) {
> +        /*
> +         * If the user specified an ELF format firmware that could not be
> +         * loaded as an ELF, it's possible that loading it as a binary is
> +         * not what was intended.
> +         */
> +        warn_report("could not load ELF format firmware '%s' (%s). "
> +                    "Attempting to load as binary.",
> +                    firmware_filename,
> +                    load_elf_strerror(firmware_size));
> +    }
> +
>       firmware_size = load_image_targphys_as(firmware_filename,
>                                              *firmware_load_addr,
>                                              mem_size, NULL,
> @@ -195,7 +209,8 @@ hwaddr riscv_load_firmware(MachineState *machine,
>           return *firmware_load_addr + firmware_size;
>       }
>   
> -    error_report("could not load firmware '%s'", firmware_filename);
> +    error_report("could not load firmware '%s': %s", firmware_filename,
> +                 load_elf_strerror(firmware_size));
>       exit(1);
>   }
>