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