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