blob: 766e8984e765634a99989fbbd5cec74c914586a2 [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'Connorc7812932008-06-08 23:08:12 -0400103 // Mark known areas as reserved.
104 add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
105 add_e820((u32)MAKE_FARPTR(SEG_BIOS, 0), 0x10000, E820_RESERVED);
106
107 dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400108}
109
110static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500111init_boot_vectors()
112{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400113 dprintf(3, "init boot device ordering\n");
114
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500115 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400116 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500117 ip->type = IPL_TYPE_FLOPPY;
118 ip++;
119
120 // First HDD
121 ip->type = IPL_TYPE_HARDDISK;
122 ip++;
123
124 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500125 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500126 ip->type = IPL_TYPE_CDROM;
127 ip++;
128 }
129
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400130 ebda->ipl.count = ip - ebda->ipl.table;
131 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400132 if (CONFIG_COREBOOT) {
133 // XXX - hardcode defaults for coreboot.
134 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400135 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400136 } else {
137 // On emulators, get boot order from nvram.
138 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
139 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
140 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
141 ebda->ipl.checkfloppysig = 1;
142 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500143}
144
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400145// Execute a given option rom.
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500146static void
147callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500148{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500149 struct bregs br;
150 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500151 br.es = SEG_BIOS;
152 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500153 br.cs = seg;
154 br.ip = offset;
155 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500156}
157
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400158// Find and run any "option roms" found in the given address range.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500159static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500160rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500161{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500162 u8 *p = (u8*)start;
163 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500164 u8 *rom = p;
165 if (*(u16*)rom != 0xaa55)
166 continue;
167 u32 len = rom[2] * 512;
168 if (checksum(rom, len) != 0)
169 continue;
170 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400171 dprintf(1, "Running option rom at %p\n", rom+3);
Kevin O'Connor070231b2008-03-22 20:44:37 -0400172 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500173
Kevin O'Connorc1adedc2008-06-07 10:24:48 -0400174 if (GET_BDA(ebda_seg) != SEG_EBDA)
175 BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
176 , rom, SEG_EBDA, GET_BDA(ebda_seg));
177
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500178 // Look at the ROM's PnP Expansion header. Properly, we're supposed
179 // to init all the ROMs and then go back and build an IPL table of
180 // all the bootable devices, but we can get away with one pass.
181 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
182 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
183 continue;
184 // 0x1A is also the offset into the expansion header of...
185 // the Bootstrap Entry Vector, or zero if there is none.
186 u16 entry = *(u16*)&rom[0x1a+0x1a];
187 if (!entry)
188 continue;
189 // Found a device that thinks it can boot the system. Record
190 // its BEV and product name string.
191
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400192 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500193 continue;
194
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400195 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500196 ip->type = IPL_TYPE_BEV;
Kevin O'Connor070231b2008-03-22 20:44:37 -0400197 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500198
199 u16 desc = *(u16*)&rom[0x1a+0x10];
200 if (desc)
Kevin O'Connore0113c92008-04-05 15:51:12 -0400201 ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500202
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400203 ebda->ipl.count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500204 }
205}
206
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400207// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500208static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500209post()
210{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500211 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500212 init_ebda();
213
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400214 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400215 timer_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500216 kbd_setup();
217 lpt_setup();
218 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400219 mouse_setup();
220 mathcp_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500221
Kevin O'Connorc7812932008-06-08 23:08:12 -0400222 memmap_setup();
223
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400224 ram_probe();
225
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400226 dprintf(1, "Scan for VGA option rom\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500227 rom_scan(0xc0000, 0xc7800);
228
229 printf("BIOS - begin\n\n");
230
Kevin O'Connora4d35762008-03-08 15:43:03 -0500231 rombios32_init();
232
Kevin O'Connorc7812932008-06-08 23:08:12 -0400233 memmap_finalize();
234
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400235 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400236 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500237
238 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500239
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400240 dprintf(1, "Scan for option roms\n");
Kevin O'Connora2e73802008-02-27 10:27:00 -0500241 rom_scan(0xc8000, 0xe0000);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500242}
243
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400244// Clear .bss section for C code.
245static void
246clear_bss()
247{
248 dprintf(3, "clearing .bss section\n");
249 extern char __bss_start[], __bss_end[];
250 memset(__bss_start, 0, __bss_end - __bss_start);
251}
252
253// Reset DMA controller
Kevin O'Connor95576e72008-03-01 09:57:51 -0500254static void
255init_dma()
256{
257 // first reset the DMA controllers
258 outb(0, PORT_DMA1_MASTER_CLEAR);
259 outb(0, PORT_DMA2_MASTER_CLEAR);
260
261 // then initialize the DMA controllers
262 outb(0xc0, PORT_DMA2_MODE_REG);
263 outb(0x00, PORT_DMA2_MASK_REG);
264}
265
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400266// Check if the machine was setup with a special restart vector.
Kevin O'Connor95576e72008-03-01 09:57:51 -0500267static void
268check_restart_status()
269{
270 // Get and then clear CMOS shutdown status.
271 u8 status = inb_cmos(CMOS_RESET_CODE);
272 outb_cmos(0, CMOS_RESET_CODE);
273
274 if (status == 0x00 || status == 0x09 || status >= 0x0d)
275 // Normal post
276 return;
277
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500278 if (status != 0x05) {
279 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
280 return;
281 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500282
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500283 // XXX - this is supposed to jump without changing any memory -
284 // but the stack has been altered by the time the code gets here.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400285 eoi_pic2();
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500286 struct bregs br;
287 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400288 br.cs = GET_BDA(jump_cs_ip) >> 16;
289 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500290 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500291}
292
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400293// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500294void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500295_start()
296{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500297 init_dma();
298 check_restart_status();
299
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400300 dprintf(1, "Start bios\n");
301
302 // Setup for .bss and .data sections
303 clear_bss();
304 make_bios_writable();
305
306 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500307 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400308
309 // Present the user with a bootup menu.
310 interactive_bootmenu();
311
312 // Prep for boot process.
313 make_bios_readonly();
314 clear_bss();
315
316 // Invoke int 19 to start boot process.
317 dprintf(3, "Jump to int19\n");
318 struct bregs br;
319 memset(&br, 0, sizeof(br));
320 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500321}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400322
323// Externally visible 32bit entry point.
324asm(
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"
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400331 "movl $" __stringify(BUILD_STACK_ADDR) ", %esp\n"
Kevin O'Connor59fead62008-05-10 15:49:20 -0400332 "ljmp $0x10, $_start\n"
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400333 );