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