@@ -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;
}