b41e77f8f264c0a77aaaa272b2339e140c394e61
[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" // hard_drive_setup
15 #include "kbd.h" // kbd_setup
16 #include "disk.h" // floppy_drive_setup
17 #include "memmap.h" // add_e820
18 #include "pic.h" // pic_setup
19
20 #define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
21 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
22
23 static void
24 init_bda()
25 {
26     dprintf(3, "init bda\n");
27     memset(bda, 0, sizeof(*bda));
28
29     SET_BDA(mem_size_kb, BASE_MEM_IN_K);
30
31     int i;
32     for (i=0; i<256; i++) {
33         SET_BDA(ivecs[i].seg, SEG_BIOS);
34         SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
35     }
36
37     SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
38     SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
39     //SET_BDA(ivecs[0x0a].offset, OFFSET_entry_hwirq);
40     //SET_BDA(ivecs[0x0b].offset, OFFSET_entry_hwirq);
41     //SET_BDA(ivecs[0x0c].offset, OFFSET_entry_hwirq);
42     //SET_BDA(ivecs[0x0d].offset, OFFSET_entry_hwirq);
43     SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
44     //SET_BDA(ivecs[0x0f].offset, OFFSET_entry_hwirq);
45     SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
46     SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
47     SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
48     SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
49     SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
50     SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
51     SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
52     SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
53     SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
54     SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
55     SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
56     SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
57     SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
58     SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
59     //SET_BDA(ivecs[0x71].offset, OFFSET_entry_hwirq);
60     //SET_BDA(ivecs[0x72].offset, OFFSET_entry_hwirq);
61     //SET_BDA(ivecs[0x73].offset, OFFSET_entry_hwirq);
62     SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
63     SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
64     SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
65     //SET_BDA(ivecs[0x77].offset, OFFSET_entry_hwirq);
66
67     // set vector 0x79 to zero
68     // this is used by 'gardian angel' protection system
69     SET_BDA(ivecs[0x79].seg, 0);
70     SET_BDA(ivecs[0x79].offset, 0);
71
72     SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
73 }
74
75 static void
76 init_ebda()
77 {
78     memset(ebda, 0, sizeof(*ebda));
79     ebda->size = EBDA_SIZE;
80     SET_BDA(ebda_seg, SEG_EBDA);
81     SET_BDA(ivecs[0x41].seg, SEG_EBDA);
82     SET_BDA(ivecs[0x41].offset
83             , offsetof(struct extended_bios_data_area_s, fdpt[0]));
84     SET_BDA(ivecs[0x46].seg, SEG_EBDA);
85     SET_BDA(ivecs[0x41].offset
86             , offsetof(struct extended_bios_data_area_s, fdpt[1]));
87 }
88
89 static void
90 ram_probe(void)
91 {
92     dprintf(3, "Find memory size\n");
93     if (CONFIG_COREBOOT) {
94         coreboot_fill_map();
95     } else {
96         // On emulators, get memory size from nvram.
97         u32 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
98                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
99         if (rs)
100             rs += 16 * 1024 * 1024;
101         else
102             rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
103                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
104                   + 1 * 1024 * 1024);
105         SET_EBDA(ram_size, rs);
106         add_e820(0, rs, E820_RAM);
107
108         /* reserve 256KB BIOS area at the end of 4 GB */
109         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
110     }
111
112     // Don't declare any memory between 0xa0000 and 0x100000
113     add_e820(0xa0000, 0x50000, E820_HOLE);
114
115     // Mark known areas as reserved.
116     add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
117     add_e820((u32)MAKE_FARPTR(SEG_BIOS, 0), 0x10000, E820_RESERVED);
118
119     dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
120 }
121
122 static void
123 init_boot_vectors()
124 {
125     dprintf(3, "init boot device ordering\n");
126
127     // Floppy drive
128     struct ipl_entry_s *ip = &ebda->ipl.table[0];
129     ip->type = IPL_TYPE_FLOPPY;
130     ip++;
131
132     // First HDD
133     ip->type = IPL_TYPE_HARDDISK;
134     ip++;
135
136     // CDROM
137     if (CONFIG_CDROM_BOOT) {
138         ip->type = IPL_TYPE_CDROM;
139         ip++;
140     }
141
142     ebda->ipl.count = ip - ebda->ipl.table;
143     ebda->ipl.sequence = 0xffff;
144     if (CONFIG_COREBOOT) {
145         // XXX - hardcode defaults for coreboot.
146         ebda->ipl.bootorder = 0x00000231;
147         ebda->ipl.checkfloppysig = 1;
148     } else {
149         // On emulators, get boot order from nvram.
150         ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
151                                | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
152         if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
153             ebda->ipl.checkfloppysig = 1;
154     }
155 }
156
157 // Execute a given option rom.
158 static void
159 callrom(u16 seg, u16 offset)
160 {
161     struct bregs br;
162     memset(&br, 0, sizeof(br));
163     br.es = SEG_BIOS;
164     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
165     br.cs = seg;
166     br.ip = offset;
167     call16(&br);
168
169     debug_serial_setup();
170 }
171
172 // Find and run any "option roms" found in the given address range.
173 static void
174 rom_scan(u32 start, u32 end)
175 {
176     u8 *p = (u8*)start;
177     for (; p <= (u8*)end; p += 2048) {
178         u8 *rom = p;
179         if (*(u16*)rom != 0xaa55)
180             continue;
181         u32 len = rom[2] * 512;
182         u8 sum = checksum(rom, len);
183         if (sum != 0) {
184             dprintf(1, "Found option rom with bad checksum:"
185                     " loc=%p len=%d sum=%x\n"
186                     , rom, len, sum);
187             continue;
188         }
189         p = (u8*)(((u32)p + len) / 2048 * 2048);
190         dprintf(1, "Running option rom at %p\n", rom+3);
191         callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
192
193         if (GET_BDA(ebda_seg) != SEG_EBDA)
194             BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
195                      , rom, SEG_EBDA, GET_BDA(ebda_seg));
196
197         // Look at the ROM's PnP Expansion header.  Properly, we're supposed
198         // to init all the ROMs and then go back and build an IPL table of
199         // all the bootable devices, but we can get away with one pass.
200         if (rom[0x1a] != '$' || rom[0x1b] != 'P'
201             || rom[0x1c] != 'n' || rom[0x1d] != 'P')
202             continue;
203         // 0x1A is also the offset into the expansion header of...
204         // the Bootstrap Entry Vector, or zero if there is none.
205         u16 entry = *(u16*)&rom[0x1a+0x1a];
206         if (!entry)
207             continue;
208         // Found a device that thinks it can boot the system.  Record
209         // its BEV and product name string.
210
211         if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
212             continue;
213
214         struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
215         ip->type = IPL_TYPE_BEV;
216         ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
217
218         u16 desc = *(u16*)&rom[0x1a+0x10];
219         if (desc)
220             ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
221
222         ebda->ipl.count++;
223     }
224 }
225
226 // Main setup code.
227 static void
228 post()
229 {
230     init_bda();
231     init_ebda();
232
233     pic_setup();
234     timer_setup();
235     kbd_setup();
236     lpt_setup();
237     serial_setup();
238     mouse_setup();
239     mathcp_setup();
240
241     memmap_setup();
242
243     ram_probe();
244
245     dprintf(1, "Scan for VGA option rom\n");
246     rom_scan(0xc0000, 0xc7800);
247
248     printf("BIOS - begin\n\n");
249
250     rombios32_init();
251
252     memmap_finalize();
253
254     floppy_drive_setup();
255     hard_drive_setup();
256
257     init_boot_vectors();
258
259     dprintf(1, "Scan for option roms\n");
260     rom_scan(0xc8000, 0xe0000);
261 }
262
263 // Clear .bss section for C code.
264 static void
265 clear_bss()
266 {
267     dprintf(3, "clearing .bss section\n");
268     extern char __bss_start[], __bss_end[];
269     memset(__bss_start, 0, __bss_end - __bss_start);
270 }
271
272 // Reset DMA controller
273 static void
274 init_dma()
275 {
276     // first reset the DMA controllers
277     outb(0, PORT_DMA1_MASTER_CLEAR);
278     outb(0, PORT_DMA2_MASTER_CLEAR);
279
280     // then initialize the DMA controllers
281     outb(0xc0, PORT_DMA2_MODE_REG);
282     outb(0x00, PORT_DMA2_MASK_REG);
283 }
284
285 // Check if the machine was setup with a special restart vector.
286 static void
287 check_restart_status()
288 {
289     // Get and then clear CMOS shutdown status.
290     u8 status = inb_cmos(CMOS_RESET_CODE);
291     outb_cmos(0, CMOS_RESET_CODE);
292
293     if (status == 0x00 || status == 0x09 || status >= 0x0d)
294         // Normal post
295         return;
296
297     if (status != 0x05) {
298         BX_PANIC("Unimplemented shutdown status: %02x\n", status);
299         return;
300     }
301
302     // XXX - this is supposed to jump without changing any memory -
303     // but the stack has been altered by the time the code gets here.
304     eoi_pic2();
305     struct bregs br;
306     memset(&br, 0, sizeof(br));
307     br.cs = GET_BDA(jump_cs_ip) >> 16;
308     br.ip = GET_BDA(jump_cs_ip);
309     call16(&br);
310 }
311
312 // 32-bit entry point.
313 void VISIBLE32
314 _start()
315 {
316     init_dma();
317     check_restart_status();
318
319     debug_serial_setup();
320     dprintf(1, "Start bios\n");
321
322     // Setup for .bss and .data sections
323     clear_bss();
324     make_bios_writable();
325
326     // Perform main setup code.
327     post();
328
329     // Present the user with a bootup menu.
330     interactive_bootmenu();
331
332     // Setup bios checksum.
333     *(u8*)0xfffff = -checksum((u8*)0xf0000, 0xffff);
334
335     // Prep for boot process.
336     make_bios_readonly();
337     clear_bss();
338
339     // Invoke int 19 to start boot process.
340     dprintf(3, "Jump to int19\n");
341     struct bregs br;
342     memset(&br, 0, sizeof(br));
343     call16_int(0x19, &br);
344 }
345
346 // Externally visible 32bit entry point.
347 asm(
348     ".global post32\n"
349     "post32:\n"
350     "cli\n"
351     "cld\n"
352     "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
353     "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
354     "movl $" __stringify(BUILD_STACK_ADDR) ", %esp\n"
355     "ljmp $0x10, $_start\n"
356     );