blob: b59794edb8e90b0fdb3baf2a694a0f435d494f4d [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;
102 int bus = pci_bdf_to_bus(bdf);
103 if (bus)
104 // XXX - this isn't the correct path syntax
105 p += snprintf(p, max, "/bus%x", bus);
106
107 int dev = pci_bdf_to_dev(bdf), fn = pci_bdf_to_fn(bdf);
108 p += snprintf(p, buf+max-p, "%s/%s@%x", FW_PCI_DOMAIN, devname, dev);
109 if (fn)
110 p += snprintf(p, buf+max-p, ",%x", fn);
111 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500112}
113
Kevin O'Connor031ef552010-12-27 19:26:57 -0500114int bootprio_find_pci_device(int bdf)
115{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500116 // Find pci device - for example: /pci@i0cf8/ethernet@5
117 char desc[256];
118 build_pci_path(desc, sizeof(desc), "*", bdf);
119 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500120}
121
122int bootprio_find_ata_device(int bdf, int chanid, int slave)
123{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500124 if (bdf == -1)
125 // support only pci machine for now
126 return -1;
127 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
128 char desc[256], *p;
129 p = build_pci_path(desc, sizeof(desc), "*", bdf);
130 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
131 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500132}
133
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500134int bootprio_find_fdc_device(int bdf, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500135{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500136 if (bdf == -1)
137 // support only pci machine for now
138 return -1;
139 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
140 char desc[256], *p;
141 p = build_pci_path(desc, sizeof(desc), "isa", bdf);
142 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
143 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500144}
145
146int bootprio_find_pci_rom(int bdf, int instance)
147{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500148 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
149 char desc[256], *p;
150 p = build_pci_path(desc, sizeof(desc), "*", bdf);
151 if (instance)
152 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
153 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500154}
155
156int bootprio_find_named_rom(const char *name, int instance)
157{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500158 // Find named rom - for example: /rom@genroms/linuxboot.bin
159 char desc[256], *p;
160 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
161 if (instance)
162 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
163 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500164}
165
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500166int bootprio_find_usb(int bdf, u64 path)
167{
168 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
169 int i;
170 char desc[256], *p;
171 p = build_pci_path(desc, sizeof(desc), "usb", bdf);
172 for (i=56; i>0; i-=8) {
173 int port = (path >> i) & 0xff;
174 if (port != 0xff)
175 p += snprintf(p, desc+sizeof(desc)-p, "/hub@%x", port);
176 }
177 snprintf(p, desc+sizeof(desc)-p, "/*@%x", (u32)(path & 0xff));
178 return find_prio(desc);
179}
180
Kevin O'Connor031ef552010-12-27 19:26:57 -0500181
182/****************************************************************
183 * Boot setup
184 ****************************************************************/
185
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500186static int CheckFloppySig = 1;
187
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500188#define DEFAULT_PRIO 9999
189
190static int DefaultFloppyPrio = 101;
191static int DefaultCDPrio = 102;
192static int DefaultHDPrio = 103;
193static int DefaultBEVPrio = 104;
194
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500195void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500196boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500197{
198 if (! CONFIG_BOOT)
199 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500200
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500201 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500202
203 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500204 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500205 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500206 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500207 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500208 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500209 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
210 = DefaultBEVPrio = DEFAULT_PRIO;
211 int i;
212 for (i=101; i<104; i++) {
213 u32 val = bootorder & 0x0f;
214 bootorder >>= 4;
215 switch (val) {
216 case 1: DefaultFloppyPrio = i; break;
217 case 2: DefaultHDPrio = i; break;
218 case 3: DefaultCDPrio = i; break;
219 case 4: DefaultBEVPrio = i; break;
220 }
221 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500222 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200223
Kevin O'Connord1a17462010-12-24 11:17:03 -0500224 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500225}
226
Kevin O'Connord1a17462010-12-24 11:17:03 -0500227
228/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500229 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500230 ****************************************************************/
231
Kevin O'Connor031ef552010-12-27 19:26:57 -0500232struct bootentry_s {
233 int type;
234 union {
235 u32 data;
236 struct segoff_s vector;
237 struct drive_s *drive;
238 };
239 int priority;
240 const char *description;
241 struct bootentry_s *next;
242};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500243static struct bootentry_s *BootList;
244
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500245#define IPL_TYPE_FLOPPY 0x01
246#define IPL_TYPE_HARDDISK 0x02
247#define IPL_TYPE_CDROM 0x03
248#define IPL_TYPE_CBFS 0x20
249#define IPL_TYPE_BEV 0x80
250#define IPL_TYPE_BCV 0x81
251
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500252static void
253bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500254{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500255 if (! CONFIG_BOOT)
256 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500257 struct bootentry_s *be = malloc_tmp(sizeof(*be));
258 if (!be) {
259 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500260 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500261 }
262 be->type = type;
263 be->priority = prio;
264 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500265 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500266 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
267 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500268
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500269 // Add entry in sorted order.
270 struct bootentry_s **pprev;
271 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
272 struct bootentry_s *pos = *pprev;
273 if (be->priority < pos->priority)
274 break;
275 if (be->priority > pos->priority)
276 continue;
277 if (be->type < pos->type)
278 break;
279 if (be->type > pos->type)
280 continue;
281 if (be->type <= IPL_TYPE_CDROM
282 && (be->drive->type < pos->drive->type
283 || (be->drive->type == pos->drive->type
284 && be->drive->cntl_id < pos->drive->cntl_id)))
285 break;
286 }
287 be->next = *pprev;
288 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500289}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500290
Kevin O'Connor031ef552010-12-27 19:26:57 -0500291// Return the given priority if it's set - defaultprio otherwise.
292static inline int defPrio(int priority, int defaultprio) {
293 return (priority < 0) ? defaultprio : priority;
294}
295
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500296// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200297void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500298boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200299{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500300 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
301 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500302 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
303 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200304}
305
Kevin O'Connor0a924122009-02-08 19:43:47 -0500306// Add a bcv entry for an expansion card harddrive or legacy option rom
307void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500308boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500309{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500310 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DEFAULT_PRIO)
311 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500312 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500313}
314
Kevin O'Connor0a924122009-02-08 19:43:47 -0500315void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500316boot_add_floppy(struct drive_s *drive_g, const char *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_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500319 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500320}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500321
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500322void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500323boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500324{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500325 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500326 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500327}
328
329void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500330boot_add_cd(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_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500333 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500334}
335
336// Add a CBFS payload entry
337void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500338boot_add_cbfs(void *data, 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_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500341}
342
343
344/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500345 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500346 ****************************************************************/
347
Kevin O'Connor0a924122009-02-08 19:43:47 -0500348// Show IPL option menu.
349static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500350interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500351{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400352 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500353 return;
354
355 while (get_keystroke(0) >= 0)
356 ;
357
358 printf("Press F12 for boot menu.\n\n");
359
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400360 enable_bootsplash();
Kevin O'Connor217d3632009-04-29 22:00:21 -0400361 int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400362 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500363 if (scan_code != 0x86)
364 /* not F12 */
365 return;
366
367 while (get_keystroke(0) >= 0)
368 ;
369
370 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400371 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500372
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500373 // Show menu items
374 struct bootentry_s *pos = BootList;
375 int maxmenu = 0;
376 while (pos) {
377 char desc[60];
378 maxmenu++;
379 printf("%d. %s\n", maxmenu
380 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
381 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500382 }
383
Kevin O'Connor551caa22010-12-29 13:25:18 -0500384 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500385 for (;;) {
386 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500387 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500388 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500389 }
390 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500391 if (scan_code == 0x01)
392 // ESC
393 return;
394
395 // Find entry and make top priority.
396 int choice = scan_code - 1;
397 struct bootentry_s **pprev = &BootList;
398 while (--choice)
399 pprev = &(*pprev)->next;
400 pos = *pprev;
401 *pprev = pos->next;
402 pos->next = BootList;
403 BootList = pos;
404 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500405}
406
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500407// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500408struct bev_s {
409 int type;
410 u32 vector;
411};
412static struct bev_s BEV[20];
413static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500414static int HaveHDBoot, HaveFDBoot;
415
Kevin O'Connor0a924122009-02-08 19:43:47 -0500416static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500417add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500418{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500419 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
420 return;
421 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
422 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500423 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500424 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500425 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500426 bev->type = type;
427 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500428}
429
430// Prepare for boot - show menu and run bcvs.
431void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500432boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500433{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400434 if (! CONFIG_BOOT) {
435 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500436 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400437 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500438
Kevin O'Connora5826b52009-10-24 17:57:29 -0400439 // XXX - show available drives?
440
Kevin O'Connor0a924122009-02-08 19:43:47 -0500441 // Allow user to modify BCV/IPL order.
442 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400443 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500444
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500445 // Map drives and populate BEV list
446 struct bootentry_s *pos = BootList;
447 while (pos) {
448 switch (pos->type) {
449 case IPL_TYPE_BCV:
450 call_bcv(pos->vector.seg, pos->vector.offset);
451 add_bev(IPL_TYPE_HARDDISK, 0);
452 break;
453 case IPL_TYPE_FLOPPY:
454 map_floppy_drive(pos->drive);
455 add_bev(IPL_TYPE_FLOPPY, 0);
456 break;
457 case IPL_TYPE_HARDDISK:
458 map_hd_drive(pos->drive);
459 add_bev(IPL_TYPE_HARDDISK, 0);
460 break;
461 case IPL_TYPE_CDROM:
462 map_cd_drive(pos->drive);
463 // NO BREAK
464 default:
465 add_bev(pos->type, pos->data);
466 break;
467 }
468 pos = pos->next;
469 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400470
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500471 // If nothing added a floppy/hd boot - add it manually.
472 add_bev(IPL_TYPE_FLOPPY, 0);
473 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500474}
475
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500476
477/****************************************************************
478 * Boot code (int 18/19)
479 ****************************************************************/
480
Kevin O'Connor0a924122009-02-08 19:43:47 -0500481// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500482static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500483call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500484{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500485 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500486 struct bregs br;
487 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400488 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500489 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500490 // Set the magic number in ax and the boot drive in dl.
491 br.dl = bootdrv;
492 br.ax = 0xaa55;
493 call16(&br);
494}
495
496// Boot from a disk (either floppy or harddrive)
497static void
498boot_disk(u8 bootdrv, int checksig)
499{
500 u16 bootseg = 0x07c0;
501
502 // Read sector
503 struct bregs br;
504 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400505 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500506 br.dl = bootdrv;
507 br.es = bootseg;
508 br.ah = 2;
509 br.al = 1;
510 br.cl = 1;
511 call16_int(0x13, &br);
512
513 if (br.flags & F_CF) {
514 printf("Boot failed: could not read the boot disk\n\n");
515 return;
516 }
517
518 if (checksig) {
519 struct mbr_s *mbr = (void*)0;
520 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
521 printf("Boot failed: not a bootable disk\n\n");
522 return;
523 }
524 }
525
526 /* Canonicalize bootseg:bootip */
527 u16 bootip = (bootseg & 0x0fff) << 4;
528 bootseg &= 0xf000;
529
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500530 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500531}
532
533// Boot from a CD-ROM
534static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500535boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500536{
537 if (! CONFIG_CDROM_BOOT)
538 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500539 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200540
Gleb Natapov4c90a202010-12-07 13:50:54 +0200541 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500542 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500543 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500544 return;
545 }
546
547 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400548 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500549 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
550 /* Canonicalize bootseg:bootip */
551 u16 bootip = (bootseg & 0x0fff) << 4;
552 bootseg &= 0xf000;
553
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500554 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500555}
556
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400557// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400558static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500559boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400560{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400561 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400562 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500563 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500564 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400565}
566
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500567// Boot from a BEV entry on an optionrom.
568static void
569boot_rom(u32 vector)
570{
571 printf("Booting from ROM...\n");
572 struct segoff_s so;
573 so.segoff = vector;
574 call_boot_entry(so, 0);
575}
576
577// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500578static void
579do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500580{
Kevin O'Connor40967022008-07-21 22:23:05 -0400581 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500582 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400583
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500584 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400585 printf("No bootable device.\n");
586 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
587 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400588 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500589 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500590
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500591 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500592 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400593 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400594 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500595 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500596 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500597 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400598 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500599 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500600 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500601 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500602 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500603 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400604 break;
605 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500606 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500607 break;
608 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500609 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500610 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500611 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500612
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500613 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400614 struct bregs br;
615 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400616 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400617 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500618}
619
620// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500621void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500622handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500623{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400624 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400625 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500626 u16 ebda_seg = get_ebda_seg();
627 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
628 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400629 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500630}
631
632// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500633void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500634handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500635{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400636 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400637 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500638 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400639 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500640}