blob: fb7372be3de45f4ce300355cacfc8d6d860260a1 [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
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008#include "config.h" // CONFIG_*
9#include "cmos.h" // CMOS_*
10#include "util.h" // memset
11#include "biosvar.h" // struct bios_data_area_s
Kevin O'Connord83c87b2013-01-21 01:14:12 -050012#include "disk.h" // floppy_setup
Kevin O'Connorc892b132009-08-11 21:59:37 -040013#include "ata.h" // ata_setup
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010014#include "ahci.h" // ahci_setup
Kevin O'Connorc7812932008-06-08 23:08:12 -040015#include "memmap.h" // add_e820
Kevin O'Connorf54c1502008-06-14 15:56:16 -040016#include "pic.h" // pic_setup
Kevin O'Connord83c87b2013-01-21 01:14:12 -050017#include "pci.h" // pirtable_setup
18#include "acpi.h" // acpi_setup
Kevin O'Connor9521e262008-07-04 13:04:29 -040019#include "bregs.h" // struct bregs
Kevin O'Connord83c87b2013-01-21 01:14:12 -050020#include "mptable.h" // mptable_setup
21#include "boot.h" // boot_init
Kevin O'Connor114592f2009-09-28 21:32:08 -040022#include "usb.h" // usb_setup
Kevin O'Connord83c87b2013-01-21 01:14:12 -050023#include "smbios.h" // smbios_setup
24#include "paravirt.h" // qemu_cfg_preinit
25#include "xen.h" // xen_preinit
Kevin O'Connor57877482009-12-09 21:00:41 -050026#include "ps2port.h" // ps2port_setup
Gleb Natapov89acfa32010-05-10 11:36:37 +030027#include "virtio-blk.h" // virtio_blk_setup
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010028#include "virtio-scsi.h" // virtio_scsi_setup
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +020029#include "lsi-scsi.h" // lsi_scsi_setup
Paolo Bonzini7a39e722012-08-06 13:15:06 +020030#include "esp-scsi.h" // esp_scsi_setup
Hannes Reinecke2df70bf2012-11-13 15:03:31 +010031#include "megasas.h" // megasas_setup
Kevin O'Connor025cabd2010-09-15 21:59:11 -040032
33/****************************************************************
34 * BIOS init
35 ****************************************************************/
36
Kevin O'Connore3677b12008-07-04 15:29:23 -040037static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050038ivt_init(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050039{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050040 dprintf(3, "init ivt\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050041
Kevin O'Connord67a7032009-01-17 19:37:26 -050042 // Initialize all vectors to the default handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050043 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040044 for (i=0; i<256; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040045 SET_IVT(i, FUNC16(entry_iret_official));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050046
Kevin O'Connord21c0892008-11-26 17:02:43 -050047 // Initialize all hw vectors to a default hw handler.
48 for (i=0x08; i<=0x0f; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040049 SET_IVT(i, FUNC16(entry_hwpic1));
Kevin O'Connord21c0892008-11-26 17:02:43 -050050 for (i=0x70; i<=0x77; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040051 SET_IVT(i, FUNC16(entry_hwpic2));
Kevin O'Connord21c0892008-11-26 17:02:43 -050052
53 // Initialize software handlers.
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040054 SET_IVT(0x02, FUNC16(entry_02));
55 SET_IVT(0x10, FUNC16(entry_10));
56 SET_IVT(0x11, FUNC16(entry_11));
57 SET_IVT(0x12, FUNC16(entry_12));
58 SET_IVT(0x13, FUNC16(entry_13_official));
59 SET_IVT(0x14, FUNC16(entry_14));
60 SET_IVT(0x15, FUNC16(entry_15));
61 SET_IVT(0x16, FUNC16(entry_16));
62 SET_IVT(0x17, FUNC16(entry_17));
63 SET_IVT(0x18, FUNC16(entry_18));
64 SET_IVT(0x19, FUNC16(entry_19_official));
65 SET_IVT(0x1a, FUNC16(entry_1a));
66 SET_IVT(0x40, FUNC16(entry_40));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050067
Kevin O'Connorb164d2c2009-12-26 23:24:52 -050068 // INT 60h-66h reserved for user interrupt
69 for (i=0x60; i<=0x66; i++)
70 SET_IVT(i, SEGOFF(0, 0));
71
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050072 // set vector 0x79 to zero
73 // this is used by 'gardian angel' protection system
Kevin O'Connor9f985422009-09-09 11:34:39 -040074 SET_IVT(0x79, SEGOFF(0, 0));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050075}
76
77static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050078bda_init(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050080 dprintf(3, "init bda\n");
81
Kevin O'Connor35ae7262009-01-19 15:44:44 -050082 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050083 memset(bda, 0, sizeof(*bda));
84
Kevin O'Connor49553a42009-05-17 10:31:34 -040085 int esize = EBDA_SIZE_START;
Kevin O'Connorf416fe92009-09-24 20:51:55 -040086 SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
Kevin O'Connor025cabd2010-09-15 21:59:11 -040087 u16 ebda_seg = EBDA_SEGMENT_START;
88 SET_BDA(ebda_seg, ebda_seg);
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050089
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'Connor4bc49972012-05-13 22:58:08 -040095 add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA(ebda_seg, size) * 1024
Kevin O'Connor025cabd2010-09-15 21:59:11 -040096 , E820_RESERVED);
Kevin O'Connorbf2e8c22012-05-28 12:59:58 -040097
98 // Init extra stack
Kevin O'Connorc91da7a2012-06-08 21:14:19 -040099 StackPos = (void*)(&ExtraStack[BUILD_EXTRA_STACK_SIZE] - datalow_base);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500100}
101
102static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500103ramsize_preinit(void)
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400104{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400105 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400106 if (CONFIG_COREBOOT) {
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500107 coreboot_preinit();
Ian Campbell74c78782011-06-01 11:00:29 +0100108 } else if (usingXen()) {
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500109 xen_ramsize_preinit();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400110 } else {
111 // On emulators, get memory size from nvram.
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400112 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
113 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400114 if (rs)
115 rs += 16 * 1024 * 1024;
116 else
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400117 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
118 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400119 + 1 * 1024 * 1024);
Kevin O'Connore7916362008-12-28 22:03:17 -0500120 RamSize = rs;
Kevin O'Connorc7812932008-06-08 23:08:12 -0400121 add_e820(0, rs, E820_RAM);
122
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400123 // Check for memory over 4Gig
124 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
Kevin O'Connor669c9912010-01-05 19:50:04 -0500125 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400126 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
Kevin O'Connore7916362008-12-28 22:03:17 -0500127 RamSizeOver4G = high;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400128 add_e820(0x100000000ull, high, E820_RAM);
129
Kevin O'Connorc7812932008-06-08 23:08:12 -0400130 /* reserve 256KB BIOS area at the end of 4 GB */
131 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400132 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400133
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400134 // Don't declare any memory between 0xa0000 and 0x100000
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400135 add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400136
Kevin O'Connorc7812932008-06-08 23:08:12 -0400137 // Mark known areas as reserved.
Kevin O'Connore3677b12008-07-04 15:29:23 -0400138 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400139
Jes Sorensen0360e8e2010-02-16 09:46:08 +0100140 u32 count = qemu_cfg_e820_entries();
141 if (count) {
142 struct e820_reservation entry;
143 int i;
144
145 for (i = 0; i < count; i++) {
146 qemu_cfg_e820_load_next(&entry);
147 add_e820(entry.address, entry.length, entry.type);
148 }
149 } else if (kvm_para_available()) {
150 // Backwards compatibility - provide hard coded range.
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500151 // 4 pages before the bios, 3 pages for vmx tss pages, the
152 // other page for EPT real mode pagetable
153 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
Jes Sorensen0360e8e2010-02-16 09:46:08 +0100154 }
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500155
Kevin O'Connorc04090d2012-03-05 10:14:07 -0500156 dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400157}
158
159static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500160biostable_setup(void)
Kevin O'Connor0525d292008-07-04 06:18:30 -0400161{
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400162 if (CONFIG_COREBOOT) {
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500163 coreboot_biostable_setup();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400164 return;
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400165 }
Ian Campbell74c78782011-06-01 11:00:29 +0100166 if (usingXen()) {
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500167 xen_biostable_setup();
Kevin O'Connor7a08ae72012-03-14 21:11:39 -0400168 return;
Ian Campbell74c78782011-06-01 11:00:29 +0100169 }
Kevin O'Connor0525d292008-07-04 06:18:30 -0400170
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500171 pirtable_setup();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400172
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500173 mptable_setup();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400174
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500175 smbios_setup();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400176
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500177 acpi_setup();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400178}
179
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400180// Initialize hardware devices
181static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500182device_hardware_setup(void)
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400183{
184 usb_setup();
185 ps2port_setup();
186 lpt_setup();
187 serial_setup();
188
189 floppy_setup();
190 ata_setup();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100191 ahci_setup();
Kevin O'Connor89a1efd2011-01-08 12:24:39 -0500192 cbfs_payload_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400193 ramdisk_setup();
Gleb Natapov89acfa32010-05-10 11:36:37 +0300194 virtio_blk_setup();
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100195 virtio_scsi_setup();
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200196 lsi_scsi_setup();
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200197 esp_scsi_setup();
Hannes Reinecke2df70bf2012-11-13 15:03:31 +0100198 megasas_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400199}
200
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400201// Begin the boot process by invoking an int0x19 in 16bit mode.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400202void VISIBLE32FLAT
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400203startBoot(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500204{
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400205 // Clear low-memory allocations (required by PMM spec).
206 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
207
208 dprintf(3, "Jump to int19\n");
209 struct bregs br;
210 memset(&br, 0, sizeof(br));
211 br.flags = F_IF;
212 call16_int(0x19, &br);
213}
214
215// Main setup code.
216static void
217maininit(void)
218{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400219 // Setup romfile items.
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500220 qemu_romfile_init();
221 coreboot_cbfs_init();
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400222
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400223 // Setup ivt/bda/ebda
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500224 ivt_init();
225 bda_init();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500226
Kevin O'Connor969df602009-11-25 19:05:04 -0500227 // Init base pc hardware.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400228 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400229 timer_setup();
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500230 mathcp_init();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400231
Kevin O'Connor969df602009-11-25 19:05:04 -0500232 // Initialize pci
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400233 pci_setup();
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500234 smm_setup();
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400235
Jason Barona39a9c32012-11-28 10:17:30 +0100236 // Initialize mtrr
237 mtrr_setup();
238
Ian Campbell7fc039e2011-07-13 11:46:40 +0100239 // Setup Xen hypercalls
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500240 xen_hypercall_setup();
Ian Campbell7fc039e2011-07-13 11:46:40 +0100241
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400242 // Initialize internal tables
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500243 boot_init();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400244
245 // Start hardware initialization (if optionrom threading)
246 if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500247 device_hardware_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400248
249 // Find and initialize other cpus
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500250 smp_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400251
Kevin O'Connor98032802009-12-09 20:18:31 -0500252 // Setup interfaces that option roms may need
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500253 bios32_init();
254 pmm_init();
255 pnp_init();
256 kbd_init();
257 mouse_init();
258 biostable_setup();
Kevin O'Connor98032802009-12-09 20:18:31 -0500259
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400260 // Run vga option rom
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500261 vgarom_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400262
Laszlo Ersek37676f82012-12-14 12:59:39 +0100263 // SMBIOS tables and VGA console are ready, print UUID
264 display_uuid();
265
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400266 // Do hardware initialization (if running synchronously)
267 if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500268 device_hardware_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400269 wait_threads();
270 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500271
Kevin O'Connor98032802009-12-09 20:18:31 -0500272 // Run option roms
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400273 optionrom_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400274
Kevin O'Connor969df602009-11-25 19:05:04 -0500275 // Run BCVs and show optional boot menu
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500276 boot_prepboot();
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400277
Kevin O'Connor969df602009-11-25 19:05:04 -0500278 // Finalize data structures before boot
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500279 cdrom_prepboot();
280 pmm_prepboot();
281 malloc_prepboot();
282 memmap_prepboot();
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400283
284 // Setup bios checksum.
285 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
286
287 // Write protect bios memory.
288 make_bios_readonly();
289
290 // Invoke int 19 to start boot process.
291 startBoot();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500292}
293
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400294
295/****************************************************************
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400296 * POST entry and code relocation
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400297 ****************************************************************/
298
Kevin O'Connor46b82622012-05-13 12:10:30 -0400299// Relocation fixup code that runs at new address after relocation complete.
300static void
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400301afterReloc(void)
Kevin O'Connor46b82622012-05-13 12:10:30 -0400302{
303 // Running at new code address - do code relocation fixups
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500304 malloc_fixupreloc_init();
Kevin O'Connor46b82622012-05-13 12:10:30 -0400305
306 // Move low-memory initial variable content to new location.
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400307 extern u8 datalow_start[], datalow_end[], final_datalow_start[];
308 memmove(final_datalow_start, datalow_start, datalow_end - datalow_start);
Kevin O'Connor46b82622012-05-13 12:10:30 -0400309
310 // Run main code
311 maininit();
312}
313
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400314// Update given relocs for the code at 'dest' with a given 'delta'
315static void
316updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
317{
318 u32 *reloc;
319 for (reloc = rstart; reloc < rend; reloc++)
320 *((u32*)(dest + *reloc)) += delta;
321}
322
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400323// Relocate init code and then call maininit() at new address.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400324static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500325reloc_preinit(void)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400326{
327 if (!CONFIG_RELOCATE_INIT) {
328 maininit();
329 return;
330 }
331 // Symbols populated by the build.
332 extern u8 code32flat_start[];
Kevin O'Connor46b82622012-05-13 12:10:30 -0400333 extern u8 _reloc_min_align;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400334 extern u32 _reloc_abs_start[], _reloc_abs_end[];
335 extern u32 _reloc_rel_start[], _reloc_rel_end[];
336 extern u32 _reloc_init_start[], _reloc_init_end[];
337 extern u8 code32init_start[], code32init_end[];
Kevin O'Connor46b82622012-05-13 12:10:30 -0400338 extern u32 _reloc_datalow_start[], _reloc_datalow_end[];
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400339 extern u8 datalow_start[], datalow_end[], final_datalow_start[];
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400340
341 // Allocate space for init code.
342 u32 initsize = code32init_end - code32init_start;
Kevin O'Connor46b82622012-05-13 12:10:30 -0400343 u32 codealign = (u32)&_reloc_min_align;
344 void *codedest = memalign_tmp(codealign, initsize);
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400345 if (!codedest)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400346 panic("No space for init relocation.\n");
347
348 // Copy code and update relocs (init absolute, init relative, and runtime)
Kevin O'Connor46b82622012-05-13 12:10:30 -0400349 dprintf(1, "Relocating low data from %p to %p (size %d)\n"
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400350 , datalow_start, final_datalow_start, datalow_end - datalow_start);
Kevin O'Connor46b82622012-05-13 12:10:30 -0400351 updateRelocs(code32flat_start, _reloc_datalow_start, _reloc_datalow_end
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400352 , final_datalow_start - datalow_start);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400353 dprintf(1, "Relocating init from %p to %p (size %d)\n"
Kevin O'Connor46b82622012-05-13 12:10:30 -0400354 , code32init_start, codedest, initsize);
355 s32 delta = codedest - (void*)code32init_start;
356 memcpy(codedest, code32init_start, initsize);
357 updateRelocs(codedest, _reloc_abs_start, _reloc_abs_end, delta);
358 updateRelocs(codedest, _reloc_rel_start, _reloc_rel_end, -delta);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400359 updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
360
361 // Call maininit() in relocated code.
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400362 void (*func)(void) = (void*)afterReloc + delta;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400363 barrier();
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400364 func();
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400365}
366
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400367// Setup for code relocation and then call reloc_init
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400368void VISIBLE32INIT
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400369dopost(void)
370{
371 HaveRunPost = 1;
372
373 // Detect ram and setup internal malloc.
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500374 qemu_cfg_preinit();
375 ramsize_preinit();
376 malloc_preinit();
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400377
378 // Relocate initialization code and call maininit().
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500379 reloc_preinit();
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400380}
381
382// Entry point for Power On Self Test (POST) - the BIOS initilization
383// phase. This function makes the memory at 0xc0000-0xfffff
384// read/writable and then calls dopost().
385void VISIBLE32FLAT
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400386handle_post(void)
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400387{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500388 debug_serial_preinit();
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400389 dprintf(1, "Start bios (version %s)\n", VERSION);
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400390
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400391 // Enable CPU caching
392 setcr0(getcr0() & ~(CR0_CD|CR0_NW));
393
394 // Clear CMOS reboot flag.
395 outb_cmos(0, CMOS_RESET_CODE);
396
397 // Make sure legacy DMA isn't running.
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500398 dma_preinit();
Kevin O'Connoradaf3732010-09-13 20:22:07 -0400399
Ian Campbell74c78782011-06-01 11:00:29 +0100400 // Check if we are running under Xen.
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500401 xen_preinit();
Ian Campbell74c78782011-06-01 11:00:29 +0100402
Kevin O'Connora8999452010-09-25 12:48:43 -0400403 // Allow writes to modify bios area (0xf0000)
404 make_bios_writable();
Kevin O'Connora8999452010-09-25 12:48:43 -0400405
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400406 // Now that memory is read/writable - start post process.
407 dopost();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500408}