blob: e83dcdc3e41a3eec69bdf1babf4e4e5f415a61b9 [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'Connorf13a1802010-12-29 11:52:33 -050013#include "boot.h" // func defs
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050014#include "cmos.h" // inb_cmos
Kevin O'Connorf13a1802010-12-29 11:52:33 -050015#include "paravirt.h" // romfile_loadfile
Kevin O'Connorc659fde2008-12-28 23:43:20 -050016
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050017
18/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050019 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050020 ****************************************************************/
21
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050022static char **Bootorder;
23static int BootorderCount;
24
Kevin O'Connord1a17462010-12-24 11:17:03 -050025static void
26loadBootOrder(void)
27{
28 char *f = romfile_loadfile("bootorder", NULL);
29 if (!f)
30 return;
31
32 int i;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050033 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050034 while (f[i]) {
35 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050036 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050037 i++;
38 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050039 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
40 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050041 warn_noalloc();
42 free(f);
43 return;
44 }
45
46 dprintf(3, "boot order:\n");
47 i = 0;
48 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050049 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050050 f = strchr(f, '\n');
51 if (f) {
52 *f = '\0';
53 f++;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050054 dprintf(3, "%d: %s\n", i, Bootorder[i]);
Kevin O'Connord1a17462010-12-24 11:17:03 -050055 i++;
56 }
57 } while(f);
58}
59
Kevin O'Connor031ef552010-12-27 19:26:57 -050060int bootprio_find_pci_device(int bdf)
61{
62 return -1;
63}
64
65int bootprio_find_ata_device(int bdf, int chanid, int slave)
66{
67 return -1;
68}
69
70int bootprio_find_fdc_device(int bfd, int port, int fdid)
71{
72 return -1;
73}
74
75int bootprio_find_pci_rom(int bdf, int instance)
76{
77 return -1;
78}
79
80int bootprio_find_named_rom(const char *name, int instance)
81{
82 return -1;
83}
84
85
86/****************************************************************
87 * Boot setup
88 ****************************************************************/
89
Kevin O'Connorbaaadb62010-12-29 11:18:04 -050090static int CheckFloppySig = 1;
91
Kevin O'Connor72eee3e2010-12-27 19:07:49 -050092#define DEFAULT_PRIO 9999
93
94static int DefaultFloppyPrio = 101;
95static int DefaultCDPrio = 102;
96static int DefaultHDPrio = 103;
97static int DefaultBEVPrio = 104;
98
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050099void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500100boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500101{
102 if (! CONFIG_BOOT)
103 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500104
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500105 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500106
107 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500108 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500109 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500110 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500111 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500112 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500113 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
114 = DefaultBEVPrio = DEFAULT_PRIO;
115 int i;
116 for (i=101; i<104; i++) {
117 u32 val = bootorder & 0x0f;
118 bootorder >>= 4;
119 switch (val) {
120 case 1: DefaultFloppyPrio = i; break;
121 case 2: DefaultHDPrio = i; break;
122 case 3: DefaultCDPrio = i; break;
123 case 4: DefaultBEVPrio = i; break;
124 }
125 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500126 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200127
Kevin O'Connord1a17462010-12-24 11:17:03 -0500128 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500129}
130
Kevin O'Connord1a17462010-12-24 11:17:03 -0500131
132/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500133 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500134 ****************************************************************/
135
Kevin O'Connor031ef552010-12-27 19:26:57 -0500136struct bootentry_s {
137 int type;
138 union {
139 u32 data;
140 struct segoff_s vector;
141 struct drive_s *drive;
142 };
143 int priority;
144 const char *description;
145 struct bootentry_s *next;
146};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500147static struct bootentry_s *BootList;
148
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500149#define IPL_TYPE_FLOPPY 0x01
150#define IPL_TYPE_HARDDISK 0x02
151#define IPL_TYPE_CDROM 0x03
152#define IPL_TYPE_CBFS 0x20
153#define IPL_TYPE_BEV 0x80
154#define IPL_TYPE_BCV 0x81
155
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500156static void
157bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500158{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500159 if (! CONFIG_BOOT)
160 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500161 struct bootentry_s *be = malloc_tmp(sizeof(*be));
162 if (!be) {
163 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500164 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500165 }
166 be->type = type;
167 be->priority = prio;
168 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500169 be->description = desc ?: "?";
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500170
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500171 // Add entry in sorted order.
172 struct bootentry_s **pprev;
173 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
174 struct bootentry_s *pos = *pprev;
175 if (be->priority < pos->priority)
176 break;
177 if (be->priority > pos->priority)
178 continue;
179 if (be->type < pos->type)
180 break;
181 if (be->type > pos->type)
182 continue;
183 if (be->type <= IPL_TYPE_CDROM
184 && (be->drive->type < pos->drive->type
185 || (be->drive->type == pos->drive->type
186 && be->drive->cntl_id < pos->drive->cntl_id)))
187 break;
188 }
189 be->next = *pprev;
190 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500191}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500192
Kevin O'Connor031ef552010-12-27 19:26:57 -0500193// Return the given priority if it's set - defaultprio otherwise.
194static inline int defPrio(int priority, int defaultprio) {
195 return (priority < 0) ? defaultprio : priority;
196}
197
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500198// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200199void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500200boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200201{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500202 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
203 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500204 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
205 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200206}
207
Kevin O'Connor0a924122009-02-08 19:43:47 -0500208// Add a bcv entry for an expansion card harddrive or legacy option rom
209void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500210boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500211{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500212 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
213 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500214 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500215}
216
Kevin O'Connor0a924122009-02-08 19:43:47 -0500217void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500218boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500219{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500220 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500221 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500222}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500223
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500224void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500225boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500226{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500227 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500228 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500229}
230
231void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500232boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500233{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500234 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500235 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500236}
237
238// Add a CBFS payload entry
239void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500240boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500241{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500242 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500243}
244
245
246/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500247 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500248 ****************************************************************/
249
Kevin O'Connor0a924122009-02-08 19:43:47 -0500250// Show IPL option menu.
251static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500252interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500253{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400254 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500255 return;
256
257 while (get_keystroke(0) >= 0)
258 ;
259
260 printf("Press F12 for boot menu.\n\n");
261
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400262 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400263 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400264 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500265 if (scan_code != 0x86)
266 /* not F12 */
267 return;
268
269 while (get_keystroke(0) >= 0)
270 ;
271
272 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400273 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500274
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500275 // Show menu items
276 struct bootentry_s *pos = BootList;
277 int maxmenu = 0;
278 while (pos) {
279 char desc[60];
280 maxmenu++;
281 printf("%d. %s\n", maxmenu
282 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
283 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500284 }
285
Kevin O'Connor551caa22010-12-29 13:25:18 -0500286 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500287 for (;;) {
288 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500289 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500290 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500291 }
292 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500293 if (scan_code == 0x01)
294 // ESC
295 return;
296
297 // Find entry and make top priority.
298 int choice = scan_code - 1;
299 struct bootentry_s **pprev = &BootList;
300 while (--choice)
301 pprev = &(*pprev)->next;
302 pos = *pprev;
303 *pprev = pos->next;
304 pos->next = BootList;
305 BootList = pos;
306 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500307}
308
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500309// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500310struct bev_s {
311 int type;
312 u32 vector;
313};
314static struct bev_s BEV[20];
315static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500316static int HaveHDBoot, HaveFDBoot;
317
Kevin O'Connor0a924122009-02-08 19:43:47 -0500318static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500319add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500320{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500321 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
322 return;
323 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
324 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500325 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500326 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500327 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500328 bev->type = type;
329 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500330}
331
332// Prepare for boot - show menu and run bcvs.
333void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500334boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500335{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400336 if (! CONFIG_BOOT) {
337 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500338 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400339 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500340
Kevin O'Connora5826b52009-10-24 17:57:29 -0400341 // XXX - show available drives?
342
Kevin O'Connor0a924122009-02-08 19:43:47 -0500343 // Allow user to modify BCV/IPL order.
344 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400345 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500346
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500347 // Map drives and populate BEV list
348 struct bootentry_s *pos = BootList;
349 while (pos) {
350 switch (pos->type) {
351 case IPL_TYPE_BCV:
352 call_bcv(pos->vector.seg, pos->vector.offset);
353 add_bev(IPL_TYPE_HARDDISK, 0);
354 break;
355 case IPL_TYPE_FLOPPY:
356 map_floppy_drive(pos->drive);
357 add_bev(IPL_TYPE_FLOPPY, 0);
358 break;
359 case IPL_TYPE_HARDDISK:
360 map_hd_drive(pos->drive);
361 add_bev(IPL_TYPE_HARDDISK, 0);
362 break;
363 case IPL_TYPE_CDROM:
364 map_cd_drive(pos->drive);
365 // NO BREAK
366 default:
367 add_bev(pos->type, pos->data);
368 break;
369 }
370 pos = pos->next;
371 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400372
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500373 // If nothing added a floppy/hd boot - add it manually.
374 add_bev(IPL_TYPE_FLOPPY, 0);
375 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500376}
377
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500378
379/****************************************************************
380 * Boot code (int 18/19)
381 ****************************************************************/
382
Kevin O'Connor0a924122009-02-08 19:43:47 -0500383// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500384static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500385call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500386{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500387 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500388 struct bregs br;
389 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400390 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500391 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500392 // Set the magic number in ax and the boot drive in dl.
393 br.dl = bootdrv;
394 br.ax = 0xaa55;
395 call16(&br);
396}
397
398// Boot from a disk (either floppy or harddrive)
399static void
400boot_disk(u8 bootdrv, int checksig)
401{
402 u16 bootseg = 0x07c0;
403
404 // Read sector
405 struct bregs br;
406 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400407 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500408 br.dl = bootdrv;
409 br.es = bootseg;
410 br.ah = 2;
411 br.al = 1;
412 br.cl = 1;
413 call16_int(0x13, &br);
414
415 if (br.flags & F_CF) {
416 printf("Boot failed: could not read the boot disk\n\n");
417 return;
418 }
419
420 if (checksig) {
421 struct mbr_s *mbr = (void*)0;
422 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
423 printf("Boot failed: not a bootable disk\n\n");
424 return;
425 }
426 }
427
428 /* Canonicalize bootseg:bootip */
429 u16 bootip = (bootseg & 0x0fff) << 4;
430 bootseg &= 0xf000;
431
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500432 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500433}
434
435// Boot from a CD-ROM
436static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500437boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500438{
439 if (! CONFIG_CDROM_BOOT)
440 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500441 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200442
Gleb Natapov4c90a202010-12-07 13:50:54 +0200443 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500444 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500445 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500446 return;
447 }
448
449 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400450 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500451 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
452 /* Canonicalize bootseg:bootip */
453 u16 bootip = (bootseg & 0x0fff) << 4;
454 bootseg &= 0xf000;
455
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500456 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500457}
458
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400459// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400460static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500461boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400462{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400463 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400464 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500465 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500466 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400467}
468
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500469// Boot from a BEV entry on an optionrom.
470static void
471boot_rom(u32 vector)
472{
473 printf("Booting from ROM...\n");
474 struct segoff_s so;
475 so.segoff = vector;
476 call_boot_entry(so, 0);
477}
478
479// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500480static void
481do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500482{
Kevin O'Connor40967022008-07-21 22:23:05 -0400483 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500484 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400485
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500486 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400487 printf("No bootable device.\n");
488 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
489 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400490 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500491 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500492
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500493 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500494 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400495 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400496 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500497 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500498 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500499 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400500 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500501 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500502 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500503 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500504 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500505 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400506 break;
507 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500508 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500509 break;
510 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500511 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500512 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500513 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500514
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500515 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400516 struct bregs br;
517 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400518 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400519 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500520}
521
522// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500523void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500524handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500525{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400526 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400527 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500528 u16 ebda_seg = get_ebda_seg();
529 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
530 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400531 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500532}
533
534// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500535void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500536handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500537{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400538 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400539 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500540 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400541 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500542}