1 // Code to load disk image and start system boot.
3 // Copyright (C) 2008 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" // irq_enable
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
19 /****************************************************************
20 * IPL and BCV handlers
21 ****************************************************************/
28 dprintf(3, "init boot device ordering\n");
30 memset(&IPL, 0, sizeof(IPL));
33 struct ipl_entry_s *ie = &IPL.bev[0];
34 ie->type = IPL_TYPE_FLOPPY;
35 ie->description = "Floppy";
39 ie->type = IPL_TYPE_HARDDISK;
40 ie->description = "Hard Disk";
44 if (CONFIG_CDROM_BOOT) {
45 ie->type = IPL_TYPE_CDROM;
46 ie->description = "CD-Rom";
50 if (CONFIG_COREBOOT_FLASH) {
51 ie->type = IPL_TYPE_CBFS;
52 ie->description = "CBFS";
56 IPL.bevcount = ie - IPL.bev;
57 SET_EBDA(boot_sequence, 0xffff);
58 if (CONFIG_COREBOOT) {
59 // XXX - hardcode defaults for coreboot.
60 IPL.bootorder = 0x87654231;
61 IPL.checkfloppysig = 1;
63 // On emulators, get boot order from nvram.
64 IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
65 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
66 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
67 IPL.checkfloppysig = 1;
71 // Add a BEV vector for a given pnp compatible option rom.
73 add_bev(u16 seg, u16 bev, u16 desc)
77 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
80 struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
81 ie->type = IPL_TYPE_BEV;
82 ie->vector = (seg << 16) | bev;
83 const char *d = "Unknown";
85 d = MAKE_FLATPTR(seg, desc);
89 // Add a bcv entry for an expansion card harddrive or legacy option rom
91 add_bcv(u16 seg, u16 ip, u16 desc)
95 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
98 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
99 ie->type = IPL_TYPE_BEV;
100 ie->vector = (seg << 16) | ip;
101 const char *d = "Legacy option rom";
103 d = MAKE_FLATPTR(seg, desc);
107 // Add a bcv entry for an ata harddrive
109 add_bcv_hd(int driveid, const char *desc)
113 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
116 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
117 ie->type = IPL_TYPE_HARDDISK;
118 ie->vector = driveid;
119 ie->description = desc;
123 /****************************************************************
124 * Boot menu and BCV execution
125 ****************************************************************/
127 // Show a generic menu item
129 menu_show_default(struct ipl_entry_s *ie, int menupos)
132 printf("%d. %s\n", menupos
133 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
137 // Show floppy menu item - but only if there exists a floppy drive.
139 menu_show_floppy(struct ipl_entry_s *ie, int menupos)
143 return menu_show_default(ie, menupos);
146 // Show menu items from BCV list.
148 menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
151 for (i = 0; i < IPL.bcvcount; i++) {
152 struct ipl_entry_s *ie = &IPL.bcv[i];
154 case IPL_TYPE_HARDDISK:
155 printf("%d. ata%d-%d %s\n", menupos + i
156 , ie->vector / 2, ie->vector % 2, ie->description);
159 menu_show_default(ie, menupos+i);
166 // Show cdrom menu item - but only if there exists a cdrom drive.
168 menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
171 for (i = 0; i < ATA.cdcount; i++) {
172 int driveid = ATA.idmap[1][i];
173 printf("%d. CD-Rom [ata%d-%d %s]\n", menupos + i
174 , driveid / 2, driveid % 2, ATA.devices[driveid].model);
179 // Show coreboot-fs menu item.
181 menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
185 const char *filename = cbfs_findNprefix("img/", count);
188 printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
196 // Show IPL option menu.
198 interactive_bootmenu()
200 if (! CONFIG_BOOTMENU)
203 while (get_keystroke(0) >= 0)
206 printf("Press F12 for boot menu.\n\n");
208 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
209 if (scan_code != 0x86)
213 while (get_keystroke(0) >= 0)
216 printf("Select boot device:\n\n");
218 int subcount[ARRAY_SIZE(IPL.bev)];
221 for (i = 0; i < IPL.bevcount; i++) {
222 struct ipl_entry_s *ie = &IPL.bev[i];
225 case IPL_TYPE_FLOPPY:
226 sc = menu_show_floppy(ie, menupos);
228 case IPL_TYPE_HARDDISK:
229 sc = menu_show_harddisk(ie, menupos);
232 sc = menu_show_cdrom(ie, menupos);
235 sc = menu_show_cbfs(ie, menupos);
238 sc = menu_show_default(ie, menupos);
246 scan_code = get_keystroke(1000);
247 if (scan_code == 0x01)
250 if (scan_code < 1 || scan_code > menupos)
252 int choice = scan_code - 1;
254 // Find out which IPL this was for.
256 while (choice > subcount[bev]) {
257 choice -= subcount[bev];
260 IPL.bev[bev].subchoice = choice-1;
262 // Add user choice to the boot order.
263 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
269 // Run the specified bcv.
271 run_bcv(struct ipl_entry_s *ie)
274 case IPL_TYPE_HARDDISK:
275 map_drive(ie->vector);
278 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
283 // Prepare for boot - show menu and run bcvs.
290 // Allow user to modify BCV/IPL order.
291 interactive_bootmenu();
294 int override = IPL.bev[1].subchoice;
295 if (override < IPL.bcvcount)
296 run_bcv(&IPL.bcv[override]);
298 for (i=0; i<IPL.bcvcount; i++)
300 run_bcv(&IPL.bcv[i]);
304 /****************************************************************
305 * Boot code (int 18/19)
306 ****************************************************************/
308 // Jump to a bootup entry point.
310 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
312 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
315 memset(&br, 0, sizeof(br));
318 // Set the magic number in ax and the boot drive in dl.
324 // Boot from a disk (either floppy or harddrive)
326 boot_disk(u8 bootdrv, int checksig)
328 u16 bootseg = 0x07c0;
332 memset(&br, 0, sizeof(br));
338 call16_int(0x13, &br);
340 if (br.flags & F_CF) {
341 printf("Boot failed: could not read the boot disk\n\n");
346 struct mbr_s *mbr = (void*)0;
347 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
348 printf("Boot failed: not a bootable disk\n\n");
353 /* Canonicalize bootseg:bootip */
354 u16 bootip = (bootseg & 0x0fff) << 4;
357 call_boot_entry(bootseg, bootip, bootdrv);
360 // Boot from a CD-ROM
362 boot_cdrom(struct ipl_entry_s *ie)
364 if (! CONFIG_CDROM_BOOT)
366 int status = cdrom_boot(ie->subchoice);
368 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
372 u16 ebda_seg = get_ebda_seg();
373 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
374 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
375 /* Canonicalize bootseg:bootip */
376 u16 bootip = (bootseg & 0x0fff) << 4;
379 call_boot_entry(bootseg, bootip, bootdrv);
382 // Boot from a CBFS payload
384 boot_cbfs(struct ipl_entry_s *ie)
386 if (! CONFIG_COREBOOT_FLASH)
388 const char *filename = cbfs_findNprefix("img/", ie->subchoice);
391 cbfs_run_payload(filename);
398 panic("Boot support not compiled in.\n");
400 u32 bootdev = IPL.bootorder;
401 bootdev >>= 4 * seq_nr;
404 /* Translate bootdev to an IPL table offset by subtracting 1 */
407 if (bootdev >= IPL.bevcount) {
408 printf("No bootable device.\n");
409 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
414 /* Do the loading, and set up vector as a far pointer to the boot
415 * address, and bootdrv as the boot drive */
416 struct ipl_entry_s *ie = &IPL.bev[bootdev];
418 printf("Booting from %s...\n"
419 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
422 case IPL_TYPE_FLOPPY:
423 boot_disk(0x00, IPL.checkfloppysig);
425 case IPL_TYPE_HARDDISK:
435 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
439 // Boot failed: invoke the boot recovery function
441 memset(&br, 0, sizeof(br));
442 call16_int(0x18, &br);
445 // Boot Failure recovery: try the next device.
449 debug_serial_setup();
450 debug_enter(NULL, DEBUG_HDL_18);
451 u16 ebda_seg = get_ebda_seg();
452 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
453 SET_EBDA2(ebda_seg, boot_sequence, seq);
457 // INT 19h Boot Load Service Entry Point
461 debug_serial_setup();
462 debug_enter(NULL, DEBUG_HDL_19);
463 SET_EBDA(boot_sequence, 0);