@samitouri / QOSamiQemu / commits / 971851b046

hw/riscv/boot: Warn if a ELF format file is loaded as a binary

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> Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> Signed-off-by: Joel Stanley <joel@jms.id.au> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Message-ID: <20260415064838.652297-4-joel@jms.id.au> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

Nicholas Piggin committed Apr 15, 2026 at 16:48 UTC 971851b0463806bae208ecae1ff200012d528b30
1 file changed +19 -4
hw/riscv/boot.c
+19 -4
@@ -163,13 +163,27 @@ hwaddr riscv_load_firmware(const char *firmware_filename,
163
164 g_assert(firmware_filename != NULL);
165
166 - if (load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
167 - &firmware_entry, NULL, &firmware_end, NULL,
168 - 0, EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
166 + firmware_size = load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
167 + &firmware_entry, NULL, &firmware_end,
168 + NULL, 0, EM_RISCV, 1, 0, NULL, false,
169 + sym_cb);
170 + if (firmware_size > 0) {
171 *firmware_load_addr = firmware_entry;
172 return firmware_end;
173 }
174
175 + if (firmware_size != ELF_LOAD_NOT_ELF) {
176 + /*
177 + * If the user specified an ELF format firmware that could not be
178 + * loaded as an ELF, it's possible that loading it as a binary is
179 + * not what was intended.
180 + */
181 + warn_report("could not load ELF format firmware '%s' (%s). "
182 + "Attempting to load as binary.",
183 + firmware_filename,
184 + load_elf_strerror(firmware_size));
185 + }
186 +
187 firmware_size = load_image_targphys_as(firmware_filename,
188 *firmware_load_addr,
189 current_machine->ram_size, NULL,
@@ -179,7 +193,8 @@ hwaddr riscv_load_firmware(const char *firmware_filename,
193 return *firmware_load_addr + firmware_size;
194 }
195
182 - error_report("could not load firmware '%s'", firmware_filename);
196 + error_report("could not load firmware '%s': %s", firmware_filename,
197 + load_elf_strerror(firmware_size));
198 exit(1);
199 }
200