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
20 /****************************************************************
21 * Boot priority ordering
22 ****************************************************************/
27 char *f = romfile_loadfile("bootorder", NULL);
32 IPL.fw_bootorder_count = 1;
35 IPL.fw_bootorder_count++;
38 IPL.fw_bootorder = malloc_tmphigh(IPL.fw_bootorder_count*sizeof(char*));
39 if (!IPL.fw_bootorder) {
45 dprintf(3, "boot order:\n");
48 IPL.fw_bootorder[i] = f;
53 dprintf(3, "%d: %s\n", i, IPL.fw_bootorder[i]);
59 int bootprio_find_pci_device(int bdf)
64 int bootprio_find_ata_device(int bdf, int chanid, int slave)
69 int bootprio_find_fdc_device(int bfd, int port, int fdid)
74 int bootprio_find_pci_rom(int bdf, int instance)
79 int bootprio_find_named_rom(const char *name, int instance)
85 /****************************************************************
87 ****************************************************************/
89 #define DEFAULT_PRIO 9999
91 static int DefaultFloppyPrio = 101;
92 static int DefaultCDPrio = 102;
93 static int DefaultHDPrio = 103;
94 static int DefaultBEVPrio = 104;
102 SET_EBDA(boot_sequence, 0xffff);
103 IPL.checkfloppysig = 1;
105 if (!CONFIG_COREBOOT) {
106 // On emulators, get boot order from nvram.
107 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
108 IPL.checkfloppysig = 0;
109 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
110 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
111 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
112 = DefaultBEVPrio = DEFAULT_PRIO;
114 for (i=101; i<104; i++) {
115 u32 val = bootorder & 0x0f;
118 case 1: DefaultFloppyPrio = i; break;
119 case 2: DefaultHDPrio = i; break;
120 case 3: DefaultCDPrio = i; break;
121 case 4: DefaultBEVPrio = i; break;
130 /****************************************************************
132 ****************************************************************/
138 struct segoff_s vector;
139 struct drive_s *drive;
142 const char *description;
143 struct bootentry_s *next;
146 static struct bootentry_s *BootList;
149 bootentry_add(int type, int prio, u32 data, const char *desc)
153 struct bootentry_s *be = malloc_tmp(sizeof(*be));
161 be->description = desc;
163 // Add entry in sorted order.
164 struct bootentry_s **pprev;
165 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
166 struct bootentry_s *pos = *pprev;
167 if (be->priority < pos->priority)
169 if (be->priority > pos->priority)
171 if (be->type < pos->type)
173 if (be->type > pos->type)
175 if (be->type <= IPL_TYPE_CDROM
176 && (be->drive->type < pos->drive->type
177 || (be->drive->type == pos->drive->type
178 && be->drive->cntl_id < pos->drive->cntl_id)))
185 // Return the given priority if it's set - defaultprio otherwise.
186 static inline int defPrio(int priority, int defaultprio) {
187 return (priority < 0) ? defaultprio : priority;
190 // Add a BEV vector for a given pnp compatible option rom.
192 boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
194 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
195 , SEGOFF(seg, bev).segoff
196 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
197 DefaultBEVPrio = DEFAULT_PRIO;
200 // Add a bcv entry for an expansion card harddrive or legacy option rom
202 boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
204 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
205 , SEGOFF(seg, ip).segoff
206 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
210 boot_add_floppy(struct drive_s *drive_g, int prio)
212 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
213 , (u32)drive_g, drive_g->desc);
217 boot_add_hd(struct drive_s *drive_g, int prio)
219 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
220 , (u32)drive_g, drive_g->desc);
224 boot_add_cd(struct drive_s *drive_g, int prio)
226 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
227 , (u32)drive_g, drive_g->desc);
230 // Add a CBFS payload entry
232 boot_add_cbfs(void *data, const char *desc, int prio)
234 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
238 /****************************************************************
239 * Boot menu and BCV execution
240 ****************************************************************/
242 // Show IPL option menu.
244 interactive_bootmenu(void)
246 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
249 while (get_keystroke(0) >= 0)
252 printf("Press F12 for boot menu.\n\n");
255 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
256 disable_bootsplash();
257 if (scan_code != 0x86)
261 while (get_keystroke(0) >= 0)
264 printf("Select boot device:\n\n");
268 struct bootentry_s *pos = BootList;
273 printf("%d. %s\n", maxmenu
274 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
280 scan_code = get_keystroke(1000);
281 if (scan_code >= 1 && scan_code <= maxmenu+1)
285 if (scan_code == 0x01)
289 // Find entry and make top priority.
290 int choice = scan_code - 1;
291 struct bootentry_s **pprev = &BootList;
293 pprev = &(*pprev)->next;
296 pos->next = BootList;
301 static int HaveHDBoot, HaveFDBoot;
304 add_bev(int type, u32 vector)
306 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
308 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
310 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
312 struct ipl_entry_s *bev = &IPL.bev[IPL.bevcount++];
314 bev->vector = vector;
317 // Prepare for boot - show menu and run bcvs.
326 // XXX - show available drives?
328 // Allow user to modify BCV/IPL order.
329 interactive_bootmenu();
332 // Map drives and populate BEV list
333 struct bootentry_s *pos = BootList;
337 call_bcv(pos->vector.seg, pos->vector.offset);
338 add_bev(IPL_TYPE_HARDDISK, 0);
340 case IPL_TYPE_FLOPPY:
341 map_floppy_drive(pos->drive);
342 add_bev(IPL_TYPE_FLOPPY, 0);
344 case IPL_TYPE_HARDDISK:
345 map_hd_drive(pos->drive);
346 add_bev(IPL_TYPE_HARDDISK, 0);
349 map_cd_drive(pos->drive);
352 add_bev(pos->type, pos->data);
358 // If nothing added a floppy/hd boot - add it manually.
359 add_bev(IPL_TYPE_FLOPPY, 0);
360 add_bev(IPL_TYPE_HARDDISK, 0);
364 /****************************************************************
365 * Boot code (int 18/19)
366 ****************************************************************/
368 // Jump to a bootup entry point.
370 call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
372 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
374 memset(&br, 0, sizeof(br));
376 br.code = SEGOFF(bootseg, bootip);
377 // Set the magic number in ax and the boot drive in dl.
383 // Boot from a disk (either floppy or harddrive)
385 boot_disk(u8 bootdrv, int checksig)
387 u16 bootseg = 0x07c0;
391 memset(&br, 0, sizeof(br));
398 call16_int(0x13, &br);
400 if (br.flags & F_CF) {
401 printf("Boot failed: could not read the boot disk\n\n");
406 struct mbr_s *mbr = (void*)0;
407 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
408 printf("Boot failed: not a bootable disk\n\n");
413 /* Canonicalize bootseg:bootip */
414 u16 bootip = (bootseg & 0x0fff) << 4;
417 call_boot_entry(bootseg, bootip, bootdrv);
420 // Boot from a CD-ROM
422 boot_cdrom(struct ipl_entry_s *ie)
424 if (! CONFIG_CDROM_BOOT)
429 printf("Booting from DVD/CD...\n");
431 struct drive_s *drive_g = (void*)ie->vector;
432 int status = cdrom_boot(drive_g);
434 printf("Boot failed: Could not read from CDROM %s (code %04x)\n", drive_g->desc, status);
438 u16 ebda_seg = get_ebda_seg();
439 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
440 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
441 /* Canonicalize bootseg:bootip */
442 u16 bootip = (bootseg & 0x0fff) << 4;
445 call_boot_entry(bootseg, bootip, bootdrv);
448 // Boot from a CBFS payload
450 boot_cbfs(struct ipl_entry_s *ie)
452 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
454 printf("Booting from CBFS...\n");
455 cbfs_run_payload((void*)ie->vector);
462 panic("Boot support not compiled in.\n");
464 if (seq_nr >= IPL.bevcount) {
465 printf("No bootable device.\n");
466 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
471 // Boot the given BEV type.
472 struct ipl_entry_s *ie = &IPL.bev[seq_nr];
474 case IPL_TYPE_FLOPPY:
475 printf("Booting from Floppy...\n");
476 boot_disk(0x00, IPL.checkfloppysig);
478 case IPL_TYPE_HARDDISK:
479 printf("Booting from Hard Disk...\n");
489 printf("Booting from ROM...\n");
490 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
494 // Boot failed: invoke the boot recovery function
496 memset(&br, 0, sizeof(br));
498 call16_int(0x18, &br);
501 // Boot Failure recovery: try the next device.
505 debug_serial_setup();
506 debug_enter(NULL, DEBUG_HDL_18);
507 u16 ebda_seg = get_ebda_seg();
508 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
509 SET_EBDA2(ebda_seg, boot_sequence, seq);
513 // INT 19h Boot Load Service Entry Point
517 debug_serial_setup();
518 debug_enter(NULL, DEBUG_HDL_19);
519 SET_EBDA(boot_sequence, 0);