[SeaBIOS] [PATCH 3/5] Convert common compile time checks to started/runningOnX() checks.

Kevin O'Connor kevin at koconnor.net
Fri Feb 8 06:08:51 CET 2013


Convert CONFIG_COREBOOT, CONFIG_CSM, and usingXen() calls to
startedOnX() and runningOnX() calls as appropriate.

This change should not alter the runtime behavior as Kconfig will
still enforce compile time constraints.  However, this change will
make it easier for future Kconfig changes to enable simultaneous
multi-platform support.

Signed-off-by: Kevin O'Connor <kevin at koconnor.net>
---
 src/ata.c         |  3 ++-
 src/block.c       |  3 ++-
 src/blockcmd.c    |  2 +-
 src/boot.c        | 10 +++++-----
 src/coreboot.c    |  8 ++++----
 src/floppy.c      |  3 ++-
 src/mtrr.c        |  4 ++--
 src/optionroms.c  |  3 ++-
 src/paravirt.c    |  4 ++--
 src/pciinit.c     |  4 ++--
 src/post.c        | 10 +++++++---
 src/ramdisk.c     |  2 +-
 src/resume.c      |  6 +-----
 src/shadow.c      | 12 ++++++++----
 src/smm.c         | 10 +++-------
 src/smp.c         |  4 ++--
 src/virtio-blk.c  |  4 ++--
 src/virtio-scsi.c |  2 +-
 src/xen.c         |  2 +-
 src/xen.h         |  6 ------
 20 files changed, 50 insertions(+), 52 deletions(-)

diff --git a/src/ata.c b/src/ata.c
index d1d8dff..f1223f0 100644
--- a/src/ata.c
+++ b/src/ata.c
@@ -19,6 +19,7 @@
 #include "disk.h" // struct ata_s
 #include "ata.h" // ATA_CB_STAT
 #include "blockcmd.h" // CDB_CMD_READ_10
+#include "paravirt.h" // runningOnQEMU
 
 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
 
