1e351421034abf05b2ff233195dec40cbc022811
[seabios.git] / src / post.c
1 // 32bit code to Power On Self Test (POST) a machine.
2 //
3 // Copyright (C) 2008-2010  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 LGPLv3 license.
7
8 #include "ioport.h" // PORT_*
9 #include "config.h" // CONFIG_*
10 #include "cmos.h" // CMOS_*
11 #include "util.h" // memset
12 #include "biosvar.h" // struct bios_data_area_s
13 #include "disk.h" // floppy_drive_setup
14 #include "ata.h" // ata_setup
15 #include "memmap.h" // add_e820
16 #include "pic.h" // pic_setup
17 #include "pci.h" // create_pirtable
18 #include "acpi.h" // acpi_bios_init
19 #include "bregs.h" // struct bregs
20 #include "mptable.h" // mptable_init
21 #include "boot.h" // IPL
22 #include "usb.h" // usb_setup
23 #include "smbios.h" // smbios_init
24 #include "paravirt.h" // qemu_cfg_port_probe
25 #include "ps2port.h" // ps2port_setup
26 #include "virtio-blk.h" // virtio_blk_setup
27
28
29 /****************************************************************
30  * BIOS init
31  ****************************************************************/
32
33 static void
34 init_ivt(void)
35 {
36     dprintf(3, "init ivt\n");
37
38     // Initialize all vectors to the default handler.
39     int i;
40     for (i=0; i<256; i++)
41         SET_IVT(i, FUNC16(entry_iret_official));
42
43     // Initialize all hw vectors to a default hw handler.
44     for (i=0x08; i<=0x0f; i++)
45         SET_IVT(i, FUNC16(entry_hwpic1));
46     for (i=0x70; i<=0x77; i++)
47         SET_IVT(i, FUNC16(entry_hwpic2));
48
49     // Initialize software handlers.
50     SET_IVT(0x02, FUNC16(entry_02));
51     SET_IVT(0x10, FUNC16(entry_10));
52     SET_IVT(0x11, FUNC16(entry_11));
53     SET_IVT(0x12, FUNC16(entry_12));
54     SET_IVT(0x13, FUNC16(entry_13_official));
55     SET_IVT(0x14, FUNC16(entry_14));
56     SET_IVT(0x15, FUNC16(entry_15));
57     SET_IVT(0x16, FUNC16(entry_16));
58     SET_IVT(0x17, FUNC16(entry_17));
59     SET_IVT(0x18, FUNC16(entry_18));
60     SET_IVT(0x19, FUNC16(entry_19_official));
61     SET_IVT(0x1a, FUNC16(entry_1a));
62     SET_IVT(0x40, FUNC16(entry_40));
63
64     // INT 60h-66h reserved for user interrupt
65     for (i=0x60; i<=0x66; i++)
66         SET_IVT(i, SEGOFF(0, 0));
67
68     // set vector 0x79 to zero
69     // this is used by 'gardian angel' protection system
70     SET_IVT(0x79, SEGOFF(0, 0));
71
72     SET_IVT(0x1E, SEGOFF(SEG_BIOS, (u32)&diskette_param_table2 - BUILD_BIOS_ADDR));
73 }
74
75 static void
76 init_bda(void)
77 {
78     dprintf(3, "init bda\n");
79
80     struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
81     memset(bda, 0, sizeof(*bda));
82
83     int esize = EBDA_SIZE_START;
84     SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
85     u16 ebda_seg = EBDA_SEGMENT_START;
86     SET_BDA(ebda_seg, ebda_seg);
87
88     // Init ebda
89     struct extended_bios_data_area_s *ebda = get_ebda_ptr();
90     memset(ebda, 0, sizeof(*ebda));
91     ebda->size = esize;
92
93     add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
94              , E820_RESERVED);
95 }
96
97 static void
98 ram_probe(void)
99 {
100     dprintf(3, "Find memory size\n");
101     if (CONFIG_COREBOOT) {
102         coreboot_setup();
103     } else {
104         // On emulators, get memory size from nvram.
105         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
106                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
107         if (rs)
108             rs += 16 * 1024 * 1024;
109         else
110             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
111                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
112                   + 1 * 1024 * 1024);
113         RamSize = rs;
114         add_e820(0, rs, E820_RAM);
115
116         // Check for memory over 4Gig
117         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
118                     | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
119                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
120         RamSizeOver4G = high;
121         add_e820(0x100000000ull, high, E820_RAM);
122
123         /* reserve 256KB BIOS area at the end of 4 GB */
124         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
125     }
126
127     // Don't declare any memory between 0xa0000 and 0x100000
128     add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
129
130     // Mark known areas as reserved.
131     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
132
133     u32 count = qemu_cfg_e820_entries();
134     if (count) {
135         struct e820_reservation entry;
136         int i;
137
138         for (i = 0; i < count; i++) {
139             qemu_cfg_e820_load_next(&entry);
140             add_e820(entry.address, entry.length, entry.type);
141         }
142     } else if (kvm_para_available()) {
143         // Backwards compatibility - provide hard coded range.
144         // 4 pages before the bios, 3 pages for vmx tss pages, the
145         // other page for EPT real mode pagetable
146         add_e820(0xfffbc000, 4*4096, E820_RESERVED);
147     }
148
149     dprintf(1, "Ram Size=0x%08x (0x%08x%08x high)\n"
150             , RamSize, (u32)(RamSizeOver4G >> 32), (u32)RamSizeOver4G);
151 }
152
153 static void
154 init_bios_tables(void)
155 {
156     if (CONFIG_COREBOOT) {
157         coreboot_copy_biostable();
158         return;
159     }
160
161     create_pirtable();
162
163     mptable_init();
164
165     smbios_init();
166
167     acpi_bios_init();
168 }
169
170 // Initialize hardware devices
171 static void
172 init_hw(void)
173 {
174     usb_setup();
175     ps2port_setup();
176     lpt_setup();
177     serial_setup();
178
179     floppy_setup();
180     ata_setup();
181     ramdisk_setup();
182     virtio_blk_setup();
183 }
184
185 // Begin the boot process by invoking an int0x19 in 16bit mode.
186 void VISIBLE32FLAT
187 startBoot(void)
188 {
189     // Clear low-memory allocations (required by PMM spec).
190     memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
191
192     dprintf(3, "Jump to int19\n");
193     struct bregs br;
194     memset(&br, 0, sizeof(br));
195     br.flags = F_IF;
196     call16_int(0x19, &br);
197 }
198
199 // Main setup code.
200 static void
201 maininit(void)
202 {
203     // Setup ivt/bda/ebda
204     init_ivt();
205     init_bda();
206
207     // Init base pc hardware.
208     pic_setup();
209     timer_setup();
210     mathcp_setup();
211
212     // Initialize mtrr
213     mtrr_setup();
214
215     // Initialize pci
216     pci_setup();
217     smm_init();
218
219     // Initialize internal tables
220     boot_setup();
221
222     // Start hardware initialization (if optionrom threading)
223     if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
224         init_hw();
225
226     // Find and initialize other cpus
227     smp_probe();
228
229     // Setup interfaces that option roms may need
230     bios32_setup();
231     pmm_setup();
232     pnp_setup();
233     kbd_setup();
234     mouse_setup();
235     init_bios_tables();
236
237     // Run vga option rom
238     vga_setup();
239
240     // Do hardware initialization (if running synchronously)
241     if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
242         init_hw();
243         wait_threads();
244     }
245
246     // Run option roms
247     optionrom_setup();
248
249     // Run BCVs and show optional boot menu
250     boot_prep();
251
252     // Finalize data structures before boot
253     cdemu_setup();
254     pmm_finalize();
255     malloc_finalize();
256     memmap_finalize();
257
258     // Setup bios checksum.
259     BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
260
261     // Write protect bios memory.
262     make_bios_readonly();
263
264     // Invoke int 19 to start boot process.
265     startBoot();
266 }
267
268
269 /****************************************************************
270  * Code relocation
271  ****************************************************************/
272
273 // Update given relocs for the code at 'dest' with a given 'delta'
274 static void
275 updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
276 {
277     u32 *reloc;
278     for (reloc = rstart; reloc < rend; reloc++)
279         *((u32*)(dest + *reloc)) += delta;
280 }
281
282 // Start of Power On Self Test - the BIOS initilization.  This
283 // function sets up for and attempts relocation of the init code.
284 static void
285 reloc_init(void)
286 {
287     if (!CONFIG_RELOCATE_INIT) {
288         maininit();
289         return;
290     }
291     // Symbols populated by the build.
292     extern u8 code32flat_start[];
293     extern u8 _reloc_min_align[];
294     extern u32 _reloc_abs_start[], _reloc_abs_end[];
295     extern u32 _reloc_rel_start[], _reloc_rel_end[];
296     extern u32 _reloc_init_start[], _reloc_init_end[];
297     extern u8 code32init_start[], code32init_end[];
298
299     // Allocate space for init code.
300     u32 initsize = code32init_end - code32init_start;
301     u32 align = (u32)&_reloc_min_align;
302     void *dest = memalign_tmp(align, initsize);
303     if (!dest)
304         panic("No space for init relocation.\n");
305
306     // Copy code and update relocs (init absolute, init relative, and runtime)
307     dprintf(1, "Relocating init from %p to %p (size %d)\n"
308             , code32init_start, dest, initsize);
309     s32 delta = dest - (void*)code32init_start;
310     memcpy(dest, code32init_start, initsize);
311     updateRelocs(dest, _reloc_abs_start, _reloc_abs_end, delta);
312     updateRelocs(dest, _reloc_rel_start, _reloc_rel_end, -delta);
313     updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
314
315     // Call maininit() in relocated code.
316     void (*func)(void) = (void*)maininit + delta;
317     barrier();
318     func();
319 }
320
321 // Start of Power On Self Test (POST) - the BIOS initilization phase.
322 // This function sets up for and attempts relocation of the init code.
323 void VISIBLE32INIT
324 post(void)
325 {
326     // Detect ram and setup internal malloc.
327     qemu_cfg_port_probe();
328     ram_probe();
329     malloc_setup();
330
331     reloc_init();
332 }
333
334
335 /****************************************************************
336  * POST entry point
337  ****************************************************************/
338
339 static int HaveRunPost;
340
341 // Attempt to invoke a hard-reboot.
342 static void
343 tryReboot(void)
344 {
345     dprintf(1, "Attempting a hard reboot\n");
346
347     // Setup for reset on qemu.
348     if (! CONFIG_COREBOOT) {
349         qemu_prep_reset();
350         if (HaveRunPost)
351             apm_shutdown();
352     }
353
354     // Try keyboard controller reboot.
355     i8042_reboot();
356
357     // Try PCI 0xcf9 reboot
358     pci_reboot();
359
360     // Try triple fault
361     asm volatile("int3");
362
363     panic("Could not reboot");
364 }
365
366 // 32-bit entry point.
367 void VISIBLE32FLAT
368 _start(void)
369 {
370     init_dma();
371
372     debug_serial_setup();
373     dprintf(1, "Start bios (version %s)\n", VERSION);
374
375     if (HaveRunPost)
376         // This is a soft reboot - invoke a hard reboot.
377         tryReboot();
378
379     // Allow writes to modify bios area (0xf0000)
380     make_bios_writable();
381     HaveRunPost = 1;
382
383     // Perform main setup code.
384     post();
385 }