blob: 85d5051b88e638fb1d79ddc08f2c867e818755a0 [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'Connorbaaadb62010-12-29 11:18:04 -0500238static int CheckFloppySig = 1;
239
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500240#define DEFAULT_PRIO 9999
241
242static int DefaultFloppyPrio = 101;
243static int DefaultCDPrio = 102;
244static int DefaultHDPrio = 103;
245static int DefaultBEVPrio = 104;
246
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500247void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500248boot_init(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500249{
250 if (! CONFIG_BOOT)
251 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500252
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500253 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500254 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500255 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500256 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500257 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500258 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500259 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
260 = DefaultBEVPrio = DEFAULT_PRIO;
261 int i;
262 for (i=101; i<104; i++) {
263 u32 val = bootorder & 0x0f;
264 bootorder >>= 4;
265 switch (val) {
266 case 1: DefaultFloppyPrio = i; break;
267 case 2: DefaultHDPrio = i; break;
268 case 3: DefaultCDPrio = i; break;
269 case 4: DefaultBEVPrio = i; break;
270 }
271 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500272 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200273
Kevin O'Connord1a17462010-12-24 11:17:03 -0500274 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500275}
276
Kevin O'Connord1a17462010-12-24 11:17:03 -0500277
278/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500279 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500280 ****************************************************************/
281
Kevin O'Connor031ef552010-12-27 19:26:57 -0500282struct bootentry_s {
283 int type;
284 union {
285 u32 data;
286 struct segoff_s vector;
287 struct drive_s *drive;
288 };
289 int priority;
290 const char *description;
291 struct bootentry_s *next;
292};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500293static struct bootentry_s *BootList;
294
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500295#define IPL_TYPE_FLOPPY 0x01
296#define IPL_TYPE_HARDDISK 0x02
297#define IPL_TYPE_CDROM 0x03
298#define IPL_TYPE_CBFS 0x20
299#define IPL_TYPE_BEV 0x80
300#define IPL_TYPE_BCV 0x81
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500301#define IPL_TYPE_HALT 0xf0
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500302
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500303static void
304bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500305{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500306 if (! CONFIG_BOOT)
307 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500308 struct bootentry_s *be = malloc_tmp(sizeof(*be));
309 if (!be) {
310 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500311 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500312 }
313 be->type = type;
314 be->priority = prio;
315 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500316 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500317 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
318 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500319
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500320 // Add entry in sorted order.
321 struct bootentry_s **pprev;
322 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
323 struct bootentry_s *pos = *pprev;
324 if (be->priority < pos->priority)
325 break;
326 if (be->priority > pos->priority)
327 continue;
328 if (be->type < pos->type)
329 break;
330 if (be->type > pos->type)
331 continue;
332 if (be->type <= IPL_TYPE_CDROM
333 && (be->drive->type < pos->drive->type
334 || (be->drive->type == pos->drive->type
335 && be->drive->cntl_id < pos->drive->cntl_id)))
336 break;
337 }
338 be->next = *pprev;
339 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500340}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500341
Kevin O'Connor031ef552010-12-27 19:26:57 -0500342// Return the given priority if it's set - defaultprio otherwise.
343static inline int defPrio(int priority, int defaultprio) {
344 return (priority < 0) ? defaultprio : priority;
345}
346
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500347// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200348void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500349boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200350{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500351 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
352 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500353 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
354 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200355}
356
Kevin O'Connor0a924122009-02-08 19:43:47 -0500357// Add a bcv entry for an expansion card harddrive or legacy option rom
358void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500359boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500360{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500361 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500362 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500363 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500364}
365
Kevin O'Connor0a924122009-02-08 19:43:47 -0500366void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500367boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500368{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500369 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500370 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500371}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500372
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500373void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500374boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500375{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500376 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500377 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500378}
379
380void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500381boot_add_cd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500382{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500383 bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500384 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500385}
386
387// Add a CBFS payload entry
388void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500389boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500390{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500391 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500392}
393
394
395/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500396 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500397 ****************************************************************/
398
Kevin O'Connor73023002011-07-05 20:34:34 -0400399#define DEFAULT_BOOTMENU_WAIT 2500
400
Kevin O'Connor0a924122009-02-08 19:43:47 -0500401// Show IPL option menu.
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500402void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500403interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500404{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500405 // XXX - show available drives?
406
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400407 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500408 return;
409
410 while (get_keystroke(0) >= 0)
411 ;
412
Kevin O'Connor57174c72012-12-21 22:52:50 -0500413 printf("\nPress F12 for boot menu.\n\n");
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500414
Kevin O'Connor73023002011-07-05 20:34:34 -0400415 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400416 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400417 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400418 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500419 if (scan_code != 0x86)
420 /* not F12 */
421 return;
422
423 while (get_keystroke(0) >= 0)
424 ;
425
426 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400427 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500428
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500429 // Show menu items
430 struct bootentry_s *pos = BootList;
431 int maxmenu = 0;
432 while (pos) {
433 char desc[60];
434 maxmenu++;
435 printf("%d. %s\n", maxmenu
436 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
437 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500438 }
439
Kevin O'Connor551caa22010-12-29 13:25:18 -0500440 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500441 for (;;) {
442 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500443 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500444 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500445 }
446 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500447 if (scan_code == 0x01)
448 // ESC
449 return;
450
451 // Find entry and make top priority.
452 int choice = scan_code - 1;
453 struct bootentry_s **pprev = &BootList;
454 while (--choice)
455 pprev = &(*pprev)->next;
456 pos = *pprev;
457 *pprev = pos->next;
458 pos->next = BootList;
459 BootList = pos;
460 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500461}
462
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500463// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500464struct bev_s {
465 int type;
466 u32 vector;
467};
468static struct bev_s BEV[20];
469static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500470static int HaveHDBoot, HaveFDBoot;
471
Kevin O'Connor0a924122009-02-08 19:43:47 -0500472static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500473add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500474{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500475 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
476 return;
477 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
478 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500479 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500480 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500481 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500482 bev->type = type;
483 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500484}
485
486// Prepare for boot - show menu and run bcvs.
487void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500488bcv_prepboot(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500489{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500490 if (! CONFIG_BOOT)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500491 return;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500492
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500493 int haltprio = find_prio("HALT");
494 if (haltprio >= 0)
495 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
496
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500497 // Map drives and populate BEV list
498 struct bootentry_s *pos = BootList;
499 while (pos) {
500 switch (pos->type) {
501 case IPL_TYPE_BCV:
502 call_bcv(pos->vector.seg, pos->vector.offset);
503 add_bev(IPL_TYPE_HARDDISK, 0);
504 break;
505 case IPL_TYPE_FLOPPY:
506 map_floppy_drive(pos->drive);
507 add_bev(IPL_TYPE_FLOPPY, 0);
508 break;
509 case IPL_TYPE_HARDDISK:
510 map_hd_drive(pos->drive);
511 add_bev(IPL_TYPE_HARDDISK, 0);
512 break;
513 case IPL_TYPE_CDROM:
514 map_cd_drive(pos->drive);
515 // NO BREAK
516 default:
517 add_bev(pos->type, pos->data);
518 break;
519 }
520 pos = pos->next;
521 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400522
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500523 // If nothing added a floppy/hd boot - add it manually.
524 add_bev(IPL_TYPE_FLOPPY, 0);
525 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500526}
527
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500528
529/****************************************************************
530 * Boot code (int 18/19)
531 ****************************************************************/
532
Kevin O'Connor0a924122009-02-08 19:43:47 -0500533// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500534static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500535call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500536{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500537 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500538 struct bregs br;
539 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400540 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500541 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500542 // Set the magic number in ax and the boot drive in dl.
543 br.dl = bootdrv;
544 br.ax = 0xaa55;
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400545 farcall16(&br);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500546}
547
548// Boot from a disk (either floppy or harddrive)
549static void
550boot_disk(u8 bootdrv, int checksig)
551{
552 u16 bootseg = 0x07c0;
553
554 // Read sector
555 struct bregs br;
556 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400557 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500558 br.dl = bootdrv;
559 br.es = bootseg;
560 br.ah = 2;
561 br.al = 1;
562 br.cl = 1;
563 call16_int(0x13, &br);
564
565 if (br.flags & F_CF) {
566 printf("Boot failed: could not read the boot disk\n\n");
567 return;
568 }
569
570 if (checksig) {
571 struct mbr_s *mbr = (void*)0;
572 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
573 printf("Boot failed: not a bootable disk\n\n");
574 return;
575 }
576 }
577
578 /* Canonicalize bootseg:bootip */
579 u16 bootip = (bootseg & 0x0fff) << 4;
580 bootseg &= 0xf000;
581
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500582 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500583}
584
585// Boot from a CD-ROM
586static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500587boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500588{
589 if (! CONFIG_CDROM_BOOT)
590 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500591 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200592
Gleb Natapov4c90a202010-12-07 13:50:54 +0200593 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500594 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500595 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500596 return;
597 }
598
Kevin O'Connord3140832012-05-13 22:46:12 -0400599 u8 bootdrv = CDEmu.emulated_extdrive;
600 u16 bootseg = CDEmu.load_segment;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500601 /* Canonicalize bootseg:bootip */
602 u16 bootip = (bootseg & 0x0fff) << 4;
603 bootseg &= 0xf000;
604
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500605 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500606}
607
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400608// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400609static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500610boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400611{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400612 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400613 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500614 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500615 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400616}
617
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500618// Boot from a BEV entry on an optionrom.
619static void
620boot_rom(u32 vector)
621{
622 printf("Booting from ROM...\n");
623 struct segoff_s so;
624 so.segoff = vector;
625 call_boot_entry(so, 0);
626}
627
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400628// Unable to find bootable device - warn user and eventually retry.
629static void
630boot_fail(void)
631{
632 u32 retrytime = romfile_loadint("etc/boot-fail-wait", 60*1000);
633 if (retrytime == (u32)-1)
634 printf("No bootable device.\n");
635 else
636 printf("No bootable device. Retrying in %d seconds.\n", retrytime/1000);
637 // Wait for 'retrytime' milliseconds and then reboot.
638 u32 end = calc_future_timer(retrytime);
639 for (;;) {
640 if (retrytime != (u32)-1 && check_timer(end))
641 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400642 yield_toirq();
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400643 }
644 printf("Rebooting.\n");
645 struct bregs br;
646 memset(&br, 0, sizeof(br));
647 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400648 farcall16big(&br);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400649}
650
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500651// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500652static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400653do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500654{
Kevin O'Connor40967022008-07-21 22:23:05 -0400655 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500656 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400657
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400658 if (seq_nr >= BEVCount)
659 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500660
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500661 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500662 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400663 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400664 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500665 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500666 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500667 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400668 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500669 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500670 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500671 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500672 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500673 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400674 break;
675 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500676 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500677 break;
678 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500679 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500680 break;
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500681 case IPL_TYPE_HALT:
682 boot_fail();
683 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500684 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500685
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500686 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400687 struct bregs br;
688 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400689 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400690 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500691}
692
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400693int BootSequence VARLOW = -1;
694
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500695// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500696void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500697handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500698{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500699 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400700 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400701 int seq = BootSequence + 1;
702 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400703 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500704}
705
706// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500707void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500708handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500709{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500710 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400711 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400712 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400713 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500714}