Get CDROM emulation working.
[seabios.git] / src / disk.c
1 // 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
10 #include "config.h" // CONFIG_*
11 #include "cmos.h" // inb_cmos
12 #include "util.h" // debug_enter
13 #include "ata.h" // ATA_*
14
15
16 /****************************************************************
17  * Helper functions
18  ****************************************************************/
19
20 #define DISK_STUB(regs) do {                    \
21         struct bregs *__regs = (regs);          \
22         debug_stub(__regs);                     \
23         disk_ret(__regs, DISK_RET_SUCCESS);     \
24     } while (0)
25
26 static u8
27 checksum_seg(u16 seg, u16 offset, u32 len)
28 {
29     u32 i;
30     u8 sum = 0;
31     for (i=0; i<len; i++)
32         sum += GET_FARVAR(seg, *(u8*)(offset+i));
33     return sum;
34 }
35
36 static void
37 basic_access(struct bregs *regs, u8 device, u16 command)
38 {
39     u16 count       = regs->al;
40     u16 cylinder    = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
41     u16 sector      = regs->cl & 0x3f;
42     u16 head        = regs->dh;
43
44     if ((count > 128) || (count == 0) || (sector == 0)) {
45         BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
46                 , regs->ah);
47         disk_ret(regs, DISK_RET_EPARAM);
48         return;
49     }
50
51     u16 nlc   = GET_EBDA(ata.devices[device].lchs.cylinders);
52     u16 nlh   = GET_EBDA(ata.devices[device].lchs.heads);
53     u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
54     u16 nph   = GET_EBDA(ata.devices[device].pchs.heads);
55     u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);
56
57     // sanity check on cyl heads, sec
58     if ( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
59         BX_INFO("int13_harddisk: function %02x, parameters out of"
60                 " range %04x/%04x/%04x!\n"
61                 , regs->ah, cylinder, head, sector);
62         disk_ret(regs, DISK_RET_EPARAM);
63         return;
64     }
65
66     u32 lba = 0;
67     // if needed, translate lchs to lba, and execute command
68     if ( (nph != nlh) || (npspt != nlspt)) {
69         lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
70                + (u32)sector - 1);
71         sector = 0; // this forces the command to be lba
72     }
73
74     u16 segment = regs->es;
75     u16 offset  = regs->bx;
76
77     u8 status;
78     switch (command) {
79     case ATA_CMD_READ_SECTORS:
80         status = ata_cmd_data_in(device, ATA_CMD_READ_SECTORS
81                                  , count, cylinder, head, sector
82                                  , lba, segment, offset);
83         break;
84     case ATA_CMD_WRITE_SECTORS:
85         status = ata_cmd_data_out(device, ATA_CMD_WRITE_SECTORS
86                                   , count, cylinder, head, sector
87                                   , lba, segment, offset);
88         break;
89     default:
90         disk_ret(regs, DISK_RET_SUCCESS);
91         return;
92     }
93
94     // Set nb of sector transferred
95     regs->al = GET_EBDA(ata.trsfsectors);
96
97     if (status != 0) {
98         BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
99         disk_ret(regs, DISK_RET_EBADTRACK);
100     }
101     disk_ret(regs, DISK_RET_SUCCESS);
102 }
103
104 void
105 emu_access(struct bregs *regs, u8 device, u16 command)
106 {
107     u16 count    = regs->al;
108     u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
109     u16 sector   = regs->cl & 0x3f;
110     u16 head     = regs->dh;
111
112     if ((count > 128) || (count == 0) || (sector == 0)) {
113         BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
114                 , regs->ah);
115         disk_ret(regs, DISK_RET_EPARAM);
116         return;
117     }
118
119     u16 nlc   = GET_EBDA(cdemu.vdevice.cylinders);
120     u16 nlh   = GET_EBDA(cdemu.vdevice.heads);
121     u16 nlspt = GET_EBDA(cdemu.vdevice.spt);
122
123     // sanity check on cyl heads, sec
124     if ( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
125         BX_INFO("int13_harddisk: function %02x, parameters out of"
126                 " range %04x/%04x/%04x!\n"
127                 , regs->ah, cylinder, head, sector);
128         disk_ret(regs, DISK_RET_EPARAM);
129         return;
130     }
131
132     if (!command) {
133         // If verify or seek
134         disk_ret(regs, DISK_RET_SUCCESS);
135         return;
136     }
137
138     u32 ilba = GET_EBDA(cdemu.ilba);
139     // calculate the virtual lba inside the image
140     u32 vlba= (((((u32)cylinder*(u32)nlh)+(u32)head)*(u32)nlspt)
141                +((u32)(sector-1)));
142     // start lba on cd
143     u32 slba  = (u32)vlba/4;
144     u16 before= (u16)vlba%4;
145     u32 lba = ilba + slba;
146
147     u16 segment = regs->es;
148     u16 offset  = regs->bx;
149
150     u8 status = cdrom_read(device, lba, count*512, segment, offset, before*512);
151     if (status != 0) {
152         BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
153         regs->al = 0;
154         disk_ret(regs, DISK_RET_EBADTRACK);
155     }
156     regs->al = count;
157     disk_ret(regs, DISK_RET_SUCCESS);
158 }
159
160 void
161 extended_access(struct bregs *regs, u8 device, u16 command)
162 {
163     u16 count = GET_INT13EXT(regs, count);
164     u16 segment = GET_INT13EXT(regs, segment);
165     u16 offset = GET_INT13EXT(regs, offset);
166
167     // Can't use 64 bits lba
168     u32 lba = GET_INT13EXT(regs, lba2);
169     if (lba != 0L) {
170         BX_PANIC("int13_harddisk: function %02x. Can't use 64bits lba\n"
171                  , regs->ah);
172         disk_ret(regs, DISK_RET_EPARAM);
173         return;
174     }
175
176     u8 type = GET_EBDA(ata.devices[device].type);
177
178     // Get 32 bits lba and check
179     lba = GET_INT13EXT(regs, lba1);
180     if (type == ATA_TYPE_ATA
181         && lba >= GET_EBDA(ata.devices[device].sectors)) {
182         BX_INFO("int13_harddisk: function %02x. LBA out of range\n", regs->ah);
183         disk_ret(regs, DISK_RET_EPARAM);
184         return;
185     }
186
187     u8 status;
188     switch (command) {
189     case ATA_CMD_READ_SECTORS:
190         if (type == ATA_TYPE_ATA)
191             status = ata_cmd_data_in(device, ATA_CMD_READ_SECTORS
192                                      , count, 0, 0, 0
193                                      , lba, segment, offset);
194         else
195             status = cdrom_read(device, lba, count*2048, segment, offset, 0);
196         break;
197     case ATA_CMD_WRITE_SECTORS:
198         status = ata_cmd_data_out(device, ATA_CMD_WRITE_SECTORS
199                                   , count, 0, 0, 0
200                                   , lba, segment, offset);
201         break;
202     default:
203         // If verify or seek
204         disk_ret(regs, DISK_RET_SUCCESS);
205         return;
206     }
207
208     SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));
209
210     if (status != 0) {
211         BX_INFO("int13_harddisk: function %02x, error %02x !\n"
212                 , regs->ah, status);
213         disk_ret(regs, DISK_RET_EBADTRACK);
214         return;
215     }
216     disk_ret(regs, DISK_RET_SUCCESS);
217 }
218
219
220 /****************************************************************
221  * Hard Drive functions
222  ****************************************************************/
223
224 // disk controller reset
225 static void
226 disk_1300(struct bregs *regs, u8 device)
227 {
228     ata_reset(device);
229 }
230
231 // read disk status
232 static void
233 disk_1301(struct bregs *regs, u8 device)
234 {
235     regs->ah = GET_BDA(disk_last_status);
236     disk_ret(regs, DISK_RET_SUCCESS);
237 }
238
239 // read disk sectors
240 static void
241 disk_1302(struct bregs *regs, u8 device)
242 {
243     basic_access(regs, device, ATA_CMD_READ_SECTORS);
244 }
245
246 // write disk sectors
247 static void
248 disk_1303(struct bregs *regs, u8 device)
249 {
250     basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
251 }
252
253 // verify disk sectors
254 static void
255 disk_1304(struct bregs *regs, u8 device)
256 {
257     basic_access(regs, device, 0);
258     // FIXME verify
259 }
260
261 // format disk track
262 static void
263 disk_1305(struct bregs *regs, u8 device)
264 {
265     DISK_STUB(regs);
266 }
267
268 // read disk drive parameters
269 static void
270 disk_1308(struct bregs *regs, u8 device)
271 {
272     // Get logical geometry from table
273     u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
274     u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
275     u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
276     u16 count = GET_EBDA(ata.hdcount);
277
278     nlc = nlc - 2; /* 0 based , last sector not used */
279     regs->al = 0;
280     regs->ch = nlc & 0xff;
281     regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
282     regs->dh = nlh - 1;
283     regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
284
285     // FIXME should set ES & DI
286     disk_ret(regs, DISK_RET_SUCCESS);
287 }
288
289 // initialize drive parameters
290 static void
291 disk_1309(struct bregs *regs, u8 device)
292 {
293     DISK_STUB(regs);
294 }
295
296 // seek to specified cylinder
297 static void
298 disk_130c(struct bregs *regs, u8 device)
299 {
300     DISK_STUB(regs);
301 }
302
303 // alternate disk reset
304 static void
305 disk_130d(struct bregs *regs, u8 device)
306 {
307     DISK_STUB(regs);
308 }
309
310 // check drive ready
311 static void
312 disk_1310(struct bregs *regs, u8 device)
313 {
314     // should look at 40:8E also???
315
316     // Read the status from controller
317     u8 status = inb(GET_EBDA(ata.channels[device/2].iobase1) + ATA_CB_STAT);
318     if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
319         disk_ret(regs, DISK_RET_SUCCESS);
320     else
321         disk_ret(regs, DISK_RET_ENOTREADY);
322 }
323
324 // recalibrate
325 static void
326 disk_1311(struct bregs *regs, u8 device)
327 {
328     DISK_STUB(regs);
329 }
330
331 // controller internal diagnostic
332 static void
333 disk_1314(struct bregs *regs, u8 device)
334 {
335     DISK_STUB(regs);
336 }
337
338 // read disk drive size
339 static void
340 disk_1315(struct bregs *regs, u8 device)
341 {
342     // Get logical geometry from table
343     u16 nlc   = GET_EBDA(ata.devices[device].lchs.cylinders);
344     u16 nlh   = GET_EBDA(ata.devices[device].lchs.heads);
345     u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
346
347     // Compute sector count seen by int13
348     u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
349     regs->cx = lba >> 16;
350     regs->dx = lba & 0xffff;
351
352     disk_ret(regs, 0);
353     regs->ah = 3; // hard disk accessible
354 }
355
356 // IBM/MS installation check
357 static void
358 disk_1341(struct bregs *regs, u8 device)
359 {
360     regs->bx = 0xaa55;  // install check
361     regs->cx = 0x0007;  // ext disk access and edd, removable supported
362     disk_ret(regs, DISK_RET_SUCCESS);
363     regs->ah = 0x30;    // EDD 3.0
364 }
365
366 // IBM/MS extended read
367 static void
368 disk_1342(struct bregs *regs, u8 device)
369 {
370     extended_access(regs, device, ATA_CMD_READ_SECTORS);
371 }
372
373 // IBM/MS extended write
374 static void
375 disk_1343(struct bregs *regs, u8 device)
376 {
377     extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
378 }
379
380 // IBM/MS verify
381 static void
382 disk_1344(struct bregs *regs, u8 device)
383 {
384     extended_access(regs, device, 0);
385 }
386
387 // IBM/MS lock/unlock drive
388 static void
389 disk_1345(struct bregs *regs, u8 device)
390 {
391     // Always success for HD
392     disk_ret(regs, DISK_RET_SUCCESS);
393 }
394
395 // IBM/MS eject media
396 static void
397 disk_1346(struct bregs *regs, u8 device)
398 {
399     // Volume Not Removable
400     disk_ret(regs, DISK_RET_ENOTREMOVABLE);
401 }
402
403 // IBM/MS extended seek
404 static void
405 disk_1347(struct bregs *regs, u8 device)
406 {
407     extended_access(regs, device, 0);
408 }
409
410 // IBM/MS get drive parameters
411 static void
412 disk_1348(struct bregs *regs, u8 device)
413 {
414     u16 size = GET_INT13DPT(regs, size);
415
416     // Buffer is too small
417     if (size < 0x1a) {
418         disk_ret(regs, DISK_RET_EPARAM);
419         return;
420     }
421
422     // EDD 1.x
423
424     u8  type    = GET_EBDA(ata.devices[device].type);
425     u16 npc     = GET_EBDA(ata.devices[device].pchs.cylinders);
426     u16 nph     = GET_EBDA(ata.devices[device].pchs.heads);
427     u16 npspt   = GET_EBDA(ata.devices[device].pchs.spt);
428     u32 lba     = GET_EBDA(ata.devices[device].sectors);
429     u16 blksize = GET_EBDA(ata.devices[device].blksize);
430
431     SET_INT13DPT(regs, size, 0x1a);
432     if (type == ATA_TYPE_ATA) {
433         if ((lba/npspt)/nph > 0x3fff) {
434             SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
435             SET_INT13DPT(regs, cylinders, 0x3fff);
436         } else {
437             SET_INT13DPT(regs, infos, 0x02); // geometry is valid
438             SET_INT13DPT(regs, cylinders, (u32)npc);
439         }
440         SET_INT13DPT(regs, heads, (u32)nph);
441         SET_INT13DPT(regs, spt, (u32)npspt);
442         SET_INT13DPT(regs, sector_count1, lba);  // FIXME should be Bit64
443         SET_INT13DPT(regs, sector_count2, 0L);
444     } else {
445         // ATAPI
446         // 0x74 = removable, media change, lockable, max values
447         SET_INT13DPT(regs, infos, 0x74);
448         SET_INT13DPT(regs, cylinders, 0xffffffff);
449         SET_INT13DPT(regs, heads, 0xffffffff);
450         SET_INT13DPT(regs, spt, 0xffffffff);
451         SET_INT13DPT(regs, sector_count1, 0xffffffff);  // FIXME should be Bit64
452         SET_INT13DPT(regs, sector_count2, 0xffffffff);
453     }
454     SET_INT13DPT(regs, blksize, blksize);
455
456     if (size < 0x1e) {
457         disk_ret(regs, DISK_RET_SUCCESS);
458         return;
459     }
460
461     // EDD 2.x
462
463     SET_INT13DPT(regs, size, 0x1e);
464
465     SET_INT13DPT(regs, dpte_segment, EBDA_SEG);
466     SET_INT13DPT(regs, dpte_offset
467                  , offsetof(struct extended_bios_data_area_s, ata.dpte));
468
469     // Fill in dpte
470     u8 channel = device / 2;
471     u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
472     u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
473     u8 irq = GET_EBDA(ata.channels[channel].irq);
474     u8 mode = GET_EBDA(ata.devices[device].mode);
475
476     u16 options;
477     if (type == ATA_TYPE_ATA) {
478         u8 translation = GET_EBDA(ata.devices[device].translation);
479         options  = (translation==ATA_TRANSLATION_NONE?0:1)<<3; // chs translation
480         options |= (translation==ATA_TRANSLATION_LBA?1:0)<<9;
481         options |= (translation==ATA_TRANSLATION_RECHS?3:0)<<9;
482     } else {
483         // ATAPI
484         options  = (1<<5); // removable device
485         options |= (1<<6); // atapi device
486     }
487     options |= (1<<4); // lba translation
488     options |= (mode==ATA_MODE_PIO32?1:0)<<7;
489
490     SET_EBDA(ata.dpte.iobase1, iobase1);
491     SET_EBDA(ata.dpte.iobase2, iobase2 + ATA_CB_DC);
492     SET_EBDA(ata.dpte.prefix, (0xe | (device % 2))<<4 );
493     SET_EBDA(ata.dpte.unused, 0xcb );
494     SET_EBDA(ata.dpte.irq, irq );
495     SET_EBDA(ata.dpte.blkcount, 1 );
496     SET_EBDA(ata.dpte.dma, 0 );
497     SET_EBDA(ata.dpte.pio, 0 );
498     SET_EBDA(ata.dpte.options, options);
499     SET_EBDA(ata.dpte.reserved, 0);
500     if (size >= 0x42)
501         SET_EBDA(ata.dpte.revision, 0x11);
502     else
503         SET_EBDA(ata.dpte.revision, 0x10);
504
505     u8 sum = checksum_seg(EBDA_SEG
506                           , offsetof(struct extended_bios_data_area_s, ata.dpte)
507                           , 15);
508     SET_EBDA(ata.dpte.checksum, ~sum);
509
510     if (size < 0x42) {
511         disk_ret(regs, DISK_RET_SUCCESS);
512         return;
513     }
514
515     // EDD 3.x
516     channel = device / 2;
517     u8 iface = GET_EBDA(ata.channels[channel].iface);
518     iobase1 = GET_EBDA(ata.channels[channel].iobase1);
519
520     SET_INT13DPT(regs, size, 0x42);
521     SET_INT13DPT(regs, key, 0xbedd);
522     SET_INT13DPT(regs, dpi_length, 0x24);
523     SET_INT13DPT(regs, reserved1, 0);
524     SET_INT13DPT(regs, reserved2, 0);
525
526     if (iface==ATA_IFACE_ISA) {
527         SET_INT13DPT(regs, host_bus[0], 'I');
528         SET_INT13DPT(regs, host_bus[1], 'S');
529         SET_INT13DPT(regs, host_bus[2], 'A');
530         SET_INT13DPT(regs, host_bus[3], 0);
531     } else {
532         // FIXME PCI
533     }
534     SET_INT13DPT(regs, iface_type[0], 'A');
535     SET_INT13DPT(regs, iface_type[1], 'T');
536     SET_INT13DPT(regs, iface_type[2], 'A');
537     SET_INT13DPT(regs, iface_type[3], 0);
538
539     if (iface==ATA_IFACE_ISA) {
540         SET_INT13DPT(regs, iface_path[0], iobase1);
541         SET_INT13DPT(regs, iface_path[2], 0);
542         SET_INT13DPT(regs, iface_path[4], 0L);
543     } else {
544         // FIXME PCI
545     }
546     SET_INT13DPT(regs, device_path[0], device%2);
547     SET_INT13DPT(regs, device_path[1], 0);
548     SET_INT13DPT(regs, device_path[2], 0);
549     SET_INT13DPT(regs, device_path[4], 0L);
550
551     sum = checksum_seg(regs->ds, 30, 34);
552     SET_INT13DPT(regs, checksum, ~sum);
553 }
554
555 // IBM/MS extended media change
556 static void
557 disk_1349(struct bregs *regs, u8 device)
558 {
559     // Always success for HD
560     disk_ret(regs, DISK_RET_SUCCESS);
561 }
562
563 static void
564 disk_134e01(struct bregs *regs, u8 device)
565 {
566     disk_ret(regs, DISK_RET_SUCCESS);
567 }
568
569 static void
570 disk_134e03(struct bregs *regs, u8 device)
571 {
572     disk_ret(regs, DISK_RET_SUCCESS);
573 }
574
575 static void
576 disk_134e04(struct bregs *regs, u8 device)
577 {
578     disk_ret(regs, DISK_RET_SUCCESS);
579 }
580
581 static void
582 disk_134e06(struct bregs *regs, u8 device)
583 {
584     disk_ret(regs, DISK_RET_SUCCESS);
585 }
586
587 static void
588 disk_134eXX(struct bregs *regs, u8 device)
589 {
590     debug_stub(regs);
591     disk_ret(regs, DISK_RET_EPARAM);
592 }
593
594 // IBM/MS set hardware configuration
595 static void
596 disk_134e(struct bregs *regs, u8 device)
597 {
598     switch (regs->al) {
599     case 0x01: disk_134e01(regs, device); break;
600     case 0x03: disk_134e03(regs, device); break;
601     case 0x04: disk_134e04(regs, device); break;
602     case 0x06: disk_134e06(regs, device); break;
603     default:   disk_134eXX(regs, device); break;
604     }
605 }
606
607 void
608 disk_13XX(struct bregs *regs, u8 device)
609 {
610     debug_stub(regs);
611     disk_ret(regs, DISK_RET_EPARAM);
612 }
613
614 void
615 disk_13(struct bregs *regs, u8 device)
616 {
617     //debug_stub(regs);
618
619     // clear completion flag
620     SET_BDA(disk_interrupt_flag, 0);
621
622     switch (regs->ah) {
623     case 0x00: disk_1300(regs, device); break;
624     case 0x01: disk_1301(regs, device); break;
625     case 0x02: disk_1302(regs, device); break;
626     case 0x03: disk_1303(regs, device); break;
627     case 0x04: disk_1304(regs, device); break;
628     case 0x05: disk_1305(regs, device); break;
629     case 0x08: disk_1308(regs, device); break;
630     case 0x09: disk_1309(regs, device); break;
631     case 0x0c: disk_130c(regs, device); break;
632     case 0x0d: disk_130d(regs, device); break;
633     case 0x10: disk_1310(regs, device); break;
634     case 0x11: disk_1311(regs, device); break;
635     case 0x14: disk_1314(regs, device); break;
636     case 0x15: disk_1315(regs, device); break;
637     case 0x41: disk_1341(regs, device); break;
638     case 0x42: disk_1342(regs, device); break;
639     case 0x43: disk_1343(regs, device); break;
640     case 0x44: disk_1344(regs, device); break;
641     case 0x45: disk_1345(regs, device); break;
642     case 0x46: disk_1346(regs, device); break;
643     case 0x47: disk_1347(regs, device); break;
644     case 0x48: disk_1348(regs, device); break;
645     case 0x49: disk_1349(regs, device); break;
646     case 0x4e: disk_134e(regs, device); break;
647     default:   disk_13XX(regs, device); break;
648     }
649 }
650
651
652 /****************************************************************
653  * Entry points
654  ****************************************************************/
655
656 static u8
657 get_device(struct bregs *regs, u8 iscd, u8 drive)
658 {
659     // basic check : device has to be defined
660     if (drive >= CONFIG_MAX_ATA_DEVICES) {
661         disk_ret(regs, DISK_RET_EPARAM);
662         return CONFIG_MAX_ATA_DEVICES;
663     }
664
665     // Get the ata channel
666     u8 device = GET_EBDA(ata.idmap[iscd][drive]);
667
668     // basic check : device has to be valid
669     if (device >= CONFIG_MAX_ATA_DEVICES) {
670         disk_ret(regs, DISK_RET_EPARAM);
671         return CONFIG_MAX_ATA_DEVICES;
672     }
673
674     return device;
675 }
676
677 static void
678 handle_legacy_disk(struct bregs *regs, u8 drive)
679 {
680     if (drive < 0x80) {
681         floppy_13(regs, drive);
682         return;
683     }
684
685     if (! CONFIG_ATA) {
686         // XXX - old code had other disk access method.
687         disk_ret(regs, DISK_RET_EPARAM);
688         return;
689     }
690
691     if (drive >= 0xe0) {
692         u8 device = get_device(regs, 1, drive - 0xe0);
693         if (device >= CONFIG_MAX_ATA_DEVICES)
694             return;
695         cdrom_13(regs, device);
696         return;
697     }
698
699     u8 device = get_device(regs, 0, drive - 0x80);
700     if (device >= CONFIG_MAX_ATA_DEVICES)
701         return;
702     disk_13(regs, device);
703 }
704
705 void VISIBLE
706 handle_40(struct bregs *regs)
707 {
708     debug_enter(regs);
709     handle_legacy_disk(regs, regs->dl);
710     debug_exit(regs);
711 }
712
713 // INT 13h Fixed Disk Services Entry Point
714 void VISIBLE
715 handle_13(struct bregs *regs)
716 {
717     debug_enter(regs);
718     u8 drive = regs->dl;
719
720     if (CONFIG_CDROM_BOOT) {
721         if (regs->ah == 0x4b) {
722             cdemu_134b(regs);
723             goto done;
724         }
725         if (GET_EBDA(cdemu.active)) {
726             if (drive == GET_EBDA(cdemu.emulated_drive)) {
727                 cdemu_13(regs);
728                 goto done;
729             }
730             if (drive < 0xe0)
731                 drive--;
732         }
733     }
734     handle_legacy_disk(regs, drive);
735 done:
736     debug_exit(regs);
737 }
738
739 // record completion in BIOS task complete flag
740 void VISIBLE
741 handle_76(struct bregs *regs)
742 {
743     debug_isr(regs);
744     SET_BDA(floppy_harddisk_info, 0xff);
745     eoi_both_pics();
746 }