winhlp32: Use Win32 APIs instead of strdup().
[wine] / dlls / vwin32.vxd / vwin32.c
1 /*
2  * VWIN32 VxD implementation
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 1998 Ulrich Weigand
6  * Copyright 1998 Patrik Stridvall
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winioctl.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
31
32 typedef struct tagDIOCRegs {
33     DWORD   reg_EBX;
34     DWORD   reg_EDX;
35     DWORD   reg_ECX;
36     DWORD   reg_EAX;
37     DWORD   reg_EDI;
38     DWORD   reg_ESI;
39     DWORD   reg_Flags;
40 } DIOC_REGISTERS, *PDIOC_REGISTERS;
41
42 #define VWIN32_DIOC_DOS_IOCTL     1 /* This is the specified MS-DOS device I/O ctl - Interrupt 21h Function 4400h - 4411h */
43 #define VWIN32_DIOC_DOS_INT25     2 /* This is the Absolute Disk Read command - Interrupt 25h */
44 #define VWIN32_DIOC_DOS_INT26     3 /* This is the Absolute Disk Write command - Interrupt 25h */
45 #define VWIN32_DIOC_DOS_INT13     4 /* This is Interrupt 13h commands */
46 #define VWIN32_DIOC_SIMCTRLC      5 /* Simulate Ctrl-C */
47 #define VWIN32_DIOC_DOS_DRIVEINFO 6 /* This is Interrupt 21h Function 730X commands */
48
49 #include <pshpack1.h>
50 typedef struct tagMID {
51     WORD  midInfoLevel;
52     DWORD midSerialNum;
53     BYTE  midVolLabel[11];
54     BYTE  midFileSysType[8];
55 } MID, *PMID;
56 #include <poppack.h>
57
58 typedef void (WINAPI *CallBuiltinHandler)( CONTEXT *context, BYTE intnum );
59
60 static CallBuiltinHandler load_builtin_handler(void)
61 {
62     static CallBuiltinHandler handler;
63     static BOOL init_done;
64
65     if (!init_done)
66     {
67         HMODULE mod = LoadLibraryA( "winedos.dll" );
68         if (mod) handler = (void *)GetProcAddress( mod, "CallBuiltinHandler" );
69         if (!handler) FIXME( "DOS calls not supported\n" );
70         init_done = TRUE;
71     }
72     return handler;
73 }
74
75 /* Pop a DWORD from the 32-bit stack */
76 static inline DWORD stack32_pop( CONTEXT86 *context )
77 {
78     DWORD ret = *(DWORD *)context->Esp;
79     context->Esp += sizeof(DWORD);
80     return ret;
81 }
82
83 static void DIOCRegs_2_CONTEXT( DIOC_REGISTERS *pIn, CONTEXT86 *pCxt )
84 {
85     memset( pCxt, 0, sizeof(*pCxt) );
86     /* Note: segment registers == 0 means that CTX_SEG_OFF_TO_LIN
87              will interpret 32-bit register contents as linear pointers */
88
89     pCxt->ContextFlags=CONTEXT86_INTEGER|CONTEXT86_CONTROL;
90     pCxt->Eax = pIn->reg_EAX;
91     pCxt->Ebx = pIn->reg_EBX;
92     pCxt->Ecx = pIn->reg_ECX;
93     pCxt->Edx = pIn->reg_EDX;
94     pCxt->Esi = pIn->reg_ESI;
95     pCxt->Edi = pIn->reg_EDI;
96
97     /* FIXME: Only partial CONTEXT86_CONTROL */
98
99     pCxt->EFlags = pIn->reg_Flags & ~0x00020000; /* clear vm86 mode */
100 }
101
102 static void CONTEXT_2_DIOCRegs( CONTEXT86 *pCxt, DIOC_REGISTERS *pOut )
103 {
104     memset( pOut, 0, sizeof(DIOC_REGISTERS) );
105
106     pOut->reg_EAX = pCxt->Eax;
107     pOut->reg_EBX = pCxt->Ebx;
108     pOut->reg_ECX = pCxt->Ecx;
109     pOut->reg_EDX = pCxt->Edx;
110     pOut->reg_ESI = pCxt->Esi;
111     pOut->reg_EDI = pCxt->Edi;
112
113     /* FIXME: Only partial CONTEXT86_CONTROL */
114     pOut->reg_Flags = pCxt->EFlags;
115 }
116
117 /***********************************************************************
118  *           DeviceIoControl   (VWIN32.VXD.@)
119  */
120 BOOL WINAPI VWIN32_DeviceIoControl(DWORD dwIoControlCode,
121                                    LPVOID lpvInBuffer, DWORD cbInBuffer,
122                                    LPVOID lpvOutBuffer, DWORD cbOutBuffer,
123                                    LPDWORD lpcbBytesReturned, LPOVERLAPPED lpOverlapped)
124 {
125     switch (dwIoControlCode)
126     {
127     case VWIN32_DIOC_DOS_IOCTL:
128     case 0x10: /* Int 0x21 call, call it VWIN_DIOC_INT21 ? */
129     case VWIN32_DIOC_DOS_INT13:
130     case VWIN32_DIOC_DOS_INT25:
131     case VWIN32_DIOC_DOS_INT26:
132     case 0x29: /* Int 0x31 call, call it VWIN_DIOC_INT31 ? */
133     case VWIN32_DIOC_DOS_DRIVEINFO:
134         {
135             CONTEXT86 cxt;
136             DIOC_REGISTERS *pIn  = lpvInBuffer;
137             DIOC_REGISTERS *pOut = lpvOutBuffer;
138             BYTE intnum = 0;
139             CallBuiltinHandler handler;
140
141             if (!(handler = load_builtin_handler()))
142             {
143                 pOut->reg_Flags |= 0x0001;
144                 return FALSE;
145             }
146
147             TRACE( "Control '%s': "
148                    "eax=0x%08x, ebx=0x%08x, ecx=0x%08x, "
149                    "edx=0x%08x, esi=0x%08x, edi=0x%08x\n",
150                    (dwIoControlCode == VWIN32_DIOC_DOS_IOCTL)? "VWIN32_DIOC_DOS_IOCTL" :
151                    (dwIoControlCode == VWIN32_DIOC_DOS_INT25)? "VWIN32_DIOC_DOS_INT25" :
152                    (dwIoControlCode == VWIN32_DIOC_DOS_INT26)? "VWIN32_DIOC_DOS_INT26" :
153                    (dwIoControlCode == VWIN32_DIOC_DOS_DRIVEINFO)? "VWIN32_DIOC_DOS_DRIVEINFO" :  "???",
154                    pIn->reg_EAX, pIn->reg_EBX, pIn->reg_ECX,
155                    pIn->reg_EDX, pIn->reg_ESI, pIn->reg_EDI );
156
157             DIOCRegs_2_CONTEXT( pIn, &cxt );
158
159             switch (dwIoControlCode)
160             {
161             case VWIN32_DIOC_DOS_IOCTL: /* Call int 21h */
162             case 0x10: /* Int 0x21 call, call it VWIN_DIOC_INT21 ? */
163             case VWIN32_DIOC_DOS_DRIVEINFO:        /* Call int 21h 730x */
164                 intnum = 0x21;
165                 break;
166             case VWIN32_DIOC_DOS_INT13:
167                 intnum = 0x13;
168                 break;
169             case VWIN32_DIOC_DOS_INT25:
170                 intnum = 0x25;
171                 break;
172             case VWIN32_DIOC_DOS_INT26:
173                 intnum = 0x26;
174                 break;
175             case 0x29: /* Int 0x31 call, call it VWIN_DIOC_INT31 ? */
176                 intnum = 0x31;
177                 break;
178             }
179
180             handler( &cxt, intnum );
181             CONTEXT_2_DIOCRegs( &cxt, pOut );
182         }
183         return TRUE;
184
185     case VWIN32_DIOC_SIMCTRLC:
186         FIXME( "Control VWIN32_DIOC_SIMCTRLC not implemented\n");
187         return FALSE;
188
189     default:
190         FIXME( "Unknown Control %d\n", dwIoControlCode);
191         return FALSE;
192     }
193 }
194
195
196 /***********************************************************************
197  *           VxDCall   (VWIN32.VXD.@)
198  *
199  *  Service numbers taken from page 448 of Pietrek's "Windows 95 System
200  *  Programming Secrets".  Parameters from experimentation on real Win98.
201  *
202  */
203 DWORD WINAPI VWIN32_VxDCall( DWORD service, CONTEXT86 *context )
204 {
205     switch ( LOWORD(service) )
206     {
207     case 0x0000: /* GetVersion */
208         {
209             DWORD vers = GetVersion();
210             return (LOBYTE(vers) << 8) | HIBYTE(vers);
211         }
212     case 0x0020: /* Get VMCPD Version */
213         {
214             DWORD parm = stack32_pop(context);
215
216             FIXME("Get VMCPD Version(%08x): partial stub!\n", parm);
217
218             /* FIXME: This is what Win98 returns, it may
219              *        not be correct in all situations.
220              *        It makes Bleem! happy though.
221              */
222             return 0x0405;
223         }
224     case 0x0029: /* Int31/DPMI dispatch */
225         {
226             DWORD callnum = stack32_pop(context);
227             DWORD parm    = stack32_pop(context);
228             CallBuiltinHandler handler;
229
230             TRACE("Int31/DPMI dispatch(%08x)\n", callnum);
231
232             if (!(handler = load_builtin_handler()))
233             {
234                 context->EFlags |= 0x0001;
235                 return 0;
236             }
237
238             context->Eax = callnum;
239             context->Ecx = parm;
240             handler( context, 0x31 );
241             return LOWORD(context->Eax);
242         }
243     case 0x002a: /* Int41 dispatch - parm = int41 service number */
244         {
245             DWORD callnum = stack32_pop(context);
246             return callnum; /* FIXME: should really call INT_Int41Handler() */
247         }
248     default:
249         FIXME("Unknown service %08x\n", service);
250         return 0xffffffff;
251     }
252 }