blob: db3c029487f4c0039cf4edc2eac63ebf6280d6ca [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
8#include "disk.h" // floppy_13
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "biosvar.h" // SET_BDA
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050010#include "config.h" // CONFIG_*
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050011#include "util.h" // debug_enter
Kevin O'Connorf54c1502008-06-14 15:56:16 -040012#include "pic.h" // eoi_pic2
Kevin O'Connor9521e262008-07-04 13:04:29 -040013#include "bregs.h" // struct bregs
Kevin O'Connor53236cc2008-08-31 11:16:33 -040014#include "pci.h" // pci_bdf_to_bus
Kevin O'Connorc892b132009-08-11 21:59:37 -040015#include "ata.h" // ATA_CB_DC
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050016
Kevin O'Connorb74102d2008-03-03 21:57:30 -050017
18/****************************************************************
19 * Helper functions
20 ****************************************************************/
21
Kevin O'Connor567e4e32008-04-05 11:37:51 -040022void
Kevin O'Connor05600342009-01-02 13:10:58 -050023__disk_ret(struct bregs *regs, u32 linecode, const char *fname)
Kevin O'Connor567e4e32008-04-05 11:37:51 -040024{
Kevin O'Connor05600342009-01-02 13:10:58 -050025 u8 code = linecode;
Kevin O'Connor48410fd2009-08-16 14:13:36 -040026 if (regs->dl < 0x80)
27 SET_BDA(floppy_last_status, code);
28 else
29 SET_BDA(disk_last_status, code);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040030 if (code)
Kevin O'Connor05600342009-01-02 13:10:58 -050031 __set_code_fail(regs, linecode, fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040032 else
33 set_code_success(regs);
34}
35
36static void
Kevin O'Connor05600342009-01-02 13:10:58 -050037__disk_stub(struct bregs *regs, int lineno, const char *fname)
Kevin O'Connor567e4e32008-04-05 11:37:51 -040038{
Kevin O'Connor05600342009-01-02 13:10:58 -050039 __debug_stub(regs, lineno, fname);
40 __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040041}
42
Kevin O'Connor05600342009-01-02 13:10:58 -050043#define DISK_STUB(regs) \
44 __disk_stub((regs), __LINE__, __func__)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050045
Kevin O'Connor1625a752009-08-09 13:08:21 -040046// Execute a "disk_op_s" request - this runs on a stack in the ebda.
Kevin O'Connor7f343092009-01-01 18:31:11 -050047static int
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050048__send_disk_op(struct disk_op_s *op_far, u16 op_seg)
Kevin O'Connor4524bf72008-12-31 00:31:03 -050049{
Kevin O'Connor7f343092009-01-01 18:31:11 -050050 struct disk_op_s dop;
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050051 memcpy_far(GET_SEG(SS), &dop
52 , op_seg, op_far
53 , sizeof(dop));
Kevin O'Connor7f343092009-01-01 18:31:11 -050054
Kevin O'Connor4524bf72008-12-31 00:31:03 -050055 dprintf(DEBUG_HDL_13, "disk_op d=%d lba=%d buf=%p count=%d cmd=%d\n"
Kevin O'Connor35ae7262009-01-19 15:44:44 -050056 , dop.driveid, (u32)dop.lba, dop.buf_fl
Kevin O'Connor7f343092009-01-01 18:31:11 -050057 , dop.count, dop.command);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050058
59 irq_enable();
60
Kevin O'Connor3f6c2782009-08-09 19:17:11 -040061 int status = 0;
Kevin O'Connorc892b132009-08-11 21:59:37 -040062 u8 type = GET_GLOBAL(Drives.drives[dop.driveid].type);
Kevin O'Connor42337662009-08-10 00:06:37 -040063 if (type == DTYPE_ATA)
Kevin O'Connor3f6c2782009-08-09 19:17:11 -040064 status = process_ata_op(&dop);
Kevin O'Connor42337662009-08-10 00:06:37 -040065 else if (type == DTYPE_ATAPI)
Kevin O'Connor3f6c2782009-08-09 19:17:11 -040066 status = process_atapi_op(&dop);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050067
68 irq_disable();
69
Kevin O'Connor1625a752009-08-09 13:08:21 -040070 // Update count with total sectors transferred.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -040071 SET_FARVAR(op_seg, op_far->count, dop.count);
Kevin O'Connor1625a752009-08-09 13:08:21 -040072
Kevin O'Connor4524bf72008-12-31 00:31:03 -050073 return status;
74}
75
Kevin O'Connor1625a752009-08-09 13:08:21 -040076// Execute a "disk_op_s" request by jumping to a stack in the ebda.
Kevin O'Connor7f343092009-01-01 18:31:11 -050077static int
78send_disk_op(struct disk_op_s *op)
79{
Kevin O'Connorc892b132009-08-11 21:59:37 -040080 if (! CONFIG_DRIVES)
Kevin O'Connor0d9e6732009-01-17 21:54:16 -050081 return -1;
82
Kevin O'Connor7f343092009-01-01 18:31:11 -050083 return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
84}
85
Kevin O'Connor1625a752009-08-09 13:08:21 -040086// Obtain the requested disk lba from an old-style chs request.
87static int
Kevin O'Connorb68ac712009-08-09 17:25:19 -040088legacy_lba(struct bregs *regs, u16 lchs_seg, struct chs_s *lchs_far)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050089{
Kevin O'Connorb68ac712009-08-09 17:25:19 -040090 u8 count = regs->al;
Kevin O'Connor1625a752009-08-09 13:08:21 -040091 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
92 u16 sector = regs->cl & 0x3f;
93 u16 head = regs->dh;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050094
Kevin O'Connorb68ac712009-08-09 17:25:19 -040095 if (count > 128 || count == 0 || sector == 0) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040096 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040097 , regs->ah);
98 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040099 return -1;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -0400100 }
101
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400102 u16 nlc = GET_FARVAR(lchs_seg, lchs_far->cylinders);
103 u16 nlh = GET_FARVAR(lchs_seg, lchs_far->heads);
104 u16 nlspt = GET_FARVAR(lchs_seg, lchs_far->spt);
105
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500106 // sanity check on cyl heads, sec
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400107 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400108 dprintf(1, "int13_harddisk: function %02x, parameters out of"
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500109 " range %04x/%04x/%04x!\n"
110 , regs->ah, cylinder, head, sector);
111 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -0400112 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500113 }
114
Kevin O'Connor049d5a22008-03-13 19:09:49 -0400115 // translate lchs to lba
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400116 return (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
117 + (u32)sector - 1);
Kevin O'Connor1625a752009-08-09 13:08:21 -0400118}
119
120// Perform read/write/verify using old-style chs accesses
121static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400122basic_access(struct bregs *regs, u8 driveid, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400123{
124 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400125 dop.driveid = driveid;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400126 dop.command = command;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400127 int lba = legacy_lba(regs, get_global_seg(), &Drives.drives[driveid].lchs);
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400128 if (lba < 0)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400129 return;
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400130 dop.lba = lba;
131 dop.count = regs->al;
132 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400133
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500134 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400135
Kevin O'Connor1625a752009-08-09 13:08:21 -0400136 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500137
Kevin O'Connor126eac62009-08-16 13:32:24 -0400138 disk_ret(regs, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500139}
140
Kevin O'Connor1625a752009-08-09 13:08:21 -0400141// Perform cdemu read/verify
142void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400143cdemu_access(struct bregs *regs, u8 driveid, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400144{
145 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400146 dop.driveid = driveid;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400147 dop.command = command;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400148 u16 ebda_seg = get_ebda_seg();
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400149 int vlba = legacy_lba(
150 regs, ebda_seg
151 , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
152 if (vlba < 0)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400153 return;
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400154 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
155 u8 count = regs->al;
156 u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
157 u8 *dest_far = (void*)(regs->bx+0);
158 regs->al = 0;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400159 int status = DISK_RET_SUCCESS;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400160
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400161 if (vlba & 3) {
162 dop.count = 1;
163 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400164 status = send_disk_op(&dop);
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400165 if (status)
166 goto fail;
167 u8 thiscount = 4 - (vlba & 3);
168 if (thiscount > count)
169 thiscount = count;
170 count -= thiscount;
171 memcpy_far(regs->es, dest_far
172 , ebda_seg, cdbuf_far + (vlba & 3) * 512
173 , thiscount * 512);
174 dest_far += thiscount * 512;
175 regs->al += thiscount;
176 dop.lba++;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400177 }
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400178
179 if (count > 3) {
180 dop.count = count / 4;
181 dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400182 status = send_disk_op(&dop);
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400183 regs->al += dop.count * 4;
184 if (status)
185 goto fail;
186 u8 thiscount = count & ~3;
187 count &= 3;
188 dest_far += thiscount * 512;
189 dop.lba += thiscount / 4;
190 }
191
192 if (count) {
193 dop.count = 1;
194 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400195 status = send_disk_op(&dop);
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400196 if (status)
197 goto fail;
198 u8 thiscount = count;
199 memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
200 regs->al += thiscount;
201 }
202
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400203fail:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400204 disk_ret(regs, status);
Kevin O'Connor1625a752009-08-09 13:08:21 -0400205}
206
207// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connored128492008-03-11 11:14:59 -0400208static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400209extended_access(struct bregs *regs, u8 driveid, u16 command)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500210{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500211 struct disk_op_s dop;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400212 // Get lba and check.
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500213 dop.lba = GET_INT13EXT(regs, lba);
214 dop.command = command;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400215 dop.driveid = driveid;
216 if (dop.lba >= GET_GLOBAL(Drives.drives[driveid].sectors)) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400217 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
218 , regs->ah);
219 disk_ret(regs, DISK_RET_EPARAM);
220 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500221 }
222
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400223 u16 segment = GET_INT13EXT(regs, segment);
224 u16 offset = GET_INT13EXT(regs, offset);
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500225 dop.buf_fl = MAKE_FLATPTR(segment, offset);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500226 dop.count = GET_INT13EXT(regs, count);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400227
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500228 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400229
Kevin O'Connor1625a752009-08-09 13:08:21 -0400230 SET_INT13EXT(regs, count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500231
Kevin O'Connor126eac62009-08-16 13:32:24 -0400232 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500233}
234
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500235
236/****************************************************************
237 * Hard Drive functions
238 ****************************************************************/
239
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500240// disk controller reset
241static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400242disk_1300(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500243{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400244 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400245 dop.driveid = driveid;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400246 dop.command = CMD_RESET;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400247 int status = send_disk_op(&dop);
248 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500249}
250
251// read disk status
252static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400253disk_1301(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500254{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400255 u8 v;
256 if (regs->dl < 0x80)
257 // Floppy
258 v = GET_BDA(floppy_last_status);
259 else
260 v = GET_BDA(disk_last_status);
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400261 regs->ah = v;
262 set_cf(regs, v);
263 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500264}
265
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500266// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500267static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400268disk_1302(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500269{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400270 basic_access(regs, driveid, CMD_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500271}
272
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500273// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500274static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400275disk_1303(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500276{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400277 basic_access(regs, driveid, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500278}
279
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500280// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500281static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400282disk_1304(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500283{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400284 basic_access(regs, driveid, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500285 // FIXME verify
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500286}
287
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500288// format disk track
289static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400290disk_1305(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500291{
292 DISK_STUB(regs);
293}
294
295// read disk drive parameters
296static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400297disk_1308(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500298{
299 // Get logical geometry from table
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400300 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders) - 1;
301 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads) - 1;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400302 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400303 u8 count;
304 if (regs->dl < 0x80) {
305 // Floppy
306 count = GET_GLOBAL(Drives.floppycount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500307
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400308 regs->bx = GET_GLOBAL(Drives.drives[driveid].floppy_type);
309
310 // set es & di to point to 11 byte diskette param table in ROM
311 regs->es = SEG_BIOS;
312 regs->di = (u32)&diskette_param_table2;
313 } else {
314 // Hard drive
315 count = GET_BDA(hdcount);
316 nlc--; // last sector reserved
317 }
318
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500319 regs->al = 0;
320 regs->ch = nlc & 0xff;
321 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400322 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500323
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500324 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400325 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500326}
327
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500328// initialize drive parameters
329static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400330disk_1309(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500331{
332 DISK_STUB(regs);
333}
334
335// seek to specified cylinder
336static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400337disk_130c(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500338{
339 DISK_STUB(regs);
340}
341
342// alternate disk reset
343static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400344disk_130d(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500345{
346 DISK_STUB(regs);
347}
348
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500349// check drive ready
350static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400351disk_1310(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500352{
353 // should look at 40:8E also???
354
Kevin O'Connor42337662009-08-10 00:06:37 -0400355 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400356 dop.driveid = driveid;
Kevin O'Connor42337662009-08-10 00:06:37 -0400357 dop.command = CMD_ISREADY;
358 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400359 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500360}
361
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500362// recalibrate
363static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400364disk_1311(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500365{
366 DISK_STUB(regs);
367}
368
369// controller internal diagnostic
370static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400371disk_1314(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500372{
373 DISK_STUB(regs);
374}
375
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500376// read disk drive size
377static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400378disk_1315(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500379{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400380 disk_ret(regs, DISK_RET_SUCCESS);
381 if (regs->dl < 0x80) {
382 // Floppy
383 regs->ah = 1;
384 return;
385 }
386 // Hard drive
387
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500388 // Get logical geometry from table
Kevin O'Connor707298a2009-08-11 22:27:51 -0400389 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
390 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
391 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500392
393 // Compute sector count seen by int13
394 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
395 regs->cx = lba >> 16;
396 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500397 regs->ah = 3; // hard disk accessible
398}
399
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400400static void
401disk_1316(struct bregs *regs, u8 driveid)
402{
403 if (regs->dl >= 0x80) {
404 // Hard drive
405 disk_ret(regs, DISK_RET_EPARAM);
406 return;
407 }
408 disk_ret(regs, DISK_RET_ECHANGED);
409}
410
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500411// IBM/MS installation check
412static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400413disk_1341(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500414{
415 regs->bx = 0xaa55; // install check
416 regs->cx = 0x0007; // ext disk access and edd, removable supported
417 disk_ret(regs, DISK_RET_SUCCESS);
418 regs->ah = 0x30; // EDD 3.0
419}
420
421// IBM/MS extended read
422static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400423disk_1342(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500424{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400425 extended_access(regs, driveid, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500426}
427
428// IBM/MS extended write
429static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400430disk_1343(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500431{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400432 extended_access(regs, driveid, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500433}
434
435// IBM/MS verify
436static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400437disk_1344(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500438{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400439 extended_access(regs, driveid, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500440}
441
442// IBM/MS lock/unlock drive
443static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400444disk_1345(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500445{
446 // Always success for HD
447 disk_ret(regs, DISK_RET_SUCCESS);
448}
449
450// IBM/MS eject media
451static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400452disk_1346(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500453{
454 // Volume Not Removable
455 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
456}
457
458// IBM/MS extended seek
459static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400460disk_1347(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500461{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400462 extended_access(regs, driveid, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500463}
464
465// IBM/MS get drive parameters
466static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400467disk_1348(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500468{
469 u16 size = GET_INT13DPT(regs, size);
470
471 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400472 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500473 disk_ret(regs, DISK_RET_EPARAM);
474 return;
475 }
476
477 // EDD 1.x
478
Kevin O'Connor707298a2009-08-11 22:27:51 -0400479 u8 type = GET_GLOBAL(Drives.drives[driveid].type);
480 u16 npc = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
481 u16 nph = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
482 u16 npspt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
483 u64 lba = GET_GLOBAL(Drives.drives[driveid].sectors);
484 u16 blksize = GET_GLOBAL(Drives.drives[driveid].blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500485
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400486 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
487 , size, type, npc, nph, npspt, (u32)lba, blksize);
488
489 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400490 if (type == DTYPE_ATAPI) {
491 // 0x74 = removable, media change, lockable, max values
492 SET_INT13DPT(regs, infos, 0x74);
493 SET_INT13DPT(regs, cylinders, 0xffffffff);
494 SET_INT13DPT(regs, heads, 0xffffffff);
495 SET_INT13DPT(regs, spt, 0xffffffff);
496 SET_INT13DPT(regs, sector_count, (u64)-1);
497 } else {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400498 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500499 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
500 SET_INT13DPT(regs, cylinders, 0x3fff);
501 } else {
502 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
503 SET_INT13DPT(regs, cylinders, (u32)npc);
504 }
505 SET_INT13DPT(regs, heads, (u32)nph);
506 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400507 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500508 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500509 SET_INT13DPT(regs, blksize, blksize);
510
Kevin O'Connorb1144362009-08-11 20:43:38 -0400511 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500512 disk_ret(regs, DISK_RET_SUCCESS);
513 return;
514 }
515
516 // EDD 2.x
517
Kevin O'Connor08815372008-12-29 21:16:31 -0500518 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400519 SET_INT13DPT(regs, size, 30);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500520
Kevin O'Connor08815372008-12-29 21:16:31 -0500521 SET_INT13DPT(regs, dpte_segment, ebda_seg);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500522 SET_INT13DPT(regs, dpte_offset
Kevin O'Connor609da232008-12-28 23:18:57 -0500523 , offsetof(struct extended_bios_data_area_s, dpte));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500524
525 // Fill in dpte
Kevin O'Connor707298a2009-08-11 22:27:51 -0400526 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400527 u8 channel = ataid / 2;
528 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400529 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
530 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
531 u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500532
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400533 u16 options = 0;
Kevin O'Connor42337662009-08-10 00:06:37 -0400534 if (type == DTYPE_ATA) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400535 u8 translation = GET_GLOBAL(Drives.drives[driveid].translation);
Kevin O'Connor42337662009-08-10 00:06:37 -0400536 if (translation != TRANSLATION_NONE) {
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400537 options |= 1<<3; // CHS translation
Kevin O'Connor42337662009-08-10 00:06:37 -0400538 if (translation == TRANSLATION_LBA)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400539 options |= 1<<9;
Kevin O'Connor42337662009-08-10 00:06:37 -0400540 if (translation == TRANSLATION_RECHS)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400541 options |= 3<<9;
542 }
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500543 } else {
544 // ATAPI
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400545 options |= 1<<5; // removable device
546 options |= 1<<6; // atapi device
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500547 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400548 options |= 1<<4; // lba translation
Kevin O'Connor32945af2009-02-27 21:23:01 -0500549 if (CONFIG_ATA_PIO32)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400550 options |= 1<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500551
Kevin O'Connor08815372008-12-29 21:16:31 -0500552 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
553 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
554 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
555 | ATA_CB_DH_LBA));
556 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
557 SET_EBDA2(ebda_seg, dpte.irq, irq);
558 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
559 SET_EBDA2(ebda_seg, dpte.dma, 0);
560 SET_EBDA2(ebda_seg, dpte.pio, 0);
561 SET_EBDA2(ebda_seg, dpte.options, options);
562 SET_EBDA2(ebda_seg, dpte.reserved, 0);
563 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500564
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500565 u8 sum = checksum_far(
566 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
567 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500568
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400569 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500570 disk_ret(regs, DISK_RET_SUCCESS);
571 return;
572 }
573
574 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500575 SET_INT13DPT(regs, key, 0xbedd);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400576 SET_INT13DPT(regs, dpi_length, 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500577 SET_INT13DPT(regs, reserved1, 0);
578 SET_INT13DPT(regs, reserved2, 0);
579
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400580 SET_INT13DPT(regs, host_bus[0], 'P');
581 SET_INT13DPT(regs, host_bus[1], 'C');
582 SET_INT13DPT(regs, host_bus[2], 'I');
583 SET_INT13DPT(regs, host_bus[3], 0);
584
Kevin O'Connorc892b132009-08-11 21:59:37 -0400585 u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400586 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
587 | (pci_bdf_to_fn(bdf) << 16));
588 SET_INT13DPT(regs, iface_path, path);
589
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500590 SET_INT13DPT(regs, iface_type[0], 'A');
591 SET_INT13DPT(regs, iface_type[1], 'T');
592 SET_INT13DPT(regs, iface_type[2], 'A');
593 SET_INT13DPT(regs, iface_type[3], 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400594 SET_INT13DPT(regs, iface_type[4], 0);
595 SET_INT13DPT(regs, iface_type[5], 0);
596 SET_INT13DPT(regs, iface_type[6], 0);
597 SET_INT13DPT(regs, iface_type[7], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500598
Kevin O'Connor970a0322008-10-26 12:01:21 -0400599 SET_INT13DPT(regs, device_path, slave);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500600
Kevin O'Connor7d108212009-01-21 19:13:21 -0500601 SET_INT13DPT(regs, checksum
602 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400603
604 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500605}
606
607// IBM/MS extended media change
608static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400609disk_1349(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500610{
611 // Always success for HD
612 disk_ret(regs, DISK_RET_SUCCESS);
613}
614
615static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400616disk_134e01(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500617{
618 disk_ret(regs, DISK_RET_SUCCESS);
619}
620
621static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400622disk_134e03(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500623{
624 disk_ret(regs, DISK_RET_SUCCESS);
625}
626
627static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400628disk_134e04(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500629{
630 disk_ret(regs, DISK_RET_SUCCESS);
631}
632
633static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400634disk_134e06(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500635{
636 disk_ret(regs, DISK_RET_SUCCESS);
637}
638
639static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400640disk_134eXX(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500641{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500642 disk_ret(regs, DISK_RET_EPARAM);
643}
644
645// IBM/MS set hardware configuration
646static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400647disk_134e(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500648{
649 switch (regs->al) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400650 case 0x01: disk_134e01(regs, driveid); break;
651 case 0x03: disk_134e03(regs, driveid); break;
652 case 0x04: disk_134e04(regs, driveid); break;
653 case 0x06: disk_134e06(regs, driveid); break;
654 default: disk_134eXX(regs, driveid); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500655 }
656}
657
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500658void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400659disk_13XX(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500660{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500661 disk_ret(regs, DISK_RET_EPARAM);
662}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500663
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500664void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400665disk_13(struct bregs *regs, u8 driveid)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500666{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500667 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500668
669 // clear completion flag
670 SET_BDA(disk_interrupt_flag, 0);
671
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500672 switch (regs->ah) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400673 case 0x00: disk_1300(regs, driveid); break;
674 case 0x01: disk_1301(regs, driveid); break;
675 case 0x02: disk_1302(regs, driveid); break;
676 case 0x03: disk_1303(regs, driveid); break;
677 case 0x04: disk_1304(regs, driveid); break;
678 case 0x05: disk_1305(regs, driveid); break;
679 case 0x08: disk_1308(regs, driveid); break;
680 case 0x09: disk_1309(regs, driveid); break;
681 case 0x0c: disk_130c(regs, driveid); break;
682 case 0x0d: disk_130d(regs, driveid); break;
683 case 0x10: disk_1310(regs, driveid); break;
684 case 0x11: disk_1311(regs, driveid); break;
685 case 0x14: disk_1314(regs, driveid); break;
686 case 0x15: disk_1315(regs, driveid); break;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400687 case 0x16: disk_1316(regs, driveid); break;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400688 case 0x41: disk_1341(regs, driveid); break;
689 case 0x42: disk_1342(regs, driveid); break;
690 case 0x43: disk_1343(regs, driveid); break;
691 case 0x44: disk_1344(regs, driveid); break;
692 case 0x45: disk_1345(regs, driveid); break;
693 case 0x46: disk_1346(regs, driveid); break;
694 case 0x47: disk_1347(regs, driveid); break;
695 case 0x48: disk_1348(regs, driveid); break;
696 case 0x49: disk_1349(regs, driveid); break;
697 case 0x4e: disk_134e(regs, driveid); break;
698 default: disk_13XX(regs, driveid); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500699 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500700}
701
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500702
703/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500704 * Entry points
705 ****************************************************************/
706
Kevin O'Connorc892b132009-08-11 21:59:37 -0400707static int
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400708get_driveid(struct bregs *regs, u8 exttype, u8 extdriveoffset)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500709{
710 // basic check : device has to be defined
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400711 if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
Kevin O'Connorc892b132009-08-11 21:59:37 -0400712 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500713
714 // Get the ata channel
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400715 u8 driveid = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500716
717 // basic check : device has to be valid
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400718 if (driveid >= ARRAY_SIZE(Drives.drives))
Kevin O'Connorc892b132009-08-11 21:59:37 -0400719 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500720
Kevin O'Connorc892b132009-08-11 21:59:37 -0400721 return driveid;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500722}
723
724static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400725handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500726{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400727 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400728 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500729 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500730 return;
731 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500732
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400733 if (extdrive < 0x80) {
734 int driveid = get_driveid(regs, EXTTYPE_FLOPPY, extdrive);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400735 if (driveid < 0)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400736 goto fail;
737 floppy_13(regs, driveid);
738 return;
739 }
740
741 if (extdrive >= 0xe0) {
742 int driveid = get_driveid(regs, EXTTYPE_CD, extdrive - 0xe0);
743 if (driveid < 0)
744 goto fail;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400745 cdrom_13(regs, driveid);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500746 return;
747 }
748
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400749 int driveid = get_driveid(regs, EXTTYPE_HD, extdrive - 0x80);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400750 if (driveid < 0)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400751 goto fail;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400752 disk_13(regs, driveid);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400753 return;
754
755fail:
756 // XXX - support 1301/1308/1315 anyway?
757 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500758}
759
Kevin O'Connor19786762008-03-05 21:09:59 -0500760void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500761handle_40(struct bregs *regs)
762{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400763 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500764 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500765}
766
767// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500768void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500769handle_13(struct bregs *regs)
770{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400771 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400772 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500773
Kevin O'Connordfa16502008-03-22 20:13:08 -0400774 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500775 if (regs->ah == 0x4b) {
776 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400777 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500778 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500779 u16 ebda_seg = get_ebda_seg();
780 if (GET_EBDA2(ebda_seg, cdemu.active)) {
Kevin O'Connor669e6442009-08-11 22:36:30 -0400781 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400782 if (extdrive == emudrive) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500783 cdemu_13(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400784 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500785 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400786 if (extdrive < 0xe0 && ((emudrive ^ extdrive) & 0x80) == 0)
787 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500788 }
789 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400790 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500791}
792
793// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500794void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400795handle_76()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500796{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400797 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400798 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400799 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500800}
Kevin O'Connor30853762009-01-17 18:49:20 -0500801
802// Old Fixed Disk Parameter Table (newer tables are in the ebda).
803struct fdpt_s OldFDPT VAR16FIXED(0xe401);