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)
26 if (regs->dl < EXTSTART_HD)
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__)
47 fillLCHS(u8 driveid, u16 *nlc, u16 *nlh, u16 *nlspt)
49 if (CONFIG_CDROM_EMU && driveid == GET_GLOBAL(cdemu_driveid)) {
50 // Emulated drive - get info from ebda. (It's not possible to
51 // populate the geometry directly in the driveid because the
52 // geometry is only known after the bios segment is made
54 u16 ebda_seg = get_ebda_seg();
55 *nlc = GET_EBDA2(ebda_seg, cdemu.lchs.cylinders);
56 *nlh = GET_EBDA2(ebda_seg, cdemu.lchs.heads);
57 *nlspt = GET_EBDA2(ebda_seg, cdemu.lchs.spt);
60 *nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
61 *nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
62 *nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
65 // Perform read/write/verify using old-style chs accesses
67 basic_access(struct bregs *regs, u8 driveid, u16 command)
70 dop.driveid = driveid;
71 dop.command = command;
74 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
75 u16 sector = regs->cl & 0x3f;
78 if (count > 128 || count == 0 || sector == 0) {
79 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
81 disk_ret(regs, DISK_RET_EPARAM);
87 fillLCHS(driveid, &nlc, &nlh, &nlspt);
89 // sanity check on cyl heads, sec
90 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
91 dprintf(1, "int13_harddisk: function %02x, parameters out of"
92 " range %04x/%04x/%04x!\n"
93 , regs->ah, cylinder, head, sector);
94 disk_ret(regs, DISK_RET_EPARAM);
98 // translate lchs to lba
99 dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
102 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
104 int status = send_disk_op(&dop);
106 regs->al = dop.count;
108 disk_ret(regs, status);
111 // Perform read/write/verify using new-style "int13ext" accesses.
113 extended_access(struct bregs *regs, u8 driveid, u16 command)
115 struct disk_op_s dop;
116 // Get lba and check.
117 dop.lba = GET_INT13EXT(regs, lba);
118 dop.command = command;
119 dop.driveid = driveid;
120 if (dop.lba >= GET_GLOBAL(Drives.drives[driveid].sectors)) {
121 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
123 disk_ret(regs, DISK_RET_EPARAM);
127 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_INT13EXT(regs, data));
128 dop.count = GET_INT13EXT(regs, count);
130 int status = send_disk_op(&dop);
132 SET_INT13EXT(regs, count, dop.count);
134 disk_ret(regs, status);
138 /****************************************************************
139 * Hard Drive functions
140 ****************************************************************/
142 // disk controller reset
144 disk_1300(struct bregs *regs, u8 driveid)
146 struct disk_op_s dop;
147 dop.driveid = driveid;
148 dop.command = CMD_RESET;
149 int status = send_disk_op(&dop);
150 disk_ret(regs, status);
155 disk_1301(struct bregs *regs, u8 driveid)
158 if (regs->dl < EXTSTART_HD)
160 v = GET_BDA(floppy_last_status);
162 v = GET_BDA(disk_last_status);
165 // XXX - clear disk_last_status?
170 disk_1302(struct bregs *regs, u8 driveid)
172 basic_access(regs, driveid, CMD_READ);
175 // write disk sectors
177 disk_1303(struct bregs *regs, u8 driveid)
179 basic_access(regs, driveid, CMD_WRITE);
182 // verify disk sectors
184 disk_1304(struct bregs *regs, u8 driveid)
186 basic_access(regs, driveid, CMD_VERIFY);
192 disk_1305(struct bregs *regs, u8 driveid)
197 fillLCHS(driveid, &nlc, &nlh, &nlspt);
199 u8 num_sectors = regs->al;
202 if (head >= nlh || num_sectors == 0 || num_sectors > nlspt) {
203 disk_ret(regs, DISK_RET_EPARAM);
207 struct disk_op_s dop;
208 dop.driveid = driveid;
209 dop.command = CMD_FORMAT;
211 dop.count = num_sectors;
212 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
213 int status = send_disk_op(&dop);
214 disk_ret(regs, status);
217 // read disk drive parameters
219 disk_1308(struct bregs *regs, u8 driveid)
221 u16 ebda_seg = get_ebda_seg();
222 // Get logical geometry from table
224 fillLCHS(driveid, &nlc, &nlh, &nlspt);
228 if (regs->dl < EXTSTART_HD) {
230 count = GET_GLOBAL(Drives.floppycount);
232 if (CONFIG_CDROM_EMU && driveid == GET_GLOBAL(cdemu_driveid))
233 regs->bx = GET_EBDA2(ebda_seg, cdemu.media) * 2;
235 regs->bx = GET_GLOBAL(Drives.drives[driveid].floppy_type);
237 // set es & di to point to 11 byte diskette param table in ROM
239 regs->di = (u32)&diskette_param_table2;
240 } else if (regs->dl < EXTSTART_CD) {
242 count = GET_BDA(hdcount);
243 nlc--; // last sector reserved
245 // Not supported on CDROM
246 disk_ret(regs, DISK_RET_EPARAM);
250 if (CONFIG_CDROM_EMU && GET_EBDA2(ebda_seg, cdemu.active)) {
251 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
252 if (((emudrive ^ regs->dl) & 0x80) == 0)
253 // Note extra drive due to emulation.
255 if (regs->dl < EXTSTART_HD && count > 2)
256 // Max of two floppy drives.
261 regs->ch = nlc & 0xff;
262 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
265 disk_ret(regs, DISK_RET_SUCCESS);
269 // initialize drive parameters
271 disk_1309(struct bregs *regs, u8 driveid)
276 // seek to specified cylinder
278 disk_130c(struct bregs *regs, u8 driveid)
283 // alternate disk reset
285 disk_130d(struct bregs *regs, u8 driveid)
292 disk_1310(struct bregs *regs, u8 driveid)
294 // should look at 40:8E also???
296 struct disk_op_s dop;
297 dop.driveid = driveid;
298 dop.command = CMD_ISREADY;
299 int status = send_disk_op(&dop);
300 disk_ret(regs, status);
305 disk_1311(struct bregs *regs, u8 driveid)
310 // controller internal diagnostic
312 disk_1314(struct bregs *regs, u8 driveid)
317 // read disk drive size
319 disk_1315(struct bregs *regs, u8 driveid)
321 disk_ret(regs, DISK_RET_SUCCESS);
322 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
329 // Get logical geometry from table
331 fillLCHS(driveid, &nlc, &nlh, &nlspt);
333 // Compute sector count seen by int13
334 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
335 regs->cx = lba >> 16;
336 regs->dx = lba & 0xffff;
337 regs->ah = 3; // hard disk accessible
341 disk_1316(struct bregs *regs, u8 driveid)
343 if (regs->dl >= EXTSTART_HD) {
345 disk_ret(regs, DISK_RET_EPARAM);
348 disk_ret(regs, DISK_RET_ECHANGED);
351 // IBM/MS installation check
353 disk_1341(struct bregs *regs, u8 driveid)
355 regs->bx = 0xaa55; // install check
356 regs->cx = 0x0007; // ext disk access and edd, removable supported
357 disk_ret(regs, DISK_RET_SUCCESS);
358 regs->ah = 0x30; // EDD 3.0
361 // IBM/MS extended read
363 disk_1342(struct bregs *regs, u8 driveid)
365 extended_access(regs, driveid, CMD_READ);
368 // IBM/MS extended write
370 disk_1343(struct bregs *regs, u8 driveid)
372 extended_access(regs, driveid, CMD_WRITE);
377 disk_1344(struct bregs *regs, u8 driveid)
379 extended_access(regs, driveid, CMD_VERIFY);
384 disk_134500(struct bregs *regs, u8 driveid)
386 u16 ebda_seg = get_ebda_seg();
387 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[driveid]);
390 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
393 SET_EBDA2(ebda_seg, cdrom_locks[driveid], locks + 1);
395 disk_ret(regs, DISK_RET_SUCCESS);
400 disk_134501(struct bregs *regs, u8 driveid)
402 u16 ebda_seg = get_ebda_seg();
403 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[driveid]);
406 disk_ret(regs, DISK_RET_ENOTLOCKED);
410 SET_EBDA2(ebda_seg, cdrom_locks[driveid], locks);
411 regs->al = (locks ? 1 : 0);
412 disk_ret(regs, DISK_RET_SUCCESS);
417 disk_134502(struct bregs *regs, u8 driveid)
419 u8 locks = GET_EBDA(cdrom_locks[driveid]);
420 regs->al = (locks ? 1 : 0);
421 disk_ret(regs, DISK_RET_SUCCESS);
425 disk_1345XX(struct bregs *regs, u8 driveid)
427 disk_ret(regs, DISK_RET_EPARAM);
430 // IBM/MS lock/unlock drive
432 disk_1345(struct bregs *regs, u8 driveid)
434 if (regs->dl < EXTSTART_CD) {
435 // Always success for HD
436 disk_ret(regs, DISK_RET_SUCCESS);
441 case 0x00: disk_134500(regs, driveid); break;
442 case 0x01: disk_134501(regs, driveid); break;
443 case 0x02: disk_134502(regs, driveid); break;
444 default: disk_1345XX(regs, driveid); break;
448 // IBM/MS eject media
450 disk_1346(struct bregs *regs, u8 driveid)
452 if (regs->dl < EXTSTART_CD) {
453 // Volume Not Removable
454 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
458 u8 locks = GET_EBDA(cdrom_locks[driveid]);
460 disk_ret(regs, DISK_RET_ELOCKED);
464 // FIXME should handle 0x31 no media in device
465 // FIXME should handle 0xb5 valid request failed
467 // Call removable media eject
469 memset(&br, 0, sizeof(br));
471 call16_int(0x15, &br);
473 if (br.ah || br.flags & F_CF) {
474 disk_ret(regs, DISK_RET_ELOCKED);
477 disk_ret(regs, DISK_RET_SUCCESS);
480 // IBM/MS extended seek
482 disk_1347(struct bregs *regs, u8 driveid)
484 extended_access(regs, driveid, CMD_SEEK);
487 // IBM/MS get drive parameters
489 disk_1348(struct bregs *regs, u8 driveid)
491 u16 size = GET_INT13DPT(regs, size);
493 // Buffer is too small
495 disk_ret(regs, DISK_RET_EPARAM);
501 u8 type = GET_GLOBAL(Drives.drives[driveid].type);
502 u16 npc = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
503 u16 nph = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
504 u16 npspt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
505 u64 lba = GET_GLOBAL(Drives.drives[driveid].sectors);
506 u16 blksize = GET_GLOBAL(Drives.drives[driveid].blksize);
508 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
509 , size, type, npc, nph, npspt, (u32)lba, blksize);
511 SET_INT13DPT(regs, size, 26);
512 if (type == DTYPE_ATAPI) {
513 // 0x74 = removable, media change, lockable, max values
514 SET_INT13DPT(regs, infos, 0x74);
515 SET_INT13DPT(regs, cylinders, 0xffffffff);
516 SET_INT13DPT(regs, heads, 0xffffffff);
517 SET_INT13DPT(regs, spt, 0xffffffff);
518 SET_INT13DPT(regs, sector_count, (u64)-1);
520 if (lba > (u64)npspt*nph*0x3fff) {
521 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
522 SET_INT13DPT(regs, cylinders, 0x3fff);
524 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
525 SET_INT13DPT(regs, cylinders, (u32)npc);
527 SET_INT13DPT(regs, heads, (u32)nph);
528 SET_INT13DPT(regs, spt, (u32)npspt);
529 SET_INT13DPT(regs, sector_count, lba);
531 SET_INT13DPT(regs, blksize, blksize);
533 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
534 disk_ret(regs, DISK_RET_SUCCESS);
540 u16 ebda_seg = get_ebda_seg();
541 SET_INT13DPT(regs, size, 30);
543 SET_INT13DPT(regs, dpte_segment, ebda_seg);
544 SET_INT13DPT(regs, dpte_offset
545 , offsetof(struct extended_bios_data_area_s, dpte));
548 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
549 u8 channel = ataid / 2;
550 u8 slave = ataid % 2;
551 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
552 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
553 u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
556 if (type == DTYPE_ATA) {
557 u8 translation = GET_GLOBAL(Drives.drives[driveid].translation);
558 if (translation != TRANSLATION_NONE) {
559 options |= 1<<3; // CHS translation
560 if (translation == TRANSLATION_LBA)
562 if (translation == TRANSLATION_RECHS)
567 options |= 1<<5; // removable device
568 options |= 1<<6; // atapi device
570 options |= 1<<4; // lba translation
571 if (CONFIG_ATA_PIO32)
574 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
575 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
576 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
578 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
579 SET_EBDA2(ebda_seg, dpte.irq, irq);
580 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
581 SET_EBDA2(ebda_seg, dpte.dma, 0);
582 SET_EBDA2(ebda_seg, dpte.pio, 0);
583 SET_EBDA2(ebda_seg, dpte.options, options);
584 SET_EBDA2(ebda_seg, dpte.reserved, 0);
585 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
587 u8 sum = checksum_far(
588 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
589 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
592 disk_ret(regs, DISK_RET_SUCCESS);
597 SET_INT13DPT(regs, key, 0xbedd);
598 SET_INT13DPT(regs, dpi_length, 36);
599 SET_INT13DPT(regs, reserved1, 0);
600 SET_INT13DPT(regs, reserved2, 0);
602 SET_INT13DPT(regs, host_bus[0], 'P');
603 SET_INT13DPT(regs, host_bus[1], 'C');
604 SET_INT13DPT(regs, host_bus[2], 'I');
605 SET_INT13DPT(regs, host_bus[3], 0);
607 u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
608 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
609 | (pci_bdf_to_fn(bdf) << 16));
610 SET_INT13DPT(regs, iface_path, path);
612 SET_INT13DPT(regs, iface_type[0], 'A');
613 SET_INT13DPT(regs, iface_type[1], 'T');
614 SET_INT13DPT(regs, iface_type[2], 'A');
615 SET_INT13DPT(regs, iface_type[3], 0);
616 SET_INT13DPT(regs, iface_type[4], 0);
617 SET_INT13DPT(regs, iface_type[5], 0);
618 SET_INT13DPT(regs, iface_type[6], 0);
619 SET_INT13DPT(regs, iface_type[7], 0);
621 SET_INT13DPT(regs, device_path, slave);
623 SET_INT13DPT(regs, checksum
624 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
626 disk_ret(regs, DISK_RET_SUCCESS);
629 // IBM/MS extended media change
631 disk_1349(struct bregs *regs, u8 driveid)
633 if (regs->dl < EXTSTART_CD) {
634 // Always success for HD
635 disk_ret(regs, DISK_RET_SUCCESS);
639 // always send changed ??
640 regs->ah = DISK_RET_ECHANGED;
644 disk_134e01(struct bregs *regs, u8 driveid)
646 disk_ret(regs, DISK_RET_SUCCESS);
650 disk_134e03(struct bregs *regs, u8 driveid)
652 disk_ret(regs, DISK_RET_SUCCESS);
656 disk_134e04(struct bregs *regs, u8 driveid)
658 disk_ret(regs, DISK_RET_SUCCESS);
662 disk_134e06(struct bregs *regs, u8 driveid)
664 disk_ret(regs, DISK_RET_SUCCESS);
668 disk_134eXX(struct bregs *regs, u8 driveid)
670 disk_ret(regs, DISK_RET_EPARAM);
673 // IBM/MS set hardware configuration
675 disk_134e(struct bregs *regs, u8 driveid)
678 case 0x01: disk_134e01(regs, driveid); break;
679 case 0x03: disk_134e03(regs, driveid); break;
680 case 0x04: disk_134e04(regs, driveid); break;
681 case 0x06: disk_134e06(regs, driveid); break;
682 default: disk_134eXX(regs, driveid); break;
687 disk_13XX(struct bregs *regs, u8 driveid)
689 disk_ret(regs, DISK_RET_EPARAM);
693 disk_13(struct bregs *regs, u8 driveid)
697 // clear completion flag
698 SET_BDA(disk_interrupt_flag, 0);
701 case 0x00: disk_1300(regs, driveid); break;
702 case 0x01: disk_1301(regs, driveid); break;
703 case 0x02: disk_1302(regs, driveid); break;
704 case 0x03: disk_1303(regs, driveid); break;
705 case 0x04: disk_1304(regs, driveid); break;
706 case 0x05: disk_1305(regs, driveid); break;
707 case 0x08: disk_1308(regs, driveid); break;
708 case 0x09: disk_1309(regs, driveid); break;
709 case 0x0c: disk_130c(regs, driveid); break;
710 case 0x0d: disk_130d(regs, driveid); break;
711 case 0x10: disk_1310(regs, driveid); break;
712 case 0x11: disk_1311(regs, driveid); break;
713 case 0x14: disk_1314(regs, driveid); break;
714 case 0x15: disk_1315(regs, driveid); break;
715 case 0x16: disk_1316(regs, driveid); break;
716 case 0x41: disk_1341(regs, driveid); break;
717 case 0x42: disk_1342(regs, driveid); break;
718 case 0x43: disk_1343(regs, driveid); break;
719 case 0x44: disk_1344(regs, driveid); break;
720 case 0x45: disk_1345(regs, driveid); break;
721 case 0x46: disk_1346(regs, driveid); break;
722 case 0x47: disk_1347(regs, driveid); break;
723 case 0x48: disk_1348(regs, driveid); break;
724 case 0x49: disk_1349(regs, driveid); break;
725 case 0x4e: disk_134e(regs, driveid); break;
726 default: disk_13XX(regs, driveid); break;
731 floppy_13(struct bregs *regs, u8 driveid)
733 // Only limited commands are supported on floppies.
744 disk_13(regs, driveid);
746 default: disk_13XX(regs, driveid); break;
751 /****************************************************************
753 ****************************************************************/
756 get_driveid(struct bregs *regs, u8 exttype, u8 extdriveoffset)
758 // basic check : device has to be defined
759 if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
762 // Get the ata channel
763 u8 driveid = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
765 // basic check : device has to be valid
766 if (driveid >= ARRAY_SIZE(Drives.drives))
773 handle_legacy_disk(struct bregs *regs, u8 extdrive)
775 if (! CONFIG_DRIVES) {
776 // XXX - support handle_1301 anyway?
777 disk_ret(regs, DISK_RET_EPARAM);
781 if (extdrive < EXTSTART_HD) {
782 int driveid = get_driveid(regs, EXTTYPE_FLOPPY, extdrive);
785 floppy_13(regs, driveid);
790 if (extdrive >= EXTSTART_CD)
791 driveid = get_driveid(regs, EXTTYPE_CD, extdrive - EXTSTART_CD);
793 driveid = get_driveid(regs, EXTTYPE_HD, extdrive - EXTSTART_HD);
796 disk_13(regs, driveid);
800 // XXX - support 1301/1308/1315 anyway?
801 disk_ret(regs, DISK_RET_EPARAM);
805 handle_40(struct bregs *regs)
807 debug_enter(regs, DEBUG_HDL_40);
808 handle_legacy_disk(regs, regs->dl);
811 // INT 13h Fixed Disk Services Entry Point
813 handle_13(struct bregs *regs)
815 debug_enter(regs, DEBUG_HDL_13);
816 u8 extdrive = regs->dl;
818 if (CONFIG_CDROM_EMU) {
819 if (regs->ah == 0x4b) {
823 u16 ebda_seg = get_ebda_seg();
824 if (GET_EBDA2(ebda_seg, cdemu.active)) {
825 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
826 if (extdrive == emudrive) {
827 int cdemuid = GET_GLOBAL(cdemu_driveid);
828 if (regs->ah > 0x16) {
829 // Only old-style commands supported.
830 disk_13XX(regs, cdemuid);
833 disk_13(regs, cdemuid);
836 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
840 handle_legacy_disk(regs, extdrive);
843 // record completion in BIOS task complete flag
847 debug_isr(DEBUG_ISR_76);
848 SET_BDA(disk_interrupt_flag, 0xff);
852 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
853 struct fdpt_s OldFDPT VAR16FIXED(0xe401);