Changed the GDI driver interface to pass an opaque PHYSDEV pointer
[wine] / msdos / int13.c
1 /*
2  * BIOS interrupt 13h handler
3  *
4  * Copyright 1997 Andreas Mohr
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <fcntl.h>
26 #ifdef linux
27 # include <linux/fd.h>
28 #endif
29 #include "miscemu.h"
30 /* #define DEBUG_INT */
31 #include "wine/debug.h"
32 #include "drive.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(int);
35
36 /**********************************************************************
37  *          INT_Int13Handler (WPROCS.119)
38  *
39  * Handler for int 13h (disk I/O).
40  */
41 void WINAPI INT_Int13Handler( CONTEXT86 *context )
42 {
43     switch(AH_reg(context))
44     {
45         case 0x00:                            /* RESET DISK SYSTEM     */
46                 break; /* no return ? */
47         case 0x01:                            /* STATUS OF DISK SYSTEM */
48                 AL_reg(context) = 0; /* successful completion */
49                 break;
50         case 0x02:                            /* READ SECTORS INTO MEMORY */
51                 AL_reg(context) = 0; /* number of sectors read */
52                 AH_reg(context) = 0; /* status */
53                 break;
54         case 0x03:                            /* WRITE SECTORS FROM MEMORY */
55                 break; /* no return ? */
56         case 0x04:                            /* VERIFY DISK SECTOR(S) */
57                 AL_reg(context) = 0; /* number of sectors verified */
58                 AH_reg(context) = 0;
59                 break;
60                
61         case 0x05:                                     /* FORMAT TRACK */
62         case 0x06:             /* FORMAT TRACK AND SET BAD SECTOR FLAGS */
63         case 0x07:             /* FORMAT DRIVE STARTING AT GIVEN TRACK  */
64             /* despite what Ralf Brown says, 0x06 and 0x07 seem to
65              * set CFLAG, too (at least my BIOS does that) */
66             AH_reg(context) = 0x0c;
67             SET_CFLAG(context);
68             break;
69
70         case 0x08:                              /* GET DRIVE PARAMETERS  */
71                 if (DL_reg(context) & 0x80) { /* hard disk ? */
72                         AH_reg(context) = 0x07;
73                         SET_CFLAG(context);
74                 }
75                 else { /* floppy disk */
76 #ifdef linux
77                         unsigned int i, nr_of_drives = 0;                       
78                         BYTE drive_nr = DL_reg(context);
79                         int floppy_fd;
80                         struct floppy_drive_params floppy_parm;
81                         char root[] = "A:\\";
82
83                         AH_reg(context) = 0x00; /* success */
84
85                         for (i = 0; i < MAX_DOS_DRIVES; i++, root[0]++)
86                             if (GetDriveTypeA(root) == DRIVE_REMOVABLE) nr_of_drives++;
87                         DL_reg(context) = nr_of_drives;
88
89                         if (drive_nr > 1) { /* invalid drive ? */
90                                 BX_reg(context) = 0;
91                                 CX_reg(context) = 0;
92                                 DH_reg(context) = 0;
93                                 break;
94                         }
95
96                         if ( (floppy_fd = DRIVE_OpenDevice( drive_nr, O_NONBLOCK)) == -1)
97                         {
98                                 WARN("(GET DRIVE PARAMETERS): Can't determine floppy geometry !\n");
99                                 BX_reg(context) = 0;
100                                 CX_reg(context) = 0;
101                                 DH_reg(context) = 0;
102                                 break;
103                         }
104                         ioctl(floppy_fd, FDGETDRVPRM, &floppy_parm);
105                         close(floppy_fd);
106
107                         BL_reg(context) = floppy_parm.cmos;
108
109                         /* CH = low eight bits of max cyl
110                            CL = max sec nr (bits 5-0),
111                            hi two bits of max cyl (bits 7-6)
112                            DH = max head nr */ 
113                         DH_reg(context) = 0x01;
114                         switch (BL_reg(context))
115                         {
116                             case 0: /* no drive */
117                                 CX_reg(context) = 0x0;
118                                 DX_reg(context) = 0x0;
119                                 break;
120                             case 1: /* 360 K */
121                                 CX_reg(context) = 0x2709;
122                                 break;
123                             case 2: /* 1.2 M */
124                                 CX_reg(context) = 0x4f0f;
125                                 break;
126                             case 3: /* 720 K */
127                                 CX_reg(context) = 0x4f09;
128                                 break;
129                             case 4: /* 1.44 M */
130                                 CX_reg(context) = 0x4f12;
131                                 break;
132                             case 5:
133                             case 6: /* 2.88 M */
134                                 CX_reg(context) = 0x4f24;
135                                 break;
136                         }
137                         context->SegEs = 0x0000; /* FIXME: drive parameter table */
138                         DI_reg(context) = 0x0000;
139 #else
140                         AH_reg(context) = 0x01;
141                         SET_CFLAG(context);
142                         break;
143 #endif
144                 }
145                 break;
146
147         case 0x09:         /* INITIALIZE CONTROLLER WITH DRIVE PARAMETERS */
148         case 0x0a:         /* FIXED DISK - READ LONG (XT,AT,XT286,PS)     */
149         case 0x0b:         /* FIXED DISK - WRITE LONG (XT,AT,XT286,PS)    */
150         case 0x0c:         /* SEEK TO CYLINDER                            */
151         case 0x0d:         /* ALTERNATE RESET HARD DISKS                  */
152         case 0x10:         /* CHECK IF DRIVE READY                        */
153         case 0x11:         /* RECALIBRATE DRIVE                           */
154         case 0x14:         /* CONTROLLER INTERNAL DIAGNOSTIC              */
155                 AH_reg(context) = 0;
156                 break;
157
158         case 0x15:         /* GET DISK TYPE (AT,XT2,XT286,CONV,PS) */
159                 if (DL_reg(context) & 0x80) { /* hard disk ? */
160                         AH_reg(context) = 3; /* fixed disk */
161                         SET_CFLAG(context);
162                 }
163                 else { /* floppy disk ? */
164                         AH_reg(context) = 2; /* floppy with change detection */
165                         SET_CFLAG(context);
166                 }
167                 break;
168         case 0x0e:                    /* READ SECTOR BUFFER (XT only)      */
169         case 0x0f:                    /* WRITE SECTOR BUFFER (XT only)     */
170         case 0x12:                    /* CONTROLLER RAM DIAGNOSTIC (XT,PS) */
171         case 0x13:                    /* DRIVE DIAGNOSTIC (XT,PS)          */
172                 AH_reg(context) = 0x01;
173                 SET_CFLAG(context);
174                 break;
175
176         case 0x16:         /* FLOPPY - CHANGE OF DISK STATUS */
177                 AH_reg(context) = 0; /* FIXME - no change */
178                 break;
179         case 0x17:         /* SET DISK TYPE FOR FORMAT */
180                 if (DL_reg(context) < 4)
181                     AH_reg(context) = 0x00; /* successful completion */
182                 else
183                     AH_reg(context) = 0x01; /* error */
184                 break;
185         case 0x18:         /* SET MEDIA TYPE FOR FORMAT */
186                 if (DL_reg(context) < 4)
187                     AH_reg(context) = 0x00; /* successful completion */
188                 else
189                     AH_reg(context) = 0x01; /* error */
190                 break;
191         case 0x19:         /* FIXED DISK - PARK HEADS */
192         default:
193                 INT_BARF( context, 0x13 );
194     }
195 }