1 // 32bit code to Power On Self Test (POST) a machine.
2 //
3 // Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "biosvar.h" // SET_BDA
9 #include "block.h" // block_setup
10 #include "bregs.h" // struct bregs
11 #include "config.h" // CONFIG_*
12 #include "e820map.h" // e820_add
13 #include "fw/paravirt.h" // qemu_cfg_preinit
14 #include "fw/xen.h" // xen_preinit
15 #include "hw/pic.h" // pic_setup
16 #include "hw/ps2port.h" // ps2port_setup
17 #include "hw/rtc.h" // rtc_write
18 #include "hw/serialio.h" // serial_debug_preinit
19 #include "hw/usb.h" // usb_setup
20 #include "malloc.h" // malloc_init
21 #include "memmap.h" // SYMBOL
22 #include "output.h" // dprintf
23 #include "string.h" // memset
24 #include "util.h" // kbd_init
25 #include "tcgbios.h" // tpm_*
26
27
28 /****************************************************************
29 * BIOS initialization and hardware setup
30 ****************************************************************/
31
32 static void
ivt_init(void)33 ivt_init(void)
34 {
35 dprintf(3, "init ivt\n");
36
37 // Initialize all vectors to the default handler.
38 int i;
39 for (i=0; i<256; i++)
40 SET_IVT(i, FUNC16(entry_iret_official));
41
42 // Initialize all hw vectors to a default hw handler.
43 for (i=BIOS_HWIRQ0_VECTOR; i<BIOS_HWIRQ0_VECTOR+8; i++)
44 SET_IVT(i, FUNC16(entry_hwpic1));
45 for (i=BIOS_HWIRQ8_VECTOR; i<BIOS_HWIRQ8_VECTOR+8; i++)
46 SET_IVT(i, FUNC16(entry_hwpic2));
47
48 // Initialize software handlers.
49 SET_IVT(0x02, FUNC16(entry_02));
50 SET_IVT(0x05, FUNC16(entry_05));
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_official));
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_official));
62 SET_IVT(0x40, FUNC16(entry_40));
63
64 // INT 60h-66h reserved for user interrupt
65 for (i=0x60; i<=0x66; i++)
66 SET_IVT(i, SEGOFF(0, 0));
67
68 // set vector 0x79 to zero
69 // this is used by 'gardian angel' protection system
70 SET_IVT(0x79, SEGOFF(0, 0));
71 }
72
73 static void
bda_init(void)74 bda_init(void)
75 {
76 dprintf(3, "init bda\n");
77
78 struct bios_data_area_s *bda = get_bda_ptr();
79 memset(bda, 0, sizeof(*bda));
80
81 int esize = EBDA_SIZE_START;
82 u16 ebda_seg = EBDA_SEGMENT_START;
83 if (!CONFIG_MALLOC_UPPERMEMORY)
84 ebda_seg = FLATPTR_TO_SEG(ALIGN_DOWN(SYMBOL(final_varlow_start), 1024)
85 - EBDA_SIZE_START*1024);
86 SET_BDA(ebda_seg, ebda_seg);
87
88 SET_BDA(mem_size_kb, ebda_seg / (1024/16));
89
90 // Init ebda
91 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
92 memset(ebda, 0, sizeof(*ebda));
93 ebda->size = esize;
94
95 e820_add((u32)ebda, BUILD_LOWRAM_END-(u32)ebda, E820_RESERVED);
96
97 // Init extra stack
98 StackPos = &ExtraStack[BUILD_EXTRA_STACK_SIZE] - SYMBOL(zonelow_base);
99 }
100
101 void
interface_init(void)102 interface_init(void)
103 {
104 // Running at new code address - do code relocation fixups
105 malloc_init();
106
107 // Setup romfile items.
108 qemu_cfg_init();
109 coreboot_cbfs_init();
110 multiboot_init();
111
112 // Setup ivt/bda/ebda
113 ivt_init();
114 bda_init();
115
116 // Other interfaces
117 boot_init();
118 bios32_init();
119 pmm_init();
120 pnp_init();
121 kbd_init();
122 mouse_init();
123 }
124
125 // Initialize hardware devices
126 void
device_hardware_setup(void)127 device_hardware_setup(void)
128 {
129 usb_setup();
130 ps2port_setup();
131 block_setup();
132 lpt_setup();
133 serial_setup();
134 cbfs_payload_setup();
135 }
136
137 static void
platform_hardware_setup(void)138 platform_hardware_setup(void)
139 {
140 // Make sure legacy DMA isn't running.
141 dma_setup();
142
143 // Init base pc hardware.
144 pic_setup();
145 thread_setup();
146 mathcp_setup();
147
148 // Platform specific setup
149 qemu_platform_setup();
150 coreboot_platform_setup();
151
152 // Setup timers and periodic clock interrupt
153 timer_setup();
154 clock_setup();
155
156 // Initialize TPM
157 tpm_setup();
158 }
159
160 void
prepareboot(void)161 prepareboot(void)
162 {
163 // Change TPM phys. presence state befor leaving BIOS
164 tpm_prepboot();
165
166 // Run BCVs
167 bcv_prepboot();
168
169 // Finalize data structures before boot
170 cdrom_prepboot();
171 pmm_prepboot();
172 malloc_prepboot();
173 e820_prepboot();
174
175 HaveRunPost = 2;
176
177 // Setup bios checksum.
178 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
179 }
180
181 // Begin the boot process by invoking an int0x19 in 16bit mode.
182 void VISIBLE32FLAT
startBoot(void)183 startBoot(void)
184 {
185 // 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.
196 static void
maininit(void)197 maininit(void)
198 {
199 // Initialize internal interfaces.
200 interface_init();
201
202 // Setup platform devices.
203 platform_hardware_setup();
204
205 // Start hardware initialization (if threads allowed during optionroms)
206 if (threads_during_optionroms())
207 device_hardware_setup();
208
209 // Run vga option rom
210 vgarom_setup();
211 sercon_setup();
212 enable_vga_console();
213
214 // Do hardware initialization (if running synchronously)
215 if (!threads_during_optionroms()) {
216 device_hardware_setup();
217 wait_threads();
218 }
219
220 // Run option roms
221 optionrom_setup();
222
223 // Allow user to modify overall boot order.
224 interactive_bootmenu();
225 wait_threads();
226
227 // Prepare for boot.
228 prepareboot();
229
230 // Write protect bios memory.
231 make_bios_readonly();
232
233 // Invoke int 19 to start boot process.
234 startBoot();
235 }
236
237
238 /****************************************************************
239 * POST entry and code relocation
240 ****************************************************************/
241
242 // Update given relocs for the code at 'dest' with a given 'delta'
243 static void
updateRelocs(void * dest,u32 * rstart,u32 * rend,u32 delta)244 updateRelocs(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
251 // 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.
254 void __noreturn
reloc_preinit(void * f,void * arg)255 reloc_preinit(void *f, void *arg)
256 {
257 void (*func)(void *) __noreturn = f;
258 if (!CONFIG_RELOCATE_INIT)
259 func(arg);
260
261 // Allocate space for init code.
262 u32 initsize = SYMBOL(code32init_end) - SYMBOL(code32init_start);
263 u32 codealign = SYMBOL(_reloc_min_align);
264 void *codedest = memalign_tmp(codealign, initsize);
265 void *codesrc = VSYMBOL(code32init_start);
266 if (!codedest)
267 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"
271 , 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))
281 func = f + delta;
282
283 // Call function in relocated code.
284 barrier();
285 func(arg);
286 }
287
288 // Runs after all code is present and prior to any modifications
289 void
code_mutable_preinit(void)290 code_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
302 // Setup for code relocation and then relocate.
303 void VISIBLE32INIT
dopost(void)304 dopost(void)
305 {
306 code_mutable_preinit();
307
308 // Detect ram and setup internal malloc.
309 qemu_preinit();
310 coreboot_preinit();
311 malloc_preinit();
312
313 // Relocate initialization code and call maininit().
314 reloc_preinit(maininit, NULL);
315 }
316
317 // 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().
320 void VISIBLE32FLAT
handle_post(void)321 handle_post(void)
322 {
323 if (!CONFIG_QEMU && !CONFIG_COREBOOT)
324 return;
325
326 serial_debug_preinit();
327 debug_banner();
328
329 // Check if we are running under Xen.
330 xen_preinit();
331
332 // Allow writes to modify bios area (0xf0000)
333 make_bios_writable();
334
335 // Now that memory is read/writable - start post process.
336 dopost();
337 }
338