blob: ca1d2232b874f2dc07904e699112134336ae5e94 [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'Connorc7812932008-06-08 23:08:12 -040017#include "memmap.h" // add_e820
Kevin O'Connorf54c1502008-06-14 15:56:16 -040018#include "pic.h" // pic_setup
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050019
Kevin O'Connor438f6352008-03-30 21:46:53 -040020#define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
21#define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050022
23static void
24init_bda()
25{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040026 dprintf(3, "init bda\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050027 memset(bda, 0, sizeof(*bda));
28
Kevin O'Connorf54c1502008-06-14 15:56:16 -040029 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
30
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050031 int i;
32 for (i=0; i<256; i++) {
Kevin O'Connore9061492008-03-11 21:54:18 -040033 SET_BDA(ivecs[i].seg, SEG_BIOS);
34 SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050035 }
36
Kevin O'Connorf54c1502008-06-14 15:56:16 -040037 SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
38 SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
39 SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
40 SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
41 SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
42 SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
43 SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
44 SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
45 SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
46 SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
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[0x1a].offset, OFFSET_entry_1a);
51 SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
52 SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
53 SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
54 SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
55 SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
56 SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050057
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050058 // set vector 0x79 to zero
59 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040060 SET_BDA(ivecs[0x79].seg, 0);
61 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050062
Kevin O'Connor3bbcc142008-04-13 17:07:33 -040063 SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050064}
65
66static void
67init_ebda()
68{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050069 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050070 ebda->size = EBDA_SIZE;
Kevin O'Connor438f6352008-03-30 21:46:53 -040071 SET_BDA(ebda_seg, SEG_EBDA);
72 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040073 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040074 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
Kevin O'Connor438f6352008-03-30 21:46:53 -040075 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040076 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040077 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050078}
79
80static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -040081ram_probe(void)
82{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040083 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040084 if (CONFIG_COREBOOT) {
Kevin O'Connorc7812932008-06-08 23:08:12 -040085 coreboot_fill_map();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040086 } else {
87 // On emulators, get memory size from nvram.
Kevin O'Connorc7812932008-06-08 23:08:12 -040088 u32 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
89 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040090 if (rs)
91 rs += 16 * 1024 * 1024;
92 else
93 rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
94 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
95 + 1 * 1024 * 1024);
Kevin O'Connorc7812932008-06-08 23:08:12 -040096 SET_EBDA(ram_size, rs);
97 add_e820(0, rs, E820_RAM);
98
99 /* reserve 256KB BIOS area at the end of 4 GB */
100 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400101 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400102
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400103 // Don't declare any memory between 0xa0000 and 0x100000
104 add_e820(0xa0000, 0x50000, E820_HOLE);
105
Kevin O'Connorc7812932008-06-08 23:08:12 -0400106 // Mark known areas as reserved.
107 add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
108 add_e820((u32)MAKE_FARPTR(SEG_BIOS, 0), 0x10000, E820_RESERVED);
109
110 dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400111}
112
113static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500114init_boot_vectors()
115{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400116 dprintf(3, "init boot device ordering\n");
117
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500118 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400119 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500120 ip->type = IPL_TYPE_FLOPPY;
121 ip++;
122
123 // First HDD
124 ip->type = IPL_TYPE_HARDDISK;
125 ip++;
126
127 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500128 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500129 ip->type = IPL_TYPE_CDROM;
130 ip++;
131 }
132
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400133 ebda->ipl.count = ip - ebda->ipl.table;
134 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400135 if (CONFIG_COREBOOT) {
136 // XXX - hardcode defaults for coreboot.
137 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400138 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400139 } else {
140 // On emulators, get boot order from nvram.
141 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
142 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
143 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
144 ebda->ipl.checkfloppysig = 1;
145 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500146}
147
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400148// Execute a given option rom.
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500149static void
150callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500151{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500152 struct bregs br;
153 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500154 br.es = SEG_BIOS;
155 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500156 br.cs = seg;
157 br.ip = offset;
158 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500159}
160
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400161// Find and run any "option roms" found in the given address range.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500162static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500163rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500164{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500165 u8 *p = (u8*)start;
166 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500167 u8 *rom = p;
168 if (*(u16*)rom != 0xaa55)
169 continue;
170 u32 len = rom[2] * 512;
171 if (checksum(rom, len) != 0)
172 continue;
173 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400174 dprintf(1, "Running option rom at %p\n", rom+3);
Kevin O'Connor070231b2008-03-22 20:44:37 -0400175 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500176
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400177 if (GET_BDA(ebda_seg) != SEG_EBDA)
178 BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
179 , rom, SEG_EBDA, GET_BDA(ebda_seg));
180
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500181 // Look at the ROM's PnP Expansion header. Properly, we're supposed
182 // to init all the ROMs and then go back and build an IPL table of
183 // all the bootable devices, but we can get away with one pass.
184 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
185 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
186 continue;
187 // 0x1A is also the offset into the expansion header of...
188 // the Bootstrap Entry Vector, or zero if there is none.
189 u16 entry = *(u16*)&rom[0x1a+0x1a];
190 if (!entry)
191 continue;
192 // Found a device that thinks it can boot the system. Record
193 // its BEV and product name string.
194
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400195 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500196 continue;
197
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400198 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500199 ip->type = IPL_TYPE_BEV;
Kevin O'Connor070231b2008-03-22 20:44:37 -0400200 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500201
202 u16 desc = *(u16*)&rom[0x1a+0x10];
203 if (desc)
Kevin O'Connore0113c92008-04-05 15:51:12 -0400204 ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500205
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400206 ebda->ipl.count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500207 }
208}
209
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400210// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500211static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500212post()
213{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500214 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500215 init_ebda();
216
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400217 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400218 timer_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500219 kbd_setup();
220 lpt_setup();
221 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400222 mouse_setup();
223 mathcp_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500224
Kevin O'Connorc7812932008-06-08 23:08:12 -0400225 memmap_setup();
226
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400227 ram_probe();
228
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400229 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500230 rom_scan(0xc0000, 0xc7800);
231
232 printf("BIOS - begin\n\n");
233
Kevin O'Connora4d35762008-03-08 15:43:03 -0500234 rombios32_init();
235
Kevin O'Connorc7812932008-06-08 23:08:12 -0400236 memmap_finalize();
237
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400238 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400239 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500240
241 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500242
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400243 dprintf(1, "Scan for option roms\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500244 rom_scan(0xc8000, 0xe0000);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500245}
246
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400247// Clear .bss section for C code.
248static void
249clear_bss()
250{
251 dprintf(3, "clearing .bss section\n");
252 extern char __bss_start[], __bss_end[];
253 memset(__bss_start, 0, __bss_end - __bss_start);
254}
255
256// Reset DMA controller
Kevin O'Connor95576e72008-03-01 09:57:51 -0500257static void
258init_dma()
259{
260 // first reset the DMA controllers
261 outb(0, PORT_DMA1_MASTER_CLEAR);
262 outb(0, PORT_DMA2_MASTER_CLEAR);
263
264 // then initialize the DMA controllers
265 outb(0xc0, PORT_DMA2_MODE_REG);
266 outb(0x00, PORT_DMA2_MASK_REG);
267}
268
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400269// Check if the machine was setup with a special restart vector.
Kevin O'Connor95576e72008-03-01 09:57:51 -0500270static void
271check_restart_status()
272{
273 // Get and then clear CMOS shutdown status.
274 u8 status = inb_cmos(CMOS_RESET_CODE);
275 outb_cmos(0, CMOS_RESET_CODE);
276
277 if (status == 0x00 || status == 0x09 || status >= 0x0d)
278 // Normal post
279 return;
280
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500281 if (status != 0x05) {
282 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
283 return;
284 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500285
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500286 // XXX - this is supposed to jump without changing any memory -
287 // but the stack has been altered by the time the code gets here.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400288 eoi_pic2();
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500289 struct bregs br;
290 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400291 br.cs = GET_BDA(jump_cs_ip) >> 16;
292 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500293 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500294}
295
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400296// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500297void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500298_start()
299{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500300 init_dma();
301 check_restart_status();
302
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400303 dprintf(1, "Start bios\n");
304
305 // Setup for .bss and .data sections
306 clear_bss();
307 make_bios_writable();
308
309 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500310 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400311
312 // Present the user with a bootup menu.
313 interactive_bootmenu();
314
315 // Prep for boot process.
316 make_bios_readonly();
317 clear_bss();
318
319 // Invoke int 19 to start boot process.
320 dprintf(3, "Jump to int19\n");
321 struct bregs br;
322 memset(&br, 0, sizeof(br));
323 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500324}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400325
326// Externally visible 32bit entry point.
327asm(
328 ".global post32\n"
329 "post32:\n"
330 "cli\n"
331 "cld\n"
332 "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
333 "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400334 "movl $" __stringify(BUILD_STACK_ADDR) ", %esp\n"
Kevin O'Connor59fead62008-05-10 15:49:20 -0400335 "ljmp $0x10, $_start\n"
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400336 );