1 // 32bit code to Power On Self Test (POST) a machine.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
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
15 #define bda ((struct bios_data_area_s *)0)
16 #define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4))
17 #define ipl ((struct ipl_s *)(IPL_SEG<<4))
20 checksum(u8 *p, u32 len)
32 memset(bda, 0, sizeof(*bda));
35 for (i=0; i<256; i++) {
36 bda->ivecs[i].seg = SEG_BIOS;
37 bda->ivecs[i].offset = OFFSET_dummy_iret_handler;
40 bda->mem_size_kb = BASE_MEM_IN_K;
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;
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;
76 memset(ebda, 0, sizeof(*ebda));
77 ebda->size = EBDA_SIZE;
78 bda->ebda_seg = EBDA_SEG;
79 bda->ivecs[0x41].seg = EBDA_SEG;
80 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt0);
81 bda->ivecs[0x46].seg = EBDA_SEG;
82 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt1);
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);
95 //--------------------------------------------------------------------------
97 //--------------------------------------------------------------------------
99 keyboard_panic(u16 status)
101 // If you're getting a 993 keyboard panic here,
102 // please see the comment in keyboard_init
104 BX_PANIC("Keyboard error:%u\n",status);
108 kbd_wait(u8 mask, u8 code)
111 while ( ((inb(PORT_PS2_STATUS) & mask) == 0) && (--max>0) )
112 outb(code, PORT_DIAG);
114 keyboard_panic(code);
117 static inline void kbd_flush(u8 code) {
118 kbd_wait(0x02, code);
120 static inline void kbd_waitdata(u8 code) {
121 kbd_wait(0x01, code);
124 //--------------------------------------------------------------------------
126 //--------------------------------------------------------------------------
127 // this file is based on LinuxBIOS implementation of keyboard.c
128 // could convert to #asm to gain space
132 /* ------------------- Flush buffers ------------------------*/
133 /* Wait until buffer is empty */
136 /* flush incoming keys */
139 outb(0x00, PORT_DIAG);
140 if (inb(PORT_PS2_STATUS) & 0x01) {
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
151 /* ------------------- controller side ----------------------*/
152 /* send cmd = 0xAA, self test 8042 */
153 outb(0xaa, PORT_PS2_STATUS);
158 /* read self-test result, 0x55 should be returned from 0x60 */
159 if (inb(PORT_PS2_DATA) != 0x55)
162 /* send cmd = 0xAB, keyboard interface test */
163 outb(0xab, PORT_PS2_STATUS);
168 /* read keyboard interface test result, */
169 /* 0x00 should be returned form 0x60 */
170 if (inb(PORT_PS2_DATA) != 0x00)
173 /* Enable Keyboard clock */
174 outb(0xae, PORT_PS2_STATUS);
175 outb(0xa8, PORT_PS2_STATUS);
177 /* ------------------- keyboard side ------------------------*/
178 /* reset kerboard and self test (keyboard side) */
179 outb(0xff, PORT_PS2_DATA);
184 /* keyboard should return ACK */
185 if (inb(PORT_PS2_DATA) != 0xfa)
190 if (inb(PORT_PS2_DATA) != 0xaa)
193 /* Disable keyboard */
194 outb(0xf5, PORT_PS2_DATA);
199 /* keyboard should return ACK */
200 if (inb(PORT_PS2_DATA) != 0xfa)
203 /* Write Keyboard Mode */
204 outb(0x60, PORT_PS2_STATUS);
208 /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
209 outb(0x61, PORT_PS2_DATA);
213 /* Enable keyboard */
214 outb(0xf4, PORT_PS2_DATA);
219 /* keyboard should return ACK */
220 if (inb(PORT_PS2_DATA) != 0xfa)
223 outb(0x77, PORT_DIAG);
229 bda->kbd_mode = 0x10;
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)])
237 // mov CMOS Equipment Byte to BDA Equipment Word
238 u16 eqb = bda->equipment_list_flags;
239 bda->equipment_list_flags = (eqb & 0xff00) | inb_cmos(CMOS_EQUIPMENT_INFO);
243 detect_parport(u16 port, u8 timeout, u8 count)
246 outb(inb(port+2) & 0xdf, port+2);
249 if (inb(port) != 0xaa)
252 bda->port_lpt[count] = port;
253 bda->lpt_timeout[count] = timeout;
261 count += detect_parport(0x378, 0x14, count);
262 count += detect_parport(0x278, 0x14, count);
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);
270 detect_serial(u16 port, u8 timeout, u8 count)
273 if (inb(port+1) != 0x02)
275 if (inb(port+2) != 0x02)
278 bda->port_com[count] = port;
279 bda->com_timeout[count] = timeout;
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);
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);
300 return (val & 0xf) + ((val >> 4) * 10);
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;
319 outb(0x11, PORT_PIC1);
320 outb(0x11, PORT_PIC2);
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);
331 outb(0x9f, PORT_PIC2_DATA);
337 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
343 bda->floppy_harddisk_info = out;
344 outb(0x02, PORT_DMA1_MASK_REG);
346 bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2;
352 // hdidmap and cdidmap init.
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;
361 fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
363 u8 type = inb_cmos(typecmos);
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);
375 // no logical CHS mapping used, just physical CHS
376 // use Standard Fixed Disk Parameter Table (FDPT)
377 info->cylinders = cyl;
379 info->sectors = sectors;
383 // complies with Phoenix style Translated Fixed Disk Parameter
385 info->phys_cylinders = cyl;
386 info->phys_heads = heads;
387 info->phys_sectors = sectors;
388 info->sectors = sectors;
389 info->a0h_signature = 0xa0;
393 } else if (cyl > 4096) {
396 } else if (cyl > 2048) {
400 info->cylinders = cyl;
402 info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
408 outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
410 bda->disk_control_byte = 0xc0;
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);
427 // Clear out the IPL table.
428 memset(ipl, 0, sizeof(*ipl));
431 struct ipl_entry_s *ip = &ipl->table[0];
432 ip->type = IPL_TYPE_FLOPPY;
436 ip->type = IPL_TYPE_HARDDISK;
440 if (CONFIG_ELTORITO_BOOT) {
441 ip->type = IPL_TYPE_CDROM;
445 ipl->count = ip - ipl->table;
446 ipl->sequence = 0xffff;
450 callrom(u16 seg, u16 offset)
453 memset(&br, 0, sizeof(br));
455 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
462 rom_scan(u32 start, u32 end)
465 for (; p <= (u8*)end; p += 2048) {
467 if (*(u16*)rom != 0xaa55)
469 u32 len = rom[2] * 512;
470 if (checksum(rom, len) != 0)
472 p = (u8*)(((u32)p + len) / 2048 * 2048);
473 callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
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')
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];
486 // Found a device that thinks it can boot the system. Record
487 // its BEV and product name string.
489 if (ipl->count >= ARRAY_SIZE(ipl->table))
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;
496 u16 desc = *(u16*)&rom[0x1a+0x10];
498 ip->description = (PTR_TO_SEG(rom) << 16) | desc;
507 BX_INFO("Start bios\n");
520 rom_scan(0xc0000, 0xc7800);
522 printf("BIOS - begin\n\n");
524 // XXX - need to do pci stuff
534 rom_scan(0xc8000, 0xe0000);
536 callrom(SEG_BIOS, OFFSET_begin_boot);
542 // first reset the DMA controllers
543 outb(0, PORT_DMA1_MASTER_CLEAR);
544 outb(0, PORT_DMA2_MASTER_CLEAR);
546 // then initialize the DMA controllers
547 outb(0xc0, PORT_DMA2_MODE_REG);
548 outb(0x00, PORT_DMA2_MASK_REG);
554 // XXX - this is supposed to jump without changing any memory -
555 // but the stack has been altered by the time the code gets here.
558 memset(&br, 0, sizeof(br));
559 br.cs = bda->jump_cs_ip >> 16;
560 br.ip = bda->jump_cs_ip;
565 check_restart_status()
567 // Get and then clear CMOS shutdown status.
568 u8 status = inb_cmos(CMOS_RESET_CODE);
569 outb_cmos(0, CMOS_RESET_CODE);
571 if (status == 0x00 || status == 0x09 || status >= 0x0d)
578 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
585 check_restart_status();