Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // 32bit code to Power On Self Test (POST) a machine. |
| 2 | // |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 3 | // Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 7 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 8 | #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'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 12 | #include "disk.h" // floppy_setup |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 13 | #include "ata.h" // ata_setup |
Gerd Hoffmann | d52fdf6 | 2010-11-29 09:42:13 +0100 | [diff] [blame] | 14 | #include "ahci.h" // ahci_setup |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 15 | #include "memmap.h" // add_e820 |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 16 | #include "pic.h" // pic_setup |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 17 | #include "bregs.h" // struct bregs |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 18 | #include "boot.h" // boot_init |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 19 | #include "usb.h" // usb_setup |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 20 | #include "paravirt.h" // qemu_cfg_preinit |
| 21 | #include "xen.h" // xen_preinit |
Kevin O'Connor | 5787748 | 2009-12-09 21:00:41 -0500 | [diff] [blame] | 22 | #include "ps2port.h" // ps2port_setup |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 23 | #include "virtio-blk.h" // virtio_blk_setup |
Paolo Bonzini | c5c488f | 2012-02-27 17:22:23 +0100 | [diff] [blame] | 24 | #include "virtio-scsi.h" // virtio_scsi_setup |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 25 | #include "lsi-scsi.h" // lsi_scsi_setup |
Paolo Bonzini | 7a39e72 | 2012-08-06 13:15:06 +0200 | [diff] [blame] | 26 | #include "esp-scsi.h" // esp_scsi_setup |
Hannes Reinecke | 2df70bf | 2012-11-13 15:03:31 +0100 | [diff] [blame] | 27 | #include "megasas.h" // megasas_setup |
David Woodhouse | a216451 | 2013-01-25 19:26:48 -0600 | [diff] [blame] | 28 | #include "post.h" // interface_init |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 29 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 30 | |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 31 | /**************************************************************** |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 32 | * BIOS initialization and hardware setup |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 33 | ****************************************************************/ |
| 34 | |
Kevin O'Connor | e3677b1 | 2008-07-04 15:29:23 -0400 | [diff] [blame] | 35 | static void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 36 | ivt_init(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 37 | { |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 38 | dprintf(3, "init ivt\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 39 | |
Kevin O'Connor | 9e4d41c | 2013-01-21 12:14:29 -0500 | [diff] [blame] | 40 | // Setup reset-vector entry point (controls legacy reboots). |
| 41 | HaveRunPost = 1; |
| 42 | outb_cmos(0, CMOS_RESET_CODE); |
| 43 | |
Kevin O'Connor | d67a703 | 2009-01-17 19:37:26 -0500 | [diff] [blame] | 44 | // Initialize all vectors to the default handler. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 45 | int i; |
Kevin O'Connor | e3677b1 | 2008-07-04 15:29:23 -0400 | [diff] [blame] | 46 | for (i=0; i<256; i++) |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 47 | SET_IVT(i, FUNC16(entry_iret_official)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 48 | |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 49 | // Initialize all hw vectors to a default hw handler. |
| 50 | for (i=0x08; i<=0x0f; i++) |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 51 | SET_IVT(i, FUNC16(entry_hwpic1)); |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 52 | for (i=0x70; i<=0x77; i++) |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 53 | SET_IVT(i, FUNC16(entry_hwpic2)); |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 54 | |
| 55 | // Initialize software handlers. |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 56 | SET_IVT(0x02, FUNC16(entry_02)); |
| 57 | SET_IVT(0x10, FUNC16(entry_10)); |
| 58 | SET_IVT(0x11, FUNC16(entry_11)); |
| 59 | SET_IVT(0x12, FUNC16(entry_12)); |
| 60 | SET_IVT(0x13, FUNC16(entry_13_official)); |
| 61 | SET_IVT(0x14, FUNC16(entry_14)); |
| 62 | SET_IVT(0x15, FUNC16(entry_15)); |
| 63 | SET_IVT(0x16, FUNC16(entry_16)); |
| 64 | SET_IVT(0x17, FUNC16(entry_17)); |
| 65 | SET_IVT(0x18, FUNC16(entry_18)); |
| 66 | SET_IVT(0x19, FUNC16(entry_19_official)); |
| 67 | SET_IVT(0x1a, FUNC16(entry_1a)); |
| 68 | SET_IVT(0x40, FUNC16(entry_40)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 69 | |
Kevin O'Connor | b164d2c | 2009-12-26 23:24:52 -0500 | [diff] [blame] | 70 | // INT 60h-66h reserved for user interrupt |
| 71 | for (i=0x60; i<=0x66; i++) |
| 72 | SET_IVT(i, SEGOFF(0, 0)); |
| 73 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 74 | // set vector 0x79 to zero |
| 75 | // this is used by 'gardian angel' protection system |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 76 | SET_IVT(0x79, SEGOFF(0, 0)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 80 | bda_init(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 81 | { |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 82 | dprintf(3, "init bda\n"); |
| 83 | |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 84 | struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0); |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 85 | memset(bda, 0, sizeof(*bda)); |
| 86 | |
Kevin O'Connor | 49553a4 | 2009-05-17 10:31:34 -0400 | [diff] [blame] | 87 | int esize = EBDA_SIZE_START; |
Kevin O'Connor | f416fe9 | 2009-09-24 20:51:55 -0400 | [diff] [blame] | 88 | SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize); |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 89 | u16 ebda_seg = EBDA_SEGMENT_START; |
| 90 | SET_BDA(ebda_seg, ebda_seg); |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 91 | |
Kevin O'Connor | 9becbf1 | 2009-07-12 09:29:46 -0400 | [diff] [blame] | 92 | // Init ebda |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 93 | struct extended_bios_data_area_s *ebda = get_ebda_ptr(); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 94 | memset(ebda, 0, sizeof(*ebda)); |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 95 | ebda->size = esize; |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 96 | |
Kevin O'Connor | 4bc4997 | 2012-05-13 22:58:08 -0400 | [diff] [blame] | 97 | add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA(ebda_seg, size) * 1024 |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 98 | , E820_RESERVED); |
Kevin O'Connor | bf2e8c2 | 2012-05-28 12:59:58 -0400 | [diff] [blame] | 99 | |
| 100 | // Init extra stack |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 101 | StackPos = (void*)(&ExtraStack[BUILD_EXTRA_STACK_SIZE] - datalow_base); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 102 | } |
| 103 | |
David Woodhouse | a216451 | 2013-01-25 19:26:48 -0600 | [diff] [blame] | 104 | void |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 105 | interface_init(void) |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 106 | { |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 107 | // Running at new code address - do code relocation fixups |
| 108 | malloc_fixupreloc_init(); |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 109 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 110 | // Setup romfile items. |
| 111 | qemu_romfile_init(); |
| 112 | coreboot_cbfs_init(); |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 113 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 114 | // Setup ivt/bda/ebda |
| 115 | ivt_init(); |
| 116 | bda_init(); |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 117 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 118 | // Other interfaces |
| 119 | mathcp_init(); |
| 120 | boot_init(); |
| 121 | bios32_init(); |
| 122 | pmm_init(); |
| 123 | pnp_init(); |
| 124 | kbd_init(); |
| 125 | mouse_init(); |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 128 | // Initialize hardware devices |
David Woodhouse | a216451 | 2013-01-25 19:26:48 -0600 | [diff] [blame] | 129 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 130 | device_hardware_setup(void) |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 131 | { |
| 132 | usb_setup(); |
| 133 | ps2port_setup(); |
| 134 | lpt_setup(); |
| 135 | serial_setup(); |
| 136 | |
| 137 | floppy_setup(); |
| 138 | ata_setup(); |
Gerd Hoffmann | d52fdf6 | 2010-11-29 09:42:13 +0100 | [diff] [blame] | 139 | ahci_setup(); |
Kevin O'Connor | 89a1efd | 2011-01-08 12:24:39 -0500 | [diff] [blame] | 140 | cbfs_payload_setup(); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 141 | ramdisk_setup(); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 142 | virtio_blk_setup(); |
Paolo Bonzini | c5c488f | 2012-02-27 17:22:23 +0100 | [diff] [blame] | 143 | virtio_scsi_setup(); |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 144 | lsi_scsi_setup(); |
Paolo Bonzini | 7a39e72 | 2012-08-06 13:15:06 +0200 | [diff] [blame] | 145 | esp_scsi_setup(); |
Hannes Reinecke | 2df70bf | 2012-11-13 15:03:31 +0100 | [diff] [blame] | 146 | megasas_setup(); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 149 | static void |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 150 | platform_hardware_setup(void) |
| 151 | { |
Kevin O'Connor | 9e4d41c | 2013-01-21 12:14:29 -0500 | [diff] [blame] | 152 | // Enable CPU caching |
| 153 | setcr0(getcr0() & ~(CR0_CD|CR0_NW)); |
| 154 | |
| 155 | // Make sure legacy DMA isn't running. |
| 156 | dma_setup(); |
| 157 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 158 | // Init base pc hardware. |
| 159 | pic_setup(); |
| 160 | timer_setup(); |
| 161 | |
| 162 | // Initialize pci |
| 163 | pci_setup(); |
| 164 | smm_setup(); |
| 165 | |
Kevin O'Connor | 9f08c90 | 2013-02-10 00:25:53 -0500 | [diff] [blame^] | 166 | // Initialize mtrr and smp |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 167 | mtrr_setup(); |
Kevin O'Connor | 9f08c90 | 2013-02-10 00:25:53 -0500 | [diff] [blame^] | 168 | smp_setup(); |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 169 | |
| 170 | // Setup Xen hypercalls |
| 171 | xen_hypercall_setup(); |
| 172 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 173 | // Setup external BIOS interface tables |
Kevin O'Connor | 8ed2e53 | 2013-01-21 02:32:48 -0500 | [diff] [blame] | 174 | if (CONFIG_COREBOOT) |
| 175 | coreboot_biostable_setup(); |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 176 | else if (runningOnXen()) |
Kevin O'Connor | 8ed2e53 | 2013-01-21 02:32:48 -0500 | [diff] [blame] | 177 | xen_biostable_setup(); |
| 178 | else |
| 179 | qemu_biostable_setup(); |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 180 | } |
| 181 | |
David Woodhouse | a216451 | 2013-01-25 19:26:48 -0600 | [diff] [blame] | 182 | void |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 183 | prepareboot(void) |
| 184 | { |
| 185 | // Run BCVs |
| 186 | bcv_prepboot(); |
| 187 | |
| 188 | // Finalize data structures before boot |
| 189 | cdrom_prepboot(); |
| 190 | pmm_prepboot(); |
| 191 | malloc_prepboot(); |
| 192 | memmap_prepboot(); |
| 193 | |
| 194 | // Setup bios checksum. |
| 195 | BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE); |
| 196 | } |
| 197 | |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 198 | // Begin the boot process by invoking an int0x19 in 16bit mode. |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 199 | void VISIBLE32FLAT |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 200 | startBoot(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 201 | { |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 202 | // Clear low-memory allocations (required by PMM spec). |
| 203 | memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR); |
| 204 | |
| 205 | dprintf(3, "Jump to int19\n"); |
| 206 | struct bregs br; |
| 207 | memset(&br, 0, sizeof(br)); |
| 208 | br.flags = F_IF; |
| 209 | call16_int(0x19, &br); |
| 210 | } |
| 211 | |
| 212 | // Main setup code. |
| 213 | static void |
| 214 | maininit(void) |
| 215 | { |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 216 | // Initialize internal interfaces. |
| 217 | interface_init(); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 218 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 219 | // Setup platform devices. |
| 220 | platform_hardware_setup(); |
Kevin O'Connor | 9803280 | 2009-12-09 20:18:31 -0500 | [diff] [blame] | 221 | |
Kevin O'Connor | 9f08c90 | 2013-02-10 00:25:53 -0500 | [diff] [blame^] | 222 | // Start hardware initialization (if optionrom threading) |
| 223 | if (CONFIG_THREAD_OPTIONROMS) |
| 224 | device_hardware_setup(); |
| 225 | |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 226 | // Run vga option rom |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 227 | vgarom_setup(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 228 | |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 229 | // Do hardware initialization (if running synchronously) |
Kevin O'Connor | b5c7da6 | 2013-02-08 00:45:44 -0500 | [diff] [blame] | 230 | if (!CONFIG_THREAD_OPTIONROMS) { |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 231 | device_hardware_setup(); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 232 | wait_threads(); |
| 233 | } |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 234 | |
Kevin O'Connor | 9803280 | 2009-12-09 20:18:31 -0500 | [diff] [blame] | 235 | // Run option roms |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 236 | optionrom_setup(); |
Kevin O'Connor | e54ee38 | 2009-07-26 19:33:13 -0400 | [diff] [blame] | 237 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 238 | // Allow user to modify overall boot order. |
| 239 | interactive_bootmenu(); |
| 240 | wait_threads(); |
Kevin O'Connor | 0bf9270 | 2009-08-01 11:45:37 -0400 | [diff] [blame] | 241 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 242 | // Prepare for boot. |
| 243 | prepareboot(); |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 244 | |
| 245 | // Write protect bios memory. |
| 246 | make_bios_readonly(); |
| 247 | |
| 248 | // Invoke int 19 to start boot process. |
| 249 | startBoot(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 252 | |
| 253 | /**************************************************************** |
Kevin O'Connor | 87b533b | 2011-07-10 22:35:07 -0400 | [diff] [blame] | 254 | * POST entry and code relocation |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 255 | ****************************************************************/ |
| 256 | |
| 257 | // Update given relocs for the code at 'dest' with a given 'delta' |
| 258 | static void |
| 259 | updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta) |
| 260 | { |
| 261 | u32 *reloc; |
| 262 | for (reloc = rstart; reloc < rend; reloc++) |
| 263 | *((u32*)(dest + *reloc)) += delta; |
| 264 | } |
| 265 | |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 266 | // Relocate init code and then call a function at its new address. |
| 267 | // The passed function should be in the "init" section and must not |
| 268 | // return. |
David Woodhouse | a216451 | 2013-01-25 19:26:48 -0600 | [diff] [blame] | 269 | void __noreturn |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 270 | reloc_preinit(void *f, void *arg) |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 271 | { |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 272 | void (*func)(void *) __noreturn = f; |
| 273 | if (!CONFIG_RELOCATE_INIT) |
| 274 | func(arg); |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 275 | // Symbols populated by the build. |
| 276 | extern u8 code32flat_start[]; |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 277 | extern u8 _reloc_min_align; |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 278 | extern u32 _reloc_abs_start[], _reloc_abs_end[]; |
| 279 | extern u32 _reloc_rel_start[], _reloc_rel_end[]; |
| 280 | extern u32 _reloc_init_start[], _reloc_init_end[]; |
| 281 | extern u8 code32init_start[], code32init_end[]; |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 282 | extern u32 _reloc_datalow_start[], _reloc_datalow_end[]; |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 283 | extern u8 datalow_start[], datalow_end[], final_datalow_start[]; |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 284 | |
| 285 | // Allocate space for init code. |
| 286 | u32 initsize = code32init_end - code32init_start; |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 287 | u32 codealign = (u32)&_reloc_min_align; |
| 288 | void *codedest = memalign_tmp(codealign, initsize); |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 289 | if (!codedest) |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 290 | panic("No space for init relocation.\n"); |
| 291 | |
| 292 | // Copy code and update relocs (init absolute, init relative, and runtime) |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 293 | dprintf(1, "Relocating low data from %p to %p (size %d)\n" |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 294 | , datalow_start, final_datalow_start, datalow_end - datalow_start); |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 295 | updateRelocs(code32flat_start, _reloc_datalow_start, _reloc_datalow_end |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 296 | , final_datalow_start - datalow_start); |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 297 | dprintf(1, "Relocating init from %p to %p (size %d)\n" |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 298 | , code32init_start, codedest, initsize); |
| 299 | s32 delta = codedest - (void*)code32init_start; |
| 300 | memcpy(codedest, code32init_start, initsize); |
| 301 | updateRelocs(codedest, _reloc_abs_start, _reloc_abs_end, delta); |
| 302 | updateRelocs(codedest, _reloc_rel_start, _reloc_rel_end, -delta); |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 303 | updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta); |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 304 | if (f >= (void*)code32init_start && f < (void*)code32init_end) |
| 305 | func = f + delta; |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 306 | |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 307 | // Call function in relocated code. |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 308 | barrier(); |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 309 | func(arg); |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 310 | } |
| 311 | |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 312 | // Setup for code relocation and then relocate. |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 313 | void VISIBLE32INIT |
Kevin O'Connor | 9acdc28 | 2011-09-20 19:42:14 -0400 | [diff] [blame] | 314 | dopost(void) |
| 315 | { |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 316 | // Detect ram and setup internal malloc. |
| 317 | qemu_cfg_preinit(); |
| 318 | if (CONFIG_COREBOOT) |
| 319 | coreboot_preinit(); |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 320 | else if (runningOnXen()) |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 321 | xen_ramsize_preinit(); |
| 322 | else |
| 323 | qemu_ramsize_preinit(); |
| 324 | malloc_preinit(); |
| 325 | |
| 326 | // Relocate initialization code and call maininit(). |
| 327 | reloc_preinit(maininit, NULL); |
| 328 | } |
| 329 | |
Kevin O'Connor | b1885fc | 2013-02-08 21:00:46 -0500 | [diff] [blame] | 330 | // Entry point for Power On Self Test (POST) - the BIOS initilization |
| 331 | // phase. This function makes the memory at 0xc0000-0xfffff |
| 332 | // read/writable and then calls dopost(). |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 333 | void VISIBLE32FLAT |
| 334 | handle_post(void) |
| 335 | { |
Kevin O'Connor | b1885fc | 2013-02-08 21:00:46 -0500 | [diff] [blame] | 336 | debug_serial_preinit(); |
| 337 | dprintf(1, "Start bios (version %s)\n", VERSION); |
| 338 | |
| 339 | // Check if we are running under Xen. |
| 340 | xen_preinit(); |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 341 | |
Kevin O'Connor | a899945 | 2010-09-25 12:48:43 -0400 | [diff] [blame] | 342 | // Allow writes to modify bios area (0xf0000) |
| 343 | make_bios_writable(); |
Kevin O'Connor | a899945 | 2010-09-25 12:48:43 -0400 | [diff] [blame] | 344 | |
Kevin O'Connor | 9acdc28 | 2011-09-20 19:42:14 -0400 | [diff] [blame] | 345 | // Now that memory is read/writable - start post process. |
| 346 | dopost(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 347 | } |