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