Build mptable using C 'struct's.
[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(0x10, entry_10);
51     set_irq(0x11, entry_11_official);
52     set_irq(0x12, entry_12_official);
53     set_irq(0x13, entry_13_official);
54     set_irq(0x14, entry_14);
55     set_irq(0x15, entry_15);
56     set_irq(0x16, entry_16);
57     set_irq(0x17, entry_17);
58     set_irq(0x18, entry_18);
59     set_irq(0x19, entry_19_official);
60     set_irq(0x1a, entry_1a);
61     set_irq(0x40, entry_40);
62
63     // set vector 0x79 to zero
64     // this is used by 'gardian angel' protection system
65     SET_IVT(0x79, 0, 0);
66
67     __set_irq(0x1E, &diskette_param_table2);
68 }
69
70 static void
71 init_bda()
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 = DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024);
79     SET_BDA(mem_size_kb, 640 - esize);
80     u16 eseg = FLATPTR_TO_SEG((640 - esize) * 1024);
81     SET_BDA(ebda_seg, eseg);
82
83     struct extended_bios_data_area_s *ebda = get_ebda_ptr();
84     memset(ebda, 0, sizeof(*ebda));
85     ebda->size = esize;
86     SET_IVT(0x41, eseg, offsetof(struct extended_bios_data_area_s, fdpt[0]));
87     SET_IVT(0x46, eseg, offsetof(struct extended_bios_data_area_s, fdpt[1]));
88 }
89
90 static void
91 ram_probe(void)
92 {
93     dprintf(3, "Find memory size\n");
94     if (CONFIG_COREBOOT) {
95         coreboot_fill_map();
96     } else {
97         // On emulators, get memory size from nvram.
98         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
99                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
100         if (rs)
101             rs += 16 * 1024 * 1024;
102         else
103             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
104                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
105                   + 1 * 1024 * 1024);
106         RamSize = rs;
107         add_e820(0, rs, E820_RAM);
108
109         // Check for memory over 4Gig
110         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
111                     | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
112                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
113         RamSizeOver4G = high;
114         add_e820(0x100000000ull, high, E820_RAM);
115
116         /* reserve 256KB BIOS area at the end of 4 GB */
117         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
118     }
119
120     // Don't declare any memory between 0xa0000 and 0x100000
121     add_e820(0xa0000, 0x50000, E820_HOLE);
122
123     // Mark known areas as reserved.
124     u16 ebda_seg = get_ebda_seg();
125     add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
126              , E820_RESERVED);
127     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
128
129     if (CONFIG_KVM)
130         // 4 pages before the bios, 3 pages for vmx tss pages, the
131         // other page for EPT real mode pagetable
132         add_e820(0xfffbc000, 4*4096, E820_RESERVED);
133
134     dprintf(1, "Ram Size=0x%08x\n", RamSize);
135 }
136
137 static void
138 init_bios_tables(void)
139 {
140     if (CONFIG_COREBOOT)
141         // XXX - not supported on coreboot yet.
142         return;
143
144     create_pirtable();
145
146     mptable_init();
147
148     smbios_init();
149
150     acpi_bios_init();
151 }
152
153 // Main setup code.
154 static void
155 post()
156 {
157     init_ivt();
158     init_bda();
159
160     pic_setup();
161     timer_setup();
162     mathcp_setup();
163
164     smp_probe_setup();
165
166     memmap_setup();
167     ram_probe();
168     mtrr_setup();
169
170     pnp_setup();
171     vga_setup();
172
173     kbd_setup();
174     lpt_setup();
175     serial_setup();
176     mouse_setup();
177
178     pci_bios_setup();
179     smm_init();
180
181     init_bios_tables();
182     memmap_finalize();
183
184     boot_setup();
185
186     floppy_drive_setup();
187     hard_drive_setup();
188
189     optionrom_setup();
190 }
191
192 // 32-bit entry point.
193 void VISIBLE32
194 _start()
195 {
196     init_dma();
197
198     debug_serial_setup();
199     dprintf(1, "Start bios\n");
200
201     // Allow writes to modify bios area (0xf0000)
202     make_bios_writable();
203
204     // Perform main setup code.
205     post();
206
207     // Run BCVs
208     boot_prep();
209
210     // Setup bios checksum.
211     BiosChecksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
212
213     // Prep for boot process.
214     make_bios_readonly();
215
216     // Invoke int 19 to start boot process.
217     dprintf(3, "Jump to int19\n");
218     struct bregs br;
219     memset(&br, 0, sizeof(br));
220     call16_int(0x19, &br);
221 }
222
223 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
224 // functions to not be exported as a global variable - force _start
225 // to be global here.
226 asm(".global _start");