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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/*++
Copyright (c) Microsoft Corporation
Module Name:
EchoApp.cpp
Abstract:
An application to exercise the WDF "echo" sample driver.
Environment:
user mode only
--*/
#include <DriverSpecs.h>
_Analysis_mode_(_Analysis_code_type_user_code_)
#define INITGUID
#include <windows.h>
#include <strsafe.h>
#include <cfgmgr32.h>
#include <stdio.h>
#include <stdlib.h>
#include "public.h"
#define NUM_ASYNCH_IO 100
#define BUFFER_SIZE (40*1024)
#define READER_TYPE 1
#define WRITER_TYPE 2
#define MAX_DEVPATH_LENGTH 256
BOOLEAN G_PerformAsyncIo;
BOOLEAN G_LimitedLoops;
ULONG G_AsyncIoLoopsNum;
WCHAR G_DevicePath[MAX_DEVPATH_LENGTH];
ULONG
AsyncIo(
PVOID ThreadParameter
);
BOOLEAN
PerformWriteReadTest(
IN HANDLE hDevice,
IN ULONG TestLength
);
BOOL
GetDevicePath(
IN LPGUID InterfaceGuid,
_Out_writes_(BufLen) PWCHAR DevicePath,
_In_ size_t BufLen
);
int __cdecl
main(
_In_ int argc,
_In_reads_(argc) char* argv[]
)
{
HANDLE hDevice = INVALID_HANDLE_VALUE;
HANDLE th1 = NULL;
BOOLEAN result = TRUE;
if (argc > 1) {
if(!_strnicmp (argv[1], "-Async", 6) ) {
G_PerformAsyncIo = TRUE;
if (argc > 2) {
G_AsyncIoLoopsNum = atoi(argv[2]);
G_LimitedLoops = TRUE;
}
else {
G_LimitedLoops = FALSE;
}
} else {
printf("Usage:\n");
printf(" Echoapp.exe --- Send single write and read request synchronously\n");
printf(" Echoapp.exe -Async --- Send reads and writes asynchronously without terminating\n");
printf(" Echoapp.exe -Async <number> --- Send <number> reads and writes asynchronously\n");
printf("Exit the app anytime by pressing Ctrl-C\n");
result = FALSE;
goto exit;
}
}
if ( !GetDevicePath(
(LPGUID) &GUID_DEVINTERFACE_ECHO,
G_DevicePath,
sizeof(G_DevicePath)/sizeof(G_DevicePath[0])) )
{
result = FALSE;
goto exit;
}
printf("DevicePath: %ws\n", G_DevicePath);
hDevice = CreateFile(G_DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL );
if (hDevice == INVALID_HANDLE_VALUE) {
printf("Failed to open device. Error %d\n",GetLastError());
result = FALSE;
goto exit;
}
printf("Opened device successfully\n");
if(G_PerformAsyncIo) {
printf("Starting AsyncIo\n");
//
// Create a reader thread
//
th1 = CreateThread( NULL, // Default Security Attrib.
0, // Initial Stack Size,
(LPTHREAD_START_ROUTINE) AsyncIo, // Thread Func
(LPVOID)READER_TYPE,
0, // Creation Flags
NULL ); // Don't need the Thread Id.
if (th1 == NULL) {
printf("Couldn't create reader thread - error %d\n", GetLastError());
result = FALSE;
goto exit;
}
//
// Use this thread for peforming write.
//
result = (BOOLEAN)AsyncIo((PVOID)WRITER_TYPE);
}else {
//
// Write pattern buffers and read them back, then verify them
//
result = PerformWriteReadTest(hDevice, 512);
if(!result) {
goto exit;
}
result = PerformWriteReadTest(hDevice, 30*1024);
if(!result) {
goto exit;
}
}
exit:
if (th1 != NULL) {
WaitForSingleObject(th1, INFINITE);
CloseHandle(th1);
}
if (hDevice != INVALID_HANDLE_VALUE) {
CloseHandle(hDevice);
}
return ((result == TRUE) ? 0 : 1);
}
PUCHAR
CreatePatternBuffer(
IN ULONG Length
)
{
unsigned int i;
PUCHAR p, pBuf;
pBuf = (PUCHAR)malloc(Length);
if( pBuf == NULL ) {
printf("Could not allocate %d byte buffer\n",Length);
return NULL;
}
p = pBuf;
for(i=0; i < Length; i++ ) {
*p = (UCHAR)i;
p++;
}
return pBuf;
}
BOOLEAN
VerifyPatternBuffer(
_In_reads_bytes_(Length) PUCHAR pBuffer,
_In_ ULONG Length
)
{
unsigned int i;
PUCHAR p = pBuffer;
for( i=0; i < Length; i++ ) {
if( *p != (UCHAR)(i & 0xFF) ) {
printf("Pattern changed. SB 0x%x, Is 0x%x\n",
(UCHAR)(i & 0xFF), *p);
return FALSE;
}
p++;
}
return TRUE;
}
BOOLEAN
PerformWriteReadTest(
IN HANDLE hDevice,
IN ULONG TestLength
)
/*
*/
{
ULONG bytesReturned =0;
PUCHAR WriteBuffer = NULL,
ReadBuffer = NULL;
BOOLEAN result = TRUE;
WriteBuffer = CreatePatternBuffer(TestLength);
if( WriteBuffer == NULL ) {
result = FALSE;
goto Cleanup;
}
ReadBuffer = (PUCHAR)malloc(TestLength);
if( ReadBuffer == NULL ) {
printf("PerformWriteReadTest: Could not allocate %d "
"bytes ReadBuffer\n",TestLength);
result = FALSE;
goto Cleanup;
}
//
// Write the pattern to the device
//
bytesReturned = 0;
if (!WriteFile ( hDevice,
WriteBuffer,
TestLength,
&bytesReturned,
NULL)) {
printf ("PerformWriteReadTest: WriteFile failed: "
"Error %d\n", GetLastError());
result = FALSE;
goto Cleanup;
} else {
if( bytesReturned != TestLength ) {
printf("bytes written is not test length! Written %d, "
"SB %d\n",bytesReturned, TestLength);
result = FALSE;
goto Cleanup;
}
printf ("%d Pattern Bytes Written successfully\n",
bytesReturned);
}
bytesReturned = 0;
if ( !ReadFile (hDevice,
ReadBuffer,
TestLength,
&bytesReturned,
NULL)) {
printf ("PerformWriteReadTest: ReadFile failed: "
"Error %d\n", GetLastError());
result = FALSE;
goto Cleanup;
} else {
if( bytesReturned != TestLength ) {
printf("bytes Read is not test length! Read %d, "
"SB %d\n",bytesReturned, TestLength);
//
// Note: Is this a Failure Case??
//
result = FALSE;
goto Cleanup;
}
printf ("%d Pattern Bytes Read successfully\n",bytesReturned);
}
//
// Now compare
//
if( !VerifyPatternBuffer(ReadBuffer, TestLength) ) {
printf("Verify failed\n");
result = FALSE;
goto Cleanup;
}
printf("Pattern Verified successfully\n");
Cleanup:
//
// Free WriteBuffer if non NULL.
//
if (WriteBuffer) {
free (WriteBuffer);
}
//
// Free ReadBuffer if non NULL
//
if (ReadBuffer) {
free (ReadBuffer);
}
return result;
}
ULONG
AsyncIo(
PVOID ThreadParameter
)
{
HANDLE hDevice = INVALID_HANDLE_VALUE;
HANDLE hCompletionPort = NULL;
OVERLAPPED *pOvList = NULL;
PUCHAR buf = NULL;
ULONG numberOfBytesTransferred;
OVERLAPPED *completedOv;
ULONG_PTR i;
ULONG ioType = (ULONG)(ULONG_PTR)ThreadParameter;
ULONG_PTR key;
ULONG error;
BOOLEAN result = TRUE;
ULONG maxPendingRequests = NUM_ASYNCH_IO;
ULONG remainingRequestsToSend = 0;
ULONG remainingRequestsToReceive = 0;
hDevice = CreateFile(G_DevicePath,
GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL );
if (hDevice == INVALID_HANDLE_VALUE) {
printf("Cannot open %ws error %d\n", G_DevicePath, GetLastError());
result = FALSE;
goto Error;
}
hCompletionPort = CreateIoCompletionPort(hDevice, NULL, 1, 0);
if (hCompletionPort == NULL) {
printf("Cannot open completion port %d \n",GetLastError());
result = FALSE;
goto Error;
}
//
// We will only have NUM_ASYNCH_IO or G_AsyncIoLoopsNum pending at any
// time (whichever is less)
//
if (G_LimitedLoops == TRUE) {
remainingRequestsToReceive = G_AsyncIoLoopsNum;
if (G_AsyncIoLoopsNum > NUM_ASYNCH_IO) {
//
// After we send the initial NUM_ASYNCH_IO, we will have additional
// (G_AsyncIoLoopsNum - NUM_ASYNCH_IO) I/Os to send
//
maxPendingRequests = NUM_ASYNCH_IO;
remainingRequestsToSend = G_AsyncIoLoopsNum - NUM_ASYNCH_IO;
}
else {
maxPendingRequests = G_AsyncIoLoopsNum;
remainingRequestsToSend = 0;
}
}
pOvList = (OVERLAPPED *)malloc(maxPendingRequests * sizeof(OVERLAPPED));
if (pOvList == NULL) {
printf("Cannot allocate overlapped array \n");
result = FALSE;
goto Error;
}
buf = (PUCHAR)malloc(maxPendingRequests * BUFFER_SIZE);
if (buf == NULL) {
printf("Cannot allocate buffer \n");
result = FALSE;
goto Error;
}
ZeroMemory(pOvList, maxPendingRequests * sizeof(OVERLAPPED));
ZeroMemory(buf, maxPendingRequests * BUFFER_SIZE);
//
// Issue asynch I/O
//
for (i = 0; i < maxPendingRequests; i++) {
if (ioType == READER_TYPE) {
if ( ReadFile( hDevice,
buf + (i* BUFFER_SIZE),
BUFFER_SIZE,
NULL,
&pOvList[i]) == 0) {
error = GetLastError();
if (error != ERROR_IO_PENDING) {
printf(" %dth Read failed %d \n", (ULONG) i, GetLastError());
result = FALSE;
goto Error;
}
}
} else {
if ( WriteFile( hDevice,
buf + (i* BUFFER_SIZE),
BUFFER_SIZE,
NULL,
&pOvList[i]) == 0) {
error = GetLastError();
if (error != ERROR_IO_PENDING) {
printf(" %dth Write failed %d \n", (ULONG) i, GetLastError());
result = FALSE;
goto Error;
}
}
}
}
//
// Wait for the I/Os to complete. If one completes then reissue the I/O
//
WHILE (1) {
if ( GetQueuedCompletionStatus(hCompletionPort, &numberOfBytesTransferred, &key, &completedOv, INFINITE) == 0) {
printf("GetQueuedCompletionStatus failed %d\n", GetLastError());
result = FALSE;
goto Error;
}
//
// Read successfully completed. If we're doing unlimited I/Os then Issue another one.
//
if (ioType == READER_TYPE) {
i = completedOv - pOvList;
printf("Number of bytes read by request number %Id is %d\n", i, numberOfBytesTransferred);
//
// If we're done with the I/Os, then exit
//
if (G_LimitedLoops == TRUE) {
if ((--remainingRequestsToReceive) == 0) {
break;
}
if (remainingRequestsToSend == 0) {
continue;
}
else {
remainingRequestsToSend--;
}
}
if ( ReadFile( hDevice,
buf + (i * BUFFER_SIZE),
BUFFER_SIZE,
NULL,
completedOv) == 0) {
error = GetLastError();
if (error != ERROR_IO_PENDING) {
printf("%Idth Read failed %d \n", i, GetLastError());
result = FALSE;
goto Error;
}
}
} else {
i = completedOv - pOvList;
printf("Number of bytes written by request number %Id is %d\n", i, numberOfBytesTransferred);
//
// If we're done with the I/Os, then exit
//
if (G_LimitedLoops == TRUE) {
if ((--remainingRequestsToReceive) == 0) {
break;
}
if (remainingRequestsToSend == 0) {
continue;
}
else {
remainingRequestsToSend--;
}
}
if ( WriteFile( hDevice,
buf + (i * BUFFER_SIZE),
BUFFER_SIZE,
NULL,
completedOv) == 0) {
error = GetLastError();
if (error != ERROR_IO_PENDING) {
printf("%Idth write failed %d \n", i, GetLastError());
result = FALSE;
goto Error;
}
}
}
}
Error:
if(hDevice != INVALID_HANDLE_VALUE) {
CloseHandle(hDevice);
}
if(hCompletionPort) {
CloseHandle(hCompletionPort);
}
if(buf) {
free(buf);
}
if(pOvList) {
free(pOvList);
}
return (ULONG)result;
}
BOOL
GetDevicePath(
_In_ LPGUID InterfaceGuid,
_Out_writes_(BufLen) PWCHAR DevicePath,
_In_ size_t BufLen
)
{
CONFIGRET cr = CR_SUCCESS;
PWSTR deviceInterfaceList = NULL;
ULONG deviceInterfaceListLength = 0;
PWSTR nextInterface;
HRESULT hr = E_FAIL;
BOOL bRet = TRUE;
cr = CM_Get_Device_Interface_List_Size(
&deviceInterfaceListLength,
InterfaceGuid,
NULL,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
if (cr != CR_SUCCESS) {
printf("Error 0x%x retrieving device interface list size.\n", cr);
goto clean0;
}
if (deviceInterfaceListLength <= 1) {
bRet = FALSE;
printf("Error: No active device interfaces found.\n"
" Is the sample driver loaded?");
goto clean0;
}
deviceInterfaceList = (PWSTR)malloc(deviceInterfaceListLength * sizeof(WCHAR));
if (deviceInterfaceList == NULL) {
printf("Error allocating memory for device interface list.\n");
goto clean0;
}
ZeroMemory(deviceInterfaceList, deviceInterfaceListLength * sizeof(WCHAR));
cr = CM_Get_Device_Interface_List(
InterfaceGuid,
NULL,
deviceInterfaceList,
deviceInterfaceListLength,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
if (cr != CR_SUCCESS) {
printf("Error 0x%x retrieving device interface list.\n", cr);
goto clean0;
}
nextInterface = deviceInterfaceList + wcslen(deviceInterfaceList) + 1;
if (*nextInterface != UNICODE_NULL) {
printf("Warning: More than one device interface instance found. \n"
"Selecting first matching device.\n\n");
}
hr = StringCchCopy(DevicePath, BufLen, deviceInterfaceList);
if (FAILED(hr)) {
bRet = FALSE;
printf("Error: StringCchCopy failed with HRESULT 0x%x", hr);
goto clean0;
}
clean0:
if (deviceInterfaceList != NULL) {
free(deviceInterfaceList);
}
if (CR_SUCCESS != cr) {
bRet = FALSE;
}
return bRet;
}
|