blob: a2851b25687190526cb6231955b80de4750940cb [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'Connor135f3f62013-09-14 23:57:26 -04008#include "block.h" // struct drive_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04009#include "bregs.h" // struct bregs
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050010#include "config.h" // CONFIG_*
Kevin O'Connorccee6e82013-09-02 21:25:21 -040011#include "fw/paravirt.h" // qemu_cfg_show_boot_menu
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "hw/cmos.h" // inb_cmos
Kevin O'Connor5d369d82013-09-02 20:48:46 -040013#include "hw/pci.h" // pci_bdf_to_*
14#include "hw/usb.h" // struct usbdevice_s
Kevin O'Connor446defb2013-06-08 21:50:53 -040015#include "list.h" // hlist_node
Kevin O'Connor9dea5902013-09-14 20:23:54 -040016#include "malloc.h" // free
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040017#include "output.h" // dprintf
Kevin O'Connor41639f82013-09-14 19:37:36 -040018#include "romfile.h" // romfile_loadint
Kevin O'Connor135f3f62013-09-14 23:57:26 -040019#include "std/disk.h" // struct mbr_s
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040020#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040021#include "util.h" // irqtimer_calc
Kevin O'Connorc659fde2008-12-28 23:43:20 -050022
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050023
24/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050025 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050026 ****************************************************************/
27
Kevin O'Connor70c94dd2013-03-08 19:39:49 -050028static char **Bootorder VARVERIFY32INIT;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050029static int BootorderCount;
30
Kevin O'Connord1a17462010-12-24 11:17:03 -050031static void
32loadBootOrder(void)
33{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040034 if (!CONFIG_BOOTORDER)
35 return;
36
Kevin O'Connord1a17462010-12-24 11:17:03 -050037 char *f = romfile_loadfile("bootorder", NULL);
38 if (!f)
39 return;
40
Kevin O'Connor8bf55032011-01-01 11:00:42 -050041 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050042 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050043 while (f[i]) {
44 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050045 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050046 i++;
47 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050048 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
49 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050050 warn_noalloc();
51 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050052 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050053 return;
54 }
55
56 dprintf(3, "boot order:\n");
57 i = 0;
58 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050059 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050060 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050061 if (f)
62 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050063 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050064 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
65 i++;
66 } while (f);
67}
68
69// See if 'str' starts with 'glob' - if glob contains an '*' character
70// it will match any number of characters in str that aren't a '/' or
71// the next glob character.
72static char *
73glob_prefix(const char *glob, const char *str)
74{
75 for (;;) {
76 if (!*glob && (!*str || *str == '/'))
77 return (char*)str;
78 if (*glob == '*') {
79 if (!*str || *str == '/' || *str == glob[1])
80 glob++;
81 else
82 str++;
83 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050084 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050085 if (*glob != *str)
86 return NULL;
87 glob++;
88 str++;
89 }
90}
91
92// Search the bootorder list for the given glob pattern.
93static int
94find_prio(const char *glob)
95{
96 dprintf(1, "Searching bootorder for: %s\n", glob);
97 int i;
98 for (i = 0; i < BootorderCount; i++)
99 if (glob_prefix(glob, Bootorder[i]))
100 return i+1;
101 return -1;
102}
103
104#define FW_PCI_DOMAIN "/pci@i0cf8"
105
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400106static char *
107build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
108{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500109 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
110 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400111 if (pci->parent) {
112 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500113 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400114 if (pci->rootbus)
115 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500116 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
117 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500118
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400119 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500120 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500121 if (fn)
122 p += snprintf(p, buf+max-p, ",%x", fn);
123 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500124}
125
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400126int bootprio_find_pci_device(struct pci_device *pci)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500127{
David Woodhouse118469a2013-01-25 19:46:25 -0600128 if (CONFIG_CSM)
129 return csm_bootprio_pci(pci);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400130 if (!CONFIG_BOOTORDER)
131 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500132 // Find pci device - for example: /pci@i0cf8/ethernet@5
133 char desc[256];
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400134 build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500135 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500136}
137
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100138int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
139{
140 if (!CONFIG_BOOTORDER)
141 return -1;
142 if (!pci)
143 // support only pci machine for now
144 return -1;
145 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
146 char desc[256], *p;
147 p = build_pci_path(desc, sizeof(desc), "*", pci);
148 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
149 return find_prio(desc);
150}
151
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400152int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500153{
David Woodhouse118469a2013-01-25 19:46:25 -0600154 if (CONFIG_CSM)
155 return csm_bootprio_ata(pci, chanid, slave);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400156 if (!CONFIG_BOOTORDER)
157 return -1;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400158 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500159 // support only pci machine for now
160 return -1;
161 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
162 char desc[256], *p;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400163 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500164 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
165 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500166}
167
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400168int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500169{
David Woodhouse118469a2013-01-25 19:46:25 -0600170 if (CONFIG_CSM)
171 return csm_bootprio_fdc(pci, port, fdid);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400172 if (!CONFIG_BOOTORDER)
173 return -1;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400174 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500175 // support only pci machine for now
176 return -1;
177 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
178 char desc[256], *p;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400179 p = build_pci_path(desc, sizeof(desc), "isa", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500180 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
181 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500182}
183
Kevin O'Connorfce91892011-07-09 14:47:47 -0400184int bootprio_find_pci_rom(struct pci_device *pci, int instance)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500185{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400186 if (!CONFIG_BOOTORDER)
187 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500188 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
189 char desc[256], *p;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400190 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500191 if (instance)
192 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
193 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500194}
195
196int bootprio_find_named_rom(const char *name, int instance)
197{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400198 if (!CONFIG_BOOTORDER)
199 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500200 // Find named rom - for example: /rom@genroms/linuxboot.bin
201 char desc[256], *p;
202 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
203 if (instance)
204 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
205 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500206}
207
Kevin O'Connorea274782012-03-08 07:49:09 -0500208static char *
209build_usb_path(char *buf, int max, struct usbhub_s *hub)
210{
211 if (!hub->usbdev)
212 // Root hub - nothing to add.
213 return buf;
214 char *p = build_usb_path(buf, max, hub->usbdev->hub);
215 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
216 return p;
217}
218
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400219int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500220{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400221 if (!CONFIG_BOOTORDER)
222 return -1;
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400223 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500224 char desc[256], *p;
Kevin O'Connorea274782012-03-08 07:49:09 -0500225 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
226 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400227 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
228 , usbdev->port+1, lun);
229 int ret = find_prio(desc);
230 if (ret >= 0)
231 return ret;
232 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
233 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500234 return find_prio(desc);
235}
236
Kevin O'Connor031ef552010-12-27 19:26:57 -0500237
238/****************************************************************
239 * Boot setup
240 ****************************************************************/
241
Kevin O'Connor11a72342013-03-05 17:52:21 +0800242static int BootRetryTime;
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500243static int CheckFloppySig = 1;
244
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500245#define DEFAULT_PRIO 9999
246
247static int DefaultFloppyPrio = 101;
248static int DefaultCDPrio = 102;
249static int DefaultHDPrio = 103;
250static int DefaultBEVPrio = 104;
251
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500252void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500253boot_init(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500254{
255 if (! CONFIG_BOOT)
256 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500257
Kevin O'Connor897fb112013-02-07 23:32:48 -0500258 if (CONFIG_QEMU) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500259 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500260 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500261 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500262 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500263 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500264 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
265 = DefaultBEVPrio = DEFAULT_PRIO;
266 int i;
267 for (i=101; i<104; i++) {
268 u32 val = bootorder & 0x0f;
269 bootorder >>= 4;
270 switch (val) {
271 case 1: DefaultFloppyPrio = i; break;
272 case 2: DefaultHDPrio = i; break;
273 case 3: DefaultCDPrio = i; break;
274 case 4: DefaultBEVPrio = i; break;
275 }
276 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500277 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200278
Kevin O'Connor11a72342013-03-05 17:52:21 +0800279 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
280
Kevin O'Connord1a17462010-12-24 11:17:03 -0500281 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500282}
283
Kevin O'Connord1a17462010-12-24 11:17:03 -0500284
285/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500286 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500287 ****************************************************************/
288
Kevin O'Connor031ef552010-12-27 19:26:57 -0500289struct bootentry_s {
290 int type;
291 union {
292 u32 data;
293 struct segoff_s vector;
294 struct drive_s *drive;
295 };
296 int priority;
297 const char *description;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400298 struct hlist_node node;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500299};
Kevin O'Connor446defb2013-06-08 21:50:53 -0400300static struct hlist_head BootList VARVERIFY32INIT;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500301
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500302#define IPL_TYPE_FLOPPY 0x01
303#define IPL_TYPE_HARDDISK 0x02
304#define IPL_TYPE_CDROM 0x03
305#define IPL_TYPE_CBFS 0x20
306#define IPL_TYPE_BEV 0x80
307#define IPL_TYPE_BCV 0x81
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500308#define IPL_TYPE_HALT 0xf0
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500309
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500310static void
311bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500312{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500313 if (! CONFIG_BOOT)
314 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500315 struct bootentry_s *be = malloc_tmp(sizeof(*be));
316 if (!be) {
317 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500318 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500319 }
320 be->type = type;
321 be->priority = prio;
322 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500323 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500324 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
325 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500326
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500327 // Add entry in sorted order.
Kevin O'Connor446defb2013-06-08 21:50:53 -0400328 struct hlist_node **pprev;
329 struct bootentry_s *pos;
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400330 hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500331 if (be->priority < pos->priority)
332 break;
333 if (be->priority > pos->priority)
334 continue;
335 if (be->type < pos->type)
336 break;
337 if (be->type > pos->type)
338 continue;
339 if (be->type <= IPL_TYPE_CDROM
340 && (be->drive->type < pos->drive->type
341 || (be->drive->type == pos->drive->type
342 && be->drive->cntl_id < pos->drive->cntl_id)))
343 break;
344 }
Kevin O'Connor446defb2013-06-08 21:50:53 -0400345 hlist_add(&be->node, pprev);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500346}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500347
Kevin O'Connor031ef552010-12-27 19:26:57 -0500348// Return the given priority if it's set - defaultprio otherwise.
349static inline int defPrio(int priority, int defaultprio) {
350 return (priority < 0) ? defaultprio : priority;
351}
352
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500353// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200354void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500355boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200356{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500357 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
358 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500359 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
360 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200361}
362
Kevin O'Connor0a924122009-02-08 19:43:47 -0500363// Add a bcv entry for an expansion card harddrive or legacy option rom
364void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500365boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500366{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500367 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500368 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500369 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500370}
371
Kevin O'Connor0a924122009-02-08 19:43:47 -0500372void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500373boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500374{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500375 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500376 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500377}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500378
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500379void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500380boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500381{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500382 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500383 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500384}
385
386void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500387boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500388{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500389 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500390 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500391}
392
393// Add a CBFS payload entry
394void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500395boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500396{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500397 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500398}
399
400
401/****************************************************************
Kevin O'Connor0ac5e9e2013-09-14 13:09:27 -0400402 * Keyboard calls
403 ****************************************************************/
404
405// See if a keystroke is pending in the keyboard buffer.
406static int
407check_for_keystroke(void)
408{
409 struct bregs br;
410 memset(&br, 0, sizeof(br));
411 br.flags = F_IF|F_ZF;
412 br.ah = 1;
413 call16_int(0x16, &br);
414 return !(br.flags & F_ZF);
415}
416
417// Return a keystroke - waiting forever if necessary.
418static int
419get_raw_keystroke(void)
420{
421 struct bregs br;
422 memset(&br, 0, sizeof(br));
423 br.flags = F_IF;
424 call16_int(0x16, &br);
425 return br.ah;
426}
427
428// Read a keystroke - waiting up to 'msec' milliseconds.
429static int
430get_keystroke(int msec)
431{
432 u32 end = irqtimer_calc(msec);
433 for (;;) {
434 if (check_for_keystroke())
435 return get_raw_keystroke();
436 if (irqtimer_check(end))
437 return -1;
438 yield_toirq();
439 }
440}
441
442
443/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500444 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500445 ****************************************************************/
446
Kevin O'Connor73023002011-07-05 20:34:34 -0400447#define DEFAULT_BOOTMENU_WAIT 2500
448
Kevin O'Connor0a924122009-02-08 19:43:47 -0500449// Show IPL option menu.
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500450void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500451interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500452{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500453 // XXX - show available drives?
454
Kevin O'Connor56c50892013-02-09 19:25:51 -0500455 if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500456 return;
457
458 while (get_keystroke(0) >= 0)
459 ;
460
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400461 char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
462 int menukey = romfile_loadint("etc/boot-menu-key", 0x86);
463 printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n\n");
464 free(bootmsg);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500465
Kevin O'Connor73023002011-07-05 20:34:34 -0400466 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400467 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400468 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400469 disable_bootsplash();
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400470 if (scan_code != menukey)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500471 return;
472
473 while (get_keystroke(0) >= 0)
474 ;
475
476 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400477 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500478
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500479 // Show menu items
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500480 int maxmenu = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400481 struct bootentry_s *pos;
482 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500483 char desc[60];
484 maxmenu++;
485 printf("%d. %s\n", maxmenu
486 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500487 }
488
Kevin O'Connor551caa22010-12-29 13:25:18 -0500489 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500490 for (;;) {
491 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500492 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500493 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500494 }
495 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500496 if (scan_code == 0x01)
497 // ESC
498 return;
499
500 // Find entry and make top priority.
501 int choice = scan_code - 1;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400502 hlist_for_each_entry(pos, &BootList, node) {
503 if (! --choice)
504 break;
505 }
506 hlist_del(&pos->node);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500507 pos->priority = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400508 hlist_add_head(&pos->node, &BootList);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500509}
510
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500511// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500512struct bev_s {
513 int type;
514 u32 vector;
515};
516static struct bev_s BEV[20];
517static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500518static int HaveHDBoot, HaveFDBoot;
519
Kevin O'Connor0a924122009-02-08 19:43:47 -0500520static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500521add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500522{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500523 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
524 return;
525 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
526 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500527 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500528 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500529 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500530 bev->type = type;
531 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500532}
533
534// Prepare for boot - show menu and run bcvs.
535void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500536bcv_prepboot(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500537{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500538 if (! CONFIG_BOOT)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500539 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500540
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500541 int haltprio = find_prio("HALT");
542 if (haltprio >= 0)
543 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
544
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500545 // Map drives and populate BEV list
Kevin O'Connor446defb2013-06-08 21:50:53 -0400546 struct bootentry_s *pos;
547 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500548 switch (pos->type) {
549 case IPL_TYPE_BCV:
550 call_bcv(pos->vector.seg, pos->vector.offset);
551 add_bev(IPL_TYPE_HARDDISK, 0);
552 break;
553 case IPL_TYPE_FLOPPY:
554 map_floppy_drive(pos->drive);
555 add_bev(IPL_TYPE_FLOPPY, 0);
556 break;
557 case IPL_TYPE_HARDDISK:
558 map_hd_drive(pos->drive);
559 add_bev(IPL_TYPE_HARDDISK, 0);
560 break;
561 case IPL_TYPE_CDROM:
562 map_cd_drive(pos->drive);
563 // NO BREAK
564 default:
565 add_bev(pos->type, pos->data);
566 break;
567 }
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500568 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400569
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500570 // If nothing added a floppy/hd boot - add it manually.
571 add_bev(IPL_TYPE_FLOPPY, 0);
572 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500573}
574
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500575
576/****************************************************************
577 * Boot code (int 18/19)
578 ****************************************************************/
579
Kevin O'Connor0a924122009-02-08 19:43:47 -0500580// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500581static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500582call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500583{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500584 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500585 struct bregs br;
586 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400587 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500588 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500589 // Set the magic number in ax and the boot drive in dl.
590 br.dl = bootdrv;
591 br.ax = 0xaa55;
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400592 farcall16(&br);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500593}
594
595// Boot from a disk (either floppy or harddrive)
596static void
597boot_disk(u8 bootdrv, int checksig)
598{
599 u16 bootseg = 0x07c0;
600
601 // Read sector
602 struct bregs br;
603 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400604 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500605 br.dl = bootdrv;
606 br.es = bootseg;
607 br.ah = 2;
608 br.al = 1;
609 br.cl = 1;
610 call16_int(0x13, &br);
611
612 if (br.flags & F_CF) {
613 printf("Boot failed: could not read the boot disk\n\n");
614 return;
615 }
616
617 if (checksig) {
618 struct mbr_s *mbr = (void*)0;
619 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
620 printf("Boot failed: not a bootable disk\n\n");
621 return;
622 }
623 }
624
625 /* Canonicalize bootseg:bootip */
626 u16 bootip = (bootseg & 0x0fff) << 4;
627 bootseg &= 0xf000;
628
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500629 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500630}
631
632// Boot from a CD-ROM
633static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500634boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500635{
636 if (! CONFIG_CDROM_BOOT)
637 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500638 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200639
Gleb Natapov4c90a202010-12-07 13:50:54 +0200640 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500641 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500642 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500643 return;
644 }
645
Kevin O'Connord3140832012-05-13 22:46:12 -0400646 u8 bootdrv = CDEmu.emulated_extdrive;
647 u16 bootseg = CDEmu.load_segment;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500648 /* Canonicalize bootseg:bootip */
649 u16 bootip = (bootseg & 0x0fff) << 4;
650 bootseg &= 0xf000;
651
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500652 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500653}
654
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400655// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400656static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500657boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400658{
Kevin O'Connor897fb112013-02-07 23:32:48 -0500659 if (!CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400660 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500661 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500662 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400663}
664
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500665// Boot from a BEV entry on an optionrom.
666static void
667boot_rom(u32 vector)
668{
669 printf("Booting from ROM...\n");
670 struct segoff_s so;
671 so.segoff = vector;
672 call_boot_entry(so, 0);
673}
674
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400675// Unable to find bootable device - warn user and eventually retry.
676static void
677boot_fail(void)
678{
Kevin O'Connor11a72342013-03-05 17:52:21 +0800679 if (BootRetryTime == (u32)-1)
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400680 printf("No bootable device.\n");
681 else
Kevin O'Connor11a72342013-03-05 17:52:21 +0800682 printf("No bootable device. Retrying in %d seconds.\n"
683 , BootRetryTime/1000);
684 // Wait for 'BootRetryTime' milliseconds and then reboot.
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400685 u32 end = irqtimer_calc(BootRetryTime);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400686 for (;;) {
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400687 if (BootRetryTime != (u32)-1 && irqtimer_check(end))
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400688 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400689 yield_toirq();
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400690 }
691 printf("Rebooting.\n");
692 struct bregs br;
693 memset(&br, 0, sizeof(br));
694 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400695 farcall16big(&br);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400696}
697
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500698// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500699static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400700do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500701{
Kevin O'Connor40967022008-07-21 22:23:05 -0400702 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500703 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400704
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400705 if (seq_nr >= BEVCount)
706 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500707
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500708 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500709 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400710 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400711 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500712 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500713 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500714 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400715 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500716 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500717 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500718 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500719 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500720 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400721 break;
722 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500723 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500724 break;
725 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500726 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500727 break;
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500728 case IPL_TYPE_HALT:
729 boot_fail();
730 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500731 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500732
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500733 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400734 struct bregs br;
735 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400736 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400737 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500738}
739
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400740int BootSequence VARLOW = -1;
741
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500742// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500743void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500744handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500745{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500746 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400747 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400748 int seq = BootSequence + 1;
749 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400750 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500751}
752
753// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500754void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500755handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500756{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500757 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400758 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400759 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400760 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500761}