blob: e5475a2f2f75e98eff75951fb4615d39d7f4a49d [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'Connorf13a1802010-12-29 11:52:33 -050013#include "boot.h" // func defs
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050014#include "cmos.h" // inb_cmos
Kevin O'Connorf13a1802010-12-29 11:52:33 -050015#include "paravirt.h" // romfile_loadfile
Kevin O'Connor8bf55032011-01-01 11:00:42 -050016#include "pci.h" //pci_bdf_to_*
Kevin O'Connorc659fde2008-12-28 23:43:20 -050017
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050018
19/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050020 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050021 ****************************************************************/
22
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050023static char **Bootorder;
24static int BootorderCount;
25
Kevin O'Connord1a17462010-12-24 11:17:03 -050026static void
27loadBootOrder(void)
28{
29 char *f = romfile_loadfile("bootorder", NULL);
30 if (!f)
31 return;
32
Kevin O'Connor8bf55032011-01-01 11:00:42 -050033 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050034 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050035 while (f[i]) {
36 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050037 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050038 i++;
39 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050040 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
41 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050042 warn_noalloc();
43 free(f);
44 return;
45 }
46
47 dprintf(3, "boot order:\n");
48 i = 0;
49 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050050 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050051 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050052 if (f)
53 *(f++) = '\0';
54 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
55 i++;
56 } while (f);
57}
58
59// See if 'str' starts with 'glob' - if glob contains an '*' character
60// it will match any number of characters in str that aren't a '/' or
61// the next glob character.
62static char *
63glob_prefix(const char *glob, const char *str)
64{
65 for (;;) {
66 if (!*glob && (!*str || *str == '/'))
67 return (char*)str;
68 if (*glob == '*') {
69 if (!*str || *str == '/' || *str == glob[1])
70 glob++;
71 else
72 str++;
73 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050074 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050075 if (*glob != *str)
76 return NULL;
77 glob++;
78 str++;
79 }
80}
81
82// Search the bootorder list for the given glob pattern.
83static int
84find_prio(const char *glob)
85{
86 dprintf(1, "Searching bootorder for: %s\n", glob);
87 int i;
88 for (i = 0; i < BootorderCount; i++)
89 if (glob_prefix(glob, Bootorder[i]))
90 return i+1;
91 return -1;
92}
93
94#define FW_PCI_DOMAIN "/pci@i0cf8"
95
96static char *
97build_pci_path(char *buf, int max, const char *devname, int bdf)
98{
99 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
100 char *p = buf;
101 int bus = pci_bdf_to_bus(bdf);
102 if (bus)
103 // XXX - this isn't the correct path syntax
104 p += snprintf(p, max, "/bus%x", bus);
105
106 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
107 p += snprintf(p, buf+max-p, "%s/%s@%x", FW_PCI_DOMAIN, devname, dev);
108 if (fn)
109 p += snprintf(p, buf+max-p, ",%x", fn);
110 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500111}
112
Kevin O'Connor031ef552010-12-27 19:26:57 -0500113int bootprio_find_pci_device(int bdf)
114{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500115 // Find pci device - for example: /pci@i0cf8/ethernet@5
116 char desc[256];
117 build_pci_path(desc, sizeof(desc), "*", bdf);
118 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500119}
120
121int bootprio_find_ata_device(int bdf, int chanid, int slave)
122{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500123 if (bdf == -1)
124 // support only pci machine for now
125 return -1;
126 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
127 char desc[256], *p;
128 p = build_pci_path(desc, sizeof(desc), "*", bdf);
129 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
130 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500131}
132
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500133int bootprio_find_fdc_device(int bdf, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500134{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500135 if (bdf == -1)
136 // support only pci machine for now
137 return -1;
138 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
139 char desc[256], *p;
140 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
141 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
142 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500143}
144
145int bootprio_find_pci_rom(int bdf, int instance)
146{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500147 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
148 char desc[256], *p;
149 p = build_pci_path(desc, sizeof(desc), "*", bdf);
150 if (instance)
151 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
152 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500153}
154
155int bootprio_find_named_rom(const char *name, int instance)
156{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500157 // Find named rom - for example: /rom@genroms/linuxboot.bin
158 char desc[256], *p;
159 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
160 if (instance)
161 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
162 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500163}
164
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500165int bootprio_find_usb(int bdf, u64 path)
166{
167 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
168 int i;
169 char desc[256], *p;
170 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
171 for (i=56; i>0; i-=8) {
172 int port = (path >> i) & 0xff;
173 if (port != 0xff)
174 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
175 }
176 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
177 return find_prio(desc);
178}
179
Kevin O'Connor031ef552010-12-27 19:26:57 -0500180
181/****************************************************************
182 * Boot setup
183 ****************************************************************/
184
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500185static int CheckFloppySig = 1;
186
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500187#define DEFAULT_PRIO 9999
188
189static int DefaultFloppyPrio = 101;
190static int DefaultCDPrio = 102;
191static int DefaultHDPrio = 103;
192static int DefaultBEVPrio = 104;
193
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500194void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500195boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500196{
197 if (! CONFIG_BOOT)
198 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500199
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500200 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500201
202 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500203 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500204 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500205 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500206 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500207 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500208 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
209 = DefaultBEVPrio = DEFAULT_PRIO;
210 int i;
211 for (i=101; i<104; i++) {
212 u32 val = bootorder & 0x0f;
213 bootorder >>= 4;
214 switch (val) {
215 case 1: DefaultFloppyPrio = i; break;
216 case 2: DefaultHDPrio = i; break;
217 case 3: DefaultCDPrio = i; break;
218 case 4: DefaultBEVPrio = i; break;
219 }
220 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500221 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200222
Kevin O'Connord1a17462010-12-24 11:17:03 -0500223 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500224}
225
Kevin O'Connord1a17462010-12-24 11:17:03 -0500226
227/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500228 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500229 ****************************************************************/
230
Kevin O'Connor031ef552010-12-27 19:26:57 -0500231struct bootentry_s {
232 int type;
233 union {
234 u32 data;
235 struct segoff_s vector;
236 struct drive_s *drive;
237 };
238 int priority;
239 const char *description;
240 struct bootentry_s *next;
241};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500242static struct bootentry_s *BootList;
243
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500244#define IPL_TYPE_FLOPPY 0x01
245#define IPL_TYPE_HARDDISK 0x02
246#define IPL_TYPE_CDROM 0x03
247#define IPL_TYPE_CBFS 0x20
248#define IPL_TYPE_BEV 0x80
249#define IPL_TYPE_BCV 0x81
250
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500251static void
252bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500253{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500254 if (! CONFIG_BOOT)
255 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500256 struct bootentry_s *be = malloc_tmp(sizeof(*be));
257 if (!be) {
258 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500259 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500260 }
261 be->type = type;
262 be->priority = prio;
263 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500264 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500265 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
266 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500267
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500268 // Add entry in sorted order.
269 struct bootentry_s **pprev;
270 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
271 struct bootentry_s *pos = *pprev;
272 if (be->priority < pos->priority)
273 break;
274 if (be->priority > pos->priority)
275 continue;
276 if (be->type < pos->type)
277 break;
278 if (be->type > pos->type)
279 continue;
280 if (be->type <= IPL_TYPE_CDROM
281 && (be->drive->type < pos->drive->type
282 || (be->drive->type == pos->drive->type
283 && be->drive->cntl_id < pos->drive->cntl_id)))
284 break;
285 }
286 be->next = *pprev;
287 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500288}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500289
Kevin O'Connor031ef552010-12-27 19:26:57 -0500290// Return the given priority if it's set - defaultprio otherwise.
291static inline int defPrio(int priority, int defaultprio) {
292 return (priority < 0) ? defaultprio : priority;
293}
294
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500295// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200296void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500297boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200298{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500299 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
300 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500301 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
302 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200303}
304
Kevin O'Connor0a924122009-02-08 19:43:47 -0500305// Add a bcv entry for an expansion card harddrive or legacy option rom
306void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500307boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500308{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500309 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
310 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500311 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500312}
313
Kevin O'Connor0a924122009-02-08 19:43:47 -0500314void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500315boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500316{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500317 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500318 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500319}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500320
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500321void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500322boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500323{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500324 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500325 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500326}
327
328void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500329boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500330{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500331 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500332 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500333}
334
335// Add a CBFS payload entry
336void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500337boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500338{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500339 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500340}
341
342
343/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500344 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500345 ****************************************************************/
346
Kevin O'Connor0a924122009-02-08 19:43:47 -0500347// Show IPL option menu.
348static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500349interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500350{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400351 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500352 return;
353
354 while (get_keystroke(0) >= 0)
355 ;
356
357 printf("Press F12 for boot menu.\n\n");
358
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400359 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400360 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400361 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500362 if (scan_code != 0x86)
363 /* not F12 */
364 return;
365
366 while (get_keystroke(0) >= 0)
367 ;
368
369 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400370 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500371
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500372 // Show menu items
373 struct bootentry_s *pos = BootList;
374 int maxmenu = 0;
375 while (pos) {
376 char desc[60];
377 maxmenu++;
378 printf("%d. %s\n", maxmenu
379 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
380 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500381 }
382
Kevin O'Connor551caa22010-12-29 13:25:18 -0500383 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500384 for (;;) {
385 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500386 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500387 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500388 }
389 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500390 if (scan_code == 0x01)
391 // ESC
392 return;
393
394 // Find entry and make top priority.
395 int choice = scan_code - 1;
396 struct bootentry_s **pprev = &BootList;
397 while (--choice)
398 pprev = &(*pprev)->next;
399 pos = *pprev;
400 *pprev = pos->next;
401 pos->next = BootList;
402 BootList = pos;
403 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500404}
405
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500406// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500407struct bev_s {
408 int type;
409 u32 vector;
410};
411static struct bev_s BEV[20];
412static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500413static int HaveHDBoot, HaveFDBoot;
414
Kevin O'Connor0a924122009-02-08 19:43:47 -0500415static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500416add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500417{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500418 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
419 return;
420 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
421 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500422 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500423 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500424 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500425 bev->type = type;
426 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500427}
428
429// Prepare for boot - show menu and run bcvs.
430void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500431boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500432{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400433 if (! CONFIG_BOOT) {
434 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500435 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400436 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500437
Kevin O'Connora5826b52009-10-24 17:57:29 -0400438 // XXX - show available drives?
439
Kevin O'Connor0a924122009-02-08 19:43:47 -0500440 // Allow user to modify BCV/IPL order.
441 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400442 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500443
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500444 // Map drives and populate BEV list
445 struct bootentry_s *pos = BootList;
446 while (pos) {
447 switch (pos->type) {
448 case IPL_TYPE_BCV:
449 call_bcv(pos->vector.seg, pos->vector.offset);
450 add_bev(IPL_TYPE_HARDDISK, 0);
451 break;
452 case IPL_TYPE_FLOPPY:
453 map_floppy_drive(pos->drive);
454 add_bev(IPL_TYPE_FLOPPY, 0);
455 break;
456 case IPL_TYPE_HARDDISK:
457 map_hd_drive(pos->drive);
458 add_bev(IPL_TYPE_HARDDISK, 0);
459 break;
460 case IPL_TYPE_CDROM:
461 map_cd_drive(pos->drive);
462 // NO BREAK
463 default:
464 add_bev(pos->type, pos->data);
465 break;
466 }
467 pos = pos->next;
468 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400469
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500470 // If nothing added a floppy/hd boot - add it manually.
471 add_bev(IPL_TYPE_FLOPPY, 0);
472 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500473}
474
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500475
476/****************************************************************
477 * Boot code (int 18/19)
478 ****************************************************************/
479
Kevin O'Connor0a924122009-02-08 19:43:47 -0500480// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500481static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500482call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500483{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500484 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500485 struct bregs br;
486 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400487 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500488 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500489 // Set the magic number in ax and the boot drive in dl.
490 br.dl = bootdrv;
491 br.ax = 0xaa55;
492 call16(&br);
493}
494
495// Boot from a disk (either floppy or harddrive)
496static void
497boot_disk(u8 bootdrv, int checksig)
498{
499 u16 bootseg = 0x07c0;
500
501 // Read sector
502 struct bregs br;
503 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400504 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500505 br.dl = bootdrv;
506 br.es = bootseg;
507 br.ah = 2;
508 br.al = 1;
509 br.cl = 1;
510 call16_int(0x13, &br);
511
512 if (br.flags & F_CF) {
513 printf("Boot failed: could not read the boot disk\n\n");
514 return;
515 }
516
517 if (checksig) {
518 struct mbr_s *mbr = (void*)0;
519 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
520 printf("Boot failed: not a bootable disk\n\n");
521 return;
522 }
523 }
524
525 /* Canonicalize bootseg:bootip */
526 u16 bootip = (bootseg & 0x0fff) << 4;
527 bootseg &= 0xf000;
528
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500529 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500530}
531
532// Boot from a CD-ROM
533static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500534boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500535{
536 if (! CONFIG_CDROM_BOOT)
537 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500538 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200539
Gleb Natapov4c90a202010-12-07 13:50:54 +0200540 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500541 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500542 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500543 return;
544 }
545
546 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400547 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500548 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
549 /* Canonicalize bootseg:bootip */
550 u16 bootip = (bootseg & 0x0fff) << 4;
551 bootseg &= 0xf000;
552
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500553 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500554}
555
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400556// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400557static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500558boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400559{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400560 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400561 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500562 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500563 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400564}
565
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500566// Boot from a BEV entry on an optionrom.
567static void
568boot_rom(u32 vector)
569{
570 printf("Booting from ROM...\n");
571 struct segoff_s so;
572 so.segoff = vector;
573 call_boot_entry(so, 0);
574}
575
576// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500577static void
578do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500579{
Kevin O'Connor40967022008-07-21 22:23:05 -0400580 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500581 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400582
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500583 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400584 printf("No bootable device.\n");
585 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
586 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400587 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500588 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500589
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500590 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500591 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400592 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400593 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500594 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500595 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500596 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400597 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500598 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500599 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500600 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500601 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500602 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400603 break;
604 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500605 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500606 break;
607 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500608 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500609 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500610 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500611
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500612 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400613 struct bregs br;
614 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400615 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400616 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500617}
618
619// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500620void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500621handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500622{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400623 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400624 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500625 u16 ebda_seg = get_ebda_seg();
626 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
627 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400628 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500629}
630
631// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500632void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500633handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500634{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400635 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400636 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500637 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400638 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500639}