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 | 392d2aa | 2013-09-15 00:14:28 -0400 | [diff] [blame] | 8 | #include "biosvar.h" // SET_BDA |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 9 | #include "bregs.h" // struct bregs |
| 10 | #include "config.h" // CONFIG_* |
Kevin O'Connor | c167e54 | 2015-09-29 09:40:46 -0400 | [diff] [blame] | 11 | #include "e820map.h" // e820_add |
Kevin O'Connor | ccee6e8 | 2013-09-02 21:25:21 -0400 | [diff] [blame] | 12 | #include "fw/paravirt.h" // qemu_cfg_preinit |
| 13 | #include "fw/xen.h" // xen_preinit |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 14 | #include "hw/ahci.h" // ahci_setup |
| 15 | #include "hw/ata.h" // ata_setup |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 16 | #include "hw/esp-scsi.h" // esp_scsi_setup |
| 17 | #include "hw/lsi-scsi.h" // lsi_scsi_setup |
| 18 | #include "hw/megasas.h" // megasas_setup |
Evgeny Budilovsky | 83d60b3 | 2013-10-14 18:03:36 +0300 | [diff] [blame] | 19 | #include "hw/pvscsi.h" // pvscsi_setup |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 20 | #include "hw/pic.h" // pic_setup |
Kevin O'Connor | 5d369d8 | 2013-09-02 20:48:46 -0400 | [diff] [blame] | 21 | #include "hw/ps2port.h" // ps2port_setup |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 22 | #include "hw/rtc.h" // rtc_write |
Kevin O'Connor | 940fc1f | 2014-01-15 13:42:50 -0500 | [diff] [blame] | 23 | #include "hw/serialio.h" // serial_debug_preinit |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 24 | #include "hw/usb.h" // usb_setup |
Kevin O'Connor | 5d369d8 | 2013-09-02 20:48:46 -0400 | [diff] [blame] | 25 | #include "hw/virtio-blk.h" // virtio_blk_setup |
| 26 | #include "hw/virtio-scsi.h" // virtio_scsi_setup |
Kevin O'Connor | 9dea590 | 2013-09-14 20:23:54 -0400 | [diff] [blame] | 27 | #include "malloc.h" // malloc_init |
Kevin O'Connor | 5d91226 | 2015-10-08 11:18:17 -0400 | [diff] [blame] | 28 | #include "memmap.h" // SYMBOL |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 29 | #include "output.h" // dprintf |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 30 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 31 | #include "util.h" // kbd_init |
Stefan Berger | b310dfa | 2015-03-23 14:22:16 -0400 | [diff] [blame] | 32 | #include "tcgbios.h" // tpm_* |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 33 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 34 | |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 35 | /**************************************************************** |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 36 | * BIOS initialization and hardware setup |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 37 | ****************************************************************/ |
| 38 | |
Kevin O'Connor | e3677b1 | 2008-07-04 15:29:23 -0400 | [diff] [blame] | 39 | static void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 40 | ivt_init(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 41 | { |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 42 | dprintf(3, "init ivt\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 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. |
Kevin O'Connor | f465e1e | 2013-02-23 21:33:36 -0500 | [diff] [blame] | 50 | for (i=BIOS_HWIRQ0_VECTOR; i<BIOS_HWIRQ0_VECTOR+8; i++) |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 51 | SET_IVT(i, FUNC16(entry_hwpic1)); |
Kevin O'Connor | f465e1e | 2013-02-23 21:33:36 -0500 | [diff] [blame] | 52 | for (i=BIOS_HWIRQ8_VECTOR; i<BIOS_HWIRQ8_VECTOR+8; 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)); |
Kevin O'Connor | 3fcabf0 | 2014-05-08 18:32:32 -0400 | [diff] [blame] | 62 | SET_IVT(0x15, FUNC16(entry_15_official)); |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 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)); |
Kevin O'Connor | 922aa1b | 2013-03-02 04:02:11 -0500 | [diff] [blame] | 67 | SET_IVT(0x1a, FUNC16(entry_1a_official)); |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 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 | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 88 | u16 ebda_seg = EBDA_SEGMENT_START; |
Kevin O'Connor | 2b0fb8c | 2013-08-07 23:03:47 -0400 | [diff] [blame] | 89 | if (!CONFIG_MALLOC_UPPERMEMORY) |
Kevin O'Connor | 5d91226 | 2015-10-08 11:18:17 -0400 | [diff] [blame] | 90 | ebda_seg = FLATPTR_TO_SEG(ALIGN_DOWN(SYMBOL(final_varlow_start), 1024) |
Kevin O'Connor | 2b0fb8c | 2013-08-07 23:03:47 -0400 | [diff] [blame] | 91 | - EBDA_SIZE_START*1024); |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 92 | SET_BDA(ebda_seg, ebda_seg); |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 93 | |
Kevin O'Connor | 2b0fb8c | 2013-08-07 23:03:47 -0400 | [diff] [blame] | 94 | SET_BDA(mem_size_kb, ebda_seg / (1024/16)); |
| 95 | |
Kevin O'Connor | 9becbf1 | 2009-07-12 09:29:46 -0400 | [diff] [blame] | 96 | // Init ebda |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 97 | struct extended_bios_data_area_s *ebda = get_ebda_ptr(); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 98 | memset(ebda, 0, sizeof(*ebda)); |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 99 | ebda->size = esize; |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 100 | |
Kevin O'Connor | c167e54 | 2015-09-29 09:40:46 -0400 | [diff] [blame] | 101 | e820_add((u32)ebda, BUILD_LOWRAM_END-(u32)ebda, E820_RESERVED); |
Kevin O'Connor | bf2e8c2 | 2012-05-28 12:59:58 -0400 | [diff] [blame] | 102 | |
| 103 | // Init extra stack |
Kevin O'Connor | 5d91226 | 2015-10-08 11:18:17 -0400 | [diff] [blame] | 104 | StackPos = &ExtraStack[BUILD_EXTRA_STACK_SIZE] - SYMBOL(zonelow_base); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 105 | } |
| 106 | |
David Woodhouse | a216451 | 2013-01-25 19:26:48 -0600 | [diff] [blame] | 107 | void |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 108 | interface_init(void) |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 109 | { |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 110 | // Running at new code address - do code relocation fixups |
Kevin O'Connor | 6afc6f8 | 2013-02-19 01:02:50 -0500 | [diff] [blame] | 111 | malloc_init(); |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 112 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 113 | // Setup romfile items. |
Kevin O'Connor | b840ba9 | 2013-02-09 20:09:22 -0500 | [diff] [blame] | 114 | qemu_cfg_init(); |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 115 | coreboot_cbfs_init(); |
Vladimir Serbinenko | 1b911d7 | 2015-05-18 19:07:16 +0200 | [diff] [blame] | 116 | multiboot_init(); |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 117 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 118 | // Setup ivt/bda/ebda |
| 119 | ivt_init(); |
| 120 | bda_init(); |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 121 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 122 | // Other interfaces |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 123 | boot_init(); |
| 124 | bios32_init(); |
| 125 | pmm_init(); |
| 126 | pnp_init(); |
| 127 | kbd_init(); |
| 128 | mouse_init(); |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 131 | // Initialize hardware devices |
David Woodhouse | a216451 | 2013-01-25 19:26:48 -0600 | [diff] [blame] | 132 | void |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 133 | device_hardware_setup(void) |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 134 | { |
| 135 | usb_setup(); |
| 136 | ps2port_setup(); |
| 137 | lpt_setup(); |
| 138 | serial_setup(); |
| 139 | |
| 140 | floppy_setup(); |
| 141 | ata_setup(); |
Gerd Hoffmann | d52fdf6 | 2010-11-29 09:42:13 +0100 | [diff] [blame] | 142 | ahci_setup(); |
Kevin O'Connor | 72691a5 | 2014-12-16 09:55:16 -0500 | [diff] [blame] | 143 | sdcard_setup(); |
Kevin O'Connor | 89a1efd | 2011-01-08 12:24:39 -0500 | [diff] [blame] | 144 | cbfs_payload_setup(); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 145 | ramdisk_setup(); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 146 | virtio_blk_setup(); |
Paolo Bonzini | c5c488f | 2012-02-27 17:22:23 +0100 | [diff] [blame] | 147 | virtio_scsi_setup(); |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 148 | lsi_scsi_setup(); |
Paolo Bonzini | 7a39e72 | 2012-08-06 13:15:06 +0200 | [diff] [blame] | 149 | esp_scsi_setup(); |
Hannes Reinecke | 2df70bf | 2012-11-13 15:03:31 +0100 | [diff] [blame] | 150 | megasas_setup(); |
Evgeny Budilovsky | 83d60b3 | 2013-10-14 18:03:36 +0300 | [diff] [blame] | 151 | pvscsi_setup(); |
Kevin O'Connor | e438b0c | 2010-05-01 12:20:33 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 154 | static void |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 155 | platform_hardware_setup(void) |
| 156 | { |
Kevin O'Connor | 9e4d41c | 2013-01-21 12:14:29 -0500 | [diff] [blame] | 157 | // Make sure legacy DMA isn't running. |
| 158 | dma_setup(); |
| 159 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 160 | // Init base pc hardware. |
| 161 | pic_setup(); |
Kevin O'Connor | 8b9942f | 2015-07-14 15:44:26 -0400 | [diff] [blame] | 162 | thread_setup(); |
Kevin O'Connor | 3a735ba | 2013-02-10 00:35:01 -0500 | [diff] [blame] | 163 | mathcp_setup(); |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 164 | |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 165 | // Platform specific setup |
| 166 | qemu_platform_setup(); |
| 167 | coreboot_platform_setup(); |
Kevin O'Connor | d6aca44 | 2015-06-10 11:00:17 -0400 | [diff] [blame] | 168 | |
Kevin O'Connor | bbb3fba | 2015-07-22 16:06:27 -0400 | [diff] [blame] | 169 | // Setup timers and periodic clock interrupt |
| 170 | timer_setup(); |
| 171 | clock_setup(); |
| 172 | |
Kevin O'Connor | d6aca44 | 2015-06-10 11:00:17 -0400 | [diff] [blame] | 173 | // Initialize TPM |
| 174 | tpm_setup(); |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 175 | } |
| 176 | |
David Woodhouse | a216451 | 2013-01-25 19:26:48 -0600 | [diff] [blame] | 177 | void |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 178 | prepareboot(void) |
| 179 | { |
Kevin O'Connor | d6aca44 | 2015-06-10 11:00:17 -0400 | [diff] [blame] | 180 | // Change TPM phys. presence state befor leaving BIOS |
| 181 | tpm_prepboot(); |
| 182 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 183 | // Run BCVs |
| 184 | bcv_prepboot(); |
| 185 | |
| 186 | // Finalize data structures before boot |
| 187 | cdrom_prepboot(); |
| 188 | pmm_prepboot(); |
| 189 | malloc_prepboot(); |
Kevin O'Connor | c167e54 | 2015-09-29 09:40:46 -0400 | [diff] [blame] | 190 | e820_prepboot(); |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 191 | |
Kevin O'Connor | 6c68e7a | 2014-04-19 12:22:22 -0400 | [diff] [blame] | 192 | HaveRunPost = 2; |
| 193 | |
Kevin O'Connor | 8a0a972 | 2013-01-21 01:53:31 -0500 | [diff] [blame] | 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 | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 222 | // Start hardware initialization (if threads allowed during optionroms) |
| 223 | if (threads_during_optionroms()) |
Kevin O'Connor | 9f08c90 | 2013-02-10 00:25:53 -0500 | [diff] [blame] | 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 | d29ce62 | 2014-04-07 12:15:34 -0400 | [diff] [blame] | 230 | if (!threads_during_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 | |
| 276 | // Allocate space for init code. |
Kevin O'Connor | 5d91226 | 2015-10-08 11:18:17 -0400 | [diff] [blame] | 277 | u32 initsize = SYMBOL(code32init_end) - SYMBOL(code32init_start); |
| 278 | u32 codealign = SYMBOL(_reloc_min_align); |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 279 | void *codedest = memalign_tmp(codealign, initsize); |
Kevin O'Connor | 5d91226 | 2015-10-08 11:18:17 -0400 | [diff] [blame] | 280 | void *codesrc = VSYMBOL(code32init_start); |
Kevin O'Connor | c91da7a | 2012-06-08 21:14:19 -0400 | [diff] [blame] | 281 | if (!codedest) |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 282 | panic("No space for init relocation.\n"); |
| 283 | |
| 284 | // Copy code and update relocs (init absolute, init relative, and runtime) |
| 285 | dprintf(1, "Relocating init from %p to %p (size %d)\n" |
Kevin O'Connor | 5d91226 | 2015-10-08 11:18:17 -0400 | [diff] [blame] | 286 | , codesrc, codedest, initsize); |
| 287 | s32 delta = codedest - codesrc; |
| 288 | memcpy(codedest, codesrc, initsize); |
| 289 | updateRelocs(codedest, VSYMBOL(_reloc_abs_start), VSYMBOL(_reloc_abs_end) |
| 290 | , delta); |
| 291 | updateRelocs(codedest, VSYMBOL(_reloc_rel_start), VSYMBOL(_reloc_rel_end) |
| 292 | , -delta); |
| 293 | updateRelocs(VSYMBOL(code32flat_start), VSYMBOL(_reloc_init_start) |
| 294 | , VSYMBOL(_reloc_init_end), delta); |
| 295 | if (f >= codesrc && f < VSYMBOL(code32init_end)) |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 296 | func = f + delta; |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 297 | |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 298 | // Call function in relocated code. |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 299 | barrier(); |
Kevin O'Connor | bc62809 | 2013-02-05 20:57:10 -0500 | [diff] [blame] | 300 | func(arg); |
Kevin O'Connor | 7f55fd3 | 2010-09-25 11:34:06 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Kevin O'Connor | a48f602 | 2016-01-12 14:22:33 -0500 | [diff] [blame] | 303 | // Runs after all code is present and prior to any modifications |
| 304 | void |
| 305 | code_mutable_preinit(void) |
| 306 | { |
| 307 | if (HaveRunPost) |
| 308 | // Already run |
| 309 | return; |
| 310 | // Setup reset-vector entry point (controls legacy reboots). |
| 311 | rtc_write(CMOS_RESET_CODE, 0); |
| 312 | barrier(); |
| 313 | HaveRunPost = 1; |
| 314 | barrier(); |
| 315 | } |
| 316 | |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 317 | // Setup for code relocation and then relocate. |
Kevin O'Connor | 025cabd | 2010-09-15 21:59:11 -0400 | [diff] [blame] | 318 | void VISIBLE32INIT |
Kevin O'Connor | 9acdc28 | 2011-09-20 19:42:14 -0400 | [diff] [blame] | 319 | dopost(void) |
| 320 | { |
Kevin O'Connor | a48f602 | 2016-01-12 14:22:33 -0500 | [diff] [blame] | 321 | code_mutable_preinit(); |
| 322 | |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 323 | // Detect ram and setup internal malloc. |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 324 | qemu_preinit(); |
| 325 | coreboot_preinit(); |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 326 | malloc_preinit(); |
| 327 | |
| 328 | // Relocate initialization code and call maininit(). |
| 329 | reloc_preinit(maininit, NULL); |
| 330 | } |
| 331 | |
Kevin O'Connor | b1885fc | 2013-02-08 21:00:46 -0500 | [diff] [blame] | 332 | // Entry point for Power On Self Test (POST) - the BIOS initilization |
| 333 | // phase. This function makes the memory at 0xc0000-0xfffff |
| 334 | // read/writable and then calls dopost(). |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 335 | void VISIBLE32FLAT |
| 336 | handle_post(void) |
| 337 | { |
Kevin O'Connor | 4cf2067 | 2013-02-18 11:49:53 -0500 | [diff] [blame] | 338 | if (!CONFIG_QEMU && !CONFIG_COREBOOT) |
| 339 | return; |
| 340 | |
Kevin O'Connor | 940fc1f | 2014-01-15 13:42:50 -0500 | [diff] [blame] | 341 | serial_debug_preinit(); |
Kevin O'Connor | bb1fcb4 | 2014-01-15 13:52:14 -0500 | [diff] [blame] | 342 | debug_banner(); |
Kevin O'Connor | b1885fc | 2013-02-08 21:00:46 -0500 | [diff] [blame] | 343 | |
| 344 | // Check if we are running under Xen. |
| 345 | xen_preinit(); |
Kevin O'Connor | 6ca0460 | 2013-01-21 11:38:49 -0500 | [diff] [blame] | 346 | |
Kevin O'Connor | a899945 | 2010-09-25 12:48:43 -0400 | [diff] [blame] | 347 | // Allow writes to modify bios area (0xf0000) |
| 348 | make_bios_writable(); |
Kevin O'Connor | a899945 | 2010-09-25 12:48:43 -0400 | [diff] [blame] | 349 | |
Kevin O'Connor | 9acdc28 | 2011-09-20 19:42:14 -0400 | [diff] [blame] | 350 | // Now that memory is read/writable - start post process. |
| 351 | dopost(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 352 | } |