blob: 718cc2a22470a78074785ab4d901078891557f0d [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 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
Kevin O'Connor2ad37442008-05-06 19:49:01 -040014#include "ata.h" // hard_drive_setup
Kevin O'Connor3bbcc142008-04-13 17:07:33 -040015#include "kbd.h" // kbd_setup
16#include "disk.h" // floppy_drive_setup
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050017
Kevin O'Connor438f6352008-03-30 21:46:53 -040018#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))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050020
21static void
22init_bda()
23{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040024 dprintf(3, "init bda\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050025 memset(bda, 0, sizeof(*bda));
26
27 int i;
28 for (i=0; i<256; i++) {
Kevin O'Connore9061492008-03-11 21:54:18 -040029 SET_BDA(ivecs[i].seg, SEG_BIOS);
30 SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050031 }
32
Kevin O'Connore9061492008-03-11 21:54:18 -040033 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
Kevin O'Connor9bd83912008-03-08 12:42:36 -050034
Kevin O'Connor88c00ee2008-05-18 00:14:13 -040035 // Set BDA Equipment Word - 0x06 = FPU enable, mouse installed
36 SET_BDA(equipment_list_flags, 0x06);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050038 // set vector 0x79 to zero
39 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040040 SET_BDA(ivecs[0x79].seg, 0);
41 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050042
Kevin O'Connore9061492008-03-11 21:54:18 -040043 SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
44 SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
45 SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
46 SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
47 SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
48 SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
49 SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
50 SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
51 SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
52 SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
53 SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
54 SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
55 SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
56 SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
57 SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
58 SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
59 SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
60 SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
61 SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
62 SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
Kevin O'Connor3bbcc142008-04-13 17:07:33 -040063
64 SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050065}
66
67static void
68init_ebda()
69{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050070 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050071 ebda->size = EBDA_SIZE;
Kevin O'Connor438f6352008-03-30 21:46:53 -040072 SET_BDA(ebda_seg, SEG_EBDA);
73 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040074 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040075 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
Kevin O'Connor438f6352008-03-30 21:46:53 -040076 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040077 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040078 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079}
80
81static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -040082ram_probe(void)
83{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040084 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040085 u32 rs;
86 if (CONFIG_COREBOOT) {
87 // XXX - just hardcode for now.
88 rs = 128*1024*1024;
89 } else {
90 // On emulators, get memory size from nvram.
91 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
Kevin O'Connor9571ac22008-05-17 22:20:27 -040092 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040093 if (rs)
94 rs += 16 * 1024 * 1024;
95 else
96 rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
97 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
98 + 1 * 1024 * 1024);
99 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400100
101 SET_EBDA(ram_size, rs);
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400102 dprintf(1, "ram_size=0x%08x\n", rs);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400103}
104
105static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500106pic_setup()
107{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400108 dprintf(3, "init pic\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500109 outb(0x11, PORT_PIC1);
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500110 outb(0x11, PORT_PIC2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500111 outb(0x08, PORT_PIC1_DATA);
112 outb(0x70, PORT_PIC2_DATA);
113 outb(0x04, PORT_PIC1_DATA);
114 outb(0x02, PORT_PIC2_DATA);
115 outb(0x01, PORT_PIC1_DATA);
116 outb(0x01, PORT_PIC2_DATA);
117 outb(0xb8, PORT_PIC1_DATA);
118 if (CONFIG_PS2_MOUSE)
119 outb(0x8f, PORT_PIC2_DATA);
120 else
121 outb(0x9f, PORT_PIC2_DATA);
122}
123
124static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500125init_boot_vectors()
126{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400127 dprintf(3, "init boot device ordering\n");
128
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500129 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400130 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500131 ip->type = IPL_TYPE_FLOPPY;
132 ip++;
133
134 // First HDD
135 ip->type = IPL_TYPE_HARDDISK;
136 ip++;
137
138 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500139 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500140 ip->type = IPL_TYPE_CDROM;
141 ip++;
142 }
143
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400144 ebda->ipl.count = ip - ebda->ipl.table;
145 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400146 if (CONFIG_COREBOOT) {
147 // XXX - hardcode defaults for coreboot.
148 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400149 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400150 } else {
151 // On emulators, get boot order from nvram.
152 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
153 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
154 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
155 ebda->ipl.checkfloppysig = 1;
156 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500157}
158
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400159// Execute a given option rom.
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500160static void
161callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500162{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500163 struct bregs br;
164 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500165 br.es = SEG_BIOS;
166 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500167 br.cs = seg;
168 br.ip = offset;
169 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500170}
171
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400172// Find and run any "option roms" found in the given address range.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500173static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500174rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500175{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500176 u8 *p = (u8*)start;
177 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500178 u8 *rom = p;
179 if (*(u16*)rom != 0xaa55)
180 continue;
181 u32 len = rom[2] * 512;
182 if (checksum(rom, len) != 0)
183 continue;
184 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400185 dprintf(1, "Running option rom at %p\n", rom+3);
Kevin O'Connor070231b2008-03-22 20:44:37 -0400186 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500187
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400188 if (GET_BDA(ebda_seg) != SEG_EBDA)
189 BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
190 , rom, SEG_EBDA, GET_BDA(ebda_seg));
191
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500192 // Look at the ROM's PnP Expansion header. Properly, we're supposed
193 // to init all the ROMs and then go back and build an IPL table of
194 // all the bootable devices, but we can get away with one pass.
195 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
196 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
197 continue;
198 // 0x1A is also the offset into the expansion header of...
199 // the Bootstrap Entry Vector, or zero if there is none.
200 u16 entry = *(u16*)&rom[0x1a+0x1a];
201 if (!entry)
202 continue;
203 // Found a device that thinks it can boot the system. Record
204 // its BEV and product name string.
205
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400206 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500207 continue;
208
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400209 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500210 ip->type = IPL_TYPE_BEV;
Kevin O'Connor070231b2008-03-22 20:44:37 -0400211 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500212
213 u16 desc = *(u16*)&rom[0x1a+0x10];
214 if (desc)
Kevin O'Connore0113c92008-04-05 15:51:12 -0400215 ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500216
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400217 ebda->ipl.count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500218 }
219}
220
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400221// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500222static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500223post()
224{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500225 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500226 init_ebda();
227
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400228 timer_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500229 kbd_setup();
230 lpt_setup();
231 serial_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500232 pic_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500233
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400234 ram_probe();
235
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400236 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500237 rom_scan(0xc0000, 0xc7800);
238
239 printf("BIOS - begin\n\n");
240
Kevin O'Connora4d35762008-03-08 15:43:03 -0500241 rombios32_init();
242
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400243 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400244 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500245
246 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500247
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400248 dprintf(1, "Scan for option roms\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500249 rom_scan(0xc8000, 0xe0000);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500250}
251
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400252// Clear .bss section for C code.
253static void
254clear_bss()
255{
256 dprintf(3, "clearing .bss section\n");
257 extern char __bss_start[], __bss_end[];
258 memset(__bss_start, 0, __bss_end - __bss_start);
259}
260
261// Reset DMA controller
Kevin O'Connor95576e72008-03-01 09:57:51 -0500262static void
263init_dma()
264{
265 // first reset the DMA controllers
266 outb(0, PORT_DMA1_MASTER_CLEAR);
267 outb(0, PORT_DMA2_MASTER_CLEAR);
268
269 // then initialize the DMA controllers
270 outb(0xc0, PORT_DMA2_MODE_REG);
271 outb(0x00, PORT_DMA2_MASK_REG);
272}
273
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400274// Check if the machine was setup with a special restart vector.
Kevin O'Connor95576e72008-03-01 09:57:51 -0500275static void
276check_restart_status()
277{
278 // Get and then clear CMOS shutdown status.
279 u8 status = inb_cmos(CMOS_RESET_CODE);
280 outb_cmos(0, CMOS_RESET_CODE);
281
282 if (status == 0x00 || status == 0x09 || status >= 0x0d)
283 // Normal post
284 return;
285
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500286 if (status != 0x05) {
287 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
288 return;
289 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500290
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500291 // XXX - this is supposed to jump without changing any memory -
292 // but the stack has been altered by the time the code gets here.
293 eoi_both_pics();
294 struct bregs br;
295 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400296 br.cs = GET_BDA(jump_cs_ip) >> 16;
297 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500298 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500299}
300
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400301// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500302void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500303_start()
304{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500305 init_dma();
306 check_restart_status();
307
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400308 dprintf(1, "Start bios\n");
309
310 // Setup for .bss and .data sections
311 clear_bss();
312 make_bios_writable();
313
314 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500315 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400316
317 // Present the user with a bootup menu.
318 interactive_bootmenu();
319
320 // Prep for boot process.
321 make_bios_readonly();
322 clear_bss();
323
324 // Invoke int 19 to start boot process.
325 dprintf(3, "Jump to int19\n");
326 struct bregs br;
327 memset(&br, 0, sizeof(br));
328 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500329}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400330
331// Externally visible 32bit entry point.
332asm(
333 ".global post32\n"
334 "post32:\n"
335 "cli\n"
336 "cld\n"
337 "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
338 "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
339 "movl $" __stringify(CONFIG_STACK_OFFSET) ", %esp\n"
Kevin O'Connor59fead62008-05-10 15:49:20 -0400340 "ljmp $0x10, $_start\n"
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400341 );