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 "atabits.h" // ATA_CB_STAT
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(ATA.devices[dop.driveid].type);
60 if (type == ATA_TYPE_ATA)
61 status = process_ata_op(&dop);
62 else if (type == ATA_TYPE_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(), &ATA.devices[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(ATA.devices[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(ATA.devices[device].lchs.cylinders);
304 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
305 u16 nlspt = GET_GLOBAL(ATA.devices[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 // Read the status from controller
347 u8 status = inb(GET_GLOBAL(ATA.channels[device/2].iobase1) + ATA_CB_STAT);
348 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
349 disk_ret(regs, DISK_RET_SUCCESS);
351 disk_ret(regs, DISK_RET_ENOTREADY);
356 disk_1311(struct bregs *regs, u8 device)
361 // controller internal diagnostic
363 disk_1314(struct bregs *regs, u8 device)
368 // read disk drive size
370 disk_1315(struct bregs *regs, u8 device)
372 // Get logical geometry from table
373 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
374 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
375 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
377 // Compute sector count seen by int13
378 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
379 regs->cx = lba >> 16;
380 regs->dx = lba & 0xffff;
382 disk_ret(regs, DISK_RET_SUCCESS);
383 regs->ah = 3; // hard disk accessible
386 // IBM/MS installation check
388 disk_1341(struct bregs *regs, u8 device)
390 regs->bx = 0xaa55; // install check
391 regs->cx = 0x0007; // ext disk access and edd, removable supported
392 disk_ret(regs, DISK_RET_SUCCESS);
393 regs->ah = 0x30; // EDD 3.0
396 // IBM/MS extended read
398 disk_1342(struct bregs *regs, u8 device)
400 extended_access(regs, device, CMD_READ);
403 // IBM/MS extended write
405 disk_1343(struct bregs *regs, u8 device)
407 extended_access(regs, device, CMD_WRITE);
412 disk_1344(struct bregs *regs, u8 device)
414 extended_access(regs, device, CMD_VERIFY);
417 // IBM/MS lock/unlock drive
419 disk_1345(struct bregs *regs, u8 device)
421 // Always success for HD
422 disk_ret(regs, DISK_RET_SUCCESS);
425 // IBM/MS eject media
427 disk_1346(struct bregs *regs, u8 device)
429 // Volume Not Removable
430 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
433 // IBM/MS extended seek
435 disk_1347(struct bregs *regs, u8 device)
437 extended_access(regs, device, CMD_SEEK);
440 // IBM/MS get drive parameters
442 disk_1348(struct bregs *regs, u8 device)
444 u16 size = GET_INT13DPT(regs, size);
446 // Buffer is too small
448 disk_ret(regs, DISK_RET_EPARAM);
454 u8 type = GET_GLOBAL(ATA.devices[device].type);
455 u16 npc = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
456 u16 nph = GET_GLOBAL(ATA.devices[device].pchs.heads);
457 u16 npspt = GET_GLOBAL(ATA.devices[device].pchs.spt);
458 u64 lba = GET_GLOBAL(ATA.devices[device].sectors);
459 u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
461 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
462 , size, type, npc, nph, npspt, (u32)lba, blksize);
464 SET_INT13DPT(regs, size, 26);
465 if (type == ATA_TYPE_ATA) {
466 if (lba > (u64)npspt*nph*0x3fff) {
467 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
468 SET_INT13DPT(regs, cylinders, 0x3fff);
470 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
471 SET_INT13DPT(regs, cylinders, (u32)npc);
473 SET_INT13DPT(regs, heads, (u32)nph);
474 SET_INT13DPT(regs, spt, (u32)npspt);
475 SET_INT13DPT(regs, sector_count, lba);
478 // 0x74 = removable, media change, lockable, max values
479 SET_INT13DPT(regs, infos, 0x74);
480 SET_INT13DPT(regs, cylinders, 0xffffffff);
481 SET_INT13DPT(regs, heads, 0xffffffff);
482 SET_INT13DPT(regs, spt, 0xffffffff);
483 SET_INT13DPT(regs, sector_count, (u64)-1);
485 SET_INT13DPT(regs, blksize, blksize);
488 disk_ret(regs, DISK_RET_SUCCESS);
494 u16 ebda_seg = get_ebda_seg();
495 SET_INT13DPT(regs, size, 30);
497 SET_INT13DPT(regs, dpte_segment, ebda_seg);
498 SET_INT13DPT(regs, dpte_offset
499 , offsetof(struct extended_bios_data_area_s, dpte));
502 u8 channel = device / 2;
503 u8 slave = device % 2;
504 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
505 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
506 u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
509 if (type == ATA_TYPE_ATA) {
510 u8 translation = GET_GLOBAL(ATA.devices[device].translation);
511 if (translation != ATA_TRANSLATION_NONE) {
512 options |= 1<<3; // CHS translation
513 if (translation == ATA_TRANSLATION_LBA)
515 if (translation == ATA_TRANSLATION_RECHS)
520 options |= 1<<5; // removable device
521 options |= 1<<6; // atapi device
523 options |= 1<<4; // lba translation
524 if (CONFIG_ATA_PIO32)
527 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
528 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
529 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
531 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
532 SET_EBDA2(ebda_seg, dpte.irq, irq);
533 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
534 SET_EBDA2(ebda_seg, dpte.dma, 0);
535 SET_EBDA2(ebda_seg, dpte.pio, 0);
536 SET_EBDA2(ebda_seg, dpte.options, options);
537 SET_EBDA2(ebda_seg, dpte.reserved, 0);
538 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
540 u8 sum = checksum_far(
541 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
542 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
545 disk_ret(regs, DISK_RET_SUCCESS);
550 SET_INT13DPT(regs, key, 0xbedd);
551 SET_INT13DPT(regs, dpi_length, 36);
552 SET_INT13DPT(regs, reserved1, 0);
553 SET_INT13DPT(regs, reserved2, 0);
555 SET_INT13DPT(regs, host_bus[0], 'P');
556 SET_INT13DPT(regs, host_bus[1], 'C');
557 SET_INT13DPT(regs, host_bus[2], 'I');
558 SET_INT13DPT(regs, host_bus[3], 0);
560 u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
561 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
562 | (pci_bdf_to_fn(bdf) << 16));
563 SET_INT13DPT(regs, iface_path, path);
565 SET_INT13DPT(regs, iface_type[0], 'A');
566 SET_INT13DPT(regs, iface_type[1], 'T');
567 SET_INT13DPT(regs, iface_type[2], 'A');
568 SET_INT13DPT(regs, iface_type[3], 0);
569 SET_INT13DPT(regs, iface_type[4], 0);
570 SET_INT13DPT(regs, iface_type[5], 0);
571 SET_INT13DPT(regs, iface_type[6], 0);
572 SET_INT13DPT(regs, iface_type[7], 0);
574 SET_INT13DPT(regs, device_path, slave);
576 SET_INT13DPT(regs, checksum
577 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
579 disk_ret(regs, DISK_RET_SUCCESS);
582 // IBM/MS extended media change
584 disk_1349(struct bregs *regs, u8 device)
586 // Always success for HD
587 disk_ret(regs, DISK_RET_SUCCESS);
591 disk_134e01(struct bregs *regs, u8 device)
593 disk_ret(regs, DISK_RET_SUCCESS);
597 disk_134e03(struct bregs *regs, u8 device)
599 disk_ret(regs, DISK_RET_SUCCESS);
603 disk_134e04(struct bregs *regs, u8 device)
605 disk_ret(regs, DISK_RET_SUCCESS);
609 disk_134e06(struct bregs *regs, u8 device)
611 disk_ret(regs, DISK_RET_SUCCESS);
615 disk_134eXX(struct bregs *regs, u8 device)
617 disk_ret(regs, DISK_RET_EPARAM);
620 // IBM/MS set hardware configuration
622 disk_134e(struct bregs *regs, u8 device)
625 case 0x01: disk_134e01(regs, device); break;
626 case 0x03: disk_134e03(regs, device); break;
627 case 0x04: disk_134e04(regs, device); break;
628 case 0x06: disk_134e06(regs, device); break;
629 default: disk_134eXX(regs, device); break;
634 disk_13XX(struct bregs *regs, u8 device)
636 disk_ret(regs, DISK_RET_EPARAM);
640 disk_13(struct bregs *regs, u8 device)
644 // clear completion flag
645 SET_BDA(disk_interrupt_flag, 0);
648 case 0x00: disk_1300(regs, device); break;
649 case 0x01: disk_1301(regs, device); break;
650 case 0x02: disk_1302(regs, device); break;
651 case 0x03: disk_1303(regs, device); break;
652 case 0x04: disk_1304(regs, device); break;
653 case 0x05: disk_1305(regs, device); break;
654 case 0x08: disk_1308(regs, device); break;
655 case 0x09: disk_1309(regs, device); break;
656 case 0x0c: disk_130c(regs, device); break;
657 case 0x0d: disk_130d(regs, device); break;
658 case 0x10: disk_1310(regs, device); break;
659 case 0x11: disk_1311(regs, device); break;
660 case 0x14: disk_1314(regs, device); break;
661 case 0x15: disk_1315(regs, device); break;
662 case 0x41: disk_1341(regs, device); break;
663 case 0x42: disk_1342(regs, device); break;
664 case 0x43: disk_1343(regs, device); break;
665 case 0x44: disk_1344(regs, device); break;
666 case 0x45: disk_1345(regs, device); break;
667 case 0x46: disk_1346(regs, device); break;
668 case 0x47: disk_1347(regs, device); break;
669 case 0x48: disk_1348(regs, device); break;
670 case 0x49: disk_1349(regs, device); break;
671 case 0x4e: disk_134e(regs, device); break;
672 default: disk_13XX(regs, device); break;
677 /****************************************************************
679 ****************************************************************/
682 get_device(struct bregs *regs, u8 iscd, u8 drive)
684 // basic check : device has to be defined
685 if (drive >= CONFIG_MAX_ATA_DEVICES) {
686 disk_ret(regs, DISK_RET_EPARAM);
687 return CONFIG_MAX_ATA_DEVICES;
690 // Get the ata channel
691 u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
693 // basic check : device has to be valid
694 if (device >= CONFIG_MAX_ATA_DEVICES) {
695 disk_ret(regs, DISK_RET_EPARAM);
696 return CONFIG_MAX_ATA_DEVICES;
703 handle_legacy_disk(struct bregs *regs, u8 drive)
706 floppy_13(regs, drive);
711 // XXX - old code had other disk access method.
712 disk_ret(regs, DISK_RET_EPARAM);
717 u8 device = get_device(regs, 1, drive - 0xe0);
718 if (device >= CONFIG_MAX_ATA_DEVICES)
720 cdrom_13(regs, device);
724 u8 device = get_device(regs, 0, drive - 0x80);
725 if (device >= CONFIG_MAX_ATA_DEVICES)
727 disk_13(regs, device);
731 handle_40(struct bregs *regs)
733 debug_enter(regs, DEBUG_HDL_40);
734 handle_legacy_disk(regs, regs->dl);
737 // INT 13h Fixed Disk Services Entry Point
739 handle_13(struct bregs *regs)
741 debug_enter(regs, DEBUG_HDL_13);
744 if (CONFIG_CDROM_EMU) {
745 if (regs->ah == 0x4b) {
749 u16 ebda_seg = get_ebda_seg();
750 if (GET_EBDA2(ebda_seg, cdemu.active)) {
751 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
752 if (drive == emudrive) {
756 if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
760 handle_legacy_disk(regs, drive);
763 // record completion in BIOS task complete flag
767 debug_isr(DEBUG_ISR_76);
768 SET_BDA(disk_interrupt_flag, 0xff);
772 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
773 struct fdpt_s OldFDPT VAR16FIXED(0xe401);