blob: 29714d64df0ab47fe4ba62c14c63ce33691b00f4 [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'Connordfefeb52009-12-13 13:04:17 -050031 __set_code_invalid(regs, linecode, fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040032 else
33 set_code_success(regs);
34}
35
Kevin O'Connordfefeb52009-12-13 13:04:17 -050036void
37__disk_ret_unimplemented(struct bregs *regs, u32 linecode, const char *fname)
38{
39 u8 code = linecode;
40 if (regs->dl < EXTSTART_HD)
41 SET_BDA(floppy_last_status, code);
42 else
43 SET_BDA(disk_last_status, code);
44 __set_code_unimplemented(regs, linecode, fname);
45}
46
Kevin O'Connor567e4e32008-04-05 11:37:51 -040047static void
Kevin O'Connor05600342009-01-02 13:10:58 -050048__disk_stub(struct bregs *regs, int lineno, const char *fname)
Kevin O'Connor567e4e32008-04-05 11:37:51 -040049{
Kevin O'Connordfefeb52009-12-13 13:04:17 -050050 __warn_unimplemented(regs, lineno, fname);
Kevin O'Connor05600342009-01-02 13:10:58 -050051 __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040052}
53
Kevin O'Connor05600342009-01-02 13:10:58 -050054#define DISK_STUB(regs) \
55 __disk_stub((regs), __LINE__, __func__)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050056
Kevin O'Connor5c99b6c2009-12-30 12:36:22 -050057// Get the cylinders/heads/sectors for the given drive.
Kevin O'Connor36c93a52009-09-12 19:35:04 -040058static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -040059fillLCHS(struct drive_s *drive_g, u16 *nlc, u16 *nlh, u16 *nlspt)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050060{
Kevin O'Connor8f469b92010-02-28 01:28:11 -050061 if (CONFIG_CDROM_EMU
62 && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf))) {
Kevin O'Connor36c93a52009-09-12 19:35:04 -040063 // Emulated drive - get info from ebda. (It's not possible to
64 // populate the geometry directly in the driveid because the
65 // geometry is only known after the bios segment is made
66 // read-only).
67 u16 ebda_seg = get_ebda_seg();
68 *nlc = GET_EBDA2(ebda_seg, cdemu.lchs.cylinders);
69 *nlh = GET_EBDA2(ebda_seg, cdemu.lchs.heads);
70 *nlspt = GET_EBDA2(ebda_seg, cdemu.lchs.spt);
71 return;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040072 }
Kevin O'Connor77d227b2009-10-22 21:48:39 -040073 *nlc = GET_GLOBAL(drive_g->lchs.cylinders);
74 *nlh = GET_GLOBAL(drive_g->lchs.heads);
75 *nlspt = GET_GLOBAL(drive_g->lchs.spt);
Kevin O'Connor1625a752009-08-09 13:08:21 -040076}
77
78// Perform read/write/verify using old-style chs accesses
79static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -040080basic_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -040081{
82 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -040083 dop.drive_g = drive_g;
Kevin O'Connor1625a752009-08-09 13:08:21 -040084 dop.command = command;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040085
86 u8 count = regs->al;
87 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
88 u16 sector = regs->cl & 0x3f;
89 u16 head = regs->dh;
90
91 if (count > 128 || count == 0 || sector == 0) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050092 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040093 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040094 return;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040095 }
96 dop.count = count;
97
98 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -040099 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400100
101 // sanity check on cyl heads, sec
102 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500103 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400104 disk_ret(regs, DISK_RET_EPARAM);
105 return;
106 }
107
108 // translate lchs to lba
109 dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
110 + (u32)sector - 1);
111
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400112 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400113
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500114 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400115
Kevin O'Connor1625a752009-08-09 13:08:21 -0400116 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500117
Kevin O'Connor126eac62009-08-16 13:32:24 -0400118 disk_ret(regs, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500119}
120
Kevin O'Connor1625a752009-08-09 13:08:21 -0400121// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connored128492008-03-11 11:14:59 -0400122static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400123extended_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500124{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500125 struct disk_op_s dop;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400126 // Get lba and check.
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500127 dop.lba = GET_INT13EXT(regs, lba);
128 dop.command = command;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400129 dop.drive_g = drive_g;
130 if (dop.lba >= GET_GLOBAL(drive_g->sectors)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500131 warn_invalid(regs);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400132 disk_ret(regs, DISK_RET_EPARAM);
133 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500134 }
135
Kevin O'Connor9f985422009-09-09 11:34:39 -0400136 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_INT13EXT(regs, data));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500137 dop.count = GET_INT13EXT(regs, count);
Kevin O'Connorb50488b2012-02-11 09:38:44 -0500138 if (! dop.count) {
139 // Nothing to do.
140 disk_ret(regs, DISK_RET_SUCCESS);
141 return;
142 }
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400143
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500144 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400145
Kevin O'Connor1625a752009-08-09 13:08:21 -0400146 SET_INT13EXT(regs, count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500147
Kevin O'Connor126eac62009-08-16 13:32:24 -0400148 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500149}
150
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500151
152/****************************************************************
153 * Hard Drive functions
154 ****************************************************************/
155
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500156// disk controller reset
157static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400158disk_1300(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500159{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400160 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400161 dop.drive_g = drive_g;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400162 dop.command = CMD_RESET;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400163 int status = send_disk_op(&dop);
164 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500165}
166
167// read disk status
168static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400169disk_1301(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500170{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400171 u8 v;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400172 if (regs->dl < EXTSTART_HD)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400173 // Floppy
174 v = GET_BDA(floppy_last_status);
175 else
176 v = GET_BDA(disk_last_status);
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400177 regs->ah = v;
178 set_cf(regs, v);
179 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500180}
181
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500182// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500183static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400184disk_1302(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_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500187}
188
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500189// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500190static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400191disk_1303(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500192{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400193 basic_access(regs, drive_g, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500194}
195
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500196// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500197static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400198disk_1304(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500199{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400200 basic_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500201}
202
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500203// format disk track
204static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400205disk_1305(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500206{
Kevin O'Connor4d9d4002009-11-25 19:35:01 -0500207 debug_stub(regs);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400208
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400209 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400210 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400211
212 u8 num_sectors = regs->al;
213 u8 head = regs->dh;
214
215 if (head >= nlh || num_sectors == 0 || num_sectors > nlspt) {
216 disk_ret(regs, DISK_RET_EPARAM);
217 return;
218 }
219
220 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400221 dop.drive_g = drive_g;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400222 dop.command = CMD_FORMAT;
223 dop.lba = head;
224 dop.count = num_sectors;
225 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
226 int status = send_disk_op(&dop);
227 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500228}
229
230// read disk drive parameters
231static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400232disk_1308(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500233{
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400234 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500235 // Get logical geometry from table
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400236 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400237 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400238 nlc--;
239 nlh--;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400240 u8 count;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400241 if (regs->dl < EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400242 // Floppy
Kevin O'Connora0842f82010-12-29 11:05:46 -0500243 count = GET_GLOBAL(FloppyCount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500244
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500245 if (CONFIG_CDROM_EMU
246 && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf)))
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400247 regs->bx = GET_EBDA2(ebda_seg, cdemu.media) * 2;
248 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400249 regs->bx = GET_GLOBAL(drive_g->floppy_type);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400250
251 // set es & di to point to 11 byte diskette param table in ROM
252 regs->es = SEG_BIOS;
253 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400254 } else if (regs->dl < EXTSTART_CD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400255 // Hard drive
256 count = GET_BDA(hdcount);
257 nlc--; // last sector reserved
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400258 } else {
259 // Not supported on CDROM
260 disk_ret(regs, DISK_RET_EPARAM);
261 return;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400262 }
263
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400264 if (CONFIG_CDROM_EMU && GET_EBDA2(ebda_seg, cdemu.active)) {
265 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
266 if (((emudrive ^ regs->dl) & 0x80) == 0)
267 // Note extra drive due to emulation.
268 count++;
269 if (regs->dl < EXTSTART_HD && count > 2)
270 // Max of two floppy drives.
271 count = 2;
272 }
273
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500274 regs->al = 0;
275 regs->ch = nlc & 0xff;
276 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400277 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500278
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500279 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400280 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500281}
282
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500283// initialize drive parameters
284static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400285disk_1309(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500286{
287 DISK_STUB(regs);
288}
289
290// seek to specified cylinder
291static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400292disk_130c(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500293{
294 DISK_STUB(regs);
295}
296
297// alternate disk reset
298static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400299disk_130d(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500300{
301 DISK_STUB(regs);
302}
303
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500304// check drive ready
305static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400306disk_1310(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500307{
308 // should look at 40:8E also???
309
Kevin O'Connor42337662009-08-10 00:06:37 -0400310 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400311 dop.drive_g = drive_g;
Kevin O'Connor42337662009-08-10 00:06:37 -0400312 dop.command = CMD_ISREADY;
313 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400314 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500315}
316
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500317// recalibrate
318static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400319disk_1311(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500320{
321 DISK_STUB(regs);
322}
323
324// controller internal diagnostic
325static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400326disk_1314(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500327{
328 DISK_STUB(regs);
329}
330
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500331// read disk drive size
332static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400333disk_1315(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500334{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400335 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400336 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
337 // Floppy or cdrom
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400338 regs->ah = 1;
339 return;
340 }
341 // Hard drive
342
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500343 // Get logical geometry from table
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400344 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400345 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500346
347 // Compute sector count seen by int13
348 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
349 regs->cx = lba >> 16;
350 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500351 regs->ah = 3; // hard disk accessible
352}
353
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400354static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400355disk_1316(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400356{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400357 if (regs->dl >= EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400358 // Hard drive
359 disk_ret(regs, DISK_RET_EPARAM);
360 return;
361 }
362 disk_ret(regs, DISK_RET_ECHANGED);
363}
364
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500365// IBM/MS installation check
366static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400367disk_1341(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500368{
369 regs->bx = 0xaa55; // install check
370 regs->cx = 0x0007; // ext disk access and edd, removable supported
371 disk_ret(regs, DISK_RET_SUCCESS);
372 regs->ah = 0x30; // EDD 3.0
373}
374
375// IBM/MS extended read
376static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400377disk_1342(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500378{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400379 extended_access(regs, drive_g, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500380}
381
382// IBM/MS extended write
383static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400384disk_1343(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500385{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400386 extended_access(regs, drive_g, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500387}
388
389// IBM/MS verify
390static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400391disk_1344(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500392{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400393 extended_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500394}
395
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400396// lock
397static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400398disk_134500(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400399{
400 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400401 int cdid = regs->dl - EXTSTART_CD;
402 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400403 if (locks == 0xff) {
404 regs->al = 1;
405 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
406 return;
407 }
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400408 SET_EBDA2(ebda_seg, cdrom_locks[cdid], locks + 1);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400409 regs->al = 1;
410 disk_ret(regs, DISK_RET_SUCCESS);
411}
412
413// unlock
414static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400415disk_134501(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400416{
417 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400418 int cdid = regs->dl - EXTSTART_CD;
419 u8 locks = GET_EBDA2(ebda_seg, cdrom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400420 if (locks == 0x00) {
421 regs->al = 0;
422 disk_ret(regs, DISK_RET_ENOTLOCKED);
423 return;
424 }
425 locks--;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400426 SET_EBDA2(ebda_seg, cdrom_locks[cdid], locks);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400427 regs->al = (locks ? 1 : 0);
428 disk_ret(regs, DISK_RET_SUCCESS);
429}
430
431// status
432static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400433disk_134502(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400434{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400435 int cdid = regs->dl - EXTSTART_CD;
436 u8 locks = GET_EBDA(cdrom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400437 regs->al = (locks ? 1 : 0);
438 disk_ret(regs, DISK_RET_SUCCESS);
439}
440
441static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400442disk_1345XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400443{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500444 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400445}
446
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500447// IBM/MS lock/unlock drive
448static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400449disk_1345(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500450{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400451 if (regs->dl < EXTSTART_CD) {
452 // Always success for HD
453 disk_ret(regs, DISK_RET_SUCCESS);
454 return;
455 }
456
457 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400458 case 0x00: disk_134500(regs, drive_g); break;
459 case 0x01: disk_134501(regs, drive_g); break;
460 case 0x02: disk_134502(regs, drive_g); break;
461 default: disk_1345XX(regs, drive_g); break;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400462 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500463}
464
465// IBM/MS eject media
466static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400467disk_1346(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500468{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400469 if (regs->dl < EXTSTART_CD) {
470 // Volume Not Removable
471 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
472 return;
473 }
474
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400475 int cdid = regs->dl - EXTSTART_CD;
476 u8 locks = GET_EBDA(cdrom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400477 if (locks != 0) {
478 disk_ret(regs, DISK_RET_ELOCKED);
479 return;
480 }
481
482 // FIXME should handle 0x31 no media in device
483 // FIXME should handle 0xb5 valid request failed
484
485 // Call removable media eject
486 struct bregs br;
487 memset(&br, 0, sizeof(br));
488 br.ah = 0x52;
Kevin O'Connor74152702010-03-20 17:53:40 -0400489 br.dl = regs->dl;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400490 call16_int(0x15, &br);
491
492 if (br.ah || br.flags & F_CF) {
493 disk_ret(regs, DISK_RET_ELOCKED);
494 return;
495 }
496 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500497}
498
499// IBM/MS extended seek
500static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400501disk_1347(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500502{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400503 extended_access(regs, drive_g, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500504}
505
506// IBM/MS get drive parameters
507static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400508disk_1348(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500509{
510 u16 size = GET_INT13DPT(regs, size);
Gleb Natapovf5154e22011-01-10 10:50:27 +0200511 u16 t13 = size == 74;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500512
513 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400514 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500515 disk_ret(regs, DISK_RET_EPARAM);
516 return;
517 }
518
519 // EDD 1.x
520
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400521 u8 type = GET_GLOBAL(drive_g->type);
522 u16 npc = GET_GLOBAL(drive_g->pchs.cylinders);
523 u16 nph = GET_GLOBAL(drive_g->pchs.heads);
524 u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
525 u64 lba = GET_GLOBAL(drive_g->sectors);
526 u16 blksize = GET_GLOBAL(drive_g->blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500527
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400528 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
529 , size, type, npc, nph, npspt, (u32)lba, blksize);
530
531 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400532 if (type == DTYPE_ATAPI) {
533 // 0x74 = removable, media change, lockable, max values
534 SET_INT13DPT(regs, infos, 0x74);
535 SET_INT13DPT(regs, cylinders, 0xffffffff);
536 SET_INT13DPT(regs, heads, 0xffffffff);
537 SET_INT13DPT(regs, spt, 0xffffffff);
538 SET_INT13DPT(regs, sector_count, (u64)-1);
539 } else {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400540 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500541 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
542 SET_INT13DPT(regs, cylinders, 0x3fff);
543 } else {
544 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
545 SET_INT13DPT(regs, cylinders, (u32)npc);
546 }
547 SET_INT13DPT(regs, heads, (u32)nph);
548 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400549 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500550 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500551 SET_INT13DPT(regs, blksize, blksize);
552
Gleb Natapovf77e1792010-12-23 11:29:35 +0200553 if (size < 30 ||
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100554 (type != DTYPE_ATA && type != DTYPE_ATAPI && type != DTYPE_VIRTIO_BLK)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500555 disk_ret(regs, DISK_RET_SUCCESS);
556 return;
557 }
558
559 // EDD 2.x
560
Gleb Natapovf77e1792010-12-23 11:29:35 +0200561 int bdf;
Gleb Natapovf5154e22011-01-10 10:50:27 +0200562 u16 iobase1 = 0;
563 u64 device_path = 0;
564 u8 channel = 0;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400565 SET_INT13DPT(regs, size, 30);
Gleb Natapovf77e1792010-12-23 11:29:35 +0200566 if (type == DTYPE_ATA || type == DTYPE_ATAPI) {
567 u16 ebda_seg = get_ebda_seg();
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500568
Gleb Natapovf77e1792010-12-23 11:29:35 +0200569 SET_INT13DPT(regs, dpte_segment, ebda_seg);
570 SET_INT13DPT(regs, dpte_offset
571 , offsetof(struct extended_bios_data_area_s, dpte));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500572
Gleb Natapovf77e1792010-12-23 11:29:35 +0200573 // Fill in dpte
574 struct atadrive_s *adrive_g = container_of(
575 drive_g, struct atadrive_s, drive);
576 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
577 u8 slave = GET_GLOBAL(adrive_g->slave);
578 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
579 u8 irq = GET_GLOBALFLAT(chan_gf->irq);
580 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
581 bdf = GET_GLOBALFLAT(chan_gf->pci_bdf);
582 device_path = slave;
Gleb Natapovf5154e22011-01-10 10:50:27 +0200583 channel = GET_GLOBALFLAT(chan_gf->chanid);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500584
Gleb Natapovf77e1792010-12-23 11:29:35 +0200585 u16 options = 0;
586 if (type == DTYPE_ATA) {
587 u8 translation = GET_GLOBAL(drive_g->translation);
588 if (translation != TRANSLATION_NONE) {
589 options |= 1<<3; // CHS translation
590 if (translation == TRANSLATION_LBA)
591 options |= 1<<9;
592 if (translation == TRANSLATION_RECHS)
593 options |= 3<<9;
594 }
595 } else {
596 // ATAPI
597 options |= 1<<5; // removable device
598 options |= 1<<6; // atapi device
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400599 }
Gleb Natapovf77e1792010-12-23 11:29:35 +0200600 options |= 1<<4; // lba translation
601 if (CONFIG_ATA_PIO32)
602 options |= 1<<7;
603
604 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
605 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
606 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
607 | ATA_CB_DH_LBA));
608 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
609 SET_EBDA2(ebda_seg, dpte.irq, irq);
610 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
611 SET_EBDA2(ebda_seg, dpte.dma, 0);
612 SET_EBDA2(ebda_seg, dpte.pio, 0);
613 SET_EBDA2(ebda_seg, dpte.options, options);
614 SET_EBDA2(ebda_seg, dpte.reserved, 0);
615 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
616
617 u8 sum = checksum_far(
618 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
619 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500620 } else {
Gleb Natapovf77e1792010-12-23 11:29:35 +0200621 SET_INT13DPT(regs, dpte_segment, 0);
622 SET_INT13DPT(regs, dpte_offset, 0);
623 bdf = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500624 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500625
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400626 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500627 disk_ret(regs, DISK_RET_SUCCESS);
628 return;
629 }
630
631 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500632 SET_INT13DPT(regs, key, 0xbedd);
Gleb Natapovf5154e22011-01-10 10:50:27 +0200633 SET_INT13DPT(regs, dpi_length, t13 ? 44 : 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500634 SET_INT13DPT(regs, reserved1, 0);
635 SET_INT13DPT(regs, reserved2, 0);
636
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500637 if (bdf != -1) {
638 SET_INT13DPT(regs, host_bus[0], 'P');
639 SET_INT13DPT(regs, host_bus[1], 'C');
640 SET_INT13DPT(regs, host_bus[2], 'I');
Gleb Natapovf5154e22011-01-10 10:50:27 +0200641 SET_INT13DPT(regs, host_bus[3], ' ');
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400642
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500643 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
644 | (pci_bdf_to_fn(bdf) << 16));
Gleb Natapovf5154e22011-01-10 10:50:27 +0200645 if (t13)
646 path |= channel << 24;
647
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500648 SET_INT13DPT(regs, iface_path, path);
649 } else {
650 // ISA
651 SET_INT13DPT(regs, host_bus[0], 'I');
652 SET_INT13DPT(regs, host_bus[1], 'S');
653 SET_INT13DPT(regs, host_bus[2], 'A');
Gleb Natapovf5154e22011-01-10 10:50:27 +0200654 SET_INT13DPT(regs, host_bus[3], ' ');
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500655
656 SET_INT13DPT(regs, iface_path, iobase1);
657 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400658
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100659 if (type != DTYPE_VIRTIO_BLK) {
Gleb Natapovf77e1792010-12-23 11:29:35 +0200660 SET_INT13DPT(regs, iface_type[0], 'A');
661 SET_INT13DPT(regs, iface_type[1], 'T');
662 SET_INT13DPT(regs, iface_type[2], 'A');
Gleb Natapovf5154e22011-01-10 10:50:27 +0200663 SET_INT13DPT(regs, iface_type[3], ' ');
Gleb Natapovf77e1792010-12-23 11:29:35 +0200664 } else {
665 SET_INT13DPT(regs, iface_type[0], 'S');
666 SET_INT13DPT(regs, iface_type[1], 'C');
667 SET_INT13DPT(regs, iface_type[2], 'S');
668 SET_INT13DPT(regs, iface_type[3], 'I');
669 }
Gleb Natapovf5154e22011-01-10 10:50:27 +0200670 SET_INT13DPT(regs, iface_type[4], ' ');
671 SET_INT13DPT(regs, iface_type[5], ' ');
672 SET_INT13DPT(regs, iface_type[6], ' ');
673 SET_INT13DPT(regs, iface_type[7], ' ');
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500674
Gleb Natapovf5154e22011-01-10 10:50:27 +0200675 if (t13) {
676 SET_INT13DPT(regs, t13.device_path[0], device_path);
677 SET_INT13DPT(regs, t13.device_path[1], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500678
Gleb Natapovf5154e22011-01-10 10:50:27 +0200679 SET_INT13DPT(regs, t13.checksum
680 , -checksum_far(regs->ds, (void*)(regs->si+30), 43));
681 } else {
682 SET_INT13DPT(regs, phoenix.device_path, device_path);
683
684 SET_INT13DPT(regs, phoenix.checksum
685 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
686 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400687
688 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500689}
690
691// IBM/MS extended media change
692static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400693disk_1349(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500694{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400695 if (regs->dl < EXTSTART_CD) {
696 // Always success for HD
697 disk_ret(regs, DISK_RET_SUCCESS);
698 return;
699 }
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500700 set_invalid(regs);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400701 // always send changed ??
702 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500703}
704
705static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400706disk_134e01(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500707{
708 disk_ret(regs, DISK_RET_SUCCESS);
709}
710
711static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400712disk_134e03(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500713{
714 disk_ret(regs, DISK_RET_SUCCESS);
715}
716
717static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400718disk_134e04(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500719{
720 disk_ret(regs, DISK_RET_SUCCESS);
721}
722
723static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400724disk_134e06(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500725{
726 disk_ret(regs, DISK_RET_SUCCESS);
727}
728
729static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400730disk_134eXX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500731{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500732 disk_ret(regs, DISK_RET_EPARAM);
733}
734
735// IBM/MS set hardware configuration
736static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400737disk_134e(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500738{
739 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400740 case 0x01: disk_134e01(regs, drive_g); break;
741 case 0x03: disk_134e03(regs, drive_g); break;
742 case 0x04: disk_134e04(regs, drive_g); break;
743 case 0x06: disk_134e06(regs, drive_g); break;
744 default: disk_134eXX(regs, drive_g); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500745 }
746}
747
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400748static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400749disk_13XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500750{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500751 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500752}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500753
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400754static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400755disk_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500756{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500757 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500758
759 // clear completion flag
760 SET_BDA(disk_interrupt_flag, 0);
761
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500762 switch (regs->ah) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400763 case 0x00: disk_1300(regs, drive_g); break;
764 case 0x01: disk_1301(regs, drive_g); break;
765 case 0x02: disk_1302(regs, drive_g); break;
766 case 0x03: disk_1303(regs, drive_g); break;
767 case 0x04: disk_1304(regs, drive_g); break;
768 case 0x05: disk_1305(regs, drive_g); break;
769 case 0x08: disk_1308(regs, drive_g); break;
770 case 0x09: disk_1309(regs, drive_g); break;
771 case 0x0c: disk_130c(regs, drive_g); break;
772 case 0x0d: disk_130d(regs, drive_g); break;
773 case 0x10: disk_1310(regs, drive_g); break;
774 case 0x11: disk_1311(regs, drive_g); break;
775 case 0x14: disk_1314(regs, drive_g); break;
776 case 0x15: disk_1315(regs, drive_g); break;
777 case 0x16: disk_1316(regs, drive_g); break;
778 case 0x41: disk_1341(regs, drive_g); break;
779 case 0x42: disk_1342(regs, drive_g); break;
780 case 0x43: disk_1343(regs, drive_g); break;
781 case 0x44: disk_1344(regs, drive_g); break;
782 case 0x45: disk_1345(regs, drive_g); break;
783 case 0x46: disk_1346(regs, drive_g); break;
784 case 0x47: disk_1347(regs, drive_g); break;
785 case 0x48: disk_1348(regs, drive_g); break;
786 case 0x49: disk_1349(regs, drive_g); break;
787 case 0x4e: disk_134e(regs, drive_g); break;
788 default: disk_13XX(regs, drive_g); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500789 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500790}
791
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400792static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400793floppy_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400794{
795 // Only limited commands are supported on floppies.
796 switch (regs->ah) {
797 case 0x00:
798 case 0x01:
799 case 0x02:
800 case 0x03:
801 case 0x04:
802 case 0x05:
803 case 0x08:
804 case 0x15:
805 case 0x16:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400806 disk_13(regs, drive_g);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400807 break;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400808 default: disk_13XX(regs, drive_g); break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400809 }
810}
811
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500812
813/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500814 * Entry points
815 ****************************************************************/
816
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500817static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400818handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500819{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400820 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400821 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500822 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500823 return;
824 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500825
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400826 if (extdrive < EXTSTART_HD) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400827 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, extdrive);
828 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400829 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400830 floppy_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400831 return;
832 }
833
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400834 struct drive_s *drive_g;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400835 if (extdrive >= EXTSTART_CD)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400836 drive_g = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400837 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400838 drive_g = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
839 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400840 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400841 disk_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400842 return;
843
844fail:
845 // XXX - support 1301/1308/1315 anyway?
846 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500847}
848
Kevin O'Connor19786762008-03-05 21:09:59 -0500849void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500850handle_40(struct bregs *regs)
851{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400852 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500853 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500854}
855
856// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500857void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500858handle_13(struct bregs *regs)
859{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400860 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400861 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500862
Kevin O'Connordfa16502008-03-22 20:13:08 -0400863 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500864 if (regs->ah == 0x4b) {
865 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400866 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500867 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500868 u16 ebda_seg = get_ebda_seg();
869 if (GET_EBDA2(ebda_seg, cdemu.active)) {
Kevin O'Connor669e6442009-08-11 22:36:30 -0400870 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_extdrive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400871 if (extdrive == emudrive) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400872 // Access to an emulated drive.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500873 struct drive_s *cdemu_g;
874 cdemu_g = GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf));
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400875 if (regs->ah > 0x16) {
876 // Only old-style commands supported.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500877 disk_13XX(regs, cdemu_g);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400878 return;
879 }
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500880 disk_13(regs, cdemu_g);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400881 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500882 }
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400883 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400884 // Adjust id to make room for emulated drive.
Kevin O'Connor707298a2009-08-11 22:27:51 -0400885 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500886 }
887 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400888 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500889}
890
891// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500892void VISIBLE16
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500893handle_76(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500894{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400895 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400896 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400897 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500898}
Kevin O'Connor30853762009-01-17 18:49:20 -0500899
900// Old Fixed Disk Parameter Table (newer tables are in the ebda).
901struct fdpt_s OldFDPT VAR16FIXED(0xe401);