[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 01/13] hw/core/loader: allow loading larger ROMs
From: |
Philippe Mathieu-Daudé |
Subject: |
[PULL 01/13] hw/core/loader: allow loading larger ROMs |
Date: |
Tue, 16 Jul 2024 20:09:28 +0200 |
From: Gregor Haas <gregorhaas1997@gmail.com>
The read() syscall is not guaranteed to return all data from a file. The
default ROM loader implementation currently does not take this into account,
instead failing if all bytes are not read at once. This change loads the ROM
using g_file_get_contents() instead, which correctly reads all data using
multiple calls to read() while also returning the loaded ROM size.
Signed-off-by: Gregor Haas <gregorhaas1997@gmail.com>
Reviewed-by: Xingtao Yao <yaoxt.fnst@fujitsu.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <20240628182706.99525-1-gregorhaas1997@gmail.com>
[PMD: Use gsize with g_file_get_contents()]
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/core/loader.c | 32 +++++++-------------------------
1 file changed, 7 insertions(+), 25 deletions(-)
diff --git a/hw/core/loader.c b/hw/core/loader.c
index a3bea1e718..39bd8f9e4d 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -1076,8 +1076,8 @@ ssize_t rom_add_file(const char *file, const char *fw_dir,
{
MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
Rom *rom;
- ssize_t rc;
- int fd = -1;
+ gsize size;
+ g_autoptr(GError) gerr = NULL;
char devpath[100];
if (as && mr) {
@@ -1095,10 +1095,10 @@ ssize_t rom_add_file(const char *file, const char
*fw_dir,
rom->path = g_strdup(file);
}
- fd = open(rom->path, O_RDONLY | O_BINARY);
- if (fd == -1) {
- fprintf(stderr, "Could not open option rom '%s': %s\n",
- rom->path, strerror(errno));
+ if (!g_file_get_contents(rom->path, (gchar **) &rom->data,
+ &size, &gerr)) {
+ fprintf(stderr, "rom: file %-20s: error %s\n",
+ rom->name, gerr->message);
goto err;
}
@@ -1107,23 +1107,8 @@ ssize_t rom_add_file(const char *file, const char
*fw_dir,
rom->fw_file = g_strdup(file);
}
rom->addr = addr;
- rom->romsize = lseek(fd, 0, SEEK_END);
- if (rom->romsize == -1) {
- fprintf(stderr, "rom: file %-20s: get size error: %s\n",
- rom->name, strerror(errno));
- goto err;
- }
-
+ rom->romsize = size;
rom->datasize = rom->romsize;
- rom->data = g_malloc0(rom->datasize);
- lseek(fd, 0, SEEK_SET);
- rc = read(fd, rom->data, rom->datasize);
- if (rc != rom->datasize) {
- fprintf(stderr, "rom: file %-20s: read error: rc=%zd (expected %zd)\n",
- rom->name, rc, rom->datasize);
- goto err;
- }
- close(fd);
rom_insert(rom);
if (rom->fw_file && fw_cfg) {
const char *basename;
@@ -1160,9 +1145,6 @@ ssize_t rom_add_file(const char *file, const char *fw_dir,
return 0;
err:
- if (fd != -1)
- close(fd);
-
rom_free(rom);
return -1;
}
--
2.41.0
- [PULL 00/13] Misc HW/UI patches for 2024-07-16, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 01/13] hw/core/loader: allow loading larger ROMs,
Philippe Mathieu-Daudé <=
- [PULL 02/13] hw/isa/vt82c686: Turn "intr" irq into a named gpio, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 03/13] include/hw/qdev-core.h: Correct and clarify gpio doc comments, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 04/13] loader: remove load_image_gzipped function as its not used anywhere, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 05/13] accel/tcg: Make cpu_exec_interrupt hook mandatory, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 06/13] system/cpus: Add cpu_pause() function, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 07/13] esp: remove transfer size check from DMA DATA IN and DATA OUT transfers, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 08/13] ui/cocoa: Release CGColorSpace, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 09/13] ui/console: Convert mouse visibility parameter into bool, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 13/13] system/physmem: use return value of ram_block_discard_require() as errno, Philippe Mathieu-Daudé, 2024/07/16
- [PULL 10/13] ui/cocoa: Add cursor composition, Philippe Mathieu-Daudé, 2024/07/16