Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // 32bit code to Power On Self Test (POST) a machine. |
| 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 | |
| 8 | #include "ioport.h" // PORT_* |
| 9 | #include "../out/rom16.offset.auto.h" // OFFSET_* |
| 10 | #include "config.h" // CONFIG_* |
| 11 | #include "cmos.h" // CMOS_* |
| 12 | #include "util.h" // memset |
| 13 | #include "biosvar.h" // struct bios_data_area_s |
Kevin O'Connor | 2ad3744 | 2008-05-06 19:49:01 -0400 | [diff] [blame] | 14 | #include "ata.h" // hard_drive_setup |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 15 | #include "kbd.h" // kbd_setup |
| 16 | #include "disk.h" // floppy_drive_setup |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 17 | |
Kevin O'Connor | 438f635 | 2008-03-30 21:46:53 -0400 | [diff] [blame] | 18 | #define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0)) |
| 19 | #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0)) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 20 | |
| 21 | static void |
| 22 | init_bda() |
| 23 | { |
| 24 | memset(bda, 0, sizeof(*bda)); |
| 25 | |
| 26 | int i; |
| 27 | for (i=0; i<256; i++) { |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 28 | SET_BDA(ivecs[i].seg, SEG_BIOS); |
| 29 | SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 32 | SET_BDA(mem_size_kb, BASE_MEM_IN_K); |
Kevin O'Connor | 9bd8391 | 2008-03-08 12:42:36 -0500 | [diff] [blame] | 33 | |
Kevin O'Connor | 88c00ee | 2008-05-18 00:14:13 -0400 | [diff] [blame] | 34 | // Set BDA Equipment Word - 0x06 = FPU enable, mouse installed |
| 35 | SET_BDA(equipment_list_flags, 0x06); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 36 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 37 | // set vector 0x79 to zero |
| 38 | // this is used by 'gardian angel' protection system |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 39 | SET_BDA(ivecs[0x79].seg, 0); |
| 40 | SET_BDA(ivecs[0x79].offset, 0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 41 | |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 42 | SET_BDA(ivecs[0x40].offset, OFFSET_entry_40); |
| 43 | SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e); |
| 44 | SET_BDA(ivecs[0x13].offset, OFFSET_entry_13); |
| 45 | SET_BDA(ivecs[0x76].offset, OFFSET_entry_76); |
| 46 | SET_BDA(ivecs[0x17].offset, OFFSET_entry_17); |
| 47 | SET_BDA(ivecs[0x18].offset, OFFSET_entry_18); |
| 48 | SET_BDA(ivecs[0x19].offset, OFFSET_entry_19); |
| 49 | SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c); |
| 50 | SET_BDA(ivecs[0x12].offset, OFFSET_entry_12); |
| 51 | SET_BDA(ivecs[0x11].offset, OFFSET_entry_11); |
| 52 | SET_BDA(ivecs[0x15].offset, OFFSET_entry_15); |
| 53 | SET_BDA(ivecs[0x08].offset, OFFSET_entry_08); |
| 54 | SET_BDA(ivecs[0x09].offset, OFFSET_entry_09); |
| 55 | SET_BDA(ivecs[0x16].offset, OFFSET_entry_16); |
| 56 | SET_BDA(ivecs[0x14].offset, OFFSET_entry_14); |
| 57 | SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a); |
| 58 | SET_BDA(ivecs[0x70].offset, OFFSET_entry_70); |
| 59 | SET_BDA(ivecs[0x74].offset, OFFSET_entry_74); |
| 60 | SET_BDA(ivecs[0x75].offset, OFFSET_entry_75); |
| 61 | SET_BDA(ivecs[0x10].offset, OFFSET_entry_10); |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 62 | |
| 63 | SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void |
| 67 | init_ebda() |
| 68 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 69 | memset(ebda, 0, sizeof(*ebda)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 70 | ebda->size = EBDA_SIZE; |
Kevin O'Connor | 438f635 | 2008-03-30 21:46:53 -0400 | [diff] [blame] | 71 | SET_BDA(ebda_seg, SEG_EBDA); |
| 72 | SET_BDA(ivecs[0x41].seg, SEG_EBDA); |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 73 | SET_BDA(ivecs[0x41].offset |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 74 | , offsetof(struct extended_bios_data_area_s, fdpt[0])); |
Kevin O'Connor | 438f635 | 2008-03-30 21:46:53 -0400 | [diff] [blame] | 75 | SET_BDA(ivecs[0x46].seg, SEG_EBDA); |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 76 | SET_BDA(ivecs[0x41].offset |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 77 | , offsetof(struct extended_bios_data_area_s, fdpt[1])); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 81 | ram_probe(void) |
| 82 | { |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 83 | u32 rs; |
| 84 | if (CONFIG_COREBOOT) { |
| 85 | // XXX - just hardcode for now. |
| 86 | rs = 128*1024*1024; |
| 87 | } else { |
| 88 | // On emulators, get memory size from nvram. |
| 89 | rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW) |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 90 | | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536; |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 91 | if (rs) |
| 92 | rs += 16 * 1024 * 1024; |
| 93 | else |
| 94 | rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW) |
| 95 | | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024 |
| 96 | + 1 * 1024 * 1024); |
| 97 | } |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 98 | |
| 99 | SET_EBDA(ram_size, rs); |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 100 | dprintf(1, "ram_size=0x%08x\n", rs); |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 104 | pic_setup() |
| 105 | { |
| 106 | outb(0x11, PORT_PIC1); |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 107 | outb(0x11, PORT_PIC2); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 108 | outb(0x08, PORT_PIC1_DATA); |
| 109 | outb(0x70, PORT_PIC2_DATA); |
| 110 | outb(0x04, PORT_PIC1_DATA); |
| 111 | outb(0x02, PORT_PIC2_DATA); |
| 112 | outb(0x01, PORT_PIC1_DATA); |
| 113 | outb(0x01, PORT_PIC2_DATA); |
| 114 | outb(0xb8, PORT_PIC1_DATA); |
| 115 | if (CONFIG_PS2_MOUSE) |
| 116 | outb(0x8f, PORT_PIC2_DATA); |
| 117 | else |
| 118 | outb(0x9f, PORT_PIC2_DATA); |
| 119 | } |
| 120 | |
| 121 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 122 | init_boot_vectors() |
| 123 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 124 | // Floppy drive |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 125 | struct ipl_entry_s *ip = &ebda->ipl.table[0]; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 126 | ip->type = IPL_TYPE_FLOPPY; |
| 127 | ip++; |
| 128 | |
| 129 | // First HDD |
| 130 | ip->type = IPL_TYPE_HARDDISK; |
| 131 | ip++; |
| 132 | |
| 133 | // CDROM |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 134 | if (CONFIG_CDROM_BOOT) { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 135 | ip->type = IPL_TYPE_CDROM; |
| 136 | ip++; |
| 137 | } |
| 138 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 139 | ebda->ipl.count = ip - ebda->ipl.table; |
| 140 | ebda->ipl.sequence = 0xffff; |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 141 | if (CONFIG_COREBOOT) { |
| 142 | // XXX - hardcode defaults for coreboot. |
| 143 | ebda->ipl.bootorder = 0x00000231; |
Kevin O'Connor | abc7597 | 2008-05-18 00:20:53 -0400 | [diff] [blame] | 144 | ebda->ipl.checkfloppysig = 1; |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 145 | } else { |
| 146 | // On emulators, get boot order from nvram. |
| 147 | ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2) |
| 148 | | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4)); |
| 149 | if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1)) |
| 150 | ebda->ipl.checkfloppysig = 1; |
| 151 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame^] | 154 | // Execute a given option rom. |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 155 | static void |
| 156 | callrom(u16 seg, u16 offset) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 157 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 158 | struct bregs br; |
| 159 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 160 | br.es = SEG_BIOS; |
| 161 | br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 162 | br.cs = seg; |
| 163 | br.ip = offset; |
| 164 | call16(&br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame^] | 167 | // Find and run any "option roms" found in the given address range. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 168 | static void |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 169 | rom_scan(u32 start, u32 end) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 170 | { |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 171 | u8 *p = (u8*)start; |
| 172 | for (; p <= (u8*)end; p += 2048) { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 173 | u8 *rom = p; |
| 174 | if (*(u16*)rom != 0xaa55) |
| 175 | continue; |
| 176 | u32 len = rom[2] * 512; |
| 177 | if (checksum(rom, len) != 0) |
| 178 | continue; |
| 179 | p = (u8*)(((u32)p + len) / 2048 * 2048); |
Kevin O'Connor | c1adedc | 2008-06-07 10:24:48 -0400 | [diff] [blame] | 180 | dprintf(1, "Running option rom at %p\n", rom+3); |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 181 | callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 182 | |
Kevin O'Connor | c1adedc | 2008-06-07 10:24:48 -0400 | [diff] [blame] | 183 | if (GET_BDA(ebda_seg) != SEG_EBDA) |
| 184 | BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n" |
| 185 | , rom, SEG_EBDA, GET_BDA(ebda_seg)); |
| 186 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 187 | // Look at the ROM's PnP Expansion header. Properly, we're supposed |
| 188 | // to init all the ROMs and then go back and build an IPL table of |
| 189 | // all the bootable devices, but we can get away with one pass. |
| 190 | if (rom[0x1a] != '$' || rom[0x1b] != 'P' |
| 191 | || rom[0x1c] != 'n' || rom[0x1d] != 'P') |
| 192 | continue; |
| 193 | // 0x1A is also the offset into the expansion header of... |
| 194 | // the Bootstrap Entry Vector, or zero if there is none. |
| 195 | u16 entry = *(u16*)&rom[0x1a+0x1a]; |
| 196 | if (!entry) |
| 197 | continue; |
| 198 | // Found a device that thinks it can boot the system. Record |
| 199 | // its BEV and product name string. |
| 200 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 201 | if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table)) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 202 | continue; |
| 203 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 204 | struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count]; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 205 | ip->type = IPL_TYPE_BEV; |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 206 | ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 207 | |
| 208 | u16 desc = *(u16*)&rom[0x1a+0x10]; |
| 209 | if (desc) |
Kevin O'Connor | e0113c9 | 2008-04-05 15:51:12 -0400 | [diff] [blame] | 210 | ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 211 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 212 | ebda->ipl.count++; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame^] | 216 | // Main setup code. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 217 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 218 | post() |
| 219 | { |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 220 | dprintf(3, "init bda\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 221 | init_bda(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 222 | init_ebda(); |
| 223 | |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 224 | dprintf(3, "init timer\n"); |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 225 | timer_setup(); |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 226 | dprintf(3, "init keyboard\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 227 | kbd_setup(); |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 228 | dprintf(3, "init lpt\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 229 | lpt_setup(); |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 230 | dprintf(3, "init serial\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 231 | serial_setup(); |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 232 | dprintf(3, "init pic\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 233 | pic_setup(); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 234 | |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 235 | dprintf(3, "Find memory size\n"); |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 236 | ram_probe(); |
| 237 | |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 238 | dprintf(1, "Scan for VGA option rom\n"); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 239 | rom_scan(0xc0000, 0xc7800); |
| 240 | |
| 241 | printf("BIOS - begin\n\n"); |
| 242 | |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 243 | dprintf(3, "rombios32 init\n"); |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 244 | rombios32_init(); |
| 245 | |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 246 | dprintf(3, "init floppy drives\n"); |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 247 | floppy_drive_setup(); |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 248 | dprintf(3, "init hard drives\n"); |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 249 | hard_drive_setup(); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 250 | |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 251 | dprintf(3, "init boot device ordering\n"); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 252 | init_boot_vectors(); |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 253 | |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 254 | dprintf(1, "Scan for option roms\n"); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 255 | rom_scan(0xc8000, 0xe0000); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame^] | 258 | // Clear .bss section for C code. |
| 259 | static void |
| 260 | clear_bss() |
| 261 | { |
| 262 | dprintf(3, "clearing .bss section\n"); |
| 263 | extern char __bss_start[], __bss_end[]; |
| 264 | memset(__bss_start, 0, __bss_end - __bss_start); |
| 265 | } |
| 266 | |
| 267 | // Reset DMA controller |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 268 | static void |
| 269 | init_dma() |
| 270 | { |
| 271 | // first reset the DMA controllers |
| 272 | outb(0, PORT_DMA1_MASTER_CLEAR); |
| 273 | outb(0, PORT_DMA2_MASTER_CLEAR); |
| 274 | |
| 275 | // then initialize the DMA controllers |
| 276 | outb(0xc0, PORT_DMA2_MODE_REG); |
| 277 | outb(0x00, PORT_DMA2_MASK_REG); |
| 278 | } |
| 279 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame^] | 280 | // Check if the machine was setup with a special restart vector. |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 281 | static void |
| 282 | check_restart_status() |
| 283 | { |
| 284 | // Get and then clear CMOS shutdown status. |
| 285 | u8 status = inb_cmos(CMOS_RESET_CODE); |
| 286 | outb_cmos(0, CMOS_RESET_CODE); |
| 287 | |
| 288 | if (status == 0x00 || status == 0x09 || status >= 0x0d) |
| 289 | // Normal post |
| 290 | return; |
| 291 | |
Kevin O'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 292 | if (status != 0x05) { |
| 293 | BX_PANIC("Unimplemented shutdown status: %02x\n", status); |
| 294 | return; |
| 295 | } |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 296 | |
Kevin O'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 297 | // XXX - this is supposed to jump without changing any memory - |
| 298 | // but the stack has been altered by the time the code gets here. |
| 299 | eoi_both_pics(); |
| 300 | struct bregs br; |
| 301 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 302 | br.cs = GET_BDA(jump_cs_ip) >> 16; |
| 303 | br.ip = GET_BDA(jump_cs_ip); |
Kevin O'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 304 | call16(&br); |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame^] | 307 | // 32-bit entry point. |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 308 | void VISIBLE32 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 309 | _start() |
| 310 | { |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 311 | init_dma(); |
| 312 | check_restart_status(); |
| 313 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame^] | 314 | dprintf(1, "Start bios\n"); |
| 315 | |
| 316 | // Setup for .bss and .data sections |
| 317 | clear_bss(); |
| 318 | make_bios_writable(); |
| 319 | |
| 320 | // Perform main setup code. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 321 | post(); |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame^] | 322 | |
| 323 | // Present the user with a bootup menu. |
| 324 | interactive_bootmenu(); |
| 325 | |
| 326 | // Prep for boot process. |
| 327 | make_bios_readonly(); |
| 328 | clear_bss(); |
| 329 | |
| 330 | // Invoke int 19 to start boot process. |
| 331 | dprintf(3, "Jump to int19\n"); |
| 332 | struct bregs br; |
| 333 | memset(&br, 0, sizeof(br)); |
| 334 | call16_int(0x19, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 335 | } |
Kevin O'Connor | 4b39b82 | 2008-05-08 21:47:33 -0400 | [diff] [blame] | 336 | |
| 337 | // Externally visible 32bit entry point. |
| 338 | asm( |
| 339 | ".global post32\n" |
| 340 | "post32:\n" |
| 341 | "cli\n" |
| 342 | "cld\n" |
| 343 | "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n" |
| 344 | "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n" |
| 345 | "movl $" __stringify(CONFIG_STACK_OFFSET) ", %esp\n" |
Kevin O'Connor | 59fead6 | 2008-05-10 15:49:20 -0400 | [diff] [blame] | 346 | "ljmp $0x10, $_start\n" |
Kevin O'Connor | 4b39b82 | 2008-05-08 21:47:33 -0400 | [diff] [blame] | 347 | ); |