Add support for gcc v3.x compilers.
[seabios.git] / src / optionroms.c
index 0f99c2ee739e3b291e5aed456cfd6f3028f26bf6..2856217a85ce9ad3eac8e9dc7d9fd762f016b646 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" // FLATPTR_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;
@@ -84,8 +83,8 @@ static u32 next_rom;
 static void
 callrom(struct rom_header *rom, u16 offset, u16 bdf)
 {
-    u16 seg = FARPTR_TO_SEG(rom);
-    dprintf(1, "Running option rom at %x:%x\n", seg, offset);
+    u16 seg = FLATPTR_TO_SEG(rom);
+    dprintf(1, "Running option rom at %04x:%04x\n", seg, offset);
 
     struct bregs br;
     memset(&br, 0, sizeof(br));
@@ -93,26 +92,33 @@ 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;
     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));
+// Execute a BCV option rom registered via add_bcv().
+void
+call_bcv(u16 seg, u16 ip)
+{
+    callrom(MAKE_FLATPTR(seg, 0), ip, 0);
 }
 
 // Verify that an option rom looks valid
 static int
 is_valid_rom(struct rom_header *rom)
 {
+    dprintf(6, "Checking rom %p (sig %x size %d)\n"
+            , rom, rom->signature, rom->size);
     if (rom->signature != OPTION_ROM_SIGNATURE)
         return 0;
+    if (! rom->size)
+        return 0;
     u32 len = rom->size * 512;
-    u8 sum = checksum((void*)rom, len);
+    u8 sum = checksum(rom, len);
     if (sum != 0) {
         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
                 , rom, len, sum);
@@ -126,7 +132,19 @@ 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;
 }
@@ -141,32 +159,6 @@ get_pci_rom(struct rom_header *rom)
     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))
-        return;
-
-    struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
-    ip->type = IPL_TYPE_BEV;
-    ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
-
-    u16 desc = pnp->productname;
-    if (desc)
-        ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
-
-    ebda->ipl.count++;
-}
-
 // Copy a rom to its permanent location below 1MiB
 static struct rom_header *
 copy_rom(struct rom_header *rom)
@@ -177,28 +169,37 @@ copy_rom(struct rom_header *rom)
         dprintf(1, "Option rom %p doesn't fit.\n", rom);
         return NULL;
     }
-    dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
+    dprintf(4, "Copying option rom (size %d) from %p to %x\n"
+            , romsize, rom, next_rom);
     memcpy((void*)next_rom, rom, romsize);
     return (struct rom_header *)next_rom;
 }
 
 // Check if an option rom is at a hardcoded location for a device.
 static struct rom_header *
-lookup_hardcode(u16 bdf)
+lookup_hardcode(u32 vendev)
 {
-    if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
+    if (OPTIONROM_VENDEV_1
+        && ((OPTIONROM_VENDEV_1 >> 16)
+            | ((OPTIONROM_VENDEV_1 & 0xffff)) << 16) == vendev)
         return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
-    else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
+    if (OPTIONROM_VENDEV_2
+        && ((OPTIONROM_VENDEV_2 >> 16)
+            | ((OPTIONROM_VENDEV_2 & 0xffff)) << 16) == vendev)
         return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
-    // XXX - check LAR when in coreboot?
-    return NULL;
+    int ret = cbfs_copy_optionrom((void*)next_rom, BUILD_BIOS_ADDR - next_rom
+                                  , vendev);
+    if (ret < 0)
+        return NULL;
+    return (struct rom_header *)next_rom;
 }
 
 // Map the option rom of a given PCI device.
 static struct rom_header *
