blob: 0af8c0c47e490d34fe40d42718017f538d11cf19 [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
14
15#define bda ((struct bios_data_area_s *)0)
16#define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050017#define ipl ((struct ipl_s *)(IPL_SEG<<4))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050018
Kevin O'Connor63dbcfb2008-03-01 22:17:07 -050019static u8
Kevin O'Connorbdce35f2008-02-26 21:33:14 -050020checksum(u8 *p, u32 len)
21{
22 u32 i;
23 u8 sum = 0;
24 for (i=0; i<len; i++)
25 sum += p[i];
26 return sum;
27}
28
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050029static void
30init_bda()
31{
32 memset(bda, 0, sizeof(*bda));
33
34 int i;
35 for (i=0; i<256; i++) {
Kevin O'Connor44c631d2008-03-02 11:24:36 -050036 bda->ivecs[i].seg = SEG_BIOS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037 bda->ivecs[i].offset = OFFSET_dummy_iret_handler;
38 }
39
40 bda->mem_size_kb = BASE_MEM_IN_K;
41}
42
43static void
44init_handlers()
45{
46 // set vector 0x79 to zero
47 // this is used by 'gardian angel' protection system
48 bda->ivecs[0x79].seg = 0;
49 bda->ivecs[0x79].offset = 0;
50
51 bda->ivecs[0x40].offset = OFFSET_entry_40;
52 bda->ivecs[0x0e].offset = OFFSET_entry_0e;
53 bda->ivecs[0x13].offset = OFFSET_entry_13;
54 bda->ivecs[0x76].offset = OFFSET_entry_76;
55 bda->ivecs[0x17].offset = OFFSET_entry_17;
56 bda->ivecs[0x18].offset = OFFSET_entry_18;
57 bda->ivecs[0x19].offset = OFFSET_entry_19;
58 bda->ivecs[0x1c].offset = OFFSET_entry_1c;
59 bda->ivecs[0x12].offset = OFFSET_entry_12;
60 bda->ivecs[0x11].offset = OFFSET_entry_11;
61 bda->ivecs[0x15].offset = OFFSET_entry_15;
62 bda->ivecs[0x08].offset = OFFSET_entry_08;
63 bda->ivecs[0x09].offset = OFFSET_entry_09;
64 bda->ivecs[0x16].offset = OFFSET_entry_16;
65 bda->ivecs[0x14].offset = OFFSET_entry_14;
66 bda->ivecs[0x1a].offset = OFFSET_entry_1a;
67 bda->ivecs[0x70].offset = OFFSET_entry_70;
68 bda->ivecs[0x74].offset = OFFSET_entry_74;
69 bda->ivecs[0x75].offset = OFFSET_entry_75;
70 bda->ivecs[0x10].offset = OFFSET_entry_10;
71}
72
73static void
74init_ebda()
75{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050076 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050077 ebda->size = EBDA_SIZE;
78 bda->ebda_seg = EBDA_SEG;
79 bda->ivecs[0x41].seg = EBDA_SEG;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050080 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081 bda->ivecs[0x46].seg = EBDA_SEG;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050082 bda->ivecs[0x41].offset = offsetof(struct extended_bios_data_area_s, fdpt1);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050083}
84
85static void
86pit_setup()
87{
88 // timer0: binary count, 16bit count, mode 2
89 outb(0x34, PORT_PIT_MODE);
90 // maximum count of 0000H = 18.2Hz
91 outb(0x0, PORT_PIT_COUNTER0);
92 outb(0x0, PORT_PIT_COUNTER0);
93}
94
Kevin O'Connor4b60c002008-02-25 22:29:55 -050095//--------------------------------------------------------------------------
96// keyboard_panic
97//--------------------------------------------------------------------------
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050098static void
Kevin O'Connor4b60c002008-02-25 22:29:55 -050099keyboard_panic(u16 status)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500100{
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500101 // If you're getting a 993 keyboard panic here,
102 // please see the comment in keyboard_init
103
104 BX_PANIC("Keyboard error:%u\n",status);
105}
106
107static void
108kbd_wait(u8 mask, u8 code)
109{
110 u16 max = 0xffff;
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500111 while ( ((inb(PORT_PS2_STATUS) & mask) == 0) && (--max>0) )
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500112 outb(code, PORT_DIAG);
113 if (!max)
114 keyboard_panic(code);
115}
116
117static inline void kbd_flush(u8 code) {
118 kbd_wait(0x02, code);
119}
120static inline void kbd_waitdata(u8 code) {
121 kbd_wait(0x01, code);
122}
123
124//--------------------------------------------------------------------------
125// keyboard_init
126//--------------------------------------------------------------------------
127// this file is based on LinuxBIOS implementation of keyboard.c
128// could convert to #asm to gain space
129static void
130keyboard_init()
131{
132 /* ------------------- Flush buffers ------------------------*/
133 /* Wait until buffer is empty */
134 kbd_flush(0x00);
135
136 /* flush incoming keys */
137 u16 max=0x2000;
138 while (--max > 0) {
139 outb(0x00, PORT_DIAG);
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500140 if (inb(PORT_PS2_STATUS) & 0x01) {
141 inb(PORT_PS2_DATA);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500142 max = 0x2000;
143 }
144 }
145
146 // Due to timer issues, and if the IPS setting is > 15000000,
147 // the incoming keys might not be flushed here. That will
148 // cause a panic a few lines below. See sourceforge bug report :
149 // [ 642031 ] FATAL: Keyboard RESET error:993
150
151 /* ------------------- controller side ----------------------*/
152 /* send cmd = 0xAA, self test 8042 */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500153 outb(0xaa, PORT_PS2_STATUS);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500154
155 kbd_flush(0x00);
156 kbd_waitdata(0x01);
157
158 /* read self-test result, 0x55 should be returned from 0x60 */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500159 if (inb(PORT_PS2_DATA) != 0x55)
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500160 keyboard_panic(991);
161
162 /* send cmd = 0xAB, keyboard interface test */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500163 outb(0xab, PORT_PS2_STATUS);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500164
165 kbd_flush(0x10);
166 kbd_waitdata(0x11);
167
168 /* read keyboard interface test result, */
169 /* 0x00 should be returned form 0x60 */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500170 if (inb(PORT_PS2_DATA) != 0x00)
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500171 keyboard_panic(992);
172
173 /* Enable Keyboard clock */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500174 outb(0xae, PORT_PS2_STATUS);
175 outb(0xa8, PORT_PS2_STATUS);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500176
177 /* ------------------- keyboard side ------------------------*/
178 /* reset kerboard and self test (keyboard side) */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500179 outb(0xff, PORT_PS2_DATA);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500180
181 kbd_flush(0x20);
182 kbd_waitdata(0x21);
183
184 /* keyboard should return ACK */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500185 if (inb(PORT_PS2_DATA) != 0xfa)
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500186 keyboard_panic(993);
187
188 kbd_waitdata(0x31);
189
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500190 if (inb(PORT_PS2_DATA) != 0xaa)
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500191 keyboard_panic(994);
192
193 /* Disable keyboard */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500194 outb(0xf5, PORT_PS2_DATA);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500195
196 kbd_flush(0x40);
197 kbd_waitdata(0x41);
198
199 /* keyboard should return ACK */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500200 if (inb(PORT_PS2_DATA) != 0xfa)
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500201 keyboard_panic(995);
202
203 /* Write Keyboard Mode */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500204 outb(0x60, PORT_PS2_STATUS);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500205
206 kbd_flush(0x50);
207
208 /* send cmd: scan code convert, disable mouse, enable IRQ 1 */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500209 outb(0x61, PORT_PS2_DATA);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500210
211 kbd_flush(0x60);
212
213 /* Enable keyboard */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500214 outb(0xf4, PORT_PS2_DATA);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500215
216 kbd_flush(0x70);
217 kbd_waitdata(0x71);
218
219 /* keyboard should return ACK */
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500220 if (inb(PORT_PS2_DATA) != 0xfa)
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500221 keyboard_panic(996);
222
223 outb(0x77, PORT_DIAG);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500224}
225
226static void
227kbd_setup()
228{
229 bda->kbd_mode = 0x10;
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500230 bda->kbd_buf_head = bda->kbd_buf_tail = bda->kbd_buf_start_offset
231 = offsetof(struct bios_data_area_s, kbd_buf) - 0x400;
232 bda->kbd_buf_end_offset
233 = (offsetof(struct bios_data_area_s, kbd_buf[sizeof(bda->kbd_buf)])
234 - 0x400);
235 keyboard_init();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500236
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500237 // mov CMOS Equipment Byte to BDA Equipment Word
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500238 u16 eqb = bda->equipment_list_flags;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500239 bda->equipment_list_flags = (eqb & 0xff00) | inb_cmos(CMOS_EQUIPMENT_INFO);
240}
241
242static u16
243detect_parport(u16 port, u8 timeout, u8 count)
244{
245 // clear input mode
246 outb(inb(port+2) & 0xdf, port+2);
247
248 outb(0xaa, port);
249 if (inb(port) != 0xaa)
250 // Not present
251 return 0;
252 bda->port_lpt[count] = port;
253 bda->lpt_timeout[count] = timeout;
254 return 1;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500255}
256
257static void
258lpt_setup()
259{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500260 u16 count = 0;
261 count += detect_parport(0x378, 0x14, count);
262 count += detect_parport(0x278, 0x14, count);
263
264 // Equipment word bits 14..15 determing # parallel ports
265 u16 eqb = bda->equipment_list_flags;
266 bda->equipment_list_flags = (eqb & 0x3fff) | (count << 14);
267}
268
269static u16
270detect_serial(u16 port, u8 timeout, u8 count)
271{
272 outb(0x02, port+1);
273 if (inb(port+1) != 0x02)
274 return 0;
275 if (inb(port+2) != 0x02)
276 return 0;
277 outb(0x00, port+1);
278 bda->port_com[count] = port;
279 bda->com_timeout[count] = timeout;
280 return 1;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500281}
282
283static void
284serial_setup()
285{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500286 u16 count = 0;
287 count += detect_serial(0x3f8, 0x0a, count);
288 count += detect_serial(0x2f8, 0x0a, count);
289 count += detect_serial(0x3e8, 0x0a, count);
290 count += detect_serial(0x2e8, 0x0a, count);
291
292 // Equipment word bits 9..11 determing # serial ports
293 u16 eqb = bda->equipment_list_flags;
294 bda->equipment_list_flags = (eqb & 0xf1ff) | (count << 9);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500295}
296
297static u32
298bcd2bin(u8 val)
299{
300 return (val & 0xf) + ((val >> 4) * 10);
301}
302
303static void
304timer_setup()
305{
306 u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
307 u32 ticks = (seconds * 18206507) / 1000000;
308 u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
309 ticks += (minutes * 10923904) / 10000;
310 u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
311 ticks += (hours * 65543427) / 1000;
312 bda->timer_counter = ticks;
313 bda->timer_rollover = 0;
314}
315
316static void
317pic_setup()
318{
319 outb(0x11, PORT_PIC1);
320 outb(0x11, PORT_PIC2_DATA);
321 outb(0x08, PORT_PIC1_DATA);
322 outb(0x70, PORT_PIC2_DATA);
323 outb(0x04, PORT_PIC1_DATA);
324 outb(0x02, PORT_PIC2_DATA);
325 outb(0x01, PORT_PIC1_DATA);
326 outb(0x01, PORT_PIC2_DATA);
327 outb(0xb8, PORT_PIC1_DATA);
328 if (CONFIG_PS2_MOUSE)
329 outb(0x8f, PORT_PIC2_DATA);
330 else
331 outb(0x9f, PORT_PIC2_DATA);
332}
333
334static void
335floppy_drive_post()
336{
337 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
338 u8 out = 0;
339 if (type & 0xf0)
340 out |= 0x07;
341 if (type & 0x0f)
342 out |= 0x70;
343 bda->floppy_harddisk_info = out;
344 outb(0x02, PORT_DMA1_MASK_REG);
345
346 bda->ivecs[0x1E].offset = OFFSET_diskette_param_table2;
347}
348
349static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500350ata_init()
351{
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500352 // hdidmap and cdidmap init.
353 u8 device;
354 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
355 ebda->ata.hdidmap[device] = CONFIG_MAX_ATA_DEVICES;
356 ebda->ata.cdidmap[device] = CONFIG_MAX_ATA_DEVICES;
357 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500358}
359
360static void
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500361fill_hdinfo(struct fdpt_s *info, u8 typecmos, u8 basecmos)
362{
363 u8 type = inb_cmos(typecmos);
364 if (type != 47)
365 // XXX - halt
366 return;
367
368 info->precompensation = (inb_cmos(basecmos+4) << 8) | inb_cmos(basecmos+3);
369 info->drive_control_byte = inb_cmos(basecmos+5);
370 info->landing_zone = (inb_cmos(basecmos+7) << 8) | inb_cmos(basecmos+6);
371 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
372 u8 heads = inb_cmos(basecmos+2);
373 u8 sectors = inb_cmos(basecmos+8);
374 if (cyl < 1024) {
375 // no logical CHS mapping used, just physical CHS
376 // use Standard Fixed Disk Parameter Table (FDPT)
377 info->cylinders = cyl;
378 info->heads = heads;
379 info->sectors = sectors;
380 return;
381 }
382
383 // complies with Phoenix style Translated Fixed Disk Parameter
384 // Table (FDPT)
385 info->phys_cylinders = cyl;
386 info->phys_heads = heads;
387 info->phys_sectors = sectors;
388 info->sectors = sectors;
389 info->a0h_signature = 0xa0;
390 if (cyl > 8192) {
391 cyl >>= 4;
392 heads <<= 4;
393 } else if (cyl > 4096) {
394 cyl >>= 3;
395 heads <<= 3;
396 } else if (cyl > 2048) {
397 cyl >>= 2;
398 heads <<= 2;
399 }
400 info->cylinders = cyl;
401 info->heads = heads;
402 info->checksum = ~checksum((u8*)info, sizeof(*info)-1) + 1;
403}
404
405static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500406hard_drive_post()
407{
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500408 outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14
409 bda->disk_count = 1;
410 bda->disk_control_byte = 0xc0;
411
412 // move disk geometry data from CMOS to EBDA disk parameter table(s)
413 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
414 if ((diskinfo & 0xf0) == 0xf0)
415 // Fill EBDA table for hard disk 0.
416 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
417 if ((diskinfo & 0x0f) == 0x0f)
418 // XXX - bochs halts on any other type
419 // Fill EBDA table for hard disk 1.
420 fill_hdinfo(&ebda->fdpt0, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500421}
422
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500423
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500424static void
425init_boot_vectors()
426{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500427 // Clear out the IPL table.
428 memset(ipl, 0, sizeof(*ipl));
429
430 // Floppy drive
431 struct ipl_entry_s *ip = &ipl->table[0];
432 ip->type = IPL_TYPE_FLOPPY;
433 ip++;
434
435 // First HDD
436 ip->type = IPL_TYPE_HARDDISK;
437 ip++;
438
439 // CDROM
440 if (CONFIG_ELTORITO_BOOT) {
441 ip->type = IPL_TYPE_CDROM;
442 ip++;
443 }
444
445 ipl->count = ip - ipl->table;
446 ipl->sequence = 0xffff;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500447}
448
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500449static void
450callrom(u16 seg, u16 offset)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500451{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500452 struct bregs br;
453 memset(&br, 0, sizeof(br));
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500454 br.es = SEG_BIOS;
455 br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500456 br.cs = seg;
457 br.ip = offset;
458 call16(&br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500459}
460
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500461static void
Kevin O'Connora2e73802008-02-27 10:27:00 -0500462rom_scan(u32 start, u32 end)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500463{
Kevin O'Connora2e73802008-02-27 10:27:00 -0500464 u8 *p = (u8*)start;
465 for (; p <= (u8*)end; p += 2048) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500466 u8 *rom = p;
467 if (*(u16*)rom != 0xaa55)
468 continue;
469 u32 len = rom[2] * 512;
470 if (checksum(rom, len) != 0)
471 continue;
472 p = (u8*)(((u32)p + len) / 2048 * 2048);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500473 callrom(PTR_TO_SEG(rom), PTR_TO_OFFSET(rom + 3));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500474
475 // Look at the ROM's PnP Expansion header. Properly, we're supposed
476 // to init all the ROMs and then go back and build an IPL table of
477 // all the bootable devices, but we can get away with one pass.
478 if (rom[0x1a] != '$' || rom[0x1b] != 'P'
479 || rom[0x1c] != 'n' || rom[0x1d] != 'P')
480 continue;
481 // 0x1A is also the offset into the expansion header of...
482 // the Bootstrap Entry Vector, or zero if there is none.
483 u16 entry = *(u16*)&rom[0x1a+0x1a];
484 if (!entry)
485 continue;
486 // Found a device that thinks it can boot the system. Record
487 // its BEV and product name string.
488
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500489 if (ipl->count >= ARRAY_SIZE(ipl->table))
490 continue;
491
492 struct ipl_entry_s *ip = &ipl->table[ipl->count];
493 ip->type = IPL_TYPE_BEV;
494 ip->vector = (PTR_TO_SEG(rom) << 16) | entry;
495
496 u16 desc = *(u16*)&rom[0x1a+0x10];
497 if (desc)
498 ip->description = (PTR_TO_SEG(rom) << 16) | desc;
499
500 ipl->count++;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500501 }
502}
503
504static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500505post()
506{
Kevin O'Connor4ce6a492008-02-29 00:21:27 -0500507 BX_INFO("Start bios\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500508
509 init_bda();
510 init_handlers();
511 init_ebda();
512
513 pit_setup();
514 kbd_setup();
515 lpt_setup();
516 serial_setup();
517 timer_setup();
518 pic_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500519
520 rom_scan(0xc0000, 0xc7800);
521
522 printf("BIOS - begin\n\n");
523
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500524 // XXX - need to do pci stuff
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500525 //pci_setup();
526 init_boot_vectors();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500527
528 floppy_drive_post();
529 hard_drive_post();
Kevin O'Connor95576e72008-03-01 09:57:51 -0500530 if (CONFIG_ATA)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500531 ata_init();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500532
533 init_boot_vectors();
534 rom_scan(0xc8000, 0xe0000);
535
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500536 callrom(SEG_BIOS, OFFSET_begin_boot);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500537}
538
Kevin O'Connor95576e72008-03-01 09:57:51 -0500539static void
540init_dma()
541{
542 // first reset the DMA controllers
543 outb(0, PORT_DMA1_MASTER_CLEAR);
544 outb(0, PORT_DMA2_MASTER_CLEAR);
545
546 // then initialize the DMA controllers
547 outb(0xc0, PORT_DMA2_MODE_REG);
548 outb(0x00, PORT_DMA2_MASK_REG);
549}
550
551static void
Kevin O'Connord427a3f2008-03-01 14:02:41 -0500552eoi_jmp_post()
553{
554 // XXX - this is supposed to jump without changing any memory -
555 // but the stack has been altered by the time the code gets here.
556 eoi_both_pics();
557 struct bregs br;
558 memset(&br, 0, sizeof(br));
559 br.cs = bda->jump_cs_ip >> 16;
560 br.ip = bda->jump_cs_ip;
561 call16(&br);
562}
563
564static void
Kevin O'Connor95576e72008-03-01 09:57:51 -0500565check_restart_status()
566{
567 // Get and then clear CMOS shutdown status.
568 u8 status = inb_cmos(CMOS_RESET_CODE);
569 outb_cmos(0, CMOS_RESET_CODE);
570
571 if (status == 0x00 || status == 0x09 || status >= 0x0d)
572 // Normal post
573 return;
574
Kevin O'Connor95576e72008-03-01 09:57:51 -0500575 if (status == 0x05)
576 eoi_jmp_post();
Kevin O'Connor95576e72008-03-01 09:57:51 -0500577
Kevin O'Connoree2fd7a2008-03-02 08:42:16 -0500578 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500579}
580
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500581void VISIBLE
582_start()
583{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500584 init_dma();
585 check_restart_status();
586
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500587 post();
588}