Add simple cooperative threading scheme to allow parallel hw init.
[seabios.git] / src / boot.c
index c1f88469d570ef56e31419a257b3def77734adc8..d80eae491c9c5ea16d9644d6d11d8d2f6ca1c052 100644 (file)
-// 16bit code to load disk image and start system boot.
+// Code to load disk image and start system boot.
 //
 // 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 "util.h" // irq_enable
-#include "biosvar.h" // struct bregs
+#include "util.h" // dprintf
+#include "biosvar.h" // GET_EBDA
 #include "config.h" // CONFIG_*
-#include "cmos.h" // inb_cmos
-#include "ata.h" // ata_detect
 #include "disk.h" // cdrom_boot
+#include "bregs.h" // struct bregs
+#include "boot.h" // struct ipl_s
+#include "cmos.h" // inb_cmos
+#include "paravirt.h"
 
-// We need a copy of this string, but we are not actually a PnP BIOS,
-// so make sure it is *not* aligned, so OSes will not see it if they
-// scan.
-char pnp_string[] VISIBLE16 __attribute__((aligned (2))) = " $PnP";
+struct ipl_s IPL;
 
-//--------------------------------------------------------------------------
-// print_boot_device
-//   displays the boot device
-//--------------------------------------------------------------------------
 
-static const char drivetypes[][10]={
-    "", "Floppy","Hard Disk","CD-Rom", "Network"
-};
+/****************************************************************
+ * IPL and BCV handlers
+ ****************************************************************/
 
-static void
-print_boot_device(u16 type)
+void
+boot_setup()
 {
-    /* NIC appears as type 0x80 */
-    if (type == IPL_TYPE_BEV)
-        type = 0x4;
-    if (type == 0 || type > 0x4)
-        BX_PANIC("Bad drive type\n");
-    printf("Booting from %s...\n", drivetypes[type]);
+    if (! CONFIG_BOOT)
+        return;
+    dprintf(3, "init boot device ordering\n");
+
+    memset(&IPL, 0, sizeof(IPL));
+
+    // Floppy drive
+    struct ipl_entry_s *ie = &IPL.bev[0];
+    ie->type = IPL_TYPE_FLOPPY;
+    ie->description = "Floppy";
+    ie++;
+
+    // First HDD
+    ie->type = IPL_TYPE_HARDDISK;
+    ie->description = "Hard Disk";
+    ie++;
+
+    // CDROM
+    if (CONFIG_CDROM_BOOT) {
+        ie->type = IPL_TYPE_CDROM;
+        ie->description = "CD-Rom";
+        ie++;
+    }
 
-    // XXX - latest cvs has BEV description
+    if (CONFIG_COREBOOT_FLASH) {
+        ie->type = IPL_TYPE_CBFS;
+        ie->description = "CBFS";
+        ie++;
+    }
+
+    IPL.bevcount = ie - IPL.bev;
+    SET_EBDA(boot_sequence, 0xffff);
+    if (CONFIG_COREBOOT) {
+        // XXX - hardcode defaults for coreboot.
+        IPL.bootorder = 0x87654231;
+        IPL.checkfloppysig = 1;
+    } else {
+        // On emulators, get boot order from nvram.
+        IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
+                         | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
+        if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
+            IPL.checkfloppysig = 1;
+    }
 }
 
-//--------------------------------------------------------------------------
-// print_boot_failure
-//   displays the reason why boot failed
-//--------------------------------------------------------------------------
-static void
-print_boot_failure(u16 type, u8 reason)
+// Add a BEV vector for a given pnp compatible option rom.
+void
+add_bev(u16 seg, u16 bev, u16 desc)
 {
-    if (type == 0 || type > 0x3)
-        BX_PANIC("Bad drive type\n");
+    if (! CONFIG_BOOT)
+        return;
+    if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
+        return;
 
-    printf("Boot failed");
-    if (type < 4) {
-        /* Report the reason too */
-        if (reason==0)
-            printf(": not a bootable disk");
-        else
-            printf(": could not read the boot disk");
-    }
-    printf("\n\n");
+    struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
+    ie->type = IPL_TYPE_BEV;
+    ie->vector = (seg << 16) | bev;
+    const char *d = "Unknown";
+    if (desc)
+        d = MAKE_FLATPTR(seg, desc);
+    ie->description = d;
 }
 
