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_CDEMU_READ)
63 status = cdrom_read_512(&dop);
64 else if (dop.command == CMD_CDROM_READ)
65 status = cdrom_read(&dop);
67 status = ata_cmd_data(&dop);
71 // Update count with total sectors transferred.
72 SET_FARVAR(op_seg, op_far->count, GET_EBDA(sector_count));
75 dprintf(1, "disk_op cmd %d error %d!\n", dop.command, status);
80 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
82 send_disk_op(struct disk_op_s *op)
87 return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
90 // Obtain the requested disk lba from an old-style chs request.
92 legacy_lba(struct bregs *regs, struct disk_op_s *op, u16 nlc, u16 nlh, u16 nlspt)
95 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
96 u16 sector = regs->cl & 0x3f;
99 if (op->count > 128 || op->count == 0 || sector == 0) {
100 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
102 disk_ret(regs, DISK_RET_EPARAM);
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 op->lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
119 u16 segment = regs->es;
120 u16 offset = regs->bx;
121 op->buf_fl = MAKE_FLATPTR(segment, offset);
126 // Perform read/write/verify using old-style chs accesses
128 basic_access(struct bregs *regs, u8 device, u16 command)
130 struct disk_op_s dop;
131 dop.driveid = device;
132 dop.command = command;
133 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
134 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
135 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
136 int ret = legacy_lba(regs, &dop, nlc, nlh, nlspt);
140 int status = send_disk_op(&dop);
142 regs->al = dop.count;
145 disk_ret(regs, DISK_RET_EBADTRACK);
148 disk_ret(regs, DISK_RET_SUCCESS);
151 // Perform cdemu read/verify
153 cdemu_access(struct bregs *regs, u8 device, u16 command)
155 struct disk_op_s dop;
156 dop.driveid = device;
157 dop.command = (command == ATA_CMD_READ_SECTORS ? CMD_CDEMU_READ : 0);
158 u16 ebda_seg = get_ebda_seg();
159 u16 nlc = GET_EBDA2(ebda_seg, cdemu.cylinders);
160 u16 nlh = GET_EBDA2(ebda_seg, cdemu.heads);
161 u16 nlspt = GET_EBDA2(ebda_seg, cdemu.spt);
162 int ret = legacy_lba(regs, &dop, nlc, nlh, nlspt);
165 dop.lba += GET_EBDA2(ebda_seg, cdemu.ilba) * 4;
167 int status = send_disk_op(&dop);
169 regs->al = dop.count;
172 disk_ret(regs, DISK_RET_EBADTRACK);
175 disk_ret(regs, DISK_RET_SUCCESS);
178 // Perform read/write/verify using new-style "int13ext" accesses.
180 extended_access(struct bregs *regs, u8 device, u16 command)
182 struct disk_op_s dop;
183 // Get lba and check.
184 dop.lba = GET_INT13EXT(regs, lba);
185 dop.command = command;
186 dop.driveid = device;
187 u8 type = GET_GLOBAL(ATA.devices[device].type);
188 if (type == ATA_TYPE_ATA) {
189 if (dop.lba >= GET_GLOBAL(ATA.devices[device].sectors)) {
190 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
192 disk_ret(regs, DISK_RET_EPARAM);
196 dop.command = CMD_CDROM_READ;
199 u16 segment = GET_INT13EXT(regs, segment);
200 u16 offset = GET_INT13EXT(regs, offset);
201 dop.buf_fl = MAKE_FLATPTR(segment, offset);
202 dop.count = GET_INT13EXT(regs, count);
204 int status = send_disk_op(&dop);
206 SET_INT13EXT(regs, count, dop.count);
209 disk_ret(regs, DISK_RET_EBADTRACK);
212 disk_ret(regs, DISK_RET_SUCCESS);
216 /****************************************************************
217 * Hard Drive functions
218 ****************************************************************/
220 // disk controller reset
222 disk_1300(struct bregs *regs, u8 device)
229 disk_1301(struct bregs *regs, u8 device)
231 u8 v = GET_BDA(disk_last_status);
234 // XXX - clear disk_last_status?
239 disk_1302(struct bregs *regs, u8 device)
241 basic_access(regs, device, ATA_CMD_READ_SECTORS);
244 // write disk sectors
246 disk_1303(struct bregs *regs, u8 device)
248 basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
251 // verify disk sectors
253 disk_1304(struct bregs *regs, u8 device)
255 basic_access(regs, device, 0);
261 disk_1305(struct bregs *regs, u8 device)
266 // read disk drive parameters
268 disk_1308(struct bregs *regs, u8 device)
270 // Get logical geometry from table
271 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
272 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
273 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
274 u16 count = GET_BDA(hdcount);
276 nlc = nlc - 2; /* 0 based , last sector not used */
278 regs->ch = nlc & 0xff;
279 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
281 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
283 // FIXME should set ES & DI
284 disk_ret(regs, DISK_RET_SUCCESS);
287 // initialize drive parameters
289 disk_1309(struct bregs *regs, u8 device)
294 // seek to specified cylinder
296 disk_130c(struct bregs *regs, u8 device)
301 // alternate disk reset
303 disk_130d(struct bregs *regs, u8 device)
310 disk_1310(struct bregs *regs, u8 device)
312 // should look at 40:8E also???
314 // Read the status from controller
315 u8 status = inb(GET_GLOBAL(ATA.channels[device/2].iobase1) + ATA_CB_STAT);
316 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
317 disk_ret(regs, DISK_RET_SUCCESS);
319 disk_ret(regs, DISK_RET_ENOTREADY);
324 disk_1311(struct bregs *regs, u8 device)
329 // controller internal diagnostic
331 disk_1314(struct bregs *regs, u8 device)
336 // read disk drive size
338 disk_1315(struct bregs *regs, u8 device)
340 // Get logical geometry from table
341 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
342 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
343 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
345 // Compute sector count seen by int13
346 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
347 regs->cx = lba >> 16;
348 regs->dx = lba & 0xffff;
350 disk_ret(regs, DISK_RET_SUCCESS);
351 regs->ah = 3; // hard disk accessible
354 // IBM/MS installation check
356 disk_1341(struct bregs *regs, u8 device)
358 regs->bx = 0xaa55; // install check
359 regs->cx = 0x0007; // ext disk access and edd, removable supported
360 disk_ret(regs, DISK_RET_SUCCESS);
361 regs->ah = 0x30; // EDD 3.0
364 // IBM/MS extended read
366 disk_1342(struct bregs *regs, u8 device)
368 extended_access(regs, device, ATA_CMD_READ_SECTORS);
371 // IBM/MS extended write
373 disk_1343(struct bregs *regs, u8 device)
375 extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
380 disk_1344(struct bregs *regs, u8 device)
382 extended_access(regs, device, 0);
385 // IBM/MS lock/unlock drive
387 disk_1345(struct bregs *regs, u8 device)
389 // Always success for HD
390 disk_ret(regs, DISK_RET_SUCCESS);
393 // IBM/MS eject media
395 disk_1346(struct bregs *regs, u8 device)
397 // Volume Not Removable
398 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
401 // IBM/MS extended seek
403 disk_1347(struct bregs *regs, u8 device)
405 extended_access(regs, device, 0);
408 // IBM/MS get drive parameters
410 disk_1348(struct bregs *regs, u8 device)
412 u16 size = GET_INT13DPT(regs, size);
414 // Buffer is too small
416 disk_ret(regs, DISK_RET_EPARAM);
422 u8 type = GET_GLOBAL(ATA.devices[device].type);
423 u16 npc = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
424 u16 nph = GET_GLOBAL(ATA.devices[device].pchs.heads);
425 u16 npspt = GET_GLOBAL(ATA.devices[device].pchs.spt);
426 u64 lba = GET_GLOBAL(ATA.devices[device].sectors);
427 u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
429 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
430 , size, type, npc, nph, npspt, (u32)lba, blksize);
432 SET_INT13DPT(regs, size, 26);
433 if (type == ATA_TYPE_ATA) {
434 if (lba > (u64)npspt*nph*0x3fff) {
435 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
436 SET_INT13DPT(regs, cylinders, 0x3fff);
438 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
439 SET_INT13DPT(regs, cylinders, (u32)npc);
441 SET_INT13DPT(regs, heads, (u32)nph);
442 SET_INT13DPT(regs, spt, (u32)npspt);
443 SET_INT13DPT(regs, sector_count, lba);
446 // 0x74 = removable, media change, lockable, max values
447 SET_INT13DPT(regs, infos, 0x74);
448 SET_INT13DPT(regs, cylinders, 0xffffffff);
449 SET_INT13DPT(regs, heads, 0xffffffff);
450 SET_INT13DPT(regs, spt, 0xffffffff);
451 SET_INT13DPT(regs, sector_count, (u64)-1);
453 SET_INT13DPT(regs, blksize, blksize);
456 disk_ret(regs, DISK_RET_SUCCESS);
462 u16 ebda_seg = get_ebda_seg();
463 SET_INT13DPT(regs, size, 30);
465 SET_INT13DPT(regs, dpte_segment, ebda_seg);
466 SET_INT13DPT(regs, dpte_offset
467 , offsetof(struct extended_bios_data_area_s, dpte));
470 u8 channel = device / 2;
471 u8 slave = device % 2;
472 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
473 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
474 u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
477 if (type == ATA_TYPE_ATA) {
478 u8 translation = GET_GLOBAL(ATA.devices[device].translation);
479 if (translation != ATA_TRANSLATION_NONE) {
480 options |= 1<<3; // CHS translation
481 if (translation == ATA_TRANSLATION_LBA)
483 if (translation == ATA_TRANSLATION_RECHS)
488 options |= 1<<5; // removable device
489 options |= 1<<6; // atapi device
491 options |= 1<<4; // lba translation
492 if (CONFIG_ATA_PIO32)
495 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
496 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
497 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
499 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
500 SET_EBDA2(ebda_seg, dpte.irq, irq);
501 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
502 SET_EBDA2(ebda_seg, dpte.dma, 0);
503 SET_EBDA2(ebda_seg, dpte.pio, 0);
504 SET_EBDA2(ebda_seg, dpte.options, options);
505 SET_EBDA2(ebda_seg, dpte.reserved, 0);
506 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
508 u8 sum = checksum_far(
509 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
510 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
513 disk_ret(regs, DISK_RET_SUCCESS);
518 SET_INT13DPT(regs, key, 0xbedd);
519 SET_INT13DPT(regs, dpi_length, 36);
520 SET_INT13DPT(regs, reserved1, 0);
521 SET_INT13DPT(regs, reserved2, 0);
523 SET_INT13DPT(regs, host_bus[0], 'P');
524 SET_INT13DPT(regs, host_bus[1], 'C');
525 SET_INT13DPT(regs, host_bus[2], 'I');
526 SET_INT13DPT(regs, host_bus[3], 0);
528 u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
529 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
530 | (pci_bdf_to_fn(bdf) << 16));
531 SET_INT13DPT(regs, iface_path, path);
533 SET_INT13DPT(regs, iface_type[0], 'A');
534 SET_INT13DPT(regs, iface_type[1], 'T');
535 SET_INT13DPT(regs, iface_type[2], 'A');
536 SET_INT13DPT(regs, iface_type[3], 0);
537 SET_INT13DPT(regs, iface_type[4], 0);
538 SET_INT13DPT(regs, iface_type[5], 0);
539 SET_INT13DPT(regs, iface_type[6], 0);
540 SET_INT13DPT(regs, iface_type[7], 0);
542 SET_INT13DPT(regs, device_path, slave);
544 SET_INT13DPT(regs, checksum
545 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
547 disk_ret(regs, DISK_RET_SUCCESS);
550 // IBM/MS extended media change
552 disk_1349(struct bregs *regs, u8 device)
554 // Always success for HD
555 disk_ret(regs, DISK_RET_SUCCESS);
559 disk_134e01(struct bregs *regs, u8 device)
561 disk_ret(regs, DISK_RET_SUCCESS);
565 disk_134e03(struct bregs *regs, u8 device)
567 disk_ret(regs, DISK_RET_SUCCESS);
571 disk_134e04(struct bregs *regs, u8 device)
573 disk_ret(regs, DISK_RET_SUCCESS);
577 disk_134e06(struct bregs *regs, u8 device)
579 disk_ret(regs, DISK_RET_SUCCESS);
583 disk_134eXX(struct bregs *regs, u8 device)
585 disk_ret(regs, DISK_RET_EPARAM);
588 // IBM/MS set hardware configuration
590 disk_134e(struct bregs *regs, u8 device)
593 case 0x01: disk_134e01(regs, device); break;
594 case 0x03: disk_134e03(regs, device); break;
595 case 0x04: disk_134e04(regs, device); break;
596 case 0x06: disk_134e06(regs, device); break;
597 default: disk_134eXX(regs, device); break;
602 disk_13XX(struct bregs *regs, u8 device)
604 disk_ret(regs, DISK_RET_EPARAM);
608 disk_13(struct bregs *regs, u8 device)
612 // clear completion flag
613 SET_BDA(disk_interrupt_flag, 0);
616 case 0x00: disk_1300(regs, device); break;
617 case 0x01: disk_1301(regs, device); break;
618 case 0x02: disk_1302(regs, device); break;
619 case 0x03: disk_1303(regs, device); break;
620 case 0x04: disk_1304(regs, device); break;
621 case 0x05: disk_1305(regs, device); break;
622 case 0x08: disk_1308(regs, device); break;
623 case 0x09: disk_1309(regs, device); break;
624 case 0x0c: disk_130c(regs, device); break;
625 case 0x0d: disk_130d(regs, device); break;
626 case 0x10: disk_1310(regs, device); break;
627 case 0x11: disk_1311(regs, device); break;
628 case 0x14: disk_1314(regs, device); break;
629 case 0x15: disk_1315(regs, device); break;
630 case 0x41: disk_1341(regs, device); break;
631 case 0x42: disk_1342(regs, device); break;
632 case 0x43: disk_1343(regs, device); break;
633 case 0x44: disk_1344(regs, device); break;
634 case 0x45: disk_1345(regs, device); break;
635 case 0x46: disk_1346(regs, device); break;
636 case 0x47: disk_1347(regs, device); break;
637 case 0x48: disk_1348(regs, device); break;
638 case 0x49: disk_1349(regs, device); break;
639 case 0x4e: disk_134e(regs, device); break;
640 default: disk_13XX(regs, device); break;
645 /****************************************************************
647 ****************************************************************/
650 get_device(struct bregs *regs, u8 iscd, u8 drive)
652 // basic check : device has to be defined
653 if (drive >= CONFIG_MAX_ATA_DEVICES) {
654 disk_ret(regs, DISK_RET_EPARAM);
655 return CONFIG_MAX_ATA_DEVICES;
658 // Get the ata channel
659 u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
661 // basic check : device has to be valid
662 if (device >= CONFIG_MAX_ATA_DEVICES) {
663 disk_ret(regs, DISK_RET_EPARAM);
664 return CONFIG_MAX_ATA_DEVICES;
671 handle_legacy_disk(struct bregs *regs, u8 drive)
674 floppy_13(regs, drive);
679 // XXX - old code had other disk access method.
680 disk_ret(regs, DISK_RET_EPARAM);
685 u8 device = get_device(regs, 1, drive - 0xe0);
686 if (device >= CONFIG_MAX_ATA_DEVICES)
688 cdrom_13(regs, device);
692 u8 device = get_device(regs, 0, drive - 0x80);
693 if (device >= CONFIG_MAX_ATA_DEVICES)
695 disk_13(regs, device);
699 handle_40(struct bregs *regs)
701 debug_enter(regs, DEBUG_HDL_40);
702 handle_legacy_disk(regs, regs->dl);
705 // INT 13h Fixed Disk Services Entry Point
707 handle_13(struct bregs *regs)
709 debug_enter(regs, DEBUG_HDL_13);
712 if (CONFIG_CDROM_EMU) {
713 if (regs->ah == 0x4b) {
717 u16 ebda_seg = get_ebda_seg();
718 if (GET_EBDA2(ebda_seg, cdemu.active)) {
719 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
720 if (drive == emudrive) {
724 if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
728 handle_legacy_disk(regs, drive);
731 // record completion in BIOS task complete flag
735 debug_isr(DEBUG_ISR_76);
736 SET_BDA(disk_interrupt_flag, 0xff);
740 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
741 struct fdpt_s OldFDPT VAR16FIXED(0xe401);