Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / 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 "biosvar.h" // SET_BDA
9 #include "bregs.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "hw/ata.h" // ATA_CB_DC
12 #include "hw/pic.h" // pic_eoi2
13 #include "output.h" // debug_enter
14 #include "stacks.h" // call16_int
15 #include "std/disk.h" // DISK_RET_SUCCESS
16 #include "string.h" // memset
17 #include "util.h" // CDRom_locks
18
19
20 /****************************************************************
21  * Return status functions
22  ****************************************************************/
23
24 static void
25 __disk_ret(struct bregs *regs, u32 linecode, const char *fname)
26 {
27     u8 code = linecode;
28     if (regs->dl < EXTSTART_HD)
29         SET_BDA(floppy_last_status, code);
30     else
31         SET_BDA(disk_last_status, code);
32     if (code)
33         __set_code_invalid(regs, linecode, fname);
34     else
35         set_code_success(regs);
36 }
37
38 static void
39 __disk_ret_unimplemented(struct bregs *regs, u32 linecode, const char *fname)
40 {
41     u8 code = linecode;
42     if (regs->dl < EXTSTART_HD)
43         SET_BDA(floppy_last_status, code);
44     else
45         SET_BDA(disk_last_status, code);
46     __set_code_unimplemented(regs, linecode, fname);
47 }
48
49 static void
50 __disk_stub(struct bregs *regs, int lineno, const char *fname)
51 {
52     __warn_unimplemented(regs, lineno, fname);
53     __disk_ret(regs, DISK_RET_SUCCESS | (lineno << 8), fname);
54 }
55
56 #define disk_ret(regs, code) \
57     __disk_ret((regs), (code) | (__LINE__ << 8), __func__)
58 #define disk_ret_unimplemented(regs, code) \
59     __disk_ret_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
60 #define DISK_STUB(regs)                         \
61     __disk_stub((regs), __LINE__, __func__)
62
63
64 /****************************************************************
65  * Helper functions
66  ****************************************************************/
67
68 // Get the cylinders/heads/sectors for the given drive.
69 static struct chs_s
70 getLCHS(struct drive_s *drive_gf)
71 {
72     struct chs_s res = { };
73     if (CONFIG_CDROM_EMU && drive_gf == GET_GLOBAL(cdemu_drive_gf)) {
74         // Emulated drive - get info from CDEmu.  (It's not possible to
75         // populate the geometry directly in the driveid because the
76         // geometry is only known after the bios segment is made
77         // read-only).
78         u8 sptcyl = GET_LOW(CDEmu.chs.sptcyl);
79         res.cylinder = GET_LOW(CDEmu.chs.cyllow) + ((sptcyl << 2) & 0x300) + 1;
80         res.head = GET_LOW(CDEmu.chs.heads) + 1;
81         res.sector = sptcyl & 0x3f;
82         return res;
83     }
84     res.cylinder = GET_GLOBALFLAT(drive_gf->lchs.cylinder);
85     res.head = GET_GLOBALFLAT(drive_gf->lchs.head);
86     res.sector = GET_GLOBALFLAT(drive_gf->lchs.sector);
87     return res;
88 }
89
90 // Perform read/write/verify using old-style chs accesses
91 static void noinline
92 basic_access(struct bregs *regs, struct drive_s *drive_gf, u16 command)
93 {
94     struct disk_op_s dop;
95     dop.drive_gf = drive_gf;
96     dop.command = command;
97
98     u8 count = regs->al;
99     u16 cylinder = regs->ch | ((((u16)regs->cl) << 2) & 0x300);
100     u16 sector = regs->cl & 0x3f;
101     u16 head = regs->dh;
102
103     if (count > 128 || count == 0 || sector == 0) {
104         warn_invalid(regs);
105         disk_ret(regs, DISK_RET_EPARAM);
106         return;
107     }
108     dop.count = count;
109
110     struct chs_s chs = getLCHS(drive_gf);
111     u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
112
113     // sanity check on cyl heads, sec
114     if (cylinder >= nlc || head >= nlh || sector > nls) {
115         warn_invalid(regs);
116         disk_ret(regs, DISK_RET_EPARAM);
117         return;
118     }
119
120     // translate lchs to lba
121     dop.lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nls)
122                + (u32)sector - 1);
123
124     dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
125
126     int status = send_disk_op(&dop);
127
128     regs->al = dop.count;
129
130     disk_ret(regs, status);
131 }
132
133 // Perform read/write/verify using new-style "int13ext" accesses.
134 static void noinline
135 extended_access(struct bregs *regs, struct drive_s *drive_gf, u16 command)
136 {
137     struct disk_op_s dop;
138     struct int13ext_s *param_far = (struct int13ext_s*)(regs->si+0);
139     // Get lba and check.
140     dop.lba = GET_FARVAR(regs->ds, param_far->lba);
141     dop.command = command;
142     dop.drive_gf = drive_gf;
143     if (dop.lba >= GET_GLOBALFLAT(drive_gf->sectors)) {
144         warn_invalid(regs);
145         disk_ret(regs, DISK_RET_EPARAM);
146         return;
147     }
148
149     dop.buf_fl = SEGOFF_TO_FLATPTR(GET_FARVAR(regs->ds, param_far->data));
150     dop.count = GET_FARVAR(regs->ds, param_far->count);
151     if (! dop.count) {
152         // Nothing to do.
153         disk_ret(regs, DISK_RET_SUCCESS);
154         return;
155     }
156
157     int status = send_disk_op(&dop);
158
159     SET_FARVAR(regs->ds, param_far->count, dop.count);
160
161     disk_ret(regs, status);
162 }
163
164
165 /****************************************************************
166  * Hard Drive functions
167  ****************************************************************/
168
169 // disk controller reset
170 static void
171 disk_1300(struct bregs *regs, struct drive_s *drive_gf)
172 {
173     struct disk_op_s dop;
174     dop.drive_gf = drive_gf;
175     dop.command = CMD_RESET;
176     dop.count = 0;
177     int status = send_disk_op(&dop);
178     disk_ret(regs, status);
179 }
180
181 // read disk status
182 static void
183 disk_1301(struct bregs *regs, struct drive_s *drive_gf)
184 {
185     u8 v;
186     if (regs->dl < EXTSTART_HD)
187         // Floppy
188         v = GET_BDA(floppy_last_status);
189     else
190         v = GET_BDA(disk_last_status);
191     regs->ah = v;
192     set_cf(regs, v);
193     // XXX - clear disk_last_status?
194 }
195
196 // read disk sectors
197 static void
198 disk_1302(struct bregs *regs, struct drive_s *drive_gf)
199 {
200     basic_access(regs, drive_gf, CMD_READ);
201 }
202
203 // write disk sectors
204 static void
205 disk_1303(struct bregs *regs, struct drive_s *drive_gf)
206 {
207     basic_access(regs, drive_gf, CMD_WRITE);
208 }
209
210 // verify disk sectors
211 static void
212 disk_1304(struct bregs *regs, struct drive_s *drive_gf)
213 {
214     basic_access(regs, drive_gf, CMD_VERIFY);
215 }
216
217 // format disk track
218 static void noinline
219 disk_1305(struct bregs *regs, struct drive_s *drive_gf)
220 {
221     debug_stub(regs);
222
223     struct chs_s chs = getLCHS(drive_gf);
224     u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
225
226     u8 count = regs->al;
227     u8 cylinder = regs->ch;
228     u8 head = regs->dh;
229
230     if (cylinder >= nlc || head >= nlh || count == 0 || count > nls) {
231         disk_ret(regs, DISK_RET_EPARAM);
232         return;
233     }
234
235     struct disk_op_s dop;
236     dop.drive_gf = drive_gf;
237     dop.command = CMD_FORMAT;
238     dop.lba = (((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nls;
239     dop.count = count;
240     dop.buf_fl = MAKE_FLATPTR(regs->es, regs->bx);
241     int status = send_disk_op(&dop);
242     disk_ret(regs, status);
243 }
244
245 // read disk drive parameters
246 static void noinline
247 disk_1308(struct bregs *regs, struct drive_s *drive_gf)
248 {
249     // Get logical geometry from table
250     struct chs_s chs = getLCHS(drive_gf);
251     u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
252     nlc--;
253     nlh--;
254     u8 count;
255     if (regs->dl < EXTSTART_HD) {
256         // Floppy
257         count = GET_GLOBAL(FloppyCount);
258
259         if (CONFIG_CDROM_EMU && drive_gf == GET_GLOBAL(cdemu_drive_gf))
260             regs->bx = GET_LOW(CDEmu.media) * 2;
261         else
262             regs->bx = GET_GLOBALFLAT(drive_gf->floppy_type);
263
264         // set es & di to point to 11 byte diskette param table in ROM
265         regs->es = SEG_BIOS;
266         regs->di = (u32)&diskette_param_table2;
267     } else if (regs->dl < EXTSTART_CD) {
268         // Hard drive
269         count = GET_BDA(hdcount);
270         nlc--;  // last sector reserved
271     } else {
272         // Not supported on CDROM
273         disk_ret(regs, DISK_RET_EPARAM);
274         return;
275     }
276
277     if (CONFIG_CDROM_EMU && GET_LOW(CDEmu.media)) {
278         u8 emudrive = GET_LOW(CDEmu.emulated_drive);
279         if (((emudrive ^ regs->dl) & 0x80) == 0)
280             // Note extra drive due to emulation.
281             count++;
282         if (regs->dl < EXTSTART_HD && count > 2)
283             // Max of two floppy drives.
284             count = 2;
285     }
286
287     regs->al = 0;
288     regs->ch = nlc & 0xff;
289     regs->cl = ((nlc >> 2) & 0xc0) | (nls & 0x3f);
290     regs->dh = nlh;
291
292     disk_ret(regs, DISK_RET_SUCCESS);
293     regs->dl = count;
294 }
295
296 // initialize drive parameters
297 static void
298 disk_1309(struct bregs *regs, struct drive_s *drive_gf)
299 {
300     DISK_STUB(regs);
301 }
302
303 // seek to specified cylinder
304 static void
305 disk_130c(struct bregs *regs, struct drive_s *drive_gf)
306 {
307     DISK_STUB(regs);
308 }
309
310 // alternate disk reset
311 static void
312 disk_130d(struct bregs *regs, struct drive_s *drive_gf)
313 {
314     DISK_STUB(regs);
315 }
316
317 // check drive ready
318 static void
319 disk_1310(struct bregs *regs, struct drive_s *drive_gf)
320 {
321     // should look at 40:8E also???
322
323     struct disk_op_s dop;
324     dop.drive_gf = drive_gf;
325     dop.command = CMD_ISREADY;
326     dop.count = 0;
327     int status = send_disk_op(&dop);
328     disk_ret(regs, status);
329 }
330
331 // recalibrate
332 static void
333 disk_1311(struct bregs *regs, struct drive_s *drive_gf)
334 {
335     DISK_STUB(regs);
336 }
337
338 // controller internal diagnostic
339 static void
340 disk_1314(struct bregs *regs, struct drive_s *drive_gf)
341 {
342     DISK_STUB(regs);
343 }
344
345 // read disk drive size
346 static void noinline
347 disk_1315(struct bregs *regs, struct drive_s *drive_gf)
348 {
349     disk_ret(regs, DISK_RET_SUCCESS);
350     if (regs->dl < EXTSTART_HD || regs->dl >= EXTSTART_CD) {
351         // Floppy or cdrom
352         regs->ah = 1;
353         return;
354     }
355     // Hard drive
356
357     // Get logical geometry from table
358     struct chs_s chs = getLCHS(drive_gf);
359     u16 nlc=chs.cylinder, nlh=chs.head, nls=chs.sector;
360
361     // Compute sector count seen by int13
362     u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nls;
363     regs->cx = lba >> 16;
364     regs->dx = lba & 0xffff;
365     regs->ah = 3; // hard disk accessible
366 }
367
368 static void
369 disk_1316(struct bregs *regs, struct drive_s *drive_gf)
370 {
371     if (regs->dl >= EXTSTART_HD) {
372         // Hard drive
373         disk_ret(regs, DISK_RET_EPARAM);
374         return;
375     }
376     disk_ret(regs, DISK_RET_ECHANGED);
377 }
378
379 // IBM/MS installation check
380 static void
381 disk_1341(struct bregs *regs, struct drive_s *drive_gf)
382 {
383     regs->bx = 0xaa55;  // install check
384     regs->cx = 0x0007;  // ext disk access and edd, removable supported
385     disk_ret(regs, DISK_RET_SUCCESS);
386     regs->ah = 0x30;    // EDD 3.0
387 }
388
389 // IBM/MS extended read
390 static void
391 disk_1342(struct bregs *regs, struct drive_s *drive_gf)
392 {
393     extended_access(regs, drive_gf, CMD_READ);
394 }
395
396 // IBM/MS extended write
397 static void
398 disk_1343(struct bregs *regs, struct drive_s *drive_gf)
399 {
400     extended_access(regs, drive_gf, CMD_WRITE);
401 }
402
403 // IBM/MS verify
404 static void
405 disk_1344(struct bregs *regs, struct drive_s *drive_gf)
406 {
407     extended_access(regs, drive_gf, CMD_VERIFY);
408 }
409
410 // lock
411 static void
412 disk_134500(struct bregs *regs, struct drive_s *drive_gf)
413 {
414     int cdid = regs->dl - EXTSTART_CD;
415     u8 locks = GET_LOW(CDRom_locks[cdid]);
416     if (locks == 0xff) {
417         regs->al = 1;
418         disk_ret(regs, DISK_RET_ETOOMANYLOCKS);
419         return;
420     }
421     SET_LOW(CDRom_locks[cdid], locks + 1);
422     regs->al = 1;
423     disk_ret(regs, DISK_RET_SUCCESS);
424 }
425
426 // unlock
427 static void
428 disk_134501(struct bregs *regs, struct drive_s *drive_gf)
429 {
430     int cdid = regs->dl - EXTSTART_CD;
431     u8 locks = GET_LOW(CDRom_locks[cdid]);
432     if (locks == 0x00) {
433         regs->al = 0;
434         disk_ret(regs, DISK_RET_ENOTLOCKED);
435         return;
436     }
437     locks--;
438     SET_LOW(CDRom_locks[cdid], locks);
439     regs->al = (locks ? 1 : 0);
440     disk_ret(regs, DISK_RET_SUCCESS);
441 }
442
443 // status
444 static void
445 disk_134502(struct bregs *regs, struct drive_s *drive_gf)
446 {
447     int cdid = regs->dl - EXTSTART_CD;
448     u8 locks = GET_LOW(CDRom_locks[cdid]);
449     regs->al = (locks ? 1 : 0);
450     disk_ret(regs, DISK_RET_SUCCESS);
451 }
452
453 static void
454 disk_1345XX(struct bregs *regs, struct drive_s *drive_gf)
455 {
456     disk_ret_unimplemented(regs, DISK_RET_EPARAM);
457 }
458
459 // IBM/MS lock/unlock drive
460 static void
461 disk_1345(struct bregs *regs, struct drive_s *drive_gf)
462 {
463     if (regs->dl < EXTSTART_CD) {
464         // Always success for HD
465         disk_ret(regs, DISK_RET_SUCCESS);
466         return;
467     }
468
469     switch (regs->al) {
470     case 0x00: disk_134500(regs, drive_gf); break;
471     case 0x01: disk_134501(regs, drive_gf); break;
472     case 0x02: disk_134502(regs, drive_gf); break;
473     default:   disk_1345XX(regs, drive_gf); break;
474     }
475 }
476
477 // IBM/MS eject media
478 static void noinline
479 disk_1346(struct bregs *regs, struct drive_s *drive_gf)
480 {
481     if (regs->dl < EXTSTART_CD) {
482         // Volume Not Removable
483         disk_ret(regs, DISK_RET_ENOTREMOVABLE);
484         return;
485     }
486
487     int cdid = regs->dl - EXTSTART_CD;
488     u8 locks = GET_LOW(CDRom_locks[cdid]);
489     if (locks != 0) {
490         disk_ret(regs, DISK_RET_ELOCKED);
491         return;
492     }
493
494     // FIXME should handle 0x31 no media in device
495     // FIXME should handle 0xb5 valid request failed
496
497     // Call removable media eject
498     struct bregs br;
499     memset(&br, 0, sizeof(br));
500     br.ah = 0x52;
501     br.dl = regs->dl;
502     call16_int(0x15, &br);
503
504     if (br.ah || br.flags & F_CF) {
505         disk_ret(regs, DISK_RET_ELOCKED);
506         return;
507     }
508     disk_ret(regs, DISK_RET_SUCCESS);
509 }
510
511 // IBM/MS extended seek
512 static void
513 disk_1347(struct bregs *regs, struct drive_s *drive_gf)
514 {
515     extended_access(regs, drive_gf, CMD_SEEK);
516 }
517
518 // IBM/MS get drive parameters
519 static void
520 disk_1348(struct bregs *regs, struct drive_s *drive_gf)
521 {
522     int ret = fill_edd(regs->ds, (void*)(regs->si+0), drive_gf);
523     disk_ret(regs, ret);
524 }
525
526 // IBM/MS extended media change
527 static void
528 disk_1349(struct bregs *regs, struct drive_s *drive_gf)
529 {
530     if (regs->dl < EXTSTART_CD) {
531         // Always success for HD
532         disk_ret(regs, DISK_RET_SUCCESS);
533         return;
534     }
535     set_invalid(regs);
536     // always send changed ??
537     regs->ah = DISK_RET_ECHANGED;
538 }
539
540 static void
541 disk_134e01(struct bregs *regs, struct drive_s *drive_gf)
542 {
543     disk_ret(regs, DISK_RET_SUCCESS);
544 }
545
546 static void
547 disk_134e03(struct bregs *regs, struct drive_s *drive_gf)
548 {
549     disk_ret(regs, DISK_RET_SUCCESS);
550 }
551
552 static void
553 disk_134e04(struct bregs *regs, struct drive_s *drive_gf)
554 {
555     disk_ret(regs, DISK_RET_SUCCESS);
556 }
557
558 static void
559 disk_134e06(struct bregs *regs, struct drive_s *drive_gf)
560 {
561     disk_ret(regs, DISK_RET_SUCCESS);
562 }
563
564 static void
565 disk_134eXX(struct bregs *regs, struct drive_s *drive_gf)
566 {
567     disk_ret(regs, DISK_RET_EPARAM);
568 }
569
570 // IBM/MS set hardware configuration
571 static void
572 disk_134e(struct bregs *regs, struct drive_s *drive_gf)
573 {
574     switch (regs->al) {
575     case 0x01: disk_134e01(regs, drive_gf); break;
576     case 0x03: disk_134e03(regs, drive_gf); break;
577     case 0x04: disk_134e04(regs, drive_gf); break;
578     case 0x06: disk_134e06(regs, drive_gf); break;
579     default:   disk_134eXX(regs, drive_gf); break;
580     }
581 }
582
583 static void
584 disk_13XX(struct bregs *regs, struct drive_s *drive_gf)
585 {
586     disk_ret_unimplemented(regs, DISK_RET_EPARAM);
587 }
588
589 static void
590 disk_13(struct bregs *regs, struct drive_s *drive_gf)
591 {
592     //debug_stub(regs);
593
594     // clear completion flag
595     SET_BDA(disk_interrupt_flag, 0);
596
597     switch (regs->ah) {
598     case 0x00: disk_1300(regs, drive_gf); break;
599     case 0x01: disk_1301(regs, drive_gf); break;
600     case 0x02: disk_1302(regs, drive_gf); break;
601     case 0x03: disk_1303(regs, drive_gf); break;
602     case 0x04: disk_1304(regs, drive_gf); break;
603     case 0x05: disk_1305(regs, drive_gf); break;
604     case 0x08: disk_1308(regs, drive_gf); break;
605     case 0x09: disk_1309(regs, drive_gf); break;
606     case 0x0c: disk_130c(regs, drive_gf); break;
607     case 0x0d: disk_130d(regs, drive_gf); break;
608     case 0x10: disk_1310(regs, drive_gf); break;
609     case 0x11: disk_1311(regs, drive_gf); break;
610     case 0x14: disk_1314(regs, drive_gf); break;
611     case 0x15: disk_1315(regs, drive_gf); break;
612     case 0x16: disk_1316(regs, drive_gf); break;
613     case 0x41: disk_1341(regs, drive_gf); break;
614     case 0x42: disk_1342(regs, drive_gf); break;
615     case 0x43: disk_1343(regs, drive_gf); break;
616     case 0x44: disk_1344(regs, drive_gf); break;
617     case 0x45: disk_1345(regs, drive_gf); break;
618     case 0x46: disk_1346(regs, drive_gf); break;
619     case 0x47: disk_1347(regs, drive_gf); break;
620     case 0x48: disk_1348(regs, drive_gf); break;
621     case 0x49: disk_1349(regs, drive_gf); break;
622     case 0x4e: disk_134e(regs, drive_gf); break;
623     default:   disk_13XX(regs, drive_gf); break;
624     }
625 }
626
627 static void
628 floppy_13(struct bregs *regs, struct drive_s *drive_gf)
629 {
630     // Only limited commands are supported on floppies.
631     switch (regs->ah) {
632     case 0x00:
633     case 0x01:
634     case 0x02:
635     case 0x03:
636     case 0x04:
637     case 0x05:
638     case 0x08:
639     case 0x15:
640     case 0x16:
641         disk_13(regs, drive_gf);
642         break;
643     default:   disk_13XX(regs, drive_gf); break;
644     }
645 }
646
647 // ElTorito - Terminate disk emu
648 static void
649 cdemu_134b(struct bregs *regs)
650 {
651     memcpy_far(regs->ds, (void*)(regs->si+0), SEG_LOW, &CDEmu, sizeof(CDEmu));
652
653     // If we have to terminate emulation
654     if (regs->al == 0x00) {
655         // FIXME ElTorito Various. Should be handled accordingly to spec
656         SET_LOW(CDEmu.media, 0x00); // bye bye
657
658         // XXX - update floppy/hd count.
659     }
660
661     disk_ret(regs, DISK_RET_SUCCESS);
662 }
663
664
665 /****************************************************************
666  * Entry points
667  ****************************************************************/
668
669 static void
670 handle_legacy_disk(struct bregs *regs, u8 extdrive)
671 {
672     if (! CONFIG_DRIVES) {
673         // XXX - support handle_1301 anyway?
674         disk_ret(regs, DISK_RET_EPARAM);
675         return;
676     }
677
678     if (extdrive < EXTSTART_HD) {
679         struct drive_s *drive_gf = getDrive(EXTTYPE_FLOPPY, extdrive);
680         if (!drive_gf)
681             goto fail;
682         floppy_13(regs, drive_gf);
683         return;
684     }
685
686     struct drive_s *drive_gf;
687     if (extdrive >= EXTSTART_CD)
688         drive_gf = getDrive(EXTTYPE_CD, extdrive - EXTSTART_CD);
689     else
690         drive_gf = getDrive(EXTTYPE_HD, extdrive - EXTSTART_HD);
691     if (!drive_gf)
692         goto fail;
693     disk_13(regs, drive_gf);
694     return;
695
696 fail:
697     // XXX - support 1301/1308/1315 anyway?
698     disk_ret(regs, DISK_RET_EPARAM);
699 }
700
701 void VISIBLE16
702 handle_40(struct bregs *regs)
703 {
704     debug_enter(regs, DEBUG_HDL_40);
705     handle_legacy_disk(regs, regs->dl);
706 }
707
708 // INT 13h Fixed Disk Services Entry Point
709 void VISIBLE16
710 handle_13(struct bregs *regs)
711 {
712     debug_enter(regs, DEBUG_HDL_13);
713     u8 extdrive = regs->dl;
714
715     if (CONFIG_CDROM_EMU) {
716         if (regs->ah == 0x4b) {
717             cdemu_134b(regs);
718             return;
719         }
720         if (GET_LOW(CDEmu.media)) {
721             u8 emudrive = GET_LOW(CDEmu.emulated_drive);
722             if (extdrive == emudrive) {
723                 // Access to an emulated drive.
724                 struct drive_s *cdemu_gf = GET_GLOBAL(cdemu_drive_gf);
725                 if (regs->ah > 0x16) {
726                     // Only old-style commands supported.
727                     disk_13XX(regs, cdemu_gf);
728                     return;
729                 }
730                 disk_13(regs, cdemu_gf);
731                 return;
732             }
733             if (extdrive < EXTSTART_CD && ((emudrive ^ extdrive) & 0x80) == 0)
734                 // Adjust id to make room for emulated drive.
735                 extdrive--;
736         }
737     }
738     handle_legacy_disk(regs, extdrive);
739 }
740
741 // record completion in BIOS task complete flag
742 void VISIBLE16
743 handle_76(void)
744 {
745     debug_isr(DEBUG_ISR_76);
746     SET_BDA(disk_interrupt_flag, 0xff);
747     pic_eoi2();
748 }