Unify checksum functions.
[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 #include "ata.h"
15 #include "kbd.h"
16
17 #define bda ((struct bios_data_area_s *)0)
18 #define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4))
19
20 static void
21 init_bda()
22 {
23     memset(bda, 0, sizeof(*bda));
24
25     int i;
26     for (i=0; i<256; i++) {
27         SET_BDA(ivecs[i].seg, SEG_BIOS);
28         SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
29     }
30
31     SET_BDA(mem_size_kb, BASE_MEM_IN_K);
32
33     // mov CMOS Equipment Byte to BDA Equipment Word
34     SET_BDA(equipment_list_flags, inb_cmos(CMOS_EQUIPMENT_INFO));
35 }
36
37 static void
38 init_handlers()
39 {
40     // set vector 0x79 to zero
41     // this is used by 'gardian angel' protection system
42     SET_BDA(ivecs[0x79].seg, 0);
43     SET_BDA(ivecs[0x79].offset, 0);
44
45     SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
46     SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
47     SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
48     SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
49     SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
50     SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
51     SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
52     SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
53     SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
54     SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
55     SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
56     SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
57     SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
58     SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
59     SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
60     SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
61     SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
62     SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
63     SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
64     SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
65 }
66
67 static void
68 init_ebda()
69 {
70     memset(ebda, 0, sizeof(*ebda));
71     ebda->size = EBDA_SIZE;
72     SET_BDA(ebda_seg, EBDA_SEG);
73     SET_BDA(ivecs[0x41].seg, EBDA_SEG);
74     SET_BDA(ivecs[0x41].offset
75             , offsetof(struct extended_bios_data_area_s, fdpt0));
76     SET_BDA(ivecs[0x46].seg, EBDA_SEG);
77     SET_BDA(ivecs[0x41].offset
78             , offsetof(struct extended_bios_data_area_s, fdpt1));
79 }
80
81 static void
82 pit_setup()
83 {
84     // timer0: binary count, 16bit count, mode 2
85     outb(0x34, PORT_PIT_MODE);
86     // maximum count of 0000H = 18.2Hz
87     outb(0x0, PORT_PIT_COUNTER0);
88     outb(0x0, PORT_PIT_COUNTER0);
89 }
90
91 static u16
92 detect_parport(u16 port, u8 timeout, u8 count)
93 {
94     // clear input mode
95     outb(inb(port+2) & 0xdf, port+2);
96
97     outb(0xaa, port);
98     if (inb(port) != 0xaa)
99         // Not present
100         return 0;
101     SET_BDA(port_lpt[count], port);
102     SET_BDA(lpt_timeout[count], timeout);
103     return 1;
104 }
105
106 static void
107 lpt_setup()
108 {
109     u16 count = 0;
110     count += detect_parport(0x378, 0x14, count);
111     count += detect_parport(0x278, 0x14, count);
112
113     // Equipment word bits 14..15 determing # parallel ports
114     u16 eqb = GET_BDA(equipment_list_flags);
115     SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
116 }
117
118 static u16
119 detect_serial(u16 port, u8 timeout, u8 count)
120 {
121     outb(0x02, port+1);
122     if (inb(port+1) != 0x02)
123         return 0;
124     if (inb(port+2) != 0x02)
125         return 0;
126     outb(0x00, port+1);
127     SET_BDA(port_com[count], port);
128     SET_BDA(com_timeout[count], timeout);
129     return 1;
130 }
131
132 static void
133 serial_setup()
134 {
135     u16 count = 0;
136     count += detect_serial(0x3f8, 0x0a, count);
137     count += detect_serial(0x2f8, 0x0a, count);
138     count += detect_serial(0x3e8, 0x0a, count);
139     count += detect_serial(0x2e8, 0x0a, count);
140
141     // Equipment word bits 9..11 determing # serial ports
142     u16 eqb = GET_BDA(equipment_list_flags);
143     SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
144 }
145
146 static u32
147 bcd2bin(u8 val)
148 {
149     return (val & 0xf) + ((val >> 4) * 10);
150 }
151
152 static void
153 timer_setup()
154 {
155     u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
156     u32 ticks = (seconds * 18206507) / 1000000;
157     u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
158     ticks += (minutes * 10923904) / 10000;
159     u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
160     ticks += (hours * 65543427) / 1000;
161     SET_BDA(timer_counter, ticks);
162     SET_BDA(timer_rollover, 0);
163 }
164
165 static void
166 pic_setup()
167 {
168     outb(0x11, PORT_PIC1);
169     outb(0x11, PORT_PIC2);
170     outb(0x08, PORT_PIC1_DATA);
171     outb(0x70, PORT_PIC2_DATA);
172     outb(0x04, PORT_PIC1_DATA);
173     outb(0x02, PORT_PIC2_DATA);
174     outb(0x01, PORT_PIC1_DATA);
175     outb(0x01, PORT_PIC2_DATA);
176     outb(0xb8, PORT_PIC1_DATA);
177     if (CONFIG_PS2_MOUSE)
178         outb(0x8f, PORT_PIC2_DATA);
179     else
180         outb(0x9f, PORT_PIC2_DATA);
181 }
182
183 static void
184 floppy_drive_post()
185 {
186     u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
187     u8 out = 0;
188     if (type & 0xf0)
189         out |= 0x07;
190     if (type & 0x0f)
191         out |= 0x70;
192     SET_BDA(floppy_harddisk_info, out);
193     outb(0x02, PORT_DMA1_MASK_REG);
194
195     SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
196 }
197
198 static void
199 ata_init()
200 {
201     // hdidmap  and cdidmap init.
202     u8 device;
203     for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
204         ebda->ata.idmap[0][device] = CONFIG_MAX_ATA_DEVICES;
205         ebda->ata.idmap[1][device] = CONFIG_MAX_ATA_DEVICES;
206     }
207 }
208
209 static void
210 fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
211 {
212     u8 type = inb_cmos(typecmos);
213     if (type != 47)
214         // XXX - halt
215         return;
216
217     info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3);
218     info->drive_control_byte = inb_cmos(basecmos+5);
219     info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6);
220     u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
221     u8 heads = inb_cmos(basecmos+2);
222     u8 sectors = inb_cmos(basecmos+8);
223     if (cyl < 1024) {
224         // no logical CHS mapping used, just physical CHS
225         // use Standard Fixed Disk Parameter Table (FDPT)
226         info->cylinders = cyl;
227         info->heads = heads;
228         info->sectors = sectors;
229         return;
230     }
231
232     // complies with Phoenix style Translated Fixed Disk Parameter
233     // Table (FDPT)
234     info->phys_cylinders = cyl;
235     info->phys_heads = heads;
236     info->phys_sectors = sectors;
237     info->sectors = sectors;
238     info->a0h_signature = 0xa0;
239     if (cyl > 8192) {
240         cyl >>= 4;
241         heads <<= 4;
242     } else if (cyl > 4096) {
243         cyl >>= 3;
244         heads <<= 3;
245     } else if (cyl > 2048) {
246         cyl >>= 2;
247         heads <<= 2;
248     }
249     info->cylinders = cyl;
250     info->heads = heads;
251     info->checksum = -checksum((u8*)info, sizeof(*info)-1);
252 }
253
254 static void
255 hard_drive_post()
256 {
257     outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
258     SET_BDA(disk_count, 1);
259     SET_BDA(disk_control_byte, 0xc0);
260
261     // move disk geometry data from CMOS to EBDA disk parameter table(s)
262     u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
263     if ((diskinfo & 0xf0) == 0xf0)
264         // Fill EBDA table for hard disk 0.
265         fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
266     if ((diskinfo & 0x0f) == 0x0f)
267         // XXX - bochs halts on any other type
268         // Fill EBDA table for hard disk 1.
269         fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
270 }
271
272
273 static void
274 init_boot_vectors()
275 {
276     // Floppy drive
277     struct ipl_entry_s *ip = &ebda->ipl.table[0];
278     ip->type = IPL_TYPE_FLOPPY;
279     ip++;
280
281     // First HDD
282     ip->type = IPL_TYPE_HARDDISK;
283     ip++;
284
285     // CDROM
286     if (CONFIG_CDROM_BOOT) {
287         ip->type = IPL_TYPE_CDROM;
288         ip++;
289     }
290
291     ebda->ipl.count = ip - ebda->ipl.table;
292     ebda->ipl.sequence = 0xffff;
293 }
294
295 static void
296 callrom(u16 seg, u16 offset)
297 {
298     struct bregs br;
299     memset(&br, 0, sizeof(br));
300     br.es = SEG_BIOS;
301     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
302     br.cs = seg;
303     br.ip = offset;
304     call16(&br);
305 }
306
307 static void
308 rom_scan(u32 start, u32 end)
309 {
310     u8 *p = (u8*)start;
311     for (; p <= (u8*)end; p += 2048) {
312         u8 *rom = p;
313         if (*(u16*)rom != 0xaa55)
314             continue;
315         u32 len = rom[2] * 512;
316         if (checksum(rom, len) != 0)
317             continue;
318         p = (u8*)(((u32)p + len) / 2048 * 2048);
319         callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
320
321         // Look at the ROM's PnP Expansion header.  Properly, we're supposed
322         // to init all the ROMs and then go back and build an IPL table of
323         // all the bootable devices, but we can get away with one pass.
324         if (rom[0x1a] != '$' || rom[0x1b] != 'P'
325             || rom[0x1c] != 'n' || rom[0x1d] != 'P')
326             continue;
327         // 0x1A is also the offset into the expansion header of...
328         // the Bootstrap Entry Vector, or zero if there is none.
329         u16 entry = *(u16*)&rom[0x1a+0x1a];
330         if (!entry)
331             continue;
332         // Found a device that thinks it can boot the system.  Record
333         // its BEV and product name string.
334
335         if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
336             continue;
337
338         struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
339         ip->type = IPL_TYPE_BEV;
340         ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
341
342         u16 desc = *(u16*)&rom[0x1a+0x10];
343         if (desc)
344             ip->description = (FARPTR_TO_SEG(rom) << 16) | desc;
345
346         ebda->ipl.count++;
347     }
348 }
349
350 static void
351 post()
352 {
353     BX_INFO("Start bios\n");
354
355     init_bda();
356     init_handlers();
357     init_ebda();
358
359     pit_setup();
360     kbd_setup();
361     lpt_setup();
362     serial_setup();
363     timer_setup();
364     pic_setup();
365
366     rom_scan(0xc0000, 0xc7800);
367
368     printf("BIOS - begin\n\n");
369
370     // clear bss section -- XXX - shouldn't use globals
371     extern char __bss_start[], __bss_end[];
372     memset(__bss_start, 0, __bss_end - __bss_start);
373
374     rombios32_init();
375
376     init_boot_vectors();
377
378     floppy_drive_post();
379     hard_drive_post();
380     if (CONFIG_ATA) {
381         ata_init();
382         ata_detect();
383     }
384
385     init_boot_vectors();
386
387     rom_scan(0xc8000, 0xe0000);
388
389     // reset the memory (some boot loaders such as syslinux suppose
390     // that the memory is set to zero)
391     memset((void*)0x40000, 0, 0x40000); // XXX - shouldn't use globals
392
393     // Invoke int 19 to start boot process.
394     struct bregs br;
395     memset(&br, 0, sizeof(br));
396     call16_int(0x19, &br);
397 }
398
399 static void
400 init_dma()
401 {
402     // first reset the DMA controllers
403     outb(0, PORT_DMA1_MASTER_CLEAR);
404     outb(0, PORT_DMA2_MASTER_CLEAR);
405
406     // then initialize the DMA controllers
407     outb(0xc0, PORT_DMA2_MODE_REG);
408     outb(0x00, PORT_DMA2_MASK_REG);
409 }
410
411 static void
412 check_restart_status()
413 {
414     // Get and then clear CMOS shutdown status.
415     u8 status = inb_cmos(CMOS_RESET_CODE);
416     outb_cmos(0, CMOS_RESET_CODE);
417
418     if (status == 0x00 || status == 0x09 || status >= 0x0d)
419         // Normal post
420         return;
421
422     if (status != 0x05) {
423         BX_PANIC("Unimplemented shutdown status: %02x\n", status);
424         return;
425     }
426
427     // XXX - this is supposed to jump without changing any memory -
428     // but the stack has been altered by the time the code gets here.
429     eoi_both_pics();
430     struct bregs br;
431     memset(&br, 0, sizeof(br));
432     br.cs = GET_BDA(jump_cs_ip) >> 16;
433     br.ip = GET_BDA(jump_cs_ip);
434     call16(&br);
435 }
436
437 void VISIBLE32
438 _start()
439 {
440     init_dma();
441     check_restart_status();
442
443     post();
444 }