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 LGPLv3 license.
8 #include "disk.h" // floppy_13
9 #include "biosvar.h" // SET_BDA
10 #include "config.h" // CONFIG_*
11 #include "util.h" // debug_enter
12 #include "pic.h" // eoi_pic2
13 #include "bregs.h" // struct bregs
14 #include "pci.h" // pci_bdf_to_bus
15 #include "ata.h" // ATA_CB_DC
18 /****************************************************************
20 ****************************************************************/
23 __disk_ret(struct bregs *regs, u32 linecode, const char *fname)
27 SET_BDA(floppy_last_status, code);
29 SET_BDA(disk_last_status, code);
31 __set_code_fail(regs, linecode, fname);
33 set_code_success(regs);
37 __disk_stub(struct bregs *regs, int lineno, const char *fname)
39 __debug_stub(regs, lineno, fname);
40 __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
43 #define DISK_STUB(regs) \
44 __disk_stub((regs), __LINE__, __func__)
46 // Execute a "disk_op_s" request - this runs on a stack in the ebda.
48 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
51 memcpy_far(GET_SEG(SS), &dop
55 dprintf(DEBUG_HDL_13, "disk_op d=%d lba=%d buf=%p count=%d cmd=%d\n"
56 , dop.driveid, (u32)dop.lba, dop.buf_fl
57 , dop.count, dop.command);
62 u8 type = GET_GLOBAL(Drives.drives[dop.driveid].type);
63 if (type == DTYPE_ATA)
64 status = process_ata_op(&dop);
65 else if (type == DTYPE_ATAPI)
66 status = process_atapi_op(&dop);
70 // Update count with total sectors transferred.
71 SET_FARVAR(op_seg, op_far->count, dop.count);
76 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
78 send_disk_op(struct disk_op_s *op)
83 return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
86 // Obtain the requested disk lba from an old-style chs request.
88 legacy_lba(struct bregs *regs, u16 lchs_seg, struct chs_s *lchs_far)
91 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
92 u16 sector = regs->cl & 0x3f;
95 if (count > 128 || count == 0 || sector == 0) {
96 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
98 disk_ret(regs, DISK_RET_EPARAM);
102 u16 nlc = GET_FARVAR(lchs_seg, lchs_far->cylinders);
103 u16 nlh = GET_FARVAR(lchs_seg, lchs_far->heads);
104 u16 nlspt = GET_FARVAR(lchs_seg, lchs_far->spt);
106 // sanity check on cyl heads, sec
107 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
108 dprintf(1, "int13_harddisk: function %02x, parameters out of"
109 " range %04x/%04x/%04x!\n"
110 , regs->ah, cylinder, head, sector);
111 disk_ret(regs, DISK_RET_EPARAM);
115 // translate lchs to lba
116 return (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
120 // Perform read/write/verify using old-style chs accesses
122 basic_access(struct bregs *regs, u8 driveid, u16 command)
124 struct disk_op_s dop;
125 dop.driveid = driveid;
126 dop.command = command;
127 int lba = legacy_lba(regs, get_global_seg(), &Drives.drives[driveid].lchs);
131 dop.count = regs->al;
132 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
134 int status = send_disk_op(&dop);
136 regs->al = dop.count;
138 disk_ret(regs, status);
141 // Perform cdemu read/verify
143 cdemu_access(struct bregs *regs, u8 driveid, u16 command)
145 struct disk_op_s dop;
146 dop.driveid = driveid;
147 dop.command = command;
148 u16 ebda_seg = get_ebda_seg();
149 int vlba = legacy_lba(
151 , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
154 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
156 u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
157 u8 *dest_far = (void*)(regs->bx+0);
159 int status = DISK_RET_SUCCESS;
163 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
164 status = send_disk_op(&dop);
167 u8 thiscount = 4 - (vlba & 3);
168 if (thiscount > count)
171 memcpy_far(regs->es, dest_far
172 , ebda_seg, cdbuf_far + (vlba & 3) * 512
174 dest_far += thiscount * 512;
175 regs->al += thiscount;
180 dop.count = count / 4;
181 dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
182 status = send_disk_op(&dop);
183 regs->al += dop.count * 4;
186 u8 thiscount = count & ~3;
188 dest_far += thiscount * 512;
189 dop.lba += thiscount / 4;
194 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
195 status = send_disk_op(&dop);
198 u8 thiscount = count;
199 memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
200 regs->al += thiscount;
204 disk_ret(regs, status);
207 // Perform read/write/verify using new-style "int13ext" accesses.
209 extended_access(struct bregs *regs, u8 driveid, u16 command)
211 struct disk_op_s dop;
212 // Get lba and check.
213 dop.lba = GET_INT13EXT(regs, lba);
214 dop.command = command;
215 dop.driveid = driveid;
216 if (dop.lba >= GET_GLOBAL(Drives.drives[driveid].sectors)) {
217 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
219 disk_ret(regs, DISK_RET_EPARAM);
223 u16 segment = GET_INT13EXT(regs, segment);
224 u16 offset = GET_INT13EXT(regs, offset);
225 dop.buf_fl = MAKE_FLATPTR(segment, offset);
226 dop.count = GET_INT13EXT(regs, count);
228 int status = send_disk_op(&dop);
230 SET_INT13EXT(regs, count, dop.count);
232 disk_ret(regs, status);
236 /****************************************************************
237 * Hard Drive functions
238 ****************************************************************/
240 // disk controller reset
242 disk_1300(struct bregs *regs, u8 driveid)
244 struct disk_op_s dop;
245 dop.driveid = driveid;
246 dop.command = CMD_RESET;
247 int status = send_disk_op(&dop);
248 disk_ret(regs, status);
253 disk_1301(struct bregs *regs, u8 driveid)
258 v = GET_BDA(floppy_last_status);
260 v = GET_BDA(disk_last_status);
263 // XXX - clear disk_last_status?
268 disk_1302(struct bregs *regs, u8 driveid)
270 basic_access(regs, driveid, CMD_READ);
273 // write disk sectors
275 disk_1303(struct bregs *regs, u8 driveid)
277 basic_access(regs, driveid, CMD_WRITE);
280 // verify disk sectors
282 disk_1304(struct bregs *regs, u8 driveid)
284 basic_access(regs, driveid, CMD_VERIFY);
290 disk_1305(struct bregs *regs, u8 driveid)
295 // read disk drive parameters
297 disk_1308(struct bregs *regs, u8 driveid)
299 // Get logical geometry from table
300 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders) - 1;
301 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads) - 1;
302 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
304 if (regs->dl < 0x80) {
306 count = GET_GLOBAL(Drives.floppycount);
308 regs->bx = GET_GLOBAL(Drives.drives[driveid].floppy_type);
310 // set es & di to point to 11 byte diskette param table in ROM
312 regs->di = (u32)&diskette_param_table2;
315 count = GET_BDA(hdcount);
316 nlc--; // last sector reserved
320 regs->ch = nlc & 0xff;
321 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
324 disk_ret(regs, DISK_RET_SUCCESS);
328 // initialize drive parameters
330 disk_1309(struct bregs *regs, u8 driveid)
335 // seek to specified cylinder
337 disk_130c(struct bregs *regs, u8 driveid)
342 // alternate disk reset
344 disk_130d(struct bregs *regs, u8 driveid)
351 disk_1310(struct bregs *regs, u8 driveid)
353 // should look at 40:8E also???
355 struct disk_op_s dop;
356 dop.driveid = driveid;
357 dop.command = CMD_ISREADY;
358 int status = send_disk_op(&dop);
359 disk_ret(regs, status);
364 disk_1311(struct bregs *regs, u8 driveid)
369 // controller internal diagnostic
371 disk_1314(struct bregs *regs, u8 driveid)
376 // read disk drive size
378 disk_1315(struct bregs *regs, u8 driveid)
380 disk_ret(regs, DISK_RET_SUCCESS);
381 if (regs->dl < 0x80) {
388 // Get logical geometry from table
389 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
390 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
391 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
393 // Compute sector count seen by int13
394 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
395 regs->cx = lba >> 16;
396 regs->dx = lba & 0xffff;
397 regs->ah = 3; // hard disk accessible
401 disk_1316(struct bregs *regs, u8 driveid)
403 if (regs->dl >= 0x80) {
405 disk_ret(regs, DISK_RET_EPARAM);
408 disk_ret(regs, DISK_RET_ECHANGED);
411 // IBM/MS installation check
413 disk_1341(struct bregs *regs, u8 driveid)
415 regs->bx = 0xaa55; // install check
416 regs->cx = 0x0007; // ext disk access and edd, removable supported
417 disk_ret(regs, DISK_RET_SUCCESS);
418 regs->ah = 0x30; // EDD 3.0
421 // IBM/MS extended read
423 disk_1342(struct bregs *regs, u8 driveid)
425 extended_access(regs, driveid, CMD_READ);
428 // IBM/MS extended write
430 disk_1343(struct bregs *regs, u8 driveid)
432 extended_access(regs, driveid, CMD_WRITE);
437 disk_1344(struct bregs *regs, u8 driveid)
439 extended_access(regs, driveid, CMD_VERIFY);
442 // IBM/MS lock/unlock drive
444 disk_1345(struct bregs *regs, u8 driveid)
446 // Always success for HD
447 disk_ret(regs, DISK_RET_SUCCESS);
450 // IBM/MS eject media
452 disk_1346(struct bregs *regs, u8 driveid)
454 // Volume Not Removable
455 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
458 // IBM/MS extended seek
460 disk_1347(struct bregs *regs, u8 driveid)
462 extended_access(regs, driveid, CMD_SEEK);
465 // IBM/MS get drive parameters
467 disk_1348(struct bregs *regs, u8 driveid)
469 u16 size = GET_INT13DPT(regs, size);
471 // Buffer is too small
473 disk_ret(regs, DISK_RET_EPARAM);
479 u8 type = GET_GLOBAL(Drives.drives[driveid].type);
480 u16 npc = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
481 u16 nph = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
482 u16 npspt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
483 u64 lba = GET_GLOBAL(Drives.drives[driveid].sectors);
484 u16 blksize = GET_GLOBAL(Drives.drives[driveid].blksize);
486 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
487 , size, type, npc, nph, npspt, (u32)lba, blksize);
489 SET_INT13DPT(regs, size, 26);
490 if (type == DTYPE_ATAPI) {
491 // 0x74 = removable, media change, lockable, max values
492 SET_INT13DPT(regs, infos, 0x74);
493 SET_INT13DPT(regs, cylinders, 0xffffffff);
494 SET_INT13DPT(regs, heads, 0xffffffff);
495 SET_INT13DPT(regs, spt, 0xffffffff);
496 SET_INT13DPT(regs, sector_count, (u64)-1);
498 if (lba > (u64)npspt*nph*0x3fff) {
499 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
500 SET_INT13DPT(regs, cylinders, 0x3fff);
502 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
503 SET_INT13DPT(regs, cylinders, (u32)npc);
505 SET_INT13DPT(regs, heads, (u32)nph);
506 SET_INT13DPT(regs, spt, (u32)npspt);
507 SET_INT13DPT(regs, sector_count, lba);
509 SET_INT13DPT(regs, blksize, blksize);
511 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
512 disk_ret(regs, DISK_RET_SUCCESS);
518 u16 ebda_seg = get_ebda_seg();
519 SET_INT13DPT(regs, size, 30);
521 SET_INT13DPT(regs, dpte_segment, ebda_seg);
522 SET_INT13DPT(regs, dpte_offset
523 , offsetof(struct extended_bios_data_area_s, dpte));
526 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
527 u8 channel = ataid / 2;
528 u8 slave = ataid % 2;
529 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
530 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
531 u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
534 if (type == DTYPE_ATA) {
535 u8 translation = GET_GLOBAL(Drives.drives[driveid].translation);
536 if (translation != TRANSLATION_NONE) {
537 options |= 1<<3; // CHS translation
538 if (translation == TRANSLATION_LBA)
540 if (translation == TRANSLATION_RECHS)
545 options |= 1<<5; // removable device
546 options |= 1<<6; // atapi device
548 options |= 1<<4; // lba translation
549 if (CONFIG_ATA_PIO32)
552 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
553 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
554 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
556 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
557 SET_EBDA2(ebda_seg, dpte.irq, irq);
558 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
559 SET_EBDA2(ebda_seg, dpte.dma, 0);
560 SET_EBDA2(ebda_seg, dpte.pio, 0);
561 SET_EBDA2(ebda_seg, dpte.options, options);
562 SET_EBDA2(ebda_seg, dpte.reserved, 0);
563 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
565 u8 sum = checksum_far(
566 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
567 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
570 disk_ret(regs, DISK_RET_SUCCESS);
575 SET_INT13DPT(regs, key, 0xbedd);
576 SET_INT13DPT(regs, dpi_length, 36);
577 SET_INT13DPT(regs, reserved1, 0);
578 SET_INT13DPT(regs, reserved2, 0);
580 SET_INT13DPT(regs, host_bus[0], 'P');
581 SET_INT13DPT(regs, host_bus[1], 'C');
582 SET_INT13DPT(regs, host_bus[2], 'I');
583 SET_INT13DPT(regs, host_bus[3], 0);
585 u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
586 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
587 | (pci_bdf_to_fn(bdf) << 16));
588 SET_INT13DPT(regs, iface_path, path);
590 SET_INT13DPT(regs, iface_type[0], 'A');
591 SET_INT13DPT(regs, iface_type[1], 'T');
592 SET_INT13DPT(regs, iface_type[2], 'A');
593 SET_INT13DPT(regs, iface_type[3], 0);
594 SET_INT13DPT(regs, iface_type[4], 0);
595 SET_INT13DPT(regs, iface_type[5], 0);
596 SET_INT13DPT(regs, iface_type[6], 0);
597 SET_INT13DPT(regs, iface_type[7], 0);
599 SET_INT13DPT(regs, device_path, slave);
601 SET_INT13DPT(regs, checksum
602 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
604 disk_ret(regs, DISK_RET_SUCCESS);
607 // IBM/MS extended media change
609 disk_1349(struct bregs *regs, u8 driveid)
611 // Always success for HD
612 disk_ret(regs, DISK_RET_SUCCESS);
616 disk_134e01(struct bregs *regs, u8 driveid)
618 disk_ret(regs, DISK_RET_SUCCESS);
622 disk_134e03(struct bregs *regs, u8 driveid)
624 disk_ret(regs, DISK_RET_SUCCESS);
628 disk_134e04(struct bregs *regs, u8 driveid)
630 disk_ret(regs, DISK_RET_SUCCESS);
634 disk_134e06(struct bregs *regs, u8 driveid)
636 disk_ret(regs, DISK_RET_SUCCESS);
640 disk_134eXX(struct bregs *regs, u8 driveid)
642 disk_ret(regs, DISK_RET_EPARAM);
645 // IBM/MS set hardware configuration
647 disk_134e(struct bregs *regs, u8 driveid)
650 case 0x01: disk_134e01(regs, driveid); break;
651 case 0x03: disk_134e03(regs, driveid); break;
652 case 0x04: disk_134e04(regs, driveid); break;
653 case 0x06: disk_134e06(regs, driveid); break;
654 default: disk_134eXX(regs, driveid); break;
659 disk_13XX(struct bregs *regs, u8 driveid)
661 disk_ret(regs, DISK_RET_EPARAM);
665 disk_13(struct bregs *regs, u8 driveid)
669 // clear completion flag
670 SET_BDA(disk_interrupt_flag, 0);
673 case 0x00: disk_1300(regs, driveid); break;
674 case 0x01: disk_1301(regs, driveid); break;
675 case 0x02: disk_1302(regs, driveid); break;
676 case 0x03: disk_1303(regs, driveid); break;
677 case 0x04: disk_1304(regs, driveid); break;
678 case 0x05: disk_1305(regs, driveid); break;
679 case 0x08: disk_1308(regs, driveid); break;
680 case 0x09: disk_1309(regs, driveid); break;
681 case 0x0c: disk_130c(regs, driveid); break;
682 case 0x0d: disk_130d(regs, driveid); break;
683 case 0x10: disk_1310(regs, driveid); break;
684 case 0x11: disk_1311(regs, driveid); break;
685 case 0x14: disk_1314(regs, driveid); break;
686 case 0x15: disk_1315(regs, driveid); break;
687 case 0x16: disk_1316(regs, driveid); break;
688 case 0x41: disk_1341(regs, driveid); break;
689 case 0x42: disk_1342(regs, driveid); break;
690 case 0x43: disk_1343(regs, driveid); break;
691 case 0x44: disk_1344(regs, driveid); break;
692 case 0x45: disk_1345(regs, driveid); break;
693 case 0x46: disk_1346(regs, driveid); break;
694 case 0x47: disk_1347(regs, driveid); break;
695 case 0x48: disk_1348(regs, driveid); break;
696 case 0x49: disk_1349(regs, driveid); break;
697 case 0x4e: disk_134e(regs, driveid); break;
698 default: disk_13XX(regs, driveid); break;
703 /****************************************************************
705 ****************************************************************/
708 get_driveid(struct bregs *regs, u8 exttype, u8 extdriveoffset)
710 // basic check : device has to be defined
711 if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
714 // Get the ata channel
715 u8 driveid = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
717 // basic check : device has to be valid
718 if (driveid >= ARRAY_SIZE(Drives.drives))
725 handle_legacy_disk(struct bregs *regs, u8 extdrive)
727 if (! CONFIG_DRIVES) {
728 // XXX - support handle_1301 anyway?
729 disk_ret(regs, DISK_RET_EPARAM);
733 if (extdrive < 0x80) {
734 int driveid = get_driveid(regs, EXTTYPE_FLOPPY, extdrive);
737 floppy_13(regs, driveid);
741 if (extdrive >= 0xe0) {
742 int driveid = get_driveid(regs, EXTTYPE_CD, extdrive - 0xe0);
745 cdrom_13(regs, driveid);
749 int driveid = get_driveid(regs, EXTTYPE_HD, extdrive - 0x80);
752 disk_13(regs, driveid);
756 // XXX - support 1301/1308/1315 anyway?
757 disk_ret(regs, DISK_RET_EPARAM);
761 handle_40(struct bregs *regs)
763 debug_enter(regs, DEBUG_HDL_40);
764 handle_legacy_disk(regs, regs->dl);
767 // INT 13h Fixed Disk Services Entry Point
769 handle_13(struct bregs *regs)
771 debug_enter(regs, DEBUG_HDL_13);
772 u8 extdrive = regs->dl;
774 if (CONFIG_CDROM_EMU) {
775 if (regs->ah == 0x4b) {
779 u16 ebda_seg = get_ebda_seg();
780 if (GET_EBDA2(ebda_seg, cdemu.active)) {
781 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
782 if (extdrive == emudrive) {
786 if (extdrive < 0xe0 && ((emudrive ^ extdrive) & 0x80) == 0)
790 handle_legacy_disk(regs, extdrive);
793 // record completion in BIOS task complete flag
797 debug_isr(DEBUG_ISR_76);
798 SET_BDA(disk_interrupt_flag, 0xff);
802 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
803 struct fdpt_s OldFDPT VAR16FIXED(0xe401);