blob: 79ae68fb2aad82863f4886481a55181cc71c3516 [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
17#define bda ((struct bios_data_area_s *)0)
18#define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4))
19
Kevin O'Connor63dbcfb2008-03-01 22:17:07 -050020static u8
Kevin O'Connorbdce35f2008-02-26 21:33:14 -050021checksum(u8 *p, u32 len)
22{
23 u32 i;
24 u8 sum = 0;
25 for (i=0; i<len; i++)
26 sum += p[i];
27 return sum;
28}
29
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050030static void
31init_bda()
32{
33 memset(bda, 0, sizeof(*bda));
34
35 int i;
36 for (i=0; i<256; i++) {
Kevin O'Connore9061492008-03-11 21:54:18 -040037 SET_BDA(ivecs[i].seg, SEG_BIOS);
38 SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050039 }
40
Kevin O'Connore9061492008-03-11 21:54:18 -040041 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
Kevin O'Connor9bd83912008-03-08 12:42:36 -050042
43 // mov CMOS Equipment Byte to BDA Equipment Word
Kevin O'Connore9061492008-03-11 21:54:18 -040044 SET_BDA(equipment_list_flags, inb_cmos(CMOS_EQUIPMENT_INFO));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050045}
46
47static void
48init_handlers()
49{
50 // set vector 0x79 to zero
51 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040052 SET_BDA(ivecs[0x79].seg, 0);
53 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050054
Kevin O'Connore9061492008-03-11 21:54:18 -040055 SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
56 SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
57 SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
58 SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
59 SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
60 SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
61 SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
62 SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
63 SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
64 SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
65 SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
66 SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
67 SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
68 SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
69 SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
70 SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
71 SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
72 SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
73 SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
74 SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050075}
76
77static void
78init_ebda()
79{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050080 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081 ebda->size = EBDA_SIZE;
Kevin O'Connore9061492008-03-11 21:54:18 -040082 SET_BDA(ebda_seg, EBDA_SEG);
83 SET_BDA(ivecs[0x41].seg, EBDA_SEG);
84 SET_BDA(ivecs[0x41].offset
85 , offsetof(struct extended_bios_data_area_s, fdpt0));
86 SET_BDA(ivecs[0x46].seg, EBDA_SEG);
87 SET_BDA(ivecs[0x41].offset
88 , offsetof(struct extended_bios_data_area_s, fdpt1));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050089}
90
91static void
92pit_setup()
93{
94 // timer0: binary count, 16bit count, mode 2
95 outb(0x34, PORT_PIT_MODE);
96 // maximum count of 0000H = 18.2Hz
97 outb(0x0, PORT_PIT_COUNTER0);
98 outb(0x0, PORT_PIT_COUNTER0);
99}
100
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500101static u16
102detect_parport(u16 port, u8 timeout, u8 count)
103{
104 // clear input mode
105 outb(inb(port+2) & 0xdf, port+2);
106
107 outb(0xaa, port);
108 if (inb(port) != 0xaa)
109 // Not present
110 return 0;
Kevin O'Connore9061492008-03-11 21:54:18 -0400111 SET_BDA(port_lpt[count], port);
112 SET_BDA(lpt_timeout[count], timeout);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500113 return 1;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500114}
115
116static void
117lpt_setup()
118{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500119 u16 count = 0;
120 count += detect_parport(0x378, 0x14, count);
121 count += detect_parport(0x278, 0x14, count);
122
123 // Equipment word bits 14..15 determing # parallel ports
Kevin O'Connore9061492008-03-11 21:54:18 -0400124 u16 eqb = GET_BDA(equipment_list_flags);
125 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500126}
127
128static u16
129detect_serial(u16 port, u8 timeout, u8 count)
130{
131 outb(0x02, port+1);
132 if (inb(port+1) != 0x02)
133 return 0;
134 if (inb(port+2) != 0x02)
135 return 0;
136 outb(0x00, port+1);
Kevin O'Connore9061492008-03-11 21:54:18 -0400137 SET_BDA(port_com[count], port);
138 SET_BDA(com_timeout[count], timeout);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500139 return 1;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500140}
141
142static void
143serial_setup()
144{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500145 u16 count = 0;
146 count += detect_serial(0x3f8, 0x0a, count);
147 count += detect_serial(0x2f8, 0x0a, count);
148 count += detect_serial(0x3e8, 0x0a, count);
149 count += detect_serial(0x2e8, 0x0a, count);
150
151 // Equipment word bits 9..11 determing # serial ports
Kevin O'Connore9061492008-03-11 21:54:18 -0400152 u16 eqb = GET_BDA(equipment_list_flags);
153 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500154}
155
156static u32
157bcd2bin(u8 val)
158{
159 return (val & 0xf) + ((val >> 4) * 10);
160}
161
162static void
163timer_setup()
164{
165 u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
166 u32 ticks = (seconds * 18206507) / 1000000;
167 u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
168 ticks += (minutes * 10923904) / 10000;
169 u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
170 ticks += (hours * 65543427) / 1000;
Kevin O'Connore9061492008-03-11 21:54:18 -0400171 SET_BDA(timer_counter, ticks);
172 SET_BDA(timer_rollover, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500173}
174
175static void
176pic_setup()
177{
178 outb(0x11, PORT_PIC1);
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500179 outb(0x11, PORT_PIC2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500180 outb(0x08, PORT_PIC1_DATA);
181 outb(0x70, PORT_PIC2_DATA);
182 outb(0x04, PORT_PIC1_DATA);
183 outb(0x02, PORT_PIC2_DATA);
184 outb(0x01, PORT_PIC1_DATA);
185 outb(0x01, PORT_PIC2_DATA);
186 outb(0xb8, PORT_PIC1_DATA);
187 if (CONFIG_PS2_MOUSE)
188 outb(0x8f, PORT_PIC2_DATA);
189 else
190 outb(0x9f, PORT_PIC2_DATA);
191}
192
193static void
194floppy_drive_post()
195{
196 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
197 u8 out = 0;
198 if (type & 0xf0)
199 out |= 0x07;
200 if (type & 0x0f)
201 out |= 0x70;
Kevin O'Connore9061492008-03-11 21:54:18 -0400202 SET_BDA(floppy_harddisk_info, out);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500203 outb(0x02, PORT_DMA1_MASK_REG);
204
Kevin O'Connore9061492008-03-11 21:54:18 -0400205 SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500206}
207
208static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500209ata_init()
210{
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500211 // hdidmap and cdidmap init.
212 u8 device;
213 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
Kevin O'Connor180a9592008-03-04 22:50:53 -0500214 ebda->ata.idmap[0][device] = CONFIG_MAX_ATA_DEVICES;
215 ebda->ata.idmap[1][device] = CONFIG_MAX_ATA_DEVICES;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500216 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500217}
218
219static void
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500220fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
221{
222 u8 type = inb_cmos(typecmos);
223 if (type != 47)
224 // XXX - halt
225 return;
226
227 info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3);
228 info->drive_control_byte = inb_cmos(basecmos+5);
229 info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6);
230 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
231 u8 heads = inb_cmos(basecmos+2);
232 u8 sectors = inb_cmos(basecmos+8);
233 if (cyl < 1024) {
234 // no logical CHS mapping used, just physical CHS
235 // use Standard Fixed Disk Parameter Table (FDPT)
236 info->cylinders = cyl;
237 info->heads = heads;
238 info->sectors = sectors;
239 return;
240 }
241
242 // complies with Phoenix style Translated Fixed Disk Parameter
243 // Table (FDPT)
244 info->phys_cylinders = cyl;
245 info->phys_heads = heads;
246 info->phys_sectors = sectors;
247 info->sectors = sectors;
248 info->a0h_signature = 0xa0;
249 if (cyl > 8192) {
250 cyl >>= 4;
251 heads <<= 4;
252 } else if (cyl > 4096) {
253 cyl >>= 3;
254 heads <<= 3;
255 } else if (cyl > 2048) {
256 cyl >>= 2;
257 heads <<= 2;
258 }
259 info->cylinders = cyl;
260 info->heads = heads;
261 info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
262}
263
264static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500265hard_drive_post()
266{
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500267 outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
Kevin O'Connore9061492008-03-11 21:54:18 -0400268 SET_BDA(disk_count, 1);
269 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500270
271 // move disk geometry data from CMOS to EBDA disk parameter table(s)
272 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
273 if ((diskinfo & 0xf0) == 0xf0)
274 // Fill EBDA table for hard disk 0.
275 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
276 if ((diskinfo & 0x0f) == 0x0f)
277 // XXX - bochs halts on any other type
278 // Fill EBDA table for hard disk 1.
279 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500280}
281
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500282
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500283static void
284init_boot_vectors()
285{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500286 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400287 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500288 ip->type = IPL_TYPE_FLOPPY;
289 ip++;
290
291 // First HDD
292 ip->type = IPL_TYPE_HARDDISK;
293 ip++;
294
295 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500296 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500297 ip->type = IPL_TYPE_CDROM;
298 ip++;
299 }
300
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400301 ebda->ipl.count = ip - ebda->ipl.table;
302 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500303}
304
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500305static void
306callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500307{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500308 struct bregs br;
309 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500310 br.es = SEG_BIOS;
311 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500312 br.cs = seg;
313 br.ip = offset;
314 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500315}
316
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500317static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500318rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500319{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500320 u8 *p = (u8*)start;
321 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500322 u8 *rom = p;
323 if (*(u16*)rom != 0xaa55)
324 continue;
325 u32 len = rom[2] * 512;
326 if (checksum(rom, len) != 0)
327 continue;
328 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connor070231b2008-03-22 20:44:37 -0400329 callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500330
331 // Look at the ROM's PnP Expansion header. Properly, we're supposed
332 // to init all the ROMs and then go back and build an IPL table of
333 // all the bootable devices, but we can get away with one pass.
334 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
335 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
336 continue;
337 // 0x1A is also the offset into the expansion header of...
338 // the Bootstrap Entry Vector, or zero if there is none.
339 u16 entry = *(u16*)&rom[0x1a+0x1a];
340 if (!entry)
341 continue;
342 // Found a device that thinks it can boot the system. Record
343 // its BEV and product name string.
344
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400345 if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500346 continue;
347
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400348 struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500349 ip->type = IPL_TYPE_BEV;
Kevin O'Connor070231b2008-03-22 20:44:37 -0400350 ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500351
352 u16 desc = *(u16*)&rom[0x1a+0x10];
353 if (desc)
Kevin O'Connor070231b2008-03-22 20:44:37 -0400354 ip->description = (FARPTR_TO_SEG(rom) << 16) | desc;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500355
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400356 ebda->ipl.count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500357 }
358}
359
360static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500361post()
362{
Kevin O'Connor4ce6a492008-02-29 00:21:27 -0500363 BX_INFO("Start bios\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500364
365 init_bda();
366 init_handlers();
367 init_ebda();
368
369 pit_setup();
370 kbd_setup();
371 lpt_setup();
372 serial_setup();
373 timer_setup();
374 pic_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500375
376 rom_scan(0xc0000, 0xc7800);
377
378 printf("BIOS - begin\n\n");
379
Kevin O'Connor7958de32008-03-16 21:07:33 -0400380 // clear bss section -- XXX - shouldn't use globals
381 extern char __bss_start[], __bss_end[];
382 memset(__bss_start, 0, __bss_end - __bss_start);
383
Kevin O'Connora4d35762008-03-08 15:43:03 -0500384 rombios32_init();
385
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500386 init_boot_vectors();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500387
388 floppy_drive_post();
389 hard_drive_post();
Kevin O'Connor63ccc132008-03-12 20:57:08 -0400390 if (CONFIG_ATA) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500391 ata_init();
Kevin O'Connor63ccc132008-03-12 20:57:08 -0400392 ata_detect();
393 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500394
395 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500396
Kevin O'Connora2e73802008-02-27 10:27:00 -0500397 rom_scan(0xc8000, 0xe0000);
398
Kevin O'Connor157e2132008-03-09 13:32:03 -0400399 // reset the memory (some boot loaders such as syslinux suppose
400 // that the memory is set to zero)
401 memset((void*)0x40000, 0, 0x40000); // XXX - shouldn't use globals
402
Kevin O'Connor63ccc132008-03-12 20:57:08 -0400403 // Invoke int 19 to start boot process.
404 struct bregs br;
405 memset(&br, 0, sizeof(br));
406 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500407}
408
Kevin O'Connor95576e72008-03-01 09:57:51 -0500409static void
410init_dma()
411{
412 // first reset the DMA controllers
413 outb(0, PORT_DMA1_MASTER_CLEAR);
414 outb(0, PORT_DMA2_MASTER_CLEAR);
415
416 // then initialize the DMA controllers
417 outb(0xc0, PORT_DMA2_MODE_REG);
418 outb(0x00, PORT_DMA2_MASK_REG);
419}
420
421static void
422check_restart_status()
423{
424 // Get and then clear CMOS shutdown status.
425 u8 status = inb_cmos(CMOS_RESET_CODE);
426 outb_cmos(0, CMOS_RESET_CODE);
427
428 if (status == 0x00 || status == 0x09 || status >= 0x0d)
429 // Normal post
430 return;
431
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500432 if (status != 0x05) {
433 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
434 return;
435 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500436
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500437 // XXX - this is supposed to jump without changing any memory -
438 // but the stack has been altered by the time the code gets here.
439 eoi_both_pics();
440 struct bregs br;
441 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400442 br.cs = GET_BDA(jump_cs_ip) >> 16;
443 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500444 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500445}
446
Kevin O'Connor19786762008-03-05 21:09:59 -0500447void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500448_start()
449{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500450 init_dma();
451 check_restart_status();
452
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500453 post();
454}