blob: 5137345a418b7e5ff80e520e03ab4e64af72ef52 [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'Connord1a17462010-12-24 11:17:03 -050021 * Boot setup
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'Connor9f4e1d92009-02-08 15:44:08 -050059void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050060boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050061{
62 if (! CONFIG_BOOT)
63 return;
64 dprintf(3, "init boot device ordering\n");
65
66 memset(&IPL, 0, sizeof(IPL));
Stefan Reinauer203f6f32010-06-09 21:10:13 +020067 struct ipl_entry_s *ie = &IPL.bev[0];
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050068
69 // Floppy drive
Kevin O'Connor71f036d2009-02-08 16:57:22 -050070 ie->type = IPL_TYPE_FLOPPY;
71 ie->description = "Floppy";
72 ie++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050073
74 // First HDD
Kevin O'Connor71f036d2009-02-08 16:57:22 -050075 ie->type = IPL_TYPE_HARDDISK;
76 ie->description = "Hard Disk";
77 ie++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050078
79 // CDROM
80 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor71f036d2009-02-08 16:57:22 -050081 ie->type = IPL_TYPE_CDROM;
Stefan Reinauer203f6f32010-06-09 21:10:13 +020082 ie->description = "DVD/CD";
Kevin O'Connor71f036d2009-02-08 16:57:22 -050083 ie++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050084 }
85
Kevin O'Connord9c93612010-03-20 11:00:45 -040086 if (CONFIG_COREBOOT && CONFIG_COREBOOT_FLASH) {
Kevin O'Connor67823442009-04-13 14:14:51 -040087 ie->type = IPL_TYPE_CBFS;
88 ie->description = "CBFS";
89 ie++;
90 }
91
Kevin O'Connor0a924122009-02-08 19:43:47 -050092 IPL.bevcount = ie - IPL.bev;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050093 SET_EBDA(boot_sequence, 0xffff);
94 if (CONFIG_COREBOOT) {
95 // XXX - hardcode defaults for coreboot.
Kevin O'Connorff8c1412009-04-18 17:02:41 -040096 IPL.bootorder = 0x87654231;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050097 IPL.checkfloppysig = 1;
98 } else {
99 // On emulators, get boot order from nvram.
100 IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
101 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
102 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
103 IPL.checkfloppysig = 1;
104 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200105
Kevin O'Connord1a17462010-12-24 11:17:03 -0500106 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500107}
108
Kevin O'Connord1a17462010-12-24 11:17:03 -0500109
110/****************************************************************
111 * IPL and BCV handlers
112 ****************************************************************/
113
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500114// Add a BEV vector for a given pnp compatible option rom.
115void
116add_bev(u16 seg, u16 bev, u16 desc)
117{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500118 if (! CONFIG_BOOT)
119 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500120 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500121 return;
122
Kevin O'Connor0a924122009-02-08 19:43:47 -0500123 struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500124 ie->type = IPL_TYPE_BEV;
125 ie->vector = (seg << 16) | bev;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500126 const char *d = "Unknown";
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500127 if (desc)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500128 d = MAKE_FLATPTR(seg, desc);
129 ie->description = d;
130}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500131
Gleb Natapov4c90a202010-12-07 13:50:54 +0200132// Add a IPL entry for BAID cdrom.
133void
134add_baid_cdrom(struct drive_s *drive_g)
135{
136 if (! CONFIG_CDROM_BOOT)
137 return;
138
139 /* put first cdrom into ipl 3 for compatability with qemu */
140 struct ipl_entry_s *ie = &IPL.bev[2];
141 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev) && ie->vector)
142 return;
143
144 if (ie->vector)
145 ie = &IPL.bev[IPL.bevcount++];
146 ie->type = IPL_TYPE_CDROM;
147 ie->vector = (u32)drive_g;
148 ie->description = "DVD/CD";
149}
150
Kevin O'Connor0a924122009-02-08 19:43:47 -0500151// Add a bcv entry for an expansion card harddrive or legacy option rom
152void
153add_bcv(u16 seg, u16 ip, u16 desc)
154{
155 if (! CONFIG_BOOT)
156 return;
157 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
158 return;
159
160 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400161 ie->type = BCV_TYPE_EXTERNAL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500162 ie->vector = (seg << 16) | ip;
163 const char *d = "Legacy option rom";
164 if (desc)
165 d = MAKE_FLATPTR(seg, desc);
166 ie->description = d;
167}
168
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400169// Add a bcv entry for an internal harddrive
Kevin O'Connor0a924122009-02-08 19:43:47 -0500170void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400171add_bcv_internal(struct drive_s *drive_g)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500172{
173 if (! CONFIG_BOOT)
174 return;
175 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
176 return;
177
178 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
Kevin O'Connora5826b52009-10-24 17:57:29 -0400179 if (CONFIG_THREADS) {
180 // Add to bcv list with assured drive order.
181 struct ipl_entry_s *end = ie;
182 for (;;) {
183 struct ipl_entry_s *prev = ie - 1;
184 if (prev < IPL.bcv || prev->type != BCV_TYPE_INTERNAL)
185 break;
186 struct drive_s *prevdrive = (void*)prev->vector;
187 if (prevdrive->type < drive_g->type
188 || (prevdrive->type == drive_g->type
189 && prevdrive->cntl_id < drive_g->cntl_id))
190 break;
191 ie--;
192 }
193 if (ie != end)
194 memmove(ie+1, ie, (void*)end-(void*)ie);
195 }
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400196 ie->type = BCV_TYPE_INTERNAL;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400197 ie->vector = (u32)drive_g;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400198 ie->description = "";
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500199}
200
201
202/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500203 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500204 ****************************************************************/
205
Kevin O'Connor0a924122009-02-08 19:43:47 -0500206// Show a generic menu item
207static int
208menu_show_default(struct ipl_entry_s *ie, int menupos)
209{
210 char desc[33];
211 printf("%d. %s\n", menupos
212 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
213 return 1;
214}
215
216// Show floppy menu item - but only if there exists a floppy drive.
217static int
218menu_show_floppy(struct ipl_entry_s *ie, int menupos)
219{
Kevin O'Connorafd05202009-08-16 12:21:44 -0400220 int i;
221 for (i = 0; i < Drives.floppycount; i++) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400222 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, i);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500223 printf("%d. Floppy [%s]\n", menupos + i, drive_g->desc);
Kevin O'Connorafd05202009-08-16 12:21:44 -0400224 }
225 return Drives.floppycount;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500226}
227
228// Show menu items from BCV list.
229static int
230menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
231{
232 int i;
233 for (i = 0; i < IPL.bcvcount; i++) {
234 struct ipl_entry_s *ie = &IPL.bcv[i];
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500235 struct drive_s *drive_g = (void*)ie->vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500236 switch (ie->type) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400237 case BCV_TYPE_INTERNAL:
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500238 printf("%d. %s\n", menupos + i, drive_g->desc);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500239 break;
240 default:
241 menu_show_default(ie, menupos+i);
242 break;
243 }
244 }
245 return IPL.bcvcount;
246}
247
248// Show cdrom menu item - but only if there exists a cdrom drive.
249static int
250menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
251{
Gleb Natapov4c90a202010-12-07 13:50:54 +0200252 struct drive_s *drive_g = (void*)ie->vector;
253 if (!ie->vector)
254 return 0;
255 printf("%d. DVD/CD [%s]\n", menupos, drive_g->desc);
256 return 1;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500257}
258
Kevin O'Connor67823442009-04-13 14:14:51 -0400259// Show coreboot-fs menu item.
260static int
261menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
262{
263 int count = 0;
Kevin O'Connor31ae6382009-09-20 15:42:39 -0400264 struct cbfs_file *file = NULL;
Kevin O'Connor67823442009-04-13 14:14:51 -0400265 for (;;) {
Kevin O'Connor1f836252009-08-16 20:17:35 -0400266 file = cbfs_findprefix("img/", file);
267 if (!file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400268 break;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400269 const char *filename = cbfs_filename(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400270 printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
271 count++;
272 if (count > 8)
273 break;
274 }
275 return count;
276}
277
Kevin O'Connor0a924122009-02-08 19:43:47 -0500278// Show IPL option menu.
279static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500280interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500281{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400282 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500283 return;
284
285 while (get_keystroke(0) >= 0)
286 ;
287
288 printf("Press F12 for boot menu.\n\n");
289
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400290 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400291 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400292 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500293 if (scan_code != 0x86)
294 /* not F12 */
295 return;
296
297 while (get_keystroke(0) >= 0)
298 ;
299
300 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400301 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500302
Kevin O'Connor0a924122009-02-08 19:43:47 -0500303 int subcount[ARRAY_SIZE(IPL.bev)];
304 int menupos = 1;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500305 int i;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500306 for (i = 0; i < IPL.bevcount; i++) {
307 struct ipl_entry_s *ie = &IPL.bev[i];
Kevin O'Connor31ae6382009-09-20 15:42:39 -0400308 int sc;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500309 switch (ie->type) {
310 case IPL_TYPE_FLOPPY:
311 sc = menu_show_floppy(ie, menupos);
312 break;
313 case IPL_TYPE_HARDDISK:
314 sc = menu_show_harddisk(ie, menupos);
315 break;
316 case IPL_TYPE_CDROM:
317 sc = menu_show_cdrom(ie, menupos);
318 break;
Kevin O'Connor67823442009-04-13 14:14:51 -0400319 case IPL_TYPE_CBFS:
320 sc = menu_show_cbfs(ie, menupos);
321 break;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500322 default:
323 sc = menu_show_default(ie, menupos);
324 break;
325 }
326 subcount[i] = sc;
327 menupos += sc;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500328 }
329
330 for (;;) {
331 scan_code = get_keystroke(1000);
332 if (scan_code == 0x01)
333 // ESC
334 break;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500335 if (scan_code < 1 || scan_code > menupos)
336 continue;
337 int choice = scan_code - 1;
338
339 // Find out which IPL this was for.
340 int bev = 0;
341 while (choice > subcount[bev]) {
342 choice -= subcount[bev];
343 bev++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500344 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400345 IPL.bev[bev].subchoice = choice-1;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500346
347 // Add user choice to the boot order.
348 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
349 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500350 }
351 printf("\n");
352}
353
Kevin O'Connor0a924122009-02-08 19:43:47 -0500354// Run the specified bcv.
355static void
356run_bcv(struct ipl_entry_s *ie)
357{
358 switch (ie->type) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400359 case BCV_TYPE_INTERNAL:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400360 map_hd_drive((void*)ie->vector);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500361 break;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400362 case BCV_TYPE_EXTERNAL:
Kevin O'Connor0a924122009-02-08 19:43:47 -0500363 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
364 break;
365 }
366}
367
368// Prepare for boot - show menu and run bcvs.
369void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500370boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500371{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400372 if (! CONFIG_BOOT) {
373 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500374 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400375 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500376
Kevin O'Connora5826b52009-10-24 17:57:29 -0400377 // XXX - show available drives?
378
Kevin O'Connor0a924122009-02-08 19:43:47 -0500379 // Allow user to modify BCV/IPL order.
380 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400381 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500382
Kevin O'Connorafd05202009-08-16 12:21:44 -0400383 // Setup floppy boot order
384 int override = IPL.bev[0].subchoice;
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500385 struct drive_s *tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
Kevin O'Connorafd05202009-08-16 12:21:44 -0400386 Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
387 Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
388
Kevin O'Connor0a924122009-02-08 19:43:47 -0500389 // Run BCVs
Kevin O'Connorafd05202009-08-16 12:21:44 -0400390 override = IPL.bev[1].subchoice;
Kevin O'Connor7da1dcd2009-02-16 10:51:24 -0500391 if (override < IPL.bcvcount)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500392 run_bcv(&IPL.bcv[override]);
393 int i;
394 for (i=0; i<IPL.bcvcount; i++)
395 if (i != override)
396 run_bcv(&IPL.bcv[i]);
397}
398
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500399
400/****************************************************************
401 * Boot code (int 18/19)
402 ****************************************************************/
403
Kevin O'Connor0a924122009-02-08 19:43:47 -0500404// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500405static void
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500406call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
407{
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400408 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500409 struct bregs br;
410 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400411 br.flags = F_IF;
Kevin O'Connor9f985422009-09-09 11:34:39 -0400412 br.code = SEGOFF(bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500413 // Set the magic number in ax and the boot drive in dl.
414 br.dl = bootdrv;
415 br.ax = 0xaa55;
416 call16(&br);
417}
418
419// Boot from a disk (either floppy or harddrive)
420static void
421boot_disk(u8 bootdrv, int checksig)
422{
423 u16 bootseg = 0x07c0;
424
425 // Read sector
426 struct bregs br;
427 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400428 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500429 br.dl = bootdrv;
430 br.es = bootseg;
431 br.ah = 2;
432 br.al = 1;
433 br.cl = 1;
434 call16_int(0x13, &br);
435
436 if (br.flags & F_CF) {
437 printf("Boot failed: could not read the boot disk\n\n");
438 return;
439 }
440
441 if (checksig) {
442 struct mbr_s *mbr = (void*)0;
443 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
444 printf("Boot failed: not a bootable disk\n\n");
445 return;
446 }
447 }
448
449 /* Canonicalize bootseg:bootip */
450 u16 bootip = (bootseg & 0x0fff) << 4;
451 bootseg &= 0xf000;
452
453 call_boot_entry(bootseg, bootip, bootdrv);
454}
455
456// Boot from a CD-ROM
457static void
Kevin O'Connor67823442009-04-13 14:14:51 -0400458boot_cdrom(struct ipl_entry_s *ie)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500459{
460 if (! CONFIG_CDROM_BOOT)
461 return;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200462
463 if (!ie->vector)
464 return;
465
466 struct drive_s *drive_g = (void*)ie->vector;
467 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500468 if (status) {
Gleb Natapov4c90a202010-12-07 13:50:54 +0200469 printf("Boot failed: Could not read from CDROM %s (code %04x)\n", drive_g->desc, status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500470 return;
471 }
472
473 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400474 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500475 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
476 /* Canonicalize bootseg:bootip */
477 u16 bootip = (bootseg & 0x0fff) << 4;
478 bootseg &= 0xf000;
479
480 call_boot_entry(bootseg, bootip, bootdrv);
481}
482
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400483// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400484static void
485boot_cbfs(struct ipl_entry_s *ie)
486{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400487 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400488 return;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400489 int count = ie->subchoice;
Kevin O'Connor2edace12009-12-13 11:19:01 -0500490 struct cbfs_file *file = NULL;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400491 for (;;) {
492 file = cbfs_findprefix("img/", file);
493 if (!file)
494 return;
495 if (count--)
496 continue;
497 cbfs_run_payload(file);
498 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400499}
500
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500501static void
502do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500503{
Kevin O'Connor40967022008-07-21 22:23:05 -0400504 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500505 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400506
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500507 u32 bootdev = IPL.bootorder;
Kevin O'Connor840c5342008-03-29 14:04:34 -0400508 bootdev >>= 4 * seq_nr;
509 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400510
Kevin O'Connorabc75972008-05-18 00:20:53 -0400511 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -0400512 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500513
Kevin O'Connor0a924122009-02-08 19:43:47 -0500514 if (bootdev >= IPL.bevcount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400515 printf("No bootable device.\n");
516 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
517 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400518 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500519 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500520
521 /* Do the loading, and set up vector as a far pointer to the boot
522 * address, and bootdrv as the boot drive */
Kevin O'Connor0a924122009-02-08 19:43:47 -0500523 struct ipl_entry_s *ie = &IPL.bev[bootdev];
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500524 char desc[33];
525 printf("Booting from %s...\n"
526 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500527
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400528 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400529 case IPL_TYPE_FLOPPY:
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500530 boot_disk(0x00, IPL.checkfloppysig);
531 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400532 case IPL_TYPE_HARDDISK:
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500533 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500534 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500535 case IPL_TYPE_CDROM:
Kevin O'Connor67823442009-04-13 14:14:51 -0400536 boot_cdrom(ie);
537 break;
538 case IPL_TYPE_CBFS:
539 boot_cbfs(ie);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500540 break;
541 case IPL_TYPE_BEV:
542 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500543 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500544 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500545
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500546 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400547 struct bregs br;
548 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400549 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400550 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500551}
552
553// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500554void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500555handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500556{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400557 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400558 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500559 u16 ebda_seg = get_ebda_seg();
560 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
561 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400562 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500563}
564
565// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500566void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500567handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500568{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400569 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400570 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500571 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400572 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500573}