blob: f3c165caaf9cc25891f4d2472eb91f95d371abe6 [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{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040029 if (!CONFIG_BOOTORDER)
30 return;
31
Kevin O'Connord1a17462010-12-24 11:17:03 -050032 char *f = romfile_loadfile("bootorder", NULL);
33 if (!f)
34 return;
35
Kevin O'Connor8bf55032011-01-01 11:00:42 -050036 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050037 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050038 while (f[i]) {
39 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050040 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050041 i++;
42 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050043 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
44 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050045 warn_noalloc();
46 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050047 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050048 return;
49 }
50
51 dprintf(3, "boot order:\n");
52 i = 0;
53 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050054 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050055 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050056 if (f)
57 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050058 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050059 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
60 i++;
61 } while (f);
62}
63
64// See if 'str' starts with 'glob' - if glob contains an '*' character
65// it will match any number of characters in str that aren't a '/' or
66// the next glob character.
67static char *
68glob_prefix(const char *glob, const char *str)
69{
70 for (;;) {
71 if (!*glob && (!*str || *str == '/'))
72 return (char*)str;
73 if (*glob == '*') {
74 if (!*str || *str == '/' || *str == glob[1])
75 glob++;
76 else
77 str++;
78 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050079 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050080 if (*glob != *str)
81 return NULL;
82 glob++;
83 str++;
84 }
85}
86
87// Search the bootorder list for the given glob pattern.
88static int
89find_prio(const char *glob)
90{
91 dprintf(1, "Searching bootorder for: %s\n", glob);
92 int i;
93 for (i = 0; i < BootorderCount; i++)
94 if (glob_prefix(glob, Bootorder[i]))
95 return i+1;
96 return -1;
97}
98
99#define FW_PCI_DOMAIN "/pci@i0cf8"
100
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400101static struct pci_device *
102find_pci(u16 bdf)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500103{
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400104 struct pci_device *pci;
105 foreachpci(pci) {
106 if (pci->bdf == bdf)
107 return pci;
108 }
109 return NULL;
110}
111
112static char *
113build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
114{
115 if (!pci)
116 return buf;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500117 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
118 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400119 if (pci->parent) {
120 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500121 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400122 if (pci->rootbus)
123 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500124 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
125 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500126
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400127 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500128 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500129 if (fn)
130 p += snprintf(p, buf+max-p, ",%x", fn);
131 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500132}
133
Kevin O'Connor031ef552010-12-27 19:26:57 -0500134int bootprio_find_pci_device(int bdf)
135{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400136 if (!CONFIG_BOOTORDER)
137 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500138 // Find pci device - for example: /pci@i0cf8/ethernet@5
139 char desc[256];
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400140 build_pci_path(desc, sizeof(desc), "*", find_pci(bdf));
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500141 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500142}
143
144int bootprio_find_ata_device(int bdf, int chanid, int slave)
145{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400146 if (!CONFIG_BOOTORDER)
147 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500148 if (bdf == -1)
149 // support only pci machine for now
150 return -1;
151 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
152 char desc[256], *p;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400153 p = build_pci_path(desc, sizeof(desc), "*", find_pci(bdf));
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500154 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
155 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500156}
157
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500158int bootprio_find_fdc_device(int bdf, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500159{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400160 if (!CONFIG_BOOTORDER)
161 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500162 if (bdf == -1)
163 // support only pci machine for now
164 return -1;
165 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
166 char desc[256], *p;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400167 p = build_pci_path(desc, sizeof(desc), "isa", find_pci(bdf));
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500168 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
169 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500170}
171
172int bootprio_find_pci_rom(int bdf, int instance)
173{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400174 if (!CONFIG_BOOTORDER)
175 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500176 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
177 char desc[256], *p;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400178 p = build_pci_path(desc, sizeof(desc), "*", find_pci(bdf));
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500179 if (instance)
180 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
181 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500182}
183
184int bootprio_find_named_rom(const char *name, int instance)
185{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400186 if (!CONFIG_BOOTORDER)
187 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500188 // Find named rom - for example: /rom@genroms/linuxboot.bin
189 char desc[256], *p;
190 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
191 if (instance)
192 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
193 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500194}
195
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500196int bootprio_find_usb(int bdf, u64 path)
197{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400198 if (!CONFIG_BOOTORDER)
199 return -1;
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500200 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
201 int i;
202 char desc[256], *p;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400203 p = build_pci_path(desc, sizeof(desc), "usb", find_pci(bdf));
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500204 for (i=56; i>0; i-=8) {
205 int port = (path >> i) & 0xff;
206 if (port != 0xff)
207 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
208 }
209 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
210 return find_prio(desc);
211}
212
Kevin O'Connor031ef552010-12-27 19:26:57 -0500213
214/****************************************************************
215 * Boot setup
216 ****************************************************************/
217
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500218static int CheckFloppySig = 1;
219
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500220#define DEFAULT_PRIO 9999
221
222static int DefaultFloppyPrio = 101;
223static int DefaultCDPrio = 102;
224static int DefaultHDPrio = 103;
225static int DefaultBEVPrio = 104;
226
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500227void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500228boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500229{
230 if (! CONFIG_BOOT)
231 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500232
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500233 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500234
235 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500236 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500237 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500238 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500239 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500240 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500241 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
242 = DefaultBEVPrio = DEFAULT_PRIO;
243 int i;
244 for (i=101; i<104; i++) {
245 u32 val = bootorder & 0x0f;
246 bootorder >>= 4;
247 switch (val) {
248 case 1: DefaultFloppyPrio = i; break;
249 case 2: DefaultHDPrio = i; break;
250 case 3: DefaultCDPrio = i; break;
251 case 4: DefaultBEVPrio = i; break;
252 }
253 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500254 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200255
Kevin O'Connord1a17462010-12-24 11:17:03 -0500256 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500257}
258
Kevin O'Connord1a17462010-12-24 11:17:03 -0500259
260/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500261 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500262 ****************************************************************/
263
Kevin O'Connor031ef552010-12-27 19:26:57 -0500264struct bootentry_s {
265 int type;
266 union {
267 u32 data;
268 struct segoff_s vector;
269 struct drive_s *drive;
270 };
271 int priority;
272 const char *description;
273 struct bootentry_s *next;
274};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500275static struct bootentry_s *BootList;
276
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500277#define IPL_TYPE_FLOPPY 0x01
278#define IPL_TYPE_HARDDISK 0x02
279#define IPL_TYPE_CDROM 0x03
280#define IPL_TYPE_CBFS 0x20
281#define IPL_TYPE_BEV 0x80
282#define IPL_TYPE_BCV 0x81
283
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500284static void
285bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500286{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500287 if (! CONFIG_BOOT)
288 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500289 struct bootentry_s *be = malloc_tmp(sizeof(*be));
290 if (!be) {
291 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500292 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500293 }
294 be->type = type;
295 be->priority = prio;
296 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500297 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500298 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
299 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500300
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500301 // Add entry in sorted order.
302 struct bootentry_s **pprev;
303 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
304 struct bootentry_s *pos = *pprev;
305 if (be->priority < pos->priority)
306 break;
307 if (be->priority > pos->priority)
308 continue;
309 if (be->type < pos->type)
310 break;
311 if (be->type > pos->type)
312 continue;
313 if (be->type <= IPL_TYPE_CDROM
314 && (be->drive->type < pos->drive->type
315 || (be->drive->type == pos->drive->type
316 && be->drive->cntl_id < pos->drive->cntl_id)))
317 break;
318 }
319 be->next = *pprev;
320 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500321}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500322
Kevin O'Connor031ef552010-12-27 19:26:57 -0500323// Return the given priority if it's set - defaultprio otherwise.
324static inline int defPrio(int priority, int defaultprio) {
325 return (priority < 0) ? defaultprio : priority;
326}
327
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500328// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200329void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500330boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200331{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500332 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
333 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500334 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
335 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200336}
337
Kevin O'Connor0a924122009-02-08 19:43:47 -0500338// Add a bcv entry for an expansion card harddrive or legacy option rom
339void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500340boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500341{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500342 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
343 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500344 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500345}
346
Kevin O'Connor0a924122009-02-08 19:43:47 -0500347void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500348boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500349{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500350 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500351 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500352}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500353
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500354void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500355boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500356{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500357 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500358 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500359}
360
361void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500362boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500363{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500364 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500365 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500366}
367
368// Add a CBFS payload entry
369void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500370boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500371{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500372 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500373}
374
375
376/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500377 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500378 ****************************************************************/
379
Kevin O'Connor0a924122009-02-08 19:43:47 -0500380// Show IPL option menu.
381static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500382interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500383{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400384 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500385 return;
386
387 while (get_keystroke(0) >= 0)
388 ;
389
390 printf("Press F12 for boot menu.\n\n");
391
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400392 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400393 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400394 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500395 if (scan_code != 0x86)
396 /* not F12 */
397 return;
398
399 while (get_keystroke(0) >= 0)
400 ;
401
402 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400403 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500404
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500405 // Show menu items
406 struct bootentry_s *pos = BootList;
407 int maxmenu = 0;
408 while (pos) {
409 char desc[60];
410 maxmenu++;
411 printf("%d. %s\n", maxmenu
412 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
413 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500414 }
415
Kevin O'Connor551caa22010-12-29 13:25:18 -0500416 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500417 for (;;) {
418 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500419 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500420 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500421 }
422 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500423 if (scan_code == 0x01)
424 // ESC
425 return;
426
427 // Find entry and make top priority.
428 int choice = scan_code - 1;
429 struct bootentry_s **pprev = &BootList;
430 while (--choice)
431 pprev = &(*pprev)->next;
432 pos = *pprev;
433 *pprev = pos->next;
434 pos->next = BootList;
435 BootList = pos;
436 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500437}
438
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500439// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500440struct bev_s {
441 int type;
442 u32 vector;
443};
444static struct bev_s BEV[20];
445static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500446static int HaveHDBoot, HaveFDBoot;
447
Kevin O'Connor0a924122009-02-08 19:43:47 -0500448static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500449add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500450{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500451 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
452 return;
453 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
454 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500455 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500456 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500457 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500458 bev->type = type;
459 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500460}
461
462// Prepare for boot - show menu and run bcvs.
463void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500464boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500465{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400466 if (! CONFIG_BOOT) {
467 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500468 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400469 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500470
Kevin O'Connora5826b52009-10-24 17:57:29 -0400471 // XXX - show available drives?
472
Kevin O'Connor0a924122009-02-08 19:43:47 -0500473 // Allow user to modify BCV/IPL order.
474 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400475 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500476
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500477 // Map drives and populate BEV list
478 struct bootentry_s *pos = BootList;
479 while (pos) {
480 switch (pos->type) {
481 case IPL_TYPE_BCV:
482 call_bcv(pos->vector.seg, pos->vector.offset);
483 add_bev(IPL_TYPE_HARDDISK, 0);
484 break;
485 case IPL_TYPE_FLOPPY:
486 map_floppy_drive(pos->drive);
487 add_bev(IPL_TYPE_FLOPPY, 0);
488 break;
489 case IPL_TYPE_HARDDISK:
490 map_hd_drive(pos->drive);
491 add_bev(IPL_TYPE_HARDDISK, 0);
492 break;
493 case IPL_TYPE_CDROM:
494 map_cd_drive(pos->drive);
495 // NO BREAK
496 default:
497 add_bev(pos->type, pos->data);
498 break;
499 }
500 pos = pos->next;
501 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400502
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500503 // If nothing added a floppy/hd boot - add it manually.
504 add_bev(IPL_TYPE_FLOPPY, 0);
505 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500506}
507
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500508
509/****************************************************************
510 * Boot code (int 18/19)
511 ****************************************************************/
512
Kevin O'Connor0a924122009-02-08 19:43:47 -0500513// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500514static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500515call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500516{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500517 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500518 struct bregs br;
519 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400520 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500521 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500522 // Set the magic number in ax and the boot drive in dl.
523 br.dl = bootdrv;
524 br.ax = 0xaa55;
525 call16(&br);
526}
527
528// Boot from a disk (either floppy or harddrive)
529static void
530boot_disk(u8 bootdrv, int checksig)
531{
532 u16 bootseg = 0x07c0;
533
534 // Read sector
535 struct bregs br;
536 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400537 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500538 br.dl = bootdrv;
539 br.es = bootseg;
540 br.ah = 2;
541 br.al = 1;
542 br.cl = 1;
543 call16_int(0x13, &br);
544
545 if (br.flags & F_CF) {
546 printf("Boot failed: could not read the boot disk\n\n");
547 return;
548 }
549
550 if (checksig) {
551 struct mbr_s *mbr = (void*)0;
552 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
553 printf("Boot failed: not a bootable disk\n\n");
554 return;
555 }
556 }
557
558 /* Canonicalize bootseg:bootip */
559 u16 bootip = (bootseg & 0x0fff) << 4;
560 bootseg &= 0xf000;
561
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500562 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500563}
564
565// Boot from a CD-ROM
566static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500567boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500568{
569 if (! CONFIG_CDROM_BOOT)
570 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500571 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200572
Gleb Natapov4c90a202010-12-07 13:50:54 +0200573 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500574 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500575 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500576 return;
577 }
578
579 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400580 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500581 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
582 /* Canonicalize bootseg:bootip */
583 u16 bootip = (bootseg & 0x0fff) << 4;
584 bootseg &= 0xf000;
585
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500586 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500587}
588
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400589// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400590static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500591boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400592{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400593 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400594 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500595 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500596 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400597}
598
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500599// Boot from a BEV entry on an optionrom.
600static void
601boot_rom(u32 vector)
602{
603 printf("Booting from ROM...\n");
604 struct segoff_s so;
605 so.segoff = vector;
606 call_boot_entry(so, 0);
607}
608
609// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500610static void
611do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500612{
Kevin O'Connor40967022008-07-21 22:23:05 -0400613 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500614 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400615
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500616 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400617 printf("No bootable device.\n");
618 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
619 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400620 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500621 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500622
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500623 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500624 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400625 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400626 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500627 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500628 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500629 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400630 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500631 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500632 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500633 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500634 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500635 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400636 break;
637 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500638 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500639 break;
640 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500641 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500642 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500643 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500644
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500645 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400646 struct bregs br;
647 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400648 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400649 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500650}
651
652// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500653void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500654handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500655{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400656 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400657 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500658 u16 ebda_seg = get_ebda_seg();
659 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
660 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400661 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500662}
663
664// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500665void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500666handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500667{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400668 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400669 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500670 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400671 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500672}