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