blob: 354228389bd3394af488922728724dbfdfc8436a [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'Connor0ac5e9e2013-09-14 13:09:27 -04003// Copyright (C) 2008-2013 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'Connor38fcbfe2008-02-25 22:30:47 -05009#include "config.h" // CONFIG_*
Kevin O'Connor180a9592008-03-04 22:50:53 -050010#include "disk.h" // cdrom_boot
Kevin O'Connor9521e262008-07-04 13:04:29 -040011#include "bregs.h" // struct bregs
Kevin O'Connorf13a1802010-12-29 11:52:33 -050012#include "boot.h" // func defs
Kevin O'Connor5d369d82013-09-02 20:48:46 -040013#include "hw/cmos.h" // inb_cmos
Kevin O'Connorccee6e82013-09-02 21:25:21 -040014#include "fw/paravirt.h" // qemu_cfg_show_boot_menu
Kevin O'Connor5d369d82013-09-02 20:48:46 -040015#include "hw/pci.h" // pci_bdf_to_*
16#include "hw/usb.h" // struct usbdevice_s
Kevin O'Connorccee6e82013-09-02 21:25:21 -040017#include "fw/csm.h" // csm_bootprio_*
Kevin O'Connor446defb2013-06-08 21:50:53 -040018#include "list.h" // hlist_node
Kevin O'Connorc659fde2008-12-28 23:43:20 -050019
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050020
21/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050022 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050023 ****************************************************************/
24
Kevin O'Connor70c94dd2013-03-08 19:39:49 -050025static char **Bootorder VARVERIFY32INIT;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050026static int BootorderCount;
27
Kevin O'Connord1a17462010-12-24 11:17:03 -050028static void
29loadBootOrder(void)
30{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040031 if (!CONFIG_BOOTORDER)
32 return;
33
Kevin O'Connord1a17462010-12-24 11:17:03 -050034 char *f = romfile_loadfile("bootorder", NULL);
35 if (!f)
36 return;
37
Kevin O'Connor8bf55032011-01-01 11:00:42 -050038 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050039 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050040 while (f[i]) {
41 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050042 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050043 i++;
44 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050045 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
46 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050047 warn_noalloc();
48 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050049 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050050 return;
51 }
52
53 dprintf(3, "boot order:\n");
54 i = 0;
55 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050056 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050057 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050058 if (f)
59 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050060 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050061 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
62 i++;
63 } while (f);
64}
65
66// See if 'str' starts with 'glob' - if glob contains an '*' character
67// it will match any number of characters in str that aren't a '/' or
68// the next glob character.
69static char *
70glob_prefix(const char *glob, const char *str)
71{
72 for (;;) {
73 if (!*glob && (!*str || *str == '/'))
74 return (char*)str;
75 if (*glob == '*') {
76 if (!*str || *str == '/' || *str == glob[1])
77 glob++;
78 else
79 str++;
80 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050081 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050082 if (*glob != *str)
83 return NULL;
84 glob++;
85 str++;
86 }
87}
88
89// Search the bootorder list for the given glob pattern.
90static int
91find_prio(const char *glob)
92{
93 dprintf(1, "Searching bootorder for: %s\n", glob);
94 int i;
95 for (i = 0; i < BootorderCount; i++)
96 if (glob_prefix(glob, Bootorder[i]))
97 return i+1;
98 return -1;
99}
100
101#define FW_PCI_DOMAIN "/pci@i0cf8"
102
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400103static char *
104build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
105{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500106 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
107 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400108 if (pci->parent) {
109 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500110 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400111 if (pci->rootbus)
112 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500113 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
114 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500115
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400116 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500117 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500118 if (fn)
119 p += snprintf(p, buf+max-p, ",%x", fn);
120 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500121}
122
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400123int bootprio_find_pci_device(struct pci_device *pci)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500124{
David Woodhouse118469a2013-01-25 19:46:25 -0600125 if (CONFIG_CSM)
126 return csm_bootprio_pci(pci);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400127 if (!CONFIG_BOOTORDER)
128 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500129 // Find pci device - for example: /pci@i0cf8/ethernet@5
130 char desc[256];
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400131 build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500132 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500133}
134
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100135int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
136{
137 if (!CONFIG_BOOTORDER)
138 return -1;
139 if (!pci)
140 // support only pci machine for now
141 return -1;
142 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
143 char desc[256], *p;
144 p = build_pci_path(desc, sizeof(desc), "*", pci);
145 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
146 return find_prio(desc);
147}
148
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400149int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500150{
David Woodhouse118469a2013-01-25 19:46:25 -0600151 if (CONFIG_CSM)
152 return csm_bootprio_ata(pci, chanid, slave);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400153 if (!CONFIG_BOOTORDER)
154 return -1;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400155 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500156 // support only pci machine for now
157 return -1;
158 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
159 char desc[256], *p;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400160 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500161 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
162 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500163}
164
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400165int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500166{
David Woodhouse118469a2013-01-25 19:46:25 -0600167 if (CONFIG_CSM)
168 return csm_bootprio_fdc(pci, port, fdid);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400169 if (!CONFIG_BOOTORDER)
170 return -1;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400171 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500172 // support only pci machine for now
173 return -1;
174 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
175 char desc[256], *p;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400176 p = build_pci_path(desc, sizeof(desc), "isa", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500177 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
178 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500179}
180
Kevin O'Connorfce91892011-07-09 14:47:47 -0400181int bootprio_find_pci_rom(struct pci_device *pci, int instance)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500182{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400183 if (!CONFIG_BOOTORDER)
184 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500185 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
186 char desc[256], *p;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400187 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500188 if (instance)
189 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
190 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500191}
192
193int bootprio_find_named_rom(const char *name, int instance)
194{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400195 if (!CONFIG_BOOTORDER)
196 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500197 // Find named rom - for example: /rom@genroms/linuxboot.bin
198 char desc[256], *p;
199 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
200 if (instance)
201 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
202 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500203}
204
Kevin O'Connorea274782012-03-08 07:49:09 -0500205static char *
206build_usb_path(char *buf, int max, struct usbhub_s *hub)
207{
208 if (!hub->usbdev)
209 // Root hub - nothing to add.
210 return buf;
211 char *p = build_usb_path(buf, max, hub->usbdev->hub);
212 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
213 return p;
214}
215
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400216int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500217{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400218 if (!CONFIG_BOOTORDER)
219 return -1;
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400220 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500221 char desc[256], *p;
Kevin O'Connorea274782012-03-08 07:49:09 -0500222 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
223 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400224 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
225 , usbdev->port+1, lun);
226 int ret = find_prio(desc);
227 if (ret >= 0)
228 return ret;
229 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
230 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500231 return find_prio(desc);
232}
233
Kevin O'Connor031ef552010-12-27 19:26:57 -0500234
235/****************************************************************
236 * Boot setup
237 ****************************************************************/
238
Kevin O'Connor11a72342013-03-05 17:52:21 +0800239static int BootRetryTime;
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500240static int CheckFloppySig = 1;
241
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500242#define DEFAULT_PRIO 9999
243
244static int DefaultFloppyPrio = 101;
245static int DefaultCDPrio = 102;
246static int DefaultHDPrio = 103;
247static int DefaultBEVPrio = 104;
248
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500249void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500250boot_init(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500251{
252 if (! CONFIG_BOOT)
253 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500254
Kevin O'Connor897fb112013-02-07 23:32:48 -0500255 if (CONFIG_QEMU) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500256 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500257 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500258 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500259 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500260 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500261 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
262 = DefaultBEVPrio = DEFAULT_PRIO;
263 int i;
264 for (i=101; i<104; i++) {
265 u32 val = bootorder & 0x0f;
266 bootorder >>= 4;
267 switch (val) {
268 case 1: DefaultFloppyPrio = i; break;
269 case 2: DefaultHDPrio = i; break;
270 case 3: DefaultCDPrio = i; break;
271 case 4: DefaultBEVPrio = i; break;
272 }
273 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500274 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200275
Kevin O'Connor11a72342013-03-05 17:52:21 +0800276 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
277
Kevin O'Connord1a17462010-12-24 11:17:03 -0500278 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500279}
280
Kevin O'Connord1a17462010-12-24 11:17:03 -0500281
282/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500283 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500284 ****************************************************************/
285
Kevin O'Connor031ef552010-12-27 19:26:57 -0500286struct bootentry_s {
287 int type;
288 union {
289 u32 data;
290 struct segoff_s vector;
291 struct drive_s *drive;
292 };
293 int priority;
294 const char *description;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400295 struct hlist_node node;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500296};
Kevin O'Connor446defb2013-06-08 21:50:53 -0400297static struct hlist_head BootList VARVERIFY32INIT;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500298
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500299#define IPL_TYPE_FLOPPY 0x01
300#define IPL_TYPE_HARDDISK 0x02
301#define IPL_TYPE_CDROM 0x03
302#define IPL_TYPE_CBFS 0x20
303#define IPL_TYPE_BEV 0x80
304#define IPL_TYPE_BCV 0x81
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500305#define IPL_TYPE_HALT 0xf0
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500306
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500307static void
308bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500309{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500310 if (! CONFIG_BOOT)
311 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500312 struct bootentry_s *be = malloc_tmp(sizeof(*be));
313 if (!be) {
314 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500315 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500316 }
317 be->type = type;
318 be->priority = prio;
319 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500320 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500321 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
322 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500323
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500324 // Add entry in sorted order.
Kevin O'Connor446defb2013-06-08 21:50:53 -0400325 struct hlist_node **pprev;
326 struct bootentry_s *pos;
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400327 hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500328 if (be->priority < pos->priority)
329 break;
330 if (be->priority > pos->priority)
331 continue;
332 if (be->type < pos->type)
333 break;
334 if (be->type > pos->type)
335 continue;
336 if (be->type <= IPL_TYPE_CDROM
337 && (be->drive->type < pos->drive->type
338 || (be->drive->type == pos->drive->type
339 && be->drive->cntl_id < pos->drive->cntl_id)))
340 break;
341 }
Kevin O'Connor446defb2013-06-08 21:50:53 -0400342 hlist_add(&be->node, pprev);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500343}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500344
Kevin O'Connor031ef552010-12-27 19:26:57 -0500345// Return the given priority if it's set - defaultprio otherwise.
346static inline int defPrio(int priority, int defaultprio) {
347 return (priority < 0) ? defaultprio : priority;
348}
349
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500350// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200351void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500352boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200353{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500354 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
355 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500356 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
357 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200358}
359
Kevin O'Connor0a924122009-02-08 19:43:47 -0500360// Add a bcv entry for an expansion card harddrive or legacy option rom
361void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500362boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500363{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500364 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500365 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500366 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500367}
368
Kevin O'Connor0a924122009-02-08 19:43:47 -0500369void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500370boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500371{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500372 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500373 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500374}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500375
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500376void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500377boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500378{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500379 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500380 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500381}
382
383void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500384boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500385{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500386 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500387 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500388}
389
390// Add a CBFS payload entry
391void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500392boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500393{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500394 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500395}
396
397
398/****************************************************************
Kevin O'Connor0ac5e9e2013-09-14 13:09:27 -0400399 * Keyboard calls
400 ****************************************************************/
401
402// See if a keystroke is pending in the keyboard buffer.
403static int
404check_for_keystroke(void)
405{
406 struct bregs br;
407 memset(&br, 0, sizeof(br));
408 br.flags = F_IF|F_ZF;
409 br.ah = 1;
410 call16_int(0x16, &br);
411 return !(br.flags & F_ZF);
412}
413
414// Return a keystroke - waiting forever if necessary.
415static int
416get_raw_keystroke(void)
417{
418 struct bregs br;
419 memset(&br, 0, sizeof(br));
420 br.flags = F_IF;
421 call16_int(0x16, &br);
422 return br.ah;
423}
424
425// Read a keystroke - waiting up to 'msec' milliseconds.
426static int
427get_keystroke(int msec)
428{
429 u32 end = irqtimer_calc(msec);
430 for (;;) {
431 if (check_for_keystroke())
432 return get_raw_keystroke();
433 if (irqtimer_check(end))
434 return -1;
435 yield_toirq();
436 }
437}
438
439
440/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500441 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500442 ****************************************************************/
443
Kevin O'Connor73023002011-07-05 20:34:34 -0400444#define DEFAULT_BOOTMENU_WAIT 2500
445
Kevin O'Connor0a924122009-02-08 19:43:47 -0500446// Show IPL option menu.
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500447void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500448interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500449{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500450 // XXX - show available drives?
451
Kevin O'Connor56c50892013-02-09 19:25:51 -0500452 if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500453 return;
454
455 while (get_keystroke(0) >= 0)
456 ;
457
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400458 char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
459 int menukey = romfile_loadint("etc/boot-menu-key", 0x86);
460 printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n\n");
461 free(bootmsg);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500462
Kevin O'Connor73023002011-07-05 20:34:34 -0400463 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400464 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400465 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400466 disable_bootsplash();
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400467 if (scan_code != menukey)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500468 return;
469
470 while (get_keystroke(0) >= 0)
471 ;
472
473 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400474 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500475
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500476 // Show menu items
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500477 int maxmenu = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400478 struct bootentry_s *pos;
479 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500480 char desc[60];
481 maxmenu++;
482 printf("%d. %s\n", maxmenu
483 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500484 }
485
Kevin O'Connor551caa22010-12-29 13:25:18 -0500486 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500487 for (;;) {
488 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500489 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500490 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500491 }
492 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500493 if (scan_code == 0x01)
494 // ESC
495 return;
496
497 // Find entry and make top priority.
498 int choice = scan_code - 1;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400499 hlist_for_each_entry(pos, &BootList, node) {
500 if (! --choice)
501 break;
502 }
503 hlist_del(&pos->node);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500504 pos->priority = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400505 hlist_add_head(&pos->node, &BootList);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500506}
507
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500508// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500509struct bev_s {
510 int type;
511 u32 vector;
512};
513static struct bev_s BEV[20];
514static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500515static int HaveHDBoot, HaveFDBoot;
516
Kevin O'Connor0a924122009-02-08 19:43:47 -0500517static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500518add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500519{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500520 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
521 return;
522 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
523 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500524 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500525 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500526 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500527 bev->type = type;
528 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500529}
530
531// Prepare for boot - show menu and run bcvs.
532void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500533bcv_prepboot(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500534{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500535 if (! CONFIG_BOOT)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500536 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500537
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500538 int haltprio = find_prio("HALT");
539 if (haltprio >= 0)
540 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
541
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500542 // Map drives and populate BEV list
Kevin O'Connor446defb2013-06-08 21:50:53 -0400543 struct bootentry_s *pos;
544 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500545 switch (pos->type) {
546 case IPL_TYPE_BCV:
547 call_bcv(pos->vector.seg, pos->vector.offset);
548 add_bev(IPL_TYPE_HARDDISK, 0);
549 break;
550 case IPL_TYPE_FLOPPY:
551 map_floppy_drive(pos->drive);
552 add_bev(IPL_TYPE_FLOPPY, 0);
553 break;
554 case IPL_TYPE_HARDDISK:
555 map_hd_drive(pos->drive);
556 add_bev(IPL_TYPE_HARDDISK, 0);
557 break;
558 case IPL_TYPE_CDROM:
559 map_cd_drive(pos->drive);
560 // NO BREAK
561 default:
562 add_bev(pos->type, pos->data);
563 break;
564 }
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500565 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400566
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500567 // If nothing added a floppy/hd boot - add it manually.
568 add_bev(IPL_TYPE_FLOPPY, 0);
569 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500570}
571
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500572
573/****************************************************************
574 * Boot code (int 18/19)
575 ****************************************************************/
576
Kevin O'Connor0a924122009-02-08 19:43:47 -0500577// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500578static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500579call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500580{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500581 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500582 struct bregs br;
583 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400584 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500585 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500586 // Set the magic number in ax and the boot drive in dl.
587 br.dl = bootdrv;
588 br.ax = 0xaa55;
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400589 farcall16(&br);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500590}
591
592// Boot from a disk (either floppy or harddrive)
593static void
594boot_disk(u8 bootdrv, int checksig)
595{
596 u16 bootseg = 0x07c0;
597
598 // Read sector
599 struct bregs br;
600 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400601 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500602 br.dl = bootdrv;
603 br.es = bootseg;
604 br.ah = 2;
605 br.al = 1;
606 br.cl = 1;
607 call16_int(0x13, &br);
608
609 if (br.flags & F_CF) {
610 printf("Boot failed: could not read the boot disk\n\n");
611 return;
612 }
613
614 if (checksig) {
615 struct mbr_s *mbr = (void*)0;
616 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
617 printf("Boot failed: not a bootable disk\n\n");
618 return;
619 }
620 }
621
622 /* Canonicalize bootseg:bootip */
623 u16 bootip = (bootseg & 0x0fff) << 4;
624 bootseg &= 0xf000;
625
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500626 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500627}
628
629// Boot from a CD-ROM
630static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500631boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500632{
633 if (! CONFIG_CDROM_BOOT)
634 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500635 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200636
Gleb Natapov4c90a202010-12-07 13:50:54 +0200637 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500638 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500639 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500640 return;
641 }
642
Kevin O'Connord3140832012-05-13 22:46:12 -0400643 u8 bootdrv = CDEmu.emulated_extdrive;
644 u16 bootseg = CDEmu.load_segment;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500645 /* Canonicalize bootseg:bootip */
646 u16 bootip = (bootseg & 0x0fff) << 4;
647 bootseg &= 0xf000;
648
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500649 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500650}
651
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400652// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400653static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500654boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400655{
Kevin O'Connor897fb112013-02-07 23:32:48 -0500656 if (!CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400657 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500658 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500659 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400660}
661
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500662// Boot from a BEV entry on an optionrom.
663static void
664boot_rom(u32 vector)
665{
666 printf("Booting from ROM...\n");
667 struct segoff_s so;
668 so.segoff = vector;
669 call_boot_entry(so, 0);
670}
671
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400672// Unable to find bootable device - warn user and eventually retry.
673static void
674boot_fail(void)
675{
Kevin O'Connor11a72342013-03-05 17:52:21 +0800676 if (BootRetryTime == (u32)-1)
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400677 printf("No bootable device.\n");
678 else
Kevin O'Connor11a72342013-03-05 17:52:21 +0800679 printf("No bootable device. Retrying in %d seconds.\n"
680 , BootRetryTime/1000);
681 // Wait for 'BootRetryTime' milliseconds and then reboot.
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400682 u32 end = irqtimer_calc(BootRetryTime);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400683 for (;;) {
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400684 if (BootRetryTime != (u32)-1 && irqtimer_check(end))
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400685 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400686 yield_toirq();
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400687 }
688 printf("Rebooting.\n");
689 struct bregs br;
690 memset(&br, 0, sizeof(br));
691 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400692 farcall16big(&br);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400693}
694
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500695// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500696static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400697do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500698{
Kevin O'Connor40967022008-07-21 22:23:05 -0400699 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500700 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400701
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400702 if (seq_nr >= BEVCount)
703 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500704
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500705 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500706 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400707 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400708 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500709 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500710 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500711 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400712 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500713 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500714 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500715 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500716 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500717 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400718 break;
719 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500720 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500721 break;
722 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500723 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500724 break;
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500725 case IPL_TYPE_HALT:
726 boot_fail();
727 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500728 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500729
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500730 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400731 struct bregs br;
732 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400733 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400734 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500735}
736
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400737int BootSequence VARLOW = -1;
738
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500739// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500740void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500741handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500742{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500743 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400744 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400745 int seq = BootSequence + 1;
746 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400747 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500748}
749
750// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500751void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500752handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500753{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500754 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400755 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400756 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400757 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500758}