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 | cadaf0e | 2010-07-26 23:47:26 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008-2010 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 | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 8 | #include "util.h" // dprintf |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 9 | #include "biosvar.h" // GET_EBDA |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 10 | #include "config.h" // CONFIG_* |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 11 | #include "disk.h" // cdrom_boot |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 12 | #include "bregs.h" // struct bregs |
Kevin O'Connor | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 13 | #include "boot.h" // struct ipl_s |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 14 | #include "cmos.h" // inb_cmos |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 15 | #include "paravirt.h" |
Kevin O'Connor | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 16 | |
| 17 | struct ipl_s IPL; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 18 | |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 19 | |
| 20 | /**************************************************************** |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 21 | * IPL and BCV handlers |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 22 | ****************************************************************/ |
| 23 | |
| 24 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 25 | boot_setup(void) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 26 | { |
| 27 | if (! CONFIG_BOOT) |
| 28 | return; |
| 29 | dprintf(3, "init boot device ordering\n"); |
| 30 | |
| 31 | memset(&IPL, 0, sizeof(IPL)); |
Stefan Reinauer | 203f6f3 | 2010-06-09 21:10:13 +0200 | [diff] [blame] | 32 | struct ipl_entry_s *ie = &IPL.bev[0]; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 33 | |
| 34 | // Floppy drive |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 35 | ie->type = IPL_TYPE_FLOPPY; |
| 36 | ie->description = "Floppy"; |
| 37 | ie++; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 38 | |
| 39 | // First HDD |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 40 | ie->type = IPL_TYPE_HARDDISK; |
| 41 | ie->description = "Hard Disk"; |
| 42 | ie++; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 43 | |
| 44 | // CDROM |
| 45 | if (CONFIG_CDROM_BOOT) { |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 46 | ie->type = IPL_TYPE_CDROM; |
Stefan Reinauer | 203f6f3 | 2010-06-09 21:10:13 +0200 | [diff] [blame] | 47 | ie->description = "DVD/CD"; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 48 | ie++; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Kevin O'Connor | d9c9361 | 2010-03-20 11:00:45 -0400 | [diff] [blame] | 51 | if (CONFIG_COREBOOT && CONFIG_COREBOOT_FLASH) { |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 52 | ie->type = IPL_TYPE_CBFS; |
| 53 | ie->description = "CBFS"; |
| 54 | ie++; |
| 55 | } |
| 56 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 57 | IPL.bevcount = ie - IPL.bev; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 58 | SET_EBDA(boot_sequence, 0xffff); |
| 59 | if (CONFIG_COREBOOT) { |
| 60 | // XXX - hardcode defaults for coreboot. |
Kevin O'Connor | ff8c141 | 2009-04-18 17:02:41 -0400 | [diff] [blame] | 61 | IPL.bootorder = 0x87654231; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 62 | IPL.checkfloppysig = 1; |
| 63 | } else { |
| 64 | // On emulators, get boot order from nvram. |
| 65 | IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2) |
| 66 | | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4)); |
| 67 | if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)) |
| 68 | IPL.checkfloppysig = 1; |
| 69 | } |
Gleb Natapov | b9a7591 | 2010-12-23 11:29:38 +0200 | [diff] [blame^] | 70 | |
| 71 | u32 file = romfile_find("bootorder"); |
| 72 | if (!file) |
| 73 | return; |
| 74 | |
| 75 | int filesize = romfile_size(file); |
| 76 | dprintf(3, "bootorder file found (len %d)\n", filesize); |
| 77 | |
| 78 | if (filesize == 0) |
| 79 | return; |
| 80 | |
| 81 | char *f = malloc_tmphigh(filesize); |
| 82 | |
| 83 | if (!f) { |
| 84 | warn_noalloc(); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | romfile_copy(file, f, filesize); |
| 89 | int i; |
| 90 | IPL.fw_bootorder_count = 1; |
| 91 | while(f[i]) { |
| 92 | if (f[i] == '\n') |
| 93 | IPL.fw_bootorder_count++; |
| 94 | i++; |
| 95 | } |
| 96 | IPL.fw_bootorder = malloc_tmphigh(IPL.fw_bootorder_count*sizeof(char*)); |
| 97 | if (!IPL.fw_bootorder) { |
| 98 | warn_noalloc(); |
| 99 | free(f); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | dprintf(3, "boot order:\n"); |
| 104 | i = 0; |
| 105 | do { |
| 106 | IPL.fw_bootorder[i] = f; |
| 107 | f = strchr(f, '\n'); |
| 108 | if (f) { |
| 109 | *f = '\0'; |
| 110 | f++; |
| 111 | dprintf(3, "%d: %s\n", i, IPL.fw_bootorder[i]); |
| 112 | i++; |
| 113 | } |
| 114 | } while(f); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Add a BEV vector for a given pnp compatible option rom. |
| 118 | void |
| 119 | add_bev(u16 seg, u16 bev, u16 desc) |
| 120 | { |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 121 | if (! CONFIG_BOOT) |
| 122 | return; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 123 | if (IPL.bevcount >= ARRAY_SIZE(IPL.bev)) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 124 | return; |
| 125 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 126 | struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++]; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 127 | ie->type = IPL_TYPE_BEV; |
| 128 | ie->vector = (seg << 16) | bev; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 129 | const char *d = "Unknown"; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 130 | if (desc) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 131 | d = MAKE_FLATPTR(seg, desc); |
| 132 | ie->description = d; |
| 133 | } |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 134 | |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 135 | // Add a IPL entry for BAID cdrom. |
| 136 | void |
| 137 | add_baid_cdrom(struct drive_s *drive_g) |
| 138 | { |
| 139 | if (! CONFIG_CDROM_BOOT) |
| 140 | return; |
| 141 | |
| 142 | /* put first cdrom into ipl 3 for compatability with qemu */ |
| 143 | struct ipl_entry_s *ie = &IPL.bev[2]; |
| 144 | if (IPL.bevcount >= ARRAY_SIZE(IPL.bev) && ie->vector) |
| 145 | return; |
| 146 | |
| 147 | if (ie->vector) |
| 148 | ie = &IPL.bev[IPL.bevcount++]; |
| 149 | ie->type = IPL_TYPE_CDROM; |
| 150 | ie->vector = (u32)drive_g; |
| 151 | ie->description = "DVD/CD"; |
| 152 | } |
| 153 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 154 | // Add a bcv entry for an expansion card harddrive or legacy option rom |
| 155 | void |
| 156 | add_bcv(u16 seg, u16 ip, u16 desc) |
| 157 | { |
| 158 | if (! CONFIG_BOOT) |
| 159 | return; |
| 160 | if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv)) |
| 161 | return; |
| 162 | |
| 163 | struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++]; |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 164 | ie->type = BCV_TYPE_EXTERNAL; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 165 | ie->vector = (seg << 16) | ip; |
| 166 | const char *d = "Legacy option rom"; |
| 167 | if (desc) |
| 168 | d = MAKE_FLATPTR(seg, desc); |
| 169 | ie->description = d; |
| 170 | } |
| 171 | |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 172 | // Add a bcv entry for an internal harddrive |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 173 | void |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 174 | add_bcv_internal(struct drive_s *drive_g) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 175 | { |
| 176 | if (! CONFIG_BOOT) |
| 177 | return; |
| 178 | if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv)) |
| 179 | return; |
| 180 | |
| 181 | struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++]; |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 182 | if (CONFIG_THREADS) { |
| 183 | // Add to bcv list with assured drive order. |
| 184 | struct ipl_entry_s *end = ie; |
| 185 | for (;;) { |
| 186 | struct ipl_entry_s *prev = ie - 1; |
| 187 | if (prev < IPL.bcv || prev->type != BCV_TYPE_INTERNAL) |
| 188 | break; |
| 189 | struct drive_s *prevdrive = (void*)prev->vector; |
| 190 | if (prevdrive->type < drive_g->type |
| 191 | || (prevdrive->type == drive_g->type |
| 192 | && prevdrive->cntl_id < drive_g->cntl_id)) |
| 193 | break; |
| 194 | ie--; |
| 195 | } |
| 196 | if (ie != end) |
| 197 | memmove(ie+1, ie, (void*)end-(void*)ie); |
| 198 | } |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 199 | ie->type = BCV_TYPE_INTERNAL; |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 200 | ie->vector = (u32)drive_g; |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 201 | ie->description = ""; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | /**************************************************************** |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 206 | * Boot menu and BCV execution |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 207 | ****************************************************************/ |
| 208 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 209 | // Show a generic menu item |
| 210 | static int |
| 211 | menu_show_default(struct ipl_entry_s *ie, int menupos) |
| 212 | { |
| 213 | char desc[33]; |
| 214 | printf("%d. %s\n", menupos |
| 215 | , strtcpy(desc, ie->description, ARRAY_SIZE(desc))); |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | // Show floppy menu item - but only if there exists a floppy drive. |
| 220 | static int |
| 221 | menu_show_floppy(struct ipl_entry_s *ie, int menupos) |
| 222 | { |
Kevin O'Connor | afd0520 | 2009-08-16 12:21:44 -0400 | [diff] [blame] | 223 | int i; |
| 224 | for (i = 0; i < Drives.floppycount; i++) { |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 225 | struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, i); |
Kevin O'Connor | 575ffc8 | 2010-02-21 23:20:10 -0500 | [diff] [blame] | 226 | printf("%d. Floppy [%s]\n", menupos + i, drive_g->desc); |
Kevin O'Connor | afd0520 | 2009-08-16 12:21:44 -0400 | [diff] [blame] | 227 | } |
| 228 | return Drives.floppycount; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Show menu items from BCV list. |
| 232 | static int |
| 233 | menu_show_harddisk(struct ipl_entry_s *ie, int menupos) |
| 234 | { |
| 235 | int i; |
| 236 | for (i = 0; i < IPL.bcvcount; i++) { |
| 237 | struct ipl_entry_s *ie = &IPL.bcv[i]; |
Kevin O'Connor | 575ffc8 | 2010-02-21 23:20:10 -0500 | [diff] [blame] | 238 | struct drive_s *drive_g = (void*)ie->vector; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 239 | switch (ie->type) { |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 240 | case BCV_TYPE_INTERNAL: |
Kevin O'Connor | 575ffc8 | 2010-02-21 23:20:10 -0500 | [diff] [blame] | 241 | printf("%d. %s\n", menupos + i, drive_g->desc); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 242 | break; |
| 243 | default: |
| 244 | menu_show_default(ie, menupos+i); |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | return IPL.bcvcount; |
| 249 | } |
| 250 | |
| 251 | // Show cdrom menu item - but only if there exists a cdrom drive. |
| 252 | static int |
| 253 | menu_show_cdrom(struct ipl_entry_s *ie, int menupos) |
| 254 | { |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 255 | struct drive_s *drive_g = (void*)ie->vector; |
| 256 | if (!ie->vector) |
| 257 | return 0; |
| 258 | printf("%d. DVD/CD [%s]\n", menupos, drive_g->desc); |
| 259 | return 1; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 260 | } |
| 261 | |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 262 | // Show coreboot-fs menu item. |
| 263 | static int |
| 264 | menu_show_cbfs(struct ipl_entry_s *ie, int menupos) |
| 265 | { |
| 266 | int count = 0; |
Kevin O'Connor | 31ae638 | 2009-09-20 15:42:39 -0400 | [diff] [blame] | 267 | struct cbfs_file *file = NULL; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 268 | for (;;) { |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 269 | file = cbfs_findprefix("img/", file); |
| 270 | if (!file) |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 271 | break; |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 272 | const char *filename = cbfs_filename(file); |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 273 | printf("%d. Payload [%s]\n", menupos + count, &filename[4]); |
| 274 | count++; |
| 275 | if (count > 8) |
| 276 | break; |
| 277 | } |
| 278 | return count; |
| 279 | } |
| 280 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 281 | // Show IPL option menu. |
| 282 | static void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 283 | interactive_bootmenu(void) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 284 | { |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 285 | if (! CONFIG_BOOTMENU || ! qemu_cfg_show_boot_menu()) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 286 | return; |
| 287 | |
| 288 | while (get_keystroke(0) >= 0) |
| 289 | ; |
| 290 | |
| 291 | printf("Press F12 for boot menu.\n\n"); |
| 292 | |
Kevin O'Connor | 9a01a9c | 2010-08-25 21:07:48 -0400 | [diff] [blame] | 293 | enable_bootsplash(); |
Kevin O'Connor | 217d363 | 2009-04-29 22:00:21 -0400 | [diff] [blame] | 294 | int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT); |
Kevin O'Connor | 9a01a9c | 2010-08-25 21:07:48 -0400 | [diff] [blame] | 295 | disable_bootsplash(); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 296 | if (scan_code != 0x86) |
| 297 | /* not F12 */ |
| 298 | return; |
| 299 | |
| 300 | while (get_keystroke(0) >= 0) |
| 301 | ; |
| 302 | |
| 303 | printf("Select boot device:\n\n"); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 304 | wait_threads(); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 305 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 306 | int subcount[ARRAY_SIZE(IPL.bev)]; |
| 307 | int menupos = 1; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 308 | int i; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 309 | for (i = 0; i < IPL.bevcount; i++) { |
| 310 | struct ipl_entry_s *ie = &IPL.bev[i]; |
Kevin O'Connor | 31ae638 | 2009-09-20 15:42:39 -0400 | [diff] [blame] | 311 | int sc; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 312 | switch (ie->type) { |
| 313 | case IPL_TYPE_FLOPPY: |
| 314 | sc = menu_show_floppy(ie, menupos); |
| 315 | break; |
| 316 | case IPL_TYPE_HARDDISK: |
| 317 | sc = menu_show_harddisk(ie, menupos); |
| 318 | break; |
| 319 | case IPL_TYPE_CDROM: |
| 320 | sc = menu_show_cdrom(ie, menupos); |
| 321 | break; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 322 | case IPL_TYPE_CBFS: |
| 323 | sc = menu_show_cbfs(ie, menupos); |
| 324 | break; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 325 | default: |
| 326 | sc = menu_show_default(ie, menupos); |
| 327 | break; |
| 328 | } |
| 329 | subcount[i] = sc; |
| 330 | menupos += sc; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | for (;;) { |
| 334 | scan_code = get_keystroke(1000); |
| 335 | if (scan_code == 0x01) |
| 336 | // ESC |
| 337 | break; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 338 | if (scan_code < 1 || scan_code > menupos) |
| 339 | continue; |
| 340 | int choice = scan_code - 1; |
| 341 | |
| 342 | // Find out which IPL this was for. |
| 343 | int bev = 0; |
| 344 | while (choice > subcount[bev]) { |
| 345 | choice -= subcount[bev]; |
| 346 | bev++; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 347 | } |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 348 | IPL.bev[bev].subchoice = choice-1; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 349 | |
| 350 | // Add user choice to the boot order. |
| 351 | IPL.bootorder = (IPL.bootorder << 4) | (bev+1); |
| 352 | break; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 353 | } |
| 354 | printf("\n"); |
| 355 | } |
| 356 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 357 | // Run the specified bcv. |
| 358 | static void |
| 359 | run_bcv(struct ipl_entry_s *ie) |
| 360 | { |
| 361 | switch (ie->type) { |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 362 | case BCV_TYPE_INTERNAL: |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 363 | map_hd_drive((void*)ie->vector); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 364 | break; |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 365 | case BCV_TYPE_EXTERNAL: |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 366 | call_bcv(ie->vector >> 16, ie->vector & 0xffff); |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // Prepare for boot - show menu and run bcvs. |
| 372 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 373 | boot_prep(void) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 374 | { |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 375 | if (! CONFIG_BOOT) { |
| 376 | wait_threads(); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 377 | return; |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 378 | } |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 379 | |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 380 | // XXX - show available drives? |
| 381 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 382 | // Allow user to modify BCV/IPL order. |
| 383 | interactive_bootmenu(); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 384 | wait_threads(); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 385 | |
Kevin O'Connor | afd0520 | 2009-08-16 12:21:44 -0400 | [diff] [blame] | 386 | // Setup floppy boot order |
| 387 | int override = IPL.bev[0].subchoice; |
Kevin O'Connor | d7e998f | 2010-02-15 22:48:28 -0500 | [diff] [blame] | 388 | struct drive_s *tmp = Drives.idmap[EXTTYPE_FLOPPY][0]; |
Kevin O'Connor | afd0520 | 2009-08-16 12:21:44 -0400 | [diff] [blame] | 389 | Drives.idmap[EXTTYPE_FLOPPY][0] = Drives.idmap[EXTTYPE_FLOPPY][override]; |
| 390 | Drives.idmap[EXTTYPE_FLOPPY][override] = tmp; |
| 391 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 392 | // Run BCVs |
Kevin O'Connor | afd0520 | 2009-08-16 12:21:44 -0400 | [diff] [blame] | 393 | override = IPL.bev[1].subchoice; |
Kevin O'Connor | 7da1dcd | 2009-02-16 10:51:24 -0500 | [diff] [blame] | 394 | if (override < IPL.bcvcount) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 395 | run_bcv(&IPL.bcv[override]); |
| 396 | int i; |
| 397 | for (i=0; i<IPL.bcvcount; i++) |
| 398 | if (i != override) |
| 399 | run_bcv(&IPL.bcv[i]); |
| 400 | } |
| 401 | |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 402 | |
| 403 | /**************************************************************** |
| 404 | * Boot code (int 18/19) |
| 405 | ****************************************************************/ |
| 406 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 407 | // Jump to a bootup entry point. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 408 | static void |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 409 | call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv) |
| 410 | { |
Kevin O'Connor | cadaf0e | 2010-07-26 23:47:26 -0400 | [diff] [blame] | 411 | dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 412 | struct bregs br; |
| 413 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 414 | br.flags = F_IF; |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 415 | br.code = SEGOFF(bootseg, bootip); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 416 | // Set the magic number in ax and the boot drive in dl. |
| 417 | br.dl = bootdrv; |
| 418 | br.ax = 0xaa55; |
| 419 | call16(&br); |
| 420 | } |
| 421 | |
| 422 | // Boot from a disk (either floppy or harddrive) |
| 423 | static void |
| 424 | boot_disk(u8 bootdrv, int checksig) |
| 425 | { |
| 426 | u16 bootseg = 0x07c0; |
| 427 | |
| 428 | // Read sector |
| 429 | struct bregs br; |
| 430 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 431 | br.flags = F_IF; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 432 | br.dl = bootdrv; |
| 433 | br.es = bootseg; |
| 434 | br.ah = 2; |
| 435 | br.al = 1; |
| 436 | br.cl = 1; |
| 437 | call16_int(0x13, &br); |
| 438 | |
| 439 | if (br.flags & F_CF) { |
| 440 | printf("Boot failed: could not read the boot disk\n\n"); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | if (checksig) { |
| 445 | struct mbr_s *mbr = (void*)0; |
| 446 | if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) { |
| 447 | printf("Boot failed: not a bootable disk\n\n"); |
| 448 | return; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /* Canonicalize bootseg:bootip */ |
| 453 | u16 bootip = (bootseg & 0x0fff) << 4; |
| 454 | bootseg &= 0xf000; |
| 455 | |
| 456 | call_boot_entry(bootseg, bootip, bootdrv); |
| 457 | } |
| 458 | |
| 459 | // Boot from a CD-ROM |
| 460 | static void |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 461 | boot_cdrom(struct ipl_entry_s *ie) |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 462 | { |
| 463 | if (! CONFIG_CDROM_BOOT) |
| 464 | return; |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 465 | |
| 466 | if (!ie->vector) |
| 467 | return; |
| 468 | |
| 469 | struct drive_s *drive_g = (void*)ie->vector; |
| 470 | int status = cdrom_boot(drive_g); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 471 | if (status) { |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 472 | printf("Boot failed: Could not read from CDROM %s (code %04x)\n", drive_g->desc, status); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
| 476 | u16 ebda_seg = get_ebda_seg(); |
Kevin O'Connor | 669e644 | 2009-08-11 22:36:30 -0400 | [diff] [blame] | 477 | u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 478 | u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment); |
| 479 | /* Canonicalize bootseg:bootip */ |
| 480 | u16 bootip = (bootseg & 0x0fff) << 4; |
| 481 | bootseg &= 0xf000; |
| 482 | |
| 483 | call_boot_entry(bootseg, bootip, bootdrv); |
| 484 | } |
| 485 | |
Kevin O'Connor | e8f00ee | 2009-07-04 04:04:36 -0400 | [diff] [blame] | 486 | // Boot from a CBFS payload |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 487 | static void |
| 488 | boot_cbfs(struct ipl_entry_s *ie) |
| 489 | { |
Kevin O'Connor | d9c9361 | 2010-03-20 11:00:45 -0400 | [diff] [blame] | 490 | if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH) |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 491 | return; |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 492 | int count = ie->subchoice; |
Kevin O'Connor | 2edace1 | 2009-12-13 11:19:01 -0500 | [diff] [blame] | 493 | struct cbfs_file *file = NULL; |
Kevin O'Connor | 1f83625 | 2009-08-16 20:17:35 -0400 | [diff] [blame] | 494 | for (;;) { |
| 495 | file = cbfs_findprefix("img/", file); |
| 496 | if (!file) |
| 497 | return; |
| 498 | if (count--) |
| 499 | continue; |
| 500 | cbfs_run_payload(file); |
| 501 | } |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 502 | } |
| 503 | |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 504 | static void |
| 505 | do_boot(u16 seq_nr) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 506 | { |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 507 | if (! CONFIG_BOOT) |
Kevin O'Connor | e07e18e | 2009-02-08 17:07:29 -0500 | [diff] [blame] | 508 | panic("Boot support not compiled in.\n"); |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 509 | |
Kevin O'Connor | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 510 | u32 bootdev = IPL.bootorder; |
Kevin O'Connor | 840c534 | 2008-03-29 14:04:34 -0400 | [diff] [blame] | 511 | bootdev >>= 4 * seq_nr; |
| 512 | bootdev &= 0xf; |
Kevin O'Connor | e0113c9 | 2008-04-05 15:51:12 -0400 | [diff] [blame] | 513 | |
Kevin O'Connor | abc7597 | 2008-05-18 00:20:53 -0400 | [diff] [blame] | 514 | /* Translate bootdev to an IPL table offset by subtracting 1 */ |
Kevin O'Connor | 840c534 | 2008-03-29 14:04:34 -0400 | [diff] [blame] | 515 | bootdev -= 1; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 516 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 517 | if (bootdev >= IPL.bevcount) { |
Kevin O'Connor | ff8c141 | 2009-04-18 17:02:41 -0400 | [diff] [blame] | 518 | printf("No bootable device.\n"); |
| 519 | // Loop with irqs enabled - this allows ctrl+alt+delete to work. |
| 520 | for (;;) |
Kevin O'Connor | 9c447c3 | 2010-05-23 10:24:22 -0400 | [diff] [blame] | 521 | wait_irq(); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 522 | } |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 523 | |
| 524 | /* Do the loading, and set up vector as a far pointer to the boot |
| 525 | * address, and bootdrv as the boot drive */ |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 526 | struct ipl_entry_s *ie = &IPL.bev[bootdev]; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 527 | char desc[33]; |
| 528 | printf("Booting from %s...\n" |
| 529 | , strtcpy(desc, ie->description, ARRAY_SIZE(desc))); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 530 | |
Kevin O'Connor | cadaf0e | 2010-07-26 23:47:26 -0400 | [diff] [blame] | 531 | switch (ie->type) { |
Kevin O'Connor | f13b008 | 2008-08-17 11:26:42 -0400 | [diff] [blame] | 532 | case IPL_TYPE_FLOPPY: |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 533 | boot_disk(0x00, IPL.checkfloppysig); |
| 534 | break; |
Kevin O'Connor | f13b008 | 2008-08-17 11:26:42 -0400 | [diff] [blame] | 535 | case IPL_TYPE_HARDDISK: |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 536 | boot_disk(0x80, 1); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 537 | break; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 538 | case IPL_TYPE_CDROM: |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 539 | boot_cdrom(ie); |
| 540 | break; |
| 541 | case IPL_TYPE_CBFS: |
| 542 | boot_cbfs(ie); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 543 | break; |
| 544 | case IPL_TYPE_BEV: |
| 545 | call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 546 | break; |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 547 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 548 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 549 | // Boot failed: invoke the boot recovery function |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 550 | struct bregs br; |
| 551 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | f8e800d | 2009-09-24 21:01:16 -0400 | [diff] [blame] | 552 | br.flags = F_IF; |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 553 | call16_int(0x18, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // Boot Failure recovery: try the next device. |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 557 | void VISIBLE32FLAT |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 558 | handle_18(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 559 | { |
Kevin O'Connor | 61d6b06 | 2008-06-21 12:15:10 -0400 | [diff] [blame] | 560 | debug_serial_setup(); |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 561 | debug_enter(NULL, DEBUG_HDL_18); |
Kevin O'Connor | 0881537 | 2008-12-29 21:16:31 -0500 | [diff] [blame] | 562 | u16 ebda_seg = get_ebda_seg(); |
| 563 | u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1; |
| 564 | SET_EBDA2(ebda_seg, boot_sequence, seq); |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 565 | do_boot(seq); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // INT 19h Boot Load Service Entry Point |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 569 | void VISIBLE32FLAT |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 570 | handle_19(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 571 | { |
Kevin O'Connor | 61d6b06 | 2008-06-21 12:15:10 -0400 | [diff] [blame] | 572 | debug_serial_setup(); |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 573 | debug_enter(NULL, DEBUG_HDL_19); |
Kevin O'Connor | 0881537 | 2008-12-29 21:16:31 -0500 | [diff] [blame] | 574 | SET_EBDA(boot_sequence, 0); |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 575 | do_boot(0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 576 | } |