blob: d012af73913ad5669a2cdb3f5cca98f0b4c1ce25 [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'Connor4524bf72008-12-31 00:31:03 -050015#include "atabits.h" // ATA_CB_STAT
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);
60 if (type == ATA_TYPE_ATA)
61 status = process_ata_op(&dop);
62 else if (type == ATA_TYPE_ATAPI)
63 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
346 // Read the status from controller
Kevin O'Connor609da232008-12-28 23:18:57 -0500347 u8 status = inb(GET_GLOBAL(ATA.channels[device/2].iobase1) + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500348 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
349 disk_ret(regs, DISK_RET_SUCCESS);
350 else
351 disk_ret(regs, DISK_RET_ENOTREADY);
352}
353
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500354// recalibrate
355static void
356disk_1311(struct bregs *regs, u8 device)
357{
358 DISK_STUB(regs);
359}
360
361// controller internal diagnostic
362static void
363disk_1314(struct bregs *regs, u8 device)
364{
365 DISK_STUB(regs);
366}
367
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500368// read disk drive size
369static void
370disk_1315(struct bregs *regs, u8 device)
371{
372 // Get logical geometry from table
Kevin O'Connor609da232008-12-28 23:18:57 -0500373 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
374 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
375 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500376
377 // Compute sector count seen by int13
378 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
379 regs->cx = lba >> 16;
380 regs->dx = lba & 0xffff;
381
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500382 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500383 regs->ah = 3; // hard disk accessible
384}
385
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500386// IBM/MS installation check
387static void
388disk_1341(struct bregs *regs, u8 device)
389{
390 regs->bx = 0xaa55; // install check
391 regs->cx = 0x0007; // ext disk access and edd, removable supported
392 disk_ret(regs, DISK_RET_SUCCESS);
393 regs->ah = 0x30; // EDD 3.0
394}
395
396// IBM/MS extended read
397static void
398disk_1342(struct bregs *regs, u8 device)
399{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400400 extended_access(regs, device, CMD_READ);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500401}
402
403// IBM/MS extended write
404static void
405disk_1343(struct bregs *regs, u8 device)
406{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400407 extended_access(regs, device, CMD_WRITE);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500408}
409
410// IBM/MS verify
411static void
412disk_1344(struct bregs *regs, u8 device)
413{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400414 extended_access(regs, device, CMD_VERIFY);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500415}
416
417// IBM/MS lock/unlock drive
418static void
419disk_1345(struct bregs *regs, u8 device)
420{
421 // Always success for HD
422 disk_ret(regs, DISK_RET_SUCCESS);
423}
424
425// IBM/MS eject media
426static void
427disk_1346(struct bregs *regs, u8 device)
428{
429 // Volume Not Removable
430 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
431}
432
433// IBM/MS extended seek
434static void
435disk_1347(struct bregs *regs, u8 device)
436{
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400437 extended_access(regs, device, CMD_SEEK);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500438}
439
440// IBM/MS get drive parameters
441static void
442disk_1348(struct bregs *regs, u8 device)
443{
444 u16 size = GET_INT13DPT(regs, size);
445
446 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400447 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500448 disk_ret(regs, DISK_RET_EPARAM);
449 return;
450 }
451
452 // EDD 1.x
453
Kevin O'Connor609da232008-12-28 23:18:57 -0500454 u8 type = GET_GLOBAL(ATA.devices[device].type);
455 u16 npc = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
456 u16 nph = GET_GLOBAL(ATA.devices[device].pchs.heads);
457 u16 npspt = GET_GLOBAL(ATA.devices[device].pchs.spt);
458 u64 lba = GET_GLOBAL(ATA.devices[device].sectors);
459 u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500460
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400461 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
462 , size, type, npc, nph, npspt, (u32)lba, blksize);
463
464 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500465 if (type == ATA_TYPE_ATA) {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400466 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500467 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
468 SET_INT13DPT(regs, cylinders, 0x3fff);
469 } else {
470 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
471 SET_INT13DPT(regs, cylinders, (u32)npc);
472 }
473 SET_INT13DPT(regs, heads, (u32)nph);
474 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400475 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500476 } else {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500477 // ATAPI
478 // 0x74 = removable, media change, lockable, max values
479 SET_INT13DPT(regs, infos, 0x74);
480 SET_INT13DPT(regs, cylinders, 0xffffffff);
481 SET_INT13DPT(regs, heads, 0xffffffff);
482 SET_INT13DPT(regs, spt, 0xffffffff);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400483 SET_INT13DPT(regs, sector_count, (u64)-1);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500484 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500485 SET_INT13DPT(regs, blksize, blksize);
486
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400487 if (size < 30) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500488 disk_ret(regs, DISK_RET_SUCCESS);
489 return;
490 }
491
492 // EDD 2.x
493
Kevin O'Connor08815372008-12-29 21:16:31 -0500494 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400495 SET_INT13DPT(regs, size, 30);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500496
Kevin O'Connor08815372008-12-29 21:16:31 -0500497 SET_INT13DPT(regs, dpte_segment, ebda_seg);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500498 SET_INT13DPT(regs, dpte_offset
Kevin O'Connor609da232008-12-28 23:18:57 -0500499 , offsetof(struct extended_bios_data_area_s, dpte));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500500
501 // Fill in dpte
502 u8 channel = device / 2;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400503 u8 slave = device % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500504 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
505 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
506 u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500507
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400508 u16 options = 0;
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500509 if (type == ATA_TYPE_ATA) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500510 u8 translation = GET_GLOBAL(ATA.devices[device].translation);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400511 if (translation != ATA_TRANSLATION_NONE) {
512 options |= 1<<3; // CHS translation
513 if (translation == ATA_TRANSLATION_LBA)
514 options |= 1<<9;
515 if (translation == ATA_TRANSLATION_RECHS)
516 options |= 3<<9;
517 }
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500518 } else {
519 // ATAPI
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400520 options |= 1<<5; // removable device
521 options |= 1<<6; // atapi device
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500522 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400523 options |= 1<<4; // lba translation
Kevin O'Connor32945af2009-02-27 21:23:01 -0500524 if (CONFIG_ATA_PIO32)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400525 options |= 1<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500526
Kevin O'Connor08815372008-12-29 21:16:31 -0500527 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
528 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
529 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
530 | ATA_CB_DH_LBA));
531 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
532 SET_EBDA2(ebda_seg, dpte.irq, irq);
533 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
534 SET_EBDA2(ebda_seg, dpte.dma, 0);
535 SET_EBDA2(ebda_seg, dpte.pio, 0);
536 SET_EBDA2(ebda_seg, dpte.options, options);
537 SET_EBDA2(ebda_seg, dpte.reserved, 0);
538 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500539
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500540 u8 sum = checksum_far(
541 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
542 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500543
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400544 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500545 disk_ret(regs, DISK_RET_SUCCESS);
546 return;
547 }
548
549 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500550 SET_INT13DPT(regs, key, 0xbedd);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400551 SET_INT13DPT(regs, dpi_length, 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500552 SET_INT13DPT(regs, reserved1, 0);
553 SET_INT13DPT(regs, reserved2, 0);
554
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400555 SET_INT13DPT(regs, host_bus[0], 'P');
556 SET_INT13DPT(regs, host_bus[1], 'C');
557 SET_INT13DPT(regs, host_bus[2], 'I');
558 SET_INT13DPT(regs, host_bus[3], 0);
559
Kevin O'Connor609da232008-12-28 23:18:57 -0500560 u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400561 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
562 | (pci_bdf_to_fn(bdf) << 16));
563 SET_INT13DPT(regs, iface_path, path);
564
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500565 SET_INT13DPT(regs, iface_type[0], 'A');
566 SET_INT13DPT(regs, iface_type[1], 'T');
567 SET_INT13DPT(regs, iface_type[2], 'A');
568 SET_INT13DPT(regs, iface_type[3], 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400569 SET_INT13DPT(regs, iface_type[4], 0);
570 SET_INT13DPT(regs, iface_type[5], 0);
571 SET_INT13DPT(regs, iface_type[6], 0);
572 SET_INT13DPT(regs, iface_type[7], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500573
Kevin O'Connor970a0322008-10-26 12:01:21 -0400574 SET_INT13DPT(regs, device_path, slave);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500575
Kevin O'Connor7d108212009-01-21 19:13:21 -0500576 SET_INT13DPT(regs, checksum
577 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400578
579 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500580}
581
582// IBM/MS extended media change
583static void
584disk_1349(struct bregs *regs, u8 device)
585{
586 // Always success for HD
587 disk_ret(regs, DISK_RET_SUCCESS);
588}
589
590static void
591disk_134e01(struct bregs *regs, u8 device)
592{
593 disk_ret(regs, DISK_RET_SUCCESS);
594}
595
596static void
597disk_134e03(struct bregs *regs, u8 device)
598{
599 disk_ret(regs, DISK_RET_SUCCESS);
600}
601
602static void
603disk_134e04(struct bregs *regs, u8 device)
604{
605 disk_ret(regs, DISK_RET_SUCCESS);
606}
607
608static void
609disk_134e06(struct bregs *regs, u8 device)
610{
611 disk_ret(regs, DISK_RET_SUCCESS);
612}
613
614static void
615disk_134eXX(struct bregs *regs, u8 device)
616{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500617 disk_ret(regs, DISK_RET_EPARAM);
618}
619
620// IBM/MS set hardware configuration
621static void
622disk_134e(struct bregs *regs, u8 device)
623{
624 switch (regs->al) {
625 case 0x01: disk_134e01(regs, device); break;
626 case 0x03: disk_134e03(regs, device); break;
627 case 0x04: disk_134e04(regs, device); break;
628 case 0x06: disk_134e06(regs, device); break;
629 default: disk_134eXX(regs, device); break;
630 }
631}
632
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500633void
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500634disk_13XX(struct bregs *regs, u8 device)
635{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500636 disk_ret(regs, DISK_RET_EPARAM);
637}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500638
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500639void
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500640disk_13(struct bregs *regs, u8 device)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500641{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500642 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500643
644 // clear completion flag
645 SET_BDA(disk_interrupt_flag, 0);
646
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500647 switch (regs->ah) {
648 case 0x00: disk_1300(regs, device); break;
649 case 0x01: disk_1301(regs, device); break;
650 case 0x02: disk_1302(regs, device); break;
651 case 0x03: disk_1303(regs, device); break;
652 case 0x04: disk_1304(regs, device); break;
653 case 0x05: disk_1305(regs, device); break;
654 case 0x08: disk_1308(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500655 case 0x09: disk_1309(regs, device); break;
656 case 0x0c: disk_130c(regs, device); break;
657 case 0x0d: disk_130d(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500658 case 0x10: disk_1310(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500659 case 0x11: disk_1311(regs, device); break;
660 case 0x14: disk_1314(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500661 case 0x15: disk_1315(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500662 case 0x41: disk_1341(regs, device); break;
663 case 0x42: disk_1342(regs, device); break;
664 case 0x43: disk_1343(regs, device); break;
665 case 0x44: disk_1344(regs, device); break;
666 case 0x45: disk_1345(regs, device); break;
667 case 0x46: disk_1346(regs, device); break;
668 case 0x47: disk_1347(regs, device); break;
669 case 0x48: disk_1348(regs, device); break;
670 case 0x49: disk_1349(regs, device); break;
671 case 0x4e: disk_134e(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500672 default: disk_13XX(regs, device); break;
673 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500674}
675
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500676
677/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500678 * Entry points
679 ****************************************************************/
680
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500681static u8
Kevin O'Connor180a9592008-03-04 22:50:53 -0500682get_device(struct bregs *regs, u8 iscd, u8 drive)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500683{
684 // basic check : device has to be defined
685 if (drive >= CONFIG_MAX_ATA_DEVICES) {
686 disk_ret(regs, DISK_RET_EPARAM);
687 return CONFIG_MAX_ATA_DEVICES;
688 }
689
690 // Get the ata channel
Kevin O'Connor609da232008-12-28 23:18:57 -0500691 u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500692
693 // basic check : device has to be valid
694 if (device >= CONFIG_MAX_ATA_DEVICES) {
695 disk_ret(regs, DISK_RET_EPARAM);
696 return CONFIG_MAX_ATA_DEVICES;
697 }
698
699 return device;
700}
701
702static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500703handle_legacy_disk(struct bregs *regs, u8 drive)
704{
705 if (drive < 0x80) {
706 floppy_13(regs, drive);
707 return;
708 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500709
710 if (! CONFIG_ATA) {
711 // XXX - old code had other disk access method.
712 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500713 return;
714 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500715
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500716 if (drive >= 0xe0) {
Kevin O'Connor180a9592008-03-04 22:50:53 -0500717 u8 device = get_device(regs, 1, drive - 0xe0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500718 if (device >= CONFIG_MAX_ATA_DEVICES)
719 return;
720 cdrom_13(regs, device);
721 return;
722 }
723
Kevin O'Connor180a9592008-03-04 22:50:53 -0500724 u8 device = get_device(regs, 0, drive - 0x80);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500725 if (device >= CONFIG_MAX_ATA_DEVICES)
726 return;
727 disk_13(regs, device);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500728}
729
Kevin O'Connor19786762008-03-05 21:09:59 -0500730void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500731handle_40(struct bregs *regs)
732{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400733 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500734 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500735}
736
737// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500738void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500739handle_13(struct bregs *regs)
740{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400741 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500742 u8 drive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500743
Kevin O'Connordfa16502008-03-22 20:13:08 -0400744 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500745 if (regs->ah == 0x4b) {
746 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400747 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500748 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500749 u16 ebda_seg = get_ebda_seg();
750 if (GET_EBDA2(ebda_seg, cdemu.active)) {
Kevin O'Connor5c0c4b72009-02-17 20:40:59 -0500751 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
752 if (drive == emudrive) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500753 cdemu_13(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400754 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500755 }
Kevin O'Connor5c0c4b72009-02-17 20:40:59 -0500756 if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500757 drive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500758 }
759 }
760 handle_legacy_disk(regs, drive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500761}
762
763// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500764void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400765handle_76()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500766{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400767 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400768 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400769 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500770}
Kevin O'Connor30853762009-01-17 18:49:20 -0500771
772// Old Fixed Disk Parameter Table (newer tables are in the ebda).
773struct fdpt_s OldFDPT VAR16FIXED(0xe401);