From: Tomita Moeko <tomitamoeko@gmail.com>
To: qemu-devel@nongnu.org
Cc: "Alex Williamson" <alex@shazbot.org>,
"Cédric Le Goater" <clg@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Tomita Moeko" <tomitamoeko@gmail.com>,
"K S Maan" <kirandeepmaan45@gmail.com>
Subject: [PATCH v2 1/7] hw/pci: Recalculate option ROM checksum before patching ID
Date: Mon, 8 Jun 2026 21:45:52 +0800 [thread overview]
Message-ID: <20260608134559.23971-2-tomitamoeko@gmail.com> (raw)
In-Reply-To: <20260608134559.23971-1-tomitamoeko@gmail.com>
pci_patch_ids() only adjusts checksum based on the new IDs. For an
option ROM with invalid checksum, the patched one will still have
an invalid checksum. Always calculate the checksum and patch it if
necessary to ensure the option ROM is valid.
This is intended for fixing the romfile used in IGD passthrough as
multiple IGD devices share the same rom with possible non-matching
device ID, and its checksum is known to be bogus [1].
A helper function pci_rom_calculate_checksum() is added and exported
for reusing in IGD-specific quirk later.
[1] hw/vfio/pci.c:1090
Reported-by: K S Maan <kirandeepmaan45@gmail.com>
Signed-off-by: Tomita Moeko <tomitamoeko@gmail.com>
---
hw/pci/pci.c | 35 ++++++++++++++++++++++++++---------
include/hw/pci/pci.h | 2 ++
2 files changed, 28 insertions(+), 9 deletions(-)
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index cec065d108..742917f79d 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -2479,6 +2479,21 @@ static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
return found;
}
+uint8_t pci_rom_calculate_checksum(const uint8_t *ptr, uint32_t size)
+{
+ uint8_t checksum = 0;
+ uint32_t i;
+
+ for (i = 0; i < size; i++) {
+ if (i == 6) {
+ continue;
+ }
+ checksum += ptr[i];
+ }
+
+ return checksum;
+}
+
/* Patch the PCI vendor and device ids 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_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size)
@@ -2514,25 +2529,27 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size)
trace_pci_rom_and_pci_ids(pdev->romfile, vendor_id, device_id,
rom_vendor_id, rom_device_id);
- checksum = ptr[6];
+ /* In case the checksum is bogus */
+ checksum = pci_rom_calculate_checksum(ptr, size);
if (vendor_id != rom_vendor_id) {
/* Patch vendor id and checksum (at offset 6 for etherboot roms). */
- checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
- checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
- trace_pci_rom_checksum_change(ptr[6], checksum);
- ptr[6] = checksum;
+ checksum += (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
+ checksum -= (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
pci_set_word(ptr + pcir_offset + 4, vendor_id);
}
if (device_id != rom_device_id) {
/* Patch device id and checksum (at offset 6 for etherboot roms). */
- checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
- checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
- trace_pci_rom_checksum_change(ptr[6], checksum);
- ptr[6] = checksum;
+ checksum += (uint8_t)device_id + (uint8_t)(device_id >> 8);
+ checksum -= (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
pci_set_word(ptr + pcir_offset + 6, device_id);
}
+
+ if (ptr[6] != (uint8_t)-checksum) {
+ trace_pci_rom_checksum_change(ptr[6], (uint8_t)-checksum);
+ ptr[6] = (uint8_t)-checksum;
+ }
}
/* Add an option rom for the device */
diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
index 5b179091de..2d8a4ad0eb 100644
--- a/include/hw/pci/pci.h
+++ b/include/hw/pci/pci.h
@@ -1103,4 +1103,6 @@ void pci_set_enabled(PCIDevice *pci_dev, bool state);
void pci_set_power(PCIDevice *pci_dev, bool state);
int pci_pm_init(PCIDevice *pci_dev, uint8_t offset, Error **errp);
+uint8_t pci_rom_calculate_checksum(const uint8_t *ptr, uint32_t size);
+
#endif
--
2.53.0
next prev parent reply other threads:[~2026-06-08 13:47 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-08 13:45 [PATCH v2 0/7] vfio/igd: Fix garbled screen on IGD passthrough with legacy VBIOS Tomita Moeko
2026-06-08 13:45 ` Tomita Moeko [this message]
2026-06-09 15:36 ` [PATCH v2 1/7] hw/pci: Recalculate option ROM checksum before patching ID Alex Williamson
2026-06-08 13:45 ` [PATCH v2 2/7] hw/pci: Skip EFI option ROM in pci_patch_ids() Tomita Moeko
2026-06-09 15:36 ` Alex Williamson
2026-06-08 13:45 ` [PATCH v2 3/7] hw/pci: Introduce rom_need_patch_id flag in PCIDevice Tomita Moeko
2026-06-08 13:45 ` [PATCH v2 4/7] hw/pci: Promote pci_patch_ids() to public pci_rom_patch_ids() Tomita Moeko
2026-06-08 13:45 ` [PATCH v2 5/7] vfio/igd: Toggle rom_need_patch_id flag on IGD devices Tomita Moeko
2026-06-08 13:45 ` [PATCH v2 6/7] vfio/pci: Use pci_rom_patch_ids() for IGD ROM ID patching Tomita Moeko
2026-06-08 13:45 ` [PATCH v2 7/7] vfio/igd: Clear saved BDSM in legacy VBIOS ROM at load time Tomita Moeko
2026-06-09 3:05 ` K S Maan
2026-06-09 5:04 ` Cédric Le Goater
2026-06-09 11:55 ` Cédric Le Goater
2026-06-09 15:35 ` Alex Williamson
2026-06-09 15:46 ` Alex Williamson
2026-06-09 11:20 ` [PATCH v2 0/7] vfio/igd: Fix garbled screen on IGD passthrough with legacy VBIOS K S Maan
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=20260608134559.23971-2-tomitamoeko@gmail.com \
--to=tomitamoeko@gmail.com \
--cc=alex@shazbot.org \
--cc=clg@redhat.com \
--cc=kirandeepmaan45@gmail.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.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: link
Be 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.