blob: dfaa24fe5f8cbcdfef9128b0408fd357d4ee2d31 [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'Connor7f343092009-01-01 18:31:11 -050043static int
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050044__send_disk_op(struct disk_op_s *op_far, u16 op_seg)
Kevin O'Connor4524bf72008-12-31 00:31:03 -050045{
Kevin O'Connor7f343092009-01-01 18:31:11 -050046 struct disk_op_s dop;
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050047 memcpy_far(GET_SEG(SS), &dop
48 , op_seg, op_far
49 , sizeof(dop));
Kevin O'Connor7f343092009-01-01 18:31:11 -050050
Kevin O'Connor4524bf72008-12-31 00:31:03 -050051 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 -050052 , dop.driveid, (u32)dop.lba, dop.buf_fl
Kevin O'Connor7f343092009-01-01 18:31:11 -050053 , dop.count, dop.command);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050054
55 irq_enable();
56
57 int status;
Kevin O'Connor7f343092009-01-01 18:31:11 -050058 if (dop.command == CMD_CDEMU_READ)
59 status = cdrom_read_512(&dop);
60 else if (dop.command == CMD_CDROM_READ)
61 status = cdrom_read(&dop);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050062 else
Kevin O'Connor7f343092009-01-01 18:31:11 -050063 status = ata_cmd_data(&dop);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050064
65 irq_disable();
66
67 return status;
68}
69
Kevin O'Connor7f343092009-01-01 18:31:11 -050070static int
71send_disk_op(struct disk_op_s *op)
72{
Kevin O'Connor0d9e6732009-01-17 21:54:16 -050073 if (! CONFIG_ATA)
74 return -1;
75
Kevin O'Connor7f343092009-01-01 18:31:11 -050076 return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
77}
78
Kevin O'Connore43df9e2008-03-01 22:16:32 -050079static void
80basic_access(struct bregs *regs, u8 device, u16 command)
81{
Kevin O'Connor4524bf72008-12-31 00:31:03 -050082 struct disk_op_s dop;
83 dop.lba = 0;
84 dop.driveid = device;
Kevin O'Connor609da232008-12-28 23:18:57 -050085 u8 type = GET_GLOBAL(ATA.devices[device].type);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -040086 u16 nlc, nlh, nlspt;
87 if (type == ATA_TYPE_ATA) {
Kevin O'Connor609da232008-12-28 23:18:57 -050088 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
89 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
90 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050091 dop.command = command;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -040092 } else {
93 // Must be cd emulation.
Kevin O'Connor4a16ef62008-12-31 00:09:28 -050094 u16 ebda_seg = get_ebda_seg();
95 nlc = GET_EBDA2(ebda_seg, cdemu.cylinders);
96 nlh = GET_EBDA2(ebda_seg, cdemu.heads);
97 nlspt = GET_EBDA2(ebda_seg, cdemu.spt);
Kevin O'Connor4524bf72008-12-31 00:31:03 -050098 dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) * 4;
99 dop.command = CMD_CDEMU_READ;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400100 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500101
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500102 dop.count = regs->al;
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -0400103 u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
104 u16 sector = regs->cl & 0x3f;
105 u16 head = regs->dh;
106
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500107 if (dop.count > 128 || dop.count == 0 || sector == 0) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400108 dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -0400109 , regs->ah);
110 disk_ret(regs, DISK_RET_EPARAM);
111 return;
112 }
113
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500114 // sanity check on cyl heads, sec
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400115 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400116 dprintf(1, "int13_harddisk: function %02x, parameters out of"
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500117 " range %04x/%04x/%04x!\n"
118 , regs->ah, cylinder, head, sector);
119 disk_ret(regs, DISK_RET_EPARAM);
120 return;
121 }
122
Kevin O'Connor3a049632008-03-11 11:48:04 -0400123 if (!command) {
124 // If verify or seek
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500125 disk_ret(regs, DISK_RET_SUCCESS);
126 return;
127 }
128
Kevin O'Connor049d5a22008-03-13 19:09:49 -0400129 // translate lchs to lba
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500130 dop.lba += (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
131 + (u32)sector - 1);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400132
133 u16 segment = regs->es;
134 u16 offset = regs->bx;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500135 dop.buf_fl = MAKE_FLATPTR(segment, offset);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400136
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500137 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400138
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500139 // Set nb of sector transferred
Kevin O'Connor609da232008-12-28 23:18:57 -0500140 regs->al = GET_EBDA(sector_count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500141
142 if (status != 0) {
Kevin O'Connorfe42eb22008-06-21 11:38:21 -0400143 dprintf(1, "int13_harddisk: function %02x, error %d!\n"
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400144 , regs->ah, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500145 disk_ret(regs, DISK_RET_EBADTRACK);
Kevin O'Connor6aa673d2009-01-02 12:36:46 -0500146 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500147 }
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500148 disk_ret(regs, DISK_RET_SUCCESS);
149}
150
Kevin O'Connored128492008-03-11 11:14:59 -0400151static void
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500152extended_access(struct bregs *regs, u8 device, u16 command)
153{
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500154 struct disk_op_s dop;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400155 // Get lba and check.
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500156 dop.lba = GET_INT13EXT(regs, lba);
157 dop.command = command;
158 dop.driveid = device;
Kevin O'Connor609da232008-12-28 23:18:57 -0500159 u8 type = GET_GLOBAL(ATA.devices[device].type);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500160 if (type == ATA_TYPE_ATA) {
161 if (dop.lba >= GET_GLOBAL(ATA.devices[device].sectors)) {
162 dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
163 , regs->ah);
164 disk_ret(regs, DISK_RET_EPARAM);
165 return;
166 }
167 } else {
168 dop.command = CMD_CDROM_READ;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500169 }
170
Kevin O'Connor3a049632008-03-11 11:48:04 -0400171 if (!command) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500172 // If verify or seek
173 disk_ret(regs, DISK_RET_SUCCESS);
174 return;
175 }
176
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400177 u16 segment = GET_INT13EXT(regs, segment);
178 u16 offset = GET_INT13EXT(regs, offset);
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500179 dop.buf_fl = MAKE_FLATPTR(segment, offset);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500180 dop.count = GET_INT13EXT(regs, count);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400181
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500182 int status = send_disk_op(&dop);
Kevin O'Connor74799df2008-03-12 20:49:07 -0400183
Kevin O'Connor609da232008-12-28 23:18:57 -0500184 SET_INT13EXT(regs, count, GET_EBDA(sector_count));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500185
186 if (status != 0) {
Kevin O'Connorfe42eb22008-06-21 11:38:21 -0400187 dprintf(1, "int13_harddisk: function %02x, error %d!\n"
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500188 , regs->ah, status);
189 disk_ret(regs, DISK_RET_EBADTRACK);
190 return;
191 }
192 disk_ret(regs, DISK_RET_SUCCESS);
193}
194
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500195
196/****************************************************************
197 * Hard Drive functions
198 ****************************************************************/
199
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500200// disk controller reset
201static void
202disk_1300(struct bregs *regs, u8 device)
203{
204 ata_reset(device);
205}
206
207// read disk status
208static void
209disk_1301(struct bregs *regs, u8 device)
210{
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400211 u8 v = GET_BDA(disk_last_status);
212 regs->ah = v;
213 set_cf(regs, v);
214 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500215}
216
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500217// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500218static void
219disk_1302(struct bregs *regs, u8 device)
220{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500221 basic_access(regs, device, ATA_CMD_READ_SECTORS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500222}
223
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500224// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500225static void
226disk_1303(struct bregs *regs, u8 device)
227{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500228 basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500229}
230
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500231// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500232static void
233disk_1304(struct bregs *regs, u8 device)
234{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500235 basic_access(regs, device, 0);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500236 // FIXME verify
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500237}
238
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500239// format disk track
240static void
241disk_1305(struct bregs *regs, u8 device)
242{
243 DISK_STUB(regs);
244}
245
246// read disk drive parameters
247static void
248disk_1308(struct bregs *regs, u8 device)
249{
250 // Get logical geometry from table
Kevin O'Connor609da232008-12-28 23:18:57 -0500251 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
252 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
253 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500254 u16 count = GET_BDA(hdcount);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500255
256 nlc = nlc - 2; /* 0 based , last sector not used */
257 regs->al = 0;
258 regs->ch = nlc & 0xff;
259 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
260 regs->dh = nlh - 1;
261 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
262
263 // FIXME should set ES & DI
264 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500265}
266
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500267// initialize drive parameters
268static void
269disk_1309(struct bregs *regs, u8 device)
270{
271 DISK_STUB(regs);
272}
273
274// seek to specified cylinder
275static void
276disk_130c(struct bregs *regs, u8 device)
277{
278 DISK_STUB(regs);
279}
280
281// alternate disk reset
282static void
283disk_130d(struct bregs *regs, u8 device)
284{
285 DISK_STUB(regs);
286}
287
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500288// check drive ready
289static void
290disk_1310(struct bregs *regs, u8 device)
291{
292 // should look at 40:8E also???
293
294 // Read the status from controller
Kevin O'Connor609da232008-12-28 23:18:57 -0500295 u8 status = inb(GET_GLOBAL(ATA.channels[device/2].iobase1) + ATA_CB_STAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500296 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
297 disk_ret(regs, DISK_RET_SUCCESS);
298 else
299 disk_ret(regs, DISK_RET_ENOTREADY);
300}
301
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500302// recalibrate
303static void
304disk_1311(struct bregs *regs, u8 device)
305{
306 DISK_STUB(regs);
307}
308
309// controller internal diagnostic
310static void
311disk_1314(struct bregs *regs, u8 device)
312{
313 DISK_STUB(regs);
314}
315
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500316// read disk drive size
317static void
318disk_1315(struct bregs *regs, u8 device)
319{
320 // Get logical geometry from table
Kevin O'Connor609da232008-12-28 23:18:57 -0500321 u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
322 u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
323 u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500324
325 // Compute sector count seen by int13
326 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
327 regs->cx = lba >> 16;
328 regs->dx = lba & 0xffff;
329
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500330 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500331 regs->ah = 3; // hard disk accessible
332}
333
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500334// IBM/MS installation check
335static void
336disk_1341(struct bregs *regs, u8 device)
337{
338 regs->bx = 0xaa55; // install check
339 regs->cx = 0x0007; // ext disk access and edd, removable supported
340 disk_ret(regs, DISK_RET_SUCCESS);
341 regs->ah = 0x30; // EDD 3.0
342}
343
344// IBM/MS extended read
345static void
346disk_1342(struct bregs *regs, u8 device)
347{
348 extended_access(regs, device, ATA_CMD_READ_SECTORS);
349}
350
351// IBM/MS extended write
352static void
353disk_1343(struct bregs *regs, u8 device)
354{
355 extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
356}
357
358// IBM/MS verify
359static void
360disk_1344(struct bregs *regs, u8 device)
361{
362 extended_access(regs, device, 0);
363}
364
365// IBM/MS lock/unlock drive
366static void
367disk_1345(struct bregs *regs, u8 device)
368{
369 // Always success for HD
370 disk_ret(regs, DISK_RET_SUCCESS);
371}
372
373// IBM/MS eject media
374static void
375disk_1346(struct bregs *regs, u8 device)
376{
377 // Volume Not Removable
378 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
379}
380
381// IBM/MS extended seek
382static void
383disk_1347(struct bregs *regs, u8 device)
384{
385 extended_access(regs, device, 0);
386}
387
388// IBM/MS get drive parameters
389static void
390disk_1348(struct bregs *regs, u8 device)
391{
392 u16 size = GET_INT13DPT(regs, size);
393
394 // Buffer is too small
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400395 if (size < 26) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500396 disk_ret(regs, DISK_RET_EPARAM);
397 return;
398 }
399
400 // EDD 1.x
401
Kevin O'Connor609da232008-12-28 23:18:57 -0500402 u8 type = GET_GLOBAL(ATA.devices[device].type);
403 u16 npc = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
404 u16 nph = GET_GLOBAL(ATA.devices[device].pchs.heads);
405 u16 npspt = GET_GLOBAL(ATA.devices[device].pchs.spt);
406 u64 lba = GET_GLOBAL(ATA.devices[device].sectors);
407 u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500408
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400409 dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
410 , size, type, npc, nph, npspt, (u32)lba, blksize);
411
412 SET_INT13DPT(regs, size, 26);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500413 if (type == ATA_TYPE_ATA) {
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400414 if (lba > (u64)npspt*nph*0x3fff) {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500415 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
416 SET_INT13DPT(regs, cylinders, 0x3fff);
417 } else {
418 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
419 SET_INT13DPT(regs, cylinders, (u32)npc);
420 }
421 SET_INT13DPT(regs, heads, (u32)nph);
422 SET_INT13DPT(regs, spt, (u32)npspt);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400423 SET_INT13DPT(regs, sector_count, lba);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500424 } else {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500425 // ATAPI
426 // 0x74 = removable, media change, lockable, max values
427 SET_INT13DPT(regs, infos, 0x74);
428 SET_INT13DPT(regs, cylinders, 0xffffffff);
429 SET_INT13DPT(regs, heads, 0xffffffff);
430 SET_INT13DPT(regs, spt, 0xffffffff);
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400431 SET_INT13DPT(regs, sector_count, (u64)-1);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500432 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500433 SET_INT13DPT(regs, blksize, blksize);
434
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400435 if (size < 30) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500436 disk_ret(regs, DISK_RET_SUCCESS);
437 return;
438 }
439
440 // EDD 2.x
441
Kevin O'Connor08815372008-12-29 21:16:31 -0500442 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400443 SET_INT13DPT(regs, size, 30);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500444
Kevin O'Connor08815372008-12-29 21:16:31 -0500445 SET_INT13DPT(regs, dpte_segment, ebda_seg);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500446 SET_INT13DPT(regs, dpte_offset
Kevin O'Connor609da232008-12-28 23:18:57 -0500447 , offsetof(struct extended_bios_data_area_s, dpte));
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500448
449 // Fill in dpte
450 u8 channel = device / 2;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400451 u8 slave = device % 2;
Kevin O'Connor609da232008-12-28 23:18:57 -0500452 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
453 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
454 u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
455 u8 mode = GET_GLOBAL(ATA.devices[device].mode);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500456
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400457 u16 options = 0;
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500458 if (type == ATA_TYPE_ATA) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500459 u8 translation = GET_GLOBAL(ATA.devices[device].translation);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400460 if (translation != ATA_TRANSLATION_NONE) {
461 options |= 1<<3; // CHS translation
462 if (translation == ATA_TRANSLATION_LBA)
463 options |= 1<<9;
464 if (translation == ATA_TRANSLATION_RECHS)
465 options |= 3<<9;
466 }
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500467 } else {
468 // ATAPI
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400469 options |= 1<<5; // removable device
470 options |= 1<<6; // atapi device
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500471 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400472 options |= 1<<4; // lba translation
473 if (mode == ATA_MODE_PIO32)
474 options |= 1<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500475
Kevin O'Connor08815372008-12-29 21:16:31 -0500476 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
477 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
478 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
479 | ATA_CB_DH_LBA));
480 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
481 SET_EBDA2(ebda_seg, dpte.irq, irq);
482 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
483 SET_EBDA2(ebda_seg, dpte.dma, 0);
484 SET_EBDA2(ebda_seg, dpte.pio, 0);
485 SET_EBDA2(ebda_seg, dpte.options, options);
486 SET_EBDA2(ebda_seg, dpte.reserved, 0);
487 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500488
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500489 u8 sum = checksum_far(
490 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
491 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500492
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400493 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500494 disk_ret(regs, DISK_RET_SUCCESS);
495 return;
496 }
497
498 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500499 SET_INT13DPT(regs, key, 0xbedd);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400500 SET_INT13DPT(regs, dpi_length, 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500501 SET_INT13DPT(regs, reserved1, 0);
502 SET_INT13DPT(regs, reserved2, 0);
503
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400504 SET_INT13DPT(regs, host_bus[0], 'P');
505 SET_INT13DPT(regs, host_bus[1], 'C');
506 SET_INT13DPT(regs, host_bus[2], 'I');
507 SET_INT13DPT(regs, host_bus[3], 0);
508
Kevin O'Connor609da232008-12-28 23:18:57 -0500509 u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400510 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
511 | (pci_bdf_to_fn(bdf) << 16));
512 SET_INT13DPT(regs, iface_path, path);
513
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500514 SET_INT13DPT(regs, iface_type[0], 'A');
515 SET_INT13DPT(regs, iface_type[1], 'T');
516 SET_INT13DPT(regs, iface_type[2], 'A');
517 SET_INT13DPT(regs, iface_type[3], 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400518 SET_INT13DPT(regs, iface_type[4], 0);
519 SET_INT13DPT(regs, iface_type[5], 0);
520 SET_INT13DPT(regs, iface_type[6], 0);
521 SET_INT13DPT(regs, iface_type[7], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500522
Kevin O'Connor970a0322008-10-26 12:01:21 -0400523 SET_INT13DPT(regs, device_path, slave);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500524
Kevin O'Connor7d108212009-01-21 19:13:21 -0500525 SET_INT13DPT(regs, checksum
526 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400527
528 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500529}
530
531// IBM/MS extended media change
532static void
533disk_1349(struct bregs *regs, u8 device)
534{
535 // Always success for HD
536 disk_ret(regs, DISK_RET_SUCCESS);
537}
538
539static void
540disk_134e01(struct bregs *regs, u8 device)
541{
542 disk_ret(regs, DISK_RET_SUCCESS);
543}
544
545static void
546disk_134e03(struct bregs *regs, u8 device)
547{
548 disk_ret(regs, DISK_RET_SUCCESS);
549}
550
551static void
552disk_134e04(struct bregs *regs, u8 device)
553{
554 disk_ret(regs, DISK_RET_SUCCESS);
555}
556
557static void
558disk_134e06(struct bregs *regs, u8 device)
559{
560 disk_ret(regs, DISK_RET_SUCCESS);
561}
562
563static void
564disk_134eXX(struct bregs *regs, u8 device)
565{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500566 disk_ret(regs, DISK_RET_EPARAM);
567}
568
569// IBM/MS set hardware configuration
570static void
571disk_134e(struct bregs *regs, u8 device)
572{
573 switch (regs->al) {
574 case 0x01: disk_134e01(regs, device); break;
575 case 0x03: disk_134e03(regs, device); break;
576 case 0x04: disk_134e04(regs, device); break;
577 case 0x06: disk_134e06(regs, device); break;
578 default: disk_134eXX(regs, device); break;
579 }
580}
581
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500582void
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500583disk_13XX(struct bregs *regs, u8 device)
584{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500585 disk_ret(regs, DISK_RET_EPARAM);
586}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500587
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500588void
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500589disk_13(struct bregs *regs, u8 device)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500590{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500591 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500592
593 // clear completion flag
594 SET_BDA(disk_interrupt_flag, 0);
595
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500596 switch (regs->ah) {
597 case 0x00: disk_1300(regs, device); break;
598 case 0x01: disk_1301(regs, device); break;
599 case 0x02: disk_1302(regs, device); break;
600 case 0x03: disk_1303(regs, device); break;
601 case 0x04: disk_1304(regs, device); break;
602 case 0x05: disk_1305(regs, device); break;
603 case 0x08: disk_1308(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500604 case 0x09: disk_1309(regs, device); break;
605 case 0x0c: disk_130c(regs, device); break;
606 case 0x0d: disk_130d(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500607 case 0x10: disk_1310(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500608 case 0x11: disk_1311(regs, device); break;
609 case 0x14: disk_1314(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500610 case 0x15: disk_1315(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500611 case 0x41: disk_1341(regs, device); break;
612 case 0x42: disk_1342(regs, device); break;
613 case 0x43: disk_1343(regs, device); break;
614 case 0x44: disk_1344(regs, device); break;
615 case 0x45: disk_1345(regs, device); break;
616 case 0x46: disk_1346(regs, device); break;
617 case 0x47: disk_1347(regs, device); break;
618 case 0x48: disk_1348(regs, device); break;
619 case 0x49: disk_1349(regs, device); break;
620 case 0x4e: disk_134e(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500621 default: disk_13XX(regs, device); break;
622 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500623}
624
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500625
626/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500627 * Entry points
628 ****************************************************************/
629
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500630static u8
Kevin O'Connor180a9592008-03-04 22:50:53 -0500631get_device(struct bregs *regs, u8 iscd, u8 drive)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500632{
633 // basic check : device has to be defined
634 if (drive >= CONFIG_MAX_ATA_DEVICES) {
635 disk_ret(regs, DISK_RET_EPARAM);
636 return CONFIG_MAX_ATA_DEVICES;
637 }
638
639 // Get the ata channel
Kevin O'Connor609da232008-12-28 23:18:57 -0500640 u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500641
642 // basic check : device has to be valid
643 if (device >= CONFIG_MAX_ATA_DEVICES) {
644 disk_ret(regs, DISK_RET_EPARAM);
645 return CONFIG_MAX_ATA_DEVICES;
646 }
647
648 return device;
649}
650
651static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500652handle_legacy_disk(struct bregs *regs, u8 drive)
653{
654 if (drive < 0x80) {
655 floppy_13(regs, drive);
656 return;
657 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500658
659 if (! CONFIG_ATA) {
660 // XXX - old code had other disk access method.
661 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500662 return;
663 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500664
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500665 if (drive >= 0xe0) {
Kevin O'Connor180a9592008-03-04 22:50:53 -0500666 u8 device = get_device(regs, 1, drive - 0xe0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500667 if (device >= CONFIG_MAX_ATA_DEVICES)
668 return;
669 cdrom_13(regs, device);
670 return;
671 }
672
Kevin O'Connor180a9592008-03-04 22:50:53 -0500673 u8 device = get_device(regs, 0, drive - 0x80);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500674 if (device >= CONFIG_MAX_ATA_DEVICES)
675 return;
676 disk_13(regs, device);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500677}
678
Kevin O'Connor19786762008-03-05 21:09:59 -0500679void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500680handle_40(struct bregs *regs)
681{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400682 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500683 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500684}
685
686// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500687void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500688handle_13(struct bregs *regs)
689{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400690 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500691 u8 drive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500692
Kevin O'Connordfa16502008-03-22 20:13:08 -0400693 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500694 if (regs->ah == 0x4b) {
695 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400696 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500697 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500698 u16 ebda_seg = get_ebda_seg();
699 if (GET_EBDA2(ebda_seg, cdemu.active)) {
700 if (drive == GET_EBDA2(ebda_seg, cdemu.emulated_drive)) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500701 cdemu_13(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400702 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500703 }
Kevin O'Connor180a9592008-03-04 22:50:53 -0500704 if (drive < 0xe0)
705 drive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500706 }
707 }
708 handle_legacy_disk(regs, drive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500709}
710
711// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500712void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400713handle_76()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500714{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400715 debug_isr(DEBUG_ISR_76);
Kevin O'Connored128492008-03-11 11:14:59 -0400716 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400717 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500718}
Kevin O'Connor30853762009-01-17 18:49:20 -0500719
720// Old Fixed Disk Parameter Table (newer tables are in the ebda).
721struct fdpt_s OldFDPT VAR16FIXED(0xe401);