blob: 5ba9a2926ed3dc3c46de43e2d5c9caffc819d5dc [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'Connor63ccc132008-03-12 20:57:08 -040014#include "ata.h"
Kevin O'Connor2018eff2008-03-12 21:27:02 -040015#include "kbd.h"
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050016
Kevin O'Connor438f6352008-03-30 21:46:53 -040017#define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
18#define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050019
20static void
21init_bda()
22{
23 memset(bda, 0, sizeof(*bda));
24
25 int i;
26 for (i=0; i<256; i++) {
Kevin O'Connore9061492008-03-11 21:54:18 -040027 SET_BDA(ivecs[i].seg, SEG_BIOS);
28 SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050029 }
30
Kevin O'Connore9061492008-03-11 21:54:18 -040031 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
Kevin O'Connor9bd83912008-03-08 12:42:36 -050032
33 // mov CMOS Equipment Byte to BDA Equipment Word
Kevin O'Connore9061492008-03-11 21:54:18 -040034 SET_BDA(equipment_list_flags, inb_cmos(CMOS_EQUIPMENT_INFO));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050035}
36
37static void
38init_handlers()
39{
40 // set vector 0x79 to zero
41 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040042 SET_BDA(ivecs[0x79].seg, 0);
43 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050044
Kevin O'Connore9061492008-03-11 21:54:18 -040045 SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
46 SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
47 SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
48 SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
49 SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
50 SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
51 SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
52 SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
53 SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
54 SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
55 SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
56 SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
57 SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
58 SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
59 SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
60 SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
61 SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
62 SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
63 SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
64 SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050065}
66
67static void
68init_ebda()
69{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050070 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050071 ebda->size = EBDA_SIZE;
Kevin O'Connor438f6352008-03-30 21:46:53 -040072 SET_BDA(ebda_seg, SEG_EBDA);
73 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040074 SET_BDA(ivecs[0x41].offset
75 , offsetof(struct extended_bios_data_area_s, fdpt0));
Kevin O'Connor438f6352008-03-30 21:46:53 -040076 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040077 SET_BDA(ivecs[0x41].offset
78 , offsetof(struct extended_bios_data_area_s, fdpt1));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079}
80
81static void
82pit_setup()
83{
84 // timer0: binary count, 16bit count, mode 2
85 outb(0x34, PORT_PIT_MODE);
86 // maximum count of 0000H = 18.2Hz
87 outb(0x0, PORT_PIT_COUNTER0);
88 outb(0x0, PORT_PIT_COUNTER0);
89}
90
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050091static u16
92detect_parport(u16 port, u8 timeout, u8 count)
93{
94 // clear input mode
95 outb(inb(port+2) & 0xdf, port+2);
96
97 outb(0xaa, port);
98 if (inb(port) != 0xaa)
99 // Not present
100 return 0;
Kevin O'Connore9061492008-03-11 21:54:18 -0400101 SET_BDA(port_lpt[count], port);
102 SET_BDA(lpt_timeout[count], timeout);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500103 return 1;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500104}
105
106static void
107lpt_setup()
108{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500109 u16 count = 0;
110 count += detect_parport(0x378, 0x14, count);
111 count += detect_parport(0x278, 0x14, count);
112
113 // Equipment word bits 14..15 determing # parallel ports
Kevin O'Connore9061492008-03-11 21:54:18 -0400114 u16 eqb = GET_BDA(equipment_list_flags);
115 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500116}
117
118static u16
119detect_serial(u16 port, u8 timeout, u8 count)
120{
121 outb(0x02, port+1);
122 if (inb(port+1) != 0x02)
123 return 0;
124 if (inb(port+2) != 0x02)
125 return 0;
126 outb(0x00, port+1);
Kevin O'Connore9061492008-03-11 21:54:18 -0400127 SET_BDA(port_com[count], port);
128 SET_BDA(com_timeout[count], timeout);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500129 return 1;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500130}
131
132static void
133serial_setup()
134{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500135 u16 count = 0;
136 count += detect_serial(0x3f8, 0x0a, count);
137 count += detect_serial(0x2f8, 0x0a, count);
138 count += detect_serial(0x3e8, 0x0a, count);
139 count += detect_serial(0x2e8, 0x0a, count);
140
141 // Equipment word bits 9..11 determing # serial ports
Kevin O'Connore9061492008-03-11 21:54:18 -0400142 u16 eqb = GET_BDA(equipment_list_flags);
143 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500144}
145
146static u32
147bcd2bin(u8 val)
148{
149 return (val & 0xf) + ((val >> 4) * 10);
150}
151
152static void
153timer_setup()
154{
155 u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
156 u32 ticks = (seconds * 18206507) / 1000000;
157 u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
158 ticks += (minutes * 10923904) / 10000;
159 u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
160 ticks += (hours * 65543427) / 1000;
Kevin O'Connore9061492008-03-11 21:54:18 -0400161 SET_BDA(timer_counter, ticks);
162 SET_BDA(timer_rollover, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500163}
164
165static void
166pic_setup()
167{
168 outb(0x11, PORT_PIC1);
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500169 outb(0x11, PORT_PIC2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500170 outb(0x08, PORT_PIC1_DATA);
171 outb(0x70, PORT_PIC2_DATA);
172 outb(0x04, PORT_PIC1_DATA);
173 outb(0x02, PORT_PIC2_DATA);
174 outb(0x01, PORT_PIC1_DATA);
175 outb(0x01, PORT_PIC2_DATA);
176 outb(0xb8, PORT_PIC1_DATA);
177 if (CONFIG_PS2_MOUSE)
178 outb(0x8f, PORT_PIC2_DATA);
179 else
180 outb(0x9f, PORT_PIC2_DATA);
181}
182
183static void
184floppy_drive_post()
185{
186 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
187 u8 out = 0;
188 if (type & 0xf0)
189 out |= 0x07;
190 if (type & 0x0f)
191 out |= 0x70;
Kevin O'Connore9061492008-03-11 21:54:18 -0400192 SET_BDA(floppy_harddisk_info, out);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500193 outb(0x02, PORT_DMA1_MASK_REG);
194
Kevin O'Connore9061492008-03-11 21:54:18 -0400195 SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500196}
197
198static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500199ata_init()
200{
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500201 // hdidmap and cdidmap init.
202 u8 device;
203 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
Kevin O'Connor180a9592008-03-04 22:50:53 -0500204 ebda->ata.idmap[0][device] = CONFIG_MAX_ATA_DEVICES;
205 ebda->ata.idmap[1][device] = CONFIG_MAX_ATA_DEVICES;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500206 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500207}
208
209static void
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500210fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
211{
212 u8 type = inb_cmos(typecmos);
213 if (type != 47)
214 // XXX - halt
215 return;
216
217 info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3);
218 info->drive_control_byte = inb_cmos(basecmos+5);
219 info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6);
220 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
221 u8 heads = inb_cmos(basecmos+2);
222 u8 sectors = inb_cmos(basecmos+8);
223 if (cyl < 1024) {
224 // no logical CHS mapping used, just physical CHS
225 // use Standard Fixed Disk Parameter Table (FDPT)
226 info->cylinders = cyl;
227 info->heads = heads;
228 info->sectors = sectors;
229 return;
230 }
231
232 // complies with Phoenix style Translated Fixed Disk Parameter
233 // Table (FDPT)
234 info->phys_cylinders = cyl;
235 info->phys_heads = heads;
236 info->phys_sectors = sectors;
237 info->sectors = sectors;
238 info->a0h_signature = 0xa0;
239 if (cyl > 8192) {
240 cyl >>= 4;
241 heads <<= 4;
242 } else if (cyl > 4096) {
243 cyl >>= 3;
244 heads <<= 3;
245 } else if (cyl > 2048) {
246 cyl >>= 2;
247 heads <<= 2;
248 }
249 info->cylinders = cyl;
250 info->heads = heads;
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -0400251 info->checksum = -checksum((u8*)info, sizeof(*info)-1);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500252}
253
254static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500255hard_drive_post()
256{
Kevin O'Connor438f6352008-03-30 21:46:53 -0400257 outb(0x0a, PORT_HD_DATA); // 0000 1010 = reserved, disable IRQ 14
Kevin O'Connore9061492008-03-11 21:54:18 -0400258 SET_BDA(disk_count, 1);
259 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500260
261 // move disk geometry data from CMOS to EBDA disk parameter table(s)
262 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
263 if ((diskinfo & 0xf0) == 0xf0)
264 // Fill EBDA table for hard disk 0.
265 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
266 if ((diskinfo & 0x0f) == 0x0f)
267 // XXX - bochs halts on any other type
268 // Fill EBDA table for hard disk 1.
269 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500270}
271
272static void
273init_boot_vectors()
274{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500275 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400276 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500277 ip->type = IPL_TYPE_FLOPPY;
278 ip++;
279
280 // First HDD
281 ip->type = IPL_TYPE_HARDDISK;
282 ip++;
283
284 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500285 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500286 ip->type = IPL_TYPE_CDROM;
287 ip++;
288 }
289
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400290 ebda->ipl.count = ip - ebda->ipl.table;
291 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500292}
293
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500294static void
295callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500296{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500297 struct bregs br;
298 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500299 br.es = SEG_BIOS;
300 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500301 br.cs = seg;
302 br.ip = offset;
303 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500304}
305
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500306static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500307rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500308{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500309 u8 *p = (u8*)start;
310 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500311 u8 *rom = p;
312 if (*(u16*)rom != 0xaa55)
313 continue;
314 u32 len = rom[2] * 512;
315 if (checksum(rom, len) != 0)
316 continue;
317 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connor070231b2008-03-22 20:44:37 -0400318 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500319
320 // Look at the ROM's PnP Expansion header. Properly, we're supposed
321 // to init all the ROMs and then go back and build an IPL table of
322 // all the bootable devices, but we can get away with one pass.
323 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
324 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
325 continue;
326 // 0x1A is also the offset into the expansion header of...
327 // the Bootstrap Entry Vector, or zero if there is none.
328 u16 entry = *(u16*)&rom[0x1a+0x1a];
329 if (!entry)
330 continue;
331 // Found a device that thinks it can boot the system. Record
332 // its BEV and product name string.
333
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400334 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500335 continue;
336
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400337 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500338 ip->type = IPL_TYPE_BEV;
Kevin O'Connor070231b2008-03-22 20:44:37 -0400339 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500340
341 u16 desc = *(u16*)&rom[0x1a+0x10];
342 if (desc)
Kevin O'Connor070231b2008-03-22 20:44:37 -0400343 ip->description = (FARPTR_TO_SEG(rom) << 16) | desc;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500344
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400345 ebda->ipl.count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500346 }
347}
348
349static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500350post()
351{
Kevin O'Connor4ce6a492008-02-29 00:21:27 -0500352 BX_INFO("Start bios\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500353
354 init_bda();
355 init_handlers();
356 init_ebda();
357
358 pit_setup();
359 kbd_setup();
360 lpt_setup();
361 serial_setup();
362 timer_setup();
363 pic_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500364
365 rom_scan(0xc0000, 0xc7800);
366
367 printf("BIOS - begin\n\n");
368
Kevin O'Connor7958de32008-03-16 21:07:33 -0400369 // clear bss section -- XXX - shouldn't use globals
370 extern char __bss_start[], __bss_end[];
371 memset(__bss_start, 0, __bss_end - __bss_start);
372
Kevin O'Connora4d35762008-03-08 15:43:03 -0500373 rombios32_init();
374
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500375 init_boot_vectors();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500376
377 floppy_drive_post();
378 hard_drive_post();
Kevin O'Connor63ccc132008-03-12 20:57:08 -0400379 if (CONFIG_ATA) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500380 ata_init();
Kevin O'Connor63ccc132008-03-12 20:57:08 -0400381 ata_detect();
382 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500383
384 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500385
Kevin O'Connora2e73802008-02-27 10:27:00 -0500386 rom_scan(0xc8000, 0xe0000);
387
Kevin O'Connor157e2132008-03-09 13:32:03 -0400388 // reset the memory (some boot loaders such as syslinux suppose
389 // that the memory is set to zero)
390 memset((void*)0x40000, 0, 0x40000); // XXX - shouldn't use globals
391
Kevin O'Connor63ccc132008-03-12 20:57:08 -0400392 // Invoke int 19 to start boot process.
393 struct bregs br;
394 memset(&br, 0, sizeof(br));
395 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500396}
397
Kevin O'Connor95576e72008-03-01 09:57:51 -0500398static void
399init_dma()
400{
401 // first reset the DMA controllers
402 outb(0, PORT_DMA1_MASTER_CLEAR);
403 outb(0, PORT_DMA2_MASTER_CLEAR);
404
405 // then initialize the DMA controllers
406 outb(0xc0, PORT_DMA2_MODE_REG);
407 outb(0x00, PORT_DMA2_MASK_REG);
408}
409
410static void
411check_restart_status()
412{
413 // Get and then clear CMOS shutdown status.
414 u8 status = inb_cmos(CMOS_RESET_CODE);
415 outb_cmos(0, CMOS_RESET_CODE);
416
417 if (status == 0x00 || status == 0x09 || status >= 0x0d)
418 // Normal post
419 return;
420
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500421 if (status != 0x05) {
422 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
423 return;
424 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500425
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500426 // XXX - this is supposed to jump without changing any memory -
427 // but the stack has been altered by the time the code gets here.
428 eoi_both_pics();
429 struct bregs br;
430 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400431 br.cs = GET_BDA(jump_cs_ip) >> 16;
432 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500433 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500434}
435
Kevin O'Connor19786762008-03-05 21:09:59 -0500436void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500437_start()
438{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500439 init_dma();
440 check_restart_status();
441
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500442 post();
443}