blob: 0291fe3e09d91c29dac897fd1355bf08312e528b [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'Connor48365582012-05-27 10:45:32 -040058static struct chs_s
59getLCHS(struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050060{
Kevin O'Connor48365582012-05-27 10:45:32 -040061 struct chs_s res = { };
Kevin O'Connor8f469b92010-02-28 01:28:11 -050062 if (CONFIG_CDROM_EMU
63 && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf))) {
Kevin O'Connord3140832012-05-13 22:46:12 -040064 // Emulated drive - get info from CDEmu. (It's not possible to
Kevin O'Connor36c93a52009-09-12 19:35:04 -040065 // populate the geometry directly in the driveid because the
66 // geometry is only known after the bios segment is made
67 // read-only).
Kevin O'Connor48365582012-05-27 10:45:32 -040068 res.cylinders = GET_LOW(CDEmu.lchs.cylinders);
69 res.heads = GET_LOW(CDEmu.lchs.heads);
70 res.spt = GET_LOW(CDEmu.lchs.spt);
71 return res;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040072 }
Kevin O'Connor48365582012-05-27 10:45:32 -040073 res.cylinders = GET_GLOBAL(drive_g->lchs.cylinders);
74 res.heads = GET_GLOBAL(drive_g->lchs.heads);
75 res.spt = GET_GLOBAL(drive_g->lchs.spt);
76 return res;
Kevin O'Connor1625a752009-08-09 13:08:21 -040077}
78
79// Perform read/write/verify using old-style chs accesses
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -040080static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -040081basic_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connor1625a752009-08-09 13:08:21 -040082{
83 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -040084 dop.drive_g = drive_g;
Kevin O'Connor1625a752009-08-09 13:08:21 -040085 dop.command = command;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040086
87 u8 count = regs->al;
88 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
89 u16 sector = regs->cl & 0x3f;
90 u16 head = regs->dh;
91
92 if (count > 128 || count == 0 || sector == 0) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050093 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040094 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040095 return;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040096 }
97 dop.count = count;
98
Kevin O'Connor48365582012-05-27 10:45:32 -040099 struct chs_s chs = getLCHS(drive_g);
100 u16 nlc=chs.cylinders, nlh=chs.heads, nlspt=chs.spt;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400101
102 // sanity check on cyl heads, sec
103 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500104 warn_invalid(regs);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400105 disk_ret(regs, DISK_RET_EPARAM);
106 return;
107 }
108
109 // translate lchs to lba
110 dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
111 + (u32)sector - 1);
112
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400113 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400114
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500115 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400116
Kevin O'Connor1625a752009-08-09 13:08:21 -0400117 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500118
Kevin O'Connor126eac62009-08-16 13:32:24 -0400119 disk_ret(regs, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500120}
121
Kevin O'Connor1625a752009-08-09 13:08:21 -0400122// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400123static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400124extended_access(struct bregs *regs, struct drive_s *drive_g, u16 command)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500125{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500126 struct disk_op_s dop;
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400127 struct int13ext_s *param_far = (struct int13ext_s*)(regs->si+0);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400128 // Get lba and check.
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400129 dop.lba = GET_FARVAR(regs->ds, param_far->lba);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500130 dop.command = command;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400131 dop.drive_g = drive_g;
132 if (dop.lba >= GET_GLOBAL(drive_g->sectors)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500133 warn_invalid(regs);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400134 disk_ret(regs, DISK_RET_EPARAM);
135 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500136 }
137
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400138 dop.buf_fl = SEGOFF_TO_FLATPTR(GET_FARVAR(regs->ds, param_far->data));
139 dop.count = GET_FARVAR(regs->ds, param_far->count);
Kevin O'Connorb50488b2012-02-11 09:38:44 -0500140 if (! dop.count) {
141 // Nothing to do.
142 disk_ret(regs, DISK_RET_SUCCESS);
143 return;
144 }
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400145
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500146 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400147
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400148 SET_FARVAR(regs->ds, param_far->count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500149
Kevin O'Connor126eac62009-08-16 13:32:24 -0400150 disk_ret(regs, status);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500151}
152
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500153
154/****************************************************************
155 * Hard Drive functions
156 ****************************************************************/
157
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500158// disk controller reset
159static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400160disk_1300(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500161{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400162 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400163 dop.drive_g = drive_g;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400164 dop.command = CMD_RESET;
Kevin O'Connor126eac62009-08-16 13:32:24 -0400165 int status = send_disk_op(&dop);
166 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500167}
168
169// read disk status
170static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400171disk_1301(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500172{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400173 u8 v;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400174 if (regs->dl < EXTSTART_HD)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400175 // Floppy
176 v = GET_BDA(floppy_last_status);
177 else
178 v = GET_BDA(disk_last_status);
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400179 regs->ah = v;
180 set_cf(regs, v);
181 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500182}
183
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500184// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500185static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400186disk_1302(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500187{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400188 basic_access(regs, drive_g, CMD_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500189}
190
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500191// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500192static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400193disk_1303(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500194{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400195 basic_access(regs, drive_g, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500196}
197
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500198// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500199static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400200disk_1304(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500201{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400202 basic_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500203}
204
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500205// format disk track
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400206static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400207disk_1305(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500208{
Kevin O'Connor4d9d4002009-11-25 19:35:01 -0500209 debug_stub(regs);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400210
Kevin O'Connor48365582012-05-27 10:45:32 -0400211 struct chs_s chs = getLCHS(drive_g);
212 u16 nlh=chs.heads, nlspt=chs.spt;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400213
214 u8 num_sectors = regs->al;
215 u8 head = regs->dh;
216
217 if (head >= nlh || num_sectors == 0 || num_sectors > nlspt) {
218 disk_ret(regs, DISK_RET_EPARAM);
219 return;
220 }
221
222 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400223 dop.drive_g = drive_g;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400224 dop.command = CMD_FORMAT;
225 dop.lba = head;
226 dop.count = num_sectors;
227 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
228 int status = send_disk_op(&dop);
229 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500230}
231
232// read disk drive parameters
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400233static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400234disk_1308(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500235{
236 // Get logical geometry from table
Kevin O'Connor48365582012-05-27 10:45:32 -0400237 struct chs_s chs = getLCHS(drive_g);
238 u16 nlc=chs.cylinders, nlh=chs.heads, nlspt=chs.spt;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400239 nlc--;
240 nlh--;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400241 u8 count;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400242 if (regs->dl < EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400243 // Floppy
Kevin O'Connora0842f82010-12-29 11:05:46 -0500244 count = GET_GLOBAL(FloppyCount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500245
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500246 if (CONFIG_CDROM_EMU
247 && drive_g == GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf)))
Kevin O'Connord3140832012-05-13 22:46:12 -0400248 regs->bx = GET_LOW(CDEmu.media) * 2;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400249 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400250 regs->bx = GET_GLOBAL(drive_g->floppy_type);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400251
252 // set es & di to point to 11 byte diskette param table in ROM
253 regs->es = SEG_BIOS;
254 regs->di = (u32)&diskette_param_table2;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400255 } else if (regs->dl < EXTSTART_CD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400256 // Hard drive
257 count = GET_BDA(hdcount);
258 nlc--; // last sector reserved
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400259 } else {
260 // Not supported on CDROM
261 disk_ret(regs, DISK_RET_EPARAM);
262 return;
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400263 }
264
Kevin O'Connord3140832012-05-13 22:46:12 -0400265 if (CONFIG_CDROM_EMU && GET_LOW(CDEmu.active)) {
266 u8 emudrive = GET_LOW(CDEmu.emulated_extdrive);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400267 if (((emudrive ^ regs->dl) & 0x80) == 0)
268 // Note extra drive due to emulation.
269 count++;
270 if (regs->dl < EXTSTART_HD && count > 2)
271 // Max of two floppy drives.
272 count = 2;
273 }
274
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500275 regs->al = 0;
276 regs->ch = nlc & 0xff;
277 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400278 regs->dh = nlh;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500279
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500280 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400281 regs->dl = count;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500282}
283
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500284// initialize drive parameters
285static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400286disk_1309(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500287{
288 DISK_STUB(regs);
289}
290
291// seek to specified cylinder
292static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400293disk_130c(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500294{
295 DISK_STUB(regs);
296}
297
298// alternate disk reset
299static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400300disk_130d(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500301{
302 DISK_STUB(regs);
303}
304
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500305// check drive ready
306static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400307disk_1310(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500308{
309 // should look at 40:8E also???
310
Kevin O'Connor42337662009-08-10 00:06:37 -0400311 struct disk_op_s dop;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400312 dop.drive_g = drive_g;
Kevin O'Connor42337662009-08-10 00:06:37 -0400313 dop.command = CMD_ISREADY;
314 int status = send_disk_op(&dop);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400315 disk_ret(regs, status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500316}
317
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500318// recalibrate
319static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400320disk_1311(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500321{
322 DISK_STUB(regs);
323}
324
325// controller internal diagnostic
326static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400327disk_1314(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500328{
329 DISK_STUB(regs);
330}
331
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500332// read disk drive size
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400333static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400334disk_1315(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500335{
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400336 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400337 if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
338 // Floppy or cdrom
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400339 regs->ah = 1;
340 return;
341 }
342 // Hard drive
343
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500344 // Get logical geometry from table
Kevin O'Connor48365582012-05-27 10:45:32 -0400345 struct chs_s chs = getLCHS(drive_g);
346 u16 nlc=chs.cylinders, nlh=chs.heads, nlspt=chs.spt;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500347
348 // Compute sector count seen by int13
349 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
350 regs->cx = lba >> 16;
351 regs->dx = lba & 0xffff;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500352 regs->ah = 3; // hard disk accessible
353}
354
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400355static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400356disk_1316(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400357{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400358 if (regs->dl >= EXTSTART_HD) {
Kevin O'Connor48410fd2009-08-16 14:13:36 -0400359 // Hard drive
360 disk_ret(regs, DISK_RET_EPARAM);
361 return;
362 }
363 disk_ret(regs, DISK_RET_ECHANGED);
364}
365
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500366// IBM/MS installation check
367static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400368disk_1341(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500369{
370 regs->bx = 0xaa55; // install check
371 regs->cx = 0x0007; // ext disk access and edd, removable supported
372 disk_ret(regs, DISK_RET_SUCCESS);
373 regs->ah = 0x30; // EDD 3.0
374}
375
376// IBM/MS extended read
377static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400378disk_1342(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500379{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400380 extended_access(regs, drive_g, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500381}
382
383// IBM/MS extended write
384static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400385disk_1343(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500386{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400387 extended_access(regs, drive_g, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500388}
389
390// IBM/MS verify
391static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400392disk_1344(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500393{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400394 extended_access(regs, drive_g, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500395}
396
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400397// lock
398static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400399disk_134500(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400400{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400401 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400402 u8 locks = GET_LOW(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'Connord3140832012-05-13 22:46:12 -0400408 SET_LOW(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{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400417 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400418 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400419 if (locks == 0x00) {
420 regs->al = 0;
421 disk_ret(regs, DISK_RET_ENOTLOCKED);
422 return;
423 }
424 locks--;
Kevin O'Connord3140832012-05-13 22:46:12 -0400425 SET_LOW(CDRom_locks[cdid], locks);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400426 regs->al = (locks ? 1 : 0);
427 disk_ret(regs, DISK_RET_SUCCESS);
428}
429
430// status
431static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400432disk_134502(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400433{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400434 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400435 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400436 regs->al = (locks ? 1 : 0);
437 disk_ret(regs, DISK_RET_SUCCESS);
438}
439
440static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400441disk_1345XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400442{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500443 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400444}
445
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500446// IBM/MS lock/unlock drive
447static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400448disk_1345(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500449{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400450 if (regs->dl < EXTSTART_CD) {
451 // Always success for HD
452 disk_ret(regs, DISK_RET_SUCCESS);
453 return;
454 }
455
456 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400457 case 0x00: disk_134500(regs, drive_g); break;
458 case 0x01: disk_134501(regs, drive_g); break;
459 case 0x02: disk_134502(regs, drive_g); break;
460 default: disk_1345XX(regs, drive_g); break;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400461 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500462}
463
464// IBM/MS eject media
Kevin O'Connorecf9b7d2012-03-25 10:21:27 -0400465static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400466disk_1346(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500467{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400468 if (regs->dl < EXTSTART_CD) {
469 // Volume Not Removable
470 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
471 return;
472 }
473
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400474 int cdid = regs->dl - EXTSTART_CD;
Kevin O'Connord3140832012-05-13 22:46:12 -0400475 u8 locks = GET_LOW(CDRom_locks[cdid]);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400476 if (locks != 0) {
477 disk_ret(regs, DISK_RET_ELOCKED);
478 return;
479 }
480
481 // FIXME should handle 0x31 no media in device
482 // FIXME should handle 0xb5 valid request failed
483
484 // Call removable media eject
485 struct bregs br;
486 memset(&br, 0, sizeof(br));
487 br.ah = 0x52;
Kevin O'Connor74152702010-03-20 17:53:40 -0400488 br.dl = regs->dl;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400489 call16_int(0x15, &br);
490
491 if (br.ah || br.flags & F_CF) {
492 disk_ret(regs, DISK_RET_ELOCKED);
493 return;
494 }
495 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500496}
497
498// IBM/MS extended seek
499static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400500disk_1347(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500501{
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400502 extended_access(regs, drive_g, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500503}
504
505// IBM/MS get drive parameters
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400506static void noinline
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400507disk_1348(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500508{
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400509 u16 seg = regs->ds;
510 struct int13dpt_s *param_far = (struct int13dpt_s*)(regs->si+0);
511 u16 size = GET_FARVAR(seg, param_far->size);
Gleb Natapovf5154e22011-01-10 10:50:27 +0200512 u16 t13 = size == 74;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500513
514 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400515 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500516 disk_ret(regs, DISK_RET_EPARAM);
517 return;
518 }
519
520 // EDD 1.x
521
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400522 u8 type = GET_GLOBAL(drive_g->type);
523 u16 npc = GET_GLOBAL(drive_g->pchs.cylinders);
524 u16 nph = GET_GLOBAL(drive_g->pchs.heads);
525 u16 npspt = GET_GLOBAL(drive_g->pchs.spt);
526 u64 lba = GET_GLOBAL(drive_g->sectors);
527 u16 blksize = GET_GLOBAL(drive_g->blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500528
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400529 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
530 , size, type, npc, nph, npspt, (u32)lba, blksize);
531
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400532 SET_FARVAR(seg, param_far->size, 26);
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400533 if (type == DTYPE_ATA_ATAPI) {
Kevin O'Connorb1144362009-08-11 20:43:38 -0400534 // 0x74 = removable, media change, lockable, max values
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400535 SET_FARVAR(seg, param_far->infos, 0x74);
536 SET_FARVAR(seg, param_far->cylinders, 0xffffffff);
537 SET_FARVAR(seg, param_far->heads, 0xffffffff);
538 SET_FARVAR(seg, param_far->spt, 0xffffffff);
539 SET_FARVAR(seg, param_far->sector_count, (u64)-1);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400540 } else {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400541 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400542 SET_FARVAR(seg, param_far->infos, 0x00); // geometry is invalid
543 SET_FARVAR(seg, param_far->cylinders, 0x3fff);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500544 } else {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400545 SET_FARVAR(seg, param_far->infos, 0x02); // geometry is valid
546 SET_FARVAR(seg, param_far->cylinders, (u32)npc);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500547 }
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400548 SET_FARVAR(seg, param_far->heads, (u32)nph);
549 SET_FARVAR(seg, param_far->spt, (u32)npspt);
550 SET_FARVAR(seg, param_far->sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500551 }
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400552 SET_FARVAR(seg, param_far->blksize, blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500553
Gleb Natapovf77e1792010-12-23 11:29:35 +0200554 if (size < 30 ||
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400555 (type != DTYPE_ATA && type != DTYPE_ATA_ATAPI &&
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100556 type != DTYPE_VIRTIO_BLK && type != DTYPE_VIRTIO_SCSI)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500557 disk_ret(regs, DISK_RET_SUCCESS);
558 return;
559 }
560
561 // EDD 2.x
562
Gleb Natapovf77e1792010-12-23 11:29:35 +0200563 int bdf;
Gleb Natapovf5154e22011-01-10 10:50:27 +0200564 u16 iobase1 = 0;
565 u64 device_path = 0;
566 u8 channel = 0;
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400567 SET_FARVAR(seg, param_far->size, 30);
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400568 if (type == DTYPE_ATA || type == DTYPE_ATA_ATAPI) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400569 SET_FARVAR(seg, param_far->dpte, SEGOFF(SEG_LOW, (u32)&DefaultDPTE));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500570
Gleb Natapovf77e1792010-12-23 11:29:35 +0200571 // Fill in dpte
572 struct atadrive_s *adrive_g = container_of(
573 drive_g, struct atadrive_s, drive);
574 struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf);
575 u8 slave = GET_GLOBAL(adrive_g->slave);
576 u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2);
577 u8 irq = GET_GLOBALFLAT(chan_gf->irq);
578 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1);
579 bdf = GET_GLOBALFLAT(chan_gf->pci_bdf);
580 device_path = slave;
Gleb Natapovf5154e22011-01-10 10:50:27 +0200581 channel = GET_GLOBALFLAT(chan_gf->chanid);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500582
Gleb Natapovf77e1792010-12-23 11:29:35 +0200583 u16 options = 0;
584 if (type == DTYPE_ATA) {
585 u8 translation = GET_GLOBAL(drive_g->translation);
586 if (translation != TRANSLATION_NONE) {
587 options |= 1<<3; // CHS translation
588 if (translation == TRANSLATION_LBA)
589 options |= 1<<9;
590 if (translation == TRANSLATION_RECHS)
591 options |= 3<<9;
592 }
593 } else {
594 // ATAPI
595 options |= 1<<5; // removable device
596 options |= 1<<6; // atapi device
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400597 }
Gleb Natapovf77e1792010-12-23 11:29:35 +0200598 options |= 1<<4; // lba translation
599 if (CONFIG_ATA_PIO32)
600 options |= 1<<7;
601
Kevin O'Connord3140832012-05-13 22:46:12 -0400602 SET_LOW(DefaultDPTE.iobase1, iobase1);
603 SET_LOW(DefaultDPTE.iobase2, iobase2 + ATA_CB_DC);
604 SET_LOW(DefaultDPTE.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
605 | ATA_CB_DH_LBA));
606 SET_LOW(DefaultDPTE.unused, 0xcb);
607 SET_LOW(DefaultDPTE.irq, irq);
608 SET_LOW(DefaultDPTE.blkcount, 1);
609 SET_LOW(DefaultDPTE.dma, 0);
610 SET_LOW(DefaultDPTE.pio, 0);
611 SET_LOW(DefaultDPTE.options, options);
612 SET_LOW(DefaultDPTE.reserved, 0);
613 SET_LOW(DefaultDPTE.revision, 0x11);
Gleb Natapovf77e1792010-12-23 11:29:35 +0200614
Kevin O'Connord3140832012-05-13 22:46:12 -0400615 u8 sum = checksum_far(SEG_LOW, &DefaultDPTE, 15);
616 SET_LOW(DefaultDPTE.checksum, -sum);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500617 } else {
Bruce Rogers9474a352012-10-25 13:48:16 -0600618 SET_FARVAR(seg, param_far->dpte.segoff, 0xffffffff);
Gleb Natapovf77e1792010-12-23 11:29:35 +0200619 bdf = GET_GLOBAL(drive_g->cntl_id);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500620 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500621
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400622 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500623 disk_ret(regs, DISK_RET_SUCCESS);
624 return;
625 }
626
627 // EDD 3.x
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400628 SET_FARVAR(seg, param_far->key, 0xbedd);
629 SET_FARVAR(seg, param_far->dpi_length, t13 ? 44 : 36);
630 SET_FARVAR(seg, param_far->reserved1, 0);
631 SET_FARVAR(seg, param_far->reserved2, 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500632
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500633 if (bdf != -1) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400634 SET_FARVAR(seg, param_far->host_bus[0], 'P');
635 SET_FARVAR(seg, param_far->host_bus[1], 'C');
636 SET_FARVAR(seg, param_far->host_bus[2], 'I');
637 SET_FARVAR(seg, param_far->host_bus[3], ' ');
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400638
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500639 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
640 | (pci_bdf_to_fn(bdf) << 16));
Gleb Natapovf5154e22011-01-10 10:50:27 +0200641 if (t13)
642 path |= channel << 24;
643
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400644 SET_FARVAR(seg, param_far->iface_path, path);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500645 } else {
646 // ISA
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400647 SET_FARVAR(seg, param_far->host_bus[0], 'I');
648 SET_FARVAR(seg, param_far->host_bus[1], 'S');
649 SET_FARVAR(seg, param_far->host_bus[2], 'A');
650 SET_FARVAR(seg, param_far->host_bus[3], ' ');
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500651
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400652 SET_FARVAR(seg, param_far->iface_path, iobase1);
Kevin O'Connor4ccb2312009-12-05 11:25:09 -0500653 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400654
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100655 if (type != DTYPE_VIRTIO_BLK) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400656 SET_FARVAR(seg, param_far->iface_type[0], 'A');
657 SET_FARVAR(seg, param_far->iface_type[1], 'T');
658 SET_FARVAR(seg, param_far->iface_type[2], 'A');
659 SET_FARVAR(seg, param_far->iface_type[3], ' ');
Gleb Natapovf77e1792010-12-23 11:29:35 +0200660 } else {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400661 SET_FARVAR(seg, param_far->iface_type[0], 'S');
662 SET_FARVAR(seg, param_far->iface_type[1], 'C');
663 SET_FARVAR(seg, param_far->iface_type[2], 'S');
664 SET_FARVAR(seg, param_far->iface_type[3], 'I');
Gleb Natapovf77e1792010-12-23 11:29:35 +0200665 }
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400666 SET_FARVAR(seg, param_far->iface_type[4], ' ');
667 SET_FARVAR(seg, param_far->iface_type[5], ' ');
668 SET_FARVAR(seg, param_far->iface_type[6], ' ');
669 SET_FARVAR(seg, param_far->iface_type[7], ' ');
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500670
Gleb Natapovf5154e22011-01-10 10:50:27 +0200671 if (t13) {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400672 SET_FARVAR(seg, param_far->t13.device_path[0], device_path);
673 SET_FARVAR(seg, param_far->t13.device_path[1], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500674
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400675 SET_FARVAR(seg, param_far->t13.checksum
676 , -checksum_far(seg, (void*)param_far+30, 43));
Gleb Natapovf5154e22011-01-10 10:50:27 +0200677 } else {
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400678 SET_FARVAR(seg, param_far->phoenix.device_path, device_path);
Gleb Natapovf5154e22011-01-10 10:50:27 +0200679
Kevin O'Connorcd8dcbe2012-05-24 22:52:39 -0400680 SET_FARVAR(seg, param_far->phoenix.checksum
681 , -checksum_far(seg, (void*)param_far+30, 35));
Gleb Natapovf5154e22011-01-10 10:50:27 +0200682 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400683
684 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500685}
686
687// IBM/MS extended media change
688static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400689disk_1349(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500690{
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400691 if (regs->dl < EXTSTART_CD) {
692 // Always success for HD
693 disk_ret(regs, DISK_RET_SUCCESS);
694 return;
695 }
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500696 set_invalid(regs);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400697 // always send changed ??
698 regs->ah = DISK_RET_ECHANGED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500699}
700
701static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400702disk_134e01(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500703{
704 disk_ret(regs, DISK_RET_SUCCESS);
705}
706
707static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400708disk_134e03(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500709{
710 disk_ret(regs, DISK_RET_SUCCESS);
711}
712
713static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400714disk_134e04(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500715{
716 disk_ret(regs, DISK_RET_SUCCESS);
717}
718
719static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400720disk_134e06(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500721{
722 disk_ret(regs, DISK_RET_SUCCESS);
723}
724
725static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400726disk_134eXX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500727{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500728 disk_ret(regs, DISK_RET_EPARAM);
729}
730
731// IBM/MS set hardware configuration
732static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400733disk_134e(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500734{
735 switch (regs->al) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400736 case 0x01: disk_134e01(regs, drive_g); break;
737 case 0x03: disk_134e03(regs, drive_g); break;
738 case 0x04: disk_134e04(regs, drive_g); break;
739 case 0x06: disk_134e06(regs, drive_g); break;
740 default: disk_134eXX(regs, drive_g); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500741 }
742}
743
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400744static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400745disk_13XX(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500746{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500747 disk_ret_unimplemented(regs, DISK_RET_EPARAM);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500748}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500749
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400750static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400751disk_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500752{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500753 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500754
755 // clear completion flag
756 SET_BDA(disk_interrupt_flag, 0);
757
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500758 switch (regs->ah) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400759 case 0x00: disk_1300(regs, drive_g); break;
760 case 0x01: disk_1301(regs, drive_g); break;
761 case 0x02: disk_1302(regs, drive_g); break;
762 case 0x03: disk_1303(regs, drive_g); break;
763 case 0x04: disk_1304(regs, drive_g); break;
764 case 0x05: disk_1305(regs, drive_g); break;
765 case 0x08: disk_1308(regs, drive_g); break;
766 case 0x09: disk_1309(regs, drive_g); break;
767 case 0x0c: disk_130c(regs, drive_g); break;
768 case 0x0d: disk_130d(regs, drive_g); break;
769 case 0x10: disk_1310(regs, drive_g); break;
770 case 0x11: disk_1311(regs, drive_g); break;
771 case 0x14: disk_1314(regs, drive_g); break;
772 case 0x15: disk_1315(regs, drive_g); break;
773 case 0x16: disk_1316(regs, drive_g); break;
774 case 0x41: disk_1341(regs, drive_g); break;
775 case 0x42: disk_1342(regs, drive_g); break;
776 case 0x43: disk_1343(regs, drive_g); break;
777 case 0x44: disk_1344(regs, drive_g); break;
778 case 0x45: disk_1345(regs, drive_g); break;
779 case 0x46: disk_1346(regs, drive_g); break;
780 case 0x47: disk_1347(regs, drive_g); break;
781 case 0x48: disk_1348(regs, drive_g); break;
782 case 0x49: disk_1349(regs, drive_g); break;
783 case 0x4e: disk_134e(regs, drive_g); break;
784 default: disk_13XX(regs, drive_g); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500785 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500786}
787
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400788static void
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400789floppy_13(struct bregs *regs, struct drive_s *drive_g)
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400790{
791 // Only limited commands are supported on floppies.
792 switch (regs->ah) {
793 case 0x00:
794 case 0x01:
795 case 0x02:
796 case 0x03:
797 case 0x04:
798 case 0x05:
799 case 0x08:
800 case 0x15:
801 case 0x16:
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400802 disk_13(regs, drive_g);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400803 break;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400804 default: disk_13XX(regs, drive_g); break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400805 }
806}
807
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500808
809/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500810 * Entry points
811 ****************************************************************/
812
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500813static void
Kevin O'Connor707298a2009-08-11 22:27:51 -0400814handle_legacy_disk(struct bregs *regs, u8 extdrive)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500815{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400816 if (! CONFIG_DRIVES) {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400817 // XXX - support handle_1301 anyway?
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500818 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500819 return;
820 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500821
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400822 if (extdrive < EXTSTART_HD) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400823 struct drive_s *drive_g = getDrive(EXTTYPE_FLOPPY, extdrive);
824 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400825 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400826 floppy_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400827 return;
828 }
829
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400830 struct drive_s *drive_g;
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400831 if (extdrive >= EXTSTART_CD)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400832 drive_g = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400833 else
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400834 drive_g = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
835 if (!drive_g)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400836 goto fail;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400837 disk_13(regs, drive_g);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400838 return;
839
840fail:
841 // XXX - support 1301/1308/1315 anyway?
842 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500843}
844
Kevin O'Connor19786762008-03-05 21:09:59 -0500845void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500846handle_40(struct bregs *regs)
847{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400848 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500849 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500850}
851
852// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500853void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500854handle_13(struct bregs *regs)
855{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400856 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400857 u8 extdrive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500858
Kevin O'Connordfa16502008-03-22 20:13:08 -0400859 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500860 if (regs->ah == 0x4b) {
861 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400862 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500863 }
Kevin O'Connord3140832012-05-13 22:46:12 -0400864 if (GET_LOW(CDEmu.active)) {
865 u8 emudrive = GET_LOW(CDEmu.emulated_extdrive);
Kevin O'Connor707298a2009-08-11 22:27:51 -0400866 if (extdrive == emudrive) {
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400867 // Access to an emulated drive.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500868 struct drive_s *cdemu_g;
869 cdemu_g = GLOBALFLAT2GLOBAL(GET_GLOBAL(cdemu_drive_gf));
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400870 if (regs->ah > 0x16) {
871 // Only old-style commands supported.
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500872 disk_13XX(regs, cdemu_g);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400873 return;
874 }
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500875 disk_13(regs, cdemu_g);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400876 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500877 }
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400878 if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400879 // Adjust id to make room for emulated drive.
Kevin O'Connor707298a2009-08-11 22:27:51 -0400880 extdrive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500881 }
882 }
Kevin O'Connor707298a2009-08-11 22:27:51 -0400883 handle_legacy_disk(regs, extdrive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500884}
885
886// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500887void VISIBLE16
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400888handle_76(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500889{
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400890 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400891 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400892 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500893}
Kevin O'Connor30853762009-01-17 18:49:20 -0500894
895// Old Fixed Disk Parameter Table (newer tables are in the ebda).
896struct fdpt_s OldFDPT VAR16FIXED(0xe401);