blob: 4fb73ac9261dbf6bfb70e07527ed6645631fcb8b [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 }
Gleb Natapovb9a75912010-12-23 11:29:38 +020070
Kevin O'Connorf9b09302010-12-24 11:15:26 -050071 char *f = romfile_loadfile("bootorder", NULL);
72 if (!f)
Gleb Natapovb9a75912010-12-23 11:29:38 +020073 return;
74
Gleb Natapovb9a75912010-12-23 11:29:38 +020075 int i;
76 IPL.fw_bootorder_count = 1;
77 while(f[i]) {
78 if (f[i] == '\n')
79 IPL.fw_bootorder_count++;
80 i++;
81 }
82 IPL.fw_bootorder = malloc_tmphigh(IPL.fw_bootorder_count*sizeof(char*));
83 if (!IPL.fw_bootorder) {
84 warn_noalloc();
85 free(f);
86 return;
87 }
88
89 dprintf(3, "boot order:\n");
90 i = 0;
91 do {
92 IPL.fw_bootorder[i] = f;
93 f = strchr(f, '\n');
94 if (f) {
95 *f = '\0';
96 f++;
97 dprintf(3, "%d: %s\n", i, IPL.fw_bootorder[i]);
98 i++;
99 }
100 } while(f);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500101}
102
103// Add a BEV vector for a given pnp compatible option rom.
104void
105add_bev(u16 seg, u16 bev, u16 desc)
106{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500107 if (! CONFIG_BOOT)
108 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500109 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500110 return;
111
Kevin O'Connor0a924122009-02-08 19:43:47 -0500112 struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++];
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500113 ie->type = IPL_TYPE_BEV;
114 ie->vector = (seg << 16) | bev;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500115 const char *d = "Unknown";
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500116 if (desc)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500117 d = MAKE_FLATPTR(seg, desc);
118 ie->description = d;
119}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500120
Gleb Natapov4c90a202010-12-07 13:50:54 +0200121// Add a IPL entry for BAID cdrom.
122void
123add_baid_cdrom(struct drive_s *drive_g)
124{
125 if (! CONFIG_CDROM_BOOT)
126 return;
127
128 /* put first cdrom into ipl 3 for compatability with qemu */
129 struct ipl_entry_s *ie = &IPL.bev[2];
130 if (IPL.bevcount >= ARRAY_SIZE(IPL.bev) && ie->vector)
131 return;
132
133 if (ie->vector)
134 ie = &IPL.bev[IPL.bevcount++];
135 ie->type = IPL_TYPE_CDROM;
136 ie->vector = (u32)drive_g;
137 ie->description = "DVD/CD";
138}
139
Kevin O'Connor0a924122009-02-08 19:43:47 -0500140// Add a bcv entry for an expansion card harddrive or legacy option rom
141void
142add_bcv(u16 seg, u16 ip, u16 desc)
143{
144 if (! CONFIG_BOOT)
145 return;
146 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
147 return;
148
149 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400150 ie->type = BCV_TYPE_EXTERNAL;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500151 ie->vector = (seg << 16) | ip;
152 const char *d = "Legacy option rom";
153 if (desc)
154 d = MAKE_FLATPTR(seg, desc);
155 ie->description = d;
156}
157
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400158// Add a bcv entry for an internal harddrive
Kevin O'Connor0a924122009-02-08 19:43:47 -0500159void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400160add_bcv_internal(struct drive_s *drive_g)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500161{
162 if (! CONFIG_BOOT)
163 return;
164 if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv))
165 return;
166
167 struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++];
Kevin O'Connora5826b52009-10-24 17:57:29 -0400168 if (CONFIG_THREADS) {
169 // Add to bcv list with assured drive order.
170 struct ipl_entry_s *end = ie;
171 for (;;) {
172 struct ipl_entry_s *prev = ie - 1;
173 if (prev < IPL.bcv || prev->type != BCV_TYPE_INTERNAL)
174 break;
175 struct drive_s *prevdrive = (void*)prev->vector;
176 if (prevdrive->type < drive_g->type
177 || (prevdrive->type == drive_g->type
178 && prevdrive->cntl_id < drive_g->cntl_id))
179 break;
180 ie--;
181 }
182 if (ie != end)
183 memmove(ie+1, ie, (void*)end-(void*)ie);
184 }
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400185 ie->type = BCV_TYPE_INTERNAL;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400186 ie->vector = (u32)drive_g;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400187 ie->description = "";
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500188}
189
190
191/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500192 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500193 ****************************************************************/
194
Kevin O'Connor0a924122009-02-08 19:43:47 -0500195// Show a generic menu item
196static int
197menu_show_default(struct ipl_entry_s *ie, int menupos)
198{
199 char desc[33];
200 printf("%d. %s\n", menupos
201 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
202 return 1;
203}
204
205// Show floppy menu item - but only if there exists a floppy drive.
206static int
207menu_show_floppy(struct ipl_entry_s *ie, int menupos)
208{
Kevin O'Connorafd05202009-08-16 12:21:44 -0400209 int i;
210 for (i = 0; i < Drives.floppycount; i++) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400211 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, i);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500212 printf("%d. Floppy [%s]\n", menupos + i, drive_g->desc);
Kevin O'Connorafd05202009-08-16 12:21:44 -0400213 }
214 return Drives.floppycount;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500215}
216
217// Show menu items from BCV list.
218static int
219menu_show_harddisk(struct ipl_entry_s *ie, int menupos)
220{
221 int i;
222 for (i = 0; i < IPL.bcvcount; i++) {
223 struct ipl_entry_s *ie = &IPL.bcv[i];
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500224 struct drive_s *drive_g = (void*)ie->vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500225 switch (ie->type) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400226 case BCV_TYPE_INTERNAL:
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500227 printf("%d. %s\n", menupos + i, drive_g->desc);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500228 break;
229 default:
230 menu_show_default(ie, menupos+i);
231 break;
232 }
233 }
234 return IPL.bcvcount;
235}
236
237// Show cdrom menu item - but only if there exists a cdrom drive.
238static int
239menu_show_cdrom(struct ipl_entry_s *ie, int menupos)
240{
Gleb Natapov4c90a202010-12-07 13:50:54 +0200241 struct drive_s *drive_g = (void*)ie->vector;
242 if (!ie->vector)
243 return 0;
244 printf("%d. DVD/CD [%s]\n", menupos, drive_g->desc);
245 return 1;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500246}
247
Kevin O'Connor67823442009-04-13 14:14:51 -0400248// Show coreboot-fs menu item.
249static int
250menu_show_cbfs(struct ipl_entry_s *ie, int menupos)
251{
252 int count = 0;
Kevin O'Connor31ae6382009-09-20 15:42:39 -0400253 struct cbfs_file *file = NULL;
Kevin O'Connor67823442009-04-13 14:14:51 -0400254 for (;;) {
Kevin O'Connor1f836252009-08-16 20:17:35 -0400255 file = cbfs_findprefix("img/", file);
256 if (!file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400257 break;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400258 const char *filename = cbfs_filename(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400259 printf("%d. Payload [%s]\n", menupos + count, &filename[4]);
260 count++;
261 if (count > 8)
262 break;
263 }
264 return count;
265}
266
Kevin O'Connor0a924122009-02-08 19:43:47 -0500267// Show IPL option menu.
268static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500269interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500270{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400271 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500272 return;
273
274 while (get_keystroke(0) >= 0)
275 ;
276
277 printf("Press F12 for boot menu.\n\n");
278
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400279 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400280 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400281 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500282 if (scan_code != 0x86)
283 /* not F12 */
284 return;
285
286 while (get_keystroke(0) >= 0)
287 ;
288
289 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400290 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500291
Kevin O'Connor0a924122009-02-08 19:43:47 -0500292 int subcount[ARRAY_SIZE(IPL.bev)];
293 int menupos = 1;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500294 int i;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500295 for (i = 0; i < IPL.bevcount; i++) {
296 struct ipl_entry_s *ie = &IPL.bev[i];
Kevin O'Connor31ae6382009-09-20 15:42:39 -0400297 int sc;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500298 switch (ie->type) {
299 case IPL_TYPE_FLOPPY:
300 sc = menu_show_floppy(ie, menupos);
301 break;
302 case IPL_TYPE_HARDDISK:
303 sc = menu_show_harddisk(ie, menupos);
304 break;
305 case IPL_TYPE_CDROM:
306 sc = menu_show_cdrom(ie, menupos);
307 break;
Kevin O'Connor67823442009-04-13 14:14:51 -0400308 case IPL_TYPE_CBFS:
309 sc = menu_show_cbfs(ie, menupos);
310 break;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500311 default:
312 sc = menu_show_default(ie, menupos);
313 break;
314 }
315 subcount[i] = sc;
316 menupos += sc;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500317 }
318
319 for (;;) {
320 scan_code = get_keystroke(1000);
321 if (scan_code == 0x01)
322 // ESC
323 break;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500324 if (scan_code < 1 || scan_code > menupos)
325 continue;
326 int choice = scan_code - 1;
327
328 // Find out which IPL this was for.
329 int bev = 0;
330 while (choice > subcount[bev]) {
331 choice -= subcount[bev];
332 bev++;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500333 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400334 IPL.bev[bev].subchoice = choice-1;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500335
336 // Add user choice to the boot order.
337 IPL.bootorder = (IPL.bootorder << 4) | (bev+1);
338 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500339 }
340 printf("\n");
341}
342
Kevin O'Connor0a924122009-02-08 19:43:47 -0500343// Run the specified bcv.
344static void
345run_bcv(struct ipl_entry_s *ie)
346{
347 switch (ie->type) {
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400348 case BCV_TYPE_INTERNAL:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400349 map_hd_drive((void*)ie->vector);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500350 break;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400351 case BCV_TYPE_EXTERNAL:
Kevin O'Connor0a924122009-02-08 19:43:47 -0500352 call_bcv(ie->vector >> 16, ie->vector & 0xffff);
353 break;
354 }
355}
356
357// Prepare for boot - show menu and run bcvs.
358void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500359boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500360{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400361 if (! CONFIG_BOOT) {
362 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500363 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400364 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500365
Kevin O'Connora5826b52009-10-24 17:57:29 -0400366 // XXX - show available drives?
367
Kevin O'Connor0a924122009-02-08 19:43:47 -0500368 // Allow user to modify BCV/IPL order.
369 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400370 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500371
Kevin O'Connorafd05202009-08-16 12:21:44 -0400372 // Setup floppy boot order
373 int override = IPL.bev[0].subchoice;
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500374 struct drive_s *tmp = Drives.idmap[EXTTYPE_FLOPPY][0];
Kevin O'Connorafd05202009-08-16 12:21:44 -0400375 Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override];
376 Drives.idmap[EXTTYPE_FLOPPY][override] = tmp;
377
Kevin O'Connor0a924122009-02-08 19:43:47 -0500378 // Run BCVs
Kevin O'Connorafd05202009-08-16 12:21:44 -0400379 override = IPL.bev[1].subchoice;
Kevin O'Connor7da1dcd2009-02-16 10:51:24 -0500380 if (override < IPL.bcvcount)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500381 run_bcv(&IPL.bcv[override]);
382 int i;
383 for (i=0; i<IPL.bcvcount; i++)
384 if (i != override)
385 run_bcv(&IPL.bcv[i]);
386}
387
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500388
389/****************************************************************
390 * Boot code (int 18/19)
391 ****************************************************************/
392
Kevin O'Connor0a924122009-02-08 19:43:47 -0500393// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500394static void
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500395call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv)
396{
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400397 dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500398 struct bregs br;
399 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400400 br.flags = F_IF;
Kevin O'Connor9f985422009-09-09 11:34:39 -0400401 br.code = SEGOFF(bootseg, bootip);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500402 // Set the magic number in ax and the boot drive in dl.
403 br.dl = bootdrv;
404 br.ax = 0xaa55;
405 call16(&br);
406}
407
408// Boot from a disk (either floppy or harddrive)
409static void
410boot_disk(u8 bootdrv, int checksig)
411{
412 u16 bootseg = 0x07c0;
413
414 // Read sector
415 struct bregs br;
416 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400417 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500418 br.dl = bootdrv;
419 br.es = bootseg;
420 br.ah = 2;
421 br.al = 1;
422 br.cl = 1;
423 call16_int(0x13, &br);
424
425 if (br.flags & F_CF) {
426 printf("Boot failed: could not read the boot disk\n\n");
427 return;
428 }
429
430 if (checksig) {
431 struct mbr_s *mbr = (void*)0;
432 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
433 printf("Boot failed: not a bootable disk\n\n");
434 return;
435 }
436 }
437
438 /* Canonicalize bootseg:bootip */
439 u16 bootip = (bootseg & 0x0fff) << 4;
440 bootseg &= 0xf000;
441
442 call_boot_entry(bootseg, bootip, bootdrv);
443}
444
445// Boot from a CD-ROM
446static void
Kevin O'Connor67823442009-04-13 14:14:51 -0400447boot_cdrom(struct ipl_entry_s *ie)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500448{
449 if (! CONFIG_CDROM_BOOT)
450 return;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200451
452 if (!ie->vector)
453 return;
454
455 struct drive_s *drive_g = (void*)ie->vector;
456 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500457 if (status) {
Gleb Natapov4c90a202010-12-07 13:50:54 +0200458 printf("Boot failed: Could not read from CDROM %s (code %04x)\n", drive_g->desc, status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500459 return;
460 }
461
462 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400463 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500464 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
465 /* Canonicalize bootseg:bootip */
466 u16 bootip = (bootseg & 0x0fff) << 4;
467 bootseg &= 0xf000;
468
469 call_boot_entry(bootseg, bootip, bootdrv);
470}
471
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400472// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400473static void
474boot_cbfs(struct ipl_entry_s *ie)
475{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400476 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400477 return;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400478 int count = ie->subchoice;
Kevin O'Connor2edace12009-12-13 11:19:01 -0500479 struct cbfs_file *file = NULL;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400480 for (;;) {
481 file = cbfs_findprefix("img/", file);
482 if (!file)
483 return;
484 if (count--)
485 continue;
486 cbfs_run_payload(file);
487 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400488}
489
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500490static void
491do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500492{
Kevin O'Connor40967022008-07-21 22:23:05 -0400493 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500494 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400495
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500496 u32 bootdev = IPL.bootorder;
Kevin O'Connor840c5342008-03-29 14:04:34 -0400497 bootdev >>= 4 * seq_nr;
498 bootdev &= 0xf;
Kevin O'Connore0113c92008-04-05 15:51:12 -0400499
Kevin O'Connorabc75972008-05-18 00:20:53 -0400500 /* Translate bootdev to an IPL table offset by subtracting 1 */
Kevin O'Connor840c5342008-03-29 14:04:34 -0400501 bootdev -= 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500502
Kevin O'Connor0a924122009-02-08 19:43:47 -0500503 if (bootdev >= IPL.bevcount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400504 printf("No bootable device.\n");
505 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
506 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400507 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500508 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500509
510 /* Do the loading, and set up vector as a far pointer to the boot
511 * address, and bootdrv as the boot drive */
Kevin O'Connor0a924122009-02-08 19:43:47 -0500512 struct ipl_entry_s *ie = &IPL.bev[bootdev];
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500513 char desc[33];
514 printf("Booting from %s...\n"
515 , strtcpy(desc, ie->description, ARRAY_SIZE(desc)));
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500516
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400517 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400518 case IPL_TYPE_FLOPPY:
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500519 boot_disk(0x00, IPL.checkfloppysig);
520 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400521 case IPL_TYPE_HARDDISK:
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500522 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500523 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500524 case IPL_TYPE_CDROM:
Kevin O'Connor67823442009-04-13 14:14:51 -0400525 boot_cdrom(ie);
526 break;
527 case IPL_TYPE_CBFS:
528 boot_cbfs(ie);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500529 break;
530 case IPL_TYPE_BEV:
531 call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500532 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500533 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500534
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500535 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400536 struct bregs br;
537 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400538 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400539 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500540}
541
542// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500543void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500544handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500545{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400546 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400547 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500548 u16 ebda_seg = get_ebda_seg();
549 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
550 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400551 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500552}
553
554// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500555void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500556handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500557{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400558 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400559 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500560 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400561 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500562}