blob: 80290ce1daf5b0782013c384396d18f37bd438ef [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 16bit code to access hard drives.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor9521e262008-07-04 13:04:29 -04008#include "biosvar.h" // SET_BDA
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "bregs.h" // struct bregs
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040010#include "config.h" // CONFIG_*
Kevin O'Connor5d369d82013-09-02 20:48:46 -040011#include "hw/ata.h" // ATA_CB_DC
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "hw/pci.h" // pci_bdf_to_bus
13#include "hw/pic.h" // pic_eoi2
14#include "output.h" // debug_enter
Kevin O'Connor3df600b2013-09-14 19:28:55 -040015#include "stacks.h" // call16_int
Kevin O'Connor135f3f62013-09-14 23:57:26 -040016#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040017#include "string.h" // memset
Kevin O'Connor135f3f62013-09-14 23:57:26 -040018#include "util.h" // CDRom_locks
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050019
Kevin O'Connorb74102d2008-03-03 21:57:30 -050020
21/****************************************************************
22 * Helper functions
23 ****************************************************************/
24
Kevin O'Connor567e4e32008-04-05 11:37:51 -040025static void
Kevin O'Connor05600342009-01-02 13:10:58 -050026__disk_stub(struct bregs *regs, int lineno, const char *fname)
Kevin O'Connor567e4e32008-04-05 11:37:51 -040027{
Kevin O'Connordfefeb52009-12-13 13:04:17 -050028 __warn_unimplemented(regs, lineno, fname);
Kevin O'Connor05600342009-01-02 13:10:58 -050029 __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040030}
31
Kevin O'Connor05600342009-01-02 13:10:58 -050032#define DISK_STUB(regs) \
33 __disk_stub((regs), __LINE__, __func__)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050034
Kevin O'Connor5c99b6c2009-12-30 12:36:22 -050035// Get the cylinders/heads/sectors for the given drive.
Kevin O'Connor48365582012-05-27 10:45:32 -040036static struct chs_s
37getLCHS(struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050038{
Kevin O'Connor48365582012-05-27 10:45:32 -040039 struct chs_s res = { };
Kevin O'Connor8f469b92010-02-28 01:28:11 -050040 if (CONFIG_CDROM_EMU
41 && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf))) {
Kevin O'Connord3140832012-05-13 22:46:12 -040042 // Emulated drive - get info from CDEmu. (It's not possible to
Kevin O'Connor36c93a52009-09-12 19:35:04 -040043 // populate the geometry directly in the driveid because the
44 // geometry is only known after the bios segment is made
45 // read-only).
Kevin O'Connor8ab9a342013-09-28 23:34:49 -040046 res.cylinder = GET_LOW(CDEmu.lchs.cylinder);
47 res.head = GET_LOW(CDEmu.lchs.head);
48 res.sector = GET_LOW(CDEmu.lchs.sector);
Kevin O'Connor48365582012-05-27 10:45:32 -040049 return res;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040050 }
Kevin O'Connor8ab9a342013-09-28 23:34:49 -040051 res.cylinder = GET_GLOBAL(drive_g->lchs.cylinder);
52 res.head = GET_GLOBAL(drive_g->lchs.head);
53 res.sector = GET_GLOBAL(drive_g->lchs.sector);
Kevin O'Connor48365582012-05-27 10:45:32 -040054 return res;
Kevin O'Connor1625a752009-08-09 13:08:21 -040055}
56
57// Perform read/write/verify using old-style chs accesses
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -040058static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -040059basic_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -040060{
61 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -040062 dop.drive_g = drive_g;
Kevin O'Connor1625a752009-08-09 13:08:21 -040063 dop.command = command;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040064
65 u8 count = regs->al;
66 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
67 u16 sector = regs->cl & 0x3f;
68 u16 head = regs->dh;
69
70 if (count > 128 || count == 0 || sector == 0) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050071 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040072 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040073 return;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040074 }
75 dop.count = count;
76
Kevin O'Connor48365582012-05-27 10:45:32 -040077 struct chs_s chs = getLCHS(drive_g);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -040078 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040079
80 // sanity check on cyl heads, sec
Kevin O'Connor8ab9a342013-09-28 23:34:49 -040081 if (cylinder >= nlc || head >= nlh || sector > nls) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050082 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040083 disk_ret(regs, DISK_RET_EPARAM);
84 return;
85 }
86
87 // translate lchs to lba
Kevin O'Connor8ab9a342013-09-28 23:34:49 -040088 dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nls)
Kevin O'Connor36c93a52009-09-12 19:35:04 -040089 + (u32)sector - 1);
90
Kevin O'Connorb68ac712009-08-09 17:25:19 -040091 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -040092
Kevin O'Connor4524bf72008-12-31 00:31:03 -050093 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -040094
Kevin O'Connor1625a752009-08-09 13:08:21 -040095 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050096
Kevin O'Connor126eac62009-08-16 13:32:24 -040097 disk_ret(regs, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -050098}
99
Kevin O'Connor1625a752009-08-09 13:08:21 -0400100// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400101static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400102extended_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500103{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500104 struct disk_op_s dop;
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400105 struct int13ext_s *param_far = (struct int13ext_s*)(regs->si+0);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400106 // Get lba and check.
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400107 dop.lba = GET_FARVAR(regs->ds, param_far->lba);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500108 dop.command = command;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400109 dop.drive_g = drive_g;
110 if (dop.lba >= GET_GLOBAL(drive_g->sectors)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500111 warn_invalid(regs);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400112 disk_ret(regs, DISK_RET_EPARAM);
113 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500114 }
115
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400116 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_FARVAR(regs->ds, param_far->data));
117 dop.count = GET_FARVAR(regs->ds, param_far->count);
Kevin O'Connorb50488b2012-02-11 09:38:44 -0500118 if (! dop.count) {
119 // Nothing to do.
120 disk_ret(regs, DISK_RET_SUCCESS);
121 return;
122 }
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400123
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500124 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400125
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400126 SET_FARVAR(regs->ds, param_far->count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500127
Kevin O'Connor126eac62009-08-16 13:32:24 -0400128 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500129}
130
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500131
132/****************************************************************
133 * Hard Drive functions
134 ****************************************************************/
135
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500136// disk controller reset
137static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400138disk_1300(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500139{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400140 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400141 dop.drive_g = drive_g;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400142 dop.command = CMD_RESET;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400143 int status = send_disk_op(&dop);
144 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500145}
146
147// read disk status
148static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400149disk_1301(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500150{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400151 u8 v;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400152 if (regs->dl < EXTSTART_HD)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400153 // Floppy
154 v = GET_BDA(floppy_last_status);
155 else
156 v = GET_BDA(disk_last_status);
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400157 regs->ah = v;
158 set_cf(regs, v);
159 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500160}
161
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500162// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500163static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400164disk_1302(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500165{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400166 basic_access(regs, drive_g, CMD_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500167}
168
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500169// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500170static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400171disk_1303(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500172{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400173 basic_access(regs, drive_g, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500174}
175
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500176// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500177static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400178disk_1304(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500179{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400180 basic_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500181}
182
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500183// format disk track
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400184static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400185disk_1305(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500186{
Kevin O'Connor4d9d4002009-11-25 19:35:01 -0500187 debug_stub(regs);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400188
Kevin O'Connor48365582012-05-27 10:45:32 -0400189 struct chs_s chs = getLCHS(drive_g);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400190 u16 nlh=chs.head, nls=chs.sector;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400191
192 u8 num_sectors = regs->al;
193 u8 head = regs->dh;
194
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400195 if (head >= nlh || num_sectors == 0 || num_sectors > nls) {
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400196 disk_ret(regs, DISK_RET_EPARAM);
197 return;
198 }
199
200 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400201 dop.drive_g = drive_g;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400202 dop.command = CMD_FORMAT;
203 dop.lba = head;
204 dop.count = num_sectors;
205 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
206 int status = send_disk_op(&dop);
207 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500208}
209
210// read disk drive parameters
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400211static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400212disk_1308(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500213{
214 // Get logical geometry from table
Kevin O'Connor48365582012-05-27 10:45:32 -0400215 struct chs_s chs = getLCHS(drive_g);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400216 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400217 nlc--;
218 nlh--;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400219 u8 count;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400220 if (regs->dl < EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400221 // Floppy
Kevin O'Connora0842f82010-12-29 11:05:46 -0500222 count = GET_GLOBAL(FloppyCount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500223
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500224 if (CONFIG_CDROM_EMU
225 && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf)))
Kevin O'Connord3140832012-05-13 22:46:12 -0400226 regs->bx = GET_LOW(CDEmu.media) * 2;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400227 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400228 regs->bx = GET_GLOBAL(drive_g->floppy_type);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400229
230 // set es & di to point to 11 byte diskette param table in ROM
231 regs->es = SEG_BIOS;
232 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400233 } else if (regs->dl < EXTSTART_CD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400234 // Hard drive
235 count = GET_BDA(hdcount);
236 nlc--; // last sector reserved
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400237 } else {
238 // Not supported on CDROM
239 disk_ret(regs, DISK_RET_EPARAM);
240 return;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400241 }
242
Kevin O'Connord3140832012-05-13 22:46:12 -0400243 if (CONFIG_CDROM_EMU && GET_LOW(CDEmu.active)) {
244 u8 emudrive = GET_LOW(CDEmu.emulated_extdrive);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400245 if (((emudrive ^ regs->dl) & 0x80) == 0)
246 // Note extra drive due to emulation.
247 count++;
248 if (regs->dl < EXTSTART_HD && count > 2)
249 // Max of two floppy drives.
250 count = 2;
251 }
252
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500253 regs->al = 0;
254 regs->ch = nlc & 0xff;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400255 regs->cl = ((nlc >> 2) & 0xc0) | (nls & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400256 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500257
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500258 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400259 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500260}
261
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500262// initialize drive parameters
263static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400264disk_1309(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500265{
266 DISK_STUB(regs);
267}
268
269// seek to specified cylinder
270static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400271disk_130c(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500272{
273 DISK_STUB(regs);
274}
275
276// alternate disk reset
277static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400278disk_130d(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500279{
280 DISK_STUB(regs);
281}
282
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500283// check drive ready
284static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400285disk_1310(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500286{
287 // should look at 40:8E also???
288
Kevin O'Connor42337662009-08-10 00:06:37 -0400289 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400290 dop.drive_g = drive_g;
Kevin O'Connor42337662009-08-10 00:06:37 -0400291 dop.command = CMD_ISREADY;
292 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400293 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500294}
295
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500296// recalibrate
297static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400298disk_1311(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500299{
300 DISK_STUB(regs);
301}
302
303// controller internal diagnostic
304static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400305disk_1314(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500306{
307 DISK_STUB(regs);
308}
309
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500310// read disk drive size
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400311static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400312disk_1315(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500313{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400314 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400315 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
316 // Floppy or cdrom
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400317 regs->ah = 1;
318 return;
319 }
320 // Hard drive
321
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500322 // Get logical geometry from table
Kevin O'Connor48365582012-05-27 10:45:32 -0400323 struct chs_s chs = getLCHS(drive_g);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400324 u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500325
326 // Compute sector count seen by int13
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400327 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nls;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500328 regs->cx = lba >> 16;
329 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500330 regs->ah = 3; // hard disk accessible
331}
332
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400333static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400334disk_1316(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400335{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400336 if (regs->dl >= EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400337 // Hard drive
338 disk_ret(regs, DISK_RET_EPARAM);
339 return;
340 }
341 disk_ret(regs, DISK_RET_ECHANGED);
342}
343
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500344// IBM/MS installation check
345static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400346disk_1341(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500347{
348 regs->bx = 0xaa55; // install check
349 regs->cx = 0x0007; // ext disk access and edd, removable supported
350 disk_ret(regs, DISK_RET_SUCCESS);
351 regs->ah = 0x30; // EDD 3.0
352}
353
354// IBM/MS extended read
355static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400356disk_1342(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500357{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400358 extended_access(regs, drive_g, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500359}
360
361// IBM/MS extended write
362static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400363disk_1343(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500364{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400365 extended_access(regs, drive_g, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500366}
367
368// IBM/MS verify
369static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400370disk_1344(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500371{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400372 extended_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500373}
374
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400375// lock
376static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400377disk_134500(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400378{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400379 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400380 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400381 if (locks == 0xff) {
382 regs->al = 1;
383 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
384 return;
385 }
Kevin O'Connord3140832012-05-13 22:46:12 -0400386 SET_LOW(CDRom_locks[cdid], locks + 1);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400387 regs->al = 1;
388 disk_ret(regs, DISK_RET_SUCCESS);
389}
390
391// unlock
392static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400393disk_134501(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400394{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400395 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400396 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400397 if (locks == 0x00) {
398 regs->al = 0;
399 disk_ret(regs, DISK_RET_ENOTLOCKED);
400 return;
401 }
402 locks--;
Kevin O'Connord3140832012-05-13 22:46:12 -0400403 SET_LOW(CDRom_locks[cdid], locks);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400404 regs->al = (locks ? 1 : 0);
405 disk_ret(regs, DISK_RET_SUCCESS);
406}
407
408// status
409static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400410disk_134502(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400411{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400412 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400413 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400414 regs->al = (locks ? 1 : 0);
415 disk_ret(regs, DISK_RET_SUCCESS);
416}
417
418static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400419disk_1345XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400420{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500421 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400422}
423
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500424// IBM/MS lock/unlock drive
425static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400426disk_1345(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500427{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400428 if (regs->dl < EXTSTART_CD) {
429 // Always success for HD
430 disk_ret(regs, DISK_RET_SUCCESS);
431 return;
432 }
433
434 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400435 case 0x00: disk_134500(regs, drive_g); break;
436 case 0x01: disk_134501(regs, drive_g); break;
437 case 0x02: disk_134502(regs, drive_g); break;
438 default: disk_1345XX(regs, drive_g); break;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400439 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500440}
441
442// IBM/MS eject media
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400443static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400444disk_1346(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500445{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400446 if (regs->dl < EXTSTART_CD) {
447 // Volume Not Removable
448 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
449 return;
450 }
451
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400452 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400453 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400454 if (locks != 0) {
455 disk_ret(regs, DISK_RET_ELOCKED);
456 return;
457 }
458
459 // FIXME should handle 0x31 no media in device
460 // FIXME should handle 0xb5 valid request failed
461
462 // Call removable media eject
463 struct bregs br;
464 memset(&br, 0, sizeof(br));
465 br.ah = 0x52;
Kevin O'Connor74152702010-03-20 17:53:40 -0400466 br.dl = regs->dl;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400467 call16_int(0x15, &br);
468
469 if (br.ah || br.flags & F_CF) {
470 disk_ret(regs, DISK_RET_ELOCKED);
471 return;
472 }
473 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500474}
475
476// IBM/MS extended seek
477static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400478disk_1347(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500479{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400480 extended_access(regs, drive_g, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500481}
482
483// IBM/MS get drive parameters
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400484static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400485disk_1348(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500486{
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400487 u16 seg = regs->ds;
488 struct int13dpt_s *param_far = (struct int13dpt_s*)(regs->si+0);
489 u16 size = GET_FARVAR(seg, param_far->size);
Gleb Natapovf5154e22011-01-10 10:50:27 +0200490 u16 t13 = size == 74;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500491
492 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400493 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500494 disk_ret(regs, DISK_RET_EPARAM);
495 return;
496 }
497
498 // EDD 1.x
499
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400500 u8 type = GET_GLOBAL(drive_g->type);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400501 u16 npc = GET_GLOBAL(drive_g->pchs.cylinder);
502 u16 nph = GET_GLOBAL(drive_g->pchs.head);
503 u16 nps = GET_GLOBAL(drive_g->pchs.sector);
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400504 u64 lba = GET_GLOBAL(drive_g->sectors);
505 u16 blksize = GET_GLOBAL(drive_g->blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500506
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400507 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400508 , size, type, npc, nph, nps, (u32)lba, blksize);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400509
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400510 SET_FARVAR(seg, param_far->size, 26);
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400511 if (type == DTYPE_ATA_ATAPI) {
Kevin O'Connorb1144362009-08-11 20:43:38 -0400512 // 0x74 = removable, media change, lockable, max values
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400513 SET_FARVAR(seg, param_far->infos, 0x74);
514 SET_FARVAR(seg, param_far->cylinders, 0xffffffff);
515 SET_FARVAR(seg, param_far->heads, 0xffffffff);
516 SET_FARVAR(seg, param_far->spt, 0xffffffff);
517 SET_FARVAR(seg, param_far->sector_count, (u64)-1);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400518 } else {
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400519 if (lba > (u64)nps*nph*0x3fff) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400520 SET_FARVAR(seg, param_far->infos, 0x00); // geometry is invalid
521 SET_FARVAR(seg, param_far->cylinders, 0x3fff);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500522 } else {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400523 SET_FARVAR(seg, param_far->infos, 0x02); // geometry is valid
524 SET_FARVAR(seg, param_far->cylinders, (u32)npc);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500525 }
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400526 SET_FARVAR(seg, param_far->heads, (u32)nph);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400527 SET_FARVAR(seg, param_far->spt, (u32)nps);
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400528 SET_FARVAR(seg, param_far->sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500529 }
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400530 SET_FARVAR(seg, param_far->blksize, blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500531
Gleb Natapovf77e1792010-12-23 11:29:35 +0200532 if (size < 30 ||
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400533 (type != DTYPE_ATA && type != DTYPE_ATA_ATAPI &&
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100534 type != DTYPE_VIRTIO_BLK && type != DTYPE_VIRTIO_SCSI)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500535 disk_ret(regs, DISK_RET_SUCCESS);
536 return;
537 }
538
539 // EDD 2.x
540
Gleb Natapovf77e1792010-12-23 11:29:35 +0200541 int bdf;
Gleb Natapovf5154e22011-01-10 10:50:27 +0200542 u16 iobase1 = 0;
543 u64 device_path = 0;
544 u8 channel = 0;
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400545 SET_FARVAR(seg, param_far->size, 30);
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400546 if (type == DTYPE_ATA || type == DTYPE_ATA_ATAPI) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400547 SET_FARVAR(seg, param_far->dpte, SEGOFF(SEG_LOW, (u32)&DefaultDPTE));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500548
Gleb Natapovf77e1792010-12-23 11:29:35 +0200549 // Fill in dpte
550 struct atadrive_s *adrive_g = container_of(
551 drive_g, struct atadrive_s, drive);
552 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
553 u8 slave = GET_GLOBAL(adrive_g->slave);
554 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
555 u8 irq = GET_GLOBALFLAT(chan_gf->irq);
556 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
557 bdf = GET_GLOBALFLAT(chan_gf->pci_bdf);
558 device_path = slave;
Gleb Natapovf5154e22011-01-10 10:50:27 +0200559 channel = GET_GLOBALFLAT(chan_gf->chanid);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500560
Gleb Natapovf77e1792010-12-23 11:29:35 +0200561 u16 options = 0;
562 if (type == DTYPE_ATA) {
563 u8 translation = GET_GLOBAL(drive_g->translation);
564 if (translation != TRANSLATION_NONE) {
565 options |= 1<<3; // CHS translation
566 if (translation == TRANSLATION_LBA)
567 options |= 1<<9;
568 if (translation == TRANSLATION_RECHS)
569 options |= 3<<9;
570 }
571 } else {
572 // ATAPI
573 options |= 1<<5; // removable device
574 options |= 1<<6; // atapi device
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400575 }
Gleb Natapovf77e1792010-12-23 11:29:35 +0200576 options |= 1<<4; // lba translation
577 if (CONFIG_ATA_PIO32)
578 options |= 1<<7;
579
Kevin O'Connord3140832012-05-13 22:46:12 -0400580 SET_LOW(DefaultDPTE.iobase1, iobase1);
581 SET_LOW(DefaultDPTE.iobase2, iobase2 + ATA_CB_DC);
582 SET_LOW(DefaultDPTE.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
583 | ATA_CB_DH_LBA));
584 SET_LOW(DefaultDPTE.unused, 0xcb);
585 SET_LOW(DefaultDPTE.irq, irq);
586 SET_LOW(DefaultDPTE.blkcount, 1);
587 SET_LOW(DefaultDPTE.dma, 0);
588 SET_LOW(DefaultDPTE.pio, 0);
589 SET_LOW(DefaultDPTE.options, options);
590 SET_LOW(DefaultDPTE.reserved, 0);
591 SET_LOW(DefaultDPTE.revision, 0x11);
Gleb Natapovf77e1792010-12-23 11:29:35 +0200592
Kevin O'Connord3140832012-05-13 22:46:12 -0400593 u8 sum = checksum_far(SEG_LOW, &DefaultDPTE, 15);
594 SET_LOW(DefaultDPTE.checksum, -sum);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500595 } else {
Bruce Rogers9474a352012-10-25 13:48:16 -0600596 SET_FARVAR(seg, param_far->dpte.segoff, 0xffffffff);
Gleb Natapovf77e1792010-12-23 11:29:35 +0200597 bdf = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500598 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500599
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400600 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500601 disk_ret(regs, DISK_RET_SUCCESS);
602 return;
603 }
604
605 // EDD 3.x
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400606 SET_FARVAR(seg, param_far->key, 0xbedd);
607 SET_FARVAR(seg, param_far->dpi_length, t13 ? 44 : 36);
608 SET_FARVAR(seg, param_far->reserved1, 0);
609 SET_FARVAR(seg, param_far->reserved2, 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500610
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500611 if (bdf != -1) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400612 SET_FARVAR(seg, param_far->host_bus[0], 'P');
613 SET_FARVAR(seg, param_far->host_bus[1], 'C');
614 SET_FARVAR(seg, param_far->host_bus[2], 'I');
615 SET_FARVAR(seg, param_far->host_bus[3], ' ');
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400616
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500617 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
618 | (pci_bdf_to_fn(bdf) << 16));
Gleb Natapovf5154e22011-01-10 10:50:27 +0200619 if (t13)
620 path |= channel << 24;
621
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400622 SET_FARVAR(seg, param_far->iface_path, path);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500623 } else {
624 // ISA
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400625 SET_FARVAR(seg, param_far->host_bus[0], 'I');
626 SET_FARVAR(seg, param_far->host_bus[1], 'S');
627 SET_FARVAR(seg, param_far->host_bus[2], 'A');
628 SET_FARVAR(seg, param_far->host_bus[3], ' ');
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500629
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400630 SET_FARVAR(seg, param_far->iface_path, iobase1);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500631 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400632
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100633 if (type != DTYPE_VIRTIO_BLK) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400634 SET_FARVAR(seg, param_far->iface_type[0], 'A');
635 SET_FARVAR(seg, param_far->iface_type[1], 'T');
636 SET_FARVAR(seg, param_far->iface_type[2], 'A');
637 SET_FARVAR(seg, param_far->iface_type[3], ' ');
Gleb Natapovf77e1792010-12-23 11:29:35 +0200638 } else {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400639 SET_FARVAR(seg, param_far->iface_type[0], 'S');
640 SET_FARVAR(seg, param_far->iface_type[1], 'C');
641 SET_FARVAR(seg, param_far->iface_type[2], 'S');
642 SET_FARVAR(seg, param_far->iface_type[3], 'I');
Gleb Natapovf77e1792010-12-23 11:29:35 +0200643 }
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400644 SET_FARVAR(seg, param_far->iface_type[4], ' ');
645 SET_FARVAR(seg, param_far->iface_type[5], ' ');
646 SET_FARVAR(seg, param_far->iface_type[6], ' ');
647 SET_FARVAR(seg, param_far->iface_type[7], ' ');
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500648
Gleb Natapovf5154e22011-01-10 10:50:27 +0200649 if (t13) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400650 SET_FARVAR(seg, param_far->t13.device_path[0], device_path);
651 SET_FARVAR(seg, param_far->t13.device_path[1], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500652
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400653 SET_FARVAR(seg, param_far->t13.checksum
654 , -checksum_far(seg, (void*)param_far+30, 43));
Gleb Natapovf5154e22011-01-10 10:50:27 +0200655 } else {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400656 SET_FARVAR(seg, param_far->phoenix.device_path, device_path);
Gleb Natapovf5154e22011-01-10 10:50:27 +0200657
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400658 SET_FARVAR(seg, param_far->phoenix.checksum
659 , -checksum_far(seg, (void*)param_far+30, 35));
Gleb Natapovf5154e22011-01-10 10:50:27 +0200660 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400661
662 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500663}
664
665// IBM/MS extended media change
666static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400667disk_1349(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500668{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400669 if (regs->dl < EXTSTART_CD) {
670 // Always success for HD
671 disk_ret(regs, DISK_RET_SUCCESS);
672 return;
673 }
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500674 set_invalid(regs);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400675 // always send changed ??
676 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500677}
678
679static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400680disk_134e01(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500681{
682 disk_ret(regs, DISK_RET_SUCCESS);
683}
684
685static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400686disk_134e03(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500687{
688 disk_ret(regs, DISK_RET_SUCCESS);
689}
690
691static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400692disk_134e04(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500693{
694 disk_ret(regs, DISK_RET_SUCCESS);
695}
696
697static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400698disk_134e06(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500699{
700 disk_ret(regs, DISK_RET_SUCCESS);
701}
702
703static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400704disk_134eXX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500705{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500706 disk_ret(regs, DISK_RET_EPARAM);
707}
708
709// IBM/MS set hardware configuration
710static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400711disk_134e(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500712{
713 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400714 case 0x01: disk_134e01(regs, drive_g); break;
715 case 0x03: disk_134e03(regs, drive_g); break;
716 case 0x04: disk_134e04(regs, drive_g); break;
717 case 0x06: disk_134e06(regs, drive_g); break;
718 default: disk_134eXX(regs, drive_g); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500719 }
720}
721
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400722static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400723disk_13XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500724{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500725 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500726}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500727
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400728static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400729disk_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500730{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500731 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500732
733 // clear completion flag
734 SET_BDA(disk_interrupt_flag, 0);
735
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500736 switch (regs->ah) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400737 case 0x00: disk_1300(regs, drive_g); break;
738 case 0x01: disk_1301(regs, drive_g); break;
739 case 0x02: disk_1302(regs, drive_g); break;
740 case 0x03: disk_1303(regs, drive_g); break;
741 case 0x04: disk_1304(regs, drive_g); break;
742 case 0x05: disk_1305(regs, drive_g); break;
743 case 0x08: disk_1308(regs, drive_g); break;
744 case 0x09: disk_1309(regs, drive_g); break;
745 case 0x0c: disk_130c(regs, drive_g); break;
746 case 0x0d: disk_130d(regs, drive_g); break;
747 case 0x10: disk_1310(regs, drive_g); break;
748 case 0x11: disk_1311(regs, drive_g); break;
749 case 0x14: disk_1314(regs, drive_g); break;
750 case 0x15: disk_1315(regs, drive_g); break;
751 case 0x16: disk_1316(regs, drive_g); break;
752 case 0x41: disk_1341(regs, drive_g); break;
753 case 0x42: disk_1342(regs, drive_g); break;
754 case 0x43: disk_1343(regs, drive_g); break;
755 case 0x44: disk_1344(regs, drive_g); break;
756 case 0x45: disk_1345(regs, drive_g); break;
757 case 0x46: disk_1346(regs, drive_g); break;
758 case 0x47: disk_1347(regs, drive_g); break;
759 case 0x48: disk_1348(regs, drive_g); break;
760 case 0x49: disk_1349(regs, drive_g); break;
761 case 0x4e: disk_134e(regs, drive_g); break;
762 default: disk_13XX(regs, drive_g); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500763 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500764}
765
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400766static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400767floppy_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400768{
769 // Only limited commands are supported on floppies.
770 switch (regs->ah) {
771 case 0x00:
772 case 0x01:
773 case 0x02:
774 case 0x03:
775 case 0x04:
776 case 0x05:
777 case 0x08:
778 case 0x15:
779 case 0x16:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400780 disk_13(regs, drive_g);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400781 break;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400782 default: disk_13XX(regs, drive_g); break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400783 }
784}
785
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500786
787/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500788 * Entry points
789 ****************************************************************/
790
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500791static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400792handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500793{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400794 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400795 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500796 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500797 return;
798 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500799
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400800 if (extdrive < EXTSTART_HD) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400801 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, extdrive);
802 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400803 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400804 floppy_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400805 return;
806 }
807
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400808 struct drive_s *drive_g;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400809 if (extdrive >= EXTSTART_CD)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400810 drive_g = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400811 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400812 drive_g = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
813 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400814 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400815 disk_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400816 return;
817
818fail:
819 // XXX - support 1301/1308/1315 anyway?
820 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500821}
822
Kevin O'Connor19786762008-03-05 21:09:59 -0500823void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500824handle_40(struct bregs *regs)
825{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400826 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500827 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500828}
829
830// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500831void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500832handle_13(struct bregs *regs)
833{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400834 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400835 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500836
Kevin O'Connordfa16502008-03-22 20:13:08 -0400837 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500838 if (regs->ah == 0x4b) {
839 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400840 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500841 }
Kevin O'Connord3140832012-05-13 22:46:12 -0400842 if (GET_LOW(CDEmu.active)) {
843 u8 emudrive = GET_LOW(CDEmu.emulated_extdrive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400844 if (extdrive == emudrive) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400845 // Access to an emulated drive.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500846 struct drive_s *cdemu_g;
847 cdemu_g = GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf));
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400848 if (regs->ah > 0x16) {
849 // Only old-style commands supported.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500850 disk_13XX(regs, cdemu_g);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400851 return;
852 }
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500853 disk_13(regs, cdemu_g);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400854 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500855 }
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400856 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400857 // Adjust id to make room for emulated drive.
Kevin O'Connor707298a2009-08-11 22:27:51 -0400858 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500859 }
860 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400861 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500862}
863
864// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500865void VISIBLE16
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400866handle_76(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500867{
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400868 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400869 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connoraa7c2342013-07-14 15:07:21 -0400870 pic_eoi2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500871}
Kevin O'Connor30853762009-01-17 18:49:20 -0500872
873// Old Fixed Disk Parameter Table (newer tables are in the ebda).
874struct fdpt_s OldFDPT VAR16FIXED(0xe401);