blob: 08876fa4d9d53fa8424dabea55bfc1506baa5576 [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'Connor41639f82013-09-14 19:37:36 -040019#include "romfile.h" // romfile_loadint
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040020#include "string.h" // memset
Kevin O'Connorc659fde2008-12-28 23:43:20 -050021
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050022
23/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050024 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050025 ****************************************************************/
26
Kevin O'Connor70c94dd2013-03-08 19:39:49 -050027static char **Bootorder VARVERIFY32INIT;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050028static int BootorderCount;
29
Kevin O'Connord1a17462010-12-24 11:17:03 -050030static void
31loadBootOrder(void)
32{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040033 if (!CONFIG_BOOTORDER)
34 return;
35
Kevin O'Connord1a17462010-12-24 11:17:03 -050036 char *f = romfile_loadfile("bootorder", NULL);
37 if (!f)
38 return;
39
Kevin O'Connor8bf55032011-01-01 11:00:42 -050040 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050041 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050042 while (f[i]) {
43 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050044 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050045 i++;
46 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050047 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
48 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050049 warn_noalloc();
50 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050051 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050052 return;
53 }
54
55 dprintf(3, "boot order:\n");
56 i = 0;
57 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050058 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050059 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050060 if (f)
61 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050062 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050063 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
64 i++;
65 } while (f);
66}
67
68// See if 'str' starts with 'glob' - if glob contains an '*' character
69// it will match any number of characters in str that aren't a '/' or
70// the next glob character.
71static char *
72glob_prefix(const char *glob, const char *str)
73{
74 for (;;) {
75 if (!*glob && (!*str || *str == '/'))
76 return (char*)str;
77 if (*glob == '*') {
78 if (!*str || *str == '/' || *str == glob[1])
79 glob++;
80 else
81 str++;
82 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050083 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050084 if (*glob != *str)
85 return NULL;
86 glob++;
87 str++;
88 }
89}
90
91// Search the bootorder list for the given glob pattern.
92static int
93find_prio(const char *glob)
94{
95 dprintf(1, "Searching bootorder for: %s\n", glob);
96 int i;
97 for (i = 0; i < BootorderCount; i++)
98 if (glob_prefix(glob, Bootorder[i]))
99 return i+1;
100 return -1;
101}
102
103#define FW_PCI_DOMAIN "/pci@i0cf8"
104
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400105static char *
106build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
107{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500108 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
109 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400110 if (pci->parent) {
111 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500112 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400113 if (pci->rootbus)
114 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500115 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
116 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500117
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400118 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500119 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500120 if (fn)
121 p += snprintf(p, buf+max-p, ",%x", fn);
122 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500123}
124
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400125int bootprio_find_pci_device(struct pci_device *pci)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500126{
David Woodhouse118469a2013-01-25 19:46:25 -0600127 if (CONFIG_CSM)
128 return csm_bootprio_pci(pci);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400129 if (!CONFIG_BOOTORDER)
130 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500131 // Find pci device - for example: /pci@i0cf8/ethernet@5
132 char desc[256];
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400133 build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500134 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500135}
136
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100137int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
138{
139 if (!CONFIG_BOOTORDER)
140 return -1;
141 if (!pci)
142 // support only pci machine for now
143 return -1;
144 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
145 char desc[256], *p;
146 p = build_pci_path(desc, sizeof(desc), "*", pci);
147 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
148 return find_prio(desc);
149}
150
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400151int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500152{
David Woodhouse118469a2013-01-25 19:46:25 -0600153 if (CONFIG_CSM)
154 return csm_bootprio_ata(pci, chanid, slave);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400155 if (!CONFIG_BOOTORDER)
156 return -1;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400157 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500158 // support only pci machine for now
159 return -1;
160 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
161 char desc[256], *p;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400162 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500163 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
164 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500165}
166
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400167int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500168{
David Woodhouse118469a2013-01-25 19:46:25 -0600169 if (CONFIG_CSM)
170 return csm_bootprio_fdc(pci, port, fdid);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400171 if (!CONFIG_BOOTORDER)
172 return -1;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400173 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500174 // support only pci machine for now
175 return -1;
176 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
177 char desc[256], *p;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400178 p = build_pci_path(desc, sizeof(desc), "isa", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500179 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
180 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500181}
182
Kevin O'Connorfce91892011-07-09 14:47:47 -0400183int bootprio_find_pci_rom(struct pci_device *pci, int instance)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500184{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400185 if (!CONFIG_BOOTORDER)
186 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500187 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
188 char desc[256], *p;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400189 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500190 if (instance)
191 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
192 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500193}
194
195int bootprio_find_named_rom(const char *name, int instance)
196{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400197 if (!CONFIG_BOOTORDER)
198 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500199 // Find named rom - for example: /rom@genroms/linuxboot.bin
200 char desc[256], *p;
201 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
202 if (instance)
203 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
204 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500205}
206
Kevin O'Connorea274782012-03-08 07:49:09 -0500207static char *
208build_usb_path(char *buf, int max, struct usbhub_s *hub)
209{
210 if (!hub->usbdev)
211 // Root hub - nothing to add.
212 return buf;
213 char *p = build_usb_path(buf, max, hub->usbdev->hub);
214 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
215 return p;
216}
217
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400218int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500219{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400220 if (!CONFIG_BOOTORDER)
221 return -1;
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400222 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500223 char desc[256], *p;
Kevin O'Connorea274782012-03-08 07:49:09 -0500224 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
225 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400226 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
227 , usbdev->port+1, lun);
228 int ret = find_prio(desc);
229 if (ret >= 0)
230 return ret;
231 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
232 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500233 return find_prio(desc);
234}
235
Kevin O'Connor031ef552010-12-27 19:26:57 -0500236
237/****************************************************************
238 * Boot setup
239 ****************************************************************/
240
Kevin O'Connor11a72342013-03-05 17:52:21 +0800241static int BootRetryTime;
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500242static int CheckFloppySig = 1;
243
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500244#define DEFAULT_PRIO 9999
245
246static int DefaultFloppyPrio = 101;
247static int DefaultCDPrio = 102;
248static int DefaultHDPrio = 103;
249static int DefaultBEVPrio = 104;
250
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500251void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500252boot_init(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500253{
254 if (! CONFIG_BOOT)
255 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500256
Kevin O'Connor897fb112013-02-07 23:32:48 -0500257 if (CONFIG_QEMU) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500258 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500259 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500260 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500261 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500262 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500263 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
264 = DefaultBEVPrio = DEFAULT_PRIO;
265 int i;
266 for (i=101; i<104; i++) {
267 u32 val = bootorder & 0x0f;
268 bootorder >>= 4;
269 switch (val) {
270 case 1: DefaultFloppyPrio = i; break;
271 case 2: DefaultHDPrio = i; break;
272 case 3: DefaultCDPrio = i; break;
273 case 4: DefaultBEVPrio = i; break;
274 }
275 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500276 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200277
Kevin O'Connor11a72342013-03-05 17:52:21 +0800278 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
279
Kevin O'Connord1a17462010-12-24 11:17:03 -0500280 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500281}
282
Kevin O'Connord1a17462010-12-24 11:17:03 -0500283
284/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500285 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500286 ****************************************************************/
287
Kevin O'Connor031ef552010-12-27 19:26:57 -0500288struct bootentry_s {
289 int type;
290 union {
291 u32 data;
292 struct segoff_s vector;
293 struct drive_s *drive;
294 };
295 int priority;
296 const char *description;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400297 struct hlist_node node;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500298};
Kevin O'Connor446defb2013-06-08 21:50:53 -0400299static struct hlist_head BootList VARVERIFY32INIT;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500300
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500301#define IPL_TYPE_FLOPPY 0x01
302#define IPL_TYPE_HARDDISK 0x02
303#define IPL_TYPE_CDROM 0x03
304#define IPL_TYPE_CBFS 0x20
305#define IPL_TYPE_BEV 0x80
306#define IPL_TYPE_BCV 0x81
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500307#define IPL_TYPE_HALT 0xf0
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500308
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500309static void
310bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500311{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500312 if (! CONFIG_BOOT)
313 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500314 struct bootentry_s *be = malloc_tmp(sizeof(*be));
315 if (!be) {
316 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500317 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500318 }
319 be->type = type;
320 be->priority = prio;
321 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500322 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500323 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
324 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500325
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500326 // Add entry in sorted order.
Kevin O'Connor446defb2013-06-08 21:50:53 -0400327 struct hlist_node **pprev;
328 struct bootentry_s *pos;
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400329 hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500330 if (be->priority < pos->priority)
331 break;
332 if (be->priority > pos->priority)
333 continue;
334 if (be->type < pos->type)
335 break;
336 if (be->type > pos->type)
337 continue;
338 if (be->type <= IPL_TYPE_CDROM
339 && (be->drive->type < pos->drive->type
340 || (be->drive->type == pos->drive->type
341 && be->drive->cntl_id < pos->drive->cntl_id)))
342 break;
343 }
Kevin O'Connor446defb2013-06-08 21:50:53 -0400344 hlist_add(&be->node, pprev);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500345}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500346
Kevin O'Connor031ef552010-12-27 19:26:57 -0500347// Return the given priority if it's set - defaultprio otherwise.
348static inline int defPrio(int priority, int defaultprio) {
349 return (priority < 0) ? defaultprio : priority;
350}
351
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500352// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200353void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500354boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200355{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500356 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
357 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500358 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
359 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200360}
361
Kevin O'Connor0a924122009-02-08 19:43:47 -0500362// Add a bcv entry for an expansion card harddrive or legacy option rom
363void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500364boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500365{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500366 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500367 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500368 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500369}
370
Kevin O'Connor0a924122009-02-08 19:43:47 -0500371void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500372boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500373{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500374 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500375 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500376}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500377
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500378void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500379boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500380{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500381 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500382 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500383}
384
385void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500386boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500387{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500388 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500389 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500390}
391
392// Add a CBFS payload entry
393void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500394boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500395{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500396 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500397}
398
399
400/****************************************************************
Kevin O'Connor0ac5e9e2013-09-14 13:09:27 -0400401 * Keyboard calls
402 ****************************************************************/
403
404// See if a keystroke is pending in the keyboard buffer.
405static int
406check_for_keystroke(void)
407{
408 struct bregs br;
409 memset(&br, 0, sizeof(br));
410 br.flags = F_IF|F_ZF;
411 br.ah = 1;
412 call16_int(0x16, &br);
413 return !(br.flags & F_ZF);
414}
415
416// Return a keystroke - waiting forever if necessary.
417static int
418get_raw_keystroke(void)
419{
420 struct bregs br;
421 memset(&br, 0, sizeof(br));
422 br.flags = F_IF;
423 call16_int(0x16, &br);
424 return br.ah;
425}
426
427// Read a keystroke - waiting up to 'msec' milliseconds.
428static int
429get_keystroke(int msec)
430{
431 u32 end = irqtimer_calc(msec);
432 for (;;) {
433 if (check_for_keystroke())
434 return get_raw_keystroke();
435 if (irqtimer_check(end))
436 return -1;
437 yield_toirq();
438 }
439}
440
441
442/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500443 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500444 ****************************************************************/
445
Kevin O'Connor73023002011-07-05 20:34:34 -0400446#define DEFAULT_BOOTMENU_WAIT 2500
447
Kevin O'Connor0a924122009-02-08 19:43:47 -0500448// Show IPL option menu.
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500449void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500450interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500451{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500452 // XXX - show available drives?
453
Kevin O'Connor56c50892013-02-09 19:25:51 -0500454 if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500455 return;
456
457 while (get_keystroke(0) >= 0)
458 ;
459
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400460 char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
461 int menukey = romfile_loadint("etc/boot-menu-key", 0x86);
462 printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n\n");
463 free(bootmsg);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500464
Kevin O'Connor73023002011-07-05 20:34:34 -0400465 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400466 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400467 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400468 disable_bootsplash();
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400469 if (scan_code != menukey)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500470 return;
471
472 while (get_keystroke(0) >= 0)
473 ;
474
475 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400476 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500477
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500478 // Show menu items
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500479 int maxmenu = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400480 struct bootentry_s *pos;
481 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500482 char desc[60];
483 maxmenu++;
484 printf("%d. %s\n", maxmenu
485 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500486 }
487
Kevin O'Connor551caa22010-12-29 13:25:18 -0500488 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500489 for (;;) {
490 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500491 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500492 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500493 }
494 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500495 if (scan_code == 0x01)
496 // ESC
497 return;
498
499 // Find entry and make top priority.
500 int choice = scan_code - 1;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400501 hlist_for_each_entry(pos, &BootList, node) {
502 if (! --choice)
503 break;
504 }
505 hlist_del(&pos->node);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500506 pos->priority = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400507 hlist_add_head(&pos->node, &BootList);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500508}
509
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500510// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500511struct bev_s {
512 int type;
513 u32 vector;
514};
515static struct bev_s BEV[20];
516static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500517static int HaveHDBoot, HaveFDBoot;
518
Kevin O'Connor0a924122009-02-08 19:43:47 -0500519static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500520add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500521{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500522 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
523 return;
524 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
525 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500526 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500527 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500528 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500529 bev->type = type;
530 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500531}
532
533// Prepare for boot - show menu and run bcvs.
534void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500535bcv_prepboot(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500536{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500537 if (! CONFIG_BOOT)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500538 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500539
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500540 int haltprio = find_prio("HALT");
541 if (haltprio >= 0)
542 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
543
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500544 // Map drives and populate BEV list
Kevin O'Connor446defb2013-06-08 21:50:53 -0400545 struct bootentry_s *pos;
546 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500547 switch (pos->type) {
548 case IPL_TYPE_BCV:
549 call_bcv(pos->vector.seg, pos->vector.offset);
550 add_bev(IPL_TYPE_HARDDISK, 0);
551 break;
552 case IPL_TYPE_FLOPPY:
553 map_floppy_drive(pos->drive);
554 add_bev(IPL_TYPE_FLOPPY, 0);
555 break;
556 case IPL_TYPE_HARDDISK:
557 map_hd_drive(pos->drive);
558 add_bev(IPL_TYPE_HARDDISK, 0);
559 break;
560 case IPL_TYPE_CDROM:
561 map_cd_drive(pos->drive);
562 // NO BREAK
563 default:
564 add_bev(pos->type, pos->data);
565 break;
566 }
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500567 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400568
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500569 // If nothing added a floppy/hd boot - add it manually.
570 add_bev(IPL_TYPE_FLOPPY, 0);
571 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500572}
573
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500574
575/****************************************************************
576 * Boot code (int 18/19)
577 ****************************************************************/
578
Kevin O'Connor0a924122009-02-08 19:43:47 -0500579// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500580static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500581call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500582{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500583 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500584 struct bregs br;
585 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400586 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500587 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500588 // Set the magic number in ax and the boot drive in dl.
589 br.dl = bootdrv;
590 br.ax = 0xaa55;
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400591 farcall16(&br);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500592}
593
594// Boot from a disk (either floppy or harddrive)
595static void
596boot_disk(u8 bootdrv, int checksig)
597{
598 u16 bootseg = 0x07c0;
599
600 // Read sector
601 struct bregs br;
602 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400603 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500604 br.dl = bootdrv;
605 br.es = bootseg;
606 br.ah = 2;
607 br.al = 1;
608 br.cl = 1;
609 call16_int(0x13, &br);
610
611 if (br.flags & F_CF) {
612 printf("Boot failed: could not read the boot disk\n\n");
613 return;
614 }
615
616 if (checksig) {
617 struct mbr_s *mbr = (void*)0;
618 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
619 printf("Boot failed: not a bootable disk\n\n");
620 return;
621 }
622 }
623
624 /* Canonicalize bootseg:bootip */
625 u16 bootip = (bootseg & 0x0fff) << 4;
626 bootseg &= 0xf000;
627
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500628 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500629}
630
631// Boot from a CD-ROM
632static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500633boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500634{
635 if (! CONFIG_CDROM_BOOT)
636 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500637 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200638
Gleb Natapov4c90a202010-12-07 13:50:54 +0200639 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500640 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500641 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500642 return;
643 }
644
Kevin O'Connord3140832012-05-13 22:46:12 -0400645 u8 bootdrv = CDEmu.emulated_extdrive;
646 u16 bootseg = CDEmu.load_segment;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500647 /* Canonicalize bootseg:bootip */
648 u16 bootip = (bootseg & 0x0fff) << 4;
649 bootseg &= 0xf000;
650
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500651 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500652}
653
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400654// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400655static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500656boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400657{
Kevin O'Connor897fb112013-02-07 23:32:48 -0500658 if (!CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400659 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500660 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500661 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400662}
663
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500664// Boot from a BEV entry on an optionrom.
665static void
666boot_rom(u32 vector)
667{
668 printf("Booting from ROM...\n");
669 struct segoff_s so;
670 so.segoff = vector;
671 call_boot_entry(so, 0);
672}
673
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400674// Unable to find bootable device - warn user and eventually retry.
675static void
676boot_fail(void)
677{
Kevin O'Connor11a72342013-03-05 17:52:21 +0800678 if (BootRetryTime == (u32)-1)
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400679 printf("No bootable device.\n");
680 else
Kevin O'Connor11a72342013-03-05 17:52:21 +0800681 printf("No bootable device. Retrying in %d seconds.\n"
682 , BootRetryTime/1000);
683 // Wait for 'BootRetryTime' milliseconds and then reboot.
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400684 u32 end = irqtimer_calc(BootRetryTime);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400685 for (;;) {
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400686 if (BootRetryTime != (u32)-1 && irqtimer_check(end))
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400687 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400688 yield_toirq();
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400689 }
690 printf("Rebooting.\n");
691 struct bregs br;
692 memset(&br, 0, sizeof(br));
693 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400694 farcall16big(&br);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400695}
696
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500697// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500698static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400699do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500700{
Kevin O'Connor40967022008-07-21 22:23:05 -0400701 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500702 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400703
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400704 if (seq_nr >= BEVCount)
705 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500706
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500707 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500708 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400709 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400710 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500711 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500712 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500713 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400714 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500715 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500716 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500717 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500718 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500719 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400720 break;
721 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500722 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500723 break;
724 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500725 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500726 break;
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500727 case IPL_TYPE_HALT:
728 boot_fail();
729 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500730 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500731
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500732 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400733 struct bregs br;
734 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400735 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400736 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500737}
738
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400739int BootSequence VARLOW = -1;
740
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500741// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500742void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500743handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500744{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500745 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400746 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400747 int seq = BootSequence + 1;
748 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400749 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500750}
751
752// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500753void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500754handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500755{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500756 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400757 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400758 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400759 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500760}