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 GPLv3 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 "ata.h" // ATA_*
13 #include "pic.h" // eoi_pic2
14 #include "bregs.h" // struct bregs
15 #include "pci.h" // pci_bdf_to_bus
18 /****************************************************************
20 ****************************************************************/
23 __disk_ret(const char *fname, int lineno, struct bregs *regs, u8 code)
25 SET_BDA(disk_last_status, code);
27 __set_code_fail(fname, lineno, regs, code);
29 set_code_success(regs);
33 __disk_stub(const char *fname, int lineno, struct bregs *regs)
35 __debug_stub(fname, lineno, regs);
36 __disk_ret(fname, lineno, regs, DISK_RET_SUCCESS);
39 #define DISK_STUB(regs) \
40 __disk_stub(__func__, __LINE__, (regs))
43 basic_access(struct bregs *regs, u8 device, u16 command)
45 u8 type = GET_EBDA(ata.devices[device].type);
47 if (type == ATA_TYPE_ATA) {
48 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
49 nlh = GET_EBDA(ata.devices[device].lchs.heads);
50 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
52 // Must be cd emulation.
53 nlc = GET_EBDA(cdemu.vdevice.cylinders);
54 nlh = GET_EBDA(cdemu.vdevice.heads);
55 nlspt = GET_EBDA(cdemu.vdevice.spt);
59 u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
60 u16 sector = regs->cl & 0x3f;
63 if (count > 128 || count == 0 || sector == 0) {
64 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
66 disk_ret(regs, DISK_RET_EPARAM);
70 // sanity check on cyl heads, sec
71 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
72 dprintf(1, "int13_harddisk: function %02x, parameters out of"
73 " range %04x/%04x/%04x!\n"
74 , regs->ah, cylinder, head, sector);
75 disk_ret(regs, DISK_RET_EPARAM);
81 disk_ret(regs, DISK_RET_SUCCESS);
85 // translate lchs to lba
86 u32 lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
89 u16 segment = regs->es;
90 u16 offset = regs->bx;
91 void *far_buffer = MAKE_FARPTR(segment, offset);
96 if (type == ATA_TYPE_ATA)
97 status = ata_cmd_data(device, command, lba, count, far_buffer);
99 status = cdrom_read_emu(device, lba, count, far_buffer);
103 // Set nb of sector transferred
104 regs->al = GET_EBDA(ata.trsfsectors);
107 dprintf(1, "int13_harddisk: function %02x, error %d!\n"
109 disk_ret(regs, DISK_RET_EBADTRACK);
111 disk_ret(regs, DISK_RET_SUCCESS);
115 extended_access(struct bregs *regs, u8 device, u16 command)
117 // Get lba and check.
118 u64 lba = GET_INT13EXT(regs, lba);
119 u8 type = GET_EBDA(ata.devices[device].type);
120 if (type == ATA_TYPE_ATA
121 && lba >= GET_EBDA(ata.devices[device].sectors)) {
122 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
124 disk_ret(regs, DISK_RET_EPARAM);
130 disk_ret(regs, DISK_RET_SUCCESS);
134 u16 segment = GET_INT13EXT(regs, segment);
135 u16 offset = GET_INT13EXT(regs, offset);
136 void *far_buffer = MAKE_FARPTR(segment, offset);
137 u16 count = GET_INT13EXT(regs, count);
139 dprintf(DEBUG_HDL_13, "extacc lba=%d buf=%p count=%d\n"
140 , (u32)lba, far_buffer, count);
145 if (type == ATA_TYPE_ATA)
146 status = ata_cmd_data(device, command, lba, count, far_buffer);
148 status = cdrom_read(device, lba, count, far_buffer);
152 SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
155 dprintf(1, "int13_harddisk: function %02x, error %d!\n"
157 disk_ret(regs, DISK_RET_EBADTRACK);
160 disk_ret(regs, DISK_RET_SUCCESS);
164 /****************************************************************
165 * Hard Drive functions
166 ****************************************************************/
168 // disk controller reset
170 disk_1300(struct bregs *regs, u8 device)
177 disk_1301(struct bregs *regs, u8 device)
179 u8 v = GET_BDA(disk_last_status);
182 // XXX - clear disk_last_status?
187 disk_1302(struct bregs *regs, u8 device)
189 basic_access(regs, device, ATA_CMD_READ_SECTORS);
192 // write disk sectors
194 disk_1303(struct bregs *regs, u8 device)
196 basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
199 // verify disk sectors
201 disk_1304(struct bregs *regs, u8 device)
203 basic_access(regs, device, 0);
209 disk_1305(struct bregs *regs, u8 device)
214 // read disk drive parameters
216 disk_1308(struct bregs *regs, u8 device)
218 // Get logical geometry from table
219 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
220 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
221 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
222 u16 count = GET_EBDA(ata.hdcount);
224 nlc = nlc - 2; /* 0 based , last sector not used */
226 regs->ch = nlc & 0xff;
227 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
229 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
231 // FIXME should set ES & DI
232 disk_ret(regs, DISK_RET_SUCCESS);
235 // initialize drive parameters
237 disk_1309(struct bregs *regs, u8 device)
242 // seek to specified cylinder
244 disk_130c(struct bregs *regs, u8 device)
249 // alternate disk reset
251 disk_130d(struct bregs *regs, u8 device)
258 disk_1310(struct bregs *regs, u8 device)
260 // should look at 40:8E also???
262 // Read the status from controller
263 u8 status = inb(GET_EBDA(ata.channels[device/2].iobase1) + ATA_CB_STAT);
264 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
265 disk_ret(regs, DISK_RET_SUCCESS);
267 disk_ret(regs, DISK_RET_ENOTREADY);
272 disk_1311(struct bregs *regs, u8 device)
277 // controller internal diagnostic
279 disk_1314(struct bregs *regs, u8 device)
284 // read disk drive size
286 disk_1315(struct bregs *regs, u8 device)
288 // Get logical geometry from table
289 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
290 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
291 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
293 // Compute sector count seen by int13
294 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
295 regs->cx = lba >> 16;
296 regs->dx = lba & 0xffff;
298 disk_ret(regs, DISK_RET_SUCCESS);
299 regs->ah = 3; // hard disk accessible
302 // IBM/MS installation check
304 disk_1341(struct bregs *regs, u8 device)
306 regs->bx = 0xaa55; // install check
307 regs->cx = 0x0007; // ext disk access and edd, removable supported
308 disk_ret(regs, DISK_RET_SUCCESS);
309 regs->ah = 0x30; // EDD 3.0
312 // IBM/MS extended read
314 disk_1342(struct bregs *regs, u8 device)
316 extended_access(regs, device, ATA_CMD_READ_SECTORS);
319 // IBM/MS extended write
321 disk_1343(struct bregs *regs, u8 device)
323 extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
328 disk_1344(struct bregs *regs, u8 device)
330 extended_access(regs, device, 0);
333 // IBM/MS lock/unlock drive
335 disk_1345(struct bregs *regs, u8 device)
337 // Always success for HD
338 disk_ret(regs, DISK_RET_SUCCESS);
341 // IBM/MS eject media
343 disk_1346(struct bregs *regs, u8 device)
345 // Volume Not Removable
346 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
349 // IBM/MS extended seek
351 disk_1347(struct bregs *regs, u8 device)
353 extended_access(regs, device, 0);
356 // IBM/MS get drive parameters
358 disk_1348(struct bregs *regs, u8 device)
360 u16 size = GET_INT13DPT(regs, size);
362 // Buffer is too small
364 disk_ret(regs, DISK_RET_EPARAM);
370 u8 type = GET_EBDA(ata.devices[device].type);
371 u16 npc = GET_EBDA(ata.devices[device].pchs.cylinders);
372 u16 nph = GET_EBDA(ata.devices[device].pchs.heads);
373 u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);
374 u64 lba = GET_EBDA(ata.devices[device].sectors);
375 u16 blksize = GET_EBDA(ata.devices[device].blksize);
377 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
378 , size, type, npc, nph, npspt, (u32)lba, blksize);
380 SET_INT13DPT(regs, size, 26);
381 if (type == ATA_TYPE_ATA) {
382 if (lba > (u64)npspt*nph*0x3fff) {
383 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
384 SET_INT13DPT(regs, cylinders, 0x3fff);
386 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
387 SET_INT13DPT(regs, cylinders, (u32)npc);
389 SET_INT13DPT(regs, heads, (u32)nph);
390 SET_INT13DPT(regs, spt, (u32)npspt);
391 SET_INT13DPT(regs, sector_count, lba);
394 // 0x74 = removable, media change, lockable, max values
395 SET_INT13DPT(regs, infos, 0x74);
396 SET_INT13DPT(regs, cylinders, 0xffffffff);
397 SET_INT13DPT(regs, heads, 0xffffffff);
398 SET_INT13DPT(regs, spt, 0xffffffff);
399 SET_INT13DPT(regs, sector_count, (u64)-1);
401 SET_INT13DPT(regs, blksize, blksize);
404 disk_ret(regs, DISK_RET_SUCCESS);
410 SET_INT13DPT(regs, size, 30);
412 SET_INT13DPT(regs, dpte_segment, GET_BDA(ebda_seg));
413 SET_INT13DPT(regs, dpte_offset
414 , offsetof(struct extended_bios_data_area_s, ata.dpte));
417 u8 channel = device / 2;
418 u8 slave = device % 2;
419 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
420 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
421 u8 irq = GET_EBDA(ata.channels[channel].irq);
422 u8 mode = GET_EBDA(ata.devices[device].mode);
425 if (type == ATA_TYPE_ATA) {
426 u8 translation = GET_EBDA(ata.devices[device].translation);
427 if (translation != ATA_TRANSLATION_NONE) {
428 options |= 1<<3; // CHS translation
429 if (translation == ATA_TRANSLATION_LBA)
431 if (translation == ATA_TRANSLATION_RECHS)
436 options |= 1<<5; // removable device
437 options |= 1<<6; // atapi device
439 options |= 1<<4; // lba translation
440 if (mode == ATA_MODE_PIO32)
443 SET_EBDA(ata.dpte.iobase1, iobase1);
444 SET_EBDA(ata.dpte.iobase2, iobase2 + ATA_CB_DC);
445 SET_EBDA(ata.dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
447 SET_EBDA(ata.dpte.unused, 0xcb);
448 SET_EBDA(ata.dpte.irq, irq);
449 SET_EBDA(ata.dpte.blkcount, 1);
450 SET_EBDA(ata.dpte.dma, 0);
451 SET_EBDA(ata.dpte.pio, 0);
452 SET_EBDA(ata.dpte.options, options);
453 SET_EBDA(ata.dpte.reserved, 0);
454 SET_EBDA(ata.dpte.revision, 0x11);
456 u8 *p = MAKE_FARPTR(GET_BDA(ebda_seg)
457 , offsetof(struct extended_bios_data_area_s, ata.dpte));
458 SET_EBDA(ata.dpte.checksum, -checksum(p, 15));
461 disk_ret(regs, DISK_RET_SUCCESS);
466 SET_INT13DPT(regs, key, 0xbedd);
467 SET_INT13DPT(regs, dpi_length, 36);
468 SET_INT13DPT(regs, reserved1, 0);
469 SET_INT13DPT(regs, reserved2, 0);
471 SET_INT13DPT(regs, host_bus[0], 'P');
472 SET_INT13DPT(regs, host_bus[1], 'C');
473 SET_INT13DPT(regs, host_bus[2], 'I');
474 SET_INT13DPT(regs, host_bus[3], 0);
476 u32 bdf = GET_EBDA(ata.channels[channel].pci_bdf);
477 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
478 | (pci_bdf_to_fn(bdf) << 16));
479 SET_INT13DPT(regs, iface_path, path);
481 SET_INT13DPT(regs, iface_type[0], 'A');
482 SET_INT13DPT(regs, iface_type[1], 'T');
483 SET_INT13DPT(regs, iface_type[2], 'A');
484 SET_INT13DPT(regs, iface_type[3], 0);
485 SET_INT13DPT(regs, iface_type[4], 0);
486 SET_INT13DPT(regs, iface_type[5], 0);
487 SET_INT13DPT(regs, iface_type[6], 0);
488 SET_INT13DPT(regs, iface_type[7], 0);
490 SET_INT13DPT(regs, device_path, slave);
492 SET_INT13DPT(regs, checksum, -checksum(MAKE_FARPTR(regs->ds, 30), 35));
494 disk_ret(regs, DISK_RET_SUCCESS);
497 // IBM/MS extended media change
499 disk_1349(struct bregs *regs, u8 device)
501 // Always success for HD
502 disk_ret(regs, DISK_RET_SUCCESS);
506 disk_134e01(struct bregs *regs, u8 device)
508 disk_ret(regs, DISK_RET_SUCCESS);
512 disk_134e03(struct bregs *regs, u8 device)
514 disk_ret(regs, DISK_RET_SUCCESS);
518 disk_134e04(struct bregs *regs, u8 device)
520 disk_ret(regs, DISK_RET_SUCCESS);
524 disk_134e06(struct bregs *regs, u8 device)
526 disk_ret(regs, DISK_RET_SUCCESS);
530 disk_134eXX(struct bregs *regs, u8 device)
532 disk_ret(regs, DISK_RET_EPARAM);
535 // IBM/MS set hardware configuration
537 disk_134e(struct bregs *regs, u8 device)
540 case 0x01: disk_134e01(regs, device); break;
541 case 0x03: disk_134e03(regs, device); break;
542 case 0x04: disk_134e04(regs, device); break;
543 case 0x06: disk_134e06(regs, device); break;
544 default: disk_134eXX(regs, device); break;
549 disk_13XX(struct bregs *regs, u8 device)
551 disk_ret(regs, DISK_RET_EPARAM);
555 disk_13(struct bregs *regs, u8 device)
559 // clear completion flag
560 SET_BDA(disk_interrupt_flag, 0);
563 case 0x00: disk_1300(regs, device); break;
564 case 0x01: disk_1301(regs, device); break;
565 case 0x02: disk_1302(regs, device); break;
566 case 0x03: disk_1303(regs, device); break;
567 case 0x04: disk_1304(regs, device); break;
568 case 0x05: disk_1305(regs, device); break;
569 case 0x08: disk_1308(regs, device); break;
570 case 0x09: disk_1309(regs, device); break;
571 case 0x0c: disk_130c(regs, device); break;
572 case 0x0d: disk_130d(regs, device); break;
573 case 0x10: disk_1310(regs, device); break;
574 case 0x11: disk_1311(regs, device); break;
575 case 0x14: disk_1314(regs, device); break;
576 case 0x15: disk_1315(regs, device); break;
577 case 0x41: disk_1341(regs, device); break;
578 case 0x42: disk_1342(regs, device); break;
579 case 0x43: disk_1343(regs, device); break;
580 case 0x44: disk_1344(regs, device); break;
581 case 0x45: disk_1345(regs, device); break;
582 case 0x46: disk_1346(regs, device); break;
583 case 0x47: disk_1347(regs, device); break;
584 case 0x48: disk_1348(regs, device); break;
585 case 0x49: disk_1349(regs, device); break;
586 case 0x4e: disk_134e(regs, device); break;
587 default: disk_13XX(regs, device); break;
592 /****************************************************************
594 ****************************************************************/
597 get_device(struct bregs *regs, u8 iscd, u8 drive)
599 // basic check : device has to be defined
600 if (drive >= CONFIG_MAX_ATA_DEVICES) {
601 disk_ret(regs, DISK_RET_EPARAM);
602 return CONFIG_MAX_ATA_DEVICES;
605 // Get the ata channel
606 u8 device = GET_EBDA(ata.idmap[iscd][drive]);
608 // basic check : device has to be valid
609 if (device >= CONFIG_MAX_ATA_DEVICES) {
610 disk_ret(regs, DISK_RET_EPARAM);
611 return CONFIG_MAX_ATA_DEVICES;
618 handle_legacy_disk(struct bregs *regs, u8 drive)
621 floppy_13(regs, drive);
626 // XXX - old code had other disk access method.
627 disk_ret(regs, DISK_RET_EPARAM);
632 u8 device = get_device(regs, 1, drive - 0xe0);
633 if (device >= CONFIG_MAX_ATA_DEVICES)
635 cdrom_13(regs, device);
639 u8 device = get_device(regs, 0, drive - 0x80);
640 if (device >= CONFIG_MAX_ATA_DEVICES)
642 disk_13(regs, device);
646 handle_40(struct bregs *regs)
648 debug_enter(regs, DEBUG_HDL_40);
649 handle_legacy_disk(regs, regs->dl);
652 // INT 13h Fixed Disk Services Entry Point
654 handle_13(struct bregs *regs)
656 debug_enter(regs, DEBUG_HDL_13);
659 if (CONFIG_CDROM_EMU) {
660 if (regs->ah == 0x4b) {
664 if (GET_EBDA(cdemu.active)) {
665 if (drive == GET_EBDA(cdemu.emulated_drive)) {
673 handle_legacy_disk(regs, drive);
676 // record completion in BIOS task complete flag
680 debug_isr(DEBUG_ISR_76);
681 SET_BDA(disk_interrupt_flag, 0xff);