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
18 /****************************************************************
19 * Boot priority ordering
20 ****************************************************************/
22 static char **Bootorder;
23 static int BootorderCount;
28 char *f = romfile_loadfile("bootorder", NULL);
39 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
46 dprintf(3, "boot order:\n");
54 dprintf(3, "%d: %s\n", i, Bootorder[i]);
60 int bootprio_find_pci_device(int bdf)
65 int bootprio_find_ata_device(int bdf, int chanid, int slave)
70 int bootprio_find_fdc_device(int bfd, int port, int fdid)
75 int bootprio_find_pci_rom(int bdf, int instance)
80 int bootprio_find_named_rom(const char *name, int instance)
86 /****************************************************************
88 ****************************************************************/
90 static int CheckFloppySig = 1;
92 #define DEFAULT_PRIO 9999
94 static int DefaultFloppyPrio = 101;
95 static int DefaultCDPrio = 102;
96 static int DefaultHDPrio = 103;
97 static int DefaultBEVPrio = 104;
105 SET_EBDA(boot_sequence, 0xffff);
107 if (!CONFIG_COREBOOT) {
108 // On emulators, get boot order from nvram.
109 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
111 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
112 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
113 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
114 = DefaultBEVPrio = DEFAULT_PRIO;
116 for (i=101; i<104; i++) {
117 u32 val = bootorder & 0x0f;
120 case 1: DefaultFloppyPrio = i; break;
121 case 2: DefaultHDPrio = i; break;
122 case 3: DefaultCDPrio = i; break;
123 case 4: DefaultBEVPrio = i; break;
132 /****************************************************************
134 ****************************************************************/
140 struct segoff_s vector;
141 struct drive_s *drive;
144 const char *description;
145 struct bootentry_s *next;
147 static struct bootentry_s *BootList;
149 #define IPL_TYPE_FLOPPY 0x01
150 #define IPL_TYPE_HARDDISK 0x02
151 #define IPL_TYPE_CDROM 0x03
152 #define IPL_TYPE_CBFS 0x20
153 #define IPL_TYPE_BEV 0x80
154 #define IPL_TYPE_BCV 0x81
157 bootentry_add(int type, int prio, u32 data, const char *desc)
161 struct bootentry_s *be = malloc_tmp(sizeof(*be));
169 be->description = desc;
171 // Add entry in sorted order.
172 struct bootentry_s **pprev;
173 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
174 struct bootentry_s *pos = *pprev;
175 if (be->priority < pos->priority)
177 if (be->priority > pos->priority)
179 if (be->type < pos->type)
181 if (be->type > pos->type)
183 if (be->type <= IPL_TYPE_CDROM
184 && (be->drive->type < pos->drive->type
185 || (be->drive->type == pos->drive->type
186 && be->drive->cntl_id < pos->drive->cntl_id)))
193 // Return the given priority if it's set - defaultprio otherwise.
194 static inline int defPrio(int priority, int defaultprio) {
195 return (priority < 0) ? defaultprio : priority;
198 // Add a BEV vector for a given pnp compatible option rom.
200 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
202 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
203 , SEGOFF(seg, bev).segoff
204 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
205 DefaultBEVPrio = DEFAULT_PRIO;
208 // Add a bcv entry for an expansion card harddrive or legacy option rom
210 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
212 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
213 , SEGOFF(seg, ip).segoff
214 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
218 boot_add_floppy(struct drive_s *drive_g, int prio)
220 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
221 , (u32)drive_g, drive_g->desc);
225 boot_add_hd(struct drive_s *drive_g, int prio)
227 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
228 , (u32)drive_g, drive_g->desc);
232 boot_add_cd(struct drive_s *drive_g, int prio)
234 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
235 , (u32)drive_g, drive_g->desc);
238 // Add a CBFS payload entry
240 boot_add_cbfs(void *data, const char *desc, int prio)
242 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
246 /****************************************************************
247 * Boot menu and BCV execution
248 ****************************************************************/
250 // Show IPL option menu.
252 interactive_bootmenu(void)
254 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
257 while (get_keystroke(0) >= 0)
260 printf("Press F12 for boot menu.\n\n");
263 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
264 disable_bootsplash();
265 if (scan_code != 0x86)
269 while (get_keystroke(0) >= 0)
272 printf("Select boot device:\n\n");
276 struct bootentry_s *pos = BootList;
281 printf("%d. %s\n", maxmenu
282 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
288 scan_code = get_keystroke(1000);
289 if (scan_code >= 1 && scan_code <= maxmenu+1)
293 if (scan_code == 0x01)
297 // Find entry and make top priority.
298 int choice = scan_code - 1;
299 struct bootentry_s **pprev = &BootList;
301 pprev = &(*pprev)->next;
304 pos->next = BootList;
313 static struct bev_s BEV[20];
315 static int HaveHDBoot, HaveFDBoot;
318 add_bev(int type, u32 vector)
320 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
322 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
324 if (BEVCount >= ARRAY_SIZE(BEV))
326 struct bev_s *bev = &BEV[BEVCount++];
328 bev->vector = vector;
331 // Prepare for boot - show menu and run bcvs.
340 // XXX - show available drives?
342 // Allow user to modify BCV/IPL order.
343 interactive_bootmenu();
346 // Map drives and populate BEV list
347 struct bootentry_s *pos = BootList;
351 call_bcv(pos->vector.seg, pos->vector.offset);
352 add_bev(IPL_TYPE_HARDDISK, 0);
354 case IPL_TYPE_FLOPPY:
355 map_floppy_drive(pos->drive);
356 add_bev(IPL_TYPE_FLOPPY, 0);
358 case IPL_TYPE_HARDDISK:
359 map_hd_drive(pos->drive);
360 add_bev(IPL_TYPE_HARDDISK, 0);
363 map_cd_drive(pos->drive);
366 add_bev(pos->type, pos->data);
372 // If nothing added a floppy/hd boot - add it manually.
373 add_bev(IPL_TYPE_FLOPPY, 0);
374 add_bev(IPL_TYPE_HARDDISK, 0);
378 /****************************************************************
379 * Boot code (int 18/19)
380 ****************************************************************/
382 // Jump to a bootup entry point.
384 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
386 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
388 memset(&br, 0, sizeof(br));
390 br.code = SEGOFF(bootseg, bootip);
391 // Set the magic number in ax and the boot drive in dl.
397 // Boot from a disk (either floppy or harddrive)
399 boot_disk(u8 bootdrv, int checksig)
401 u16 bootseg = 0x07c0;
405 memset(&br, 0, sizeof(br));
412 call16_int(0x13, &br);
414 if (br.flags & F_CF) {
415 printf("Boot failed: could not read the boot disk\n\n");
420 struct mbr_s *mbr = (void*)0;
421 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
422 printf("Boot failed: not a bootable disk\n\n");
427 /* Canonicalize bootseg:bootip */
428 u16 bootip = (bootseg & 0x0fff) << 4;
431 call_boot_entry(bootseg, bootip, bootdrv);
434 // Boot from a CD-ROM
436 boot_cdrom(struct bev_s *ie)
438 if (! CONFIG_CDROM_BOOT)
443 printf("Booting from DVD/CD...\n");
445 struct drive_s *drive_g = (void*)ie->vector;
446 int status = cdrom_boot(drive_g);
448 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
452 u16 ebda_seg = get_ebda_seg();
453 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
454 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
455 /* Canonicalize bootseg:bootip */
456 u16 bootip = (bootseg & 0x0fff) << 4;
459 call_boot_entry(bootseg, bootip, bootdrv);
462 // Boot from a CBFS payload
464 boot_cbfs(struct bev_s *ie)
466 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
468 printf("Booting from CBFS...\n");
469 cbfs_run_payload((void*)ie->vector);
476 panic("Boot support not compiled in.\n");
478 if (seq_nr >= BEVCount) {
479 printf("No bootable device.\n");
480 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
485 // Boot the given BEV type.
486 struct bev_s *ie = &BEV[seq_nr];
488 case IPL_TYPE_FLOPPY:
489 printf("Booting from Floppy...\n");
490 boot_disk(0x00, CheckFloppySig);
492 case IPL_TYPE_HARDDISK:
493 printf("Booting from Hard Disk...\n");
503 printf("Booting from ROM...\n");
504 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
508 // Boot failed: invoke the boot recovery function
510 memset(&br, 0, sizeof(br));
512 call16_int(0x18, &br);
515 // Boot Failure recovery: try the next device.
519 debug_serial_setup();
520 debug_enter(NULL, DEBUG_HDL_18);
521 u16 ebda_seg = get_ebda_seg();
522 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
523 SET_EBDA2(ebda_seg, boot_sequence, seq);
527 // INT 19h Boot Load Service Entry Point
531 debug_serial_setup();
532 debug_enter(NULL, DEBUG_HDL_19);
533 SET_EBDA(boot_sequence, 0);