1 // Code to load disk image and start system boot.
3 // Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "util.h" // dprintf
9 #include "biosvar.h" // GET_EBDA
10 #include "config.h" // CONFIG_*
11 #include "disk.h" // cdrom_boot
12 #include "bregs.h" // struct bregs
13 #include "boot.h" // struct ipl_s
14 #include "cmos.h" // inb_cmos
20 /****************************************************************
21 * Boot priority ordering
22 ****************************************************************/
27 char *f = romfile_loadfile("bootorder", NULL);
32 IPL.fw_bootorder_count = 1;
35 IPL.fw_bootorder_count++;
38 IPL.fw_bootorder = malloc_tmphigh(IPL.fw_bootorder_count*sizeof(char*));
39 if (!IPL.fw_bootorder) {
45 dprintf(3, "boot order:\n");
48 IPL.fw_bootorder[i] = f;
53 dprintf(3, "%d: %s\n", i, IPL.fw_bootorder[i]);
59 int bootprio_find_pci_device(int bdf)
64 int bootprio_find_ata_device(int bdf, int chanid, int slave)
69 int bootprio_find_fdc_device(int bfd, int port, int fdid)
74 int bootprio_find_pci_rom(int bdf, int instance)
79 int bootprio_find_named_rom(const char *name, int instance)
85 /****************************************************************
87 ****************************************************************/
89 static int CheckFloppySig = 1;
91 #define DEFAULT_PRIO 9999
93 static int DefaultFloppyPrio = 101;
94 static int DefaultCDPrio = 102;
95 static int DefaultHDPrio = 103;
96 static int DefaultBEVPrio = 104;
104 SET_EBDA(boot_sequence, 0xffff);
106 if (!CONFIG_COREBOOT) {
107 // On emulators, get boot order from nvram.
108 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
110 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
111 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
112 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
113 = DefaultBEVPrio = DEFAULT_PRIO;
115 for (i=101; i<104; i++) {
116 u32 val = bootorder & 0x0f;
119 case 1: DefaultFloppyPrio = i; break;
120 case 2: DefaultHDPrio = i; break;
121 case 3: DefaultCDPrio = i; break;
122 case 4: DefaultBEVPrio = i; break;
131 /****************************************************************
133 ****************************************************************/
139 struct segoff_s vector;
140 struct drive_s *drive;
143 const char *description;
144 struct bootentry_s *next;
147 static struct bootentry_s *BootList;
150 bootentry_add(int type, int prio, u32 data, const char *desc)
154 struct bootentry_s *be = malloc_tmp(sizeof(*be));
162 be->description = desc;
164 // Add entry in sorted order.
165 struct bootentry_s **pprev;
166 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
167 struct bootentry_s *pos = *pprev;
168 if (be->priority < pos->priority)
170 if (be->priority > pos->priority)
172 if (be->type < pos->type)
174 if (be->type > pos->type)
176 if (be->type <= IPL_TYPE_CDROM
177 && (be->drive->type < pos->drive->type
178 || (be->drive->type == pos->drive->type
179 && be->drive->cntl_id < pos->drive->cntl_id)))
186 // Return the given priority if it's set - defaultprio otherwise.
187 static inline int defPrio(int priority, int defaultprio) {
188 return (priority < 0) ? defaultprio : priority;
191 // Add a BEV vector for a given pnp compatible option rom.
193 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
195 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
196 , SEGOFF(seg, bev).segoff
197 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
198 DefaultBEVPrio = DEFAULT_PRIO;
201 // Add a bcv entry for an expansion card harddrive or legacy option rom
203 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
205 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
206 , SEGOFF(seg, ip).segoff
207 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
211 boot_add_floppy(struct drive_s *drive_g, int prio)
213 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
214 , (u32)drive_g, drive_g->desc);
218 boot_add_hd(struct drive_s *drive_g, int prio)
220 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
221 , (u32)drive_g, drive_g->desc);
225 boot_add_cd(struct drive_s *drive_g, int prio)
227 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
228 , (u32)drive_g, drive_g->desc);
231 // Add a CBFS payload entry
233 boot_add_cbfs(void *data, const char *desc, int prio)
235 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
239 /****************************************************************
240 * Boot menu and BCV execution
241 ****************************************************************/
243 // Show IPL option menu.
245 interactive_bootmenu(void)
247 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
250 while (get_keystroke(0) >= 0)
253 printf("Press F12 for boot menu.\n\n");
256 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
257 disable_bootsplash();
258 if (scan_code != 0x86)
262 while (get_keystroke(0) >= 0)
265 printf("Select boot device:\n\n");
269 struct bootentry_s *pos = BootList;
274 printf("%d. %s\n", maxmenu
275 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
281 scan_code = get_keystroke(1000);
282 if (scan_code >= 1 && scan_code <= maxmenu+1)
286 if (scan_code == 0x01)
290 // Find entry and make top priority.
291 int choice = scan_code - 1;
292 struct bootentry_s **pprev = &BootList;
294 pprev = &(*pprev)->next;
297 pos->next = BootList;
302 static int HaveHDBoot, HaveFDBoot;
305 add_bev(int type, u32 vector)
307 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
309 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
311 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
313 struct ipl_entry_s *bev = &IPL.bev[IPL.bevcount++];
315 bev->vector = vector;
318 // Prepare for boot - show menu and run bcvs.
327 // XXX - show available drives?
329 // Allow user to modify BCV/IPL order.
330 interactive_bootmenu();
333 // Map drives and populate BEV list
334 struct bootentry_s *pos = BootList;
338 call_bcv(pos->vector.seg, pos->vector.offset);
339 add_bev(IPL_TYPE_HARDDISK, 0);
341 case IPL_TYPE_FLOPPY:
342 map_floppy_drive(pos->drive);
343 add_bev(IPL_TYPE_FLOPPY, 0);
345 case IPL_TYPE_HARDDISK:
346 map_hd_drive(pos->drive);
347 add_bev(IPL_TYPE_HARDDISK, 0);
350 map_cd_drive(pos->drive);
353 add_bev(pos->type, pos->data);
359 // If nothing added a floppy/hd boot - add it manually.
360 add_bev(IPL_TYPE_FLOPPY, 0);
361 add_bev(IPL_TYPE_HARDDISK, 0);
365 /****************************************************************
366 * Boot code (int 18/19)
367 ****************************************************************/
369 // Jump to a bootup entry point.
371 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
373 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
375 memset(&br, 0, sizeof(br));
377 br.code = SEGOFF(bootseg, bootip);
378 // Set the magic number in ax and the boot drive in dl.
384 // Boot from a disk (either floppy or harddrive)
386 boot_disk(u8 bootdrv, int checksig)
388 u16 bootseg = 0x07c0;
392 memset(&br, 0, sizeof(br));
399 call16_int(0x13, &br);
401 if (br.flags & F_CF) {
402 printf("Boot failed: could not read the boot disk\n\n");
407 struct mbr_s *mbr = (void*)0;
408 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
409 printf("Boot failed: not a bootable disk\n\n");
414 /* Canonicalize bootseg:bootip */
415 u16 bootip = (bootseg & 0x0fff) << 4;
418 call_boot_entry(bootseg, bootip, bootdrv);
421 // Boot from a CD-ROM
423 boot_cdrom(struct ipl_entry_s *ie)
425 if (! CONFIG_CDROM_BOOT)
430 printf("Booting from DVD/CD...\n");
432 struct drive_s *drive_g = (void*)ie->vector;
433 int status = cdrom_boot(drive_g);
435 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
439 u16 ebda_seg = get_ebda_seg();
440 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
441 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
442 /* Canonicalize bootseg:bootip */
443 u16 bootip = (bootseg & 0x0fff) << 4;
446 call_boot_entry(bootseg, bootip, bootdrv);
449 // Boot from a CBFS payload
451 boot_cbfs(struct ipl_entry_s *ie)
453 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
455 printf("Booting from CBFS...\n");
456 cbfs_run_payload((void*)ie->vector);
463 panic("Boot support not compiled in.\n");
465 if (seq_nr >= IPL.bevcount) {
466 printf("No bootable device.\n");
467 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
472 // Boot the given BEV type.
473 struct ipl_entry_s *ie = &IPL.bev[seq_nr];
475 case IPL_TYPE_FLOPPY:
476 printf("Booting from Floppy...\n");
477 boot_disk(0x00, CheckFloppySig);
479 case IPL_TYPE_HARDDISK:
480 printf("Booting from Hard Disk...\n");
490 printf("Booting from ROM...\n");
491 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
495 // Boot failed: invoke the boot recovery function
497 memset(&br, 0, sizeof(br));
499 call16_int(0x18, &br);
502 // Boot Failure recovery: try the next device.
506 debug_serial_setup();
507 debug_enter(NULL, DEBUG_HDL_18);
508 u16 ebda_seg = get_ebda_seg();
509 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
510 SET_EBDA2(ebda_seg, boot_sequence, seq);
514 // INT 19h Boot Load Service Entry Point
518 debug_serial_setup();
519 debug_enter(NULL, DEBUG_HDL_19);
520 SET_EBDA(boot_sequence, 0);