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