From: Olivia Yin <hong-hua.yin@freescale.com> To: <kvm-ppc@vger.kernel.org>, <kvm@vger.kernel.org> Cc: Olivia Yin <hong-hua.yin@freescale.com> Subject: [PATCH 1/2] QEMU: extract file_load() function from rom_add_file() for reusing Date: Mon, 13 Aug 2012 13:21:04 +0800 [thread overview] Message-ID: <1344835265-23499-1-git-send-email-hong-hua.yin@freescale.com> (raw) Sanity check in rom_add_file() could be reused by other image loaders. Signed-off-by: Olivia Yin <hong-hua.yin@freescale.com> --- This patch is based on branch 'ppc-next' of Alex's upstream QEMU repo: http://repo.or.cz/r/qemu/agraf.git hw/loader.c | 61 +++++++++++++++++++++++++++++++--------------------------- 1 files changed, 33 insertions(+), 28 deletions(-) diff --git a/hw/loader.c b/hw/loader.c index 33acc2f..8475850 100644 --- a/hw/loader.c +++ b/hw/loader.c @@ -86,6 +86,36 @@ int load_image(const char *filename, uint8_t *addr) return size; } +static int file_load(const char *file, uint8_t **data) +{ + int rc, fd = -1; + int size; + + fd = open(file, O_RDONLY | O_BINARY); + if (fd == -1) { + fprintf(stderr, "Could not open file '%s': %s\n", + file, strerror(errno)); + goto err; + } + + size = lseek(fd, 0, SEEK_END); + *data = g_malloc0(size); + lseek(fd, 0, SEEK_SET); + rc = read(fd, *data, size); + if (rc != size) { + fprintf(stderr, "file %-20s: read error: rc=%d (expected %zd)\n", + file, rc, size); + goto err; + } + close(fd); + return size; +err: + if (fd != -1) + close(fd); + g_free(*data); + return -1; +} + /* read()-like version */ ssize_t read_targphys(const char *name, int fd, target_phys_addr_t dst_addr, size_t nbytes) @@ -568,38 +598,22 @@ int rom_add_file(const char *file, const char *fw_dir, target_phys_addr_t addr, int32_t bootindex) { Rom *rom; - int rc, fd = -1; char devpath[100]; rom = g_malloc0(sizeof(*rom)); rom->name = g_strdup(file); + rom->addr = addr; rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name); if (rom->path == NULL) { 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)); - goto err; - } - if (fw_dir) { rom->fw_dir = g_strdup(fw_dir); rom->fw_file = g_strdup(file); } - rom->addr = addr; - rom->romsize = lseek(fd, 0, SEEK_END); - rom->data = g_malloc0(rom->romsize); - lseek(fd, 0, SEEK_SET); - rc = read(fd, rom->data, rom->romsize); - if (rc != rom->romsize) { - fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n", - rom->name, rc, rom->romsize); - goto err; - } - close(fd); + + rom->romsize = file_load(rom->path, &rom->data); rom_insert(rom); if (rom->fw_file && fw_cfg) { const char *basename; @@ -621,15 +635,6 @@ int rom_add_file(const char *file, const char *fw_dir, add_boot_device_path(bootindex, NULL, devpath); return 0; - -err: - if (fd != -1) - close(fd); - g_free(rom->data); - g_free(rom->path); - g_free(rom->name); - g_free(rom); - return -1; } int rom_add_blob(const char *name, const void *blob, size_t len, -- 1.7.1
WARNING: multiple messages have this Message-ID (diff)
From: Olivia Yin <hong-hua.yin@freescale.com> To: kvm-ppc@vger.kernel.org, kvm@vger.kernel.org Cc: Olivia Yin <hong-hua.yin@freescale.com> Subject: [PATCH 1/2] QEMU: extract file_load() function from rom_add_file() for reusing Date: Mon, 13 Aug 2012 05:21:04 +0000 [thread overview] Message-ID: <1344835265-23499-1-git-send-email-hong-hua.yin@freescale.com> (raw) Sanity check in rom_add_file() could be reused by other image loaders. Signed-off-by: Olivia Yin <hong-hua.yin@freescale.com> --- This patch is based on branch 'ppc-next' of Alex's upstream QEMU repo: http://repo.or.cz/r/qemu/agraf.git hw/loader.c | 61 +++++++++++++++++++++++++++++++--------------------------- 1 files changed, 33 insertions(+), 28 deletions(-) diff --git a/hw/loader.c b/hw/loader.c index 33acc2f..8475850 100644 --- a/hw/loader.c +++ b/hw/loader.c @@ -86,6 +86,36 @@ int load_image(const char *filename, uint8_t *addr) return size; } +static int file_load(const char *file, uint8_t **data) +{ + int rc, fd = -1; + int size; + + fd = open(file, O_RDONLY | O_BINARY); + if (fd = -1) { + fprintf(stderr, "Could not open file '%s': %s\n", + file, strerror(errno)); + goto err; + } + + size = lseek(fd, 0, SEEK_END); + *data = g_malloc0(size); + lseek(fd, 0, SEEK_SET); + rc = read(fd, *data, size); + if (rc != size) { + fprintf(stderr, "file %-20s: read error: rc=%d (expected %zd)\n", + file, rc, size); + goto err; + } + close(fd); + return size; +err: + if (fd != -1) + close(fd); + g_free(*data); + return -1; +} + /* read()-like version */ ssize_t read_targphys(const char *name, int fd, target_phys_addr_t dst_addr, size_t nbytes) @@ -568,38 +598,22 @@ int rom_add_file(const char *file, const char *fw_dir, target_phys_addr_t addr, int32_t bootindex) { Rom *rom; - int rc, fd = -1; char devpath[100]; rom = g_malloc0(sizeof(*rom)); rom->name = g_strdup(file); + rom->addr = addr; rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name); if (rom->path = NULL) { 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)); - goto err; - } - if (fw_dir) { rom->fw_dir = g_strdup(fw_dir); rom->fw_file = g_strdup(file); } - rom->addr = addr; - rom->romsize = lseek(fd, 0, SEEK_END); - rom->data = g_malloc0(rom->romsize); - lseek(fd, 0, SEEK_SET); - rc = read(fd, rom->data, rom->romsize); - if (rc != rom->romsize) { - fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n", - rom->name, rc, rom->romsize); - goto err; - } - close(fd); + + rom->romsize = file_load(rom->path, &rom->data); rom_insert(rom); if (rom->fw_file && fw_cfg) { const char *basename; @@ -621,15 +635,6 @@ int rom_add_file(const char *file, const char *fw_dir, add_boot_device_path(bootindex, NULL, devpath); return 0; - -err: - if (fd != -1) - close(fd); - g_free(rom->data); - g_free(rom->path); - g_free(rom->name); - g_free(rom); - return -1; } int rom_add_blob(const char *name, const void *blob, size_t len, -- 1.7.1
next reply other threads:[~2012-08-13 5:21 UTC|newest] Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top 2012-08-13 5:21 Olivia Yin [this message] 2012-08-13 5:21 ` [PATCH 1/2] QEMU: extract file_load() function from rom_add_file() for reusing Olivia Yin 2012-08-13 5:21 ` [PATCH 2/2] QEMU: register reset handlers to write images into memory Olivia Yin 2012-08-13 5:21 ` Olivia Yin 2012-08-13 8:41 ` [PATCH 1/2] QEMU: extract file_load() function from rom_add_file() for reusing Alexander Graf 2012-08-13 8:41 ` Alexander Graf
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=1344835265-23499-1-git-send-email-hong-hua.yin@freescale.com \ --to=hong-hua.yin@freescale.com \ --cc=kvm-ppc@vger.kernel.org \ --cc=kvm@vger.kernel.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: linkBe 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.