blob: b021c4055933ddff0c88bd36e324373f99b226ab [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'Connor51cfbe72009-08-18 22:38:49 -040026 if (regs->dl < EXTSTART_HD)
Kevin O'Connor48410fd2009-08-16 14:13:36 -040027 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// Obtain the requested disk lba from an old-style chs request.
47static int
Kevin O'Connorb68ac712009-08-09 17:25:19 -040048legacy_lba(struct bregs *regs, u16 lchs_seg, struct chs_s *lchs_far)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050049{
Kevin O'Connorb68ac712009-08-09 17:25:19 -040050 u8 count = regs->al;
Kevin O'Connor1625a752009-08-09 13:08:21 -040051 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
52 u16 sector = regs->cl & 0x3f;
53 u16 head = regs->dh;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050054
Kevin O'Connorb68ac712009-08-09 17:25:19 -040055 if (count > 128 || count == 0 || sector == 0) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040056 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040057 , regs->ah);
58 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040059 return -1;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040060 }
61
Kevin O'Connorb68ac712009-08-09 17:25:19 -040062 u16 nlc = GET_FARVAR(lchs_seg, lchs_far->cylinders);
63 u16 nlh = GET_FARVAR(lchs_seg, lchs_far->heads);
64 u16 nlspt = GET_FARVAR(lchs_seg, lchs_far->spt);
65
Kevin O'Connore43df9e2008-03-01 22:16:32 -050066 // sanity check on cyl heads, sec
Kevin O'Connor1fcf1442008-03-11 19:42:41 -040067 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040068 dprintf(1, "int13_harddisk: function %02x, parameters out of"
Kevin O'Connore43df9e2008-03-01 22:16:32 -050069 " range %04x/%04x/%04x!\n"
70 , regs->ah, cylinder, head, sector);
71 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040072 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050073 }
74
Kevin O'Connor049d5a22008-03-13 19:09:49 -040075 // translate lchs to lba
Kevin O'Connorb68ac712009-08-09 17:25:19 -040076 return (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
77 + (u32)sector - 1);
Kevin O'Connor1625a752009-08-09 13:08:21 -040078}
79
80// Perform read/write/verify using old-style chs accesses
81static void
Kevin O'Connor707298a2009-08-11 22:27:51 -040082basic_access(struct bregs *regs, u8 driveid, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -040083{
84 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -040085 dop.driveid = driveid;
Kevin O'Connor1625a752009-08-09 13:08:21 -040086 dop.command = command;
Kevin O'Connor707298a2009-08-11 22:27:51 -040087 int lba = legacy_lba(regs, get_global_seg(), &Drives.drives[driveid].lchs);
Kevin O'Connorb68ac712009-08-09 17:25:19 -040088 if (lba < 0)
Kevin O'Connor1625a752009-08-09 13:08:21 -040089 return;
Kevin O'Connorb68ac712009-08-09 17:25:19 -040090 dop.lba = lba;
91 dop.count = regs->al;
92 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -040093
Kevin O'Connor4524bf72008-12-31 00:31:03 -050094 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -040095
Kevin O'Connor1625a752009-08-09 13:08:21 -040096 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050097
Kevin O'Connor126eac62009-08-16 13:32:24 -040098 disk_ret(regs, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -050099}
100
Kevin O'Connor1625a752009-08-09 13:08:21 -0400101// Perform cdemu read/verify
102void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400103cdemu_access(struct bregs *regs, u8 driveid, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400104{
105 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400106 dop.driveid = driveid;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400107 dop.command = command;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400108 u16 ebda_seg = get_ebda_seg();
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400109 int vlba = legacy_lba(
110 regs, ebda_seg
111 , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
112 if (vlba < 0)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400113 return;
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400114 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
115 u8 count = regs->al;
116 u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
117 u8 *dest_far = (void*)(regs->bx+0);
118 regs->al = 0;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400119 int status = DISK_RET_SUCCESS;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400120
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400121 if (vlba & 3) {
122 dop.count = 1;
123 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400124 status = send_disk_op(&dop);
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400125 if (status)
126 goto fail;
127 u8 thiscount = 4 - (vlba & 3);
128 if (thiscount > count)
129 thiscount = count;
130 count -= thiscount;
131 memcpy_far(regs->es, dest_far
132 , ebda_seg, cdbuf_far + (vlba & 3) * 512
133 , thiscount * 512);
134 dest_far += thiscount * 512;
135 regs->al += thiscount;
136 dop.lba++;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400137 }
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400138
139 if (count > 3) {
140 dop.count = count / 4;
141 dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400142 status = send_disk_op(&dop);
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400143 regs->al += dop.count * 4;
144 if (status)
145 goto fail;
146 u8 thiscount = count & ~3;
147 count &= 3;
148 dest_far += thiscount * 512;
149 dop.lba += thiscount / 4;
150 }
151
152 if (count) {
153 dop.count = 1;
154 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400155 status = send_disk_op(&dop);
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400156 if (status)
157 goto fail;
158 u8 thiscount = count;
159 memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
160 regs->al += thiscount;
161 }
162
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400163fail:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400164 disk_ret(regs, status);
Kevin O'Connor1625a752009-08-09 13:08:21 -0400165}
166
167// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connored128492008-03-11 11:14:59 -0400168static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400169extended_access(struct bregs *regs, u8 driveid, u16 command)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500170{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500171 struct disk_op_s dop;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400172 // Get lba and check.
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500173 dop.lba = GET_INT13EXT(regs, lba);
174 dop.command = command;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400175 dop.driveid = driveid;
176 if (dop.lba >= GET_GLOBAL(Drives.drives[driveid].sectors)) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400177 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
178 , regs->ah);
179 disk_ret(regs, DISK_RET_EPARAM);
180 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500181 }
182
Kevin O'Connor9f985422009-09-09 11:34:39 -0400183 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_INT13EXT(regs, data));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500184 dop.count = GET_INT13EXT(regs, count);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400185
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500186 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400187
Kevin O'Connor1625a752009-08-09 13:08:21 -0400188 SET_INT13EXT(regs, count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500189
Kevin O'Connor126eac62009-08-16 13:32:24 -0400190 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500191}
192
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500193
194/****************************************************************
195 * Hard Drive functions
196 ****************************************************************/
197
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500198// disk controller reset
199static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400200disk_1300(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500201{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400202 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400203 dop.driveid = driveid;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400204 dop.command = CMD_RESET;
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'Connor707298a2009-08-11 22:27:51 -0400211disk_1301(struct bregs *regs, u8 driveid)
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'Connor707298a2009-08-11 22:27:51 -0400226disk_1302(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500227{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400228 basic_access(regs, driveid, 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'Connor707298a2009-08-11 22:27:51 -0400233disk_1303(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500234{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400235 basic_access(regs, driveid, 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'Connor707298a2009-08-11 22:27:51 -0400240disk_1304(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500241{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400242 basic_access(regs, driveid, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500243 // FIXME verify
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500244}
245
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500246// format disk track
247static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400248disk_1305(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500249{
250 DISK_STUB(regs);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400251
252 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
253 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
254
255 u8 num_sectors = regs->al;
256 u8 head = regs->dh;
257
258 if (head >= nlh || num_sectors == 0 || num_sectors > nlspt) {
259 disk_ret(regs, DISK_RET_EPARAM);
260 return;
261 }
262
263 struct disk_op_s dop;
264 dop.driveid = driveid;
265 dop.command = CMD_FORMAT;
266 dop.lba = head;
267 dop.count = num_sectors;
268 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
274static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400275disk_1308(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500276{
277 // Get logical geometry from table
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400278 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders) - 1;
279 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads) - 1;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400280 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400281 u8 count;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400282 if (regs->dl < EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400283 // Floppy
284 count = GET_GLOBAL(Drives.floppycount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500285
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400286 regs->bx = GET_GLOBAL(Drives.drives[driveid].floppy_type);
287
288 // set es & di to point to 11 byte diskette param table in ROM
289 regs->es = SEG_BIOS;
290 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400291 } else if (regs->dl < EXTSTART_CD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400292 // Hard drive
293 count = GET_BDA(hdcount);
294 nlc--; // last sector reserved
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400295 } else {
296 // Not supported on CDROM
297 disk_ret(regs, DISK_RET_EPARAM);
298 return;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400299 }
300
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500301 regs->al = 0;
302 regs->ch = nlc & 0xff;
303 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400304 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500305
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500306 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400307 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500308}
309
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500310// initialize drive parameters
311static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400312disk_1309(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500313{
314 DISK_STUB(regs);
315}
316
317// seek to specified cylinder
318static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400319disk_130c(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500320{
321 DISK_STUB(regs);
322}
323
324// alternate disk reset
325static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400326disk_130d(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500327{
328 DISK_STUB(regs);
329}
330
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500331// check drive ready
332static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400333disk_1310(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500334{
335 // should look at 40:8E also???
336
Kevin O'Connor42337662009-08-10 00:06:37 -0400337 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400338 dop.driveid = driveid;
Kevin O'Connor42337662009-08-10 00:06:37 -0400339 dop.command = CMD_ISREADY;
340 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400341 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500342}
343
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500344// recalibrate
345static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400346disk_1311(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500347{
348 DISK_STUB(regs);
349}
350
351// controller internal diagnostic
352static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400353disk_1314(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500354{
355 DISK_STUB(regs);
356}
357
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500358// read disk drive size
359static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400360disk_1315(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500361{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400362 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400363 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
364 // Floppy or cdrom
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400365 regs->ah = 1;
366 return;
367 }
368 // Hard drive
369
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500370 // Get logical geometry from table
Kevin O'Connor707298a2009-08-11 22:27:51 -0400371 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
372 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
373 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500374
375 // Compute sector count seen by int13
376 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
377 regs->cx = lba >> 16;
378 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500379 regs->ah = 3; // hard disk accessible
380}
381
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400382static void
383disk_1316(struct bregs *regs, u8 driveid)
384{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400385 if (regs->dl >= EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400386 // Hard drive
387 disk_ret(regs, DISK_RET_EPARAM);
388 return;
389 }
390 disk_ret(regs, DISK_RET_ECHANGED);
391}
392
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500393// IBM/MS installation check
394static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400395disk_1341(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500396{
397 regs->bx = 0xaa55; // install check
398 regs->cx = 0x0007; // ext disk access and edd, removable supported
399 disk_ret(regs, DISK_RET_SUCCESS);
400 regs->ah = 0x30; // EDD 3.0
401}
402
403// IBM/MS extended read
404static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400405disk_1342(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500406{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400407 extended_access(regs, driveid, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500408}
409
410// IBM/MS extended write
411static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400412disk_1343(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500413{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400414 extended_access(regs, driveid, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500415}
416
417// IBM/MS verify
418static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400419disk_1344(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500420{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400421 extended_access(regs, driveid, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500422}
423
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400424// lock
425static void
426disk_134500(struct bregs *regs, u8 driveid)
427{
428 u16 ebda_seg = get_ebda_seg();
429 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[driveid]);
430 if (locks == 0xff) {
431 regs->al = 1;
432 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
433 return;
434 }
435 SET_EBDA2(ebda_seg, cdrom_locks[driveid], locks + 1);
436 regs->al = 1;
437 disk_ret(regs, DISK_RET_SUCCESS);
438}
439
440// unlock
441static void
442disk_134501(struct bregs *regs, u8 driveid)
443{
444 u16 ebda_seg = get_ebda_seg();
445 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[driveid]);
446 if (locks == 0x00) {
447 regs->al = 0;
448 disk_ret(regs, DISK_RET_ENOTLOCKED);
449 return;
450 }
451 locks--;
452 SET_EBDA2(ebda_seg, cdrom_locks[driveid], locks);
453 regs->al = (locks ? 1 : 0);
454 disk_ret(regs, DISK_RET_SUCCESS);
455}
456
457// status
458static void
459disk_134502(struct bregs *regs, u8 driveid)
460{
461 u8 locks = GET_EBDA(cdrom_locks[driveid]);
462 regs->al = (locks ? 1 : 0);
463 disk_ret(regs, DISK_RET_SUCCESS);
464}
465
466static void
467disk_1345XX(struct bregs *regs, u8 driveid)
468{
469 disk_ret(regs, DISK_RET_EPARAM);
470}
471
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500472// IBM/MS lock/unlock drive
473static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400474disk_1345(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500475{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400476 if (regs->dl < EXTSTART_CD) {
477 // Always success for HD
478 disk_ret(regs, DISK_RET_SUCCESS);
479 return;
480 }
481
482 switch (regs->al) {
483 case 0x00: disk_134500(regs, driveid); break;
484 case 0x01: disk_134501(regs, driveid); break;
485 case 0x02: disk_134502(regs, driveid); break;
486 default: disk_1345XX(regs, driveid); break;
487 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500488}
489
490// IBM/MS eject media
491static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400492disk_1346(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500493{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400494 if (regs->dl < EXTSTART_CD) {
495 // Volume Not Removable
496 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
497 return;
498 }
499
500 u8 locks = GET_EBDA(cdrom_locks[driveid]);
501 if (locks != 0) {
502 disk_ret(regs, DISK_RET_ELOCKED);
503 return;
504 }
505
506 // FIXME should handle 0x31 no media in device
507 // FIXME should handle 0xb5 valid request failed
508
509 // Call removable media eject
510 struct bregs br;
511 memset(&br, 0, sizeof(br));
512 br.ah = 0x52;
513 call16_int(0x15, &br);
514
515 if (br.ah || br.flags & F_CF) {
516 disk_ret(regs, DISK_RET_ELOCKED);
517 return;
518 }
519 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500520}
521
522// IBM/MS extended seek
523static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400524disk_1347(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500525{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400526 extended_access(regs, driveid, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500527}
528
529// IBM/MS get drive parameters
530static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400531disk_1348(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500532{
533 u16 size = GET_INT13DPT(regs, size);
534
535 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400536 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500537 disk_ret(regs, DISK_RET_EPARAM);
538 return;
539 }
540
541 // EDD 1.x
542
Kevin O'Connor707298a2009-08-11 22:27:51 -0400543 u8 type = GET_GLOBAL(Drives.drives[driveid].type);
544 u16 npc = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
545 u16 nph = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
546 u16 npspt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
547 u64 lba = GET_GLOBAL(Drives.drives[driveid].sectors);
548 u16 blksize = GET_GLOBAL(Drives.drives[driveid].blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500549
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400550 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
551 , size, type, npc, nph, npspt, (u32)lba, blksize);
552
553 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400554 if (type == DTYPE_ATAPI) {
555 // 0x74 = removable, media change, lockable, max values
556 SET_INT13DPT(regs, infos, 0x74);
557 SET_INT13DPT(regs, cylinders, 0xffffffff);
558 SET_INT13DPT(regs, heads, 0xffffffff);
559 SET_INT13DPT(regs, spt, 0xffffffff);
560 SET_INT13DPT(regs, sector_count, (u64)-1);
561 } else {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400562 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500563 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
564 SET_INT13DPT(regs, cylinders, 0x3fff);
565 } else {
566 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
567 SET_INT13DPT(regs, cylinders, (u32)npc);
568 }
569 SET_INT13DPT(regs, heads, (u32)nph);
570 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400571 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500572 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500573 SET_INT13DPT(regs, blksize, blksize);
574
Kevin O'Connorb1144362009-08-11 20:43:38 -0400575 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500576 disk_ret(regs, DISK_RET_SUCCESS);
577 return;
578 }
579
580 // EDD 2.x
581
Kevin O'Connor08815372008-12-29 21:16:31 -0500582 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400583 SET_INT13DPT(regs, size, 30);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500584
Kevin O'Connor08815372008-12-29 21:16:31 -0500585 SET_INT13DPT(regs, dpte_segment, ebda_seg);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500586 SET_INT13DPT(regs, dpte_offset
Kevin O'Connor609da232008-12-28 23:18:57 -0500587 , offsetof(struct extended_bios_data_area_s, dpte));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500588
589 // Fill in dpte
Kevin O'Connor707298a2009-08-11 22:27:51 -0400590 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400591 u8 channel = ataid / 2;
592 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400593 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
594 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
595 u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500596
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400597 u16 options = 0;
Kevin O'Connor42337662009-08-10 00:06:37 -0400598 if (type == DTYPE_ATA) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400599 u8 translation = GET_GLOBAL(Drives.drives[driveid].translation);
Kevin O'Connor42337662009-08-10 00:06:37 -0400600 if (translation != TRANSLATION_NONE) {
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400601 options |= 1<<3; // CHS translation
Kevin O'Connor42337662009-08-10 00:06:37 -0400602 if (translation == TRANSLATION_LBA)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400603 options |= 1<<9;
Kevin O'Connor42337662009-08-10 00:06:37 -0400604 if (translation == TRANSLATION_RECHS)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400605 options |= 3<<9;
606 }
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500607 } else {
608 // ATAPI
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400609 options |= 1<<5; // removable device
610 options |= 1<<6; // atapi device
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500611 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400612 options |= 1<<4; // lba translation
Kevin O'Connor32945af2009-02-27 21:23:01 -0500613 if (CONFIG_ATA_PIO32)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400614 options |= 1<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500615
Kevin O'Connor08815372008-12-29 21:16:31 -0500616 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
617 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
618 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
619 | ATA_CB_DH_LBA));
620 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
621 SET_EBDA2(ebda_seg, dpte.irq, irq);
622 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
623 SET_EBDA2(ebda_seg, dpte.dma, 0);
624 SET_EBDA2(ebda_seg, dpte.pio, 0);
625 SET_EBDA2(ebda_seg, dpte.options, options);
626 SET_EBDA2(ebda_seg, dpte.reserved, 0);
627 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500628
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500629 u8 sum = checksum_far(
630 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
631 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500632
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400633 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500634 disk_ret(regs, DISK_RET_SUCCESS);
635 return;
636 }
637
638 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500639 SET_INT13DPT(regs, key, 0xbedd);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400640 SET_INT13DPT(regs, dpi_length, 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500641 SET_INT13DPT(regs, reserved1, 0);
642 SET_INT13DPT(regs, reserved2, 0);
643
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400644 SET_INT13DPT(regs, host_bus[0], 'P');
645 SET_INT13DPT(regs, host_bus[1], 'C');
646 SET_INT13DPT(regs, host_bus[2], 'I');
647 SET_INT13DPT(regs, host_bus[3], 0);
648
Kevin O'Connorc892b132009-08-11 21:59:37 -0400649 u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400650 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
651 | (pci_bdf_to_fn(bdf) << 16));
652 SET_INT13DPT(regs, iface_path, path);
653
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500654 SET_INT13DPT(regs, iface_type[0], 'A');
655 SET_INT13DPT(regs, iface_type[1], 'T');
656 SET_INT13DPT(regs, iface_type[2], 'A');
657 SET_INT13DPT(regs, iface_type[3], 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400658 SET_INT13DPT(regs, iface_type[4], 0);
659 SET_INT13DPT(regs, iface_type[5], 0);
660 SET_INT13DPT(regs, iface_type[6], 0);
661 SET_INT13DPT(regs, iface_type[7], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500662
Kevin O'Connor970a0322008-10-26 12:01:21 -0400663 SET_INT13DPT(regs, device_path, slave);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500664
Kevin O'Connor7d108212009-01-21 19:13:21 -0500665 SET_INT13DPT(regs, checksum
666 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400667
668 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500669}
670
671// IBM/MS extended media change
672static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400673disk_1349(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500674{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400675 if (regs->dl < EXTSTART_CD) {
676 // Always success for HD
677 disk_ret(regs, DISK_RET_SUCCESS);
678 return;
679 }
680 set_fail(regs);
681 // always send changed ??
682 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500683}
684
685static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400686disk_134e01(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500687{
688 disk_ret(regs, DISK_RET_SUCCESS);
689}
690
691static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400692disk_134e03(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500693{
694 disk_ret(regs, DISK_RET_SUCCESS);
695}
696
697static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400698disk_134e04(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500699{
700 disk_ret(regs, DISK_RET_SUCCESS);
701}
702
703static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400704disk_134e06(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500705{
706 disk_ret(regs, DISK_RET_SUCCESS);
707}
708
709static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400710disk_134eXX(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500711{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500712 disk_ret(regs, DISK_RET_EPARAM);
713}
714
715// IBM/MS set hardware configuration
716static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400717disk_134e(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500718{
719 switch (regs->al) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400720 case 0x01: disk_134e01(regs, driveid); break;
721 case 0x03: disk_134e03(regs, driveid); break;
722 case 0x04: disk_134e04(regs, driveid); break;
723 case 0x06: disk_134e06(regs, driveid); break;
724 default: disk_134eXX(regs, driveid); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500725 }
726}
727
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500728void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400729disk_13XX(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500730{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500731 disk_ret(regs, DISK_RET_EPARAM);
732}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500733
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500734void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400735disk_13(struct bregs *regs, u8 driveid)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500736{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500737 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500738
739 // clear completion flag
740 SET_BDA(disk_interrupt_flag, 0);
741
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500742 switch (regs->ah) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400743 case 0x00: disk_1300(regs, driveid); break;
744 case 0x01: disk_1301(regs, driveid); break;
745 case 0x02: disk_1302(regs, driveid); break;
746 case 0x03: disk_1303(regs, driveid); break;
747 case 0x04: disk_1304(regs, driveid); break;
748 case 0x05: disk_1305(regs, driveid); break;
749 case 0x08: disk_1308(regs, driveid); break;
750 case 0x09: disk_1309(regs, driveid); break;
751 case 0x0c: disk_130c(regs, driveid); break;
752 case 0x0d: disk_130d(regs, driveid); break;
753 case 0x10: disk_1310(regs, driveid); break;
754 case 0x11: disk_1311(regs, driveid); break;
755 case 0x14: disk_1314(regs, driveid); break;
756 case 0x15: disk_1315(regs, driveid); break;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400757 case 0x16: disk_1316(regs, driveid); break;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400758 case 0x41: disk_1341(regs, driveid); break;
759 case 0x42: disk_1342(regs, driveid); break;
760 case 0x43: disk_1343(regs, driveid); break;
761 case 0x44: disk_1344(regs, driveid); break;
762 case 0x45: disk_1345(regs, driveid); break;
763 case 0x46: disk_1346(regs, driveid); break;
764 case 0x47: disk_1347(regs, driveid); break;
765 case 0x48: disk_1348(regs, driveid); break;
766 case 0x49: disk_1349(regs, driveid); break;
767 case 0x4e: disk_134e(regs, driveid); break;
768 default: disk_13XX(regs, driveid); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500769 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500770}
771
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400772static void
773floppy_13(struct bregs *regs, u8 driveid)
774{
775 // Only limited commands are supported on floppies.
776 switch (regs->ah) {
777 case 0x00:
778 case 0x01:
779 case 0x02:
780 case 0x03:
781 case 0x04:
782 case 0x05:
783 case 0x08:
784 case 0x15:
785 case 0x16:
786 disk_13(regs, driveid);
787 break;
788 default: disk_13XX(regs, driveid); break;
789 }
790}
791
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500792
793/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500794 * Entry points
795 ****************************************************************/
796
Kevin O'Connorc892b132009-08-11 21:59:37 -0400797static int
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400798get_driveid(struct bregs *regs, u8 exttype, u8 extdriveoffset)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500799{
800 // basic check : device has to be defined
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400801 if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
Kevin O'Connorc892b132009-08-11 21:59:37 -0400802 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500803
804 // Get the ata channel
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400805 u8 driveid = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500806
807 // basic check : device has to be valid
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400808 if (driveid >= ARRAY_SIZE(Drives.drives))
Kevin O'Connorc892b132009-08-11 21:59:37 -0400809 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500810
Kevin O'Connorc892b132009-08-11 21:59:37 -0400811 return driveid;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500812}
813
814static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400815handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500816{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400817 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400818 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500819 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500820 return;
821 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500822
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400823 if (extdrive < EXTSTART_HD) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400824 int driveid = get_driveid(regs, EXTTYPE_FLOPPY, extdrive);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400825 if (driveid < 0)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400826 goto fail;
827 floppy_13(regs, driveid);
828 return;
829 }
830
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400831 int driveid;
832 if (extdrive >= EXTSTART_CD)
833 driveid = get_driveid(regs, EXTTYPE_CD, extdrive - EXTSTART_CD);
834 else
835 driveid = get_driveid(regs, EXTTYPE_HD, extdrive - EXTSTART_HD);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400836 if (driveid < 0)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400837 goto fail;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400838 disk_13(regs, driveid);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400839 return;
840
841fail:
842 // XXX - support 1301/1308/1315 anyway?
843 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500844}
845
Kevin O'Connor19786762008-03-05 21:09:59 -0500846void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500847handle_40(struct bregs *regs)
848{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400849 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500850 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500851}
852
853// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500854void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500855handle_13(struct bregs *regs)
856{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400857 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400858 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500859
Kevin O'Connordfa16502008-03-22 20:13:08 -0400860 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500861 if (regs->ah == 0x4b) {
862 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400863 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500864 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500865 u16 ebda_seg = get_ebda_seg();
866 if (GET_EBDA2(ebda_seg, cdemu.active)) {
Kevin O'Connor669e6442009-08-11 22:36:30 -0400867 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400868 if (extdrive == emudrive) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500869 cdemu_13(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400870 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500871 }
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400872 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
Kevin O'Connor707298a2009-08-11 22:27:51 -0400873 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500874 }
875 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400876 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500877}
878
879// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500880void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400881handle_76()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500882{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400883 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400884 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400885 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500886}
Kevin O'Connor30853762009-01-17 18:49:20 -0500887
888// Old Fixed Disk Parameter Table (newer tables are in the ebda).
889struct fdpt_s OldFDPT VAR16FIXED(0xe401);