Avoid -fwhole-program on broken gcc instead of stopping build.
[seabios.git] / src / boot.c
index fc81ed60d24f23f9209e9f3831f1bb728daa0b66..26427ba51f8e46e6988eb3a486449d9ee861d219 100644 (file)
@@ -17,7 +17,7 @@ struct ipl_s IPL;
 
 
 /****************************************************************
- * IPL handlers
+ * IPL and BCV handlers
  ****************************************************************/
 
 void
@@ -30,7 +30,7 @@ boot_setup()
     memset(&IPL, 0, sizeof(IPL));
 
     // Floppy drive
-    struct ipl_entry_s *ie = &IPL.table[0];
+    struct ipl_entry_s *ie = &IPL.bev[0];
     ie->type = IPL_TYPE_FLOPPY;
     ie->description = "Floppy";
     ie++;
@@ -47,11 +47,17 @@ boot_setup()
         ie++;
     }
 
-    IPL.count = ie - IPL.table;
+    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 = 0x00000231;
+        IPL.bootorder = 0x87654231;
         IPL.checkfloppysig = 1;
     } else {
         // On emulators, get boot order from nvram.
@@ -66,30 +72,129 @@ boot_setup()
 void
 add_bev(u16 seg, u16 bev, u16 desc)
 {
-    // Found a device that thinks it can boot the system.  Record
-    // its BEV and product name string.
-
     if (! CONFIG_BOOT)
         return;
-
-    if (IPL.count >= ARRAY_SIZE(IPL.table))
+    if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
         return;
 
-    struct ipl_entry_s *ie = &IPL.table[IPL.count];
+    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)
-        ie->description = MAKE_FLATPTR(seg, desc);
+        d = MAKE_FLATPTR(seg, desc);
+    ie->description = d;
+}
+
+// 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;
 
-    IPL.count++;
+    struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
+    ie->type = IPL_TYPE_BEV;
+    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 ata harddrive
+void
+add_bcv_hd(int driveid, const char *desc)
+{
+    if (! CONFIG_BOOT)
+        return;
+    if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
+        return;
+
+    struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
+    ie->type = IPL_TYPE_HARDDISK;
+    ie->vector = driveid;
+    ie->description = desc;
 }
 
 
 /****************************************************************
- * Boot menu
+ * Boot menu and BCV execution
  ****************************************************************/
 
