From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alex Williamson Subject: [PATCH 3/3] device-assignment: Byte-wise ROM read Date: Fri, 30 Jul 2010 13:40:25 -0600 Message-ID: <20100730194025.10110.47519.stgit@localhost6.localdomain6> References: <20100730193941.10110.92913.stgit@localhost6.localdomain6> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Cc: ddutile@redhat.com, chrisw@redhat.com, gleb@redhat.com, alex.williamson@redhat.com To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:8074 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754956Ab0G3Tk1 (ORCPT ); Fri, 30 Jul 2010 15:40:27 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o6UJeRHN019756 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 30 Jul 2010 15:40:27 -0400 In-Reply-To: <20100730193941.10110.92913.stgit@localhost6.localdomain6> Sender: kvm-owner@vger.kernel.org List-ID: The host kernel filters the PCI option ROM, returning only bytes for the actual ROM size, not for the whole BAR. That means we typically do a short read of the PCI sysfs ROM file. Read it a byte at a time so we know how much to actually copy and only skip the copy if we get nothing. Signed-off-by: Alex Williamson --- hw/device-assignment.c | 9 +++++---- 1 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hw/device-assignment.c b/hw/device-assignment.c index 0e82a16..3bb7f0b 100644 --- a/hw/device-assignment.c +++ b/hw/device-assignment.c @@ -1680,19 +1680,20 @@ static void assigned_dev_load_option_rom(AssignedDevice *dev) return; } - ret = fread(buf, size, 1, fp); - if (!feof(fp) || ferror(fp) || ret != 1) { + if (!(ret = fread(buf, 1, size, fp))) { free(buf); fclose(fp); return; } fclose(fp); + /* The number of bytes read is often much smaller than the BAR size */ + size = ret; + /* Copy ROM contents into the space backing the ROM BAR */ if (dev->v_addrs[PCI_ROM_SLOT].r_size >= size && dev->v_addrs[PCI_ROM_SLOT].u.r_virtbase) { - memcpy(dev->v_addrs[PCI_ROM_SLOT].u.r_virtbase, - buf, size); + memcpy(dev->v_addrs[PCI_ROM_SLOT].u.r_virtbase, buf, size); } free(buf);