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;
67 // set vector 0x79 to zero
68 // this is used by 'gardian angel' protection system
69 bda->ivecs[0x79].seg = 0;
70 bda->ivecs[0x79].offset = 0;
72 bda->ivecs[0x40].offset = OFFSET_entry_40;
73 bda->ivecs[0x0e].offset = OFFSET_entry_0e;
74 bda->ivecs[0x13].offset = OFFSET_entry_13;
75 bda->ivecs[0x76].offset = OFFSET_entry_76;
76 bda->ivecs[0x17].offset = OFFSET_entry_17;
77 bda->ivecs[0x18].offset = OFFSET_entry_18;
78 bda->ivecs[0x19].offset = OFFSET_entry_19;
79 bda->ivecs[0x1c].offset = OFFSET_entry_1c;
80 bda->ivecs[0x12].offset = OFFSET_entry_12;
81 bda->ivecs[0x11].offset = OFFSET_entry_11;
82 bda->ivecs[0x15].offset = OFFSET_entry_15;
83 bda->ivecs[0x08].offset = OFFSET_entry_08;
84 bda->ivecs[0x09].offset = OFFSET_entry_09;
85 bda->ivecs[0x16].offset = OFFSET_entry_16;
86 bda->ivecs[0x14].offset = OFFSET_entry_14;
87 bda->ivecs[0x1a].offset = OFFSET_entry_1a;
88 bda->ivecs[0x70].offset = OFFSET_entry_70;
89 bda->ivecs[0x74].offset = OFFSET_entry_74;
90 bda->ivecs[0x75].offset = OFFSET_entry_75;
91 bda->ivecs[0x10].offset = OFFSET_entry_10;
97 memset(ebda, 0, sizeof(*ebda));
98 ebda->size = EBDA_SIZE;
99 bda->ebda_seg = EBDA_SEG;
100 bda->ivecs[0x41].seg = EBDA_SEG;
101 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt0);
102 bda->ivecs[0x46].seg = EBDA_SEG;
103 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt1);
109 // timer0: binary count, 16bit count, mode 2
110 outb(0x34, PORT_PIT_MODE);
111 // maximum count of 0000H = 18.2Hz
112 outb(0x0, PORT_PIT_COUNTER0);
113 outb(0x0, PORT_PIT_COUNTER0);
116 //--------------------------------------------------------------------------
118 //--------------------------------------------------------------------------
120 keyboard_panic(u16 status)
122 // If you're getting a 993 keyboard panic here,
123 // please see the comment in keyboard_init
125 BX_PANIC("Keyboard error:%u\n",status);
129 kbd_wait(u8 mask, u8 code)
132 while ( ((inb(PORT_PS2_STATUS) & mask) == 0) && (--max>0) )
133 outb(code, PORT_DIAG);
135 keyboard_panic(code);
138 static inline void kbd_flush(u8 code) {
139 kbd_wait(0x02, code);
141 static inline void kbd_waitdata(u8 code) {
142 kbd_wait(0x01, code);
145 //--------------------------------------------------------------------------
147 //--------------------------------------------------------------------------
148 // this file is based on LinuxBIOS implementation of keyboard.c
149 // could convert to #asm to gain space
153 /* ------------------- Flush buffers ------------------------*/
154 /* Wait until buffer is empty */
157 /* flush incoming keys */
160 outb(0x00, PORT_DIAG);
161 if (inb(PORT_PS2_STATUS) & 0x01) {
167 // Due to timer issues, and if the IPS setting is > 15000000,
168 // the incoming keys might not be flushed here. That will
169 // cause a panic a few lines below. See sourceforge bug report :
170 // [ 642031 ] FATAL: Keyboard RESET error:993
172 /* ------------------- controller side ----------------------*/
173 /* send cmd = 0xAA, self test 8042 */
174 outb(0xaa, PORT_PS2_STATUS);
179 /* read self-test result, 0x55 should be returned from 0x60 */
180 if (inb(PORT_PS2_DATA) != 0x55)
183 /* send cmd = 0xAB, keyboard interface test */
184 outb(0xab, PORT_PS2_STATUS);
189 /* read keyboard interface test result, */
190 /* 0x00 should be returned form 0x60 */
191 if (inb(PORT_PS2_DATA) != 0x00)
194 /* Enable Keyboard clock */
195 outb(0xae, PORT_PS2_STATUS);
196 outb(0xa8, PORT_PS2_STATUS);
198 /* ------------------- keyboard side ------------------------*/
199 /* reset kerboard and self test (keyboard side) */
200 outb(0xff, PORT_PS2_DATA);
205 /* keyboard should return ACK */
206 if (inb(PORT_PS2_DATA) != 0xfa)
211 if (inb(PORT_PS2_DATA) != 0xaa)
214 /* Disable keyboard */
215 outb(0xf5, PORT_PS2_DATA);
220 /* keyboard should return ACK */
221 if (inb(PORT_PS2_DATA) != 0xfa)
224 /* Write Keyboard Mode */
225 outb(0x60, PORT_PS2_STATUS);
229 /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
230 outb(0x61, PORT_PS2_DATA);
234 /* Enable keyboard */
235 outb(0xf4, PORT_PS2_DATA);
240 /* keyboard should return ACK */
241 if (inb(PORT_PS2_DATA) != 0xfa)
244 outb(0x77, PORT_DIAG);
250 bda->kbd_mode = 0x10;
251 bda->kbd_buf_head = bda->kbd_buf_tail = bda->kbd_buf_start_offset
252 = offsetof(struct bios_data_area_s, kbd_buf) - 0x400;
253 bda->kbd_buf_end_offset
254 = (offsetof(struct bios_data_area_s, kbd_buf[sizeof(bda->kbd_buf)])
258 // mov CMOS Equipment Byte to BDA Equipment Word
259 u16 eqb = bda->equipment_list_flags;
260 bda->equipment_list_flags = (eqb & 0xff00) | inb_cmos(CMOS_EQUIPMENT_INFO);
264 detect_parport(u16 port, u8 timeout, u8 count)
267 outb(inb(port+2) & 0xdf, port+2);
270 if (inb(port) != 0xaa)
273 bda->port_lpt[count] = port;
274 bda->lpt_timeout[count] = timeout;
282 count += detect_parport(0x378, 0x14, count);
283 count += detect_parport(0x278, 0x14, count);
285 // Equipment word bits 14..15 determing # parallel ports
286 u16 eqb = bda->equipment_list_flags;
287 bda->equipment_list_flags = (eqb & 0x3fff) | (count << 14);
291 detect_serial(u16 port, u8 timeout, u8 count)
294 if (inb(port+1) != 0x02)
296 if (inb(port+2) != 0x02)
299 bda->port_com[count] = port;
300 bda->com_timeout[count] = timeout;
308 count += detect_serial(0x3f8, 0x0a, count);
309 count += detect_serial(0x2f8, 0x0a, count);
310 count += detect_serial(0x3e8, 0x0a, count);
311 count += detect_serial(0x2e8, 0x0a, count);
313 // Equipment word bits 9..11 determing # serial ports
314 u16 eqb = bda->equipment_list_flags;
315 bda->equipment_list_flags = (eqb & 0xf1ff) | (count << 9);
321 return (val & 0xf) + ((val >> 4) * 10);
327 u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
328 u32 ticks = (seconds * 18206507) / 1000000;
329 u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
330 ticks += (minutes * 10923904) / 10000;
331 u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
332 ticks += (hours * 65543427) / 1000;
333 bda->timer_counter = ticks;
334 bda->timer_rollover = 0;
340 outb(0x11, PORT_PIC1);
341 outb(0x11, PORT_PIC2);
342 outb(0x08, PORT_PIC1_DATA);
343 outb(0x70, PORT_PIC2_DATA);
344 outb(0x04, PORT_PIC1_DATA);
345 outb(0x02, PORT_PIC2_DATA);
346 outb(0x01, PORT_PIC1_DATA);
347 outb(0x01, PORT_PIC2_DATA);
348 outb(0xb8, PORT_PIC1_DATA);
349 if (CONFIG_PS2_MOUSE)
350 outb(0x8f, PORT_PIC2_DATA);
352 outb(0x9f, PORT_PIC2_DATA);
358 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
364 bda->floppy_harddisk_info = out;
365 outb(0x02, PORT_DMA1_MASK_REG);
367 bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2;
373 // hdidmap and cdidmap init.
375 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
376 ebda->ata.idmap[0][device] = CONFIG_MAX_ATA_DEVICES;
377 ebda->ata.idmap[1][device] = CONFIG_MAX_ATA_DEVICES;
382 fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
384 u8 type = inb_cmos(typecmos);
389 info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3);
390 info->drive_control_byte = inb_cmos(basecmos+5);
391 info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6);
392 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
393 u8 heads = inb_cmos(basecmos+2);
394 u8 sectors = inb_cmos(basecmos+8);
396 // no logical CHS mapping used, just physical CHS
397 // use Standard Fixed Disk Parameter Table (FDPT)
398 info->cylinders = cyl;
400 info->sectors = sectors;
404 // complies with Phoenix style Translated Fixed Disk Parameter
406 info->phys_cylinders = cyl;
407 info->phys_heads = heads;
408 info->phys_sectors = sectors;
409 info->sectors = sectors;
410 info->a0h_signature = 0xa0;
414 } else if (cyl > 4096) {
417 } else if (cyl > 2048) {
421 info->cylinders = cyl;
423 info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
429 outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
431 bda->disk_control_byte = 0xc0;
433 // move disk geometry data from CMOS to EBDA disk parameter table(s)
434 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
435 if ((diskinfo & 0xf0) == 0xf0)
436 // Fill EBDA table for hard disk 0.
437 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
438 if ((diskinfo & 0x0f) == 0x0f)
439 // XXX - bochs halts on any other type
440 // Fill EBDA table for hard disk 1.
441 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
448 // Clear out the IPL table.
449 memset(ipl, 0, sizeof(*ipl));
452 struct ipl_entry_s *ip = &ipl->table[0];
453 ip->type = IPL_TYPE_FLOPPY;
457 ip->type = IPL_TYPE_HARDDISK;
461 if (CONFIG_CDROM_BOOT) {
462 ip->type = IPL_TYPE_CDROM;
466 ipl->count = ip - ipl->table;
467 ipl->sequence = 0xffff;
471 callrom(u16 seg, u16 offset)
474 memset(&br, 0, sizeof(br));
476 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
483 rom_scan(u32 start, u32 end)
486 for (; p <= (u8*)end; p += 2048) {
488 if (*(u16*)rom != 0xaa55)
490 u32 len = rom[2] * 512;
491 if (checksum(rom, len) != 0)
493 p = (u8*)(((u32)p + len) / 2048 * 2048);
494 callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
496 // Look at the ROM's PnP Expansion header. Properly, we're supposed
497 // to init all the ROMs and then go back and build an IPL table of
498 // all the bootable devices, but we can get away with one pass.
499 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
500 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
502 // 0x1A is also the offset into the expansion header of...
503 // the Bootstrap Entry Vector, or zero if there is none.
504 u16 entry = *(u16*)&rom[0x1a+0x1a];
507 // Found a device that thinks it can boot the system. Record
508 // its BEV and product name string.
510 if (ipl->count >= ARRAY_SIZE(ipl->table))
513 struct ipl_entry_s *ip = &ipl->table[ipl->count];
514 ip->type = IPL_TYPE_BEV;
515 ip->vector = (PTR_TO_SEG(rom) << 16) | entry;
517 u16 desc = *(u16*)&rom[0x1a+0x10];
519 ip->description = (PTR_TO_SEG(rom) << 16) | desc;
528 if (inb_cmos(0x34) | inb_cmos(0x35))
529 ram_size = (inb_cmos(0x34) | (inb_cmos(0x35) << 8)) * 65536 +
532 ram_size = (inb_cmos(0x17) | (inb_cmos(0x18) << 8)) * 1024;
533 #ifdef CONFIG_USE_EBDA_TABLES
534 ebda_cur_addr = ((*(u16 *)(0x40e)) << 4) + 0x380;
537 BX_INFO("ram_size=0x%08lx\n", ram_size);
543 u32 eax, ebx, ecx, edx;
545 cpuid(1, eax, ebx, ecx, edx);
546 cpuid_signature = eax;
547 cpuid_features = edx;
548 cpuid_ext_features = ecx;
554 BX_INFO("Start bios\n");
567 rom_scan(0xc0000, 0xc7800);
569 printf("BIOS - begin\n\n");
571 // XXX - need to do pci stuff
581 rom_scan(0xc8000, 0xe0000);
588 if (bios_table_cur_addr != 0) {
596 bios_lock_shadow_ram();
598 BX_INFO("bios_table_cur_addr: 0x%08lx\n", bios_table_cur_addr);
599 if (bios_table_cur_addr > bios_table_end_addr)
600 BX_PANIC("bios_table_end_addr overflow!\n");
603 callrom(SEG_BIOS, OFFSET_begin_boot);
609 // first reset the DMA controllers
610 outb(0, PORT_DMA1_MASTER_CLEAR);
611 outb(0, PORT_DMA2_MASTER_CLEAR);
613 // then initialize the DMA controllers
614 outb(0xc0, PORT_DMA2_MODE_REG);
615 outb(0x00, PORT_DMA2_MASK_REG);
619 check_restart_status()
621 // Get and then clear CMOS shutdown status.
622 u8 status = inb_cmos(CMOS_RESET_CODE);
623 outb_cmos(0, CMOS_RESET_CODE);
625 if (status == 0x00 || status == 0x09 || status >= 0x0d)
629 if (status != 0x05) {
630 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
634 // XXX - this is supposed to jump without changing any memory -
635 // but the stack has been altered by the time the code gets here.
638 memset(&br, 0, sizeof(br));
639 br.cs = bda->jump_cs_ip >> 16;
640 br.ip = bda->jump_cs_ip;
648 check_restart_status();