Move acpi code out of rombios32.c; clean up use of fixed memory addresses.
[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
19 #define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
20 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
21
22 static void
23 init_bda()
24 {
25     dprintf(3, "init bda\n");
26     memset(bda, 0, sizeof(*bda));
27
28     int i;
29     for (i=0; i<256; i++) {
30         SET_BDA(ivecs[i].seg, SEG_BIOS);
31         SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
32     }
33
34     SET_BDA(mem_size_kb, BASE_MEM_IN_K);
35
36     // Set BDA Equipment Word - 0x06 = FPU enable, mouse installed
37     SET_BDA(equipment_list_flags, 0x06);
38
39     // set vector 0x79 to zero
40     // this is used by 'gardian angel' protection system
41     SET_BDA(ivecs[0x79].seg, 0);
42     SET_BDA(ivecs[0x79].offset, 0);
43
44     SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
45     SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
46     SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
47     SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
48     SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
49     SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
50     SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
51     SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
52     SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
53     SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
54     SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
55     SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
56     SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
57     SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
58     SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
59     SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
60     SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
61     SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
62     SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
63     SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
64
65     SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
66 }
67
68 static void
69 init_ebda()
70 {
71     memset(ebda, 0, sizeof(*ebda));
72     ebda->size = EBDA_SIZE;
73     SET_BDA(ebda_seg, SEG_EBDA);
74     SET_BDA(ivecs[0x41].seg, SEG_EBDA);
75     SET_BDA(ivecs[0x41].offset
76             , offsetof(struct extended_bios_data_area_s, fdpt[0]));
77     SET_BDA(ivecs[0x46].seg, SEG_EBDA);
78     SET_BDA(ivecs[0x41].offset
79             , offsetof(struct extended_bios_data_area_s, fdpt[1]));
80 }
81
82 static void
83 ram_probe(void)
84 {
85     dprintf(3, "Find memory size\n");
86     if (CONFIG_COREBOOT) {
87         coreboot_fill_map();
88     } else {
89         // On emulators, get memory size from nvram.
90         u32 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
91                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
92         if (rs)
93             rs += 16 * 1024 * 1024;
94         else
95             rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
96                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
97                   + 1 * 1024 * 1024);
98         SET_EBDA(ram_size, rs);
99         add_e820(0, rs, E820_RAM);
100
101         /* reserve 256KB BIOS area at the end of 4 GB */
102         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
103     }
104
105     // Mark known areas as reserved.
106     add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
107     add_e820((u32)MAKE_FARPTR(SEG_BIOS, 0), 0x10000, E820_RESERVED);
108
109     dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
110 }
111
112 static void
113 pic_setup()
114 {
115     dprintf(3, "init pic\n");
116     outb(0x11, PORT_PIC1);
117     outb(0x11, PORT_PIC2);
118     outb(0x08, PORT_PIC1_DATA);
119     outb(0x70, PORT_PIC2_DATA);
120     outb(0x04, PORT_PIC1_DATA);
121     outb(0x02, PORT_PIC2_DATA);
122     outb(0x01, PORT_PIC1_DATA);
123     outb(0x01, PORT_PIC2_DATA);
124     outb(0xb8, PORT_PIC1_DATA);
125     if (CONFIG_PS2_MOUSE)
126         outb(0x8f, PORT_PIC2_DATA);
127     else
128         outb(0x9f, PORT_PIC2_DATA);
129 }
130
131 static void
132 init_boot_vectors()
133 {
134     dprintf(3, "init boot device ordering\n");
135
136     // Floppy drive
137     struct ipl_entry_s *ip = &ebda->ipl.table[0];
138     ip->type = IPL_TYPE_FLOPPY;
139     ip++;
140
141     // First HDD
142     ip->type = IPL_TYPE_HARDDISK;
143     ip++;
144
145     // CDROM
146     if (CONFIG_CDROM_BOOT) {
147         ip->type = IPL_TYPE_CDROM;
148         ip++;
149     }
150
151     ebda->ipl.count = ip - ebda->ipl.table;
152     ebda->ipl.sequence = 0xffff;
153     if (CONFIG_COREBOOT) {
154         // XXX - hardcode defaults for coreboot.
155         ebda->ipl.bootorder = 0x00000231;
156         ebda->ipl.checkfloppysig = 1;
157     } else {
158         // On emulators, get boot order from nvram.
159         ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
160                                | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
161         if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
162             ebda->ipl.checkfloppysig = 1;
163     }
164 }
165
166 // Execute a given option rom.
167 static void
168 callrom(u16 seg, u16 offset)
169 {
170     struct bregs br;
171     memset(&br, 0, sizeof(br));
172     br.es = SEG_BIOS;
173     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
174     br.cs = seg;
175     br.ip = offset;
176     call16(&br);
177 }
178
179 // Find and run any "option roms" found in the given address range.
180 static void
181 rom_scan(u32 start, u32 end)
182 {
183     u8 *p = (u8*)start;
184     for (; p <= (u8*)end; p += 2048) {
185         u8 *rom = p;
186         if (*(u16*)rom != 0xaa55)
187             continue;
188         u32 len = rom[2] * 512;
189         if (checksum(rom, len) != 0)
190             continue;
191         p = (u8*)(((u32)p + len) / 2048 * 2048);
192         dprintf(1, "Running option rom at %p\n", rom+3);
193         callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
194
195         if (GET_BDA(ebda_seg) != SEG_EBDA)
196             BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
197                      , rom, SEG_EBDA, GET_BDA(ebda_seg));
198
199         // Look at the ROM's PnP Expansion header.  Properly, we're supposed
200         // to init all the ROMs and then go back and build an IPL table of
201         // all the bootable devices, but we can get away with one pass.
202         if (rom[0x1a] != '$' || rom[0x1b] != 'P'
203             || rom[0x1c] != 'n' || rom[0x1d] != 'P')
204             continue;
205         // 0x1A is also the offset into the expansion header of...
206         // the Bootstrap Entry Vector, or zero if there is none.
207         u16 entry = *(u16*)&rom[0x1a+0x1a];
208         if (!entry)
209             continue;
210         // Found a device that thinks it can boot the system.  Record
211         // its BEV and product name string.
212
213         if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
214             continue;
215
216         struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
217         ip->type = IPL_TYPE_BEV;
218         ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
219
220         u16 desc = *(u16*)&rom[0x1a+0x10];
221         if (desc)
222             ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
223
224         ebda->ipl.count++;
225     }
226 }
227
228 // Main setup code.
229 static void
230 post()
231 {
232     init_bda();
233     init_ebda();
234
235     timer_setup();
236     kbd_setup();
237     lpt_setup();
238     serial_setup();
239     pic_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_both_pics();
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     dprintf(1, "Start bios\n");
320
321     // Setup for .bss and .data sections
322     clear_bss();
323     make_bios_writable();
324
325     // Perform main setup code.
326     post();
327
328     // Present the user with a bootup menu.
329     interactive_bootmenu();
330
331     // Prep for boot process.
332     make_bios_readonly();
333     clear_bss();
334
335     // Invoke int 19 to start boot process.
336     dprintf(3, "Jump to int19\n");
337     struct bregs br;
338     memset(&br, 0, sizeof(br));
339     call16_int(0x19, &br);
340 }
341
342 // Externally visible 32bit entry point.
343 asm(
344     ".global post32\n"
345     "post32:\n"
346     "cli\n"
347     "cld\n"
348     "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
349     "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
350     "movl $" __stringify(BUILD_STACK_ADDR) ", %esp\n"
351     "ljmp $0x10, $_start\n"
352     );