blob: 68a20d9d38c923bdfe6fb8b43f8ed82b8a4e0e7a [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'Connorfa9c66a2013-09-14 19:10:40 -040019#include "string.h" // memset
Kevin O'Connorc659fde2008-12-28 23:43:20 -050020
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050021
22/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050023 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050024 ****************************************************************/
25
Kevin O'Connor70c94dd2013-03-08 19:39:49 -050026static char **Bootorder VARVERIFY32INIT;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050027static int BootorderCount;
28
Kevin O'Connord1a17462010-12-24 11:17:03 -050029static void
30loadBootOrder(void)
31{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040032 if (!CONFIG_BOOTORDER)
33 return;
34
Kevin O'Connord1a17462010-12-24 11:17:03 -050035 char *f = romfile_loadfile("bootorder", NULL);
36 if (!f)
37 return;
38
Kevin O'Connor8bf55032011-01-01 11:00:42 -050039 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050040 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050041 while (f[i]) {
42 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050043 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050044 i++;
45 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050046 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
47 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050048 warn_noalloc();
49 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050050 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050051 return;
52 }
53
54 dprintf(3, "boot order:\n");
55 i = 0;
56 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050057 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050058 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050059 if (f)
60 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050061 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050062 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
63 i++;
64 } while (f);
65}
66
67// See if 'str' starts with 'glob' - if glob contains an '*' character
68// it will match any number of characters in str that aren't a '/' or
69// the next glob character.
70static char *
71glob_prefix(const char *glob, const char *str)
72{
73 for (;;) {
74 if (!*glob && (!*str || *str == '/'))
75 return (char*)str;
76 if (*glob == '*') {
77 if (!*str || *str == '/' || *str == glob[1])
78 glob++;
79 else
80 str++;
81 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050082 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050083 if (*glob != *str)
84 return NULL;
85 glob++;
86 str++;
87 }
88}
89
90// Search the bootorder list for the given glob pattern.
91static int
92find_prio(const char *glob)
93{
94 dprintf(1, "Searching bootorder for: %s\n", glob);
95 int i;
96 for (i = 0; i < BootorderCount; i++)
97 if (glob_prefix(glob, Bootorder[i]))
98 return i+1;
99 return -1;
100}
101
102#define FW_PCI_DOMAIN "/pci@i0cf8"
103
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400104static char *
105build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
106{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500107 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
108 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400109 if (pci->parent) {
110 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500111 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400112 if (pci->rootbus)
113 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500114 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
115 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500116
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400117 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500118 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500119 if (fn)
120 p += snprintf(p, buf+max-p, ",%x", fn);
121 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500122}
123
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400124int bootprio_find_pci_device(struct pci_device *pci)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500125{
David Woodhouse118469a2013-01-25 19:46:25 -0600126 if (CONFIG_CSM)
127 return csm_bootprio_pci(pci);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400128 if (!CONFIG_BOOTORDER)
129 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500130 // Find pci device - for example: /pci@i0cf8/ethernet@5
131 char desc[256];
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400132 build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500133 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500134}
135
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100136int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
137{
138 if (!CONFIG_BOOTORDER)
139 return -1;
140 if (!pci)
141 // support only pci machine for now
142 return -1;
143 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
144 char desc[256], *p;
145 p = build_pci_path(desc, sizeof(desc), "*", pci);
146 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
147 return find_prio(desc);
148}
149
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400150int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500151{
David Woodhouse118469a2013-01-25 19:46:25 -0600152 if (CONFIG_CSM)
153 return csm_bootprio_ata(pci, chanid, slave);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400154 if (!CONFIG_BOOTORDER)
155 return -1;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400156 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500157 // support only pci machine for now
158 return -1;
159 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
160 char desc[256], *p;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400161 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500162 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
163 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500164}
165
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400166int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500167{
David Woodhouse118469a2013-01-25 19:46:25 -0600168 if (CONFIG_CSM)
169 return csm_bootprio_fdc(pci, port, fdid);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400170 if (!CONFIG_BOOTORDER)
171 return -1;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400172 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500173 // support only pci machine for now
174 return -1;
175 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
176 char desc[256], *p;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400177 p = build_pci_path(desc, sizeof(desc), "isa", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500178 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
179 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500180}
181
Kevin O'Connorfce91892011-07-09 14:47:47 -0400182int bootprio_find_pci_rom(struct pci_device *pci, int instance)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500183{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400184 if (!CONFIG_BOOTORDER)
185 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500186 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
187 char desc[256], *p;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400188 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500189 if (instance)
190 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
191 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500192}
193
194int bootprio_find_named_rom(const char *name, int instance)
195{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400196 if (!CONFIG_BOOTORDER)
197 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500198 // Find named rom - for example: /rom@genroms/linuxboot.bin
199 char desc[256], *p;
200 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
201 if (instance)
202 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
203 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500204}
205
Kevin O'Connorea274782012-03-08 07:49:09 -0500206static char *
207build_usb_path(char *buf, int max, struct usbhub_s *hub)
208{
209 if (!hub->usbdev)
210 // Root hub - nothing to add.
211 return buf;
212 char *p = build_usb_path(buf, max, hub->usbdev->hub);
213 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
214 return p;
215}
216
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400217int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500218{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400219 if (!CONFIG_BOOTORDER)
220 return -1;
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400221 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500222 char desc[256], *p;
Kevin O'Connorea274782012-03-08 07:49:09 -0500223 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
224 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400225 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
226 , usbdev->port+1, lun);
227 int ret = find_prio(desc);
228 if (ret >= 0)
229 return ret;
230 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
231 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500232 return find_prio(desc);
233}
234
Kevin O'Connor031ef552010-12-27 19:26:57 -0500235
236/****************************************************************
237 * Boot setup
238 ****************************************************************/
239
Kevin O'Connor11a72342013-03-05 17:52:21 +0800240static int BootRetryTime;
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500241static int CheckFloppySig = 1;
242
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500243#define DEFAULT_PRIO 9999
244
245static int DefaultFloppyPrio = 101;
246static int DefaultCDPrio = 102;
247static int DefaultHDPrio = 103;
248static int DefaultBEVPrio = 104;
249
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500250void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500251boot_init(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500252{
253 if (! CONFIG_BOOT)
254 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500255
Kevin O'Connor897fb112013-02-07 23:32:48 -0500256 if (CONFIG_QEMU) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500257 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500258 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500259 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500260 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500261 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500262 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
263 = DefaultBEVPrio = DEFAULT_PRIO;
264 int i;
265 for (i=101; i<104; i++) {
266 u32 val = bootorder & 0x0f;
267 bootorder >>= 4;
268 switch (val) {
269 case 1: DefaultFloppyPrio = i; break;
270 case 2: DefaultHDPrio = i; break;
271 case 3: DefaultCDPrio = i; break;
272 case 4: DefaultBEVPrio = i; break;
273 }
274 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500275 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200276
Kevin O'Connor11a72342013-03-05 17:52:21 +0800277 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
278
Kevin O'Connord1a17462010-12-24 11:17:03 -0500279 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500280}
281
Kevin O'Connord1a17462010-12-24 11:17:03 -0500282
283/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500284 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500285 ****************************************************************/
286
Kevin O'Connor031ef552010-12-27 19:26:57 -0500287struct bootentry_s {
288 int type;
289 union {
290 u32 data;
291 struct segoff_s vector;
292 struct drive_s *drive;
293 };
294 int priority;
295 const char *description;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400296 struct hlist_node node;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500297};
Kevin O'Connor446defb2013-06-08 21:50:53 -0400298static struct hlist_head BootList VARVERIFY32INIT;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500299
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500300#define IPL_TYPE_FLOPPY 0x01
301#define IPL_TYPE_HARDDISK 0x02
302#define IPL_TYPE_CDROM 0x03
303#define IPL_TYPE_CBFS 0x20
304#define IPL_TYPE_BEV 0x80
305#define IPL_TYPE_BCV 0x81
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500306#define IPL_TYPE_HALT 0xf0
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500307
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500308static void
309bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500310{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500311 if (! CONFIG_BOOT)
312 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500313 struct bootentry_s *be = malloc_tmp(sizeof(*be));
314 if (!be) {
315 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500316 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500317 }
318 be->type = type;
319 be->priority = prio;
320 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500321 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500322 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
323 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500324
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500325 // Add entry in sorted order.
Kevin O'Connor446defb2013-06-08 21:50:53 -0400326 struct hlist_node **pprev;
327 struct bootentry_s *pos;
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400328 hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500329 if (be->priority < pos->priority)
330 break;
331 if (be->priority > pos->priority)
332 continue;
333 if (be->type < pos->type)
334 break;
335 if (be->type > pos->type)
336 continue;
337 if (be->type <= IPL_TYPE_CDROM
338 && (be->drive->type < pos->drive->type
339 || (be->drive->type == pos->drive->type
340 && be->drive->cntl_id < pos->drive->cntl_id)))
341 break;
342 }
Kevin O'Connor446defb2013-06-08 21:50:53 -0400343 hlist_add(&be->node, pprev);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500344}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500345
Kevin O'Connor031ef552010-12-27 19:26:57 -0500346// Return the given priority if it's set - defaultprio otherwise.
347static inline int defPrio(int priority, int defaultprio) {
348 return (priority < 0) ? defaultprio : priority;
349}
350
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500351// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200352void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500353boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200354{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500355 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
356 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500357 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
358 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200359}
360
Kevin O'Connor0a924122009-02-08 19:43:47 -0500361// Add a bcv entry for an expansion card harddrive or legacy option rom
362void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500363boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500364{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500365 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500366 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500367 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500368}
369
Kevin O'Connor0a924122009-02-08 19:43:47 -0500370void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500371boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500372{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500373 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500374 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500375}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500376
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500377void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500378boot_add_hd(struct drive_s *drive_g, 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_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500381 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500382}
383
384void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500385boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500386{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500387 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500388 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500389}
390
391// Add a CBFS payload entry
392void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500393boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500394{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500395 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500396}
397
398
399/****************************************************************
Kevin O'Connor0ac5e9e2013-09-14 13:09:27 -0400400 * Keyboard calls
401 ****************************************************************/
402
403// See if a keystroke is pending in the keyboard buffer.
404static int
405check_for_keystroke(void)
406{
407 struct bregs br;
408 memset(&br, 0, sizeof(br));
409 br.flags = F_IF|F_ZF;
410 br.ah = 1;
411 call16_int(0x16, &br);
412 return !(br.flags & F_ZF);
413}
414
415// Return a keystroke - waiting forever if necessary.
416static int
417get_raw_keystroke(void)
418{
419 struct bregs br;
420 memset(&br, 0, sizeof(br));
421 br.flags = F_IF;
422 call16_int(0x16, &br);
423 return br.ah;
424}
425
426// Read a keystroke - waiting up to 'msec' milliseconds.
427static int
428get_keystroke(int msec)
429{
430 u32 end = irqtimer_calc(msec);
431 for (;;) {
432 if (check_for_keystroke())
433 return get_raw_keystroke();
434 if (irqtimer_check(end))
435 return -1;
436 yield_toirq();
437 }
438}
439
440
441/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500442 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500443 ****************************************************************/
444
Kevin O'Connor73023002011-07-05 20:34:34 -0400445#define DEFAULT_BOOTMENU_WAIT 2500
446
Kevin O'Connor0a924122009-02-08 19:43:47 -0500447// Show IPL option menu.
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500448void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500449interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500450{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500451 // XXX - show available drives?
452
Kevin O'Connor56c50892013-02-09 19:25:51 -0500453 if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500454 return;
455
456 while (get_keystroke(0) >= 0)
457 ;
458
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400459 char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
460 int menukey = romfile_loadint("etc/boot-menu-key", 0x86);
461 printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n\n");
462 free(bootmsg);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500463
Kevin O'Connor73023002011-07-05 20:34:34 -0400464 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400465 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400466 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400467 disable_bootsplash();
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400468 if (scan_code != menukey)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500469 return;
470
471 while (get_keystroke(0) >= 0)
472 ;
473
474 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400475 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500476
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500477 // Show menu items
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500478 int maxmenu = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400479 struct bootentry_s *pos;
480 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500481 char desc[60];
482 maxmenu++;
483 printf("%d. %s\n", maxmenu
484 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500485 }
486
Kevin O'Connor551caa22010-12-29 13:25:18 -0500487 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500488 for (;;) {
489 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500490 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500491 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500492 }
493 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500494 if (scan_code == 0x01)
495 // ESC
496 return;
497
498 // Find entry and make top priority.
499 int choice = scan_code - 1;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400500 hlist_for_each_entry(pos, &BootList, node) {
501 if (! --choice)
502 break;
503 }
504 hlist_del(&pos->node);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500505 pos->priority = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400506 hlist_add_head(&pos->node, &BootList);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500507}
508
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500509// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500510struct bev_s {
511 int type;
512 u32 vector;
513};
514static struct bev_s BEV[20];
515static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500516static int HaveHDBoot, HaveFDBoot;
517
Kevin O'Connor0a924122009-02-08 19:43:47 -0500518static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500519add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500520{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500521 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
522 return;
523 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
524 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500525 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500526 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500527 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500528 bev->type = type;
529 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500530}
531
532// Prepare for boot - show menu and run bcvs.
533void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500534bcv_prepboot(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500535{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500536 if (! CONFIG_BOOT)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500537 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500538
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500539 int haltprio = find_prio("HALT");
540 if (haltprio >= 0)
541 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
542
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500543 // Map drives and populate BEV list
Kevin O'Connor446defb2013-06-08 21:50:53 -0400544 struct bootentry_s *pos;
545 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500546 switch (pos->type) {
547 case IPL_TYPE_BCV:
548 call_bcv(pos->vector.seg, pos->vector.offset);
549 add_bev(IPL_TYPE_HARDDISK, 0);
550 break;
551 case IPL_TYPE_FLOPPY:
552 map_floppy_drive(pos->drive);
553 add_bev(IPL_TYPE_FLOPPY, 0);
554 break;
555 case IPL_TYPE_HARDDISK:
556 map_hd_drive(pos->drive);
557 add_bev(IPL_TYPE_HARDDISK, 0);
558 break;
559 case IPL_TYPE_CDROM:
560 map_cd_drive(pos->drive);
561 // NO BREAK
562 default:
563 add_bev(pos->type, pos->data);
564 break;
565 }
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500566 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400567
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500568 // If nothing added a floppy/hd boot - add it manually.
569 add_bev(IPL_TYPE_FLOPPY, 0);
570 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500571}
572
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500573
574/****************************************************************
575 * Boot code (int 18/19)
576 ****************************************************************/
577
Kevin O'Connor0a924122009-02-08 19:43:47 -0500578// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500579static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500580call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500581{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500582 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500583 struct bregs br;
584 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400585 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500586 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500587 // Set the magic number in ax and the boot drive in dl.
588 br.dl = bootdrv;
589 br.ax = 0xaa55;
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400590 farcall16(&br);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500591}
592
593// Boot from a disk (either floppy or harddrive)
594static void
595boot_disk(u8 bootdrv, int checksig)
596{
597 u16 bootseg = 0x07c0;
598
599 // Read sector
600 struct bregs br;
601 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400602 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500603 br.dl = bootdrv;
604 br.es = bootseg;
605 br.ah = 2;
606 br.al = 1;
607 br.cl = 1;
608 call16_int(0x13, &br);
609
610 if (br.flags & F_CF) {
611 printf("Boot failed: could not read the boot disk\n\n");
612 return;
613 }
614
615 if (checksig) {
616 struct mbr_s *mbr = (void*)0;
617 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
618 printf("Boot failed: not a bootable disk\n\n");
619 return;
620 }
621 }
622
623 /* Canonicalize bootseg:bootip */
624 u16 bootip = (bootseg & 0x0fff) << 4;
625 bootseg &= 0xf000;
626
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500627 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500628}
629
630// Boot from a CD-ROM
631static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500632boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500633{
634 if (! CONFIG_CDROM_BOOT)
635 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500636 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200637
Gleb Natapov4c90a202010-12-07 13:50:54 +0200638 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500639 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500640 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500641 return;
642 }
643
Kevin O'Connord3140832012-05-13 22:46:12 -0400644 u8 bootdrv = CDEmu.emulated_extdrive;
645 u16 bootseg = CDEmu.load_segment;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500646 /* Canonicalize bootseg:bootip */
647 u16 bootip = (bootseg & 0x0fff) << 4;
648 bootseg &= 0xf000;
649
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500650 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500651}
652
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400653// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400654static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500655boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400656{
Kevin O'Connor897fb112013-02-07 23:32:48 -0500657 if (!CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400658 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500659 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500660 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400661}
662
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500663// Boot from a BEV entry on an optionrom.
664static void
665boot_rom(u32 vector)
666{
667 printf("Booting from ROM...\n");
668 struct segoff_s so;
669 so.segoff = vector;
670 call_boot_entry(so, 0);
671}
672
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400673// Unable to find bootable device - warn user and eventually retry.
674static void
675boot_fail(void)
676{
Kevin O'Connor11a72342013-03-05 17:52:21 +0800677 if (BootRetryTime == (u32)-1)
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400678 printf("No bootable device.\n");
679 else
Kevin O'Connor11a72342013-03-05 17:52:21 +0800680 printf("No bootable device. Retrying in %d seconds.\n"
681 , BootRetryTime/1000);
682 // Wait for 'BootRetryTime' milliseconds and then reboot.
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400683 u32 end = irqtimer_calc(BootRetryTime);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400684 for (;;) {
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400685 if (BootRetryTime != (u32)-1 && irqtimer_check(end))
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400686 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400687 yield_toirq();
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400688 }
689 printf("Rebooting.\n");
690 struct bregs br;
691 memset(&br, 0, sizeof(br));
692 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400693 farcall16big(&br);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400694}
695
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500696// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500697static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400698do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500699{
Kevin O'Connor40967022008-07-21 22:23:05 -0400700 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500701 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400702
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400703 if (seq_nr >= BEVCount)
704 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500705
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500706 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500707 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400708 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400709 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500710 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500711 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500712 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400713 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500714 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500715 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500716 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500717 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500718 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400719 break;
720 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500721 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500722 break;
723 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500724 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500725 break;
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500726 case IPL_TYPE_HALT:
727 boot_fail();
728 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500729 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500730
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500731 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400732 struct bregs br;
733 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400734 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400735 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500736}
737
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400738int BootSequence VARLOW = -1;
739
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500740// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500741void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500742handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500743{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500744 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400745 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400746 int seq = BootSequence + 1;
747 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400748 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500749}
750
751// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500752void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500753handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500754{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500755 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400756 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400757 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400758 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500759}