blob: 9f02a215271be38c60851f748461834c60ee436c [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'Connord3140832012-05-13 22:46:12 -040063 // Emulated drive - get info from CDEmu. (It's not possible to
Kevin O'Connor36c93a52009-09-12 19:35:04 -040064 // populate the geometry directly in the driveid because the
65 // geometry is only known after the bios segment is made
66 // read-only).
Kevin O'Connord3140832012-05-13 22:46:12 -040067 *nlc = GET_LOW(CDEmu.lchs.cylinders);
68 *nlh = GET_LOW(CDEmu.lchs.heads);
69 *nlspt = GET_LOW(CDEmu.lchs.spt);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040070 return;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040071 }
Kevin O'Connor77d227b2009-10-22 21:48:39 -040072 *nlc = GET_GLOBAL(drive_g->lchs.cylinders);
73 *nlh = GET_GLOBAL(drive_g->lchs.heads);
74 *nlspt = GET_GLOBAL(drive_g->lchs.spt);
Kevin O'Connor1625a752009-08-09 13:08:21 -040075}
76
77// Perform read/write/verify using old-style chs accesses
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -040078static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -040079basic_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -040080{
81 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -040082 dop.drive_g = drive_g;
Kevin O'Connor1625a752009-08-09 13:08:21 -040083 dop.command = command;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040084
85 u8 count = regs->al;
86 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
87 u16 sector = regs->cl & 0x3f;
88 u16 head = regs->dh;
89
90 if (count > 128 || count == 0 || sector == 0) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050091 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040092 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040093 return;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040094 }
95 dop.count = count;
96
97 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -040098 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040099
100 // sanity check on cyl heads, sec
101 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500102 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400103 disk_ret(regs, DISK_RET_EPARAM);
104 return;
105 }
106
107 // translate lchs to lba
108 dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
109 + (u32)sector - 1);
110
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400111 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400112
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500113 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400114
Kevin O'Connor1625a752009-08-09 13:08:21 -0400115 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500116
Kevin O'Connor126eac62009-08-16 13:32:24 -0400117 disk_ret(regs, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500118}
119
Kevin O'Connor1625a752009-08-09 13:08:21 -0400120// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400121static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400122extended_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500123{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500124 struct disk_op_s dop;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400125 // Get lba and check.
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500126 dop.lba = GET_INT13EXT(regs, lba);
127 dop.command = command;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400128 dop.drive_g = drive_g;
129 if (dop.lba >= GET_GLOBAL(drive_g->sectors)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500130 warn_invalid(regs);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400131 disk_ret(regs, DISK_RET_EPARAM);
132 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500133 }
134
Kevin O'Connor9f985422009-09-09 11:34:39 -0400135 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_INT13EXT(regs, data));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500136 dop.count = GET_INT13EXT(regs, count);
Kevin O'Connorb50488b2012-02-11 09:38:44 -0500137 if (! dop.count) {
138 // Nothing to do.
139 disk_ret(regs, DISK_RET_SUCCESS);
140 return;
141 }
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400142
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500143 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400144
Kevin O'Connor1625a752009-08-09 13:08:21 -0400145 SET_INT13EXT(regs, count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500146
Kevin O'Connor126eac62009-08-16 13:32:24 -0400147 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500148}
149
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500150
151/****************************************************************
152 * Hard Drive functions
153 ****************************************************************/
154
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500155// disk controller reset
156static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400157disk_1300(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500158{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400159 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400160 dop.drive_g = drive_g;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400161 dop.command = CMD_RESET;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400162 int status = send_disk_op(&dop);
163 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500164}
165
166// read disk status
167static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400168disk_1301(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500169{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400170 u8 v;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400171 if (regs->dl < EXTSTART_HD)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400172 // Floppy
173 v = GET_BDA(floppy_last_status);
174 else
175 v = GET_BDA(disk_last_status);
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400176 regs->ah = v;
177 set_cf(regs, v);
178 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500179}
180
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500181// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500182static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400183disk_1302(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500184{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400185 basic_access(regs, drive_g, CMD_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500186}
187
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500188// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500189static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400190disk_1303(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500191{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400192 basic_access(regs, drive_g, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500193}
194
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500195// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500196static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400197disk_1304(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500198{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400199 basic_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500200}
201
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500202// format disk track
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400203static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400204disk_1305(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500205{
Kevin O'Connor4d9d4002009-11-25 19:35:01 -0500206 debug_stub(regs);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400207
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400208 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400209 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400210
211 u8 num_sectors = regs->al;
212 u8 head = regs->dh;
213
214 if (head >= nlh || num_sectors == 0 || num_sectors > nlspt) {
215 disk_ret(regs, DISK_RET_EPARAM);
216 return;
217 }
218
219 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400220 dop.drive_g = drive_g;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400221 dop.command = CMD_FORMAT;
222 dop.lba = head;
223 dop.count = num_sectors;
224 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
225 int status = send_disk_op(&dop);
226 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500227}
228
229// read disk drive parameters
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400230static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400231disk_1308(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500232{
233 // Get logical geometry from table
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400234 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400235 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400236 nlc--;
237 nlh--;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400238 u8 count;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400239 if (regs->dl < EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400240 // Floppy
Kevin O'Connora0842f82010-12-29 11:05:46 -0500241 count = GET_GLOBAL(FloppyCount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500242
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500243 if (CONFIG_CDROM_EMU
244 && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf)))
Kevin O'Connord3140832012-05-13 22:46:12 -0400245 regs->bx = GET_LOW(CDEmu.media) * 2;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400246 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400247 regs->bx = GET_GLOBAL(drive_g->floppy_type);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400248
249 // set es & di to point to 11 byte diskette param table in ROM
250 regs->es = SEG_BIOS;
251 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400252 } else if (regs->dl < EXTSTART_CD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400253 // Hard drive
254 count = GET_BDA(hdcount);
255 nlc--; // last sector reserved
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400256 } else {
257 // Not supported on CDROM
258 disk_ret(regs, DISK_RET_EPARAM);
259 return;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400260 }
261
Kevin O'Connord3140832012-05-13 22:46:12 -0400262 if (CONFIG_CDROM_EMU && GET_LOW(CDEmu.active)) {
263 u8 emudrive = GET_LOW(CDEmu.emulated_extdrive);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400264 if (((emudrive ^ regs->dl) & 0x80) == 0)
265 // Note extra drive due to emulation.
266 count++;
267 if (regs->dl < EXTSTART_HD && count > 2)
268 // Max of two floppy drives.
269 count = 2;
270 }
271
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500272 regs->al = 0;
273 regs->ch = nlc & 0xff;
274 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400275 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500276
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500277 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400278 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500279}
280
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500281// initialize drive parameters
282static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400283disk_1309(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500284{
285 DISK_STUB(regs);
286}
287
288// seek to specified cylinder
289static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400290disk_130c(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500291{
292 DISK_STUB(regs);
293}
294
295// alternate disk reset
296static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400297disk_130d(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500298{
299 DISK_STUB(regs);
300}
301
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500302// check drive ready
303static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400304disk_1310(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500305{
306 // should look at 40:8E also???
307
Kevin O'Connor42337662009-08-10 00:06:37 -0400308 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400309 dop.drive_g = drive_g;
Kevin O'Connor42337662009-08-10 00:06:37 -0400310 dop.command = CMD_ISREADY;
311 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400312 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500313}
314
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500315// recalibrate
316static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400317disk_1311(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500318{
319 DISK_STUB(regs);
320}
321
322// controller internal diagnostic
323static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400324disk_1314(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500325{
326 DISK_STUB(regs);
327}
328
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500329// read disk drive size
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400330static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400331disk_1315(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500332{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400333 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400334 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
335 // Floppy or cdrom
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400336 regs->ah = 1;
337 return;
338 }
339 // Hard drive
340
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500341 // Get logical geometry from table
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400342 u16 nlc, nlh, nlspt;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400343 fillLCHS(drive_g, &nlc, &nlh, &nlspt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500344
345 // Compute sector count seen by int13
346 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
347 regs->cx = lba >> 16;
348 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500349 regs->ah = 3; // hard disk accessible
350}
351
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400352static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400353disk_1316(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400354{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400355 if (regs->dl >= EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400356 // Hard drive
357 disk_ret(regs, DISK_RET_EPARAM);
358 return;
359 }
360 disk_ret(regs, DISK_RET_ECHANGED);
361}
362
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500363// IBM/MS installation check
364static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400365disk_1341(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500366{
367 regs->bx = 0xaa55; // install check
368 regs->cx = 0x0007; // ext disk access and edd, removable supported
369 disk_ret(regs, DISK_RET_SUCCESS);
370 regs->ah = 0x30; // EDD 3.0
371}
372
373// IBM/MS extended read
374static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400375disk_1342(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500376{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400377 extended_access(regs, drive_g, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500378}
379
380// IBM/MS extended write
381static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400382disk_1343(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500383{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400384 extended_access(regs, drive_g, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500385}
386
387// IBM/MS verify
388static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400389disk_1344(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500390{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400391 extended_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500392}
393
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400394// lock
395static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400396disk_134500(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400397{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400398 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400399 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400400 if (locks == 0xff) {
401 regs->al = 1;
402 disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
403 return;
404 }
Kevin O'Connord3140832012-05-13 22:46:12 -0400405 SET_LOW(CDRom_locks[cdid], locks + 1);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400406 regs->al = 1;
407 disk_ret(regs, DISK_RET_SUCCESS);
408}
409
410// unlock
411static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400412disk_134501(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400413{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400414 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400415 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400416 if (locks == 0x00) {
417 regs->al = 0;
418 disk_ret(regs, DISK_RET_ENOTLOCKED);
419 return;
420 }
421 locks--;
Kevin O'Connord3140832012-05-13 22:46:12 -0400422 SET_LOW(CDRom_locks[cdid], locks);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400423 regs->al = (locks ? 1 : 0);
424 disk_ret(regs, DISK_RET_SUCCESS);
425}
426
427// status
428static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400429disk_134502(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400430{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400431 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400432 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400433 regs->al = (locks ? 1 : 0);
434 disk_ret(regs, DISK_RET_SUCCESS);
435}
436
437static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400438disk_1345XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400439{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500440 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400441}
442
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500443// IBM/MS lock/unlock drive
444static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400445disk_1345(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500446{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400447 if (regs->dl < EXTSTART_CD) {
448 // Always success for HD
449 disk_ret(regs, DISK_RET_SUCCESS);
450 return;
451 }
452
453 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400454 case 0x00: disk_134500(regs, drive_g); break;
455 case 0x01: disk_134501(regs, drive_g); break;
456 case 0x02: disk_134502(regs, drive_g); break;
457 default: disk_1345XX(regs, drive_g); break;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400458 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500459}
460
461// IBM/MS eject media
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400462static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400463disk_1346(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500464{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400465 if (regs->dl < EXTSTART_CD) {
466 // Volume Not Removable
467 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
468 return;
469 }
470
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400471 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400472 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400473 if (locks != 0) {
474 disk_ret(regs, DISK_RET_ELOCKED);
475 return;
476 }
477
478 // FIXME should handle 0x31 no media in device
479 // FIXME should handle 0xb5 valid request failed
480
481 // Call removable media eject
482 struct bregs br;
483 memset(&br, 0, sizeof(br));
484 br.ah = 0x52;
Kevin O'Connor74152702010-03-20 17:53:40 -0400485 br.dl = regs->dl;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400486 call16_int(0x15, &br);
487
488 if (br.ah || br.flags & F_CF) {
489 disk_ret(regs, DISK_RET_ELOCKED);
490 return;
491 }
492 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500493}
494
495// IBM/MS extended seek
496static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400497disk_1347(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500498{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400499 extended_access(regs, drive_g, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500500}
501
502// IBM/MS get drive parameters
503static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400504disk_1348(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500505{
506 u16 size = GET_INT13DPT(regs, size);
Gleb Natapovf5154e22011-01-10 10:50:27 +0200507 u16 t13 = size == 74;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500508
509 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400510 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500511 disk_ret(regs, DISK_RET_EPARAM);
512 return;
513 }
514
515 // EDD 1.x
516
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400517 u8 type = GET_GLOBAL(drive_g->type);
518 u16 npc = GET_GLOBAL(drive_g->pchs.cylinders);
519 u16 nph = GET_GLOBAL(drive_g->pchs.heads);
520 u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
521 u64 lba = GET_GLOBAL(drive_g->sectors);
522 u16 blksize = GET_GLOBAL(drive_g->blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500523
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400524 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
525 , size, type, npc, nph, npspt, (u32)lba, blksize);
526
527 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400528 if (type == DTYPE_ATAPI) {
529 // 0x74 = removable, media change, lockable, max values
530 SET_INT13DPT(regs, infos, 0x74);
531 SET_INT13DPT(regs, cylinders, 0xffffffff);
532 SET_INT13DPT(regs, heads, 0xffffffff);
533 SET_INT13DPT(regs, spt, 0xffffffff);
534 SET_INT13DPT(regs, sector_count, (u64)-1);
535 } else {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400536 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500537 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
538 SET_INT13DPT(regs, cylinders, 0x3fff);
539 } else {
540 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
541 SET_INT13DPT(regs, cylinders, (u32)npc);
542 }
543 SET_INT13DPT(regs, heads, (u32)nph);
544 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400545 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500546 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500547 SET_INT13DPT(regs, blksize, blksize);
548
Gleb Natapovf77e1792010-12-23 11:29:35 +0200549 if (size < 30 ||
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100550 (type != DTYPE_ATA && type != DTYPE_ATAPI &&
551 type != DTYPE_VIRTIO_BLK && type != DTYPE_VIRTIO_SCSI)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500552 disk_ret(regs, DISK_RET_SUCCESS);
553 return;
554 }
555
556 // EDD 2.x
557
Gleb Natapovf77e1792010-12-23 11:29:35 +0200558 int bdf;
Gleb Natapovf5154e22011-01-10 10:50:27 +0200559 u16 iobase1 = 0;
560 u64 device_path = 0;
561 u8 channel = 0;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400562 SET_INT13DPT(regs, size, 30);
Gleb Natapovf77e1792010-12-23 11:29:35 +0200563 if (type == DTYPE_ATA || type == DTYPE_ATAPI) {
Kevin O'Connord3140832012-05-13 22:46:12 -0400564 SET_INT13DPT(regs, dpte, SEGOFF(SEG_LOW, (u32)&DefaultDPTE));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500565
Gleb Natapovf77e1792010-12-23 11:29:35 +0200566 // Fill in dpte
567 struct atadrive_s *adrive_g = container_of(
568 drive_g, struct atadrive_s, drive);
569 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
570 u8 slave = GET_GLOBAL(adrive_g->slave);
571 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
572 u8 irq = GET_GLOBALFLAT(chan_gf->irq);
573 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
574 bdf = GET_GLOBALFLAT(chan_gf->pci_bdf);
575 device_path = slave;
Gleb Natapovf5154e22011-01-10 10:50:27 +0200576 channel = GET_GLOBALFLAT(chan_gf->chanid);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500577
Gleb Natapovf77e1792010-12-23 11:29:35 +0200578 u16 options = 0;
579 if (type == DTYPE_ATA) {
580 u8 translation = GET_GLOBAL(drive_g->translation);
581 if (translation != TRANSLATION_NONE) {
582 options |= 1<<3; // CHS translation
583 if (translation == TRANSLATION_LBA)
584 options |= 1<<9;
585 if (translation == TRANSLATION_RECHS)
586 options |= 3<<9;
587 }
588 } else {
589 // ATAPI
590 options |= 1<<5; // removable device
591 options |= 1<<6; // atapi device
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400592 }
Gleb Natapovf77e1792010-12-23 11:29:35 +0200593 options |= 1<<4; // lba translation
594 if (CONFIG_ATA_PIO32)
595 options |= 1<<7;
596
Kevin O'Connord3140832012-05-13 22:46:12 -0400597 SET_LOW(DefaultDPTE.iobase1, iobase1);
598 SET_LOW(DefaultDPTE.iobase2, iobase2 + ATA_CB_DC);
599 SET_LOW(DefaultDPTE.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
600 | ATA_CB_DH_LBA));
601 SET_LOW(DefaultDPTE.unused, 0xcb);
602 SET_LOW(DefaultDPTE.irq, irq);
603 SET_LOW(DefaultDPTE.blkcount, 1);
604 SET_LOW(DefaultDPTE.dma, 0);
605 SET_LOW(DefaultDPTE.pio, 0);
606 SET_LOW(DefaultDPTE.options, options);
607 SET_LOW(DefaultDPTE.reserved, 0);
608 SET_LOW(DefaultDPTE.revision, 0x11);
Gleb Natapovf77e1792010-12-23 11:29:35 +0200609
Kevin O'Connord3140832012-05-13 22:46:12 -0400610 u8 sum = checksum_far(SEG_LOW, &DefaultDPTE, 15);
611 SET_LOW(DefaultDPTE.checksum, -sum);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500612 } else {
Kevin O'Connord3140832012-05-13 22:46:12 -0400613 SET_INT13DPT(regs, dpte.segoff, 0);
Gleb Natapovf77e1792010-12-23 11:29:35 +0200614 bdf = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500615 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500616
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400617 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500618 disk_ret(regs, DISK_RET_SUCCESS);
619 return;
620 }
621
622 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500623 SET_INT13DPT(regs, key, 0xbedd);
Gleb Natapovf5154e22011-01-10 10:50:27 +0200624 SET_INT13DPT(regs, dpi_length, t13 ? 44 : 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500625 SET_INT13DPT(regs, reserved1, 0);
626 SET_INT13DPT(regs, reserved2, 0);
627
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500628 if (bdf != -1) {
629 SET_INT13DPT(regs, host_bus[0], 'P');
630 SET_INT13DPT(regs, host_bus[1], 'C');
631 SET_INT13DPT(regs, host_bus[2], 'I');
Gleb Natapovf5154e22011-01-10 10:50:27 +0200632 SET_INT13DPT(regs, host_bus[3], ' ');
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400633
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500634 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
635 | (pci_bdf_to_fn(bdf) << 16));
Gleb Natapovf5154e22011-01-10 10:50:27 +0200636 if (t13)
637 path |= channel << 24;
638
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500639 SET_INT13DPT(regs, iface_path, path);
640 } else {
641 // ISA
642 SET_INT13DPT(regs, host_bus[0], 'I');
643 SET_INT13DPT(regs, host_bus[1], 'S');
644 SET_INT13DPT(regs, host_bus[2], 'A');
Gleb Natapovf5154e22011-01-10 10:50:27 +0200645 SET_INT13DPT(regs, host_bus[3], ' ');
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500646
647 SET_INT13DPT(regs, iface_path, iobase1);
648 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400649
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100650 if (type != DTYPE_VIRTIO_BLK) {
Gleb Natapovf77e1792010-12-23 11:29:35 +0200651 SET_INT13DPT(regs, iface_type[0], 'A');
652 SET_INT13DPT(regs, iface_type[1], 'T');
653 SET_INT13DPT(regs, iface_type[2], 'A');
Gleb Natapovf5154e22011-01-10 10:50:27 +0200654 SET_INT13DPT(regs, iface_type[3], ' ');
Gleb Natapovf77e1792010-12-23 11:29:35 +0200655 } else {
656 SET_INT13DPT(regs, iface_type[0], 'S');
657 SET_INT13DPT(regs, iface_type[1], 'C');
658 SET_INT13DPT(regs, iface_type[2], 'S');
659 SET_INT13DPT(regs, iface_type[3], 'I');
660 }
Gleb Natapovf5154e22011-01-10 10:50:27 +0200661 SET_INT13DPT(regs, iface_type[4], ' ');
662 SET_INT13DPT(regs, iface_type[5], ' ');
663 SET_INT13DPT(regs, iface_type[6], ' ');
664 SET_INT13DPT(regs, iface_type[7], ' ');
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500665
Gleb Natapovf5154e22011-01-10 10:50:27 +0200666 if (t13) {
667 SET_INT13DPT(regs, t13.device_path[0], device_path);
668 SET_INT13DPT(regs, t13.device_path[1], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500669
Gleb Natapovf5154e22011-01-10 10:50:27 +0200670 SET_INT13DPT(regs, t13.checksum
671 , -checksum_far(regs->ds, (void*)(regs->si+30), 43));
672 } else {
673 SET_INT13DPT(regs, phoenix.device_path, device_path);
674
675 SET_INT13DPT(regs, phoenix.checksum
676 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
677 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400678
679 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500680}
681
682// IBM/MS extended media change
683static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400684disk_1349(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500685{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400686 if (regs->dl < EXTSTART_CD) {
687 // Always success for HD
688 disk_ret(regs, DISK_RET_SUCCESS);
689 return;
690 }
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500691 set_invalid(regs);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400692 // always send changed ??
693 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500694}
695
696static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400697disk_134e01(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500698{
699 disk_ret(regs, DISK_RET_SUCCESS);
700}
701
702static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400703disk_134e03(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500704{
705 disk_ret(regs, DISK_RET_SUCCESS);
706}
707
708static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400709disk_134e04(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500710{
711 disk_ret(regs, DISK_RET_SUCCESS);
712}
713
714static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400715disk_134e06(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500716{
717 disk_ret(regs, DISK_RET_SUCCESS);
718}
719
720static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400721disk_134eXX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500722{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500723 disk_ret(regs, DISK_RET_EPARAM);
724}
725
726// IBM/MS set hardware configuration
727static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400728disk_134e(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500729{
730 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400731 case 0x01: disk_134e01(regs, drive_g); break;
732 case 0x03: disk_134e03(regs, drive_g); break;
733 case 0x04: disk_134e04(regs, drive_g); break;
734 case 0x06: disk_134e06(regs, drive_g); break;
735 default: disk_134eXX(regs, drive_g); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500736 }
737}
738
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400739static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400740disk_13XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500741{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500742 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500743}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500744
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400745static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400746disk_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500747{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500748 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500749
750 // clear completion flag
751 SET_BDA(disk_interrupt_flag, 0);
752
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500753 switch (regs->ah) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400754 case 0x00: disk_1300(regs, drive_g); break;
755 case 0x01: disk_1301(regs, drive_g); break;
756 case 0x02: disk_1302(regs, drive_g); break;
757 case 0x03: disk_1303(regs, drive_g); break;
758 case 0x04: disk_1304(regs, drive_g); break;
759 case 0x05: disk_1305(regs, drive_g); break;
760 case 0x08: disk_1308(regs, drive_g); break;
761 case 0x09: disk_1309(regs, drive_g); break;
762 case 0x0c: disk_130c(regs, drive_g); break;
763 case 0x0d: disk_130d(regs, drive_g); break;
764 case 0x10: disk_1310(regs, drive_g); break;
765 case 0x11: disk_1311(regs, drive_g); break;
766 case 0x14: disk_1314(regs, drive_g); break;
767 case 0x15: disk_1315(regs, drive_g); break;
768 case 0x16: disk_1316(regs, drive_g); break;
769 case 0x41: disk_1341(regs, drive_g); break;
770 case 0x42: disk_1342(regs, drive_g); break;
771 case 0x43: disk_1343(regs, drive_g); break;
772 case 0x44: disk_1344(regs, drive_g); break;
773 case 0x45: disk_1345(regs, drive_g); break;
774 case 0x46: disk_1346(regs, drive_g); break;
775 case 0x47: disk_1347(regs, drive_g); break;
776 case 0x48: disk_1348(regs, drive_g); break;
777 case 0x49: disk_1349(regs, drive_g); break;
778 case 0x4e: disk_134e(regs, drive_g); break;
779 default: disk_13XX(regs, drive_g); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500780 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500781}
782
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400783static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400784floppy_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400785{
786 // Only limited commands are supported on floppies.
787 switch (regs->ah) {
788 case 0x00:
789 case 0x01:
790 case 0x02:
791 case 0x03:
792 case 0x04:
793 case 0x05:
794 case 0x08:
795 case 0x15:
796 case 0x16:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400797 disk_13(regs, drive_g);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400798 break;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400799 default: disk_13XX(regs, drive_g); break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400800 }
801}
802
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500803
804/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500805 * Entry points
806 ****************************************************************/
807
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500808static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400809handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500810{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400811 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400812 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500813 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500814 return;
815 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500816
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400817 if (extdrive < EXTSTART_HD) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400818 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, extdrive);
819 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400820 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400821 floppy_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400822 return;
823 }
824
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400825 struct drive_s *drive_g;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400826 if (extdrive >= EXTSTART_CD)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400827 drive_g = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400828 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400829 drive_g = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
830 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400831 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400832 disk_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400833 return;
834
835fail:
836 // XXX - support 1301/1308/1315 anyway?
837 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500838}
839
Kevin O'Connor19786762008-03-05 21:09:59 -0500840void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500841handle_40(struct bregs *regs)
842{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400843 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500844 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500845}
846
847// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500848void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500849handle_13(struct bregs *regs)
850{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400851 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400852 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500853
Kevin O'Connordfa16502008-03-22 20:13:08 -0400854 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500855 if (regs->ah == 0x4b) {
856 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400857 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500858 }
Kevin O'Connord3140832012-05-13 22:46:12 -0400859 if (GET_LOW(CDEmu.active)) {
860 u8 emudrive = GET_LOW(CDEmu.emulated_extdrive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400861 if (extdrive == emudrive) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400862 // Access to an emulated drive.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500863 struct drive_s *cdemu_g;
864 cdemu_g = GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf));
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400865 if (regs->ah > 0x16) {
866 // Only old-style commands supported.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500867 disk_13XX(regs, cdemu_g);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400868 return;
869 }
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500870 disk_13(regs, cdemu_g);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400871 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500872 }
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400873 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400874 // Adjust id to make room for emulated drive.
Kevin O'Connor707298a2009-08-11 22:27:51 -0400875 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500876 }
877 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400878 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500879}
880
881// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500882void VISIBLE16
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500883handle_76(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500884{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400885 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400886 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400887 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500888}
Kevin O'Connor30853762009-01-17 18:49:20 -0500889
890// Old Fixed Disk Parameter Table (newer tables are in the ebda).
891struct fdpt_s OldFDPT VAR16FIXED(0xe401);