1 // 32bit code to Power On Self Test (POST) a machine.
3 // Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
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
29 /****************************************************************
31 ****************************************************************/
36 dprintf(3, "init ivt\n");
38 // Initialize all vectors to the default handler.
41 SET_IVT(i, FUNC16(entry_iret_official));
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));
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));
64 // INT 60h-66h reserved for user interrupt
65 for (i=0x60; i<=0x66; i++)
66 SET_IVT(i, SEGOFF(0, 0));
68 // set vector 0x79 to zero
69 // this is used by 'gardian angel' protection system
70 SET_IVT(0x79, SEGOFF(0, 0));
72 SET_IVT(0x1E, SEGOFF(SEG_BIOS, (u32)&diskette_param_table2 - BUILD_BIOS_ADDR));
78 dprintf(3, "init bda\n");
80 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
81 memset(bda, 0, sizeof(*bda));
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);
89 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
90 memset(ebda, 0, sizeof(*ebda));
93 add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
100 dprintf(3, "Find memory size\n");
101 if (CONFIG_COREBOOT) {
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));
108 rs += 16 * 1024 * 1024;
110 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
111 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
114 add_e820(0, rs, E820_RAM);
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);
123 /* reserve 256KB BIOS area at the end of 4 GB */
124 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
127 // Don't declare any memory between 0xa0000 and 0x100000
128 add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
130 // Mark known areas as reserved.
131 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
133 u32 count = qemu_cfg_e820_entries();
135 struct e820_reservation entry;
138 for (i = 0; i < count; i++) {
139 qemu_cfg_e820_load_next(&entry);
140 add_e820(entry.address, entry.length, entry.type);
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);
149 dprintf(1, "Ram Size=0x%08x (0x%08x%08x high)\n"
150 , RamSize, (u32)(RamSizeOver4G >> 32), (u32)RamSizeOver4G);
154 init_bios_tables(void)
156 if (CONFIG_COREBOOT) {
157 coreboot_copy_biostable();
170 // Initialize hardware devices
185 // Begin the boot process by invoking an int0x19 in 16bit mode.
189 // Clear low-memory allocations (required by PMM spec).
190 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
192 dprintf(3, "Jump to int19\n");
194 memset(&br, 0, sizeof(br));
196 call16_int(0x19, &br);
203 // Setup ivt/bda/ebda
207 // Init base pc hardware.
221 // Initialize internal tables
225 // Start hardware initialization (if optionrom threading)
226 if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
229 // Find and initialize other cpus
232 // Setup interfaces that option roms may need
240 // Run vga option rom
243 // Do hardware initialization (if running synchronously)
244 if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
252 // Run BCVs and show optional boot menu
255 // Finalize data structures before boot
261 // Setup bios checksum.
262 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
264 // Write protect bios memory.
265 make_bios_readonly();
267 // Invoke int 19 to start boot process.
272 /****************************************************************
274 ****************************************************************/
276 // Update given relocs for the code at 'dest' with a given 'delta'
278 updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
281 for (reloc = rstart; reloc < rend; reloc++)
282 *((u32*)(dest + *reloc)) += delta;
285 // Start of Power On Self Test - the BIOS initilization. This
286 // function sets up for and attempts relocation of the init code.
290 if (!CONFIG_RELOCATE_INIT) {
294 // Symbols populated by the build.
295 extern u8 code32flat_start[];
296 extern u8 _reloc_min_align[];
297 extern u32 _reloc_abs_start[], _reloc_abs_end[];
298 extern u32 _reloc_rel_start[], _reloc_rel_end[];
299 extern u32 _reloc_init_start[], _reloc_init_end[];
300 extern u8 code32init_start[], code32init_end[];
302 // Allocate space for init code.
303 u32 initsize = code32init_end - code32init_start;
304 u32 align = (u32)&_reloc_min_align;
305 void *dest = memalign_tmp(align, initsize);
307 panic("No space for init relocation.\n");
309 // Copy code and update relocs (init absolute, init relative, and runtime)
310 dprintf(1, "Relocating init from %p to %p (size %d)\n"
311 , code32init_start, dest, initsize);
312 s32 delta = dest - (void*)code32init_start;
313 memcpy(dest, code32init_start, initsize);
314 updateRelocs(dest, _reloc_abs_start, _reloc_abs_end, delta);
315 updateRelocs(dest, _reloc_rel_start, _reloc_rel_end, -delta);
316 updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
318 // Call maininit() in relocated code.
319 void (*func)(void) = (void*)maininit + delta;
324 static int HaveRunPost;
326 // Start of Power On Self Test (POST) - the BIOS initilization phase.
327 // This function sets up for and attempts relocation of the init code.
331 // Allow writes to modify bios area (0xf0000)
332 make_bios_writable();
336 // Detect ram and setup internal malloc.
338 qemu_cfg_port_probe();
346 /****************************************************************
348 ****************************************************************/
350 // Attempt to invoke a hard-reboot.
354 dprintf(1, "Attempting a hard reboot\n");
356 // Setup for reset on qemu.
357 if (! CONFIG_COREBOOT) {
363 // Try keyboard controller reboot.
366 // Try PCI 0xcf9 reboot
370 asm volatile("int3");
372 panic("Could not reboot");
375 // 32-bit entry point.
381 debug_serial_setup();
382 dprintf(1, "Start bios (version %s)\n", VERSION);
385 // This is a soft reboot - invoke a hard reboot.
388 // Perform main setup code.