From patchwork Mon Aug 13 05:21:04 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivia Yin X-Patchwork-Id: 176861 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 4C5322C0084 for ; Mon, 13 Aug 2012 15:48:49 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751238Ab2HMFss (ORCPT ); Mon, 13 Aug 2012 01:48:48 -0400 Received: from ch1ehsobe002.messaging.microsoft.com ([216.32.181.182]:1886 "EHLO ch1outboundpool.messaging.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751226Ab2HMFsr (ORCPT ); Mon, 13 Aug 2012 01:48:47 -0400 Received: from mail51-ch1-R.bigfish.com (10.43.68.241) by CH1EHSOBE003.bigfish.com (10.43.70.53) with Microsoft SMTP Server id 14.1.225.23; Mon, 13 Aug 2012 05:48:46 +0000 Received: from mail51-ch1 (localhost [127.0.0.1]) by mail51-ch1-R.bigfish.com (Postfix) with ESMTP id 5567A1C03D1; Mon, 13 Aug 2012 05:48:46 +0000 (UTC) X-Forefront-Antispam-Report: CIP:70.37.183.190; KIP:(null); UIP:(null); IPV:NLI; H:mail.freescale.net; RD:none; EFVD:NLI X-SpamScore: 0 X-BigFish: VS0(zzzz1202hzz8275bhz2dh2a8h668h839hd24he5bhf0ah107ah) Received: from mail51-ch1 (localhost.localdomain [127.0.0.1]) by mail51-ch1 (MessageSwitch) id 1344836924102217_11121; Mon, 13 Aug 2012 05:48:44 +0000 (UTC) Received: from CH1EHSMHS018.bigfish.com (snatpool2.int.messaging.microsoft.com [10.43.68.235]) by mail51-ch1.bigfish.com (Postfix) with ESMTP id 0D4A74600AC; Mon, 13 Aug 2012 05:48:44 +0000 (UTC) Received: from mail.freescale.net (70.37.183.190) by CH1EHSMHS018.bigfish.com (10.43.70.18) with Microsoft SMTP Server (TLS) id 14.1.225.23; Mon, 13 Aug 2012 05:48:43 +0000 Received: from az84smr01.freescale.net (10.64.34.197) by 039-SN1MMR1-005.039d.mgd.msft.net (10.84.1.17) with Microsoft SMTP Server (TLS) id 14.2.298.5; Mon, 13 Aug 2012 00:48:42 -0500 Received: from localhost.localdomain (rock.ap.freescale.net [10.193.20.106]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id q7D5mebu002924; Sun, 12 Aug 2012 22:48:41 -0700 From: Olivia Yin To: , CC: Olivia Yin Subject: [PATCH 1/2] QEMU: extract file_load() function from rom_add_file() for reusing Date: Mon, 13 Aug 2012 13:21:04 +0800 Message-ID: <1344835265-23499-1-git-send-email-hong-hua.yin@freescale.com> X-Mailer: git-send-email 1.6.4 MIME-Version: 1.0 X-OriginatorOrg: freescale.com Sender: kvm-ppc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm-ppc@vger.kernel.org Sanity check in rom_add_file() could be reused by other image loaders. Signed-off-by: Olivia Yin --- 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,