Remove unused cdemu ATA code.
[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 LGPLv3 license.
7
8 #include "disk.h" // floppy_13
9 #include "biosvar.h" // SET_BDA
10 #include "config.h" // CONFIG_*
11 #include "util.h" // debug_enter
12 #include "pic.h" // eoi_pic2
13 #include "bregs.h" // struct bregs
14 #include "pci.h" // pci_bdf_to_bus
15 #include "atabits.h" // ATA_CB_STAT
16
17
18 /****************************************************************
19  * Helper functions
20  ****************************************************************/
21
22 void
23 __disk_ret(struct bregs *regs, u32 linecode, const char *fname)
24 {
25     u8 code = linecode;
26     SET_BDA(disk_last_status, code);
27     if (code)
28         __set_code_fail(regs, linecode, fname);
29     else
30         set_code_success(regs);
31 }
32
33 static void
34 __disk_stub(struct bregs *regs, int lineno, const char *fname)
35 {
36     __debug_stub(regs, lineno, fname);
37     __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
38 }
39
40 #define DISK_STUB(regs)                         \
41     __disk_stub((regs), __LINE__, __func__)
42
43 // Execute a "disk_op_s" request - this runs on a stack in the ebda.
44 static int
45 __send_disk_op(struct disk_op_s *op_far, u16 op_seg)
46 {
47     struct disk_op_s dop;
48     memcpy_far(GET_SEG(SS), &dop
49                , op_seg, op_far
50                , sizeof(dop));
51
52     dprintf(DEBUG_HDL_13, "disk_op d=%d lba=%d buf=%p count=%d cmd=%d\n"
53             , dop.driveid, (u32)dop.lba, dop.buf_fl
54             , dop.count, dop.command);
55
56     irq_enable();
57
58     int status;
59     if (!dop.command)
60         // If verify or seek
61         status = 0;
62     if (dop.command == CMD_CDROM_READ)
63         status = cdrom_read(&dop);
64     else
65         status = ata_cmd_data(&dop);
66
67     irq_disable();
68
69     // Update count with total sectors transferred.
70     SET_FARVAR(op_seg, op_far->count, dop.count);
71
72     if (status)
73         dprintf(1, "disk_op cmd %d error %d!\n", dop.command, status);
74
75     return status;
76 }
77
78 // Execute a "disk_op_s" request by jumping to a stack in the ebda.
79 static int
80 send_disk_op(struct disk_op_s *op)
81 {
82     if (! CONFIG_ATA)
83         return -1;
84
85     return stack_hop((u32)op, GET_SEG(SS), 0, __send_disk_op);
86 }
87
88 // Obtain the requested disk lba from an old-style chs request.
89 static int
90 legacy_lba(struct bregs *regs, u16 lchs_seg, struct chs_s *lchs_far)
91 {
92     u8 count = regs->al;
93     u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
94     u16 sector = regs->cl & 0x3f;
95     u16 head = regs->dh;
96
97     if (count > 128 || count == 0 || sector == 0) {
98         dprintf(1, "int13_harddisk: function %02x, parameter out of range!\n"
99                 , regs->ah);
100         disk_ret(regs, DISK_RET_EPARAM);
101         return -1;
102     }
103
104     u16 nlc = GET_FARVAR(lchs_seg, lchs_far->cylinders);
105     u16 nlh = GET_FARVAR(lchs_seg, lchs_far->heads);
106     u16 nlspt = GET_FARVAR(lchs_seg, lchs_far->spt);
107
108     // sanity check on cyl heads, sec
109     if (cylinder >= nlc || head >= nlh || sector > nlspt) {
110         dprintf(1, "int13_harddisk: function %02x, parameters out of"
111                 " range %04x/%04x/%04x!\n"
112                 , regs->ah, cylinder, head, sector);
113         disk_ret(regs, DISK_RET_EPARAM);
114         return -1;
115     }
116
117     // translate lchs to lba
118     return (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
119             + (u32)sector - 1);
120 }
121
122 // Perform read/write/verify using old-style chs accesses
123 static void
124 basic_access(struct bregs *regs, u8 device, u16 command)
125 {
126     struct disk_op_s dop;
127     dop.driveid = device;
128     dop.command = command;
129     int lba = legacy_lba(regs, get_global_seg(), &ATA.devices[device].lchs);
130     if (lba < 0)
131         return;
132     dop.lba = lba;
133     dop.count = regs->al;
134     dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
135
136     int status = send_disk_op(&dop);
137
138     regs->al = dop.count;
139
140     if (status) {
141         disk_ret(regs, DISK_RET_EBADTRACK);
142         return;
143     }
144     disk_ret(regs, DISK_RET_SUCCESS);
145 }
146
147 // Perform cdemu read/verify
148 void
149 cdemu_access(struct bregs *regs, u8 device, u16 command)
150 {
151     struct disk_op_s dop;
152     dop.driveid = device;
153     dop.command = (command == ATA_CMD_READ_SECTORS ? CMD_CDROM_READ : 0);
154     u16 ebda_seg = get_ebda_seg();
155     int vlba = legacy_lba(
156         regs, ebda_seg
157         , (void*)offsetof(struct extended_bios_data_area_s, cdemu.lchs));
158     if (vlba < 0)
159         return;
160     dop.lba = GET_EBDA2(ebda_seg, cdemu.ilba) + vlba / 4;
161     u8 count = regs->al;
162     u8 *cdbuf_far = (void*)offsetof(struct extended_bios_data_area_s, cdemu_buf);
163     u8 *dest_far = (void*)(regs->bx+0);
164     regs->al = 0;
165
166     if (vlba & 3) {
167         dop.count = 1;
168         dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
169         int status = send_disk_op(&dop);
170         if (status)
171             goto fail;
172         u8 thiscount = 4 - (vlba & 3);
173         if (thiscount > count)
174             thiscount = count;
175         count -= thiscount;
176         memcpy_far(regs->es, dest_far
177                    , ebda_seg, cdbuf_far + (vlba & 3) * 512
178                    , thiscount * 512);
179         dest_far += thiscount * 512;
180         regs->al += thiscount;
181         dop.lba++;
182     }
183
184     if (count > 3) {
185         dop.count = count / 4;
186         dop.buf_fl = MAKE_FLATPTR(regs->es, dest_far);
187         int status = send_disk_op(&dop);
188         regs->al += dop.count * 4;
189         if (status)
190             goto fail;
191         u8 thiscount = count & ~3;
192         count &= 3;
193         dest_far += thiscount * 512;
194         dop.lba += thiscount / 4;
195     }
196
197     if (count) {
198         dop.count = 1;
199         dop.buf_fl = MAKE_FLATPTR(ebda_seg, cdbuf_far);
200         int status = send_disk_op(&dop);
201         if (status)
202             goto fail;
203         u8 thiscount = count;
204         memcpy_far(regs->es, dest_far, ebda_seg, cdbuf_far, thiscount * 512);
205         regs->al += thiscount;
206     }
207
208     disk_ret(regs, DISK_RET_SUCCESS);
209     return;
210 fail:
211     disk_ret(regs, DISK_RET_EBADTRACK);
212 }
213
214 // Perform read/write/verify using new-style "int13ext" accesses.
215 static void
216 extended_access(struct bregs *regs, u8 device, u16 command)
217 {
218     struct disk_op_s dop;
219     // Get lba and check.
220     dop.lba = GET_INT13EXT(regs, lba);
221     dop.command = command;
222     dop.driveid = device;
223     u8 type = GET_GLOBAL(ATA.devices[device].type);
224     if (type == ATA_TYPE_ATA) {
225         if (dop.lba >= GET_GLOBAL(ATA.devices[device].sectors)) {
226             dprintf(1, "int13_harddisk: function %02x. LBA out of range\n"
227                     , regs->ah);
228             disk_ret(regs, DISK_RET_EPARAM);
229             return;
230         }
231     } else {
232         dop.command = CMD_CDROM_READ;
233     }
234
235     u16 segment = GET_INT13EXT(regs, segment);
236     u16 offset = GET_INT13EXT(regs, offset);
237     dop.buf_fl = MAKE_FLATPTR(segment, offset);
238     dop.count = GET_INT13EXT(regs, count);
239
240     int status = send_disk_op(&dop);
241
242     SET_INT13EXT(regs, count, dop.count);
243
244     if (status) {
245         disk_ret(regs, DISK_RET_EBADTRACK);
246         return;
247     }
248     disk_ret(regs, DISK_RET_SUCCESS);
249 }
250
251
252 /****************************************************************
253  * Hard Drive functions
254  ****************************************************************/
255
256 // disk controller reset
257 static void
258 disk_1300(struct bregs *regs, u8 device)
259 {
260     ata_reset(device);
261 }
262
263 // read disk status
264 static void
265 disk_1301(struct bregs *regs, u8 device)
266 {
267     u8 v = GET_BDA(disk_last_status);
268     regs->ah = v;
269     set_cf(regs, v);
270     // XXX - clear disk_last_status?
271 }
272
273 // read disk sectors
274 static void
275 disk_1302(struct bregs *regs, u8 device)
276 {
277     basic_access(regs, device, ATA_CMD_READ_SECTORS);
278 }
279
280 // write disk sectors
281 static void
282 disk_1303(struct bregs *regs, u8 device)
283 {
284     basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
285 }
286
287 // verify disk sectors
288 static void
289 disk_1304(struct bregs *regs, u8 device)
290 {
291     basic_access(regs, device, 0);
292     // FIXME verify
293 }
294
295 // format disk track
296 static void
297 disk_1305(struct bregs *regs, u8 device)
298 {
299     DISK_STUB(regs);
300 }
301
302 // read disk drive parameters
303 static void
304 disk_1308(struct bregs *regs, u8 device)
305 {
306     // Get logical geometry from table
307     u16 nlc = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
308     u16 nlh = GET_GLOBAL(ATA.devices[device].lchs.heads);
309     u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
310     u16 count = GET_BDA(hdcount);
311
312     nlc = nlc - 2; /* 0 based , last sector not used */
313     regs->al = 0;
314     regs->ch = nlc & 0xff;
315     regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
316     regs->dh = nlh - 1;
317     regs->dl = count; /* FIXME returns 0, 1, or n hard drives */
318
319     // FIXME should set ES & DI
320     disk_ret(regs, DISK_RET_SUCCESS);
321 }
322
323 // initialize drive parameters
324 static void
325 disk_1309(struct bregs *regs, u8 device)
326 {
327     DISK_STUB(regs);
328 }
329
330 // seek to specified cylinder
331 static void
332 disk_130c(struct bregs *regs, u8 device)
333 {
334     DISK_STUB(regs);
335 }
336
337 // alternate disk reset
338 static void
339 disk_130d(struct bregs *regs, u8 device)
340 {
341     DISK_STUB(regs);
342 }
343
344 // check drive ready
345 static void
346 disk_1310(struct bregs *regs, u8 device)
347 {
348     // should look at 40:8E also???
349
350     // Read the status from controller
351     u8 status = inb(GET_GLOBAL(ATA.channels[device/2].iobase1) + ATA_CB_STAT);
352     if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
353         disk_ret(regs, DISK_RET_SUCCESS);
354     else
355         disk_ret(regs, DISK_RET_ENOTREADY);
356 }
357
358 // recalibrate
359 static void
360 disk_1311(struct bregs *regs, u8 device)
361 {
362     DISK_STUB(regs);
363 }
364
365 // controller internal diagnostic
366 static void
367 disk_1314(struct bregs *regs, u8 device)
368 {
369     DISK_STUB(regs);
370 }
371
372 // read disk drive size
373 static void
374 disk_1315(struct bregs *regs, u8 device)
375 {
376     // Get logical geometry from table
377     u16 nlc   = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
378     u16 nlh   = GET_GLOBAL(ATA.devices[device].lchs.heads);
379     u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
380
381     // Compute sector count seen by int13
382     u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
383     regs->cx = lba >> 16;
384     regs->dx = lba & 0xffff;
385
386     disk_ret(regs, DISK_RET_SUCCESS);
387     regs->ah = 3; // hard disk accessible
388 }
389
390 // IBM/MS installation check
391 static void
392 disk_1341(struct bregs *regs, u8 device)
393 {
394     regs->bx = 0xaa55;  // install check
395     regs->cx = 0x0007;  // ext disk access and edd, removable supported
396     disk_ret(regs, DISK_RET_SUCCESS);
397     regs->ah = 0x30;    // EDD 3.0
398 }
399
400 // IBM/MS extended read
401 static void
402 disk_1342(struct bregs *regs, u8 device)
403 {
404     extended_access(regs, device, ATA_CMD_READ_SECTORS);
405 }
406
407 // IBM/MS extended write
408 static void
409 disk_1343(struct bregs *regs, u8 device)
410 {
411     extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
412 }
413
414 // IBM/MS verify
415 static void
416 disk_1344(struct bregs *regs, u8 device)
417 {
418     extended_access(regs, device, 0);
419 }
420
421 // IBM/MS lock/unlock drive
422 static void
423 disk_1345(struct bregs *regs, u8 device)
424 {
425     // Always success for HD
426     disk_ret(regs, DISK_RET_SUCCESS);
427 }
428
429 // IBM/MS eject media
430 static void
431 disk_1346(struct bregs *regs, u8 device)
432 {
433     // Volume Not Removable
434     disk_ret(regs, DISK_RET_ENOTREMOVABLE);
435 }
436
437 // IBM/MS extended seek
438 static void
439 disk_1347(struct bregs *regs, u8 device)
440 {
441     extended_access(regs, device, 0);
442 }
443
444 // IBM/MS get drive parameters
445 static void
446 disk_1348(struct bregs *regs, u8 device)
447 {
448     u16 size = GET_INT13DPT(regs, size);
449
450     // Buffer is too small
451     if (size < 26) {
452         disk_ret(regs, DISK_RET_EPARAM);
453         return;
454     }
455
456     // EDD 1.x
457
458     u8  type    = GET_GLOBAL(ATA.devices[device].type);
459     u16 npc     = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
460     u16 nph     = GET_GLOBAL(ATA.devices[device].pchs.heads);
461     u16 npspt   = GET_GLOBAL(ATA.devices[device].pchs.spt);
462     u64 lba     = GET_GLOBAL(ATA.devices[device].sectors);
463     u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
464
465     dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
466             , size, type, npc, nph, npspt, (u32)lba, blksize);
467
468     SET_INT13DPT(regs, size, 26);
469     if (type == ATA_TYPE_ATA) {
470         if (lba > (u64)npspt*nph*0x3fff) {
471             SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
472             SET_INT13DPT(regs, cylinders, 0x3fff);
473         } else {
474             SET_INT13DPT(regs, infos, 0x02); // geometry is valid
475             SET_INT13DPT(regs, cylinders, (u32)npc);
476         }
477         SET_INT13DPT(regs, heads, (u32)nph);
478         SET_INT13DPT(regs, spt, (u32)npspt);
479         SET_INT13DPT(regs, sector_count, lba);
480     } else {
481         // ATAPI
482         // 0x74 = removable, media change, lockable, max values
483         SET_INT13DPT(regs, infos, 0x74);
484         SET_INT13DPT(regs, cylinders, 0xffffffff);
485         SET_INT13DPT(regs, heads, 0xffffffff);
486         SET_INT13DPT(regs, spt, 0xffffffff);
487         SET_INT13DPT(regs, sector_count, (u64)-1);
488     }
489     SET_INT13DPT(regs, blksize, blksize);
490
491     if (size < 30) {
492         disk_ret(regs, DISK_RET_SUCCESS);
493         return;
494     }
495
496     // EDD 2.x
497
498     u16 ebda_seg = get_ebda_seg();
499     SET_INT13DPT(regs, size, 30);
500
501     SET_INT13DPT(regs, dpte_segment, ebda_seg);
502     SET_INT13DPT(regs, dpte_offset
503                  , offsetof(struct extended_bios_data_area_s, dpte));
504
505     // Fill in dpte
506     u8 channel = device / 2;
507     u8 slave = device % 2;
508     u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
509     u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
510     u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
511
512     u16 options = 0;
513     if (type == ATA_TYPE_ATA) {
514         u8 translation = GET_GLOBAL(ATA.devices[device].translation);
515         if (translation != ATA_TRANSLATION_NONE) {
516             options |= 1<<3; // CHS translation
517             if (translation == ATA_TRANSLATION_LBA)
518                 options |= 1<<9;
519             if (translation == ATA_TRANSLATION_RECHS)
520                 options |= 3<<9;
521         }
522     } else {
523         // ATAPI
524         options |= 1<<5; // removable device
525         options |= 1<<6; // atapi device
526     }
527     options |= 1<<4; // lba translation
528     if (CONFIG_ATA_PIO32)
529         options |= 1<<7;
530
531     SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
532     SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
533     SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
534                                       | ATA_CB_DH_LBA));
535     SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
536     SET_EBDA2(ebda_seg, dpte.irq, irq);
537     SET_EBDA2(ebda_seg, dpte.blkcount, 1);
538     SET_EBDA2(ebda_seg, dpte.dma, 0);
539     SET_EBDA2(ebda_seg, dpte.pio, 0);
540     SET_EBDA2(ebda_seg, dpte.options, options);
541     SET_EBDA2(ebda_seg, dpte.reserved, 0);
542     SET_EBDA2(ebda_seg, dpte.revision, 0x11);
543
544     u8 sum = checksum_far(
545         ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
546     SET_EBDA2(ebda_seg, dpte.checksum, -sum);
547
548     if (size < 66) {
549         disk_ret(regs, DISK_RET_SUCCESS);
550         return;
551     }
552
553     // EDD 3.x
554     SET_INT13DPT(regs, key, 0xbedd);
555     SET_INT13DPT(regs, dpi_length, 36);
556     SET_INT13DPT(regs, reserved1, 0);
557     SET_INT13DPT(regs, reserved2, 0);
558
559     SET_INT13DPT(regs, host_bus[0], 'P');
560     SET_INT13DPT(regs, host_bus[1], 'C');
561     SET_INT13DPT(regs, host_bus[2], 'I');
562     SET_INT13DPT(regs, host_bus[3], 0);
563
564     u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
565     u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
566                 | (pci_bdf_to_fn(bdf) << 16));
567     SET_INT13DPT(regs, iface_path, path);
568
569     SET_INT13DPT(regs, iface_type[0], 'A');
570     SET_INT13DPT(regs, iface_type[1], 'T');
571     SET_INT13DPT(regs, iface_type[2], 'A');
572     SET_INT13DPT(regs, iface_type[3], 0);
573     SET_INT13DPT(regs, iface_type[4], 0);
574     SET_INT13DPT(regs, iface_type[5], 0);
575     SET_INT13DPT(regs, iface_type[6], 0);
576     SET_INT13DPT(regs, iface_type[7], 0);
577
578     SET_INT13DPT(regs, device_path, slave);
579
580     SET_INT13DPT(regs, checksum
581                  , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
582
583     disk_ret(regs, DISK_RET_SUCCESS);
584 }
585
586 // IBM/MS extended media change
587 static void
588 disk_1349(struct bregs *regs, u8 device)
589 {
590     // Always success for HD
591     disk_ret(regs, DISK_RET_SUCCESS);
592 }
593
594 static void
595 disk_134e01(struct bregs *regs, u8 device)
596 {
597     disk_ret(regs, DISK_RET_SUCCESS);
598 }
599
600 static void
601 disk_134e03(struct bregs *regs, u8 device)
602 {
603     disk_ret(regs, DISK_RET_SUCCESS);
604 }
605
606 static void
607 disk_134e04(struct bregs *regs, u8 device)
608 {
609     disk_ret(regs, DISK_RET_SUCCESS);
610 }
611
612 static void
613 disk_134e06(struct bregs *regs, u8 device)
614 {
615     disk_ret(regs, DISK_RET_SUCCESS);
616 }
617
618 static void
619 disk_134eXX(struct bregs *regs, u8 device)
620 {
621     disk_ret(regs, DISK_RET_EPARAM);
622 }
623
624 // IBM/MS set hardware configuration
625 static void
626 disk_134e(struct bregs *regs, u8 device)
627 {
628     switch (regs->al) {
629     case 0x01: disk_134e01(regs, device); break;
630     case 0x03: disk_134e03(regs, device); break;
631     case 0x04: disk_134e04(regs, device); break;
632     case 0x06: disk_134e06(regs, device); break;
633     default:   disk_134eXX(regs, device); break;
634     }
635 }
636
637 void
638 disk_13XX(struct bregs *regs, u8 device)
639 {
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 iscd, 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_GLOBAL(ATA.idmap[iscd][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, 1, 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, 0, drive - 0x80);
729     if (device >= CONFIG_MAX_ATA_DEVICES)
730         return;
731     disk_13(regs, device);
732 }
733
734 void VISIBLE16
735 handle_40(struct bregs *regs)
736 {
737     debug_enter(regs, DEBUG_HDL_40);
738     handle_legacy_disk(regs, regs->dl);
739 }
740
741 // INT 13h Fixed Disk Services Entry Point
742 void VISIBLE16
743 handle_13(struct bregs *regs)
744 {
745     debug_enter(regs, DEBUG_HDL_13);
746     u8 drive = regs->dl;
747
748     if (CONFIG_CDROM_EMU) {
749         if (regs->ah == 0x4b) {
750             cdemu_134b(regs);
751             return;
752         }
753         u16 ebda_seg = get_ebda_seg();
754         if (GET_EBDA2(ebda_seg, cdemu.active)) {
755             u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
756             if (drive == emudrive) {
757                 cdemu_13(regs);
758                 return;
759             }
760             if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
761                 drive--;
762         }
763     }
764     handle_legacy_disk(regs, drive);
765 }
766
767 // record completion in BIOS task complete flag
768 void VISIBLE16
769 handle_76()
770 {
771     debug_isr(DEBUG_ISR_76);
772     SET_BDA(disk_interrupt_flag, 0xff);
773     eoi_pic2();
774 }
775
776 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
777 struct fdpt_s OldFDPT VAR16FIXED(0xe401);