blob: a6f7c3d6c565953acc541a01def8576e8d53fa08 [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'Connorf076a3e2008-02-25 22:25:15 -050017
Kevin O'Connor438f6352008-03-30 21:46:53 -040018#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))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050020
21static void
22init_bda()
23{
24 memset(bda, 0, sizeof(*bda));
25
26 int i;
27 for (i=0; i<256; i++) {
Kevin O'Connore9061492008-03-11 21:54:18 -040028 SET_BDA(ivecs[i].seg, SEG_BIOS);
29 SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050030 }
31
Kevin O'Connore9061492008-03-11 21:54:18 -040032 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
Kevin O'Connor9bd83912008-03-08 12:42:36 -050033
Kevin O'Connor88c00ee2008-05-18 00:14:13 -040034 // Set BDA Equipment Word - 0x06 = FPU enable, mouse installed
35 SET_BDA(equipment_list_flags, 0x06);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050036
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037 // set vector 0x79 to zero
38 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040039 SET_BDA(ivecs[0x79].seg, 0);
40 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050041
Kevin O'Connore9061492008-03-11 21:54:18 -040042 SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
43 SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
44 SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
45 SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
46 SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
47 SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
48 SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
49 SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
50 SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
51 SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
52 SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
53 SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
54 SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
55 SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
56 SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
57 SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
58 SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
59 SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
60 SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
61 SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
Kevin O'Connor3bbcc142008-04-13 17:07:33 -040062
63 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'Connorf64f0db2008-05-18 02:42:58 -040083 u32 rs;
84 if (CONFIG_COREBOOT) {
85 // XXX - just hardcode for now.
86 rs = 128*1024*1024;
87 } else {
88 // On emulators, get memory size from nvram.
89 rs = (inb_cmos(CMOS_MEM_EXTMEM2_LOW)
Kevin O'Connor9571ac22008-05-17 22:20:27 -040090 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 8)) * 65536;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040091 if (rs)
92 rs += 16 * 1024 * 1024;
93 else
94 rs = ((inb_cmos(CMOS_MEM_EXTMEM_LOW)
95 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 8)) * 1024
96 + 1 * 1024 * 1024);
97 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -040098
99 SET_EBDA(ram_size, rs);
100 BX_INFO("ram_size=0x%08x\n", rs);
101}
102
103static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500104pic_setup()
105{
106 outb(0x11, PORT_PIC1);
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500107 outb(0x11, PORT_PIC2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500108 outb(0x08, PORT_PIC1_DATA);
109 outb(0x70, PORT_PIC2_DATA);
110 outb(0x04, PORT_PIC1_DATA);
111 outb(0x02, PORT_PIC2_DATA);
112 outb(0x01, PORT_PIC1_DATA);
113 outb(0x01, PORT_PIC2_DATA);
114 outb(0xb8, PORT_PIC1_DATA);
115 if (CONFIG_PS2_MOUSE)
116 outb(0x8f, PORT_PIC2_DATA);
117 else
118 outb(0x9f, PORT_PIC2_DATA);
119}
120
121static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500122init_boot_vectors()
123{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500124 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400125 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500126 ip->type = IPL_TYPE_FLOPPY;
127 ip++;
128
129 // First HDD
130 ip->type = IPL_TYPE_HARDDISK;
131 ip++;
132
133 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500134 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500135 ip->type = IPL_TYPE_CDROM;
136 ip++;
137 }
138
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400139 ebda->ipl.count = ip - ebda->ipl.table;
140 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400141 if (CONFIG_COREBOOT) {
142 // XXX - hardcode defaults for coreboot.
143 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400144 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400145 } else {
146 // On emulators, get boot order from nvram.
147 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
148 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
149 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
150 ebda->ipl.checkfloppysig = 1;
151 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500152}
153
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500154static void
155callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500156{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500157 struct bregs br;
158 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500159 br.es = SEG_BIOS;
160 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500161 br.cs = seg;
162 br.ip = offset;
163 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500164}
165
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500166static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500167rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500168{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500169 u8 *p = (u8*)start;
170 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500171 u8 *rom = p;
172 if (*(u16*)rom != 0xaa55)
173 continue;
174 u32 len = rom[2] * 512;
175 if (checksum(rom, len) != 0)
176 continue;
177 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connor070231b2008-03-22 20:44:37 -0400178 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500179
180 // Look at the ROM's PnP Expansion header. Properly, we're supposed
181 // to init all the ROMs and then go back and build an IPL table of
182 // all the bootable devices, but we can get away with one pass.
183 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
184 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
185 continue;
186 // 0x1A is also the offset into the expansion header of...
187 // the Bootstrap Entry Vector, or zero if there is none.
188 u16 entry = *(u16*)&rom[0x1a+0x1a];
189 if (!entry)
190 continue;
191 // Found a device that thinks it can boot the system. Record
192 // its BEV and product name string.
193
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400194 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500195 continue;
196
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400197 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500198 ip->type = IPL_TYPE_BEV;
Kevin O'Connor070231b2008-03-22 20:44:37 -0400199 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500200
201 u16 desc = *(u16*)&rom[0x1a+0x10];
202 if (desc)
Kevin O'Connore0113c92008-04-05 15:51:12 -0400203 ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500204
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400205 ebda->ipl.count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500206 }
207}
208
209static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500210post()
211{
Kevin O'Connor1812e202008-05-07 21:29:50 -0400212 if (CONFIG_DEBUG_SERIAL)
213 debug_serial_setup();
214
Kevin O'Connor4ce6a492008-02-29 00:21:27 -0500215 BX_INFO("Start bios\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500216
217 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500218 init_ebda();
219
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400220 timer_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500221 kbd_setup();
222 lpt_setup();
223 serial_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500224 pic_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500225
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400226 ram_probe();
227
Kevin O'Connora2e73802008-02-27 10:27:00 -0500228 rom_scan(0xc0000, 0xc7800);
229
230 printf("BIOS - begin\n\n");
231
Kevin O'Connor7958de32008-03-16 21:07:33 -0400232 // clear bss section -- XXX - shouldn't use globals
233 extern char __bss_start[], __bss_end[];
234 memset(__bss_start, 0, __bss_end - __bss_start);
235
Kevin O'Connora4d35762008-03-08 15:43:03 -0500236 rombios32_init();
237
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400238 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400239 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500240
241 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500242
Kevin O'Connora2e73802008-02-27 10:27:00 -0500243 rom_scan(0xc8000, 0xe0000);
244
Kevin O'Connore0113c92008-04-05 15:51:12 -0400245 interactive_bootmenu();
246
Kevin O'Connor157e2132008-03-09 13:32:03 -0400247 // reset the memory (some boot loaders such as syslinux suppose
248 // that the memory is set to zero)
249 memset((void*)0x40000, 0, 0x40000); // XXX - shouldn't use globals
250
Kevin O'Connor63ccc132008-03-12 20:57:08 -0400251 // Invoke int 19 to start boot process.
252 struct bregs br;
253 memset(&br, 0, sizeof(br));
254 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500255}
256
Kevin O'Connor95576e72008-03-01 09:57:51 -0500257static void
258init_dma()
259{
260 // first reset the DMA controllers
261 outb(0, PORT_DMA1_MASTER_CLEAR);
262 outb(0, PORT_DMA2_MASTER_CLEAR);
263
264 // then initialize the DMA controllers
265 outb(0xc0, PORT_DMA2_MODE_REG);
266 outb(0x00, PORT_DMA2_MASK_REG);
267}
268
269static void
270check_restart_status()
271{
272 // Get and then clear CMOS shutdown status.
273 u8 status = inb_cmos(CMOS_RESET_CODE);
274 outb_cmos(0, CMOS_RESET_CODE);
275
276 if (status == 0x00 || status == 0x09 || status >= 0x0d)
277 // Normal post
278 return;
279
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500280 if (status != 0x05) {
281 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
282 return;
283 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500284
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500285 // XXX - this is supposed to jump without changing any memory -
286 // but the stack has been altered by the time the code gets here.
287 eoi_both_pics();
288 struct bregs br;
289 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400290 br.cs = GET_BDA(jump_cs_ip) >> 16;
291 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500292 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500293}
294
Kevin O'Connor19786762008-03-05 21:09:59 -0500295void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500296_start()
297{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500298 init_dma();
299 check_restart_status();
300
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500301 post();
302}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400303
304// Externally visible 32bit entry point.
305asm(
306 ".global post32\n"
307 "post32:\n"
308 "cli\n"
309 "cld\n"
310 "lidtl " __stringify(0xf0000 | OFFSET_pmode_IDT_info) "\n"
311 "lgdtl " __stringify(0xf0000 | OFFSET_rombios32_gdt_48) "\n"
312 "movl $" __stringify(CONFIG_STACK_OFFSET) ", %esp\n"
Kevin O'Connor59fead62008-05-10 15:49:20 -0400313 "ljmp $0x10, $_start\n"
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400314 );