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 |
| 14 | |
| 15 | #define bda ((struct bios_data_area_s *)0) |
| 16 | #define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4)) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 17 | #define ipl ((struct ipl_s *)(IPL_SEG<<4)) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 18 | |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 19 | static int |
| 20 | checksum(u8 *p, u32 len) |
| 21 | { |
| 22 | u32 i; |
| 23 | u8 sum = 0; |
| 24 | for (i=0; i<len; i++) |
| 25 | sum += p[i]; |
| 26 | return sum; |
| 27 | } |
| 28 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 29 | static void |
| 30 | init_bda() |
| 31 | { |
| 32 | memset(bda, 0, sizeof(*bda)); |
| 33 | |
| 34 | int i; |
| 35 | for (i=0; i<256; i++) { |
| 36 | bda->ivecs[i].seg = 0xf000; |
| 37 | bda->ivecs[i].offset = OFFSET_dummy_iret_handler; |
| 38 | } |
| 39 | |
| 40 | bda->mem_size_kb = BASE_MEM_IN_K; |
| 41 | } |
| 42 | |
| 43 | static void |
| 44 | init_handlers() |
| 45 | { |
| 46 | // set vector 0x79 to zero |
| 47 | // this is used by 'gardian angel' protection system |
| 48 | bda->ivecs[0x79].seg = 0; |
| 49 | bda->ivecs[0x79].offset = 0; |
| 50 | |
| 51 | bda->ivecs[0x40].offset = OFFSET_entry_40; |
| 52 | bda->ivecs[0x0e].offset = OFFSET_entry_0e; |
| 53 | bda->ivecs[0x13].offset = OFFSET_entry_13; |
| 54 | bda->ivecs[0x76].offset = OFFSET_entry_76; |
| 55 | bda->ivecs[0x17].offset = OFFSET_entry_17; |
| 56 | bda->ivecs[0x18].offset = OFFSET_entry_18; |
| 57 | bda->ivecs[0x19].offset = OFFSET_entry_19; |
| 58 | bda->ivecs[0x1c].offset = OFFSET_entry_1c; |
| 59 | bda->ivecs[0x12].offset = OFFSET_entry_12; |
| 60 | bda->ivecs[0x11].offset = OFFSET_entry_11; |
| 61 | bda->ivecs[0x15].offset = OFFSET_entry_15; |
| 62 | bda->ivecs[0x08].offset = OFFSET_entry_08; |
| 63 | bda->ivecs[0x09].offset = OFFSET_entry_09; |
| 64 | bda->ivecs[0x16].offset = OFFSET_entry_16; |
| 65 | bda->ivecs[0x14].offset = OFFSET_entry_14; |
| 66 | bda->ivecs[0x1a].offset = OFFSET_entry_1a; |
| 67 | bda->ivecs[0x70].offset = OFFSET_entry_70; |
| 68 | bda->ivecs[0x74].offset = OFFSET_entry_74; |
| 69 | bda->ivecs[0x75].offset = OFFSET_entry_75; |
| 70 | bda->ivecs[0x10].offset = OFFSET_entry_10; |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | init_ebda() |
| 75 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 76 | memset(ebda, 0, sizeof(*ebda)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 77 | ebda->size = EBDA_SIZE; |
| 78 | bda->ebda_seg = EBDA_SEG; |
| 79 | bda->ivecs[0x41].seg = EBDA_SEG; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 80 | bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 81 | bda->ivecs[0x46].seg = EBDA_SEG; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 82 | bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt1); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void |
| 86 | pit_setup() |
| 87 | { |
| 88 | // timer0: binary count, 16bit count, mode 2 |
| 89 | outb(0x34, PORT_PIT_MODE); |
| 90 | // maximum count of 0000H = 18.2Hz |
| 91 | outb(0x0, PORT_PIT_COUNTER0); |
| 92 | outb(0x0, PORT_PIT_COUNTER0); |
| 93 | } |
| 94 | |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 95 | //-------------------------------------------------------------------------- |
| 96 | // keyboard_panic |
| 97 | //-------------------------------------------------------------------------- |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 98 | static void |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 99 | keyboard_panic(u16 status) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 100 | { |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 101 | // If you're getting a 993 keyboard panic here, |
| 102 | // please see the comment in keyboard_init |
| 103 | |
| 104 | BX_PANIC("Keyboard error:%u\n",status); |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | kbd_wait(u8 mask, u8 code) |
| 109 | { |
| 110 | u16 max = 0xffff; |
| 111 | while ( ((inb(PORT_KBD_STATUS) & mask) == 0) && (--max>0) ) |
| 112 | outb(code, PORT_DIAG); |
| 113 | if (!max) |
| 114 | keyboard_panic(code); |
| 115 | } |
| 116 | |
| 117 | static inline void kbd_flush(u8 code) { |
| 118 | kbd_wait(0x02, code); |
| 119 | } |
| 120 | static inline void kbd_waitdata(u8 code) { |
| 121 | kbd_wait(0x01, code); |
| 122 | } |
| 123 | |
| 124 | //-------------------------------------------------------------------------- |
| 125 | // keyboard_init |
| 126 | //-------------------------------------------------------------------------- |
| 127 | // this file is based on LinuxBIOS implementation of keyboard.c |
| 128 | // could convert to #asm to gain space |
| 129 | static void |
| 130 | keyboard_init() |
| 131 | { |
| 132 | /* ------------------- Flush buffers ------------------------*/ |
| 133 | /* Wait until buffer is empty */ |
| 134 | kbd_flush(0x00); |
| 135 | |
| 136 | /* flush incoming keys */ |
| 137 | u16 max=0x2000; |
| 138 | while (--max > 0) { |
| 139 | outb(0x00, PORT_DIAG); |
| 140 | if (inb(PORT_KBD_STATUS) & 0x01) { |
| 141 | inb(PORT_KBD_DATA); |
| 142 | max = 0x2000; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Due to timer issues, and if the IPS setting is > 15000000, |
| 147 | // the incoming keys might not be flushed here. That will |
| 148 | // cause a panic a few lines below. See sourceforge bug report : |
| 149 | // [ 642031 ] FATAL: Keyboard RESET error:993 |
| 150 | |
| 151 | /* ------------------- controller side ----------------------*/ |
| 152 | /* send cmd = 0xAA, self test 8042 */ |
| 153 | outb(0xaa, PORT_KBD_STATUS); |
| 154 | |
| 155 | kbd_flush(0x00); |
| 156 | kbd_waitdata(0x01); |
| 157 | |
| 158 | /* read self-test result, 0x55 should be returned from 0x60 */ |
| 159 | if ((inb(PORT_KBD_DATA) != 0x55)) |
| 160 | keyboard_panic(991); |
| 161 | |
| 162 | /* send cmd = 0xAB, keyboard interface test */ |
| 163 | outb(0xab, PORT_KBD_STATUS); |
| 164 | |
| 165 | kbd_flush(0x10); |
| 166 | kbd_waitdata(0x11); |
| 167 | |
| 168 | /* read keyboard interface test result, */ |
| 169 | /* 0x00 should be returned form 0x60 */ |
| 170 | if ((inb(PORT_KBD_DATA) != 0x00)) |
| 171 | keyboard_panic(992); |
| 172 | |
| 173 | /* Enable Keyboard clock */ |
| 174 | outb(0xae, PORT_KBD_STATUS); |
| 175 | outb(0xa8, PORT_KBD_STATUS); |
| 176 | |
| 177 | /* ------------------- keyboard side ------------------------*/ |
| 178 | /* reset kerboard and self test (keyboard side) */ |
| 179 | outb(0xff, PORT_KBD_DATA); |
| 180 | |
| 181 | kbd_flush(0x20); |
| 182 | kbd_waitdata(0x21); |
| 183 | |
| 184 | /* keyboard should return ACK */ |
| 185 | if ((inb(PORT_KBD_DATA) != 0xfa)) |
| 186 | keyboard_panic(993); |
| 187 | |
| 188 | kbd_waitdata(0x31); |
| 189 | |
| 190 | if ((inb(PORT_KBD_DATA) != 0xaa)) |
| 191 | keyboard_panic(994); |
| 192 | |
| 193 | /* Disable keyboard */ |
| 194 | outb(0xf5, PORT_KBD_DATA); |
| 195 | |
| 196 | kbd_flush(0x40); |
| 197 | kbd_waitdata(0x41); |
| 198 | |
| 199 | /* keyboard should return ACK */ |
| 200 | if ((inb(PORT_KBD_DATA) != 0xfa)) |
| 201 | keyboard_panic(995); |
| 202 | |
| 203 | /* Write Keyboard Mode */ |
| 204 | outb(0x60, PORT_KBD_STATUS); |
| 205 | |
| 206 | kbd_flush(0x50); |
| 207 | |
| 208 | /* send cmd: scan code convert, disable mouse, enable IRQ 1 */ |
| 209 | outb(0x61, PORT_KBD_DATA); |
| 210 | |
| 211 | kbd_flush(0x60); |
| 212 | |
| 213 | /* Enable keyboard */ |
| 214 | outb(0xf4, PORT_KBD_DATA); |
| 215 | |
| 216 | kbd_flush(0x70); |
| 217 | kbd_waitdata(0x71); |
| 218 | |
| 219 | /* keyboard should return ACK */ |
| 220 | if ((inb(PORT_KBD_DATA) != 0xfa)) |
| 221 | keyboard_panic(996); |
| 222 | |
| 223 | outb(0x77, PORT_DIAG); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static void |
| 227 | kbd_setup() |
| 228 | { |
| 229 | bda->kbd_mode = 0x10; |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 230 | bda->kbd_buf_head = bda->kbd_buf_tail = bda->kbd_buf_start_offset |
| 231 | = offsetof(struct bios_data_area_s, kbd_buf) - 0x400; |
| 232 | bda->kbd_buf_end_offset |
| 233 | = (offsetof(struct bios_data_area_s, kbd_buf[sizeof(bda->kbd_buf)]) |
| 234 | - 0x400); |
| 235 | keyboard_init(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 236 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 237 | // mov CMOS Equipment Byte to BDA Equipment Word |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 238 | u16 eqb = bda->equipment_list_flags; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 239 | bda->equipment_list_flags = (eqb & 0xff00) | inb_cmos(CMOS_EQUIPMENT_INFO); |
| 240 | } |
| 241 | |
| 242 | static u16 |
| 243 | detect_parport(u16 port, u8 timeout, u8 count) |
| 244 | { |
| 245 | // clear input mode |
| 246 | outb(inb(port+2) & 0xdf, port+2); |
| 247 | |
| 248 | outb(0xaa, port); |
| 249 | if (inb(port) != 0xaa) |
| 250 | // Not present |
| 251 | return 0; |
| 252 | bda->port_lpt[count] = port; |
| 253 | bda->lpt_timeout[count] = timeout; |
| 254 | return 1; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void |
| 258 | lpt_setup() |
| 259 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 260 | u16 count = 0; |
| 261 | count += detect_parport(0x378, 0x14, count); |
| 262 | count += detect_parport(0x278, 0x14, count); |
| 263 | |
| 264 | // Equipment word bits 14..15 determing # parallel ports |
| 265 | u16 eqb = bda->equipment_list_flags; |
| 266 | bda->equipment_list_flags = (eqb & 0x3fff) | (count << 14); |
| 267 | } |
| 268 | |
| 269 | static u16 |
| 270 | detect_serial(u16 port, u8 timeout, u8 count) |
| 271 | { |
| 272 | outb(0x02, port+1); |
| 273 | if (inb(port+1) != 0x02) |
| 274 | return 0; |
| 275 | if (inb(port+2) != 0x02) |
| 276 | return 0; |
| 277 | outb(0x00, port+1); |
| 278 | bda->port_com[count] = port; |
| 279 | bda->com_timeout[count] = timeout; |
| 280 | return 1; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static void |
| 284 | serial_setup() |
| 285 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 286 | u16 count = 0; |
| 287 | count += detect_serial(0x3f8, 0x0a, count); |
| 288 | count += detect_serial(0x2f8, 0x0a, count); |
| 289 | count += detect_serial(0x3e8, 0x0a, count); |
| 290 | count += detect_serial(0x2e8, 0x0a, count); |
| 291 | |
| 292 | // Equipment word bits 9..11 determing # serial ports |
| 293 | u16 eqb = bda->equipment_list_flags; |
| 294 | bda->equipment_list_flags = (eqb & 0xf1ff) | (count << 9); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static u32 |
| 298 | bcd2bin(u8 val) |
| 299 | { |
| 300 | return (val & 0xf) + ((val >> 4) * 10); |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | timer_setup() |
| 305 | { |
| 306 | u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS)); |
| 307 | u32 ticks = (seconds * 18206507) / 1000000; |
| 308 | u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES)); |
| 309 | ticks += (minutes * 10923904) / 10000; |
| 310 | u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS)); |
| 311 | ticks += (hours * 65543427) / 1000; |
| 312 | bda->timer_counter = ticks; |
| 313 | bda->timer_rollover = 0; |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | pic_setup() |
| 318 | { |
| 319 | outb(0x11, PORT_PIC1); |
| 320 | outb(0x11, PORT_PIC2_DATA); |
| 321 | outb(0x08, PORT_PIC1_DATA); |
| 322 | outb(0x70, PORT_PIC2_DATA); |
| 323 | outb(0x04, PORT_PIC1_DATA); |
| 324 | outb(0x02, PORT_PIC2_DATA); |
| 325 | outb(0x01, PORT_PIC1_DATA); |
| 326 | outb(0x01, PORT_PIC2_DATA); |
| 327 | outb(0xb8, PORT_PIC1_DATA); |
| 328 | if (CONFIG_PS2_MOUSE) |
| 329 | outb(0x8f, PORT_PIC2_DATA); |
| 330 | else |
| 331 | outb(0x9f, PORT_PIC2_DATA); |
| 332 | } |
| 333 | |
| 334 | static void |
| 335 | floppy_drive_post() |
| 336 | { |
| 337 | u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE); |
| 338 | u8 out = 0; |
| 339 | if (type & 0xf0) |
| 340 | out |= 0x07; |
| 341 | if (type & 0x0f) |
| 342 | out |= 0x70; |
| 343 | bda->floppy_harddisk_info = out; |
| 344 | outb(0x02, PORT_DMA1_MASK_REG); |
| 345 | |
| 346 | bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2; |
| 347 | } |
| 348 | |
| 349 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 350 | ata_init() |
| 351 | { |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 352 | // hdidmap and cdidmap init. |
| 353 | u8 device; |
| 354 | for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) { |
| 355 | ebda->ata.hdidmap[device] = CONFIG_MAX_ATA_DEVICES; |
| 356 | ebda->ata.cdidmap[device] = CONFIG_MAX_ATA_DEVICES; |
| 357 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static void |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 361 | fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos) |
| 362 | { |
| 363 | u8 type = inb_cmos(typecmos); |
| 364 | if (type != 47) |
| 365 | // XXX - halt |
| 366 | return; |
| 367 | |
| 368 | info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3); |
| 369 | info->drive_control_byte = inb_cmos(basecmos+5); |
| 370 | info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6); |
| 371 | u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0); |
| 372 | u8 heads = inb_cmos(basecmos+2); |
| 373 | u8 sectors = inb_cmos(basecmos+8); |
| 374 | if (cyl < 1024) { |
| 375 | // no logical CHS mapping used, just physical CHS |
| 376 | // use Standard Fixed Disk Parameter Table (FDPT) |
| 377 | info->cylinders = cyl; |
| 378 | info->heads = heads; |
| 379 | info->sectors = sectors; |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | // complies with Phoenix style Translated Fixed Disk Parameter |
| 384 | // Table (FDPT) |
| 385 | info->phys_cylinders = cyl; |
| 386 | info->phys_heads = heads; |
| 387 | info->phys_sectors = sectors; |
| 388 | info->sectors = sectors; |
| 389 | info->a0h_signature = 0xa0; |
| 390 | if (cyl > 8192) { |
| 391 | cyl >>= 4; |
| 392 | heads <<= 4; |
| 393 | } else if (cyl > 4096) { |
| 394 | cyl >>= 3; |
| 395 | heads <<= 3; |
| 396 | } else if (cyl > 2048) { |
| 397 | cyl >>= 2; |
| 398 | heads <<= 2; |
| 399 | } |
| 400 | info->cylinders = cyl; |
| 401 | info->heads = heads; |
| 402 | info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1; |
| 403 | } |
| 404 | |
| 405 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 406 | hard_drive_post() |
| 407 | { |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 408 | outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14 |
| 409 | bda->disk_count = 1; |
| 410 | bda->disk_control_byte = 0xc0; |
| 411 | |
| 412 | // move disk geometry data from CMOS to EBDA disk parameter table(s) |
| 413 | u8 diskinfo = inb_cmos(CMOS_DISK_DATA); |
| 414 | if ((diskinfo & 0xf0) == 0xf0) |
| 415 | // Fill EBDA table for hard disk 0. |
| 416 | fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL); |
| 417 | if ((diskinfo & 0x0f) == 0x0f) |
| 418 | // XXX - bochs halts on any other type |
| 419 | // Fill EBDA table for hard disk 1. |
| 420 | fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 423 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 424 | static void |
| 425 | init_boot_vectors() |
| 426 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 427 | // Clear out the IPL table. |
| 428 | memset(ipl, 0, sizeof(*ipl)); |
| 429 | |
| 430 | // Floppy drive |
| 431 | struct ipl_entry_s *ip = &ipl->table[0]; |
| 432 | ip->type = IPL_TYPE_FLOPPY; |
| 433 | ip++; |
| 434 | |
| 435 | // First HDD |
| 436 | ip->type = IPL_TYPE_HARDDISK; |
| 437 | ip++; |
| 438 | |
| 439 | // CDROM |
| 440 | if (CONFIG_ELTORITO_BOOT) { |
| 441 | ip->type = IPL_TYPE_CDROM; |
| 442 | ip++; |
| 443 | } |
| 444 | |
| 445 | ipl->count = ip - ipl->table; |
| 446 | ipl->sequence = 0xffff; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 449 | static void |
| 450 | callrom(u16 seg, u16 offset) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 451 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 452 | struct bregs br; |
| 453 | memset(&br, 0, sizeof(br)); |
| 454 | br.es = 0xf000; |
| 455 | br.di = OFFSET_pnp_string; |
| 456 | br.cs = seg; |
| 457 | br.ip = offset; |
| 458 | call16(&br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 459 | } |
| 460 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 461 | static void |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 462 | rom_scan(u32 start, u32 end) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 463 | { |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 464 | u8 *p = (u8*)start; |
| 465 | for (; p <= (u8*)end; p += 2048) { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 466 | u8 *rom = p; |
| 467 | if (*(u16*)rom != 0xaa55) |
| 468 | continue; |
| 469 | u32 len = rom[2] * 512; |
| 470 | if (checksum(rom, len) != 0) |
| 471 | continue; |
| 472 | p = (u8*)(((u32)p + len) / 2048 * 2048); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 473 | callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 474 | |
| 475 | // Look at the ROM's PnP Expansion header. Properly, we're supposed |
| 476 | // to init all the ROMs and then go back and build an IPL table of |
| 477 | // all the bootable devices, but we can get away with one pass. |
| 478 | if (rom[0x1a] != '$' || rom[0x1b] != 'P' |
| 479 | || rom[0x1c] != 'n' || rom[0x1d] != 'P') |
| 480 | continue; |
| 481 | // 0x1A is also the offset into the expansion header of... |
| 482 | // the Bootstrap Entry Vector, or zero if there is none. |
| 483 | u16 entry = *(u16*)&rom[0x1a+0x1a]; |
| 484 | if (!entry) |
| 485 | continue; |
| 486 | // Found a device that thinks it can boot the system. Record |
| 487 | // its BEV and product name string. |
| 488 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 489 | if (ipl->count >= ARRAY_SIZE(ipl->table)) |
| 490 | continue; |
| 491 | |
| 492 | struct ipl_entry_s *ip = &ipl->table[ipl->count]; |
| 493 | ip->type = IPL_TYPE_BEV; |
| 494 | ip->vector = (PTR_TO_SEG(rom) << 16) | entry; |
| 495 | |
| 496 | u16 desc = *(u16*)&rom[0x1a+0x10]; |
| 497 | if (desc) |
| 498 | ip->description = (PTR_TO_SEG(rom) << 16) | desc; |
| 499 | |
| 500 | ipl->count++; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 505 | post() |
| 506 | { |
Kevin O'Connor | 4ce6a49 | 2008-02-29 00:21:27 -0500 | [diff] [blame] | 507 | BX_INFO("Start bios\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 508 | |
| 509 | init_bda(); |
| 510 | init_handlers(); |
| 511 | init_ebda(); |
| 512 | |
| 513 | pit_setup(); |
| 514 | kbd_setup(); |
| 515 | lpt_setup(); |
| 516 | serial_setup(); |
| 517 | timer_setup(); |
| 518 | pic_setup(); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 519 | |
| 520 | rom_scan(0xc0000, 0xc7800); |
| 521 | |
| 522 | printf("BIOS - begin\n\n"); |
| 523 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 524 | // XXX - need to do pci stuff |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 525 | //pci_setup(); |
| 526 | init_boot_vectors(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 527 | |
| 528 | floppy_drive_post(); |
| 529 | hard_drive_post(); |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame^] | 530 | if (CONFIG_ATA) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 531 | ata_init(); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 532 | |
| 533 | init_boot_vectors(); |
| 534 | rom_scan(0xc8000, 0xe0000); |
| 535 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 536 | callrom(0xf000, OFFSET_begin_boot); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame^] | 539 | static void |
| 540 | init_dma() |
| 541 | { |
| 542 | // first reset the DMA controllers |
| 543 | outb(0, PORT_DMA1_MASTER_CLEAR); |
| 544 | outb(0, PORT_DMA2_MASTER_CLEAR); |
| 545 | |
| 546 | // then initialize the DMA controllers |
| 547 | outb(0xc0, PORT_DMA2_MODE_REG); |
| 548 | outb(0x00, PORT_DMA2_MASK_REG); |
| 549 | } |
| 550 | |
| 551 | static void |
| 552 | check_restart_status() |
| 553 | { |
| 554 | // Get and then clear CMOS shutdown status. |
| 555 | u8 status = inb_cmos(CMOS_RESET_CODE); |
| 556 | outb_cmos(0, CMOS_RESET_CODE); |
| 557 | |
| 558 | if (status == 0x00 || status == 0x09 || status >= 0x0d) |
| 559 | // Normal post |
| 560 | return; |
| 561 | |
| 562 | // XXX |
| 563 | #if 0 |
| 564 | if (status == 0x05) |
| 565 | eoi_jmp_post(); |
| 566 | #endif |
| 567 | |
| 568 | BX_PANIC("Unimplemented shutdown status: %02x\n",(Bit8u)status); |
| 569 | } |
| 570 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 571 | void VISIBLE |
| 572 | _start() |
| 573 | { |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame^] | 574 | init_dma(); |
| 575 | check_restart_status(); |
| 576 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 577 | post(); |
| 578 | } |