-static void
-try_boot(u16 seq_nr)
+// Add a bcv entry for an expansion card harddrive or legacy option rom
+void
+add_bcv(u16 seg, u16 ip, u16 desc)
+{
+    if (! CONFIG_BOOT)
+        return;
+    if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
+        return;
+
+    struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
+    ie->type = BCV_TYPE_EXTERNAL;
+    ie->vector = (seg << 16) | ip;
+    const char *d = "Legacy option rom";
+    if (desc)
+        d = MAKE_FLATPTR(seg, desc);
+    ie->description = d;
+}
+
+// Add a bcv entry for an internal harddrive
+void
+add_bcv_internal(struct drive_s *drive_g)
 {
-    irq_enable();
+    if (! CONFIG_BOOT)
+        return;
+    if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
+        return;
 
-    SET_EBDA(ipl.sequence, seq_nr);
+    struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
+    if (CONFIG_THREADS) {
+        // Add to bcv list with assured drive order.
+        struct ipl_entry_s *end = ie;
+        for (;;) {
+            struct ipl_entry_s *prev = ie - 1;
+            if (prev < IPL.bcv || prev->type != BCV_TYPE_INTERNAL)
+                break;
+            struct drive_s *prevdrive = (void*)prev->vector;
+            if (prevdrive->type < drive_g->type
+                || (prevdrive->type == drive_g->type
+                    && prevdrive->cntl_id < drive_g->cntl_id))
+                break;
+            ie--;
+        }
+        if (ie != end)
+            memmove(ie+1, ie, (void*)end-(void*)ie);
+    }
+    ie->type = BCV_TYPE_INTERNAL;
+    ie->vector = (u32)drive_g;
+    ie->description = "";
+}
 
-    u16 bootseg;
-    u8 bootdrv = 0;
-    u16 bootdev, bootip;
 
-    bootdev = inb_cmos(CMOS_BIOS_BOOTFLAG2);
-    bootdev |= ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4);
-    bootdev >>= 4 * seq_nr;
-    bootdev &= 0xf;
-    if (bootdev == 0)
-        BX_PANIC("No bootable device.\n");
+/****************************************************************
+ * Boot menu and BCV execution
+ ****************************************************************/
 
