aecb2e30906edfd8c3d722c3e75d3717ab7094d6
[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     // mov CMOS Equipment Byte to BDA Equipment Word
43     bda->equipment_list_flags = inb_cmos(CMOS_EQUIPMENT_INFO);
44 }
45
46 static void
47 init_handlers()
48 {
49     // set vector 0x79 to zero
50     // this is used by 'gardian angel' protection system
51     bda->ivecs[0x79].seg = 0;
52     bda->ivecs[0x79].offset = 0;
53
54     bda->ivecs[0x40].offset = OFFSET_entry_40;
55     bda->ivecs[0x0e].offset = OFFSET_entry_0e;
56     bda->ivecs[0x13].offset = OFFSET_entry_13;
57     bda->ivecs[0x76].offset = OFFSET_entry_76;
58     bda->ivecs[0x17].offset = OFFSET_entry_17;
59     bda->ivecs[0x18].offset = OFFSET_entry_18;
60     bda->ivecs[0x19].offset = OFFSET_entry_19;
61     bda->ivecs[0x1c].offset = OFFSET_entry_1c;
62     bda->ivecs[0x12].offset = OFFSET_entry_12;
63     bda->ivecs[0x11].offset = OFFSET_entry_11;
64     bda->ivecs[0x15].offset = OFFSET_entry_15;
65     bda->ivecs[0x08].offset = OFFSET_entry_08;
66     bda->ivecs[0x09].offset = OFFSET_entry_09;
67     bda->ivecs[0x16].offset = OFFSET_entry_16;
68     bda->ivecs[0x14].offset = OFFSET_entry_14;
69     bda->ivecs[0x1a].offset = OFFSET_entry_1a;
70     bda->ivecs[0x70].offset = OFFSET_entry_70;
71     bda->ivecs[0x74].offset = OFFSET_entry_74;
72     bda->ivecs[0x75].offset = OFFSET_entry_75;
73     bda->ivecs[0x10].offset = OFFSET_entry_10;
74 }
75
76 static void
77 init_ebda()
78 {
79     memset(ebda, 0, sizeof(*ebda));
80     ebda->size = EBDA_SIZE;
81     bda->ebda_seg = EBDA_SEG;
82     bda->ivecs[0x41].seg = EBDA_SEG;
83     bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt0);
84     bda->ivecs[0x46].seg = EBDA_SEG;
85     bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt1);
86 }
87
88 static void
89 pit_setup()
90 {
91     // timer0: binary count, 16bit count, mode 2
92     outb(0x34, PORT_PIT_MODE);
93     // maximum count of 0000H = 18.2Hz
94     outb(0x0, PORT_PIT_COUNTER0);
95     outb(0x0, PORT_PIT_COUNTER0);
96 }
97
98 //--------------------------------------------------------------------------
99 // keyboard_panic
100 //--------------------------------------------------------------------------
101 static void
102 keyboard_panic(u16 status)
103 {
104   // If you're getting a 993 keyboard panic here,
105   // please see the comment in keyboard_init
106
107   BX_PANIC("Keyboard error:%u\n",status);
108 }
109
110 static void
111 kbd_flush(u8 code)
112 {
113     u16 max = 0xffff;
114     while ((inb(PORT_PS2_STATUS) & 0x02) && (--max > 0))
115         outb(code, PORT_DIAG);
116     if (!max && code != 0xff)
117         keyboard_panic(code);
118 }
119
120 static void
121 kbd_waitdata(u8 code)
122 {
123     u16 max = 0xffff;
124     while ( ((inb(PORT_PS2_STATUS) & 0x01) == 0) && (--max>0) )
125         outb(code, PORT_DIAG);
126     if (!max)
127         keyboard_panic(code);
128 }
129
130 //--------------------------------------------------------------------------
131 // keyboard_init
132 //--------------------------------------------------------------------------
133 // this file is based on LinuxBIOS implementation of keyboard.c
134 // could convert to #asm to gain space
135 static void
136 keyboard_init()
137 {
138     /* ------------------- Flush buffers ------------------------*/
139     /* Wait until buffer is empty */
140     kbd_flush(0xff);
141
142     /* flush incoming keys */
143     u16 max=0x2000;
144     while (--max > 0) {
145         outb(0x00, PORT_DIAG);
146         if (inb(PORT_PS2_STATUS) & 0x01) {
147             inb(PORT_PS2_DATA);
148             max = 0x2000;
149             }
150         }
151
152     // Due to timer issues, and if the IPS setting is > 15000000,
153     // the incoming keys might not be flushed here. That will
154     // cause a panic a few lines below.  See sourceforge bug report :
155     // [ 642031 ] FATAL: Keyboard RESET error:993
156
157     /* ------------------- controller side ----------------------*/
158     /* send cmd = 0xAA, self test 8042 */
159     outb(0xaa, PORT_PS2_STATUS);
160
161     kbd_flush(0x00);
162     kbd_waitdata(0x01);
163
164     /* read self-test result, 0x55 should be returned from 0x60 */
165     if (inb(PORT_PS2_DATA) != 0x55)
166         keyboard_panic(991);
167
168     /* send cmd = 0xAB, keyboard interface test */
169     outb(0xab, PORT_PS2_STATUS);
170
171     kbd_flush(0x10);
172     kbd_waitdata(0x11);
173
174     /* read keyboard interface test result, */
175     /* 0x00 should be returned form 0x60 */
176     if (inb(PORT_PS2_DATA) != 0x00)
177         keyboard_panic(992);
178
179     /* Enable Keyboard clock */
180     outb(0xae, PORT_PS2_STATUS);
181     outb(0xa8, PORT_PS2_STATUS);
182
183     /* ------------------- keyboard side ------------------------*/
184     /* reset kerboard and self test  (keyboard side) */
185     outb(0xff, PORT_PS2_DATA);
186
187     kbd_flush(0x20);
188     kbd_waitdata(0x21);
189
190     /* keyboard should return ACK */
191     if (inb(PORT_PS2_DATA) != 0xfa)
192         keyboard_panic(993);
193
194     kbd_waitdata(0x31);
195
196     if (inb(PORT_PS2_DATA) != 0xaa)
197         keyboard_panic(994);
198
199     /* Disable keyboard */
200     outb(0xf5, PORT_PS2_DATA);
201
202     kbd_flush(0x40);
203     kbd_waitdata(0x41);
204
205     /* keyboard should return ACK */
206     if (inb(PORT_PS2_DATA) != 0xfa)
207         keyboard_panic(995);
208
209     /* Write Keyboard Mode */
210     outb(0x60, PORT_PS2_STATUS);
211
212     kbd_flush(0x50);
213
214     /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
215     outb(0x61, PORT_PS2_DATA);
216
217     kbd_flush(0x60);
218
219     /* Enable keyboard */
220     outb(0xf4, PORT_PS2_DATA);
221
222     kbd_flush(0x70);
223     kbd_waitdata(0x71);
224
225     /* keyboard should return ACK */
226     if (inb(PORT_PS2_DATA) != 0xfa)
227         keyboard_panic(996);
228
229     outb(0x77, PORT_DIAG);
230 }
231
232 static void
233 kbd_setup()
234 {
235     bda->kbd_mode = 0x10;
236     bda->kbd_buf_head = bda->kbd_buf_tail = bda->kbd_buf_start_offset
237         = offsetof(struct bios_data_area_s, kbd_buf) - 0x400;
238     bda->kbd_buf_end_offset
239         = (offsetof(struct bios_data_area_s, kbd_buf[sizeof(bda->kbd_buf)])
240            - 0x400);
241     keyboard_init();
242 }
243
244 static u16
245 detect_parport(u16 port, u8 timeout, u8 count)
246 {
247     // clear input mode
248     outb(inb(port+2) & 0xdf, port+2);
249
250     outb(0xaa, port);
251     if (inb(port) != 0xaa)
252         // Not present
253         return 0;
254     bda->port_lpt[count] = port;
255     bda->lpt_timeout[count] = timeout;
256     return 1;
257 }
258
259 static void
260 lpt_setup()
261 {
262     u16 count = 0;
263     count += detect_parport(0x378, 0x14, count);
264     count += detect_parport(0x278, 0x14, count);
265
266     // Equipment word bits 14..15 determing # parallel ports
267     u16 eqb = bda->equipment_list_flags;
268     bda->equipment_list_flags = (eqb & 0x3fff) | (count << 14);
269 }
270
271 static u16
272 detect_serial(u16 port, u8 timeout, u8 count)
273 {
274     outb(0x02, port+1);
275     if (inb(port+1) != 0x02)
276         return 0;
277     if (inb(port+2) != 0x02)
278         return 0;
279     outb(0x00, port+1);
280     bda->port_com[count] = port;
281     bda->com_timeout[count] = timeout;
282     return 1;
283 }
284
285 static void
286 serial_setup()
287 {
288     u16 count = 0;
289     count += detect_serial(0x3f8, 0x0a, count);
290     count += detect_serial(0x2f8, 0x0a, count);
291     count += detect_serial(0x3e8, 0x0a, count);
292     count += detect_serial(0x2e8, 0x0a, count);
293
294     // Equipment word bits 9..11 determing # serial ports
295     u16 eqb = bda->equipment_list_flags;
296     bda->equipment_list_flags = (eqb & 0xf1ff) | (count << 9);
297 }
298
299 static u32
300 bcd2bin(u8 val)
301 {
302     return (val & 0xf) + ((val >> 4) * 10);
303 }
304
305 static void
306 timer_setup()
307 {
308     u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
309     u32 ticks = (seconds * 18206507) / 1000000;
310     u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
311     ticks += (minutes * 10923904) / 10000;
312     u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
313     ticks += (hours * 65543427) / 1000;
314     bda->timer_counter = ticks;
315     bda->timer_rollover = 0;
316 }
317
318 static void
319 pic_setup()
320 {
321     outb(0x11, PORT_PIC1);
322     outb(0x11, PORT_PIC2);
323     outb(0x08, PORT_PIC1_DATA);
324     outb(0x70, PORT_PIC2_DATA);
325     outb(0x04, PORT_PIC1_DATA);
326     outb(0x02, PORT_PIC2_DATA);
327     outb(0x01, PORT_PIC1_DATA);
328     outb(0x01, PORT_PIC2_DATA);
329     outb(0xb8, PORT_PIC1_DATA);
330     if (CONFIG_PS2_MOUSE)
331         outb(0x8f, PORT_PIC2_DATA);
332     else
333         outb(0x9f, PORT_PIC2_DATA);
334 }
335
336 static void
337 floppy_drive_post()
338 {
339     u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
340     u8 out = 0;
341     if (type & 0xf0)
342         out |= 0x07;
343     if (type & 0x0f)
344         out |= 0x70;
345     bda->floppy_harddisk_info = out;
346     outb(0x02, PORT_DMA1_MASK_REG);
347
348     bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2;
349 }
350
351 static void
352 ata_init()
353 {
354     // hdidmap  and cdidmap init.
355     u8 device;
356     for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
357         ebda->ata.idmap[0][device] = CONFIG_MAX_ATA_DEVICES;
358         ebda->ata.idmap[1][device] = CONFIG_MAX_ATA_DEVICES;
359     }
360 }
361
362 static void
363 fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
364 {
365     u8 type = inb_cmos(typecmos);
366     if (type != 47)
367         // XXX - halt
368         return;
369
370     info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3);
371     info->drive_control_byte = inb_cmos(basecmos+5);
372     info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6);
373     u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
374     u8 heads = inb_cmos(basecmos+2);
375     u8 sectors = inb_cmos(basecmos+8);
376     if (cyl < 1024) {
377         // no logical CHS mapping used, just physical CHS
378         // use Standard Fixed Disk Parameter Table (FDPT)
379         info->cylinders = cyl;
380         info->heads = heads;
381         info->sectors = sectors;
382         return;
383     }
384
385     // complies with Phoenix style Translated Fixed Disk Parameter
386     // Table (FDPT)
387     info->phys_cylinders = cyl;
388     info->phys_heads = heads;
389     info->phys_sectors = sectors;
390     info->sectors = sectors;
391     info->a0h_signature = 0xa0;
392     if (cyl > 8192) {
393         cyl >>= 4;
394         heads <<= 4;
395     } else if (cyl > 4096) {
396         cyl >>= 3;
397         heads <<= 3;
398     } else if (cyl > 2048) {
399         cyl >>= 2;
400         heads <<= 2;
401     }
402     info->cylinders = cyl;
403     info->heads = heads;
404     info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
405 }
406
407 static void
408 hard_drive_post()
409 {
410     outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
411     bda->disk_count = 1;
412     bda->disk_control_byte = 0xc0;
413
414     // move disk geometry data from CMOS to EBDA disk parameter table(s)
415     u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
416     if ((diskinfo & 0xf0) == 0xf0)
417         // Fill EBDA table for hard disk 0.
418         fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
419     if ((diskinfo & 0x0f) == 0x0f)
420         // XXX - bochs halts on any other type
421         // Fill EBDA table for hard disk 1.
422         fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
423 }
424
425
426 static void
427 init_boot_vectors()
428 {
429     // Clear out the IPL table.
430     memset(ipl, 0, sizeof(*ipl));
431
432     // Floppy drive
433     struct ipl_entry_s *ip = &ipl->table[0];
434     ip->type = IPL_TYPE_FLOPPY;
435     ip++;
436
437     // First HDD
438     ip->type = IPL_TYPE_HARDDISK;
439     ip++;
440
441     // CDROM
442     if (CONFIG_CDROM_BOOT) {
443         ip->type = IPL_TYPE_CDROM;
444         ip++;
445     }
446
447     ipl->count = ip - ipl->table;
448     ipl->sequence = 0xffff;
449 }
450
451 static void
452 callrom(u16 seg, u16 offset)
453 {
454     struct bregs br;
455     memset(&br, 0, sizeof(br));
456     br.es = SEG_BIOS;
457     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
458     br.cs = seg;
459     br.ip = offset;
460     call16(&br);
461 }
462
463 static void
464 rom_scan(u32 start, u32 end)
465 {
466     u8 *p = (u8*)start;
467     for (; p <= (u8*)end; p += 2048) {
468         u8 *rom = p;
469         if (*(u16*)rom != 0xaa55)
470             continue;
471         u32 len = rom[2] * 512;
472         if (checksum(rom, len) != 0)
473             continue;
474         p = (u8*)(((u32)p + len) / 2048 * 2048);
475         callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
476
477         // Look at the ROM's PnP Expansion header.  Properly, we're supposed
478         // to init all the ROMs and then go back and build an IPL table of
479         // all the bootable devices, but we can get away with one pass.
480         if (rom[0x1a] != '$' || rom[0x1b] != 'P'
481             || rom[0x1c] != 'n' || rom[0x1d] != 'P')
482             continue;
483         // 0x1A is also the offset into the expansion header of...
484         // the Bootstrap Entry Vector, or zero if there is none.
485         u16 entry = *(u16*)&rom[0x1a+0x1a];
486         if (!entry)
487             continue;
488         // Found a device that thinks it can boot the system.  Record
489         // its BEV and product name string.
490
491         if (ipl->count >= ARRAY_SIZE(ipl->table))
492             continue;
493
494         struct ipl_entry_s *ip = &ipl->table[ipl->count];
495         ip->type = IPL_TYPE_BEV;
496         ip->vector = (PTR_TO_SEG(rom) << 16) | entry;
497
498         u16 desc = *(u16*)&rom[0x1a+0x10];
499         if (desc)
500             ip->description = (PTR_TO_SEG(rom) << 16) | desc;
501
502         ipl->count++;
503     }
504 }
505
506 static void
507 post()
508 {
509     BX_INFO("Start bios\n");
510
511     init_bda();
512     init_handlers();
513     init_ebda();
514
515     pit_setup();
516     kbd_setup();
517     lpt_setup();
518     serial_setup();
519     timer_setup();
520     pic_setup();
521
522     rom_scan(0xc0000, 0xc7800);
523
524     printf("BIOS - begin\n\n");
525
526     // XXX - need to do pci stuff
527     //pci_setup();
528     init_boot_vectors();
529
530     floppy_drive_post();
531     hard_drive_post();
532     if (CONFIG_ATA)
533         ata_init();
534
535     init_boot_vectors();
536     rom_scan(0xc8000, 0xe0000);
537
538     callrom(SEG_BIOS, OFFSET_begin_boot);
539 }
540
541 static void
542 init_dma()
543 {
544     // first reset the DMA controllers
545     outb(0, PORT_DMA1_MASTER_CLEAR);
546     outb(0, PORT_DMA2_MASTER_CLEAR);
547
548     // then initialize the DMA controllers
549     outb(0xc0, PORT_DMA2_MODE_REG);
550     outb(0x00, PORT_DMA2_MASK_REG);
551 }
552
553 static void
554 check_restart_status()
555 {
556     // Get and then clear CMOS shutdown status.
557     u8 status = inb_cmos(CMOS_RESET_CODE);
558     outb_cmos(0, CMOS_RESET_CODE);
559
560     if (status == 0x00 || status == 0x09 || status >= 0x0d)
561         // Normal post
562         return;
563
564     if (status != 0x05) {
565         BX_PANIC("Unimplemented shutdown status: %02x\n", status);
566         return;
567     }
568
569     // XXX - this is supposed to jump without changing any memory -
570     // but the stack has been altered by the time the code gets here.
571     eoi_both_pics();
572     struct bregs br;
573     memset(&br, 0, sizeof(br));
574     br.cs = bda->jump_cs_ip >> 16;
575     br.ip = bda->jump_cs_ip;
576     call16(&br);
577 }
578
579 void VISIBLE32
580 _start()
581 {
582     init_dma();
583     check_restart_status();
584
585     post();
586 }