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 /****************************************************************
21 ****************************************************************/
28 dprintf(3, "init boot device ordering\n");
30 memset(&IPL, 0, sizeof(IPL));
33 struct ipl_entry_s *ie = &IPL.table[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.count = ie - IPL.table;
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)
69 // Found a device that thinks it can boot the system. Record
70 // its BEV and product name string.
75 if (IPL.count >= ARRAY_SIZE(IPL.table))
78 struct ipl_entry_s *ie = &IPL.table[IPL.count];
79 ie->type = IPL_TYPE_BEV;
80 ie->vector = (seg << 16) | bev;
82 ie->description = MAKE_FLATPTR(seg, desc);
88 /****************************************************************
90 ****************************************************************/
93 interactive_bootmenu()
95 if (! CONFIG_BOOTMENU)
98 while (get_keystroke(0) >= 0)
101 printf("Press F12 for boot menu.\n\n");
103 int scan_code = get_keystroke(2500);
104 if (scan_code != 0x86)
108 while (get_keystroke(0) >= 0)
111 printf("Select boot device:\n\n");
113 int count = IPL.count;
115 for (i = 0; i < count; i++) {
116 struct ipl_entry_s *ie = &IPL.table[i];
118 printf("%d. %s\n", i+1
119 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
123 scan_code = get_keystroke(1000);
124 if (scan_code == 0x01)
127 if (scan_code >= 0 && scan_code <= count + 1) {
128 // Add user choice to the boot order.
129 u16 choice = scan_code - 1;
130 u32 bootorder = IPL.bootorder;
131 IPL.bootorder = (bootorder << 4) | choice;
139 /****************************************************************
140 * Boot code (int 18/19)
141 ****************************************************************/
144 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
146 dprintf(1, "Booting from %x:%x\n", bootseg, bootip);
149 memset(&br, 0, sizeof(br));
152 // Set the magic number in ax and the boot drive in dl.
158 // Boot from a disk (either floppy or harddrive)
160 boot_disk(u8 bootdrv, int checksig)
162 u16 bootseg = 0x07c0;
166 memset(&br, 0, sizeof(br));
172 call16_int(0x13, &br);
174 if (br.flags & F_CF) {
175 printf("Boot failed: could not read the boot disk\n\n");
180 struct mbr_s *mbr = (void*)0;
181 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
182 printf("Boot failed: not a bootable disk\n\n");
187 /* Canonicalize bootseg:bootip */
188 u16 bootip = (bootseg & 0x0fff) << 4;
191 call_boot_entry(bootseg, bootip, bootdrv);
194 // Boot from a CD-ROM
198 if (! CONFIG_CDROM_BOOT)
200 int status = cdrom_boot();
202 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
206 u16 ebda_seg = get_ebda_seg();
207 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
208 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
209 /* Canonicalize bootseg:bootip */
210 u16 bootip = (bootseg & 0x0fff) << 4;
213 call_boot_entry(bootseg, bootip, bootdrv);
220 panic("Boot support not compiled in.\n");
222 u32 bootdev = IPL.bootorder;
223 bootdev >>= 4 * seq_nr;
227 panic("No bootable device.\n");
229 /* Translate bootdev to an IPL table offset by subtracting 1 */
232 if (bootdev >= IPL.count) {
233 dprintf(1, "Invalid boot device (0x%x)\n", bootdev);
237 /* Do the loading, and set up vector as a far pointer to the boot
238 * address, and bootdrv as the boot drive */
239 struct ipl_entry_s *ie = &IPL.table[bootdev];
241 printf("Booting from %s...\n"
242 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
245 case IPL_TYPE_FLOPPY:
246 boot_disk(0x00, IPL.checkfloppysig);
248 case IPL_TYPE_HARDDISK:
255 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
259 // Boot failed: invoke the boot recovery function
262 memset(&br, 0, sizeof(br));
263 call16_int(0x18, &br);
266 // Boot Failure recovery: try the next device.
270 debug_serial_setup();
271 debug_enter(NULL, DEBUG_HDL_18);
272 u16 ebda_seg = get_ebda_seg();
273 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
274 SET_EBDA2(ebda_seg, boot_sequence, seq);
278 // INT 19h Boot Load Service Entry Point
282 debug_serial_setup();
283 debug_enter(NULL, DEBUG_HDL_19);
284 SET_EBDA(boot_sequence, 0);
288 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
289 // functions to not be exported as global variables.
290 asm(".global handle_18, handle_19");