1 // 16bit code to access hard drives.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "disk.h" // floppy_13
9 #include "biosvar.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "cmos.h" // inb_cmos
12 #include "util.h" // debug_enter
13 #include "ata.h" // ATA_*
15 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
16 #define DEBUGF(fmt, args...)
19 /****************************************************************
21 ****************************************************************/
23 #define DISK_STUB(regs) do { \
24 struct bregs *__regs = (regs); \
26 disk_ret(__regs, DISK_RET_SUCCESS); \
30 checksum_seg(u16 seg, u16 offset, u32 len)
35 sum += GET_FARVAR(seg, *(u8*)(offset+i));
40 basic_access(struct bregs *regs, u8 device, u16 command)
42 u8 type = GET_EBDA(ata.devices[device].type);
44 if (type == ATA_TYPE_ATA) {
45 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
46 nlh = GET_EBDA(ata.devices[device].lchs.heads);
47 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
49 // Must be cd emulation.
50 nlc = GET_EBDA(cdemu.vdevice.cylinders);
51 nlh = GET_EBDA(cdemu.vdevice.heads);
52 nlspt = GET_EBDA(cdemu.vdevice.spt);
56 u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
57 u16 sector = regs->cl & 0x3f;
60 if (count > 128 || count == 0 || sector == 0) {
61 BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
63 disk_ret(regs, DISK_RET_EPARAM);
67 // sanity check on cyl heads, sec
68 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
69 BX_INFO("int13_harddisk: function %02x, parameters out of"
70 " range %04x/%04x/%04x!\n"
71 , regs->ah, cylinder, head, sector);
72 disk_ret(regs, DISK_RET_EPARAM);
78 disk_ret(regs, DISK_RET_SUCCESS);
82 // translate lchs to lba
83 u32 lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
86 u16 segment = regs->es;
87 u16 offset = regs->bx;
88 void *far_buffer = MAKE_FARPTR(segment, offset);
93 if (type == ATA_TYPE_ATA)
94 status = ata_cmd_data(device, command, lba, count, far_buffer);
96 status = cdrom_read_emu(device, lba, count, far_buffer);
100 // Set nb of sector transferred
101 regs->al = GET_EBDA(ata.trsfsectors);
104 BX_INFO("int13_harddisk: function %02x, error %02x !\n"
106 disk_ret(regs, DISK_RET_EBADTRACK);
108 disk_ret(regs, DISK_RET_SUCCESS);
112 extended_access(struct bregs *regs, u8 device, u16 command)
114 u16 count = GET_INT13EXT(regs, count);
116 // Can't use 64 bits lba
117 u32 lba = GET_INT13EXT(regs, lba2);
119 BX_PANIC("int13_harddisk: function %02x. Can't use 64bits lba\n"
121 disk_ret(regs, DISK_RET_EPARAM);
125 u8 type = GET_EBDA(ata.devices[device].type);
127 // Get 32 bits lba and check
128 lba = GET_INT13EXT(regs, lba1);
129 if (type == ATA_TYPE_ATA
130 && lba >= GET_EBDA(ata.devices[device].sectors)) {
131 BX_INFO("int13_harddisk: function %02x. LBA out of range\n", regs->ah);
132 disk_ret(regs, DISK_RET_EPARAM);
138 disk_ret(regs, DISK_RET_SUCCESS);
142 u16 segment = GET_INT13EXT(regs, segment);
143 u16 offset = GET_INT13EXT(regs, offset);
144 void *far_buffer = MAKE_FARPTR(segment, offset);
149 if (type == ATA_TYPE_ATA)
150 status = ata_cmd_data(device, command, lba, count, far_buffer);
152 status = cdrom_read(device, lba, count, far_buffer);
156 SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
159 BX_INFO("int13_harddisk: function %02x, error %02x !\n"
161 disk_ret(regs, DISK_RET_EBADTRACK);
164 disk_ret(regs, DISK_RET_SUCCESS);
168 /****************************************************************
169 * Hard Drive functions
170 ****************************************************************/
172 // disk controller reset
174 disk_1300(struct bregs *regs, u8 device)
181 disk_1301(struct bregs *regs, u8 device)
183 u8 v = GET_BDA(disk_last_status);
186 // XXX - clear disk_last_status?
191 disk_1302(struct bregs *regs, u8 device)
193 basic_access(regs, device, ATA_CMD_READ_SECTORS);
196 // write disk sectors
198 disk_1303(struct bregs *regs, u8 device)
200 basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
203 // verify disk sectors
205 disk_1304(struct bregs *regs, u8 device)
207 basic_access(regs, device, 0);
213 disk_1305(struct bregs *regs, u8 device)
218 // read disk drive parameters
220 disk_1308(struct bregs *regs, u8 device)
222 // Get logical geometry from table
223 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
224 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
225 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
226 u16 count = GET_EBDA(ata.hdcount);
228 nlc = nlc - 2; /* 0 based , last sector not used */
230 regs->ch = nlc & 0xff;
231 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
233 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
235 // FIXME should set ES & DI
236 disk_ret(regs, DISK_RET_SUCCESS);
239 // initialize drive parameters
241 disk_1309(struct bregs *regs, u8 device)
246 // seek to specified cylinder
248 disk_130c(struct bregs *regs, u8 device)
253 // alternate disk reset
255 disk_130d(struct bregs *regs, u8 device)
262 disk_1310(struct bregs *regs, u8 device)
264 // should look at 40:8E also???
266 // Read the status from controller
267 u8 status = inb(GET_EBDA(ata.channels[device/2].iobase1) + ATA_CB_STAT);
268 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
269 disk_ret(regs, DISK_RET_SUCCESS);
271 disk_ret(regs, DISK_RET_ENOTREADY);
276 disk_1311(struct bregs *regs, u8 device)
281 // controller internal diagnostic
283 disk_1314(struct bregs *regs, u8 device)
288 // read disk drive size
290 disk_1315(struct bregs *regs, u8 device)
292 // Get logical geometry from table
293 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
294 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
295 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
297 // Compute sector count seen by int13
298 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
299 regs->cx = lba >> 16;
300 regs->dx = lba & 0xffff;
302 disk_ret(regs, DISK_RET_SUCCESS);
303 regs->ah = 3; // hard disk accessible
306 // IBM/MS installation check
308 disk_1341(struct bregs *regs, u8 device)
310 regs->bx = 0xaa55; // install check
311 regs->cx = 0x0007; // ext disk access and edd, removable supported
312 disk_ret(regs, DISK_RET_SUCCESS);
313 regs->ah = 0x30; // EDD 3.0
316 // IBM/MS extended read
318 disk_1342(struct bregs *regs, u8 device)
320 extended_access(regs, device, ATA_CMD_READ_SECTORS);
323 // IBM/MS extended write
325 disk_1343(struct bregs *regs, u8 device)
327 extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
332 disk_1344(struct bregs *regs, u8 device)
334 extended_access(regs, device, 0);
337 // IBM/MS lock/unlock drive
339 disk_1345(struct bregs *regs, u8 device)
341 // Always success for HD
342 disk_ret(regs, DISK_RET_SUCCESS);
345 // IBM/MS eject media
347 disk_1346(struct bregs *regs, u8 device)
349 // Volume Not Removable
350 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
353 // IBM/MS extended seek
355 disk_1347(struct bregs *regs, u8 device)
357 extended_access(regs, device, 0);
360 // IBM/MS get drive parameters
362 disk_1348(struct bregs *regs, u8 device)
364 u16 size = GET_INT13DPT(regs, size);
366 // Buffer is too small
368 disk_ret(regs, DISK_RET_EPARAM);
374 u8 type = GET_EBDA(ata.devices[device].type);
375 u16 npc = GET_EBDA(ata.devices[device].pchs.cylinders);
376 u16 nph = GET_EBDA(ata.devices[device].pchs.heads);
377 u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);
378 u32 lba = GET_EBDA(ata.devices[device].sectors);
379 u16 blksize = GET_EBDA(ata.devices[device].blksize);
381 SET_INT13DPT(regs, size, 0x1a);
382 if (type == ATA_TYPE_ATA) {
383 if ((lba/npspt)/nph > 0x3fff) {
384 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
385 SET_INT13DPT(regs, cylinders, 0x3fff);
387 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
388 SET_INT13DPT(regs, cylinders, (u32)npc);
390 SET_INT13DPT(regs, heads, (u32)nph);
391 SET_INT13DPT(regs, spt, (u32)npspt);
392 SET_INT13DPT(regs, sector_count1, lba); // FIXME should be Bit64
393 SET_INT13DPT(regs, sector_count2, 0L);
396 // 0x74 = removable, media change, lockable, max values
397 SET_INT13DPT(regs, infos, 0x74);
398 SET_INT13DPT(regs, cylinders, 0xffffffff);
399 SET_INT13DPT(regs, heads, 0xffffffff);
400 SET_INT13DPT(regs, spt, 0xffffffff);
401 SET_INT13DPT(regs, sector_count1, 0xffffffff); // FIXME should be Bit64
402 SET_INT13DPT(regs, sector_count2, 0xffffffff);
404 SET_INT13DPT(regs, blksize, blksize);
407 disk_ret(regs, DISK_RET_SUCCESS);
413 SET_INT13DPT(regs, size, 0x1e);
415 SET_INT13DPT(regs, dpte_segment, EBDA_SEG);
416 SET_INT13DPT(regs, dpte_offset
417 , offsetof(struct extended_bios_data_area_s, ata.dpte));
420 u8 channel = device / 2;
421 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
422 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
423 u8 irq = GET_EBDA(ata.channels[channel].irq);
424 u8 mode = GET_EBDA(ata.devices[device].mode);
427 if (type == ATA_TYPE_ATA) {
428 u8 translation = GET_EBDA(ata.devices[device].translation);
429 options = (translation==ATA_TRANSLATION_NONE?0:1)<<3; // chs translation
430 options |= (translation==ATA_TRANSLATION_LBA?1:0)<<9;
431 options |= (translation==ATA_TRANSLATION_RECHS?3:0)<<9;
434 options = (1<<5); // removable device
435 options |= (1<<6); // atapi device
437 options |= (1<<4); // lba translation
438 options |= (mode==ATA_MODE_PIO32?1:0)<<7;
440 SET_EBDA(ata.dpte.iobase1, iobase1);
441 SET_EBDA(ata.dpte.iobase2, iobase2 + ATA_CB_DC);
442 SET_EBDA(ata.dpte.prefix, (0xe | (device % 2))<<4 );
443 SET_EBDA(ata.dpte.unused, 0xcb );
444 SET_EBDA(ata.dpte.irq, irq );
445 SET_EBDA(ata.dpte.blkcount, 1 );
446 SET_EBDA(ata.dpte.dma, 0 );
447 SET_EBDA(ata.dpte.pio, 0 );
448 SET_EBDA(ata.dpte.options, options);
449 SET_EBDA(ata.dpte.reserved, 0);
451 SET_EBDA(ata.dpte.revision, 0x11);
453 SET_EBDA(ata.dpte.revision, 0x10);
455 u8 sum = checksum_seg(EBDA_SEG
456 , offsetof(struct extended_bios_data_area_s, ata.dpte)
458 SET_EBDA(ata.dpte.checksum, ~sum);
461 disk_ret(regs, DISK_RET_SUCCESS);
466 channel = device / 2;
467 u8 iface = GET_EBDA(ata.channels[channel].iface);
468 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
470 SET_INT13DPT(regs, size, 0x42);
471 SET_INT13DPT(regs, key, 0xbedd);
472 SET_INT13DPT(regs, dpi_length, 0x24);
473 SET_INT13DPT(regs, reserved1, 0);
474 SET_INT13DPT(regs, reserved2, 0);
476 if (iface==ATA_IFACE_ISA) {
477 SET_INT13DPT(regs, host_bus[0], 'I');
478 SET_INT13DPT(regs, host_bus[1], 'S');
479 SET_INT13DPT(regs, host_bus[2], 'A');
480 SET_INT13DPT(regs, host_bus[3], 0);
484 SET_INT13DPT(regs, iface_type[0], 'A');
485 SET_INT13DPT(regs, iface_type[1], 'T');
486 SET_INT13DPT(regs, iface_type[2], 'A');
487 SET_INT13DPT(regs, iface_type[3], 0);
489 if (iface==ATA_IFACE_ISA) {
490 SET_INT13DPT(regs, iface_path[0], iobase1);
491 SET_INT13DPT(regs, iface_path[2], 0);
492 SET_INT13DPT(regs, iface_path[4], 0L);
496 SET_INT13DPT(regs, device_path[0], device%2);
497 SET_INT13DPT(regs, device_path[1], 0);
498 SET_INT13DPT(regs, device_path[2], 0);
499 SET_INT13DPT(regs, device_path[4], 0L);
501 sum = checksum_seg(regs->ds, 30, 34);
502 SET_INT13DPT(regs, checksum, ~sum);
505 // IBM/MS extended media change
507 disk_1349(struct bregs *regs, u8 device)
509 // Always success for HD
510 disk_ret(regs, DISK_RET_SUCCESS);
514 disk_134e01(struct bregs *regs, u8 device)
516 disk_ret(regs, DISK_RET_SUCCESS);
520 disk_134e03(struct bregs *regs, u8 device)
522 disk_ret(regs, DISK_RET_SUCCESS);
526 disk_134e04(struct bregs *regs, u8 device)
528 disk_ret(regs, DISK_RET_SUCCESS);
532 disk_134e06(struct bregs *regs, u8 device)
534 disk_ret(regs, DISK_RET_SUCCESS);
538 disk_134eXX(struct bregs *regs, u8 device)
540 disk_ret(regs, DISK_RET_EPARAM);
543 // IBM/MS set hardware configuration
545 disk_134e(struct bregs *regs, u8 device)
548 case 0x01: disk_134e01(regs, device); break;
549 case 0x03: disk_134e03(regs, device); break;
550 case 0x04: disk_134e04(regs, device); break;
551 case 0x06: disk_134e06(regs, device); break;
552 default: disk_134eXX(regs, device); break;
557 disk_13XX(struct bregs *regs, u8 device)
559 disk_ret(regs, DISK_RET_EPARAM);
563 disk_13(struct bregs *regs, u8 device)
567 // clear completion flag
568 SET_BDA(disk_interrupt_flag, 0);
571 case 0x00: disk_1300(regs, device); break;
572 case 0x01: disk_1301(regs, device); break;
573 case 0x02: disk_1302(regs, device); break;
574 case 0x03: disk_1303(regs, device); break;
575 case 0x04: disk_1304(regs, device); break;
576 case 0x05: disk_1305(regs, device); break;
577 case 0x08: disk_1308(regs, device); break;
578 case 0x09: disk_1309(regs, device); break;
579 case 0x0c: disk_130c(regs, device); break;
580 case 0x0d: disk_130d(regs, device); break;
581 case 0x10: disk_1310(regs, device); break;
582 case 0x11: disk_1311(regs, device); break;
583 case 0x14: disk_1314(regs, device); break;
584 case 0x15: disk_1315(regs, device); break;
585 case 0x41: disk_1341(regs, device); break;
586 case 0x42: disk_1342(regs, device); break;
587 case 0x43: disk_1343(regs, device); break;
588 case 0x44: disk_1344(regs, device); break;
589 case 0x45: disk_1345(regs, device); break;
590 case 0x46: disk_1346(regs, device); break;
591 case 0x47: disk_1347(regs, device); break;
592 case 0x48: disk_1348(regs, device); break;
593 case 0x49: disk_1349(regs, device); break;
594 case 0x4e: disk_134e(regs, device); break;
595 default: disk_13XX(regs, device); break;
600 /****************************************************************
602 ****************************************************************/
605 get_device(struct bregs *regs, u8 iscd, u8 drive)
607 // basic check : device has to be defined
608 if (drive >= CONFIG_MAX_ATA_DEVICES) {
609 disk_ret(regs, DISK_RET_EPARAM);
610 return CONFIG_MAX_ATA_DEVICES;
613 // Get the ata channel
614 u8 device = GET_EBDA(ata.idmap[iscd][drive]);
616 // basic check : device has to be valid
617 if (device >= CONFIG_MAX_ATA_DEVICES) {
618 disk_ret(regs, DISK_RET_EPARAM);
619 return CONFIG_MAX_ATA_DEVICES;
626 handle_legacy_disk(struct bregs *regs, u8 drive)
629 floppy_13(regs, drive);
634 // XXX - old code had other disk access method.
635 disk_ret(regs, DISK_RET_EPARAM);
640 u8 device = get_device(regs, 1, drive - 0xe0);
641 if (device >= CONFIG_MAX_ATA_DEVICES)
643 cdrom_13(regs, device);
647 u8 device = get_device(regs, 0, drive - 0x80);
648 if (device >= CONFIG_MAX_ATA_DEVICES)
650 disk_13(regs, device);
654 handle_40(struct bregs *regs)
657 handle_legacy_disk(regs, regs->dl);
660 // INT 13h Fixed Disk Services Entry Point
662 handle_13(struct bregs *regs)
667 if (CONFIG_CDROM_EMU) {
668 if (regs->ah == 0x4b) {
672 if (GET_EBDA(cdemu.active)) {
673 if (drive == GET_EBDA(cdemu.emulated_drive)) {
681 handle_legacy_disk(regs, drive);
684 // record completion in BIOS task complete flag
689 SET_BDA(disk_interrupt_flag, 0xff);