Support for booting from virtio disks
[seabios.git] / src / post.c
1 // 32bit code to Power On Self Test (POST) a machine.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "ioport.h" // PORT_*
9 #include "config.h" // CONFIG_*
10 #include "cmos.h" // CMOS_*
11 #include "util.h" // memset
12 #include "biosvar.h" // struct bios_data_area_s
13 #include "disk.h" // floppy_drive_setup
14 #include "ata.h" // ata_setup
15 #include "memmap.h" // add_e820
16 #include "pic.h" // pic_setup
17 #include "pci.h" // create_pirtable
18 #include "acpi.h" // acpi_bios_init
19 #include "bregs.h" // struct bregs
20 #include "mptable.h" // mptable_init
21 #include "boot.h" // IPL
22 #include "usb.h" // usb_setup
23 #include "smbios.h" // smbios_init
24 #include "paravirt.h" // qemu_cfg_port_probe
25 #include "ps2port.h" // ps2port_setup
26 #include "virtio-blk.h" // virtio_blk_setup
27
28 void
29 __set_irq(int vector, void *loc)
30 {
31     SET_IVT(vector, SEGOFF(SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR));
32 }
33
34 #define set_irq(vector, func) do {              \
35         extern void func (void);                \
36         __set_irq(vector, func);                \
37     } while (0)
38
39 static void
40 init_ivt(void)
41 {
42     dprintf(3, "init ivt\n");
43
44     // Initialize all vectors to the default handler.
45     int i;
46     for (i=0; i<256; i++)
47         set_irq(i, entry_iret_official);
48
49     // Initialize all hw vectors to a default hw handler.
50     for (i=0x08; i<=0x0f; i++)
51         set_irq(i, entry_hwpic1);
52     for (i=0x70; i<=0x77; i++)
53         set_irq(i, entry_hwpic2);
54
55     // Initialize software handlers.
56     set_irq(0x02, entry_02);
57     set_irq(0x10, entry_10);
58     set_irq(0x11, entry_11);
59     set_irq(0x12, entry_12);
60     set_irq(0x13, entry_13_official);
61     set_irq(0x14, entry_14);
62     set_irq(0x15, entry_15);
63     set_irq(0x16, entry_16);
64     set_irq(0x17, entry_17);
65     set_irq(0x18, entry_18);
66     set_irq(0x19, entry_19_official);
67     set_irq(0x1a, entry_1a);
68     set_irq(0x40, entry_40);
69
70     // INT 60h-66h reserved for user interrupt
71     for (i=0x60; i<=0x66; i++)
72         SET_IVT(i, SEGOFF(0, 0));
73
74     // set vector 0x79 to zero
75     // this is used by 'gardian angel' protection system
76     SET_IVT(0x79, SEGOFF(0, 0));
77
78     __set_irq(0x1E, &diskette_param_table2);
79 }
80
81 static void
82 init_bda(void)
83 {
84     dprintf(3, "init bda\n");
85
86     struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
87     memset(bda, 0, sizeof(*bda));
88
89     int esize = EBDA_SIZE_START;
90     SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
91     u16 eseg = EBDA_SEGMENT_START;
92     SET_BDA(ebda_seg, eseg);
93
94     // Init ebda
95     struct extended_bios_data_area_s *ebda = get_ebda_ptr();
96     memset(ebda, 0, sizeof(*ebda));
97     ebda->size = esize;
98 }
99
100 static void
101 ram_probe(void)
102 {
103     dprintf(3, "Find memory size\n");
104     if (CONFIG_COREBOOT) {
105         coreboot_setup();
106     } else {
107         // On emulators, get memory size from nvram.
108         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
109                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
110         if (rs)
111             rs += 16 * 1024 * 1024;
112         else
113             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
114                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
115                   + 1 * 1024 * 1024);
116         RamSize = rs;
117         add_e820(0, rs, E820_RAM);
118
119         // Check for memory over 4Gig
120         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
121                     | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
122                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
123         RamSizeOver4G = high;
124         add_e820(0x100000000ull, high, E820_RAM);
125
126         /* reserve 256KB BIOS area at the end of 4 GB */
127         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
128     }
129
130     // Don't declare any memory between 0xa0000 and 0x100000
131     add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
132
133     // Mark known areas as reserved.
134     u16 ebda_seg = get_ebda_seg();
135     add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
136              , E820_RESERVED);
137     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
138
139     u32 count = qemu_cfg_e820_entries();
140     if (count) {
141         struct e820_reservation entry;
142         int i;
143
144         for (i = 0; i < count; i++) {
145             qemu_cfg_e820_load_next(&entry);
146             add_e820(entry.address, entry.length, entry.type);
147         }
148     } else if (kvm_para_available()) {
149         // Backwards compatibility - provide hard coded range.
150         // 4 pages before the bios, 3 pages for vmx tss pages, the
151         // other page for EPT real mode pagetable
152         add_e820(0xfffbc000, 4*4096, E820_RESERVED);
153     }
154
155     dprintf(1, "Ram Size=0x%08x (0x%08x%08x high)\n"
156             , RamSize, (u32)(RamSizeOver4G >> 32), (u32)RamSizeOver4G);
157 }
158
159 static void
160 init_bios_tables(void)
161 {
162     if (CONFIG_COREBOOT) {
163         coreboot_copy_biostable();
164         return;
165     }
166
167     create_pirtable();
168
169     mptable_init();
170
171     smbios_init();
172
173     acpi_bios_init();
174 }
175
176 // Initialize hardware devices
177 static void
178 init_hw(void)
179 {
180     usb_setup();
181     ps2port_setup();
182     lpt_setup();
183     serial_setup();
184
185     floppy_setup();
186     ata_setup();
187     ramdisk_setup();
188     virtio_blk_setup();
189 }
190
191 // Main setup code.
192 static void
193 post(void)
194 {
195     // Detect and init ram.
196     init_ivt();
197     init_bda();
198     memmap_setup();
199     qemu_cfg_port_probe();
200     ram_probe();
201     malloc_setup();
202     thread_setup();
203
204     // Init base pc hardware.
205     pic_setup();
206     timer_setup();
207     mathcp_setup();
208
209     // Initialize mtrr
210     smp_probe_setup();
211     mtrr_setup();
212
213     // Initialize pci
214     pci_setup();
215     smm_init();
216
217     // Initialize internal tables
218     boot_setup();
219     drive_setup();
220     cdemu_setup();
221
222     // Start hardware initialization (if optionrom threading)
223     if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
224         init_hw();
225
226     // Find and initialize other cpus
227     smp_probe();
228
229     // Setup interfaces that option roms may need
230     bios32_setup();
231     pmm_setup();
232     pnp_setup();
233     kbd_setup();
234     mouse_setup();
235     init_bios_tables();
236
237     // Run vga option rom
238     vga_setup();
239
240     // Do hardware initialization (if running synchronously)
241     if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
242         init_hw();
243         wait_threads();
244     }
245
246     // Run option roms
247     optionrom_setup();
248
249     // Run BCVs and show optional boot menu
250     boot_prep();
251
252     // Finalize data structures before boot
253     pmm_finalize();
254     malloc_finalize();
255     memmap_finalize();
256 }
257
258 // 32-bit entry point.
259 void VISIBLE32FLAT
260 _start(void)
261 {
262     init_dma();
263
264     debug_serial_setup();
265     dprintf(1, "Start bios (version %s)\n", VERSION);
266
267     // Allow writes to modify bios area (0xf0000)
268     make_bios_writable();
269
270     // Perform main setup code.
271     post();
272
273     // Setup bios checksum.
274     BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
275
276     // Write protect bios memory.
277     make_bios_readonly();
278
279     // Invoke int 19 to start boot process.
280     dprintf(3, "Jump to int19\n");
281     struct bregs br;
282     memset(&br, 0, sizeof(br));
283     br.flags = F_IF;
284     call16_int(0x19, &br);
285 }