Add support for more than 4Gig of ram.
[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
21 #define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
22 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
23
24 static 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 // Symbols defined in romlayout.S
32 extern void dummy_iret_handler();
33 extern void entry_08();
34 extern void entry_09();
35 extern void entry_hwirq();
36 extern void entry_0e();
37 extern void entry_10();
38 extern void entry_11();
39 extern void entry_12();
40 extern void entry_13();
41 extern void entry_14();
42 extern void entry_15();
43 extern void entry_16();
44 extern void entry_17();
45 extern void entry_18();
46 extern void entry_19();
47 extern void entry_1a();
48 extern void entry_1c();
49 extern void entry_40();
50 extern void entry_70();
51 extern void entry_74();
52 extern void entry_75();
53 extern void entry_76();
54
55 static void
56 init_bda()
57 {
58     dprintf(3, "init bda\n");
59     memset(bda, 0, sizeof(*bda));
60
61     SET_BDA(mem_size_kb, BASE_MEM_IN_K);
62
63     int i;
64     for (i=0; i<256; i++)
65         set_irq(i, &dummy_iret_handler);
66
67     set_irq(0x08, &entry_08);
68     set_irq(0x09, &entry_09);
69     //set_irq(0x0a, &entry_hwirq);
70     //set_irq(0x0b, &entry_hwirq);
71     //set_irq(0x0c, &entry_hwirq);
72     //set_irq(0x0d, &entry_hwirq);
73     set_irq(0x0e, &entry_0e);
74     //set_irq(0x0f, &entry_hwirq);
75     set_irq(0x10, &entry_10);
76     set_irq(0x11, &entry_11);
77     set_irq(0x12, &entry_12);
78     set_irq(0x13, &entry_13);
79     set_irq(0x14, &entry_14);
80     set_irq(0x15, &entry_15);
81     set_irq(0x16, &entry_16);
82     set_irq(0x17, &entry_17);
83     set_irq(0x18, &entry_18);
84     set_irq(0x19, &entry_19);
85     set_irq(0x1a, &entry_1a);
86     set_irq(0x1c, &entry_1c);
87     set_irq(0x40, &entry_40);
88     set_irq(0x70, &entry_70);
89     //set_irq(0x71, &entry_hwirq);
90     //set_irq(0x72, &entry_hwirq);
91     //set_irq(0x73, &entry_hwirq);
92     set_irq(0x74, &entry_74);
93     set_irq(0x75, &entry_75);
94     set_irq(0x76, &entry_76);
95     //set_irq(0x77, &entry_hwirq);
96
97     // set vector 0x79 to zero
98     // this is used by 'gardian angel' protection system
99     SET_BDA(ivecs[0x79].seg, 0);
100     SET_BDA(ivecs[0x79].offset, 0);
101
102     set_irq(0x1E, &diskette_param_table2);
103 }
104
105 static void
106 init_ebda()
107 {
108     memset(ebda, 0, sizeof(*ebda));
109     ebda->size = EBDA_SIZE;
110     SET_BDA(ebda_seg, SEG_EBDA);
111     SET_BDA(ivecs[0x41].seg, SEG_EBDA);
112     SET_BDA(ivecs[0x41].offset
113             , offsetof(struct extended_bios_data_area_s, fdpt[0]));
114     SET_BDA(ivecs[0x46].seg, SEG_EBDA);
115     SET_BDA(ivecs[0x41].offset
116             , offsetof(struct extended_bios_data_area_s, fdpt[1]));
117 }
118
119 static void
120 ram_probe(void)
121 {
122     dprintf(3, "Find memory size\n");
123     if (CONFIG_COREBOOT) {
124         coreboot_fill_map();
125     } else {
126         // On emulators, get memory size from nvram.
127         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
128                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
129         if (rs)
130             rs += 16 * 1024 * 1024;
131         else
132             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
133                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
134                   + 1 * 1024 * 1024);
135         SET_EBDA(ram_size, rs);
136         add_e820(0, rs, E820_RAM);
137
138         // Check for memory over 4Gig
139         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
140                     | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
141                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
142         SET_EBDA(ram_size_over4G, high);
143         add_e820(0x100000000ull, high, E820_RAM);
144
145         /* reserve 256KB BIOS area at the end of 4 GB */
146         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
147     }
148
149     // Don't declare any memory between 0xa0000 and 0x100000
150     add_e820(0xa0000, 0x50000, E820_HOLE);
151
152     // Mark known areas as reserved.
153     add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
154     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
155
156     dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
157 }
158
159 static void
160 init_bios_tables(void)
161 {
162     if (CONFIG_COREBOOT)
163         // XXX - not supported on coreboot yet.
164         return;
165
166     smm_init();
167
168     create_pirtable();
169
170     mptable_init();
171
172     smbios_init();
173
174     acpi_bios_init();
175 }
176
177 static void
178 init_boot_vectors()
179 {
180     if (! CONFIG_BOOT)
181         return;
182     dprintf(3, "init boot device ordering\n");
183
184     // Floppy drive
185     struct ipl_entry_s *ip = &ebda->ipl.table[0];
186     ip->type = IPL_TYPE_FLOPPY;
187     ip++;
188
189     // First HDD
190     ip->type = IPL_TYPE_HARDDISK;
191     ip++;
192
193     // CDROM
194     if (CONFIG_CDROM_BOOT) {
195         ip->type = IPL_TYPE_CDROM;
196         ip++;
197     }
198
199     ebda->ipl.count = ip - ebda->ipl.table;
200     ebda->ipl.sequence = 0xffff;
201     if (CONFIG_COREBOOT) {
202         // XXX - hardcode defaults for coreboot.
203         ebda->ipl.bootorder = 0x00000231;
204         ebda->ipl.checkfloppysig = 1;
205     } else {
206         // On emulators, get boot order from nvram.
207         ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
208                                | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
209         if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
210             ebda->ipl.checkfloppysig = 1;
211     }
212 }
213
214 // Execute a given option rom.
215 static void
216 callrom(u16 seg, u16 offset)
217 {
218     struct bregs br;
219     memset(&br, 0, sizeof(br));
220     // XXX - should set br.ax to PCI Bus/DevFn
221     br.bx = 0xffff;
222     br.dx = 0xffff;
223     br.es = SEG_BIOS;
224     extern char pnp_string[];
225     br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
226     br.cs = seg;
227     br.ip = offset;
228     call16(&br);
229
230     debug_serial_setup();
231 }
232
233 // Find and run any "option roms" found in the given address range.
234 static void
235 rom_scan(u32 start, u32 end)
236 {
237     if (! CONFIG_OPTIONROMS)
238         return;
239
240     u8 *p = (u8*)start;
241     for (; p <= (u8*)end; p += 2048) {
242         u8 *rom = p;
243         if (*(u16*)rom != 0xaa55)
244             continue;
245         u32 len = rom[2] * 512;
246         u8 sum = checksum(rom, len);
247         if (sum != 0) {
248             dprintf(1, "Found option rom with bad checksum:"
249                     " loc=%p len=%d sum=%x\n"
250                     , rom, len, sum);
251             continue;
252         }
253         p = (u8*)(((u32)p + len) / 2048 * 2048);
254         dprintf(1, "Running option rom at %p\n", rom+3);
255         callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
256
257         if (GET_BDA(ebda_seg) != SEG_EBDA)
258             BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
259                      , rom, SEG_EBDA, GET_BDA(ebda_seg));
260
261         // Look at the ROM's PnP Expansion header.  Properly, we're supposed
262         // to init all the ROMs and then go back and build an IPL table of
263         // all the bootable devices, but we can get away with one pass.
264         if (rom[0x1a] != '$' || rom[0x1b] != 'P'
265             || rom[0x1c] != 'n' || rom[0x1d] != 'P')
266             continue;
267         // 0x1A is also the offset into the expansion header of...
268         // the Bootstrap Entry Vector, or zero if there is none.
269         u16 entry = *(u16*)&rom[0x1a+0x1a];
270         if (!entry)
271             continue;
272         // Found a device that thinks it can boot the system.  Record
273         // its BEV and product name string.
274
275         if (! CONFIG_BOOT)
276             continue;
277
278         if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
279             continue;
280
281         struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
282         ip->type = IPL_TYPE_BEV;
283         ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
284
285         u16 desc = *(u16*)&rom[0x1a+0x10];
286         if (desc)
287             ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
288
289         ebda->ipl.count++;
290     }
291 }
292
293 // Call into vga code to turn on console.
294 static void
295 vga_setup()
296 {
297     dprintf(1, "Scan for VGA option rom\n");
298     rom_scan(0xc0000, 0xc7800);
299
300     dprintf(1, "Turning on vga console\n");
301     struct bregs br;
302     memset(&br, 0, sizeof(br));
303     br.ax = 0x0003;
304     call16_int(0x10, &br);
305 }
306
307 // Main setup code.
308 static void
309 post()
310 {
311     init_bda();
312     init_ebda();
313
314     pic_setup();
315     timer_setup();
316     kbd_setup();
317     lpt_setup();
318     serial_setup();
319     mouse_setup();
320     mathcp_setup();
321
322     memmap_setup();
323
324     ram_probe();
325
326     vga_setup();
327
328     printf("BIOS - begin\n\n");
329
330     pci_bios_setup();
331
332     init_bios_tables();
333
334     memmap_finalize();
335
336     floppy_drive_setup();
337     hard_drive_setup();
338
339     init_boot_vectors();
340
341     dprintf(1, "Scan for option roms\n");
342     rom_scan(0xc8000, 0xe0000);
343 }
344
345 // Clear .bss section for C code.
346 static void
347 clear_bss()
348 {
349     dprintf(3, "clearing .bss section\n");
350     extern char __bss_start[], __bss_end[];
351     memset(__bss_start, 0, __bss_end - __bss_start);
352 }
353
354 // Reset DMA controller
355 static void
356 init_dma()
357 {
358     // first reset the DMA controllers
359     outb(0, PORT_DMA1_MASTER_CLEAR);
360     outb(0, PORT_DMA2_MASTER_CLEAR);
361
362     // then initialize the DMA controllers
363     outb(0xc0, PORT_DMA2_MODE_REG);
364     outb(0x00, PORT_DMA2_MASK_REG);
365 }
366
367 // Check if the machine was setup with a special restart vector.
368 static void
369 check_restart_status()
370 {
371     // Get and then clear CMOS shutdown status.
372     u8 status = inb_cmos(CMOS_RESET_CODE);
373     outb_cmos(0, CMOS_RESET_CODE);
374
375     if (status == 0x00 || status == 0x09 || status >= 0x0d)
376         // Normal post
377         return;
378
379     if (status != 0x05) {
380         BX_PANIC("Unimplemented shutdown status: %02x\n", status);
381         return;
382     }
383
384     // XXX - this is supposed to jump without changing any memory -
385     // but the stack has been altered by the time the code gets here.
386     eoi_pic2();
387     struct bregs br;
388     memset(&br, 0, sizeof(br));
389     br.cs = GET_BDA(jump_cs_ip) >> 16;
390     br.ip = GET_BDA(jump_cs_ip);
391     call16(&br);
392 }
393
394 // 32-bit entry point.
395 void VISIBLE32
396 _start()
397 {
398     init_dma();
399     check_restart_status();
400
401     debug_serial_setup();
402     dprintf(1, "Start bios\n");
403
404     // Setup for .bss and .data sections
405     clear_bss();
406     make_bios_writable();
407
408     // Perform main setup code.
409     post();
410
411     // Present the user with a bootup menu.
412     interactive_bootmenu();
413
414     // Setup bios checksum.
415     extern char bios_checksum;
416     bios_checksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
417
418     // Prep for boot process.
419     make_bios_readonly();
420     clear_bss();
421
422     // Invoke int 19 to start boot process.
423     dprintf(3, "Jump to int19\n");
424     struct bregs br;
425     memset(&br, 0, sizeof(br));
426     call16_int(0x19, &br);
427 }
428
429 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
430 // functions to not be exported as a global variable - force _start
431 // to be global here.
432 asm(".global _start");