blob: 05fb3ddfa09be62df3935b7a99448b179b6b354e [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';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050054 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050055 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
56 i++;
57 } while (f);
58}
59
60// See if 'str' starts with 'glob' - if glob contains an '*' character
61// it will match any number of characters in str that aren't a '/' or
62// the next glob character.
63static char *
64glob_prefix(const char *glob, const char *str)
65{
66 for (;;) {
67 if (!*glob && (!*str || *str == '/'))
68 return (char*)str;
69 if (*glob == '*') {
70 if (!*str || *str == '/' || *str == glob[1])
71 glob++;
72 else
73 str++;
74 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050075 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050076 if (*glob != *str)
77 return NULL;
78 glob++;
79 str++;
80 }
81}
82
83// Search the bootorder list for the given glob pattern.
84static int
85find_prio(const char *glob)
86{
87 dprintf(1, "Searching bootorder for: %s\n", glob);
88 int i;
89 for (i = 0; i < BootorderCount; i++)
90 if (glob_prefix(glob, Bootorder[i]))
91 return i+1;
92 return -1;
93}
94
95#define FW_PCI_DOMAIN "/pci@i0cf8"
96
97static char *
98build_pci_path(char *buf, int max, const char *devname, int bdf)
99{
100 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
101 char *p = buf;
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500102 int parent = pci_bdf_to_bus(bdf);
103 if (PCIpaths)
104 parent = PCIpaths[parent];
105 int parentdev = parent & 0xffff;
106 if (parent & PP_PCIBRIDGE) {
107 p = build_pci_path(p, max, "pci-bridge", parentdev);
108 } else {
109 if (parentdev)
110 p += snprintf(p, max, "/pci-root@%x", parentdev);
111 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
112 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500113
114 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500115 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500116 if (fn)
117 p += snprintf(p, buf+max-p, ",%x", fn);
118 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500119}
120
Kevin O'Connor031ef552010-12-27 19:26:57 -0500121int bootprio_find_pci_device(int bdf)
122{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500123 // Find pci device - for example: /pci@i0cf8/ethernet@5
124 char desc[256];
125 build_pci_path(desc, sizeof(desc), "*", bdf);
126 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500127}
128
129int bootprio_find_ata_device(int bdf, int chanid, int slave)
130{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500131 if (bdf == -1)
132 // support only pci machine for now
133 return -1;
134 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
135 char desc[256], *p;
136 p = build_pci_path(desc, sizeof(desc), "*", bdf);
137 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
138 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500139}
140
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500141int bootprio_find_fdc_device(int bdf, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500142{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500143 if (bdf == -1)
144 // support only pci machine for now
145 return -1;
146 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
147 char desc[256], *p;
148 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
149 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
150 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500151}
152
153int bootprio_find_pci_rom(int bdf, int instance)
154{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500155 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
156 char desc[256], *p;
157 p = build_pci_path(desc, sizeof(desc), "*", bdf);
158 if (instance)
159 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
160 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500161}
162
163int bootprio_find_named_rom(const char *name, int instance)
164{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500165 // Find named rom - for example: /rom@genroms/linuxboot.bin
166 char desc[256], *p;
167 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
168 if (instance)
169 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
170 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500171}
172
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500173int bootprio_find_usb(int bdf, u64 path)
174{
175 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
176 int i;
177 char desc[256], *p;
178 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
179 for (i=56; i>0; i-=8) {
180 int port = (path >> i) & 0xff;
181 if (port != 0xff)
182 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
183 }
184 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
185 return find_prio(desc);
186}
187
Kevin O'Connor031ef552010-12-27 19:26:57 -0500188
189/****************************************************************
190 * Boot setup
191 ****************************************************************/
192
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500193static int CheckFloppySig = 1;
194
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500195#define DEFAULT_PRIO 9999
196
197static int DefaultFloppyPrio = 101;
198static int DefaultCDPrio = 102;
199static int DefaultHDPrio = 103;
200static int DefaultBEVPrio = 104;
201
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500202void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500203boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500204{
205 if (! CONFIG_BOOT)
206 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500207
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500208 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500209
210 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500211 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500212 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500213 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500214 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500215 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500216 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
217 = DefaultBEVPrio = DEFAULT_PRIO;
218 int i;
219 for (i=101; i<104; i++) {
220 u32 val = bootorder & 0x0f;
221 bootorder >>= 4;
222 switch (val) {
223 case 1: DefaultFloppyPrio = i; break;
224 case 2: DefaultHDPrio = i; break;
225 case 3: DefaultCDPrio = i; break;
226 case 4: DefaultBEVPrio = i; break;
227 }
228 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500229 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200230
Kevin O'Connord1a17462010-12-24 11:17:03 -0500231 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500232}
233
Kevin O'Connord1a17462010-12-24 11:17:03 -0500234
235/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500236 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500237 ****************************************************************/
238
Kevin O'Connor031ef552010-12-27 19:26:57 -0500239struct bootentry_s {
240 int type;
241 union {
242 u32 data;
243 struct segoff_s vector;
244 struct drive_s *drive;
245 };
246 int priority;
247 const char *description;
248 struct bootentry_s *next;
249};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500250static struct bootentry_s *BootList;
251
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500252#define IPL_TYPE_FLOPPY 0x01
253#define IPL_TYPE_HARDDISK 0x02
254#define IPL_TYPE_CDROM 0x03
255#define IPL_TYPE_CBFS 0x20
256#define IPL_TYPE_BEV 0x80
257#define IPL_TYPE_BCV 0x81
258
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500259static void
260bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500261{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500262 if (! CONFIG_BOOT)
263 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500264 struct bootentry_s *be = malloc_tmp(sizeof(*be));
265 if (!be) {
266 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500267 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500268 }
269 be->type = type;
270 be->priority = prio;
271 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500272 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500273 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
274 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500275
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500276 // Add entry in sorted order.
277 struct bootentry_s **pprev;
278 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
279 struct bootentry_s *pos = *pprev;
280 if (be->priority < pos->priority)
281 break;
282 if (be->priority > pos->priority)
283 continue;
284 if (be->type < pos->type)
285 break;
286 if (be->type > pos->type)
287 continue;
288 if (be->type <= IPL_TYPE_CDROM
289 && (be->drive->type < pos->drive->type
290 || (be->drive->type == pos->drive->type
291 && be->drive->cntl_id < pos->drive->cntl_id)))
292 break;
293 }
294 be->next = *pprev;
295 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500296}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500297
Kevin O'Connor031ef552010-12-27 19:26:57 -0500298// Return the given priority if it's set - defaultprio otherwise.
299static inline int defPrio(int priority, int defaultprio) {
300 return (priority < 0) ? defaultprio : priority;
301}
302
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500303// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200304void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500305boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200306{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500307 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
308 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500309 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
310 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200311}
312
Kevin O'Connor0a924122009-02-08 19:43:47 -0500313// Add a bcv entry for an expansion card harddrive or legacy option rom
314void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500315boot_add_bcv(u16 seg, u16 ip, u16 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_BCV, defPrio(prio, DEFAULT_PRIO)
318 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500319 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500320}
321
Kevin O'Connor0a924122009-02-08 19:43:47 -0500322void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500323boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500324{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500325 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500326 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500327}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500328
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500329void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500330boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500331{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500332 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500333 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500334}
335
336void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500337boot_add_cd(struct drive_s *drive_g, 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_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500340 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500341}
342
343// Add a CBFS payload entry
344void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500345boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500346{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500347 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500348}
349
350
351/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500352 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500353 ****************************************************************/
354
Kevin O'Connor0a924122009-02-08 19:43:47 -0500355// Show IPL option menu.
356static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500357interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500358{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400359 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500360 return;
361
362 while (get_keystroke(0) >= 0)
363 ;
364
365 printf("Press F12 for boot menu.\n\n");
366
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400367 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400368 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400369 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500370 if (scan_code != 0x86)
371 /* not F12 */
372 return;
373
374 while (get_keystroke(0) >= 0)
375 ;
376
377 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400378 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500379
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500380 // Show menu items
381 struct bootentry_s *pos = BootList;
382 int maxmenu = 0;
383 while (pos) {
384 char desc[60];
385 maxmenu++;
386 printf("%d. %s\n", maxmenu
387 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
388 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500389 }
390
Kevin O'Connor551caa22010-12-29 13:25:18 -0500391 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500392 for (;;) {
393 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500394 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500395 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500396 }
397 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500398 if (scan_code == 0x01)
399 // ESC
400 return;
401
402 // Find entry and make top priority.
403 int choice = scan_code - 1;
404 struct bootentry_s **pprev = &BootList;
405 while (--choice)
406 pprev = &(*pprev)->next;
407 pos = *pprev;
408 *pprev = pos->next;
409 pos->next = BootList;
410 BootList = pos;
411 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500412}
413
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500414// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500415struct bev_s {
416 int type;
417 u32 vector;
418};
419static struct bev_s BEV[20];
420static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500421static int HaveHDBoot, HaveFDBoot;
422
Kevin O'Connor0a924122009-02-08 19:43:47 -0500423static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500424add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500425{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500426 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
427 return;
428 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
429 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500430 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500431 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500432 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500433 bev->type = type;
434 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500435}
436
437// Prepare for boot - show menu and run bcvs.
438void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500439boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500440{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400441 if (! CONFIG_BOOT) {
442 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500443 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400444 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500445
Kevin O'Connora5826b52009-10-24 17:57:29 -0400446 // XXX - show available drives?
447
Kevin O'Connor0a924122009-02-08 19:43:47 -0500448 // Allow user to modify BCV/IPL order.
449 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400450 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500451
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500452 // Map drives and populate BEV list
453 struct bootentry_s *pos = BootList;
454 while (pos) {
455 switch (pos->type) {
456 case IPL_TYPE_BCV:
457 call_bcv(pos->vector.seg, pos->vector.offset);
458 add_bev(IPL_TYPE_HARDDISK, 0);
459 break;
460 case IPL_TYPE_FLOPPY:
461 map_floppy_drive(pos->drive);
462 add_bev(IPL_TYPE_FLOPPY, 0);
463 break;
464 case IPL_TYPE_HARDDISK:
465 map_hd_drive(pos->drive);
466 add_bev(IPL_TYPE_HARDDISK, 0);
467 break;
468 case IPL_TYPE_CDROM:
469 map_cd_drive(pos->drive);
470 // NO BREAK
471 default:
472 add_bev(pos->type, pos->data);
473 break;
474 }
475 pos = pos->next;
476 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400477
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500478 // If nothing added a floppy/hd boot - add it manually.
479 add_bev(IPL_TYPE_FLOPPY, 0);
480 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500481}
482
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500483
484/****************************************************************
485 * Boot code (int 18/19)
486 ****************************************************************/
487
Kevin O'Connor0a924122009-02-08 19:43:47 -0500488// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500489static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500490call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500491{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500492 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500493 struct bregs br;
494 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400495 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500496 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500497 // Set the magic number in ax and the boot drive in dl.
498 br.dl = bootdrv;
499 br.ax = 0xaa55;
500 call16(&br);
501}
502
503// Boot from a disk (either floppy or harddrive)
504static void
505boot_disk(u8 bootdrv, int checksig)
506{
507 u16 bootseg = 0x07c0;
508
509 // Read sector
510 struct bregs br;
511 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400512 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500513 br.dl = bootdrv;
514 br.es = bootseg;
515 br.ah = 2;
516 br.al = 1;
517 br.cl = 1;
518 call16_int(0x13, &br);
519
520 if (br.flags & F_CF) {
521 printf("Boot failed: could not read the boot disk\n\n");
522 return;
523 }
524
525 if (checksig) {
526 struct mbr_s *mbr = (void*)0;
527 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
528 printf("Boot failed: not a bootable disk\n\n");
529 return;
530 }
531 }
532
533 /* Canonicalize bootseg:bootip */
534 u16 bootip = (bootseg & 0x0fff) << 4;
535 bootseg &= 0xf000;
536
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500537 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500538}
539
540// Boot from a CD-ROM
541static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500542boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500543{
544 if (! CONFIG_CDROM_BOOT)
545 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500546 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200547
Gleb Natapov4c90a202010-12-07 13:50:54 +0200548 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500549 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500550 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500551 return;
552 }
553
554 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400555 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500556 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
557 /* Canonicalize bootseg:bootip */
558 u16 bootip = (bootseg & 0x0fff) << 4;
559 bootseg &= 0xf000;
560
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500561 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500562}
563
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400564// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400565static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500566boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400567{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400568 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400569 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500570 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500571 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400572}
573
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500574// Boot from a BEV entry on an optionrom.
575static void
576boot_rom(u32 vector)
577{
578 printf("Booting from ROM...\n");
579 struct segoff_s so;
580 so.segoff = vector;
581 call_boot_entry(so, 0);
582}
583
584// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500585static void
586do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500587{
Kevin O'Connor40967022008-07-21 22:23:05 -0400588 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500589 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400590
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500591 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400592 printf("No bootable device.\n");
593 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
594 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400595 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500596 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500597
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500598 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500599 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400600 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400601 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500602 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500603 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500604 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400605 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500606 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500607 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500608 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500609 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500610 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400611 break;
612 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500613 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500614 break;
615 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500616 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500617 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500618 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500619
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500620 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400621 struct bregs br;
622 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400623 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400624 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500625}
626
627// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500628void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500629handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500630{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400631 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400632 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500633 u16 ebda_seg = get_ebda_seg();
634 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
635 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400636 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500637}
638
639// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500640void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500641handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500642{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400643 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400644 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500645 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400646 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500647}