blob: f93106a1c9c9694f1412450f019b7a28cf222bdf [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 32bit code to Power On Self Test (POST) a machine.
2//
Kevin O'Connor8a0a9722013-01-21 01:53:31 -05003// Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor392d2aa2013-09-15 00:14:28 -04008#include "biosvar.h" // SET_BDA
Kevin O'Connor7c480762016-02-03 11:00:17 -05009#include "block.h" // block_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040010#include "bregs.h" // struct bregs
11#include "config.h" // CONFIG_*
Kevin O'Connorc167e542015-09-29 09:40:46 -040012#include "e820map.h" // e820_add
Kevin O'Connorccee6e82013-09-02 21:25:21 -040013#include "fw/paravirt.h" // qemu_cfg_preinit
14#include "fw/xen.h" // xen_preinit
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040015#include "hw/pic.h" // pic_setup
Kevin O'Connor5d369d82013-09-02 20:48:46 -040016#include "hw/ps2port.h" // ps2port_setup
Kevin O'Connor8b7861c2013-09-15 02:29:06 -040017#include "hw/rtc.h" // rtc_write
Kevin O'Connor940fc1f2014-01-15 13:42:50 -050018#include "hw/serialio.h" // serial_debug_preinit
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040019#include "hw/usb.h" // usb_setup
Kevin O'Connor9dea5902013-09-14 20:23:54 -040020#include "malloc.h" // malloc_init
Kevin O'Connor5d912262015-10-08 11:18:17 -040021#include "memmap.h" // SYMBOL
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040022#include "output.h" // dprintf
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040023#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040024#include "util.h" // kbd_init
Stefan Bergerb310dfa2015-03-23 14:22:16 -040025#include "tcgbios.h" // tpm_*
Kevin O'Connor025cabd2010-09-15 21:59:11 -040026
Kevin O'Connor8a0a9722013-01-21 01:53:31 -050027
Kevin O'Connor025cabd2010-09-15 21:59:11 -040028/****************************************************************
Kevin O'Connor6ca04602013-01-21 11:38:49 -050029 * BIOS initialization and hardware setup
Kevin O'Connor025cabd2010-09-15 21:59:11 -040030 ****************************************************************/
31
Kevin O'Connore3677b12008-07-04 15:29:23 -040032static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050033ivt_init(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050034{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050035 dprintf(3, "init ivt\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050036
Kevin O'Connord67a7032009-01-17 19:37:26 -050037 // Initialize all vectors to the default handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050038 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040039 for (i=0; i<256; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040040 SET_IVT(i, FUNC16(entry_iret_official));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050041
Kevin O'Connord21c0892008-11-26 17:02:43 -050042 // Initialize all hw vectors to a default hw handler.
Kevin O'Connorf465e1e2013-02-23 21:33:36 -050043 for (i=BIOS_HWIRQ0_VECTOR; i<BIOS_HWIRQ0_VECTOR+8; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040044 SET_IVT(i, FUNC16(entry_hwpic1));
Kevin O'Connorf465e1e2013-02-23 21:33:36 -050045 for (i=BIOS_HWIRQ8_VECTOR; i<BIOS_HWIRQ8_VECTOR+8; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040046 SET_IVT(i, FUNC16(entry_hwpic2));
Kevin O'Connord21c0892008-11-26 17:02:43 -050047
48 // Initialize software handlers.
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040049 SET_IVT(0x02, FUNC16(entry_02));
Kevin O'Connor642db192016-09-05 13:49:18 -040050 SET_IVT(0x05, FUNC16(entry_05));
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040051 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));
Kevin O'Connor3fcabf02014-05-08 18:32:32 -040056 SET_IVT(0x15, FUNC16(entry_15_official));
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040057 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));
Kevin O'Connor922aa1b2013-03-02 04:02:11 -050061 SET_IVT(0x1a, FUNC16(entry_1a_official));
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040062 SET_IVT(0x40, FUNC16(entry_40));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050063
Kevin O'Connorb164d2c2009-12-26 23:24:52 -050064 // INT 60h-66h reserved for user interrupt
65 for (i=0x60; i<=0x66; i++)
66 SET_IVT(i, SEGOFF(0, 0));
67
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050068 // set vector 0x79 to zero
69 // this is used by 'gardian angel' protection system
Kevin O'Connor9f985422009-09-09 11:34:39 -040070 SET_IVT(0x79, SEGOFF(0, 0));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050071}
72
73static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050074bda_init(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050075{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050076 dprintf(3, "init bda\n");
77
Kevin O'Connor35ae7262009-01-19 15:44:44 -050078 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050079 memset(bda, 0, sizeof(*bda));
80
Kevin O'Connor49553a42009-05-17 10:31:34 -040081 int esize = EBDA_SIZE_START;
Kevin O'Connor025cabd2010-09-15 21:59:11 -040082 u16 ebda_seg = EBDA_SEGMENT_START;
Kevin O'Connor2b0fb8c2013-08-07 23:03:47 -040083 if (!CONFIG_MALLOC_UPPERMEMORY)
Kevin O'Connor5d912262015-10-08 11:18:17 -040084 ebda_seg = FLATPTR_TO_SEG(ALIGN_DOWN(SYMBOL(final_varlow_start), 1024)
Kevin O'Connor2b0fb8c2013-08-07 23:03:47 -040085 - EBDA_SIZE_START*1024);
Kevin O'Connor025cabd2010-09-15 21:59:11 -040086 SET_BDA(ebda_seg, ebda_seg);
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050087
Kevin O'Connor2b0fb8c2013-08-07 23:03:47 -040088 SET_BDA(mem_size_kb, ebda_seg / (1024/16));
89
Kevin O'Connor9becbf12009-07-12 09:29:46 -040090 // Init ebda
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050091 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050092 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050093 ebda->size = esize;
Kevin O'Connor025cabd2010-09-15 21:59:11 -040094
Kevin O'Connorc167e542015-09-29 09:40:46 -040095 e820_add((u32)ebda, BUILD_LOWRAM_END-(u32)ebda, E820_RESERVED);
Kevin O'Connorbf2e8c22012-05-28 12:59:58 -040096
97 // Init extra stack
Kevin O'Connor5d912262015-10-08 11:18:17 -040098 StackPos = &ExtraStack[BUILD_EXTRA_STACK_SIZE] - SYMBOL(zonelow_base);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050099}
100
David Woodhousea2164512013-01-25 19:26:48 -0600101void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500102interface_init(void)
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400103{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500104 // Running at new code address - do code relocation fixups
Kevin O'Connor6afc6f82013-02-19 01:02:50 -0500105 malloc_init();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400106
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500107 // Setup romfile items.
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500108 qemu_cfg_init();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500109 coreboot_cbfs_init();
Vladimir Serbinenko1b911d72015-05-18 19:07:16 +0200110 multiboot_init();
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400111
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500112 // Setup ivt/bda/ebda
113 ivt_init();
114 bda_init();
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400115
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500116 // Other interfaces
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500117 boot_init();
118 bios32_init();
119 pmm_init();
120 pnp_init();
121 kbd_init();
122 mouse_init();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400123}
124
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400125// Initialize hardware devices
David Woodhousea2164512013-01-25 19:26:48 -0600126void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500127device_hardware_setup(void)
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400128{
129 usb_setup();
130 ps2port_setup();
Kevin O'Connor7c480762016-02-03 11:00:17 -0500131 block_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400132 lpt_setup();
133 serial_setup();
Kevin O'Connor89a1efd2011-01-08 12:24:39 -0500134 cbfs_payload_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400135}
136
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500137static void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500138platform_hardware_setup(void)
139{
Kevin O'Connor9e4d41c2013-01-21 12:14:29 -0500140 // Make sure legacy DMA isn't running.
141 dma_setup();
142
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500143 // Init base pc hardware.
144 pic_setup();
Kevin O'Connor8b9942f2015-07-14 15:44:26 -0400145 thread_setup();
Kevin O'Connor3a735ba2013-02-10 00:35:01 -0500146 mathcp_setup();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500147
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500148 // Platform specific setup
149 qemu_platform_setup();
150 coreboot_platform_setup();
Kevin O'Connord6aca442015-06-10 11:00:17 -0400151
Kevin O'Connorbbb3fba2015-07-22 16:06:27 -0400152 // Setup timers and periodic clock interrupt
153 timer_setup();
154 clock_setup();
155
Kevin O'Connord6aca442015-06-10 11:00:17 -0400156 // Initialize TPM
157 tpm_setup();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500158}
159
David Woodhousea2164512013-01-25 19:26:48 -0600160void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500161prepareboot(void)
162{
Kevin O'Connord6aca442015-06-10 11:00:17 -0400163 // Change TPM phys. presence state befor leaving BIOS
164 tpm_prepboot();
165
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500166 // Run BCVs
167 bcv_prepboot();
168
169 // Finalize data structures before boot
170 cdrom_prepboot();
171 pmm_prepboot();
172 malloc_prepboot();
Kevin O'Connorc167e542015-09-29 09:40:46 -0400173 e820_prepboot();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500174
Kevin O'Connor6c68e7a2014-04-19 12:22:22 -0400175 HaveRunPost = 2;
176
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500177 // Setup bios checksum.
178 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
179}
180
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400181// Begin the boot process by invoking an int0x19 in 16bit mode.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400182void VISIBLE32FLAT
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400183startBoot(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500184{
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400185 // Clear low-memory allocations (required by PMM spec).
186 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
187
188 dprintf(3, "Jump to int19\n");
189 struct bregs br;
190 memset(&br, 0, sizeof(br));
191 br.flags = F_IF;
192 call16_int(0x19, &br);
193}
194
195// Main setup code.
196static void
197maininit(void)
198{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500199 // Initialize internal interfaces.
200 interface_init();
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400201
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500202 // Setup platform devices.
203 platform_hardware_setup();
Kevin O'Connor98032802009-12-09 20:18:31 -0500204
Kevin O'Connord29ce622014-04-07 12:15:34 -0400205 // Start hardware initialization (if threads allowed during optionroms)
206 if (threads_during_optionroms())
Kevin O'Connor9f08c902013-02-10 00:25:53 -0500207 device_hardware_setup();
208
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400209 // Run vga option rom
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500210 vgarom_setup();
Gerd Hoffmannd6728f32017-09-18 10:47:23 +0200211 sercon_setup();
212 enable_vga_console();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400213
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400214 // Do hardware initialization (if running synchronously)
Kevin O'Connord29ce622014-04-07 12:15:34 -0400215 if (!threads_during_optionroms()) {
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500216 device_hardware_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400217 wait_threads();
218 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500219
Kevin O'Connor98032802009-12-09 20:18:31 -0500220 // Run option roms
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400221 optionrom_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400222
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500223 // Allow user to modify overall boot order.
224 interactive_bootmenu();
225 wait_threads();
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400226
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500227 // Prepare for boot.
228 prepareboot();
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400229
230 // Write protect bios memory.
231 make_bios_readonly();
232
233 // Invoke int 19 to start boot process.
234 startBoot();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500235}
236
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400237
238/****************************************************************
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400239 * POST entry and code relocation
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400240 ****************************************************************/
241
242// Update given relocs for the code at 'dest' with a given 'delta'
243static void
244updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
245{
246 u32 *reloc;
247 for (reloc = rstart; reloc < rend; reloc++)
248 *((u32*)(dest + *reloc)) += delta;
249}
250
Kevin O'Connorbc628092013-02-05 20:57:10 -0500251// Relocate init code and then call a function at its new address.
252// The passed function should be in the "init" section and must not
253// return.
David Woodhousea2164512013-01-25 19:26:48 -0600254void __noreturn
Kevin O'Connorbc628092013-02-05 20:57:10 -0500255reloc_preinit(void *f, void *arg)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400256{
Kevin O'Connorbc628092013-02-05 20:57:10 -0500257 void (*func)(void *) __noreturn = f;
258 if (!CONFIG_RELOCATE_INIT)
259 func(arg);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400260
261 // Allocate space for init code.
Kevin O'Connor5d912262015-10-08 11:18:17 -0400262 u32 initsize = SYMBOL(code32init_end) - SYMBOL(code32init_start);
263 u32 codealign = SYMBOL(_reloc_min_align);
Kevin O'Connor46b82622012-05-13 12:10:30 -0400264 void *codedest = memalign_tmp(codealign, initsize);
Kevin O'Connor5d912262015-10-08 11:18:17 -0400265 void *codesrc = VSYMBOL(code32init_start);
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400266 if (!codedest)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400267 panic("No space for init relocation.\n");
268
269 // Copy code and update relocs (init absolute, init relative, and runtime)
270 dprintf(1, "Relocating init from %p to %p (size %d)\n"
Kevin O'Connor5d912262015-10-08 11:18:17 -0400271 , codesrc, codedest, initsize);
272 s32 delta = codedest - codesrc;
273 memcpy(codedest, codesrc, initsize);
274 updateRelocs(codedest, VSYMBOL(_reloc_abs_start), VSYMBOL(_reloc_abs_end)
275 , delta);
276 updateRelocs(codedest, VSYMBOL(_reloc_rel_start), VSYMBOL(_reloc_rel_end)
277 , -delta);
278 updateRelocs(VSYMBOL(code32flat_start), VSYMBOL(_reloc_init_start)
279 , VSYMBOL(_reloc_init_end), delta);
280 if (f >= codesrc && f < VSYMBOL(code32init_end))
Kevin O'Connorbc628092013-02-05 20:57:10 -0500281 func = f + delta;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400282
Kevin O'Connorbc628092013-02-05 20:57:10 -0500283 // Call function in relocated code.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400284 barrier();
Kevin O'Connorbc628092013-02-05 20:57:10 -0500285 func(arg);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400286}
287
Kevin O'Connora48f6022016-01-12 14:22:33 -0500288// Runs after all code is present and prior to any modifications
289void
290code_mutable_preinit(void)
291{
292 if (HaveRunPost)
293 // Already run
294 return;
295 // Setup reset-vector entry point (controls legacy reboots).
296 rtc_write(CMOS_RESET_CODE, 0);
297 barrier();
298 HaveRunPost = 1;
299 barrier();
300}
301
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500302// Setup for code relocation and then relocate.
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400303void VISIBLE32INIT
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400304dopost(void)
305{
Kevin O'Connora48f6022016-01-12 14:22:33 -0500306 code_mutable_preinit();
307
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500308 // Detect ram and setup internal malloc.
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500309 qemu_preinit();
310 coreboot_preinit();
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500311 malloc_preinit();
312
313 // Relocate initialization code and call maininit().
314 reloc_preinit(maininit, NULL);
315}
316
Kevin O'Connorb1885fc2013-02-08 21:00:46 -0500317// Entry point for Power On Self Test (POST) - the BIOS initilization
318// phase. This function makes the memory at 0xc0000-0xfffff
319// read/writable and then calls dopost().
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500320void VISIBLE32FLAT
321handle_post(void)
322{
Kevin O'Connor4cf20672013-02-18 11:49:53 -0500323 if (!CONFIG_QEMU && !CONFIG_COREBOOT)
324 return;
325
Kevin O'Connor940fc1f2014-01-15 13:42:50 -0500326 serial_debug_preinit();
Kevin O'Connorbb1fcb42014-01-15 13:52:14 -0500327 debug_banner();
Kevin O'Connorb1885fc2013-02-08 21:00:46 -0500328
329 // Check if we are running under Xen.
330 xen_preinit();
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500331
Kevin O'Connora8999452010-09-25 12:48:43 -0400332 // Allow writes to modify bios area (0xf0000)
333 make_bios_writable();
Kevin O'Connora8999452010-09-25 12:48:43 -0400334
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400335 // Now that memory is read/writable - start post process.
336 dopost();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500337}