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