blob: ef21fe4e87bd0f925fda6a775103a4b875b1e3f9 [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'Connorea274782012-03-08 07:49:09 -050017#include "usb.h" // struct usbdevice_s
Kevin O'Connorc659fde2008-12-28 23:43:20 -050018
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050019
20/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050021 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050022 ****************************************************************/
23
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050024static char **Bootorder;
25static int BootorderCount;
26
Kevin O'Connord1a17462010-12-24 11:17:03 -050027static void
28loadBootOrder(void)
29{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040030 if (!CONFIG_BOOTORDER)
31 return;
32
Kevin O'Connord1a17462010-12-24 11:17:03 -050033 char *f = romfile_loadfile("bootorder", NULL);
34 if (!f)
35 return;
36
Kevin O'Connor8bf55032011-01-01 11:00:42 -050037 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050038 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050039 while (f[i]) {
40 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050041 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050042 i++;
43 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050044 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
45 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050046 warn_noalloc();
47 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050048 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050049 return;
50 }
51
52 dprintf(3, "boot order:\n");
53 i = 0;
54 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050055 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050056 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050057 if (f)
58 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050059 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050060 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
61 i++;
62 } while (f);
63}
64
65// See if 'str' starts with 'glob' - if glob contains an '*' character
66// it will match any number of characters in str that aren't a '/' or
67// the next glob character.
68static char *
69glob_prefix(const char *glob, const char *str)
70{
71 for (;;) {
72 if (!*glob && (!*str || *str == '/'))
73 return (char*)str;
74 if (*glob == '*') {
75 if (!*str || *str == '/' || *str == glob[1])
76 glob++;
77 else
78 str++;
79 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050080 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050081 if (*glob != *str)
82 return NULL;
83 glob++;
84 str++;
85 }
86}
87
88// Search the bootorder list for the given glob pattern.
89static int
90find_prio(const char *glob)
91{
92 dprintf(1, "Searching bootorder for: %s\n", glob);
93 int i;
94 for (i = 0; i < BootorderCount; i++)
95 if (glob_prefix(glob, Bootorder[i]))
96 return i+1;
97 return -1;
98}
99
100#define FW_PCI_DOMAIN "/pci@i0cf8"
101
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400102static char *
103build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
104{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500105 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
106 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400107 if (pci->parent) {
108 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500109 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400110 if (pci->rootbus)
111 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500112 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
113 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500114
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400115 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->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'Connordc3a7d62011-07-09 14:33:56 -0400122int bootprio_find_pci_device(struct pci_device *pci)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500123{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400124 if (!CONFIG_BOOTORDER)
125 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500126 // Find pci device - for example: /pci@i0cf8/ethernet@5
127 char desc[256];
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400128 build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500129 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500130}
131
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100132int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
133{
134 if (!CONFIG_BOOTORDER)
135 return -1;
136 if (!pci)
137 // support only pci machine for now
138 return -1;
139 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
140 char desc[256], *p;
141 p = build_pci_path(desc, sizeof(desc), "*", pci);
142 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
143 return find_prio(desc);
144}
145
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400146int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500147{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400148 if (!CONFIG_BOOTORDER)
149 return -1;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400150 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500151 // support only pci machine for now
152 return -1;
153 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
154 char desc[256], *p;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400155 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500156 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
157 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500158}
159
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400160int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500161{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400162 if (!CONFIG_BOOTORDER)
163 return -1;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400164 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500165 // support only pci machine for now
166 return -1;
167 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
168 char desc[256], *p;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400169 p = build_pci_path(desc, sizeof(desc), "isa", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500170 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
171 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500172}
173
Kevin O'Connorfce91892011-07-09 14:47:47 -0400174int bootprio_find_pci_rom(struct pci_device *pci, int instance)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500175{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400176 if (!CONFIG_BOOTORDER)
177 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500178 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
179 char desc[256], *p;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400180 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500181 if (instance)
182 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
183 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500184}
185
186int bootprio_find_named_rom(const char *name, int instance)
187{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400188 if (!CONFIG_BOOTORDER)
189 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500190 // Find named rom - for example: /rom@genroms/linuxboot.bin
191 char desc[256], *p;
192 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
193 if (instance)
194 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
195 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500196}
197
Kevin O'Connorea274782012-03-08 07:49:09 -0500198static char *
199build_usb_path(char *buf, int max, struct usbhub_s *hub)
200{
201 if (!hub->usbdev)
202 // Root hub - nothing to add.
203 return buf;
204 char *p = build_usb_path(buf, max, hub->usbdev->hub);
205 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
206 return p;
207}
208
209int bootprio_find_usb(struct usbdevice_s *usbdev)
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500210{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400211 if (!CONFIG_BOOTORDER)
212 return -1;
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500213 // Find usb - for example: /pci@i0cf8/usb@1,2/hub@1/network@0/ethernet@0
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500214 char desc[256], *p;
Kevin O'Connorea274782012-03-08 07:49:09 -0500215 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
216 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
217 snprintf(p, desc+sizeof(desc)-p, "/*@%x", usbdev->port+1);
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500218 return find_prio(desc);
219}
220
Kevin O'Connor031ef552010-12-27 19:26:57 -0500221
222/****************************************************************
223 * Boot setup
224 ****************************************************************/
225
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500226static int CheckFloppySig = 1;
227
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500228#define DEFAULT_PRIO 9999
229
230static int DefaultFloppyPrio = 101;
231static int DefaultCDPrio = 102;
232static int DefaultHDPrio = 103;
233static int DefaultBEVPrio = 104;
234
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500235void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500236boot_setup(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500237{
238 if (! CONFIG_BOOT)
239 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500240
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500241 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500242 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500243 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500244 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500245 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500246 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500247 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
248 = DefaultBEVPrio = DEFAULT_PRIO;
249 int i;
250 for (i=101; i<104; i++) {
251 u32 val = bootorder & 0x0f;
252 bootorder >>= 4;
253 switch (val) {
254 case 1: DefaultFloppyPrio = i; break;
255 case 2: DefaultHDPrio = i; break;
256 case 3: DefaultCDPrio = i; break;
257 case 4: DefaultBEVPrio = i; break;
258 }
259 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500260 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200261
Kevin O'Connord1a17462010-12-24 11:17:03 -0500262 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500263}
264
Kevin O'Connord1a17462010-12-24 11:17:03 -0500265
266/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500267 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500268 ****************************************************************/
269
Kevin O'Connor031ef552010-12-27 19:26:57 -0500270struct bootentry_s {
271 int type;
272 union {
273 u32 data;
274 struct segoff_s vector;
275 struct drive_s *drive;
276 };
277 int priority;
278 const char *description;
279 struct bootentry_s *next;
280};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500281static struct bootentry_s *BootList;
282
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500283#define IPL_TYPE_FLOPPY 0x01
284#define IPL_TYPE_HARDDISK 0x02
285#define IPL_TYPE_CDROM 0x03
286#define IPL_TYPE_CBFS 0x20
287#define IPL_TYPE_BEV 0x80
288#define IPL_TYPE_BCV 0x81
289
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500290static void
291bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500292{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500293 if (! CONFIG_BOOT)
294 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500295 struct bootentry_s *be = malloc_tmp(sizeof(*be));
296 if (!be) {
297 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500298 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500299 }
300 be->type = type;
301 be->priority = prio;
302 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500303 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500304 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
305 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500306
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500307 // Add entry in sorted order.
308 struct bootentry_s **pprev;
309 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
310 struct bootentry_s *pos = *pprev;
311 if (be->priority < pos->priority)
312 break;
313 if (be->priority > pos->priority)
314 continue;
315 if (be->type < pos->type)
316 break;
317 if (be->type > pos->type)
318 continue;
319 if (be->type <= IPL_TYPE_CDROM
320 && (be->drive->type < pos->drive->type
321 || (be->drive->type == pos->drive->type
322 && be->drive->cntl_id < pos->drive->cntl_id)))
323 break;
324 }
325 be->next = *pprev;
326 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500327}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500328
Kevin O'Connor031ef552010-12-27 19:26:57 -0500329// Return the given priority if it's set - defaultprio otherwise.
330static inline int defPrio(int priority, int defaultprio) {
331 return (priority < 0) ? defaultprio : priority;
332}
333
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500334// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200335void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500336boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200337{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500338 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
339 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500340 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
341 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200342}
343
Kevin O'Connor0a924122009-02-08 19:43:47 -0500344// Add a bcv entry for an expansion card harddrive or legacy option rom
345void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500346boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500347{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500348 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500349 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500350 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500351}
352
Kevin O'Connor0a924122009-02-08 19:43:47 -0500353void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500354boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500355{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500356 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500357 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500358}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500359
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500360void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500361boot_add_hd(struct drive_s *drive_g, 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_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500364 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500365}
366
367void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500368boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500369{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500370 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500371 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500372}
373
374// Add a CBFS payload entry
375void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500376boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500377{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500378 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500379}
380
381
382/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500383 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500384 ****************************************************************/
385
Kevin O'Connor73023002011-07-05 20:34:34 -0400386#define DEFAULT_BOOTMENU_WAIT 2500
387
Kevin O'Connor0a924122009-02-08 19:43:47 -0500388// Show IPL option menu.
389static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500390interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500391{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400392 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500393 return;
394
395 while (get_keystroke(0) >= 0)
396 ;
397
398 printf("Press F12 for boot menu.\n\n");
399
Kevin O'Connor73023002011-07-05 20:34:34 -0400400 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400401 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400402 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400403 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500404 if (scan_code != 0x86)
405 /* not F12 */
406 return;
407
408 while (get_keystroke(0) >= 0)
409 ;
410
411 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400412 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500413
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500414 // Show menu items
415 struct bootentry_s *pos = BootList;
416 int maxmenu = 0;
417 while (pos) {
418 char desc[60];
419 maxmenu++;
420 printf("%d. %s\n", maxmenu
421 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
422 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500423 }
424
Kevin O'Connor551caa22010-12-29 13:25:18 -0500425 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500426 for (;;) {
427 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500428 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500429 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500430 }
431 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500432 if (scan_code == 0x01)
433 // ESC
434 return;
435
436 // Find entry and make top priority.
437 int choice = scan_code - 1;
438 struct bootentry_s **pprev = &BootList;
439 while (--choice)
440 pprev = &(*pprev)->next;
441 pos = *pprev;
442 *pprev = pos->next;
443 pos->next = BootList;
444 BootList = pos;
445 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500446}
447
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500448// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500449struct bev_s {
450 int type;
451 u32 vector;
452};
453static struct bev_s BEV[20];
454static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500455static int HaveHDBoot, HaveFDBoot;
456
Kevin O'Connor0a924122009-02-08 19:43:47 -0500457static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500458add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500459{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500460 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
461 return;
462 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
463 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500464 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500465 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500466 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500467 bev->type = type;
468 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500469}
470
471// Prepare for boot - show menu and run bcvs.
472void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500473boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500474{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400475 if (! CONFIG_BOOT) {
476 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500477 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400478 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500479
Kevin O'Connora5826b52009-10-24 17:57:29 -0400480 // XXX - show available drives?
481
Kevin O'Connor0a924122009-02-08 19:43:47 -0500482 // Allow user to modify BCV/IPL order.
483 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400484 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500485
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500486 // Map drives and populate BEV list
487 struct bootentry_s *pos = BootList;
488 while (pos) {
489 switch (pos->type) {
490 case IPL_TYPE_BCV:
491 call_bcv(pos->vector.seg, pos->vector.offset);
492 add_bev(IPL_TYPE_HARDDISK, 0);
493 break;
494 case IPL_TYPE_FLOPPY:
495 map_floppy_drive(pos->drive);
496 add_bev(IPL_TYPE_FLOPPY, 0);
497 break;
498 case IPL_TYPE_HARDDISK:
499 map_hd_drive(pos->drive);
500 add_bev(IPL_TYPE_HARDDISK, 0);
501 break;
502 case IPL_TYPE_CDROM:
503 map_cd_drive(pos->drive);
504 // NO BREAK
505 default:
506 add_bev(pos->type, pos->data);
507 break;
508 }
509 pos = pos->next;
510 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400511
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500512 // If nothing added a floppy/hd boot - add it manually.
513 add_bev(IPL_TYPE_FLOPPY, 0);
514 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500515}
516
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500517
518/****************************************************************
519 * Boot code (int 18/19)
520 ****************************************************************/
521
Kevin O'Connor0a924122009-02-08 19:43:47 -0500522// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500523static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500524call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500525{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500526 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500527 struct bregs br;
528 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400529 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500530 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500531 // Set the magic number in ax and the boot drive in dl.
532 br.dl = bootdrv;
533 br.ax = 0xaa55;
534 call16(&br);
535}
536
537// Boot from a disk (either floppy or harddrive)
538static void
539boot_disk(u8 bootdrv, int checksig)
540{
541 u16 bootseg = 0x07c0;
542
543 // Read sector
544 struct bregs br;
545 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400546 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500547 br.dl = bootdrv;
548 br.es = bootseg;
549 br.ah = 2;
550 br.al = 1;
551 br.cl = 1;
552 call16_int(0x13, &br);
553
554 if (br.flags & F_CF) {
555 printf("Boot failed: could not read the boot disk\n\n");
556 return;
557 }
558
559 if (checksig) {
560 struct mbr_s *mbr = (void*)0;
561 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
562 printf("Boot failed: not a bootable disk\n\n");
563 return;
564 }
565 }
566
567 /* Canonicalize bootseg:bootip */
568 u16 bootip = (bootseg & 0x0fff) << 4;
569 bootseg &= 0xf000;
570
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500571 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500572}
573
574// Boot from a CD-ROM
575static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500576boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500577{
578 if (! CONFIG_CDROM_BOOT)
579 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500580 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200581
Gleb Natapov4c90a202010-12-07 13:50:54 +0200582 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500583 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500584 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500585 return;
586 }
587
588 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400589 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500590 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
591 /* Canonicalize bootseg:bootip */
592 u16 bootip = (bootseg & 0x0fff) << 4;
593 bootseg &= 0xf000;
594
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500595 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500596}
597
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400598// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400599static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500600boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400601{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400602 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400603 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500604 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500605 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400606}
607
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500608// Boot from a BEV entry on an optionrom.
609static void
610boot_rom(u32 vector)
611{
612 printf("Booting from ROM...\n");
613 struct segoff_s so;
614 so.segoff = vector;
615 call_boot_entry(so, 0);
616}
617
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400618// Unable to find bootable device - warn user and eventually retry.
619static void
620boot_fail(void)
621{
622 u32 retrytime = romfile_loadint("etc/boot-fail-wait", 60*1000);
623 if (retrytime == (u32)-1)
624 printf("No bootable device.\n");
625 else
626 printf("No bootable device. Retrying in %d seconds.\n", retrytime/1000);
627 // Wait for 'retrytime' milliseconds and then reboot.
628 u32 end = calc_future_timer(retrytime);
629 for (;;) {
630 if (retrytime != (u32)-1 && check_timer(end))
631 break;
632 wait_irq();
633 }
634 printf("Rebooting.\n");
635 struct bregs br;
636 memset(&br, 0, sizeof(br));
637 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
638 call16big(&br);
639}
640
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500641// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500642static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400643do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500644{
Kevin O'Connor40967022008-07-21 22:23:05 -0400645 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500646 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400647
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400648 if (seq_nr >= BEVCount)
649 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500650
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500651 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500652 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400653 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400654 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500655 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500656 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500657 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400658 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500659 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500660 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500661 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500662 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500663 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400664 break;
665 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500666 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500667 break;
668 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500669 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500670 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500671 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500672
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500673 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400674 struct bregs br;
675 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400676 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400677 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500678}
679
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400680int BootSequence VARLOW = -1;
681
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500682// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500683void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500684handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500685{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400686 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400687 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400688 int seq = BootSequence + 1;
689 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400690 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500691}
692
693// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500694void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500695handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500696{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400697 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400698 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400699 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400700 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500701}