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 "kbd.h" // kbd_setup
16 #include "disk.h" // floppy_drive_setup
18 #define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
19 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
24 dprintf(3, "init bda\n");
25 memset(bda, 0, sizeof(*bda));
28 for (i=0; i<256; i++) {
29 SET_BDA(ivecs[i].seg, SEG_BIOS);
30 SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
33 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
35 // Set BDA Equipment Word - 0x06 = FPU enable, mouse installed
36 SET_BDA(equipment_list_flags, 0x06);
38 // set vector 0x79 to zero
39 // this is used by 'gardian angel' protection system
40 SET_BDA(ivecs[0x79].seg, 0);
41 SET_BDA(ivecs[0x79].offset, 0);
43 SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
44 SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
45 SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
46 SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
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[0x1c].offset, OFFSET_entry_1c);
51 SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
52 SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
53 SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
54 SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
55 SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
56 SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
57 SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
58 SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
59 SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
60 SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
61 SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
62 SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
64 SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
70 memset(ebda, 0, sizeof(*ebda));
71 ebda->size = EBDA_SIZE;
72 SET_BDA(ebda_seg, SEG_EBDA);
73 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
74 SET_BDA(ivecs[0x41].offset
75 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
76 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
77 SET_BDA(ivecs[0x41].offset
78 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
84 dprintf(3, "Find memory size\n");
86 if (CONFIG_COREBOOT) {
87 // XXX - just hardcode for now.
90 // On emulators, get memory size from nvram.
91 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
92 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
94 rs += 16 * 1024 * 1024;
96 rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
97 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
101 SET_EBDA(ram_size, rs);
102 dprintf(1, "ram_size=0x%08x\n", rs);
108 dprintf(3, "init pic\n");
109 outb(0x11, PORT_PIC1);
110 outb(0x11, PORT_PIC2);
111 outb(0x08, PORT_PIC1_DATA);
112 outb(0x70, PORT_PIC2_DATA);
113 outb(0x04, PORT_PIC1_DATA);
114 outb(0x02, PORT_PIC2_DATA);
115 outb(0x01, PORT_PIC1_DATA);
116 outb(0x01, PORT_PIC2_DATA);
117 outb(0xb8, PORT_PIC1_DATA);
118 if (CONFIG_PS2_MOUSE)
119 outb(0x8f, PORT_PIC2_DATA);
121 outb(0x9f, PORT_PIC2_DATA);
127 dprintf(3, "init boot device ordering\n");
130 struct ipl_entry_s *ip = &ebda->ipl.table[0];
131 ip->type = IPL_TYPE_FLOPPY;
135 ip->type = IPL_TYPE_HARDDISK;
139 if (CONFIG_CDROM_BOOT) {
140 ip->type = IPL_TYPE_CDROM;
144 ebda->ipl.count = ip - ebda->ipl.table;
145 ebda->ipl.sequence = 0xffff;
146 if (CONFIG_COREBOOT) {
147 // XXX - hardcode defaults for coreboot.
148 ebda->ipl.bootorder = 0x00000231;
149 ebda->ipl.checkfloppysig = 1;
151 // On emulators, get boot order from nvram.
152 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
153 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
154 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
155 ebda->ipl.checkfloppysig = 1;
159 // Execute a given option rom.
161 callrom(u16 seg, u16 offset)
164 memset(&br, 0, sizeof(br));
166 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
172 // Find and run any "option roms" found in the given address range.
174 rom_scan(u32 start, u32 end)
177 for (; p <= (u8*)end; p += 2048) {
179 if (*(u16*)rom != 0xaa55)
181 u32 len = rom[2] * 512;
182 if (checksum(rom, len) != 0)
184 p = (u8*)(((u32)p + len) / 2048 * 2048);
185 dprintf(1, "Running option rom at %p\n", rom+3);
186 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
188 if (GET_BDA(ebda_seg) != SEG_EBDA)
189 BX_PANIC("Option rom at %p attempted to move ebda from %x to %x\n"
190 , rom, SEG_EBDA, GET_BDA(ebda_seg));
192 // Look at the ROM's PnP Expansion header. Properly, we're supposed
193 // to init all the ROMs and then go back and build an IPL table of
194 // all the bootable devices, but we can get away with one pass.
195 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
196 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
198 // 0x1A is also the offset into the expansion header of...
199 // the Bootstrap Entry Vector, or zero if there is none.
200 u16 entry = *(u16*)&rom[0x1a+0x1a];
203 // Found a device that thinks it can boot the system. Record
204 // its BEV and product name string.
206 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
209 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
210 ip->type = IPL_TYPE_BEV;
211 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
213 u16 desc = *(u16*)&rom[0x1a+0x10];
215 ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
236 dprintf(1, "Scan for VGA option rom\n");
237 rom_scan(0xc0000, 0xc7800);
239 printf("BIOS - begin\n\n");
243 floppy_drive_setup();
248 dprintf(1, "Scan for option roms\n");
249 rom_scan(0xc8000, 0xe0000);
252 // Clear .bss section for C code.
256 dprintf(3, "clearing .bss section\n");
257 extern char __bss_start[], __bss_end[];
258 memset(__bss_start, 0, __bss_end - __bss_start);
261 // Reset DMA controller
265 // first reset the DMA controllers
266 outb(0, PORT_DMA1_MASTER_CLEAR);
267 outb(0, PORT_DMA2_MASTER_CLEAR);
269 // then initialize the DMA controllers
270 outb(0xc0, PORT_DMA2_MODE_REG);
271 outb(0x00, PORT_DMA2_MASK_REG);
274 // Check if the machine was setup with a special restart vector.
276 check_restart_status()
278 // Get and then clear CMOS shutdown status.
279 u8 status = inb_cmos(CMOS_RESET_CODE);
280 outb_cmos(0, CMOS_RESET_CODE);
282 if (status == 0x00 || status == 0x09 || status >= 0x0d)
286 if (status != 0x05) {
287 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
291 // XXX - this is supposed to jump without changing any memory -
292 // but the stack has been altered by the time the code gets here.
295 memset(&br, 0, sizeof(br));
296 br.cs = GET_BDA(jump_cs_ip) >> 16;
297 br.ip = GET_BDA(jump_cs_ip);
301 // 32-bit entry point.
306 check_restart_status();
308 dprintf(1, "Start bios\n");
310 // Setup for .bss and .data sections
312 make_bios_writable();
314 // Perform main setup code.
317 // Present the user with a bootup menu.
318 interactive_bootmenu();
320 // Prep for boot process.
321 make_bios_readonly();
324 // Invoke int 19 to start boot process.
325 dprintf(3, "Jump to int19\n");
327 memset(&br, 0, sizeof(br));
328 call16_int(0x19, &br);
331 // Externally visible 32bit entry point.
337 "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
338 "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
339 "movl $" __stringify(CONFIG_STACK_OFFSET) ", %esp\n"
340 "ljmp $0x10, $_start\n"