-map_optionrom(u16 bdf)
+map_optionrom(u16 bdf, u32 vendev)
 {
-    dprintf(6, "Attempting to map option rom on dev %x\n", 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) {
@@ -215,7 +216,9 @@ map_optionrom(u16 bdf)
     if (!sz || sz == 0xffffffff)
         goto fail;
 
-    if (orig < 16*1024*1024) {
+    if (orig == sz || (u32)(orig + 4*1024*1024) < 20*1024*1024) {
+        // Don't try to map to a pci addresses at its max, in the last
+        // 4MiB of ram, or the first 16MiB of ram.
         dprintf(6, "Preset rom address doesn't look valid\n");
         goto fail;
     }
@@ -223,10 +226,9 @@ map_optionrom(u16 bdf)
     // Looks like a rom - enable it.
     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
 
-    u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
     struct rom_header *rom = (struct rom_header *)orig;
     for (;;) {
-        dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
+        dprintf(5, "Inspecting possible rom at %p (dv=%x bdf=%x)\n"
                 , rom, vendev, bdf);
         if (rom->signature != OPTION_ROM_SIGNATURE) {
             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
@@ -242,7 +244,7 @@ map_optionrom(u16 bdf)
         if (vd == vendev && pci->type == PCIROM_CODETYPE_X86)
             // A match
             break;
-        dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
+        dprintf(6, "Didn't match dev/ven (got %x) or type (got %d)\n"
                 , vd, pci->type);
         if (pci->indicator & 0x80) {
             dprintf(6, "No more images left\n");
@@ -260,18 +262,10 @@ fail:
     return NULL;
 }
 
-// Attempt to map and initialize the option rom on a given PCI device.
+// Adjust for the size of an option rom; allow it to resize.
 static struct rom_header *
-init_optionrom(u16 bdf)
+verifysize_optionrom(struct rom_header *rom, u16 bdf)
 {
-    dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
-    struct rom_header *rom = lookup_hardcode(bdf);
-    if (! rom)
-        rom = map_optionrom(bdf);
-    if (! rom)
-        // No ROM present.
-        return NULL;
-
     if (! is_valid_rom(rom))
         return NULL;
 
@@ -284,6 +278,23 @@ init_optionrom(u16 bdf)
     return rom;
 }
 
+// Attempt to map and initialize the option rom on a given PCI device.
+static struct rom_header *
+init_optionrom(u16 bdf)
+{
+    u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
+    dprintf(4, "Attempting to init PCI bdf %02x:%02x.%x (dev/ven %x)\n"
+            , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf)
+            , vendev);
+    struct rom_header *rom = lookup_hardcode(vendev);
+    if (! rom)
+        rom = map_optionrom(bdf, vendev);
+    if (! rom)
+        // No ROM present.
+        return NULL;
+    return verifysize_optionrom(rom, bdf);
+}
+
 
 /****************************************************************
  * Non-VGA option rom init
@@ -323,6 +334,16 @@ optionrom_setup()
                 continue;
             init_optionrom(bdf);
         }
+
+        // Find and deploy CBFS roms not associated with a device.
+        struct cbfs_file *tmp = NULL;
+        for (;;) {
+            tmp = cbfs_copy_gen_optionrom(
+                (void*)next_rom, BUILD_BIOS_ADDR - next_rom, tmp);
+            if (!tmp)
+                break;
+            verifysize_optionrom((void*)next_rom, 0);
+        }
     }
 
     // All option roms found and deployed - now build BEV/BCV vectors.
@@ -337,17 +358,20 @@ optionrom_setup()
         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
         struct pnp_data *pnp = get_pnp_rom(rom);
         if (! pnp) {
-            // Legacy rom - run init vector now.
-            callrom(rom, OPTION_ROM_INITVECTOR, 0);
+            // Legacy rom.
+            add_bcv(FLATPTR_TO_SEG(rom), OPTION_ROM_INITVECTOR, 0);
             continue;
         }
         // PnP rom.
         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);
+            add_bev(FLATPTR_TO_SEG(rom), pnp->bev, pnp->productname);
+        else
+            // Check for BCV (there may be multiple).
+            while (pnp && pnp->bcv) {
+                add_bcv(FLATPTR_TO_SEG(rom), pnp->bcv, pnp->productname);
+                pnp = get_pnp_next(rom, pnp);
+            }
     }
 }