Only allow CONFIG_XEN when not CONFIG_COREBOOT.
[seabios.git] / src / optionroms.c
index 15f67950b9f254e50b811b8f50e9f5a6eba5bfa7..37a4e6c512091c77d6fbc64098a9dbd8e3df24d7 100644 (file)
@@ -9,10 +9,11 @@
 #include "farptr.h" // FLATPTR_TO_SEG
 #include "config.h" // CONFIG_*
 #include "util.h" // dprintf
-#include "pci.h" // pci_find_class
+#include "pci.h" // foreachpci
 #include "pci_regs.h" // PCI_ROM_ADDRESS
 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
 #include "boot.h" // IPL
+#include "paravirt.h" // qemu_cfg_*
 
 
 /****************************************************************
@@ -71,7 +72,7 @@ struct pnp_data {
 #define PCIROM_CODETYPE_X86 0
 
 // The end of the last deployed rom.
-u32 RomEnd;
+u32 RomEnd = BUILD_ROM_START;
 
 
 /****************************************************************
@@ -87,14 +88,16 @@ __callrom(struct rom_header *rom, u16 offset, u16 bdf)
 
     struct bregs br;
     memset(&br, 0, sizeof(br));
+    br.flags = F_IF;
     br.ax = bdf;
     br.bx = 0xffff;
     br.dx = 0xffff;
     br.es = SEG_BIOS;
     br.di = get_pnp_offset();
-    br.cs = seg;
-    br.ip = offset;
+    br.code = SEGOFF(seg, offset);
+    start_preempt();
     call16big(&br);
+    finish_preempt();
 
     debug_serial_setup();
 }
@@ -128,7 +131,8 @@ is_valid_rom(struct rom_header *rom)
     if (sum != 0) {
         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
                 , rom, len, sum);
-        return 0;
+        if (CONFIG_OPTIONROMS_CHECKSUM)
+            return 0;
     }
     return 1;
 }
@@ -165,19 +169,30 @@ get_pci_rom(struct rom_header *rom)
     return pci;
 }
 
+// Return start of code in 0xc0000-0xf0000 space.
+static inline u32 _max_rom(void) {
+    extern u8 code32flat_start[], code32init_end[];
+    return CONFIG_RELOCATE_INIT ? (u32)code32init_end : (u32)code32flat_start;
+}
+// Return the memory position up to which roms may be located.
+static inline u32 max_rom(void) {
+    u32 end = _max_rom();
+    return end > BUILD_BIOS_ADDR ? BUILD_BIOS_ADDR : end;
+}
+
 // Copy a rom to its permanent location below 1MiB
 static struct rom_header *
 copy_rom(struct rom_header *rom)
 {
     u32 romsize = rom->size * 512;
-    if (RomEnd + romsize > BUILD_BIOS_ADDR) {
+    if (RomEnd + romsize > max_rom()) {
         // Option rom doesn't fit.
-        dprintf(1, "Option rom %p doesn't fit.\n", rom);
+        warn_noalloc();
         return NULL;
     }
     dprintf(4, "Copying option rom (size %d) from %p to %x\n"
             , romsize, rom, RomEnd);
-    memcpy((void*)RomEnd, rom, romsize);
+    iomemcpy((void*)RomEnd, rom, romsize);
     return (void*)RomEnd;
 }
 
@@ -197,6 +212,25 @@ init_optionrom(struct rom_header *rom, u16 bdf, int isvga)
     return 0;
 }
 
+#define RS_PCIROM (1LL<<33)
+
+static void
+setRomSource(u64 *sources, struct rom_header *rom, u64 source)
+{
+    if (sources)
+        sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN] = source;
+}
+
+static int
+getRomPriority(u64 *sources, struct rom_header *rom, int instance)
+{
+    u64 source = sources[((u32)rom - BUILD_ROM_START) / OPTION_ROM_ALIGN];
+    if (!source)
+        return -1;
+    if (source & RS_PCIROM)
+        return bootprio_find_pci_rom(source, instance);
+    return bootprio_find_named_rom(romfile_name(source), instance);
+}
 
 /****************************************************************
  * Roms in CBFS
@@ -214,27 +248,31 @@ lookup_hardcode(u32 vendev)
         && ((OPTIONROM_VENDEV_2 >> 16)
             | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
         return copy_rom((void*)OPTIONROM_MEM_2);
-    int ret = cbfs_copy_optionrom((void*)RomEnd, BUILD_BIOS_ADDR - RomEnd
-                                  , vendev);
-    if (ret < 0)
+    char fname[17];
+    snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
+             , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
+    int ret = romfile_copy(romfile_find(fname), (void*)RomEnd
+                           , max_rom() - RomEnd);
+    if (ret <= 0)
         return NULL;
     return (void*)RomEnd;
 }
 
 // Run all roms in a given CBFS directory.
 static void
-run_cbfs_roms(const char *prefix, int isvga)
+run_file_roms(const char *prefix, int isvga, u64 *sources)
 {
-    struct cbfs_file *file = NULL;
+    u32 file = 0;
     for (;;) {
-        int iscomp = 0;
-        file = cbfs_finddataprefix(prefix, file, &iscomp);
+        file = romfile_findprefix(prefix, file);
         if (!file)
             break;
-        int ret = cbfs_copyfile(file, (void*)RomEnd, BUILD_BIOS_ADDR - RomEnd
-                                , iscomp);
-        if (ret > 0)
-            init_optionrom((void*)RomEnd, 0, isvga);
+        struct rom_header *rom = (void*)RomEnd;
+        int ret = romfile_copy(file, rom, max_rom() - RomEnd);
+        if (ret > 0) {
+            setRomSource(sources, rom, file);
+            init_optionrom(rom, 0, isvga);
+        }
     }
 }
 
@@ -277,8 +315,10 @@ map_pcirom(u16 bdf, u32 vendev)
 
     struct rom_header *rom = (void*)orig;
     for (;;) {
-        dprintf(5, "Inspecting possible rom at %p (dv=%08x bdf=%x)\n"
-                , rom, vendev, bdf);
+        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)
+                , 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;
@@ -289,7 +329,7 @@ map_pcirom(u16 bdf, u32 vendev)
             goto fail;
         }
 
-        u32 vd = (pci->device << 16) | pci->vendor;
+        u32 vd = pci_vd(pci->vendor, pci->device);
         if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
             // A match
             break;
@@ -313,18 +353,19 @@ fail:
 
 // Attempt to map and initialize the option rom on a given PCI device.
 static int
-init_pcirom(u16 bdf, int isvga)
+init_pcirom(u16 bdf, int isvga, u64 *sources)
 {
     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
-    dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %08x)\n"
+    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)
-            , vendev);
+            , pci_vd_to_ven(vendev), pci_vd_to_dev(vendev));
     struct rom_header *rom = lookup_hardcode(vendev);
     if (! rom)
         rom = map_pcirom(bdf, vendev);
     if (! rom)
         // No ROM present.
         return -1;
+    setRomSource(sources, rom, RS_PCIROM | bdf);
     return init_optionrom(rom, bdf, isvga);
 }
 
@@ -334,19 +375,20 @@ init_pcirom(u16 bdf, int isvga)
  ****************************************************************/
 
 void
