1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
testapp.c
Abstract:
Purpose of this app to test the NONPNP sample driver. The app
makes four different ioctl calls to test all the buffer types, write
some random buffer content to a file created by the driver in \SystemRoot\Temp
directory, and reads the same file and matches the content.
If -l option is specified, it does the write and read operation in a loop
until the app is terminated by pressing ^C.
Make sure you have the \SystemRoot\Temp directory exists before you run the test.
Environment:
Win32 console application.
--*/
#include <DriverSpecs.h>
_Analysis_mode_(_Analysis_code_type_user_code_)
#include <windows.h>
#pragma warning(disable:4201) // nameless struct/union
#include <winioctl.h>
#pragma warning(default:4201)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <strsafe.h>
#include "public.h"
BOOLEAN
ManageDriver(
IN LPCTSTR DriverName,
IN LPCTSTR ServiceName,
IN USHORT Function
);
BOOLEAN
SetupDriverName(
_Inout_updates_all_(BufferLength) PCHAR DriverLocation,
_In_ ULONG BufferLength
);
BOOLEAN
DoFileReadWrite(
HANDLE HDevice
);
VOID
DoIoctls(
HANDLE hDevice
);
BOOLEAN G_fLoop = FALSE;
//-----------------------------------------------------------------------------
// 4127 -- Conditional Expression is Constant warning
//-----------------------------------------------------------------------------
#define WHILE(constant) \
__pragma(warning(disable: 4127)) while(constant); __pragma(warning(default: 4127))
#define USAGE \
"Usage: nonpnpapp <-l> \n" \
" -l { option to continuously read & write to the file} \n"
LONG
Parse(
_In_ int argc,
_In_reads_(argc) char *argv[]
)
/*++
Routine Description:
Called by main() to parse command line parms
Arguments:
argc and argv that was passed to main()
Return Value:
Sets global flags as per user function request
--*/
{
int i;
LONG error = ERROR_SUCCESS;
for (i=0; i<argc; i++) {
if (argv[i][0] == '-' ||
argv[i][0] == '/') {
switch(argv[i][1]) {
case 'l':
case 'L':
G_fLoop = TRUE;
break;
default:
printf(USAGE);
error = ERROR_INVALID_PARAMETER;
}
}
}
return error;
}
VOID __cdecl
main(
_In_ ULONG argc,
_In_reads_(argc) PCHAR argv[]
)
{
HANDLE hDevice;
DWORD errNum = 0;
CHAR driverLocation [MAX_PATH];
BOOL ok;
LONG error;
//
// Parse command line args
// -l -- loop option
//
if ( argc > 1 ) {// give usage if invoked with no parms
error = Parse(argc, argv);
if (error != ERROR_SUCCESS) {
return;
}
}
//
// open the device
//
hDevice = CreateFile(DEVICE_NAME,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hDevice == INVALID_HANDLE_VALUE) {
errNum = GetLastError();
if (!(errNum == ERROR_FILE_NOT_FOUND ||
errNum == ERROR_PATH_NOT_FOUND)) {
printf("CreateFile failed! ERROR_FILE_NOT_FOUND = %d\n",
errNum);
return ;
}
//
// The driver is not started yet so let us install the driver.
// First setup full path to driver name.
//
ok = SetupDriverName( driverLocation, MAX_PATH );
if (!ok) {
return ;
}
ok = ManageDriver( DRIVER_NAME,
driverLocation,
DRIVER_FUNC_INSTALL );
if (!ok) {
printf("Unable to install driver. \n");
//
// Error - remove driver.
//
ManageDriver( DRIVER_NAME,
driverLocation,
DRIVER_FUNC_REMOVE );
return;
}
hDevice = CreateFile( DEVICE_NAME,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (hDevice == INVALID_HANDLE_VALUE) {
printf ( "Error: CreatFile Failed : %d\n", GetLastError());
return;
}
}
DoIoctls(hDevice);
do {
if(!DoFileReadWrite(hDevice)) {
break;
}
if(!G_fLoop) {
break;
}
Sleep(1000); // sleep for 1 sec.
} WHILE (TRUE);
//
// Close the handle to the device before unloading the driver.
//
CloseHandle ( hDevice );
//
// Unload the driver. Ignore any errors.
//
ManageDriver( DRIVER_NAME,
driverLocation,
DRIVER_FUNC_REMOVE );
return;
}
VOID
DoIoctls(
HANDLE hDevice
)
{
char OutputBuffer[100];
char InputBuffer[200];
BOOL bRc;
ULONG bytesReturned;
//
// Printing Input & Output buffer pointers and size
//
printf("InputBuffer Pointer = %p, BufLength = %Id\n", InputBuffer,
sizeof(InputBuffer));
printf("OutputBuffer Pointer = %p BufLength = %Id\n", OutputBuffer,
sizeof(OutputBuffer));
//
// Performing METHOD_BUFFERED
//
if(FAILED(StringCchCopy(InputBuffer, sizeof(InputBuffer),
"this String is from User Application; using METHOD_BUFFERED"))){
return;
}
printf("\nCalling DeviceIoControl METHOD_BUFFERED:\n");
memset(OutputBuffer, 0, sizeof(OutputBuffer));
bRc = DeviceIoControl ( hDevice,
(DWORD) IOCTL_NONPNP_METHOD_BUFFERED,
InputBuffer,
(DWORD) strlen( InputBuffer )+1,
OutputBuffer,
sizeof( OutputBuffer),
&bytesReturned,
NULL
);
if ( !bRc )
{
printf ( "Error in DeviceIoControl : %d", GetLastError());
return;
}
printf(" OutBuffer (%d): %s\n", bytesReturned, OutputBuffer);
//
// Performing METHOD_NEITHER
//
printf("\nCalling DeviceIoControl METHOD_NEITHER\n");
if(FAILED(StringCchCopy(InputBuffer, sizeof(InputBuffer),
"this String is from User Application; using METHOD_NEITHER"))) {
return;
}
memset(OutputBuffer, 0, sizeof(OutputBuffer));
bRc = DeviceIoControl ( hDevice,
(DWORD) IOCTL_NONPNP_METHOD_NEITHER,
InputBuffer,
(DWORD) strlen( InputBuffer )+1,
OutputBuffer,
sizeof( OutputBuffer),
&bytesReturned,
NULL
);
if ( !bRc )
{
printf ( "Error in DeviceIoControl : %d\n", GetLastError());
return;
}
printf(" OutBuffer (%d): %s\n", bytesReturned, OutputBuffer);
//
// Performing METHOD_IN_DIRECT
//
printf("\nCalling DeviceIoControl METHOD_IN_DIRECT\n");
if(FAILED(StringCchCopy(InputBuffer, sizeof(InputBuffer),
"this String is from User Application; using METHOD_IN_DIRECT"))) {
return;
}
if(FAILED(StringCchCopy(OutputBuffer, sizeof(OutputBuffer),
"This String is from User Application in OutBuffer; using METHOD_IN_DIRECT"))) {
return;
}
bRc = DeviceIoControl ( hDevice,
(DWORD) IOCTL_NONPNP_METHOD_IN_DIRECT,
InputBuffer,
(DWORD) strlen( InputBuffer )+1,
OutputBuffer,
sizeof( OutputBuffer),
&bytesReturned,
NULL
);
if ( !bRc )
{
printf ( "Error in DeviceIoControl : : %d", GetLastError());
return;
}
printf(" Number of bytes transfered from OutBuffer: %d\n",
bytesReturned);
//
// Performing METHOD_OUT_DIRECT
//
printf("\nCalling DeviceIoControl METHOD_OUT_DIRECT\n");
if(FAILED(StringCchCopy(InputBuffer, sizeof(InputBuffer),
"this String is from User Application; using METHOD_OUT_DIRECT"))){
return;
}
memset(OutputBuffer, 0, sizeof(OutputBuffer));
bRc = DeviceIoControl ( hDevice,
(DWORD) IOCTL_NONPNP_METHOD_OUT_DIRECT,
InputBuffer,
(DWORD) strlen( InputBuffer )+1,
OutputBuffer,
sizeof( OutputBuffer),
&bytesReturned,
NULL
);
if ( !bRc )
{
printf ( "Error in DeviceIoControl : : %d", GetLastError());
return;
}
printf(" OutBuffer (%d): %s\n", bytesReturned, OutputBuffer);
return;
}
BOOLEAN
DoFileReadWrite(
HANDLE HDevice
)
{
ULONG bufLength, index;
PUCHAR readBuf = NULL;
PUCHAR writeBuf = NULL;
BOOLEAN ret;
ULONG bytesWritten, bytesRead;
//
// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
//
srand( (unsigned)time( NULL ) );
//
// rand function returns a pseudorandom integer in the range 0 to RAND_MAX
// (0x7fff)
//
bufLength = rand();
//
// Try until the bufLength is not zero.
//
while(bufLength == 0) {
bufLength = rand();
}
//
// Allocate a buffer of that size to use for write operation.
//
writeBuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufLength);
if(!writeBuf) {
ret = FALSE;
goto End;
}
//
// Fill the buffer with randon number less than UCHAR_MAX.
//
index = bufLength;
while(index){
writeBuf[index-1] = (UCHAR) rand() % UCHAR_MAX;
index--;
}
printf("Write %d bytes to file\n", bufLength);
//
// Tell the driver to write the buffer content to the file from the
// begining of the file.
//
if (!WriteFile(HDevice,
writeBuf,
bufLength,
&bytesWritten,
NULL)) {
printf("ReadFile failed with error 0x%x\n", GetLastError());
ret = FALSE;
goto End;
}
//
// Allocate another buffer of same size.
//
readBuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufLength);
if(!readBuf) {
ret = FALSE;
goto End;
}
printf("Read %d bytes from the same file\n", bufLength);
//
// Tell the driver to read the file from the begining.
//
if (!ReadFile(HDevice,
readBuf,
bufLength,
&bytesRead,
NULL)) {
printf("Error: ReadFile failed with error 0x%x\n", GetLastError());
ret = FALSE;
goto End;
}
//
// Now compare the readBuf and writeBuf content. They should be the same.
//
if(bytesRead != bytesWritten) {
printf("bytesRead(%d) != bytesWritten(%d)\n", bytesRead, bytesWritten);
ret = FALSE;
goto End;
}
if(memcmp(readBuf, writeBuf, bufLength) != 0){
printf("Error: ReadBuf and WriteBuf contents are not the same\n");
ret = FALSE;
goto End;
}
ret = TRUE;
End:
if(readBuf){
HeapFree (GetProcessHeap(), 0, readBuf);
}
if(writeBuf){
HeapFree (GetProcessHeap(), 0, writeBuf);
}
return ret;
}
|