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