blob: 8cc94b0e005955c855397908e869a5f64b102633 [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'Connor9f4e1d92009-02-08 15:44:08 -0500241 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500242
243 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500244 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500245 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500246 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500247 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500248 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500249 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
250 = DefaultBEVPrio = DEFAULT_PRIO;
251 int i;
252 for (i=101; i<104; i++) {
253 u32 val = bootorder & 0x0f;
254 bootorder >>= 4;
255 switch (val) {
256 case 1: DefaultFloppyPrio = i; break;
257 case 2: DefaultHDPrio = i; break;
258 case 3: DefaultCDPrio = i; break;
259 case 4: DefaultBEVPrio = i; break;
260 }
261 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500262 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200263
Kevin O'Connord1a17462010-12-24 11:17:03 -0500264 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500265}
266
Kevin O'Connord1a17462010-12-24 11:17:03 -0500267
268/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500269 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500270 ****************************************************************/
271
Kevin O'Connor031ef552010-12-27 19:26:57 -0500272struct bootentry_s {
273 int type;
274 union {
275 u32 data;
276 struct segoff_s vector;
277 struct drive_s *drive;
278 };
279 int priority;
280 const char *description;
281 struct bootentry_s *next;
282};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500283static struct bootentry_s *BootList;
284
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500285#define IPL_TYPE_FLOPPY 0x01
286#define IPL_TYPE_HARDDISK 0x02
287#define IPL_TYPE_CDROM 0x03
288#define IPL_TYPE_CBFS 0x20
289#define IPL_TYPE_BEV 0x80
290#define IPL_TYPE_BCV 0x81
291
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500292static void
293bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500294{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500295 if (! CONFIG_BOOT)
296 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500297 struct bootentry_s *be = malloc_tmp(sizeof(*be));
298 if (!be) {
299 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500300 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500301 }
302 be->type = type;
303 be->priority = prio;
304 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500305 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500306 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
307 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500308
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500309 // Add entry in sorted order.
310 struct bootentry_s **pprev;
311 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
312 struct bootentry_s *pos = *pprev;
313 if (be->priority < pos->priority)
314 break;
315 if (be->priority > pos->priority)
316 continue;
317 if (be->type < pos->type)
318 break;
319 if (be->type > pos->type)
320 continue;
321 if (be->type <= IPL_TYPE_CDROM
322 && (be->drive->type < pos->drive->type
323 || (be->drive->type == pos->drive->type
324 && be->drive->cntl_id < pos->drive->cntl_id)))
325 break;
326 }
327 be->next = *pprev;
328 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500329}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500330
Kevin O'Connor031ef552010-12-27 19:26:57 -0500331// Return the given priority if it's set - defaultprio otherwise.
332static inline int defPrio(int priority, int defaultprio) {
333 return (priority < 0) ? defaultprio : priority;
334}
335
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500336// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200337void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500338boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200339{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500340 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
341 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500342 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
343 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200344}
345
Kevin O'Connor0a924122009-02-08 19:43:47 -0500346// Add a bcv entry for an expansion card harddrive or legacy option rom
347void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500348boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500349{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500350 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500351 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500352 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500353}
354
Kevin O'Connor0a924122009-02-08 19:43:47 -0500355void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500356boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500357{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500358 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500359 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500360}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500361
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500362void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500363boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500364{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500365 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500366 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500367}
368
369void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500370boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500371{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500372 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500373 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500374}
375
376// Add a CBFS payload entry
377void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500378boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500379{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500380 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500381}
382
383
384/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500385 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500386 ****************************************************************/
387
Kevin O'Connor73023002011-07-05 20:34:34 -0400388#define DEFAULT_BOOTMENU_WAIT 2500
389
Kevin O'Connor0a924122009-02-08 19:43:47 -0500390// Show IPL option menu.
391static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500392interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500393{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400394 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500395 return;
396
397 while (get_keystroke(0) >= 0)
398 ;
399
400 printf("Press F12 for boot menu.\n\n");
401
Kevin O'Connor73023002011-07-05 20:34:34 -0400402 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400403 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400404 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400405 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500406 if (scan_code != 0x86)
407 /* not F12 */
408 return;
409
410 while (get_keystroke(0) >= 0)
411 ;
412
413 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400414 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500415
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500416 // Show menu items
417 struct bootentry_s *pos = BootList;
418 int maxmenu = 0;
419 while (pos) {
420 char desc[60];
421 maxmenu++;
422 printf("%d. %s\n", maxmenu
423 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
424 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500425 }
426
Kevin O'Connor551caa22010-12-29 13:25:18 -0500427 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500428 for (;;) {
429 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500430 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500431 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500432 }
433 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500434 if (scan_code == 0x01)
435 // ESC
436 return;
437
438 // Find entry and make top priority.
439 int choice = scan_code - 1;
440 struct bootentry_s **pprev = &BootList;
441 while (--choice)
442 pprev = &(*pprev)->next;
443 pos = *pprev;
444 *pprev = pos->next;
445 pos->next = BootList;
446 BootList = pos;
447 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500448}
449
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500450// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500451struct bev_s {
452 int type;
453 u32 vector;
454};
455static struct bev_s BEV[20];
456static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500457static int HaveHDBoot, HaveFDBoot;
458
Kevin O'Connor0a924122009-02-08 19:43:47 -0500459static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500460add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500461{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500462 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
463 return;
464 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
465 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500466 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500467 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500468 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500469 bev->type = type;
470 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500471}
472
473// Prepare for boot - show menu and run bcvs.
474void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500475boot_prep(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500476{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400477 if (! CONFIG_BOOT) {
478 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500479 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400480 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500481
Kevin O'Connora5826b52009-10-24 17:57:29 -0400482 // XXX - show available drives?
483
Kevin O'Connor0a924122009-02-08 19:43:47 -0500484 // Allow user to modify BCV/IPL order.
485 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400486 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500487
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500488 // Map drives and populate BEV list
489 struct bootentry_s *pos = BootList;
490 while (pos) {
491 switch (pos->type) {
492 case IPL_TYPE_BCV:
493 call_bcv(pos->vector.seg, pos->vector.offset);
494 add_bev(IPL_TYPE_HARDDISK, 0);
495 break;
496 case IPL_TYPE_FLOPPY:
497 map_floppy_drive(pos->drive);
498 add_bev(IPL_TYPE_FLOPPY, 0);
499 break;
500 case IPL_TYPE_HARDDISK:
501 map_hd_drive(pos->drive);
502 add_bev(IPL_TYPE_HARDDISK, 0);
503 break;
504 case IPL_TYPE_CDROM:
505 map_cd_drive(pos->drive);
506 // NO BREAK
507 default:
508 add_bev(pos->type, pos->data);
509 break;
510 }
511 pos = pos->next;
512 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400513
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500514 // If nothing added a floppy/hd boot - add it manually.
515 add_bev(IPL_TYPE_FLOPPY, 0);
516 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500517}
518
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500519
520/****************************************************************
521 * Boot code (int 18/19)
522 ****************************************************************/
523
Kevin O'Connor0a924122009-02-08 19:43:47 -0500524// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500525static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500526call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500527{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500528 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500529 struct bregs br;
530 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400531 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500532 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500533 // Set the magic number in ax and the boot drive in dl.
534 br.dl = bootdrv;
535 br.ax = 0xaa55;
536 call16(&br);
537}
538
539// Boot from a disk (either floppy or harddrive)
540static void
541boot_disk(u8 bootdrv, int checksig)
542{
543 u16 bootseg = 0x07c0;
544
545 // Read sector
546 struct bregs br;
547 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400548 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500549 br.dl = bootdrv;
550 br.es = bootseg;
551 br.ah = 2;
552 br.al = 1;
553 br.cl = 1;
554 call16_int(0x13, &br);
555
556 if (br.flags & F_CF) {
557 printf("Boot failed: could not read the boot disk\n\n");
558 return;
559 }
560
561 if (checksig) {
562 struct mbr_s *mbr = (void*)0;
563 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
564 printf("Boot failed: not a bootable disk\n\n");
565 return;
566 }
567 }
568
569 /* Canonicalize bootseg:bootip */
570 u16 bootip = (bootseg & 0x0fff) << 4;
571 bootseg &= 0xf000;
572
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500573 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500574}
575
576// Boot from a CD-ROM
577static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500578boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500579{
580 if (! CONFIG_CDROM_BOOT)
581 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500582 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200583
Gleb Natapov4c90a202010-12-07 13:50:54 +0200584 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500585 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500586 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500587 return;
588 }
589
590 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor669e6442009-08-11 22:36:30 -0400591 u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500592 u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment);
593 /* Canonicalize bootseg:bootip */
594 u16 bootip = (bootseg & 0x0fff) << 4;
595 bootseg &= 0xf000;
596
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500597 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500598}
599
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400600// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400601static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500602boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400603{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400604 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400605 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500606 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500607 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400608}
609
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500610// Boot from a BEV entry on an optionrom.
611static void
612boot_rom(u32 vector)
613{
614 printf("Booting from ROM...\n");
615 struct segoff_s so;
616 so.segoff = vector;
617 call_boot_entry(so, 0);
618}
619
620// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500621static void
622do_boot(u16 seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500623{
Kevin O'Connor40967022008-07-21 22:23:05 -0400624 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500625 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400626
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500627 if (seq_nr >= BEVCount) {
Kevin O'Connorff8c1412009-04-18 17:02:41 -0400628 printf("No bootable device.\n");
629 // Loop with irqs enabled - this allows ctrl+alt+delete to work.
630 for (;;)
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400631 wait_irq();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500632 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500633
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500634 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500635 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400636 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400637 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500638 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500639 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500640 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400641 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500642 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500643 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500644 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500645 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500646 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400647 break;
648 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500649 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500650 break;
651 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500652 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500653 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500654 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500655
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500656 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400657 struct bregs br;
658 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400659 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400660 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500661}
662
663// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500664void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500665handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500666{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400667 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400668 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor08815372008-12-29 21:16:31 -0500669 u16 ebda_seg = get_ebda_seg();
670 u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1;
671 SET_EBDA2(ebda_seg, boot_sequence, seq);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400672 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500673}
674
675// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500676void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500677handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500678{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400679 debug_serial_setup();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400680 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor08815372008-12-29 21:16:31 -0500681 SET_EBDA(boot_sequence, 0);
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400682 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500683}