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 | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 14 | #include "ata.h" // ata_detect |
| 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 | |
| 34 | // mov CMOS Equipment Byte to BDA Equipment Word |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 35 | SET_BDA(equipment_list_flags, inb_cmos(CMOS_EQUIPMENT_INFO)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static void |
| 39 | init_handlers() |
| 40 | { |
| 41 | // set vector 0x79 to zero |
| 42 | // this is used by 'gardian angel' protection system |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 43 | SET_BDA(ivecs[0x79].seg, 0); |
| 44 | SET_BDA(ivecs[0x79].offset, 0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 45 | |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 46 | SET_BDA(ivecs[0x40].offset, OFFSET_entry_40); |
| 47 | SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e); |
| 48 | SET_BDA(ivecs[0x13].offset, OFFSET_entry_13); |
| 49 | SET_BDA(ivecs[0x76].offset, OFFSET_entry_76); |
| 50 | SET_BDA(ivecs[0x17].offset, OFFSET_entry_17); |
| 51 | SET_BDA(ivecs[0x18].offset, OFFSET_entry_18); |
| 52 | SET_BDA(ivecs[0x19].offset, OFFSET_entry_19); |
| 53 | SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c); |
| 54 | SET_BDA(ivecs[0x12].offset, OFFSET_entry_12); |
| 55 | SET_BDA(ivecs[0x11].offset, OFFSET_entry_11); |
| 56 | SET_BDA(ivecs[0x15].offset, OFFSET_entry_15); |
| 57 | SET_BDA(ivecs[0x08].offset, OFFSET_entry_08); |
| 58 | SET_BDA(ivecs[0x09].offset, OFFSET_entry_09); |
| 59 | SET_BDA(ivecs[0x16].offset, OFFSET_entry_16); |
| 60 | SET_BDA(ivecs[0x14].offset, OFFSET_entry_14); |
| 61 | SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a); |
| 62 | SET_BDA(ivecs[0x70].offset, OFFSET_entry_70); |
| 63 | SET_BDA(ivecs[0x74].offset, OFFSET_entry_74); |
| 64 | SET_BDA(ivecs[0x75].offset, OFFSET_entry_75); |
| 65 | SET_BDA(ivecs[0x10].offset, OFFSET_entry_10); |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 66 | |
| 67 | SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void |
| 71 | init_ebda() |
| 72 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 73 | memset(ebda, 0, sizeof(*ebda)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 74 | ebda->size = EBDA_SIZE; |
Kevin O'Connor | 438f635 | 2008-03-30 21:46:53 -0400 | [diff] [blame] | 75 | SET_BDA(ebda_seg, SEG_EBDA); |
| 76 | SET_BDA(ivecs[0x41].seg, SEG_EBDA); |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 77 | SET_BDA(ivecs[0x41].offset |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 78 | , offsetof(struct extended_bios_data_area_s, fdpt[0])); |
Kevin O'Connor | 438f635 | 2008-03-30 21:46:53 -0400 | [diff] [blame] | 79 | SET_BDA(ivecs[0x46].seg, SEG_EBDA); |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 80 | SET_BDA(ivecs[0x41].offset |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 81 | , offsetof(struct extended_bios_data_area_s, fdpt[1])); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 85 | pic_setup() |
| 86 | { |
| 87 | outb(0x11, PORT_PIC1); |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 88 | outb(0x11, PORT_PIC2); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 89 | outb(0x08, PORT_PIC1_DATA); |
| 90 | outb(0x70, PORT_PIC2_DATA); |
| 91 | outb(0x04, PORT_PIC1_DATA); |
| 92 | outb(0x02, PORT_PIC2_DATA); |
| 93 | outb(0x01, PORT_PIC1_DATA); |
| 94 | outb(0x01, PORT_PIC2_DATA); |
| 95 | outb(0xb8, PORT_PIC1_DATA); |
| 96 | if (CONFIG_PS2_MOUSE) |
| 97 | outb(0x8f, PORT_PIC2_DATA); |
| 98 | else |
| 99 | outb(0x9f, PORT_PIC2_DATA); |
| 100 | } |
| 101 | |
| 102 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 103 | init_boot_vectors() |
| 104 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 105 | // Floppy drive |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 106 | struct ipl_entry_s *ip = &ebda->ipl.table[0]; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 107 | ip->type = IPL_TYPE_FLOPPY; |
| 108 | ip++; |
| 109 | |
| 110 | // First HDD |
| 111 | ip->type = IPL_TYPE_HARDDISK; |
| 112 | ip++; |
| 113 | |
| 114 | // CDROM |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 115 | if (CONFIG_CDROM_BOOT) { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 116 | ip->type = IPL_TYPE_CDROM; |
| 117 | ip++; |
| 118 | } |
| 119 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 120 | ebda->ipl.count = ip - ebda->ipl.table; |
| 121 | ebda->ipl.sequence = 0xffff; |
Kevin O'Connor | e0113c9 | 2008-04-05 15:51:12 -0400 | [diff] [blame] | 122 | ebda->ipl.bootfirst = 0xffff; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 125 | static void |
| 126 | callrom(u16 seg, u16 offset) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 127 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 128 | struct bregs br; |
| 129 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 130 | br.es = SEG_BIOS; |
| 131 | br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 132 | br.cs = seg; |
| 133 | br.ip = offset; |
| 134 | call16(&br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 137 | static void |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 138 | rom_scan(u32 start, u32 end) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 139 | { |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 140 | u8 *p = (u8*)start; |
| 141 | for (; p <= (u8*)end; p += 2048) { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 142 | u8 *rom = p; |
| 143 | if (*(u16*)rom != 0xaa55) |
| 144 | continue; |
| 145 | u32 len = rom[2] * 512; |
| 146 | if (checksum(rom, len) != 0) |
| 147 | continue; |
| 148 | p = (u8*)(((u32)p + len) / 2048 * 2048); |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 149 | callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 150 | |
| 151 | // Look at the ROM's PnP Expansion header. Properly, we're supposed |
| 152 | // to init all the ROMs and then go back and build an IPL table of |
| 153 | // all the bootable devices, but we can get away with one pass. |
| 154 | if (rom[0x1a] != '$' || rom[0x1b] != 'P' |
| 155 | || rom[0x1c] != 'n' || rom[0x1d] != 'P') |
| 156 | continue; |
| 157 | // 0x1A is also the offset into the expansion header of... |
| 158 | // the Bootstrap Entry Vector, or zero if there is none. |
| 159 | u16 entry = *(u16*)&rom[0x1a+0x1a]; |
| 160 | if (!entry) |
| 161 | continue; |
| 162 | // Found a device that thinks it can boot the system. Record |
| 163 | // its BEV and product name string. |
| 164 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 165 | if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table)) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 166 | continue; |
| 167 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 168 | struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count]; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 169 | ip->type = IPL_TYPE_BEV; |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 170 | ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 171 | |
| 172 | u16 desc = *(u16*)&rom[0x1a+0x10]; |
| 173 | if (desc) |
Kevin O'Connor | e0113c9 | 2008-04-05 15:51:12 -0400 | [diff] [blame] | 174 | ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 175 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame] | 176 | ebda->ipl.count++; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 181 | post() |
| 182 | { |
Kevin O'Connor | 4ce6a49 | 2008-02-29 00:21:27 -0500 | [diff] [blame] | 183 | BX_INFO("Start bios\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 184 | |
| 185 | init_bda(); |
| 186 | init_handlers(); |
| 187 | init_ebda(); |
| 188 | |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame^] | 189 | timer_setup(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 190 | kbd_setup(); |
| 191 | lpt_setup(); |
| 192 | serial_setup(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 193 | pic_setup(); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 194 | |
| 195 | rom_scan(0xc0000, 0xc7800); |
| 196 | |
| 197 | printf("BIOS - begin\n\n"); |
| 198 | |
Kevin O'Connor | 7958de3 | 2008-03-16 21:07:33 -0400 | [diff] [blame] | 199 | // clear bss section -- XXX - shouldn't use globals |
| 200 | extern char __bss_start[], __bss_end[]; |
| 201 | memset(__bss_start, 0, __bss_end - __bss_start); |
| 202 | |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 203 | rombios32_init(); |
| 204 | |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 205 | floppy_drive_setup(); |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 206 | hard_drive_setup(); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 207 | |
| 208 | init_boot_vectors(); |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 209 | |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 210 | rom_scan(0xc8000, 0xe0000); |
| 211 | |
Kevin O'Connor | e0113c9 | 2008-04-05 15:51:12 -0400 | [diff] [blame] | 212 | interactive_bootmenu(); |
| 213 | |
Kevin O'Connor | 157e213 | 2008-03-09 13:32:03 -0400 | [diff] [blame] | 214 | // reset the memory (some boot loaders such as syslinux suppose |
| 215 | // that the memory is set to zero) |
| 216 | memset((void*)0x40000, 0, 0x40000); // XXX - shouldn't use globals |
| 217 | |
Kevin O'Connor | 63ccc13 | 2008-03-12 20:57:08 -0400 | [diff] [blame] | 218 | // Invoke int 19 to start boot process. |
| 219 | struct bregs br; |
| 220 | memset(&br, 0, sizeof(br)); |
| 221 | call16_int(0x19, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 224 | static void |
| 225 | init_dma() |
| 226 | { |
| 227 | // first reset the DMA controllers |
| 228 | outb(0, PORT_DMA1_MASTER_CLEAR); |
| 229 | outb(0, PORT_DMA2_MASTER_CLEAR); |
| 230 | |
| 231 | // then initialize the DMA controllers |
| 232 | outb(0xc0, PORT_DMA2_MODE_REG); |
| 233 | outb(0x00, PORT_DMA2_MASK_REG); |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | check_restart_status() |
| 238 | { |
| 239 | // Get and then clear CMOS shutdown status. |
| 240 | u8 status = inb_cmos(CMOS_RESET_CODE); |
| 241 | outb_cmos(0, CMOS_RESET_CODE); |
| 242 | |
| 243 | if (status == 0x00 || status == 0x09 || status >= 0x0d) |
| 244 | // Normal post |
| 245 | return; |
| 246 | |
Kevin O'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 247 | if (status != 0x05) { |
| 248 | BX_PANIC("Unimplemented shutdown status: %02x\n", status); |
| 249 | return; |
| 250 | } |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 251 | |
Kevin O'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 252 | // XXX - this is supposed to jump without changing any memory - |
| 253 | // but the stack has been altered by the time the code gets here. |
| 254 | eoi_both_pics(); |
| 255 | struct bregs br; |
| 256 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 257 | br.cs = GET_BDA(jump_cs_ip) >> 16; |
| 258 | br.ip = GET_BDA(jump_cs_ip); |
Kevin O'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 259 | call16(&br); |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 260 | } |
| 261 | |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 262 | void VISIBLE32 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 263 | _start() |
| 264 | { |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 265 | init_dma(); |
| 266 | check_restart_status(); |
| 267 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 268 | post(); |
| 269 | } |