Initial PnP bios call support.
[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 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     SET_BDA(mem_size_kb, BASE_MEM_IN_K);
43
44     // Initialize all vectors to a dummy handler.
45     int i;
46     for (i=0; i<256; i++)
47         set_irq(i, dummy_iret_handler);
48
49     // Initialize all hw vectors to a default hw handler.
50     for (i=0x08; i<=0x0f; i++)
51         set_irq(i, entry_hwpic1);
52     for (i=0x70; i<=0x77; i++)
53         set_irq(i, entry_hwpic2);
54
55     // Initialize software handlers.
56     set_irq(0x10, entry_10);
57     set_irq(0x11, entry_11_official);
58     set_irq(0x12, entry_12_official);
59     set_irq(0x13, entry_13_official);
60     set_irq(0x14, entry_14);
61     set_irq(0x15, entry_15);
62     set_irq(0x16, entry_16);
63     set_irq(0x17, entry_17);
64     set_irq(0x18, entry_18);
65     set_irq(0x19, entry_19_official);
66     set_irq(0x1a, entry_1a);
67     set_irq(0x1c, entry_1c);
68     set_irq(0x40, entry_40);
69
70     // set vector 0x79 to zero
71     // this is used by 'gardian angel' protection system
72     SET_BDA(ivecs[0x79].seg, 0);
73     SET_BDA(ivecs[0x79].offset, 0);
74
75     __set_irq(0x1E, &diskette_param_table2);
76 }
77
78 static void
79 init_ebda()
80 {
81     memset(ebda, 0, sizeof(*ebda));
82     ebda->size = EBDA_SIZE;
83     SET_BDA(ebda_seg, SEG_EBDA);
84     SET_BDA(ivecs[0x41].seg, SEG_EBDA);
85     SET_BDA(ivecs[0x41].offset
86             , offsetof(struct extended_bios_data_area_s, fdpt[0]));
87     SET_BDA(ivecs[0x46].seg, SEG_EBDA);
88     SET_BDA(ivecs[0x41].offset
89             , offsetof(struct extended_bios_data_area_s, fdpt[1]));
90 }
91
92 static void
93 ram_probe(void)
94 {
95     dprintf(3, "Find memory size\n");
96     if (CONFIG_COREBOOT) {
97         coreboot_fill_map();
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         SET_EBDA(ram_size, 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         SET_EBDA(ram_size_over4G, 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(0xa0000, 0x50000, E820_HOLE);
124
125     // Mark known areas as reserved.
126     add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
127     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
128
129     dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
130 }
131
132 static void
133 init_bios_tables(void)
134 {
135     if (CONFIG_COREBOOT)
136         // XXX - not supported on coreboot yet.
137         return;
138
139     create_pirtable();
140
141     mptable_init();
142
143     smbios_init();
144
145     acpi_bios_init();
146 }
147
148 static void
149 init_boot_vectors()
150 {
151     if (! CONFIG_BOOT)
152         return;
153     dprintf(3, "init boot device ordering\n");
154
155     // Floppy drive
156     struct ipl_entry_s *ip = &ebda->ipl.table[0];
157     ip->type = IPL_TYPE_FLOPPY;
158     ip++;
159
160     // First HDD
161     ip->type = IPL_TYPE_HARDDISK;
162     ip++;
163
164     // CDROM
165     if (CONFIG_CDROM_BOOT) {
166         ip->type = IPL_TYPE_CDROM;
167         ip++;
168     }
169
170     ebda->ipl.count = ip - ebda->ipl.table;
171     ebda->ipl.sequence = 0xffff;
172     if (CONFIG_COREBOOT) {
173         // XXX - hardcode defaults for coreboot.
174         ebda->ipl.bootorder = 0x00000231;
175         ebda->ipl.checkfloppysig = 1;
176     } else {
177         // On emulators, get boot order from nvram.
178         ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
179                                | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
180         if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
181             ebda->ipl.checkfloppysig = 1;
182     }
183 }
184
185 // Main setup code.
186 static void
187 post()
188 {
189     init_bda();
190     init_ebda();
191
192     pic_setup();
193     timer_setup();
194     mathcp_setup();
195
196     smp_probe_setup();
197
198     memmap_setup();
199     ram_probe();
200
201     pnp_setup();
202     vga_setup();
203
204     kbd_setup();
205     lpt_setup();
206     serial_setup();
207     mouse_setup();
208
209     pci_bios_setup();
210     smm_init();
211
212     init_bios_tables();
213     memmap_finalize();
214
215     floppy_drive_setup();
216     hard_drive_setup();
217
218     init_boot_vectors();
219
220     optionrom_setup();
221 }
222
223 // 32-bit entry point.
224 void VISIBLE32
225 _start()
226 {
227     init_dma();
228
229     debug_serial_setup();
230     dprintf(1, "Start bios\n");
231
232     // Allow writes to modify bios area (0xf0000)
233     make_bios_writable();
234
235     // Perform main setup code.
236     post();
237
238     // Present the user with a bootup menu.
239     interactive_bootmenu();
240
241     // Setup bios checksum.
242     extern char bios_checksum;
243     bios_checksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
244
245     // Prep for boot process.
246     make_bios_readonly();
247
248     // Invoke int 19 to start boot process.
249     dprintf(3, "Jump to int19\n");
250     struct bregs br;
251     memset(&br, 0, sizeof(br));
252     call16_int(0x19, &br);
253 }
254
255 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
256 // functions to not be exported as a global variable - force _start
257 // to be global here.
258 asm(".global _start");