@@ -1008,7 +1009,7 @@ static const struct pci_device_id pci_ata_tbl[] = {
 static void
 ata_scan(void)
 {
-    if (!CONFIG_COREBOOT && !PCIDevices) {
+    if (runningOnQEMU() && !PCIDevices) {
         // No PCI devices found - probably a QEMU "-M isapc" machine.
         // Try using ISA ports for ATA controllers.
         init_controller(NULL, IRQ_ATA1
diff --git a/src/block.c b/src/block.c
index e5f3038..274eae2 100644
--- a/src/block.c
+++ b/src/block.c
@@ -13,6 +13,7 @@
 #include "ahci.h" // process_ahci_op
 #include "virtio-blk.h" // process_virtio_blk_op
 #include "blockcmd.h" // cdb_*
+#include "paravirt.h" // runningOnQEMU
 
 u8 FloppyCount VAR16VISIBLE;
 u8 CDCount;
@@ -62,7 +63,7 @@ static u8
 get_translation(struct drive_s *drive_g)
 {
     u8 type = GET_GLOBAL(drive_g->type);
-    if (! CONFIG_COREBOOT && type == DTYPE_ATA) {
+    if (runningOnQEMU() && type == DTYPE_ATA) {
         // Emulators pass in the translation info via nvram.
         u8 ataid = GET_GLOBAL(drive_g->cntl_id);
         u8 channel = ataid / 2;
diff --git a/src/blockcmd.c b/src/blockcmd.c
index e033ba7..71175cc 100644
--- a/src/blockcmd.c
+++ b/src/blockcmd.c
@@ -165,7 +165,7 @@ scsi_drive_setup(struct drive_s *drive, const char *s, int prio)
     // but some old USB keys only support a very small subset of SCSI which
     // does not even include the MODE SENSE command!
     //
-    if (! CONFIG_COREBOOT && memcmp(vendor, "QEMU", 5) == 0) {
+    if (CONFIG_QEMU && memcmp(vendor, "QEMU", 5) == 0) {
         struct cdbres_mode_sense_geom geomdata;
         ret = cdb_mode_sense_geom(&dop, &geomdata);
         if (ret == 0) {
diff --git a/src/boot.c b/src/boot.c
index 85d5051..6b3e2a8 100644
--- a/src/boot.c
+++ b/src/boot.c
@@ -121,7 +121,7 @@ build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
 
 int bootprio_find_pci_device(struct pci_device *pci)
 {
-    if (CONFIG_CSM)
+    if (startedOnCSM())
         return csm_bootprio_pci(pci);
     if (!CONFIG_BOOTORDER)
         return -1;
@@ -147,7 +147,7 @@ int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
 
 int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
 {
-    if (CONFIG_CSM)
+    if (startedOnCSM())
         return csm_bootprio_ata(pci, chanid, slave);
     if (!CONFIG_BOOTORDER)
         return -1;
@@ -163,7 +163,7 @@ int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
 
 int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
 {
-    if (CONFIG_CSM)
+    if (startedOnCSM())
         return csm_bootprio_fdc(pci, port, fdid);
     if (!CONFIG_BOOTORDER)
         return -1;
@@ -250,7 +250,7 @@ boot_init(void)
     if (! CONFIG_BOOT)
         return;
 
-    if (!CONFIG_COREBOOT) {
+    if (runningOnQEMU()) {
         // On emulators, get boot order from nvram.
         if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
             CheckFloppySig = 0;
@@ -609,7 +609,7 @@ boot_cdrom(struct drive_s *drive_g)
 static void
 boot_cbfs(struct cbfs_file *file)
 {
-    if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
+    if (!CONFIG_COREBOOT_FLASH)
         return;
     printf("Booting from CBFS...\n");
     cbfs_run_payload(file);
diff --git a/src/coreboot.c b/src/coreboot.c
index 40a7e72..daaec7f 100644
--- a/src/coreboot.c
+++ b/src/coreboot.c
@@ -303,7 +303,7 @@ struct cbfs_file {
 static int
 cbfs_copyfile(struct romfile_s *file, void *dst, u32 maxlen)
 {
-    if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
+    if (!CONFIG_COREBOOT_FLASH)
         return -1;
 
     u32 size = file->rawsize;
@@ -335,7 +335,7 @@ cbfs_copyfile(struct romfile_s *file, void *dst, u32 maxlen)
 void
 coreboot_cbfs_init(void)
 {
-    if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
+    if (!CONFIG_COREBOOT_FLASH)
         return;
 
     struct cbfs_header *hdr = *(void **)CBFS_HEADPTR_ADDR;
@@ -401,7 +401,7 @@ struct cbfs_payload {
 void
 cbfs_run_payload(struct cbfs_file *file)
 {
-    if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
+    if (!CONFIG_COREBOOT_FLASH || !file)
         return;
     dprintf(1, "Run %s\n", file->filename);
     struct cbfs_payload *pay = (void*)file + be32_to_cpu(file->offset);
@@ -452,7 +452,7 @@ cbfs_run_payload(struct cbfs_file *file)
 void
 cbfs_payload_setup(void)
 {
-    if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
+    if (!CONFIG_COREBOOT_FLASH)
         return;
     struct romfile_s *file = NULL;
     for (;;) {
diff --git a/src/floppy.c b/src/floppy.c
index e9f8916..14136ed 100644
--- a/src/floppy.c
+++ b/src/floppy.c
@@ -16,6 +16,7 @@
 #include "boot.h" // boot_add_floppy
 #include "pci.h" // pci_to_bdf
 #include "pci_ids.h" // PCI_CLASS_BRIDGE_ISA
+#include "paravirt.h" // runningOnQEMU
 
 #define FLOPPY_SIZE_CODE 0x02 // 512 byte sectors
 #define FLOPPY_DATALEN 0xff   // Not used - because size code is 0x02
@@ -125,7 +126,7 @@ floppy_setup(void)
         return;
     dprintf(3, "init floppy drives\n");
 
-    if (CONFIG_COREBOOT) {
+    if (!runningOnQEMU()) {
         u8 type = romfile_loadint("etc/floppy0", 0);
         if (type)
             addFloppy(0, type);
diff --git a/src/mtrr.c b/src/mtrr.c
index 2cbf234..90ae517 100644
--- a/src/mtrr.c
+++ b/src/mtrr.c
@@ -6,8 +6,8 @@
 
 #include "util.h" // dprintf
 #include "config.h" // CONFIG_*
-#include "xen.h" // usingXen
 #include "pci.h" // pcimem_start
+#include "paravirt.h" // startedOnQEMU()
 
 #define MSR_MTRRcap                    0x000000fe
 #define MSR_MTRRfix64K_00000           0x00000250
@@ -34,7 +34,7 @@
 
 void mtrr_setup(void)
 {
-    if (!CONFIG_MTRR_INIT || CONFIG_COREBOOT || usingXen())
+    if (!CONFIG_MTRR_INIT || !startedOnQEMU())
         return;
 
     u32 eax, ebx, ecx, edx, cpuid_features;
diff --git a/src/optionroms.c b/src/optionroms.c
index c9e7d7b..3876823 100644
--- a/src/optionroms.c
+++ b/src/optionroms.c
@@ -14,6 +14,7 @@
 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
 #include "boot.h" // IPL
 #include "optionroms.h" // struct rom_header
+#include "paravirt.h" // runningOnQEMU
 
 
 /****************************************************************
@@ -425,7 +426,7 @@ vgarom_setup(void)
 
     // Load some config settings that impact VGA.
     EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
-    S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", !CONFIG_COREBOOT);
+    S3ResumeVga = romfile_loadint("etc/s3-resume-vga-init", runningOnQEMU());
     ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
 
     if (CONFIG_OPTIONROMS_DEPLOYED) {
diff --git a/src/paravirt.c b/src/paravirt.c
index 35b7c11..c44f2bf 100644
--- a/src/paravirt.c
+++ b/src/paravirt.c
@@ -7,7 +7,7 @@
 //
 // This file may be distributed under the terms of the GNU LGPLv3 license.
 
-#include "config.h" // CONFIG_COREBOOT
+#include "config.h" // CONFIG_QEMU
 #include "util.h" // dprintf
 #include "byteorder.h" // be32_to_cpu
 #include "ioport.h" // outw
@@ -391,7 +391,7 @@ struct QemuCfgFile {
 
 void qemu_romfile_init(void)
 {
-    if (CONFIG_COREBOOT || !qemu_cfg_present)
+    if (!CONFIG_QEMU || !qemu_cfg_present)
         return;
 
     u32 count;
diff --git a/src/pciinit.c b/src/pciinit.c
index 34b47b6..f29e444 100644
--- a/src/pciinit.c
+++ b/src/pciinit.c
@@ -11,8 +11,8 @@
 #include "pci_regs.h" // PCI_COMMAND
 #include "ioport.h" // PORT_ATA1_CMD_BASE
 #include "config.h" // CONFIG_*
-#include "xen.h" // usingXen
 #include "memmap.h" // add_e820
+#include "paravirt.h" // startedOnQEMU()
 #include "dev-q35.h"
 
 /* PM Timer ticks per second (HZ) */
@@ -734,7 +734,7 @@ static void pci_bios_map_devices(struct pci_bus *busses)
 void
 pci_setup(void)
 {
-    if (CONFIG_COREBOOT || usingXen()) {
+    if (!startedOnQEMU()) {
         // PCI setup already done by coreboot or Xen - just do probe.
         pci_probe_devices();
         return;
diff --git a/src/post.c b/src/post.c
index 4ab020c..b84e75a 100644
--- a/src/post.c
+++ b/src/post.c
@@ -177,11 +177,11 @@ platform_hardware_setup(void)
     smp_setup();
 
     // Setup external BIOS interface tables
-    if (CONFIG_COREBOOT)
+    if (startedOnCoreboot())
         coreboot_biostable_setup();
-    else if (usingXen())
+    else if (startedOnXen())
         xen_biostable_setup();
-    else
+    else if (startedOnQEMU())
         qemu_biostable_setup();
 }
 
@@ -324,6 +324,8 @@ debug_splash(void)
 void VISIBLE32INIT
 handle_elf(void)
 {
+    if (!CONFIG_COREBOOT && !CONFIG_XEN)
+        return;
     debug_splash();
 
     coreboot_preinit();
@@ -350,6 +352,8 @@ qemu_post(void)
 void VISIBLE32FLAT
 handle_post(void)
 {
+    if (!CONFIG_QEMU)
+        return;
     debug_splash();
 
     // Allow writes to modify bios area (0xf0000)
diff --git a/src/ramdisk.c b/src/ramdisk.c
index 9249a49..b9da2ad 100644
--- a/src/ramdisk.c
+++ b/src/ramdisk.c
@@ -88,7 +88,7 @@ ramdisk_copy(struct disk_op_s *op, int iswrite)
 int
 process_ramdisk_op(struct disk_op_s *op)
 {
-    if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
+    if (!CONFIG_FLASH_FLOPPY)
         return 0;
 
     switch (op->command) {
diff --git a/src/resume.c b/src/resume.c
index ffc84fc..adc3594 100644
--- a/src/resume.c
+++ b/src/resume.c
@@ -130,11 +130,7 @@ tryReboot(void)
     dprintf(1, "Attempting a hard reboot\n");
 
     // Setup for reset on qemu.
-    if (! CONFIG_COREBOOT) {
-        qemu_prep_reset();
-        if (HaveRunPost)
-            apm_shutdown();
-    }
+    qemu_prep_reset();
 
     // Try keyboard controller reboot.
     i8042_reboot();
diff --git a/src/shadow.c b/src/shadow.c
index a2195da..00d6c7f 100644
--- a/src/shadow.c
+++ b/src/shadow.c
@@ -10,8 +10,8 @@
 #include "config.h" // CONFIG_*
 #include "pci_ids.h" // PCI_VENDOR_ID_INTEL
 #include "pci_regs.h" // PCI_VENDOR_ID
-#include "xen.h" // usingXen
 #include "dev-q35.h" // PCI_VENDOR_ID_INTEL
+#include "paravirt.h" // startedOnQEMU
 
 // On the emulators, the bios at 0xf0000 is also at 0xffff0000
 #define BIOS_SRC_OFFSET 0xfff00000
@@ -119,7 +119,7 @@ static const struct pci_device_id dram_controller_make_readonly_tbl[] = {
 void
 make_bios_writable(void)
 {
-    if (CONFIG_COREBOOT || usingXen())
+    if (!CONFIG_QEMU)
         return;
 
     dprintf(3, "enabling shadow ram\n");
@@ -148,7 +148,7 @@ make_bios_writable(void)
 void
 make_bios_readonly(void)
 {
-    if (CONFIG_COREBOOT || usingXen())
+    if (!startedOnQEMU())
         return;
 
     dprintf(3, "locking shadow ram\n");
@@ -161,7 +161,7 @@ make_bios_readonly(void)
 void
 qemu_prep_reset(void)
 {
-    if (CONFIG_COREBOOT)
+    if (!startedOnQEMU())
         return;
     // QEMU doesn't map 0xc0000-0xfffff back to the original rom on a
     // reset, so do that manually before invoking a hard reset.
@@ -169,4 +169,8 @@ qemu_prep_reset(void)
     extern u8 code32flat_start[], code32flat_end[];
     memcpy(code32flat_start, code32flat_start + BIOS_SRC_OFFSET
            , code32flat_end - code32flat_start);
+
+    if (HaveRunPost)
+        // Memcpy failed to reset memory - try to shutdown machine.
+        apm_shutdown();
 }
diff --git a/src/smm.c b/src/smm.c
index c69f0fd..95caf26 100644
--- a/src/smm.c
+++ b/src/smm.c
@@ -10,8 +10,8 @@
 #include "config.h" // CONFIG_*
 #include "ioport.h" // outb
 #include "pci_ids.h" // PCI_VENDOR_ID_INTEL
-#include "xen.h" // usingXen
 #include "dev-q35.h"
+#include "paravirt.h" // startedOnQEMU
 
 ASM32FLAT(
     ".global smm_relocation_start\n"
@@ -184,13 +184,9 @@ static const struct pci_device_id smm_init_tbl[] = {
 void
 smm_setup(void)
 {
-    if (CONFIG_COREBOOT)
-        // SMM only supported on emulators.
+    if (!CONFIG_USE_SMM || !startedOnQEMU())
+        // SMM only supported on QEMU style emulators.
         return;
-    if (!CONFIG_USE_SMM)
-        return;
-    if (usingXen())
-	return;
 
     dprintf(3, "init smm\n");
     pci_find_init_device(smm_init_tbl, NULL);
diff --git a/src/smp.c b/src/smp.c
index 18bb05f..f1dba5f 100644
--- a/src/smp.c
+++ b/src/smp.c
@@ -113,7 +113,7 @@ smp_setup(void)
     u32 val = readl(APIC_SVR);
     writel(APIC_SVR, val | APIC_ENABLED);
 
-    if (! CONFIG_COREBOOT) {
+    if (runningOnQEMU()) {
         /* Set LINT0 as Ext_INT, level triggered */
         writel(APIC_LINT0, 0x8700);
 
@@ -128,7 +128,7 @@ smp_setup(void)
     writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
 
     // Wait for other CPUs to process the SIPI.
-    if (CONFIG_COREBOOT) {
+    if (!runningOnQEMU()) {
         msleep(10);
     } else {
         u8 cmos_smp_count = inb_cmos(CMOS_BIOS_SMP_COUNT);
diff --git a/src/virtio-blk.c b/src/virtio-blk.c
index 194deaf..9aa9d07 100644
--- a/src/virtio-blk.c
+++ b/src/virtio-blk.c
@@ -77,7 +77,7 @@ virtio_blk_op(struct disk_op_s *op, int write)
 int
 process_virtio_blk_op(struct disk_op_s *op)
 {
-    if (! CONFIG_VIRTIO_BLK || CONFIG_COREBOOT)
+    if (!CONFIG_VIRTIO_BLK)
         return 0;
     switch (op->command) {
     case CMD_READ:
@@ -159,7 +159,7 @@ void
 virtio_blk_setup(void)
 {
     ASSERT32FLAT();
-    if (! CONFIG_VIRTIO_BLK || CONFIG_COREBOOT)
+    if (! CONFIG_VIRTIO_BLK)
         return;
 
     dprintf(3, "init virtio-blk\n");
diff --git a/src/virtio-scsi.c b/src/virtio-scsi.c
index 4bbff8f..879ddfb 100644
--- a/src/virtio-scsi.c
+++ b/src/virtio-scsi.c
@@ -166,7 +166,7 @@ void
 virtio_scsi_setup(void)
 {
     ASSERT32FLAT();
-    if (! CONFIG_VIRTIO_SCSI || CONFIG_COREBOOT)
+    if (! CONFIG_VIRTIO_SCSI)
         return;
 
     dprintf(3, "init virtio-scsi\n");
diff --git a/src/xen.c b/src/xen.c
index 5122a3c..4d10b96 100644
--- a/src/xen.c
+++ b/src/xen.c
@@ -125,7 +125,7 @@ void xen_hypercall_setup(void)
     xen_extraversion_t extraversion;
     unsigned long i;
 
-    if (!usingXen())
+    if (!runningOnXen())
         return;
 
     cpuid(xen_cpuid_base + 2, &eax, &ebx, &ecx, &edx);
diff --git a/src/xen.h b/src/xen.h
index 2be2453..fb52702 100644
--- a/src/xen.h
+++ b/src/xen.h
@@ -10,12 +10,6 @@ void xen_preinit(void);
 void xen_hypercall_setup(void);
 void xen_biostable_setup(void);
 
-static inline int usingXen(void) {
-    if (!CONFIG_XEN)
-        return 0;
-    return (xen_cpuid_base != 0);
-}
-
 extern unsigned long xen_hypercall_page;
 
 #define _hypercall0(type, name)                                         \
-- 
1.7.11.7




More information about the SeaBIOS mailing list