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