Fixed a problem were a trace accesses a data member that may be
[wine] / dlls / winedos / dosaspi.c
1 /*
2  * Copyright 2000 David Elliott
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wine/windef16.h"
26 #include "wine/winaspi.h"
27 #include "wine/debug.h"
28 #include "miscemu.h" /* DOSMEM_* */
29 #include "dosexe.h"
30 #include "winerror.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(aspi);
33
34 static HINSTANCE hWNASPI32 = INVALID_HANDLE_VALUE;
35 static DWORD (__cdecl *pSendASPI32Command) (LPSRB) = NULL;
36
37 static void
38 DOSASPI_PostProc( SRB_ExecSCSICmd *lpPRB )
39 {
40         DWORD ptrSRB;
41         LPSRB16 lpSRB16;
42
43
44         memcpy(&ptrSRB,lpPRB->SenseArea + lpPRB->SRB_SenseLen,sizeof(DWORD));
45         TRACE("Copying data back to DOS client at 0x%8lx\n",ptrSRB);
46         lpSRB16 = PTR_REAL_TO_LIN(SELECTOROF(ptrSRB),OFFSETOF(ptrSRB));
47         lpSRB16->cmd.SRB_TargStat = lpPRB->SRB_TargStat;
48         lpSRB16->cmd.SRB_HaStat = lpPRB->SRB_HaStat;
49         memcpy(lpSRB16->cmd.CDBByte + lpSRB16->cmd.SRB_CDBLen,lpPRB->SenseArea,lpSRB16->cmd.SRB_SenseLen);
50
51         /* Now do posting */
52         if( lpPRB->SRB_Status == SS_SECURITY_VIOLATION )
53         {
54                 /* SS_SECURITY_VIOLATION isn't defined in DOS ASPI */
55                 TRACE("Returning SS_NO_DEVICE for SS_SECURITY_VIOLATION\n");
56                 lpPRB->SRB_Status = SS_NO_DEVICE;
57         }
58
59         lpSRB16->cmd.SRB_Status = lpPRB->SRB_Status;
60         TRACE("SRB_Status = 0x%x\n", lpPRB->SRB_Status);
61
62         HeapFree(GetProcessHeap(),0,lpPRB);
63
64         if( (lpSRB16->cmd.SRB_Flags & SRB_POSTING) && lpSRB16->cmd.SRB_PostProc )
65         {
66                 CONTEXT86 ctx;
67 /* The stack should look like this on entry to proc
68  * NOTE: the SDK draws the following diagram bass akwards, use this one
69  * to avoid being confused.  Remember, the act of pushing something on
70  * an intel stack involves decreasing the stack pointer by the size of
71  * the data, and then copying the data at the new SP.
72  */
73 /***************************
74  * ... Other crap that is already on the stack ...
75  * Segment of SRB Pointer               <- SP+6
76  * Offset of SRB Pointer                <- SP+4
77  * Segment of return address            <- SP+2
78  * Offset of return address             <- SP+0
79  */
80                 /* FIXME: I am about 99% sure what is here is correct,
81                  * but this code has never been tested (and probably
82                  * won't be either until someone finds a DOS program
83                  * that actually uses a Post Routine) */
84
85                 /* Zero everything */
86                 memset(&ctx, 0, sizeof(ctx));
87                 ctx.EFlags |= V86_FLAG;
88
89                 /* CS:IP is routine to call */
90                 ctx.SegCs = SELECTOROF(lpSRB16->cmd.SRB_PostProc);
91                 ctx.Eip   = OFFSETOF(lpSRB16->cmd.SRB_PostProc);
92                 /* DPMI_CallRMProc will push the pointer to the stack
93                  * it is given (in this case &ptrSRB) with length
94                  * 2*sizeof(WORD), that is, it copies the the contents
95                  * of ptrSRB onto the stack, and decs sp by 2*sizeof(WORD).
96                  * After doing that, it pushes the return address
97                  * onto the stack (so we don't need to worry about that)
98                  * So the stack should be okay for the PostProc
99                  */
100                 if(DPMI_CallRMProc(&ctx, (LPWORD)&ptrSRB, 2, FALSE))
101                 {
102                         TRACE("DPMI_CallRMProc returned nonzero (error) status\n");
103                 }
104         } /* if ((SRB_Flags&SRB_POSTING) && SRB_PostProc) */
105 }
106
107 static
108 DWORD ASPI_SendASPIDOSCommand(DWORD ptrSRB)
109 {
110         PSRB_ExecSCSICmd lpPRB;
111         DWORD retval;
112         union tagSRB16 * lpSRB16;
113
114         lpSRB16 = PTR_REAL_TO_LIN(SELECTOROF(ptrSRB),OFFSETOF(ptrSRB));
115
116         retval = SS_ERR;
117         switch( lpSRB16->common.SRB_Cmd )
118         {
119         case SC_HA_INQUIRY:
120                 TRACE("SC_HA_INQUIRY\n");
121                 /* Format is identical in this case */
122                 retval = (*pSendASPI32Command)((LPSRB)lpSRB16);
123                 break;
124         case SC_GET_DEV_TYPE:
125                 TRACE("SC_GET_DEV_TYPE\n");
126                 /* Format is identical in this case */
127                 retval = (*pSendASPI32Command)((LPSRB)lpSRB16);
128                 break;
129         case SC_EXEC_SCSI_CMD:
130                 TRACE("SC_EXEC_SCSI_CMD\n");
131                 TRACE("Copying data from DOS client at 0x%8lx\n",ptrSRB);
132                 lpPRB = HeapAlloc(GetProcessHeap(),0,sizeof(SRB)+lpSRB16->cmd.SRB_SenseLen+sizeof(DWORD));
133 #define srb_dos_to_w32(name) \
134                 lpPRB->SRB_##name = lpSRB16->cmd.SRB_##name
135
136                 srb_dos_to_w32(Cmd);
137                 srb_dos_to_w32(Status);
138                 srb_dos_to_w32(HaId);
139                 srb_dos_to_w32(BufLen);
140                 srb_dos_to_w32(SenseLen);
141                 srb_dos_to_w32(CDBLen);
142                 srb_dos_to_w32(Target);
143                 srb_dos_to_w32(Lun);
144 #undef srb_dos_to_w32
145
146                 /* Allow certain flags to go on to WNASPI32, we also need
147                  * to make sure SRB_POSTING is enabled */
148                 lpPRB->SRB_Flags = SRB_POSTING | (lpSRB16->cmd.SRB_Flags&(SRB_DIR_IN|SRB_DIR_OUT|SRB_ENABLE_RESIDUAL_COUNT));
149
150                 /* Pointer to data buffer */
151                 lpPRB->SRB_BufPointer = PTR_REAL_TO_LIN(SELECTOROF(lpSRB16->cmd.SRB_BufPointer),
152                                                         OFFSETOF(lpSRB16->cmd.SRB_BufPointer));
153                 /* Copy CDB in */
154                 memcpy(&lpPRB->CDBByte[0],&lpSRB16->cmd.CDBByte[0],lpSRB16->cmd.SRB_CDBLen);
155
156                 /* Set post proc to our post proc */
157                 lpPRB->SRB_PostProc = &DOSASPI_PostProc;
158
159                 /* Stick the DWORD after all the sense info */
160                 memcpy(lpPRB->SenseArea + lpPRB->SRB_SenseLen,&ptrSRB,sizeof(DWORD));
161                 retval = (*pSendASPI32Command)((LPSRB)lpPRB);
162                 break;
163         case SC_ABORT_SRB:
164                 TRACE("SC_ABORT_SRB\n");
165                 /* Would need some sort of table of active shit */
166                 break;
167         case SC_RESET_DEV:
168                 TRACE("SC_RESET_DEV\n");
169                 break;
170         default:
171                 TRACE("Unkown command code\n");
172                 break;
173         }
174
175         TRACE("Returning %lx\n", retval );
176         return retval;
177 }
178
179 void WINAPI ASPI_DOS_func(CONTEXT86 *context)
180 {
181         WORD *stack = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
182         DWORD ptrSRB = *(DWORD *)&stack[2];
183
184         ASPI_SendASPIDOSCommand(ptrSRB);
185
186         /* simulate a normal RETF sequence as required by DPMI CallRMProcFar */
187         context->Eip = *(stack++);
188         context->SegCs  = *(stack++);
189         context->Esp += 2*sizeof(WORD);
190 }
191
192
193 /**********************************************************************
194  *          ASPIHandler  (WINEDOS.@)
195  *
196  * returns the address of a real mode callback to ASPI_DOS_func()
197  */
198 void WINAPI DOSVM_ASPIHandler( CONTEXT86 *context )
199 {
200         FARPROC16 *p = (FARPROC16 *)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx);
201         TRACE("DOS ASPI opening\n");
202         if ((CX_reg(context) == 4) || (CX_reg(context) == 5))
203         {
204                 if( hWNASPI32 == INVALID_HANDLE_VALUE )
205                 {
206                         TRACE("Loading WNASPI32\n");
207                         hWNASPI32 = LoadLibraryExA("WNASPI32", 0, 0);
208                 }
209
210                 if( hWNASPI32 == INVALID_HANDLE_VALUE )
211                 {
212                         ERR("Error loading WNASPI32\n");
213                         goto error_exit;
214                 }
215
216                 /* Get SendASPI32Command by Ordinal 2 */
217                 /* Cast to correct argument/return types */
218                 pSendASPI32Command = (DWORD (*)(LPSRB))GetProcAddress(hWNASPI32, (LPBYTE)2);
219                 if( !pSendASPI32Command )
220                 {
221                         ERR("Error getting ordinal 2 from WNASPI32\n");
222                         goto error_exit;
223                 }
224
225                 *p = DPMI_AllocInternalRMCB(ASPI_DOS_func);
226                 TRACE("allocated real mode proc %p\n", *p);
227                 SET_AX( context, CX_reg(context) );
228
229                 return;
230         }
231 error_exit:
232         /* Return some error... General Failure sounds okay */
233         SET_AX( context, ERROR_GEN_FAILURE );
234         SET_CFLAG(context);
235 }