1b26e2b41158d84ccf10806727ebb4da1627527a
[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     // Don't declare any memory between 0xa0000 and 0x100000
104     add_e820(0xa0000, 0x50000, E820_HOLE);
105
106     // Mark known areas as reserved.
107     add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
108     add_e820((u32)MAKE_FARPTR(SEG_BIOS, 0), 0x10000, E820_RESERVED);
109
110     dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
111 }
112
113 static void
114 init_boot_vectors()
115 {
116     dprintf(3, "init boot device ordering\n");
117
118     // Floppy drive
119     struct ipl_entry_s *ip = &ebda->ipl.table[0];
120     ip->type = IPL_TYPE_FLOPPY;
121     ip++;
122
123     // First HDD
124     ip->type = IPL_TYPE_HARDDISK;
125     ip++;
126
127     // CDROM
128     if (CONFIG_CDROM_BOOT) {
129         ip->type = IPL_TYPE_CDROM;
130         ip++;
131     }
132
133     ebda->ipl.count = ip - ebda->ipl.table;
134     ebda->ipl.sequence = 0xffff;
135     if (CONFIG_COREBOOT) {
136         // XXX - hardcode defaults for coreboot.
137         ebda->ipl.bootorder = 0x00000231;
138         ebda->ipl.checkfloppysig = 1;
139     } else {
140         // On emulators, get boot order from nvram.
141         ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
142                                | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
143         if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
144             ebda->ipl.checkfloppysig = 1;
145     }
146 }
147
148 // Execute a given option rom.
149 static void
150 callrom(u16 seg, u16 offset)
151 {
152     struct bregs br;
153     memset(&br, 0, sizeof(br));
154     br.es = SEG_BIOS;
155     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
156     br.cs = seg;
157     br.ip = offset;
158     call16(&br);
159
160     debug_serial_setup();
161 }
162
163 // Find and run any "option roms" found in the given address range.
164 static void
165 rom_scan(u32 start, u32 end)
166 {
167     u8 *p = (u8*)start;
168     for (; p <= (u8*)end; p += 2048) {
169         u8 *rom = p;
170         if (*(u16*)rom != 0xaa55)
171             continue;
172         u32 len = rom[2] * 512;
173         u8 sum = checksum(rom, len);
174         if (sum != 0) {
175             dprintf(1, "Found option rom with bad checksum:"
176                     " loc=%p len=%d sum=%x\n"
177                     , rom, len, sum);
178             continue;
179         }
180         p = (u8*)(((u32)p + len) / 2048 * 2048);
181         dprintf(1, "Running option rom at %p\n", rom+3);
182         callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
183
184         if (GET_BDA(ebda_seg) != SEG_EBDA)
185             BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
186                      , rom, SEG_EBDA, GET_BDA(ebda_seg));
187
188         // Look at the ROM's PnP Expansion header.  Properly, we're supposed
189         // to init all the ROMs and then go back and build an IPL table of
190         // all the bootable devices, but we can get away with one pass.
191         if (rom[0x1a] != '$' || rom[0x1b] != 'P'
192             || rom[0x1c] != 'n' || rom[0x1d] != 'P')
193             continue;
194         // 0x1A is also the offset into the expansion header of...
195         // the Bootstrap Entry Vector, or zero if there is none.
196         u16 entry = *(u16*)&rom[0x1a+0x1a];
197         if (!entry)
198             continue;
199         // Found a device that thinks it can boot the system.  Record
200         // its BEV and product name string.
201
202         if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
203             continue;
204
205         struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
206         ip->type = IPL_TYPE_BEV;
207         ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
208
209         u16 desc = *(u16*)&rom[0x1a+0x10];
210         if (desc)
211             ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
212
213         ebda->ipl.count++;
214     }
215 }
216
217 // Main setup code.
218 static void
219 post()
220 {
221     init_bda();
222     init_ebda();
223
224     pic_setup();
225     timer_setup();
226     kbd_setup();
227     lpt_setup();
228     serial_setup();
229     mouse_setup();
230     mathcp_setup();
231
232     memmap_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     memmap_finalize();
244
245     floppy_drive_setup();
246     hard_drive_setup();
247
248     init_boot_vectors();
249
250     dprintf(1, "Scan for option roms\n");
251     rom_scan(0xc8000, 0xe0000);
252 }
253
254 // Clear .bss section for C code.
255 static void
256 clear_bss()
257 {
258     dprintf(3, "clearing .bss section\n");
259     extern char __bss_start[], __bss_end[];
260     memset(__bss_start, 0, __bss_end - __bss_start);
261 }
262
263 // Reset DMA controller
264 static void
265 init_dma()
266 {
267     // first reset the DMA controllers
268     outb(0, PORT_DMA1_MASTER_CLEAR);
269     outb(0, PORT_DMA2_MASTER_CLEAR);
270
271     // then initialize the DMA controllers
272     outb(0xc0, PORT_DMA2_MODE_REG);
273     outb(0x00, PORT_DMA2_MASK_REG);
274 }
275
276 // Check if the machine was setup with a special restart vector.
277 static void
278 check_restart_status()
279 {
280     // Get and then clear CMOS shutdown status.
281     u8 status = inb_cmos(CMOS_RESET_CODE);
282     outb_cmos(0, CMOS_RESET_CODE);
283
284     if (status == 0x00 || status == 0x09 || status >= 0x0d)
285         // Normal post
286         return;
287
288     if (status != 0x05) {
289         BX_PANIC("Unimplemented shutdown status: %02x\n", status);
290         return;
291     }
292
293     // XXX - this is supposed to jump without changing any memory -
294     // but the stack has been altered by the time the code gets here.
295     eoi_pic2();
296     struct bregs br;
297     memset(&br, 0, sizeof(br));
298     br.cs = GET_BDA(jump_cs_ip) >> 16;
299     br.ip = GET_BDA(jump_cs_ip);
300     call16(&br);
301 }
302
303 // 32-bit entry point.
304 void VISIBLE32
305 _start()
306 {
307     init_dma();
308     check_restart_status();
309
310     debug_serial_setup();
311     dprintf(1, "Start bios\n");
312
313     // Setup for .bss and .data sections
314     clear_bss();
315     make_bios_writable();
316
317     // Perform main setup code.
318     post();
319
320     // Present the user with a bootup menu.
321     interactive_bootmenu();
322
323     // Prep for boot process.
324     make_bios_readonly();
325     clear_bss();
326
327     // Invoke int 19 to start boot process.
328     dprintf(3, "Jump to int19\n");
329     struct bregs br;
330     memset(&br, 0, sizeof(br));
331     call16_int(0x19, &br);
332 }
333
334 // Externally visible 32bit entry point.
335 asm(
336     ".global post32\n"
337     "post32:\n"
338     "cli\n"
339     "cld\n"
340     "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
341     "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
342     "movl $" __stringify(BUILD_STACK_ADDR) ", %esp\n"
343     "ljmp $0x10, $_start\n"
344     );