blob: 1cd4126a8d8ccfd301068dd482cb0d091c39ba60 [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);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050044 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050045 return;
46 }
47
48 dprintf(3, "boot order:\n");
49 i = 0;
50 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050051 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050052 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050053 if (f)
54 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050055 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050056 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
57 i++;
58 } while (f);
59}
60
61// See if 'str' starts with 'glob' - if glob contains an '*' character
62// it will match any number of characters in str that aren't a '/' or
63// the next glob character.
64static char *
65glob_prefix(const char *glob, const char *str)
66{
67 for (;;) {
68 if (!*glob && (!*str || *str == '/'))
69 return (char*)str;
70 if (*glob == '*') {
71 if (!*str || *str == '/' || *str == glob[1])
72 glob++;
73 else
74 str++;
75 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050076 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050077 if (*glob != *str)
78 return NULL;
79 glob++;
80 str++;
81 }
82}
83
84// Search the bootorder list for the given glob pattern.
85static int
86find_prio(const char *glob)
87{
88 dprintf(1, "Searching bootorder for: %s\n", glob);
89 int i;
90 for (i = 0; i < BootorderCount; i++)
91 if (glob_prefix(glob, Bootorder[i]))
92 return i+1;
93 return -1;
94}
95
96#define FW_PCI_DOMAIN "/pci@i0cf8"
97
98static char *
99build_pci_path(char *buf, int max, const char *devname, int bdf)
100{
101 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
102 char *p = buf;
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500103 int parent = pci_bdf_to_bus(bdf);
104 if (PCIpaths)
105 parent = PCIpaths[parent];
106 int parentdev = parent & 0xffff;
107 if (parent & PP_PCIBRIDGE) {
108 p = build_pci_path(p, max, "pci-bridge", parentdev);
109 } else {
110 if (parentdev)
111 p += snprintf(p, max, "/pci-root@%x", parentdev);
112 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
113 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500114
115 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500116 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500117 if (fn)
118 p += snprintf(p, buf+max-p, ",%x", fn);
119 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500120}
121
Kevin O'Connor031ef552010-12-27 19:26:57 -0500122int bootprio_find_pci_device(int bdf)
123{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500124 // Find pci device - for example: /pci@i0cf8/ethernet@5
125 char desc[256];
126 build_pci_path(desc, sizeof(desc), "*", bdf);
127 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500128}
129
130int bootprio_find_ata_device(int bdf, int chanid, int slave)
131{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500132 if (bdf == -1)
133 // support only pci machine for now
134 return -1;
135 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
136 char desc[256], *p;
137 p = build_pci_path(desc, sizeof(desc), "*", bdf);
138 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
139 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500140}
141
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500142int bootprio_find_fdc_device(int bdf, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500143{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500144 if (bdf == -1)
145 // support only pci machine for now
146 return -1;
147 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
148 char desc[256], *p;
149 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
150 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
151 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500152}
153
154int bootprio_find_pci_rom(int bdf, int instance)
155{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500156 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
157 char desc[256], *p;
158 p = build_pci_path(desc, sizeof(desc), "*", bdf);
159 if (instance)
160 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
161 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500162}
163
164int bootprio_find_named_rom(const char *name, int instance)
165{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500166 // Find named rom - for example: /rom@genroms/linuxboot.bin
167 char desc[256], *p;
168 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
169 if (instance)
170 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
171 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500172}
173
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500174int bootprio_find_usb(int bdf, u64 path)
175{
176 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
177 int i;
178 char desc[256], *p;
179 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
180 for (i=56; i>0; i-=8) {
181 int port = (path >> i) & 0xff;
182 if (port != 0xff)
183 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
184 }
185 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
186 return find_prio(desc);
187}
188
Kevin O'Connor031ef552010-12-27 19:26:57 -0500189
190/****************************************************************
191 * Boot setup
192 ****************************************************************/
193
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500194static int CheckFloppySig = 1;
195
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500196#define DEFAULT_PRIO 9999
197
198static int DefaultFloppyPrio = 101;
199static int DefaultCDPrio = 102;
200static int DefaultHDPrio = 103;
201static int DefaultBEVPrio = 104;
202
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500203void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500204boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500205{
206 if (! CONFIG_BOOT)
207 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500208
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500209 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500210
211 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500212 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500213 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500214 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500215 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500216 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500217 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
218 = DefaultBEVPrio = DEFAULT_PRIO;
219 int i;
220 for (i=101; i<104; i++) {
221 u32 val = bootorder & 0x0f;
222 bootorder >>= 4;
223 switch (val) {
224 case 1: DefaultFloppyPrio = i; break;
225 case 2: DefaultHDPrio = i; break;
226 case 3: DefaultCDPrio = i; break;
227 case 4: DefaultBEVPrio = i; break;
228 }
229 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500230 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200231
Kevin O'Connord1a17462010-12-24 11:17:03 -0500232 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500233}
234
Kevin O'Connord1a17462010-12-24 11:17:03 -0500235
236/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500237 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500238 ****************************************************************/
239
Kevin O'Connor031ef552010-12-27 19:26:57 -0500240struct bootentry_s {
241 int type;
242 union {
243 u32 data;
244 struct segoff_s vector;
245 struct drive_s *drive;
246 };
247 int priority;
248 const char *description;
249 struct bootentry_s *next;
250};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500251static struct bootentry_s *BootList;
252
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500253#define IPL_TYPE_FLOPPY 0x01
254#define IPL_TYPE_HARDDISK 0x02
255#define IPL_TYPE_CDROM 0x03
256#define IPL_TYPE_CBFS 0x20
257#define IPL_TYPE_BEV 0x80
258#define IPL_TYPE_BCV 0x81
259
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500260static void
261bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500262{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500263 if (! CONFIG_BOOT)
264 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500265 struct bootentry_s *be = malloc_tmp(sizeof(*be));
266 if (!be) {
267 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500268 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500269 }
270 be->type = type;
271 be->priority = prio;
272 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500273 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500274 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
275 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500276
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500277 // Add entry in sorted order.
278 struct bootentry_s **pprev;
279 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
280 struct bootentry_s *pos = *pprev;
281 if (be->priority < pos->priority)
282 break;
283 if (be->priority > pos->priority)
284 continue;
285 if (be->type < pos->type)
286 break;
287 if (be->type > pos->type)
288 continue;
289 if (be->type <= IPL_TYPE_CDROM
290 && (be->drive->type < pos->drive->type
291 || (be->drive->type == pos->drive->type
292 && be->drive->cntl_id < pos->drive->cntl_id)))
293 break;
294 }
295 be->next = *pprev;
296 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500297}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500298
Kevin O'Connor031ef552010-12-27 19:26:57 -0500299// Return the given priority if it's set - defaultprio otherwise.
300static inline int defPrio(int priority, int defaultprio) {
301 return (priority < 0) ? defaultprio : priority;
302}
303
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500304// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200305void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500306boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200307{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500308 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
309 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500310 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
311 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200312}
313
Kevin O'Connor0a924122009-02-08 19:43:47 -0500314// Add a bcv entry for an expansion card harddrive or legacy option rom
315void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500316boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500317{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500318 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
319 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500320 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500321}
322
Kevin O'Connor0a924122009-02-08 19:43:47 -0500323void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500324boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500325{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500326 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500327 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500328}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500329
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500330void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500331boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500332{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500333 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500334 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500335}
336
337void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500338boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500339{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500340 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500341 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500342}
343
344// Add a CBFS payload entry
345void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500346boot_add_cbfs(void *data, 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_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500349}
350
351
352/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500353 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500354 ****************************************************************/
355
Kevin O'Connor0a924122009-02-08 19:43:47 -0500356// Show IPL option menu.
357static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500358interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500359{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400360 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500361 return;
362
363 while (get_keystroke(0) >= 0)
364 ;
365
366 printf("Press F12 for boot menu.\n\n");
367
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400368 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400369 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400370 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500371 if (scan_code != 0x86)
372 /* not F12 */
373 return;
374
375 while (get_keystroke(0) >= 0)
376 ;
377
378 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400379 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500380
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500381 // Show menu items
382 struct bootentry_s *pos = BootList;
383 int maxmenu = 0;
384 while (pos) {
385 char desc[60];
386 maxmenu++;
387 printf("%d. %s\n", maxmenu
388 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
389 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500390 }
391
Kevin O'Connor551caa22010-12-29 13:25:18 -0500392 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500393 for (;;) {
394 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500395 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500396 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500397 }
398 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500399 if (scan_code == 0x01)
400 // ESC
401 return;
402
403 // Find entry and make top priority.
404 int choice = scan_code - 1;
405 struct bootentry_s **pprev = &BootList;
406 while (--choice)
407 pprev = &(*pprev)->next;
408 pos = *pprev;
409 *pprev = pos->next;
410 pos->next = BootList;
411 BootList = pos;
412 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500413}
414
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500415// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500416struct bev_s {
417 int type;
418 u32 vector;
419};
420static struct bev_s BEV[20];
421static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500422static int HaveHDBoot, HaveFDBoot;
423
Kevin O'Connor0a924122009-02-08 19:43:47 -0500424static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500425add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500426{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500427 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
428 return;
429 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
430 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500431 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500432 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500433 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500434 bev->type = type;
435 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500436}
437
438// Prepare for boot - show menu and run bcvs.
439void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500440boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500441{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400442 if (! CONFIG_BOOT) {
443 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500444 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400445 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500446
Kevin O'Connora5826b52009-10-24 17:57:29 -0400447 // XXX - show available drives?
448
Kevin O'Connor0a924122009-02-08 19:43:47 -0500449 // Allow user to modify BCV/IPL order.
450 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400451 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500452
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500453 // Map drives and populate BEV list
454 struct bootentry_s *pos = BootList;
455 while (pos) {
456 switch (pos->type) {
457 case IPL_TYPE_BCV:
458 call_bcv(pos->vector.seg, pos->vector.offset);
459 add_bev(IPL_TYPE_HARDDISK, 0);
460 break;
461 case IPL_TYPE_FLOPPY:
462 map_floppy_drive(pos->drive);
463 add_bev(IPL_TYPE_FLOPPY, 0);
464 break;
465 case IPL_TYPE_HARDDISK:
466 map_hd_drive(pos->drive);
467 add_bev(IPL_TYPE_HARDDISK, 0);
468 break;
469 case IPL_TYPE_CDROM:
470 map_cd_drive(pos->drive);
471 // NO BREAK
472 default:
473 add_bev(pos->type, pos->data);
474 break;
475 }
476 pos = pos->next;
477 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400478
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500479 // If nothing added a floppy/hd boot - add it manually.
480 add_bev(IPL_TYPE_FLOPPY, 0);
481 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500482}
483
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500484
485/****************************************************************
486 * Boot code (int 18/19)
487 ****************************************************************/
488
Kevin O'Connor0a924122009-02-08 19:43:47 -0500489// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500490static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500491call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500492{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500493 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500494 struct bregs br;
495 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400496 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500497 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500498 // Set the magic number in ax and the boot drive in dl.
499 br.dl = bootdrv;
500 br.ax = 0xaa55;
501 call16(&br);
502}
503
504// Boot from a disk (either floppy or harddrive)
505static void
506boot_disk(u8 bootdrv, int checksig)
507{
508 u16 bootseg = 0x07c0;
509
510 // Read sector
511 struct bregs br;
512 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400513 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500514 br.dl = bootdrv;
515 br.es = bootseg;
516 br.ah = 2;
517 br.al = 1;
518 br.cl = 1;
519 call16_int(0x13, &br);
520
521 if (br.flags & F_CF) {
522 printf("Boot failed: could not read the boot disk\n\n");
523 return;
524 }
525
526 if (checksig) {
527 struct mbr_s *mbr = (void*)0;
528 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
529 printf("Boot failed: not a bootable disk\n\n");
530 return;
531 }
532 }
533
534 /* Canonicalize bootseg:bootip */
535 u16 bootip = (bootseg & 0x0fff) << 4;
536 bootseg &= 0xf000;
537
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500538 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500539}
540
541// Boot from a CD-ROM
542static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500543boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500544{
545 if (! CONFIG_CDROM_BOOT)
546 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500547 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200548
Gleb Natapov4c90a202010-12-07 13:50:54 +0200549 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500550 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500551 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500552 return;
553 }
554
555 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400556 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500557 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
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
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400565// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400566static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500567boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400568{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400569 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400570 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500571 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500572 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400573}
574
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500575// Boot from a BEV entry on an optionrom.
576static void
577boot_rom(u32 vector)
578{
579 printf("Booting from ROM...\n");
580 struct segoff_s so;
581 so.segoff = vector;
582 call_boot_entry(so, 0);
583}
584
585// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500586static void
587do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500588{
Kevin O'Connor40967022008-07-21 22:23:05 -0400589 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500590 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400591
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500592 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400593 printf("No bootable device.\n");
594 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
595 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400596 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500597 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500598
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500599 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500600 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400601 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400602 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500603 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500604 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500605 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400606 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500607 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500608 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500609 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500610 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500611 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400612 break;
613 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500614 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500615 break;
616 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500617 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500618 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500619 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500620
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500621 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400622 struct bregs br;
623 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400624 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400625 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500626}
627
628// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500629void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500630handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500631{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400632 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400633 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500634 u16 ebda_seg = get_ebda_seg();
635 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
636 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400637 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500638}
639
640// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500641void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500642handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500643{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400644 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400645 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500646 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400647 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500648}