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