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