From patchwork Fri Oct 15 20:51:06 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Weil X-Patchwork-Id: 67998 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 331F8B70E0 for ; Sat, 16 Oct 2010 07:52:44 +1100 (EST) Received: from localhost ([127.0.0.1]:59231 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P6rGQ-0006b8-Ck for incoming@patchwork.ozlabs.org; Fri, 15 Oct 2010 16:52:30 -0400 Received: from [140.186.70.92] (port=48704 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P6rFY-0006Zf-N4 for qemu-devel@nongnu.org; Fri, 15 Oct 2010 16:51:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P6rFX-00087x-Do for qemu-devel@nongnu.org; Fri, 15 Oct 2010 16:51:36 -0400 Received: from moutng.kundenserver.de ([212.227.17.8]:51258) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P6rFX-00086U-32 for qemu-devel@nongnu.org; Fri, 15 Oct 2010 16:51:35 -0400 Received: from flocke.weilnetz.de (p54ADBE0D.dip.t-dialin.net [84.173.190.13]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0LzHnN-1OcDO80O2L-014rgA; Fri, 15 Oct 2010 22:51:25 +0200 Received: from stefan by flocke.weilnetz.de with local (Exim 4.72) (envelope-from ) id 1P6rFM-00021k-8t; Fri, 15 Oct 2010 22:51:24 +0200 From: Stefan Weil To: QEMU Developers Date: Fri, 15 Oct 2010 22:51:06 +0200 Message-Id: <1287175867-7757-1-git-send-email-weil@mail.berlios.de> X-Mailer: git-send-email 1.7.1 In-Reply-To: References: X-Provags-ID: V02:K0:gCHBDL2pUE/bq7W//6gEjnK9stwkAtOurrx7L4T7QOc +FOrzSjgQanyAoHEWRTz5sjR+w4c919z0VP1LlGuH6ZIX45aW0 svsW2BtqayLqQycnOBvs9Q/4IAw905vqpdmiWzUca77xgOAeyh uw7btc9WbipTauA/9X2BXMcCt3/zskIvJ/uTdf1O3JcLjD12mh 2FFh8YFmQZs+qCagpD+oQ70NRe9u4hV4p70DFyEqotIbg5irEN mkHJ2ksZz/fKp X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Markus Armbruster , "Michael S. Tsirkin" Subject: [Qemu-devel] [PATCH 1/2] pci: Automatically patch PCI device id in PCI ROM X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org PCI device with different device ids sometimes share the same rom code. Only the device id and the checksum differ in a boot rom for such devices. The i825xx ethernet controller family is a typical example which is implemented in hw/eepro100.c. It uses at least 3 different device ids, so normally 3 boot roms would be needed. By automatically patching the device id (and the checksum) in qemu, all emulated family members can share the same boot rom. Cc: Markus Armbruster Cc: Michael S. Tsirkin Signed-off-by: Stefan Weil --- hw/pci.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 53 insertions(+), 0 deletions(-) diff --git a/hw/pci.c b/hw/pci.c index 1280d4d..c1f8218 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -1797,6 +1797,57 @@ static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, p cpu_register_physical_memory(addr, size, pdev->rom_offset); } +/* Patch the PCI device id in a PCI rom image if necessary. + This is needed for an option rom which is used for more than one device. */ +static void pci_patch_device_id(PCIDevice *pdev, uint8_t *ptr, int size) +{ + uint16_t vendor_id; + uint16_t device_id; + uint16_t rom_vendor_id; + uint16_t rom_device_id; + uint16_t rom_magic; + uint16_t pcir_offset; + + /* Words in rom data are little endian (like in PCI configuration), + so they can be read / written with pci_get_word / pci_set_word. */ + + /* Only a valid rom will be patched. */ + rom_magic = pci_get_word(ptr); + if (rom_magic != 0xaa55) { + PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic); + return; + } + pcir_offset = pci_get_word(ptr + 0x18); + if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) { + PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset); + return; + } + + vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID); + device_id = pci_get_word(pdev->config + PCI_DEVICE_ID); + rom_vendor_id = pci_get_word(ptr + pcir_offset + 4); + rom_device_id = pci_get_word(ptr + pcir_offset + 6); + + /* Don't patch a rom with wrong vendor id (might be changed if needed). */ + if (vendor_id != rom_vendor_id) { + return; + } + + PCI_DPRINTF("ROM id %04x%04x / PCI id %04x%04x\n", + vendor_id, device_id, rom_vendor_id, rom_device_id); + + if (device_id != rom_device_id) { + /* Patch device id and checksum (at offset 6 for etherboot roms). */ + uint8_t checksum; + checksum = ptr[6]; + checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8); + checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8); + PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); + ptr[6] = checksum; + pci_set_word(ptr + pcir_offset + 6, device_id); + } +} + /* Add an option rom for the device */ static int pci_add_option_rom(PCIDevice *pdev) { @@ -1849,6 +1900,8 @@ static int pci_add_option_rom(PCIDevice *pdev) load_image(path, ptr); qemu_free(path); + pci_patch_device_id(pdev, ptr, size); + pci_register_bar(pdev, PCI_ROM_SLOT, size, 0, pci_map_option_rom);