bf4583d01946ba146bdf2b8c21edb2c5aa4ec09a
[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 #include "acpi.h"
16 #include "smbios.h"
17 #include "smp.h"
18 #include "pci.h"
19
20 u32 cpuid_signature;
21 u32 cpuid_features;
22 u32 cpuid_ext_features;
23 unsigned long ram_size;
24 unsigned long bios_table_cur_addr;
25 unsigned long bios_table_end_addr;
26
27 #ifdef CONFIG_USE_EBDA_TABLES
28 unsigned long ebda_cur_addr;
29 #endif
30
31 #define cpuid(index, eax, ebx, ecx, edx) \
32   asm volatile ("cpuid" \
33                 : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) \
34                 : "0" (index))
35
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))
39
40 static u8
41 checksum(u8 *p, u32 len)
42 {
43     u32 i;
44     u8 sum = 0;
45     for (i=0; i<len; i++)
46         sum += p[i];
47     return sum;
48 }
49
50 static void
51 init_bda()
52 {
53     memset(bda, 0, sizeof(*bda));
54
55     int i;
56     for (i=0; i<256; i++) {
57         bda->ivecs[i].seg = SEG_BIOS;
58         bda->ivecs[i].offset = OFFSET_dummy_iret_handler;
59     }
60
61     bda->mem_size_kb = BASE_MEM_IN_K;
62
63     // mov CMOS Equipment Byte to BDA Equipment Word
64     bda->equipment_list_flags = inb_cmos(CMOS_EQUIPMENT_INFO);
65 }
66
67 static void
68 init_handlers()
69 {
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;
74
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;
95 }
96
97 static void
98 init_ebda()
99 {
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);
107 }
108
109 static void
110 pit_setup()
111 {
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);
117 }
118
119 //--------------------------------------------------------------------------
120 // keyboard_panic
121 //--------------------------------------------------------------------------
122 static void
123 keyboard_panic(u16 status)
124 {
125   // If you're getting a 993 keyboard panic here,
126   // please see the comment in keyboard_init
127
128   BX_PANIC("Keyboard error:%u\n",status);
129 }
130
131 static void
132 kbd_flush(u8 code)
133 {
134     u16 max = 0xffff;
135     while ((inb(PORT_PS2_STATUS) & 0x02) && (--max > 0))
136         outb(code, PORT_DIAG);
137     if (!max && code != 0xff)
138         keyboard_panic(code);
139 }
140
141 static void
142 kbd_waitdata(u8 code)
143 {
144     u16 max = 0xffff;
145     while ( ((inb(PORT_PS2_STATUS) & 0x01) == 0) && (--max>0) )
146         outb(code, PORT_DIAG);
147     if (!max)
148         keyboard_panic(code);
149 }
150
151 //--------------------------------------------------------------------------
152 // keyboard_init
153 //--------------------------------------------------------------------------
154 // this file is based on LinuxBIOS implementation of keyboard.c
155 // could convert to #asm to gain space
156 static void
157 keyboard_init()
158 {
159     /* ------------------- Flush buffers ------------------------*/
160     /* Wait until buffer is empty */
161     kbd_flush(0xff);
162
163     /* flush incoming keys */
164     u16 max=0x2000;
165     while (--max > 0) {
166         outb(0x00, PORT_DIAG);
167         if (inb(PORT_PS2_STATUS) & 0x01) {
168             inb(PORT_PS2_DATA);
169             max = 0x2000;
170             }
171         }
172
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
177
178     /* ------------------- controller side ----------------------*/
179     /* send cmd = 0xAA, self test 8042 */
180     outb(0xaa, PORT_PS2_STATUS);
181
182     kbd_flush(0x00);
183     kbd_waitdata(0x01);
184
185     /* read self-test result, 0x55 should be returned from 0x60 */
186     if (inb(PORT_PS2_DATA) != 0x55)
187         keyboard_panic(991);
188
189     /* send cmd = 0xAB, keyboard interface test */
190     outb(0xab, PORT_PS2_STATUS);
191
192     kbd_flush(0x10);
193     kbd_waitdata(0x11);
194
195     /* read keyboard interface test result, */
196     /* 0x00 should be returned form 0x60 */
197     if (inb(PORT_PS2_DATA) != 0x00)
198         keyboard_panic(992);
199
200     /* Enable Keyboard clock */
201     outb(0xae, PORT_PS2_STATUS);
202     outb(0xa8, PORT_PS2_STATUS);
203
204     /* ------------------- keyboard side ------------------------*/
205     /* reset kerboard and self test  (keyboard side) */
206     outb(0xff, PORT_PS2_DATA);
207
208     kbd_flush(0x20);
209     kbd_waitdata(0x21);
210
211     /* keyboard should return ACK */
212     if (inb(PORT_PS2_DATA) != 0xfa)
213         keyboard_panic(993);
214
215     kbd_waitdata(0x31);
216
217     if (inb(PORT_PS2_DATA) != 0xaa)
218         keyboard_panic(994);
219
220     /* Disable keyboard */
221     outb(0xf5, PORT_PS2_DATA);
222
223     kbd_flush(0x40);
224     kbd_waitdata(0x41);
225
226     /* keyboard should return ACK */
227     if (inb(PORT_PS2_DATA) != 0xfa)
228         keyboard_panic(995);
229
230     /* Write Keyboard Mode */
231     outb(0x60, PORT_PS2_STATUS);
232
233     kbd_flush(0x50);
234
235     /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
236     outb(0x61, PORT_PS2_DATA);
237
238     kbd_flush(0x60);
239
240     /* Enable keyboard */
241     outb(0xf4, PORT_PS2_DATA);
242
243     kbd_flush(0x70);
244     kbd_waitdata(0x71);
245
246     /* keyboard should return ACK */
247     if (inb(PORT_PS2_DATA) != 0xfa)
248         keyboard_panic(996);
249
250     outb(0x77, PORT_DIAG);
251 }
252
253 static void
254 kbd_setup()
255 {
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)])
261            - 0x400);
262     keyboard_init();
263 }
264
265 static u16
266 detect_parport(u16 port, u8 timeout, u8 count)
267 {
268     // clear input mode
269     outb(inb(port+2) & 0xdf, port+2);
270
271     outb(0xaa, port);
272     if (inb(port) != 0xaa)
273         // Not present
274         return 0;
275     bda->port_lpt[count] = port;
276     bda->lpt_timeout[count] = timeout;
277     return 1;
278 }
279
280 static void
281 lpt_setup()
282 {
283     u16 count = 0;
284     count += detect_parport(0x378, 0x14, count);
285     count += detect_parport(0x278, 0x14, count);
286
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);
290 }
291
292 static u16
293 detect_serial(u16 port, u8 timeout, u8 count)
294 {
295     outb(0x02, port+1);
296     if (inb(port+1) != 0x02)
297         return 0;
298     if (inb(port+2) != 0x02)
299         return 0;
300     outb(0x00, port+1);
301     bda->port_com[count] = port;
302     bda->com_timeout[count] = timeout;
303     return 1;
304 }
305
306 static void
307 serial_setup()
308 {
309     u16 count = 0;
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);
314
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);
318 }
319
320 static u32
321 bcd2bin(u8 val)
322 {
323     return (val & 0xf) + ((val >> 4) * 10);
324 }
325
326 static void
327 timer_setup()
328 {
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;
337 }
338
339 static void
340 pic_setup()
341 {
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);
353     else
354         outb(0x9f, PORT_PIC2_DATA);
355 }
356
357 static void
358 floppy_drive_post()
359 {
360     u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
361     u8 out = 0;
362     if (type & 0xf0)
363         out |= 0x07;
364     if (type & 0x0f)
365         out |= 0x70;
366     bda->floppy_harddisk_info = out;
367     outb(0x02, PORT_DMA1_MASK_REG);
368
369     bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2;
370 }
371
372 static void
373 ata_init()
374 {
375     // hdidmap  and cdidmap init.
376     u8 device;
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;
380     }
381 }
382
383 static void
384 fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
385 {
386     u8 type = inb_cmos(typecmos);
387     if (type != 47)
388         // XXX - halt
389         return;
390
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);
397     if (cyl < 1024) {
398         // no logical CHS mapping used, just physical CHS
399         // use Standard Fixed Disk Parameter Table (FDPT)
400         info->cylinders = cyl;
401         info->heads = heads;
402         info->sectors = sectors;
403         return;
404     }
405
406     // complies with Phoenix style Translated Fixed Disk Parameter
407     // Table (FDPT)
408     info->phys_cylinders = cyl;
409     info->phys_heads = heads;
410     info->phys_sectors = sectors;
411     info->sectors = sectors;
412     info->a0h_signature = 0xa0;
413     if (cyl > 8192) {
414         cyl >>= 4;
415         heads <<= 4;
416     } else if (cyl > 4096) {
417         cyl >>= 3;
418         heads <<= 3;
419     } else if (cyl > 2048) {
420         cyl >>= 2;
421         heads <<= 2;
422     }
423     info->cylinders = cyl;
424     info->heads = heads;
425     info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
426 }
427
428 static void
429 hard_drive_post()
430 {
431     outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
432     bda->disk_count = 1;
433     bda->disk_control_byte = 0xc0;
434
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);
444 }
445
446
447 static void
448 init_boot_vectors()
449 {
450     // Clear out the IPL table.
451     memset(ipl, 0, sizeof(*ipl));
452
453     // Floppy drive
454     struct ipl_entry_s *ip = &ipl->table[0];
455     ip->type = IPL_TYPE_FLOPPY;
456     ip++;
457
458     // First HDD
459     ip->type = IPL_TYPE_HARDDISK;
460     ip++;
461
462     // CDROM
463     if (CONFIG_CDROM_BOOT) {
464         ip->type = IPL_TYPE_CDROM;
465         ip++;
466     }
467
468     ipl->count = ip - ipl->table;
469     ipl->sequence = 0xffff;
470 }
471
472 static void
473 callrom(u16 seg, u16 offset)
474 {
475     struct bregs br;
476     memset(&br, 0, sizeof(br));
477     br.es = SEG_BIOS;
478     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
479     br.cs = seg;
480     br.ip = offset;
481     call16(&br);
482 }
483
484 static void
485 rom_scan(u32 start, u32 end)
486 {
487     u8 *p = (u8*)start;
488     for (; p <= (u8*)end; p += 2048) {
489         u8 *rom = p;
490         if (*(u16*)rom != 0xaa55)
491             continue;
492         u32 len = rom[2] * 512;
493         if (checksum(rom, len) != 0)
494             continue;
495         p = (u8*)(((u32)p + len) / 2048 * 2048);
496         callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
497
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')
503             continue;
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];
507         if (!entry)
508             continue;
509         // Found a device that thinks it can boot the system.  Record
510         // its BEV and product name string.
511
512         if (ipl->count >= ARRAY_SIZE(ipl->table))
513             continue;
514
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;
518
519         u16 desc = *(u16*)&rom[0x1a+0x10];
520         if (desc)
521             ip->description = (PTR_TO_SEG(rom) << 16) | desc;
522
523         ipl->count++;
524     }
525 }
526
527 static void
528 ram_probe(void)
529 {
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;
533     else
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;
537 #endif
538
539     BX_INFO("ram_size=0x%08lx\n", ram_size);
540 }
541
542 static void
543 cpu_probe(void)
544 {
545     u32 eax, ebx, ecx, edx;
546
547     cpuid(1, eax, ebx, ecx, edx);
548     cpuid_signature = eax;
549     cpuid_features = edx;
550     cpuid_ext_features = ecx;
551 }
552
553 static void
554 post()
555 {
556     BX_INFO("Start bios\n");
557
558     init_bda();
559     init_handlers();
560     init_ebda();
561
562     pit_setup();
563     kbd_setup();
564     lpt_setup();
565     serial_setup();
566     timer_setup();
567     pic_setup();
568
569     rom_scan(0xc0000, 0xc7800);
570
571     printf("BIOS - begin\n\n");
572
573     // XXX - need to do pci stuff
574     //pci_setup();
575     init_boot_vectors();
576
577     floppy_drive_post();
578     hard_drive_post();
579     if (CONFIG_ATA)
580         ata_init();
581
582     init_boot_vectors();
583     rom_scan(0xc8000, 0xe0000);
584
585     ram_probe();
586     cpu_probe();
587     smp_probe();
588     //pci_bios_init();
589
590     if (bios_table_cur_addr != 0) {
591         mptable_init();
592
593         smbios_init();
594
595         if (acpi_enabled)
596             acpi_bios_init();
597
598         bios_lock_shadow_ram();
599
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");
603     }
604
605     callrom(SEG_BIOS, OFFSET_begin_boot);
606 }
607
608 static void
609 init_dma()
610 {
611     // first reset the DMA controllers
612     outb(0, PORT_DMA1_MASTER_CLEAR);
613     outb(0, PORT_DMA2_MASTER_CLEAR);
614
615     // then initialize the DMA controllers
616     outb(0xc0, PORT_DMA2_MODE_REG);
617     outb(0x00, PORT_DMA2_MASK_REG);
618 }
619
620 static void
621 check_restart_status()
622 {
623     // Get and then clear CMOS shutdown status.
624     u8 status = inb_cmos(CMOS_RESET_CODE);
625     outb_cmos(0, CMOS_RESET_CODE);
626
627     if (status == 0x00 || status == 0x09 || status >= 0x0d)
628         // Normal post
629         return;
630
631     if (status != 0x05) {
632         BX_PANIC("Unimplemented shutdown status: %02x\n", status);
633         return;
634     }
635
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.
638     eoi_both_pics();
639     struct bregs br;
640     memset(&br, 0, sizeof(br));
641     br.cs = bda->jump_cs_ip >> 16;
642     br.ip = bda->jump_cs_ip;
643     call16(&br);
644 }
645
646 void VISIBLE32
647 _start()
648 {
649     init_dma();
650     check_restart_status();
651
652     post();
653 }