Kevin O'Connor | f13b008 | 2008-08-17 11:26:42 -0400 | [diff] [blame] | 1 | // Code to load disk image and start system boot. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 2 | // |
Kevin O'Connor | 0ac5e9e | 2013-09-14 13:09:27 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 7 | |
Kevin O'Connor | 135f3f6 | 2013-09-14 23:57:26 -0400 | [diff] [blame] | 8 | #include "block.h" // struct drive_s |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 9 | #include "bregs.h" // struct bregs |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 10 | #include "config.h" // CONFIG_* |
Kevin O'Connor | ccee6e8 | 2013-09-02 21:25:21 -0400 | [diff] [blame] | 11 | #include "fw/paravirt.h" // qemu_cfg_show_boot_menu |
Kevin O'Connor | 5d369d8 | 2013-09-02 20:48:46 -0400 | [diff] [blame] | 12 | #include "hw/pci.h" // pci_bdf_to_* |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 13 | #include "hw/rtc.h" // rtc_read |
Kevin O'Connor | 5d369d8 | 2013-09-02 20:48:46 -0400 | [diff] [blame] | 14 | #include "hw/usb.h" // struct usbdevice_s |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 15 | #include "list.h" // hlist_node |
Kevin O'Connor | 9dea590 | 2013-09-14 20:23:54 -0400 | [diff] [blame] | 16 | #include "malloc.h" // free |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 17 | #include "output.h" // dprintf |
Kevin O'Connor | 41639f8 | 2013-09-14 19:37:36 -0400 | [diff] [blame] | 18 | #include "romfile.h" // romfile_loadint |
Kevin O'Connor | 135f3f6 | 2013-09-14 23:57:26 -0400 | [diff] [blame] | 19 | #include "std/disk.h" // struct mbr_s |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 20 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 21 | #include "util.h" // irqtimer_calc |
Kevin O'Connor | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 22 | |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 23 | |
| 24 | /**************************************************************** |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 25 | * Boot priority ordering |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 26 | ****************************************************************/ |
| 27 | |
Kevin O'Connor | 70c94dd | 2013-03-08 19:39:49 -0500 | [diff] [blame] | 28 | static char **Bootorder VARVERIFY32INIT; |
Kevin O'Connor | 3da2c1c | 2010-12-29 11:37:41 -0500 | [diff] [blame] | 29 | static int BootorderCount; |
| 30 | |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 31 | static void |
| 32 | loadBootOrder(void) |
| 33 | { |
Kevin O'Connor | 49bf57b | 2011-05-10 22:08:30 -0400 | [diff] [blame] | 34 | if (!CONFIG_BOOTORDER) |
| 35 | return; |
| 36 | |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 37 | char *f = romfile_loadfile("bootorder", NULL); |
| 38 | if (!f) |
| 39 | return; |
| 40 | |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 41 | int i = 0; |
Kevin O'Connor | 3da2c1c | 2010-12-29 11:37:41 -0500 | [diff] [blame] | 42 | BootorderCount = 1; |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 43 | while (f[i]) { |
| 44 | if (f[i] == '\n') |
Kevin O'Connor | 3da2c1c | 2010-12-29 11:37:41 -0500 | [diff] [blame] | 45 | BootorderCount++; |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 46 | i++; |
| 47 | } |
Kevin O'Connor | 3da2c1c | 2010-12-29 11:37:41 -0500 | [diff] [blame] | 48 | Bootorder = malloc_tmphigh(BootorderCount*sizeof(char*)); |
| 49 | if (!Bootorder) { |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 50 | warn_noalloc(); |
| 51 | free(f); |
Kevin O'Connor | 4d0c592 | 2011-01-26 21:04:01 -0500 | [diff] [blame] | 52 | BootorderCount = 0; |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
Gerd Hoffmann | f5beab4 | 2013-12-06 10:29:09 +0100 | [diff] [blame] | 56 | dprintf(1, "boot order:\n"); |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 57 | i = 0; |
| 58 | do { |
Kevin O'Connor | 3da2c1c | 2010-12-29 11:37:41 -0500 | [diff] [blame] | 59 | Bootorder[i] = f; |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 60 | f = strchr(f, '\n'); |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 61 | if (f) |
| 62 | *(f++) = '\0'; |
Kevin O'Connor | d15b010 | 2014-01-31 19:38:36 -0500 | [diff] [blame] | 63 | Bootorder[i] = nullTrailingSpace(Bootorder[i]); |
Gerd Hoffmann | f5beab4 | 2013-12-06 10:29:09 +0100 | [diff] [blame] | 64 | dprintf(1, "%d: %s\n", i+1, Bootorder[i]); |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 65 | i++; |
| 66 | } while (f); |
| 67 | } |
| 68 | |
| 69 | // See if 'str' starts with 'glob' - if glob contains an '*' character |
| 70 | // it will match any number of characters in str that aren't a '/' or |
| 71 | // the next glob character. |
| 72 | static char * |
| 73 | glob_prefix(const char *glob, const char *str) |
| 74 | { |
| 75 | for (;;) { |
| 76 | if (!*glob && (!*str || *str == '/')) |
| 77 | return (char*)str; |
| 78 | if (*glob == '*') { |
| 79 | if (!*str || *str == '/' || *str == glob[1]) |
| 80 | glob++; |
| 81 | else |
| 82 | str++; |
| 83 | continue; |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 84 | } |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 85 | if (*glob != *str) |
| 86 | return NULL; |
| 87 | glob++; |
| 88 | str++; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Search the bootorder list for the given glob pattern. |
| 93 | static int |
| 94 | find_prio(const char *glob) |
| 95 | { |
| 96 | dprintf(1, "Searching bootorder for: %s\n", glob); |
| 97 | int i; |
| 98 | for (i = 0; i < BootorderCount; i++) |
| 99 | if (glob_prefix(glob, Bootorder[i])) |
| 100 | return i+1; |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | #define FW_PCI_DOMAIN "/pci@i0cf8" |
| 105 | |
Kevin O'Connor | 659c99d | 2011-06-20 23:29:15 -0400 | [diff] [blame] | 106 | static char * |
| 107 | build_pci_path(char *buf, int max, const char *devname, struct pci_device *pci) |
| 108 | { |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 109 | // Build the string path of a bdf - for example: /pci@i0cf8/isa@1,2 |
| 110 | char *p = buf; |
Kevin O'Connor | 659c99d | 2011-06-20 23:29:15 -0400 | [diff] [blame] | 111 | if (pci->parent) { |
| 112 | p = build_pci_path(p, max, "pci-bridge", pci->parent); |
Kevin O'Connor | d08eb9c | 2011-01-10 00:48:45 -0500 | [diff] [blame] | 113 | } else { |
Kevin O'Connor | 659c99d | 2011-06-20 23:29:15 -0400 | [diff] [blame] | 114 | if (pci->rootbus) |
| 115 | p += snprintf(p, max, "/pci-root@%x", pci->rootbus); |
Kevin O'Connor | d08eb9c | 2011-01-10 00:48:45 -0500 | [diff] [blame] | 116 | p += snprintf(p, buf+max-p, "%s", FW_PCI_DOMAIN); |
| 117 | } |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 118 | |
Kevin O'Connor | 659c99d | 2011-06-20 23:29:15 -0400 | [diff] [blame] | 119 | int dev = pci_bdf_to_dev(pci->bdf), fn = pci_bdf_to_fn(pci->bdf); |
Kevin O'Connor | d08eb9c | 2011-01-10 00:48:45 -0500 | [diff] [blame] | 120 | p += snprintf(p, buf+max-p, "/%s@%x", devname, dev); |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 121 | if (fn) |
| 122 | p += snprintf(p, buf+max-p, ",%x", fn); |
| 123 | return p; |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 126 | int bootprio_find_pci_device(struct pci_device *pci) |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 127 | { |
David Woodhouse | 118469a | 2013-01-25 19:46:25 -0600 | [diff] [blame] | 128 | if (CONFIG_CSM) |
| 129 | return csm_bootprio_pci(pci); |
Kevin O'Connor | 49bf57b | 2011-05-10 22:08:30 -0400 | [diff] [blame] | 130 | if (!CONFIG_BOOTORDER) |
| 131 | return -1; |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 132 | // Find pci device - for example: /pci@i0cf8/ethernet@5 |
| 133 | char desc[256]; |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 134 | build_pci_path(desc, sizeof(desc), "*", pci); |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 135 | return find_prio(desc); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Paolo Bonzini | c5c488f | 2012-02-27 17:22:23 +0100 | [diff] [blame] | 138 | int bootprio_find_scsi_device(struct pci_device *pci, int target, int lun) |
| 139 | { |
| 140 | if (!CONFIG_BOOTORDER) |
| 141 | return -1; |
| 142 | if (!pci) |
| 143 | // support only pci machine for now |
| 144 | return -1; |
| 145 | // Find scsi drive - for example: /pci@i0cf8/scsi@5/channel@0/disk@1,0 |
| 146 | char desc[256], *p; |
| 147 | p = build_pci_path(desc, sizeof(desc), "*", pci); |
Markus Armbruster | 275672e | 2014-08-15 08:58:17 +0200 | [diff] [blame^] | 148 | snprintf(p, desc+sizeof(desc)-p, "/*@0/*@%x,%x", target, lun); |
Paolo Bonzini | c5c488f | 2012-02-27 17:22:23 +0100 | [diff] [blame] | 149 | return find_prio(desc); |
| 150 | } |
| 151 | |
Kevin O'Connor | 95b2e0c | 2011-07-09 14:42:11 -0400 | [diff] [blame] | 152 | int bootprio_find_ata_device(struct pci_device *pci, int chanid, int slave) |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 153 | { |
David Woodhouse | 118469a | 2013-01-25 19:46:25 -0600 | [diff] [blame] | 154 | if (CONFIG_CSM) |
| 155 | return csm_bootprio_ata(pci, chanid, slave); |
Kevin O'Connor | 49bf57b | 2011-05-10 22:08:30 -0400 | [diff] [blame] | 156 | if (!CONFIG_BOOTORDER) |
| 157 | return -1; |
Kevin O'Connor | 95b2e0c | 2011-07-09 14:42:11 -0400 | [diff] [blame] | 158 | if (!pci) |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 159 | // support only pci machine for now |
| 160 | return -1; |
| 161 | // Find ata drive - for example: /pci@i0cf8/ide@1,1/drive@1/disk@0 |
| 162 | char desc[256], *p; |
Kevin O'Connor | 95b2e0c | 2011-07-09 14:42:11 -0400 | [diff] [blame] | 163 | p = build_pci_path(desc, sizeof(desc), "*", pci); |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 164 | snprintf(p, desc+sizeof(desc)-p, "/drive@%x/disk@%x", chanid, slave); |
| 165 | return find_prio(desc); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Kevin O'Connor | 03e589c | 2011-07-09 14:35:37 -0400 | [diff] [blame] | 168 | int bootprio_find_fdc_device(struct pci_device *pci, int port, int fdid) |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 169 | { |
David Woodhouse | 118469a | 2013-01-25 19:46:25 -0600 | [diff] [blame] | 170 | if (CONFIG_CSM) |
| 171 | return csm_bootprio_fdc(pci, port, fdid); |
Kevin O'Connor | 49bf57b | 2011-05-10 22:08:30 -0400 | [diff] [blame] | 172 | if (!CONFIG_BOOTORDER) |
| 173 | return -1; |
Kevin O'Connor | 03e589c | 2011-07-09 14:35:37 -0400 | [diff] [blame] | 174 | if (!pci) |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 175 | // support only pci machine for now |
| 176 | return -1; |
| 177 | // Find floppy - for example: /pci@i0cf8/isa@1/fdc@03f1/floppy@0 |
| 178 | char desc[256], *p; |
Kevin O'Connor | 03e589c | 2011-07-09 14:35:37 -0400 | [diff] [blame] | 179 | p = build_pci_path(desc, sizeof(desc), "isa", pci); |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 180 | snprintf(p, desc+sizeof(desc)-p, "/fdc@%04x/floppy@%x", port, fdid); |
| 181 | return find_prio(desc); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Kevin O'Connor | fce9189 | 2011-07-09 14:47:47 -0400 | [diff] [blame] | 184 | int bootprio_find_pci_rom(struct pci_device *pci, int instance) |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 185 | { |
Kevin O'Connor | 49bf57b | 2011-05-10 22:08:30 -0400 | [diff] [blame] | 186 | if (!CONFIG_BOOTORDER) |
| 187 | return -1; |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 188 | // Find pci rom - for example: /pci@i0cf8/scsi@3:rom2 |
| 189 | char desc[256], *p; |
Kevin O'Connor | fce9189 | 2011-07-09 14:47:47 -0400 | [diff] [blame] | 190 | p = build_pci_path(desc, sizeof(desc), "*", pci); |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 191 | if (instance) |
| 192 | snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance); |
| 193 | return find_prio(desc); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | int bootprio_find_named_rom(const char *name, int instance) |
| 197 | { |
Kevin O'Connor | 49bf57b | 2011-05-10 22:08:30 -0400 | [diff] [blame] | 198 | if (!CONFIG_BOOTORDER) |
| 199 | return -1; |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 200 | // Find named rom - for example: /rom@genroms/linuxboot.bin |
| 201 | char desc[256], *p; |
| 202 | p = desc + snprintf(desc, sizeof(desc), "/rom@%s", name); |
| 203 | if (instance) |
| 204 | snprintf(p, desc+sizeof(desc)-p, ":rom%d", instance); |
| 205 | return find_prio(desc); |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Kevin O'Connor | ea27478 | 2012-03-08 07:49:09 -0500 | [diff] [blame] | 208 | static char * |
| 209 | build_usb_path(char *buf, int max, struct usbhub_s *hub) |
| 210 | { |
| 211 | if (!hub->usbdev) |
| 212 | // Root hub - nothing to add. |
| 213 | return buf; |
| 214 | char *p = build_usb_path(buf, max, hub->usbdev->hub); |
| 215 | p += snprintf(p, buf+max-p, "/hub@%x", hub->usbdev->port+1); |
| 216 | return p; |
| 217 | } |
| 218 | |
Kevin O'Connor | 7fa31b5 | 2012-06-13 08:47:03 -0400 | [diff] [blame] | 219 | int bootprio_find_usb(struct usbdevice_s *usbdev, int lun) |
Kevin O'Connor | c2002a1 | 2010-12-31 14:38:10 -0500 | [diff] [blame] | 220 | { |
Kevin O'Connor | 49bf57b | 2011-05-10 22:08:30 -0400 | [diff] [blame] | 221 | if (!CONFIG_BOOTORDER) |
| 222 | return -1; |
Kevin O'Connor | 7fa31b5 | 2012-06-13 08:47:03 -0400 | [diff] [blame] | 223 | // Find usb - for example: /pci@i0cf8/usb@1,2/storage@1/channel@0/disk@0,0 |
Kevin O'Connor | c2002a1 | 2010-12-31 14:38:10 -0500 | [diff] [blame] | 224 | char desc[256], *p; |
Kevin O'Connor | ea27478 | 2012-03-08 07:49:09 -0500 | [diff] [blame] | 225 | p = build_pci_path(desc, sizeof(desc), "usb", usbdev->hub->cntl->pci); |
| 226 | p = build_usb_path(p, desc+sizeof(desc)-p, usbdev->hub); |
Markus Armbruster | 275672e | 2014-08-15 08:58:17 +0200 | [diff] [blame^] | 227 | snprintf(p, desc+sizeof(desc)-p, "/storage@%x/*@0/*@0,%x" |
Kevin O'Connor | 7fa31b5 | 2012-06-13 08:47:03 -0400 | [diff] [blame] | 228 | , usbdev->port+1, lun); |
| 229 | int ret = find_prio(desc); |
| 230 | if (ret >= 0) |
| 231 | return ret; |
| 232 | // Try usb-host/redir - for example: /pci@i0cf8/usb@1,2/usb-host@1 |
| 233 | snprintf(p, desc+sizeof(desc)-p, "/usb-*@%x", usbdev->port+1); |
Kevin O'Connor | c2002a1 | 2010-12-31 14:38:10 -0500 | [diff] [blame] | 234 | return find_prio(desc); |
| 235 | } |
| 236 | |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 237 | |
| 238 | /**************************************************************** |
| 239 | * Boot setup |
| 240 | ****************************************************************/ |
| 241 | |
Kevin O'Connor | 11a7234 | 2013-03-05 17:52:21 +0800 | [diff] [blame] | 242 | static int BootRetryTime; |
Kevin O'Connor | baaadb6 | 2010-12-29 11:18:04 -0500 | [diff] [blame] | 243 | static int CheckFloppySig = 1; |
| 244 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 245 | #define DEFAULT_PRIO 9999 |
| 246 | |
| 247 | static int DefaultFloppyPrio = 101; |
| 248 | static int DefaultCDPrio = 102; |
| 249 | static int DefaultHDPrio = 103; |
| 250 | static int DefaultBEVPrio = 104; |
| 251 | |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 252 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 253 | boot_init(void) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 254 | { |
| 255 | if (! CONFIG_BOOT) |
| 256 | return; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 257 | |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 258 | if (CONFIG_QEMU) { |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 259 | // On emulators, get boot order from nvram. |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 260 | if (rtc_read(CMOS_BIOS_BOOTFLAG1) & 1) |
Kevin O'Connor | baaadb6 | 2010-12-29 11:18:04 -0500 | [diff] [blame] | 261 | CheckFloppySig = 0; |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 262 | u32 bootorder = (rtc_read(CMOS_BIOS_BOOTFLAG2) |
| 263 | | ((rtc_read(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4)); |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 264 | DefaultFloppyPrio = DefaultCDPrio = DefaultHDPrio |
| 265 | = DefaultBEVPrio = DEFAULT_PRIO; |
| 266 | int i; |
| 267 | for (i=101; i<104; i++) { |
| 268 | u32 val = bootorder & 0x0f; |
| 269 | bootorder >>= 4; |
| 270 | switch (val) { |
| 271 | case 1: DefaultFloppyPrio = i; break; |
| 272 | case 2: DefaultHDPrio = i; break; |
| 273 | case 3: DefaultCDPrio = i; break; |
| 274 | case 4: DefaultBEVPrio = i; break; |
| 275 | } |
| 276 | } |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 277 | } |
Gleb Natapov | b9a7591 | 2010-12-23 11:29:38 +0200 | [diff] [blame] | 278 | |
Kevin O'Connor | 11a7234 | 2013-03-05 17:52:21 +0800 | [diff] [blame] | 279 | BootRetryTime = romfile_loadint("etc/boot-fail-wait", 60*1000); |
| 280 | |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 281 | loadBootOrder(); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 284 | |
| 285 | /**************************************************************** |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 286 | * BootList handling |
Kevin O'Connor | d1a1746 | 2010-12-24 11:17:03 -0500 | [diff] [blame] | 287 | ****************************************************************/ |
| 288 | |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 289 | struct bootentry_s { |
| 290 | int type; |
| 291 | union { |
| 292 | u32 data; |
| 293 | struct segoff_s vector; |
| 294 | struct drive_s *drive; |
| 295 | }; |
| 296 | int priority; |
| 297 | const char *description; |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 298 | struct hlist_node node; |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 299 | }; |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 300 | static struct hlist_head BootList VARVERIFY32INIT; |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 301 | |
Kevin O'Connor | 7bb1584 | 2010-12-29 11:25:14 -0500 | [diff] [blame] | 302 | #define IPL_TYPE_FLOPPY 0x01 |
| 303 | #define IPL_TYPE_HARDDISK 0x02 |
| 304 | #define IPL_TYPE_CDROM 0x03 |
| 305 | #define IPL_TYPE_CBFS 0x20 |
| 306 | #define IPL_TYPE_BEV 0x80 |
| 307 | #define IPL_TYPE_BCV 0x81 |
Kevin O'Connor | 54ae543 | 2013-01-12 16:36:11 -0500 | [diff] [blame] | 308 | #define IPL_TYPE_HALT 0xf0 |
Kevin O'Connor | 7bb1584 | 2010-12-29 11:25:14 -0500 | [diff] [blame] | 309 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 310 | static void |
| 311 | bootentry_add(int type, int prio, u32 data, const char *desc) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 312 | { |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 313 | if (! CONFIG_BOOT) |
| 314 | return; |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 315 | struct bootentry_s *be = malloc_tmp(sizeof(*be)); |
| 316 | if (!be) { |
| 317 | warn_noalloc(); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 318 | return; |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 319 | } |
| 320 | be->type = type; |
| 321 | be->priority = prio; |
| 322 | be->data = data; |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 323 | be->description = desc ?: "?"; |
Kevin O'Connor | 8bf5503 | 2011-01-01 11:00:42 -0500 | [diff] [blame] | 324 | dprintf(3, "Registering bootable: %s (type:%d prio:%d data:%x)\n" |
| 325 | , be->description, type, prio, data); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 326 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 327 | // Add entry in sorted order. |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 328 | struct hlist_node **pprev; |
| 329 | struct bootentry_s *pos; |
Kevin O'Connor | 030a58a | 2013-06-13 21:24:14 -0400 | [diff] [blame] | 330 | hlist_for_each_entry_pprev(pos, pprev, &BootList, node) { |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 331 | if (be->priority < pos->priority) |
| 332 | break; |
| 333 | if (be->priority > pos->priority) |
| 334 | continue; |
| 335 | if (be->type < pos->type) |
| 336 | break; |
| 337 | if (be->type > pos->type) |
| 338 | continue; |
| 339 | if (be->type <= IPL_TYPE_CDROM |
| 340 | && (be->drive->type < pos->drive->type |
| 341 | || (be->drive->type == pos->drive->type |
| 342 | && be->drive->cntl_id < pos->drive->cntl_id))) |
| 343 | break; |
| 344 | } |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 345 | hlist_add(&be->node, pprev); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 346 | } |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 347 | |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 348 | // Return the given priority if it's set - defaultprio otherwise. |
| 349 | static inline int defPrio(int priority, int defaultprio) { |
| 350 | return (priority < 0) ? defaultprio : priority; |
| 351 | } |
| 352 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 353 | // Add a BEV vector for a given pnp compatible option rom. |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 354 | void |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 355 | boot_add_bev(u16 seg, u16 bev, u16 desc, int prio) |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 356 | { |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 357 | bootentry_add(IPL_TYPE_BEV, defPrio(prio, DefaultBEVPrio) |
| 358 | , SEGOFF(seg, bev).segoff |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 359 | , desc ? MAKE_FLATPTR(seg, desc) : "Unknown"); |
| 360 | DefaultBEVPrio = DEFAULT_PRIO; |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 363 | // Add a bcv entry for an expansion card harddrive or legacy option rom |
| 364 | void |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 365 | boot_add_bcv(u16 seg, u16 ip, u16 desc, int prio) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 366 | { |
Kevin O'Connor | 88e745e | 2012-01-14 11:58:14 -0500 | [diff] [blame] | 367 | bootentry_add(IPL_TYPE_BCV, defPrio(prio, DefaultHDPrio) |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 368 | , SEGOFF(seg, ip).segoff |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 369 | , desc ? MAKE_FLATPTR(seg, desc) : "Legacy option rom"); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 370 | } |
| 371 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 372 | void |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 373 | boot_add_floppy(struct drive_s *drive_g, const char *desc, int prio) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 374 | { |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 375 | bootentry_add(IPL_TYPE_FLOPPY, defPrio(prio, DefaultFloppyPrio) |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 376 | , (u32)drive_g, desc); |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 377 | } |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 378 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 379 | void |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 380 | boot_add_hd(struct drive_s *drive_g, const char *desc, int prio) |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 381 | { |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 382 | bootentry_add(IPL_TYPE_HARDDISK, defPrio(prio, DefaultHDPrio) |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 383 | , (u32)drive_g, desc); |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 387 | boot_add_cd(struct drive_s *drive_g, const char *desc, int prio) |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 388 | { |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 389 | bootentry_add(IPL_TYPE_CDROM, defPrio(prio, DefaultCDPrio) |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 390 | , (u32)drive_g, desc); |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // Add a CBFS payload entry |
| 394 | void |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 395 | boot_add_cbfs(void *data, const char *desc, int prio) |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 396 | { |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 397 | bootentry_add(IPL_TYPE_CBFS, defPrio(prio, DEFAULT_PRIO), (u32)data, desc); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
| 401 | /**************************************************************** |
Kevin O'Connor | 0ac5e9e | 2013-09-14 13:09:27 -0400 | [diff] [blame] | 402 | * Keyboard calls |
| 403 | ****************************************************************/ |
| 404 | |
| 405 | // See if a keystroke is pending in the keyboard buffer. |
| 406 | static int |
| 407 | check_for_keystroke(void) |
| 408 | { |
| 409 | struct bregs br; |
| 410 | memset(&br, 0, sizeof(br)); |
| 411 | br.flags = F_IF|F_ZF; |
| 412 | br.ah = 1; |
| 413 | call16_int(0x16, &br); |
| 414 | return !(br.flags & F_ZF); |
| 415 | } |
| 416 | |
| 417 | // Return a keystroke - waiting forever if necessary. |
| 418 | static int |
| 419 | get_raw_keystroke(void) |
| 420 | { |
| 421 | struct bregs br; |
| 422 | memset(&br, 0, sizeof(br)); |
| 423 | br.flags = F_IF; |
| 424 | call16_int(0x16, &br); |
| 425 | return br.ah; |
| 426 | } |
| 427 | |
| 428 | // Read a keystroke - waiting up to 'msec' milliseconds. |
| 429 | static int |
| 430 | get_keystroke(int msec) |
| 431 | { |
| 432 | u32 end = irqtimer_calc(msec); |
| 433 | for (;;) { |
| 434 | if (check_for_keystroke()) |
| 435 | return get_raw_keystroke(); |
| 436 | if (irqtimer_check(end)) |
| 437 | return -1; |
| 438 | yield_toirq(); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /**************************************************************** |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 444 | * Boot menu and BCV execution |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 445 | ****************************************************************/ |
| 446 | |
Kevin O'Connor | 7302300 | 2011-07-05 20:34:34 -0400 | [diff] [blame] | 447 | #define DEFAULT_BOOTMENU_WAIT 2500 |
| 448 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 449 | // Show IPL option menu. |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 450 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 451 | interactive_bootmenu(void) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 452 | { |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 453 | // XXX - show available drives? |
| 454 | |
Kevin O'Connor | 56c5089 | 2013-02-09 19:25:51 -0500 | [diff] [blame] | 455 | if (! CONFIG_BOOTMENU || !romfile_loadint("etc/show-boot-menu", 1)) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 456 | return; |
| 457 | |
| 458 | while (get_keystroke(0) >= 0) |
| 459 | ; |
| 460 | |
Kevin O'Connor | 129f11e | 2013-08-02 14:13:50 -0400 | [diff] [blame] | 461 | char *bootmsg = romfile_loadfile("etc/boot-menu-message", NULL); |
| 462 | int menukey = romfile_loadint("etc/boot-menu-key", 0x86); |
| 463 | printf("%s", bootmsg ?: "\nPress F12 for boot menu.\n\n"); |
| 464 | free(bootmsg); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 465 | |
Kevin O'Connor | 7302300 | 2011-07-05 20:34:34 -0400 | [diff] [blame] | 466 | u32 menutime = romfile_loadint("etc/boot-menu-wait", DEFAULT_BOOTMENU_WAIT); |
Kevin O'Connor | 9a01a9c | 2010-08-25 21:07:48 -0400 | [diff] [blame] | 467 | enable_bootsplash(); |
Kevin O'Connor | 7302300 | 2011-07-05 20:34:34 -0400 | [diff] [blame] | 468 | int scan_code = get_keystroke(menutime); |
Kevin O'Connor | 9a01a9c | 2010-08-25 21:07:48 -0400 | [diff] [blame] | 469 | disable_bootsplash(); |
Kevin O'Connor | 129f11e | 2013-08-02 14:13:50 -0400 | [diff] [blame] | 470 | if (scan_code != menukey) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 471 | return; |
| 472 | |
| 473 | while (get_keystroke(0) >= 0) |
| 474 | ; |
| 475 | |
| 476 | printf("Select boot device:\n\n"); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 477 | wait_threads(); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 478 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 479 | // Show menu items |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 480 | int maxmenu = 0; |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 481 | struct bootentry_s *pos; |
| 482 | hlist_for_each_entry(pos, &BootList, node) { |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 483 | char desc[60]; |
| 484 | maxmenu++; |
| 485 | printf("%d. %s\n", maxmenu |
| 486 | , strtcpy(desc, pos->description, ARRAY_SIZE(desc))); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 487 | } |
| 488 | |
Kevin O'Connor | 551caa2 | 2010-12-29 13:25:18 -0500 | [diff] [blame] | 489 | // Get key press |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 490 | for (;;) { |
| 491 | scan_code = get_keystroke(1000); |
Kevin O'Connor | 551caa2 | 2010-12-29 13:25:18 -0500 | [diff] [blame] | 492 | if (scan_code >= 1 && scan_code <= maxmenu+1) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 493 | break; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 494 | } |
| 495 | printf("\n"); |
Kevin O'Connor | 551caa2 | 2010-12-29 13:25:18 -0500 | [diff] [blame] | 496 | if (scan_code == 0x01) |
| 497 | // ESC |
| 498 | return; |
| 499 | |
| 500 | // Find entry and make top priority. |
| 501 | int choice = scan_code - 1; |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 502 | hlist_for_each_entry(pos, &BootList, node) { |
| 503 | if (! --choice) |
| 504 | break; |
| 505 | } |
| 506 | hlist_del(&pos->node); |
Kevin O'Connor | 551caa2 | 2010-12-29 13:25:18 -0500 | [diff] [blame] | 507 | pos->priority = 0; |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 508 | hlist_add_head(&pos->node, &BootList); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 509 | } |
| 510 | |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 511 | // BEV (Boot Execution Vector) list |
Kevin O'Connor | 7bb1584 | 2010-12-29 11:25:14 -0500 | [diff] [blame] | 512 | struct bev_s { |
| 513 | int type; |
| 514 | u32 vector; |
| 515 | }; |
| 516 | static struct bev_s BEV[20]; |
| 517 | static int BEVCount; |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 518 | static int HaveHDBoot, HaveFDBoot; |
| 519 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 520 | static void |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 521 | add_bev(int type, u32 vector) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 522 | { |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 523 | if (type == IPL_TYPE_HARDDISK && HaveHDBoot++) |
| 524 | return; |
| 525 | if (type == IPL_TYPE_FLOPPY && HaveFDBoot++) |
| 526 | return; |
Kevin O'Connor | 7bb1584 | 2010-12-29 11:25:14 -0500 | [diff] [blame] | 527 | if (BEVCount >= ARRAY_SIZE(BEV)) |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 528 | return; |
Kevin O'Connor | 7bb1584 | 2010-12-29 11:25:14 -0500 | [diff] [blame] | 529 | struct bev_s *bev = &BEV[BEVCount++]; |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 530 | bev->type = type; |
| 531 | bev->vector = vector; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // Prepare for boot - show menu and run bcvs. |
| 535 | void |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 536 | bcv_prepboot(void) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 537 | { |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 538 | if (! CONFIG_BOOT) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 539 | return; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 540 | |
Kevin O'Connor | 54ae543 | 2013-01-12 16:36:11 -0500 | [diff] [blame] | 541 | int haltprio = find_prio("HALT"); |
| 542 | if (haltprio >= 0) |
| 543 | bootentry_add(IPL_TYPE_HALT, haltprio, 0, "HALT"); |
| 544 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 545 | // Map drives and populate BEV list |
Kevin O'Connor | 446defb | 2013-06-08 21:50:53 -0400 | [diff] [blame] | 546 | struct bootentry_s *pos; |
| 547 | hlist_for_each_entry(pos, &BootList, node) { |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 548 | switch (pos->type) { |
| 549 | case IPL_TYPE_BCV: |
| 550 | call_bcv(pos->vector.seg, pos->vector.offset); |
| 551 | add_bev(IPL_TYPE_HARDDISK, 0); |
| 552 | break; |
| 553 | case IPL_TYPE_FLOPPY: |
| 554 | map_floppy_drive(pos->drive); |
| 555 | add_bev(IPL_TYPE_FLOPPY, 0); |
| 556 | break; |
| 557 | case IPL_TYPE_HARDDISK: |
| 558 | map_hd_drive(pos->drive); |
| 559 | add_bev(IPL_TYPE_HARDDISK, 0); |
| 560 | break; |
| 561 | case IPL_TYPE_CDROM: |
| 562 | map_cd_drive(pos->drive); |
| 563 | // NO BREAK |
| 564 | default: |
| 565 | add_bev(pos->type, pos->data); |
| 566 | break; |
| 567 | } |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 568 | } |
Kevin O'Connor | afd0520 | 2009-08-16 12:21:44 -0400 | [diff] [blame] | 569 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 570 | // If nothing added a floppy/hd boot - add it manually. |
| 571 | add_bev(IPL_TYPE_FLOPPY, 0); |
| 572 | add_bev(IPL_TYPE_HARDDISK, 0); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 573 | } |
| 574 | |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 575 | |
| 576 | /**************************************************************** |
| 577 | * Boot code (int 18/19) |
| 578 | ****************************************************************/ |
| 579 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 580 | // Jump to a bootup entry point. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 581 | static void |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 582 | call_boot_entry(struct segoff_s bootsegip, u8 bootdrv) |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 583 | { |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 584 | dprintf(1, "Booting from %04x:%04x\n", bootsegip.seg, bootsegip.offset); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 585 | struct bregs br; |
| 586 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 587 | br.flags = F_IF; |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 588 | br.code = bootsegip; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 589 | // Set the magic number in ax and the boot drive in dl. |
| 590 | br.dl = bootdrv; |
| 591 | br.ax = 0xaa55; |
Kevin O'Connor | c7ffbac | 2012-03-25 11:04:10 -0400 | [diff] [blame] | 592 | farcall16(&br); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // Boot from a disk (either floppy or harddrive) |
| 596 | static void |
| 597 | boot_disk(u8 bootdrv, int checksig) |
| 598 | { |
| 599 | u16 bootseg = 0x07c0; |
| 600 | |
| 601 | // Read sector |
| 602 | struct bregs br; |
| 603 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 604 | br.flags = F_IF; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 605 | br.dl = bootdrv; |
| 606 | br.es = bootseg; |
| 607 | br.ah = 2; |
| 608 | br.al = 1; |
| 609 | br.cl = 1; |
| 610 | call16_int(0x13, &br); |
| 611 | |
| 612 | if (br.flags & F_CF) { |
| 613 | printf("Boot failed: could not read the boot disk\n\n"); |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | if (checksig) { |
| 618 | struct mbr_s *mbr = (void*)0; |
| 619 | if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) { |
| 620 | printf("Boot failed: not a bootable disk\n\n"); |
| 621 | return; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /* Canonicalize bootseg:bootip */ |
| 626 | u16 bootip = (bootseg & 0x0fff) << 4; |
| 627 | bootseg &= 0xf000; |
| 628 | |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 629 | call_boot_entry(SEGOFF(bootseg, bootip), bootdrv); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | // Boot from a CD-ROM |
| 633 | static void |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 634 | boot_cdrom(struct drive_s *drive_g) |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 635 | { |
| 636 | if (! CONFIG_CDROM_BOOT) |
| 637 | return; |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 638 | printf("Booting from DVD/CD...\n"); |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 639 | |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 640 | int status = cdrom_boot(drive_g); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 641 | if (status) { |
Kevin O'Connor | ecbcf77 | 2010-12-29 13:26:01 -0500 | [diff] [blame] | 642 | printf("Boot failed: Could not read from CDROM (code %04x)\n", status); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 643 | return; |
| 644 | } |
| 645 | |
Kevin O'Connor | 02f7676 | 2014-05-10 01:20:46 -0400 | [diff] [blame] | 646 | u8 bootdrv = CDEmu.emulated_drive; |
Kevin O'Connor | d314083 | 2012-05-13 22:46:12 -0400 | [diff] [blame] | 647 | u16 bootseg = CDEmu.load_segment; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 648 | /* Canonicalize bootseg:bootip */ |
| 649 | u16 bootip = (bootseg & 0x0fff) << 4; |
| 650 | bootseg &= 0xf000; |
| 651 | |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 652 | call_boot_entry(SEGOFF(bootseg, bootip), bootdrv); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 653 | } |
| 654 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 655 | // Boot from a CBFS payload |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 656 | static void |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 657 | boot_cbfs(struct cbfs_file *file) |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 658 | { |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 659 | if (!CONFIG_COREBOOT_FLASH) |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 660 | return; |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 661 | printf("Booting from CBFS...\n"); |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 662 | cbfs_run_payload(file); |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 663 | } |
| 664 | |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 665 | // Boot from a BEV entry on an optionrom. |
| 666 | static void |
| 667 | boot_rom(u32 vector) |
| 668 | { |
| 669 | printf("Booting from ROM...\n"); |
| 670 | struct segoff_s so; |
| 671 | so.segoff = vector; |
| 672 | call_boot_entry(so, 0); |
| 673 | } |
| 674 | |
Kevin O'Connor | b8fcf46 | 2012-05-12 22:12:22 -0400 | [diff] [blame] | 675 | // Unable to find bootable device - warn user and eventually retry. |
| 676 | static void |
| 677 | boot_fail(void) |
| 678 | { |
Kevin O'Connor | 11a7234 | 2013-03-05 17:52:21 +0800 | [diff] [blame] | 679 | if (BootRetryTime == (u32)-1) |
Kevin O'Connor | b8fcf46 | 2012-05-12 22:12:22 -0400 | [diff] [blame] | 680 | printf("No bootable device.\n"); |
| 681 | else |
Kevin O'Connor | 11a7234 | 2013-03-05 17:52:21 +0800 | [diff] [blame] | 682 | printf("No bootable device. Retrying in %d seconds.\n" |
| 683 | , BootRetryTime/1000); |
| 684 | // Wait for 'BootRetryTime' milliseconds and then reboot. |
Kevin O'Connor | 95ee382 | 2013-07-20 18:07:50 -0400 | [diff] [blame] | 685 | u32 end = irqtimer_calc(BootRetryTime); |
Kevin O'Connor | b8fcf46 | 2012-05-12 22:12:22 -0400 | [diff] [blame] | 686 | for (;;) { |
Kevin O'Connor | 95ee382 | 2013-07-20 18:07:50 -0400 | [diff] [blame] | 687 | if (BootRetryTime != (u32)-1 && irqtimer_check(end)) |
Kevin O'Connor | b8fcf46 | 2012-05-12 22:12:22 -0400 | [diff] [blame] | 688 | break; |
Kevin O'Connor | 94c749c | 2012-05-28 11:44:02 -0400 | [diff] [blame] | 689 | yield_toirq(); |
Kevin O'Connor | b8fcf46 | 2012-05-12 22:12:22 -0400 | [diff] [blame] | 690 | } |
| 691 | printf("Rebooting.\n"); |
Kevin O'Connor | 43197a2 | 2014-06-06 13:49:33 -0400 | [diff] [blame] | 692 | reset(); |
Kevin O'Connor | b8fcf46 | 2012-05-12 22:12:22 -0400 | [diff] [blame] | 693 | } |
| 694 | |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 695 | // Determine next boot method and attempt a boot using it. |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 696 | static void |
Kevin O'Connor | 9d8e0a6 | 2012-05-13 12:23:58 -0400 | [diff] [blame] | 697 | do_boot(int seq_nr) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 698 | { |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 699 | if (! CONFIG_BOOT) |
Kevin O'Connor | e07e18e | 2009-02-08 17:07:29 -0500 | [diff] [blame] | 700 | panic("Boot support not compiled in.\n"); |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 701 | |
Kevin O'Connor | b8fcf46 | 2012-05-12 22:12:22 -0400 | [diff] [blame] | 702 | if (seq_nr >= BEVCount) |
| 703 | boot_fail(); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 704 | |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 705 | // Boot the given BEV type. |
Kevin O'Connor | 7bb1584 | 2010-12-29 11:25:14 -0500 | [diff] [blame] | 706 | struct bev_s *ie = &BEV[seq_nr]; |
Kevin O'Connor | cadaf0e | 2010-07-26 23:47:26 -0400 | [diff] [blame] | 707 | switch (ie->type) { |
Kevin O'Connor | f13b008 | 2008-08-17 11:26:42 -0400 | [diff] [blame] | 708 | case IPL_TYPE_FLOPPY: |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 709 | printf("Booting from Floppy...\n"); |
Kevin O'Connor | baaadb6 | 2010-12-29 11:18:04 -0500 | [diff] [blame] | 710 | boot_disk(0x00, CheckFloppySig); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 711 | break; |
Kevin O'Connor | f13b008 | 2008-08-17 11:26:42 -0400 | [diff] [blame] | 712 | case IPL_TYPE_HARDDISK: |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 713 | printf("Booting from Hard Disk...\n"); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 714 | boot_disk(0x80, 1); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 715 | break; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 716 | case IPL_TYPE_CDROM: |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 717 | boot_cdrom((void*)ie->vector); |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 718 | break; |
| 719 | case IPL_TYPE_CBFS: |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 720 | boot_cbfs((void*)ie->vector); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 721 | break; |
| 722 | case IPL_TYPE_BEV: |
Kevin O'Connor | f13a180 | 2010-12-29 11:52:33 -0500 | [diff] [blame] | 723 | boot_rom(ie->vector); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 724 | break; |
Kevin O'Connor | 54ae543 | 2013-01-12 16:36:11 -0500 | [diff] [blame] | 725 | case IPL_TYPE_HALT: |
| 726 | boot_fail(); |
| 727 | break; |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 728 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 729 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 730 | // Boot failed: invoke the boot recovery function |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 731 | struct bregs br; |
| 732 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 733 | br.flags = F_IF; |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 734 | call16_int(0x18, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 735 | } |
| 736 | |
Kevin O'Connor | 9d8e0a6 | 2012-05-13 12:23:58 -0400 | [diff] [blame] | 737 | int BootSequence VARLOW = -1; |
| 738 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 739 | // Boot Failure recovery: try the next device. |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 740 | void VISIBLE32FLAT |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 741 | handle_18(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 742 | { |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 743 | debug_enter(NULL, DEBUG_HDL_18); |
Kevin O'Connor | 9d8e0a6 | 2012-05-13 12:23:58 -0400 | [diff] [blame] | 744 | int seq = BootSequence + 1; |
| 745 | BootSequence = seq; |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 746 | do_boot(seq); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | // INT 19h Boot Load Service Entry Point |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 750 | void VISIBLE32FLAT |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 751 | handle_19(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 752 | { |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 753 | debug_enter(NULL, DEBUG_HDL_19); |
Kevin O'Connor | 9d8e0a6 | 2012-05-13 12:23:58 -0400 | [diff] [blame] | 754 | BootSequence = 0; |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 755 | do_boot(0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 756 | } |