blob: c3438401de2c10fe4eb0e7291ef436892d82ac8c [file] [log] [blame]
Kevin O'Connorf13b0082008-08-17 11:26:42 -04001// Code to load disk image and start system boot.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05002//
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -04003// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor10ad7992009-10-24 11:06:08 -04008#include "util.h" // dprintf
Kevin O'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'Connor9f4e1d92009-02-08 15:44:08 -050013#include "cmos.h" // inb_cmos
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040014#include "paravirt.h" // qemu_cfg_show_boot_menu
15#include "pci.h" // pci_bdf_to_*
Kevin O'Connorea274782012-03-08 07:49:09 -050016#include "usb.h" // struct usbdevice_s
David Woodhouse118469a2013-01-25 19:46:25 -060017#include "csm.h" // csm_bootprio_*
Kevin O'Connor446defb2013-06-08 21:50:53 -040018#include "list.h" // hlist_node
Kevin O'Connorc659fde2008-12-28 23:43:20 -050019
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050020
21/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050022 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050023 ****************************************************************/
24
Kevin O'Connor70c94dd2013-03-08 19:39:49 -050025static char **Bootorder VARVERIFY32INIT;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050026static int BootorderCount;
27
Kevin O'Connord1a17462010-12-24 11:17:03 -050028static void
29loadBootOrder(void)
30{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040031 if (!CONFIG_BOOTORDER)
32 return;
33
Kevin O'Connord1a17462010-12-24 11:17:03 -050034 char *f = romfile_loadfile("bootorder", NULL);
35 if (!f)
36 return;
37
Kevin O'Connor8bf55032011-01-01 11:00:42 -050038 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050039 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050040 while (f[i]) {
41 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050042 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050043 i++;
44 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050045 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
46 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050047 warn_noalloc();
48 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050049 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050050 return;
51 }
52
53 dprintf(3, "boot order:\n");
54 i = 0;
55 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050056 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050057 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050058 if (f)
59 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050060 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050061 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
62 i++;
63 } while (f);
64}
65
66// See if 'str' starts with 'glob' - if glob contains an '*' character
67// it will match any number of characters in str that aren't a '/' or
68// the next glob character.
69static char *
70glob_prefix(const char *glob, const char *str)
71{
72 for (;;) {
73 if (!*glob && (!*str || *str == '/'))
74 return (char*)str;
75 if (*glob == '*') {
76 if (!*str || *str == '/' || *str == glob[1])
77 glob++;
78 else
79 str++;
80 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050081 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050082 if (*glob != *str)
83 return NULL;
84 glob++;
85 str++;
86 }
87}
88
89// Search the bootorder list for the given glob pattern.
90static int
91find_prio(const char *glob)
92{
93 dprintf(1, "Searching bootorder for: %s\n", glob);
94 int i;
95 for (i = 0; i < BootorderCount; i++)
96 if (glob_prefix(glob, Bootorder[i]))
97 return i+1;
98 return -1;
99}
100
101#define FW_PCI_DOMAIN "/pci@i0cf8"
102
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400103static char *
104build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
105{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500106 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
107 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400108 if (pci->parent) {
109 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500110 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400111 if (pci->rootbus)
112 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500113 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
114 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500115
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400116 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500117 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500118 if (fn)
119 p += snprintf(p, buf+max-p, ",%x", fn);
120 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500121}
122
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400123int bootprio_find_pci_device(struct pci_device *pci)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500124{
David Woodhouse118469a2013-01-25 19:46:25 -0600125 if (CONFIG_CSM)
126 return csm_bootprio_pci(pci);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400127 if (!CONFIG_BOOTORDER)
128 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500129 // Find pci device - for example: /pci@i0cf8/ethernet@5
130 char desc[256];
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400131 build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500132 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500133}
134
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100135int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
136{
137 if (!CONFIG_BOOTORDER)
138 return -1;
139 if (!pci)
140 // support only pci machine for now
141 return -1;
142 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
143 char desc[256], *p;
144 p = build_pci_path(desc, sizeof(desc), "*", pci);
145 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
146 return find_prio(desc);
147}
148
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400149int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500150{
David Woodhouse118469a2013-01-25 19:46:25 -0600151 if (CONFIG_CSM)
152 return csm_bootprio_ata(pci, chanid, slave);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400153 if (!CONFIG_BOOTORDER)
154 return -1;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400155 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500156 // support only pci machine for now
157 return -1;
158 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
159 char desc[256], *p;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400160 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500161 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
162 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500163}
164
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400165int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500166{
David Woodhouse118469a2013-01-25 19:46:25 -0600167 if (CONFIG_CSM)
168 return csm_bootprio_fdc(pci, port, fdid);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400169 if (!CONFIG_BOOTORDER)
170 return -1;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400171 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500172 // support only pci machine for now
173 return -1;
174 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
175 char desc[256], *p;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400176 p = build_pci_path(desc, sizeof(desc), "isa", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500177 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
178 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500179}
180
Kevin O'Connorfce91892011-07-09 14:47:47 -0400181int bootprio_find_pci_rom(struct pci_device *pci, int instance)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500182{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400183 if (!CONFIG_BOOTORDER)
184 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500185 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
186 char desc[256], *p;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400187 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500188 if (instance)
189 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
190 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500191}
192
193int bootprio_find_named_rom(const char *name, int instance)
194{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400195 if (!CONFIG_BOOTORDER)
196 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500197 // Find named rom - for example: /rom@genroms/linuxboot.bin
198 char desc[256], *p;
199 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
200 if (instance)
201 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
202 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500203}
204
Kevin O'Connorea274782012-03-08 07:49:09 -0500205static char *
206build_usb_path(char *buf, int max, struct usbhub_s *hub)
207{
208 if (!hub->usbdev)
209 // Root hub - nothing to add.
210 return buf;
211 char *p = build_usb_path(buf, max, hub->usbdev->hub);
212 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
213 return p;
214}
215
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400216int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500217{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400218 if (!CONFIG_BOOTORDER)
219 return -1;
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400220 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500221 char desc[256], *p;
Kevin O'Connorea274782012-03-08 07:49:09 -0500222 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
223 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400224 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
225 , usbdev->port+1, lun);
226 int ret = find_prio(desc);
227 if (ret >= 0)
228 return ret;
229 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
230 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500231 return find_prio(desc);
232}
233
Kevin O'Connor031ef552010-12-27 19:26:57 -0500234
235/****************************************************************
236 * Boot setup
237 ****************************************************************/
238
Kevin O'Connor11a72342013-03-05 17:52:21 +0800239static int BootRetryTime;
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500240static int CheckFloppySig = 1;
241
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500242#define DEFAULT_PRIO 9999
243
244static int DefaultFloppyPrio = 101;
245static int DefaultCDPrio = 102;
246static int DefaultHDPrio = 103;
247static int DefaultBEVPrio = 104;
248
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500249void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500250boot_init(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500251{
252 if (! CONFIG_BOOT)
253 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500254
Kevin O'Connor897fb112013-02-07 23:32:48 -0500255 if (CONFIG_QEMU) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500256 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500257 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500258 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500259 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500260 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500261 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
262 = DefaultBEVPrio = DEFAULT_PRIO;
263 int i;
264 for (i=101; i<104; i++) {
265 u32 val = bootorder & 0x0f;
266 bootorder >>= 4;
267 switch (val) {
268 case 1: DefaultFloppyPrio = i; break;
269 case 2: DefaultHDPrio = i; break;
270 case 3: DefaultCDPrio = i; break;
271 case 4: DefaultBEVPrio = i; break;
272 }
273 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500274 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200275
Kevin O'Connor11a72342013-03-05 17:52:21 +0800276 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
277
Kevin O'Connord1a17462010-12-24 11:17:03 -0500278 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500279}
280
Kevin O'Connord1a17462010-12-24 11:17:03 -0500281
282/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500283 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500284 ****************************************************************/
285
Kevin O'Connor031ef552010-12-27 19:26:57 -0500286struct bootentry_s {
287 int type;
288 union {
289 u32 data;
290 struct segoff_s vector;
291 struct drive_s *drive;
292 };
293 int priority;
294 const char *description;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400295 struct hlist_node node;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500296};
Kevin O'Connor446defb2013-06-08 21:50:53 -0400297static struct hlist_head BootList VARVERIFY32INIT;
Kevin O'Connor031ef552010-12-27 19:26:57 -0500298
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500299#define IPL_TYPE_FLOPPY 0x01
300#define IPL_TYPE_HARDDISK 0x02
301#define IPL_TYPE_CDROM 0x03
302#define IPL_TYPE_CBFS 0x20
303#define IPL_TYPE_BEV 0x80
304#define IPL_TYPE_BCV 0x81
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500305#define IPL_TYPE_HALT 0xf0
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500306
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500307static void
308bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500309{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500310 if (! CONFIG_BOOT)
311 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500312 struct bootentry_s *be = malloc_tmp(sizeof(*be));
313 if (!be) {
314 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500315 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500316 }
317 be->type = type;
318 be->priority = prio;
319 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500320 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500321 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
322 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500323
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500324 // Add entry in sorted order.
Kevin O'Connor446defb2013-06-08 21:50:53 -0400325 struct hlist_node **pprev;
326 struct bootentry_s *pos;
Kevin O'Connor030a58a2013-06-13 21:24:14 -0400327 hlist_for_each_entry_pprev(pos, pprev, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500328 if (be->priority < pos->priority)
329 break;
330 if (be->priority > pos->priority)
331 continue;
332 if (be->type < pos->type)
333 break;
334 if (be->type > pos->type)
335 continue;
336 if (be->type <= IPL_TYPE_CDROM
337 && (be->drive->type < pos->drive->type
338 || (be->drive->type == pos->drive->type
339 && be->drive->cntl_id < pos->drive->cntl_id)))
340 break;
341 }
Kevin O'Connor446defb2013-06-08 21:50:53 -0400342 hlist_add(&be->node, pprev);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500343}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500344
Kevin O'Connor031ef552010-12-27 19:26:57 -0500345// Return the given priority if it's set - defaultprio otherwise.
346static inline int defPrio(int priority, int defaultprio) {
347 return (priority < 0) ? defaultprio : priority;
348}
349
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500350// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200351void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500352boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200353{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500354 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
355 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500356 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
357 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200358}
359
Kevin O'Connor0a924122009-02-08 19:43:47 -0500360// Add a bcv entry for an expansion card harddrive or legacy option rom
361void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500362boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500363{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500364 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500365 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500366 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500367}
368
Kevin O'Connor0a924122009-02-08 19:43:47 -0500369void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500370boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500371{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500372 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500373 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500374}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500375
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500376void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500377boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500378{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500379 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500380 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500381}
382
383void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500384boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500385{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500386 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500387 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500388}
389
390// Add a CBFS payload entry
391void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500392boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500393{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500394 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500395}
396
397
398/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500399 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500400 ****************************************************************/
401
Kevin O'Connor73023002011-07-05 20:34:34 -0400402#define DEFAULT_BOOTMENU_WAIT 2500
403
Kevin O'Connor0a924122009-02-08 19:43:47 -0500404// Show IPL option menu.
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500405void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500406interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500407{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500408 // XXX - show available drives?
409
Kevin O'Connor56c50892013-02-09 19:25:51 -0500410 if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500411 return;
412
413 while (get_keystroke(0) >= 0)
414 ;
415
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400416 char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL);
417 int menukey = romfile_loadint("etc/boot-menu-key", 0x86);
418 printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n\n");
419 free(bootmsg);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500420
Kevin O'Connor73023002011-07-05 20:34:34 -0400421 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400422 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400423 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400424 disable_bootsplash();
Kevin O'Connor129f11e2013-08-02 14:13:50 -0400425 if (scan_code != menukey)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500426 return;
427
428 while (get_keystroke(0) >= 0)
429 ;
430
431 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400432 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500433
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500434 // Show menu items
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500435 int maxmenu = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400436 struct bootentry_s *pos;
437 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500438 char desc[60];
439 maxmenu++;
440 printf("%d. %s\n", maxmenu
441 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500442 }
443
Kevin O'Connor551caa22010-12-29 13:25:18 -0500444 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500445 for (;;) {
446 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500447 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500448 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500449 }
450 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500451 if (scan_code == 0x01)
452 // ESC
453 return;
454
455 // Find entry and make top priority.
456 int choice = scan_code - 1;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400457 hlist_for_each_entry(pos, &BootList, node) {
458 if (! --choice)
459 break;
460 }
461 hlist_del(&pos->node);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500462 pos->priority = 0;
Kevin O'Connor446defb2013-06-08 21:50:53 -0400463 hlist_add_head(&pos->node, &BootList);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500464}
465
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500466// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500467struct bev_s {
468 int type;
469 u32 vector;
470};
471static struct bev_s BEV[20];
472static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500473static int HaveHDBoot, HaveFDBoot;
474
Kevin O'Connor0a924122009-02-08 19:43:47 -0500475static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500476add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500477{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500478 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
479 return;
480 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
481 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500482 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500483 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500484 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500485 bev->type = type;
486 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500487}
488
489// Prepare for boot - show menu and run bcvs.
490void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500491bcv_prepboot(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500492{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500493 if (! CONFIG_BOOT)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500494 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500495
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500496 int haltprio = find_prio("HALT");
497 if (haltprio >= 0)
498 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
499
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500500 // Map drives and populate BEV list
Kevin O'Connor446defb2013-06-08 21:50:53 -0400501 struct bootentry_s *pos;
502 hlist_for_each_entry(pos, &BootList, node) {
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500503 switch (pos->type) {
504 case IPL_TYPE_BCV:
505 call_bcv(pos->vector.seg, pos->vector.offset);
506 add_bev(IPL_TYPE_HARDDISK, 0);
507 break;
508 case IPL_TYPE_FLOPPY:
509 map_floppy_drive(pos->drive);
510 add_bev(IPL_TYPE_FLOPPY, 0);
511 break;
512 case IPL_TYPE_HARDDISK:
513 map_hd_drive(pos->drive);
514 add_bev(IPL_TYPE_HARDDISK, 0);
515 break;
516 case IPL_TYPE_CDROM:
517 map_cd_drive(pos->drive);
518 // NO BREAK
519 default:
520 add_bev(pos->type, pos->data);
521 break;
522 }
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500523 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400524
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500525 // If nothing added a floppy/hd boot - add it manually.
526 add_bev(IPL_TYPE_FLOPPY, 0);
527 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500528}
529
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500530
531/****************************************************************
532 * Boot code (int 18/19)
533 ****************************************************************/
534
Kevin O'Connor0a924122009-02-08 19:43:47 -0500535// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500536static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500537call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500538{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500539 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500540 struct bregs br;
541 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400542 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500543 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500544 // Set the magic number in ax and the boot drive in dl.
545 br.dl = bootdrv;
546 br.ax = 0xaa55;
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400547 farcall16(&br);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500548}
549
550// Boot from a disk (either floppy or harddrive)
551static void
552boot_disk(u8 bootdrv, int checksig)
553{
554 u16 bootseg = 0x07c0;
555
556 // Read sector
557 struct bregs br;
558 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400559 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500560 br.dl = bootdrv;
561 br.es = bootseg;
562 br.ah = 2;
563 br.al = 1;
564 br.cl = 1;
565 call16_int(0x13, &br);
566
567 if (br.flags & F_CF) {
568 printf("Boot failed: could not read the boot disk\n\n");
569 return;
570 }
571
572 if (checksig) {
573 struct mbr_s *mbr = (void*)0;
574 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
575 printf("Boot failed: not a bootable disk\n\n");
576 return;
577 }
578 }
579
580 /* Canonicalize bootseg:bootip */
581 u16 bootip = (bootseg & 0x0fff) << 4;
582 bootseg &= 0xf000;
583
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500584 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500585}
586
587// Boot from a CD-ROM
588static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500589boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500590{
591 if (! CONFIG_CDROM_BOOT)
592 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500593 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200594
Gleb Natapov4c90a202010-12-07 13:50:54 +0200595 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500596 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500597 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500598 return;
599 }
600
Kevin O'Connord3140832012-05-13 22:46:12 -0400601 u8 bootdrv = CDEmu.emulated_extdrive;
602 u16 bootseg = CDEmu.load_segment;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500603 /* Canonicalize bootseg:bootip */
604 u16 bootip = (bootseg & 0x0fff) << 4;
605 bootseg &= 0xf000;
606
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500607 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500608}
609
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400610// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400611static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500612boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400613{
Kevin O'Connor897fb112013-02-07 23:32:48 -0500614 if (!CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400615 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500616 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500617 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400618}
619
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500620// Boot from a BEV entry on an optionrom.
621static void
622boot_rom(u32 vector)
623{
624 printf("Booting from ROM...\n");
625 struct segoff_s so;
626 so.segoff = vector;
627 call_boot_entry(so, 0);
628}
629
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400630// Unable to find bootable device - warn user and eventually retry.
631static void
632boot_fail(void)
633{
Kevin O'Connor11a72342013-03-05 17:52:21 +0800634 if (BootRetryTime == (u32)-1)
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400635 printf("No bootable device.\n");
636 else
Kevin O'Connor11a72342013-03-05 17:52:21 +0800637 printf("No bootable device. Retrying in %d seconds.\n"
638 , BootRetryTime/1000);
639 // Wait for 'BootRetryTime' milliseconds and then reboot.
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400640 u32 end = irqtimer_calc(BootRetryTime);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400641 for (;;) {
Kevin O'Connor95ee3822013-07-20 18:07:50 -0400642 if (BootRetryTime != (u32)-1 && irqtimer_check(end))
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400643 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400644 yield_toirq();
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400645 }
646 printf("Rebooting.\n");
647 struct bregs br;
648 memset(&br, 0, sizeof(br));
649 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400650 farcall16big(&br);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400651}
652
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500653// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500654static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400655do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500656{
Kevin O'Connor40967022008-07-21 22:23:05 -0400657 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500658 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400659
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400660 if (seq_nr >= BEVCount)
661 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500662
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500663 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500664 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400665 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400666 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500667 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500668 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500669 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400670 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500671 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500672 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500673 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500674 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500675 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400676 break;
677 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500678 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500679 break;
680 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500681 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500682 break;
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500683 case IPL_TYPE_HALT:
684 boot_fail();
685 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500686 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500687
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500688 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400689 struct bregs br;
690 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400691 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400692 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500693}
694
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400695int BootSequence VARLOW = -1;
696
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500697// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500698void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500699handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500700{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500701 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400702 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400703 int seq = BootSequence + 1;
704 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400705 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500706}
707
708// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500709void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500710handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500711{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500712 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400713 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400714 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400715 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500716}