Avoid -fwhole-program on broken gcc instead of stopping build.
[seabios.git] / src / post.c
1 // 32bit code to Power On Self Test (POST) a machine.
2 //
3 // Copyright (C) 2008  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 "memmap.h" // add_e820
15 #include "pic.h" // pic_setup
16 #include "pci.h" // create_pirtable
17 #include "acpi.h" // acpi_bios_init
18 #include "bregs.h" // struct bregs
19 #include "mptable.h" // mptable_init
20 #include "boot.h" // IPL
21
22 void
23 __set_irq(int vector, void *loc)
24 {
25     SET_IVT(vector, SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR);
26 }
27
28 #define set_irq(vector, func) do {              \
29         extern void func (void);                \
30         __set_irq(vector, func);                \
31     } while (0)
32
33 static void
34 init_ivt()
35 {
36     dprintf(3, "init ivt\n");
37
38     // Initialize all vectors to the default handler.
39     int i;
40     for (i=0; i<256; i++)
41         set_irq(i, entry_iret_official);
42
43     // Initialize all hw vectors to a default hw handler.
44     for (i=0x08; i<=0x0f; i++)
45         set_irq(i, entry_hwpic1);
46     for (i=0x70; i<=0x77; i++)
47         set_irq(i, entry_hwpic2);
48
49     // Initialize software handlers.
50     set_irq(0x02, entry_02);
51     set_irq(0x10, entry_10);
52     set_irq(0x11, entry_11);
53     set_irq(0x12, entry_12);
54     set_irq(0x13, entry_13_official);
55     set_irq(0x14, entry_14);
56     set_irq(0x15, entry_15);
57     set_irq(0x16, entry_16);
58     set_irq(0x17, entry_17);
59     set_irq(0x18, entry_18);
60     set_irq(0x19, entry_19_official);
61     set_irq(0x1a, entry_1a);
62     set_irq(0x40, entry_40);
63
64     // set vector 0x79 to zero
65     // this is used by 'gardian angel' protection system
66     SET_IVT(0x79, 0, 0);
67
68     __set_irq(0x1E, &diskette_param_table2);
69 }
70
71 static void
72 init_bda()
73 {
74     dprintf(3, "init bda\n");
75
76     struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
77     memset(bda, 0, sizeof(*bda));
78
79     int esize = EBDA_SIZE_START;
80     SET_BDA(mem_size_kb, 640 - esize);
81     u16 eseg = EBDA_SEGMENT_START;
82     SET_BDA(ebda_seg, eseg);
83
84     struct extended_bios_data_area_s *ebda = get_ebda_ptr();
85     memset(ebda, 0, sizeof(*ebda));
86     ebda->size = esize;
87     SET_IVT(0x41, eseg, offsetof(struct extended_bios_data_area_s, fdpt[0]));
88     SET_IVT(0x46, eseg, offsetof(struct extended_bios_data_area_s, fdpt[1]));
89 }
90
91 static void
92 ram_probe(void)
93 {
94     dprintf(3, "Find memory size\n");
95     if (CONFIG_COREBOOT) {
96         coreboot_setup();
97     } else {
98         // On emulators, get memory size from nvram.
99         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
100                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
101         if (rs)
102             rs += 16 * 1024 * 1024;
103         else
104             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
105                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
106                   + 1 * 1024 * 1024);
107         RamSize = rs;
108         add_e820(0, rs, E820_RAM);
109
110         // Check for memory over 4Gig
111         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
112                     | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
113                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
114         RamSizeOver4G = high;
115         add_e820(0x100000000ull, high, E820_RAM);
116
117         /* reserve 256KB BIOS area at the end of 4 GB */
118         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
119     }
120
121     // Don't declare any memory between 0xa0000 and 0x100000
122     add_e820(0xa0000, 0x50000, E820_HOLE);
123
124     // Mark known areas as reserved.
125     u16 ebda_seg = get_ebda_seg();
126     add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
127              , E820_RESERVED);
128     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
129
130     if (CONFIG_KVM)
131         // 4 pages before the bios, 3 pages for vmx tss pages, the
132         // other page for EPT real mode pagetable
133         add_e820(0xfffbc000, 4*4096, E820_RESERVED);
134
135     dprintf(1, "Ram Size=0x%08x\n", RamSize);
136 }
137
138 static void
139 init_bios_tables(void)
140 {
141     if (CONFIG_COREBOOT)
142         // XXX - not supported on coreboot yet.
143         return;
144
145     create_pirtable();
146
147     mptable_init();
148
149     smbios_init();
150
151     acpi_bios_init();
152 }
153
154 // Main setup code.
155 static void
156 post()
157 {
158     init_ivt();
159     init_bda();
160
161     pic_setup();
162     timer_setup();
163     mathcp_setup();
164
165     smp_probe_setup();
166
167     memmap_setup();
168     ram_probe();
169     mtrr_setup();
170
171     pnp_setup();
172     vga_setup();
173
174     kbd_setup();
175     lpt_setup();
176     serial_setup();
177     mouse_setup();
178
179     pci_bios_setup();
180     smm_init();
181
182     init_bios_tables();
183     memmap_finalize();
184
185     boot_setup();
186
187     floppy_drive_setup();
188     hard_drive_setup();
189
190     optionrom_setup();
191 }
192
193 // 32-bit entry point.
194 void VISIBLE32
195 _start()
196 {
197     init_dma();
198
199     debug_serial_setup();
200     dprintf(1, "Start bios\n");
201
202     // Allow writes to modify bios area (0xf0000)
203     make_bios_writable();
204
205     // Perform main setup code.
206     post();
207
208     // Run BCVs
209     boot_prep();
210
211     // Setup bios checksum.
212     BiosChecksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
213
214     // Prep for boot process.
215     make_bios_readonly();
216
217     // Invoke int 19 to start boot process.
218     dprintf(3, "Jump to int19\n");
219     struct bregs br;
220     memset(&br, 0, sizeof(br));
221     call16_int(0x19, &br);
222 }