blob: 4593b69c1e90b58dc08ec6c3c522c026d95666d7 [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'Connorf076a3e2008-02-25 22:25:15 -050018
Kevin O'Connor438f6352008-03-30 21:46:53 -040019#define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
20#define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050021
22static void
23init_bda()
24{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040025 dprintf(3, "init bda\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050026 memset(bda, 0, sizeof(*bda));
27
28 int i;
29 for (i=0; i<256; i++) {
Kevin O'Connore9061492008-03-11 21:54:18 -040030 SET_BDA(ivecs[i].seg, SEG_BIOS);
31 SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050032 }
33
Kevin O'Connore9061492008-03-11 21:54:18 -040034 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
Kevin O'Connor9bd83912008-03-08 12:42:36 -050035
Kevin O'Connor88c00ee2008-05-18 00:14:13 -040036 // Set BDA Equipment Word - 0x06 = FPU enable, mouse installed
37 SET_BDA(equipment_list_flags, 0x06);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050038
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050039 // set vector 0x79 to zero
40 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040041 SET_BDA(ivecs[0x79].seg, 0);
42 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050043
Kevin O'Connore9061492008-03-11 21:54:18 -040044 SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
45 SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
46 SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
47 SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
48 SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
49 SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
50 SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
51 SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
52 SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
53 SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
54 SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
55 SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
56 SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
57 SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
58 SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
59 SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
60 SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
61 SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
62 SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
63 SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
Kevin O'Connor3bbcc142008-04-13 17:07:33 -040064
65 SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050066}
67
68static void
69init_ebda()
70{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050071 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050072 ebda->size = EBDA_SIZE;
Kevin O'Connor438f6352008-03-30 21:46:53 -040073 SET_BDA(ebda_seg, SEG_EBDA);
74 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040075 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040076 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
Kevin O'Connor438f6352008-03-30 21:46:53 -040077 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040078 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040079 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050080}
81
82static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -040083ram_probe(void)
84{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040085 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040086 if (CONFIG_COREBOOT) {
Kevin O'Connorc7812932008-06-08 23:08:12 -040087 coreboot_fill_map();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040088 } else {
89 // On emulators, get memory size from nvram.
Kevin O'Connorc7812932008-06-08 23:08:12 -040090 u32 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
91 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040092 if (rs)
93 rs += 16 * 1024 * 1024;
94 else
95 rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
96 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
97 + 1 * 1024 * 1024);
Kevin O'Connorc7812932008-06-08 23:08:12 -040098 SET_EBDA(ram_size, rs);
99 add_e820(0, rs, E820_RAM);
100
101 /* reserve 256KB BIOS area at the end of 4 GB */
102 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400103 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400104
Kevin O'Connorc7812932008-06-08 23:08:12 -0400105 // Mark known areas as reserved.
106 add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
107 add_e820((u32)MAKE_FARPTR(SEG_BIOS, 0), 0x10000, E820_RESERVED);
108
109 dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400110}
111
112static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500113pic_setup()
114{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400115 dprintf(3, "init pic\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500116 outb(0x11, PORT_PIC1);
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500117 outb(0x11, PORT_PIC2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500118 outb(0x08, PORT_PIC1_DATA);
119 outb(0x70, PORT_PIC2_DATA);
120 outb(0x04, PORT_PIC1_DATA);
121 outb(0x02, PORT_PIC2_DATA);
122 outb(0x01, PORT_PIC1_DATA);
123 outb(0x01, PORT_PIC2_DATA);
124 outb(0xb8, PORT_PIC1_DATA);
125 if (CONFIG_PS2_MOUSE)
126 outb(0x8f, PORT_PIC2_DATA);
127 else
128 outb(0x9f, PORT_PIC2_DATA);
129}
130
131static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500132init_boot_vectors()
133{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400134 dprintf(3, "init boot device ordering\n");
135
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500136 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400137 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500138 ip->type = IPL_TYPE_FLOPPY;
139 ip++;
140
141 // First HDD
142 ip->type = IPL_TYPE_HARDDISK;
143 ip++;
144
145 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500146 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500147 ip->type = IPL_TYPE_CDROM;
148 ip++;
149 }
150
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400151 ebda->ipl.count = ip - ebda->ipl.table;
152 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400153 if (CONFIG_COREBOOT) {
154 // XXX - hardcode defaults for coreboot.
155 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400156 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400157 } else {
158 // On emulators, get boot order from nvram.
159 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
160 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
161 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
162 ebda->ipl.checkfloppysig = 1;
163 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500164}
165
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400166// Execute a given option rom.
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500167static void
168callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500169{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500170 struct bregs br;
171 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500172 br.es = SEG_BIOS;
173 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500174 br.cs = seg;
175 br.ip = offset;
176 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500177}
178
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400179// Find and run any "option roms" found in the given address range.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500180static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500181rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500182{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500183 u8 *p = (u8*)start;
184 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500185 u8 *rom = p;
186 if (*(u16*)rom != 0xaa55)
187 continue;
188 u32 len = rom[2] * 512;
189 if (checksum(rom, len) != 0)
190 continue;
191 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400192 dprintf(1, "Running option rom at %p\n", rom+3);
Kevin O'Connor070231b2008-03-22 20:44:37 -0400193 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500194
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400195 if (GET_BDA(ebda_seg) != SEG_EBDA)
196 BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
197 , rom, SEG_EBDA, GET_BDA(ebda_seg));
198
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500199 // Look at the ROM's PnP Expansion header. Properly, we're supposed
200 // to init all the ROMs and then go back and build an IPL table of
201 // all the bootable devices, but we can get away with one pass.
202 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
203 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
204 continue;
205 // 0x1A is also the offset into the expansion header of...
206 // the Bootstrap Entry Vector, or zero if there is none.
207 u16 entry = *(u16*)&rom[0x1a+0x1a];
208 if (!entry)
209 continue;
210 // Found a device that thinks it can boot the system. Record
211 // its BEV and product name string.
212
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400213 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500214 continue;
215
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400216 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500217 ip->type = IPL_TYPE_BEV;
Kevin O'Connor070231b2008-03-22 20:44:37 -0400218 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500219
220 u16 desc = *(u16*)&rom[0x1a+0x10];
221 if (desc)
Kevin O'Connore0113c92008-04-05 15:51:12 -0400222 ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500223
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400224 ebda->ipl.count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500225 }
226}
227
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400228// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500229static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500230post()
231{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500232 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500233 init_ebda();
234
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400235 timer_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500236 kbd_setup();
237 lpt_setup();
238 serial_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500239 pic_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500240
Kevin O'Connorc7812932008-06-08 23:08:12 -0400241 memmap_setup();
242
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400243 ram_probe();
244
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400245 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500246 rom_scan(0xc0000, 0xc7800);
247
248 printf("BIOS - begin\n\n");
249
Kevin O'Connora4d35762008-03-08 15:43:03 -0500250 rombios32_init();
251
Kevin O'Connorc7812932008-06-08 23:08:12 -0400252 memmap_finalize();
253
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400254 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400255 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500256
257 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500258
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400259 dprintf(1, "Scan for option roms\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500260 rom_scan(0xc8000, 0xe0000);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500261}
262
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400263// Clear .bss section for C code.
264static void
265clear_bss()
266{
267 dprintf(3, "clearing .bss section\n");
268 extern char __bss_start[], __bss_end[];
269 memset(__bss_start, 0, __bss_end - __bss_start);
270}
271
272// Reset DMA controller
Kevin O'Connor95576e72008-03-01 09:57:51 -0500273static void
274init_dma()
275{
276 // first reset the DMA controllers
277 outb(0, PORT_DMA1_MASTER_CLEAR);
278 outb(0, PORT_DMA2_MASTER_CLEAR);
279
280 // then initialize the DMA controllers
281 outb(0xc0, PORT_DMA2_MODE_REG);
282 outb(0x00, PORT_DMA2_MASK_REG);
283}
284
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400285// Check if the machine was setup with a special restart vector.
Kevin O'Connor95576e72008-03-01 09:57:51 -0500286static void
287check_restart_status()
288{
289 // Get and then clear CMOS shutdown status.
290 u8 status = inb_cmos(CMOS_RESET_CODE);
291 outb_cmos(0, CMOS_RESET_CODE);
292
293 if (status == 0x00 || status == 0x09 || status >= 0x0d)
294 // Normal post
295 return;
296
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500297 if (status != 0x05) {
298 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
299 return;
300 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500301
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500302 // XXX - this is supposed to jump without changing any memory -
303 // but the stack has been altered by the time the code gets here.
304 eoi_both_pics();
305 struct bregs br;
306 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400307 br.cs = GET_BDA(jump_cs_ip) >> 16;
308 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500309 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500310}
311
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400312// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500313void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500314_start()
315{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500316 init_dma();
317 check_restart_status();
318
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400319 dprintf(1, "Start bios\n");
320
321 // Setup for .bss and .data sections
322 clear_bss();
323 make_bios_writable();
324
325 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500326 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400327
328 // Present the user with a bootup menu.
329 interactive_bootmenu();
330
331 // Prep for boot process.
332 make_bios_readonly();
333 clear_bss();
334
335 // Invoke int 19 to start boot process.
336 dprintf(3, "Jump to int19\n");
337 struct bregs br;
338 memset(&br, 0, sizeof(br));
339 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500340}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400341
342// Externally visible 32bit entry point.
343asm(
344 ".global post32\n"
345 "post32:\n"
346 "cli\n"
347 "cld\n"
348 "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
349 "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
350 "movl $" __stringify(CONFIG_STACK_OFFSET) ", %esp\n"
Kevin O'Connor59fead62008-05-10 15:49:20 -0400351 "ljmp $0x10, $_start\n"
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400352 );