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))
22 memset(bda, 0, sizeof(*bda));
25 for (i=0; i<256; i++) {
26 bda->ivecs[i].seg = 0xf000;
27 bda->ivecs[i].offset = OFFSET_dummy_iret_handler;
30 bda->mem_size_kb = BASE_MEM_IN_K;
36 // set vector 0x79 to zero
37 // this is used by 'gardian angel' protection system
38 bda->ivecs[0x79].seg = 0;
39 bda->ivecs[0x79].offset = 0;
41 bda->ivecs[0x40].offset = OFFSET_entry_40;
42 bda->ivecs[0x0e].offset = OFFSET_entry_0e;
43 bda->ivecs[0x13].offset = OFFSET_entry_13;
44 bda->ivecs[0x76].offset = OFFSET_entry_76;
45 bda->ivecs[0x17].offset = OFFSET_entry_17;
46 bda->ivecs[0x18].offset = OFFSET_entry_18;
47 bda->ivecs[0x19].offset = OFFSET_entry_19;
48 bda->ivecs[0x1c].offset = OFFSET_entry_1c;
49 bda->ivecs[0x12].offset = OFFSET_entry_12;
50 bda->ivecs[0x11].offset = OFFSET_entry_11;
51 bda->ivecs[0x15].offset = OFFSET_entry_15;
52 bda->ivecs[0x08].offset = OFFSET_entry_08;
53 bda->ivecs[0x09].offset = OFFSET_entry_09;
54 bda->ivecs[0x16].offset = OFFSET_entry_16;
55 bda->ivecs[0x14].offset = OFFSET_entry_14;
56 bda->ivecs[0x1a].offset = OFFSET_entry_1a;
57 bda->ivecs[0x70].offset = OFFSET_entry_70;
58 bda->ivecs[0x74].offset = OFFSET_entry_74;
59 bda->ivecs[0x75].offset = OFFSET_entry_75;
60 bda->ivecs[0x10].offset = OFFSET_entry_10;
66 memset(ebda, 0, sizeof(*ebda));
67 ebda->size = EBDA_SIZE;
68 bda->ebda_seg = EBDA_SEG;
69 bda->ivecs[0x41].seg = EBDA_SEG;
70 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt0);
71 bda->ivecs[0x46].seg = EBDA_SEG;
72 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt1);
78 // timer0: binary count, 16bit count, mode 2
79 outb(0x34, PORT_PIT_MODE);
80 // maximum count of 0000H = 18.2Hz
81 outb(0x0, PORT_PIT_COUNTER0);
82 outb(0x0, PORT_PIT_COUNTER0);
85 //--------------------------------------------------------------------------
87 //--------------------------------------------------------------------------
89 keyboard_panic(u16 status)
91 // If you're getting a 993 keyboard panic here,
92 // please see the comment in keyboard_init
94 BX_PANIC("Keyboard error:%u\n",status);
98 kbd_wait(u8 mask, u8 code)
101 while ( ((inb(PORT_KBD_STATUS) & mask) == 0) && (--max>0) )
102 outb(code, PORT_DIAG);
104 keyboard_panic(code);
107 static inline void kbd_flush(u8 code) {
108 kbd_wait(0x02, code);
110 static inline void kbd_waitdata(u8 code) {
111 kbd_wait(0x01, code);
114 //--------------------------------------------------------------------------
116 //--------------------------------------------------------------------------
117 // this file is based on LinuxBIOS implementation of keyboard.c
118 // could convert to #asm to gain space
122 /* ------------------- Flush buffers ------------------------*/
123 /* Wait until buffer is empty */
126 /* flush incoming keys */
129 outb(0x00, PORT_DIAG);
130 if (inb(PORT_KBD_STATUS) & 0x01) {
136 // Due to timer issues, and if the IPS setting is > 15000000,
137 // the incoming keys might not be flushed here. That will
138 // cause a panic a few lines below. See sourceforge bug report :
139 // [ 642031 ] FATAL: Keyboard RESET error:993
141 /* ------------------- controller side ----------------------*/
142 /* send cmd = 0xAA, self test 8042 */
143 outb(0xaa, PORT_KBD_STATUS);
148 /* read self-test result, 0x55 should be returned from 0x60 */
149 if ((inb(PORT_KBD_DATA) != 0x55))
152 /* send cmd = 0xAB, keyboard interface test */
153 outb(0xab, PORT_KBD_STATUS);
158 /* read keyboard interface test result, */
159 /* 0x00 should be returned form 0x60 */
160 if ((inb(PORT_KBD_DATA) != 0x00))
163 /* Enable Keyboard clock */
164 outb(0xae, PORT_KBD_STATUS);
165 outb(0xa8, PORT_KBD_STATUS);
167 /* ------------------- keyboard side ------------------------*/
168 /* reset kerboard and self test (keyboard side) */
169 outb(0xff, PORT_KBD_DATA);
174 /* keyboard should return ACK */
175 if ((inb(PORT_KBD_DATA) != 0xfa))
180 if ((inb(PORT_KBD_DATA) != 0xaa))
183 /* Disable keyboard */
184 outb(0xf5, PORT_KBD_DATA);
189 /* keyboard should return ACK */
190 if ((inb(PORT_KBD_DATA) != 0xfa))
193 /* Write Keyboard Mode */
194 outb(0x60, PORT_KBD_STATUS);
198 /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
199 outb(0x61, PORT_KBD_DATA);
203 /* Enable keyboard */
204 outb(0xf4, PORT_KBD_DATA);
209 /* keyboard should return ACK */
210 if ((inb(PORT_KBD_DATA) != 0xfa))
213 outb(0x77, PORT_DIAG);
219 bda->kbd_mode = 0x10;
220 bda->kbd_buf_head = bda->kbd_buf_tail = bda->kbd_buf_start_offset
221 = offsetof(struct bios_data_area_s, kbd_buf) - 0x400;
222 bda->kbd_buf_end_offset
223 = (offsetof(struct bios_data_area_s, kbd_buf[sizeof(bda->kbd_buf)])
227 // mov CMOS Equipment Byte to BDA Equipment Word
228 u16 eqb = bda->equipment_list_flags;
229 bda->equipment_list_flags = (eqb & 0xff00) | inb_cmos(CMOS_EQUIPMENT_INFO);
233 detect_parport(u16 port, u8 timeout, u8 count)
236 outb(inb(port+2) & 0xdf, port+2);
239 if (inb(port) != 0xaa)
242 bda->port_lpt[count] = port;
243 bda->lpt_timeout[count] = timeout;
251 count += detect_parport(0x378, 0x14, count);
252 count += detect_parport(0x278, 0x14, count);
254 // Equipment word bits 14..15 determing # parallel ports
255 u16 eqb = bda->equipment_list_flags;
256 bda->equipment_list_flags = (eqb & 0x3fff) | (count << 14);
260 detect_serial(u16 port, u8 timeout, u8 count)
263 if (inb(port+1) != 0x02)
265 if (inb(port+2) != 0x02)
268 bda->port_com[count] = port;
269 bda->com_timeout[count] = timeout;
277 count += detect_serial(0x3f8, 0x0a, count);
278 count += detect_serial(0x2f8, 0x0a, count);
279 count += detect_serial(0x3e8, 0x0a, count);
280 count += detect_serial(0x2e8, 0x0a, count);
282 // Equipment word bits 9..11 determing # serial ports
283 u16 eqb = bda->equipment_list_flags;
284 bda->equipment_list_flags = (eqb & 0xf1ff) | (count << 9);
290 return (val & 0xf) + ((val >> 4) * 10);
296 u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
297 u32 ticks = (seconds * 18206507) / 1000000;
298 u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
299 ticks += (minutes * 10923904) / 10000;
300 u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
301 ticks += (hours * 65543427) / 1000;
302 bda->timer_counter = ticks;
303 bda->timer_rollover = 0;
309 outb(0x11, PORT_PIC1);
310 outb(0x11, PORT_PIC2_DATA);
311 outb(0x08, PORT_PIC1_DATA);
312 outb(0x70, PORT_PIC2_DATA);
313 outb(0x04, PORT_PIC1_DATA);
314 outb(0x02, PORT_PIC2_DATA);
315 outb(0x01, PORT_PIC1_DATA);
316 outb(0x01, PORT_PIC2_DATA);
317 outb(0xb8, PORT_PIC1_DATA);
318 if (CONFIG_PS2_MOUSE)
319 outb(0x8f, PORT_PIC2_DATA);
321 outb(0x9f, PORT_PIC2_DATA);
327 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
333 bda->floppy_harddisk_info = out;
334 outb(0x02, PORT_DMA1_MASK_REG);
336 bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2;
343 //ebda->cdemu.active = 0;
368 // Clear out the IPL table.
369 memset(ipl, 0, sizeof(*ipl));
372 struct ipl_entry_s *ip = &ipl->table[0];
373 ip->type = IPL_TYPE_FLOPPY;
377 ip->type = IPL_TYPE_HARDDISK;
381 if (CONFIG_ELTORITO_BOOT) {
382 ip->type = IPL_TYPE_CDROM;
386 ipl->count = ip - ipl->table;
387 ipl->sequence = 0xffff;
391 callrom(u16 seg, u16 offset)
394 memset(&br, 0, sizeof(br));
396 br.di = OFFSET_pnp_string;
403 checksum(u8 *p, u32 len)
407 for (i=0; i<len; i++)
415 u8 *p = (u8*)0xc0000;
416 for (; p <= (u8*)0xe0000; p += 2048) {
418 if (*(u16*)rom != 0xaa55)
420 u32 len = rom[2] * 512;
421 if (checksum(rom, len) != 0)
423 p = (u8*)(((u32)p + len) / 2048 * 2048);
424 callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
426 // Look at the ROM's PnP Expansion header. Properly, we're supposed
427 // to init all the ROMs and then go back and build an IPL table of
428 // all the bootable devices, but we can get away with one pass.
429 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
430 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
432 // 0x1A is also the offset into the expansion header of...
433 // the Bootstrap Entry Vector, or zero if there is none.
434 u16 entry = *(u16*)&rom[0x1a+0x1a];
437 // Found a device that thinks it can boot the system. Record
438 // its BEV and product name string.
440 if (ipl->count >= ARRAY_SIZE(ipl->table))
443 struct ipl_entry_s *ip = &ipl->table[ipl->count];
444 ip->type = IPL_TYPE_BEV;
445 ip->vector = (PTR_TO_SEG(rom) << 16) | entry;
447 u16 desc = *(u16*)&rom[0x1a+0x10];
449 ip->description = (PTR_TO_SEG(rom) << 16) | desc;
456 status_restart(u8 status)
464 BX_PANIC("Unimplemented shutdown status: %02x\n",(Bit8u)status);
470 // first reset the DMA controllers
471 outb(0, PORT_DMA1_MASTER_CLEAR);
472 outb(0, PORT_DMA2_MASTER_CLEAR);
474 // then initialize the DMA controllers
475 outb(0xc0, PORT_DMA2_MODE_REG);
476 outb(0x00, PORT_DMA2_MASK_REG);
478 // Get and then clear CMOS shutdown status.
479 u8 status = inb_cmos(CMOS_RESET_CODE);
480 outb_cmos(0, CMOS_RESET_CODE);
482 if (status != 0x00 && status != 0x09 && status < 0x0d)
483 status_restart(status);
485 BX_INFO("Start bios");
497 // XXX - need to do pci stuff
502 printf("BIOS - begin\n\n");
511 callrom(0xf000, OFFSET_begin_boot);