1 // 32bit code to Power On Self Test (POST) a machine.
3 // Copyright (C) 2008-2013 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 "biosvar.h" // SET_BDA
9 #include "bregs.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "e820map.h" // e820_add
12 #include "fw/paravirt.h" // qemu_cfg_preinit
13 #include "fw/xen.h" // xen_preinit
14 #include "hw/ahci.h" // ahci_setup
15 #include "hw/ata.h" // ata_setup
16 #include "hw/esp-scsi.h" // esp_scsi_setup
17 #include "hw/lsi-scsi.h" // lsi_scsi_setup
18 #include "hw/megasas.h" // megasas_setup
19 #include "hw/pvscsi.h" // pvscsi_setup
20 #include "hw/pic.h" // pic_setup
21 #include "hw/ps2port.h" // ps2port_setup
22 #include "hw/rtc.h" // rtc_write
23 #include "hw/serialio.h" // serial_debug_preinit
24 #include "hw/usb.h" // usb_setup
25 #include "hw/virtio-blk.h" // virtio_blk_setup
26 #include "hw/virtio-scsi.h" // virtio_scsi_setup
27 #include "malloc.h" // malloc_init
28 #include "memmap.h" // SYMBOL
29 #include "output.h" // dprintf
30 #include "string.h" // memset
31 #include "util.h" // kbd_init
32 #include "tcgbios.h" // tpm_*
35 /****************************************************************
36 * BIOS initialization and hardware setup
37 ****************************************************************/
42 dprintf(3, "init ivt\n");
44 // Setup reset-vector entry point (controls legacy reboots).
46 rtc_write(CMOS_RESET_CODE, 0);
48 // Initialize all vectors to the default handler.
51 SET_IVT(i, FUNC16(entry_iret_official));
53 // Initialize all hw vectors to a default hw handler.
54 for (i=BIOS_HWIRQ0_VECTOR; i<BIOS_HWIRQ0_VECTOR+8; i++)
55 SET_IVT(i, FUNC16(entry_hwpic1));
56 for (i=BIOS_HWIRQ8_VECTOR; i<BIOS_HWIRQ8_VECTOR+8; i++)
57 SET_IVT(i, FUNC16(entry_hwpic2));
59 // Initialize software handlers.
60 SET_IVT(0x02, FUNC16(entry_02));
61 SET_IVT(0x10, FUNC16(entry_10));
62 SET_IVT(0x11, FUNC16(entry_11));
63 SET_IVT(0x12, FUNC16(entry_12));
64 SET_IVT(0x13, FUNC16(entry_13_official));
65 SET_IVT(0x14, FUNC16(entry_14));
66 SET_IVT(0x15, FUNC16(entry_15_official));
67 SET_IVT(0x16, FUNC16(entry_16));
68 SET_IVT(0x17, FUNC16(entry_17));
69 SET_IVT(0x18, FUNC16(entry_18));
70 SET_IVT(0x19, FUNC16(entry_19_official));
71 SET_IVT(0x1a, FUNC16(entry_1a_official));
72 SET_IVT(0x40, FUNC16(entry_40));
74 // INT 60h-66h reserved for user interrupt
75 for (i=0x60; i<=0x66; i++)
76 SET_IVT(i, SEGOFF(0, 0));
78 // set vector 0x79 to zero
79 // this is used by 'gardian angel' protection system
80 SET_IVT(0x79, SEGOFF(0, 0));
86 dprintf(3, "init bda\n");
88 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
89 memset(bda, 0, sizeof(*bda));
91 int esize = EBDA_SIZE_START;
92 u16 ebda_seg = EBDA_SEGMENT_START;
93 if (!CONFIG_MALLOC_UPPERMEMORY)
94 ebda_seg = FLATPTR_TO_SEG(ALIGN_DOWN(SYMBOL(final_varlow_start), 1024)
95 - EBDA_SIZE_START*1024);
96 SET_BDA(ebda_seg, ebda_seg);
98 SET_BDA(mem_size_kb, ebda_seg / (1024/16));
101 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
102 memset(ebda, 0, sizeof(*ebda));
105 e820_add((u32)ebda, BUILD_LOWRAM_END-(u32)ebda, E820_RESERVED);
108 StackPos = &ExtraStack[BUILD_EXTRA_STACK_SIZE] - SYMBOL(zonelow_base);
114 // Running at new code address - do code relocation fixups
117 // Setup romfile items.
119 coreboot_cbfs_init();
122 // Setup ivt/bda/ebda
135 // Initialize hardware devices
137 device_hardware_setup(void)
148 cbfs_payload_setup();
159 platform_hardware_setup(void)
161 // Make sure legacy DMA isn't running.
164 // Init base pc hardware.
169 // Platform specific setup
170 qemu_platform_setup();
171 coreboot_platform_setup();
173 // Setup timers and periodic clock interrupt
184 // Change TPM phys. presence state befor leaving BIOS
190 // Finalize data structures before boot
198 // Setup bios checksum.
199 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
202 // Begin the boot process by invoking an int0x19 in 16bit mode.
206 // Clear low-memory allocations (required by PMM spec).
207 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
209 dprintf(3, "Jump to int19\n");
211 memset(&br, 0, sizeof(br));
213 call16_int(0x19, &br);
220 // Initialize internal interfaces.
223 // Setup platform devices.
224 platform_hardware_setup();
226 // Start hardware initialization (if threads allowed during optionroms)
227 if (threads_during_optionroms())
228 device_hardware_setup();
230 // Run vga option rom
233 // Do hardware initialization (if running synchronously)
234 if (!threads_during_optionroms()) {
235 device_hardware_setup();
242 // Allow user to modify overall boot order.
243 interactive_bootmenu();
249 // Write protect bios memory.
250 make_bios_readonly();
252 // Invoke int 19 to start boot process.
257 /****************************************************************
258 * POST entry and code relocation
259 ****************************************************************/
261 // Update given relocs for the code at 'dest' with a given 'delta'
263 updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
266 for (reloc = rstart; reloc < rend; reloc++)
267 *((u32*)(dest + *reloc)) += delta;
270 // Relocate init code and then call a function at its new address.
271 // The passed function should be in the "init" section and must not
274 reloc_preinit(void *f, void *arg)
276 void (*func)(void *) __noreturn = f;
277 if (!CONFIG_RELOCATE_INIT)
280 // Allocate space for init code.
281 u32 initsize = SYMBOL(code32init_end) - SYMBOL(code32init_start);
282 u32 codealign = SYMBOL(_reloc_min_align);
283 void *codedest = memalign_tmp(codealign, initsize);
284 void *codesrc = VSYMBOL(code32init_start);
286 panic("No space for init relocation.\n");
288 // Copy code and update relocs (init absolute, init relative, and runtime)
289 dprintf(1, "Relocating init from %p to %p (size %d)\n"
290 , codesrc, codedest, initsize);
291 s32 delta = codedest - codesrc;
292 memcpy(codedest, codesrc, initsize);
293 updateRelocs(codedest, VSYMBOL(_reloc_abs_start), VSYMBOL(_reloc_abs_end)
295 updateRelocs(codedest, VSYMBOL(_reloc_rel_start), VSYMBOL(_reloc_rel_end)
297 updateRelocs(VSYMBOL(code32flat_start), VSYMBOL(_reloc_init_start)
298 , VSYMBOL(_reloc_init_end), delta);
299 if (f >= codesrc && f < VSYMBOL(code32init_end))
302 // Call function in relocated code.
307 // Setup for code relocation and then relocate.
311 // Detect ram and setup internal malloc.
316 // Relocate initialization code and call maininit().
317 reloc_preinit(maininit, NULL);
320 // Entry point for Power On Self Test (POST) - the BIOS initilization
321 // phase. This function makes the memory at 0xc0000-0xfffff
322 // read/writable and then calls dopost().
326 if (!CONFIG_QEMU && !CONFIG_COREBOOT)
329 serial_debug_preinit();
332 // Check if we are running under Xen.
335 // Allow writes to modify bios area (0xf0000)
336 make_bios_writable();
338 // Now that memory is read/writable - start post process.