blob: 924b3111c35bb35fb5d3c1fadde545a89ba6fa4a [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'Connorcadaf0e2010-07-26 23:47:26 -04003// Copyright (C) 2008-2010 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
8#include "ioport.h" // PORT_*
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05009#include "config.h" // CONFIG_*
10#include "cmos.h" // CMOS_*
11#include "util.h" // memset
12#include "biosvar.h" // struct bios_data_area_s
Kevin O'Connor3bbcc142008-04-13 17:07:33 -040013#include "disk.h" // floppy_drive_setup
Kevin O'Connorc892b132009-08-11 21:59:37 -040014#include "ata.h" // ata_setup
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010015#include "ahci.h" // ahci_setup
Kevin O'Connorc7812932008-06-08 23:08:12 -040016#include "memmap.h" // add_e820
Kevin O'Connorf54c1502008-06-14 15:56:16 -040017#include "pic.h" // pic_setup
Kevin O'Connor0525d292008-07-04 06:18:30 -040018#include "pci.h" // create_pirtable
19#include "acpi.h" // acpi_bios_init
Kevin O'Connor9521e262008-07-04 13:04:29 -040020#include "bregs.h" // struct bregs
Kevin O'Connor35746442009-02-27 20:54:51 -050021#include "mptable.h" // mptable_init
Kevin O'Connorc659fde2008-12-28 23:43:20 -050022#include "boot.h" // IPL
Kevin O'Connor114592f2009-09-28 21:32:08 -040023#include "usb.h" // usb_setup
Kevin O'Connor01a85202009-10-18 09:49:59 -040024#include "smbios.h" // smbios_init
25#include "paravirt.h" // qemu_cfg_port_probe
Ian Campbell74c78782011-06-01 11:00:29 +010026#include "xen.h" // xen_probe_hvm_info
Kevin O'Connor57877482009-12-09 21:00:41 -050027#include "ps2port.h" // ps2port_setup
Gleb Natapov89acfa32010-05-10 11:36:37 +030028#include "virtio-blk.h" // virtio_blk_setup
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010029#include "virtio-scsi.h" // virtio_scsi_setup
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +020030#include "lsi-scsi.h" // lsi_scsi_setup
Paolo Bonzini7a39e722012-08-06 13:15:06 +020031#include "esp-scsi.h" // esp_scsi_setup
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050032
Kevin O'Connor025cabd2010-09-15 21:59:11 -040033
34/****************************************************************
35 * BIOS init
36 ****************************************************************/
37
Kevin O'Connore3677b12008-07-04 15:29:23 -040038static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050039init_ivt(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050040{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050041 dprintf(3, "init ivt\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050042
Kevin O'Connord67a7032009-01-17 19:37:26 -050043 // Initialize all vectors to the default handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050044 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040045 for (i=0; i<256; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040046 SET_IVT(i, FUNC16(entry_iret_official));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050047
Kevin O'Connord21c0892008-11-26 17:02:43 -050048 // Initialize all hw vectors to a default hw handler.
49 for (i=0x08; i<=0x0f; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040050 SET_IVT(i, FUNC16(entry_hwpic1));
Kevin O'Connord21c0892008-11-26 17:02:43 -050051 for (i=0x70; i<=0x77; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040052 SET_IVT(i, FUNC16(entry_hwpic2));
Kevin O'Connord21c0892008-11-26 17:02:43 -050053
54 // Initialize software handlers.
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040055 SET_IVT(0x02, FUNC16(entry_02));
56 SET_IVT(0x10, FUNC16(entry_10));
57 SET_IVT(0x11, FUNC16(entry_11));
58 SET_IVT(0x12, FUNC16(entry_12));
59 SET_IVT(0x13, FUNC16(entry_13_official));
60 SET_IVT(0x14, FUNC16(entry_14));
61 SET_IVT(0x15, FUNC16(entry_15));
62 SET_IVT(0x16, FUNC16(entry_16));
63 SET_IVT(0x17, FUNC16(entry_17));
64 SET_IVT(0x18, FUNC16(entry_18));
65 SET_IVT(0x19, FUNC16(entry_19_official));
66 SET_IVT(0x1a, FUNC16(entry_1a));
67 SET_IVT(0x40, FUNC16(entry_40));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050068
Kevin O'Connorb164d2c2009-12-26 23:24:52 -050069 // INT 60h-66h reserved for user interrupt
70 for (i=0x60; i<=0x66; i++)
71 SET_IVT(i, SEGOFF(0, 0));
72
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050073 // set vector 0x79 to zero
74 // this is used by 'gardian angel' protection system
Kevin O'Connor9f985422009-09-09 11:34:39 -040075 SET_IVT(0x79, SEGOFF(0, 0));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050076
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040077 SET_IVT(0x1E, SEGOFF(SEG_BIOS, (u32)&diskette_param_table2 - BUILD_BIOS_ADDR));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050078}
79
80static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050081init_bda(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050082{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050083 dprintf(3, "init bda\n");
84
Kevin O'Connor35ae7262009-01-19 15:44:44 -050085 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050086 memset(bda, 0, sizeof(*bda));
87
Kevin O'Connor49553a42009-05-17 10:31:34 -040088 int esize = EBDA_SIZE_START;
Kevin O'Connorf416fe92009-09-24 20:51:55 -040089 SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
Kevin O'Connor025cabd2010-09-15 21:59:11 -040090 u16 ebda_seg = EBDA_SEGMENT_START;
91 SET_BDA(ebda_seg, ebda_seg);
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050092
Kevin O'Connor9becbf12009-07-12 09:29:46 -040093 // Init ebda
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050094 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050095 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050096 ebda->size = esize;
Kevin O'Connor025cabd2010-09-15 21:59:11 -040097
Kevin O'Connor4bc49972012-05-13 22:58:08 -040098 add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA(ebda_seg, size) * 1024
Kevin O'Connor025cabd2010-09-15 21:59:11 -040099 , E820_RESERVED);
Kevin O'Connorbf2e8c22012-05-28 12:59:58 -0400100
101 // Init extra stack
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400102 StackPos = (void*)(&ExtraStack[BUILD_EXTRA_STACK_SIZE] - datalow_base);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500103}
104
105static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400106ram_probe(void)
107{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400108 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400109 if (CONFIG_COREBOOT) {
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400110 coreboot_setup();
Ian Campbell74c78782011-06-01 11:00:29 +0100111 } else if (usingXen()) {
Kevin O'Connor7a08ae72012-03-14 21:11:39 -0400112 xen_setup();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400113 } else {
114 // On emulators, get memory size from nvram.
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400115 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
116 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400117 if (rs)
118 rs += 16 * 1024 * 1024;
119 else
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400120 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
121 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400122 + 1 * 1024 * 1024);
Kevin O'Connore7916362008-12-28 22:03:17 -0500123 RamSize = rs;
Kevin O'Connorc7812932008-06-08 23:08:12 -0400124 add_e820(0, rs, E820_RAM);
125
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400126 // Check for memory over 4Gig
127 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
Kevin O'Connor669c9912010-01-05 19:50:04 -0500128 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400129 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
Kevin O'Connore7916362008-12-28 22:03:17 -0500130 RamSizeOver4G = high;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400131 add_e820(0x100000000ull, high, E820_RAM);
132
Kevin O'Connorc7812932008-06-08 23:08:12 -0400133 /* reserve 256KB BIOS area at the end of 4 GB */
134 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400135 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400136
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400137 // Don't declare any memory between 0xa0000 and 0x100000
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400138 add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400139
Kevin O'Connorc7812932008-06-08 23:08:12 -0400140 // Mark known areas as reserved.
Kevin O'Connore3677b12008-07-04 15:29:23 -0400141 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400142
Jes Sorensen0360e8e2010-02-16 09:46:08 +0100143 u32 count = qemu_cfg_e820_entries();
144 if (count) {
145 struct e820_reservation entry;
146 int i;
147
148 for (i = 0; i < count; i++) {
149 qemu_cfg_e820_load_next(&entry);
150 add_e820(entry.address, entry.length, entry.type);
151 }
152 } else if (kvm_para_available()) {
153 // Backwards compatibility - provide hard coded range.
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500154 // 4 pages before the bios, 3 pages for vmx tss pages, the
155 // other page for EPT real mode pagetable
156 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
Jes Sorensen0360e8e2010-02-16 09:46:08 +0100157 }
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500158
Kevin O'Connorc04090d2012-03-05 10:14:07 -0500159 dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400160}
161
162static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400163init_bios_tables(void)
164{
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400165 if (CONFIG_COREBOOT) {
166 coreboot_copy_biostable();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400167 return;
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400168 }
Ian Campbell74c78782011-06-01 11:00:29 +0100169 if (usingXen()) {
Kevin O'Connor7a08ae72012-03-14 21:11:39 -0400170 xen_copy_biostables();
171 return;
Ian Campbell74c78782011-06-01 11:00:29 +0100172 }
Kevin O'Connor0525d292008-07-04 06:18:30 -0400173
Kevin O'Connor0525d292008-07-04 06:18:30 -0400174 create_pirtable();
175
176 mptable_init();
177
178 smbios_init();
179
180 acpi_bios_init();
181}
182
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400183// Initialize hardware devices
184static void
185init_hw(void)
186{
187 usb_setup();
188 ps2port_setup();
189 lpt_setup();
190 serial_setup();
191
192 floppy_setup();
193 ata_setup();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100194 ahci_setup();
Kevin O'Connor89a1efd2011-01-08 12:24:39 -0500195 cbfs_payload_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400196 ramdisk_setup();
Gleb Natapov89acfa32010-05-10 11:36:37 +0300197 virtio_blk_setup();
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100198 virtio_scsi_setup();
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200199 lsi_scsi_setup();
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200200 esp_scsi_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400201}
202
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400203// Begin the boot process by invoking an int0x19 in 16bit mode.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400204void VISIBLE32FLAT
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400205startBoot(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500206{
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400207 // Clear low-memory allocations (required by PMM spec).
208 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
209
210 dprintf(3, "Jump to int19\n");
211 struct bregs br;
212 memset(&br, 0, sizeof(br));
213 br.flags = F_IF;
214 call16_int(0x19, &br);
215}
216
217// Main setup code.
218static void
219maininit(void)
220{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400221 // Setup romfile items.
222 qemu_cfg_romfile_setup();
223 coreboot_cbfs_setup();
224
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400225 // Setup ivt/bda/ebda
Kevin O'Connor8c0e3722009-01-02 14:19:43 -0500226 init_ivt();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500227 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500228
Kevin O'Connor969df602009-11-25 19:05:04 -0500229 // Init base pc hardware.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400230 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400231 timer_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400232 mathcp_setup();
233
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400234 // Initialize mtrr
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500235 mtrr_setup();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500236
Kevin O'Connor969df602009-11-25 19:05:04 -0500237 // Initialize pci
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400238 pci_setup();
239 smm_init();
240
Ian Campbell7fc039e2011-07-13 11:46:40 +0100241 // Setup Xen hypercalls
242 xen_init_hypercalls();
243
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400244 // Initialize internal tables
245 boot_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400246
247 // Start hardware initialization (if optionrom threading)
248 if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
249 init_hw();
250
251 // Find and initialize other cpus
252 smp_probe();
253
Kevin O'Connor98032802009-12-09 20:18:31 -0500254 // Setup interfaces that option roms may need
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500255 bios32_setup();
Kevin O'Connor969df602009-11-25 19:05:04 -0500256 pmm_setup();
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500257 pnp_setup();
Kevin O'Connor57877482009-12-09 21:00:41 -0500258 kbd_setup();
259 mouse_setup();
Kevin O'Connor98032802009-12-09 20:18:31 -0500260 init_bios_tables();
261
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400262 // Run vga option rom
263 vga_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400264
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400265 // Do hardware initialization (if running synchronously)
266 if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
267 init_hw();
268 wait_threads();
269 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500270
Kevin O'Connor98032802009-12-09 20:18:31 -0500271 // Run option roms
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400272 optionrom_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400273
Kevin O'Connor969df602009-11-25 19:05:04 -0500274 // Run BCVs and show optional boot menu
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400275 boot_prep();
276
Kevin O'Connor969df602009-11-25 19:05:04 -0500277 // Finalize data structures before boot
Kevin O'Connord5d02b62010-06-06 16:18:03 -0400278 cdemu_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400279 pmm_finalize();
280 malloc_finalize();
281 memmap_finalize();
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400282
283 // Setup bios checksum.
284 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
285
286 // Write protect bios memory.
287 make_bios_readonly();
288
289 // Invoke int 19 to start boot process.
290 startBoot();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500291}
292
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400293
294/****************************************************************
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400295 * POST entry and code relocation
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400296 ****************************************************************/
297
Kevin O'Connor46b82622012-05-13 12:10:30 -0400298// Relocation fixup code that runs at new address after relocation complete.
299static void
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400300afterReloc(void)
Kevin O'Connor46b82622012-05-13 12:10:30 -0400301{
302 // Running at new code address - do code relocation fixups
303 malloc_fixupreloc();
304
305 // Move low-memory initial variable content to new location.
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400306 extern u8 datalow_start[], datalow_end[], final_datalow_start[];
307 memmove(final_datalow_start, datalow_start, datalow_end - datalow_start);
Kevin O'Connor46b82622012-05-13 12:10:30 -0400308
309 // Run main code
310 maininit();
311}
312
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400313// Update given relocs for the code at 'dest' with a given 'delta'
314static void
315updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
316{
317 u32 *reloc;
318 for (reloc = rstart; reloc < rend; reloc++)
319 *((u32*)(dest + *reloc)) += delta;
320}
321
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400322// Relocate init code and then call maininit() at new address.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400323static void
324reloc_init(void)
325{
326 if (!CONFIG_RELOCATE_INIT) {
327 maininit();
328 return;
329 }
330 // Symbols populated by the build.
331 extern u8 code32flat_start[];
Kevin O'Connor46b82622012-05-13 12:10:30 -0400332 extern u8 _reloc_min_align;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400333 extern u32 _reloc_abs_start[], _reloc_abs_end[];
334 extern u32 _reloc_rel_start[], _reloc_rel_end[];
335 extern u32 _reloc_init_start[], _reloc_init_end[];
336 extern u8 code32init_start[], code32init_end[];
Kevin O'Connor46b82622012-05-13 12:10:30 -0400337 extern u32 _reloc_datalow_start[], _reloc_datalow_end[];
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400338 extern u8 datalow_start[], datalow_end[], final_datalow_start[];
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400339
340 // Allocate space for init code.
341 u32 initsize = code32init_end - code32init_start;
Kevin O'Connor46b82622012-05-13 12:10:30 -0400342 u32 codealign = (u32)&_reloc_min_align;
343 void *codedest = memalign_tmp(codealign, initsize);
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400344 if (!codedest)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400345 panic("No space for init relocation.\n");
346
347 // Copy code and update relocs (init absolute, init relative, and runtime)
Kevin O'Connor46b82622012-05-13 12:10:30 -0400348 dprintf(1, "Relocating low data from %p to %p (size %d)\n"
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400349 , datalow_start, final_datalow_start, datalow_end - datalow_start);
Kevin O'Connor46b82622012-05-13 12:10:30 -0400350 updateRelocs(code32flat_start, _reloc_datalow_start, _reloc_datalow_end
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400351 , final_datalow_start - datalow_start);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400352 dprintf(1, "Relocating init from %p to %p (size %d)\n"
Kevin O'Connor46b82622012-05-13 12:10:30 -0400353 , code32init_start, codedest, initsize);
354 s32 delta = codedest - (void*)code32init_start;
355 memcpy(codedest, code32init_start, initsize);
356 updateRelocs(codedest, _reloc_abs_start, _reloc_abs_end, delta);
357 updateRelocs(codedest, _reloc_rel_start, _reloc_rel_end, -delta);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400358 updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
359
360 // Call maininit() in relocated code.
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400361 void (*func)(void) = (void*)afterReloc + delta;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400362 barrier();
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400363 func();
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400364}
365
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400366// Setup for code relocation and then call reloc_init
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400367void VISIBLE32INIT
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400368dopost(void)
369{
370 HaveRunPost = 1;
371
372 // Detect ram and setup internal malloc.
373 qemu_cfg_port_probe();
374 ram_probe();
375 malloc_setup();
376
377 // Relocate initialization code and call maininit().
378 reloc_init();
379}
380
381// Entry point for Power On Self Test (POST) - the BIOS initilization
382// phase. This function makes the memory at 0xc0000-0xfffff
383// read/writable and then calls dopost().
384void VISIBLE32FLAT
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400385handle_post(void)
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400386{
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400387 debug_serial_setup();
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400388 dprintf(1, "Start bios (version %s)\n", VERSION);
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400389
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400390 // Enable CPU caching
391 setcr0(getcr0() & ~(CR0_CD|CR0_NW));
392
393 // Clear CMOS reboot flag.
394 outb_cmos(0, CMOS_RESET_CODE);
395
396 // Make sure legacy DMA isn't running.
397 init_dma();
Kevin O'Connoradaf3732010-09-13 20:22:07 -0400398
Ian Campbell74c78782011-06-01 11:00:29 +0100399 // Check if we are running under Xen.
400 xen_probe();
401
Kevin O'Connora8999452010-09-25 12:48:43 -0400402 // Allow writes to modify bios area (0xf0000)
403 make_bios_writable();
Kevin O'Connora8999452010-09-25 12:48:43 -0400404
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400405 // Now that memory is read/writable - start post process.
406 dopost();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500407}