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 "biosvar.h" // SET_BDA
9 #include "bregs.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "hw/ata.h" // ATA_CB_DC
12 #include "hw/pic.h" // pic_eoi2
13 #include "output.h" // debug_enter
14 #include "stacks.h" // call16_int
15 #include "std/disk.h" // DISK_RET_SUCCESS
16 #include "string.h" // memset
17 #include "util.h" // CDRom_locks
20 /****************************************************************
21 * Return status functions
22 ****************************************************************/
25 __disk_ret(struct bregs *regs, u32 linecode, const char *fname)
28 if (regs->dl < EXTSTART_HD)
29 SET_BDA(floppy_last_status, code);
31 SET_BDA(disk_last_status, code);
33 __set_code_invalid(regs, linecode, fname);
35 set_code_success(regs);
39 __disk_ret_unimplemented(struct bregs *regs, u32 linecode, const char *fname)
42 if (regs->dl < EXTSTART_HD)
43 SET_BDA(floppy_last_status, code);
45 SET_BDA(disk_last_status, code);
46 __set_code_unimplemented(regs, linecode, fname);
50 __disk_stub(struct bregs *regs, int lineno, const char *fname)
52 __warn_unimplemented(regs, lineno, fname);
53 __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
56 #define disk_ret(regs, code) \
57 __disk_ret((regs), (code) | (__LINE__ << 8), __func__)
58 #define disk_ret_unimplemented(regs, code) \
59 __disk_ret_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
60 #define DISK_STUB(regs) \
61 __disk_stub((regs), __LINE__, __func__)
64 /****************************************************************
66 ****************************************************************/
68 // Get the cylinders/heads/sectors for the given drive.
70 getLCHS(struct drive_s *drive_gf)
72 struct chs_s res = { };
73 if (CONFIG_CDROM_EMU && drive_gf == GET_GLOBAL(cdemu_drive_gf)) {
74 // Emulated drive - get info from CDEmu. (It's not possible to
75 // populate the geometry directly in the driveid because the
76 // geometry is only known after the bios segment is made
78 u8 sptcyl = GET_LOW(CDEmu.chs.sptcyl);
79 res.cylinder = GET_LOW(CDEmu.chs.cyllow) + ((sptcyl << 2) & 0x300) + 1;
80 res.head = GET_LOW(CDEmu.chs.heads) + 1;
81 res.sector = sptcyl & 0x3f;
84 res.cylinder = GET_GLOBALFLAT(drive_gf->lchs.cylinder);
85 res.head = GET_GLOBALFLAT(drive_gf->lchs.head);
86 res.sector = GET_GLOBALFLAT(drive_gf->lchs.sector);
90 // Perform read/write/verify using old-style chs accesses
92 basic_access(struct bregs *regs, struct drive_s *drive_gf, u16 command)
95 dop.drive_gf = drive_gf;
96 dop.command = command;
99 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
100 u16 sector = regs->cl & 0x3f;
103 if (count > 128 || count == 0 || sector == 0) {
105 disk_ret(regs, DISK_RET_EPARAM);
110 struct chs_s chs = getLCHS(drive_gf);
111 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
113 // sanity check on cyl heads, sec
114 if (cylinder >= nlc || head >= nlh || sector > nls) {
116 disk_ret(regs, DISK_RET_EPARAM);
120 // translate lchs to lba
121 dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nls)
124 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
126 int status = send_disk_op(&dop);
128 regs->al = dop.count;
130 disk_ret(regs, status);
133 // Perform read/write/verify using new-style "int13ext" accesses.
135 extended_access(struct bregs *regs, struct drive_s *drive_gf, u16 command)
137 struct disk_op_s dop;
138 struct int13ext_s *param_far = (struct int13ext_s*)(regs->si+0);
139 // Get lba and check.
140 dop.lba = GET_FARVAR(regs->ds, param_far->lba);
141 dop.command = command;
142 dop.drive_gf = drive_gf;
143 if (dop.lba >= GET_GLOBALFLAT(drive_gf->sectors)) {
145 disk_ret(regs, DISK_RET_EPARAM);
149 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_FARVAR(regs->ds, param_far->data));
150 dop.count = GET_FARVAR(regs->ds, param_far->count);
153 disk_ret(regs, DISK_RET_SUCCESS);
157 int status = send_disk_op(&dop);
159 SET_FARVAR(regs->ds, param_far->count, dop.count);
161 disk_ret(regs, status);
165 /****************************************************************
166 * Hard Drive functions
167 ****************************************************************/
169 // disk controller reset
171 disk_1300(struct bregs *regs, struct drive_s *drive_gf)
173 struct disk_op_s dop;
174 dop.drive_gf = drive_gf;
175 dop.command = CMD_RESET;
177 int status = send_disk_op(&dop);
178 disk_ret(regs, status);
183 disk_1301(struct bregs *regs, struct drive_s *drive_gf)
186 if (regs->dl < EXTSTART_HD)
188 v = GET_BDA(floppy_last_status);
190 v = GET_BDA(disk_last_status);
193 // XXX - clear disk_last_status?
198 disk_1302(struct bregs *regs, struct drive_s *drive_gf)
200 basic_access(regs, drive_gf, CMD_READ);
203 // write disk sectors
205 disk_1303(struct bregs *regs, struct drive_s *drive_gf)
207 basic_access(regs, drive_gf, CMD_WRITE);
210 // verify disk sectors
212 disk_1304(struct bregs *regs, struct drive_s *drive_gf)
214 basic_access(regs, drive_gf, CMD_VERIFY);
219 disk_1305(struct bregs *regs, struct drive_s *drive_gf)
223 struct chs_s chs = getLCHS(drive_gf);
224 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
227 u8 cylinder = regs->ch;
230 if (cylinder >= nlc || head >= nlh || count == 0 || count > nls) {
231 disk_ret(regs, DISK_RET_EPARAM);
235 struct disk_op_s dop;
236 dop.drive_gf = drive_gf;
237 dop.command = CMD_FORMAT;
238 dop.lba = (((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nls;
240 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
241 int status = send_disk_op(&dop);
242 disk_ret(regs, status);
245 // read disk drive parameters
247 disk_1308(struct bregs *regs, struct drive_s *drive_gf)
249 // Get logical geometry from table
250 struct chs_s chs = getLCHS(drive_gf);
251 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
255 if (regs->dl < EXTSTART_HD) {
257 count = GET_GLOBAL(FloppyCount);
259 if (CONFIG_CDROM_EMU && drive_gf == GET_GLOBAL(cdemu_drive_gf))
260 regs->bx = GET_LOW(CDEmu.media) * 2;
262 regs->bx = GET_GLOBALFLAT(drive_gf->floppy_type);
264 // set es & di to point to 11 byte diskette param table in ROM
266 regs->di = (u32)&diskette_param_table2;
267 } else if (regs->dl < EXTSTART_CD) {
269 count = GET_BDA(hdcount);
270 nlc--; // last sector reserved
272 // Not supported on CDROM
273 disk_ret(regs, DISK_RET_EPARAM);
277 if (CONFIG_CDROM_EMU && GET_LOW(CDEmu.media)) {
278 u8 emudrive = GET_LOW(CDEmu.emulated_drive);
279 if (((emudrive ^ regs->dl) & 0x80) == 0)
280 // Note extra drive due to emulation.
282 if (regs->dl < EXTSTART_HD && count > 2)
283 // Max of two floppy drives.
288 regs->ch = nlc & 0xff;
289 regs->cl = ((nlc >> 2) & 0xc0) | (nls & 0x3f);
292 disk_ret(regs, DISK_RET_SUCCESS);
296 // initialize drive parameters
298 disk_1309(struct bregs *regs, struct drive_s *drive_gf)
303 // seek to specified cylinder
305 disk_130c(struct bregs *regs, struct drive_s *drive_gf)
310 // alternate disk reset
312 disk_130d(struct bregs *regs, struct drive_s *drive_gf)
319 disk_1310(struct bregs *regs, struct drive_s *drive_gf)
321 // should look at 40:8E also???
323 struct disk_op_s dop;
324 dop.drive_gf = drive_gf;
325 dop.command = CMD_ISREADY;
327 int status = send_disk_op(&dop);
328 disk_ret(regs, status);
333 disk_1311(struct bregs *regs, struct drive_s *drive_gf)
338 // controller internal diagnostic
340 disk_1314(struct bregs *regs, struct drive_s *drive_gf)
345 // read disk drive size
347 disk_1315(struct bregs *regs, struct drive_s *drive_gf)
349 disk_ret(regs, DISK_RET_SUCCESS);
350 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
357 // Get logical geometry from table
358 struct chs_s chs = getLCHS(drive_gf);
359 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
361 // Compute sector count seen by int13
362 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nls;
363 regs->cx = lba >> 16;
364 regs->dx = lba & 0xffff;
365 regs->ah = 3; // hard disk accessible
369 disk_1316(struct bregs *regs, struct drive_s *drive_gf)
371 if (regs->dl >= EXTSTART_HD) {
373 disk_ret(regs, DISK_RET_EPARAM);
376 disk_ret(regs, DISK_RET_ECHANGED);
379 // IBM/MS installation check
381 disk_1341(struct bregs *regs, struct drive_s *drive_gf)
383 regs->bx = 0xaa55; // install check
384 regs->cx = 0x0007; // ext disk access and edd, removable supported
385 disk_ret(regs, DISK_RET_SUCCESS);
386 regs->ah = 0x30; // EDD 3.0
389 // IBM/MS extended read
391 disk_1342(struct bregs *regs, struct drive_s *drive_gf)
393 extended_access(regs, drive_gf, CMD_READ);
396 // IBM/MS extended write
398 disk_1343(struct bregs *regs, struct drive_s *drive_gf)
400 extended_access(regs, drive_gf, CMD_WRITE);
405 disk_1344(struct bregs *regs, struct drive_s *drive_gf)
407 extended_access(regs, drive_gf, CMD_VERIFY);
410 // Locks for removable devices
411 u8 CDRom_locks[BUILD_MAX_EXTDRIVE] VARLOW;
415 disk_134500(struct bregs *regs, struct drive_s *drive_gf)
417 int cdid = regs->dl - EXTSTART_CD;
418 u8 locks = GET_LOW(CDRom_locks[cdid]);
421 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
424 SET_LOW(CDRom_locks[cdid], locks + 1);
426 disk_ret(regs, DISK_RET_SUCCESS);
431 disk_134501(struct bregs *regs, struct drive_s *drive_gf)
433 int cdid = regs->dl - EXTSTART_CD;
434 u8 locks = GET_LOW(CDRom_locks[cdid]);
437 disk_ret(regs, DISK_RET_ENOTLOCKED);
441 SET_LOW(CDRom_locks[cdid], locks);
442 regs->al = (locks ? 1 : 0);
443 disk_ret(regs, DISK_RET_SUCCESS);
448 disk_134502(struct bregs *regs, struct drive_s *drive_gf)
450 int cdid = regs->dl - EXTSTART_CD;
451 u8 locks = GET_LOW(CDRom_locks[cdid]);
452 regs->al = (locks ? 1 : 0);
453 disk_ret(regs, DISK_RET_SUCCESS);
457 disk_1345XX(struct bregs *regs, struct drive_s *drive_gf)
459 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
462 // IBM/MS lock/unlock drive
464 disk_1345(struct bregs *regs, struct drive_s *drive_gf)
466 if (regs->dl < EXTSTART_CD) {
467 // Always success for HD
468 disk_ret(regs, DISK_RET_SUCCESS);
473 case 0x00: disk_134500(regs, drive_gf); break;
474 case 0x01: disk_134501(regs, drive_gf); break;
475 case 0x02: disk_134502(regs, drive_gf); break;
476 default: disk_1345XX(regs, drive_gf); break;
480 // IBM/MS eject media
482 disk_1346(struct bregs *regs, struct drive_s *drive_gf)
484 if (regs->dl < EXTSTART_CD) {
485 // Volume Not Removable
486 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
490 int cdid = regs->dl - EXTSTART_CD;
491 u8 locks = GET_LOW(CDRom_locks[cdid]);
493 disk_ret(regs, DISK_RET_ELOCKED);
497 // FIXME should handle 0x31 no media in device
498 // FIXME should handle 0xb5 valid request failed
500 // Call removable media eject
502 memset(&br, 0, sizeof(br));
505 call16_int(0x15, &br);
507 if (br.ah || br.flags & F_CF) {
508 disk_ret(regs, DISK_RET_ELOCKED);
511 disk_ret(regs, DISK_RET_SUCCESS);
514 // IBM/MS extended seek
516 disk_1347(struct bregs *regs, struct drive_s *drive_gf)
518 extended_access(regs, drive_gf, CMD_SEEK);
521 // IBM/MS get drive parameters
523 disk_1348(struct bregs *regs, struct drive_s *drive_gf)
525 int ret = fill_edd(SEGOFF(regs->ds, regs->si), drive_gf);
529 // IBM/MS extended media change
531 disk_1349(struct bregs *regs, struct drive_s *drive_gf)
533 if (regs->dl < EXTSTART_CD) {
534 // Always success for HD
535 disk_ret(regs, DISK_RET_SUCCESS);
539 // always send changed ??
540 regs->ah = DISK_RET_ECHANGED;
544 disk_134e01(struct bregs *regs, struct drive_s *drive_gf)
546 disk_ret(regs, DISK_RET_SUCCESS);
550 disk_134e03(struct bregs *regs, struct drive_s *drive_gf)
552 disk_ret(regs, DISK_RET_SUCCESS);
556 disk_134e04(struct bregs *regs, struct drive_s *drive_gf)
558 disk_ret(regs, DISK_RET_SUCCESS);
562 disk_134e06(struct bregs *regs, struct drive_s *drive_gf)
564 disk_ret(regs, DISK_RET_SUCCESS);
568 disk_134eXX(struct bregs *regs, struct drive_s *drive_gf)
570 disk_ret(regs, DISK_RET_EPARAM);
573 // IBM/MS set hardware configuration
575 disk_134e(struct bregs *regs, struct drive_s *drive_gf)
578 case 0x01: disk_134e01(regs, drive_gf); break;
579 case 0x03: disk_134e03(regs, drive_gf); break;
580 case 0x04: disk_134e04(regs, drive_gf); break;
581 case 0x06: disk_134e06(regs, drive_gf); break;
582 default: disk_134eXX(regs, drive_gf); break;
587 disk_13XX(struct bregs *regs, struct drive_s *drive_gf)
589 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
593 disk_13(struct bregs *regs, struct drive_s *drive_gf)
597 // clear completion flag
598 SET_BDA(disk_interrupt_flag, 0);
601 case 0x00: disk_1300(regs, drive_gf); break;
602 case 0x01: disk_1301(regs, drive_gf); break;
603 case 0x02: disk_1302(regs, drive_gf); break;
604 case 0x03: disk_1303(regs, drive_gf); break;
605 case 0x04: disk_1304(regs, drive_gf); break;
606 case 0x05: disk_1305(regs, drive_gf); break;
607 case 0x08: disk_1308(regs, drive_gf); break;
608 case 0x09: disk_1309(regs, drive_gf); break;
609 case 0x0c: disk_130c(regs, drive_gf); break;
610 case 0x0d: disk_130d(regs, drive_gf); break;
611 case 0x10: disk_1310(regs, drive_gf); break;
612 case 0x11: disk_1311(regs, drive_gf); break;
613 case 0x14: disk_1314(regs, drive_gf); break;
614 case 0x15: disk_1315(regs, drive_gf); break;
615 case 0x16: disk_1316(regs, drive_gf); break;
616 case 0x41: disk_1341(regs, drive_gf); break;
617 case 0x42: disk_1342(regs, drive_gf); break;
618 case 0x43: disk_1343(regs, drive_gf); break;
619 case 0x44: disk_1344(regs, drive_gf); break;
620 case 0x45: disk_1345(regs, drive_gf); break;
621 case 0x46: disk_1346(regs, drive_gf); break;
622 case 0x47: disk_1347(regs, drive_gf); break;
623 case 0x48: disk_1348(regs, drive_gf); break;
624 case 0x49: disk_1349(regs, drive_gf); break;
625 case 0x4e: disk_134e(regs, drive_gf); break;
626 default: disk_13XX(regs, drive_gf); break;
631 floppy_13(struct bregs *regs, struct drive_s *drive_gf)
633 // Only limited commands are supported on floppies.
644 disk_13(regs, drive_gf);
646 default: disk_13XX(regs, drive_gf); break;
650 // ElTorito - Terminate disk emu
652 cdemu_134b(struct bregs *regs)
654 memcpy_far(regs->ds, (void*)(regs->si+0), SEG_LOW, &CDEmu, sizeof(CDEmu));
656 // If we have to terminate emulation
657 if (regs->al == 0x00) {
658 // FIXME ElTorito Various. Should be handled accordingly to spec
659 SET_LOW(CDEmu.media, 0x00); // bye bye
661 // XXX - update floppy/hd count.
664 disk_ret(regs, DISK_RET_SUCCESS);
668 /****************************************************************
670 ****************************************************************/
673 handle_legacy_disk(struct bregs *regs, u8 extdrive)
675 if (! CONFIG_DRIVES) {
676 // XXX - support handle_1301 anyway?
677 disk_ret(regs, DISK_RET_EPARAM);
681 if (extdrive < EXTSTART_HD) {
682 struct drive_s *drive_gf = getDrive(EXTTYPE_FLOPPY, extdrive);
685 floppy_13(regs, drive_gf);
689 struct drive_s *drive_gf;
690 if (extdrive >= EXTSTART_CD)
691 drive_gf = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
693 drive_gf = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
696 disk_13(regs, drive_gf);
700 // XXX - support 1301/1308/1315 anyway?
701 disk_ret(regs, DISK_RET_EPARAM);
705 handle_40(struct bregs *regs)
707 debug_enter(regs, DEBUG_HDL_40);
708 handle_legacy_disk(regs, regs->dl);
711 // INT 13h Fixed Disk Services Entry Point
713 handle_13(struct bregs *regs)
715 debug_enter(regs, DEBUG_HDL_13);
716 u8 extdrive = regs->dl;
718 if (CONFIG_CDROM_EMU) {
719 if (regs->ah == 0x4b) {
723 if (GET_LOW(CDEmu.media)) {
724 u8 emudrive = GET_LOW(CDEmu.emulated_drive);
725 if (extdrive == emudrive) {
726 // Access to an emulated drive.
727 struct drive_s *cdemu_gf = GET_GLOBAL(cdemu_drive_gf);
728 if (regs->ah > 0x16) {
729 // Only old-style commands supported.
730 disk_13XX(regs, cdemu_gf);
733 disk_13(regs, cdemu_gf);
736 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
737 // Adjust id to make room for emulated drive.
741 handle_legacy_disk(regs, extdrive);
744 // record completion in BIOS task complete flag
748 debug_isr(DEBUG_ISR_76);
749 SET_BDA(disk_interrupt_flag, 0xff);