blob: 07f0a2b90cf39d8fc8043ed8e9297e157312631c [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 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'Connor38fcbfe2008-02-25 22:30:47 -050017#define ipl ((struct ipl_s *)(IPL_SEG<<4))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050018
Kevin O'Connorbdce35f2008-02-26 21:33:14 -050019static int
20checksum(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'Connorf076a3e2008-02-25 22:25:15 -050029static void
30init_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
43static void
44init_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
73static void
74init_ebda()
75{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050076 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050077 ebda->size = EBDA_SIZE;
78 bda->ebda_seg = EBDA_SEG;
79 bda->ivecs[0x41].seg = EBDA_SEG;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050080 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081 bda->ivecs[0x46].seg = EBDA_SEG;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050082 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt1);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050083}
84
85static void
86pit_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'Connor4b60c002008-02-25 22:29:55 -050095//--------------------------------------------------------------------------
96// keyboard_panic
97//--------------------------------------------------------------------------
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050098static void
Kevin O'Connor4b60c002008-02-25 22:29:55 -050099keyboard_panic(u16 status)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500100{
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500101 // 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
107static void
108kbd_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
117static inline void kbd_flush(u8 code) {
118 kbd_wait(0x02, code);
119}
120static 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
129static void
130keyboard_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'Connorf076a3e2008-02-25 22:25:15 -0500224}
225
226static void
227kbd_setup()
228{
229 bda->kbd_mode = 0x10;
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500230 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'Connorf076a3e2008-02-25 22:25:15 -0500236
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500237 // mov CMOS Equipment Byte to BDA Equipment Word
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500238 u16 eqb = bda->equipment_list_flags;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500239 bda->equipment_list_flags = (eqb & 0xff00) | inb_cmos(CMOS_EQUIPMENT_INFO);
240}
241
242static u16
243detect_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'Connorf076a3e2008-02-25 22:25:15 -0500255}
256
257static void
258lpt_setup()
259{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500260 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
269static u16
270detect_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'Connorf076a3e2008-02-25 22:25:15 -0500281}
282
283static void
284serial_setup()
285{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500286 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'Connorf076a3e2008-02-25 22:25:15 -0500295}
296
297static u32
298bcd2bin(u8 val)
299{
300 return (val & 0xf) + ((val >> 4) * 10);
301}
302
303static void
304timer_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
316static void
317pic_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
334static void
335floppy_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
349static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500350ata_init()
351{
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500352 // 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'Connorf076a3e2008-02-25 22:25:15 -0500358}
359
360static void
361ata_detect()
362{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500363 // XXX
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500364}
365
366static void
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500367fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
368{
369 u8 type = inb_cmos(typecmos);
370 if (type != 47)
371 // XXX - halt
372 return;
373
374 info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3);
375 info->drive_control_byte = inb_cmos(basecmos+5);
376 info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6);
377 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
378 u8 heads = inb_cmos(basecmos+2);
379 u8 sectors = inb_cmos(basecmos+8);
380 if (cyl < 1024) {
381 // no logical CHS mapping used, just physical CHS
382 // use Standard Fixed Disk Parameter Table (FDPT)
383 info->cylinders = cyl;
384 info->heads = heads;
385 info->sectors = sectors;
386 return;
387 }
388
389 // complies with Phoenix style Translated Fixed Disk Parameter
390 // Table (FDPT)
391 info->phys_cylinders = cyl;
392 info->phys_heads = heads;
393 info->phys_sectors = sectors;
394 info->sectors = sectors;
395 info->a0h_signature = 0xa0;
396 if (cyl > 8192) {
397 cyl >>= 4;
398 heads <<= 4;
399 } else if (cyl > 4096) {
400 cyl >>= 3;
401 heads <<= 3;
402 } else if (cyl > 2048) {
403 cyl >>= 2;
404 heads <<= 2;
405 }
406 info->cylinders = cyl;
407 info->heads = heads;
408 info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
409}
410
411static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500412hard_drive_post()
413{
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500414 outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
415 bda->disk_count = 1;
416 bda->disk_control_byte = 0xc0;
417
418 // move disk geometry data from CMOS to EBDA disk parameter table(s)
419 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
420 if ((diskinfo & 0xf0) == 0xf0)
421 // Fill EBDA table for hard disk 0.
422 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
423 if ((diskinfo & 0x0f) == 0x0f)
424 // XXX - bochs halts on any other type
425 // Fill EBDA table for hard disk 1.
426 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500427}
428
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500429
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500430static void
431init_boot_vectors()
432{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500433 // Clear out the IPL table.
434 memset(ipl, 0, sizeof(*ipl));
435
436 // Floppy drive
437 struct ipl_entry_s *ip = &ipl->table[0];
438 ip->type = IPL_TYPE_FLOPPY;
439 ip++;
440
441 // First HDD
442 ip->type = IPL_TYPE_HARDDISK;
443 ip++;
444
445 // CDROM
446 if (CONFIG_ELTORITO_BOOT) {
447 ip->type = IPL_TYPE_CDROM;
448 ip++;
449 }
450
451 ipl->count = ip - ipl->table;
452 ipl->sequence = 0xffff;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500453}
454
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500455static void
456callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500457{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500458 struct bregs br;
459 memset(&br, 0, sizeof(br));
460 br.es = 0xf000;
461 br.di = OFFSET_pnp_string;
462 br.cs = seg;
463 br.ip = offset;
464 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500465}
466
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500467static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500468rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500469{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500470 u8 *p = (u8*)start;
471 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500472 u8 *rom = p;
473 if (*(u16*)rom != 0xaa55)
474 continue;
475 u32 len = rom[2] * 512;
476 if (checksum(rom, len) != 0)
477 continue;
478 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500479 callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500480
481 // Look at the ROM's PnP Expansion header. Properly, we're supposed
482 // to init all the ROMs and then go back and build an IPL table of
483 // all the bootable devices, but we can get away with one pass.
484 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
485 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
486 continue;
487 // 0x1A is also the offset into the expansion header of...
488 // the Bootstrap Entry Vector, or zero if there is none.
489 u16 entry = *(u16*)&rom[0x1a+0x1a];
490 if (!entry)
491 continue;
492 // Found a device that thinks it can boot the system. Record
493 // its BEV and product name string.
494
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500495 if (ipl->count >= ARRAY_SIZE(ipl->table))
496 continue;
497
498 struct ipl_entry_s *ip = &ipl->table[ipl->count];
499 ip->type = IPL_TYPE_BEV;
500 ip->vector = (PTR_TO_SEG(rom) << 16) | entry;
501
502 u16 desc = *(u16*)&rom[0x1a+0x10];
503 if (desc)
504 ip->description = (PTR_TO_SEG(rom) << 16) | desc;
505
506 ipl->count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500507 }
508}
509
510static void
511status_restart(u8 status)
512{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500513 // XXX
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500514#if 0
515 if (status == 0x05)
516 eoi_jmp_post();
517#endif
518
519 BX_PANIC("Unimplemented shutdown status: %02x\n",(Bit8u)status);
520}
521
522static void
523post()
524{
525 // first reset the DMA controllers
526 outb(0, PORT_DMA1_MASTER_CLEAR);
527 outb(0, PORT_DMA2_MASTER_CLEAR);
528
529 // then initialize the DMA controllers
530 outb(0xc0, PORT_DMA2_MODE_REG);
531 outb(0x00, PORT_DMA2_MASK_REG);
532
533 // Get and then clear CMOS shutdown status.
534 u8 status = inb_cmos(CMOS_RESET_CODE);
535 outb_cmos(0, CMOS_RESET_CODE);
536
537 if (status != 0x00 && status != 0x09 && status < 0x0d)
538 status_restart(status);
539
Kevin O'Connor4ce6a492008-02-29 00:21:27 -0500540 BX_INFO("Start bios\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500541
542 init_bda();
543 init_handlers();
544 init_ebda();
545
546 pit_setup();
547 kbd_setup();
548 lpt_setup();
549 serial_setup();
550 timer_setup();
551 pic_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500552
553 rom_scan(0xc0000, 0xc7800);
554
555 printf("BIOS - begin\n\n");
556
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500557 // XXX - need to do pci stuff
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500558 //pci_setup();
559 init_boot_vectors();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500560
561 floppy_drive_post();
562 hard_drive_post();
563 if (CONFIG_ATA) {
564 ata_init();
565 ata_detect();
566 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500567
568 init_boot_vectors();
569 rom_scan(0xc8000, 0xe0000);
570
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500571 callrom(0xf000, OFFSET_begin_boot);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500572}
573
574void VISIBLE
575_start()
576{
577 post();
578}