Release 980301
[wine] / msdos / int13.c
1 /*
2  * BIOS interrupt 13h handler
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/ioctl.h>
11 #include <fcntl.h>
12 #ifdef linux
13 #include <linux/fd.h>
14 #endif
15 #include "miscemu.h"
16 /* #define DEBUG_INT */
17 #include "debug.h"
18 #include "drive.h"
19
20 /**********************************************************************
21  *          INT_Int13Handler
22  *
23  * Handler for int 13h (disk I/O).
24  */
25 void WINAPI INT_Int13Handler( CONTEXT *context )
26 {
27     switch(AH_reg(context))
28     {
29         case 0x00:                            /* RESET DISK SYSTEM     */
30         case 0x04:                            /* VERIFY DISK SECTOR(S) */
31                 AH_reg(context) = 0;
32                 break;
33                
34         case 0x05:                                     /* FORMAT TRACK */
35         case 0x06:             /* FORMAT TRACK AND SET BAD SECTOR FLAGS */
36         case 0x07:             /* FORMAT DRIVE STARTING AT GIVEN TRACK  */
37             /* despite what Ralf Brown says, 0x06 and 0x07 seem to
38              * set CFLAG, too (at least my BIOS does that) */
39             AH_reg(context) = 0x0c;
40             SET_CFLAG(context);
41             break;
42
43         case 0x08:                              /* GET DRIVE PARAMETERS  */
44                 if (DL_reg(context) & 0x80) { /* hard disk ? */
45                         AH_reg(context) = 0x07;
46                         SET_CFLAG(context);
47                 }
48                 else { /* floppy disk */
49 #ifdef linux
50                         unsigned int i, nr_of_drives = 0;                       
51                         BYTE drive_nr = DL_reg(context);
52                         int floppy_fd;
53                         struct floppy_drive_params floppy_parm;
54
55                         AH_reg(context) = 0x00; /* success */
56
57                         for (i = 0; i < MAX_DOS_DRIVES; i++)
58                         if (DRIVE_GetType(i) == TYPE_FLOPPY) nr_of_drives++;
59                         DL_reg(context) = nr_of_drives;
60
61                         if (drive_nr > 1) { /* invalid drive ? */
62                                 BX_reg(context) = 0;
63                                 CX_reg(context) = 0;
64                                 DH_reg(context) = 0;
65                                 break;
66                         }
67
68                         if ( (floppy_fd = DRIVE_OpenDevice( drive_nr, O_NONBLOCK)) == -1)
69                         {
70                                 fprintf(stderr, "INT 0x13 (GET DRIVE PARAMETERS): can't determine floppy geometry !\n");
71                                 BX_reg(context) = 0;
72                                 CX_reg(context) = 0;
73                                 DH_reg(context) = 0;
74                                 break;
75                         }
76                         ioctl(floppy_fd, FDGETDRVPRM, &floppy_parm);
77                         close(floppy_fd);
78
79                         BL_reg(context) = floppy_parm.cmos;
80
81                         /* CH = low eight bits of max cyl
82                            CL = max sec nr (bits 5-0),
83                            hi two bits of max cyl (bits 7-6)
84                            DH = max head nr */ 
85                         DH_reg(context) = 0x01;
86                         switch (BL_reg(context))
87                         {
88                             case 0: /* no drive */
89                                 CX_reg(context) = 0x0;
90                                 DX_reg(context) = 0x0;
91                                 break;
92                             case 1: /* 360 K */
93                                 CX_reg(context) = 0x2709;
94                                 break;
95                             case 2: /* 1.2 M */
96                                 CX_reg(context) = 0x4f0f;
97                                 break;
98                             case 3: /* 720 K */
99                                 CX_reg(context) = 0x4f09;
100                                 break;
101                             case 4: /* 1.44 M */
102                                 CX_reg(context) = 0x4f12;
103                                 break;
104                             case 5:
105                             case 6: /* 2.88 M */
106                                 CX_reg(context) = 0x4f24;
107                                 break;
108                         }
109                         ES_reg(context) = 0x0000; /* FIXME: drive parameter table */
110                         DI_reg(context) = 0x0000;
111 #else
112                         AH_reg(context) = 0x01;
113                         SET_CFLAG(context);
114                         break;
115 #endif
116                 }
117                 break;
118
119         case 0x09:         /* INITIALIZE CONTROLLER WITH DRIVE PARAMETERS */
120         case 0x0c:         /* SEEK TO CYLINDER                            */
121         case 0x0d:         /* RESET HARD DISKS                            */
122         case 0x10:         /* CHECK IF DRIVE READY                        */
123         case 0x11:         /* RECALIBRATE DRIVE                           */
124         case 0x14:         /* CONTROLLER INTERNAL DIAGNOSTIC              */
125                 AH_reg(context) = 0;
126                 break;
127
128         case 0x0e:                    /* READ SECTOR BUFFER (XT only)      */
129         case 0x0f:                    /* WRITE SECTOR BUFFER (XT only)     */
130         case 0x12:                    /* CONTROLLER RAM DIAGNOSTIC (XT,PS) */
131         case 0x13:                    /* DRIVE DIAGNOSTIC (XT,PS)          */
132                 AH_reg(context) = 0x01;
133                 SET_CFLAG(context);
134                 break;
135
136         default:
137                 INT_BARF( context, 0x13 );
138     }
139 }