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