aboutsummaryrefslogtreecommitdiffstats
path: root/src/disk.c
blob: 9320c3c47da1953494b813666f687a10928e1d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// 16bit code to access hard drives.
//
// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2002  MandrakeSoft S.A.
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include "disk.h" // floppy_13
#include "biosvar.h" // struct bregs
#include "config.h" // CONFIG_*
#include "cmos.h" // inb_cmos
#include "util.h" // debug_enter
#include "ata.h" // ATA_*

#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
#define DEBUGF(fmt, args...)


/****************************************************************
 * Helper functions
 ****************************************************************/

#define DISK_STUB(regs) do {                    \
        struct bregs *__regs = (regs);          \
        debug_stub(__regs);                     \
        disk_ret(__regs, DISK_RET_SUCCESS);     \
    } while (0)

static u8
checksum_seg(u16 seg, u16 offset, u32 len)
{
    u32 i;
    u8 sum = 0;
    for (i=0; i<len; i++)
        sum += GET_FARVAR(seg, *(u8*)(offset+i));
    return sum;
}

static void
basic_access(struct bregs *regs, u8 device, u16 command)
{
    u16 count       = regs->al;
    u16 cylinder    = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
    u16 sector      = regs->cl & 0x3f;
    u16 head        = regs->dh;

    if (count > 128 || count == 0 || sector == 0) {
        BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
                , regs->ah);
        disk_ret(regs, DISK_RET_EPARAM);
        return;
    }

    u16 nlc   = GET_EBDA(ata.devices[device].lchs.cylinders);
    u16 nlh   = GET_EBDA(ata.devices[device].lchs.heads);
    u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
    u16 nph   = GET_EBDA(ata.devices[device].pchs.heads);
    u16 npspt = GET_EBDA(ata.devices[device].pchs.spt);

    // sanity check on cyl heads, sec
    if (cylinder >= nlc || head >= nlh || sector > nlspt) {
        BX_INFO("int13_harddisk: function %02x, parameters out of"
                " range %04x/%04x/%04x!\n"
                , regs->ah, cylinder, head, sector);
        disk_ret(regs, DISK_RET_EPARAM);
        return;
    }

    if (!command) {
        // If verify or seek
        disk_ret(regs, DISK_RET_SUCCESS);
        return;
    }

    u16 segment = regs->es;
    u16 offset  = regs->bx;

    u8 status;
    u32 lba;
    if (nph != nlh || npspt != nlspt) {
        // translate lchs to lba
        lba = (((((u32)cylinder * (u32)nlh) + (u32)head) * (u32)nlspt)
               + (u32)sector - 1);
        status = ata_cmd_data(device, command, lba, count
                              , MAKE_32_PTR(segment, offset));
    } else {
        // XXX - see if lba access can always be used.
        status = ata_cmd_data_chs(device, command
                                  , cylinder, head, sector, count
                                  , MAKE_32_PTR(segment, offset));
    }

    // Set nb of sector transferred
    regs->al = GET_EBDA(ata.trsfsectors);

    if (status != 0) {
        BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
        disk_ret(regs, DISK_RET_EBADTRACK);
    }
    disk_ret(regs, DISK_RET_SUCCESS);
}

