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