From patchwork Wed Nov 21 02:06:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Zhijian X-Patchwork-Id: 10691545 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D9B9D13BF for ; Wed, 21 Nov 2018 02:09:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C586F2B1A0 for ; Wed, 21 Nov 2018 02:09:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BA10A2B1A6; Wed, 21 Nov 2018 02:09:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6E2852B1A0 for ; Wed, 21 Nov 2018 02:09:29 +0000 (UTC) Received: from localhost ([::1]:36849 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPHxI-0000at-Os for patchwork-qemu-devel@patchwork.kernel.org; Tue, 20 Nov 2018 21:09:28 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49888) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPHtq-0005lo-6r for qemu-devel@nongnu.org; Tue, 20 Nov 2018 21:05:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gPHtp-0002pl-CG for qemu-devel@nongnu.org; Tue, 20 Nov 2018 21:05:54 -0500 Received: from mga05.intel.com ([192.55.52.43]:12715) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gPHtm-0002gn-0P for qemu-devel@nongnu.org; Tue, 20 Nov 2018 21:05:51 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Nov 2018 18:05:47 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,259,1539673200"; d="scan'208";a="90916998" Received: from bangyuan-mobl.ccr.corp.intel.com (HELO haswell-OptiPlex-9020.ccr.corp.intel.com) ([10.255.31.159]) by orsmga007.jf.intel.com with ESMTP; 20 Nov 2018 18:05:46 -0800 From: Li Zhijian To: qemu-devel@nongnu.org Date: Wed, 21 Nov 2018 10:06:04 +0800 Message-Id: <1542765966-20244-4-git-send-email-lizhijian@cn.fujitsu.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1542765966-20244-3-git-send-email-lizhijian@cn.fujitsu.com> References: <1542765966-20244-1-git-send-email-lizhijian@cn.fujitsu.com> <1542765966-20244-2-git-send-email-lizhijian@cn.fujitsu.com> <1542765966-20244-3-git-send-email-lizhijian@cn.fujitsu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.43 Subject: [Qemu-devel] [PATCH v2 3/5] refactor load_image/load_image_size X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Philip Li , Li Zhijian , Li Zhijian Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Don't expect read(2) can always read as many as it's told. Signed-off-by: Li Zhijian --- hw/core/loader.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/hw/core/loader.c b/hw/core/loader.c index 0d53229..5f891e2 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -78,7 +78,7 @@ int64_t get_image_size(const char *filename) ssize_t load_image(const char *filename, uint8_t *addr) { int fd; - ssize_t size; + ssize_t size, r = 0, l = 0; fd = open(filename, O_RDONLY | O_BINARY); if (fd < 0) return -1; @@ -91,11 +91,16 @@ ssize_t load_image(const char *filename, uint8_t *addr) } lseek(fd, 0, SEEK_SET); - if (read(fd, addr, size) != size) { - close(fd); - return -1; + while ((r = read(fd, addr + l, size - l)) > 0 ) { + l += r; } close(fd); + + if (l != size) { + fprintf(stderr, "expect read %ld, actual read %ld\n", size, l); + return -1; + } + return size; } @@ -103,21 +108,20 @@ ssize_t load_image(const char *filename, uint8_t *addr) ssize_t load_image_size(const char *filename, void *addr, size_t size) { int fd; - ssize_t actsize; + ssize_t actsize, l = 0; fd = open(filename, O_RDONLY | O_BINARY); if (fd < 0) { return -1; } - actsize = read(fd, addr, size); - if (actsize < 0) { - close(fd); - return -1; + while ((actsize = read(fd, addr + l, size - l)) > 0) { + l += actsize; } + close(fd); - return actsize; + return actsize < 0 ? -1 : l; } /* read()-like version */