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