Misc ATA cleanups.
[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_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     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 == DTYPE_ATA)
61         status = process_ata_op(&dop);
62     else if (type == DTYPE_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     struct disk_op_s dop;
347     dop.driveid = device;
348     dop.command = CMD_ISREADY;
349     int status = send_disk_op(&dop);
350     if (status)
351         disk_ret(regs, DISK_RET_ENOTREADY);
352     else
353         disk_ret(regs, DISK_RET_SUCCESS);
354 }
355
356 // recalibrate
357 static void
358 disk_1311(struct bregs *regs, u8 device)
359 {
360     DISK_STUB(regs);
361 }
362
363 // controller internal diagnostic
364 static void
365 disk_1314(struct bregs *regs, u8 device)
366 {
367     DISK_STUB(regs);
368 }
369
370 // read disk drive size
371 static void
372 disk_1315(struct bregs *regs, u8 device)
373 {
374     // Get logical geometry from table
375     u16 nlc   = GET_GLOBAL(ATA.devices[device].lchs.cylinders);
376     u16 nlh   = GET_GLOBAL(ATA.devices[device].lchs.heads);
377     u16 nlspt = GET_GLOBAL(ATA.devices[device].lchs.spt);
378
379     // Compute sector count seen by int13
380     u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
381     regs->cx = lba >> 16;
382     regs->dx = lba & 0xffff;
383
384     disk_ret(regs, DISK_RET_SUCCESS);
385     regs->ah = 3; // hard disk accessible
386 }
387
388 // IBM/MS installation check
389 static void
390 disk_1341(struct bregs *regs, u8 device)
391 {
392     regs->bx = 0xaa55;  // install check
393     regs->cx = 0x0007;  // ext disk access and edd, removable supported
394     disk_ret(regs, DISK_RET_SUCCESS);
395     regs->ah = 0x30;    // EDD 3.0
396 }
397
398 // IBM/MS extended read
399 static void
400 disk_1342(struct bregs *regs, u8 device)
401 {
402     extended_access(regs, device, CMD_READ);
403 }
404
405 // IBM/MS extended write
406 static void
407 disk_1343(struct bregs *regs, u8 device)
408 {
409     extended_access(regs, device, CMD_WRITE);
410 }
411
412 // IBM/MS verify
413 static void
414 disk_1344(struct bregs *regs, u8 device)
415 {
416     extended_access(regs, device, CMD_VERIFY);
417 }
418
419 // IBM/MS lock/unlock drive
420 static void
421 disk_1345(struct bregs *regs, u8 device)
422 {
423     // Always success for HD
424     disk_ret(regs, DISK_RET_SUCCESS);
425 }
426
427 // IBM/MS eject media
428 static void
429 disk_1346(struct bregs *regs, u8 device)
430 {
431     // Volume Not Removable
432     disk_ret(regs, DISK_RET_ENOTREMOVABLE);
433 }
434
435 // IBM/MS extended seek
436 static void
437 disk_1347(struct bregs *regs, u8 device)
438 {
439     extended_access(regs, device, CMD_SEEK);
440 }
441
442 // IBM/MS get drive parameters
443 static void
444 disk_1348(struct bregs *regs, u8 device)
445 {
446     u16 size = GET_INT13DPT(regs, size);
447
448     // Buffer is too small
449     if (size < 26) {
450         disk_ret(regs, DISK_RET_EPARAM);
451         return;
452     }
453
454     // EDD 1.x
455
456     u8  type    = GET_GLOBAL(ATA.devices[device].type);
457     u16 npc     = GET_GLOBAL(ATA.devices[device].pchs.cylinders);
458     u16 nph     = GET_GLOBAL(ATA.devices[device].pchs.heads);
459     u16 npspt   = GET_GLOBAL(ATA.devices[device].pchs.spt);
460     u64 lba     = GET_GLOBAL(ATA.devices[device].sectors);
461     u16 blksize = GET_GLOBAL(ATA.devices[device].blksize);
462
463     dprintf(DEBUG_HDL_13, "disk_1348 size=%d t=%d chs=%d,%d,%d lba=%d bs=%d\n"
464             , size, type, npc, nph, npspt, (u32)lba, blksize);
465
466     SET_INT13DPT(regs, size, 26);
467     if (type == DTYPE_ATA) {
468         if (lba > (u64)npspt*nph*0x3fff) {
469             SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
470             SET_INT13DPT(regs, cylinders, 0x3fff);
471         } else {
472             SET_INT13DPT(regs, infos, 0x02); // geometry is valid
473             SET_INT13DPT(regs, cylinders, (u32)npc);
474         }
475         SET_INT13DPT(regs, heads, (u32)nph);
476         SET_INT13DPT(regs, spt, (u32)npspt);
477         SET_INT13DPT(regs, sector_count, lba);
478     } else {
479         // ATAPI
480         // 0x74 = removable, media change, lockable, max values
481         SET_INT13DPT(regs, infos, 0x74);
482         SET_INT13DPT(regs, cylinders, 0xffffffff);
483         SET_INT13DPT(regs, heads, 0xffffffff);
484         SET_INT13DPT(regs, spt, 0xffffffff);
485         SET_INT13DPT(regs, sector_count, (u64)-1);
486     }
487     SET_INT13DPT(regs, blksize, blksize);
488
489     if (size < 30) {
490         disk_ret(regs, DISK_RET_SUCCESS);
491         return;
492     }
493
494     // EDD 2.x
495
496     u16 ebda_seg = get_ebda_seg();
497     SET_INT13DPT(regs, size, 30);
498
499     SET_INT13DPT(regs, dpte_segment, ebda_seg);
500     SET_INT13DPT(regs, dpte_offset
501                  , offsetof(struct extended_bios_data_area_s, dpte));
502
503     // Fill in dpte
504     u8 channel = device / 2;
505     u8 slave = device % 2;
506     u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
507     u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
508     u8 irq = GET_GLOBAL(ATA.channels[channel].irq);
509
510     u16 options = 0;
511     if (type == DTYPE_ATA) {
512         u8 translation = GET_GLOBAL(ATA.devices[device].translation);
513         if (translation != TRANSLATION_NONE) {
514             options |= 1<<3; // CHS translation
515             if (translation == TRANSLATION_LBA)
516                 options |= 1<<9;
517             if (translation == TRANSLATION_RECHS)
518                 options |= 3<<9;
519         }
520     } else {
521         // ATAPI
522         options |= 1<<5; // removable device
523         options |= 1<<6; // atapi device
524     }
525     options |= 1<<4; // lba translation
526     if (CONFIG_ATA_PIO32)
527         options |= 1<<7;
528
529     SET_EBDA2(ebda_seg, dpte.iobase1, iobase1);
530     SET_EBDA2(ebda_seg, dpte.iobase2, iobase2 + ATA_CB_DC);
531     SET_EBDA2(ebda_seg, dpte.prefix, ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
532                                       | ATA_CB_DH_LBA));
533     SET_EBDA2(ebda_seg, dpte.unused, 0xcb);
534     SET_EBDA2(ebda_seg, dpte.irq, irq);
535     SET_EBDA2(ebda_seg, dpte.blkcount, 1);
536     SET_EBDA2(ebda_seg, dpte.dma, 0);
537     SET_EBDA2(ebda_seg, dpte.pio, 0);
538     SET_EBDA2(ebda_seg, dpte.options, options);
539     SET_EBDA2(ebda_seg, dpte.reserved, 0);
540     SET_EBDA2(ebda_seg, dpte.revision, 0x11);
541
542     u8 sum = checksum_far(
543         ebda_seg, (void*)offsetof(struct extended_bios_data_area_s, dpte), 15);
544     SET_EBDA2(ebda_seg, dpte.checksum, -sum);
545
546     if (size < 66) {
547         disk_ret(regs, DISK_RET_SUCCESS);
548         return;
549     }
550
551     // EDD 3.x
552     SET_INT13DPT(regs, key, 0xbedd);
553     SET_INT13DPT(regs, dpi_length, 36);
554     SET_INT13DPT(regs, reserved1, 0);
555     SET_INT13DPT(regs, reserved2, 0);
556
557     SET_INT13DPT(regs, host_bus[0], 'P');
558     SET_INT13DPT(regs, host_bus[1], 'C');
559     SET_INT13DPT(regs, host_bus[2], 'I');
560     SET_INT13DPT(regs, host_bus[3], 0);
561
562     u32 bdf = GET_GLOBAL(ATA.channels[channel].pci_bdf);
563     u32 path = (pci_bdf_to_bus(bdf) | (pci_bdf_to_dev(bdf) << 8)
564                 | (pci_bdf_to_fn(bdf) << 16));
565     SET_INT13DPT(regs, iface_path, path);
566
567     SET_INT13DPT(regs, iface_type[0], 'A');
568     SET_INT13DPT(regs, iface_type[1], 'T');
569     SET_INT13DPT(regs, iface_type[2], 'A');
570     SET_INT13DPT(regs, iface_type[3], 0);
571     SET_INT13DPT(regs, iface_type[4], 0);
572     SET_INT13DPT(regs, iface_type[5], 0);
573     SET_INT13DPT(regs, iface_type[6], 0);
574     SET_INT13DPT(regs, iface_type[7], 0);
575
576     SET_INT13DPT(regs, device_path, slave);
577
578     SET_INT13DPT(regs, checksum
579                  , -checksum_far(regs->ds, (void*)(regs->si+30), 35));
580
581     disk_ret(regs, DISK_RET_SUCCESS);
582 }
583
584 // IBM/MS extended media change
585 static void
586 disk_1349(struct bregs *regs, u8 device)
587 {
588     // Always success for HD
589     disk_ret(regs, DISK_RET_SUCCESS);
590 }
591
592 static void
593 disk_134e01(struct bregs *regs, u8 device)
594 {
595     disk_ret(regs, DISK_RET_SUCCESS);
596 }
597
598 static void
599 disk_134e03(struct bregs *regs, u8 device)
600 {
601     disk_ret(regs, DISK_RET_SUCCESS);
602 }
603
604 static void
605 disk_134e04(struct bregs *regs, u8 device)
606 {
607     disk_ret(regs, DISK_RET_SUCCESS);
608 }
609
610 static void
611 disk_134e06(struct bregs *regs, u8 device)
612 {
613     disk_ret(regs, DISK_RET_SUCCESS);
614 }
615
616 static void
617 disk_134eXX(struct bregs *regs, u8 device)
618 {
619     disk_ret(regs, DISK_RET_EPARAM);
620 }
621
622 // IBM/MS set hardware configuration
623 static void
624 disk_134e(struct bregs *regs, u8 device)
625 {
626     switch (regs->al) {
627     case 0x01: disk_134e01(regs, device); break;
628     case 0x03: disk_134e03(regs, device); break;
629     case 0x04: disk_134e04(regs, device); break;
630     case 0x06: disk_134e06(regs, device); break;
631     default:   disk_134eXX(regs, device); break;
632     }
633 }
634
635 void
636 disk_13XX(struct bregs *regs, u8 device)
637 {
638     disk_ret(regs, DISK_RET_EPARAM);
639 }
640
641 void
642 disk_13(struct bregs *regs, u8 device)
643 {
644     //debug_stub(regs);
645
646     // clear completion flag
647     SET_BDA(disk_interrupt_flag, 0);
648
649     switch (regs->ah) {
650     case 0x00: disk_1300(regs, device); break;
651     case 0x01: disk_1301(regs, device); break;
652     case 0x02: disk_1302(regs, device); break;
653     case 0x03: disk_1303(regs, device); break;
654     case 0x04: disk_1304(regs, device); break;
655     case 0x05: disk_1305(regs, device); break;
656     case 0x08: disk_1308(regs, device); break;
657     case 0x09: disk_1309(regs, device); break;
658     case 0x0c: disk_130c(regs, device); break;
659     case 0x0d: disk_130d(regs, device); break;
660     case 0x10: disk_1310(regs, device); break;
661     case 0x11: disk_1311(regs, device); break;
662     case 0x14: disk_1314(regs, device); break;
663     case 0x15: disk_1315(regs, device); break;
664     case 0x41: disk_1341(regs, device); break;
665     case 0x42: disk_1342(regs, device); break;
666     case 0x43: disk_1343(regs, device); break;
667     case 0x44: disk_1344(regs, device); break;
668     case 0x45: disk_1345(regs, device); break;
669     case 0x46: disk_1346(regs, device); break;
670     case 0x47: disk_1347(regs, device); break;
671     case 0x48: disk_1348(regs, device); break;
672     case 0x49: disk_1349(regs, device); break;
673     case 0x4e: disk_134e(regs, device); break;
674     default:   disk_13XX(regs, device); break;
675     }
676 }
677
678
679 /****************************************************************
680  * Entry points
681  ****************************************************************/
682
683 static u8
684 get_device(struct bregs *regs, u8 iscd, u8 drive)
685 {
686     // basic check : device has to be defined
687     if (drive >= CONFIG_MAX_ATA_DEVICES) {
688         disk_ret(regs, DISK_RET_EPARAM);
689         return CONFIG_MAX_ATA_DEVICES;
690     }
691
692     // Get the ata channel
693     u8 device = GET_GLOBAL(ATA.idmap[iscd][drive]);
694
695     // basic check : device has to be valid
696     if (device >= CONFIG_MAX_ATA_DEVICES) {
697         disk_ret(regs, DISK_RET_EPARAM);
698         return CONFIG_MAX_ATA_DEVICES;
699     }
700
701     return device;
702 }
703
704 static void
705 handle_legacy_disk(struct bregs *regs, u8 drive)
706 {
707     if (drive < 0x80) {
708         floppy_13(regs, drive);
709         return;
710     }
711
712     if (! CONFIG_ATA) {
713         // XXX - old code had other disk access method.
714         disk_ret(regs, DISK_RET_EPARAM);
715         return;
716     }
717
718     if (drive >= 0xe0) {
719         u8 device = get_device(regs, 1, drive - 0xe0);
720         if (device >= CONFIG_MAX_ATA_DEVICES)
721             return;
722         cdrom_13(regs, device);
723         return;
724     }
725
726     u8 device = get_device(regs, 0, drive - 0x80);
727     if (device >= CONFIG_MAX_ATA_DEVICES)
728         return;
729     disk_13(regs, device);
730 }
731
732 void VISIBLE16
733 handle_40(struct bregs *regs)
734 {
735     debug_enter(regs, DEBUG_HDL_40);
736     handle_legacy_disk(regs, regs->dl);
737 }
738
739 // INT 13h Fixed Disk Services Entry Point
740 void VISIBLE16
741 handle_13(struct bregs *regs)
742 {
743     debug_enter(regs, DEBUG_HDL_13);
744     u8 drive = regs->dl;
745
746     if (CONFIG_CDROM_EMU) {
747         if (regs->ah == 0x4b) {
748             cdemu_134b(regs);
749             return;
750         }
751         u16 ebda_seg = get_ebda_seg();
752         if (GET_EBDA2(ebda_seg, cdemu.active)) {
753             u8 emudrive = GET_EBDA2(ebda_seg, cdemu.emulated_drive);
754             if (drive == emudrive) {
755                 cdemu_13(regs);
756                 return;
757             }
758             if (drive < 0xe0 && ((emudrive ^ drive) & 0x80) == 0)
759                 drive--;
760         }
761     }
762     handle_legacy_disk(regs, drive);
763 }
764
765 // record completion in BIOS task complete flag
766 void VISIBLE16
767 handle_76()
768 {
769     debug_isr(DEBUG_ISR_76);
770     SET_BDA(disk_interrupt_flag, 0xff);
771     eoi_pic2();
772 }
773
774 // Old Fixed Disk Parameter Table (newer tables are in the ebda).
775 struct fdpt_s OldFDPT VAR16FIXED(0xe401);