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 SET_BDA(disk_last_status, code);
28 __set_code_fail(regs, linecode, fname);
30 set_code_success(regs);
34 __disk_stub(struct bregs *regs, int lineno, const char *fname)
36 __debug_stub(regs, lineno, fname);
37 __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
40 #define DISK_STUB(regs) \
41 __disk_stub((regs), __LINE__, __func__)
43 // Execute a "disk_op_s" request - this runs on a stack in the ebda.
45 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
48 memcpy_far(GET_SEG(SS), &dop
52 dprintf(DEBUG_HDL_13, "disk_op d=%d lba=%d buf=%p count=%d cmd=%d\n"
53 , dop.driveid, (u32)dop.lba, dop.buf_fl
54 , dop.count, dop.command);
59 u8 type = GET_GLOBAL(Drives.drives[dop.driveid].type);
60 if (type == DTYPE_ATA)
61 status = process_ata_op(&dop);
62 else if (type == DTYPE_ATAPI)
63 status = process_atapi_op(&dop);
67 // Update count with total sectors transferred.
68 SET_FARVAR(op_seg, op_far->count, dop.count);
73 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
75 send_disk_op(struct disk_op_s *op)
80 return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
83 // Obtain the requested disk lba from an old-style chs request.
85 legacy_lba(struct bregs *regs, u16 lchs_seg, struct chs_s *lchs_far)
88 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
89 u16 sector = regs->cl & 0x3f;
92 if (count > 128 || count == 0 || sector == 0) {
93 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
95 disk_ret(regs, DISK_RET_EPARAM);
99 u16 nlc = GET_FARVAR(lchs_seg, lchs_far->cylinders);
100 u16 nlh = GET_FARVAR(lchs_seg, lchs_far->heads);
101 u16 nlspt = GET_FARVAR(lchs_seg, lchs_far->spt);
103 // sanity check on cyl heads, sec
104 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
105 dprintf(1, "int13_harddisk: function %02x, parameters out of"
106 " range %04x/%04x/%04x!\n"
107 , regs->ah, cylinder, head, sector);
108 disk_ret(regs, DISK_RET_EPARAM);
112 // translate lchs to lba
113 return (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
117 // Perform read/write/verify using old-style chs accesses
119 basic_access(struct bregs *regs, u8 driveid, u16 command)
121 struct disk_op_s dop;
122 dop.driveid = driveid;
123 dop.command = command;
124 int lba = legacy_lba(regs, get_global_seg(), &Drives.drives[driveid].lchs);
128 dop.count = regs->al;
129 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
131 int status = send_disk_op(&dop);
133 regs->al = dop.count;
135 disk_ret(regs, status);
138 // Perform cdemu read/verify
140 cdemu_access(struct bregs *regs, u8 driveid, u16 command)
142 struct disk_op_s dop;
143 dop.driveid = driveid;
144 dop.command = command;
145 u16 ebda_seg = get_ebda_seg();
146 int vlba = legacy_lba(
148 , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
151 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
153 u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
154 u8 *dest_far = (void*)(regs->bx+0);
156 int status = DISK_RET_SUCCESS;
160 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
161 status = send_disk_op(&dop);
164 u8 thiscount = 4 - (vlba & 3);
165 if (thiscount > count)
168 memcpy_far(regs->es, dest_far
169 , ebda_seg, cdbuf_far + (vlba & 3) * 512
171 dest_far += thiscount * 512;
172 regs->al += thiscount;
177 dop.count = count / 4;
178 dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
179 status = send_disk_op(&dop);
180 regs->al += dop.count * 4;
183 u8 thiscount = count & ~3;
185 dest_far += thiscount * 512;
186 dop.lba += thiscount / 4;
191 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
192 status = send_disk_op(&dop);
195 u8 thiscount = count;
196 memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
197 regs->al += thiscount;
201 disk_ret(regs, status);
204 // Perform read/write/verify using new-style "int13ext" accesses.
206 extended_access(struct bregs *regs, u8 driveid, u16 command)
208 struct disk_op_s dop;
209 // Get lba and check.
210 dop.lba = GET_INT13EXT(regs, lba);
211 dop.command = command;
212 dop.driveid = driveid;
213 if (dop.lba >= GET_GLOBAL(Drives.drives[driveid].sectors)) {
214 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
216 disk_ret(regs, DISK_RET_EPARAM);
220 u16 segment = GET_INT13EXT(regs, segment);
221 u16 offset = GET_INT13EXT(regs, offset);
222 dop.buf_fl = MAKE_FLATPTR(segment, offset);
223 dop.count = GET_INT13EXT(regs, count);
225 int status = send_disk_op(&dop);
227 SET_INT13EXT(regs, count, dop.count);
229 disk_ret(regs, status);
233 /****************************************************************
234 * Hard Drive functions
235 ****************************************************************/
237 // disk controller reset
239 disk_1300(struct bregs *regs, u8 driveid)
241 struct disk_op_s dop;
242 dop.driveid = driveid;
243 dop.command = CMD_RESET;
244 int status = send_disk_op(&dop);
245 disk_ret(regs, status);
250 disk_1301(struct bregs *regs, u8 driveid)
252 u8 v = GET_BDA(disk_last_status);
255 // XXX - clear disk_last_status?
260 disk_1302(struct bregs *regs, u8 driveid)
262 basic_access(regs, driveid, CMD_READ);
265 // write disk sectors
267 disk_1303(struct bregs *regs, u8 driveid)
269 basic_access(regs, driveid, CMD_WRITE);
272 // verify disk sectors
274 disk_1304(struct bregs *regs, u8 driveid)
276 basic_access(regs, driveid, CMD_VERIFY);
282 disk_1305(struct bregs *regs, u8 driveid)
287 // read disk drive parameters
289 disk_1308(struct bregs *regs, u8 driveid)
291 // Get logical geometry from table
292 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
293 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
294 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
295 u16 count = GET_BDA(hdcount);
297 nlc = nlc - 2; /* 0 based , last sector not used */
299 regs->ch = nlc & 0xff;
300 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
302 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
304 // FIXME should set ES & DI
305 disk_ret(regs, DISK_RET_SUCCESS);
308 // initialize drive parameters
310 disk_1309(struct bregs *regs, u8 driveid)
315 // seek to specified cylinder
317 disk_130c(struct bregs *regs, u8 driveid)
322 // alternate disk reset
324 disk_130d(struct bregs *regs, u8 driveid)
331 disk_1310(struct bregs *regs, u8 driveid)
333 // should look at 40:8E also???
335 struct disk_op_s dop;
336 dop.driveid = driveid;
337 dop.command = CMD_ISREADY;
338 int status = send_disk_op(&dop);
339 disk_ret(regs, status);
344 disk_1311(struct bregs *regs, u8 driveid)
349 // controller internal diagnostic
351 disk_1314(struct bregs *regs, u8 driveid)
356 // read disk drive size
358 disk_1315(struct bregs *regs, u8 driveid)
360 // Get logical geometry from table
361 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
362 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
363 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
365 // Compute sector count seen by int13
366 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
367 regs->cx = lba >> 16;
368 regs->dx = lba & 0xffff;
370 disk_ret(regs, DISK_RET_SUCCESS);
371 regs->ah = 3; // hard disk accessible
374 // IBM/MS installation check
376 disk_1341(struct bregs *regs, u8 driveid)
378 regs->bx = 0xaa55; // install check
379 regs->cx = 0x0007; // ext disk access and edd, removable supported
380 disk_ret(regs, DISK_RET_SUCCESS);
381 regs->ah = 0x30; // EDD 3.0
384 // IBM/MS extended read
386 disk_1342(struct bregs *regs, u8 driveid)
388 extended_access(regs, driveid, CMD_READ);
391 // IBM/MS extended write
393 disk_1343(struct bregs *regs, u8 driveid)
395 extended_access(regs, driveid, CMD_WRITE);
400 disk_1344(struct bregs *regs, u8 driveid)
402 extended_access(regs, driveid, CMD_VERIFY);
405 // IBM/MS lock/unlock drive
407 disk_1345(struct bregs *regs, u8 driveid)
409 // Always success for HD
410 disk_ret(regs, DISK_RET_SUCCESS);
413 // IBM/MS eject media
415 disk_1346(struct bregs *regs, u8 driveid)
417 // Volume Not Removable
418 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
421 // IBM/MS extended seek
423 disk_1347(struct bregs *regs, u8 driveid)
425 extended_access(regs, driveid, CMD_SEEK);
428 // IBM/MS get drive parameters
430 disk_1348(struct bregs *regs, u8 driveid)
432 u16 size = GET_INT13DPT(regs, size);
434 // Buffer is too small
436 disk_ret(regs, DISK_RET_EPARAM);
442 u8 type = GET_GLOBAL(Drives.drives[driveid].type);
443 u16 npc = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
444 u16 nph = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
445 u16 npspt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
446 u64 lba = GET_GLOBAL(Drives.drives[driveid].sectors);
447 u16 blksize = GET_GLOBAL(Drives.drives[driveid].blksize);
449 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
450 , size, type, npc, nph, npspt, (u32)lba, blksize);
452 SET_INT13DPT(regs, size, 26);
453 if (type == DTYPE_ATAPI) {
454 // 0x74 = removable, media change, lockable, max values
455 SET_INT13DPT(regs, infos, 0x74);
456 SET_INT13DPT(regs, cylinders, 0xffffffff);
457 SET_INT13DPT(regs, heads, 0xffffffff);
458 SET_INT13DPT(regs, spt, 0xffffffff);
459 SET_INT13DPT(regs, sector_count, (u64)-1);
461 if (lba > (u64)npspt*nph*0x3fff) {
462 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
463 SET_INT13DPT(regs, cylinders, 0x3fff);
465 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
466 SET_INT13DPT(regs, cylinders, (u32)npc);
468 SET_INT13DPT(regs, heads, (u32)nph);
469 SET_INT13DPT(regs, spt, (u32)npspt);
470 SET_INT13DPT(regs, sector_count, lba);
472 SET_INT13DPT(regs, blksize, blksize);
474 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
475 disk_ret(regs, DISK_RET_SUCCESS);
481 u16 ebda_seg = get_ebda_seg();
482 SET_INT13DPT(regs, size, 30);
484 SET_INT13DPT(regs, dpte_segment, ebda_seg);
485 SET_INT13DPT(regs, dpte_offset
486 , offsetof(struct extended_bios_data_area_s, dpte));
489 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
490 u8 channel = ataid / 2;
491 u8 slave = ataid % 2;
492 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
493 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
494 u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
497 if (type == DTYPE_ATA) {
498 u8 translation = GET_GLOBAL(Drives.drives[driveid].translation);
499 if (translation != TRANSLATION_NONE) {
500 options |= 1<<3; // CHS translation
501 if (translation == TRANSLATION_LBA)
503 if (translation == TRANSLATION_RECHS)
508 options |= 1<<5; // removable device
509 options |= 1<<6; // atapi device
511 options |= 1<<4; // lba translation
512 if (CONFIG_ATA_PIO32)
515 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
516 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
517 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
519 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
520 SET_EBDA2(ebda_seg, dpte.irq, irq);
521 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
522 SET_EBDA2(ebda_seg, dpte.dma, 0);
523 SET_EBDA2(ebda_seg, dpte.pio, 0);
524 SET_EBDA2(ebda_seg, dpte.options, options);
525 SET_EBDA2(ebda_seg, dpte.reserved, 0);
526 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
528 u8 sum = checksum_far(
529 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
530 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
533 disk_ret(regs, DISK_RET_SUCCESS);
538 SET_INT13DPT(regs, key, 0xbedd);
539 SET_INT13DPT(regs, dpi_length, 36);
540 SET_INT13DPT(regs, reserved1, 0);
541 SET_INT13DPT(regs, reserved2, 0);
543 SET_INT13DPT(regs, host_bus[0], 'P');
544 SET_INT13DPT(regs, host_bus[1], 'C');
545 SET_INT13DPT(regs, host_bus[2], 'I');
546 SET_INT13DPT(regs, host_bus[3], 0);
548 u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
549 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
550 | (pci_bdf_to_fn(bdf) << 16));
551 SET_INT13DPT(regs, iface_path, path);
553 SET_INT13DPT(regs, iface_type[0], 'A');
554 SET_INT13DPT(regs, iface_type[1], 'T');
555 SET_INT13DPT(regs, iface_type[2], 'A');
556 SET_INT13DPT(regs, iface_type[3], 0);
557 SET_INT13DPT(regs, iface_type[4], 0);
558 SET_INT13DPT(regs, iface_type[5], 0);
559 SET_INT13DPT(regs, iface_type[6], 0);
560 SET_INT13DPT(regs, iface_type[7], 0);
562 SET_INT13DPT(regs, device_path, slave);
564 SET_INT13DPT(regs, checksum
565 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
567 disk_ret(regs, DISK_RET_SUCCESS);
570 // IBM/MS extended media change
572 disk_1349(struct bregs *regs, u8 driveid)
574 // Always success for HD
575 disk_ret(regs, DISK_RET_SUCCESS);
579 disk_134e01(struct bregs *regs, u8 driveid)
581 disk_ret(regs, DISK_RET_SUCCESS);
585 disk_134e03(struct bregs *regs, u8 driveid)
587 disk_ret(regs, DISK_RET_SUCCESS);
591 disk_134e04(struct bregs *regs, u8 driveid)
593 disk_ret(regs, DISK_RET_SUCCESS);
597 disk_134e06(struct bregs *regs, u8 driveid)
599 disk_ret(regs, DISK_RET_SUCCESS);
603 disk_134eXX(struct bregs *regs, u8 driveid)
605 disk_ret(regs, DISK_RET_EPARAM);
608 // IBM/MS set hardware configuration
610 disk_134e(struct bregs *regs, u8 driveid)
613 case 0x01: disk_134e01(regs, driveid); break;
614 case 0x03: disk_134e03(regs, driveid); break;
615 case 0x04: disk_134e04(regs, driveid); break;
616 case 0x06: disk_134e06(regs, driveid); break;
617 default: disk_134eXX(regs, driveid); break;
622 disk_13XX(struct bregs *regs, u8 driveid)
624 disk_ret(regs, DISK_RET_EPARAM);
628 disk_13(struct bregs *regs, u8 driveid)
632 // clear completion flag
633 SET_BDA(disk_interrupt_flag, 0);
636 case 0x00: disk_1300(regs, driveid); break;
637 case 0x01: disk_1301(regs, driveid); break;
638 case 0x02: disk_1302(regs, driveid); break;
639 case 0x03: disk_1303(regs, driveid); break;
640 case 0x04: disk_1304(regs, driveid); break;
641 case 0x05: disk_1305(regs, driveid); break;
642 case 0x08: disk_1308(regs, driveid); break;
643 case 0x09: disk_1309(regs, driveid); break;
644 case 0x0c: disk_130c(regs, driveid); break;
645 case 0x0d: disk_130d(regs, driveid); break;
646 case 0x10: disk_1310(regs, driveid); break;
647 case 0x11: disk_1311(regs, driveid); break;
648 case 0x14: disk_1314(regs, driveid); break;
649 case 0x15: disk_1315(regs, driveid); break;
650 case 0x41: disk_1341(regs, driveid); break;
651 case 0x42: disk_1342(regs, driveid); break;
652 case 0x43: disk_1343(regs, driveid); break;
653 case 0x44: disk_1344(regs, driveid); break;
654 case 0x45: disk_1345(regs, driveid); break;
655 case 0x46: disk_1346(regs, driveid); break;
656 case 0x47: disk_1347(regs, driveid); break;
657 case 0x48: disk_1348(regs, driveid); break;
658 case 0x49: disk_1349(regs, driveid); break;
659 case 0x4e: disk_134e(regs, driveid); break;
660 default: disk_13XX(regs, driveid); break;
665 /****************************************************************
667 ****************************************************************/
670 get_driveid(struct bregs *regs, u8 exttype, u8 extdriveoffset)
672 // basic check : device has to be defined
673 if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
676 // Get the ata channel
677 u8 driveid = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
679 // basic check : device has to be valid
680 if (driveid >= ARRAY_SIZE(Drives.drives))
687 handle_legacy_disk(struct bregs *regs, u8 extdrive)
689 if (! CONFIG_DRIVES) {
690 // XXX - support handle_1301 anyway?
691 disk_ret(regs, DISK_RET_EPARAM);
695 if (extdrive < 0x80) {
696 int driveid = get_driveid(regs, EXTTYPE_FLOPPY, extdrive);
699 floppy_13(regs, driveid);
703 if (extdrive >= 0xe0) {
704 int driveid = get_driveid(regs, EXTTYPE_CD, extdrive - 0xe0);
707 cdrom_13(regs, driveid);
711 int driveid = get_driveid(regs, EXTTYPE_HD, extdrive - 0x80);
714 disk_13(regs, driveid);
718 // XXX - support 1301/1308/1315 anyway?
719 disk_ret(regs, DISK_RET_EPARAM);
723 handle_40(struct bregs *regs)
725 debug_enter(regs, DEBUG_HDL_40);
726 handle_legacy_disk(regs, regs->dl);
729 // INT 13h Fixed Disk Services Entry Point
731 handle_13(struct bregs *regs)
733 debug_enter(regs, DEBUG_HDL_13);
734 u8 extdrive = regs->dl;
736 if (CONFIG_CDROM_EMU) {
737 if (regs->ah == 0x4b) {
741 u16 ebda_seg = get_ebda_seg();
742 if (GET_EBDA2(ebda_seg, cdemu.active)) {
743 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
744 if (extdrive == emudrive) {
748 if (extdrive < 0xe0 && ((emudrive ^ extdrive) & 0x80) == 0)
752 handle_legacy_disk(regs, extdrive);
755 // record completion in BIOS task complete flag
759 debug_isr(DEBUG_ISR_76);
760 SET_BDA(disk_interrupt_flag, 0xff);
764 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
765 struct fdpt_s OldFDPT VAR16FIXED(0xe401);