blob: 8345208725853a4757a300620505ae9164d11a34 [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//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
8#include "disk.h" // floppy_13
9#include "biosvar.h" // struct bregs
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050010#include "config.h" // CONFIG_*
11#include "cmos.h" // inb_cmos
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050012#include "util.h" // debug_enter
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050013#include "ata.h" // ATA_*
14
Kevin O'Connorf205f9f2008-03-09 16:11:49 -040015#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
16#define DEBUGF(fmt, args...)
17
Kevin O'Connorb74102d2008-03-03 21:57:30 -050018
19/****************************************************************
20 * Helper functions
21 ****************************************************************/
22
Kevin O'Connore43df9e2008-03-01 22:16:32 -050023#define DISK_STUB(regs) do { \
24 struct bregs *__regs = (regs); \
25 debug_stub(__regs); \
26 disk_ret(__regs, DISK_RET_SUCCESS); \
27 } while (0)
28
29static u8
30checksum_seg(u16 seg, u16 offset, u32 len)
31{
32 u32 i;
33 u8 sum = 0;
34 for (i=0; i<len; i++)
35 sum += GET_FARVAR(seg, *(u8*)(offset+i));
36 return sum;
37}
38
39static void
40basic_access(struct bregs *regs, u8 device, u16 command)
41{
Kevin O'Connoraa2590c2008-03-22 23:13:24 -040042 u8 type = GET_EBDA(ata.devices[device].type);
43 u16 nlc, nlh, nlspt;
44 if (type == ATA_TYPE_ATA) {
45 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
46 nlh = GET_EBDA(ata.devices[device].lchs.heads);
47 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
48 } else {
49 // Must be cd emulation.
50 nlc = GET_EBDA(cdemu.vdevice.cylinders);
51 nlh = GET_EBDA(cdemu.vdevice.heads);
52 nlspt = GET_EBDA(cdemu.vdevice.spt);
53 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -050054
Kevin O'Connor0cdac0e2008-03-29 12:45:53 -040055 u16 count = regs->al;
56 u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
57 u16 sector = regs->cl & 0x3f;
58 u16 head = regs->dh;
59
60 if (count > 128 || count == 0 || sector == 0) {
61 BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
62 , regs->ah);
63 disk_ret(regs, DISK_RET_EPARAM);
64 return;
65 }
66
Kevin O'Connore43df9e2008-03-01 22:16:32 -050067 // sanity check on cyl heads, sec
Kevin O'Connor1fcf1442008-03-11 19:42:41 -040068 if (cylinder >= nlc || head >= nlh || sector > nlspt) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -050069 BX_INFO("int13_harddisk: function %02x, parameters out of"
70 " range %04x/%04x/%04x!\n"
71 , regs->ah, cylinder, head, sector);
72 disk_ret(regs, DISK_RET_EPARAM);
73 return;
74 }
75
Kevin O'Connor3a049632008-03-11 11:48:04 -040076 if (!command) {
77 // If verify or seek
Kevin O'Connore43df9e2008-03-01 22:16:32 -050078 disk_ret(regs, DISK_RET_SUCCESS);
79 return;
80 }
81
Kevin O'Connor049d5a22008-03-13 19:09:49 -040082 // translate lchs to lba
83 u32 lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -040084 + (u32)sector - 1);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -040085
86 u16 segment = regs->es;
87 u16 offset = regs->bx;
88 void *far_buffer = MAKE_FARPTR(segment, offset);
89
Kevin O'Connor049d5a22008-03-13 19:09:49 -040090 irq_enable();
Kevin O'Connoraa2590c2008-03-22 23:13:24 -040091
92 int status;
93 if (type == ATA_TYPE_ATA)
94 status = ata_cmd_data(device, command, lba, count, far_buffer);
95 else
96 status = cdrom_read_emu(device, lba, count, far_buffer);
97
Kevin O'Connor74799df2008-03-12 20:49:07 -040098 irq_disable();
99
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500100 // Set nb of sector transferred
101 regs->al = GET_EBDA(ata.trsfsectors);
102
103 if (status != 0) {
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400104 BX_INFO("int13_harddisk: function %02x, error %02x !\n"
105 , regs->ah, status);
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500106 disk_ret(regs, DISK_RET_EBADTRACK);
107 }
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500108 disk_ret(regs, DISK_RET_SUCCESS);
109}
110
Kevin O'Connored128492008-03-11 11:14:59 -0400111static void
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500112extended_access(struct bregs *regs, u8 device, u16 command)
113{
114 u16 count = GET_INT13EXT(regs, count);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500115
116 // Can't use 64 bits lba
117 u32 lba = GET_INT13EXT(regs, lba2);
118 if (lba != 0L) {
119 BX_PANIC("int13_harddisk: function %02x. Can't use 64bits lba\n"
120 , regs->ah);
121 disk_ret(regs, DISK_RET_EPARAM);
122 return;
123 }
124
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500125 u8 type = GET_EBDA(ata.devices[device].type);
126
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500127 // Get 32 bits lba and check
128 lba = GET_INT13EXT(regs, lba1);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500129 if (type == ATA_TYPE_ATA
130 && lba >= GET_EBDA(ata.devices[device].sectors)) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500131 BX_INFO("int13_harddisk: function %02x. LBA out of range\n", regs->ah);
132 disk_ret(regs, DISK_RET_EPARAM);
133 return;
134 }
135
Kevin O'Connor3a049632008-03-11 11:48:04 -0400136 if (!command) {
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500137 // If verify or seek
138 disk_ret(regs, DISK_RET_SUCCESS);
139 return;
140 }
141
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400142 u16 segment = GET_INT13EXT(regs, segment);
143 u16 offset = GET_INT13EXT(regs, offset);
144 void *far_buffer = MAKE_FARPTR(segment, offset);
145
Kevin O'Connor74799df2008-03-12 20:49:07 -0400146 irq_enable();
147
Kevin O'Connor3a049632008-03-11 11:48:04 -0400148 u8 status;
149 if (type == ATA_TYPE_ATA)
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400150 status = ata_cmd_data(device, command, lba, count, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400151 else
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400152 status = cdrom_read(device, lba, count, far_buffer);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400153
Kevin O'Connor74799df2008-03-12 20:49:07 -0400154 irq_disable();
155
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500156 SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
157
158 if (status != 0) {
159 BX_INFO("int13_harddisk: function %02x, error %02x !\n"
160 , regs->ah, status);
161 disk_ret(regs, DISK_RET_EBADTRACK);
162 return;
163 }
164 disk_ret(regs, DISK_RET_SUCCESS);
165}
166
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500167
168/****************************************************************
169 * Hard Drive functions
170 ****************************************************************/
171
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500172// disk controller reset
173static void
174disk_1300(struct bregs *regs, u8 device)
175{
176 ata_reset(device);
177}
178
179// read disk status
180static void
181disk_1301(struct bregs *regs, u8 device)
182{
Kevin O'Connorfad2da82008-03-22 20:37:44 -0400183 u8 v = GET_BDA(disk_last_status);
184 regs->ah = v;
185 set_cf(regs, v);
186 // XXX - clear disk_last_status?
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500187}
188
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500189// read disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500190static void
191disk_1302(struct bregs *regs, u8 device)
192{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500193 basic_access(regs, device, ATA_CMD_READ_SECTORS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500194}
195
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500196// write disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500197static void
198disk_1303(struct bregs *regs, u8 device)
199{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500200 basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500201}
202
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500203// verify disk sectors
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500204static void
205disk_1304(struct bregs *regs, u8 device)
206{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500207 basic_access(regs, device, 0);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500208 // FIXME verify
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500209}
210
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500211// format disk track
212static void
213disk_1305(struct bregs *regs, u8 device)
214{
215 DISK_STUB(regs);
216}
217
218// read disk drive parameters
219static void
220disk_1308(struct bregs *regs, u8 device)
221{
222 // Get logical geometry from table
223 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
224 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
225 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
226 u16 count = GET_EBDA(ata.hdcount);
227
228 nlc = nlc - 2; /* 0 based , last sector not used */
229 regs->al = 0;
230 regs->ch = nlc & 0xff;
231 regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
232 regs->dh = nlh - 1;
233 regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
234
235 // FIXME should set ES & DI
236 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500237}
238
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500239// initialize drive parameters
240static void
241disk_1309(struct bregs *regs, u8 device)
242{
243 DISK_STUB(regs);
244}
245
246// seek to specified cylinder
247static void
248disk_130c(struct bregs *regs, u8 device)
249{
250 DISK_STUB(regs);
251}
252
253// alternate disk reset
254static void
255disk_130d(struct bregs *regs, u8 device)
256{
257 DISK_STUB(regs);
258}
259
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500260// check drive ready
261static void
262disk_1310(struct bregs *regs, u8 device)
263{
264 // should look at 40:8E also???
265
266 // Read the status from controller
267 u8 status = inb(GET_EBDA(ata.channels[device/2].iobase1) + ATA_CB_STAT);
268 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
269 disk_ret(regs, DISK_RET_SUCCESS);
270 else
271 disk_ret(regs, DISK_RET_ENOTREADY);
272}
273
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500274// recalibrate
275static void
276disk_1311(struct bregs *regs, u8 device)
277{
278 DISK_STUB(regs);
279}
280
281// controller internal diagnostic
282static void
283disk_1314(struct bregs *regs, u8 device)
284{
285 DISK_STUB(regs);
286}
287
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500288// read disk drive size
289static void
290disk_1315(struct bregs *regs, u8 device)
291{
292 // Get logical geometry from table
293 u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
294 u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
295 u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
296
297 // Compute sector count seen by int13
298 u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
299 regs->cx = lba >> 16;
300 regs->dx = lba & 0xffff;
301
Kevin O'Connordcc7a4f2008-03-08 23:25:16 -0500302 disk_ret(regs, DISK_RET_SUCCESS);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500303 regs->ah = 3; // hard disk accessible
304}
305
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500306// IBM/MS installation check
307static void
308disk_1341(struct bregs *regs, u8 device)
309{
310 regs->bx = 0xaa55; // install check
311 regs->cx = 0x0007; // ext disk access and edd, removable supported
312 disk_ret(regs, DISK_RET_SUCCESS);
313 regs->ah = 0x30; // EDD 3.0
314}
315
316// IBM/MS extended read
317static void
318disk_1342(struct bregs *regs, u8 device)
319{
320 extended_access(regs, device, ATA_CMD_READ_SECTORS);
321}
322
323// IBM/MS extended write
324static void
325disk_1343(struct bregs *regs, u8 device)
326{
327 extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
328}
329
330// IBM/MS verify
331static void
332disk_1344(struct bregs *regs, u8 device)
333{
334 extended_access(regs, device, 0);
335}
336
337// IBM/MS lock/unlock drive
338static void
339disk_1345(struct bregs *regs, u8 device)
340{
341 // Always success for HD
342 disk_ret(regs, DISK_RET_SUCCESS);
343}
344
345// IBM/MS eject media
346static void
347disk_1346(struct bregs *regs, u8 device)
348{
349 // Volume Not Removable
350 disk_ret(regs, DISK_RET_ENOTREMOVABLE);
351}
352
353// IBM/MS extended seek
354static void
355disk_1347(struct bregs *regs, u8 device)
356{
357 extended_access(regs, device, 0);
358}
359
360// IBM/MS get drive parameters
361static void
362disk_1348(struct bregs *regs, u8 device)
363{
364 u16 size = GET_INT13DPT(regs, size);
365
366 // Buffer is too small
367 if (size < 0x1a) {
368 disk_ret(regs, DISK_RET_EPARAM);
369 return;
370 }
371
372 // EDD 1.x
373
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500374 u8 type = GET_EBDA(ata.devices[device].type);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500375 u16 npc = GET_EBDA(ata.devices[device].pchs.cylinders);
376 u16 nph = GET_EBDA(ata.devices[device].pchs.heads);
377 u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);
378 u32 lba = GET_EBDA(ata.devices[device].sectors);
379 u16 blksize = GET_EBDA(ata.devices[device].blksize);
380
381 SET_INT13DPT(regs, size, 0x1a);
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500382 if (type == ATA_TYPE_ATA) {
383 if ((lba/npspt)/nph > 0x3fff) {
384 SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
385 SET_INT13DPT(regs, cylinders, 0x3fff);
386 } else {
387 SET_INT13DPT(regs, infos, 0x02); // geometry is valid
388 SET_INT13DPT(regs, cylinders, (u32)npc);
389 }
390 SET_INT13DPT(regs, heads, (u32)nph);
391 SET_INT13DPT(regs, spt, (u32)npspt);
392 SET_INT13DPT(regs, sector_count1, lba); // FIXME should be Bit64
393 SET_INT13DPT(regs, sector_count2, 0L);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500394 } else {
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500395 // ATAPI
396 // 0x74 = removable, media change, lockable, max values
397 SET_INT13DPT(regs, infos, 0x74);
398 SET_INT13DPT(regs, cylinders, 0xffffffff);
399 SET_INT13DPT(regs, heads, 0xffffffff);
400 SET_INT13DPT(regs, spt, 0xffffffff);
401 SET_INT13DPT(regs, sector_count1, 0xffffffff); // FIXME should be Bit64
402 SET_INT13DPT(regs, sector_count2, 0xffffffff);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500403 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500404 SET_INT13DPT(regs, blksize, blksize);
405
406 if (size < 0x1e) {
407 disk_ret(regs, DISK_RET_SUCCESS);
408 return;
409 }
410
411 // EDD 2.x
412
413 SET_INT13DPT(regs, size, 0x1e);
414
415 SET_INT13DPT(regs, dpte_segment, EBDA_SEG);
416 SET_INT13DPT(regs, dpte_offset
417 , offsetof(struct extended_bios_data_area_s, ata.dpte));
418
419 // Fill in dpte
420 u8 channel = device / 2;
421 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
422 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
423 u8 irq = GET_EBDA(ata.channels[channel].irq);
424 u8 mode = GET_EBDA(ata.devices[device].mode);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500425
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500426 u16 options;
427 if (type == ATA_TYPE_ATA) {
428 u8 translation = GET_EBDA(ata.devices[device].translation);
429 options = (translation==ATA_TRANSLATION_NONE?0:1)<<3; // chs translation
430 options |= (translation==ATA_TRANSLATION_LBA?1:0)<<9;
431 options |= (translation==ATA_TRANSLATION_RECHS?3:0)<<9;
432 } else {
433 // ATAPI
434 options = (1<<5); // removable device
435 options |= (1<<6); // atapi device
436 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500437 options |= (1<<4); // lba translation
438 options |= (mode==ATA_MODE_PIO32?1:0)<<7;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500439
440 SET_EBDA(ata.dpte.iobase1, iobase1);
441 SET_EBDA(ata.dpte.iobase2, iobase2 + ATA_CB_DC);
442 SET_EBDA(ata.dpte.prefix, (0xe | (device % 2))<<4 );
443 SET_EBDA(ata.dpte.unused, 0xcb );
444 SET_EBDA(ata.dpte.irq, irq );
445 SET_EBDA(ata.dpte.blkcount, 1 );
446 SET_EBDA(ata.dpte.dma, 0 );
447 SET_EBDA(ata.dpte.pio, 0 );
448 SET_EBDA(ata.dpte.options, options);
449 SET_EBDA(ata.dpte.reserved, 0);
450 if (size >= 0x42)
451 SET_EBDA(ata.dpte.revision, 0x11);
452 else
453 SET_EBDA(ata.dpte.revision, 0x10);
454
455 u8 sum = checksum_seg(EBDA_SEG
456 , offsetof(struct extended_bios_data_area_s, ata.dpte)
457 , 15);
458 SET_EBDA(ata.dpte.checksum, ~sum);
459
460 if (size < 0x42) {
461 disk_ret(regs, DISK_RET_SUCCESS);
462 return;
463 }
464
465 // EDD 3.x
466 channel = device / 2;
467 u8 iface = GET_EBDA(ata.channels[channel].iface);
468 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
469
470 SET_INT13DPT(regs, size, 0x42);
471 SET_INT13DPT(regs, key, 0xbedd);
472 SET_INT13DPT(regs, dpi_length, 0x24);
473 SET_INT13DPT(regs, reserved1, 0);
474 SET_INT13DPT(regs, reserved2, 0);
475
476 if (iface==ATA_IFACE_ISA) {
477 SET_INT13DPT(regs, host_bus[0], 'I');
478 SET_INT13DPT(regs, host_bus[1], 'S');
479 SET_INT13DPT(regs, host_bus[2], 'A');
480 SET_INT13DPT(regs, host_bus[3], 0);
481 } else {
482 // FIXME PCI
483 }
484 SET_INT13DPT(regs, iface_type[0], 'A');
485 SET_INT13DPT(regs, iface_type[1], 'T');
486 SET_INT13DPT(regs, iface_type[2], 'A');
487 SET_INT13DPT(regs, iface_type[3], 0);
488
489 if (iface==ATA_IFACE_ISA) {
490 SET_INT13DPT(regs, iface_path[0], iobase1);
491 SET_INT13DPT(regs, iface_path[2], 0);
492 SET_INT13DPT(regs, iface_path[4], 0L);
493 } else {
494 // FIXME PCI
495 }
496 SET_INT13DPT(regs, device_path[0], device%2);
497 SET_INT13DPT(regs, device_path[1], 0);
498 SET_INT13DPT(regs, device_path[2], 0);
499 SET_INT13DPT(regs, device_path[4], 0L);
500
501 sum = checksum_seg(regs->ds, 30, 34);
502 SET_INT13DPT(regs, checksum, ~sum);
503}
504
505// IBM/MS extended media change
506static void
507disk_1349(struct bregs *regs, u8 device)
508{
509 // Always success for HD
510 disk_ret(regs, DISK_RET_SUCCESS);
511}
512
513static void
514disk_134e01(struct bregs *regs, u8 device)
515{
516 disk_ret(regs, DISK_RET_SUCCESS);
517}
518
519static void
520disk_134e03(struct bregs *regs, u8 device)
521{
522 disk_ret(regs, DISK_RET_SUCCESS);
523}
524
525static void
526disk_134e04(struct bregs *regs, u8 device)
527{
528 disk_ret(regs, DISK_RET_SUCCESS);
529}
530
531static void
532disk_134e06(struct bregs *regs, u8 device)
533{
534 disk_ret(regs, DISK_RET_SUCCESS);
535}
536
537static void
538disk_134eXX(struct bregs *regs, u8 device)
539{
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500540 disk_ret(regs, DISK_RET_EPARAM);
541}
542
543// IBM/MS set hardware configuration
544static void
545disk_134e(struct bregs *regs, u8 device)
546{
547 switch (regs->al) {
548 case 0x01: disk_134e01(regs, device); break;
549 case 0x03: disk_134e03(regs, device); break;
550 case 0x04: disk_134e04(regs, device); break;
551 case 0x06: disk_134e06(regs, device); break;
552 default: disk_134eXX(regs, device); break;
553 }
554}
555
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500556void
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500557disk_13XX(struct bregs *regs, u8 device)
558{
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500559 disk_ret(regs, DISK_RET_EPARAM);
560}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500561
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500562void
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500563disk_13(struct bregs *regs, u8 device)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500564{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500565 //debug_stub(regs);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500566
567 // clear completion flag
568 SET_BDA(disk_interrupt_flag, 0);
569
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500570 switch (regs->ah) {
571 case 0x00: disk_1300(regs, device); break;
572 case 0x01: disk_1301(regs, device); break;
573 case 0x02: disk_1302(regs, device); break;
574 case 0x03: disk_1303(regs, device); break;
575 case 0x04: disk_1304(regs, device); break;
576 case 0x05: disk_1305(regs, device); break;
577 case 0x08: disk_1308(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500578 case 0x09: disk_1309(regs, device); break;
579 case 0x0c: disk_130c(regs, device); break;
580 case 0x0d: disk_130d(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500581 case 0x10: disk_1310(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500582 case 0x11: disk_1311(regs, device); break;
583 case 0x14: disk_1314(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500584 case 0x15: disk_1315(regs, device); break;
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500585 case 0x41: disk_1341(regs, device); break;
586 case 0x42: disk_1342(regs, device); break;
587 case 0x43: disk_1343(regs, device); break;
588 case 0x44: disk_1344(regs, device); break;
589 case 0x45: disk_1345(regs, device); break;
590 case 0x46: disk_1346(regs, device); break;
591 case 0x47: disk_1347(regs, device); break;
592 case 0x48: disk_1348(regs, device); break;
593 case 0x49: disk_1349(regs, device); break;
594 case 0x4e: disk_134e(regs, device); break;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500595 default: disk_13XX(regs, device); break;
596 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500597}
598
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500599
600/****************************************************************
Kevin O'Connorb74102d2008-03-03 21:57:30 -0500601 * Entry points
602 ****************************************************************/
603
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500604static u8
Kevin O'Connor180a9592008-03-04 22:50:53 -0500605get_device(struct bregs *regs, u8 iscd, u8 drive)
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500606{
607 // basic check : device has to be defined
608 if (drive >= CONFIG_MAX_ATA_DEVICES) {
609 disk_ret(regs, DISK_RET_EPARAM);
610 return CONFIG_MAX_ATA_DEVICES;
611 }
612
613 // Get the ata channel
Kevin O'Connor180a9592008-03-04 22:50:53 -0500614 u8 device = GET_EBDA(ata.idmap[iscd][drive]);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500615
616 // basic check : device has to be valid
617 if (device >= CONFIG_MAX_ATA_DEVICES) {
618 disk_ret(regs, DISK_RET_EPARAM);
619 return CONFIG_MAX_ATA_DEVICES;
620 }
621
622 return device;
623}
624
625static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500626handle_legacy_disk(struct bregs *regs, u8 drive)
627{
628 if (drive < 0x80) {
629 floppy_13(regs, drive);
630 return;
631 }
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500632
633 if (! CONFIG_ATA) {
634 // XXX - old code had other disk access method.
635 disk_ret(regs, DISK_RET_EPARAM);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500636 return;
637 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500638
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500639 if (drive >= 0xe0) {
Kevin O'Connor180a9592008-03-04 22:50:53 -0500640 u8 device = get_device(regs, 1, drive - 0xe0);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500641 if (device >= CONFIG_MAX_ATA_DEVICES)
642 return;
643 cdrom_13(regs, device);
644 return;
645 }
646
Kevin O'Connor180a9592008-03-04 22:50:53 -0500647 u8 device = get_device(regs, 0, drive - 0x80);
Kevin O'Connore43df9e2008-03-01 22:16:32 -0500648 if (device >= CONFIG_MAX_ATA_DEVICES)
649 return;
650 disk_13(regs, device);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500651}
652
Kevin O'Connor19786762008-03-05 21:09:59 -0500653void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500654handle_40(struct bregs *regs)
655{
656 debug_enter(regs);
657 handle_legacy_disk(regs, regs->dl);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500658}
659
660// INT 13h Fixed Disk Services Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500661void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500662handle_13(struct bregs *regs)
663{
Kevin O'Connor127cbd72008-03-08 11:34:28 -0500664 //debug_enter(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500665 u8 drive = regs->dl;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500666
Kevin O'Connordfa16502008-03-22 20:13:08 -0400667 if (CONFIG_CDROM_EMU) {
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500668 if (regs->ah == 0x4b) {
669 cdemu_134b(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400670 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500671 }
672 if (GET_EBDA(cdemu.active)) {
673 if (drive == GET_EBDA(cdemu.emulated_drive)) {
674 cdemu_13(regs);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400675 return;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500676 }
Kevin O'Connor180a9592008-03-04 22:50:53 -0500677 if (drive < 0xe0)
678 drive--;
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500679 }
680 }
681 handle_legacy_disk(regs, drive);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500682}
683
684// record completion in BIOS task complete flag
Kevin O'Connor19786762008-03-05 21:09:59 -0500685void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400686handle_76()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500687{
Kevin O'Connored128492008-03-11 11:14:59 -0400688 debug_isr();
689 SET_BDA(disk_interrupt_flag, 0xff);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500690 eoi_both_pics();
691}