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);
62 if (dop.command == CMD_CDROM_READ)
63 status = cdrom_read(&dop);
65 status = ata_cmd_data(&dop);
69 // Update count with total sectors transferred.
70 SET_FARVAR(op_seg, op_far->count, dop.count);
73 dprintf(1, "disk_op cmd %d error %d!\n", dop.command, status);
78 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
80 send_disk_op(struct disk_op_s *op)
85 return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
88 // Obtain the requested disk lba from an old-style chs request.
90 legacy_lba(struct bregs *regs, u16 lchs_seg, struct chs_s *lchs_far)
93 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
94 u16 sector = regs->cl & 0x3f;
97 if (count > 128 || count == 0 || sector == 0) {
98 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
100 disk_ret(regs, DISK_RET_EPARAM);
104 u16 nlc = GET_FARVAR(lchs_seg, lchs_far->cylinders);
105 u16 nlh = GET_FARVAR(lchs_seg, lchs_far->heads);
106 u16 nlspt = GET_FARVAR(lchs_seg, lchs_far->spt);
108 // sanity check on cyl heads, sec
109 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
110 dprintf(1, "int13_harddisk: function %02x, parameters out of"
111 " range %04x/%04x/%04x!\n"
112 , regs->ah, cylinder, head, sector);
113 disk_ret(regs, DISK_RET_EPARAM);
117 // translate lchs to lba
118 return (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
122 // Perform read/write/verify using old-style chs accesses
124 basic_access(struct bregs *regs, u8 device, u16 command)
126 struct disk_op_s dop;
127 dop.driveid = device;
128 dop.command = command;
129 int lba = legacy_lba(regs, get_global_seg(), &ATA.devices[device].lchs);
133 dop.count = regs->al;
134 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
136 int status = send_disk_op(&dop);
138 regs->al = dop.count;
141 disk_ret(regs, DISK_RET_EBADTRACK);
144 disk_ret(regs, DISK_RET_SUCCESS);
147 // Perform cdemu read/verify
149 cdemu_access(struct bregs *regs, u8 device, u16 command)
151 struct disk_op_s dop;
152 dop.driveid = device;
153 dop.command = (command == ATA_CMD_READ_SECTORS ? CMD_CDROM_READ : 0);
154 u16 ebda_seg = get_ebda_seg();
155 int vlba = legacy_lba(
157 , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
160 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
162 u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
163 u8 *dest_far = (void*)(regs->bx+0);
168 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
169 int status = send_disk_op(&dop);
172 u8 thiscount = 4 - (vlba & 3);
173 if (thiscount > count)
176 memcpy_far(regs->es, dest_far
177 , ebda_seg, cdbuf_far + (vlba & 3) * 512
179 dest_far += thiscount * 512;
180 regs->al += thiscount;
185 dop.count = count / 4;
186 dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
187 int status = send_disk_op(&dop);
188 regs->al += dop.count * 4;
191 u8 thiscount = count & ~3;
193 dest_far += thiscount * 512;
194 dop.lba += thiscount / 4;
199 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
200 int status = send_disk_op(&dop);
203 u8 thiscount = count;
204 memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
205 regs->al += thiscount;
208 disk_ret(regs, DISK_RET_SUCCESS);
211 disk_ret(regs, DISK_RET_EBADTRACK);
214 // Perform read/write/verify using new-style "int13ext" accesses.
216 extended_access(struct bregs *regs, u8 device, u16 command)
218 struct disk_op_s dop;
219 // Get lba and check.
220 dop.lba = GET_INT13EXT(regs, lba);
221 dop.command = command;
222 dop.driveid = device;
223 u8 type = GET_GLOBAL(ATA.devices[device].type);
224 if (type == ATA_TYPE_ATA) {
225 if (dop.lba >= GET_GLOBAL(ATA.devices[device].sectors)) {
226 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
228 disk_ret(regs, DISK_RET_EPARAM);
232 dop.command = CMD_CDROM_READ;
235 u16 segment = GET_INT13EXT(regs, segment);
236 u16 offset = GET_INT13EXT(regs, offset);
237 dop.buf_fl = MAKE_FLATPTR(segment, offset);
238 dop.count = GET_INT13EXT(regs, count);
240 int status = send_disk_op(&dop);
242 SET_INT13EXT(regs, count, dop.count);
245 disk_ret(regs, DISK_RET_EBADTRACK);
248 disk_ret(regs, DISK_RET_SUCCESS);
252 /****************************************************************
253 * Hard Drive functions
254 ****************************************************************/
256 // disk controller reset
258 disk_1300(struct bregs *regs, u8 device)
265 disk_1301(struct bregs *regs, u8 device)
267 u8 v = GET_BDA(disk_last_status);
270 // XXX - clear disk_last_status?
275 disk_1302(struct bregs *regs, u8 device)
277 basic_access(regs, device, ATA_CMD_READ_SECTORS);
280 // write disk sectors
282 disk_1303(struct bregs *regs, u8 device)
284 basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
287 // verify disk sectors
289 disk_1304(struct bregs *regs, u8 device)
291 basic_access(regs, device, 0);
297 disk_1305(struct bregs *regs, u8 device)
302 // read disk drive parameters
304 disk_1308(struct bregs *regs, u8 device)
306 // Get logical geometry from table
307 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
308 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
309 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
310 u16 count = GET_BDA(hdcount);
312 nlc = nlc - 2; /* 0 based , last sector not used */
314 regs->ch = nlc & 0xff;
315 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
317 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
319 // FIXME should set ES & DI
320 disk_ret(regs, DISK_RET_SUCCESS);
323 // initialize drive parameters
325 disk_1309(struct bregs *regs, u8 device)
330 // seek to specified cylinder
332 disk_130c(struct bregs *regs, u8 device)
337 // alternate disk reset
339 disk_130d(struct bregs *regs, u8 device)
346 disk_1310(struct bregs *regs, u8 device)
348 // should look at 40:8E also???
350 // Read the status from controller
351 u8 status = inb(GET_GLOBAL(ATA.channels[device/2].iobase1) + ATA_CB_STAT);
352 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
353 disk_ret(regs, DISK_RET_SUCCESS);
355 disk_ret(regs, DISK_RET_ENOTREADY);
360 disk_1311(struct bregs *regs, u8 device)
365 // controller internal diagnostic
367 disk_1314(struct bregs *regs, u8 device)
372 // read disk drive size
374 disk_1315(struct bregs *regs, u8 device)
376 // Get logical geometry from table
377 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
378 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
379 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
381 // Compute sector count seen by int13
382 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
383 regs->cx = lba >> 16;
384 regs->dx = lba & 0xffff;
386 disk_ret(regs, DISK_RET_SUCCESS);
387 regs->ah = 3; // hard disk accessible
390 // IBM/MS installation check
392 disk_1341(struct bregs *regs, u8 device)
394 regs->bx = 0xaa55; // install check
395 regs->cx = 0x0007; // ext disk access and edd, removable supported
396 disk_ret(regs, DISK_RET_SUCCESS);
397 regs->ah = 0x30; // EDD 3.0
400 // IBM/MS extended read
402 disk_1342(struct bregs *regs, u8 device)
404 extended_access(regs, device, ATA_CMD_READ_SECTORS);
407 // IBM/MS extended write
409 disk_1343(struct bregs *regs, u8 device)
411 extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
416 disk_1344(struct bregs *regs, u8 device)
418 extended_access(regs, device, 0);
421 // IBM/MS lock/unlock drive
423 disk_1345(struct bregs *regs, u8 device)
425 // Always success for HD
426 disk_ret(regs, DISK_RET_SUCCESS);
429 // IBM/MS eject media
431 disk_1346(struct bregs *regs, u8 device)
433 // Volume Not Removable
434 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
437 // IBM/MS extended seek
439 disk_1347(struct bregs *regs, u8 device)
441 extended_access(regs, device, 0);
444 // IBM/MS get drive parameters
446 disk_1348(struct bregs *regs, u8 device)
448 u16 size = GET_INT13DPT(regs, size);
450 // Buffer is too small
452 disk_ret(regs, DISK_RET_EPARAM);
458 u8 type = GET_GLOBAL(ATA.devices[device].type);
459 u16 npc = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
460 u16 nph = GET_GLOBAL(ATA.devices[device].pchs.heads);
461 u16 npspt = GET_GLOBAL(ATA.devices[device].pchs.spt);
462 u64 lba = GET_GLOBAL(ATA.devices[device].sectors);
463 u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
465 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
466 , size, type, npc, nph, npspt, (u32)lba, blksize);
468 SET_INT13DPT(regs, size, 26);
469 if (type == ATA_TYPE_ATA) {
470 if (lba > (u64)npspt*nph*0x3fff) {
471 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
472 SET_INT13DPT(regs, cylinders, 0x3fff);
474 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
475 SET_INT13DPT(regs, cylinders, (u32)npc);
477 SET_INT13DPT(regs, heads, (u32)nph);
478 SET_INT13DPT(regs, spt, (u32)npspt);
479 SET_INT13DPT(regs, sector_count, lba);
482 // 0x74 = removable, media change, lockable, max values
483 SET_INT13DPT(regs, infos, 0x74);
484 SET_INT13DPT(regs, cylinders, 0xffffffff);
485 SET_INT13DPT(regs, heads, 0xffffffff);
486 SET_INT13DPT(regs, spt, 0xffffffff);
487 SET_INT13DPT(regs, sector_count, (u64)-1);
489 SET_INT13DPT(regs, blksize, blksize);
492 disk_ret(regs, DISK_RET_SUCCESS);
498 u16 ebda_seg = get_ebda_seg();
499 SET_INT13DPT(regs, size, 30);
501 SET_INT13DPT(regs, dpte_segment, ebda_seg);
502 SET_INT13DPT(regs, dpte_offset
503 , offsetof(struct extended_bios_data_area_s, dpte));
506 u8 channel = device / 2;
507 u8 slave = device % 2;
508 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
509 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
510 u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
513 if (type == ATA_TYPE_ATA) {
514 u8 translation = GET_GLOBAL(ATA.devices[device].translation);
515 if (translation != ATA_TRANSLATION_NONE) {
516 options |= 1<<3; // CHS translation
517 if (translation == ATA_TRANSLATION_LBA)
519 if (translation == ATA_TRANSLATION_RECHS)
524 options |= 1<<5; // removable device
525 options |= 1<<6; // atapi device
527 options |= 1<<4; // lba translation
528 if (CONFIG_ATA_PIO32)
531 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
532 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
533 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
535 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
536 SET_EBDA2(ebda_seg, dpte.irq, irq);
537 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
538 SET_EBDA2(ebda_seg, dpte.dma, 0);
539 SET_EBDA2(ebda_seg, dpte.pio, 0);
540 SET_EBDA2(ebda_seg, dpte.options, options);
541 SET_EBDA2(ebda_seg, dpte.reserved, 0);
542 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
544 u8 sum = checksum_far(
545 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
546 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
549 disk_ret(regs, DISK_RET_SUCCESS);
554 SET_INT13DPT(regs, key, 0xbedd);
555 SET_INT13DPT(regs, dpi_length, 36);
556 SET_INT13DPT(regs, reserved1, 0);
557 SET_INT13DPT(regs, reserved2, 0);
559 SET_INT13DPT(regs, host_bus[0], 'P');
560 SET_INT13DPT(regs, host_bus[1], 'C');
561 SET_INT13DPT(regs, host_bus[2], 'I');
562 SET_INT13DPT(regs, host_bus[3], 0);
564 u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
565 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
566 | (pci_bdf_to_fn(bdf) << 16));
567 SET_INT13DPT(regs, iface_path, path);
569 SET_INT13DPT(regs, iface_type[0], 'A');
570 SET_INT13DPT(regs, iface_type[1], 'T');
571 SET_INT13DPT(regs, iface_type[2], 'A');
572 SET_INT13DPT(regs, iface_type[3], 0);
573 SET_INT13DPT(regs, iface_type[4], 0);
574 SET_INT13DPT(regs, iface_type[5], 0);
575 SET_INT13DPT(regs, iface_type[6], 0);
576 SET_INT13DPT(regs, iface_type[7], 0);
578 SET_INT13DPT(regs, device_path, slave);
580 SET_INT13DPT(regs, checksum
581 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
583 disk_ret(regs, DISK_RET_SUCCESS);
586 // IBM/MS extended media change
588 disk_1349(struct bregs *regs, u8 device)
590 // Always success for HD
591 disk_ret(regs, DISK_RET_SUCCESS);
595 disk_134e01(struct bregs *regs, u8 device)
597 disk_ret(regs, DISK_RET_SUCCESS);
601 disk_134e03(struct bregs *regs, u8 device)
603 disk_ret(regs, DISK_RET_SUCCESS);
607 disk_134e04(struct bregs *regs, u8 device)
609 disk_ret(regs, DISK_RET_SUCCESS);
613 disk_134e06(struct bregs *regs, u8 device)
615 disk_ret(regs, DISK_RET_SUCCESS);
619 disk_134eXX(struct bregs *regs, u8 device)
621 disk_ret(regs, DISK_RET_EPARAM);
624 // IBM/MS set hardware configuration
626 disk_134e(struct bregs *regs, u8 device)
629 case 0x01: disk_134e01(regs, device); break;
630 case 0x03: disk_134e03(regs, device); break;
631 case 0x04: disk_134e04(regs, device); break;
632 case 0x06: disk_134e06(regs, device); break;
633 default: disk_134eXX(regs, device); break;
638 disk_13XX(struct bregs *regs, u8 device)
640 disk_ret(regs, DISK_RET_EPARAM);
644 disk_13(struct bregs *regs, u8 device)
648 // clear completion flag
649 SET_BDA(disk_interrupt_flag, 0);
652 case 0x00: disk_1300(regs, device); break;
653 case 0x01: disk_1301(regs, device); break;
654 case 0x02: disk_1302(regs, device); break;
655 case 0x03: disk_1303(regs, device); break;
656 case 0x04: disk_1304(regs, device); break;
657 case 0x05: disk_1305(regs, device); break;
658 case 0x08: disk_1308(regs, device); break;
659 case 0x09: disk_1309(regs, device); break;
660 case 0x0c: disk_130c(regs, device); break;
661 case 0x0d: disk_130d(regs, device); break;
662 case 0x10: disk_1310(regs, device); break;
663 case 0x11: disk_1311(regs, device); break;
664 case 0x14: disk_1314(regs, device); break;
665 case 0x15: disk_1315(regs, device); break;
666 case 0x41: disk_1341(regs, device); break;
667 case 0x42: disk_1342(regs, device); break;
668 case 0x43: disk_1343(regs, device); break;
669 case 0x44: disk_1344(regs, device); break;
670 case 0x45: disk_1345(regs, device); break;
671 case 0x46: disk_1346(regs, device); break;
672 case 0x47: disk_1347(regs, device); break;
673 case 0x48: disk_1348(regs, device); break;
674 case 0x49: disk_1349(regs, device); break;
675 case 0x4e: disk_134e(regs, device); break;
676 default: disk_13XX(regs, device); break;
681 /****************************************************************
683 ****************************************************************/
686 get_device(struct bregs *regs, u8 iscd, u8 drive)
688 // basic check : device has to be defined
689 if (drive >= CONFIG_MAX_ATA_DEVICES) {
690 disk_ret(regs, DISK_RET_EPARAM);
691 return CONFIG_MAX_ATA_DEVICES;
694 // Get the ata channel
695 u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
697 // basic check : device has to be valid
698 if (device >= CONFIG_MAX_ATA_DEVICES) {
699 disk_ret(regs, DISK_RET_EPARAM);
700 return CONFIG_MAX_ATA_DEVICES;
707 handle_legacy_disk(struct bregs *regs, u8 drive)
710 floppy_13(regs, drive);
715 // XXX - old code had other disk access method.
716 disk_ret(regs, DISK_RET_EPARAM);
721 u8 device = get_device(regs, 1, drive - 0xe0);
722 if (device >= CONFIG_MAX_ATA_DEVICES)
724 cdrom_13(regs, device);
728 u8 device = get_device(regs, 0, drive - 0x80);
729 if (device >= CONFIG_MAX_ATA_DEVICES)
731 disk_13(regs, device);
735 handle_40(struct bregs *regs)
737 debug_enter(regs, DEBUG_HDL_40);
738 handle_legacy_disk(regs, regs->dl);
741 // INT 13h Fixed Disk Services Entry Point
743 handle_13(struct bregs *regs)
745 debug_enter(regs, DEBUG_HDL_13);
748 if (CONFIG_CDROM_EMU) {
749 if (regs->ah == 0x4b) {
753 u16 ebda_seg = get_ebda_seg();
754 if (GET_EBDA2(ebda_seg, cdemu.active)) {
755 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
756 if (drive == emudrive) {
760 if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
764 handle_legacy_disk(regs, drive);
767 // record completion in BIOS task complete flag
771 debug_isr(DEBUG_ISR_76);
772 SET_BDA(disk_interrupt_flag, 0xff);
776 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
777 struct fdpt_s OldFDPT VAR16FIXED(0xe401);