blob: 609beaad0378e2261fc54f1335c0d00ec05278af [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'Connor42337662009-08-10 00:06:37 -040015#include "atabits.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'Connor567e4e32008-04-05 11:37:51 -040026 SET_BDA(disk_last_status, code);
27 if (code)
Kevin O'Connor05600342009-01-02 13:10:58 -050028 __set_code_fail(regs, linecode, fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040029 else
30 set_code_success(regs);
31}
32
33static void
Kevin O'Connor05600342009-01-02 13:10:58 -050034__disk_stub(struct bregs *regs, int lineno, const char *fname)
Kevin O'Connor567e4e32008-04-05 11:37:51 -040035{
Kevin O'Connor05600342009-01-02 13:10:58 -050036 __debug_stub(regs, lineno, fname);
37 __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -040038}
39
Kevin O'Connor05600342009-01-02 13:10:58 -050040#define DISK_STUB(regs) \
41 __disk_stub((regs), __LINE__, __func__)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050042
Kevin O'Connor1625a752009-08-09 13:08:21 -040043// Execute a "disk_op_s" request - this runs on a stack in the ebda.
Kevin O'Connor7f343092009-01-01 18:31:11 -050044static int
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050045__send_disk_op(struct disk_op_s *op_far, u16 op_seg)
Kevin O'Connor4524bf72008-12-31 00:31:03 -050046{
Kevin O'Connor7f343092009-01-01 18:31:11 -050047 struct disk_op_s dop;
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050048 memcpy_far(GET_SEG(SS), &dop
49 , op_seg, op_far
50 , sizeof(dop));
Kevin O'Connor7f343092009-01-01 18:31:11 -050051
Kevin O'Connor4524bf72008-12-31 00:31:03 -050052 dprintf(DEBUG_HDL_13, "disk_op d=%d lba=%d buf=%p count=%d cmd=%d\n"
Kevin O'Connor35ae7262009-01-19 15:44:44 -050053 , dop.driveid, (u32)dop.lba, dop.buf_fl
Kevin O'Connor7f343092009-01-01 18:31:11 -050054 , dop.count, dop.command);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050055
56 irq_enable();
57
Kevin O'Connor3f6c2782009-08-09 19:17:11 -040058 int status = 0;
59 u8 type = GET_GLOBAL(ATA.devices[dop.driveid].type);
Kevin O'Connor42337662009-08-10 00:06:37 -040060 if (type == DTYPE_ATA)
Kevin O'Connor3f6c2782009-08-09 19:17:11 -040061 status = process_ata_op(&dop);
Kevin O'Connor42337662009-08-10 00:06:37 -040062 else if (type == DTYPE_ATAPI)
Kevin O'Connor3f6c2782009-08-09 19:17:11 -040063 status = process_atapi_op(&dop);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050064
65 irq_disable();
66
Kevin O'Connor1625a752009-08-09 13:08:21 -040067 // Update count with total sectors transferred.
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -040068 SET_FARVAR(op_seg, op_far->count, dop.count);
Kevin O'Connor1625a752009-08-09 13:08:21 -040069
70 if (status)
71 dprintf(1, "disk_op cmd %d error %d!\n", dop.command, status);
72
Kevin O'Connor4524bf72008-12-31 00:31:03 -050073 return status;
74}
75
Kevin O'Connor1625a752009-08-09 13:08:21 -040076// Execute a "disk_op_s" request by jumping to a stack in the ebda.
Kevin O'Connor7f343092009-01-01 18:31:11 -050077static int
78send_disk_op(struct disk_op_s *op)
79{
Kevin O'Connor0d9e6732009-01-17 21:54:16 -050080 if (! CONFIG_ATA)
81 return -1;
82
Kevin O'Connor7f343092009-01-01 18:31:11 -050083 return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
84}
85
Kevin O'Connor1625a752009-08-09 13:08:21 -040086// Obtain the requested disk lba from an old-style chs request.
87static int
Kevin O'Connorb68ac712009-08-09 17:25:19 -040088legacy_lba(struct bregs *regs, u16 lchs_seg, struct chs_s *lchs_far)
Kevin O'Connore43df9e2008-03-01 22:16:32 -050089{
Kevin O'Connorb68ac712009-08-09 17:25:19 -040090 u8 count = regs->al;
Kevin O'Connor1625a752009-08-09 13:08:21 -040091 u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
92 u16 sector = regs->cl & 0x3f;
93 u16 head = regs->dh;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050094
Kevin O'Connorb68ac712009-08-09 17:25:19 -040095 if (count > 128 || count == 0 || sector == 0) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040096 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040097 , regs->ah);
98 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -040099 return -1;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -0400100 }
101
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400102 u16 nlc = GET_FARVAR(lchs_seg, lchs_far->cylinders);
103 u16 nlh = GET_FARVAR(lchs_seg, lchs_far->heads);
104 u16 nlspt = GET_FARVAR(lchs_seg, lchs_far->spt);
105
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500106 // sanity check on cyl heads, sec
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400107 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400108 dprintf(1, "int13_harddisk: function %02x, parameters out of"
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500109 " range %04x/%04x/%04x!\n"
110 , regs->ah, cylinder, head, sector);
111 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connor1625a752009-08-09 13:08:21 -0400112 return -1;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500113 }
114
Kevin O'Connor049d5a22008-03-13 19:09:49 -0400115 // translate lchs to lba
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400116 return (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
117 + (u32)sector - 1);
Kevin O'Connor1625a752009-08-09 13:08:21 -0400118}
119
120// Perform read/write/verify using old-style chs accesses
121static void
122basic_access(struct bregs *regs, u8 device, u16 command)
123{
124 struct disk_op_s dop;
125 dop.driveid = device;
126 dop.command = command;
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400127 int lba = legacy_lba(regs, get_global_seg(), &ATA.devices[device].lchs);
128 if (lba < 0)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400129 return;
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400130 dop.lba = lba;
131 dop.count = regs->al;
132 dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400133
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500134 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400135
Kevin O'Connor1625a752009-08-09 13:08:21 -0400136 regs->al = dop.count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500137
Kevin O'Connor1625a752009-08-09 13:08:21 -0400138 if (status) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500139 disk_ret(regs, DISK_RET_EBADTRACK);
Kevin O'Connor6aa673d2009-01-02 12:36:46 -0500140 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500141 }
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500142 disk_ret(regs, DISK_RET_SUCCESS);
143}
144
Kevin O'Connor1625a752009-08-09 13:08:21 -0400145// Perform cdemu read/verify
146void
147cdemu_access(struct bregs *regs, u8 device, u16 command)
148{
149 struct disk_op_s dop;
150 dop.driveid = device;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400151 dop.command = command;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400152 u16 ebda_seg = get_ebda_seg();
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400153 int vlba = legacy_lba(
154 regs, ebda_seg
155 , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
156 if (vlba < 0)
Kevin O'Connor1625a752009-08-09 13:08:21 -0400157 return;
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400158 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
159 u8 count = regs->al;
160 u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
161 u8 *dest_far = (void*)(regs->bx+0);
162 regs->al = 0;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400163
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400164 if (vlba & 3) {
165 dop.count = 1;
166 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
167 int status = send_disk_op(&dop);
168 if (status)
169 goto fail;
170 u8 thiscount = 4 - (vlba & 3);
171 if (thiscount > count)
172 thiscount = count;
173 count -= thiscount;
174 memcpy_far(regs->es, dest_far
175 , ebda_seg, cdbuf_far + (vlba & 3) * 512
176 , thiscount * 512);
177 dest_far += thiscount * 512;
178 regs->al += thiscount;
179 dop.lba++;
Kevin O'Connor1625a752009-08-09 13:08:21 -0400180 }
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400181
182 if (count > 3) {
183 dop.count = count / 4;
184 dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
185 int status = send_disk_op(&dop);
186 regs->al += dop.count * 4;
187 if (status)
188 goto fail;
189 u8 thiscount = count & ~3;
190 count &= 3;
191 dest_far += thiscount * 512;
192 dop.lba += thiscount / 4;
193 }
194
195 if (count) {
196 dop.count = 1;
197 dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
198 int status = send_disk_op(&dop);
199 if (status)
200 goto fail;
201 u8 thiscount = count;
202 memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
203 regs->al += thiscount;
204 }
205
Kevin O'Connor1625a752009-08-09 13:08:21 -0400206 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connorb68ac712009-08-09 17:25:19 -0400207 return;
208fail:
209 disk_ret(regs, DISK_RET_EBADTRACK);
Kevin O'Connor1625a752009-08-09 13:08:21 -0400210}
211
212// Perform read/write/verify using new-style "int13ext" accesses.
Kevin O'Connored128492008-03-11 11:14:59 -0400213static void
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500214extended_access(struct bregs *regs, u8 device, u16 command)
215{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500216 struct disk_op_s dop;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400217 // Get lba and check.
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500218 dop.lba = GET_INT13EXT(regs, lba);
219 dop.command = command;
220 dop.driveid = device;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400221 if (dop.lba >= GET_GLOBAL(ATA.devices[device].sectors)) {
222 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
223 , regs->ah);
224 disk_ret(regs, DISK_RET_EPARAM);
225 return;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500226 }
227
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400228 u16 segment = GET_INT13EXT(regs, segment);
229 u16 offset = GET_INT13EXT(regs, offset);
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500230 dop.buf_fl = MAKE_FLATPTR(segment, offset);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500231 dop.count = GET_INT13EXT(regs, count);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400232
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500233 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400234
Kevin O'Connor1625a752009-08-09 13:08:21 -0400235 SET_INT13EXT(regs, count, dop.count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500236
Kevin O'Connor1625a752009-08-09 13:08:21 -0400237 if (status) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500238 disk_ret(regs, DISK_RET_EBADTRACK);
239 return;
240 }
241 disk_ret(regs, DISK_RET_SUCCESS);
242}
243
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500244
245/****************************************************************
246 * Hard Drive functions
247 ****************************************************************/
248
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500249// disk controller reset
250static void
251disk_1300(struct bregs *regs, u8 device)
252{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400253 struct disk_op_s dop;
254 dop.driveid = device;
255 dop.command = CMD_RESET;
256 send_disk_op(&dop);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500257}
258
259// read disk status
260static void
261disk_1301(struct bregs *regs, u8 device)
262{
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400263 u8 v = GET_BDA(disk_last_status);
264 regs->ah = v;
265 set_cf(regs, v);
266 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500267}
268
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500269// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500270static void
271disk_1302(struct bregs *regs, u8 device)
272{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400273 basic_access(regs, device, CMD_READ);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500274}
275
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500276// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500277static void
278disk_1303(struct bregs *regs, u8 device)
279{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400280 basic_access(regs, device, CMD_WRITE);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500281}
282
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500283// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500284static void
285disk_1304(struct bregs *regs, u8 device)
286{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400287 basic_access(regs, device, CMD_VERIFY);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500288 // FIXME verify
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500289}
290
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500291// format disk track
292static void
293disk_1305(struct bregs *regs, u8 device)
294{
295 DISK_STUB(regs);
296}
297
298// read disk drive parameters
299static void
300disk_1308(struct bregs *regs, u8 device)
301{
302 // Get logical geometry from table
Kevin O'Connor609da232008-12-28 23:18:57 -0500303 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
304 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
305 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500306 u16 count = GET_BDA(hdcount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500307
308 nlc = nlc - 2; /* 0 based , last sector not used */
309 regs->al = 0;
310 regs->ch = nlc & 0xff;
311 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
312 regs->dh = nlh - 1;
313 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
314
315 // FIXME should set ES & DI
316 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500317}
318
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500319// initialize drive parameters
320static void
321disk_1309(struct bregs *regs, u8 device)
322{
323 DISK_STUB(regs);
324}
325
326// seek to specified cylinder
327static void
328disk_130c(struct bregs *regs, u8 device)
329{
330 DISK_STUB(regs);
331}
332
333// alternate disk reset
334static void
335disk_130d(struct bregs *regs, u8 device)
336{
337 DISK_STUB(regs);
338}
339
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500340// check drive ready
341static void
342disk_1310(struct bregs *regs, u8 device)
343{
344 // should look at 40:8E also???
345
Kevin O'Connor42337662009-08-10 00:06:37 -0400346 struct disk_op_s dop;
347 dop.driveid = device;
348 dop.command = CMD_ISREADY;
349 int status = send_disk_op(&dop);
350 if (status)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500351 disk_ret(regs, DISK_RET_ENOTREADY);
Kevin O'Connor42337662009-08-10 00:06:37 -0400352 else
353 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500354}
355
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500356// recalibrate
357static void
358disk_1311(struct bregs *regs, u8 device)
359{
360 DISK_STUB(regs);
361}
362
363// controller internal diagnostic
364static void
365disk_1314(struct bregs *regs, u8 device)
366{
367 DISK_STUB(regs);
368}
369
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500370// read disk drive size
371static void
372disk_1315(struct bregs *regs, u8 device)
373{
374 // Get logical geometry from table
Kevin O'Connor609da232008-12-28 23:18:57 -0500375 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
376 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
377 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500378
379 // Compute sector count seen by int13
380 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
381 regs->cx = lba >> 16;
382 regs->dx = lba & 0xffff;
383
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500384 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500385 regs->ah = 3; // hard disk accessible
386}
387
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500388// IBM/MS installation check
389static void
390disk_1341(struct bregs *regs, u8 device)
391{
392 regs->bx = 0xaa55; // install check
393 regs->cx = 0x0007; // ext disk access and edd, removable supported
394 disk_ret(regs, DISK_RET_SUCCESS);
395 regs->ah = 0x30; // EDD 3.0
396}
397
398// IBM/MS extended read
399static void
400disk_1342(struct bregs *regs, u8 device)
401{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400402 extended_access(regs, device, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500403}
404
405// IBM/MS extended write
406static void
407disk_1343(struct bregs *regs, u8 device)
408{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400409 extended_access(regs, device, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500410}
411
412// IBM/MS verify
413static void
414disk_1344(struct bregs *regs, u8 device)
415{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400416 extended_access(regs, device, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500417}
418
419// IBM/MS lock/unlock drive
420static void
421disk_1345(struct bregs *regs, u8 device)
422{
423 // Always success for HD
424 disk_ret(regs, DISK_RET_SUCCESS);
425}
426
427// IBM/MS eject media
428static void
429disk_1346(struct bregs *regs, u8 device)
430{
431 // Volume Not Removable
432 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
433}
434
435// IBM/MS extended seek
436static void
437disk_1347(struct bregs *regs, u8 device)
438{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400439 extended_access(regs, device, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500440}
441
442// IBM/MS get drive parameters
443static void
444disk_1348(struct bregs *regs, u8 device)
445{
446 u16 size = GET_INT13DPT(regs, size);
447
448 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400449 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500450 disk_ret(regs, DISK_RET_EPARAM);
451 return;
452 }
453
454 // EDD 1.x
455
Kevin O'Connor609da232008-12-28 23:18:57 -0500456 u8 type = GET_GLOBAL(ATA.devices[device].type);
457 u16 npc = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
458 u16 nph = GET_GLOBAL(ATA.devices[device].pchs.heads);
459 u16 npspt = GET_GLOBAL(ATA.devices[device].pchs.spt);
460 u64 lba = GET_GLOBAL(ATA.devices[device].sectors);
461 u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500462
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400463 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
464 , size, type, npc, nph, npspt, (u32)lba, blksize);
465
466 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400467 if (type == DTYPE_ATAPI) {
468 // 0x74 = removable, media change, lockable, max values
469 SET_INT13DPT(regs, infos, 0x74);
470 SET_INT13DPT(regs, cylinders, 0xffffffff);
471 SET_INT13DPT(regs, heads, 0xffffffff);
472 SET_INT13DPT(regs, spt, 0xffffffff);
473 SET_INT13DPT(regs, sector_count, (u64)-1);
474 } else {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400475 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500476 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
477 SET_INT13DPT(regs, cylinders, 0x3fff);
478 } else {
479 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
480 SET_INT13DPT(regs, cylinders, (u32)npc);
481 }
482 SET_INT13DPT(regs, heads, (u32)nph);
483 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400484 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500485 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500486 SET_INT13DPT(regs, blksize, blksize);
487
Kevin O'Connorb1144362009-08-11 20:43:38 -0400488 if (size < 30 || (type != DTYPE_ATA && type != DTYPE_ATAPI)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500489 disk_ret(regs, DISK_RET_SUCCESS);
490 return;
491 }
492
493 // EDD 2.x
494
Kevin O'Connor08815372008-12-29 21:16:31 -0500495 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400496 SET_INT13DPT(regs, size, 30);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500497
Kevin O'Connor08815372008-12-29 21:16:31 -0500498 SET_INT13DPT(regs, dpte_segment, ebda_seg);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500499 SET_INT13DPT(regs, dpte_offset
Kevin O'Connor609da232008-12-28 23:18:57 -0500500 , offsetof(struct extended_bios_data_area_s, dpte));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500501
502 // Fill in dpte
Kevin O'Connorb1144362009-08-11 20:43:38 -0400503 u8 ataid = GET_GLOBAL(ATA.devices[device].cntl_id);
504 u8 channel = ataid / 2;
505 u8 slave = ataid % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500506 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
507 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
508 u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500509
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400510 u16 options = 0;
Kevin O'Connor42337662009-08-10 00:06:37 -0400511 if (type == DTYPE_ATA) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500512 u8 translation = GET_GLOBAL(ATA.devices[device].translation);
Kevin O'Connor42337662009-08-10 00:06:37 -0400513 if (translation != TRANSLATION_NONE) {
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400514 options |= 1<<3; // CHS translation
Kevin O'Connor42337662009-08-10 00:06:37 -0400515 if (translation == TRANSLATION_LBA)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400516 options |= 1<<9;
Kevin O'Connor42337662009-08-10 00:06:37 -0400517 if (translation == TRANSLATION_RECHS)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400518 options |= 3<<9;
519 }
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500520 } else {
521 // ATAPI
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400522 options |= 1<<5; // removable device
523 options |= 1<<6; // atapi device
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500524 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400525 options |= 1<<4; // lba translation
Kevin O'Connor32945af2009-02-27 21:23:01 -0500526 if (CONFIG_ATA_PIO32)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400527 options |= 1<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500528
Kevin O'Connor08815372008-12-29 21:16:31 -0500529 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
530 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
531 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
532 | ATA_CB_DH_LBA));
533 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
534 SET_EBDA2(ebda_seg, dpte.irq, irq);
535 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
536 SET_EBDA2(ebda_seg, dpte.dma, 0);
537 SET_EBDA2(ebda_seg, dpte.pio, 0);
538 SET_EBDA2(ebda_seg, dpte.options, options);
539 SET_EBDA2(ebda_seg, dpte.reserved, 0);
540 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500541
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500542 u8 sum = checksum_far(
543 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
544 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500545
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400546 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500547 disk_ret(regs, DISK_RET_SUCCESS);
548 return;
549 }
550
551 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500552 SET_INT13DPT(regs, key, 0xbedd);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400553 SET_INT13DPT(regs, dpi_length, 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500554 SET_INT13DPT(regs, reserved1, 0);
555 SET_INT13DPT(regs, reserved2, 0);
556
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400557 SET_INT13DPT(regs, host_bus[0], 'P');
558 SET_INT13DPT(regs, host_bus[1], 'C');
559 SET_INT13DPT(regs, host_bus[2], 'I');
560 SET_INT13DPT(regs, host_bus[3], 0);
561
Kevin O'Connor609da232008-12-28 23:18:57 -0500562 u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400563 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
564 | (pci_bdf_to_fn(bdf) << 16));
565 SET_INT13DPT(regs, iface_path, path);
566
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500567 SET_INT13DPT(regs, iface_type[0], 'A');
568 SET_INT13DPT(regs, iface_type[1], 'T');
569 SET_INT13DPT(regs, iface_type[2], 'A');
570 SET_INT13DPT(regs, iface_type[3], 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400571 SET_INT13DPT(regs, iface_type[4], 0);
572 SET_INT13DPT(regs, iface_type[5], 0);
573 SET_INT13DPT(regs, iface_type[6], 0);
574 SET_INT13DPT(regs, iface_type[7], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500575
Kevin O'Connor970a0322008-10-26 12:01:21 -0400576 SET_INT13DPT(regs, device_path, slave);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500577
Kevin O'Connor7d108212009-01-21 19:13:21 -0500578 SET_INT13DPT(regs, checksum
579 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400580
581 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500582}
583
584// IBM/MS extended media change
585static void
586disk_1349(struct bregs *regs, u8 device)
587{
588 // Always success for HD
589 disk_ret(regs, DISK_RET_SUCCESS);
590}
591
592static void
593disk_134e01(struct bregs *regs, u8 device)
594{
595 disk_ret(regs, DISK_RET_SUCCESS);
596}
597
598static void
599disk_134e03(struct bregs *regs, u8 device)
600{
601 disk_ret(regs, DISK_RET_SUCCESS);
602}
603
604static void
605disk_134e04(struct bregs *regs, u8 device)
606{
607 disk_ret(regs, DISK_RET_SUCCESS);
608}
609
610static void
611disk_134e06(struct bregs *regs, u8 device)
612{
613 disk_ret(regs, DISK_RET_SUCCESS);
614}
615
616static void
617disk_134eXX(struct bregs *regs, u8 device)
618{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500619 disk_ret(regs, DISK_RET_EPARAM);
620}
621
622// IBM/MS set hardware configuration
623static void
624disk_134e(struct bregs *regs, u8 device)
625{
626 switch (regs->al) {
627 case 0x01: disk_134e01(regs, device); break;
628 case 0x03: disk_134e03(regs, device); break;
629 case 0x04: disk_134e04(regs, device); break;
630 case 0x06: disk_134e06(regs, device); break;
631 default: disk_134eXX(regs, device); break;
632 }
633}
634
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500635void
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500636disk_13XX(struct bregs *regs, u8 device)
637{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500638 disk_ret(regs, DISK_RET_EPARAM);
639}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500640
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500641void
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500642disk_13(struct bregs *regs, u8 device)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500643{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500644 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500645
646 // clear completion flag
647 SET_BDA(disk_interrupt_flag, 0);
648
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500649 switch (regs->ah) {
650 case 0x00: disk_1300(regs, device); break;
651 case 0x01: disk_1301(regs, device); break;
652 case 0x02: disk_1302(regs, device); break;
653 case 0x03: disk_1303(regs, device); break;
654 case 0x04: disk_1304(regs, device); break;
655 case 0x05: disk_1305(regs, device); break;
656 case 0x08: disk_1308(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500657 case 0x09: disk_1309(regs, device); break;
658 case 0x0c: disk_130c(regs, device); break;
659 case 0x0d: disk_130d(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500660 case 0x10: disk_1310(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500661 case 0x11: disk_1311(regs, device); break;
662 case 0x14: disk_1314(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500663 case 0x15: disk_1315(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500664 case 0x41: disk_1341(regs, device); break;
665 case 0x42: disk_1342(regs, device); break;
666 case 0x43: disk_1343(regs, device); break;
667 case 0x44: disk_1344(regs, device); break;
668 case 0x45: disk_1345(regs, device); break;
669 case 0x46: disk_1346(regs, device); break;
670 case 0x47: disk_1347(regs, device); break;
671 case 0x48: disk_1348(regs, device); break;
672 case 0x49: disk_1349(regs, device); break;
673 case 0x4e: disk_134e(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500674 default: disk_13XX(regs, device); break;
675 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500676}
677
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500678
679/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500680 * Entry points
681 ****************************************************************/
682
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500683static u8
Kevin O'Connor180a9592008-03-04 22:50:53 -0500684get_device(struct bregs *regs, u8 iscd, u8 drive)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500685{
686 // basic check : device has to be defined
687 if (drive >= CONFIG_MAX_ATA_DEVICES) {
688 disk_ret(regs, DISK_RET_EPARAM);
689 return CONFIG_MAX_ATA_DEVICES;
690 }
691
692 // Get the ata channel
Kevin O'Connor609da232008-12-28 23:18:57 -0500693 u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500694
695 // basic check : device has to be valid
696 if (device >= CONFIG_MAX_ATA_DEVICES) {
697 disk_ret(regs, DISK_RET_EPARAM);
698 return CONFIG_MAX_ATA_DEVICES;
699 }
700
701 return device;
702}
703
704static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500705handle_legacy_disk(struct bregs *regs, u8 drive)
706{
707 if (drive < 0x80) {
708 floppy_13(regs, drive);
709 return;
710 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500711
712 if (! CONFIG_ATA) {
713 // XXX - old code had other disk access method.
714 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500715 return;
716 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500717
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500718 if (drive >= 0xe0) {
Kevin O'Connor180a9592008-03-04 22:50:53 -0500719 u8 device = get_device(regs, 1, drive - 0xe0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500720 if (device >= CONFIG_MAX_ATA_DEVICES)
721 return;
722 cdrom_13(regs, device);
723 return;
724 }
725
Kevin O'Connor180a9592008-03-04 22:50:53 -0500726 u8 device = get_device(regs, 0, drive - 0x80);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500727 if (device >= CONFIG_MAX_ATA_DEVICES)
728 return;
729 disk_13(regs, device);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500730}
731
Kevin O'Connor19786762008-03-05 21:09:59 -0500732void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500733handle_40(struct bregs *regs)
734{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400735 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500736 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500737}
738
739// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500740void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500741handle_13(struct bregs *regs)
742{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400743 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500744 u8 drive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500745
Kevin O'Connordfa16502008-03-22 20:13:08 -0400746 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500747 if (regs->ah == 0x4b) {
748 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400749 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500750 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500751 u16 ebda_seg = get_ebda_seg();
752 if (GET_EBDA2(ebda_seg, cdemu.active)) {
Kevin O'Connor5c0c4b72009-02-17 20:40:59 -0500753 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
754 if (drive == emudrive) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500755 cdemu_13(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400756 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500757 }
Kevin O'Connor5c0c4b72009-02-17 20:40:59 -0500758 if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500759 drive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500760 }
761 }
762 handle_legacy_disk(regs, drive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500763}
764
765// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500766void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400767handle_76()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500768{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400769 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400770 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400771 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500772}
Kevin O'Connor30853762009-01-17 18:49:20 -0500773
774// Old Fixed Disk Parameter Table (newer tables are in the ebda).
775struct fdpt_s OldFDPT VAR16FIXED(0xe401);