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