Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // 16bit code to load disk image and start system boot. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
| 6 | // This file may be distributed under the terms of the GNU GPLv3 license. |
| 7 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 8 | #include "util.h" // irq_enable |
| 9 | #include "biosvar.h" // struct bregs |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 10 | #include "config.h" // CONFIG_* |
| 11 | #include "cmos.h" // inb_cmos |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 12 | #include "ata.h" // ata_detect |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 13 | #include "disk.h" // cdrom_boot |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 14 | |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 15 | // We need a copy of this string, but we are not actually a PnP BIOS, |
| 16 | // so make sure it is *not* aligned, so OSes will not see it if they |
| 17 | // scan. |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 18 | char pnp_string[] VISIBLE16 __attribute__((aligned (2))) = " $PnP"; |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 19 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 20 | //-------------------------------------------------------------------------- |
| 21 | // print_boot_device |
| 22 | // displays the boot device |
| 23 | //-------------------------------------------------------------------------- |
| 24 | |
| 25 | static const char drivetypes[][10]={ |
| 26 | "", "Floppy","Hard Disk","CD-Rom", "Network" |
| 27 | }; |
| 28 | |
| 29 | static void |
| 30 | print_boot_device(u16 type) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 31 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 32 | /* NIC appears as type 0x80 */ |
| 33 | if (type == IPL_TYPE_BEV) |
| 34 | type = 0x4; |
| 35 | if (type == 0 || type > 0x4) |
| 36 | BX_PANIC("Bad drive type\n"); |
| 37 | printf("Booting from %s...\n", drivetypes[type]); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 38 | |
| 39 | // XXX - latest cvs has BEV description |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 42 | //-------------------------------------------------------------------------- |
| 43 | // print_boot_failure |
| 44 | // displays the reason why boot failed |
| 45 | //-------------------------------------------------------------------------- |
| 46 | static void |
| 47 | print_boot_failure(u16 type, u8 reason) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 48 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 49 | if (type == 0 || type > 0x3) |
| 50 | BX_PANIC("Bad drive type\n"); |
| 51 | |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 52 | printf("Boot failed"); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 53 | if (type < 4) { |
| 54 | /* Report the reason too */ |
| 55 | if (reason==0) |
| 56 | printf(": not a bootable disk"); |
| 57 | else |
| 58 | printf(": could not read the boot disk"); |
| 59 | } |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 60 | printf("\n\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 64 | try_boot(u16 seq_nr) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 65 | { |
Kevin O'Connor | 7a558e4 | 2008-03-11 20:38:33 -0400 | [diff] [blame] | 66 | irq_enable(); |
| 67 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 68 | SET_EBDA(ipl.sequence, seq_nr); |
Kevin O'Connor | 7a558e4 | 2008-03-11 20:38:33 -0400 | [diff] [blame] | 69 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 70 | u16 bootseg; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 71 | u8 bootdrv = 0; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 72 | u16 bootdev, bootip; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 73 | |
Kevin O'Connor | dfa1650 | 2008-03-22 20:13:08 -0400 | [diff] [blame] | 74 | // XXX - why different bootdev check based on CONFIG_CDROM_BOOT? |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 75 | if (CONFIG_CDROM_BOOT) { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 76 | bootdev = inb_cmos(CMOS_BIOS_BOOTFLAG2); |
| 77 | bootdev |= ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4); |
| 78 | bootdev >>= 4 * seq_nr; |
| 79 | bootdev &= 0xf; |
| 80 | if (bootdev == 0) |
| 81 | BX_PANIC("No bootable device.\n"); |
| 82 | |
| 83 | /* Translate from CMOS runes to an IPL table offset by subtracting 1 */ |
| 84 | bootdev -= 1; |
| 85 | } else { |
| 86 | if (seq_nr ==2) |
| 87 | BX_PANIC("No more boot devices."); |
| 88 | if (!!(inb_cmos(CMOS_BIOS_CONFIG) & 0x20) ^ (seq_nr == 1)) |
| 89 | /* Boot from floppy if the bit is set or it's the second boot */ |
| 90 | bootdev = 0x00; |
| 91 | else |
| 92 | bootdev = 0x01; |
| 93 | } |
| 94 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 95 | if (bootdev >= GET_EBDA(ipl.count)) { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 96 | BX_INFO("Invalid boot device (0x%x)\n", bootdev); |
| 97 | return; |
| 98 | } |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 99 | u16 type = GET_EBDA(ipl.table[bootdev].type); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 100 | |
| 101 | /* Do the loading, and set up vector as a far pointer to the boot |
| 102 | * address, and bootdrv as the boot drive */ |
| 103 | print_boot_device(type); |
| 104 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 105 | struct bregs cr; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 106 | switch(type) { |
| 107 | case IPL_TYPE_FLOPPY: /* FDD */ |
| 108 | case IPL_TYPE_HARDDISK: /* HDD */ |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 109 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 110 | bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00; |
| 111 | bootseg = 0x07c0; |
| 112 | |
| 113 | // Read sector |
| 114 | memset(&cr, 0, sizeof(cr)); |
| 115 | cr.dl = bootdrv; |
| 116 | cr.es = bootseg; |
| 117 | cr.ah = 2; |
| 118 | cr.al = 1; |
| 119 | cr.cl = 1; |
| 120 | call16_int(0x13, &cr); |
| 121 | |
| 122 | if (cr.flags & F_CF) { |
| 123 | print_boot_failure(type, 1); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | /* Always check the signature on a HDD boot sector; on FDD, |
| 128 | * only do the check if the CMOS doesn't tell us to skip it */ |
| 129 | if ((type != IPL_TYPE_FLOPPY) |
| 130 | || !((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0x01))) { |
| 131 | if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) { |
| 132 | print_boot_failure(type, 0); |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* Canonicalize bootseg:bootip */ |
| 138 | bootip = (bootseg & 0x0fff) << 4; |
| 139 | bootseg &= 0xf000; |
| 140 | break; |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 141 | case IPL_TYPE_CDROM: { |
| 142 | /* CD-ROM */ |
| 143 | if (! CONFIG_CDROM_BOOT) |
| 144 | break; |
| 145 | u16 status = cdrom_boot(); |
| 146 | if (status) { |
| 147 | printf("CDROM boot failure code : %04x\n", status); |
| 148 | print_boot_failure(type, 1); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | bootdrv = GET_EBDA(cdemu.emulated_drive); |
| 153 | bootseg = GET_EBDA(cdemu.load_segment); |
| 154 | /* Canonicalize bootseg:bootip */ |
| 155 | bootip = (bootseg & 0x0fff) << 4; |
| 156 | bootseg &= 0xf000; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 157 | break; |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 158 | } |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 159 | case IPL_TYPE_BEV: { |
| 160 | /* Expansion ROM with a Bootstrap Entry Vector (a far |
| 161 | * pointer) */ |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 162 | u32 vector = GET_EBDA(ipl.table[bootdev].vector); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 163 | bootseg = vector >> 16; |
| 164 | bootip = vector & 0xffff; |
| 165 | break; |
| 166 | } |
| 167 | default: |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | |
Kevin O'Connor | dcc7a4f | 2008-03-08 23:25:16 -0500 | [diff] [blame] | 171 | /* Debugging info */ |
| 172 | BX_INFO("Booting from %x:%x\n", bootseg, bootip); |
| 173 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 174 | memset(&cr, 0, sizeof(cr)); |
| 175 | cr.ip = bootip; |
| 176 | cr.cs = bootseg; |
| 177 | // Set the magic number in ax and the boot drive in dl. |
| 178 | cr.dl = bootdrv; |
| 179 | cr.ax = 0xaa55; |
| 180 | call16(&cr); |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void |
| 184 | do_boot(u16 seq_nr) |
| 185 | { |
| 186 | try_boot(seq_nr); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 187 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 188 | // Boot failed: invoke the boot recovery function |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 189 | struct bregs br; |
| 190 | memset(&br, 0, sizeof(br)); |
| 191 | call16_int(0x18, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Boot Failure recovery: try the next device. |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 195 | void VISIBLE16 |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 196 | handle_18() |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 197 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 198 | debug_enter(NULL); |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 199 | u16 seq = GET_EBDA(ipl.sequence) + 1; |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 200 | do_boot(seq); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // INT 19h Boot Load Service Entry Point |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 204 | void VISIBLE16 |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 205 | handle_19() |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 206 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 207 | debug_enter(NULL); |
Kevin O'Connor | 7af49bf | 2008-03-09 13:46:13 -0400 | [diff] [blame] | 208 | do_boot(0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 209 | } |