blob: 30b6e7be9bc1d236dca6df5ec84ebc5cfb32c0ab [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'Connor36c93a52009-09-12 19:35:04 -040046static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -040047fillLCHS(struct drive_s *drive_g, u16 *nlc, u16 *nlh, u16 *nlspt)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050048{
Kevin O'Connor77d227b2009-10-22 21:48:39 -040049 if (CONFIG_CDROM_EMU && drive_g == GET_GLOBAL(cdemu_drive)) {
Kevin O'Connor36c93a52009-09-12 19:35:04 -040050 // Emulated drive - get info from ebda. (It's not possible to
51 // populate the geometry directly in the driveid because the
52 // geometry is only known after the bios segment is made
53 // read-only).
54 u16 ebda_seg = get_ebda_seg();
55 *nlc = GET_EBDA2(ebda_seg, cdemu.lchs.cylinders);
56 *nlh = GET_EBDA2(ebda_seg, cdemu.lchs.heads);
57 *nlspt = GET_EBDA2(ebda_seg, cdemu.lchs.spt);
58 return;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040059 }
Kevin O'Connor77d227b2009-10-22 21:48:39 -040060 *nlc = GET_GLOBAL(drive_g->lchs.cylinders);
61 *nlh = GET_GLOBAL(drive_g->lchs.heads);
62 *nlspt = GET_GLOBAL(drive_g->lchs.spt);
Kevin O'Connor1625a752009-08-09 13:08:21 -040063}
64
65// Perform read/write/verify using old-style chs accesses
66static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -040067basic_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -040068{
69 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -040070 dop.drive_g = drive_g;
Kevin O'Connor1625a752009-08-09 13:08:21 -040071 dop.command = command;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040072
73 u8 count = regs->al;
74 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
75 u16 sector = regs->cl & 0x3f;
76 u16 head = regs->dh;
77
78 if (count > 128 || count == 0 || sector == 0) {
79 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
80 , regs->ah);
81 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040082 return;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040083 }
84 dop.count = count;
85
86 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -040087 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040088
89 // sanity check on cyl heads, sec
90 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
91 dprintf(1, "int13_harddisk: function %02x, parameters out of"
92 " range %04x/%04x/%04x!\n"
93 , regs->ah, cylinder, head, sector);
94 disk_ret(regs, DISK_RET_EPARAM);
95 return;
96 }
97
98 // translate lchs to lba
99 dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
100 + (u32)sector - 1);
101
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400102 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400103
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500104 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400105
Kevin O'Connor1625a752009-08-09 13:08:21 -0400106 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500107
Kevin O'Connor126eac62009-08-16 13:32:24 -0400108 disk_ret(regs, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500109}
110
Kevin O'Connor1625a752009-08-09 13:08:21 -0400111// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connored128492008-03-11 11:14:59 -0400112static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400113extended_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500114{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500115 struct disk_op_s dop;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400116 // Get lba and check.
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500117 dop.lba = GET_INT13EXT(regs, lba);
118 dop.command = command;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400119 dop.drive_g = drive_g;
120 if (dop.lba >= GET_GLOBAL(drive_g->sectors)) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400121 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
122 , regs->ah);
123 disk_ret(regs, DISK_RET_EPARAM);
124 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500125 }
126
Kevin O'Connor9f985422009-09-09 11:34:39 -0400127 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_INT13EXT(regs, data));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500128 dop.count = GET_INT13EXT(regs, count);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400129
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500130 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400131
Kevin O'Connor1625a752009-08-09 13:08:21 -0400132 SET_INT13EXT(regs, count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500133
Kevin O'Connor126eac62009-08-16 13:32:24 -0400134 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500135}
136
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500137
138/****************************************************************
139 * Hard Drive functions
140 ****************************************************************/
141
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500142// disk controller reset
143static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400144disk_1300(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500145{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400146 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400147 dop.drive_g = drive_g;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400148 dop.command = CMD_RESET;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400149 int status = send_disk_op(&dop);
150 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500151}
152
153// read disk status
154static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400155disk_1301(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500156{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400157 u8 v;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400158 if (regs->dl < EXTSTART_HD)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400159 // Floppy
160 v = GET_BDA(floppy_last_status);
161 else
162 v = GET_BDA(disk_last_status);
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400163 regs->ah = v;
164 set_cf(regs, v);
165 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500166}
167
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500168// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500169static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400170disk_1302(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500171{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400172 basic_access(regs, drive_g, CMD_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500173}
174
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500175// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500176static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400177disk_1303(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500178{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400179 basic_access(regs, drive_g, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500180}
181
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500182// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500183static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400184disk_1304(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500185{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400186 basic_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500187}
188
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500189// format disk track
190static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400191disk_1305(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500192{
Kevin O'Connor4d9d4002009-11-25 19:35:01 -0500193 debug_stub(regs);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400194
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400195 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400196 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400197
198 u8 num_sectors = regs->al;
199 u8 head = regs->dh;
200
201 if (head >= nlh || num_sectors == 0 || num_sectors > nlspt) {
202 disk_ret(regs, DISK_RET_EPARAM);
203 return;
204 }
205
206 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400207 dop.drive_g = drive_g;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400208 dop.command = CMD_FORMAT;
209 dop.lba = head;
210 dop.count = num_sectors;
211 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
212 int status = send_disk_op(&dop);
213 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500214}
215
216// read disk drive parameters
217static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400218disk_1308(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500219{
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400220 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500221 // Get logical geometry from table
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400222 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400223 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400224 nlc--;
225 nlh--;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400226 u8 count;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400227 if (regs->dl < EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400228 // Floppy
229 count = GET_GLOBAL(Drives.floppycount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500230
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400231 if (CONFIG_CDROM_EMU && drive_g == GET_GLOBAL(cdemu_drive))
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400232 regs->bx = GET_EBDA2(ebda_seg, cdemu.media) * 2;
233 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400234 regs->bx = GET_GLOBAL(drive_g->floppy_type);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400235
236 // set es & di to point to 11 byte diskette param table in ROM
237 regs->es = SEG_BIOS;
238 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400239 } else if (regs->dl < EXTSTART_CD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400240 // Hard drive
241 count = GET_BDA(hdcount);
242 nlc--; // last sector reserved
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400243 } else {
244 // Not supported on CDROM
245 disk_ret(regs, DISK_RET_EPARAM);
246 return;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400247 }
248
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400249 if (CONFIG_CDROM_EMU && GET_EBDA2(ebda_seg, cdemu.active)) {
250 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
251 if (((emudrive ^ regs->dl) & 0x80) == 0)
252 // Note extra drive due to emulation.
253 count++;
254 if (regs->dl < EXTSTART_HD && count > 2)
255 // Max of two floppy drives.
256 count = 2;
257 }
258
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500259 regs->al = 0;
260 regs->ch = nlc & 0xff;
261 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400262 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500263
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500264 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400265 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500266}
267
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500268// initialize drive parameters
269static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400270disk_1309(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500271{
272 DISK_STUB(regs);
273}
274
275// seek to specified cylinder
276static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400277disk_130c(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500278{
279 DISK_STUB(regs);
280}
281
282// alternate disk reset
283static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400284disk_130d(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500285{
286 DISK_STUB(regs);
287}
288
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500289// check drive ready
290static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400291disk_1310(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500292{
293 // should look at 40:8E also???
294
Kevin O'Connor42337662009-08-10 00:06:37 -0400295 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400296 dop.drive_g = drive_g;
Kevin O'Connor42337662009-08-10 00:06:37 -0400297 dop.command = CMD_ISREADY;
298 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400299 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500300}
301
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500302// recalibrate
303static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400304disk_1311(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500305{
306 DISK_STUB(regs);
307}
308
309// controller internal diagnostic
310static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400311disk_1314(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500312{
313 DISK_STUB(regs);
314}
315
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500316// read disk drive size
317static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400318disk_1315(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500319{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400320 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400321 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
322 // Floppy or cdrom
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400323 regs->ah = 1;
324 return;
325 }
326 // Hard drive
327
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500328 // Get logical geometry from table
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400329 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400330 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500331
332 // Compute sector count seen by int13
333 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
334 regs->cx = lba >> 16;
335 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500336 regs->ah = 3; // hard disk accessible
337}
338
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400339static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400340disk_1316(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400341{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400342 if (regs->dl >= EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400343 // Hard drive
344 disk_ret(regs, DISK_RET_EPARAM);
345 return;
346 }
347 disk_ret(regs, DISK_RET_ECHANGED);
348}
349
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500350// IBM/MS installation check
351static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400352disk_1341(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500353{
354 regs->bx = 0xaa55; // install check
355 regs->cx = 0x0007; // ext disk access and edd, removable supported
356 disk_ret(regs, DISK_RET_SUCCESS);
357 regs->ah = 0x30; // EDD 3.0
358}
359
360// IBM/MS extended read
361static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400362disk_1342(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500363{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400364 extended_access(regs, drive_g, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500365}
366
367// IBM/MS extended write
368static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400369disk_1343(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500370{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400371 extended_access(regs, drive_g, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500372}
373
374// IBM/MS verify
375static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400376disk_1344(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500377{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400378 extended_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500379}
380
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400381// lock
382static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400383disk_134500(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400384{
385 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400386 int cdid = regs->dl - EXTSTART_CD;
387 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400388 if (locks == 0xff) {
389 regs->al = 1;
390 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
391 return;
392 }
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400393 SET_EBDA2(ebda_seg, cdrom_locks[cdid], locks + 1);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400394 regs->al = 1;
395 disk_ret(regs, DISK_RET_SUCCESS);
396}
397
398// unlock
399static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400400disk_134501(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400401{
402 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400403 int cdid = regs->dl - EXTSTART_CD;
404 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400405 if (locks == 0x00) {
406 regs->al = 0;
407 disk_ret(regs, DISK_RET_ENOTLOCKED);
408 return;
409 }
410 locks--;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400411 SET_EBDA2(ebda_seg, cdrom_locks[cdid], locks);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400412 regs->al = (locks ? 1 : 0);
413 disk_ret(regs, DISK_RET_SUCCESS);
414}
415
416// status
417static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400418disk_134502(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400419{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400420 int cdid = regs->dl - EXTSTART_CD;
421 u8 locks = GET_EBDA(cdrom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400422 regs->al = (locks ? 1 : 0);
423 disk_ret(regs, DISK_RET_SUCCESS);
424}
425
426static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400427disk_1345XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400428{
429 disk_ret(regs, DISK_RET_EPARAM);
430}
431
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500432// IBM/MS lock/unlock drive
433static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400434disk_1345(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500435{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400436 if (regs->dl < EXTSTART_CD) {
437 // Always success for HD
438 disk_ret(regs, DISK_RET_SUCCESS);
439 return;
440 }
441
442 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400443 case 0x00: disk_134500(regs, drive_g); break;
444 case 0x01: disk_134501(regs, drive_g); break;
445 case 0x02: disk_134502(regs, drive_g); break;
446 default: disk_1345XX(regs, drive_g); break;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400447 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500448}
449
450// IBM/MS eject media
451static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400452disk_1346(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500453{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400454 if (regs->dl < EXTSTART_CD) {
455 // Volume Not Removable
456 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
457 return;
458 }
459
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400460 int cdid = regs->dl - EXTSTART_CD;
461 u8 locks = GET_EBDA(cdrom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400462 if (locks != 0) {
463 disk_ret(regs, DISK_RET_ELOCKED);
464 return;
465 }
466
467 // FIXME should handle 0x31 no media in device
468 // FIXME should handle 0xb5 valid request failed
469
470 // Call removable media eject
471 struct bregs br;
472 memset(&br, 0, sizeof(br));
473 br.ah = 0x52;
474 call16_int(0x15, &br);
475
476 if (br.ah || br.flags & F_CF) {
477 disk_ret(regs, DISK_RET_ELOCKED);
478 return;
479 }
480 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500481}
482
483// IBM/MS extended seek
484static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400485disk_1347(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500486{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400487 extended_access(regs, drive_g, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500488}
489
490// IBM/MS get drive parameters
491static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400492disk_1348(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500493{
494 u16 size = GET_INT13DPT(regs, size);
495
496 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400497 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500498 disk_ret(regs, DISK_RET_EPARAM);
499 return;
500 }
501
502 // EDD 1.x
503
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400504 u8 type = GET_GLOBAL(drive_g->type);
505 u16 npc = GET_GLOBAL(drive_g->pchs.cylinders);
506 u16 nph = GET_GLOBAL(drive_g->pchs.heads);
507 u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
508 u64 lba = GET_GLOBAL(drive_g->sectors);
509 u16 blksize = GET_GLOBAL(drive_g->blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500510
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400511 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
512 , size, type, npc, nph, npspt, (u32)lba, blksize);
513
514 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400515 if (type == DTYPE_ATAPI) {
516 // 0x74 = removable, media change, lockable, max values
517 SET_INT13DPT(regs, infos, 0x74);
518 SET_INT13DPT(regs, cylinders, 0xffffffff);
519 SET_INT13DPT(regs, heads, 0xffffffff);
520 SET_INT13DPT(regs, spt, 0xffffffff);
521 SET_INT13DPT(regs, sector_count, (u64)-1);
522 } else {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400523 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500524 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
525 SET_INT13DPT(regs, cylinders, 0x3fff);
526 } else {
527 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
528 SET_INT13DPT(regs, cylinders, (u32)npc);
529 }
530 SET_INT13DPT(regs, heads, (u32)nph);
531 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400532 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500533 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500534 SET_INT13DPT(regs, blksize, blksize);
535
Kevin O'Connorb1144362009-08-11 20:43:38 -0400536 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500537 disk_ret(regs, DISK_RET_SUCCESS);
538 return;
539 }
540
541 // EDD 2.x
542
Kevin O'Connor08815372008-12-29 21:16:31 -0500543 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400544 SET_INT13DPT(regs, size, 30);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500545
Kevin O'Connor08815372008-12-29 21:16:31 -0500546 SET_INT13DPT(regs, dpte_segment, ebda_seg);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500547 SET_INT13DPT(regs, dpte_offset
Kevin O'Connor609da232008-12-28 23:18:57 -0500548 , offsetof(struct extended_bios_data_area_s, dpte));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500549
550 // Fill in dpte
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400551 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400552 u8 channel = ataid / 2;
553 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400554 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
555 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
556 u8 irq = GET_GLOBAL(ATA_channels[channel].irq);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500557
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400558 u16 options = 0;
Kevin O'Connor42337662009-08-10 00:06:37 -0400559 if (type == DTYPE_ATA) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400560 u8 translation = GET_GLOBAL(drive_g->translation);
Kevin O'Connor42337662009-08-10 00:06:37 -0400561 if (translation != TRANSLATION_NONE) {
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400562 options |= 1<<3; // CHS translation
Kevin O'Connor42337662009-08-10 00:06:37 -0400563 if (translation == TRANSLATION_LBA)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400564 options |= 1<<9;
Kevin O'Connor42337662009-08-10 00:06:37 -0400565 if (translation == TRANSLATION_RECHS)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400566 options |= 3<<9;
567 }
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500568 } else {
569 // ATAPI
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400570 options |= 1<<5; // removable device
571 options |= 1<<6; // atapi device
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500572 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400573 options |= 1<<4; // lba translation
Kevin O'Connor32945af2009-02-27 21:23:01 -0500574 if (CONFIG_ATA_PIO32)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400575 options |= 1<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500576
Kevin O'Connor08815372008-12-29 21:16:31 -0500577 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
578 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
579 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
580 | ATA_CB_DH_LBA));
581 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
582 SET_EBDA2(ebda_seg, dpte.irq, irq);
583 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
584 SET_EBDA2(ebda_seg, dpte.dma, 0);
585 SET_EBDA2(ebda_seg, dpte.pio, 0);
586 SET_EBDA2(ebda_seg, dpte.options, options);
587 SET_EBDA2(ebda_seg, dpte.reserved, 0);
588 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500589
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500590 u8 sum = checksum_far(
591 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
592 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500593
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400594 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500595 disk_ret(regs, DISK_RET_SUCCESS);
596 return;
597 }
598
599 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500600 SET_INT13DPT(regs, key, 0xbedd);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400601 SET_INT13DPT(regs, dpi_length, 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500602 SET_INT13DPT(regs, reserved1, 0);
603 SET_INT13DPT(regs, reserved2, 0);
604
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400605 SET_INT13DPT(regs, host_bus[0], 'P');
606 SET_INT13DPT(regs, host_bus[1], 'C');
607 SET_INT13DPT(regs, host_bus[2], 'I');
608 SET_INT13DPT(regs, host_bus[3], 0);
609
Kevin O'Connorc892b132009-08-11 21:59:37 -0400610 u32 bdf = GET_GLOBAL(ATA_channels[channel].pci_bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400611 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
612 | (pci_bdf_to_fn(bdf) << 16));
613 SET_INT13DPT(regs, iface_path, path);
614
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500615 SET_INT13DPT(regs, iface_type[0], 'A');
616 SET_INT13DPT(regs, iface_type[1], 'T');
617 SET_INT13DPT(regs, iface_type[2], 'A');
618 SET_INT13DPT(regs, iface_type[3], 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400619 SET_INT13DPT(regs, iface_type[4], 0);
620 SET_INT13DPT(regs, iface_type[5], 0);
621 SET_INT13DPT(regs, iface_type[6], 0);
622 SET_INT13DPT(regs, iface_type[7], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500623
Kevin O'Connor970a0322008-10-26 12:01:21 -0400624 SET_INT13DPT(regs, device_path, slave);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500625
Kevin O'Connor7d108212009-01-21 19:13:21 -0500626 SET_INT13DPT(regs, checksum
627 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400628
629 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500630}
631
632// IBM/MS extended media change
633static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400634disk_1349(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500635{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400636 if (regs->dl < EXTSTART_CD) {
637 // Always success for HD
638 disk_ret(regs, DISK_RET_SUCCESS);
639 return;
640 }
641 set_fail(regs);
642 // always send changed ??
643 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500644}
645
646static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400647disk_134e01(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500648{
649 disk_ret(regs, DISK_RET_SUCCESS);
650}
651
652static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400653disk_134e03(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500654{
655 disk_ret(regs, DISK_RET_SUCCESS);
656}
657
658static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400659disk_134e04(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500660{
661 disk_ret(regs, DISK_RET_SUCCESS);
662}
663
664static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400665disk_134e06(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500666{
667 disk_ret(regs, DISK_RET_SUCCESS);
668}
669
670static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400671disk_134eXX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500672{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500673 disk_ret(regs, DISK_RET_EPARAM);
674}
675
676// IBM/MS set hardware configuration
677static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400678disk_134e(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500679{
680 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400681 case 0x01: disk_134e01(regs, drive_g); break;
682 case 0x03: disk_134e03(regs, drive_g); break;
683 case 0x04: disk_134e04(regs, drive_g); break;
684 case 0x06: disk_134e06(regs, drive_g); break;
685 default: disk_134eXX(regs, drive_g); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500686 }
687}
688
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400689static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400690disk_13XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500691{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500692 disk_ret(regs, DISK_RET_EPARAM);
693}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500694
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400695static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400696disk_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500697{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500698 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500699
700 // clear completion flag
701 SET_BDA(disk_interrupt_flag, 0);
702
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500703 switch (regs->ah) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400704 case 0x00: disk_1300(regs, drive_g); break;
705 case 0x01: disk_1301(regs, drive_g); break;
706 case 0x02: disk_1302(regs, drive_g); break;
707 case 0x03: disk_1303(regs, drive_g); break;
708 case 0x04: disk_1304(regs, drive_g); break;
709 case 0x05: disk_1305(regs, drive_g); break;
710 case 0x08: disk_1308(regs, drive_g); break;
711 case 0x09: disk_1309(regs, drive_g); break;
712 case 0x0c: disk_130c(regs, drive_g); break;
713 case 0x0d: disk_130d(regs, drive_g); break;
714 case 0x10: disk_1310(regs, drive_g); break;
715 case 0x11: disk_1311(regs, drive_g); break;
716 case 0x14: disk_1314(regs, drive_g); break;
717 case 0x15: disk_1315(regs, drive_g); break;
718 case 0x16: disk_1316(regs, drive_g); break;
719 case 0x41: disk_1341(regs, drive_g); break;
720 case 0x42: disk_1342(regs, drive_g); break;
721 case 0x43: disk_1343(regs, drive_g); break;
722 case 0x44: disk_1344(regs, drive_g); break;
723 case 0x45: disk_1345(regs, drive_g); break;
724 case 0x46: disk_1346(regs, drive_g); break;
725 case 0x47: disk_1347(regs, drive_g); break;
726 case 0x48: disk_1348(regs, drive_g); break;
727 case 0x49: disk_1349(regs, drive_g); break;
728 case 0x4e: disk_134e(regs, drive_g); break;
729 default: disk_13XX(regs, drive_g); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500730 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500731}
732
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400733static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400734floppy_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400735{
736 // Only limited commands are supported on floppies.
737 switch (regs->ah) {
738 case 0x00:
739 case 0x01:
740 case 0x02:
741 case 0x03:
742 case 0x04:
743 case 0x05:
744 case 0x08:
745 case 0x15:
746 case 0x16:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400747 disk_13(regs, drive_g);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400748 break;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400749 default: disk_13XX(regs, drive_g); break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400750 }
751}
752
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500753
754/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500755 * Entry points
756 ****************************************************************/
757
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500758static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400759handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500760{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400761 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400762 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500763 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500764 return;
765 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500766
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400767 if (extdrive < EXTSTART_HD) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400768 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, extdrive);
769 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400770 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400771 floppy_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400772 return;
773 }
774
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400775 struct drive_s *drive_g;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400776 if (extdrive >= EXTSTART_CD)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400777 drive_g = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400778 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400779 drive_g = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
780 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400781 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400782 disk_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400783 return;
784
785fail:
786 // XXX - support 1301/1308/1315 anyway?
787 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500788}
789
Kevin O'Connor19786762008-03-05 21:09:59 -0500790void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500791handle_40(struct bregs *regs)
792{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400793 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500794 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500795}
796
797// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500798void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500799handle_13(struct bregs *regs)
800{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400801 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400802 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500803
Kevin O'Connordfa16502008-03-22 20:13:08 -0400804 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500805 if (regs->ah == 0x4b) {
806 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400807 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500808 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500809 u16 ebda_seg = get_ebda_seg();
810 if (GET_EBDA2(ebda_seg, cdemu.active)) {
Kevin O'Connor669e6442009-08-11 22:36:30 -0400811 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400812 if (extdrive == emudrive) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400813 // Access to an emulated drive.
814 struct drive_s *cdemu = GET_GLOBAL(cdemu_drive);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400815 if (regs->ah > 0x16) {
816 // Only old-style commands supported.
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400817 disk_13XX(regs, cdemu);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400818 return;
819 }
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400820 disk_13(regs, cdemu);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400821 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500822 }
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400823 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400824 // Adjust id to make room for emulated drive.
Kevin O'Connor707298a2009-08-11 22:27:51 -0400825 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500826 }
827 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400828 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500829}
830
831// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500832void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400833handle_76()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500834{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400835 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400836 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400837 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500838}
Kevin O'Connor30853762009-01-17 18:49:20 -0500839
840// Old Fixed Disk Parameter Table (newer tables are in the ebda).
841struct fdpt_s OldFDPT VAR16FIXED(0xe401);