blob: 9c94439605a54579ecaec8e5de8d5b2c1b2136fd [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
Gleb Natapov4c90a202010-12-07 13:50:54 +020090// Add a IPL entry for BAID cdrom.
91void
92add_baid_cdrom(struct drive_s *drive_g)
93{
94 if (! CONFIG_CDROM_BOOT)
95 return;
96
97 /* put first cdrom into ipl 3 for compatability with qemu */
98 struct ipl_entry_s *ie = &IPL.bev[2];
99 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev) && ie->vector)
100 return;
101
102 if (ie->vector)
103 ie = &IPL.bev[IPL.bevcount++];
104 ie->type = IPL_TYPE_CDROM;
105 ie->vector = (u32)drive_g;
106 ie->description = "DVD/CD";
107}
108
Kevin O'Connor0a924122009-02-08 19:43:47 -0500109// Add a bcv entry for an expansion card harddrive or legacy option rom
110void
111add_bcv(u16 seg, u16 ip, u16 desc)
112{
113 if (! CONFIG_BOOT)
114 return;
115 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
116 return;
117
118 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400119 ie->type = BCV_TYPE_EXTERNAL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500120 ie->vector = (seg << 16) | ip;
121 const char *d = "Legacy option rom";
122 if (desc)
123 d = MAKE_FLATPTR(seg, desc);
124 ie->description = d;
125}
126
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400127// Add a bcv entry for an internal harddrive
Kevin O'Connor0a924122009-02-08 19:43:47 -0500128void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400129add_bcv_internal(struct drive_s *drive_g)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500130{
131 if (! CONFIG_BOOT)
132 return;
133 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
134 return;
135
136 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
Kevin O'Connora5826b52009-10-24 17:57:29 -0400137 if (CONFIG_THREADS) {
138 // Add to bcv list with assured drive order.
139 struct ipl_entry_s *end = ie;
140 for (;;) {
141 struct ipl_entry_s *prev = ie - 1;
142 if (prev < IPL.bcv || prev->type != BCV_TYPE_INTERNAL)
143 break;
144 struct drive_s *prevdrive = (void*)prev->vector;
145 if (prevdrive->type < drive_g->type
146 || (prevdrive->type == drive_g->type
147 && prevdrive->cntl_id < drive_g->cntl_id))
148 break;
149 ie--;
150 }
151 if (ie != end)
152 memmove(ie+1, ie, (void*)end-(void*)ie);
153 }
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400154 ie->type = BCV_TYPE_INTERNAL;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400155 ie->vector = (u32)drive_g;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400156 ie->description = "";
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500157}
158
159
160/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500161 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500162 ****************************************************************/
163
Kevin O'Connor0a924122009-02-08 19:43:47 -0500164// Show a generic menu item
165static int
166menu_show_default(struct ipl_entry_s *ie, int menupos)
167{
168 char desc[33];
169 printf("%d. %s\n", menupos
170 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
171 return 1;
172}
173
174// Show floppy menu item - but only if there exists a floppy drive.
175static int
176menu_show_floppy(struct ipl_entry_s *ie, int menupos)
177{
Kevin O'Connorafd05202009-08-16 12:21:44 -0400178 int i;
179 for (i = 0; i < Drives.floppycount; i++) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400180 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, i);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500181 printf("%d. Floppy [%s]\n", menupos + i, drive_g->desc);
Kevin O'Connorafd05202009-08-16 12:21:44 -0400182 }
183 return Drives.floppycount;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500184}
185
186// Show menu items from BCV list.
187static int
188menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
189{
190 int i;
191 for (i = 0; i < IPL.bcvcount; i++) {
192 struct ipl_entry_s *ie = &IPL.bcv[i];
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500193 struct drive_s *drive_g = (void*)ie->vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500194 switch (ie->type) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400195 case BCV_TYPE_INTERNAL:
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500196 printf("%d. %s\n", menupos + i, drive_g->desc);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500197 break;
198 default:
199 menu_show_default(ie, menupos+i);
200 break;
201 }
202 }
203 return IPL.bcvcount;
204}
205
206// Show cdrom menu item - but only if there exists a cdrom drive.
207static int
208menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
209{
Gleb Natapov4c90a202010-12-07 13:50:54 +0200210 struct drive_s *drive_g = (void*)ie->vector;
211 if (!ie->vector)
212 return 0;
213 printf("%d. DVD/CD [%s]\n", menupos, drive_g->desc);
214 return 1;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500215}
216
Kevin O'Connor67823442009-04-13 14:14:51 -0400217// Show coreboot-fs menu item.
218static int
219menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
220{
221 int count = 0;
Kevin O'Connor31ae6382009-09-20 15:42:39 -0400222 struct cbfs_file *file = NULL;
Kevin O'Connor67823442009-04-13 14:14:51 -0400223 for (;;) {
Kevin O'Connor1f836252009-08-16 20:17:35 -0400224 file = cbfs_findprefix("img/", file);
225 if (!file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400226 break;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400227 const char *filename = cbfs_filename(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400228 printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
229 count++;
230 if (count > 8)
231 break;
232 }
233 return count;
234}
235
Kevin O'Connor0a924122009-02-08 19:43:47 -0500236// Show IPL option menu.
237static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500238interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500239{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400240 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500241 return;
242
243 while (get_keystroke(0) >= 0)
244 ;
245
246 printf("Press F12 for boot menu.\n\n");
247
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400248 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400249 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400250 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500251 if (scan_code != 0x86)
252 /* not F12 */
253 return;
254
255 while (get_keystroke(0) >= 0)
256 ;
257
258 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400259 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500260
Kevin O'Connor0a924122009-02-08 19:43:47 -0500261 int subcount[ARRAY_SIZE(IPL.bev)];
262 int menupos = 1;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500263 int i;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500264 for (i = 0; i < IPL.bevcount; i++) {
265 struct ipl_entry_s *ie = &IPL.bev[i];
Kevin O'Connor31ae6382009-09-20 15:42:39 -0400266 int sc;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500267 switch (ie->type) {
268 case IPL_TYPE_FLOPPY:
269 sc = menu_show_floppy(ie, menupos);
270 break;
271 case IPL_TYPE_HARDDISK:
272 sc = menu_show_harddisk(ie, menupos);
273 break;
274 case IPL_TYPE_CDROM:
275 sc = menu_show_cdrom(ie, menupos);
276 break;
Kevin O'Connor67823442009-04-13 14:14:51 -0400277 case IPL_TYPE_CBFS:
278 sc = menu_show_cbfs(ie, menupos);
279 break;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500280 default:
281 sc = menu_show_default(ie, menupos);
282 break;
283 }
284 subcount[i] = sc;
285 menupos += sc;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500286 }
287
288 for (;;) {
289 scan_code = get_keystroke(1000);
290 if (scan_code == 0x01)
291 // ESC
292 break;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500293 if (scan_code < 1 || scan_code > menupos)
294 continue;
295 int choice = scan_code - 1;
296
297 // Find out which IPL this was for.
298 int bev = 0;
299 while (choice > subcount[bev]) {
300 choice -= subcount[bev];
301 bev++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500302 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400303 IPL.bev[bev].subchoice = choice-1;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500304
305 // Add user choice to the boot order.
306 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
307 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500308 }
309 printf("\n");
310}
311
Kevin O'Connor0a924122009-02-08 19:43:47 -0500312// Run the specified bcv.
313static void
314run_bcv(struct ipl_entry_s *ie)
315{
316 switch (ie->type) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400317 case BCV_TYPE_INTERNAL:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400318 map_hd_drive((void*)ie->vector);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500319 break;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400320 case BCV_TYPE_EXTERNAL:
Kevin O'Connor0a924122009-02-08 19:43:47 -0500321 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
322 break;
323 }
324}
325
326// Prepare for boot - show menu and run bcvs.
327void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500328boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500329{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400330 if (! CONFIG_BOOT) {
331 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500332 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400333 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500334
Kevin O'Connora5826b52009-10-24 17:57:29 -0400335 // XXX - show available drives?
336
Kevin O'Connor0a924122009-02-08 19:43:47 -0500337 // Allow user to modify BCV/IPL order.
338 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400339 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500340
Kevin O'Connorafd05202009-08-16 12:21:44 -0400341 // Setup floppy boot order
342 int override = IPL.bev[0].subchoice;
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500343 struct drive_s *tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
Kevin O'Connorafd05202009-08-16 12:21:44 -0400344 Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
345 Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
346
Kevin O'Connor0a924122009-02-08 19:43:47 -0500347 // Run BCVs
Kevin O'Connorafd05202009-08-16 12:21:44 -0400348 override = IPL.bev[1].subchoice;
Kevin O'Connor7da1dcd2009-02-16 10:51:24 -0500349 if (override < IPL.bcvcount)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500350 run_bcv(&IPL.bcv[override]);
351 int i;
352 for (i=0; i<IPL.bcvcount; i++)
353 if (i != override)
354 run_bcv(&IPL.bcv[i]);
355}
356
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500357
358/****************************************************************
359 * Boot code (int 18/19)
360 ****************************************************************/
361
Kevin O'Connor0a924122009-02-08 19:43:47 -0500362// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500363static void
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500364call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
365{
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400366 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500367 struct bregs br;
368 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400369 br.flags = F_IF;
Kevin O'Connor9f985422009-09-09 11:34:39 -0400370 br.code = SEGOFF(bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500371 // Set the magic number in ax and the boot drive in dl.
372 br.dl = bootdrv;
373 br.ax = 0xaa55;
374 call16(&br);
375}
376
377// Boot from a disk (either floppy or harddrive)
378static void
379boot_disk(u8 bootdrv, int checksig)
380{
381 u16 bootseg = 0x07c0;
382
383 // Read sector
384 struct bregs br;
385 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400386 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500387 br.dl = bootdrv;
388 br.es = bootseg;
389 br.ah = 2;
390 br.al = 1;
391 br.cl = 1;
392 call16_int(0x13, &br);
393
394 if (br.flags & F_CF) {
395 printf("Boot failed: could not read the boot disk\n\n");
396 return;
397 }
398
399 if (checksig) {
400 struct mbr_s *mbr = (void*)0;
401 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
402 printf("Boot failed: not a bootable disk\n\n");
403 return;
404 }
405 }
406
407 /* Canonicalize bootseg:bootip */
408 u16 bootip = (bootseg & 0x0fff) << 4;
409 bootseg &= 0xf000;
410
411 call_boot_entry(bootseg, bootip, bootdrv);
412}
413
414// Boot from a CD-ROM
415static void
Kevin O'Connor67823442009-04-13 14:14:51 -0400416boot_cdrom(struct ipl_entry_s *ie)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500417{
418 if (! CONFIG_CDROM_BOOT)
419 return;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200420
421 if (!ie->vector)
422 return;
423
424 struct drive_s *drive_g = (void*)ie->vector;
425 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500426 if (status) {
Gleb Natapov4c90a202010-12-07 13:50:54 +0200427 printf("Boot failed: Could not read from CDROM %s (code %04x)\n", drive_g->desc, status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500428 return;
429 }
430
431 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400432 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500433 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
434 /* Canonicalize bootseg:bootip */
435 u16 bootip = (bootseg & 0x0fff) << 4;
436 bootseg &= 0xf000;
437
438 call_boot_entry(bootseg, bootip, bootdrv);
439}
440
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400441// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400442static void
443boot_cbfs(struct ipl_entry_s *ie)
444{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400445 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400446 return;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400447 int count = ie->subchoice;
Kevin O'Connor2edace12009-12-13 11:19:01 -0500448 struct cbfs_file *file = NULL;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400449 for (;;) {
450 file = cbfs_findprefix("img/", file);
451 if (!file)
452 return;
453 if (count--)
454 continue;
455 cbfs_run_payload(file);
456 }
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'Connorc659fde2008-12-28 23:43:20 -0500465 u32 bootdev = IPL.bootorder;
Kevin O'Connor840c5342008-03-29 14:04:34 -0400466 bootdev >>= 4 * seq_nr;
467 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400468
Kevin O'Connorabc75972008-05-18 00:20:53 -0400469 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -0400470 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500471
Kevin O'Connor0a924122009-02-08 19:43:47 -0500472 if (bootdev >= IPL.bevcount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400473 printf("No bootable device.\n");
474 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
475 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400476 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500477 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500478
479 /* Do the loading, and set up vector as a far pointer to the boot
480 * address, and bootdrv as the boot drive */
Kevin O'Connor0a924122009-02-08 19:43:47 -0500481 struct ipl_entry_s *ie = &IPL.bev[bootdev];
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500482 char desc[33];
483 printf("Booting from %s...\n"
484 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500485
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400486 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400487 case IPL_TYPE_FLOPPY:
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500488 boot_disk(0x00, IPL.checkfloppysig);
489 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400490 case IPL_TYPE_HARDDISK:
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500491 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500492 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500493 case IPL_TYPE_CDROM:
Kevin O'Connor67823442009-04-13 14:14:51 -0400494 boot_cdrom(ie);
495 break;
496 case IPL_TYPE_CBFS:
497 boot_cbfs(ie);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500498 break;
499 case IPL_TYPE_BEV:
500 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500501 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500502 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500503
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500504 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400505 struct bregs br;
506 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400507 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400508 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500509}
510
511// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500512void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500513handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500514{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400515 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400516 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500517 u16 ebda_seg = get_ebda_seg();
518 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
519 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400520 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500521}
522
523// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500524void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500525handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500526{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400527 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400528 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500529 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400530 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500531}