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
23 struct segoff_s vector;
24 struct drive_s *drive;
27 const char *description;
28 struct bootentry_s *next;
31 static struct bootentry_s *BootList;
34 /****************************************************************
36 ****************************************************************/
41 char *f = romfile_loadfile("bootorder", NULL);
46 IPL.fw_bootorder_count = 1;
49 IPL.fw_bootorder_count++;
52 IPL.fw_bootorder = malloc_tmphigh(IPL.fw_bootorder_count*sizeof(char*));
53 if (!IPL.fw_bootorder) {
59 dprintf(3, "boot order:\n");
62 IPL.fw_bootorder[i] = f;
67 dprintf(3, "%d: %s\n", i, IPL.fw_bootorder[i]);
73 #define DEFAULT_PRIO 9999
75 static int DefaultFloppyPrio = 101;
76 static int DefaultCDPrio = 102;
77 static int DefaultHDPrio = 103;
78 static int DefaultBEVPrio = 104;
86 SET_EBDA(boot_sequence, 0xffff);
87 IPL.checkfloppysig = 1;
89 if (!CONFIG_COREBOOT) {
90 // On emulators, get boot order from nvram.
91 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
92 IPL.checkfloppysig = 0;
93 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
94 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
95 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
96 = DefaultBEVPrio = DEFAULT_PRIO;
98 for (i=101; i<104; i++) {
99 u32 val = bootorder & 0x0f;
102 case 1: DefaultFloppyPrio = i; break;
103 case 2: DefaultHDPrio = i; break;
104 case 3: DefaultCDPrio = i; break;
105 case 4: DefaultBEVPrio = i; break;
114 /****************************************************************
115 * IPL and BCV handlers
116 ****************************************************************/
119 bootentry_add(int type, int prio, u32 data, const char *desc)
123 struct bootentry_s *be = malloc_tmp(sizeof(*be));
131 be->description = desc;
133 // Add entry in sorted order.
134 struct bootentry_s **pprev;
135 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
136 struct bootentry_s *pos = *pprev;
137 if (be->priority < pos->priority)
139 if (be->priority > pos->priority)
141 if (be->type < pos->type)
143 if (be->type > pos->type)
145 if (be->type <= IPL_TYPE_CDROM
146 && (be->drive->type < pos->drive->type
147 || (be->drive->type == pos->drive->type
148 && be->drive->cntl_id < pos->drive->cntl_id)))
155 // Add a BEV vector for a given pnp compatible option rom.
157 boot_add_bev(u16 seg, u16 bev, u16 desc)
159 bootentry_add(IPL_TYPE_BEV, DefaultBEVPrio, SEGOFF(seg, bev).segoff
160 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
161 DefaultBEVPrio = DEFAULT_PRIO;
164 // Add a bcv entry for an expansion card harddrive or legacy option rom
166 boot_add_bcv(u16 seg, u16 ip, u16 desc)
168 bootentry_add(IPL_TYPE_BCV, DEFAULT_PRIO, SEGOFF(seg, ip).segoff
169 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
173 boot_add_floppy(struct drive_s *drive_g)
175 bootentry_add(IPL_TYPE_FLOPPY, DefaultFloppyPrio, (u32)drive_g
180 boot_add_hd(struct drive_s *drive_g)
182 bootentry_add(IPL_TYPE_HARDDISK, DefaultHDPrio, (u32)drive_g
187 boot_add_cd(struct drive_s *drive_g)
189 bootentry_add(IPL_TYPE_CDROM, DefaultCDPrio, (u32)drive_g
193 // Add a CBFS payload entry
195 boot_add_cbfs(void *data, const char *desc)
197 bootentry_add(IPL_TYPE_CBFS, DEFAULT_PRIO, (u32)data, desc);
201 /****************************************************************
202 * Boot menu and BCV execution
203 ****************************************************************/
205 // Show IPL option menu.
207 interactive_bootmenu(void)
209 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
212 while (get_keystroke(0) >= 0)
215 printf("Press F12 for boot menu.\n\n");
218 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
219 disable_bootsplash();
220 if (scan_code != 0x86)
224 while (get_keystroke(0) >= 0)
227 printf("Select boot device:\n\n");
231 struct bootentry_s *pos = BootList;
236 printf("%d. %s\n", maxmenu
237 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
242 scan_code = get_keystroke(1000);
243 if (scan_code == 0x01)
246 if (scan_code < 1 || scan_code > maxmenu+1)
248 int choice = scan_code - 1;
250 // Find entry and make top priority.
251 struct bootentry_s **pprev = &BootList;
253 pprev = &(*pprev)->next;
256 pos->next = BootList;
264 static int HaveHDBoot, HaveFDBoot;
267 add_bev(int type, u32 vector)
269 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
271 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
273 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
275 struct ipl_entry_s *bev = &IPL.bev[IPL.bevcount++];
277 bev->vector = vector;
280 // Prepare for boot - show menu and run bcvs.
289 // XXX - show available drives?
291 // Allow user to modify BCV/IPL order.
292 interactive_bootmenu();
295 // Map drives and populate BEV list
296 struct bootentry_s *pos = BootList;
300 call_bcv(pos->vector.seg, pos->vector.offset);
301 add_bev(IPL_TYPE_HARDDISK, 0);
303 case IPL_TYPE_FLOPPY:
304 map_floppy_drive(pos->drive);
305 add_bev(IPL_TYPE_FLOPPY, 0);
307 case IPL_TYPE_HARDDISK:
308 map_hd_drive(pos->drive);
309 add_bev(IPL_TYPE_HARDDISK, 0);
312 map_cd_drive(pos->drive);
315 add_bev(pos->type, pos->data);
321 // If nothing added a floppy/hd boot - add it manually.
322 add_bev(IPL_TYPE_FLOPPY, 0);
323 add_bev(IPL_TYPE_HARDDISK, 0);
327 /****************************************************************
328 * Boot code (int 18/19)
329 ****************************************************************/
331 // Jump to a bootup entry point.
333 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
335 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
337 memset(&br, 0, sizeof(br));
339 br.code = SEGOFF(bootseg, bootip);
340 // Set the magic number in ax and the boot drive in dl.
346 // Boot from a disk (either floppy or harddrive)
348 boot_disk(u8 bootdrv, int checksig)
350 u16 bootseg = 0x07c0;
354 memset(&br, 0, sizeof(br));
361 call16_int(0x13, &br);
363 if (br.flags & F_CF) {
364 printf("Boot failed: could not read the boot disk\n\n");
369 struct mbr_s *mbr = (void*)0;
370 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
371 printf("Boot failed: not a bootable disk\n\n");
376 /* Canonicalize bootseg:bootip */
377 u16 bootip = (bootseg & 0x0fff) << 4;
380 call_boot_entry(bootseg, bootip, bootdrv);
383 // Boot from a CD-ROM
385 boot_cdrom(struct ipl_entry_s *ie)
387 if (! CONFIG_CDROM_BOOT)
392 printf("Booting from DVD/CD...\n");
394 struct drive_s *drive_g = (void*)ie->vector;
395 int status = cdrom_boot(drive_g);
397 printf("Boot failed: Could not read from CDROM %s (code %04x)\n", drive_g->desc, status);
401 u16 ebda_seg = get_ebda_seg();
402 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
403 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
404 /* Canonicalize bootseg:bootip */
405 u16 bootip = (bootseg & 0x0fff) << 4;
408 call_boot_entry(bootseg, bootip, bootdrv);
411 // Boot from a CBFS payload
413 boot_cbfs(struct ipl_entry_s *ie)
415 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
417 printf("Booting from CBFS...\n");
418 cbfs_run_payload((void*)ie->vector);
425 panic("Boot support not compiled in.\n");
427 if (seq_nr >= IPL.bevcount) {
428 printf("No bootable device.\n");
429 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
434 // Boot the given BEV type.
435 struct ipl_entry_s *ie = &IPL.bev[seq_nr];
437 case IPL_TYPE_FLOPPY:
438 printf("Booting from Floppy...\n");
439 boot_disk(0x00, IPL.checkfloppysig);
441 case IPL_TYPE_HARDDISK:
442 printf("Booting from Hard Disk...\n");
452 printf("Booting from ROM...\n");
453 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
457 // Boot failed: invoke the boot recovery function
459 memset(&br, 0, sizeof(br));
461 call16_int(0x18, &br);
464 // Boot Failure recovery: try the next device.
468 debug_serial_setup();
469 debug_enter(NULL, DEBUG_HDL_18);
470 u16 ebda_seg = get_ebda_seg();
471 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
472 SET_EBDA2(ebda_seg, boot_sequence, seq);
476 // INT 19h Boot Load Service Entry Point
480 debug_serial_setup();
481 debug_enter(NULL, DEBUG_HDL_19);
482 SET_EBDA(boot_sequence, 0);