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