-optionrom_setup()
+optionrom_setup(void)
 {
     if (! CONFIG_OPTIONROMS)
         return;
 
     dprintf(1, "Scan for option roms\n");
-
+    u64 sources[(BUILD_BIOS_ADDR - BUILD_ROM_START) / OPTION_ROM_ALIGN];
+    memset(sources, 0, sizeof(sources));
     u32 post_vga = RomEnd;
 
     if (CONFIG_OPTIONROMS_DEPLOYED) {
         // Option roms are already deployed on the system.
         u32 pos = RomEnd;
-        while (pos < BUILD_BIOS_ADDR) {
+        while (pos < max_rom()) {
             int ret = init_optionrom((void*)pos, 0, 0);
             if (ret)
                 pos += OPTION_ROM_ALIGN;
@@ -361,11 +403,11 @@ optionrom_setup()
             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
                 || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
                 continue;
-            init_pcirom(bdf, 0);
+            init_pcirom(bdf, 0, sources);
         }
 
         // Find and deploy CBFS roms not associated with a device.
-        run_cbfs_roms("genroms/", 0);
+        run_file_roms("genroms/", 0, sources);
     }
 
     // All option roms found and deployed - now build BEV/BCV vectors.
@@ -381,19 +423,24 @@ optionrom_setup()
         struct pnp_data *pnp = get_pnp_rom(rom);
         if (! pnp) {
             // Legacy rom.
-            add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
+            boot_add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0
+                         , getRomPriority(sources, rom, 0));
             continue;
         }
         // PnP rom.
-        if (pnp->bev)
+        if (pnp->bev) {
             // Can boot system - add to IPL list.
-            add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
-        else
+            boot_add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname
+                         , getRomPriority(sources, rom, 0));
+        } else {
             // Check for BCV (there may be multiple).
+            int instance = 0;
             while (pnp && pnp->bcv) {
-                add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
+                boot_add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname
+                             , getRomPriority(sources, rom, instance++));
                 pnp = get_pnp_next(rom, pnp);
             }
+        }
     }
 }
 
@@ -404,11 +451,8 @@ optionrom_setup()
 
 // Call into vga code to turn on console.
 void
-vga_setup()
+vga_setup(void)
 {
-    VGAbdf = -1;
-    RomEnd = BUILD_ROM_START;
-
     if (! CONFIG_OPTIONROMS)
         return;
 
@@ -418,13 +462,16 @@ vga_setup()
         // Option roms are already deployed on the system.
         init_optionrom((void*)BUILD_ROM_START, 0, 1);
     } else {
+        // Clear option rom memory
+        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);
+            init_pcirom(bdf, 1, NULL);
 
         // Find and deploy CBFS vga-style roms not associated with a device.
-        run_cbfs_roms("vgaroms/", 1);
+        run_file_roms("vgaroms/", 1, NULL);
     }
 
     if (RomEnd == BUILD_ROM_START) {
@@ -433,18 +480,11 @@ vga_setup()
         return;
     }
 
-    dprintf(1, "Turning on vga console\n");
-    struct bregs br;
-    memset(&br, 0, sizeof(br));
-    br.ax = 0x0003;
-    call16_int(0x10, &br);
-
-    // Write to screen.
-    printf("Starting SeaBIOS (version %s)\n\n", VERSION);
+    enable_vga_console();
 }
 
 void
-s3_resume_vga_init()
+s3_resume_vga_init(void)
 {
     if (!CONFIG_S3_RESUME_VGA_INIT)
         return;