winedos: Win64 printf format warning fixes.
[wine] / dlls / winedos / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
31 #endif
32 #include <fcntl.h>
33 #ifdef linux
34 #ifdef HAVE_LINUX_COMPILER_H
35 #include <linux/compiler.h>
36 #endif
37 # include <linux/fd.h>
38 #endif
39
40 #include "dosexe.h"
41 #include "wine/server.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(int);
45
46
47 /*
48  * Status of last int13 operation.
49  */
50 static BYTE INT13_last_status;
51
52
53 /**********************************************************************
54  *         INT13_SetStatus
55  *
56  * Write status to AH register and set carry flag on error (AH != 0).
57  *
58  * Despite what Ralf Brown says, at least functions 0x06 and 0x07 
59  * seem to set carry, too.
60  */
61 static void INT13_SetStatus( CONTEXT86 *context, BYTE status )
62 {
63     INT13_last_status = status;
64
65     SET_AH( context, status );
66
67     if (status)
68         SET_CFLAG( context );
69     else
70         RESET_CFLAG( context );        
71 }
72
73
74 /**********************************************************************
75  *          INT13_ReadFloppyParams
76  *
77  * Read floppy disk parameters.
78  */
79 static void INT13_ReadFloppyParams( CONTEXT86 *context )
80 {
81 #ifdef linux
82     static const BYTE floppy_params[2][13] =
83     {
84         { 0xaf, 0x02, 0x25, 0x02, 0x12, 0x1b, 0xff, 0x6c, 0xf6, 0x0f, 0x08 },
85         { 0xaf, 0x02, 0x25, 0x02, 0x12, 0x1b, 0xff, 0x6c, 0xf6, 0x0f, 0x08 }
86     };
87
88     static const DWORD drive_type_info[7]={
89         0x0000, /* none */
90         0x2709, /* 360 K */
91         0x4f0f, /* 1.2 M */
92         0x4f09, /* 720 K */
93         0x4f12, /* 1.44 M */
94         0x4f24, /* 2.88 M */
95         0x4f24  /* 2.88 M */
96     };
97
98     unsigned int i;
99     unsigned int nr_of_drives = 0;
100     BYTE drive_nr = DL_reg( context );
101     int floppy_fd;
102     int r;
103     struct floppy_drive_params floppy_parm;
104     WCHAR root[] = {'A',':','\\',0}, drive_root[] = {'\\','\\','.','\\','A',':',0};
105     HANDLE h;
106
107     TRACE("in  [ EDX=%08x ]\n", context->Edx );
108
109     SET_AL( context, 0 );
110     SET_BX( context, 0 );
111     SET_CX( context, 0 );
112     SET_DH( context, 0 );
113
114     for (i = 0; i < MAX_DOS_DRIVES; i++, root[0]++)
115         if (GetDriveTypeW(root) == DRIVE_REMOVABLE) nr_of_drives++;
116     SET_DL( context, nr_of_drives );
117
118     if (drive_nr > 1) { 
119         /* invalid drive ? */
120         INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
121         return;
122     }
123
124     drive_root[4] = 'A' + drive_nr;
125     h = CreateFileW(drive_root, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
126                     FILE_FLAG_BACKUP_SEMANTICS, NULL);
127     if (h == INVALID_HANDLE_VALUE ||
128         wine_server_handle_to_fd(h, FILE_READ_DATA, &floppy_fd, NULL))
129     {
130         WARN("Can't determine floppy geometry !\n");
131         INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
132         return;
133     }
134     r = ioctl(floppy_fd, FDGETDRVPRM, &floppy_parm);
135     wine_server_release_fd( h, floppy_fd );
136     CloseHandle(h);
137
138     if(r<0)
139     {
140         INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
141         return;
142     }
143
144     SET_BL( context, floppy_parm.cmos );
145
146     /*
147      * CH = low eight bits of max cyl
148      * CL = max sec nr (bits 5-0),
149      *      hi two bits of max cyl (bits 7-6)
150      * DH = max head nr 
151      */
152     if(BL_reg( context ) && BL_reg( context ) < 7)
153     {
154         SET_DH( context, 0x01 );
155         SET_CX( context, drive_type_info[BL_reg( context )] );
156     }
157
158     context->Edi = (DWORD)floppy_params[drive_nr];
159
160     if(!context->Edi)
161     {
162         ERR("Get floppy params failed for drive %d\n", drive_nr);
163         INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
164         return;
165     }
166
167     TRACE("out [ EAX=%08x EBX=%08x ECX=%08x EDX=%08x EDI=%08x ]\n",
168           context->Eax, context->Ebx, context->Ecx, context->Edx, context->Edi);
169
170     INT13_SetStatus( context, 0x00 ); /* success */
171
172     /* FIXME: Word exits quietly if we return with no error. Why? */
173     FIXME("Returned ERROR!\n");
174     SET_CFLAG( context );
175
176 #else
177     INT13_SetStatus( context, 0x01 ); /* invalid function */
178 #endif
179 }
180
181
182 /**********************************************************************
183  *         DOSVM_Int13Handler (WINEDOS16.119)
184  *
185  * Handler for int 13h (disk I/O).
186  */
187 void WINAPI DOSVM_Int13Handler( CONTEXT86 *context )
188 {
189     TRACE( "AH=%02x\n", AH_reg( context ) );
190
191     switch( AH_reg( context ) )
192     {
193     case 0x00: /* RESET DISK SYSTEM */
194         INT13_SetStatus( context, 0x00 ); /* success */
195         break;
196
197     case 0x01: /* STATUS OF DISK SYSTEM */
198         INT13_SetStatus( context, INT13_last_status );
199         break;
200
201     case 0x02: /* READ SECTORS INTO MEMORY */
202         SET_AL( context, 0 ); /* number of sectors transferred */
203         INT13_SetStatus( context, 0x00 ); /* success */
204         break;
205
206     case 0x03: /* WRITE SECTORS FROM MEMORY */
207         SET_AL( context, 0 ); /* number of sectors transferred */
208         INT13_SetStatus( context, 0x00 ); /* success */
209         break;
210
211     case 0x04: /* VERIFY DISK SECTOR(S) */
212         SET_AL( context, 0 ); /* number of sectors verified */
213         INT13_SetStatus( context, 0x00 ); /* success */
214         break;
215
216     case 0x05: /* FORMAT TRACK */
217     case 0x06: /* FORMAT TRACK AND SET BAD SECTOR FLAGS */
218     case 0x07: /* FORMAT DRIVE STARTING AT GIVEN TRACK  */
219         INT13_SetStatus( context, 0x0c ); /* unsupported track or invalid media */
220         break;
221
222     case 0x08: /* GET DRIVE PARAMETERS  */
223         if (DL_reg( context ) & 0x80) 
224         {
225             /* hard disk ? */
226             INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
227         }
228         else
229         { 
230             /* floppy disk */
231             INT13_ReadFloppyParams( context );
232         }
233         break;
234
235     case 0x09: /* INITIALIZE CONTROLLER WITH DRIVE PARAMETERS */
236     case 0x0a: /* FIXED DISK - READ LONG */
237     case 0x0b: /* FIXED DISK - WRITE LONG */
238     case 0x0c: /* SEEK TO CYLINDER */
239     case 0x0d: /* ALTERNATE RESET HARD DISK */
240         INT13_SetStatus( context, 0x00 ); /* success */
241         break;
242
243     case 0x0e: /* READ SECTOR BUFFER */
244     case 0x0f: /* WRITE SECTOR BUFFER */
245         INT13_SetStatus( context, 0x01 ); /* invalid function */
246         break;
247
248     case 0x10: /* CHECK IF DRIVE READY */
249     case 0x11: /* RECALIBRATE DRIVE */
250         INT13_SetStatus( context, 0x00 ); /* success */
251         break;
252
253     case 0x12: /* CONTROLLER RAM DIAGNOSTIC */
254     case 0x13: /* DRIVE DIAGNOSTIC */
255         INT13_SetStatus( context, 0x01 ); /* invalid function */
256         break;
257
258     case 0x14: /* CONTROLLER INTERNAL DIAGNOSTIC */
259         INT13_SetStatus( context, 0x00 ); /* success */
260         break;
261
262     case 0x15: /* GET DISK TYPE */
263         if (DL_reg( context ) & 0x80) 
264         {
265             /* hard disk ? */
266             INT13_SetStatus( context, 0x00 ); /* success */
267             /* type is fixed disk, overwrites status */
268             SET_AH( context, 0x03 );
269         }
270         else
271         { 
272             /* floppy disk */
273             INT13_SetStatus( context, 0x00 ); /* success */
274             /* type is floppy with change detection, overwrites status */
275             SET_AH( context, 0x02 );
276         }
277         break;
278
279     case 0x16: /* FLOPPY - CHANGE OF DISK STATUS */
280         INT13_SetStatus( context, 0x00 ); /* success */
281         break;
282
283     case 0x17: /* SET DISK TYPE FOR FORMAT */
284         if (DL_reg( context ) < 4)
285             INT13_SetStatus( context, 0x00 ); /* successful completion */
286         else
287             INT13_SetStatus( context, 0x01 ); /* error */
288         break;
289
290     case 0x18: /* SET MEDIA TYPE FOR FORMAT */
291         if (DL_reg( context ) < 4)
292             INT13_SetStatus( context, 0x00 ); /* success */
293         else
294             INT13_SetStatus( context, 0x01 ); /* error */
295         break;
296
297     case 0x19: /* FIXED DISK - PARK HEADS */
298         INT13_SetStatus( context, 0x00 ); /* success */
299         break;
300
301     default:
302         INT_BARF( context, 0x13 );
303         INT13_SetStatus( context, 0x01 ); /* invalid function */
304     } 
305 }