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 IPL.bevcount = ie - IPL.bev;
51 SET_EBDA(boot_sequence, 0xffff);
52 if (CONFIG_COREBOOT) {
53 // XXX - hardcode defaults for coreboot.
54 IPL.bootorder = 0x00000231;
55 IPL.checkfloppysig = 1;
57 // On emulators, get boot order from nvram.
58 IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
59 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
60 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
61 IPL.checkfloppysig = 1;
65 // Add a BEV vector for a given pnp compatible option rom.
67 add_bev(u16 seg, u16 bev, u16 desc)
71 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
74 struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
75 ie->type = IPL_TYPE_BEV;
76 ie->vector = (seg << 16) | bev;
77 const char *d = "Unknown";
79 d = MAKE_FLATPTR(seg, desc);
83 // Add a bcv entry for an expansion card harddrive or legacy option rom
85 add_bcv(u16 seg, u16 ip, u16 desc)
89 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
92 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
93 ie->type = IPL_TYPE_BEV;
94 ie->vector = (seg << 16) | ip;
95 const char *d = "Legacy option rom";
97 d = MAKE_FLATPTR(seg, desc);
101 // Add a bcv entry for an ata harddrive
103 add_bcv_hd(int driveid, const char *desc)
107 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
110 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
111 ie->type = IPL_TYPE_HARDDISK;
112 ie->vector = driveid;
113 ie->description = desc;
117 /****************************************************************
118 * Boot menu and BCV execution
119 ****************************************************************/
121 // Show a generic menu item
123 menu_show_default(struct ipl_entry_s *ie, int menupos)
126 printf("%d. %s\n", menupos
127 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
131 // Show floppy menu item - but only if there exists a floppy drive.
133 menu_show_floppy(struct ipl_entry_s *ie, int menupos)
137 return menu_show_default(ie, menupos);
140 // Show menu items from BCV list.
142 menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
145 for (i = 0; i < IPL.bcvcount; i++) {
146 struct ipl_entry_s *ie = &IPL.bcv[i];
148 case IPL_TYPE_HARDDISK:
149 printf("%d. ata%d-%d %s\n", menupos + i
150 , ie->vector / 2, ie->vector % 2, ie->description);
153 menu_show_default(ie, menupos+i);
160 // Show cdrom menu item - but only if there exists a cdrom drive.
162 menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
165 for (i = 0; i < ATA.cdcount; i++) {
166 int driveid = ATA.idmap[1][i];
167 printf("%d. CD-Rom [ata%d-%d %s]\n", menupos + i
168 , driveid / 2, driveid % 2, ATA.devices[driveid].model);
173 // Show IPL option menu.
175 interactive_bootmenu()
177 if (! CONFIG_BOOTMENU)
180 while (get_keystroke(0) >= 0)
183 printf("Press F12 for boot menu.\n\n");
185 int scan_code = get_keystroke(2500);
186 if (scan_code != 0x86)
190 while (get_keystroke(0) >= 0)
193 printf("Select boot device:\n\n");
195 int subcount[ARRAY_SIZE(IPL.bev)];
198 for (i = 0; i < IPL.bevcount; i++) {
199 struct ipl_entry_s *ie = &IPL.bev[i];
202 case IPL_TYPE_FLOPPY:
203 sc = menu_show_floppy(ie, menupos);
205 case IPL_TYPE_HARDDISK:
206 sc = menu_show_harddisk(ie, menupos);
209 sc = menu_show_cdrom(ie, menupos);
212 sc = menu_show_default(ie, menupos);
220 scan_code = get_keystroke(1000);
221 if (scan_code == 0x01)
224 if (scan_code < 1 || scan_code > menupos)
226 int choice = scan_code - 1;
228 // Find out which IPL this was for.
230 while (choice > subcount[bev]) {
231 choice -= subcount[bev];
235 switch (IPL.bev[bev].type) {
236 case IPL_TYPE_HARDDISK:
237 // A harddrive request enables a BCV order.
238 IPL.bcv_override = choice-1;
241 // Select cdrom to boot from.
242 IPL.cdrom_override = choice-1;
246 // Add user choice to the boot order.
247 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
253 // Run the specified bcv.
255 run_bcv(struct ipl_entry_s *ie)
258 case IPL_TYPE_HARDDISK:
259 map_drive(ie->vector);
262 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
267 // Prepare for boot - show menu and run bcvs.
274 // Allow user to modify BCV/IPL order.
275 interactive_bootmenu();
278 int override = IPL.bcv_override;
279 if (override < IPL.bcvcount)
280 run_bcv(&IPL.bcv[override]);
282 for (i=0; i<IPL.bcvcount; i++)
284 run_bcv(&IPL.bcv[i]);
288 /****************************************************************
289 * Boot code (int 18/19)
290 ****************************************************************/
292 // Jump to a bootup entry point.
294 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
296 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
299 memset(&br, 0, sizeof(br));
302 // Set the magic number in ax and the boot drive in dl.
308 // Boot from a disk (either floppy or harddrive)
310 boot_disk(u8 bootdrv, int checksig)
312 u16 bootseg = 0x07c0;
316 memset(&br, 0, sizeof(br));
322 call16_int(0x13, &br);
324 if (br.flags & F_CF) {
325 printf("Boot failed: could not read the boot disk\n\n");
330 struct mbr_s *mbr = (void*)0;
331 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
332 printf("Boot failed: not a bootable disk\n\n");
337 /* Canonicalize bootseg:bootip */
338 u16 bootip = (bootseg & 0x0fff) << 4;
341 call_boot_entry(bootseg, bootip, bootdrv);
344 // Boot from a CD-ROM
348 if (! CONFIG_CDROM_BOOT)
350 int status = cdrom_boot(IPL.cdrom_override);
352 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
356 u16 ebda_seg = get_ebda_seg();
357 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
358 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
359 /* Canonicalize bootseg:bootip */
360 u16 bootip = (bootseg & 0x0fff) << 4;
363 call_boot_entry(bootseg, bootip, bootdrv);
370 panic("Boot support not compiled in.\n");
372 u32 bootdev = IPL.bootorder;
373 bootdev >>= 4 * seq_nr;
377 printf("No bootable device.\n");
378 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
383 /* Translate bootdev to an IPL table offset by subtracting 1 */
386 if (bootdev >= IPL.bevcount) {
387 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
391 /* Do the loading, and set up vector as a far pointer to the boot
392 * address, and bootdrv as the boot drive */
393 struct ipl_entry_s *ie = &IPL.bev[bootdev];
395 printf("Booting from %s...\n"
396 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
399 case IPL_TYPE_FLOPPY:
400 boot_disk(0x00, IPL.checkfloppysig);
402 case IPL_TYPE_HARDDISK:
409 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
413 // Boot failed: invoke the boot recovery function
416 memset(&br, 0, sizeof(br));
417 call16_int(0x18, &br);
420 // Boot Failure recovery: try the next device.
424 debug_serial_setup();
425 debug_enter(NULL, DEBUG_HDL_18);
426 u16 ebda_seg = get_ebda_seg();
427 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
428 SET_EBDA2(ebda_seg, boot_sequence, seq);
432 // INT 19h Boot Load Service Entry Point
436 debug_serial_setup();
437 debug_enter(NULL, DEBUG_HDL_19);
438 SET_EBDA(boot_sequence, 0);
442 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
443 // functions to not be exported as global variables.
444 asm(".global handle_18, handle_19");