blob: 0328fbd1ad07eee80892bdc320d085c66827b697 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 16bit code to access hard drives.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor9521e262008-07-04 13:04:29 -04008#include "biosvar.h" // SET_BDA
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "bregs.h" // struct bregs
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040010#include "config.h" // CONFIG_*
Kevin O'Connor5d369d82013-09-02 20:48:46 -040011#include "hw/ata.h" // ATA_CB_DC
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "hw/pic.h" // pic_eoi2
13#include "output.h" // debug_enter
Kevin O'Connor3df600b2013-09-14 19:28:55 -040014#include "stacks.h" // call16_int
Kevin O'Connor135f3f62013-09-14 23:57:26 -040015#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040016#include "string.h" // memset
Kevin O'Connor135f3f62013-09-14 23:57:26 -040017#include "util.h" // CDRom_locks
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050018
Kevin O'Connorb74102d2008-03-03 21:57:30 -050019
20/****************************************************************
Kevin O'Connorc029a912014-05-10 15:51:40 -040021 * Return status functions
Kevin O'Connorb74102d2008-03-03 21:57:30 -050022 ****************************************************************/
23
Kevin O'Connor567e4e32008-04-05 11:37:51 -040024static void
Kevin O'Connorc029a912014-05-10 15:51:40 -040025__disk_ret(struct bregs *regs, u32 linecode, const char *fname)
26{
27 u8 code = linecode;
28 if (regs->dl < EXTSTART_HD)
29 SET_BDA(floppy_last_status, code);
30 else
31 SET_BDA(disk_last_status, code);
32 if (code)
33 __set_code_invalid(regs, linecode, fname);
34 else
35 set_code_success(regs);
36}
37
38static void
39__disk_ret_unimplemented(struct bregs *regs, u32 linecode, const char *fname)
40{
41 u8 code = linecode;
42 if (regs->dl < EXTSTART_HD)
43 SET_BDA(floppy_last_status, code);
44 else
45 SET_BDA(disk_last_status, code);
46 __set_code_unimplemented(regs, linecode, fname);
47}
48
49static void
Kevin O'Connor05600342009-01-02 13:10:58 -050050__disk_stub(struct bregs *regs, int lineno, const char *fname)
Kevin O'Connor567e4e32008-04-05 11:37:51 -040051{
Kevin O'Connordfefeb52009-12-13 13:04:17 -050052 __warn_unimplemented(regs, lineno, fname);
Kevin O'Connor05600342009-01-02 13:10:58 -050053 __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040054}
55
Kevin O'Connorc029a912014-05-10 15:51:40 -040056#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__)
Kevin O'Connor05600342009-01-02 13:10:58 -050060#define DISK_STUB(regs) \
61 __disk_stub((regs), __LINE__, __func__)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050062
Kevin O'Connorc029a912014-05-10 15:51:40 -040063
64/****************************************************************
65 * Helper functions
66 ****************************************************************/
67
Kevin O'Connor5c99b6c2009-12-30 12:36:22 -050068// Get the cylinders/heads/sectors for the given drive.
Kevin O'Connor48365582012-05-27 10:45:32 -040069static struct chs_s
Kevin O'Connore5a0b612017-07-11 12:24:50 -040070getLCHS(struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050071{
Kevin O'Connor48365582012-05-27 10:45:32 -040072 struct chs_s res = { };
Kevin O'Connore5a0b612017-07-11 12:24:50 -040073 if (CONFIG_CDROM_EMU && drive_fl == GET_GLOBAL(cdemu_drive_gf)) {
Kevin O'Connord3140832012-05-13 22:46:12 -040074 // Emulated drive - get info from CDEmu. (It's not possible to
Kevin O'Connor36c93a52009-09-12 19:35:04 -040075 // populate the geometry directly in the driveid because the
76 // geometry is only known after the bios segment is made
77 // read-only).
Kevin O'Connor02f76762014-05-10 01:20:46 -040078 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;
Kevin O'Connor48365582012-05-27 10:45:32 -040082 return res;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040083 }
Kevin O'Connore5a0b612017-07-11 12:24:50 -040084 res.cylinder = GET_FLATPTR(drive_fl->lchs.cylinder);
85 res.head = GET_FLATPTR(drive_fl->lchs.head);
86 res.sector = GET_FLATPTR(drive_fl->lchs.sector);
Kevin O'Connor48365582012-05-27 10:45:32 -040087 return res;
Kevin O'Connor1625a752009-08-09 13:08:21 -040088}
89
Kevin O'Connor8ef686f2016-03-31 14:29:07 -040090// Execute a "disk_op_s" request after jumping to the extra stack.
Kevin O'Connor193632b2016-03-31 14:20:30 -040091static int
92__send_disk_op(struct disk_op_s *op_far, u16 op_seg)
93{
94 struct disk_op_s dop;
Kevin O'Connor8ef686f2016-03-31 14:29:07 -040095 memcpy_far(GET_SEG(SS), &dop, op_seg, op_far, sizeof(dop));
Kevin O'Connor193632b2016-03-31 14:20:30 -040096
97 int status = process_op(&dop);
98
99 // Update count with total sectors transferred.
100 SET_FARVAR(op_seg, op_far->count, dop.count);
101
102 return status;
103}
104
Kevin O'Connor8ef686f2016-03-31 14:29:07 -0400105// Execute a "disk_op_s" request (using the extra 16bit stack).
Kevin O'Connor193632b2016-03-31 14:20:30 -0400106static int
107send_disk_op(struct disk_op_s *op)
108{
109 ASSERT16();
110 if (! CONFIG_DRIVES)
111 return -1;
Kevin O'Connor8ef686f2016-03-31 14:29:07 -0400112 if (!CONFIG_ENTRY_EXTRASTACK)
113 // Jump on to extra stack
114 return stack_hop(__send_disk_op, op, GET_SEG(SS));
115 return process_op(op);
Kevin O'Connor193632b2016-03-31 14:20:30 -0400116}
117
Kevin O'Connor1625a752009-08-09 13:08:21 -0400118// Perform read/write/verify using old-style chs accesses
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400119static void noinline
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400120basic_access(struct bregs *regs, struct drive_s *drive_fl, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400121{
122 struct disk_op_s dop;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400123 dop.drive_fl = drive_fl;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400124 dop.command = command;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400125
126 u8 count = regs->al;
127 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
128 u16 sector = regs->cl & 0x3f;
129 u16 head = regs->dh;
130
131 if (count > 128 || count == 0 || sector == 0) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500132 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400133 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -0400134 return;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400135 }
136 dop.count = count;
137
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400138 struct chs_s chs = getLCHS(drive_fl);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400139 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400140
141 // sanity check on cyl heads, sec
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400142 if (cylinder >= nlc || head >= nlh || sector > nls) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500143 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400144 disk_ret(regs, DISK_RET_EPARAM);
145 return;
146 }
147
148 // translate lchs to lba
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400149 dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nls)
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400150 + (u32)sector - 1);
151
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400152 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400153
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500154 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400155
Kevin O'Connor1625a752009-08-09 13:08:21 -0400156 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500157
Kevin O'Connor126eac62009-08-16 13:32:24 -0400158 disk_ret(regs, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500159}
160
Kevin O'Connor1625a752009-08-09 13:08:21 -0400161// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400162static void noinline
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400163extended_access(struct bregs *regs, struct drive_s *drive_fl, u16 command)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500164{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500165 struct disk_op_s dop;
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400166 struct int13ext_s *param_far = (struct int13ext_s*)(regs->si+0);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400167 // Get lba and check.
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400168 dop.lba = GET_FARVAR(regs->ds, param_far->lba);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500169 dop.command = command;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400170 dop.drive_fl = drive_fl;
171 if (dop.lba >= GET_FLATPTR(drive_fl->sectors)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500172 warn_invalid(regs);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400173 disk_ret(regs, DISK_RET_EPARAM);
174 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500175 }
176
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400177 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_FARVAR(regs->ds, param_far->data));
178 dop.count = GET_FARVAR(regs->ds, param_far->count);
Kevin O'Connorb50488b2012-02-11 09:38:44 -0500179 if (! dop.count) {
180 // Nothing to do.
181 disk_ret(regs, DISK_RET_SUCCESS);
182 return;
183 }
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400184
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500185 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400186
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400187 SET_FARVAR(regs->ds, param_far->count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500188
Kevin O'Connor126eac62009-08-16 13:32:24 -0400189 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500190}
191
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500192
193/****************************************************************
194 * Hard Drive functions
195 ****************************************************************/
196
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500197// disk controller reset
198static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400199disk_1300(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500200{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400201 struct disk_op_s dop;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400202 dop.drive_fl = drive_fl;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400203 dop.command = CMD_RESET;
Kevin O'Connor45e71722014-12-29 10:29:34 -0500204 dop.count = 0;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400205 int status = send_disk_op(&dop);
206 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500207}
208
209// read disk status
210static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400211disk_1301(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500212{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400213 u8 v;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400214 if (regs->dl < EXTSTART_HD)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400215 // Floppy
216 v = GET_BDA(floppy_last_status);
217 else
218 v = GET_BDA(disk_last_status);
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400219 regs->ah = v;
220 set_cf(regs, v);
221 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500222}
223
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500224// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500225static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400226disk_1302(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500227{
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400228 basic_access(regs, drive_fl, CMD_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500229}
230
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500231// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500232static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400233disk_1303(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500234{
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400235 basic_access(regs, drive_fl, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500236}
237
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500238// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500239static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400240disk_1304(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500241{
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400242 basic_access(regs, drive_fl, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500243}
244
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500245// format disk track
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400246static void noinline
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400247disk_1305(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500248{
Kevin O'Connor4d9d4002009-11-25 19:35:01 -0500249 debug_stub(regs);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400250
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400251 struct chs_s chs = getLCHS(drive_fl);
Kevin O'Connorb84899e2013-12-11 19:15:45 -0500252 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400253
Kevin O'Connorb84899e2013-12-11 19:15:45 -0500254 u8 count = regs->al;
255 u8 cylinder = regs->ch;
256 u8 head = regs->dh;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400257
Kevin O'Connorb84899e2013-12-11 19:15:45 -0500258 if (cylinder >= nlc || head >= nlh || count == 0 || count > nls) {
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400259 disk_ret(regs, DISK_RET_EPARAM);
260 return;
261 }
262
263 struct disk_op_s dop;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400264 dop.drive_fl = drive_fl;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400265 dop.command = CMD_FORMAT;
Kevin O'Connorb84899e2013-12-11 19:15:45 -0500266 dop.lba = (((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nls;
267 dop.count = count;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400268 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
269 int status = send_disk_op(&dop);
270 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500271}
272
273// read disk drive parameters
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400274static void noinline
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400275disk_1308(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500276{
277 // Get logical geometry from table
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400278 struct chs_s chs = getLCHS(drive_fl);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400279 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400280 nlc--;
281 nlh--;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400282 u8 count;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400283 if (regs->dl < EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400284 // Floppy
Kevin O'Connora0842f82010-12-29 11:05:46 -0500285 count = GET_GLOBAL(FloppyCount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500286
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400287 if (CONFIG_CDROM_EMU && drive_fl == GET_GLOBAL(cdemu_drive_gf))
Kevin O'Connord3140832012-05-13 22:46:12 -0400288 regs->bx = GET_LOW(CDEmu.media) * 2;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400289 else
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400290 regs->bx = GET_FLATPTR(drive_fl->floppy_type);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400291
292 // set es & di to point to 11 byte diskette param table in ROM
293 regs->es = SEG_BIOS;
294 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400295 } else if (regs->dl < EXTSTART_CD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400296 // Hard drive
297 count = GET_BDA(hdcount);
298 nlc--; // last sector reserved
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400299 } else {
300 // Not supported on CDROM
301 disk_ret(regs, DISK_RET_EPARAM);
302 return;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400303 }
304
Kevin O'Connor02f76762014-05-10 01:20:46 -0400305 if (CONFIG_CDROM_EMU && GET_LOW(CDEmu.media)) {
306 u8 emudrive = GET_LOW(CDEmu.emulated_drive);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400307 if (((emudrive ^ regs->dl) & 0x80) == 0)
308 // Note extra drive due to emulation.
309 count++;
310 if (regs->dl < EXTSTART_HD && count > 2)
311 // Max of two floppy drives.
312 count = 2;
313 }
314
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500315 regs->al = 0;
316 regs->ch = nlc & 0xff;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400317 regs->cl = ((nlc >> 2) & 0xc0) | (nls & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400318 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500319
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500320 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400321 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500322}
323
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500324// initialize drive parameters
325static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400326disk_1309(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500327{
328 DISK_STUB(regs);
329}
330
331// seek to specified cylinder
332static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400333disk_130c(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500334{
335 DISK_STUB(regs);
336}
337
338// alternate disk reset
339static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400340disk_130d(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500341{
342 DISK_STUB(regs);
343}
344
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500345// check drive ready
346static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400347disk_1310(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500348{
349 // should look at 40:8E also???
350
Kevin O'Connor42337662009-08-10 00:06:37 -0400351 struct disk_op_s dop;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400352 dop.drive_fl = drive_fl;
Kevin O'Connor42337662009-08-10 00:06:37 -0400353 dop.command = CMD_ISREADY;
Kevin O'Connor45e71722014-12-29 10:29:34 -0500354 dop.count = 0;
Kevin O'Connor42337662009-08-10 00:06:37 -0400355 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400356 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500357}
358
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500359// recalibrate
360static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400361disk_1311(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500362{
363 DISK_STUB(regs);
364}
365
366// controller internal diagnostic
367static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400368disk_1314(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500369{
370 DISK_STUB(regs);
371}
372
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500373// read disk drive size
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400374static void noinline
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400375disk_1315(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500376{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400377 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400378 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
379 // Floppy or cdrom
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400380 regs->ah = 1;
381 return;
382 }
383 // Hard drive
384
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500385 // Get logical geometry from table
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400386 struct chs_s chs = getLCHS(drive_fl);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400387 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500388
389 // Compute sector count seen by int13
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400390 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nls;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500391 regs->cx = lba >> 16;
392 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500393 regs->ah = 3; // hard disk accessible
394}
395
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400396static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400397disk_1316(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400398{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400399 if (regs->dl >= EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400400 // Hard drive
401 disk_ret(regs, DISK_RET_EPARAM);
402 return;
403 }
404 disk_ret(regs, DISK_RET_ECHANGED);
405}
406
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500407// IBM/MS installation check
408static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400409disk_1341(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500410{
411 regs->bx = 0xaa55; // install check
412 regs->cx = 0x0007; // ext disk access and edd, removable supported
413 disk_ret(regs, DISK_RET_SUCCESS);
414 regs->ah = 0x30; // EDD 3.0
415}
416
417// IBM/MS extended read
418static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400419disk_1342(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500420{
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400421 extended_access(regs, drive_fl, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500422}
423
424// IBM/MS extended write
425static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400426disk_1343(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500427{
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400428 extended_access(regs, drive_fl, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500429}
430
431// IBM/MS verify
432static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400433disk_1344(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500434{
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400435 extended_access(regs, drive_fl, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500436}
437
Kevin O'Connor8c76bd62015-07-24 13:17:38 -0400438// Locks for removable devices
439u8 CDRom_locks[BUILD_MAX_EXTDRIVE] VARLOW;
440
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400441// lock
442static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400443disk_134500(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400444{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400445 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400446 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400447 if (locks == 0xff) {
448 regs->al = 1;
449 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
450 return;
451 }
Kevin O'Connord3140832012-05-13 22:46:12 -0400452 SET_LOW(CDRom_locks[cdid], locks + 1);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400453 regs->al = 1;
454 disk_ret(regs, DISK_RET_SUCCESS);
455}
456
457// unlock
458static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400459disk_134501(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400460{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400461 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400462 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400463 if (locks == 0x00) {
464 regs->al = 0;
465 disk_ret(regs, DISK_RET_ENOTLOCKED);
466 return;
467 }
468 locks--;
Kevin O'Connord3140832012-05-13 22:46:12 -0400469 SET_LOW(CDRom_locks[cdid], locks);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400470 regs->al = (locks ? 1 : 0);
471 disk_ret(regs, DISK_RET_SUCCESS);
472}
473
474// status
475static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400476disk_134502(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400477{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400478 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400479 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400480 regs->al = (locks ? 1 : 0);
481 disk_ret(regs, DISK_RET_SUCCESS);
482}
483
484static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400485disk_1345XX(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400486{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500487 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400488}
489
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500490// IBM/MS lock/unlock drive
491static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400492disk_1345(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500493{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400494 if (regs->dl < EXTSTART_CD) {
495 // Always success for HD
496 disk_ret(regs, DISK_RET_SUCCESS);
497 return;
498 }
499
500 switch (regs->al) {
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400501 case 0x00: disk_134500(regs, drive_fl); break;
502 case 0x01: disk_134501(regs, drive_fl); break;
503 case 0x02: disk_134502(regs, drive_fl); break;
504 default: disk_1345XX(regs, drive_fl); break;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400505 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500506}
507
508// IBM/MS eject media
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400509static void noinline
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400510disk_1346(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500511{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400512 if (regs->dl < EXTSTART_CD) {
513 // Volume Not Removable
514 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
515 return;
516 }
517
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400518 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400519 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400520 if (locks != 0) {
521 disk_ret(regs, DISK_RET_ELOCKED);
522 return;
523 }
524
525 // FIXME should handle 0x31 no media in device
526 // FIXME should handle 0xb5 valid request failed
527
528 // Call removable media eject
529 struct bregs br;
530 memset(&br, 0, sizeof(br));
531 br.ah = 0x52;
Kevin O'Connor74152702010-03-20 17:53:40 -0400532 br.dl = regs->dl;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400533 call16_int(0x15, &br);
534
535 if (br.ah || br.flags & F_CF) {
536 disk_ret(regs, DISK_RET_ELOCKED);
537 return;
538 }
539 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500540}
541
542// IBM/MS extended seek
543static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400544disk_1347(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500545{
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400546 extended_access(regs, drive_fl, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500547}
548
549// IBM/MS get drive parameters
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400550static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400551disk_1348(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500552{
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400553 int ret = fill_edd(SEGOFF(regs->ds, regs->si), drive_fl);
Kevin O'Connor39ca4982014-05-10 11:42:22 -0400554 disk_ret(regs, ret);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500555}
556
557// IBM/MS extended media change
558static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400559disk_1349(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500560{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400561 if (regs->dl < EXTSTART_CD) {
562 // Always success for HD
563 disk_ret(regs, DISK_RET_SUCCESS);
564 return;
565 }
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500566 set_invalid(regs);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400567 // always send changed ??
568 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500569}
570
571static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400572disk_134e01(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500573{
574 disk_ret(regs, DISK_RET_SUCCESS);
575}
576
577static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400578disk_134e03(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500579{
580 disk_ret(regs, DISK_RET_SUCCESS);
581}
582
583static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400584disk_134e04(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500585{
586 disk_ret(regs, DISK_RET_SUCCESS);
587}
588
589static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400590disk_134e06(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500591{
592 disk_ret(regs, DISK_RET_SUCCESS);
593}
594
595static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400596disk_134eXX(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500597{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500598 disk_ret(regs, DISK_RET_EPARAM);
599}
600
601// IBM/MS set hardware configuration
602static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400603disk_134e(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500604{
605 switch (regs->al) {
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400606 case 0x01: disk_134e01(regs, drive_fl); break;
607 case 0x03: disk_134e03(regs, drive_fl); break;
608 case 0x04: disk_134e04(regs, drive_fl); break;
609 case 0x06: disk_134e06(regs, drive_fl); break;
610 default: disk_134eXX(regs, drive_fl); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500611 }
612}
613
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400614static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400615disk_13XX(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500616{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500617 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500618}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500619
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400620static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400621disk_13(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500622{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500623 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500624
625 // clear completion flag
626 SET_BDA(disk_interrupt_flag, 0);
627
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500628 switch (regs->ah) {
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400629 case 0x00: disk_1300(regs, drive_fl); break;
630 case 0x01: disk_1301(regs, drive_fl); break;
631 case 0x02: disk_1302(regs, drive_fl); break;
632 case 0x03: disk_1303(regs, drive_fl); break;
633 case 0x04: disk_1304(regs, drive_fl); break;
634 case 0x05: disk_1305(regs, drive_fl); break;
635 case 0x08: disk_1308(regs, drive_fl); break;
636 case 0x09: disk_1309(regs, drive_fl); break;
637 case 0x0c: disk_130c(regs, drive_fl); break;
638 case 0x0d: disk_130d(regs, drive_fl); break;
639 case 0x10: disk_1310(regs, drive_fl); break;
640 case 0x11: disk_1311(regs, drive_fl); break;
641 case 0x14: disk_1314(regs, drive_fl); break;
642 case 0x15: disk_1315(regs, drive_fl); break;
643 case 0x16: disk_1316(regs, drive_fl); break;
644 case 0x41: disk_1341(regs, drive_fl); break;
645 case 0x42: disk_1342(regs, drive_fl); break;
646 case 0x43: disk_1343(regs, drive_fl); break;
647 case 0x44: disk_1344(regs, drive_fl); break;
648 case 0x45: disk_1345(regs, drive_fl); break;
649 case 0x46: disk_1346(regs, drive_fl); break;
650 case 0x47: disk_1347(regs, drive_fl); break;
651 case 0x48: disk_1348(regs, drive_fl); break;
652 case 0x49: disk_1349(regs, drive_fl); break;
653 case 0x4e: disk_134e(regs, drive_fl); break;
654 default: disk_13XX(regs, drive_fl); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500655 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500656}
657
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400658static void
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400659floppy_13(struct bregs *regs, struct drive_s *drive_fl)
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400660{
661 // Only limited commands are supported on floppies.
662 switch (regs->ah) {
663 case 0x00:
664 case 0x01:
665 case 0x02:
666 case 0x03:
667 case 0x04:
668 case 0x05:
669 case 0x08:
670 case 0x15:
671 case 0x16:
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400672 disk_13(regs, drive_fl);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400673 break;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400674 default: disk_13XX(regs, drive_fl); break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400675 }
676}
677
Kevin O'Connorc029a912014-05-10 15:51:40 -0400678// ElTorito - Terminate disk emu
679static void
680cdemu_134b(struct bregs *regs)
681{
682 memcpy_far(regs->ds, (void*)(regs->si+0), SEG_LOW, &CDEmu, sizeof(CDEmu));
683
684 // If we have to terminate emulation
685 if (regs->al == 0x00) {
686 // FIXME ElTorito Various. Should be handled accordingly to spec
687 SET_LOW(CDEmu.media, 0x00); // bye bye
688
689 // XXX - update floppy/hd count.
690 }
691
692 disk_ret(regs, DISK_RET_SUCCESS);
693}
694
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500695
696/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500697 * Entry points
698 ****************************************************************/
699
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500700static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400701handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500702{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400703 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400704 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500705 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500706 return;
707 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500708
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400709 if (extdrive < EXTSTART_HD) {
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400710 struct drive_s *drive_fl = getDrive(EXTTYPE_FLOPPY, extdrive);
711 if (!drive_fl)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400712 goto fail;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400713 floppy_13(regs, drive_fl);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400714 return;
715 }
716
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400717 struct drive_s *drive_fl;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400718 if (extdrive >= EXTSTART_CD)
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400719 drive_fl = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400720 else
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400721 drive_fl = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
722 if (!drive_fl)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400723 goto fail;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400724 disk_13(regs, drive_fl);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400725 return;
726
727fail:
728 // XXX - support 1301/1308/1315 anyway?
729 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500730}
731
Kevin O'Connor19786762008-03-05 21:09:59 -0500732void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500733handle_40(struct bregs *regs)
734{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400735 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500736 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500737}
738
739// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500740void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500741handle_13(struct bregs *regs)
742{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400743 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400744 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500745
Kevin O'Connordfa16502008-03-22 20:13:08 -0400746 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500747 if (regs->ah == 0x4b) {
748 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400749 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500750 }
Kevin O'Connor02f76762014-05-10 01:20:46 -0400751 if (GET_LOW(CDEmu.media)) {
752 u8 emudrive = GET_LOW(CDEmu.emulated_drive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400753 if (extdrive == emudrive) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400754 // Access to an emulated drive.
Kevin O'Connor1902c942013-10-26 11:48:06 -0400755 struct drive_s *cdemu_gf = GET_GLOBAL(cdemu_drive_gf);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400756 if (regs->ah > 0x16) {
757 // Only old-style commands supported.
Kevin O'Connor1902c942013-10-26 11:48:06 -0400758 disk_13XX(regs, cdemu_gf);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400759 return;
760 }
Kevin O'Connor1902c942013-10-26 11:48:06 -0400761 disk_13(regs, cdemu_gf);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400762 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500763 }
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400764 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400765 // Adjust id to make room for emulated drive.
Kevin O'Connor707298a2009-08-11 22:27:51 -0400766 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500767 }
768 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400769 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500770}
771
772// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500773void VISIBLE16
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400774handle_76(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500775{
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400776 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400777 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connoraa7c2342013-07-14 15:07:21 -0400778 pic_eoi2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500779}