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