void
emu_access(struct bregs *regs, u8 device, u16 command)
{
    u16 count    = regs->al;
    u16 cylinder = regs->ch | ((((u16) regs->cl) << 2) & 0x300);
    u16 sector   = regs->cl & 0x3f;
    u16 head     = regs->dh;

    if ((count > 128) || (count == 0) || (sector == 0)) {
        BX_INFO("int13_harddisk: function %02x, parameter out of range!\n"
                , regs->ah);
        disk_ret(regs, DISK_RET_EPARAM);
        return;
    }

    u16 nlc   = GET_EBDA(cdemu.vdevice.cylinders);
    u16 nlh   = GET_EBDA(cdemu.vdevice.heads);
    u16 nlspt = GET_EBDA(cdemu.vdevice.spt);

    // sanity check on cyl heads, sec
    if ( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
        BX_INFO("int13_harddisk: function %02x, parameters out of"
                " range %04x/%04x/%04x!\n"
                , regs->ah, cylinder, head, sector);
        disk_ret(regs, DISK_RET_EPARAM);
        return;
    }

    if (!command) {
        // If verify or seek
        disk_ret(regs, DISK_RET_SUCCESS);
        return;
    }

    u32 ilba = GET_EBDA(cdemu.ilba);
    // calculate the virtual lba inside the image
    u32 vlba= (((((u32)cylinder*(u32)nlh)+(u32)head)*(u32)nlspt)
               +((u32)(sector-1)));
    // start lba on cd
    u32 slba  = (u32)vlba/4;
    u16 before= (u16)vlba%4;
    u32 lba = ilba + slba;

    u16 segment = regs->es;
    u16 offset  = regs->bx;

    u8 status = cdrom_read(device, lba, count*512
                           , MAKE_32_PTR(segment, offset), before*512);
    if (status != 0) {
        BX_INFO("int13_harddisk: function %02x, error %02x !\n",regs->ah,status);
        regs->al = 0;
        disk_ret(regs, DISK_RET_EBADTRACK);
    }
    regs->al = count;
    disk_ret(regs, DISK_RET_SUCCESS);
}

static void
extended_access(struct bregs *regs, u8 device, u16 command)
{
    u16 count = GET_INT13EXT(regs, count);
    u16 segment = GET_INT13EXT(regs, segment);
    u16 offset = GET_INT13EXT(regs, offset);

    // Can't use 64 bits lba
    u32 lba = GET_INT13EXT(regs, lba2);
    if (lba != 0L) {
        BX_PANIC("int13_harddisk: function %02x. Can't use 64bits lba\n"
                 , regs->ah);
        disk_ret(regs, DISK_RET_EPARAM);
        return;
    }

    u8 type = GET_EBDA(ata.devices[device].type);

    // Get 32 bits lba and check
    lba = GET_INT13EXT(regs, lba1);
    if (type == ATA_TYPE_ATA
        && lba >= GET_EBDA(ata.devices[device].sectors)) {
        BX_INFO("int13_harddisk: function %02x. LBA out of range\n", regs->ah);
        disk_ret(regs, DISK_RET_EPARAM);
        return;
    }

    if (!command) {
        // If verify or seek
        disk_ret(regs, DISK_RET_SUCCESS);
        return;
    }

    u8 status;
    if (type == ATA_TYPE_ATA)
        status = ata_cmd_data(device, command, lba, count
                              , MAKE_32_PTR(segment, offset));
    else
        status = cdrom_read(device, lba, count*2048
                            , MAKE_32_PTR(segment, offset), 0);

    SET_INT13EXT(regs, count, GET_EBDA(ata.trsfsectors));

    if (status != 0) {
        BX_INFO("int13_harddisk: function %02x, error %02x !\n"
                , regs->ah, status);
        disk_ret(regs, DISK_RET_EBADTRACK);
        return;
    }
    disk_ret(regs, DISK_RET_SUCCESS);
}


/****************************************************************
 * Hard Drive functions
 ****************************************************************/

// disk controller reset
static void
disk_1300(struct bregs *regs, u8 device)
{
    ata_reset(device);
}

// read disk status
static void
disk_1301(struct bregs *regs, u8 device)
{
    regs->ah = GET_BDA(disk_last_status);
    disk_ret(regs, DISK_RET_SUCCESS);
}

// read disk sectors
static void
disk_1302(struct bregs *regs, u8 device)
{
    basic_access(regs, device, ATA_CMD_READ_SECTORS);
}

// write disk sectors
static void
disk_1303(struct bregs *regs, u8 device)
{
    basic_access(regs, device, ATA_CMD_WRITE_SECTORS);
}

// verify disk sectors
static void
disk_1304(struct bregs *regs, u8 device)
{
    basic_access(regs, device, 0);
    // FIXME verify
}

