blob: 898fe499217c711dfdd06b9b0eb1a5a595ebbe6d [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);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500455
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400456 u16 options = 0;
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500457 if (type == ATA_TYPE_ATA) {
Kevin O'Connor609da232008-12-28 23:18:57 -0500458 u8 translation = GET_GLOBAL(ATA.devices[device].translation);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400459 if (translation != ATA_TRANSLATION_NONE) {
460 options |= 1<<3; // CHS translation
461 if (translation == ATA_TRANSLATION_LBA)
462 options |= 1<<9;
463 if (translation == ATA_TRANSLATION_RECHS)
464 options |= 3<<9;
465 }
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500466 } else {
467 // ATAPI
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400468 options |= 1<<5; // removable device
469 options |= 1<<6; // atapi device
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500470 }
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400471 options |= 1<<4; // lba translation
Kevin O'Connor32945af2009-02-27 21:23:01 -0500472 if (CONFIG_ATA_PIO32)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400473 options |= 1<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500474
Kevin O'Connor08815372008-12-29 21:16:31 -0500475 SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
476 SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
477 SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
478 | ATA_CB_DH_LBA));
479 SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
480 SET_EBDA2(ebda_seg, dpte.irq, irq);
481 SET_EBDA2(ebda_seg, dpte.blkcount, 1);
482 SET_EBDA2(ebda_seg, dpte.dma, 0);
483 SET_EBDA2(ebda_seg, dpte.pio, 0);
484 SET_EBDA2(ebda_seg, dpte.options, options);
485 SET_EBDA2(ebda_seg, dpte.reserved, 0);
486 SET_EBDA2(ebda_seg, dpte.revision, 0x11);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500487
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500488 u8 sum = checksum_far(
489 ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
490 SET_EBDA2(ebda_seg, dpte.checksum, -sum);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500491
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400492 if (size < 66) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500493 disk_ret(regs, DISK_RET_SUCCESS);
494 return;
495 }
496
497 // EDD 3.x
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500498 SET_INT13DPT(regs, key, 0xbedd);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400499 SET_INT13DPT(regs, dpi_length, 36);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500500 SET_INT13DPT(regs, reserved1, 0);
501 SET_INT13DPT(regs, reserved2, 0);
502
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400503 SET_INT13DPT(regs, host_bus[0], 'P');
504 SET_INT13DPT(regs, host_bus[1], 'C');
505 SET_INT13DPT(regs, host_bus[2], 'I');
506 SET_INT13DPT(regs, host_bus[3], 0);
507
Kevin O'Connor609da232008-12-28 23:18:57 -0500508 u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400509 u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
510 | (pci_bdf_to_fn(bdf) << 16));
511 SET_INT13DPT(regs, iface_path, path);
512
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500513 SET_INT13DPT(regs, iface_type[0], 'A');
514 SET_INT13DPT(regs, iface_type[1], 'T');
515 SET_INT13DPT(regs, iface_type[2], 'A');
516 SET_INT13DPT(regs, iface_type[3], 0);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400517 SET_INT13DPT(regs, iface_type[4], 0);
518 SET_INT13DPT(regs, iface_type[5], 0);
519 SET_INT13DPT(regs, iface_type[6], 0);
520 SET_INT13DPT(regs, iface_type[7], 0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500521
Kevin O'Connor970a0322008-10-26 12:01:21 -0400522 SET_INT13DPT(regs, device_path, slave);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500523
Kevin O'Connor7d108212009-01-21 19:13:21 -0500524 SET_INT13DPT(regs, checksum
525 , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400526
527 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500528}
529
530// IBM/MS extended media change
531static void
532disk_1349(struct bregs *regs, u8 device)
533{
534 // Always success for HD
535 disk_ret(regs, DISK_RET_SUCCESS);
536}
537
538static void
539disk_134e01(struct bregs *regs, u8 device)
540{
541 disk_ret(regs, DISK_RET_SUCCESS);
542}
543
544static void
545disk_134e03(struct bregs *regs, u8 device)
546{
547 disk_ret(regs, DISK_RET_SUCCESS);
548}
549
550static void
551disk_134e04(struct bregs *regs, u8 device)
552{
553 disk_ret(regs, DISK_RET_SUCCESS);
554}
555
556static void
557disk_134e06(struct bregs *regs, u8 device)
558{
559 disk_ret(regs, DISK_RET_SUCCESS);
560}
561
562static void
563disk_134eXX(struct bregs *regs, u8 device)
564{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500565 disk_ret(regs, DISK_RET_EPARAM);
566}
567
568// IBM/MS set hardware configuration
569static void
570disk_134e(struct bregs *regs, u8 device)
571{
572 switch (regs->al) {
573 case 0x01: disk_134e01(regs, device); break;
574 case 0x03: disk_134e03(regs, device); break;
575 case 0x04: disk_134e04(regs, device); break;
576 case 0x06: disk_134e06(regs, device); break;
577 default: disk_134eXX(regs, device); break;
578 }
579}
580
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500581void
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500582disk_13XX(struct bregs *regs, u8 device)
583{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500584 disk_ret(regs, DISK_RET_EPARAM);
585}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500586
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500587void
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500588disk_13(struct bregs *regs, u8 device)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500589{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500590 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500591
592 // clear completion flag
593 SET_BDA(disk_interrupt_flag, 0);
594
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500595 switch (regs->ah) {
596 case 0x00: disk_1300(regs, device); break;
597 case 0x01: disk_1301(regs, device); break;
598 case 0x02: disk_1302(regs, device); break;
599 case 0x03: disk_1303(regs, device); break;
600 case 0x04: disk_1304(regs, device); break;
601 case 0x05: disk_1305(regs, device); break;
602 case 0x08: disk_1308(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500603 case 0x09: disk_1309(regs, device); break;
604 case 0x0c: disk_130c(regs, device); break;
605 case 0x0d: disk_130d(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500606 case 0x10: disk_1310(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500607 case 0x11: disk_1311(regs, device); break;
608 case 0x14: disk_1314(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500609 case 0x15: disk_1315(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500610 case 0x41: disk_1341(regs, device); break;
611 case 0x42: disk_1342(regs, device); break;
612 case 0x43: disk_1343(regs, device); break;
613 case 0x44: disk_1344(regs, device); break;
614 case 0x45: disk_1345(regs, device); break;
615 case 0x46: disk_1346(regs, device); break;
616 case 0x47: disk_1347(regs, device); break;
617 case 0x48: disk_1348(regs, device); break;
618 case 0x49: disk_1349(regs, device); break;
619 case 0x4e: disk_134e(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500620 default: disk_13XX(regs, device); break;
621 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500622}
623
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500624
625/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500626 * Entry points
627 ****************************************************************/
628
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500629static u8
Kevin O'Connor180a9592008-03-04 22:50:53 -0500630get_device(struct bregs *regs, u8 iscd, u8 drive)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500631{
632 // basic check : device has to be defined
633 if (drive >= CONFIG_MAX_ATA_DEVICES) {
634 disk_ret(regs, DISK_RET_EPARAM);
635 return CONFIG_MAX_ATA_DEVICES;
636 }
637
638 // Get the ata channel
Kevin O'Connor609da232008-12-28 23:18:57 -0500639 u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500640
641 // basic check : device has to be valid
642 if (device >= CONFIG_MAX_ATA_DEVICES) {
643 disk_ret(regs, DISK_RET_EPARAM);
644 return CONFIG_MAX_ATA_DEVICES;
645 }
646
647 return device;
648}
649
650static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500651handle_legacy_disk(struct bregs *regs, u8 drive)
652{
653 if (drive < 0x80) {
654 floppy_13(regs, drive);
655 return;
656 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500657
658 if (! CONFIG_ATA) {
659 // XXX - old code had other disk access method.
660 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500661 return;
662 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500663
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500664 if (drive >= 0xe0) {
Kevin O'Connor180a9592008-03-04 22:50:53 -0500665 u8 device = get_device(regs, 1, drive - 0xe0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500666 if (device >= CONFIG_MAX_ATA_DEVICES)
667 return;
668 cdrom_13(regs, device);
669 return;
670 }
671
Kevin O'Connor180a9592008-03-04 22:50:53 -0500672 u8 device = get_device(regs, 0, drive - 0x80);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500673 if (device >= CONFIG_MAX_ATA_DEVICES)
674 return;
675 disk_13(regs, device);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500676}
677
Kevin O'Connor19786762008-03-05 21:09:59 -0500678void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500679handle_40(struct bregs *regs)
680{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400681 debug_enter(regs, DEBUG_HDL_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500682 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500683}
684
685// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500686void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500687handle_13(struct bregs *regs)
688{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400689 debug_enter(regs, DEBUG_HDL_13);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500690 u8 drive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500691
Kevin O'Connordfa16502008-03-22 20:13:08 -0400692 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500693 if (regs->ah == 0x4b) {
694 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400695 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500696 }
Kevin O'Connor4a16ef62008-12-31 00:09:28 -0500697 u16 ebda_seg = get_ebda_seg();
698 if (GET_EBDA2(ebda_seg, cdemu.active)) {
Kevin O'Connor5c0c4b72009-02-17 20:40:59 -0500699 u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
700 if (drive == emudrive) {
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'Connor5c0c4b72009-02-17 20:40:59 -0500704 if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500705 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);