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
15 #define bda ((struct bios_data_area_s *)0)
16 #define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4))
21 memset(bda, 0, sizeof(*bda));
24 for (i=0; i<256; i++) {
25 bda->ivecs[i].seg = 0xf000;
26 bda->ivecs[i].offset = OFFSET_dummy_iret_handler;
29 bda->mem_size_kb = BASE_MEM_IN_K;
35 // set vector 0x79 to zero
36 // this is used by 'gardian angel' protection system
37 bda->ivecs[0x79].seg = 0;
38 bda->ivecs[0x79].offset = 0;
40 bda->ivecs[0x40].offset = OFFSET_entry_40;
41 bda->ivecs[0x0e].offset = OFFSET_entry_0e;
42 bda->ivecs[0x13].offset = OFFSET_entry_13;
43 bda->ivecs[0x76].offset = OFFSET_entry_76;
44 bda->ivecs[0x17].offset = OFFSET_entry_17;
45 bda->ivecs[0x18].offset = OFFSET_entry_18;
46 bda->ivecs[0x19].offset = OFFSET_entry_19;
47 bda->ivecs[0x1c].offset = OFFSET_entry_1c;
48 bda->ivecs[0x12].offset = OFFSET_entry_12;
49 bda->ivecs[0x11].offset = OFFSET_entry_11;
50 bda->ivecs[0x15].offset = OFFSET_entry_15;
51 bda->ivecs[0x08].offset = OFFSET_entry_08;
52 bda->ivecs[0x09].offset = OFFSET_entry_09;
53 bda->ivecs[0x16].offset = OFFSET_entry_16;
54 bda->ivecs[0x14].offset = OFFSET_entry_14;
55 bda->ivecs[0x1a].offset = OFFSET_entry_1a;
56 bda->ivecs[0x70].offset = OFFSET_entry_70;
57 bda->ivecs[0x74].offset = OFFSET_entry_74;
58 bda->ivecs[0x75].offset = OFFSET_entry_75;
59 bda->ivecs[0x10].offset = OFFSET_entry_10;
65 ebda->size = EBDA_SIZE;
66 bda->ebda_seg = EBDA_SEG;
67 bda->ivecs[0x41].seg = EBDA_SEG;
68 bda->ivecs[0x41].offset = 0x3d; // XXX
69 bda->ivecs[0x46].seg = EBDA_SEG;
70 bda->ivecs[0x46].offset = 0x4d; // XXX
76 // timer0: binary count, 16bit count, mode 2
77 outb(0x34, PORT_PIT_MODE);
78 // maximum count of 0000H = 18.2Hz
79 outb(0x0, PORT_PIT_COUNTER0);
80 outb(0x0, PORT_PIT_COUNTER0);
92 bda->kbd_buf_head = bda->kbd_buf_tail = offsetof(struct bios_data_area_s, kbd_buf);
93 bda->kbd_buf_start_offset = offsetof(struct bios_data_area_s, kbd_buf);
94 bda->kbd_buf_end_offset = offsetof(struct bios_data_area_s, kbd_buf[sizeof(bda->kbd_buf)]);
98 u16 eqb = bda->equipment_list_flags;
99 eqb = (eqb & 0xff00) | inb_cmos(CMOS_EQUIPMENT_INFO);
100 bda->equipment_list_flags = eqb;
118 return (val & 0xf) + ((val >> 4) * 10);
124 u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
125 u32 ticks = (seconds * 18206507) / 1000000;
126 u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
127 ticks += (minutes * 10923904) / 10000;
128 u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
129 ticks += (hours * 65543427) / 1000;
130 bda->timer_counter = ticks;
131 bda->timer_rollover = 0;
137 outb(0x11, PORT_PIC1);
138 outb(0x11, PORT_PIC2_DATA);
139 outb(0x08, PORT_PIC1_DATA);
140 outb(0x70, PORT_PIC2_DATA);
141 outb(0x04, PORT_PIC1_DATA);
142 outb(0x02, PORT_PIC2_DATA);
143 outb(0x01, PORT_PIC1_DATA);
144 outb(0x01, PORT_PIC2_DATA);
145 outb(0xb8, PORT_PIC1_DATA);
146 if (CONFIG_PS2_MOUSE)
147 outb(0x8f, PORT_PIC2_DATA);
149 outb(0x9f, PORT_PIC2_DATA);
155 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
161 bda->floppy_harddisk_info = out;
162 outb(0x02, PORT_DMA1_MASK_REG);
164 bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2;
170 //ebda->cdemu.active = 0;
193 static void __attribute__((noinline))
194 call16(u16 seg, u16 offset)
196 u32 segoff = (seg << 16) | offset;
198 "pushal\n" // Save registers
199 "ljmp $0x20, %0\n" // Jump to 16bit transition code
200 ".globl call16_resume\n"
201 "call16_resume:\n" // point of return
202 "popal\n" // restore registers
203 : : "Z" (OFFSET_call16), "b" (segoff));
207 checksum(u8 *p, u32 len)
211 for (i=0; i<len; i++)
216 #define PTR_TO_SEG(p) ((((u32)(p)) >> 4) & 0xf000)
217 #define PTR_TO_OFFSET(p) (((u32)(p)) & 0xffff)
222 u8 *p = (u8*)0xc0000;
223 for (; p <= (u8*)0xe0000; p += 2048) {
225 if (*(u16*)rom != 0xaa55)
227 u32 len = rom[2] * 512;
228 if (checksum(rom, len) != 0)
230 p = (u8*)(((u32)p + len) / 2048 * 2048);
231 call16(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
233 // Look at the ROM's PnP Expansion header. Properly, we're supposed
234 // to init all the ROMs and then go back and build an IPL table of
235 // all the bootable devices, but we can get away with one pass.
236 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
237 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
239 // 0x1A is also the offset into the expansion header of...
240 // the Bootstrap Entry Vector, or zero if there is none.
241 u16 entry = *(u16*)&rom[0x1a+0x1a];
244 // Found a device that thinks it can boot the system. Record
245 // its BEV and product name string.
252 status_restart(u8 status)
259 BX_PANIC("Unimplemented shutdown status: %02x\n",(Bit8u)status);
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);
273 // Get and then clear CMOS shutdown status.
274 u8 status = inb_cmos(CMOS_RESET_CODE);
275 outb_cmos(0, CMOS_RESET_CODE);
277 if (status != 0x00 && status != 0x09 && status < 0x0d)
278 status_restart(status);
280 BX_INFO("Start bios");
296 printf("BIOS - begin\n\n");
305 call16(0xf000, OFFSET_begin_boot);