2
* ntcd.cpp - Interface to cdenable.sys driver
4
* Basilisk II (C) 1997-2005 Christian Bauer
6
* Windows platform specific code copyright (C) Lauri Pesonen
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.
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.
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
33
static char *sDriverShort = "cdenable";
34
static char *sDriverLong = "System32\\Drivers\\cdenable.sys";
35
static char *sCompleteName = "\\\\.\\cdenable";
40
static char THIS_FILE[] = __FILE__;
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,
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
71
if (schService == NULL) {
73
if (err == ERROR_SERVICE_EXISTS) {
79
CloseServiceHandle (schService);
83
static BOOL RemoveDriver(
84
IN SC_HANDLE SchSCManager,
91
schService = OpenService (SchSCManager,
95
if (schService == NULL) return FALSE;
96
ret = DeleteService (schService);
97
CloseServiceHandle (schService);
101
static BOOL StartDriver(
102
IN SC_HANDLE SchSCManager,
103
IN LPCTSTR DriverName
105
SC_HANDLE schService;
109
schService = OpenService (SchSCManager,
113
if (schService == NULL) return FALSE;
114
ret = StartService (schService, // service identifier
115
0, // number of arguments
116
NULL // pointer to arguments
119
err = GetLastError();
120
if (err == ERROR_SERVICE_ALREADY_RUNNING) {
126
CloseServiceHandle (schService);
130
static BOOL StopDriver(
131
IN SC_HANDLE SchSCManager,
132
IN LPCTSTR DriverName
135
SC_HANDLE schService;
137
SERVICE_STATUS serviceStatus;
139
schService = OpenService (SchSCManager,
143
if (schService == NULL) return FALSE;
144
ret = ControlService (schService,
145
SERVICE_CONTROL_STOP,
148
CloseServiceHandle (schService);
152
static BOOL __cdecl start_driver( void )
154
SC_HANDLE schSCManager;
157
schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
158
if(!schSCManager) return(FALSE);
159
if(!InstallDriver( schSCManager, sDriverShort, sDriverLong )) {
160
CloseServiceHandle( schSCManager );
163
ret = StartDriver( schSCManager, sDriverShort );
165
(void)RemoveDriver( schSCManager, sDriverShort );
167
CloseServiceHandle( schSCManager );
171
static BOOL __cdecl stop_driver( void )
173
SC_HANDLE schSCManager;
176
schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
177
if(!schSCManager) return(FALSE);
178
if(StopDriver( schSCManager, sDriverShort )) ret = TRUE;
179
CloseServiceHandle( schSCManager );
183
static BOOL __cdecl remove_driver( void )
185
SC_HANDLE schSCManager;
188
schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
189
if(!schSCManager) return(FALSE);
190
if(RemoveDriver( schSCManager, sDriverShort )) ret = TRUE;
191
CloseServiceHandle( schSCManager );
197
// Exported stuff begins
199
int CdenableSysReadCdBytes( HANDLE h, DWORD start, DWORD count, char *buf )
205
DWORD out_buffer[10];
209
in_buffer[0] = (DWORD)h;
210
in_buffer[1] = (DWORD)start;
211
in_buffer[2] = (DWORD)count;
212
in_buffer[3] = (DWORD)buf;
215
hDevice = CreateFile (sCompleteName,
216
GENERIC_READ | GENERIC_WRITE,
220
FILE_ATTRIBUTE_NORMAL,
224
if (hDevice == ((HANDLE)-1)) {
227
if ( DeviceIoControl( hDevice,
229
(LPVOID)in_buffer, 16,
230
(LPVOID)out_buffer, 4,
233
if(out_buffer[0] != 0) ret = count;
235
CloseHandle (hDevice);
241
int CdenableSysReadCdSectors( HANDLE h, DWORD start, DWORD count, char *buf )
243
return( CdenableSysReadCdBytes( h, (start<<11), (count<<11), buf ) );
246
int CdenableSysWriteCdBytes( HANDLE h, DWORD start, DWORD count, char *buf )
255
DWORD out_buffer[10];
259
in_buffer[0] = (DWORD)h;
260
in_buffer[1] = (DWORD)start;
261
in_buffer[2] = (DWORD)count;
262
in_buffer[3] = (DWORD)buf;
265
hDevice = CreateFile (sCompleteName,
266
GENERIC_READ | GENERIC_WRITE,
270
FILE_ATTRIBUTE_NORMAL,
274
if (hDevice == ((HANDLE)-1)) {
277
if ( DeviceIoControl( hDevice,
278
IOCTL_CDENABLE_WRITE,
279
(LPVOID)in_buffer, 16,
280
(LPVOID)out_buffer, 4,
283
if(out_buffer[0] != 0) ret = count;
285
CloseHandle (hDevice);
292
int CdenableSysWriteCdSectors( HANDLE h, DWORD start, DWORD count, char *buf )
294
// return( CdenableSysWriteCdBytes( h, (start<<11), (count<<11), buf ) );
298
BOOL CdenableSysInstallStart(void)
300
return(start_driver());
303
void CdenableSysStopRemove(void)
309
DWORD CdenableSysGetVersion( void )
314
DWORD out_buffer[10];
318
hDevice = CreateFile (sCompleteName,
319
GENERIC_READ | GENERIC_WRITE,
323
FILE_ATTRIBUTE_NORMAL,
326
if (hDevice == ((HANDLE)-1)) {
329
if ( DeviceIoControl( hDevice,
330
IOCTL_CDENABLE_GET_VERSION,
332
(LPVOID)out_buffer, 4,
337
CloseHandle (hDevice);