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
31 dprintf(3, "init ivt\n");
33 // Initialize all vectors to the default handler.
36 SET_IVT(i, FUNC16(entry_iret_official));
38 // Initialize all hw vectors to a default hw handler.
39 for (i=0x08; i<=0x0f; i++)
40 SET_IVT(i, FUNC16(entry_hwpic1));
41 for (i=0x70; i<=0x77; i++)
42 SET_IVT(i, FUNC16(entry_hwpic2));
44 // Initialize software handlers.
45 SET_IVT(0x02, FUNC16(entry_02));
46 SET_IVT(0x10, FUNC16(entry_10));
47 SET_IVT(0x11, FUNC16(entry_11));
48 SET_IVT(0x12, FUNC16(entry_12));
49 SET_IVT(0x13, FUNC16(entry_13_official));
50 SET_IVT(0x14, FUNC16(entry_14));
51 SET_IVT(0x15, FUNC16(entry_15));
52 SET_IVT(0x16, FUNC16(entry_16));
53 SET_IVT(0x17, FUNC16(entry_17));
54 SET_IVT(0x18, FUNC16(entry_18));
55 SET_IVT(0x19, FUNC16(entry_19_official));
56 SET_IVT(0x1a, FUNC16(entry_1a));
57 SET_IVT(0x40, FUNC16(entry_40));
59 // INT 60h-66h reserved for user interrupt
60 for (i=0x60; i<=0x66; i++)
61 SET_IVT(i, SEGOFF(0, 0));
63 // set vector 0x79 to zero
64 // this is used by 'gardian angel' protection system
65 SET_IVT(0x79, SEGOFF(0, 0));
67 SET_IVT(0x1E, SEGOFF(SEG_BIOS, (u32)&diskette_param_table2 - BUILD_BIOS_ADDR));
73 dprintf(3, "init bda\n");
75 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
76 memset(bda, 0, sizeof(*bda));
78 int esize = EBDA_SIZE_START;
79 SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
80 u16 eseg = EBDA_SEGMENT_START;
81 SET_BDA(ebda_seg, eseg);
84 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
85 memset(ebda, 0, sizeof(*ebda));
92 dprintf(3, "Find memory size\n");
93 if (CONFIG_COREBOOT) {
96 // On emulators, get memory size from nvram.
97 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
98 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
100 rs += 16 * 1024 * 1024;
102 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
103 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
106 add_e820(0, rs, E820_RAM);
108 // Check for memory over 4Gig
109 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
110 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
111 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
112 RamSizeOver4G = high;
113 add_e820(0x100000000ull, high, E820_RAM);
115 /* reserve 256KB BIOS area at the end of 4 GB */
116 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
119 // Don't declare any memory between 0xa0000 and 0x100000
120 add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
122 // Mark known areas as reserved.
123 u16 ebda_seg = get_ebda_seg();
124 add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
126 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
128 u32 count = qemu_cfg_e820_entries();
130 struct e820_reservation entry;
133 for (i = 0; i < count; i++) {
134 qemu_cfg_e820_load_next(&entry);
135 add_e820(entry.address, entry.length, entry.type);
137 } else if (kvm_para_available()) {
138 // Backwards compatibility - provide hard coded range.
139 // 4 pages before the bios, 3 pages for vmx tss pages, the
140 // other page for EPT real mode pagetable
141 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
144 dprintf(1, "Ram Size=0x%08x (0x%08x%08x high)\n"
145 , RamSize, (u32)(RamSizeOver4G >> 32), (u32)RamSizeOver4G);
149 init_bios_tables(void)
151 if (CONFIG_COREBOOT) {
152 coreboot_copy_biostable();
165 // Initialize hardware devices
184 // Detect and init ram.
188 qemu_cfg_port_probe();
193 // Init base pc hardware.
206 // Initialize internal tables
210 // Start hardware initialization (if optionrom threading)
211 if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
214 // Find and initialize other cpus
217 // Setup interfaces that option roms may need
225 // Run vga option rom
228 // Do hardware initialization (if running synchronously)
229 if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
237 // Run BCVs and show optional boot menu
240 // Finalize data structures before boot
247 // 32-bit entry point.
253 debug_serial_setup();
254 dprintf(1, "Start bios (version %s)\n", VERSION);
256 // Allow writes to modify bios area (0xf0000)
257 make_bios_writable();
259 // Perform main setup code.
262 // Setup bios checksum.
263 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
265 // Write protect bios memory.
266 make_bios_readonly();
268 // Disable bootsplash if something has hooked int19.
269 if (GET_IVT(0x19).segoff != FUNC16(entry_19_official).segoff)
270 disable_bootsplash();
272 // Invoke int 19 to start boot process.
273 dprintf(3, "Jump to int19\n");
275 memset(&br, 0, sizeof(br));
277 call16_int(0x19, &br);