c2a9b62200a4ace39020c45b22ccd7309ecbbc0b
[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 GPLv3 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 "ata.h" // hard_drive_setup
14 #include "disk.h" // floppy_drive_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 "boot.h" // IPL
21
22 #define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
23
24 void
25 __set_irq(int vector, void *loc)
26 {
27     SET_BDA(ivecs[vector].seg, SEG_BIOS);
28     SET_BDA(ivecs[vector].offset, (u32)loc - BUILD_BIOS_ADDR);
29 }
30
31 #define set_irq(vector, func) do {              \
32         extern void func (void);                \
33         __set_irq(vector, func);                \
34     } while (0)
35
36 static void
37 init_bda()
38 {
39     dprintf(3, "init bda\n");
40     memset(bda, 0, sizeof(*bda));
41
42     // Initialize all vectors to a dummy handler.
43     int i;
44     for (i=0; i<256; i++)
45         set_irq(i, dummy_iret_handler);
46
47     // Initialize all hw vectors to a default hw handler.
48     for (i=0x08; i<=0x0f; i++)
49         set_irq(i, entry_hwpic1);
50     for (i=0x70; i<=0x77; i++)
51         set_irq(i, entry_hwpic2);
52
53     // Initialize software handlers.
54     set_irq(0x10, entry_10);
55     set_irq(0x11, entry_11_official);
56     set_irq(0x12, entry_12_official);
57     set_irq(0x13, entry_13_official);
58     set_irq(0x14, entry_14);
59     set_irq(0x15, entry_15);
60     set_irq(0x16, entry_16);
61     set_irq(0x17, entry_17);
62     set_irq(0x18, entry_18);
63     set_irq(0x19, entry_19_official);
64     set_irq(0x1a, entry_1a);
65     set_irq(0x1c, entry_1c);
66     set_irq(0x40, entry_40);
67
68     // set vector 0x79 to zero
69     // this is used by 'gardian angel' protection system
70     SET_BDA(ivecs[0x79].seg, 0);
71     SET_BDA(ivecs[0x79].offset, 0);
72
73     __set_irq(0x1E, &diskette_param_table2);
74 }
75
76 static void
77 init_ebda()
78 {
79     int esize = DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024);
80     SET_BDA(mem_size_kb, 640 - esize);
81     u16 eseg = FARPTR_TO_SEG((640 - esize) * 1024);
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_BDA(ivecs[0x41].seg, eseg);
88     SET_BDA(ivecs[0x41].offset
89             , offsetof(struct extended_bios_data_area_s, fdpt[0]));
90     SET_BDA(ivecs[0x46].seg, eseg);
91     SET_BDA(ivecs[0x46].offset
92             , offsetof(struct extended_bios_data_area_s, fdpt[1]));
93 }
94
95 static void
96 ram_probe(void)
97 {
98     dprintf(3, "Find memory size\n");
99     if (CONFIG_COREBOOT) {
100         coreboot_fill_map();
101     } else {
102         // On emulators, get memory size from nvram.
103         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
104                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
105         if (rs)
106             rs += 16 * 1024 * 1024;
107         else
108             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
109                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
110                   + 1 * 1024 * 1024);
111         RamSize = rs;
112         add_e820(0, rs, E820_RAM);
113
114         // Check for memory over 4Gig
115         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
116                     | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
117                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
118         RamSizeOver4G = high;
119         add_e820(0x100000000ull, high, E820_RAM);
120
121         /* reserve 256KB BIOS area at the end of 4 GB */
122         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
123     }
124
125     // Don't declare any memory between 0xa0000 and 0x100000
126     add_e820(0xa0000, 0x50000, E820_HOLE);
127
128     // Mark known areas as reserved.
129     u16 ebda_seg = get_ebda_seg();
130     add_e820((u32)MAKE_FARPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
131              , E820_RESERVED);
132     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, 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 static void
154 init_boot_vectors()
155 {
156     if (! CONFIG_BOOT)
157         return;
158     dprintf(3, "init boot device ordering\n");
159
160     // Floppy drive
161     struct ipl_entry_s *ip = &IPL.table[0];
162     ip->type = IPL_TYPE_FLOPPY;
163     ip++;
164
165     // First HDD
166     ip->type = IPL_TYPE_HARDDISK;
167     ip++;
168
169     // CDROM
170     if (CONFIG_CDROM_BOOT) {
171         ip->type = IPL_TYPE_CDROM;
172         ip++;
173     }
174
175     IPL.count = ip - IPL.table;
176     SET_EBDA(boot_sequence, 0xffff);
177     if (CONFIG_COREBOOT) {
178         // XXX - hardcode defaults for coreboot.
179         IPL.bootorder = 0x00000231;
180         IPL.checkfloppysig = 1;
181     } else {
182         // On emulators, get boot order from nvram.
183         IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
184                          | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
185         if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
186             IPL.checkfloppysig = 1;
187     }
188 }
189
190 // Main setup code.
191 static void
192 post()
193 {
194     init_bda();
195     init_ebda();
196
197     pic_setup();
198     timer_setup();
199     mathcp_setup();
200
201     smp_probe_setup();
202
203     memmap_setup();
204     ram_probe();
205
206     pnp_setup();
207     vga_setup();
208
209     kbd_setup();
210     lpt_setup();
211     serial_setup();
212     mouse_setup();
213
214     pci_bios_setup();
215     smm_init();
216
217     init_bios_tables();
218     memmap_finalize();
219
220     floppy_drive_setup();
221     hard_drive_setup();
222
223     init_boot_vectors();
224
225     optionrom_setup();
226 }
227
228 // 32-bit entry point.
229 void VISIBLE32
230 _start()
231 {
232     init_dma();
233
234     debug_serial_setup();
235     dprintf(1, "Start bios\n");
236
237     // Allow writes to modify bios area (0xf0000)
238     make_bios_writable();
239
240     // Perform main setup code.
241     post();
242
243     // Present the user with a bootup menu.
244     interactive_bootmenu();
245
246     // Setup bios checksum.
247     extern char bios_checksum;
248     bios_checksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
249
250     // Prep for boot process.
251     make_bios_readonly();
252
253     // Invoke int 19 to start boot process.
254     dprintf(3, "Jump to int19\n");
255     struct bregs br;
256     memset(&br, 0, sizeof(br));
257     call16_int(0x19, &br);
258 }
259
260 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
261 // functions to not be exported as a global variable - force _start
262 // to be global here.
263 asm(".global _start");