blob: 6ece8a5febc9c35236ff37f37099c6857ad8598e [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 "disk.h" // floppy_drive_setup
Kevin O'Connorc7812932008-06-08 23:08:12 -040016#include "memmap.h" // add_e820
Kevin O'Connorf54c1502008-06-14 15:56:16 -040017#include "pic.h" // pic_setup
Kevin O'Connor0525d292008-07-04 06:18:30 -040018#include "pci.h" // create_pirtable
19#include "acpi.h" // acpi_bios_init
Kevin O'Connor9521e262008-07-04 13:04:29 -040020#include "bregs.h" // struct bregs
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050021
Kevin O'Connor438f6352008-03-30 21:46:53 -040022#define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
23#define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050024
25static void
Kevin O'Connore3677b12008-07-04 15:29:23 -040026set_irq(int vector, u32 loc)
27{
28 SET_BDA(ivecs[vector].seg, SEG_BIOS);
29 SET_BDA(ivecs[vector].offset, loc - BUILD_BIOS_ADDR);
30}
31
32static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050033init_bda()
34{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040035 dprintf(3, "init bda\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050036 memset(bda, 0, sizeof(*bda));
37
Kevin O'Connorf54c1502008-06-14 15:56:16 -040038 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
39
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050040 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040041 for (i=0; i<256; i++)
42 set_irq(i, OFFSET_dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050043
Kevin O'Connore3677b12008-07-04 15:29:23 -040044 set_irq(0x08, OFFSET_entry_08);
45 set_irq(0x09, OFFSET_entry_09);
46 //set_irq(0x0a, OFFSET_entry_hwirq);
47 //set_irq(0x0b, OFFSET_entry_hwirq);
48 //set_irq(0x0c, OFFSET_entry_hwirq);
49 //set_irq(0x0d, OFFSET_entry_hwirq);
50 set_irq(0x0e, OFFSET_entry_0e);
51 //set_irq(0x0f, OFFSET_entry_hwirq);
52 set_irq(0x10, OFFSET_entry_10);
53 set_irq(0x11, OFFSET_entry_11);
54 set_irq(0x12, OFFSET_entry_12);
55 set_irq(0x13, OFFSET_entry_13);
56 set_irq(0x14, OFFSET_entry_14);
57 set_irq(0x15, OFFSET_entry_15);
58 set_irq(0x16, OFFSET_entry_16);
59 set_irq(0x17, OFFSET_entry_17);
60 set_irq(0x18, OFFSET_entry_18);
61 set_irq(0x19, OFFSET_entry_19);
62 set_irq(0x1a, OFFSET_entry_1a);
63 set_irq(0x1c, OFFSET_entry_1c);
64 set_irq(0x40, OFFSET_entry_40);
65 set_irq(0x70, OFFSET_entry_70);
66 //set_irq(0x71, OFFSET_entry_hwirq);
67 //set_irq(0x72, OFFSET_entry_hwirq);
68 //set_irq(0x73, OFFSET_entry_hwirq);
69 set_irq(0x74, OFFSET_entry_74);
70 set_irq(0x75, OFFSET_entry_75);
71 set_irq(0x76, OFFSET_entry_76);
72 //set_irq(0x77, OFFSET_entry_hwirq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050073
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050074 // set vector 0x79 to zero
75 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040076 SET_BDA(ivecs[0x79].seg, 0);
77 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050078
Kevin O'Connore3677b12008-07-04 15:29:23 -040079 set_irq(0x1E, OFFSET_diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050080}
81
82static void
83init_ebda()
84{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050085 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050086 ebda->size = EBDA_SIZE;
Kevin O'Connor438f6352008-03-30 21:46:53 -040087 SET_BDA(ebda_seg, SEG_EBDA);
88 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040089 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040090 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
Kevin O'Connor438f6352008-03-30 21:46:53 -040091 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040092 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040093 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050094}
95
96static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -040097ram_probe(void)
98{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040099 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400100 if (CONFIG_COREBOOT) {
Kevin O'Connorc7812932008-06-08 23:08:12 -0400101 coreboot_fill_map();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400102 } else {
103 // On emulators, get memory size from nvram.
Kevin O'Connorc7812932008-06-08 23:08:12 -0400104 u32 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
105 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400106 if (rs)
107 rs += 16 * 1024 * 1024;
108 else
109 rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
110 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
111 + 1 * 1024 * 1024);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400112 SET_EBDA(ram_size, rs);
113 add_e820(0, rs, E820_RAM);
114
115 /* reserve 256KB BIOS area at the end of 4 GB */
116 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400117 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400118
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400119 // Don't declare any memory between 0xa0000 and 0x100000
120 add_e820(0xa0000, 0x50000, E820_HOLE);
121
Kevin O'Connorc7812932008-06-08 23:08:12 -0400122 // Mark known areas as reserved.
123 add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
Kevin O'Connore3677b12008-07-04 15:29:23 -0400124 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400125
126 dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400127}
128
129static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400130init_bios_tables(void)
131{
132 if (CONFIG_COREBOOT)
133 // XXX - not supported on coreboot yet.
134 return;
135
136 smm_init();
137
138 create_pirtable();
139
140 mptable_init();
141
142 smbios_init();
143
144 acpi_bios_init();
145}
146
147static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500148init_boot_vectors()
149{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400150 dprintf(3, "init boot device ordering\n");
151
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500152 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400153 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500154 ip->type = IPL_TYPE_FLOPPY;
155 ip++;
156
157 // First HDD
158 ip->type = IPL_TYPE_HARDDISK;
159 ip++;
160
161 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500162 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500163 ip->type = IPL_TYPE_CDROM;
164 ip++;
165 }
166
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400167 ebda->ipl.count = ip - ebda->ipl.table;
168 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400169 if (CONFIG_COREBOOT) {
170 // XXX - hardcode defaults for coreboot.
171 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400172 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400173 } else {
174 // On emulators, get boot order from nvram.
175 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
176 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
177 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
178 ebda->ipl.checkfloppysig = 1;
179 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500180}
181
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400182// Execute a given option rom.
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500183static void
184callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500185{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500186 struct bregs br;
187 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500188 br.es = SEG_BIOS;
Kevin O'Connore3677b12008-07-04 15:29:23 -0400189 // starts 1 past for alignment
190 br.di = OFFSET_pnp_string - BUILD_BIOS_ADDR + 1;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500191 br.cs = seg;
192 br.ip = offset;
193 call16(&br);
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400194
195 debug_serial_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500196}
197
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400198// Find and run any "option roms" found in the given address range.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500199static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500200rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500201{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500202 u8 *p = (u8*)start;
203 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500204 u8 *rom = p;
205 if (*(u16*)rom != 0xaa55)
206 continue;
207 u32 len = rom[2] * 512;
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400208 u8 sum = checksum(rom, len);
209 if (sum != 0) {
210 dprintf(1, "Found option rom with bad checksum:"
211 " loc=%p len=%d sum=%x\n"
212 , rom, len, sum);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500213 continue;
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400214 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500215 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400216 dprintf(1, "Running option rom at %p\n", rom+3);
Kevin O'Connor070231b2008-03-22 20:44:37 -0400217 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500218
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400219 if (GET_BDA(ebda_seg) != SEG_EBDA)
220 BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
221 , rom, SEG_EBDA, GET_BDA(ebda_seg));
222
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500223 // Look at the ROM's PnP Expansion header. Properly, we're supposed
224 // to init all the ROMs and then go back and build an IPL table of
225 // all the bootable devices, but we can get away with one pass.
226 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
227 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
228 continue;
229 // 0x1A is also the offset into the expansion header of...
230 // the Bootstrap Entry Vector, or zero if there is none.
231 u16 entry = *(u16*)&rom[0x1a+0x1a];
232 if (!entry)
233 continue;
234 // Found a device that thinks it can boot the system. Record
235 // its BEV and product name string.
236
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400237 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500238 continue;
239
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400240 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500241 ip->type = IPL_TYPE_BEV;
Kevin O'Connor070231b2008-03-22 20:44:37 -0400242 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500243
244 u16 desc = *(u16*)&rom[0x1a+0x10];
245 if (desc)
Kevin O'Connore0113c92008-04-05 15:51:12 -0400246 ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500247
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400248 ebda->ipl.count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500249 }
250}
251
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400252// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500253static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500254post()
255{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500256 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500257 init_ebda();
258
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400259 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400260 timer_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500261 kbd_setup();
262 lpt_setup();
263 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400264 mouse_setup();
265 mathcp_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500266
Kevin O'Connorc7812932008-06-08 23:08:12 -0400267 memmap_setup();
268
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400269 ram_probe();
270
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400271 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500272 rom_scan(0xc0000, 0xc7800);
273
274 printf("BIOS - begin\n\n");
275
Kevin O'Connor0525d292008-07-04 06:18:30 -0400276 pci_bios_setup();
277
278 init_bios_tables();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500279
Kevin O'Connorc7812932008-06-08 23:08:12 -0400280 memmap_finalize();
281
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400282 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400283 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500284
285 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500286
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400287 dprintf(1, "Scan for option roms\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500288 rom_scan(0xc8000, 0xe0000);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500289}
290
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400291// Clear .bss section for C code.
292static void
293clear_bss()
294{
295 dprintf(3, "clearing .bss section\n");
296 extern char __bss_start[], __bss_end[];
297 memset(__bss_start, 0, __bss_end - __bss_start);
298}
299
300// Reset DMA controller
Kevin O'Connor95576e72008-03-01 09:57:51 -0500301static void
302init_dma()
303{
304 // first reset the DMA controllers
305 outb(0, PORT_DMA1_MASTER_CLEAR);
306 outb(0, PORT_DMA2_MASTER_CLEAR);
307
308 // then initialize the DMA controllers
309 outb(0xc0, PORT_DMA2_MODE_REG);
310 outb(0x00, PORT_DMA2_MASK_REG);
311}
312
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400313// Check if the machine was setup with a special restart vector.
Kevin O'Connor95576e72008-03-01 09:57:51 -0500314static void
315check_restart_status()
316{
317 // Get and then clear CMOS shutdown status.
318 u8 status = inb_cmos(CMOS_RESET_CODE);
319 outb_cmos(0, CMOS_RESET_CODE);
320
321 if (status == 0x00 || status == 0x09 || status >= 0x0d)
322 // Normal post
323 return;
324
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500325 if (status != 0x05) {
326 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
327 return;
328 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500329
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500330 // XXX - this is supposed to jump without changing any memory -
331 // but the stack has been altered by the time the code gets here.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400332 eoi_pic2();
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500333 struct bregs br;
334 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400335 br.cs = GET_BDA(jump_cs_ip) >> 16;
336 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500337 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500338}
339
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400340// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500341void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500342_start()
343{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500344 init_dma();
345 check_restart_status();
346
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400347 debug_serial_setup();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400348 dprintf(1, "Start bios\n");
349
350 // Setup for .bss and .data sections
351 clear_bss();
352 make_bios_writable();
353
354 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500355 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400356
357 // Present the user with a bootup menu.
358 interactive_bootmenu();
359
Kevin O'Connore90f2642008-06-28 12:18:39 -0400360 // Setup bios checksum.
Kevin O'Connore3677b12008-07-04 15:29:23 -0400361 *(u8*)OFFSET_bios_checksum = -checksum((u8*)BUILD_BIOS_ADDR
362 , BUILD_BIOS_SIZE - 1);
Kevin O'Connore90f2642008-06-28 12:18:39 -0400363
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400364 // Prep for boot process.
365 make_bios_readonly();
366 clear_bss();
367
368 // Invoke int 19 to start boot process.
369 dprintf(3, "Jump to int19\n");
370 struct bregs br;
371 memset(&br, 0, sizeof(br));
372 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500373}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400374
375// Externally visible 32bit entry point.
376asm(
377 ".global post32\n"
378 "post32:\n"
379 "cli\n"
380 "cld\n"
Kevin O'Connore3677b12008-07-04 15:29:23 -0400381 "lidtl " __stringify(OFFSET_pmode_IDT_info) "\n"
382 "lgdtl " __stringify(OFFSET_rombios32_gdt_48) "\n"
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400383 "movl $" __stringify(BUILD_STACK_ADDR) ", %esp\n"
Kevin O'Connor59fead62008-05-10 15:49:20 -0400384 "ljmp $0x10, $_start\n"
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400385 );