Verify that option roms don't try to move the ebda block.
[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 "../out/rom16.offset.auto.h" // OFFSET_*
10 #include "config.h" // CONFIG_*
11 #include "cmos.h" // CMOS_*
12 #include "util.h" // memset
13 #include "biosvar.h" // struct bios_data_area_s
14 #include "ata.h" // hard_drive_setup
15 #include "kbd.h" // kbd_setup
16 #include "disk.h" // floppy_drive_setup
17
18 #define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
19 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
20
21 static void
22 init_bda()
23 {
24     memset(bda, 0, sizeof(*bda));
25
26     int i;
27     for (i=0; i<256; i++) {
28         SET_BDA(ivecs[i].seg, SEG_BIOS);
29         SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
30     }
31
32     SET_BDA(mem_size_kb, BASE_MEM_IN_K);
33
34     // Set BDA Equipment Word - 0x06 = FPU enable, mouse installed
35     SET_BDA(equipment_list_flags, 0x06);
36
37     // set vector 0x79 to zero
38     // this is used by 'gardian angel' protection system
39     SET_BDA(ivecs[0x79].seg, 0);
40     SET_BDA(ivecs[0x79].offset, 0);
41
42     SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
43     SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
44     SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
45     SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
46     SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
47     SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
48     SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
49     SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
50     SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
51     SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
52     SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
53     SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
54     SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
55     SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
56     SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
57     SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
58     SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
59     SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
60     SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
61     SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
62
63     SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
64 }
65
66 static void
67 init_ebda()
68 {
69     memset(ebda, 0, sizeof(*ebda));
70     ebda->size = EBDA_SIZE;
71     SET_BDA(ebda_seg, SEG_EBDA);
72     SET_BDA(ivecs[0x41].seg, SEG_EBDA);
73     SET_BDA(ivecs[0x41].offset
74             , offsetof(struct extended_bios_data_area_s, fdpt[0]));
75     SET_BDA(ivecs[0x46].seg, SEG_EBDA);
76     SET_BDA(ivecs[0x41].offset
77             , offsetof(struct extended_bios_data_area_s, fdpt[1]));
78 }
79
80 static void
81 ram_probe(void)
82 {
83     u32 rs;
84     if (CONFIG_COREBOOT) {
85         // XXX - just hardcode for now.
86         rs = 128*1024*1024;
87     } else {
88         // On emulators, get memory size from nvram.
89         rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
90               | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
91         if (rs)
92             rs += 16 * 1024 * 1024;
93         else
94             rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
95                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
96                   + 1 * 1024 * 1024);
97     }
98
99     SET_EBDA(ram_size, rs);
100     dprintf(1, "ram_size=0x%08x\n", rs);
101 }
102
103 static void
104 pic_setup()
105 {
106     outb(0x11, PORT_PIC1);
107     outb(0x11, PORT_PIC2);
108     outb(0x08, PORT_PIC1_DATA);
109     outb(0x70, PORT_PIC2_DATA);
110     outb(0x04, PORT_PIC1_DATA);
111     outb(0x02, PORT_PIC2_DATA);
112     outb(0x01, PORT_PIC1_DATA);
113     outb(0x01, PORT_PIC2_DATA);
114     outb(0xb8, PORT_PIC1_DATA);
115     if (CONFIG_PS2_MOUSE)
116         outb(0x8f, PORT_PIC2_DATA);
117     else
118         outb(0x9f, PORT_PIC2_DATA);
119 }
120
121 static void
122 init_boot_vectors()
123 {
124     // Floppy drive
125     struct ipl_entry_s *ip = &ebda->ipl.table[0];
126     ip->type = IPL_TYPE_FLOPPY;
127     ip++;
128
129     // First HDD
130     ip->type = IPL_TYPE_HARDDISK;
131     ip++;
132
133     // CDROM
134     if (CONFIG_CDROM_BOOT) {
135         ip->type = IPL_TYPE_CDROM;
136         ip++;
137     }
138
139     ebda->ipl.count = ip - ebda->ipl.table;
140     ebda->ipl.sequence = 0xffff;
141     if (CONFIG_COREBOOT) {
142         // XXX - hardcode defaults for coreboot.
143         ebda->ipl.bootorder = 0x00000231;
144         ebda->ipl.checkfloppysig = 1;
145     } else {
146         // On emulators, get boot order from nvram.
147         ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
148                                | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
149         if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
150             ebda->ipl.checkfloppysig = 1;
151     }
152 }
153
154 static void
155 callrom(u16 seg, u16 offset)
156 {
157     struct bregs br;
158     memset(&br, 0, sizeof(br));
159     br.es = SEG_BIOS;
160     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
161     br.cs = seg;
162     br.ip = offset;
163     call16(&br);
164 }
165
166 static void
167 rom_scan(u32 start, u32 end)
168 {
169     u8 *p = (u8*)start;
170     for (; p <= (u8*)end; p += 2048) {
171         u8 *rom = p;
172         if (*(u16*)rom != 0xaa55)
173             continue;
174         u32 len = rom[2] * 512;
175         if (checksum(rom, len) != 0)
176             continue;
177         p = (u8*)(((u32)p + len) / 2048 * 2048);
178         dprintf(1, "Running option rom at %p\n", rom+3);
179         callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
180
181         if (GET_BDA(ebda_seg) != SEG_EBDA)
182             BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
183                      , rom, SEG_EBDA, GET_BDA(ebda_seg));
184
185         // Look at the ROM's PnP Expansion header.  Properly, we're supposed
186         // to init all the ROMs and then go back and build an IPL table of
187         // all the bootable devices, but we can get away with one pass.
188         if (rom[0x1a] != '$' || rom[0x1b] != 'P'
189             || rom[0x1c] != 'n' || rom[0x1d] != 'P')
190             continue;
191         // 0x1A is also the offset into the expansion header of...
192         // the Bootstrap Entry Vector, or zero if there is none.
193         u16 entry = *(u16*)&rom[0x1a+0x1a];
194         if (!entry)
195             continue;
196         // Found a device that thinks it can boot the system.  Record
197         // its BEV and product name string.
198
199         if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
200             continue;
201
202         struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
203         ip->type = IPL_TYPE_BEV;
204         ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
205
206         u16 desc = *(u16*)&rom[0x1a+0x10];
207         if (desc)
208             ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
209
210         ebda->ipl.count++;
211     }
212 }
213
214 static void
215 post()
216 {
217     if (CONFIG_DEBUG_SERIAL)
218         debug_serial_setup();
219
220     dprintf(1, "Start bios\n");
221
222     dprintf(3, "init bda\n");
223     init_bda();
224     init_ebda();
225
226     dprintf(3, "init timer\n");
227     timer_setup();
228     dprintf(3, "init keyboard\n");
229     kbd_setup();
230     dprintf(3, "init lpt\n");
231     lpt_setup();
232     dprintf(3, "init serial\n");
233     serial_setup();
234     dprintf(3, "init pic\n");
235     pic_setup();
236
237     dprintf(3, "Find memory size\n");
238     ram_probe();
239
240     dprintf(1, "Scan for VGA option rom\n");
241     rom_scan(0xc0000, 0xc7800);
242
243     printf("BIOS - begin\n\n");
244
245     // clear bss section -- XXX - shouldn't use globals
246     extern char __bss_start[], __bss_end[];
247     memset(__bss_start, 0, __bss_end - __bss_start);
248
249     dprintf(3, "rombios32 init\n");
250     rombios32_init();
251
252     dprintf(3, "init floppy drives\n");
253     floppy_drive_setup();
254     dprintf(3, "init hard drives\n");
255     hard_drive_setup();
256
257     dprintf(3, "init boot device ordering\n");
258     init_boot_vectors();
259
260     dprintf(1, "Scan for option roms\n");
261     rom_scan(0xc8000, 0xe0000);
262
263     interactive_bootmenu();
264
265     // reset the memory (some boot loaders such as syslinux suppose
266     // that the memory is set to zero)
267     memset((void*)0x40000, 0, 0x40000); // XXX - shouldn't use globals
268
269     // Invoke int 19 to start boot process.
270     dprintf(3, "Jump to int19\n");
271     struct bregs br;
272     memset(&br, 0, sizeof(br));
273     call16_int(0x19, &br);
274 }
275
276 static void
277 init_dma()
278 {
279     // first reset the DMA controllers
280     outb(0, PORT_DMA1_MASTER_CLEAR);
281     outb(0, PORT_DMA2_MASTER_CLEAR);
282
283     // then initialize the DMA controllers
284     outb(0xc0, PORT_DMA2_MODE_REG);
285     outb(0x00, PORT_DMA2_MASK_REG);
286 }
287
288 static void
289 check_restart_status()
290 {
291     // Get and then clear CMOS shutdown status.
292     u8 status = inb_cmos(CMOS_RESET_CODE);
293     outb_cmos(0, CMOS_RESET_CODE);
294
295     if (status == 0x00 || status == 0x09 || status >= 0x0d)
296         // Normal post
297         return;
298
299     if (status != 0x05) {
300         BX_PANIC("Unimplemented shutdown status: %02x\n", status);
301         return;
302     }
303
304     // XXX - this is supposed to jump without changing any memory -
305     // but the stack has been altered by the time the code gets here.
306     eoi_both_pics();
307     struct bregs br;
308     memset(&br, 0, sizeof(br));
309     br.cs = GET_BDA(jump_cs_ip) >> 16;
310     br.ip = GET_BDA(jump_cs_ip);
311     call16(&br);
312 }
313
314 void VISIBLE32
315 _start()
316 {
317     init_dma();
318     check_restart_status();
319
320     post();
321 }
322
323 // Externally visible 32bit entry point.
324 asm(
325     ".global post32\n"
326     "post32:\n"
327     "cli\n"
328     "cld\n"
329     "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
330     "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
331     "movl $" __stringify(CONFIG_STACK_OFFSET) ", %esp\n"
332     "ljmp $0x10, $_start\n"
333     );