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 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 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 | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 8 | #include "util.h" // irq_enable |
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 | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 15 | |
| 16 | struct ipl_s IPL; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 17 | |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 18 | |
| 19 | /**************************************************************** |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 20 | * IPL and BCV handlers |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 21 | ****************************************************************/ |
| 22 | |
| 23 | void |
| 24 | boot_setup() |
| 25 | { |
| 26 | if (! CONFIG_BOOT) |
| 27 | return; |
| 28 | dprintf(3, "init boot device ordering\n"); |
| 29 | |
| 30 | memset(&IPL, 0, sizeof(IPL)); |
| 31 | |
| 32 | // Floppy drive |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 33 | struct ipl_entry_s *ie = &IPL.bev[0]; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 34 | ie->type = IPL_TYPE_FLOPPY; |
| 35 | ie->description = "Floppy"; |
| 36 | ie++; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 37 | |
| 38 | // First HDD |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 39 | ie->type = IPL_TYPE_HARDDISK; |
| 40 | ie->description = "Hard Disk"; |
| 41 | ie++; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 42 | |
| 43 | // CDROM |
| 44 | if (CONFIG_CDROM_BOOT) { |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 45 | ie->type = IPL_TYPE_CDROM; |
| 46 | ie->description = "CD-Rom"; |
| 47 | ie++; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 50 | if (CONFIG_COREBOOT_FLASH) { |
| 51 | ie->type = IPL_TYPE_CBFS; |
| 52 | ie->description = "CBFS"; |
| 53 | ie++; |
| 54 | } |
| 55 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 56 | IPL.bevcount = ie - IPL.bev; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 57 | SET_EBDA(boot_sequence, 0xffff); |
| 58 | if (CONFIG_COREBOOT) { |
| 59 | // XXX - hardcode defaults for coreboot. |
Kevin O'Connor | ff8c141 | 2009-04-18 17:02:41 -0400 | [diff] [blame] | 60 | IPL.bootorder = 0x87654231; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 61 | IPL.checkfloppysig = 1; |
| 62 | } else { |
| 63 | // On emulators, get boot order from nvram. |
| 64 | IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2) |
| 65 | | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4)); |
| 66 | if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)) |
| 67 | IPL.checkfloppysig = 1; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Add a BEV vector for a given pnp compatible option rom. |
| 72 | void |
| 73 | add_bev(u16 seg, u16 bev, u16 desc) |
| 74 | { |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 75 | if (! CONFIG_BOOT) |
| 76 | return; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 77 | if (IPL.bevcount >= ARRAY_SIZE(IPL.bev)) |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 78 | return; |
| 79 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 80 | struct ipl_entry_s *ie = &IPL.bev[IPL.bevcount++]; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 81 | ie->type = IPL_TYPE_BEV; |
| 82 | ie->vector = (seg << 16) | bev; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 83 | const char *d = "Unknown"; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 84 | if (desc) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 85 | d = MAKE_FLATPTR(seg, desc); |
| 86 | ie->description = d; |
| 87 | } |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 88 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 89 | // Add a bcv entry for an expansion card harddrive or legacy option rom |
| 90 | void |
| 91 | add_bcv(u16 seg, u16 ip, u16 desc) |
| 92 | { |
| 93 | if (! CONFIG_BOOT) |
| 94 | return; |
| 95 | if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv)) |
| 96 | return; |
| 97 | |
| 98 | struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++]; |
| 99 | ie->type = IPL_TYPE_BEV; |
| 100 | ie->vector = (seg << 16) | ip; |
| 101 | const char *d = "Legacy option rom"; |
| 102 | if (desc) |
| 103 | d = MAKE_FLATPTR(seg, desc); |
| 104 | ie->description = d; |
| 105 | } |
| 106 | |
| 107 | // Add a bcv entry for an ata harddrive |
| 108 | void |
| 109 | add_bcv_hd(int driveid, const char *desc) |
| 110 | { |
| 111 | if (! CONFIG_BOOT) |
| 112 | return; |
| 113 | if (IPL.bcvcount >= ARRAY_SIZE(IPL.bcv)) |
| 114 | return; |
| 115 | |
| 116 | struct ipl_entry_s *ie = &IPL.bcv[IPL.bcvcount++]; |
| 117 | ie->type = IPL_TYPE_HARDDISK; |
| 118 | ie->vector = driveid; |
| 119 | ie->description = desc; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | /**************************************************************** |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 124 | * Boot menu and BCV execution |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 125 | ****************************************************************/ |
| 126 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 127 | // Show a generic menu item |
| 128 | static int |
| 129 | menu_show_default(struct ipl_entry_s *ie, int menupos) |
| 130 | { |
| 131 | char desc[33]; |
| 132 | printf("%d. %s\n", menupos |
| 133 | , strtcpy(desc, ie->description, ARRAY_SIZE(desc))); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | // Show floppy menu item - but only if there exists a floppy drive. |
| 138 | static int |
| 139 | menu_show_floppy(struct ipl_entry_s *ie, int menupos) |
| 140 | { |
| 141 | if (!FloppyCount) |
| 142 | return 0; |
| 143 | return menu_show_default(ie, menupos); |
| 144 | } |
| 145 | |
| 146 | // Show menu items from BCV list. |
| 147 | static int |
| 148 | menu_show_harddisk(struct ipl_entry_s *ie, int menupos) |
| 149 | { |
| 150 | int i; |
| 151 | for (i = 0; i < IPL.bcvcount; i++) { |
| 152 | struct ipl_entry_s *ie = &IPL.bcv[i]; |
| 153 | switch (ie->type) { |
| 154 | case IPL_TYPE_HARDDISK: |
| 155 | printf("%d. ata%d-%d %s\n", menupos + i |
Kevin O'Connor | 7da1dcd | 2009-02-16 10:51:24 -0500 | [diff] [blame] | 156 | , ie->vector / 2, ie->vector % 2, ie->description); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 157 | break; |
| 158 | default: |
| 159 | menu_show_default(ie, menupos+i); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | return IPL.bcvcount; |
| 164 | } |
| 165 | |
| 166 | // Show cdrom menu item - but only if there exists a cdrom drive. |
| 167 | static int |
| 168 | menu_show_cdrom(struct ipl_entry_s *ie, int menupos) |
| 169 | { |
Kevin O'Connor | 7da1dcd | 2009-02-16 10:51:24 -0500 | [diff] [blame] | 170 | int i; |
| 171 | for (i = 0; i < ATA.cdcount; i++) { |
| 172 | int driveid = ATA.idmap[1][i]; |
| 173 | printf("%d. CD-Rom [ata%d-%d %s]\n", menupos + i |
| 174 | , driveid / 2, driveid % 2, ATA.devices[driveid].model); |
| 175 | } |
| 176 | return ATA.cdcount; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 179 | // Show coreboot-fs menu item. |
| 180 | static int |
| 181 | menu_show_cbfs(struct ipl_entry_s *ie, int menupos) |
| 182 | { |
| 183 | int count = 0; |
| 184 | for (;;) { |
| 185 | const char *filename = cbfs_findNprefix("img/", count); |
| 186 | if (!filename) |
| 187 | break; |
| 188 | printf("%d. Payload [%s]\n", menupos + count, &filename[4]); |
| 189 | count++; |
| 190 | if (count > 8) |
| 191 | break; |
| 192 | } |
| 193 | return count; |
| 194 | } |
| 195 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 196 | // Show IPL option menu. |
| 197 | static void |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 198 | interactive_bootmenu() |
| 199 | { |
| 200 | if (! CONFIG_BOOTMENU) |
| 201 | return; |
| 202 | |
| 203 | while (get_keystroke(0) >= 0) |
| 204 | ; |
| 205 | |
| 206 | printf("Press F12 for boot menu.\n\n"); |
| 207 | |
Kevin O'Connor | 217d363 | 2009-04-29 22:00:21 -0400 | [diff] [blame] | 208 | int scan_code = get_keystroke(CONFIG_BOOTMENU_WAIT); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 209 | if (scan_code != 0x86) |
| 210 | /* not F12 */ |
| 211 | return; |
| 212 | |
| 213 | while (get_keystroke(0) >= 0) |
| 214 | ; |
| 215 | |
| 216 | printf("Select boot device:\n\n"); |
| 217 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 218 | int subcount[ARRAY_SIZE(IPL.bev)]; |
| 219 | int menupos = 1; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 220 | int i; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 221 | for (i = 0; i < IPL.bevcount; i++) { |
| 222 | struct ipl_entry_s *ie = &IPL.bev[i]; |
| 223 | int sc = 1; |
| 224 | switch (ie->type) { |
| 225 | case IPL_TYPE_FLOPPY: |
| 226 | sc = menu_show_floppy(ie, menupos); |
| 227 | break; |
| 228 | case IPL_TYPE_HARDDISK: |
| 229 | sc = menu_show_harddisk(ie, menupos); |
| 230 | break; |
| 231 | case IPL_TYPE_CDROM: |
| 232 | sc = menu_show_cdrom(ie, menupos); |
| 233 | break; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 234 | case IPL_TYPE_CBFS: |
| 235 | sc = menu_show_cbfs(ie, menupos); |
| 236 | break; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 237 | default: |
| 238 | sc = menu_show_default(ie, menupos); |
| 239 | break; |
| 240 | } |
| 241 | subcount[i] = sc; |
| 242 | menupos += sc; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | for (;;) { |
| 246 | scan_code = get_keystroke(1000); |
| 247 | if (scan_code == 0x01) |
| 248 | // ESC |
| 249 | break; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 250 | if (scan_code < 1 || scan_code > menupos) |
| 251 | continue; |
| 252 | int choice = scan_code - 1; |
| 253 | |
| 254 | // Find out which IPL this was for. |
| 255 | int bev = 0; |
| 256 | while (choice > subcount[bev]) { |
| 257 | choice -= subcount[bev]; |
| 258 | bev++; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 259 | } |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 260 | IPL.bev[bev].subchoice = choice-1; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 261 | |
| 262 | // Add user choice to the boot order. |
| 263 | IPL.bootorder = (IPL.bootorder << 4) | (bev+1); |
| 264 | break; |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 265 | } |
| 266 | printf("\n"); |
| 267 | } |
| 268 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 269 | // Run the specified bcv. |
| 270 | static void |
| 271 | run_bcv(struct ipl_entry_s *ie) |
| 272 | { |
| 273 | switch (ie->type) { |
| 274 | case IPL_TYPE_HARDDISK: |
| 275 | map_drive(ie->vector); |
| 276 | break; |
| 277 | case IPL_TYPE_BEV: |
| 278 | call_bcv(ie->vector >> 16, ie->vector & 0xffff); |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Prepare for boot - show menu and run bcvs. |
| 284 | void |
| 285 | boot_prep() |
| 286 | { |
| 287 | if (! CONFIG_BOOT) |
| 288 | return; |
| 289 | |
| 290 | // Allow user to modify BCV/IPL order. |
| 291 | interactive_bootmenu(); |
| 292 | |
| 293 | // Run BCVs |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 294 | int override = IPL.bev[1].subchoice; |
Kevin O'Connor | 7da1dcd | 2009-02-16 10:51:24 -0500 | [diff] [blame] | 295 | if (override < IPL.bcvcount) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 296 | run_bcv(&IPL.bcv[override]); |
| 297 | int i; |
| 298 | for (i=0; i<IPL.bcvcount; i++) |
| 299 | if (i != override) |
| 300 | run_bcv(&IPL.bcv[i]); |
| 301 | } |
| 302 | |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 303 | |
| 304 | /**************************************************************** |
| 305 | * Boot code (int 18/19) |
| 306 | ****************************************************************/ |
| 307 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 308 | // Jump to a bootup entry point. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 309 | static void |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 310 | call_boot_entry(u16 bootseg, u16 bootip, u8 bootdrv) |
| 311 | { |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 312 | dprintf(1, "Booting from %04x:%04x\n", bootseg, bootip); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 313 | |
| 314 | struct bregs br; |
| 315 | memset(&br, 0, sizeof(br)); |
| 316 | br.ip = bootip; |
| 317 | br.cs = bootseg; |
| 318 | // Set the magic number in ax and the boot drive in dl. |
| 319 | br.dl = bootdrv; |
| 320 | br.ax = 0xaa55; |
| 321 | call16(&br); |
| 322 | } |
| 323 | |
| 324 | // Boot from a disk (either floppy or harddrive) |
| 325 | static void |
| 326 | boot_disk(u8 bootdrv, int checksig) |
| 327 | { |
| 328 | u16 bootseg = 0x07c0; |
| 329 | |
| 330 | // Read sector |
| 331 | struct bregs br; |
| 332 | memset(&br, 0, sizeof(br)); |
| 333 | br.dl = bootdrv; |
| 334 | br.es = bootseg; |
| 335 | br.ah = 2; |
| 336 | br.al = 1; |
| 337 | br.cl = 1; |
| 338 | call16_int(0x13, &br); |
| 339 | |
| 340 | if (br.flags & F_CF) { |
| 341 | printf("Boot failed: could not read the boot disk\n\n"); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (checksig) { |
| 346 | struct mbr_s *mbr = (void*)0; |
| 347 | if (GET_FARVAR(bootseg, mbr->signature) != MBR_SIGNATURE) { |
| 348 | printf("Boot failed: not a bootable disk\n\n"); |
| 349 | return; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* Canonicalize bootseg:bootip */ |
| 354 | u16 bootip = (bootseg & 0x0fff) << 4; |
| 355 | bootseg &= 0xf000; |
| 356 | |
| 357 | call_boot_entry(bootseg, bootip, bootdrv); |
| 358 | } |
| 359 | |
| 360 | // Boot from a CD-ROM |
| 361 | static void |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 362 | boot_cdrom(struct ipl_entry_s *ie) |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 363 | { |
| 364 | if (! CONFIG_CDROM_BOOT) |
| 365 | return; |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 366 | int status = cdrom_boot(ie->subchoice); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 367 | if (status) { |
| 368 | printf("Boot failed: Could not read from CDROM (code %04x)\n", status); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | u16 ebda_seg = get_ebda_seg(); |
| 373 | u8 bootdrv = GET_EBDA2(ebda_seg, cdemu.emulated_drive); |
| 374 | u16 bootseg = GET_EBDA2(ebda_seg, cdemu.load_segment); |
| 375 | /* Canonicalize bootseg:bootip */ |
| 376 | u16 bootip = (bootseg & 0x0fff) << 4; |
| 377 | bootseg &= 0xf000; |
| 378 | |
| 379 | call_boot_entry(bootseg, bootip, bootdrv); |
| 380 | } |
| 381 | |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 382 | // Boot from a CD-ROM |
| 383 | static void |
| 384 | boot_cbfs(struct ipl_entry_s *ie) |
| 385 | { |
| 386 | if (! CONFIG_COREBOOT_FLASH) |
| 387 | return; |
| 388 | const char *filename = cbfs_findNprefix("img/", ie->subchoice); |
| 389 | if (! filename) |
| 390 | return; |
| 391 | cbfs_run_payload(filename); |
| 392 | } |
| 393 | |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 394 | static void |
| 395 | do_boot(u16 seq_nr) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 396 | { |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 397 | if (! CONFIG_BOOT) |
Kevin O'Connor | e07e18e | 2009-02-08 17:07:29 -0500 | [diff] [blame] | 398 | panic("Boot support not compiled in.\n"); |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 399 | |
Kevin O'Connor | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 400 | u32 bootdev = IPL.bootorder; |
Kevin O'Connor | 840c534 | 2008-03-29 14:04:34 -0400 | [diff] [blame] | 401 | bootdev >>= 4 * seq_nr; |
| 402 | bootdev &= 0xf; |
Kevin O'Connor | e0113c9 | 2008-04-05 15:51:12 -0400 | [diff] [blame] | 403 | |
Kevin O'Connor | abc7597 | 2008-05-18 00:20:53 -0400 | [diff] [blame] | 404 | /* Translate bootdev to an IPL table offset by subtracting 1 */ |
Kevin O'Connor | 840c534 | 2008-03-29 14:04:34 -0400 | [diff] [blame] | 405 | bootdev -= 1; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 406 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 407 | if (bootdev >= IPL.bevcount) { |
Kevin O'Connor | ff8c141 | 2009-04-18 17:02:41 -0400 | [diff] [blame] | 408 | printf("No bootable device.\n"); |
| 409 | // Loop with irqs enabled - this allows ctrl+alt+delete to work. |
| 410 | for (;;) |
| 411 | usleep(1000000); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 412 | } |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 413 | |
| 414 | /* Do the loading, and set up vector as a far pointer to the boot |
| 415 | * address, and bootdrv as the boot drive */ |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 416 | struct ipl_entry_s *ie = &IPL.bev[bootdev]; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 417 | char desc[33]; |
| 418 | printf("Booting from %s...\n" |
| 419 | , strtcpy(desc, ie->description, ARRAY_SIZE(desc))); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 420 | |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 421 | switch(ie->type) { |
Kevin O'Connor | f13b008 | 2008-08-17 11:26:42 -0400 | [diff] [blame] | 422 | case IPL_TYPE_FLOPPY: |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 423 | boot_disk(0x00, IPL.checkfloppysig); |
| 424 | break; |
Kevin O'Connor | f13b008 | 2008-08-17 11:26:42 -0400 | [diff] [blame] | 425 | case IPL_TYPE_HARDDISK: |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 426 | boot_disk(0x80, 1); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 427 | break; |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 428 | case IPL_TYPE_CDROM: |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 429 | boot_cdrom(ie); |
| 430 | break; |
| 431 | case IPL_TYPE_CBFS: |
| 432 | boot_cbfs(ie); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 433 | break; |
| 434 | case IPL_TYPE_BEV: |
| 435 | call_boot_entry(ie->vector >> 16, ie->vector & 0xffff, 0); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 436 | break; |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 437 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 438 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 439 | // Boot failed: invoke the boot recovery function |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 440 | struct bregs br; |
| 441 | memset(&br, 0, sizeof(br)); |
| 442 | call16_int(0x18, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Boot Failure recovery: try the next device. |
Kevin O'Connor | 44eeaf1 | 2008-07-06 10:14:49 -0400 | [diff] [blame] | 446 | void VISIBLE32 |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 447 | handle_18() |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 448 | { |
Kevin O'Connor | 61d6b06 | 2008-06-21 12:15:10 -0400 | [diff] [blame] | 449 | debug_serial_setup(); |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 450 | debug_enter(NULL, DEBUG_HDL_18); |
Kevin O'Connor | 0881537 | 2008-12-29 21:16:31 -0500 | [diff] [blame] | 451 | u16 ebda_seg = get_ebda_seg(); |
| 452 | u16 seq = GET_EBDA2(ebda_seg, boot_sequence) + 1; |
| 453 | SET_EBDA2(ebda_seg, boot_sequence, seq); |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 454 | do_boot(seq); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // INT 19h Boot Load Service Entry Point |
Kevin O'Connor | 44eeaf1 | 2008-07-06 10:14:49 -0400 | [diff] [blame] | 458 | void VISIBLE32 |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 459 | handle_19() |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 460 | { |
Kevin O'Connor | 61d6b06 | 2008-06-21 12:15:10 -0400 | [diff] [blame] | 461 | debug_serial_setup(); |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 462 | debug_enter(NULL, DEBUG_HDL_19); |
Kevin O'Connor | 0881537 | 2008-12-29 21:16:31 -0500 | [diff] [blame] | 463 | SET_EBDA(boot_sequence, 0); |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 464 | do_boot(0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 465 | } |
Kevin O'Connor | cdd3d65 | 2008-07-12 14:22:14 -0400 | [diff] [blame] | 466 | |
| 467 | // Ughh - some older gcc compilers have a bug which causes VISIBLE32 |
Kevin O'Connor | f13b008 | 2008-08-17 11:26:42 -0400 | [diff] [blame] | 468 | // functions to not be exported as global variables. |
Kevin O'Connor | cdd3d65 | 2008-07-12 14:22:14 -0400 | [diff] [blame] | 469 | asm(".global handle_18, handle_19"); |