blob: 014701a40f461311fdfeea0a84f327fa58217d54 [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'Connoraa2590c2008-03-22 23:13:24 -0400183 u16 segment = GET_INT13EXT(regs, segment);
184 u16 offset = GET_INT13EXT(regs, offset);
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500185 dop.buf_fl = MAKE_FLATPTR(segment, offset);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500186 dop.count = GET_INT13EXT(regs, count);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400187
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500188 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400189
Kevin O'Connor1625a752009-08-09 13:08:21 -0400190 SET_INT13EXT(regs, count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500191
Kevin O'Connor126eac62009-08-16 13:32:24 -0400192 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500193}
194
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500195
196/****************************************************************
197 * Hard Drive functions
198 ****************************************************************/
199
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500200// disk controller reset
201static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400202disk_1300(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500203{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400204 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400205 dop.driveid = driveid;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400206 dop.command = CMD_RESET;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400207 int status = send_disk_op(&dop);
208 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500209}
210
211// read disk status
212static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400213disk_1301(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500214{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400215 u8 v;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400216 if (regs->dl < EXTSTART_HD)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400217 // Floppy
218 v = GET_BDA(floppy_last_status);
219 else
220 v = GET_BDA(disk_last_status);
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400221 regs->ah = v;
222 set_cf(regs, v);
223 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500224}
225
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500226// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500227static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400228disk_1302(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500229{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400230 basic_access(regs, driveid, CMD_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500231}
232
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500233// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500234static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400235disk_1303(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500236{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400237 basic_access(regs, driveid, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500238}
239
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500240// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500241static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400242disk_1304(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500243{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400244 basic_access(regs, driveid, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500245 // FIXME verify
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500246}
247
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500248// format disk track
249static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400250disk_1305(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500251{
252 DISK_STUB(regs);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400253
254 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
255 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
256
257 u8 num_sectors = regs->al;
258 u8 head = regs->dh;
259
260 if (head >= nlh || num_sectors == 0 || num_sectors > nlspt) {
261 disk_ret(regs, DISK_RET_EPARAM);
262 return;
263 }
264
265 struct disk_op_s dop;
266 dop.driveid = driveid;
267 dop.command = CMD_FORMAT;
268 dop.lba = head;
269 dop.count = num_sectors;
270 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
271 int status = send_disk_op(&dop);
272 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500273}
274
275// read disk drive parameters
276static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400277disk_1308(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500278{
279 // Get logical geometry from table
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400280 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders) - 1;
281 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads) - 1;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400282 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400283 u8 count;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400284 if (regs->dl < EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400285 // Floppy
286 count = GET_GLOBAL(Drives.floppycount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500287
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400288 regs->bx = GET_GLOBAL(Drives.drives[driveid].floppy_type);
289
290 // set es & di to point to 11 byte diskette param table in ROM
291 regs->es = SEG_BIOS;
292 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400293 } else if (regs->dl < EXTSTART_CD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400294 // Hard drive
295 count = GET_BDA(hdcount);
296 nlc--; // last sector reserved
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400297 } else {
298 // Not supported on CDROM
299 disk_ret(regs, DISK_RET_EPARAM);
300 return;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400301 }
302
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500303 regs->al = 0;
304 regs->ch = nlc & 0xff;
305 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400306 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500307
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500308 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400309 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500310}
311
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500312// initialize drive parameters
313static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400314disk_1309(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500315{
316 DISK_STUB(regs);
317}
318
319// seek to specified cylinder
320static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400321disk_130c(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500322{
323 DISK_STUB(regs);
324}
325
326// alternate disk reset
327static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400328disk_130d(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500329{
330 DISK_STUB(regs);
331}
332
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500333// check drive ready
334static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400335disk_1310(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500336{
337 // should look at 40:8E also???
338
Kevin O'Connor42337662009-08-10 00:06:37 -0400339 struct disk_op_s dop;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400340 dop.driveid = driveid;
Kevin O'Connor42337662009-08-10 00:06:37 -0400341 dop.command = CMD_ISREADY;
342 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400343 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500344}
345
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500346// recalibrate
347static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400348disk_1311(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500349{
350 DISK_STUB(regs);
351}
352
353// controller internal diagnostic
354static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400355disk_1314(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500356{
357 DISK_STUB(regs);
358}
359
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500360// read disk drive size
361static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400362disk_1315(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500363{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400364 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400365 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
366 // Floppy or cdrom
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400367 regs->ah = 1;
368 return;
369 }
370 // Hard drive
371
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500372 // Get logical geometry from table
Kevin O'Connor707298a2009-08-11 22:27:51 -0400373 u16 nlc = GET_GLOBAL(Drives.drives[driveid].lchs.cylinders);
374 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
375 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500376
377 // Compute sector count seen by int13
378 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
379 regs->cx = lba >> 16;
380 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500381 regs->ah = 3; // hard disk accessible
382}
383
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400384static void
385disk_1316(struct bregs *regs, u8 driveid)
386{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400387 if (regs->dl >= EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400388 // Hard drive
389 disk_ret(regs, DISK_RET_EPARAM);
390 return;
391 }
392 disk_ret(regs, DISK_RET_ECHANGED);
393}
394
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500395// IBM/MS installation check
396static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400397disk_1341(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500398{
399 regs->bx = 0xaa55; // install check
400 regs->cx = 0x0007; // ext disk access and edd, removable supported
401 disk_ret(regs, DISK_RET_SUCCESS);
402 regs->ah = 0x30; // EDD 3.0
403}
404
405// IBM/MS extended read
406static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400407disk_1342(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500408{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400409 extended_access(regs, driveid, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500410}
411
412// IBM/MS extended write
413static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400414disk_1343(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500415{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400416 extended_access(regs, driveid, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500417}
418
419// IBM/MS verify
420static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400421disk_1344(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500422{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400423 extended_access(regs, driveid, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500424}
425
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400426// lock
427static void
428disk_134500(struct bregs *regs, u8 driveid)
429{
430 u16 ebda_seg = get_ebda_seg();
431 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[driveid]);
432 if (locks == 0xff) {
433 regs->al = 1;
434 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
435 return;
436 }
437 SET_EBDA2(ebda_seg, cdrom_locks[driveid], locks + 1);
438 regs->al = 1;
439 disk_ret(regs, DISK_RET_SUCCESS);
440}
441
442// unlock
443static void
444disk_134501(struct bregs *regs, u8 driveid)
445{
446 u16 ebda_seg = get_ebda_seg();
447 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[driveid]);
448 if (locks == 0x00) {
449 regs->al = 0;
450 disk_ret(regs, DISK_RET_ENOTLOCKED);
451 return;
452 }
453 locks--;
454 SET_EBDA2(ebda_seg, cdrom_locks[driveid], locks);
455 regs->al = (locks ? 1 : 0);
456 disk_ret(regs, DISK_RET_SUCCESS);
457}
458
459// status
460static void
461disk_134502(struct bregs *regs, u8 driveid)
462{
463 u8 locks = GET_EBDA(cdrom_locks[driveid]);
464 regs->al = (locks ? 1 : 0);
465 disk_ret(regs, DISK_RET_SUCCESS);
466}
467
468static void
469disk_1345XX(struct bregs *regs, u8 driveid)
470{
471 disk_ret(regs, DISK_RET_EPARAM);
472}
473
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500474// IBM/MS lock/unlock drive
475static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400476disk_1345(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500477{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400478 if (regs->dl < EXTSTART_CD) {
479 // Always success for HD
480 disk_ret(regs, DISK_RET_SUCCESS);
481 return;
482 }
483
484 switch (regs->al) {
485 case 0x00: disk_134500(regs, driveid); break;
486 case 0x01: disk_134501(regs, driveid); break;
487 case 0x02: disk_134502(regs, driveid); break;
488 default: disk_1345XX(regs, driveid); break;
489 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500490}
491
492// IBM/MS eject media
493static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400494disk_1346(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500495{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400496 if (regs->dl < EXTSTART_CD) {
497 // Volume Not Removable
498 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
499 return;
500 }
501
502 u8 locks = GET_EBDA(cdrom_locks[driveid]);
503 if (locks != 0) {
504 disk_ret(regs, DISK_RET_ELOCKED);
505 return;
506 }
507
508 // FIXME should handle 0x31 no media in device
509 // FIXME should handle 0xb5 valid request failed
510
511 // Call removable media eject
512 struct bregs br;
513 memset(&br, 0, sizeof(br));
514 br.ah = 0x52;
515 call16_int(0x15, &br);
516
517 if (br.ah || br.flags & F_CF) {
518 disk_ret(regs, DISK_RET_ELOCKED);
519 return;
520 }
521 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500522}
523
524// IBM/MS extended seek
525static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400526disk_1347(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500527{
Kevin O'Connor707298a2009-08-11 22:27:51 -0400528 extended_access(regs, driveid, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500529}
530
531// IBM/MS get drive parameters
532static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400533disk_1348(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500534{
535 u16 size = GET_INT13DPT(regs, size);
536
537 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400538 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500539 disk_ret(regs, DISK_RET_EPARAM);
540 return;
541 }
542
543 // EDD 1.x
544
Kevin O'Connor707298a2009-08-11 22:27:51 -0400545 u8 type = GET_GLOBAL(Drives.drives[driveid].type);
546 u16 npc = GET_GLOBAL(Drives.drives[driveid].pchs.cylinders);
547 u16 nph = GET_GLOBAL(Drives.drives[driveid].pchs.heads);
548 u16 npspt = GET_GLOBAL(Drives.drives[driveid].pchs.spt);
549 u64 lba = GET_GLOBAL(Drives.drives[driveid].sectors);
550 u16 blksize = GET_GLOBAL(Drives.drives[driveid].blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500551
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400552 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
553 , size, type, npc, nph, npspt, (u32)lba, blksize);
554
555 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400556 if (type == DTYPE_ATAPI) {
557 // 0x74 = removable, media change, lockable, max values
558 SET_INT13DPT(regs, infos, 0x74);
559 SET_INT13DPT(regs, cylinders, 0xffffffff);
560 SET_INT13DPT(regs, heads, 0xffffffff);
561 SET_INT13DPT(regs, spt, 0xffffffff);
562 SET_INT13DPT(regs, sector_count, (u64)-1);
563 } else {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400564 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500565 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
566 SET_INT13DPT(regs, cylinders, 0x3fff);
567 } else {
568 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
569 SET_INT13DPT(regs, cylinders, (u32)npc);
570 }
571 SET_INT13DPT(regs, heads, (u32)nph);
572 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400573 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500574 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500575 SET_INT13DPT(regs, blksize, blksize);
576
Kevin O'Connorb1144362009-08-11 20:43:38 -0400577 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500578 disk_ret(regs, DISK_RET_SUCCESS);
579 return;
580 }
581
582 // EDD 2.x
583
Kevin O'Connor08815372008-12-29 21:16:31 -0500584 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400585 SET_INT13DPT(regs, size, 30);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500586
Kevin O'Connor08815372008-12-29 21:16:31 -0500587 SET_INT13DPT(regs, dpte_segment, ebda_seg);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500588 SET_INT13DPT(regs, dpte_offset
Kevin O'Connor609da232008-12-28 23:18:57 -0500589 , offsetof(struct extended_bios_data_area_s, dpte));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500590
591 // Fill in dpte
Kevin O'Connor707298a2009-08-11 22:27:51 -0400592 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400593 u8 channel = ataid / 2;
594 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400595 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
596 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
597 u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500598
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400599 u16 options = 0;
Kevin O'Connor42337662009-08-10 00:06:37 -0400600 if (type == DTYPE_ATA) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400601 u8 translation = GET_GLOBAL(Drives.drives[driveid].translation);
Kevin O'Connor42337662009-08-10 00:06:37 -0400602 if (translation != TRANSLATION_NONE) {
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400603 options |= 1<<3; // CHS translation
Kevin O'Connor42337662009-08-10 00:06:37 -0400604 if (translation == TRANSLATION_LBA)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400605 options |= 1<<9;
Kevin O'Connor42337662009-08-10 00:06:37 -0400606 if (translation == TRANSLATION_RECHS)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400607 options |= 3<<9;
608 }
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500609 } else {
610 // ATAPI
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400611 options |= 1<<5; // removable device
612 options |= 1<<6; // atapi device
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500613 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400614 options |= 1<<4; // lba translation
Kevin O'Connor32945af2009-02-27 21:23:01 -0500615 if (CONFIG_ATA_PIO32)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400616 options |= 1<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500617
Kevin O'Connor08815372008-12-29 21:16:31 -0500618 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
619 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
620 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
621 | ATA_CB_DH_LBA));
622 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
623 SET_EBDA2(ebda_seg, dpte.irq, irq);
624 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
625 SET_EBDA2(ebda_seg, dpte.dma, 0);
626 SET_EBDA2(ebda_seg, dpte.pio, 0);
627 SET_EBDA2(ebda_seg, dpte.options, options);
628 SET_EBDA2(ebda_seg, dpte.reserved, 0);
629 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500630
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500631 u8 sum = checksum_far(
632 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
633 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500634
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400635 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500636 disk_ret(regs, DISK_RET_SUCCESS);
637 return;
638 }
639
640 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500641 SET_INT13DPT(regs, key, 0xbedd);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400642 SET_INT13DPT(regs, dpi_length, 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500643 SET_INT13DPT(regs, reserved1, 0);
644 SET_INT13DPT(regs, reserved2, 0);
645
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400646 SET_INT13DPT(regs, host_bus[0], 'P');
647 SET_INT13DPT(regs, host_bus[1], 'C');
648 SET_INT13DPT(regs, host_bus[2], 'I');
649 SET_INT13DPT(regs, host_bus[3], 0);
650
Kevin O'Connorc892b132009-08-11 21:59:37 -0400651 u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400652 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
653 | (pci_bdf_to_fn(bdf) << 16));
654 SET_INT13DPT(regs, iface_path, path);
655
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500656 SET_INT13DPT(regs, iface_type[0], 'A');
657 SET_INT13DPT(regs, iface_type[1], 'T');
658 SET_INT13DPT(regs, iface_type[2], 'A');
659 SET_INT13DPT(regs, iface_type[3], 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400660 SET_INT13DPT(regs, iface_type[4], 0);
661 SET_INT13DPT(regs, iface_type[5], 0);
662 SET_INT13DPT(regs, iface_type[6], 0);
663 SET_INT13DPT(regs, iface_type[7], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500664
Kevin O'Connor970a0322008-10-26 12:01:21 -0400665 SET_INT13DPT(regs, device_path, slave);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500666
Kevin O'Connor7d108212009-01-21 19:13:21 -0500667 SET_INT13DPT(regs, checksum
668 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400669
670 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500671}
672
673// IBM/MS extended media change
674static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400675disk_1349(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500676{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400677 if (regs->dl < EXTSTART_CD) {
678 // Always success for HD
679 disk_ret(regs, DISK_RET_SUCCESS);
680 return;
681 }
682 set_fail(regs);
683 // always send changed ??
684 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500685}
686
687static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400688disk_134e01(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500689{
690 disk_ret(regs, DISK_RET_SUCCESS);
691}
692
693static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400694disk_134e03(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500695{
696 disk_ret(regs, DISK_RET_SUCCESS);
697}
698
699static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400700disk_134e04(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500701{
702 disk_ret(regs, DISK_RET_SUCCESS);
703}
704
705static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400706disk_134e06(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500707{
708 disk_ret(regs, DISK_RET_SUCCESS);
709}
710
711static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400712disk_134eXX(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500713{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500714 disk_ret(regs, DISK_RET_EPARAM);
715}
716
717// IBM/MS set hardware configuration
718static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400719disk_134e(struct bregs *regs, u8 driveid)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500720{
721 switch (regs->al) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400722 case 0x01: disk_134e01(regs, driveid); break;
723 case 0x03: disk_134e03(regs, driveid); break;
724 case 0x04: disk_134e04(regs, driveid); break;
725 case 0x06: disk_134e06(regs, driveid); break;
726 default: disk_134eXX(regs, driveid); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500727 }
728}
729
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500730void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400731disk_13XX(struct bregs *regs, u8 driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500732{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500733 disk_ret(regs, DISK_RET_EPARAM);
734}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500735
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500736void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400737disk_13(struct bregs *regs, u8 driveid)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500738{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500739 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500740
741 // clear completion flag
742 SET_BDA(disk_interrupt_flag, 0);
743
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500744 switch (regs->ah) {
Kevin O'Connor707298a2009-08-11 22:27:51 -0400745 case 0x00: disk_1300(regs, driveid); break;
746 case 0x01: disk_1301(regs, driveid); break;
747 case 0x02: disk_1302(regs, driveid); break;
748 case 0x03: disk_1303(regs, driveid); break;
749 case 0x04: disk_1304(regs, driveid); break;
750 case 0x05: disk_1305(regs, driveid); break;
751 case 0x08: disk_1308(regs, driveid); break;
752 case 0x09: disk_1309(regs, driveid); break;
753 case 0x0c: disk_130c(regs, driveid); break;
754 case 0x0d: disk_130d(regs, driveid); break;
755 case 0x10: disk_1310(regs, driveid); break;
756 case 0x11: disk_1311(regs, driveid); break;
757 case 0x14: disk_1314(regs, driveid); break;
758 case 0x15: disk_1315(regs, driveid); break;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400759 case 0x16: disk_1316(regs, driveid); break;
Kevin O'Connor707298a2009-08-11 22:27:51 -0400760 case 0x41: disk_1341(regs, driveid); break;
761 case 0x42: disk_1342(regs, driveid); break;
762 case 0x43: disk_1343(regs, driveid); break;
763 case 0x44: disk_1344(regs, driveid); break;
764 case 0x45: disk_1345(regs, driveid); break;
765 case 0x46: disk_1346(regs, driveid); break;
766 case 0x47: disk_1347(regs, driveid); break;
767 case 0x48: disk_1348(regs, driveid); break;
768 case 0x49: disk_1349(regs, driveid); break;
769 case 0x4e: disk_134e(regs, driveid); break;
770 default: disk_13XX(regs, driveid); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500771 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500772}
773
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400774static void
775floppy_13(struct bregs *regs, u8 driveid)
776{
777 // Only limited commands are supported on floppies.
778 switch (regs->ah) {
779 case 0x00:
780 case 0x01:
781 case 0x02:
782 case 0x03:
783 case 0x04:
784 case 0x05:
785 case 0x08:
786 case 0x15:
787 case 0x16:
788 disk_13(regs, driveid);
789 break;
790 default: disk_13XX(regs, driveid); break;
791 }
792}
793
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500794
795/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500796 * Entry points
797 ****************************************************************/
798
Kevin O'Connorc892b132009-08-11 21:59:37 -0400799static int
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400800get_driveid(struct bregs *regs, u8 exttype, u8 extdriveoffset)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500801{
802 // basic check : device has to be defined
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400803 if (extdriveoffset >= ARRAY_SIZE(Drives.idmap[0]))
Kevin O'Connorc892b132009-08-11 21:59:37 -0400804 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500805
806 // Get the ata channel
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400807 u8 driveid = GET_GLOBAL(Drives.idmap[exttype][extdriveoffset]);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500808
809 // basic check : device has to be valid
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400810 if (driveid >= ARRAY_SIZE(Drives.drives))
Kevin O'Connorc892b132009-08-11 21:59:37 -0400811 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500812
Kevin O'Connorc892b132009-08-11 21:59:37 -0400813 return driveid;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500814}
815
816static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400817handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500818{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400819 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400820 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500821 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500822 return;
823 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500824
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400825 if (extdrive < EXTSTART_HD) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400826 int driveid = get_driveid(regs, EXTTYPE_FLOPPY, extdrive);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400827 if (driveid < 0)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400828 goto fail;
829 floppy_13(regs, driveid);
830 return;
831 }
832
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400833 int driveid;
834 if (extdrive >= EXTSTART_CD)
835 driveid = get_driveid(regs, EXTTYPE_CD, extdrive - EXTSTART_CD);
836 else
837 driveid = get_driveid(regs, EXTTYPE_HD, extdrive - EXTSTART_HD);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400838 if (driveid < 0)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400839 goto fail;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400840 disk_13(regs, driveid);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400841 return;
842
843fail:
844 // XXX - support 1301/1308/1315 anyway?
845 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500846}
847
Kevin O'Connor19786762008-03-05 21:09:59 -0500848void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500849handle_40(struct bregs *regs)
850{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400851 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500852 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500853}
854
855// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500856void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500857handle_13(struct bregs *regs)
858{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400859 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400860 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500861
Kevin O'Connordfa16502008-03-22 20:13:08 -0400862 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500863 if (regs->ah == 0x4b) {
864 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400865 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500866 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500867 u16 ebda_seg = get_ebda_seg();
868 if (GET_EBDA2(ebda_seg, cdemu.active)) {
Kevin O'Connor669e6442009-08-11 22:36:30 -0400869 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400870 if (extdrive == emudrive) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500871 cdemu_13(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400872 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500873 }
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400874 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
Kevin O'Connor707298a2009-08-11 22:27:51 -0400875 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500876 }
877 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400878 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500879}
880
881// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500882void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400883handle_76()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500884{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400885 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400886 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400887 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500888}
Kevin O'Connor30853762009-01-17 18:49:20 -0500889
890// Old Fixed Disk Parameter Table (newer tables are in the ebda).
891struct fdpt_s OldFDPT VAR16FIXED(0xe401);