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);
71 dprintf(1, "disk_op cmd %d error %d!\n", dop.command, status);
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 device, u16 command)
124 struct disk_op_s dop;
125 dop.driveid = device;
126 dop.command = command;
127 int lba = legacy_lba(regs, get_global_seg(), &Drives.drives[device].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;
139 disk_ret(regs, DISK_RET_EBADTRACK);
142 disk_ret(regs, DISK_RET_SUCCESS);
145 // Perform cdemu read/verify
147 cdemu_access(struct bregs *regs, u8 device, u16 command)
149 struct disk_op_s dop;
150 dop.driveid = device;
151 dop.command = command;
152 u16 ebda_seg = get_ebda_seg();
153 int vlba = legacy_lba(
155 , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
158 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
160 u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
161 u8 *dest_far = (void*)(regs->bx+0);
166 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
167 int status = send_disk_op(&dop);
170 u8 thiscount = 4 - (vlba & 3);
171 if (thiscount > count)
174 memcpy_far(regs->es, dest_far
175 , ebda_seg, cdbuf_far + (vlba & 3) * 512
177 dest_far += thiscount * 512;
178 regs->al += thiscount;
183 dop.count = count / 4;
184 dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
185 int status = send_disk_op(&dop);
186 regs->al += dop.count * 4;
189 u8 thiscount = count & ~3;
191 dest_far += thiscount * 512;
192 dop.lba += thiscount / 4;
197 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
198 int status = send_disk_op(&dop);
201 u8 thiscount = count;
202 memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
203 regs->al += thiscount;
206 disk_ret(regs, DISK_RET_SUCCESS);
209 disk_ret(regs, DISK_RET_EBADTRACK);
212 // Perform read/write/verify using new-style "int13ext" accesses.
214 extended_access(struct bregs *regs, u8 device, u16 command)
216 struct disk_op_s dop;
217 // Get lba and check.
218 dop.lba = GET_INT13EXT(regs, lba);
219 dop.command = command;
220 dop.driveid = device;
221 if (dop.lba >= GET_GLOBAL(Drives.drives[device].sectors)) {
222 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
224 disk_ret(regs, DISK_RET_EPARAM);
228 u16 segment = GET_INT13EXT(regs, segment);
229 u16 offset = GET_INT13EXT(regs, offset);
230 dop.buf_fl = MAKE_FLATPTR(segment, offset);
231 dop.count = GET_INT13EXT(regs, count);
233 int status = send_disk_op(&dop);
235 SET_INT13EXT(regs, count, dop.count);
238 disk_ret(regs, DISK_RET_EBADTRACK);
241 disk_ret(regs, DISK_RET_SUCCESS);
245 /****************************************************************
246 * Hard Drive functions
247 ****************************************************************/
249 // disk controller reset
251 disk_1300(struct bregs *regs, u8 device)
253 struct disk_op_s dop;
254 dop.driveid = device;
255 dop.command = CMD_RESET;
261 disk_1301(struct bregs *regs, u8 device)
263 u8 v = GET_BDA(disk_last_status);
266 // XXX - clear disk_last_status?
271 disk_1302(struct bregs *regs, u8 device)
273 basic_access(regs, device, CMD_READ);
276 // write disk sectors
278 disk_1303(struct bregs *regs, u8 device)
280 basic_access(regs, device, CMD_WRITE);
283 // verify disk sectors
285 disk_1304(struct bregs *regs, u8 device)
287 basic_access(regs, device, CMD_VERIFY);
293 disk_1305(struct bregs *regs, u8 device)
298 // read disk drive parameters
300 disk_1308(struct bregs *regs, u8 device)
302 // Get logical geometry from table
303 u16 nlc = GET_GLOBAL(Drives.drives[device].lchs.cylinders);
304 u16 nlh = GET_GLOBAL(Drives.drives[device].lchs.heads);
305 u16 nlspt = GET_GLOBAL(Drives.drives[device].lchs.spt);
306 u16 count = GET_BDA(hdcount);
308 nlc = nlc - 2; /* 0 based , last sector not used */
310 regs->ch = nlc & 0xff;
311 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
313 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
315 // FIXME should set ES & DI
316 disk_ret(regs, DISK_RET_SUCCESS);
319 // initialize drive parameters
321 disk_1309(struct bregs *regs, u8 device)
326 // seek to specified cylinder
328 disk_130c(struct bregs *regs, u8 device)
333 // alternate disk reset
335 disk_130d(struct bregs *regs, u8 device)
342 disk_1310(struct bregs *regs, u8 device)
344 // should look at 40:8E also???
346 struct disk_op_s dop;
347 dop.driveid = device;
348 dop.command = CMD_ISREADY;
349 int status = send_disk_op(&dop);
351 disk_ret(regs, DISK_RET_ENOTREADY);
353 disk_ret(regs, DISK_RET_SUCCESS);
358 disk_1311(struct bregs *regs, u8 device)
363 // controller internal diagnostic
365 disk_1314(struct bregs *regs, u8 device)
370 // read disk drive size
372 disk_1315(struct bregs *regs, u8 device)
374 // Get logical geometry from table
375 u16 nlc = GET_GLOBAL(Drives.drives[device].lchs.cylinders);
376 u16 nlh = GET_GLOBAL(Drives.drives[device].lchs.heads);
377 u16 nlspt = GET_GLOBAL(Drives.drives[device].lchs.spt);
379 // Compute sector count seen by int13
380 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
381 regs->cx = lba >> 16;
382 regs->dx = lba & 0xffff;
384 disk_ret(regs, DISK_RET_SUCCESS);
385 regs->ah = 3; // hard disk accessible
388 // IBM/MS installation check
390 disk_1341(struct bregs *regs, u8 device)
392 regs->bx = 0xaa55; // install check
393 regs->cx = 0x0007; // ext disk access and edd, removable supported
394 disk_ret(regs, DISK_RET_SUCCESS);
395 regs->ah = 0x30; // EDD 3.0
398 // IBM/MS extended read
400 disk_1342(struct bregs *regs, u8 device)
402 extended_access(regs, device, CMD_READ);
405 // IBM/MS extended write
407 disk_1343(struct bregs *regs, u8 device)
409 extended_access(regs, device, CMD_WRITE);
414 disk_1344(struct bregs *regs, u8 device)
416 extended_access(regs, device, CMD_VERIFY);
419 // IBM/MS lock/unlock drive
421 disk_1345(struct bregs *regs, u8 device)
423 // Always success for HD
424 disk_ret(regs, DISK_RET_SUCCESS);
427 // IBM/MS eject media
429 disk_1346(struct bregs *regs, u8 device)
431 // Volume Not Removable
432 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
435 // IBM/MS extended seek
437 disk_1347(struct bregs *regs, u8 device)
439 extended_access(regs, device, CMD_SEEK);
442 // IBM/MS get drive parameters
444 disk_1348(struct bregs *regs, u8 device)
446 u16 size = GET_INT13DPT(regs, size);
448 // Buffer is too small
450 disk_ret(regs, DISK_RET_EPARAM);
456 u8 type = GET_GLOBAL(Drives.drives[device].type);
457 u16 npc = GET_GLOBAL(Drives.drives[device].pchs.cylinders);
458 u16 nph = GET_GLOBAL(Drives.drives[device].pchs.heads);
459 u16 npspt = GET_GLOBAL(Drives.drives[device].pchs.spt);
460 u64 lba = GET_GLOBAL(Drives.drives[device].sectors);
461 u16 blksize = GET_GLOBAL(Drives.drives[device].blksize);
463 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
464 , size, type, npc, nph, npspt, (u32)lba, blksize);
466 SET_INT13DPT(regs, size, 26);
467 if (type == DTYPE_ATAPI) {
468 // 0x74 = removable, media change, lockable, max values
469 SET_INT13DPT(regs, infos, 0x74);
470 SET_INT13DPT(regs, cylinders, 0xffffffff);
471 SET_INT13DPT(regs, heads, 0xffffffff);
472 SET_INT13DPT(regs, spt, 0xffffffff);
473 SET_INT13DPT(regs, sector_count, (u64)-1);
475 if (lba > (u64)npspt*nph*0x3fff) {
476 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
477 SET_INT13DPT(regs, cylinders, 0x3fff);
479 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
480 SET_INT13DPT(regs, cylinders, (u32)npc);
482 SET_INT13DPT(regs, heads, (u32)nph);
483 SET_INT13DPT(regs, spt, (u32)npspt);
484 SET_INT13DPT(regs, sector_count, lba);
486 SET_INT13DPT(regs, blksize, blksize);
488 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
489 disk_ret(regs, DISK_RET_SUCCESS);
495 u16 ebda_seg = get_ebda_seg();
496 SET_INT13DPT(regs, size, 30);
498 SET_INT13DPT(regs, dpte_segment, ebda_seg);
499 SET_INT13DPT(regs, dpte_offset
500 , offsetof(struct extended_bios_data_area_s, dpte));
503 u8 ataid = GET_GLOBAL(Drives.drives[device].cntl_id);
504 u8 channel = ataid / 2;
505 u8 slave = ataid % 2;
506 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
507 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
508 u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
511 if (type == DTYPE_ATA) {
512 u8 translation = GET_GLOBAL(Drives.drives[device].translation);
513 if (translation != TRANSLATION_NONE) {
514 options |= 1<<3; // CHS translation
515 if (translation == TRANSLATION_LBA)
517 if (translation == TRANSLATION_RECHS)
522 options |= 1<<5; // removable device
523 options |= 1<<6; // atapi device
525 options |= 1<<4; // lba translation
526 if (CONFIG_ATA_PIO32)
529 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
530 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
531 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
533 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
534 SET_EBDA2(ebda_seg, dpte.irq, irq);
535 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
536 SET_EBDA2(ebda_seg, dpte.dma, 0);
537 SET_EBDA2(ebda_seg, dpte.pio, 0);
538 SET_EBDA2(ebda_seg, dpte.options, options);
539 SET_EBDA2(ebda_seg, dpte.reserved, 0);
540 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
542 u8 sum = checksum_far(
543 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
544 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
547 disk_ret(regs, DISK_RET_SUCCESS);
552 SET_INT13DPT(regs, key, 0xbedd);
553 SET_INT13DPT(regs, dpi_length, 36);
554 SET_INT13DPT(regs, reserved1, 0);
555 SET_INT13DPT(regs, reserved2, 0);
557 SET_INT13DPT(regs, host_bus[0], 'P');
558 SET_INT13DPT(regs, host_bus[1], 'C');
559 SET_INT13DPT(regs, host_bus[2], 'I');
560 SET_INT13DPT(regs, host_bus[3], 0);
562 u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
563 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
564 | (pci_bdf_to_fn(bdf) << 16));
565 SET_INT13DPT(regs, iface_path, path);
567 SET_INT13DPT(regs, iface_type[0], 'A');
568 SET_INT13DPT(regs, iface_type[1], 'T');
569 SET_INT13DPT(regs, iface_type[2], 'A');
570 SET_INT13DPT(regs, iface_type[3], 0);
571 SET_INT13DPT(regs, iface_type[4], 0);
572 SET_INT13DPT(regs, iface_type[5], 0);
573 SET_INT13DPT(regs, iface_type[6], 0);
574 SET_INT13DPT(regs, iface_type[7], 0);
576 SET_INT13DPT(regs, device_path, slave);
578 SET_INT13DPT(regs, checksum
579 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
581 disk_ret(regs, DISK_RET_SUCCESS);
584 // IBM/MS extended media change
586 disk_1349(struct bregs *regs, u8 device)
588 // Always success for HD
589 disk_ret(regs, DISK_RET_SUCCESS);
593 disk_134e01(struct bregs *regs, u8 device)
595 disk_ret(regs, DISK_RET_SUCCESS);
599 disk_134e03(struct bregs *regs, u8 device)
601 disk_ret(regs, DISK_RET_SUCCESS);
605 disk_134e04(struct bregs *regs, u8 device)
607 disk_ret(regs, DISK_RET_SUCCESS);
611 disk_134e06(struct bregs *regs, u8 device)
613 disk_ret(regs, DISK_RET_SUCCESS);
617 disk_134eXX(struct bregs *regs, u8 device)
619 disk_ret(regs, DISK_RET_EPARAM);
622 // IBM/MS set hardware configuration
624 disk_134e(struct bregs *regs, u8 device)
627 case 0x01: disk_134e01(regs, device); break;
628 case 0x03: disk_134e03(regs, device); break;
629 case 0x04: disk_134e04(regs, device); break;
630 case 0x06: disk_134e06(regs, device); break;
631 default: disk_134eXX(regs, device); break;
636 disk_13XX(struct bregs *regs, u8 device)
638 disk_ret(regs, DISK_RET_EPARAM);
642 disk_13(struct bregs *regs, u8 device)
646 // clear completion flag
647 SET_BDA(disk_interrupt_flag, 0);
650 case 0x00: disk_1300(regs, device); break;
651 case 0x01: disk_1301(regs, device); break;
652 case 0x02: disk_1302(regs, device); break;
653 case 0x03: disk_1303(regs, device); break;
654 case 0x04: disk_1304(regs, device); break;
655 case 0x05: disk_1305(regs, device); break;
656 case 0x08: disk_1308(regs, device); break;
657 case 0x09: disk_1309(regs, device); break;
658 case 0x0c: disk_130c(regs, device); break;
659 case 0x0d: disk_130d(regs, device); break;
660 case 0x10: disk_1310(regs, device); break;
661 case 0x11: disk_1311(regs, device); break;
662 case 0x14: disk_1314(regs, device); break;
663 case 0x15: disk_1315(regs, device); break;
664 case 0x41: disk_1341(regs, device); break;
665 case 0x42: disk_1342(regs, device); break;
666 case 0x43: disk_1343(regs, device); break;
667 case 0x44: disk_1344(regs, device); break;
668 case 0x45: disk_1345(regs, device); break;
669 case 0x46: disk_1346(regs, device); break;
670 case 0x47: disk_1347(regs, device); break;
671 case 0x48: disk_1348(regs, device); break;
672 case 0x49: disk_1349(regs, device); break;
673 case 0x4e: disk_134e(regs, device); break;
674 default: disk_13XX(regs, device); break;
679 /****************************************************************
681 ****************************************************************/
684 get_device(struct bregs *regs, u8 iscd, u8 drive)
686 // basic check : device has to be defined
687 if (drive >= ARRAY_SIZE(Drives.idmap[0])) {
688 disk_ret(regs, DISK_RET_EPARAM);
692 // Get the ata channel
693 u8 driveid = GET_GLOBAL(Drives.idmap[iscd][drive]);
695 // basic check : device has to be valid
696 if (driveid >= ARRAY_SIZE(Drives.drives)) {
697 disk_ret(regs, DISK_RET_EPARAM);
705 handle_legacy_disk(struct bregs *regs, u8 drive)
708 floppy_13(regs, drive);
712 if (! CONFIG_DRIVES) {
713 // XXX - old code had other disk access method.
714 disk_ret(regs, DISK_RET_EPARAM);
719 int driveid = get_device(regs, 1, drive - 0xe0);
722 cdrom_13(regs, driveid);
726 int driveid = get_device(regs, 0, drive - 0x80);
729 disk_13(regs, driveid);
733 handle_40(struct bregs *regs)
735 debug_enter(regs, DEBUG_HDL_40);
736 handle_legacy_disk(regs, regs->dl);
739 // INT 13h Fixed Disk Services Entry Point
741 handle_13(struct bregs *regs)
743 debug_enter(regs, DEBUG_HDL_13);
746 if (CONFIG_CDROM_EMU) {
747 if (regs->ah == 0x4b) {
751 u16 ebda_seg = get_ebda_seg();
752 if (GET_EBDA2(ebda_seg, cdemu.active)) {
753 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
754 if (drive == emudrive) {
758 if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
762 handle_legacy_disk(regs, drive);
765 // record completion in BIOS task complete flag
769 debug_isr(DEBUG_ISR_76);
770 SET_BDA(disk_interrupt_flag, 0xff);
774 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
775 struct fdpt_s OldFDPT VAR16FIXED(0xe401);