-    /* Translate from CMOS runes to an IPL table offset by subtracting 1 */
-    bootdev -= 1;
+// Show a generic menu item
+static int
+menu_show_default(struct ipl_entry_s *ie, int menupos)
+{
+    char desc[33];
+    printf("%d. %s\n", menupos
+           , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
+    return 1;
+}
 
-    if (bootdev >= GET_EBDA(ipl.count)) {
-        BX_INFO("Invalid boot device (0x%x)\n", bootdev);
-        return;
+// Show floppy menu item - but only if there exists a floppy drive.
+static int
+menu_show_floppy(struct ipl_entry_s *ie, int menupos)
+{
+    int i;
+    for (i = 0; i < Drives.floppycount; i++) {
+        struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, i);
+        printf("%d. Floppy [", menupos + i);
+        describe_drive(drive_g);
+        printf("]\n");
     }
-    u16 type = GET_EBDA(ipl.table[bootdev].type);
+    return Drives.floppycount;
+}
 
-    /* Do the loading, and set up vector as a far pointer to the boot
-     * address, and bootdrv as the boot drive */
-    print_boot_device(type);
-
-    struct bregs cr;
-    switch(type) {
-    case IPL_TYPE_FLOPPY: /* FDD */
-    case IPL_TYPE_HARDDISK: /* HDD */
-
-        bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
-        bootseg = 0x07c0;
-
-        // Read sector
-        memset(&cr, 0, sizeof(cr));
-        cr.dl = bootdrv;
-        cr.es = bootseg;
-        cr.ah = 2;
-        cr.al = 1;
-        cr.cl = 1;
-        call16_int(0x13, &cr);
-
-        if (cr.flags & F_CF) {
-            print_boot_failure(type, 1);
-            return;
+// Show menu items from BCV list.
+static int
+menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
+{
+    int i;
+    for (i = 0; i < IPL.bcvcount; i++) {
+        struct ipl_entry_s *ie = &IPL.bcv[i];
+        switch (ie->type) {
+        case BCV_TYPE_INTERNAL:
+            printf("%d. ", menupos + i);
+            describe_drive((void*)ie->vector);
+            printf("\n");
+            break;
+        default:
+            menu_show_default(ie, menupos+i);
+            break;
         }
+    }
+    return IPL.bcvcount;
+}
+
+// Show cdrom menu item - but only if there exists a cdrom drive.
+static int
+menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
+{
+    int i;
+    for (i = 0; i < Drives.cdcount; i++) {
+        struct drive_s *drive_g = getDrive(EXTTYPE_CD, i);
+        printf("%d. CD-Rom [", menupos + i);
+        describe_drive(drive_g);
+        printf("]\n");
+    }
+    return Drives.cdcount;
+}
+
+// Show coreboot-fs menu item.
+static int
+menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
+{
+    int count = 0;
+    struct cbfs_file *file = NULL;
+    for (;;) {
+        file = cbfs_findprefix("img/", file);
+        if (!file)
+            break;
+        const char *filename = cbfs_filename(file);
+        printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
+        count++;
+        if (count > 8)
+            break;
+    }
+    return count;
+}
 
-        /* Always check the signature on a HDD boot sector; on FDD,
-         * only do the check if the CMOS doesn't tell us to skip it */
-        if ((type != IPL_TYPE_FLOPPY)
-            || !((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0x01))) {
-            if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
-                print_boot_failure(type, 0);
-                return;
-            }
+// Show IPL option menu.
+static void
+interactive_bootmenu()
+{
+    if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
+        return;
+
+    while (get_keystroke(0) >= 0)
+        ;
+
+    printf("Press F12 for boot menu.\n\n");
+
+    int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
+    if (scan_code != 0x86)
+        /* not F12 */
+        return;
+
+    while (get_keystroke(0) >= 0)
+        ;
+
+    printf("Select boot device:\n\n");
+
+    int subcount[ARRAY_SIZE(IPL.bev)];
+    int menupos = 1;
+    int i;
+    for (i = 0; i < IPL.bevcount; i++) {
+        struct ipl_entry_s *ie = &IPL.bev[i];
+        int sc;
+        switch (ie->type) {
+        case IPL_TYPE_FLOPPY:
+            sc = menu_show_floppy(ie, menupos);
+            break;
+        case IPL_TYPE_HARDDISK:
+            sc = menu_show_harddisk(ie, menupos);
+            break;
+        case IPL_TYPE_CDROM:
+            sc = menu_show_cdrom(ie, menupos);
+            break;
+        case IPL_TYPE_CBFS:
+            sc = menu_show_cbfs(ie, menupos);
+            break;
+        default:
+            sc = menu_show_default(ie, menupos);
+            break;
         }
+        subcount[i] = sc;
+        menupos += sc;
+    }
 
-        /* Canonicalize bootseg:bootip */
-        bootip = (bootseg & 0x0fff) << 4;
-        bootseg &= 0xf000;
-        break;
-    case IPL_TYPE_CDROM: {
-        /* CD-ROM */
-        if (! CONFIG_CDROM_BOOT)
+    for (;;) {
+        scan_code = get_keystroke(1000);
+        if (scan_code == 0x01)
+            // ESC
             break;
-        u16 status = cdrom_boot();
-        if (status) {
-            printf("CDROM boot failure code : %04x\n", status);
-            print_boot_failure(type, 1);
-            return;
+        if (scan_code < 1 || scan_code > menupos)
+            continue;
+        int choice = scan_code - 1;
+
+        // Find out which IPL this was for.
+        int bev = 0;
+        while (choice > subcount[bev]) {
+            choice -= subcount[bev];
+            bev++;
         }
+        IPL.bev[bev].subchoice = choice-1;
 
-        bootdrv = GET_EBDA(cdemu.emulated_drive);
-        bootseg = GET_EBDA(cdemu.load_segment);
-        /* Canonicalize bootseg:bootip */
-        bootip = (bootseg & 0x0fff) << 4;
-        bootseg &= 0xf000;
+        // Add user choice to the boot order.
+        IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
         break;
     }
-    case IPL_TYPE_BEV: {
-        /* Expansion ROM with a Bootstrap Entry Vector (a far
-         * pointer) */
-        u32 vector = GET_EBDA(ipl.table[bootdev].vector);
-        bootseg = vector >> 16;
-        bootip = vector & 0xffff;
+    printf("\n");
+}
+
+// Run the specified bcv.
+static void
+run_bcv(struct ipl_entry_s *ie)
+{
+    switch (ie->type) {
+    case BCV_TYPE_INTERNAL:
+        map_hd_drive((void*)ie->vector);
+        break;
+    case BCV_TYPE_EXTERNAL:
+        call_bcv(ie->vector >> 16, ie->vector & 0xffff);
         break;
     }
-    default:
+}
+
+// Prepare for boot - show menu and run bcvs.
+void
+boot_prep()
+{
+    if (! CONFIG_BOOT)
         return;
-    }
 
-    /* Debugging info */
-    BX_INFO("Booting from %x:%x\n", bootseg, bootip);
+    // XXX - show available drives?
+
+    // Allow user to modify BCV/IPL order.
+    interactive_bootmenu();
+
+    // Setup floppy boot order
+    int override = IPL.bev[0].subchoice;
+    int tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
+    Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
+    Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
+
+    // Run BCVs
+    override = IPL.bev[1].subchoice;
+    if (override < IPL.bcvcount)
+        run_bcv(&IPL.bcv[override]);
+    int i;
+    for (i=0; i<IPL.bcvcount; i++)
+        if (i != override)
+            run_bcv(&IPL.bcv[i]);
+}
+
+
+/****************************************************************
+ * Boot code (int 18/19)
+ ****************************************************************/
+
+// Jump to a bootup entry point.
+static void
+call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
+{
+    dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
 
-    memset(&cr, 0, sizeof(cr));
-    cr.ip = bootip;
-    cr.cs = bootseg;
+    struct bregs br;
+    memset(&br, 0, sizeof(br));
+    br.flags = F_IF;
+    br.code = SEGOFF(bootseg, bootip);
     // Set the magic number in ax and the boot drive in dl.
-    cr.dl = bootdrv;
-    cr.ax = 0xaa55;
-    call16(&cr);
+    br.dl = bootdrv;
+    br.ax = 0xaa55;
+    call16(&br);
+}
+
+// Boot from a disk (either floppy or harddrive)
+static void
+boot_disk(u8 bootdrv, int checksig)
+{
+    u16 bootseg = 0x07c0;
+
+    // Read sector
+    struct bregs br;
+    memset(&br, 0, sizeof(br));
+    br.flags = F_IF;
+    br.dl = bootdrv;
+    br.es = bootseg;
+    br.ah = 2;
+    br.al = 1;
+    br.cl = 1;
+    call16_int(0x13, &br);
+
+    if (br.flags & F_CF) {
+        printf("Boot failed: could not read the boot disk\n\n");
+        return;
+    }
+
+    if (checksig) {
+        struct mbr_s *mbr = (void*)0;
+        if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
+            printf("Boot failed: not a bootable disk\n\n");
+            return;
+        }
+    }
+
+    /* Canonicalize bootseg:bootip */
+    u16 bootip = (bootseg & 0x0fff) << 4;
+    bootseg &= 0xf000;
+
+    call_boot_entry(bootseg, bootip, bootdrv);
+}
+
+// Boot from a CD-ROM
+static void
+boot_cdrom(struct ipl_entry_s *ie)
+{
+    if (! CONFIG_CDROM_BOOT)
+        return;
+    int status = cdrom_boot(ie->subchoice);
+    if (status) {
+        printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
+        return;
+    }
+
+    u16 ebda_seg = get_ebda_seg();
+    u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
+    u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
+    /* Canonicalize bootseg:bootip */
+    u16 bootip = (bootseg & 0x0fff) << 4;
+    bootseg &= 0xf000;
+
+    call_boot_entry(bootseg, bootip, bootdrv);
+}
+
+// Boot from a CBFS payload
+static void
+boot_cbfs(struct ipl_entry_s *ie)
+{
+    if (! CONFIG_COREBOOT_FLASH)
+        return;
+    int count = ie->subchoice;
+    struct cbfs_file *file;
+    for (;;) {
+        file = cbfs_findprefix("img/", file);
+        if (!file)
+            return;
+        if (count--)
+            continue;
+        cbfs_run_payload(file);
+    }
 }
 
 static void
 do_boot(u16 seq_nr)
 {
-    try_boot(seq_nr);
+    if (! CONFIG_BOOT)
+        panic("Boot support not compiled in.\n");
+
+    u32 bootdev = IPL.bootorder;
+    bootdev >>= 4 * seq_nr;
+    bootdev &= 0xf;
+
+    /* Translate bootdev to an IPL table offset by subtracting 1 */
+    bootdev -= 1;
+
+    if (bootdev >= IPL.bevcount) {
+        printf("No bootable device.\n");
+        // Loop with irqs enabled - this allows ctrl+alt+delete to work.
+        for (;;)
+            biosusleep(1000000);
+    }
+
+    /* Do the loading, and set up vector as a far pointer to the boot
+     * address, and bootdrv as the boot drive */
+    struct ipl_entry_s *ie = &IPL.bev[bootdev];
+    char desc[33];
+    printf("Booting from %s...\n"
+           , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
+
+    switch(ie->type) {
+    case IPL_TYPE_FLOPPY:
+        boot_disk(0x00, IPL.checkfloppysig);
+        break;
+    case IPL_TYPE_HARDDISK:
+        boot_disk(0x80, 1);
+        break;
+    case IPL_TYPE_CDROM:
+        boot_cdrom(ie);
+        break;
+    case IPL_TYPE_CBFS:
+        boot_cbfs(ie);
+        break;
+    case IPL_TYPE_BEV:
+        call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
+        break;
+    }
 
     // Boot failed: invoke the boot recovery function
     struct bregs br;
     memset(&br, 0, sizeof(br));
+    br.flags = F_IF;
     call16_int(0x18, &br);
 }
 
 // Boot Failure recovery: try the next device.
-void VISIBLE16
+void VISIBLE32
 handle_18()
 {
-    debug_enter(NULL);
-    u16 seq = GET_EBDA(ipl.sequence) + 1;
+    debug_serial_setup();
+    debug_enter(NULL, DEBUG_HDL_18);
+    u16 ebda_seg = get_ebda_seg();
+    u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
+    SET_EBDA2(ebda_seg, boot_sequence, seq);
     do_boot(seq);
 }
 
 // INT 19h Boot Load Service Entry Point
-void VISIBLE16
+void VISIBLE32
 handle_19()
 {
-    debug_enter(NULL);
+    debug_serial_setup();
+    debug_enter(NULL, DEBUG_HDL_19);
+    SET_EBDA(boot_sequence, 0);
     do_boot(0);
 }