// format disk track
static void
disk_1305(struct bregs *regs, u8 device)
{
    DISK_STUB(regs);
}

// read disk drive parameters
static void
disk_1308(struct bregs *regs, u8 device)
{
    // Get logical geometry from table
    u16 nlc = GET_EBDA(ata.devices[device].lchs.cylinders);
    u16 nlh = GET_EBDA(ata.devices[device].lchs.heads);
    u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);
    u16 count = GET_EBDA(ata.hdcount);

    nlc = nlc - 2; /* 0 based , last sector not used */
    regs->al = 0;
    regs->ch = nlc & 0xff;
    regs->cl = ((nlc >> 2) & 0xc0) | (nlspt & 0x3f);
    regs->dh = nlh - 1;
    regs->dl = count; /* FIXME returns 0, 1, or n hard drives */

    // FIXME should set ES & DI
    disk_ret(regs, DISK_RET_SUCCESS);
}

// initialize drive parameters
static void
disk_1309(struct bregs *regs, u8 device)
{
    DISK_STUB(regs);
}

// seek to specified cylinder
static void
disk_130c(struct bregs *regs, u8 device)
{
    DISK_STUB(regs);
}

// alternate disk reset
static void
disk_130d(struct bregs *regs, u8 device)
{
    DISK_STUB(regs);
}

// check drive ready
static void
disk_1310(struct bregs *regs, u8 device)
{
    // should look at 40:8E also???

    // Read the status from controller
    u8 status = inb(GET_EBDA(ata.channels[device/2].iobase1) + ATA_CB_STAT);
    if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY )
        disk_ret(regs, DISK_RET_SUCCESS);
    else
        disk_ret(regs, DISK_RET_ENOTREADY);
}

// recalibrate
static void
disk_1311(struct bregs *regs, u8 device)
{
    DISK_STUB(regs);
}

// controller internal diagnostic
static void
disk_1314(struct bregs *regs, u8 device)
{
    DISK_STUB(regs);
}

// read disk drive size
static void
disk_1315(struct bregs *regs, u8 device)
{
    // Get logical geometry from table
    u16 nlc   = GET_EBDA(ata.devices[device].lchs.cylinders);
    u16 nlh   = GET_EBDA(ata.devices[device].lchs.heads);
    u16 nlspt = GET_EBDA(ata.devices[device].lchs.spt);

    // Compute sector count seen by int13
    u32 lba = (u32)(nlc - 1) * (u32)nlh * (u32)nlspt;
    regs->cx = lba >> 16;
    regs->dx = lba & 0xffff;

    disk_ret(regs, DISK_RET_SUCCESS);
    regs->ah = 3; // hard disk accessible
}

// IBM/MS installation check
static void
disk_1341(struct bregs *regs, u8 device)
{
    regs->bx = 0xaa55;  // install check
    regs->cx = 0x0007;  // ext disk access and edd, removable supported
    disk_ret(regs, DISK_RET_SUCCESS);
    regs->ah = 0x30;    // EDD 3.0
}

// IBM/MS extended read
static void
disk_1342(struct bregs *regs, u8 device)
{
    extended_access(regs, device, ATA_CMD_READ_SECTORS);
}

// IBM/MS extended write
static void
disk_1343(struct bregs *regs, u8 device)
{
    extended_access(regs, device, ATA_CMD_WRITE_SECTORS);
}

// IBM/MS verify
static void
disk_1344(struct bregs *regs, u8 device)
{
    extended_access(regs, device, 0);
}

// IBM/MS lock/unlock drive
static void
disk_1345(struct bregs *regs, u8 device)
{
    // Always success for HD
    disk_ret(regs, DISK_RET_SUCCESS);
}

// IBM/MS eject media
static void
disk_1346(struct bregs *regs, u8 device)
{
    // Volume Not Removable
    disk_ret(regs, DISK_RET_ENOTREMOVABLE);
}

