Convert option rom scan to use struct pci_device.
diff --git a/src/optionroms.c b/src/optionroms.c
index a612f3c..b2415cc 100644
--- a/src/optionroms.c
+++ b/src/optionroms.c
@@ -9,7 +9,7 @@
 #include "farptr.h" // FLATPTR_TO_SEG
 #include "config.h" // CONFIG_*
 #include "util.h" // dprintf
-#include "pci.h" // foreachbdf
+#include "pci.h" // foreachpci
 #include "pci_regs.h" // PCI_ROM_ADDRESS
 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
 #include "boot.h" // IPL
@@ -163,10 +163,10 @@
 static struct pci_data *
 get_pci_rom(struct rom_header *rom)
 {
-    struct pci_data *pci = (void*)((u32)rom + rom->pcioffset);
-    if (pci->signature != PCI_ROM_SIGNATURE)
+    struct pci_data *pd = (void*)((u32)rom + rom->pcioffset);
+    if (pd->signature != PCI_ROM_SIGNATURE)
         return NULL;
-    return pci;
+    return pd;
 }
 
 // Return start of code in 0xc0000-0xf0000 space.
@@ -238,11 +238,11 @@
 
 // Check if an option rom is at a hardcoded location or in CBFS.
 static struct rom_header *
-lookup_hardcode(u32 vendev)
+lookup_hardcode(struct pci_device *pci)
 {
     char fname[17];
     snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
-             , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
+             , pci->vendor, pci->device);
     int ret = romfile_copy(romfile_find(fname), (void*)RomEnd
                            , max_rom() - RomEnd);
     if (ret <= 0)
@@ -273,16 +273,35 @@
  * PCI roms
  ****************************************************************/
 
+// Verify device is a vga device with legacy address decoding enabled.
+static int
+is_pci_vga(struct pci_device *pci)
+{
+    if (pci->class != PCI_CLASS_DISPLAY_VGA)
+        return 0;
+    u16 cmd = pci_config_readw(pci->bdf, PCI_COMMAND);
+    if (!(cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY))
+        return 0;
+    while (pci->parent) {
+        pci = pci->parent;
+        u32 ctrl = pci_config_readb(pci->bdf, PCI_BRIDGE_CONTROL);
+        if (!(ctrl & PCI_BRIDGE_CTL_VGA))
+            return 0;
+    }
+    return 1;
+}
+
 // Map the option rom of a given PCI device.
 static struct rom_header *
