Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // 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'Connor | 63ccc13 | 2008-03-12 20:57:08 -0400 | [diff] [blame] | 14 | #include "ata.h" |
Kevin O'Connor | 2018eff | 2008-03-12 21:27:02 -0400 | [diff] [blame] | 15 | #include "kbd.h" |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 16 | |
| 17 | #define bda ((struct bios_data_area_s *)0) |
| 18 | #define ebda ((struct extended_bios_data_area_s *)(EBDA_SEG<<4)) |
| 19 | |
Kevin O'Connor | 63dbcfb | 2008-03-01 22:17:07 -0500 | [diff] [blame] | 20 | static u8 |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 21 | checksum(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'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 30 | static void |
| 31 | init_bda() |
| 32 | { |
| 33 | memset(bda, 0, sizeof(*bda)); |
| 34 | |
| 35 | int i; |
| 36 | for (i=0; i<256; i++) { |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 37 | SET_BDA(ivecs[i].seg, SEG_BIOS); |
| 38 | SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 41 | SET_BDA(mem_size_kb, BASE_MEM_IN_K); |
Kevin O'Connor | 9bd8391 | 2008-03-08 12:42:36 -0500 | [diff] [blame] | 42 | |
| 43 | // mov CMOS Equipment Byte to BDA Equipment Word |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 44 | SET_BDA(equipment_list_flags, inb_cmos(CMOS_EQUIPMENT_INFO)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static void |
| 48 | init_handlers() |
| 49 | { |
| 50 | // set vector 0x79 to zero |
| 51 | // this is used by 'gardian angel' protection system |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 52 | SET_BDA(ivecs[0x79].seg, 0); |
| 53 | SET_BDA(ivecs[0x79].offset, 0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 54 | |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 55 | 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'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
| 78 | init_ebda() |
| 79 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 80 | memset(ebda, 0, sizeof(*ebda)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 81 | ebda->size = EBDA_SIZE; |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 82 | 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'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void |
| 92 | pit_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'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 101 | static u16 |
| 102 | detect_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'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 111 | SET_BDA(port_lpt[count], port); |
| 112 | SET_BDA(lpt_timeout[count], timeout); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 113 | return 1; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void |
| 117 | lpt_setup() |
| 118 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 119 | 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'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 124 | u16 eqb = GET_BDA(equipment_list_flags); |
| 125 | SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14)); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static u16 |
| 129 | detect_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'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 137 | SET_BDA(port_com[count], port); |
| 138 | SET_BDA(com_timeout[count], timeout); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 139 | return 1; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void |
| 143 | serial_setup() |
| 144 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 145 | 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'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 152 | u16 eqb = GET_BDA(equipment_list_flags); |
| 153 | SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static u32 |
| 157 | bcd2bin(u8 val) |
| 158 | { |
| 159 | return (val & 0xf) + ((val >> 4) * 10); |
| 160 | } |
| 161 | |
| 162 | static void |
| 163 | timer_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'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 171 | SET_BDA(timer_counter, ticks); |
| 172 | SET_BDA(timer_rollover, 0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void |
| 176 | pic_setup() |
| 177 | { |
| 178 | outb(0x11, PORT_PIC1); |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 179 | outb(0x11, PORT_PIC2); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 180 | 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 | |
| 193 | static void |
| 194 | floppy_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'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 202 | SET_BDA(floppy_harddisk_info, out); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 203 | outb(0x02, PORT_DMA1_MASK_REG); |
| 204 | |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 205 | SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 209 | ata_init() |
| 210 | { |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 211 | // hdidmap and cdidmap init. |
| 212 | u8 device; |
| 213 | for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) { |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 214 | ebda->ata.idmap[0][device] = CONFIG_MAX_ATA_DEVICES; |
| 215 | ebda->ata.idmap[1][device] = CONFIG_MAX_ATA_DEVICES; |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 216 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 220 | fill_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 | |
| 264 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 265 | hard_drive_post() |
| 266 | { |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 267 | outb(0x0a, 0x03f6); // 0000 1010 = reserved, disable IRQ 14 |
Kevin O'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 268 | SET_BDA(disk_count, 1); |
| 269 | SET_BDA(disk_control_byte, 0xc0); |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 270 | |
| 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'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 282 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 283 | static void |
| 284 | init_boot_vectors() |
| 285 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 286 | // Floppy drive |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 287 | struct ipl_entry_s *ip = &ebda->ipl.table[0]; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 288 | ip->type = IPL_TYPE_FLOPPY; |
| 289 | ip++; |
| 290 | |
| 291 | // First HDD |
| 292 | ip->type = IPL_TYPE_HARDDISK; |
| 293 | ip++; |
| 294 | |
| 295 | // CDROM |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 296 | if (CONFIG_CDROM_BOOT) { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 297 | ip->type = IPL_TYPE_CDROM; |
| 298 | ip++; |
| 299 | } |
| 300 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 301 | ebda->ipl.count = ip - ebda->ipl.table; |
| 302 | ebda->ipl.sequence = 0xffff; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 305 | static void |
| 306 | callrom(u16 seg, u16 offset) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 307 | { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 308 | struct bregs br; |
| 309 | memset(&br, 0, sizeof(br)); |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 310 | br.es = SEG_BIOS; |
| 311 | br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 312 | br.cs = seg; |
| 313 | br.ip = offset; |
| 314 | call16(&br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 317 | static void |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 318 | rom_scan(u32 start, u32 end) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 319 | { |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 320 | u8 *p = (u8*)start; |
| 321 | for (; p <= (u8*)end; p += 2048) { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 322 | 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'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 329 | callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3)); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 330 | |
| 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'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 345 | if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table)) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 346 | continue; |
| 347 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 348 | struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count]; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 349 | ip->type = IPL_TYPE_BEV; |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 350 | ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 351 | |
| 352 | u16 desc = *(u16*)&rom[0x1a+0x10]; |
| 353 | if (desc) |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 354 | ip->description = (FARPTR_TO_SEG(rom) << 16) | desc; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 355 | |
Kevin O'Connor | 56a506d | 2008-03-29 13:15:36 -0400 | [diff] [blame^] | 356 | ebda->ipl.count++; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 361 | post() |
| 362 | { |
Kevin O'Connor | 4ce6a49 | 2008-02-29 00:21:27 -0500 | [diff] [blame] | 363 | BX_INFO("Start bios\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 364 | |
| 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'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 375 | |
| 376 | rom_scan(0xc0000, 0xc7800); |
| 377 | |
| 378 | printf("BIOS - begin\n\n"); |
| 379 | |
Kevin O'Connor | 7958de3 | 2008-03-16 21:07:33 -0400 | [diff] [blame] | 380 | // 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'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 384 | rombios32_init(); |
| 385 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 386 | init_boot_vectors(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 387 | |
| 388 | floppy_drive_post(); |
| 389 | hard_drive_post(); |
Kevin O'Connor | 63ccc13 | 2008-03-12 20:57:08 -0400 | [diff] [blame] | 390 | if (CONFIG_ATA) { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 391 | ata_init(); |
Kevin O'Connor | 63ccc13 | 2008-03-12 20:57:08 -0400 | [diff] [blame] | 392 | ata_detect(); |
| 393 | } |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 394 | |
| 395 | init_boot_vectors(); |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 396 | |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 397 | rom_scan(0xc8000, 0xe0000); |
| 398 | |
Kevin O'Connor | 157e213 | 2008-03-09 13:32:03 -0400 | [diff] [blame] | 399 | // 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'Connor | 63ccc13 | 2008-03-12 20:57:08 -0400 | [diff] [blame] | 403 | // Invoke int 19 to start boot process. |
| 404 | struct bregs br; |
| 405 | memset(&br, 0, sizeof(br)); |
| 406 | call16_int(0x19, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 409 | static void |
| 410 | init_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 | |
| 421 | static void |
| 422 | check_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'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 432 | if (status != 0x05) { |
| 433 | BX_PANIC("Unimplemented shutdown status: %02x\n", status); |
| 434 | return; |
| 435 | } |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 436 | |
Kevin O'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 437 | // 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'Connor | e906149 | 2008-03-11 21:54:18 -0400 | [diff] [blame] | 442 | br.cs = GET_BDA(jump_cs_ip) >> 16; |
| 443 | br.ip = GET_BDA(jump_cs_ip); |
Kevin O'Connor | ad4ec34 | 2008-03-02 23:25:11 -0500 | [diff] [blame] | 444 | call16(&br); |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 445 | } |
| 446 | |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 447 | void VISIBLE32 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 448 | _start() |
| 449 | { |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 450 | init_dma(); |
| 451 | check_restart_status(); |
| 452 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 453 | post(); |
| 454 | } |