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 "fw/paravirt.h" // qemu_cfg_preinit
12 #include "fw/xen.h" // xen_preinit
13 #include "hw/ahci.h" // ahci_setup
14 #include "hw/ata.h" // ata_setup
15 #include "hw/esp-scsi.h" // esp_scsi_setup
16 #include "hw/lsi-scsi.h" // lsi_scsi_setup
17 #include "hw/megasas.h" // megasas_setup
18 #include "hw/pvscsi.h" // pvscsi_setup
19 #include "hw/pic.h" // pic_setup
20 #include "hw/ps2port.h" // ps2port_setup
21 #include "hw/rtc.h" // rtc_write
22 #include "hw/serialio.h" // serial_debug_preinit
23 #include "hw/usb.h" // usb_setup
24 #include "hw/virtio-blk.h" // virtio_blk_setup
25 #include "hw/virtio-scsi.h" // virtio_scsi_setup
26 #include "malloc.h" // malloc_init
27 #include "memmap.h" // add_e820
28 #include "output.h" // dprintf
29 #include "string.h" // memset
30 #include "util.h" // kbd_init
33 /****************************************************************
34 * BIOS initialization and hardware setup
35 ****************************************************************/
40 dprintf(3, "init ivt\n");
42 // Setup reset-vector entry point (controls legacy reboots).
44 rtc_write(CMOS_RESET_CODE, 0);
46 // Initialize all vectors to the default handler.
49 SET_IVT(i, FUNC16(entry_iret_official));
51 // Initialize all hw vectors to a default hw handler.
52 for (i=BIOS_HWIRQ0_VECTOR; i<BIOS_HWIRQ0_VECTOR+8; i++)
53 SET_IVT(i, FUNC16(entry_hwpic1));
54 for (i=BIOS_HWIRQ8_VECTOR; i<BIOS_HWIRQ8_VECTOR+8; i++)
55 SET_IVT(i, FUNC16(entry_hwpic2));
57 // Initialize software handlers.
58 SET_IVT(0x02, FUNC16(entry_02));
59 SET_IVT(0x10, FUNC16(entry_10));
60 SET_IVT(0x11, FUNC16(entry_11));
61 SET_IVT(0x12, FUNC16(entry_12));
62 SET_IVT(0x13, FUNC16(entry_13_official));
63 SET_IVT(0x14, FUNC16(entry_14));
64 SET_IVT(0x15, FUNC16(entry_15_official));
65 SET_IVT(0x16, FUNC16(entry_16));
66 SET_IVT(0x17, FUNC16(entry_17));
67 SET_IVT(0x18, FUNC16(entry_18));
68 SET_IVT(0x19, FUNC16(entry_19_official));
69 SET_IVT(0x1a, FUNC16(entry_1a_official));
70 SET_IVT(0x40, FUNC16(entry_40));
72 // INT 60h-66h reserved for user interrupt
73 for (i=0x60; i<=0x66; i++)
74 SET_IVT(i, SEGOFF(0, 0));
76 // set vector 0x79 to zero
77 // this is used by 'gardian angel' protection system
78 SET_IVT(0x79, SEGOFF(0, 0));
84 dprintf(3, "init bda\n");
86 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
87 memset(bda, 0, sizeof(*bda));
89 int esize = EBDA_SIZE_START;
90 u16 ebda_seg = EBDA_SEGMENT_START;
91 extern u8 final_varlow_start[];
92 if (!CONFIG_MALLOC_UPPERMEMORY)
93 ebda_seg = FLATPTR_TO_SEG(ALIGN_DOWN((u32)final_varlow_start, 1024)
94 - EBDA_SIZE_START*1024);
95 SET_BDA(ebda_seg, ebda_seg);
97 SET_BDA(mem_size_kb, ebda_seg / (1024/16));
100 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
101 memset(ebda, 0, sizeof(*ebda));
104 add_e820((u32)ebda, BUILD_LOWRAM_END-(u32)ebda, E820_RESERVED);
107 StackPos = (void*)(&ExtraStack[BUILD_EXTRA_STACK_SIZE] - zonelow_base);
113 // Running at new code address - do code relocation fixups
116 // Setup romfile items.
118 coreboot_cbfs_init();
120 // Setup ivt/bda/ebda
134 // Initialize hardware devices
136 device_hardware_setup(void)
147 cbfs_payload_setup();
158 platform_hardware_setup(void)
160 // Enable CPU caching
161 setcr0(getcr0() & ~(CR0_CD|CR0_NW));
163 // Make sure legacy DMA isn't running.
166 // Init base pc hardware.
172 // Platform specific setup
173 qemu_platform_setup();
174 coreboot_platform_setup();
183 // Finalize data structures before boot
191 // Setup bios checksum.
192 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
195 // Begin the boot process by invoking an int0x19 in 16bit mode.
199 // Clear low-memory allocations (required by PMM spec).
200 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
202 dprintf(3, "Jump to int19\n");
204 memset(&br, 0, sizeof(br));
206 call16_int(0x19, &br);
213 // Initialize internal interfaces.
216 // Setup platform devices.
217 platform_hardware_setup();
219 // Start hardware initialization (if threads allowed during optionroms)
220 if (threads_during_optionroms())
221 device_hardware_setup();
223 // Run vga option rom
226 // Do hardware initialization (if running synchronously)
227 if (!threads_during_optionroms()) {
228 device_hardware_setup();
235 // Allow user to modify overall boot order.
236 interactive_bootmenu();
242 // Write protect bios memory.
243 make_bios_readonly();
245 // Invoke int 19 to start boot process.
250 /****************************************************************
251 * POST entry and code relocation
252 ****************************************************************/
254 // Update given relocs for the code at 'dest' with a given 'delta'
256 updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
259 for (reloc = rstart; reloc < rend; reloc++)
260 *((u32*)(dest + *reloc)) += delta;
263 // Relocate init code and then call a function at its new address.
264 // The passed function should be in the "init" section and must not
267 reloc_preinit(void *f, void *arg)
269 void (*func)(void *) __noreturn = f;
270 if (!CONFIG_RELOCATE_INIT)
272 // Symbols populated by the build.
273 extern u8 code32flat_start[];
274 extern u8 _reloc_min_align;
275 extern u32 _reloc_abs_start[], _reloc_abs_end[];
276 extern u32 _reloc_rel_start[], _reloc_rel_end[];
277 extern u32 _reloc_init_start[], _reloc_init_end[];
278 extern u8 code32init_start[], code32init_end[];
280 // Allocate space for init code.
281 u32 initsize = code32init_end - code32init_start;
282 u32 codealign = (u32)&_reloc_min_align;
283 void *codedest = memalign_tmp(codealign, initsize);
285 panic("No space for init relocation.\n");
287 // Copy code and update relocs (init absolute, init relative, and runtime)
288 dprintf(1, "Relocating init from %p to %p (size %d)\n"
289 , code32init_start, codedest, initsize);
290 s32 delta = codedest - (void*)code32init_start;
291 memcpy(codedest, code32init_start, initsize);
292 updateRelocs(codedest, _reloc_abs_start, _reloc_abs_end, delta);
293 updateRelocs(codedest, _reloc_rel_start, _reloc_rel_end, -delta);
294 updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
295 if (f >= (void*)code32init_start && f < (void*)code32init_end)
298 // Call function in relocated code.
303 // Setup for code relocation and then relocate.
307 // Detect ram and setup internal malloc.
312 // Relocate initialization code and call maininit().
313 reloc_preinit(maininit, NULL);
316 // Entry point for Power On Self Test (POST) - the BIOS initilization
317 // phase. This function makes the memory at 0xc0000-0xfffff
318 // read/writable and then calls dopost().
322 if (!CONFIG_QEMU && !CONFIG_COREBOOT)
325 serial_debug_preinit();
328 // Check if we are running under Xen.
331 // Allow writes to modify bios area (0xf0000)
332 make_bios_writable();
334 // Now that memory is read/writable - start post process.