-map_pcirom(u16 bdf, u32 vendev)
+map_pcirom(struct pci_device *pci)
 {
+    u16 bdf = pci->bdf;
     dprintf(6, "Attempting to map option rom on dev %02x:%02x.%x\n"
             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
 
-    u8 htype = pci_config_readb(bdf, PCI_HEADER_TYPE);
-    if ((htype & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
-        dprintf(6, "Skipping non-normal pci device (type=%x)\n", htype);
+    if ((pci->header_type & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
+        dprintf(6, "Skipping non-normal pci device (type=%x)\n"
+                , pci->header_type);
         return NULL;
     }
 
@@ -309,29 +328,29 @@
     for (;;) {
         dprintf(5, "Inspecting possible rom at %p (vd=%04x:%04x"
                 " bdf=%02x:%02x.%x)\n"
-                , rom, pci_vd_to_ven(vendev), pci_vd_to_dev(vendev)
+                , rom, pci->vendor, pci->device
                 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
         if (rom->signature != OPTION_ROM_SIGNATURE) {
             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
             goto fail;
         }
-        struct pci_data *pci = get_pci_rom(rom);
-        if (! pci) {
+        struct pci_data *pd = get_pci_rom(rom);
+        if (! pd) {
             dprintf(6, "No valid pci signature found\n");
             goto fail;
         }
 
-        u32 vd = pci_vd(pci->vendor, pci->device);
-        if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
+        if (pd->vendor == pci->vendor && pd->device == pci->device
+            && pd->type == PCIROM_CODETYPE_X86)
             // A match
             break;
-        dprintf(6, "Didn't match dev/ven (got %08x) or type (got %d)\n"
-                , vd, pci->type);
-        if (pci->indicator & 0x80) {
+        dprintf(6, "Didn't match dev/ven (got %04x:%04x) or type (got %d)\n"
+                , pd->vendor, pd->device, pd->type);
+        if (pd->indicator & 0x80) {
             dprintf(6, "No more images left\n");
             goto fail;
         }
-        rom = (void*)((u32)rom + pci->ilen * 512);
+        rom = (void*)((u32)rom + pd->ilen * 512);
     }
 
     rom = copy_rom(rom);
@@ -345,15 +364,15 @@
 
 // Attempt to map and initialize the option rom on a given PCI device.
 static int
-init_pcirom(u16 bdf, int isvga, u64 *sources)
+init_pcirom(struct pci_device *pci, int isvga, u64 *sources)
 {
-    u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
+    u16 bdf = pci->bdf;
     dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (vd %04x:%04x)\n"
             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
-            , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
-    struct rom_header *rom = lookup_hardcode(vendev);
+            , pci->vendor, pci->device);
+    struct rom_header *rom = lookup_hardcode(pci);
     if (! rom)
-        rom = map_pcirom(bdf, vendev);
+        rom = map_pcirom(pci);
     if (! rom)
         // No ROM present.
         return -1;
@@ -389,13 +408,13 @@
         }
     } else {
         // Find and deploy PCI roms.
-        int bdf, max;
-        foreachbdf(bdf, max) {
-            u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
+        struct pci_device *pci;
+        foreachpci(pci) {
+            u16 v = pci->class;
             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
                 continue;
-            init_pcirom(bdf, 0, sources);
+            init_pcirom(pci, 0, sources);
         }
 
         // Find and deploy CBFS roms not associated with a device.
@@ -458,9 +477,14 @@
         memset((void*)RomEnd, 0, _max_rom() - RomEnd);
 
         // Find and deploy PCI VGA rom.
-        int bdf = VGAbdf = pci_find_vga();
-        if (bdf >= 0)
-            init_pcirom(bdf, 1, NULL);
+        struct pci_device *pci;
+        foreachpci(pci) {
+            if (!is_pci_vga(pci))
+                continue;
+            VGAbdf = pci->bdf;
+            init_pcirom(pci, 1, NULL);
+            break;
+        }
 
         // Find and deploy CBFS vga-style roms not associated with a device.
         run_file_roms("vgaroms/", 1, NULL);
diff --git a/src/pci.c b/src/pci.c
index f0953e9..9a696b9 100644
--- a/src/pci.c
+++ b/src/pci.c
@@ -163,61 +163,6 @@
     }
 }
 
-// Find a vga device with legacy address decoding enabled.
-int
-pci_find_vga(void)
-{
-    int bdf = 0x0000, max = 0x0100;
-    for (;;) {
-        if (bdf >= max) {
-            if (CONFIG_PCI_ROOT1 && bdf <= (CONFIG_PCI_ROOT1 << 8))
-                bdf = CONFIG_PCI_ROOT1 << 8;
-            else if (CONFIG_PCI_ROOT2 && bdf <= (CONFIG_PCI_ROOT2 << 8))
-                bdf = CONFIG_PCI_ROOT2 << 8;
-            else
-            	return -1;
-            max = bdf + 0x0100;
-        }
-
-        u16 cls = pci_config_readw(bdf, PCI_CLASS_DEVICE);
-        if (cls == 0x0000 || cls == 0xffff) {
-            // Device not present.
-            if (pci_bdf_to_fn(bdf) == 0)
-                bdf += 8;
-            else
-                bdf += 1;
-            continue;
-        }
-        if (cls == PCI_CLASS_DISPLAY_VGA) {
-            u16 cmd = pci_config_readw(bdf, PCI_COMMAND);
-            if (cmd & PCI_COMMAND_IO && cmd & PCI_COMMAND_MEMORY)
-                // Found active vga card
-                return bdf;
-        }
-
-        // Check if device is a bridge.
-        u8 hdr = pci_config_readb(bdf, PCI_HEADER_TYPE);
-        u8 ht = hdr & 0x7f;
-        if (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS) {
-            u32 ctrl = pci_config_readb(bdf, PCI_BRIDGE_CONTROL);
-            if (ctrl & PCI_BRIDGE_CTL_VGA) {
-                // Found a VGA enabled bridge.
-                u32 pbus = pci_config_readl(bdf, PCI_PRIMARY_BUS);
-                bdf = (pbus & 0xff00);
-                max = bdf + 0x100;
-                continue;
-            }
-        }
-
-        if (pci_bdf_to_fn(bdf) == 0 && (hdr & 0x80) == 0)
-            // Last found device wasn't a multi-function device - skip to
-            // the next device.
-            bdf += 8;
-        else
-            bdf += 1;
-    }
-}
-
 // Search for a device with the specified vendor and device ids.
 int
 pci_find_device(u16 vendid, u16 devid)
diff --git a/src/pci.h b/src/pci.h
index f1d84ed..a214bb1 100644
--- a/src/pci.h
+++ b/src/pci.h
@@ -25,16 +25,6 @@
     return (bus << 8) | devfn;
 }
 
-static inline u32 pci_vd(u16 vendor, u16 device) {
-    return (device << 16) | vendor;
-}
-static inline u16 pci_vd_to_ven(u32 vd) {
-    return vd & 0xffff;
-}
-static inline u16 pci_vd_to_dev(u32 vd) {
-    return vd >> 16;
-}
-
 void pci_config_writel(u16 bdf, u32 addr, u32 val);
 void pci_config_writew(u16 bdf, u32 addr, u16 val);
 void pci_config_writeb(u16 bdf, u32 addr, u8 val);
@@ -43,7 +33,6 @@
 u8 pci_config_readb(u16 bdf, u32 addr);
 void pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on);
 
-int pci_find_vga(void);
 int pci_find_device(u16 vendid, u16 devid);
 int pci_find_class(u16 classid);