Change license from GPLv3 to LGPLv3.
[seabios.git] / src / optionroms.c
index 8888aa524fda11a962c8808b5d6d9aefba614600..c2a563b56bd8b8e0d162f9e26c8a6ffeb7e5252b 100644 (file)
@@ -3,23 +3,22 @@
 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
 // Copyright (C) 2002  MandrakeSoft S.A.
 //
-// This file may be distributed under the terms of the GNU GPLv3 license.
+// This file may be distributed under the terms of the GNU LGPLv3 license.
 
 #include "bregs.h" // struct bregs
-#include "biosvar.h" // struct ipl_entry_s
+#include "farptr.h" // FARPTR_TO_SEG
+#include "config.h" // CONFIG_*
 #include "util.h" // dprintf
 #include "pci.h" // pci_find_class
 #include "pci_regs.h" // PCI_ROM_ADDRESS
 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
+#include "boot.h" // IPL
 
 
 /****************************************************************
  * Definitions
  ****************************************************************/
 
-// $PnP string with special alignment in romlayout.S
-extern char pnp_string[];
-
 struct rom_header {
     u16 signature;
     u8 size;
@@ -65,16 +64,12 @@ struct pnp_data {
     u16 staticresource;
 } PACKED;
 
-#define OPTIONROM_BDF_1 0x0000
-#define OPTIONROM_MEM_1 0x00000000
-#define OPTIONROM_BDF_2 0x0000
-#define OPTIONROM_MEM_2 0x00000000
-
 #define OPTION_ROM_START 0xc0000
 #define OPTION_ROM_SIGNATURE 0xaa55
 #define OPTION_ROM_ALIGN 2048
 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
+#define PCIROM_CODETYPE_X86 0
 
 // Next available position for an option rom.
 static u32 next_rom;
@@ -97,17 +92,12 @@ callrom(struct rom_header *rom, u16 offset, u16 bdf)
     br.bx = 0xffff;
     br.dx = 0xffff;
     br.es = SEG_BIOS;
-    br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
+    br.di = get_pnp_offset();
     br.cs = seg;
     br.ip = offset;
-    // XXX - should call option rom in "big real mode".
-    call16(&br);
+    call16big(&br);
 
     debug_serial_setup();
-
-    if (GET_BDA(ebda_seg) != SEG_EBDA)
-        BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
-                 , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
 }
 
 // Verify that an option rom looks valid
@@ -131,27 +121,47 @@ static struct pnp_data *
 get_pnp_rom(struct rom_header *rom)
 {
     struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
-    if (pnp->signature != *(u32*)pnp_string)
+    if (pnp->signature != PNP_SIGNATURE)
         return NULL;
     return pnp;
 }
 
+// Check for multiple pnp option rom headers.
+static struct pnp_data *
+get_pnp_next(struct rom_header *rom, struct pnp_data *pnp)
+{
+    if (! pnp->nextoffset)
+        return NULL;
+    pnp = (struct pnp_data *)((u8*)rom + pnp->nextoffset);
+    if (pnp->signature != PNP_SIGNATURE)
+        return NULL;
+    return pnp;
+}
+
+// Check if a valid option rom has a pci struct; return it if so.
+static struct pci_data *
+get_pci_rom(struct rom_header *rom)
+{
+    struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
+    if (pci->signature != PCI_ROM_SIGNATURE)
+        return NULL;
+    return pci;
+}
+
 // Add a BEV vector for a given pnp compatible option rom.
 static void
 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
 {
-#define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
-
     // Found a device that thinks it can boot the system.  Record
     // its BEV and product name string.
 
     if (! CONFIG_BOOT)
         return;
 
-    if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
+    if (IPL.count >= ARRAY_SIZE(IPL.table))
         return;
 
-    struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
+    struct ipl_entry_s *ip = &IPL.table[IPL.count];
     ip->type = IPL_TYPE_BEV;
     ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
 
@@ -159,7 +169,7 @@ add_ipl(struct rom_header *rom, struct pnp_data *pnp)
     if (desc)
         ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
 
-    ebda->ipl.count++;
+    IPL.count++;
 }
 
 // Copy a rom to its permanent location below 1MiB
@@ -195,6 +205,12 @@ map_optionrom(u16 bdf)
 {
     dprintf(6, "Attempting to map option rom on dev %x\n", 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);
+        return NULL;
+    }
+
     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
@@ -204,6 +220,11 @@ map_optionrom(u16 bdf)
     if (!sz || sz == 0xffffffff)
         goto fail;
 
+    if (orig < 16*1024*1024) {
+        dprintf(6, "Preset rom address doesn't look valid\n");
+        goto fail;
+    }
+
     // Looks like a rom - enable it.
     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
 
@@ -216,17 +237,14 @@ map_optionrom(u16 bdf)
             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
             goto fail;
         }
-        if (!rom->pcioffset) {
-            dprintf(6, "No PCI offset\n");
-            goto fail;
-        }
-        struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
-        if (pci->signature != PCI_ROM_SIGNATURE) {
-            dprintf(6, "Invalid pci signature (got %x)\n", pci->signature);
+        struct pci_data *pci = get_pci_rom(rom);
+        if (! pci) {
+            dprintf(6, "No valid pci signature found\n");
             goto fail;
         }
+
         u32 vd = (pci->device << 16) | pci->vendor;
-        if (vd == vendev && pci->type == 0)
+        if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
             // A match
             break;
         dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
@@ -303,9 +321,10 @@ optionrom_setup()
     } else {
         // Find and deploy PCI roms.
         int bdf, max;
-        foreachpci(bdf, max, 0) {
+        foreachpci(bdf, max) {
             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
-            if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA)
+            if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA
+                || (CONFIG_ATA && v == PCI_CLASS_STORAGE_IDE))
                 continue;
             init_optionrom(bdf);
         }
@@ -331,9 +350,13 @@ optionrom_setup()
         if (pnp->bev)
             // Can boot system - add to IPL list.
             add_ipl(rom, pnp);
-        else if (pnp->bcv)
-            // Has BCV - run it now.
-            callrom(rom, pnp->bcv, 0);
+        else
+            // Check for BCV (there may be multiple).
+            while (pnp && pnp->bcv) {
+                // Has BCV - run it now.
+                callrom(rom, pnp->bcv, 0);
+                pnp = get_pnp_next(rom, pnp);
+            }
     }
 }
 
@@ -361,7 +384,7 @@ vga_setup()
         next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
     } else {
         // Find and deploy PCI VGA rom.
-        int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA, 0);
+        int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
         if (bdf < 0)
             // Device not found
             return;