// IBM/MS extended seek
static void
disk_1347(struct bregs *regs, u8 device)
{
    extended_access(regs, device, 0);
}

// IBM/MS get drive parameters
static void
disk_1348(struct bregs *regs, u8 device)
{
    u16 size = GET_INT13DPT(regs, size);

    // Buffer is too small
    if (size < 0x1a) {
        disk_ret(regs, DISK_RET_EPARAM);
        return;
    }

    // EDD 1.x

    u8  type    = GET_EBDA(ata.devices[device].type);
    u16 npc     = GET_EBDA(ata.devices[device].pchs.cylinders);
    u16 nph     = GET_EBDA(ata.devices[device].pchs.heads);
    u16 npspt   = GET_EBDA(ata.devices[device].pchs.spt);
    u32 lba     = GET_EBDA(ata.devices[device].sectors);
    u16 blksize = GET_EBDA(ata.devices[device].blksize);

    SET_INT13DPT(regs, size, 0x1a);
    if (type == ATA_TYPE_ATA) {
        if ((lba/npspt)/nph > 0x3fff) {
            SET_INT13DPT(regs, infos, 0x00); // geometry is invalid
            SET_INT13DPT(regs, cylinders, 0x3fff);
        } else {
            SET_INT13DPT(regs, infos, 0x02); // geometry is valid
            SET_INT13DPT(regs, cylinders, (u32)npc);
        }
        SET_INT13DPT(regs, heads, (u32)nph);
        SET_INT13DPT(regs, spt, (u32)npspt);
        SET_INT13DPT(regs, sector_count1, lba);  // FIXME should be Bit64
        SET_INT13DPT(regs, sector_count2, 0L);
    } else {
        // ATAPI
        // 0x74 = removable, media change, lockable, max values
        SET_INT13DPT(regs, infos, 0x74);
        SET_INT13DPT(regs, cylinders, 0xffffffff);
        SET_INT13DPT(regs, heads, 0xffffffff);
        SET_INT13DPT(regs, spt, 0xffffffff);
        SET_INT13DPT(regs, sector_count1, 0xffffffff);  // FIXME should be Bit64
        SET_INT13DPT(regs, sector_count2, 0xffffffff);
    }
    SET_INT13DPT(regs, blksize, blksize);

    if (size < 0x1e) {
        disk_ret(regs, DISK_RET_SUCCESS);
        return;
    }

    // EDD 2.x

    SET_INT13DPT(regs, size, 0x1e);

    SET_INT13DPT(regs, dpte_segment, EBDA_SEG);
    SET_INT13DPT(regs, dpte_offset
                 , offsetof(struct extended_bios_data_area_s, ata.dpte));

    // Fill in dpte
    u8 channel = device / 2;
    u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
    u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
    u8 irq = GET_EBDA(ata.channels[channel].irq);
    u8 mode = GET_EBDA(ata.devices[device].mode);

    u16 options;
    if (type == ATA_TYPE_ATA) {
        u8 translation = GET_EBDA(ata.devices[device].translation);
        options  = (translation==ATA_TRANSLATION_NONE?0:1)<<3; // chs translation
        options |= (translation==ATA_TRANSLATION_LBA?1:0)<<9;
        options |= (translation==ATA_TRANSLATION_RECHS?3:0)<<9;
    } else {
        // ATAPI
        options  = (1<<5); // removable device
        options |= (1<<6); // atapi device
    }
    options |= (1<<4); // lba translation
    options |= (mode==ATA_MODE_PIO32?1:0)<<7;

    SET_EBDA(ata.dpte.iobase1, iobase1);
    SET_EBDA(ata.dpte.iobase2, iobase2 + ATA_CB_DC);
    SET_EBDA(ata.dpte.prefix, (0xe | (device % 2))<<4 );
    SET_EBDA(ata.dpte.unused, 0xcb );
    SET_EBDA(ata.dpte.irq, irq );
    SET_EBDA(ata.dpte.blkcount, 1 );
    SET_EBDA(ata.dpte.dma, 0 );
    SET_EBDA(ata.dpte.pio, 0 );
    SET_EBDA(ata.dpte.options, options);
    SET_EBDA(ata.dpte.reserved, 0);
    if (size >= 0x42)
        SET_EBDA(ata.dpte.revision, 0x11);
    else
        SET_EBDA(ata.dpte.revision, 0x10);

    u8 sum = checksum_seg(EBDA_SEG
                          , offsetof(struct extended_bios_data_area_s, ata.dpte)
                          , 15);
    SET_EBDA(ata.dpte.checksum, ~sum);

    if (size < 0x42) {
        disk_ret(regs, DISK_RET_SUCCESS);
        return;
    }

    // EDD 3.x
    channel = device / 2;
    u8 iface = GET_EBDA(ata.channels[channel].iface);
    iobase1 = GET_EBDA(ata.channels[channel].iobase1);

    SET_INT13DPT(regs, size, 0x42);
    SET_INT13DPT(regs, key, 0xbedd);
    SET_INT13DPT(regs, dpi_length, 0x24);
    SET_INT13DPT(regs, reserved1, 0);
    SET_INT13DPT(regs, reserved2, 0);

    if (iface==ATA_IFACE_ISA) {
        SET_INT13DPT(regs, host_bus[0], 'I');
        SET_INT13DPT(regs, host_bus[1], 'S');
        SET_INT13DPT(regs, host_bus[2], 'A');
        SET_INT13DPT(regs, host_bus[3], 0);
    } else {
        // FIXME PCI
    }
    SET_INT13DPT(regs, iface_type[0], 'A');
    SET_INT13DPT(regs, iface_type[1], 'T');
    SET_INT13DPT(regs, iface_type[2], 'A');
    SET_INT13DPT(regs, iface_type[3], 0);

    if (iface==ATA_IFACE_ISA) {
        SET_INT13DPT(regs, iface_path[0], iobase1);
        SET_INT13DPT(regs, iface_path[2], 0);
        SET_INT13DPT(regs, iface_path[4], 0L);
    } else {
        // FIXME PCI
    }
    SET_INT13DPT(regs, device_path[0], device%2);
    SET_INT13DPT(regs, device_path[1], 0);
    SET_INT13DPT(regs, device_path[2], 0);
    SET_INT13DPT(regs, device_path[4], 0L);

    sum = checksum_seg(regs->ds, 30, 34);
    SET_INT13DPT(regs, checksum, ~sum);
}