-void
+// 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;
+}
+
+// Show floppy menu item - but only if there exists a floppy drive.
+static int
+menu_show_floppy(struct ipl_entry_s *ie, int menupos)
+{
+    if (!FloppyCount)
+        return 0;
+    return menu_show_default(ie, menupos);
+}
+
+// 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 IPL_TYPE_HARDDISK:
+            printf("%d. ata%d-%d %s\n", menupos + i
+                   , ie->vector / 2, ie->vector % 2, ie->description);
+            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 < ATA.cdcount; i++) {
+        int driveid = ATA.idmap[1][i];
+        printf("%d. CD-Rom [ata%d-%d %s]\n", menupos + i
+               , driveid / 2, driveid % 2, ATA.devices[driveid].model);
+    }
+    return ATA.cdcount;
+}
+
+// Show coreboot-fs menu item.
+static int
+menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
+{
+    int count = 0;
+    for (;;) {
+        const char *filename = cbfs_findNprefix("img/", count);
+        if (!filename)
+            break;
+        printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
+        count++;
+        if (count > 8)
+            break;
+    }
+    return count;
+}
+
+// Show IPL option menu.
+static void
 interactive_bootmenu()
 {
     if (! CONFIG_BOOTMENU)
@@ -100,7 +205,7 @@ interactive_bootmenu()
 
     printf("Press F12 for boot menu.\n\n");
 
-    int scan_code = get_keystroke(2500);
+    int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
     if (scan_code != 0x86)
         /* not F12 */
         return;
@@ -110,13 +215,31 @@ interactive_bootmenu()
 
     printf("Select boot device:\n\n");
 
-    int count = IPL.count;
+    int subcount[ARRAY_SIZE(IPL.bev)];
+    int menupos = 1;
     int i;
-    for (i = 0; i < count; i++) {
-        struct ipl_entry_s *ie = &IPL.table[i];
-        char desc[33];
-        printf("%d. %s\n", i+1
-               , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
+    for (i = 0; i < IPL.bevcount; i++) {
+        struct ipl_entry_s *ie = &IPL.bev[i];
+        int sc = 1;
+        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;
     }
 
     for (;;) {
@@ -124,26 +247,69 @@ interactive_bootmenu()
         if (scan_code == 0x01)
             // ESC
             break;
-        if (scan_code >= 0 && scan_code <= count + 1) {
-            // Add user choice to the boot order.
-            u16 choice = scan_code - 1;
-            u32 bootorder = IPL.bootorder;
-            IPL.bootorder = (bootorder << 4) | choice;
-            break;
+        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;
+
+        // Add user choice to the boot order.
+        IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
+        break;
     }
     printf("\n");
 }
 
+// Run the specified bcv.
+static void
+run_bcv(struct ipl_entry_s *ie)
+{
+    switch (ie->type) {
+    case IPL_TYPE_HARDDISK:
+        map_drive(ie->vector);
+        break;
+    case IPL_TYPE_BEV:
+        call_bcv(ie->vector >> 16, ie->vector & 0xffff);
+        break;
+    }
+}
+
+// Prepare for boot - show menu and run bcvs.
+void
+boot_prep()
+{
+    if (! CONFIG_BOOT)
+        return;
+
+    // Allow user to modify BCV/IPL order.
+    interactive_bootmenu();
+
+    // Run BCVs
+    int 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 %x:%x\n", bootseg, bootip);
+    dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
 
     struct bregs br;
     memset(&br, 0, sizeof(br));
@@ -193,11 +359,11 @@ boot_disk(u8 bootdrv, int checksig)
 
 // Boot from a CD-ROM
 static void
-boot_cdrom()
+boot_cdrom(struct ipl_entry_s *ie)
 {
     if (! CONFIG_CDROM_BOOT)
         return;
-    int status = cdrom_boot();
+    int status = cdrom_boot(ie->subchoice);
     if (status) {
         printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
         return;
@@ -213,6 +379,18 @@ boot_cdrom()
     call_boot_entry(bootseg, bootip, bootdrv);
 }
 
+// Boot from a CD-ROM
+static void
+boot_cbfs(struct ipl_entry_s *ie)
+{
+    if (! CONFIG_COREBOOT_FLASH)
+        return;
+    const char *filename = cbfs_findNprefix("img/", ie->subchoice);
+    if (! filename)
+        return;
+    cbfs_run_payload(filename);
+}
+
 static void
 do_boot(u16 seq_nr)
 {
@@ -223,20 +401,19 @@ do_boot(u16 seq_nr)
     bootdev >>= 4 * seq_nr;
     bootdev &= 0xf;
 
-    if (bootdev == 0)
-        panic("No bootable device.\n");
-
     /* Translate bootdev to an IPL table offset by subtracting 1 */
     bootdev -= 1;
 
-    if (bootdev >= IPL.count) {
-        dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
-        goto fail;
+    if (bootdev >= IPL.bevcount) {
+        printf("No bootable device.\n");
+        // Loop with irqs enabled - this allows ctrl+alt+delete to work.
+        for (;;)
+            usleep(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.table[bootdev];
+    struct ipl_entry_s *ie = &IPL.bev[bootdev];
     char desc[33];
     printf("Booting from %s...\n"
            , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
@@ -249,7 +426,10 @@ do_boot(u16 seq_nr)
         boot_disk(0x80, 1);
         break;
     case IPL_TYPE_CDROM:
-        boot_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);
@@ -258,7 +438,6 @@ do_boot(u16 seq_nr)
 
     // Boot failed: invoke the boot recovery function
     struct bregs br;
-fail:
     memset(&br, 0, sizeof(br));
     call16_int(0x18, &br);
 }
@@ -284,7 +463,3 @@ handle_19()
     SET_EBDA(boot_sequence, 0);
     do_boot(0);
 }
-
-// Ughh - some older gcc compilers have a bug which causes VISIBLE32
-// functions to not be exported as global variables.
-asm(".global handle_18, handle_19");