blob: 021b8ac070170be378c558f8d0f76e436a44ad3a [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'Connor0a924122009-02-08 19:43:47 -050021 * IPL and BCV handlers
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050022 ****************************************************************/
23
24void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050025boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050026{
27 if (! CONFIG_BOOT)
28 return;
29 dprintf(3, "init boot device ordering\n");
30
31 memset(&IPL, 0, sizeof(IPL));
Stefan Reinauer203f6f32010-06-09 21:10:13 +020032 struct ipl_entry_s *ie = &IPL.bev[0];
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050033
34 // Floppy drive
Kevin O'Connor71f036d2009-02-08 16:57:22 -050035 ie->type = IPL_TYPE_FLOPPY;
36 ie->description = "Floppy";
37 ie++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050038
39 // First HDD
Kevin O'Connor71f036d2009-02-08 16:57:22 -050040 ie->type = IPL_TYPE_HARDDISK;
41 ie->description = "Hard Disk";
42 ie++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050043
44 // CDROM
45 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor71f036d2009-02-08 16:57:22 -050046 ie->type = IPL_TYPE_CDROM;
Stefan Reinauer203f6f32010-06-09 21:10:13 +020047 ie->description = "DVD/CD";
Kevin O'Connor71f036d2009-02-08 16:57:22 -050048 ie++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050049 }
50
Kevin O'Connord9c93612010-03-20 11:00:45 -040051 if (CONFIG_COREBOOT && CONFIG_COREBOOT_FLASH) {
Kevin O'Connor67823442009-04-13 14:14:51 -040052 ie->type = IPL_TYPE_CBFS;
53 ie->description = "CBFS";
54 ie++;
55 }
56
Kevin O'Connor0a924122009-02-08 19:43:47 -050057 IPL.bevcount = ie - IPL.bev;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050058 SET_EBDA(boot_sequence, 0xffff);
59 if (CONFIG_COREBOOT) {
60 // XXX - hardcode defaults for coreboot.
Kevin O'Connorff8c1412009-04-18 17:02:41 -040061 IPL.bootorder = 0x87654231;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050062 IPL.checkfloppysig = 1;
63 } else {
64 // On emulators, get boot order from nvram.
65 IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
66 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
67 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
68 IPL.checkfloppysig = 1;
69 }
70}
71
72// Add a BEV vector for a given pnp compatible option rom.
73void
74add_bev(u16 seg, u16 bev, u16 desc)
75{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050076 if (! CONFIG_BOOT)
77 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -050078 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050079 return;
80
Kevin O'Connor0a924122009-02-08 19:43:47 -050081 struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
Kevin O'Connor71f036d2009-02-08 16:57:22 -050082 ie->type = IPL_TYPE_BEV;
83 ie->vector = (seg << 16) | bev;
Kevin O'Connor0a924122009-02-08 19:43:47 -050084 const char *d = "Unknown";
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050085 if (desc)
Kevin O'Connor0a924122009-02-08 19:43:47 -050086 d = MAKE_FLATPTR(seg, desc);
87 ie->description = d;
88}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050089
Kevin O'Connor0a924122009-02-08 19:43:47 -050090// Add a bcv entry for an expansion card harddrive or legacy option rom
91void
92add_bcv(u16 seg, u16 ip, u16 desc)
93{
94 if (! CONFIG_BOOT)
95 return;
96 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
97 return;
98
99 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400100 ie->type = BCV_TYPE_EXTERNAL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500101 ie->vector = (seg << 16) | ip;
102 const char *d = "Legacy option rom";
103 if (desc)
104 d = MAKE_FLATPTR(seg, desc);
105 ie->description = d;
106}
107
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400108// Add a bcv entry for an internal harddrive
Kevin O'Connor0a924122009-02-08 19:43:47 -0500109void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400110add_bcv_internal(struct drive_s *drive_g)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500111{
112 if (! CONFIG_BOOT)
113 return;
114 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
115 return;
116
117 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
Kevin O'Connora5826b52009-10-24 17:57:29 -0400118 if (CONFIG_THREADS) {
119 // Add to bcv list with assured drive order.
120 struct ipl_entry_s *end = ie;
121 for (;;) {
122 struct ipl_entry_s *prev = ie - 1;
123 if (prev < IPL.bcv || prev->type != BCV_TYPE_INTERNAL)
124 break;
125 struct drive_s *prevdrive = (void*)prev->vector;
126 if (prevdrive->type < drive_g->type
127 || (prevdrive->type == drive_g->type
128 && prevdrive->cntl_id < drive_g->cntl_id))
129 break;
130 ie--;
131 }
132 if (ie != end)
133 memmove(ie+1, ie, (void*)end-(void*)ie);
134 }
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400135 ie->type = BCV_TYPE_INTERNAL;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400136 ie->vector = (u32)drive_g;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400137 ie->description = "";
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500138}
139
140
141/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500142 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500143 ****************************************************************/
144
Kevin O'Connor0a924122009-02-08 19:43:47 -0500145// Show a generic menu item
146static int
147menu_show_default(struct ipl_entry_s *ie, int menupos)
148{
149 char desc[33];
150 printf("%d. %s\n", menupos
151 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
152 return 1;
153}
154
155// Show floppy menu item - but only if there exists a floppy drive.
156static int
157menu_show_floppy(struct ipl_entry_s *ie, int menupos)
158{
Kevin O'Connorafd05202009-08-16 12:21:44 -0400159 int i;
160 for (i = 0; i < Drives.floppycount; i++) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400161 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, i);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500162 printf("%d. Floppy [%s]\n", menupos + i, drive_g->desc);
Kevin O'Connorafd05202009-08-16 12:21:44 -0400163 }
164 return Drives.floppycount;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500165}
166
167// Show menu items from BCV list.
168static int
169menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
170{
171 int i;
172 for (i = 0; i < IPL.bcvcount; i++) {
173 struct ipl_entry_s *ie = &IPL.bcv[i];
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500174 struct drive_s *drive_g = (void*)ie->vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500175 switch (ie->type) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400176 case BCV_TYPE_INTERNAL:
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500177 printf("%d. %s\n", menupos + i, drive_g->desc);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500178 break;
179 default:
180 menu_show_default(ie, menupos+i);
181 break;
182 }
183 }
184 return IPL.bcvcount;
185}
186
187// Show cdrom menu item - but only if there exists a cdrom drive.
188static int
189menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
190{
Kevin O'Connor7da1dcd2009-02-16 10:51:24 -0500191 int i;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400192 for (i = 0; i < Drives.cdcount; i++) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400193 struct drive_s *drive_g = getDrive(EXTTYPE_CD, i);
Stefan Reinauer203f6f32010-06-09 21:10:13 +0200194 printf("%d. DVD/CD [%s]\n", menupos + i, drive_g->desc);
Kevin O'Connor7da1dcd2009-02-16 10:51:24 -0500195 }
Kevin O'Connorc892b132009-08-11 21:59:37 -0400196 return Drives.cdcount;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500197}
198
Kevin O'Connor67823442009-04-13 14:14:51 -0400199// Show coreboot-fs menu item.
200static int
201menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
202{
203 int count = 0;
Kevin O'Connor31ae6382009-09-20 15:42:39 -0400204 struct cbfs_file *file = NULL;
Kevin O'Connor67823442009-04-13 14:14:51 -0400205 for (;;) {
Kevin O'Connor1f836252009-08-16 20:17:35 -0400206 file = cbfs_findprefix("img/", file);
207 if (!file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400208 break;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400209 const char *filename = cbfs_filename(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400210 printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
211 count++;
212 if (count > 8)
213 break;
214 }
215 return count;
216}
217
Kevin O'Connor0a924122009-02-08 19:43:47 -0500218// Show IPL option menu.
219static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500220interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500221{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400222 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500223 return;
224
225 while (get_keystroke(0) >= 0)
226 ;
227
228 printf("Press F12 for boot menu.\n\n");
229
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400230 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400231 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400232 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500233 if (scan_code != 0x86)
234 /* not F12 */
235 return;
236
237 while (get_keystroke(0) >= 0)
238 ;
239
240 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400241 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500242
Kevin O'Connor0a924122009-02-08 19:43:47 -0500243 int subcount[ARRAY_SIZE(IPL.bev)];
244 int menupos = 1;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500245 int i;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500246 for (i = 0; i < IPL.bevcount; i++) {
247 struct ipl_entry_s *ie = &IPL.bev[i];
Kevin O'Connor31ae6382009-09-20 15:42:39 -0400248 int sc;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500249 switch (ie->type) {
250 case IPL_TYPE_FLOPPY:
251 sc = menu_show_floppy(ie, menupos);
252 break;
253 case IPL_TYPE_HARDDISK:
254 sc = menu_show_harddisk(ie, menupos);
255 break;
256 case IPL_TYPE_CDROM:
257 sc = menu_show_cdrom(ie, menupos);
258 break;
Kevin O'Connor67823442009-04-13 14:14:51 -0400259 case IPL_TYPE_CBFS:
260 sc = menu_show_cbfs(ie, menupos);
261 break;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500262 default:
263 sc = menu_show_default(ie, menupos);
264 break;
265 }
266 subcount[i] = sc;
267 menupos += sc;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500268 }
269
270 for (;;) {
271 scan_code = get_keystroke(1000);
272 if (scan_code == 0x01)
273 // ESC
274 break;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500275 if (scan_code < 1 || scan_code > menupos)
276 continue;
277 int choice = scan_code - 1;
278
279 // Find out which IPL this was for.
280 int bev = 0;
281 while (choice > subcount[bev]) {
282 choice -= subcount[bev];
283 bev++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500284 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400285 IPL.bev[bev].subchoice = choice-1;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500286
287 // Add user choice to the boot order.
288 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
289 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500290 }
291 printf("\n");
292}
293
Kevin O'Connor0a924122009-02-08 19:43:47 -0500294// Run the specified bcv.
295static void
296run_bcv(struct ipl_entry_s *ie)
297{
298 switch (ie->type) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400299 case BCV_TYPE_INTERNAL:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400300 map_hd_drive((void*)ie->vector);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500301 break;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400302 case BCV_TYPE_EXTERNAL:
Kevin O'Connor0a924122009-02-08 19:43:47 -0500303 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
304 break;
305 }
306}
307
308// Prepare for boot - show menu and run bcvs.
309void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500310boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500311{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400312 if (! CONFIG_BOOT) {
313 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500314 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400315 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500316
Kevin O'Connora5826b52009-10-24 17:57:29 -0400317 // XXX - show available drives?
318
Kevin O'Connor0a924122009-02-08 19:43:47 -0500319 // Allow user to modify BCV/IPL order.
320 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400321 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500322
Kevin O'Connorafd05202009-08-16 12:21:44 -0400323 // Setup floppy boot order
324 int override = IPL.bev[0].subchoice;
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500325 struct drive_s *tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
Kevin O'Connorafd05202009-08-16 12:21:44 -0400326 Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
327 Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
328
Kevin O'Connor0a924122009-02-08 19:43:47 -0500329 // Run BCVs
Kevin O'Connorafd05202009-08-16 12:21:44 -0400330 override = IPL.bev[1].subchoice;
Kevin O'Connor7da1dcd2009-02-16 10:51:24 -0500331 if (override < IPL.bcvcount)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500332 run_bcv(&IPL.bcv[override]);
333 int i;
334 for (i=0; i<IPL.bcvcount; i++)
335 if (i != override)
336 run_bcv(&IPL.bcv[i]);
337}
338
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500339
340/****************************************************************
341 * Boot code (int 18/19)
342 ****************************************************************/
343
Kevin O'Connor0a924122009-02-08 19:43:47 -0500344// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500345static void
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500346call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
347{
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400348 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500349 struct bregs br;
350 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400351 br.flags = F_IF;
Kevin O'Connor9f985422009-09-09 11:34:39 -0400352 br.code = SEGOFF(bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500353 // Set the magic number in ax and the boot drive in dl.
354 br.dl = bootdrv;
355 br.ax = 0xaa55;
356 call16(&br);
357}
358
359// Boot from a disk (either floppy or harddrive)
360static void
361boot_disk(u8 bootdrv, int checksig)
362{
363 u16 bootseg = 0x07c0;
364
365 // Read sector
366 struct bregs br;
367 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400368 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500369 br.dl = bootdrv;
370 br.es = bootseg;
371 br.ah = 2;
372 br.al = 1;
373 br.cl = 1;
374 call16_int(0x13, &br);
375
376 if (br.flags & F_CF) {
377 printf("Boot failed: could not read the boot disk\n\n");
378 return;
379 }
380
381 if (checksig) {
382 struct mbr_s *mbr = (void*)0;
383 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
384 printf("Boot failed: not a bootable disk\n\n");
385 return;
386 }
387 }
388
389 /* Canonicalize bootseg:bootip */
390 u16 bootip = (bootseg & 0x0fff) << 4;
391 bootseg &= 0xf000;
392
393 call_boot_entry(bootseg, bootip, bootdrv);
394}
395
396// Boot from a CD-ROM
397static void
Kevin O'Connor67823442009-04-13 14:14:51 -0400398boot_cdrom(struct ipl_entry_s *ie)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500399{
400 if (! CONFIG_CDROM_BOOT)
401 return;
Kevin O'Connor67823442009-04-13 14:14:51 -0400402 int status = cdrom_boot(ie->subchoice);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500403 if (status) {
404 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
405 return;
406 }
407
408 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400409 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500410 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
411 /* Canonicalize bootseg:bootip */
412 u16 bootip = (bootseg & 0x0fff) << 4;
413 bootseg &= 0xf000;
414
415 call_boot_entry(bootseg, bootip, bootdrv);
416}
417
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400418// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400419static void
420boot_cbfs(struct ipl_entry_s *ie)
421{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400422 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400423 return;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400424 int count = ie->subchoice;
Kevin O'Connor2edace12009-12-13 11:19:01 -0500425 struct cbfs_file *file = NULL;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400426 for (;;) {
427 file = cbfs_findprefix("img/", file);
428 if (!file)
429 return;
430 if (count--)
431 continue;
432 cbfs_run_payload(file);
433 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400434}
435
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500436static void
437do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500438{
Kevin O'Connor40967022008-07-21 22:23:05 -0400439 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500440 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400441
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500442 u32 bootdev = IPL.bootorder;
Kevin O'Connor840c5342008-03-29 14:04:34 -0400443 bootdev >>= 4 * seq_nr;
444 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400445
Kevin O'Connorabc75972008-05-18 00:20:53 -0400446 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -0400447 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500448
Kevin O'Connor0a924122009-02-08 19:43:47 -0500449 if (bootdev >= IPL.bevcount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400450 printf("No bootable device.\n");
451 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
452 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400453 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500454 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500455
456 /* Do the loading, and set up vector as a far pointer to the boot
457 * address, and bootdrv as the boot drive */
Kevin O'Connor0a924122009-02-08 19:43:47 -0500458 struct ipl_entry_s *ie = &IPL.bev[bootdev];
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500459 char desc[33];
460 printf("Booting from %s...\n"
461 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500462
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400463 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400464 case IPL_TYPE_FLOPPY:
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500465 boot_disk(0x00, IPL.checkfloppysig);
466 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400467 case IPL_TYPE_HARDDISK:
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500468 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500469 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500470 case IPL_TYPE_CDROM:
Kevin O'Connor67823442009-04-13 14:14:51 -0400471 boot_cdrom(ie);
472 break;
473 case IPL_TYPE_CBFS:
474 boot_cbfs(ie);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500475 break;
476 case IPL_TYPE_BEV:
477 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500478 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500479 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500480
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500481 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400482 struct bregs br;
483 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400484 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400485 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500486}
487
488// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500489void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500490handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500491{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400492 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400493 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500494 u16 ebda_seg = get_ebda_seg();
495 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
496 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400497 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500498}
499
500// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500501void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500502handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500503{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400504 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400505 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500506 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400507 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500508}