// IBM/MS extended media change
static void
disk_1349(struct bregs *regs, u8 device)
{
    // Always success for HD
    disk_ret(regs, DISK_RET_SUCCESS);
}

static void
disk_134e01(struct bregs *regs, u8 device)
{
    disk_ret(regs, DISK_RET_SUCCESS);
}

static void
disk_134e03(struct bregs *regs, u8 device)
{
    disk_ret(regs, DISK_RET_SUCCESS);
}

static void
disk_134e04(struct bregs *regs, u8 device)
{
    disk_ret(regs, DISK_RET_SUCCESS);
}

static void
disk_134e06(struct bregs *regs, u8 device)
{
    disk_ret(regs, DISK_RET_SUCCESS);
}

static void
disk_134eXX(struct bregs *regs, u8 device)
{
    debug_stub(regs);
    disk_ret(regs, DISK_RET_EPARAM);
}

// IBM/MS set hardware configuration
static void
disk_134e(struct bregs *regs, u8 device)
{
    switch (regs->al) {
    case 0x01: disk_134e01(regs, device); break;
    case 0x03: disk_134e03(regs, device); break;
    case 0x04: disk_134e04(regs, device); break;
    case 0x06: disk_134e06(regs, device); break;
    default:   disk_134eXX(regs, device); break;
    }
}

void
disk_13XX(struct bregs *regs, u8 device)
{
    debug_stub(regs);
    disk_ret(regs, DISK_RET_EPARAM);
}

