blob: 2cb3b86e9fa79387eab96f2add7fc10cc4a4e61d [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
Kevin O'Connorc659fde2008-12-28 23:43:20 -050017
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050018
19/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -050020 * Boot priority ordering
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050021 ****************************************************************/
22
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050023static char **Bootorder;
24static int BootorderCount;
25
Kevin O'Connord1a17462010-12-24 11:17:03 -050026static void
27loadBootOrder(void)
28{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -040029 if (!CONFIG_BOOTORDER)
30 return;
31
Kevin O'Connord1a17462010-12-24 11:17:03 -050032 char *f = romfile_loadfile("bootorder", NULL);
33 if (!f)
34 return;
35
Kevin O'Connor8bf55032011-01-01 11:00:42 -050036 int i = 0;
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050037 BootorderCount = 1;
Kevin O'Connord1a17462010-12-24 11:17:03 -050038 while (f[i]) {
39 if (f[i] == '\n')
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050040 BootorderCount++;
Kevin O'Connord1a17462010-12-24 11:17:03 -050041 i++;
42 }
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050043 Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*));
44 if (!Bootorder) {
Kevin O'Connord1a17462010-12-24 11:17:03 -050045 warn_noalloc();
46 free(f);
Kevin O'Connor4d0c5922011-01-26 21:04:01 -050047 BootorderCount = 0;
Kevin O'Connord1a17462010-12-24 11:17:03 -050048 return;
49 }
50
51 dprintf(3, "boot order:\n");
52 i = 0;
53 do {
Kevin O'Connor3da2c1c2010-12-29 11:37:41 -050054 Bootorder[i] = f;
Kevin O'Connord1a17462010-12-24 11:17:03 -050055 f = strchr(f, '\n');
Kevin O'Connor8bf55032011-01-01 11:00:42 -050056 if (f)
57 *(f++) = '\0';
Kevin O'Connor9e881a32011-01-08 12:06:54 -050058 nullTrailingSpace(Bootorder[i]);
Kevin O'Connor8bf55032011-01-01 11:00:42 -050059 dprintf(3, "%d: %s\n", i+1, Bootorder[i]);
60 i++;
61 } while (f);
62}
63
64// See if 'str' starts with 'glob' - if glob contains an '*' character
65// it will match any number of characters in str that aren't a '/' or
66// the next glob character.
67static char *
68glob_prefix(const char *glob, const char *str)
69{
70 for (;;) {
71 if (!*glob && (!*str || *str == '/'))
72 return (char*)str;
73 if (*glob == '*') {
74 if (!*str || *str == '/' || *str == glob[1])
75 glob++;
76 else
77 str++;
78 continue;
Kevin O'Connord1a17462010-12-24 11:17:03 -050079 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -050080 if (*glob != *str)
81 return NULL;
82 glob++;
83 str++;
84 }
85}
86
87// Search the bootorder list for the given glob pattern.
88static int
89find_prio(const char *glob)
90{
91 dprintf(1, "Searching bootorder for: %s\n", glob);
92 int i;
93 for (i = 0; i < BootorderCount; i++)
94 if (glob_prefix(glob, Bootorder[i]))
95 return i+1;
96 return -1;
97}
98
99#define FW_PCI_DOMAIN "/pci@i0cf8"
100
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400101static char *
102build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci)
103{
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500104 // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2
105 char *p = buf;
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400106 if (pci->parent) {
107 p = build_pci_path(p, max, "pci-bridge", pci->parent);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500108 } else {
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400109 if (pci->rootbus)
110 p += snprintf(p, max, "/pci-root@%x", pci->rootbus);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500111 p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN);
112 }
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500113
Kevin O'Connor659c99d2011-06-20 23:29:15 -0400114 int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf);
Kevin O'Connord08eb9c2011-01-10 00:48:45 -0500115 p += snprintf(p, buf+max-p, "/%s@%x", devname, dev);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500116 if (fn)
117 p += snprintf(p, buf+max-p, ",%x", fn);
118 return p;
Kevin O'Connord1a17462010-12-24 11:17:03 -0500119}
120
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400121int bootprio_find_pci_device(struct pci_device *pci)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500122{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400123 if (!CONFIG_BOOTORDER)
124 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500125 // Find pci device - for example: /pci@i0cf8/ethernet@5
126 char desc[256];
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400127 build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500128 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500129}
130
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100131int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun)
132{
133 if (!CONFIG_BOOTORDER)
134 return -1;
135 if (!pci)
136 // support only pci machine for now
137 return -1;
138 // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0
139 char desc[256], *p;
140 p = build_pci_path(desc, sizeof(desc), "*", pci);
141 snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%d,%d", target, lun);
142 return find_prio(desc);
143}
144
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400145int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500146{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400147 if (!CONFIG_BOOTORDER)
148 return -1;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400149 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500150 // support only pci machine for now
151 return -1;
152 // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0
153 char desc[256], *p;
Kevin O'Connor95b2e0c2011-07-09 14:42:11 -0400154 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500155 snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave);
156 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500157}
158
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400159int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500160{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400161 if (!CONFIG_BOOTORDER)
162 return -1;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400163 if (!pci)
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500164 // support only pci machine for now
165 return -1;
166 // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0
167 char desc[256], *p;
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400168 p = build_pci_path(desc, sizeof(desc), "isa", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500169 snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid);
170 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500171}
172
Kevin O'Connorfce91892011-07-09 14:47:47 -0400173int bootprio_find_pci_rom(struct pci_device *pci, int instance)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500174{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400175 if (!CONFIG_BOOTORDER)
176 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500177 // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2
178 char desc[256], *p;
Kevin O'Connorfce91892011-07-09 14:47:47 -0400179 p = build_pci_path(desc, sizeof(desc), "*", pci);
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500180 if (instance)
181 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
182 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500183}
184
185int bootprio_find_named_rom(const char *name, int instance)
186{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400187 if (!CONFIG_BOOTORDER)
188 return -1;
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500189 // Find named rom - for example: /rom@genroms/linuxboot.bin
190 char desc[256], *p;
191 p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name);
192 if (instance)
193 snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance);
194 return find_prio(desc);
Kevin O'Connor031ef552010-12-27 19:26:57 -0500195}
196
Kevin O'Connorea274782012-03-08 07:49:09 -0500197static char *
198build_usb_path(char *buf, int max, struct usbhub_s *hub)
199{
200 if (!hub->usbdev)
201 // Root hub - nothing to add.
202 return buf;
203 char *p = build_usb_path(buf, max, hub->usbdev->hub);
204 p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1);
205 return p;
206}
207
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400208int bootprio_find_usb(struct usbdevice_s *usbdev, int lun)
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500209{
Kevin O'Connor49bf57b2011-05-10 22:08:30 -0400210 if (!CONFIG_BOOTORDER)
211 return -1;
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400212 // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500213 char desc[256], *p;
Kevin O'Connorea274782012-03-08 07:49:09 -0500214 p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci);
215 p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub);
Kevin O'Connor7fa31b52012-06-13 08:47:03 -0400216 snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%d"
217 , usbdev->port+1, lun);
218 int ret = find_prio(desc);
219 if (ret >= 0)
220 return ret;
221 // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1
222 snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1);
Kevin O'Connorc2002a12010-12-31 14:38:10 -0500223 return find_prio(desc);
224}
225
Kevin O'Connor031ef552010-12-27 19:26:57 -0500226
227/****************************************************************
228 * Boot setup
229 ****************************************************************/
230
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500231static int CheckFloppySig = 1;
232
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500233#define DEFAULT_PRIO 9999
234
235static int DefaultFloppyPrio = 101;
236static int DefaultCDPrio = 102;
237static int DefaultHDPrio = 103;
238static int DefaultBEVPrio = 104;
239
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500240void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500241boot_init(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500242{
243 if (! CONFIG_BOOT)
244 return;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500245
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500246 if (!CONFIG_COREBOOT) {
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500247 // On emulators, get boot order from nvram.
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500248 if (inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500249 CheckFloppySig = 0;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500250 u32 bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500251 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500252 DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio
253 = DefaultBEVPrio = DEFAULT_PRIO;
254 int i;
255 for (i=101; i<104; i++) {
256 u32 val = bootorder & 0x0f;
257 bootorder >>= 4;
258 switch (val) {
259 case 1: DefaultFloppyPrio = i; break;
260 case 2: DefaultHDPrio = i; break;
261 case 3: DefaultCDPrio = i; break;
262 case 4: DefaultBEVPrio = i; break;
263 }
264 }
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500265 }
Gleb Natapovb9a75912010-12-23 11:29:38 +0200266
Kevin O'Connord1a17462010-12-24 11:17:03 -0500267 loadBootOrder();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500268}
269
Kevin O'Connord1a17462010-12-24 11:17:03 -0500270
271/****************************************************************
Kevin O'Connor031ef552010-12-27 19:26:57 -0500272 * BootList handling
Kevin O'Connord1a17462010-12-24 11:17:03 -0500273 ****************************************************************/
274
Kevin O'Connor031ef552010-12-27 19:26:57 -0500275struct bootentry_s {
276 int type;
277 union {
278 u32 data;
279 struct segoff_s vector;
280 struct drive_s *drive;
281 };
282 int priority;
283 const char *description;
284 struct bootentry_s *next;
285};
Kevin O'Connor031ef552010-12-27 19:26:57 -0500286static struct bootentry_s *BootList;
287
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500288#define IPL_TYPE_FLOPPY 0x01
289#define IPL_TYPE_HARDDISK 0x02
290#define IPL_TYPE_CDROM 0x03
291#define IPL_TYPE_CBFS 0x20
292#define IPL_TYPE_BEV 0x80
293#define IPL_TYPE_BCV 0x81
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500294#define IPL_TYPE_HALT 0xf0
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500295
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500296static void
297bootentry_add(int type, int prio, u32 data, const char *desc)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500298{
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500299 if (! CONFIG_BOOT)
300 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500301 struct bootentry_s *be = malloc_tmp(sizeof(*be));
302 if (!be) {
303 warn_noalloc();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500304 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500305 }
306 be->type = type;
307 be->priority = prio;
308 be->data = data;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500309 be->description = desc ?: "?";
Kevin O'Connor8bf55032011-01-01 11:00:42 -0500310 dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n"
311 , be->description, type, prio, data);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500312
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500313 // Add entry in sorted order.
314 struct bootentry_s **pprev;
315 for (pprev = &BootList; *pprev; pprev = &(*pprev)->next) {
316 struct bootentry_s *pos = *pprev;
317 if (be->priority < pos->priority)
318 break;
319 if (be->priority > pos->priority)
320 continue;
321 if (be->type < pos->type)
322 break;
323 if (be->type > pos->type)
324 continue;
325 if (be->type <= IPL_TYPE_CDROM
326 && (be->drive->type < pos->drive->type
327 || (be->drive->type == pos->drive->type
328 && be->drive->cntl_id < pos->drive->cntl_id)))
329 break;
330 }
331 be->next = *pprev;
332 *pprev = be;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500333}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500334
Kevin O'Connor031ef552010-12-27 19:26:57 -0500335// Return the given priority if it's set - defaultprio otherwise.
336static inline int defPrio(int priority, int defaultprio) {
337 return (priority < 0) ? defaultprio : priority;
338}
339
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500340// Add a BEV vector for a given pnp compatible option rom.
Gleb Natapov4c90a202010-12-07 13:50:54 +0200341void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500342boot_add_bev(u16 seg, u16 bev, u16 desc, int prio)
Gleb Natapov4c90a202010-12-07 13:50:54 +0200343{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500344 bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio)
345 , SEGOFF(seg, bev).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500346 , desc ? MAKE_FLATPTR(seg, desc) : "Unknown");
347 DefaultBEVPrio = DEFAULT_PRIO;
Gleb Natapov4c90a202010-12-07 13:50:54 +0200348}
349
Kevin O'Connor0a924122009-02-08 19:43:47 -0500350// Add a bcv entry for an expansion card harddrive or legacy option rom
351void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500352boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500353{
Kevin O'Connor88e745e2012-01-14 11:58:14 -0500354 bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio)
Kevin O'Connor031ef552010-12-27 19:26:57 -0500355 , SEGOFF(seg, ip).segoff
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500356 , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom");
Kevin O'Connor0a924122009-02-08 19:43:47 -0500357}
358
Kevin O'Connor0a924122009-02-08 19:43:47 -0500359void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500360boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500361{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500362 bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500363 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500364}
Kevin O'Connor0a924122009-02-08 19:43:47 -0500365
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500366void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500367boot_add_hd(struct drive_s *drive_g, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500368{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500369 bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500370 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500371}
372
373void
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500374boot_add_cd(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_CDROM, defPrio(prio, DefaultCDPrio)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500377 , (u32)drive_g, desc);
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500378}
379
380// Add a CBFS payload entry
381void
Kevin O'Connor031ef552010-12-27 19:26:57 -0500382boot_add_cbfs(void *data, const char *desc, int prio)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500383{
Kevin O'Connor031ef552010-12-27 19:26:57 -0500384 bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500385}
386
387
388/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500389 * Boot menu and BCV execution
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500390 ****************************************************************/
391
Kevin O'Connor73023002011-07-05 20:34:34 -0400392#define DEFAULT_BOOTMENU_WAIT 2500
393
Kevin O'Connor0a924122009-02-08 19:43:47 -0500394// Show IPL option menu.
395static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500396interactive_bootmenu(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500397{
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400398 if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu())
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500399 return;
400
401 while (get_keystroke(0) >= 0)
402 ;
403
Kevin O'Connor57174c72012-12-21 22:52:50 -0500404 printf("\nPress F12 for boot menu.\n\n");
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500405
Kevin O'Connor73023002011-07-05 20:34:34 -0400406 u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400407 enable_bootsplash();
Kevin O'Connor73023002011-07-05 20:34:34 -0400408 int scan_code = get_keystroke(menutime);
Kevin O'Connor9a01a9c2010-08-25 21:07:48 -0400409 disable_bootsplash();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500410 if (scan_code != 0x86)
411 /* not F12 */
412 return;
413
414 while (get_keystroke(0) >= 0)
415 ;
416
417 printf("Select boot device:\n\n");
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400418 wait_threads();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500419
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500420 // Show menu items
421 struct bootentry_s *pos = BootList;
422 int maxmenu = 0;
423 while (pos) {
424 char desc[60];
425 maxmenu++;
426 printf("%d. %s\n", maxmenu
427 , strtcpy(desc, pos->description, ARRAY_SIZE(desc)));
428 pos = pos->next;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500429 }
430
Kevin O'Connor551caa22010-12-29 13:25:18 -0500431 // Get key press
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500432 for (;;) {
433 scan_code = get_keystroke(1000);
Kevin O'Connor551caa22010-12-29 13:25:18 -0500434 if (scan_code >= 1 && scan_code <= maxmenu+1)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500435 break;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500436 }
437 printf("\n");
Kevin O'Connor551caa22010-12-29 13:25:18 -0500438 if (scan_code == 0x01)
439 // ESC
440 return;
441
442 // Find entry and make top priority.
443 int choice = scan_code - 1;
444 struct bootentry_s **pprev = &BootList;
445 while (--choice)
446 pprev = &(*pprev)->next;
447 pos = *pprev;
448 *pprev = pos->next;
449 pos->next = BootList;
450 BootList = pos;
451 pos->priority = 0;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500452}
453
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500454// BEV (Boot Execution Vector) list
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500455struct bev_s {
456 int type;
457 u32 vector;
458};
459static struct bev_s BEV[20];
460static int BEVCount;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500461static int HaveHDBoot, HaveFDBoot;
462
Kevin O'Connor0a924122009-02-08 19:43:47 -0500463static void
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500464add_bev(int type, u32 vector)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500465{
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500466 if (type == IPL_TYPE_HARDDISK && HaveHDBoot++)
467 return;
468 if (type == IPL_TYPE_FLOPPY && HaveFDBoot++)
469 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500470 if (BEVCount >= ARRAY_SIZE(BEV))
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500471 return;
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500472 struct bev_s *bev = &BEV[BEVCount++];
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500473 bev->type = type;
474 bev->vector = vector;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500475}
476
477// Prepare for boot - show menu and run bcvs.
478void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500479boot_prepboot(void)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500480{
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400481 if (! CONFIG_BOOT) {
482 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500483 return;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400484 }
Kevin O'Connor0a924122009-02-08 19:43:47 -0500485
Kevin O'Connora5826b52009-10-24 17:57:29 -0400486 // XXX - show available drives?
487
Kevin O'Connor0a924122009-02-08 19:43:47 -0500488 // Allow user to modify BCV/IPL order.
489 interactive_bootmenu();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400490 wait_threads();
Kevin O'Connor0a924122009-02-08 19:43:47 -0500491
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500492 int haltprio = find_prio("HALT");
493 if (haltprio >= 0)
494 bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT");
495
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500496 // Map drives and populate BEV list
497 struct bootentry_s *pos = BootList;
498 while (pos) {
499 switch (pos->type) {
500 case IPL_TYPE_BCV:
501 call_bcv(pos->vector.seg, pos->vector.offset);
502 add_bev(IPL_TYPE_HARDDISK, 0);
503 break;
504 case IPL_TYPE_FLOPPY:
505 map_floppy_drive(pos->drive);
506 add_bev(IPL_TYPE_FLOPPY, 0);
507 break;
508 case IPL_TYPE_HARDDISK:
509 map_hd_drive(pos->drive);
510 add_bev(IPL_TYPE_HARDDISK, 0);
511 break;
512 case IPL_TYPE_CDROM:
513 map_cd_drive(pos->drive);
514 // NO BREAK
515 default:
516 add_bev(pos->type, pos->data);
517 break;
518 }
519 pos = pos->next;
520 }
Kevin O'Connorafd05202009-08-16 12:21:44 -0400521
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500522 // If nothing added a floppy/hd boot - add it manually.
523 add_bev(IPL_TYPE_FLOPPY, 0);
524 add_bev(IPL_TYPE_HARDDISK, 0);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500525}
526
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500527
528/****************************************************************
529 * Boot code (int 18/19)
530 ****************************************************************/
531
Kevin O'Connor0a924122009-02-08 19:43:47 -0500532// Jump to a bootup entry point.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500533static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500534call_boot_entry(struct segoff_s bootsegip, u8 bootdrv)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500535{
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500536 dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500537 struct bregs br;
538 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400539 br.flags = F_IF;
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500540 br.code = bootsegip;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500541 // Set the magic number in ax and the boot drive in dl.
542 br.dl = bootdrv;
543 br.ax = 0xaa55;
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400544 farcall16(&br);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500545}
546
547// Boot from a disk (either floppy or harddrive)
548static void
549boot_disk(u8 bootdrv, int checksig)
550{
551 u16 bootseg = 0x07c0;
552
553 // Read sector
554 struct bregs br;
555 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400556 br.flags = F_IF;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500557 br.dl = bootdrv;
558 br.es = bootseg;
559 br.ah = 2;
560 br.al = 1;
561 br.cl = 1;
562 call16_int(0x13, &br);
563
564 if (br.flags & F_CF) {
565 printf("Boot failed: could not read the boot disk\n\n");
566 return;
567 }
568
569 if (checksig) {
570 struct mbr_s *mbr = (void*)0;
571 if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) {
572 printf("Boot failed: not a bootable disk\n\n");
573 return;
574 }
575 }
576
577 /* Canonicalize bootseg:bootip */
578 u16 bootip = (bootseg & 0x0fff) << 4;
579 bootseg &= 0xf000;
580
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500581 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500582}
583
584// Boot from a CD-ROM
585static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500586boot_cdrom(struct drive_s *drive_g)
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500587{
588 if (! CONFIG_CDROM_BOOT)
589 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500590 printf("Booting from DVD/CD...\n");
Gleb Natapov4c90a202010-12-07 13:50:54 +0200591
Gleb Natapov4c90a202010-12-07 13:50:54 +0200592 int status = cdrom_boot(drive_g);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500593 if (status) {
Kevin O'Connorecbcf772010-12-29 13:26:01 -0500594 printf("Boot failed: Could not read from CDROM (code %04x)\n", status);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500595 return;
596 }
597
Kevin O'Connord3140832012-05-13 22:46:12 -0400598 u8 bootdrv = CDEmu.emulated_extdrive;
599 u16 bootseg = CDEmu.load_segment;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500600 /* Canonicalize bootseg:bootip */
601 u16 bootip = (bootseg & 0x0fff) << 4;
602 bootseg &= 0xf000;
603
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500604 call_boot_entry(SEGOFF(bootseg, bootip), bootdrv);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500605}
606
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400607// Boot from a CBFS payload
Kevin O'Connor67823442009-04-13 14:14:51 -0400608static void
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500609boot_cbfs(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400610{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400611 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400612 return;
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500613 printf("Booting from CBFS...\n");
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500614 cbfs_run_payload(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400615}
616
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500617// Boot from a BEV entry on an optionrom.
618static void
619boot_rom(u32 vector)
620{
621 printf("Booting from ROM...\n");
622 struct segoff_s so;
623 so.segoff = vector;
624 call_boot_entry(so, 0);
625}
626
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400627// Unable to find bootable device - warn user and eventually retry.
628static void
629boot_fail(void)
630{
631 u32 retrytime = romfile_loadint("etc/boot-fail-wait", 60*1000);
632 if (retrytime == (u32)-1)
633 printf("No bootable device.\n");
634 else
635 printf("No bootable device. Retrying in %d seconds.\n", retrytime/1000);
636 // Wait for 'retrytime' milliseconds and then reboot.
637 u32 end = calc_future_timer(retrytime);
638 for (;;) {
639 if (retrytime != (u32)-1 && check_timer(end))
640 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400641 yield_toirq();
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400642 }
643 printf("Rebooting.\n");
644 struct bregs br;
645 memset(&br, 0, sizeof(br));
646 br.code = SEGOFF(SEG_BIOS, (u32)reset_vector);
Kevin O'Connorc7ffbac2012-03-25 11:04:10 -0400647 farcall16big(&br);
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400648}
649
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500650// Determine next boot method and attempt a boot using it.
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500651static void
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400652do_boot(int seq_nr)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500653{
Kevin O'Connor40967022008-07-21 22:23:05 -0400654 if (! CONFIG_BOOT)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500655 panic("Boot support not compiled in.\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400656
Kevin O'Connorb8fcf462012-05-12 22:12:22 -0400657 if (seq_nr >= BEVCount)
658 boot_fail();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500659
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500660 // Boot the given BEV type.
Kevin O'Connor7bb15842010-12-29 11:25:14 -0500661 struct bev_s *ie = &BEV[seq_nr];
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400662 switch (ie->type) {
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400663 case IPL_TYPE_FLOPPY:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500664 printf("Booting from Floppy...\n");
Kevin O'Connorbaaadb62010-12-29 11:18:04 -0500665 boot_disk(0x00, CheckFloppySig);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500666 break;
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400667 case IPL_TYPE_HARDDISK:
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500668 printf("Booting from Hard Disk...\n");
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500669 boot_disk(0x80, 1);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500670 break;
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500671 case IPL_TYPE_CDROM:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500672 boot_cdrom((void*)ie->vector);
Kevin O'Connor67823442009-04-13 14:14:51 -0400673 break;
674 case IPL_TYPE_CBFS:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500675 boot_cbfs((void*)ie->vector);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500676 break;
677 case IPL_TYPE_BEV:
Kevin O'Connorf13a1802010-12-29 11:52:33 -0500678 boot_rom(ie->vector);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500679 break;
Kevin O'Connor54ae5432013-01-12 16:36:11 -0500680 case IPL_TYPE_HALT:
681 boot_fail();
682 break;
Kevin O'Connor180a9592008-03-04 22:50:53 -0500683 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500684
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500685 // Boot failed: invoke the boot recovery function
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400686 struct bregs br;
687 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400688 br.flags = F_IF;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400689 call16_int(0x18, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500690}
691
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400692int BootSequence VARLOW = -1;
693
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500694// Boot Failure recovery: try the next device.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500695void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500696handle_18(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500697{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500698 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400699 debug_enter(NULL, DEBUG_HDL_18);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400700 int seq = BootSequence + 1;
701 BootSequence = seq;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400702 do_boot(seq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500703}
704
705// INT 19h Boot Load Service Entry Point
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500706void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500707handle_19(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500708{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500709 debug_serial_preinit();
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400710 debug_enter(NULL, DEBUG_HDL_19);
Kevin O'Connor9d8e0a62012-05-13 12:23:58 -0400711 BootSequence = 0;
Kevin O'Connor7af49bf2008-03-09 13:46:13 -0400712 do_boot(0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500713}