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