blob: 3370c2d685a56e1610b0e56a665a56a0d4c58c4d [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'Connorc659fde2008-12-28 23:43:20 -050018
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050019
20/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050021 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050022 ****************************************************************/
23
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050024static char **Bootorder;
25static int BootorderCount;
26
Kevin O'Connord1a17462010-12-24 11:17:03 -050027static void
28loadBootOrder(void)
29{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040030 if (!CONFIG_BOOTORDER)
31 return;
32
Kevin O'Connord1a17462010-12-24 11:17:03 -050033 char *f = romfile_loadfile("bootorder", NULL);
34 if (!f)
35 return;
36
Kevin O'Connor8bf55032011-01-01 11:00:42 -050037 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050038 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050039 while (f[i]) {
40 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050041 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050042 i++;
43 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050044 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
45 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050046 warn_noalloc();
47 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050048 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050049 return;
50 }
51
52 dprintf(3, "boot order:\n");
53 i = 0;
54 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050055 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050056 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050057 if (f)
58 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050059 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050060 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
61 i++;
62 } while (f);
63}
64
65// See if 'str' starts with 'glob' - if glob contains an '*' character
66// it will match any number of characters in str that aren't a '/' or
67// the next glob character.
68static char *
69glob_prefix(const char *glob, const char *str)
70{
71 for (;;) {
72 if (!*glob && (!*str || *str == '/'))
73 return (char*)str;
74 if (*glob == '*') {
75 if (!*str || *str == '/' || *str == glob[1])
76 glob++;
77 else
78 str++;
79 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050080 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050081 if (*glob != *str)
82 return NULL;
83 glob++;
84 str++;
85 }
86}
87
88// Search the bootorder list for the given glob pattern.
89static int
90find_prio(const char *glob)
91{
92 dprintf(1, "Searching bootorder for: %s\n", glob);
93 int i;
94 for (i = 0; i < BootorderCount; i++)
95 if (glob_prefix(glob, Bootorder[i]))
96 return i+1;
97 return -1;
98}
99
100#define FW_PCI_DOMAIN "/pci@i0cf8"
101
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400102static char *
103build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
104{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500105 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
106 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400107 if (pci->parent) {
108 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500109 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400110 if (pci->rootbus)
111 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500112 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
113 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500114
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400115 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500116 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500117 if (fn)
118 p += snprintf(p, buf+max-p, ",%x", fn);
119 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500120}
121
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400122int bootprio_find_pci_device(struct pci_device *pci)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500123{
David Woodhouse118469a2013-01-25 19:46:25 -0600124 if (CONFIG_CSM)
125 return csm_bootprio_pci(pci);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400126 if (!CONFIG_BOOTORDER)
127 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500128 // Find pci device - for example: /pci@i0cf8/ethernet@5
129 char desc[256];
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400130 build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500131 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500132}
133
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100134int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
135{
136 if (!CONFIG_BOOTORDER)
137 return -1;
138 if (!pci)
139 // support only pci machine for now
140 return -1;
141 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
142 char desc[256], *p;
143 p = build_pci_path(desc, sizeof(desc), "*", pci);
144 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
145 return find_prio(desc);
146}
147
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400148int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500149{
David Woodhouse118469a2013-01-25 19:46:25 -0600150 if (CONFIG_CSM)
151 return csm_bootprio_ata(pci, chanid, slave);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400152 if (!CONFIG_BOOTORDER)
153 return -1;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400154 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500155 // support only pci machine for now
156 return -1;
157 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
158 char desc[256], *p;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400159 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500160 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
161 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500162}
163
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400164int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500165{
David Woodhouse118469a2013-01-25 19:46:25 -0600166 if (CONFIG_CSM)
167 return csm_bootprio_fdc(pci, port, fdid);
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400168 if (!CONFIG_BOOTORDER)
169 return -1;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400170 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500171 // support only pci machine for now
172 return -1;
173 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
174 char desc[256], *p;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400175 p = build_pci_path(desc, sizeof(desc), "isa", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500176 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
177 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500178}
179
Kevin O'Connorfce91892011-07-09 14:47:47 -0400180int bootprio_find_pci_rom(struct pci_device *pci, int instance)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500181{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400182 if (!CONFIG_BOOTORDER)
183 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500184 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
185 char desc[256], *p;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400186 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500187 if (instance)
188 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
189 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500190}
191
192int bootprio_find_named_rom(const char *name, int instance)
193{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400194 if (!CONFIG_BOOTORDER)
195 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500196 // Find named rom - for example: /rom@genroms/linuxboot.bin
197 char desc[256], *p;
198 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
199 if (instance)
200 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
201 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500202}
203
Kevin O'Connorea274782012-03-08 07:49:09 -0500204static char *
205build_usb_path(char *buf, int max, struct usbhub_s *hub)
206{
207 if (!hub->usbdev)
208 // Root hub - nothing to add.
209 return buf;
210 char *p = build_usb_path(buf, max, hub->usbdev->hub);
211 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
212 return p;
213}
214
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400215int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500216{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400217 if (!CONFIG_BOOTORDER)
218 return -1;
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400219 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500220 char desc[256], *p;
Kevin O'Connorea274782012-03-08 07:49:09 -0500221 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
222 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400223 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
224 , usbdev->port+1, lun);
225 int ret = find_prio(desc);
226 if (ret >= 0)
227 return ret;
228 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
229 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500230 return find_prio(desc);
231}
232
Kevin O'Connor031ef552010-12-27 19:26:57 -0500233
234/****************************************************************
235 * Boot setup
236 ****************************************************************/
237
Kevin O'Connor11a72342013-03-05 17:52:21 +0800238static int BootRetryTime;
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500239static int CheckFloppySig = 1;
240
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500241#define DEFAULT_PRIO 9999
242
243static int DefaultFloppyPrio = 101;
244static int DefaultCDPrio = 102;
245static int DefaultHDPrio = 103;
246static int DefaultBEVPrio = 104;
247
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500248void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500249boot_init(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500250{
251 if (! CONFIG_BOOT)
252 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500253
Kevin O'Connor897fb112013-02-07 23:32:48 -0500254 if (CONFIG_QEMU) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500255 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500256 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500257 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500258 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500259 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500260 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
261 = DefaultBEVPrio = DEFAULT_PRIO;
262 int i;
263 for (i=101; i<104; i++) {
264 u32 val = bootorder & 0x0f;
265 bootorder >>= 4;
266 switch (val) {
267 case 1: DefaultFloppyPrio = i; break;
268 case 2: DefaultHDPrio = i; break;
269 case 3: DefaultCDPrio = i; break;
270 case 4: DefaultBEVPrio = i; break;
271 }
272 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500273 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200274
Kevin O'Connor11a72342013-03-05 17:52:21 +0800275 BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000);
276
Kevin O'Connord1a17462010-12-24 11:17:03 -0500277 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500278}
279
Kevin O'Connord1a17462010-12-24 11:17:03 -0500280
281/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500282 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500283 ****************************************************************/
284
Kevin O'Connor031ef552010-12-27 19:26:57 -0500285struct bootentry_s {
286 int type;
287 union {
288 u32 data;
289 struct segoff_s vector;
290 struct drive_s *drive;
291 };
292 int priority;
293 const char *description;
294 struct bootentry_s *next;
295};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500296static struct bootentry_s *BootList;
297
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500298#define IPL_TYPE_FLOPPY 0x01
299#define IPL_TYPE_HARDDISK 0x02
300#define IPL_TYPE_CDROM 0x03
301#define IPL_TYPE_CBFS 0x20
302#define IPL_TYPE_BEV 0x80
303#define IPL_TYPE_BCV 0x81
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500304#define IPL_TYPE_HALT 0xf0
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500305
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500306static void
307bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500308{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500309 if (! CONFIG_BOOT)
310 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500311 struct bootentry_s *be = malloc_tmp(sizeof(*be));
312 if (!be) {
313 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500314 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500315 }
316 be->type = type;
317 be->priority = prio;
318 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500319 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500320 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
321 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500322
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500323 // Add entry in sorted order.
324 struct bootentry_s **pprev;
325 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
326 struct bootentry_s *pos = *pprev;
327 if (be->priority < pos->priority)
328 break;
329 if (be->priority > pos->priority)
330 continue;
331 if (be->type < pos->type)
332 break;
333 if (be->type > pos->type)
334 continue;
335 if (be->type <= IPL_TYPE_CDROM
336 && (be->drive->type < pos->drive->type
337 || (be->drive->type == pos->drive->type
338 && be->drive->cntl_id < pos->drive->cntl_id)))
339 break;
340 }
341 be->next = *pprev;
342 *pprev = be;
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'Connor57174c72012-12-21 22:52:50 -0500416 printf("\nPress F12 for boot menu.\n\n");
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500417
Kevin O'Connor73023002011-07-05 20:34:34 -0400418 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400419 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400420 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400421 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500422 if (scan_code != 0x86)
423 /* not F12 */
424 return;
425
426 while (get_keystroke(0) >= 0)
427 ;
428
429 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400430 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500431
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500432 // Show menu items
433 struct bootentry_s *pos = BootList;
434 int maxmenu = 0;
435 while (pos) {
436 char desc[60];
437 maxmenu++;
438 printf("%d. %s\n", maxmenu
439 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
440 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500441 }
442
Kevin O'Connor551caa22010-12-29 13:25:18 -0500443 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500444 for (;;) {
445 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500446 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500447 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500448 }
449 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500450 if (scan_code == 0x01)
451 // ESC
452 return;
453
454 // Find entry and make top priority.
455 int choice = scan_code - 1;
456 struct bootentry_s **pprev = &BootList;
457 while (--choice)
458 pprev = &(*pprev)->next;
459 pos = *pprev;
460 *pprev = pos->next;
461 pos->next = BootList;
462 BootList = pos;
463 pos->priority = 0;
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
501 struct bootentry_s *pos = BootList;
502 while (pos) {
503 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 }
523 pos = pos->next;
524 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400525
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500526 // If nothing added a floppy/hd boot - add it manually.
527 add_bev(IPL_TYPE_FLOPPY, 0);
528 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500529}
530
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500531
532/****************************************************************
533 * Boot code (int 18/19)
534 ****************************************************************/
535
Kevin O'Connor0a924122009-02-08 19:43:47 -0500536// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500537static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500538call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500539{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500540 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500541 struct bregs br;
542 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400543 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500544 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500545 // Set the magic number in ax and the boot drive in dl.
546 br.dl = bootdrv;
547 br.ax = 0xaa55;
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400548 farcall16(&br);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500549}
550
551// Boot from a disk (either floppy or harddrive)
552static void
553boot_disk(u8 bootdrv, int checksig)
554{
555 u16 bootseg = 0x07c0;
556
557 // Read sector
558 struct bregs br;
559 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400560 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500561 br.dl = bootdrv;
562 br.es = bootseg;
563 br.ah = 2;
564 br.al = 1;
565 br.cl = 1;
566 call16_int(0x13, &br);
567
568 if (br.flags & F_CF) {
569 printf("Boot failed: could not read the boot disk\n\n");
570 return;
571 }
572
573 if (checksig) {
574 struct mbr_s *mbr = (void*)0;
575 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
576 printf("Boot failed: not a bootable disk\n\n");
577 return;
578 }
579 }
580
581 /* Canonicalize bootseg:bootip */
582 u16 bootip = (bootseg & 0x0fff) << 4;
583 bootseg &= 0xf000;
584
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500585 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500586}
587
588// Boot from a CD-ROM
589static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500590boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500591{
592 if (! CONFIG_CDROM_BOOT)
593 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500594 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200595
Gleb Natapov4c90a202010-12-07 13:50:54 +0200596 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500597 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500598 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500599 return;
600 }
601
Kevin O'Connord3140832012-05-13 22:46:12 -0400602 u8 bootdrv = CDEmu.emulated_extdrive;
603 u16 bootseg = CDEmu.load_segment;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500604 /* Canonicalize bootseg:bootip */
605 u16 bootip = (bootseg & 0x0fff) << 4;
606 bootseg &= 0xf000;
607
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500608 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500609}
610
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400611// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400612static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500613boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400614{
Kevin O'Connor897fb112013-02-07 23:32:48 -0500615 if (!CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400616 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500617 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500618 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400619}
620
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500621// Boot from a BEV entry on an optionrom.
622static void
623boot_rom(u32 vector)
624{
625 printf("Booting from ROM...\n");
626 struct segoff_s so;
627 so.segoff = vector;
628 call_boot_entry(so, 0);
629}
630
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400631// Unable to find bootable device - warn user and eventually retry.
632static void
633boot_fail(void)
634{
Kevin O'Connor11a72342013-03-05 17:52:21 +0800635 if (BootRetryTime == (u32)-1)
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400636 printf("No bootable device.\n");
637 else
Kevin O'Connor11a72342013-03-05 17:52:21 +0800638 printf("No bootable device. Retrying in %d seconds.\n"
639 , BootRetryTime/1000);
640 // Wait for 'BootRetryTime' milliseconds and then reboot.
641 u32 end = calc_future_timer(BootRetryTime);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400642 for (;;) {
Kevin O'Connor11a72342013-03-05 17:52:21 +0800643 if (BootRetryTime != (u32)-1 && check_timer(end))
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400644 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400645 yield_toirq();
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400646 }
647 printf("Rebooting.\n");
648 struct bregs br;
649 memset(&br, 0, sizeof(br));
650 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400651 farcall16big(&br);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400652}
653
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500654// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500655static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400656do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500657{
Kevin O'Connor40967022008-07-21 22:23:05 -0400658 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500659 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400660
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400661 if (seq_nr >= BEVCount)
662 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500663
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500664 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500665 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400666 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400667 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500668 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500669 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500670 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400671 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500672 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500673 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500674 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500675 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500676 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400677 break;
678 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500679 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500680 break;
681 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500682 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500683 break;
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500684 case IPL_TYPE_HALT:
685 boot_fail();
686 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500687 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500688
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500689 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400690 struct bregs br;
691 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400692 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400693 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500694}
695
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400696int BootSequence VARLOW = -1;
697
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500698// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500699void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500700handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500701{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500702 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400703 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400704 int seq = BootSequence + 1;
705 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400706 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500707}
708
709// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500710void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500711handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500712{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500713 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400714 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400715 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400716 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500717}