void
disk_13(struct bregs *regs, u8 device)
{
    //debug_stub(regs);

    // clear completion flag
    SET_BDA(disk_interrupt_flag, 0);

    switch (regs->ah) {
    case 0x00: disk_1300(regs, device); break;
    case 0x01: disk_1301(regs, device); break;
    case 0x02: disk_1302(regs, device); break;
    case 0x03: disk_1303(regs, device); break;
    case 0x04: disk_1304(regs, device); break;
    case 0x05: disk_1305(regs, device); break;
    case 0x08: disk_1308(regs, device); break;
    case 0x09: disk_1309(regs, device); break;
    case 0x0c: disk_130c(regs, device); break;
    case 0x0d: disk_130d(regs, device); break;
    case 0x10: disk_1310(regs, device); break;
    case 0x11: disk_1311(regs, device); break;
    case 0x14: disk_1314(regs, device); break;
    case 0x15: disk_1315(regs, device); break;
    case 0x41: disk_1341(regs, device); break;
    case 0x42: disk_1342(regs, device); break;
    case 0x43: disk_1343(regs, device); break;
    case 0x44: disk_1344(regs, device); break;
    case 0x45: disk_1345(regs, device); break;
    case 0x46: disk_1346(regs, device); break;
    case 0x47: disk_1347(regs, device); break;
    case 0x48: disk_1348(regs, device); break;
    case 0x49: disk_1349(regs, device); break;
    case 0x4e: disk_134e(regs, device); break;
    default:   disk_13XX(regs, device); break;
    }
}


/****************************************************************
 * Entry points
 ****************************************************************/

static u8
get_device(struct bregs *regs, u8 iscd, u8 drive)
{
    // basic check : device has to be defined
    if (drive >= CONFIG_MAX_ATA_DEVICES) {
        disk_ret(regs, DISK_RET_EPARAM);
        return CONFIG_MAX_ATA_DEVICES;
    }

    // Get the ata channel
    u8 device = GET_EBDA(ata.idmap[iscd][drive]);

    // basic check : device has to be valid
    if (device >= CONFIG_MAX_ATA_DEVICES) {
        disk_ret(regs, DISK_RET_EPARAM);
        return CONFIG_MAX_ATA_DEVICES;
    }

    return device;
}

static void
handle_legacy_disk(struct bregs *regs, u8 drive)
{
    if (drive < 0x80) {
        floppy_13(regs, drive);
        return;
    }

    if (! CONFIG_ATA) {
        // XXX - old code had other disk access method.
        disk_ret(regs, DISK_RET_EPARAM);
        return;
    }

    if (drive >= 0xe0) {
        u8 device = get_device(regs, 1, drive - 0xe0);
        if (device >= CONFIG_MAX_ATA_DEVICES)
            return;
        cdrom_13(regs, device);
        return;
    }

    u8 device = get_device(regs, 0, drive - 0x80);
    if (device >= CONFIG_MAX_ATA_DEVICES)
        return;
    disk_13(regs, device);
}

void VISIBLE16
handle_40(struct bregs *regs)
{
    debug_enter(regs);
    handle_legacy_disk(regs, regs->dl);
}

// INT 13h Fixed Disk Services Entry Point
void VISIBLE16
handle_13(struct bregs *regs)
{
    //debug_enter(regs);
    u8 drive = regs->dl;

    if (CONFIG_CDROM_BOOT) {
        if (regs->ah == 0x4b) {
            cdemu_134b(regs);
            return;
        }
        if (GET_EBDA(cdemu.active)) {
            if (drive == GET_EBDA(cdemu.emulated_drive)) {
                cdemu_13(regs);
                return;
            }
            if (drive < 0xe0)
                drive--;
        }
    }
    handle_legacy_disk(regs, drive);
}

// record completion in BIOS task complete flag
void VISIBLE16
handle_76()
{
    debug_isr();
    SET_BDA(disk_interrupt_flag, 0xff);
    eoi_both_pics();
}