~ubuntu-branches/ubuntu/vivid/basilisk2/vivid

« back to all changes in this revision

Viewing changes to src/Windows/cdenable/ntcd.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-03-06 19:33:01 UTC
  • mfrom: (2.1.4 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080306193301-cc2ofn705nfsq3y0
Tags: 0.9.20070407-4
* Update copyright-check cdbs snippet to parse licensecheck using perl:
  + No longer randomly drops newlines
  + More compact hint file (and ordered more like wiki-proposed new copyright
    syntax).
  + No longer ignore files without copyright.
* Update copyright_hints.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  ntcd.cpp - Interface to cdenable.sys driver
 
3
 *
 
4
 *  Basilisk II (C) 1997-2005 Christian Bauer
 
5
 *
 
6
 *  Windows platform specific code copyright (C) Lauri Pesonen
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  This program 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
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program; if not, write to the Free Software
 
20
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 */
 
22
 
 
23
#include "sysdeps.h"
 
24
#include <windows.h>
 
25
 
 
26
extern "C" {
 
27
 
 
28
#include <winioctl.h>
 
29
#include <winsvc.h>
 
30
#include "ntcd.h"
 
31
#include "cdenable.h"
 
32
 
 
33
static char *sDriverShort   = "cdenable";
 
34
static char *sDriverLong  = "System32\\Drivers\\cdenable.sys";
 
35
static char *sCompleteName  = "\\\\.\\cdenable";
 
36
 
 
37
#ifdef _DEBUG
 
38
#define new DEBUG_NEW
 
39
#undef THIS_FILE
 
40
static char THIS_FILE[] = __FILE__;
 
41
#endif
 
42
 
 
43
// Start type must be SERVICE_AUTO_START or lower, in order
 
44
// it to start automatically and allow the mechanism work
 
45
// for users with no admin rights.
 
46
static BOOL InstallDriver(
 
47
  IN SC_HANDLE  SchSCManager,
 
48
  IN LPCTSTR    DriverName,
 
49
  IN LPCTSTR    ServiceExe
 
50
)
 
51
{
 
52
  SC_HANDLE  schService;
 
53
  DWORD      err;
 
54
 
 
55
  schService = CreateService (
 
56
                SchSCManager,          // SCManager database
 
57
    DriverName,            // name of service
 
58
    DriverName,            // name to display
 
59
    SERVICE_ALL_ACCESS,    // desired access
 
60
    SERVICE_KERNEL_DRIVER, // service type
 
61
                SERVICE_AUTO_START,              // SERVICE_DEMAND_START,  // start type
 
62
    SERVICE_ERROR_NORMAL,  // error control type
 
63
    ServiceExe,            // service's binary
 
64
    NULL,                  // no load ordering group
 
65
    NULL,                  // no tag identifier
 
66
    NULL,                  // no dependencies
 
67
    NULL,                  // LocalSystem account
 
68
    NULL                   // no password
 
69
    );
 
70
 
 
71
  if (schService == NULL) {
 
72
      err = GetLastError();
 
73
      if (err == ERROR_SERVICE_EXISTS) {
 
74
                                  return TRUE;
 
75
      } else {
 
76
                      return FALSE;
 
77
      }
 
78
  }
 
79
  CloseServiceHandle (schService);
 
80
  return TRUE;
 
81
}
 
82
 
 
83
static BOOL RemoveDriver(
 
84
  IN SC_HANDLE  SchSCManager,
 
85
  IN LPCTSTR    DriverName
 
86
)
 
87
{
 
88
  SC_HANDLE  schService;
 
89
  BOOL       ret;
 
90
 
 
91
  schService = OpenService (SchSCManager,
 
92
                            DriverName,
 
93
                            SERVICE_ALL_ACCESS
 
94
                            );
 
95
  if (schService == NULL) return FALSE;
 
96
  ret = DeleteService (schService);
 
97
  CloseServiceHandle (schService);
 
98
  return ret;
 
99
}
 
100
 
 
101
static BOOL StartDriver(
 
102
  IN SC_HANDLE  SchSCManager,
 
103
  IN LPCTSTR    DriverName
 
104
) {
 
105
  SC_HANDLE  schService;
 
106
  BOOL       ret;
 
107
  DWORD      err;
 
108
 
 
109
  schService = OpenService (SchSCManager,
 
110
                            DriverName,
 
111
                            SERVICE_ALL_ACCESS
 
112
                            );
 
113
  if (schService == NULL) return FALSE;
 
114
  ret = StartService (schService,    // service identifier
 
115
                      0,             // number of arguments
 
116
                      NULL           // pointer to arguments
 
117
                      );
 
118
  if(ret == 0) {
 
119
    err = GetLastError();
 
120
    if (err == ERROR_SERVICE_ALREADY_RUNNING) {
 
121
                        ret = TRUE;
 
122
    } else {
 
123
                        ret = FALSE;
 
124
                }
 
125
  }
 
126
  CloseServiceHandle (schService);
 
127
  return ret;
 
128
}
 
129
 
 
130
static BOOL StopDriver(
 
131
  IN SC_HANDLE  SchSCManager,
 
132
  IN LPCTSTR    DriverName
 
133
)
 
134
{
 
135
  SC_HANDLE       schService;
 
136
  BOOL            ret;
 
137
  SERVICE_STATUS  serviceStatus;
 
138
 
 
139
  schService = OpenService (SchSCManager,
 
140
                            DriverName,
 
141
                            SERVICE_ALL_ACCESS
 
142
                            );
 
143
  if (schService == NULL) return FALSE;
 
144
  ret = ControlService (schService,
 
145
                        SERVICE_CONTROL_STOP,
 
146
                        &serviceStatus
 
147
                        );
 
148
  CloseServiceHandle (schService);
 
149
  return ret;
 
150
}
 
151
 
 
152
static BOOL __cdecl start_driver( void )
 
153
{
 
154
        SC_HANDLE   schSCManager;
 
155
        BOOL ret = FALSE;
 
156
 
 
157
        schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
 
158
        if(!schSCManager) return(FALSE);
 
159
        if(!InstallDriver( schSCManager, sDriverShort, sDriverLong )) {
 
160
                CloseServiceHandle( schSCManager );
 
161
                return(FALSE);
 
162
        }
 
163
        ret = StartDriver( schSCManager, sDriverShort );
 
164
        if(!ret) {
 
165
                (void)RemoveDriver( schSCManager, sDriverShort );
 
166
        }
 
167
        CloseServiceHandle( schSCManager );
 
168
        return( ret );
 
169
}
 
170
 
 
171
static BOOL __cdecl stop_driver( void )
 
172
{
 
173
        SC_HANDLE   schSCManager;
 
174
        BOOL ret = FALSE;
 
175
 
 
176
        schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
 
177
        if(!schSCManager) return(FALSE);
 
178
        if(StopDriver( schSCManager, sDriverShort )) ret = TRUE;
 
179
        CloseServiceHandle( schSCManager );
 
180
        return( ret );
 
181
}
 
182
 
 
183
static BOOL __cdecl remove_driver( void )
 
184
{
 
185
        SC_HANDLE   schSCManager;
 
186
        BOOL ret = FALSE;
 
187
 
 
188
        schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
 
189
        if(!schSCManager) return(FALSE);
 
190
        if(RemoveDriver( schSCManager, sDriverShort )) ret = TRUE;
 
191
        CloseServiceHandle( schSCManager );
 
192
        return( ret );
 
193
}
 
194
 
 
195
 
 
196
 
 
197
// Exported stuff begins
 
198
 
 
199
int CdenableSysReadCdBytes( HANDLE h, DWORD start, DWORD count, char *buf )
 
200
{
 
201
  HANDLE   hDevice;
 
202
  int      ret;
 
203
        DWORD            nb;
 
204
        DWORD    in_buffer[10];
 
205
        DWORD    out_buffer[10];
 
206
 
 
207
  ret = 0;
 
208
 
 
209
        in_buffer[0] = (DWORD)h;
 
210
        in_buffer[1] = (DWORD)start;
 
211
        in_buffer[2] = (DWORD)count;
 
212
        in_buffer[3] = (DWORD)buf;
 
213
        out_buffer[0] = 0;
 
214
 
 
215
  hDevice = CreateFile (sCompleteName,
 
216
                        GENERIC_READ | GENERIC_WRITE,
 
217
                        0,
 
218
                        NULL,
 
219
                        OPEN_EXISTING,
 
220
                        FILE_ATTRIBUTE_NORMAL,
 
221
                        NULL
 
222
                        );
 
223
 
 
224
  if (hDevice == ((HANDLE)-1)) {
 
225
    ret = 0;
 
226
        } else {
 
227
                if ( DeviceIoControl(   hDevice,
 
228
                                        IOCTL_CDENABLE_READ,
 
229
                                        (LPVOID)in_buffer, 16,
 
230
                                        (LPVOID)out_buffer, 4,
 
231
                                        &nb, NULL ) )
 
232
                {
 
233
                        if(out_buffer[0] != 0) ret = count;
 
234
                }
 
235
    CloseHandle (hDevice);
 
236
  }
 
237
 
 
238
  return ret;
 
239
}
 
240
 
 
241
int CdenableSysReadCdSectors( HANDLE h, DWORD start, DWORD count, char *buf )
 
242
{
 
243
        return( CdenableSysReadCdBytes( h, (start<<11), (count<<11), buf ) );
 
244
}
 
245
 
 
246
int CdenableSysWriteCdBytes( HANDLE h, DWORD start, DWORD count, char *buf )
 
247
{
 
248
        return( 0 );
 
249
 
 
250
        /*
 
251
  HANDLE   hDevice;
 
252
  int      ret;
 
253
        DWORD            nb;
 
254
        DWORD    in_buffer[10];
 
255
        DWORD    out_buffer[10];
 
256
 
 
257
  ret = 0;
 
258
 
 
259
        in_buffer[0] = (DWORD)h;
 
260
        in_buffer[1] = (DWORD)start;
 
261
        in_buffer[2] = (DWORD)count;
 
262
        in_buffer[3] = (DWORD)buf;
 
263
        out_buffer[0] = 0;
 
264
 
 
265
  hDevice = CreateFile (sCompleteName,
 
266
                        GENERIC_READ | GENERIC_WRITE,
 
267
                        0,
 
268
                        NULL,
 
269
                        OPEN_EXISTING,
 
270
                        FILE_ATTRIBUTE_NORMAL,
 
271
                        NULL
 
272
                        );
 
273
 
 
274
  if (hDevice == ((HANDLE)-1)) {
 
275
    ret = 0;
 
276
        } else {
 
277
                if ( DeviceIoControl(   hDevice,
 
278
                                        IOCTL_CDENABLE_WRITE,
 
279
                                        (LPVOID)in_buffer, 16,
 
280
                                        (LPVOID)out_buffer, 4,
 
281
                                        &nb, NULL ) )
 
282
                {
 
283
                        if(out_buffer[0] != 0) ret = count;
 
284
                }
 
285
    CloseHandle (hDevice);
 
286
  }
 
287
 
 
288
  return ret;
 
289
        */
 
290
}
 
291
 
 
292
int CdenableSysWriteCdSectors( HANDLE h, DWORD start, DWORD count, char *buf )
 
293
{
 
294
        // return( CdenableSysWriteCdBytes( h, (start<<11), (count<<11), buf ) );
 
295
        return( 0 );
 
296
}
 
297
 
 
298
BOOL CdenableSysInstallStart(void)
 
299
{
 
300
        return(start_driver());
 
301
}
 
302
 
 
303
void CdenableSysStopRemove(void)
 
304
{
 
305
        stop_driver();
 
306
        remove_driver();
 
307
}
 
308
 
 
309
DWORD CdenableSysGetVersion( void )
 
310
{
 
311
  HANDLE   hDevice;
 
312
  DWORD    ret;
 
313
        DWORD            nb;
 
314
        DWORD    out_buffer[10];
 
315
 
 
316
  ret = 0;
 
317
        out_buffer[0] = 0;
 
318
  hDevice = CreateFile (sCompleteName,
 
319
                        GENERIC_READ | GENERIC_WRITE,
 
320
                        0,
 
321
                        NULL,
 
322
                        OPEN_EXISTING,
 
323
                        FILE_ATTRIBUTE_NORMAL,
 
324
                        NULL
 
325
                        );
 
326
  if (hDevice == ((HANDLE)-1)) {
 
327
    ret = 0;
 
328
        } else {
 
329
                if ( DeviceIoControl(   hDevice,
 
330
                                        IOCTL_CDENABLE_GET_VERSION,
 
331
                                        NULL, 0,
 
332
                                        (LPVOID)out_buffer, 4,
 
333
                                        &nb, NULL ) )
 
334
                {
 
335
                        ret = out_buffer[0];
 
336
                }
 
337
    CloseHandle (hDevice);
 
338
  }
 
339
  return ret;
 
340
}
 
341
 
 
342
#ifdef __cplusplus
 
343
} //extern "C"
 
344
#endif
 
345