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
22 u32 cpuid_ext_features;
23 unsigned long ram_size;
24 unsigned long bios_table_cur_addr;
25 unsigned long bios_table_end_addr;
27 #ifdef CONFIG_USE_EBDA_TABLES
28 unsigned long ebda_cur_addr;
31 #define cpuid(index, eax, ebx, ecx, edx) \
32 asm volatile ("cpuid" \
33 : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) \
36 #define bda ((struct bios_data_area_s *)0)
37 #define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4))
38 #define ipl ((struct ipl_s *)(IPL_SEG<<4))
41 checksum(u8 *p, u32 len)
53 memset(bda, 0, sizeof(*bda));
56 for (i=0; i<256; i++) {
57 bda->ivecs[i].seg = SEG_BIOS;
58 bda->ivecs[i].offset = OFFSET_dummy_iret_handler;
61 bda->mem_size_kb = BASE_MEM_IN_K;
63 // mov CMOS Equipment Byte to BDA Equipment Word
64 bda->equipment_list_flags = inb_cmos(CMOS_EQUIPMENT_INFO);
70 // set vector 0x79 to zero
71 // this is used by 'gardian angel' protection system
72 bda->ivecs[0x79].seg = 0;
73 bda->ivecs[0x79].offset = 0;
75 bda->ivecs[0x40].offset = OFFSET_entry_40;
76 bda->ivecs[0x0e].offset = OFFSET_entry_0e;
77 bda->ivecs[0x13].offset = OFFSET_entry_13;
78 bda->ivecs[0x76].offset = OFFSET_entry_76;
79 bda->ivecs[0x17].offset = OFFSET_entry_17;
80 bda->ivecs[0x18].offset = OFFSET_entry_18;
81 bda->ivecs[0x19].offset = OFFSET_entry_19;
82 bda->ivecs[0x1c].offset = OFFSET_entry_1c;
83 bda->ivecs[0x12].offset = OFFSET_entry_12;
84 bda->ivecs[0x11].offset = OFFSET_entry_11;
85 bda->ivecs[0x15].offset = OFFSET_entry_15;
86 bda->ivecs[0x08].offset = OFFSET_entry_08;
87 bda->ivecs[0x09].offset = OFFSET_entry_09;
88 bda->ivecs[0x16].offset = OFFSET_entry_16;
89 bda->ivecs[0x14].offset = OFFSET_entry_14;
90 bda->ivecs[0x1a].offset = OFFSET_entry_1a;
91 bda->ivecs[0x70].offset = OFFSET_entry_70;
92 bda->ivecs[0x74].offset = OFFSET_entry_74;
93 bda->ivecs[0x75].offset = OFFSET_entry_75;
94 bda->ivecs[0x10].offset = OFFSET_entry_10;
100 memset(ebda, 0, sizeof(*ebda));
101 ebda->size = EBDA_SIZE;
102 bda->ebda_seg = EBDA_SEG;
103 bda->ivecs[0x41].seg = EBDA_SEG;
104 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt0);
105 bda->ivecs[0x46].seg = EBDA_SEG;
106 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt1);
112 // timer0: binary count, 16bit count, mode 2
113 outb(0x34, PORT_PIT_MODE);
114 // maximum count of 0000H = 18.2Hz
115 outb(0x0, PORT_PIT_COUNTER0);
116 outb(0x0, PORT_PIT_COUNTER0);
119 //--------------------------------------------------------------------------
121 //--------------------------------------------------------------------------
123 keyboard_panic(u16 status)
125 // If you're getting a 993 keyboard panic here,
126 // please see the comment in keyboard_init
128 BX_PANIC("Keyboard error:%u\n",status);
135 while ((inb(PORT_PS2_STATUS) & 0x02) && (--max > 0))
136 outb(code, PORT_DIAG);
137 if (!max && code != 0xff)
138 keyboard_panic(code);
142 kbd_waitdata(u8 code)
145 while ( ((inb(PORT_PS2_STATUS) & 0x01) == 0) && (--max>0) )
146 outb(code, PORT_DIAG);
148 keyboard_panic(code);
151 //--------------------------------------------------------------------------
153 //--------------------------------------------------------------------------
154 // this file is based on LinuxBIOS implementation of keyboard.c
155 // could convert to #asm to gain space
159 /* ------------------- Flush buffers ------------------------*/
160 /* Wait until buffer is empty */
163 /* flush incoming keys */
166 outb(0x00, PORT_DIAG);
167 if (inb(PORT_PS2_STATUS) & 0x01) {
173 // Due to timer issues, and if the IPS setting is > 15000000,
174 // the incoming keys might not be flushed here. That will
175 // cause a panic a few lines below. See sourceforge bug report :
176 // [ 642031 ] FATAL: Keyboard RESET error:993
178 /* ------------------- controller side ----------------------*/
179 /* send cmd = 0xAA, self test 8042 */
180 outb(0xaa, PORT_PS2_STATUS);
185 /* read self-test result, 0x55 should be returned from 0x60 */
186 if (inb(PORT_PS2_DATA) != 0x55)
189 /* send cmd = 0xAB, keyboard interface test */
190 outb(0xab, PORT_PS2_STATUS);
195 /* read keyboard interface test result, */
196 /* 0x00 should be returned form 0x60 */
197 if (inb(PORT_PS2_DATA) != 0x00)
200 /* Enable Keyboard clock */
201 outb(0xae, PORT_PS2_STATUS);
202 outb(0xa8, PORT_PS2_STATUS);
204 /* ------------------- keyboard side ------------------------*/
205 /* reset kerboard and self test (keyboard side) */
206 outb(0xff, PORT_PS2_DATA);
211 /* keyboard should return ACK */
212 if (inb(PORT_PS2_DATA) != 0xfa)
217 if (inb(PORT_PS2_DATA) != 0xaa)
220 /* Disable keyboard */
221 outb(0xf5, PORT_PS2_DATA);
226 /* keyboard should return ACK */
227 if (inb(PORT_PS2_DATA) != 0xfa)
230 /* Write Keyboard Mode */
231 outb(0x60, PORT_PS2_STATUS);
235 /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
236 outb(0x61, PORT_PS2_DATA);
240 /* Enable keyboard */
241 outb(0xf4, PORT_PS2_DATA);
246 /* keyboard should return ACK */
247 if (inb(PORT_PS2_DATA) != 0xfa)
250 outb(0x77, PORT_DIAG);
256 bda->kbd_mode = 0x10;
257 bda->kbd_buf_head = bda->kbd_buf_tail = bda->kbd_buf_start_offset
258 = offsetof(struct bios_data_area_s, kbd_buf) - 0x400;
259 bda->kbd_buf_end_offset
260 = (offsetof(struct bios_data_area_s, kbd_buf[sizeof(bda->kbd_buf)])
266 detect_parport(u16 port, u8 timeout, u8 count)
269 outb(inb(port+2) & 0xdf, port+2);
272 if (inb(port) != 0xaa)
275 bda->port_lpt[count] = port;
276 bda->lpt_timeout[count] = timeout;
284 count += detect_parport(0x378, 0x14, count);
285 count += detect_parport(0x278, 0x14, count);
287 // Equipment word bits 14..15 determing # parallel ports
288 u16 eqb = bda->equipment_list_flags;
289 bda->equipment_list_flags = (eqb & 0x3fff) | (count << 14);
293 detect_serial(u16 port, u8 timeout, u8 count)
296 if (inb(port+1) != 0x02)
298 if (inb(port+2) != 0x02)
301 bda->port_com[count] = port;
302 bda->com_timeout[count] = timeout;
310 count += detect_serial(0x3f8, 0x0a, count);
311 count += detect_serial(0x2f8, 0x0a, count);
312 count += detect_serial(0x3e8, 0x0a, count);
313 count += detect_serial(0x2e8, 0x0a, count);
315 // Equipment word bits 9..11 determing # serial ports
316 u16 eqb = bda->equipment_list_flags;
317 bda->equipment_list_flags = (eqb & 0xf1ff) | (count << 9);
323 return (val & 0xf) + ((val >> 4) * 10);
329 u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
330 u32 ticks = (seconds * 18206507) / 1000000;
331 u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
332 ticks += (minutes * 10923904) / 10000;
333 u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
334 ticks += (hours * 65543427) / 1000;
335 bda->timer_counter = ticks;
336 bda->timer_rollover = 0;
342 outb(0x11, PORT_PIC1);
343 outb(0x11, PORT_PIC2);
344 outb(0x08, PORT_PIC1_DATA);
345 outb(0x70, PORT_PIC2_DATA);
346 outb(0x04, PORT_PIC1_DATA);
347 outb(0x02, PORT_PIC2_DATA);
348 outb(0x01, PORT_PIC1_DATA);
349 outb(0x01, PORT_PIC2_DATA);
350 outb(0xb8, PORT_PIC1_DATA);
351 if (CONFIG_PS2_MOUSE)
352 outb(0x8f, PORT_PIC2_DATA);
354 outb(0x9f, PORT_PIC2_DATA);
360 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
366 bda->floppy_harddisk_info = out;
367 outb(0x02, PORT_DMA1_MASK_REG);
369 bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2;
375 // hdidmap and cdidmap init.
377 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
378 ebda->ata.idmap[0][device] = CONFIG_MAX_ATA_DEVICES;
379 ebda->ata.idmap[1][device] = CONFIG_MAX_ATA_DEVICES;
384 fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
386 u8 type = inb_cmos(typecmos);
391 info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3);
392 info->drive_control_byte = inb_cmos(basecmos+5);
393 info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6);
394 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
395 u8 heads = inb_cmos(basecmos+2);
396 u8 sectors = inb_cmos(basecmos+8);
398 // no logical CHS mapping used, just physical CHS
399 // use Standard Fixed Disk Parameter Table (FDPT)
400 info->cylinders = cyl;
402 info->sectors = sectors;
406 // complies with Phoenix style Translated Fixed Disk Parameter
408 info->phys_cylinders = cyl;
409 info->phys_heads = heads;
410 info->phys_sectors = sectors;
411 info->sectors = sectors;
412 info->a0h_signature = 0xa0;
416 } else if (cyl > 4096) {
419 } else if (cyl > 2048) {
423 info->cylinders = cyl;
425 info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
431 outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
433 bda->disk_control_byte = 0xc0;
435 // move disk geometry data from CMOS to EBDA disk parameter table(s)
436 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
437 if ((diskinfo & 0xf0) == 0xf0)
438 // Fill EBDA table for hard disk 0.
439 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
440 if ((diskinfo & 0x0f) == 0x0f)
441 // XXX - bochs halts on any other type
442 // Fill EBDA table for hard disk 1.
443 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
450 // Clear out the IPL table.
451 memset(ipl, 0, sizeof(*ipl));
454 struct ipl_entry_s *ip = &ipl->table[0];
455 ip->type = IPL_TYPE_FLOPPY;
459 ip->type = IPL_TYPE_HARDDISK;
463 if (CONFIG_CDROM_BOOT) {
464 ip->type = IPL_TYPE_CDROM;
468 ipl->count = ip - ipl->table;
469 ipl->sequence = 0xffff;
473 callrom(u16 seg, u16 offset)
476 memset(&br, 0, sizeof(br));
478 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
485 rom_scan(u32 start, u32 end)
488 for (; p <= (u8*)end; p += 2048) {
490 if (*(u16*)rom != 0xaa55)
492 u32 len = rom[2] * 512;
493 if (checksum(rom, len) != 0)
495 p = (u8*)(((u32)p + len) / 2048 * 2048);
496 callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
498 // Look at the ROM's PnP Expansion header. Properly, we're supposed
499 // to init all the ROMs and then go back and build an IPL table of
500 // all the bootable devices, but we can get away with one pass.
501 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
502 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
504 // 0x1A is also the offset into the expansion header of...
505 // the Bootstrap Entry Vector, or zero if there is none.
506 u16 entry = *(u16*)&rom[0x1a+0x1a];
509 // Found a device that thinks it can boot the system. Record
510 // its BEV and product name string.
512 if (ipl->count >= ARRAY_SIZE(ipl->table))
515 struct ipl_entry_s *ip = &ipl->table[ipl->count];
516 ip->type = IPL_TYPE_BEV;
517 ip->vector = (PTR_TO_SEG(rom) << 16) | entry;
519 u16 desc = *(u16*)&rom[0x1a+0x10];
521 ip->description = (PTR_TO_SEG(rom) << 16) | desc;
530 if (inb_cmos(CMOS_MEM_EXTMEM2_LOW) | inb_cmos(CMOS_MEM_EXTMEM2_HIGH))
531 ram_size = (inb_cmos(CMOS_MEM_EXTMEM2_LOW) | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) *
532 64 * 1024 + 16 * 1024 * 1024;
534 ram_size = (inb_cmos(CMOS_MEM_BASE_LOW) | (inb_cmos(CMOS_MEM_BASE_HIGH) << 8)) * 1024;
535 #ifdef CONFIG_USE_EBDA_TABLES
536 ebda_cur_addr = ((*(u16 *)(0x40e)) << 4) + 0x380;
539 BX_INFO("ram_size=0x%08lx\n", ram_size);
545 u32 eax, ebx, ecx, edx;
547 cpuid(1, eax, ebx, ecx, edx);
548 cpuid_signature = eax;
549 cpuid_features = edx;
550 cpuid_ext_features = ecx;
556 BX_INFO("Start bios\n");
569 rom_scan(0xc0000, 0xc7800);
571 printf("BIOS - begin\n\n");
573 // XXX - need to do pci stuff
583 rom_scan(0xc8000, 0xe0000);
590 if (bios_table_cur_addr != 0) {
598 bios_lock_shadow_ram();
600 BX_INFO("bios_table_cur_addr: 0x%08lx\n", bios_table_cur_addr);
601 if (bios_table_cur_addr > bios_table_end_addr)
602 BX_PANIC("bios_table_end_addr overflow!\n");
605 callrom(SEG_BIOS, OFFSET_begin_boot);
611 // first reset the DMA controllers
612 outb(0, PORT_DMA1_MASTER_CLEAR);
613 outb(0, PORT_DMA2_MASTER_CLEAR);
615 // then initialize the DMA controllers
616 outb(0xc0, PORT_DMA2_MODE_REG);
617 outb(0x00, PORT_DMA2_MASK_REG);
621 check_restart_status()
623 // Get and then clear CMOS shutdown status.
624 u8 status = inb_cmos(CMOS_RESET_CODE);
625 outb_cmos(0, CMOS_RESET_CODE);
627 if (status == 0x00 || status == 0x09 || status >= 0x0d)
631 if (status != 0x05) {
632 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
636 // XXX - this is supposed to jump without changing any memory -
637 // but the stack has been altered by the time the code gets here.
640 memset(&br, 0, sizeof(br));
641 br.cs = bda->jump_cs_ip >> 16;
642 br.ip = bda->jump_cs_ip;
650 check_restart_status();