Bug fixes; get mouse working.
[seabios.git] / src / post.c
1 // 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))
17 #define ipl ((struct ipl_s *)(IPL_SEG<<4))
18
19 static u8
20 checksum(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
29 static void
30 init_bda()
31 {
32     memset(bda, 0, sizeof(*bda));
33
34     int i;
35     for (i=0; i<256; i++) {
36         bda->ivecs[i].seg = SEG_BIOS;
37         bda->ivecs[i].offset = OFFSET_dummy_iret_handler;
38     }
39
40     bda->mem_size_kb = BASE_MEM_IN_K;
41 }
42
43 static void
44 init_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
73 static void
74 init_ebda()
75 {
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);
83 }
84
85 static void
86 pit_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
95 //--------------------------------------------------------------------------
96 // keyboard_panic
97 //--------------------------------------------------------------------------
98 static void
99 keyboard_panic(u16 status)
100 {
101   // 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
107 static void
108 kbd_wait(u8 mask, u8 code)
109 {
110     u16 max = 0xffff;
111     while ( ((inb(PORT_PS2_STATUS) & mask) == 0) && (--max>0) )
112         outb(code, PORT_DIAG);
113     if (!max)
114         keyboard_panic(code);
115 }
116
117 static inline void kbd_flush(u8 code) {
118     kbd_wait(0x02, code);
119 }
120 static 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
129 static void
130 keyboard_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_PS2_STATUS) & 0x01) {
141             inb(PORT_PS2_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_PS2_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_PS2_DATA) != 0x55)
160         keyboard_panic(991);
161
162     /* send cmd = 0xAB, keyboard interface test */
163     outb(0xab, PORT_PS2_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_PS2_DATA) != 0x00)
171         keyboard_panic(992);
172
173     /* Enable Keyboard clock */
174     outb(0xae, PORT_PS2_STATUS);
175     outb(0xa8, PORT_PS2_STATUS);
176
177     /* ------------------- keyboard side ------------------------*/
178     /* reset kerboard and self test  (keyboard side) */
179     outb(0xff, PORT_PS2_DATA);
180
181     kbd_flush(0x20);
182     kbd_waitdata(0x21);
183
184     /* keyboard should return ACK */
185     if (inb(PORT_PS2_DATA) != 0xfa)
186         keyboard_panic(993);
187
188     kbd_waitdata(0x31);
189
190     if (inb(PORT_PS2_DATA) != 0xaa)
191         keyboard_panic(994);
192
193     /* Disable keyboard */
194     outb(0xf5, PORT_PS2_DATA);
195
196     kbd_flush(0x40);
197     kbd_waitdata(0x41);
198
199     /* keyboard should return ACK */
200     if (inb(PORT_PS2_DATA) != 0xfa)
201         keyboard_panic(995);
202
203     /* Write Keyboard Mode */
204     outb(0x60, PORT_PS2_STATUS);
205
206     kbd_flush(0x50);
207
208     /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
209     outb(0x61, PORT_PS2_DATA);
210
211     kbd_flush(0x60);
212
213     /* Enable keyboard */
214     outb(0xf4, PORT_PS2_DATA);
215
216     kbd_flush(0x70);
217     kbd_waitdata(0x71);
218
219     /* keyboard should return ACK */
220     if (inb(PORT_PS2_DATA) != 0xfa)
221         keyboard_panic(996);
222
223     outb(0x77, PORT_DIAG);
224 }
225
226 static void
227 kbd_setup()
228 {
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)])
234            - 0x400);
235     keyboard_init();
236
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);
240 }
241
242 static u16
243 detect_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;
255 }
256
257 static void
258 lpt_setup()
259 {
260     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
269 static u16
270 detect_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;
281 }
282
283 static void
284 serial_setup()
285 {
286     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);
295 }
296
297 static u32
298 bcd2bin(u8 val)
299 {
300     return (val & 0xf) + ((val >> 4) * 10);
301 }
302
303 static void
304 timer_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
316 static void
317 pic_setup()
318 {
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);
330     else
331         outb(0x9f, PORT_PIC2_DATA);
332 }
333
334 static void
335 floppy_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
349 static void
350 ata_init()
351 {
352     // 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     }
358 }
359
360 static void
361 fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
362 {
363     u8 type = inb_cmos(typecmos);
364     if (type != 47)
365         // XXX - halt
366         return;
367
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);
374     if (cyl < 1024) {
375         // no logical CHS mapping used, just physical CHS
376         // use Standard Fixed Disk Parameter Table (FDPT)
377         info->cylinders = cyl;
378         info->heads = heads;
379         info->sectors = sectors;
380         return;
381     }
382
383     // complies with Phoenix style Translated Fixed Disk Parameter
384     // Table (FDPT)
385     info->phys_cylinders = cyl;
386     info->phys_heads = heads;
387     info->phys_sectors = sectors;
388     info->sectors = sectors;
389     info->a0h_signature = 0xa0;
390     if (cyl > 8192) {
391         cyl >>= 4;
392         heads <<= 4;
393     } else if (cyl > 4096) {
394         cyl >>= 3;
395         heads <<= 3;
396     } else if (cyl > 2048) {
397         cyl >>= 2;
398         heads <<= 2;
399     }
400     info->cylinders = cyl;
401     info->heads = heads;
402     info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
403 }
404
405 static void
406 hard_drive_post()
407 {
408     outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
409     bda->disk_count = 1;
410     bda->disk_control_byte = 0xc0;
411
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);
421 }
422
423
424 static void
425 init_boot_vectors()
426 {
427     // Clear out the IPL table.
428     memset(ipl, 0, sizeof(*ipl));
429
430     // Floppy drive
431     struct ipl_entry_s *ip = &ipl->table[0];
432     ip->type = IPL_TYPE_FLOPPY;
433     ip++;
434
435     // First HDD
436     ip->type = IPL_TYPE_HARDDISK;
437     ip++;
438
439     // CDROM
440     if (CONFIG_ELTORITO_BOOT) {
441         ip->type = IPL_TYPE_CDROM;
442         ip++;
443     }
444
445     ipl->count = ip - ipl->table;
446     ipl->sequence = 0xffff;
447 }
448
449 static void
450 callrom(u16 seg, u16 offset)
451 {
452     struct bregs br;
453     memset(&br, 0, sizeof(br));
454     br.es = SEG_BIOS;
455     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
456     br.cs = seg;
457     br.ip = offset;
458     call16(&br);
459 }
460
461 static void
462 rom_scan(u32 start, u32 end)
463 {
464     u8 *p = (u8*)start;
465     for (; p <= (u8*)end; p += 2048) {
466         u8 *rom = p;
467         if (*(u16*)rom != 0xaa55)
468             continue;
469         u32 len = rom[2] * 512;
470         if (checksum(rom, len) != 0)
471             continue;
472         p = (u8*)(((u32)p + len) / 2048 * 2048);
473         callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
474
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')
480             continue;
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];
484         if (!entry)
485             continue;
486         // Found a device that thinks it can boot the system.  Record
487         // its BEV and product name string.
488
489         if (ipl->count >= ARRAY_SIZE(ipl->table))
490             continue;
491
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;
495
496         u16 desc = *(u16*)&rom[0x1a+0x10];
497         if (desc)
498             ip->description = (PTR_TO_SEG(rom) << 16) | desc;
499
500         ipl->count++;
501     }
502 }
503
504 static void
505 post()
506 {
507     BX_INFO("Start bios\n");
508
509     init_bda();
510     init_handlers();
511     init_ebda();
512
513     pit_setup();
514     kbd_setup();
515     lpt_setup();
516     serial_setup();
517     timer_setup();
518     pic_setup();
519
520     rom_scan(0xc0000, 0xc7800);
521
522     printf("BIOS - begin\n\n");
523
524     // XXX - need to do pci stuff
525     //pci_setup();
526     init_boot_vectors();
527
528     floppy_drive_post();
529     hard_drive_post();
530     if (CONFIG_ATA)
531         ata_init();
532
533     init_boot_vectors();
534     rom_scan(0xc8000, 0xe0000);
535
536     callrom(SEG_BIOS, OFFSET_begin_boot);
537 }
538
539 static void
540 init_dma()
541 {
542     // first reset the DMA controllers
543     outb(0, PORT_DMA1_MASTER_CLEAR);
544     outb(0, PORT_DMA2_MASTER_CLEAR);
545
546     // then initialize the DMA controllers
547     outb(0xc0, PORT_DMA2_MODE_REG);
548     outb(0x00, PORT_DMA2_MASK_REG);
549 }
550
551 static void
552 eoi_jmp_post()
553 {
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.
556     eoi_both_pics();
557     struct bregs br;
558     memset(&br, 0, sizeof(br));
559     br.cs = bda->jump_cs_ip >> 16;
560     br.ip = bda->jump_cs_ip;
561     call16(&br);
562 }
563
564 static void
565 check_restart_status()
566 {
567     // Get and then clear CMOS shutdown status.
568     u8 status = inb_cmos(CMOS_RESET_CODE);
569     outb_cmos(0, CMOS_RESET_CODE);
570
571     if (status == 0x00 || status == 0x09 || status >= 0x0d)
572         // Normal post
573         return;
574
575     if (status == 0x05)
576         eoi_jmp_post();
577
578     BX_PANIC("Unimplemented shutdown status: %02x\n", status);
579 }
580
581 void VISIBLE
582 _start()
583 {
584     init_dma();
585     check_restart_status();
586
587     post();
588 }