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