1 // 32bit code to Power On Self Test (POST) a machine.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
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
14 #include "ata.h" // hard_drive_setup
15 #include "disk.h" // floppy_drive_setup
16 #include "memmap.h" // add_e820
17 #include "pic.h" // pic_setup
18 #include "pci.h" // create_pirtable
19 #include "acpi.h" // acpi_bios_init
20 #include "bregs.h" // struct bregs
22 #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))
28 dprintf(3, "init bda\n");
29 memset(bda, 0, sizeof(*bda));
31 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
34 for (i=0; i<256; i++) {
35 SET_BDA(ivecs[i].seg, SEG_BIOS);
36 SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
39 SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
40 SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
41 //SET_BDA(ivecs[0x0a].offset, OFFSET_entry_hwirq);
42 //SET_BDA(ivecs[0x0b].offset, OFFSET_entry_hwirq);
43 //SET_BDA(ivecs[0x0c].offset, OFFSET_entry_hwirq);
44 //SET_BDA(ivecs[0x0d].offset, OFFSET_entry_hwirq);
45 SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
46 //SET_BDA(ivecs[0x0f].offset, OFFSET_entry_hwirq);
47 SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
48 SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
49 SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
50 SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
51 SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
52 SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
53 SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
54 SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
55 SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
56 SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
57 SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
58 SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
59 SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
60 SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
61 //SET_BDA(ivecs[0x71].offset, OFFSET_entry_hwirq);
62 //SET_BDA(ivecs[0x72].offset, OFFSET_entry_hwirq);
63 //SET_BDA(ivecs[0x73].offset, OFFSET_entry_hwirq);
64 SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
65 SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
66 SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
67 //SET_BDA(ivecs[0x77].offset, OFFSET_entry_hwirq);
69 // set vector 0x79 to zero
70 // this is used by 'gardian angel' protection system
71 SET_BDA(ivecs[0x79].seg, 0);
72 SET_BDA(ivecs[0x79].offset, 0);
74 SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
80 memset(ebda, 0, sizeof(*ebda));
81 ebda->size = EBDA_SIZE;
82 SET_BDA(ebda_seg, SEG_EBDA);
83 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
84 SET_BDA(ivecs[0x41].offset
85 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
86 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
87 SET_BDA(ivecs[0x41].offset
88 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
94 dprintf(3, "Find memory size\n");
95 if (CONFIG_COREBOOT) {
98 // On emulators, get memory size from nvram.
99 u32 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
100 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
102 rs += 16 * 1024 * 1024;
104 rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
105 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
107 SET_EBDA(ram_size, rs);
108 add_e820(0, rs, E820_RAM);
110 /* reserve 256KB BIOS area at the end of 4 GB */
111 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
114 // Don't declare any memory between 0xa0000 and 0x100000
115 add_e820(0xa0000, 0x50000, E820_HOLE);
117 // Mark known areas as reserved.
118 add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
119 add_e820((u32)MAKE_FARPTR(SEG_BIOS, 0), 0x10000, E820_RESERVED);
121 dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
125 init_bios_tables(void)
128 // XXX - not supported on coreboot yet.
145 dprintf(3, "init boot device ordering\n");
148 struct ipl_entry_s *ip = &ebda->ipl.table[0];
149 ip->type = IPL_TYPE_FLOPPY;
153 ip->type = IPL_TYPE_HARDDISK;
157 if (CONFIG_CDROM_BOOT) {
158 ip->type = IPL_TYPE_CDROM;
162 ebda->ipl.count = ip - ebda->ipl.table;
163 ebda->ipl.sequence = 0xffff;
164 if (CONFIG_COREBOOT) {
165 // XXX - hardcode defaults for coreboot.
166 ebda->ipl.bootorder = 0x00000231;
167 ebda->ipl.checkfloppysig = 1;
169 // On emulators, get boot order from nvram.
170 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
171 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
172 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
173 ebda->ipl.checkfloppysig = 1;
177 // Execute a given option rom.
179 callrom(u16 seg, u16 offset)
182 memset(&br, 0, sizeof(br));
184 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
189 debug_serial_setup();
192 // Find and run any "option roms" found in the given address range.
194 rom_scan(u32 start, u32 end)
197 for (; p <= (u8*)end; p += 2048) {
199 if (*(u16*)rom != 0xaa55)
201 u32 len = rom[2] * 512;
202 u8 sum = checksum(rom, len);
204 dprintf(1, "Found option rom with bad checksum:"
205 " loc=%p len=%d sum=%x\n"
209 p = (u8*)(((u32)p + len) / 2048 * 2048);
210 dprintf(1, "Running option rom at %p\n", rom+3);
211 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
213 if (GET_BDA(ebda_seg) != SEG_EBDA)
214 BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
215 , rom, SEG_EBDA, GET_BDA(ebda_seg));
217 // Look at the ROM's PnP Expansion header. Properly, we're supposed
218 // to init all the ROMs and then go back and build an IPL table of
219 // all the bootable devices, but we can get away with one pass.
220 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
221 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
223 // 0x1A is also the offset into the expansion header of...
224 // the Bootstrap Entry Vector, or zero if there is none.
225 u16 entry = *(u16*)&rom[0x1a+0x1a];
228 // Found a device that thinks it can boot the system. Record
229 // its BEV and product name string.
231 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
234 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
235 ip->type = IPL_TYPE_BEV;
236 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
238 u16 desc = *(u16*)&rom[0x1a+0x10];
240 ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
265 dprintf(1, "Scan for VGA option rom\n");
266 rom_scan(0xc0000, 0xc7800);
268 printf("BIOS - begin\n\n");
276 floppy_drive_setup();
281 dprintf(1, "Scan for option roms\n");
282 rom_scan(0xc8000, 0xe0000);
285 // Clear .bss section for C code.
289 dprintf(3, "clearing .bss section\n");
290 extern char __bss_start[], __bss_end[];
291 memset(__bss_start, 0, __bss_end - __bss_start);
294 // Reset DMA controller
298 // first reset the DMA controllers
299 outb(0, PORT_DMA1_MASTER_CLEAR);
300 outb(0, PORT_DMA2_MASTER_CLEAR);
302 // then initialize the DMA controllers
303 outb(0xc0, PORT_DMA2_MODE_REG);
304 outb(0x00, PORT_DMA2_MASK_REG);
307 // Check if the machine was setup with a special restart vector.
309 check_restart_status()
311 // Get and then clear CMOS shutdown status.
312 u8 status = inb_cmos(CMOS_RESET_CODE);
313 outb_cmos(0, CMOS_RESET_CODE);
315 if (status == 0x00 || status == 0x09 || status >= 0x0d)
319 if (status != 0x05) {
320 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
324 // XXX - this is supposed to jump without changing any memory -
325 // but the stack has been altered by the time the code gets here.
328 memset(&br, 0, sizeof(br));
329 br.cs = GET_BDA(jump_cs_ip) >> 16;
330 br.ip = GET_BDA(jump_cs_ip);
334 // 32-bit entry point.
339 check_restart_status();
341 debug_serial_setup();
342 dprintf(1, "Start bios\n");
344 // Setup for .bss and .data sections
346 make_bios_writable();
348 // Perform main setup code.
351 // Present the user with a bootup menu.
352 interactive_bootmenu();
354 // Setup bios checksum.
355 *(u8*)0xfffff = -checksum((u8*)0xf0000, 0xffff);
357 // Prep for boot process.
358 make_bios_readonly();
361 // Invoke int 19 to start boot process.
362 dprintf(3, "Jump to int19\n");
364 memset(&br, 0, sizeof(br));
365 call16_int(0x19, &br);
368 // Externally visible 32bit entry point.
374 "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
375 "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
376 "movl $" __stringify(BUILD_STACK_ADDR) ", %esp\n"
377 "ljmp $0x10, $_start\n"