blob: 9a395c6793df4a1390117bc851ea693742281d19 [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
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;
169 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'Connor031ef552010-12-27 19:26:57 -0500218boot_add_floppy(struct drive_s *drive_g, 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)
221 , (u32)drive_g, 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'Connor031ef552010-12-27 19:26:57 -0500225boot_add_hd(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_HARDDISK, defPrio(prio, DefaultHDPrio)
228 , (u32)drive_g, drive_g->desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500229}
230
231void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500232boot_add_cd(struct drive_s *drive_g, 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)
235 , (u32)drive_g, 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'Connor7bb15842010-12-29 11:25:14 -0500309struct bev_s {
310 int type;
311 u32 vector;
312};
313static struct bev_s BEV[20];
314static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500315static int HaveHDBoot, HaveFDBoot;
316
Kevin O'Connor0a924122009-02-08 19:43:47 -0500317static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500318add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500319{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500320 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
321 return;
322 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
323 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500324 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500325 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500326 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500327 bev->type = type;
328 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500329}
330
331// Prepare for boot - show menu and run bcvs.
332void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500333boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500334{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400335 if (! CONFIG_BOOT) {
336 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500337 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400338 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500339
Kevin O'Connora5826b52009-10-24 17:57:29 -0400340 // XXX - show available drives?
341
Kevin O'Connor0a924122009-02-08 19:43:47 -0500342 // Allow user to modify BCV/IPL order.
343 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400344 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500345
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500346 // Map drives and populate BEV list
347 struct bootentry_s *pos = BootList;
348 while (pos) {
349 switch (pos->type) {
350 case IPL_TYPE_BCV:
351 call_bcv(pos->vector.seg, pos->vector.offset);
352 add_bev(IPL_TYPE_HARDDISK, 0);
353 break;
354 case IPL_TYPE_FLOPPY:
355 map_floppy_drive(pos->drive);
356 add_bev(IPL_TYPE_FLOPPY, 0);
357 break;
358 case IPL_TYPE_HARDDISK:
359 map_hd_drive(pos->drive);
360 add_bev(IPL_TYPE_HARDDISK, 0);
361 break;
362 case IPL_TYPE_CDROM:
363 map_cd_drive(pos->drive);
364 // NO BREAK
365 default:
366 add_bev(pos->type, pos->data);
367 break;
368 }
369 pos = pos->next;
370 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400371
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500372 // If nothing added a floppy/hd boot - add it manually.
373 add_bev(IPL_TYPE_FLOPPY, 0);
374 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500375}
376
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500377
378/****************************************************************
379 * Boot code (int 18/19)
380 ****************************************************************/
381
Kevin O'Connor0a924122009-02-08 19:43:47 -0500382// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500383static void
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500384call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
385{
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400386 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500387 struct bregs br;
388 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400389 br.flags = F_IF;
Kevin O'Connor9f985422009-09-09 11:34:39 -0400390 br.code = SEGOFF(bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500391 // Set the magic number in ax and the boot drive in dl.
392 br.dl = bootdrv;
393 br.ax = 0xaa55;
394 call16(&br);
395}
396
397// Boot from a disk (either floppy or harddrive)
398static void
399boot_disk(u8 bootdrv, int checksig)
400{
401 u16 bootseg = 0x07c0;
402
403 // Read sector
404 struct bregs br;
405 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400406 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500407 br.dl = bootdrv;
408 br.es = bootseg;
409 br.ah = 2;
410 br.al = 1;
411 br.cl = 1;
412 call16_int(0x13, &br);
413
414 if (br.flags & F_CF) {
415 printf("Boot failed: could not read the boot disk\n\n");
416 return;
417 }
418
419 if (checksig) {
420 struct mbr_s *mbr = (void*)0;
421 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
422 printf("Boot failed: not a bootable disk\n\n");
423 return;
424 }
425 }
426
427 /* Canonicalize bootseg:bootip */
428 u16 bootip = (bootseg & 0x0fff) << 4;
429 bootseg &= 0xf000;
430
431 call_boot_entry(bootseg, bootip, bootdrv);
432}
433
434// Boot from a CD-ROM
435static void
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500436boot_cdrom(struct bev_s *ie)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500437{
438 if (! CONFIG_CDROM_BOOT)
439 return;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200440
441 if (!ie->vector)
442 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500443 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200444
445 struct drive_s *drive_g = (void*)ie->vector;
446 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500447 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500448 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500449 return;
450 }
451
452 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400453 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500454 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
455 /* Canonicalize bootseg:bootip */
456 u16 bootip = (bootseg & 0x0fff) << 4;
457 bootseg &= 0xf000;
458
459 call_boot_entry(bootseg, bootip, bootdrv);
460}
461
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400462// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400463static void
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500464boot_cbfs(struct bev_s *ie)
Kevin O'Connor67823442009-04-13 14:14:51 -0400465{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400466 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400467 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500468 printf("Booting from CBFS...\n");
469 cbfs_run_payload((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400470}
471
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500472static void
473do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500474{
Kevin O'Connor40967022008-07-21 22:23:05 -0400475 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500476 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400477
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500478 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400479 printf("No bootable device.\n");
480 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
481 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400482 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500483 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500484
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500485 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500486 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400487 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400488 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500489 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500490 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500491 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400492 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500493 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500494 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500495 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500496 case IPL_TYPE_CDROM:
Kevin O'Connor67823442009-04-13 14:14:51 -0400497 boot_cdrom(ie);
498 break;
499 case IPL_TYPE_CBFS:
500 boot_cbfs(ie);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500501 break;
502 case IPL_TYPE_BEV:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500503 printf("Booting from ROM...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500504 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500505 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500506 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500507
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500508 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400509 struct bregs br;
510 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400511 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400512 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500513}
514
515// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500516void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500517handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500518{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400519 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400520 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500521 u16 ebda_seg = get_ebda_seg();
522 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
523 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400524 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500525}
526
527// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500528void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500529handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500530{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400531 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400532 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500533 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400534 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500535}