push c4c845830c6aff14e1b16bbb8b4a57a7e0d6213f
[wine/hacks.git] / dlls / winedos / int13.c
blob6b2b00c83d8312553df6d6ec3cb288ef081541ac
1 /*
2 * BIOS interrupt 13h handler
4 * Copyright 1997 Andreas Mohr
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.
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.
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
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
40 #include "dosexe.h"
41 #include "wine/server.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(int);
48 * Status of last int13 operation.
50 static BYTE INT13_last_status;
53 /**********************************************************************
54 * INT13_SetStatus
56 * Write status to AH register and set carry flag on error (AH != 0).
58 * Despite what Ralf Brown says, at least functions 0x06 and 0x07
59 * seem to set carry, too.
61 static void INT13_SetStatus( CONTEXT86 *context, BYTE status )
63 INT13_last_status = status;
65 SET_AH( context, status );
67 if (status)
68 SET_CFLAG( context );
69 else
70 RESET_CFLAG( context );
74 /**********************************************************************
75 * INT13_ReadFloppyParams
77 * Read floppy disk parameters.
79 static void INT13_ReadFloppyParams( CONTEXT86 *context )
81 #ifdef linux
82 static const BYTE floppy_params[2][13] =
84 { 0xaf, 0x02, 0x25, 0x02, 0x12, 0x1b, 0xff, 0x6c, 0xf6, 0x0f, 0x08 },
85 { 0xaf, 0x02, 0x25, 0x02, 0x12, 0x1b, 0xff, 0x6c, 0xf6, 0x0f, 0x08 }
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 */
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;
107 TRACE("in [ EDX=%08x ]\n", context->Edx );
109 SET_AL( context, 0 );
110 SET_BX( context, 0 );
111 SET_CX( context, 0 );
112 SET_DH( context, 0 );
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 );
118 if (drive_nr > 1) {
119 /* invalid drive ? */
120 INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
121 return;
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))
130 WARN("Can't determine floppy geometry !\n");
131 INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
132 return;
134 r = ioctl(floppy_fd, FDGETDRVPRM, &floppy_parm);
135 wine_server_release_fd( h, floppy_fd );
136 CloseHandle(h);
138 if(r<0)
140 INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
141 return;
144 SET_BL( context, floppy_parm.cmos );
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
152 if(BL_reg( context ) && BL_reg( context ) < 7)
154 SET_DH( context, 0x01 );
155 SET_CX( context, drive_type_info[BL_reg( context )] );
158 context->Edi = (DWORD)floppy_params[drive_nr];
160 if(!context->Edi)
162 ERR("Get floppy params failed for drive %d\n", drive_nr);
163 INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
164 return;
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);
170 INT13_SetStatus( context, 0x00 ); /* success */
172 /* FIXME: Word exits quietly if we return with no error. Why? */
173 FIXME("Returned ERROR!\n");
174 SET_CFLAG( context );
176 #else
177 INT13_SetStatus( context, 0x01 ); /* invalid function */
178 #endif
182 /**********************************************************************
183 * DOSVM_Int13Handler (WINEDOS16.119)
185 * Handler for int 13h (disk I/O).
187 void WINAPI DOSVM_Int13Handler( CONTEXT86 *context )
189 TRACE( "AH=%02x\n", AH_reg( context ) );
191 switch( AH_reg( context ) )
193 case 0x00: /* RESET DISK SYSTEM */
194 INT13_SetStatus( context, 0x00 ); /* success */
195 break;
197 case 0x01: /* STATUS OF DISK SYSTEM */
198 INT13_SetStatus( context, INT13_last_status );
199 break;
201 case 0x02: /* READ SECTORS INTO MEMORY */
202 SET_AL( context, 0 ); /* number of sectors transferred */
203 INT13_SetStatus( context, 0x00 ); /* success */
204 break;
206 case 0x03: /* WRITE SECTORS FROM MEMORY */
207 SET_AL( context, 0 ); /* number of sectors transferred */
208 INT13_SetStatus( context, 0x00 ); /* success */
209 break;
211 case 0x04: /* VERIFY DISK SECTOR(S) */
212 SET_AL( context, 0 ); /* number of sectors verified */
213 INT13_SetStatus( context, 0x00 ); /* success */
214 break;
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;
222 case 0x08: /* GET DRIVE PARAMETERS */
223 if (DL_reg( context ) & 0x80)
225 /* hard disk ? */
226 INT13_SetStatus( context, 0x07 ); /* drive parameter activity failed */
228 else
230 /* floppy disk */
231 INT13_ReadFloppyParams( context );
233 break;
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;
243 case 0x0e: /* READ SECTOR BUFFER */
244 case 0x0f: /* WRITE SECTOR BUFFER */
245 INT13_SetStatus( context, 0x01 ); /* invalid function */
246 break;
248 case 0x10: /* CHECK IF DRIVE READY */
249 case 0x11: /* RECALIBRATE DRIVE */
250 INT13_SetStatus( context, 0x00 ); /* success */
251 break;
253 case 0x12: /* CONTROLLER RAM DIAGNOSTIC */
254 case 0x13: /* DRIVE DIAGNOSTIC */
255 INT13_SetStatus( context, 0x01 ); /* invalid function */
256 break;
258 case 0x14: /* CONTROLLER INTERNAL DIAGNOSTIC */
259 INT13_SetStatus( context, 0x00 ); /* success */
260 break;
262 case 0x15: /* GET DISK TYPE */
263 if (DL_reg( context ) & 0x80)
265 /* hard disk ? */
266 INT13_SetStatus( context, 0x00 ); /* success */
267 /* type is fixed disk, overwrites status */
268 SET_AH( context, 0x03 );
270 else
272 /* floppy disk */
273 INT13_SetStatus( context, 0x00 ); /* success */
274 /* type is floppy with change detection, overwrites status */
275 SET_AH( context, 0x02 );
277 break;
279 case 0x16: /* FLOPPY - CHANGE OF DISK STATUS */
280 INT13_SetStatus( context, 0x00 ); /* success */
281 break;
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;
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;
297 case 0x19: /* FIXED DISK - PARK HEADS */
298 INT13_SetStatus( context, 0x00 ); /* success */
299 break;
301 default:
302 INT_BARF( context, 0x13 );
303 INT13_SetStatus( context, 0x01 ); /* invalid function */