blob: dacf6395e9762d3342b26f493a34180adc506d57 [file] [log] [blame]
Kevin O'Connorf13b0082008-08-17 11:26:42 -04001// Code to load disk image and start system boot.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05002//
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -04003// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor10ad7992009-10-24 11:06:08 -04008#include "util.h" // dprintf
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "biosvar.h" // GET_EBDA
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050010#include "config.h" // CONFIG_*
Kevin O'Connor180a9592008-03-04 22:50:53 -050011#include "disk.h" // cdrom_boot
Kevin O'Connor9521e262008-07-04 13:04:29 -040012#include "bregs.h" // struct bregs
Kevin O'Connorc659fde2008-12-28 23:43:20 -050013#include "boot.h" // struct ipl_s
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050014#include "cmos.h" // inb_cmos
Kevin O'Connore7cc7642009-10-04 10:05:16 -040015#include "paravirt.h"
Kevin O'Connorc659fde2008-12-28 23:43:20 -050016
17struct ipl_s IPL;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050018
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050019
20/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050021 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050022 ****************************************************************/
23
Kevin O'Connord1a17462010-12-24 11:17:03 -050024static void
25loadBootOrder(void)
26{
27 char *f = romfile_loadfile("bootorder", NULL);
28 if (!f)
29 return;
30
31 int i;
32 IPL.fw_bootorder_count = 1;
33 while (f[i]) {
34 if (f[i] == '\n')
35 IPL.fw_bootorder_count++;
36 i++;
37 }
38 IPL.fw_bootorder = malloc_tmphigh(IPL.fw_bootorder_count*sizeof(char*));
39 if (!IPL.fw_bootorder) {
40 warn_noalloc();
41 free(f);
42 return;
43 }
44
45 dprintf(3, "boot order:\n");
46 i = 0;
47 do {
48 IPL.fw_bootorder[i] = f;
49 f = strchr(f, '\n');
50 if (f) {
51 *f = '\0';
52 f++;
53 dprintf(3, "%d: %s\n", i, IPL.fw_bootorder[i]);
54 i++;
55 }
56 } while(f);
57}
58
Kevin O'Connor031ef552010-12-27 19:26:57 -050059int bootprio_find_pci_device(int bdf)
60{
61 return -1;
62}
63
64int bootprio_find_ata_device(int bdf, int chanid, int slave)
65{
66 return -1;
67}
68
69int bootprio_find_fdc_device(int bfd, int port, int fdid)
70{
71 return -1;
72}
73
74int bootprio_find_pci_rom(int bdf, int instance)
75{
76 return -1;
77}
78
79int bootprio_find_named_rom(const char *name, int instance)
80{
81 return -1;
82}
83
84
85/****************************************************************
86 * Boot setup
87 ****************************************************************/
88
Kevin O'Connorbaaadb62010-12-29 11:18:04 -050089static int CheckFloppySig = 1;
90
Kevin O'Connor72eee3e2010-12-27 19:07:49 -050091#define DEFAULT_PRIO 9999
92
93static int DefaultFloppyPrio = 101;
94static int DefaultCDPrio = 102;
95static int DefaultHDPrio = 103;
96static int DefaultBEVPrio = 104;
97
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050098void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050099boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500100{
101 if (! CONFIG_BOOT)
102 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500103
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500104 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500105
106 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500107 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500108 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500109 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500110 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500111 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500112 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
113 = DefaultBEVPrio = DEFAULT_PRIO;
114 int i;
115 for (i=101; i<104; i++) {
116 u32 val = bootorder & 0x0f;
117 bootorder >>= 4;
118 switch (val) {
119 case 1: DefaultFloppyPrio = i; break;
120 case 2: DefaultHDPrio = i; break;
121 case 3: DefaultCDPrio = i; break;
122 case 4: DefaultBEVPrio = i; break;
123 }
124 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500125 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200126
Kevin O'Connord1a17462010-12-24 11:17:03 -0500127 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500128}
129
Kevin O'Connord1a17462010-12-24 11:17:03 -0500130
131/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500132 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500133 ****************************************************************/
134
Kevin O'Connor031ef552010-12-27 19:26:57 -0500135struct bootentry_s {
136 int type;
137 union {
138 u32 data;
139 struct segoff_s vector;
140 struct drive_s *drive;
141 };
142 int priority;
143 const char *description;
144 struct bootentry_s *next;
145};
146
147static struct bootentry_s *BootList;
148
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500149static void
150bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500151{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500152 if (! CONFIG_BOOT)
153 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500154 struct bootentry_s *be = malloc_tmp(sizeof(*be));
155 if (!be) {
156 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500157 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500158 }
159 be->type = type;
160 be->priority = prio;
161 be->data = data;
162 be->description = desc;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500163
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500164 // Add entry in sorted order.
165 struct bootentry_s **pprev;
166 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
167 struct bootentry_s *pos = *pprev;
168 if (be->priority < pos->priority)
169 break;
170 if (be->priority > pos->priority)
171 continue;
172 if (be->type < pos->type)
173 break;
174 if (be->type > pos->type)
175 continue;
176 if (be->type <= IPL_TYPE_CDROM
177 && (be->drive->type < pos->drive->type
178 || (be->drive->type == pos->drive->type
179 && be->drive->cntl_id < pos->drive->cntl_id)))
180 break;
181 }
182 be->next = *pprev;
183 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500184}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500185
Kevin O'Connor031ef552010-12-27 19:26:57 -0500186// Return the given priority if it's set - defaultprio otherwise.
187static inline int defPrio(int priority, int defaultprio) {
188 return (priority < 0) ? defaultprio : priority;
189}
190
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500191// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200192void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500193boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200194{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500195 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
196 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500197 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
198 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200199}
200
Kevin O'Connor0a924122009-02-08 19:43:47 -0500201// Add a bcv entry for an expansion card harddrive or legacy option rom
202void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500203boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500204{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500205 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
206 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500207 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500208}
209
Kevin O'Connor0a924122009-02-08 19:43:47 -0500210void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500211boot_add_floppy(struct drive_s *drive_g, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500212{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500213 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
214 , (u32)drive_g, drive_g->desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500215}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500216
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500217void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500218boot_add_hd(struct drive_s *drive_g, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500219{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500220 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
221 , (u32)drive_g, drive_g->desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500222}
223
224void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500225boot_add_cd(struct drive_s *drive_g, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500226{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500227 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
228 , (u32)drive_g, drive_g->desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500229}
230
231// Add a CBFS payload entry
232void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500233boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500234{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500235 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500236}
237
238
239/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500240 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500241 ****************************************************************/
242
Kevin O'Connor0a924122009-02-08 19:43:47 -0500243// Show IPL option menu.
244static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500245interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500246{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400247 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500248 return;
249
250 while (get_keystroke(0) >= 0)
251 ;
252
253 printf("Press F12 for boot menu.\n\n");
254
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400255 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400256 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400257 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500258 if (scan_code != 0x86)
259 /* not F12 */
260 return;
261
262 while (get_keystroke(0) >= 0)
263 ;
264
265 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400266 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500267
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500268 // Show menu items
269 struct bootentry_s *pos = BootList;
270 int maxmenu = 0;
271 while (pos) {
272 char desc[60];
273 maxmenu++;
274 printf("%d. %s\n", maxmenu
275 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
276 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500277 }
278
Kevin O'Connor551caa22010-12-29 13:25:18 -0500279 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500280 for (;;) {
281 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500282 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500283 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500284 }
285 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500286 if (scan_code == 0x01)
287 // ESC
288 return;
289
290 // Find entry and make top priority.
291 int choice = scan_code - 1;
292 struct bootentry_s **pprev = &BootList;
293 while (--choice)
294 pprev = &(*pprev)->next;
295 pos = *pprev;
296 *pprev = pos->next;
297 pos->next = BootList;
298 BootList = pos;
299 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500300}
301
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500302static int HaveHDBoot, HaveFDBoot;
303
Kevin O'Connor0a924122009-02-08 19:43:47 -0500304static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500305add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500306{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500307 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
308 return;
309 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
310 return;
311 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
312 return;
313 struct ipl_entry_s *bev = &IPL.bev[IPL.bevcount++];
314 bev->type = type;
315 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500316}
317
318// Prepare for boot - show menu and run bcvs.
319void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500320boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500321{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400322 if (! CONFIG_BOOT) {
323 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500324 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400325 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500326
Kevin O'Connora5826b52009-10-24 17:57:29 -0400327 // XXX - show available drives?
328
Kevin O'Connor0a924122009-02-08 19:43:47 -0500329 // Allow user to modify BCV/IPL order.
330 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400331 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500332
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500333 // Map drives and populate BEV list
334 struct bootentry_s *pos = BootList;
335 while (pos) {
336 switch (pos->type) {
337 case IPL_TYPE_BCV:
338 call_bcv(pos->vector.seg, pos->vector.offset);
339 add_bev(IPL_TYPE_HARDDISK, 0);
340 break;
341 case IPL_TYPE_FLOPPY:
342 map_floppy_drive(pos->drive);
343 add_bev(IPL_TYPE_FLOPPY, 0);
344 break;
345 case IPL_TYPE_HARDDISK:
346 map_hd_drive(pos->drive);
347 add_bev(IPL_TYPE_HARDDISK, 0);
348 break;
349 case IPL_TYPE_CDROM:
350 map_cd_drive(pos->drive);
351 // NO BREAK
352 default:
353 add_bev(pos->type, pos->data);
354 break;
355 }
356 pos = pos->next;
357 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400358
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500359 // If nothing added a floppy/hd boot - add it manually.
360 add_bev(IPL_TYPE_FLOPPY, 0);
361 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500362}
363
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500364
365/****************************************************************
366 * Boot code (int 18/19)
367 ****************************************************************/
368
Kevin O'Connor0a924122009-02-08 19:43:47 -0500369// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500370static void
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500371call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
372{
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400373 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500374 struct bregs br;
375 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400376 br.flags = F_IF;
Kevin O'Connor9f985422009-09-09 11:34:39 -0400377 br.code = SEGOFF(bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500378 // Set the magic number in ax and the boot drive in dl.
379 br.dl = bootdrv;
380 br.ax = 0xaa55;
381 call16(&br);
382}
383
384// Boot from a disk (either floppy or harddrive)
385static void
386boot_disk(u8 bootdrv, int checksig)
387{
388 u16 bootseg = 0x07c0;
389
390 // Read sector
391 struct bregs br;
392 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400393 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500394 br.dl = bootdrv;
395 br.es = bootseg;
396 br.ah = 2;
397 br.al = 1;
398 br.cl = 1;
399 call16_int(0x13, &br);
400
401 if (br.flags & F_CF) {
402 printf("Boot failed: could not read the boot disk\n\n");
403 return;
404 }
405
406 if (checksig) {
407 struct mbr_s *mbr = (void*)0;
408 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
409 printf("Boot failed: not a bootable disk\n\n");
410 return;
411 }
412 }
413
414 /* Canonicalize bootseg:bootip */
415 u16 bootip = (bootseg & 0x0fff) << 4;
416 bootseg &= 0xf000;
417
418 call_boot_entry(bootseg, bootip, bootdrv);
419}
420
421// Boot from a CD-ROM
422static void
Kevin O'Connor67823442009-04-13 14:14:51 -0400423boot_cdrom(struct ipl_entry_s *ie)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500424{
425 if (! CONFIG_CDROM_BOOT)
426 return;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200427
428 if (!ie->vector)
429 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500430 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200431
432 struct drive_s *drive_g = (void*)ie->vector;
433 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500434 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500435 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500436 return;
437 }
438
439 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400440 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500441 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
442 /* Canonicalize bootseg:bootip */
443 u16 bootip = (bootseg & 0x0fff) << 4;
444 bootseg &= 0xf000;
445
446 call_boot_entry(bootseg, bootip, bootdrv);
447}
448
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400449// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400450static void
451boot_cbfs(struct ipl_entry_s *ie)
452{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400453 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400454 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500455 printf("Booting from CBFS...\n");
456 cbfs_run_payload((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400457}
458
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500459static void
460do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500461{
Kevin O'Connor40967022008-07-21 22:23:05 -0400462 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500463 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400464
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500465 if (seq_nr >= IPL.bevcount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400466 printf("No bootable device.\n");
467 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
468 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400469 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500470 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500471
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500472 // Boot the given BEV type.
473 struct ipl_entry_s *ie = &IPL.bev[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400474 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400475 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500476 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500477 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500478 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400479 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500480 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500481 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500482 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500483 case IPL_TYPE_CDROM:
Kevin O'Connor67823442009-04-13 14:14:51 -0400484 boot_cdrom(ie);
485 break;
486 case IPL_TYPE_CBFS:
487 boot_cbfs(ie);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500488 break;
489 case IPL_TYPE_BEV:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500490 printf("Booting from ROM...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500491 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500492 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500493 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500494
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500495 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400496 struct bregs br;
497 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400498 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400499 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500500}
501
502// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500503void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500504handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500505{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400506 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400507 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500508 u16 ebda_seg = get_ebda_seg();
509 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
510 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400511 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500512}
513
514// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500515void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500516handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500517{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400518 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400519 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500520 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400521 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500522}