blob: 9a6791602198f65d1da356d379c7dedf5b0004f2 [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
101static char *
102build_pci_path(char *buf, int max, const char *devname, int bdf)
103{
104 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
105 char *p = buf;
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500106 int parent = pci_bdf_to_bus(bdf);
107 if (PCIpaths)
108 parent = PCIpaths[parent];
109 int parentdev = parent & 0xffff;
110 if (parent & PP_PCIBRIDGE) {
111 p = build_pci_path(p, max, "pci-bridge", parentdev);
112 } else {
113 if (parentdev)
114 p += snprintf(p, max, "/pci-root@%x", parentdev);
115 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
116 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500117
118 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500119 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500120 if (fn)
121 p += snprintf(p, buf+max-p, ",%x", fn);
122 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500123}
124
Kevin O'Connor031ef552010-12-27 19:26:57 -0500125int bootprio_find_pci_device(int bdf)
126{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400127 if (!CONFIG_BOOTORDER)
128 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500129 // Find pci device - for example: /pci@i0cf8/ethernet@5
130 char desc[256];
131 build_pci_path(desc, sizeof(desc), "*", bdf);
132 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500133}
134
135int bootprio_find_ata_device(int bdf, int chanid, int slave)
136{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400137 if (!CONFIG_BOOTORDER)
138 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500139 if (bdf == -1)
140 // support only pci machine for now
141 return -1;
142 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
143 char desc[256], *p;
144 p = build_pci_path(desc, sizeof(desc), "*", bdf);
145 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
146 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500147}
148
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500149int bootprio_find_fdc_device(int bdf, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500150{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400151 if (!CONFIG_BOOTORDER)
152 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500153 if (bdf == -1)
154 // support only pci machine for now
155 return -1;
156 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
157 char desc[256], *p;
158 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
159 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
160 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500161}
162
163int bootprio_find_pci_rom(int bdf, int instance)
164{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400165 if (!CONFIG_BOOTORDER)
166 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500167 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
168 char desc[256], *p;
169 p = build_pci_path(desc, sizeof(desc), "*", bdf);
170 if (instance)
171 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
172 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500173}
174
175int bootprio_find_named_rom(const char *name, int instance)
176{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400177 if (!CONFIG_BOOTORDER)
178 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500179 // Find named rom - for example: /rom@genroms/linuxboot.bin
180 char desc[256], *p;
181 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
182 if (instance)
183 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
184 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500185}
186
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500187int bootprio_find_usb(int bdf, u64 path)
188{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400189 if (!CONFIG_BOOTORDER)
190 return -1;
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500191 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
192 int i;
193 char desc[256], *p;
194 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
195 for (i=56; i>0; i-=8) {
196 int port = (path >> i) & 0xff;
197 if (port != 0xff)
198 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
199 }
200 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
201 return find_prio(desc);
202}
203
Kevin O'Connor031ef552010-12-27 19:26:57 -0500204
205/****************************************************************
206 * Boot setup
207 ****************************************************************/
208
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500209static int CheckFloppySig = 1;
210
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500211#define DEFAULT_PRIO 9999
212
213static int DefaultFloppyPrio = 101;
214static int DefaultCDPrio = 102;
215static int DefaultHDPrio = 103;
216static int DefaultBEVPrio = 104;
217
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500218void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500219boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500220{
221 if (! CONFIG_BOOT)
222 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500223
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500224 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500225
226 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500227 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500228 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500229 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500230 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500231 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500232 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
233 = DefaultBEVPrio = DEFAULT_PRIO;
234 int i;
235 for (i=101; i<104; i++) {
236 u32 val = bootorder & 0x0f;
237 bootorder >>= 4;
238 switch (val) {
239 case 1: DefaultFloppyPrio = i; break;
240 case 2: DefaultHDPrio = i; break;
241 case 3: DefaultCDPrio = i; break;
242 case 4: DefaultBEVPrio = i; break;
243 }
244 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500245 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200246
Kevin O'Connord1a17462010-12-24 11:17:03 -0500247 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500248}
249
Kevin O'Connord1a17462010-12-24 11:17:03 -0500250
251/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500252 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500253 ****************************************************************/
254
Kevin O'Connor031ef552010-12-27 19:26:57 -0500255struct bootentry_s {
256 int type;
257 union {
258 u32 data;
259 struct segoff_s vector;
260 struct drive_s *drive;
261 };
262 int priority;
263 const char *description;
264 struct bootentry_s *next;
265};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500266static struct bootentry_s *BootList;
267
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500268#define IPL_TYPE_FLOPPY 0x01
269#define IPL_TYPE_HARDDISK 0x02
270#define IPL_TYPE_CDROM 0x03
271#define IPL_TYPE_CBFS 0x20
272#define IPL_TYPE_BEV 0x80
273#define IPL_TYPE_BCV 0x81
274
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500275static void
276bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500277{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500278 if (! CONFIG_BOOT)
279 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500280 struct bootentry_s *be = malloc_tmp(sizeof(*be));
281 if (!be) {
282 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500283 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500284 }
285 be->type = type;
286 be->priority = prio;
287 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500288 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500289 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
290 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500291
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500292 // Add entry in sorted order.
293 struct bootentry_s **pprev;
294 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
295 struct bootentry_s *pos = *pprev;
296 if (be->priority < pos->priority)
297 break;
298 if (be->priority > pos->priority)
299 continue;
300 if (be->type < pos->type)
301 break;
302 if (be->type > pos->type)
303 continue;
304 if (be->type <= IPL_TYPE_CDROM
305 && (be->drive->type < pos->drive->type
306 || (be->drive->type == pos->drive->type
307 && be->drive->cntl_id < pos->drive->cntl_id)))
308 break;
309 }
310 be->next = *pprev;
311 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500312}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500313
Kevin O'Connor031ef552010-12-27 19:26:57 -0500314// Return the given priority if it's set - defaultprio otherwise.
315static inline int defPrio(int priority, int defaultprio) {
316 return (priority < 0) ? defaultprio : priority;
317}
318
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500319// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200320void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500321boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200322{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500323 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
324 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500325 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
326 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200327}
328
Kevin O'Connor0a924122009-02-08 19:43:47 -0500329// Add a bcv entry for an expansion card harddrive or legacy option rom
330void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500331boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500332{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500333 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
334 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500335 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500336}
337
Kevin O'Connor0a924122009-02-08 19:43:47 -0500338void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500339boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500340{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500341 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500342 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500343}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500344
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500345void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500346boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500347{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500348 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500349 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500350}
351
352void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500353boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500354{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500355 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500356 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500357}
358
359// Add a CBFS payload entry
360void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500361boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500362{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500363 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500364}
365
366
367/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500368 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500369 ****************************************************************/
370
Kevin O'Connor0a924122009-02-08 19:43:47 -0500371// Show IPL option menu.
372static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500373interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500374{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400375 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500376 return;
377
378 while (get_keystroke(0) >= 0)
379 ;
380
381 printf("Press F12 for boot menu.\n\n");
382
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400383 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400384 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400385 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500386 if (scan_code != 0x86)
387 /* not F12 */
388 return;
389
390 while (get_keystroke(0) >= 0)
391 ;
392
393 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400394 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500395
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500396 // Show menu items
397 struct bootentry_s *pos = BootList;
398 int maxmenu = 0;
399 while (pos) {
400 char desc[60];
401 maxmenu++;
402 printf("%d. %s\n", maxmenu
403 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
404 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500405 }
406
Kevin O'Connor551caa22010-12-29 13:25:18 -0500407 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500408 for (;;) {
409 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500410 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500411 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500412 }
413 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500414 if (scan_code == 0x01)
415 // ESC
416 return;
417
418 // Find entry and make top priority.
419 int choice = scan_code - 1;
420 struct bootentry_s **pprev = &BootList;
421 while (--choice)
422 pprev = &(*pprev)->next;
423 pos = *pprev;
424 *pprev = pos->next;
425 pos->next = BootList;
426 BootList = pos;
427 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500428}
429
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500430// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500431struct bev_s {
432 int type;
433 u32 vector;
434};
435static struct bev_s BEV[20];
436static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500437static int HaveHDBoot, HaveFDBoot;
438
Kevin O'Connor0a924122009-02-08 19:43:47 -0500439static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500440add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500441{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500442 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
443 return;
444 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
445 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500446 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500447 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500448 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500449 bev->type = type;
450 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500451}
452
453// Prepare for boot - show menu and run bcvs.
454void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500455boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500456{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400457 if (! CONFIG_BOOT) {
458 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500459 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400460 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500461
Kevin O'Connora5826b52009-10-24 17:57:29 -0400462 // XXX - show available drives?
463
Kevin O'Connor0a924122009-02-08 19:43:47 -0500464 // Allow user to modify BCV/IPL order.
465 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400466 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500467
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500468 // Map drives and populate BEV list
469 struct bootentry_s *pos = BootList;
470 while (pos) {
471 switch (pos->type) {
472 case IPL_TYPE_BCV:
473 call_bcv(pos->vector.seg, pos->vector.offset);
474 add_bev(IPL_TYPE_HARDDISK, 0);
475 break;
476 case IPL_TYPE_FLOPPY:
477 map_floppy_drive(pos->drive);
478 add_bev(IPL_TYPE_FLOPPY, 0);
479 break;
480 case IPL_TYPE_HARDDISK:
481 map_hd_drive(pos->drive);
482 add_bev(IPL_TYPE_HARDDISK, 0);
483 break;
484 case IPL_TYPE_CDROM:
485 map_cd_drive(pos->drive);
486 // NO BREAK
487 default:
488 add_bev(pos->type, pos->data);
489 break;
490 }
491 pos = pos->next;
492 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400493
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500494 // If nothing added a floppy/hd boot - add it manually.
495 add_bev(IPL_TYPE_FLOPPY, 0);
496 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500497}
498
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500499
500/****************************************************************
501 * Boot code (int 18/19)
502 ****************************************************************/
503
Kevin O'Connor0a924122009-02-08 19:43:47 -0500504// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500505static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500506call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500507{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500508 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500509 struct bregs br;
510 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400511 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500512 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500513 // Set the magic number in ax and the boot drive in dl.
514 br.dl = bootdrv;
515 br.ax = 0xaa55;
516 call16(&br);
517}
518
519// Boot from a disk (either floppy or harddrive)
520static void
521boot_disk(u8 bootdrv, int checksig)
522{
523 u16 bootseg = 0x07c0;
524
525 // Read sector
526 struct bregs br;
527 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400528 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500529 br.dl = bootdrv;
530 br.es = bootseg;
531 br.ah = 2;
532 br.al = 1;
533 br.cl = 1;
534 call16_int(0x13, &br);
535
536 if (br.flags & F_CF) {
537 printf("Boot failed: could not read the boot disk\n\n");
538 return;
539 }
540
541 if (checksig) {
542 struct mbr_s *mbr = (void*)0;
543 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
544 printf("Boot failed: not a bootable disk\n\n");
545 return;
546 }
547 }
548
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
556// Boot from a CD-ROM
557static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500558boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500559{
560 if (! CONFIG_CDROM_BOOT)
561 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500562 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200563
Gleb Natapov4c90a202010-12-07 13:50:54 +0200564 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500565 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500566 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500567 return;
568 }
569
570 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400571 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500572 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
573 /* Canonicalize bootseg:bootip */
574 u16 bootip = (bootseg & 0x0fff) << 4;
575 bootseg &= 0xf000;
576
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500577 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500578}
579
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400580// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400581static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500582boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400583{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400584 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400585 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500586 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500587 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400588}
589
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500590// Boot from a BEV entry on an optionrom.
591static void
592boot_rom(u32 vector)
593{
594 printf("Booting from ROM...\n");
595 struct segoff_s so;
596 so.segoff = vector;
597 call_boot_entry(so, 0);
598}
599
600// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500601static void
602do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500603{
Kevin O'Connor40967022008-07-21 22:23:05 -0400604 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500605 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400606
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500607 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400608 printf("No bootable device.\n");
609 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
610 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400611 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500612 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500613
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500614 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500615 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400616 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400617 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500618 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500619 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500620 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400621 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500622 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500623 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500624 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500625 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500626 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400627 break;
628 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500629 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500630 break;
631 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500632 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500633 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500634 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500635
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500636 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400637 struct bregs br;
638 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400639 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400640 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500641}
642
643// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500644void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500645handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500646{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400647 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400648 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500649 u16 ebda_seg = get_ebda_seg();
650 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
651 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400652 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500653}
654
655// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500656void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500657handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500658{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400659 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400660 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500661 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400662 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500663}