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;
146 static struct bootentry_s *BootList;
148 #define IPL_TYPE_FLOPPY 0x01
149 #define IPL_TYPE_HARDDISK 0x02
150 #define IPL_TYPE_CDROM 0x03
151 #define IPL_TYPE_CBFS 0x20
152 #define IPL_TYPE_BEV 0x80
153 #define IPL_TYPE_BCV 0x81
156 bootentry_add(int type, int prio, u32 data, const char *desc)
160 struct bootentry_s *be = malloc_tmp(sizeof(*be));
168 be->description = desc;
170 // Add entry in sorted order.
171 struct bootentry_s **pprev;
172 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
173 struct bootentry_s *pos = *pprev;
174 if (be->priority < pos->priority)
176 if (be->priority > pos->priority)
178 if (be->type < pos->type)
180 if (be->type > pos->type)
182 if (be->type <= IPL_TYPE_CDROM
183 && (be->drive->type < pos->drive->type
184 || (be->drive->type == pos->drive->type
185 && be->drive->cntl_id < pos->drive->cntl_id)))
192 // Return the given priority if it's set - defaultprio otherwise.
193 static inline int defPrio(int priority, int defaultprio) {
194 return (priority < 0) ? defaultprio : priority;
197 // Add a BEV vector for a given pnp compatible option rom.
199 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
201 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
202 , SEGOFF(seg, bev).segoff
203 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
204 DefaultBEVPrio = DEFAULT_PRIO;
207 // Add a bcv entry for an expansion card harddrive or legacy option rom
209 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
211 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
212 , SEGOFF(seg, ip).segoff
213 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
217 boot_add_floppy(struct drive_s *drive_g, int prio)
219 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
220 , (u32)drive_g, drive_g->desc);
224 boot_add_hd(struct drive_s *drive_g, int prio)
226 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
227 , (u32)drive_g, drive_g->desc);
231 boot_add_cd(struct drive_s *drive_g, int prio)
233 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
234 , (u32)drive_g, drive_g->desc);
237 // Add a CBFS payload entry
239 boot_add_cbfs(void *data, const char *desc, int prio)
241 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
245 /****************************************************************
246 * Boot menu and BCV execution
247 ****************************************************************/
249 // Show IPL option menu.
251 interactive_bootmenu(void)
253 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
256 while (get_keystroke(0) >= 0)
259 printf("Press F12 for boot menu.\n\n");
262 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
263 disable_bootsplash();
264 if (scan_code != 0x86)
268 while (get_keystroke(0) >= 0)
271 printf("Select boot device:\n\n");
275 struct bootentry_s *pos = BootList;
280 printf("%d. %s\n", maxmenu
281 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
287 scan_code = get_keystroke(1000);
288 if (scan_code >= 1 && scan_code <= maxmenu+1)
292 if (scan_code == 0x01)
296 // Find entry and make top priority.
297 int choice = scan_code - 1;
298 struct bootentry_s **pprev = &BootList;
300 pprev = &(*pprev)->next;
303 pos->next = BootList;
312 static struct bev_s BEV[20];
314 static int HaveHDBoot, HaveFDBoot;
317 add_bev(int type, u32 vector)
319 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
321 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
323 if (BEVCount >= ARRAY_SIZE(BEV))
325 struct bev_s *bev = &BEV[BEVCount++];
327 bev->vector = vector;
330 // Prepare for boot - show menu and run bcvs.
339 // XXX - show available drives?
341 // Allow user to modify BCV/IPL order.
342 interactive_bootmenu();
345 // Map drives and populate BEV list
346 struct bootentry_s *pos = BootList;
350 call_bcv(pos->vector.seg, pos->vector.offset);
351 add_bev(IPL_TYPE_HARDDISK, 0);
353 case IPL_TYPE_FLOPPY:
354 map_floppy_drive(pos->drive);
355 add_bev(IPL_TYPE_FLOPPY, 0);
357 case IPL_TYPE_HARDDISK:
358 map_hd_drive(pos->drive);
359 add_bev(IPL_TYPE_HARDDISK, 0);
362 map_cd_drive(pos->drive);
365 add_bev(pos->type, pos->data);
371 // If nothing added a floppy/hd boot - add it manually.
372 add_bev(IPL_TYPE_FLOPPY, 0);
373 add_bev(IPL_TYPE_HARDDISK, 0);
377 /****************************************************************
378 * Boot code (int 18/19)
379 ****************************************************************/
381 // Jump to a bootup entry point.
383 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
385 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
387 memset(&br, 0, sizeof(br));
389 br.code = SEGOFF(bootseg, bootip);
390 // Set the magic number in ax and the boot drive in dl.
396 // Boot from a disk (either floppy or harddrive)
398 boot_disk(u8 bootdrv, int checksig)
400 u16 bootseg = 0x07c0;
404 memset(&br, 0, sizeof(br));
411 call16_int(0x13, &br);
413 if (br.flags & F_CF) {
414 printf("Boot failed: could not read the boot disk\n\n");
419 struct mbr_s *mbr = (void*)0;
420 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
421 printf("Boot failed: not a bootable disk\n\n");
426 /* Canonicalize bootseg:bootip */
427 u16 bootip = (bootseg & 0x0fff) << 4;
430 call_boot_entry(bootseg, bootip, bootdrv);
433 // Boot from a CD-ROM
435 boot_cdrom(struct bev_s *ie)
437 if (! CONFIG_CDROM_BOOT)
442 printf("Booting from DVD/CD...\n");
444 struct drive_s *drive_g = (void*)ie->vector;
445 int status = cdrom_boot(drive_g);
447 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
451 u16 ebda_seg = get_ebda_seg();
452 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
453 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
454 /* Canonicalize bootseg:bootip */
455 u16 bootip = (bootseg & 0x0fff) << 4;
458 call_boot_entry(bootseg, bootip, bootdrv);
461 // Boot from a CBFS payload
463 boot_cbfs(struct bev_s *ie)
465 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
467 printf("Booting from CBFS...\n");
468 cbfs_run_payload((void*)ie->vector);
475 panic("Boot support not compiled in.\n");
477 if (seq_nr >= BEVCount) {
478 printf("No bootable device.\n");
479 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
484 // Boot the given BEV type.
485 struct bev_s *ie = &BEV[seq_nr];
487 case IPL_TYPE_FLOPPY:
488 printf("Booting from Floppy...\n");
489 boot_disk(0x00, CheckFloppySig);
491 case IPL_TYPE_HARDDISK:
492 printf("Booting from Hard Disk...\n");
502 printf("Booting from ROM...\n");
503 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
507 // Boot failed: invoke the boot recovery function
509 memset(&br, 0, sizeof(br));
511 call16_int(0x18, &br);
514 // Boot Failure recovery: try the next device.
518 debug_serial_setup();
519 debug_enter(NULL, DEBUG_HDL_18);
520 u16 ebda_seg = get_ebda_seg();
521 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
522 SET_EBDA2(ebda_seg, boot_sequence, seq);
526 // INT 19h Boot Load Service Entry Point
530 debug_serial_setup();
531 debug_enter(NULL, DEBUG_HDL_19);
532 SET_EBDA(boot_sequence, 0);