summaryrefslogtreecommitdiff
path: root/UsbDkController/UsbDkController.cpp
blob: 0b399d0b0847b1fc980e5fdd97d8ac0651fe56f1 (plain)
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
/**********************************************************************
* Copyright (c) 2013-2014  Red Hat, Inc.
*
* Developed by Daynix Computing LTD.
*
* Authors:
*     Dmitry Fleytman <dmitry@daynix.com>
*     Pavel Gurvich <pavel@daynix.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**********************************************************************/

#include "stdafx.h"

#include "UsbDkHelper.h"
#include "UsbDkHelperHider.h"
#include "UsbDkDataHider.h"

using namespace std;

static void ShowUsage()
{
    tcout << endl;
    tcout << TEXT("    UsbDkController usage:") << endl;
    tcout << endl;
    tcout << TEXT("        UsbDkController -h         - show this help screen and exit") << endl;
    tcout << TEXT("        UsbDkController -i         - install UsbDk driver") << endl;
    tcout << TEXT("        UsbDkController -u         - uninstall UsbDk driver") << endl;
    tcout << TEXT("        UsbDkController -n         - enumerate USB devices") << endl;
    tcout << TEXT("        UsbDkController -r ID SN   - redirect device by ID and serial number.") << endl;
    tcout << TEXT("                                     ID should be formatted the same way as") << endl;
    tcout << TEXT("                                     it appears in device enumeration output") << endl;
    tcout << TEXT("                                     and with quotes, i.e. \"USB\\VID_XXXX&PID_XXXX\"") << endl;
    tcout << endl;
    tcout << TEXT("    Hider API:") << endl;
    tcout << endl;
    tcout << TEXT("        UsbDkController -H TYPE VID PID BCD Class Hide   - add dynamic hide rule") << endl;
    tcout << TEXT("        UsbDkController -P TYPE VID PID BCD Class Hide   - add persistent hide rule") << endl;
    tcout << TEXT("        UsbDkController -D TYPE VID PID BCD Class Hide   - delete persistent hide rule") << endl;
    tcout << TEXT("        UsbDkController -Z                               - delete all persistent hide rules") << endl;
    tcout << endl;
    tcout << TEXT("            <VID PID BCD Class> May be specific value or -1 to match all") << endl;
    tcout << TEXT("            <Hide>              Should be 0 or 1, if 0, the rule is terminal") << endl;
    tcout << TEXT("            <TYPE>              Should be 0 for backward-compatible hide") << endl;
    tcout << TEXT("                                          1 for hide by determinative type") << endl;
    tcout << endl;
}

static int Controller_AnalyzeInstallResult(InstallResult Res, const tstring &OpName)
{
    switch (Res)
    {
    case InstallSuccess:
        tcout << OpName << TEXT(" succeeded") << endl;
        return 0;
    case InstallFailure:
        tcout << OpName << TEXT(" failed") << endl;
        return 1;
    case InstallSuccessNeedReboot:
        tcout << OpName << TEXT(" succeeded but reboot is required in order to make it functional") << endl;
        return 2;
    case InstallAborted:
        tcout << OpName << TEXT(" aborted, couln't start the driver on the system. Please make sure you are installing a signed version of UsbDk or else try to disable \"driver signature enforcement\" on the system") << endl;
        return 4;
    default:
        tcout << OpName << TEXT(" returned unknown error code") << endl;
        assert(0);
        return 3;
    }
}

static int Controller_InstallDriver()
{
    tcout << TEXT("Installing UsbDk driver") << endl;
    auto res = UsbDk_InstallDriver();
    return Controller_AnalyzeInstallResult(res, TEXT("UsbDk driver installation"));
}

static int Controller_UninstallDriver()
{
    tcout << TEXT("Uninstalling UsbDk driver") << endl;
    if (UsbDk_UninstallDriver())
    {
        tcout << TEXT("UsbDk driver uninstall succeeded") << endl;
        return 0;
    }
    else
    {
        tcout << TEXT("UsbDk driver uninstall failed") << endl;
        return 1;
    }
}

static void Controller_DumpConfigurationDescriptors(USB_DK_DEVICE_INFO &Device)
{
    for (UCHAR i = 0; i < Device.DeviceDescriptor.bNumConfigurations; i++)
    {
        USB_DK_CONFIG_DESCRIPTOR_REQUEST Request;
        Request.ID = Device.ID;
        Request.Index = i;

        PUSB_CONFIGURATION_DESCRIPTOR Descriptor;
        ULONG Length;

        if (!UsbDk_GetConfigurationDescriptor(&Request, &Descriptor, &Length))
        {
            tcout << TEXT("Failed to read configuration descriptor #") << (int) i << endl;
        }
        else
        {
            tcout << TEXT("Descriptor for configuration #") << (int) i << TEXT(": size ") << Length << endl;
            UsbDk_ReleaseConfigurationDescriptor(Descriptor);
        }
    }
}

static int Controller_EnumerateDevices()
{
    PUSB_DK_DEVICE_INFO devicesArray;
    ULONG               numberDevices;
    tcout << TEXT("Enumerate USB devices") << endl;
    if (UsbDk_GetDevicesList(&devicesArray, &numberDevices))
    {
        tcout << TEXT("Found ") << to_tstring(numberDevices) << TEXT(" USB devices:") << endl;

        for (ULONG deviceIndex = 0; deviceIndex < numberDevices; ++deviceIndex)
        {
            auto &Dev = devicesArray[deviceIndex];

            tcout << to_tstring(deviceIndex) << TEXT(". ")
                  << TEXT("FilterID: ") << Dev.FilterID << TEXT(", ")
                  << TEXT("Port: ") << Dev.Port << TEXT(", ")
                  << TEXT("ID: ")
                    << hex
                    << setw(4) << setfill(L'0') << static_cast<int>(Dev.DeviceDescriptor.idVendor) << TEXT(":")
                    << setw(4) << setfill(L'0') << static_cast<int>(Dev.DeviceDescriptor.idProduct) << TEXT(", ")
                    << dec
                  << TEXT("Configs: ") << static_cast<int>(Dev.DeviceDescriptor.bNumConfigurations) << TEXT(", ")
                  << TEXT("Speed: ") << static_cast<int>(Dev.Speed) << endl << TEXT("\t")
                  << Dev.ID.DeviceID << TEXT(" ")
                  << Dev.ID.InstanceID
                  << endl;

            Controller_DumpConfigurationDescriptors(Dev);
        }

        UsbDk_ReleaseDevicesList(devicesArray);
        return 0;
    }
    else
    {
        tcout << TEXT("Enumeration failed") << endl;
        return 1;
    }
}

static int Controller_RedirectDevice(TCHAR *DeviceID, TCHAR *InstanceID)
{
    USB_DK_DEVICE_ID   deviceID;
    UsbDkFillIDStruct(&deviceID, tstring2wstring(DeviceID), tstring2wstring(InstanceID));

    tcout << TEXT("Redirect USB device ") << deviceID.DeviceID << TEXT(", ") << deviceID.InstanceID << endl;

    HANDLE redirectedDevice = UsbDk_StartRedirect(&deviceID);
    if (INVALID_HANDLE_VALUE == redirectedDevice)
    {
        tcout << TEXT("Redirect of USB device failed") << endl;
        return 100;
    }
    tcout << TEXT("USB device was redirected successfully. Redirected device handle = ") << redirectedDevice << endl;
    tcout << TEXT("Press any key to stop redirection");
    getchar();

    // STOP redirect
    tcout << TEXT("Restore USB device ") << redirectedDevice << endl;
    if (TRUE == UsbDk_StopRedirect(redirectedDevice))
    {
        tcout << TEXT("USB device redirection was stopped successfully.") << endl;
        return 0;
    }
    else
    {
        tcout << TEXT("Stopping of USB device redirection failed.") << endl;
        return 101;
    }

}

static bool Controller_ParseIntRuleField(const TCHAR *Name, const TCHAR * Str, ULONG64 &Value)
{
    tstringstream strm;
    strm << hex << tstring(Str);
    if (strm >> Value)
    {
        tcout << Name << TEXT(":\t")
              << hex << TEXT("0x") << left << Value
              << endl;
        return true;
    }
    else
    {
        tcout << TEXT("Invalid value for field \"") << Name << TEXT("\"") << endl;
        return true;
    }
}

static bool Controller_ParseBoolRuleField(const TCHAR *Name, const TCHAR * Str, ULONG64 &Value)
{
    if (!Controller_ParseIntRuleField(Name, Str, Value))
    {
        return false;
    }

    if (Value != !!Value)
    {
        tcout << TEXT("Invalid value for field \"") << Name
              << TEXT("\". Must be 0 or 1.") << endl;
        return false;
    }

    return true;
}

static bool Controller_ParseRule(TCHAR *Type, TCHAR *VID, TCHAR *PID, TCHAR *BCD, TCHAR *UsbClass, TCHAR *Hide, USB_DK_HIDE_RULE &Rule)
{
    return Controller_ParseIntRuleField(TEXT("VID"), VID, Rule.VID) &&
           Controller_ParseIntRuleField(TEXT("PID"), PID, Rule.PID) &&
           Controller_ParseIntRuleField(TEXT("BCD"), BCD, Rule.BCD) &&
           Controller_ParseIntRuleField(TEXT("Class"), UsbClass, Rule.Class) &&
           Controller_ParseBoolRuleField(TEXT("Hide"), Hide, Rule.Hide) &&
           Controller_ParseIntRuleField(TEXT("Type"), Type, Rule.Type);
}

static int Controller_AddPersistentHideRule(TCHAR *Type, TCHAR *VID, TCHAR *PID, TCHAR *BCD, TCHAR *UsbClass, TCHAR *Hide)
{
    USB_DK_HIDE_RULE Rule;

    if (!Controller_ParseRule(Type, VID, PID, BCD, UsbClass, Hide, Rule))
    {
        tcout << TEXT("Persistent hide rule parsing failed") << endl;
        return false;
    }

    auto res = Type == USBDK_HIDER_RULE_DEFAULT ?
        UsbDk_AddPersistentHideRule(&Rule) :
        UsbDk_AddExtendedPersistentHideRule(&Rule, (ULONG)Rule.Type);
    return Controller_AnalyzeInstallResult(res, TEXT("Persistent hide rule creation"));
}

static int Controller_DeletePersistentHideRule(TCHAR *Type, TCHAR *VID, TCHAR *PID, TCHAR *BCD, TCHAR *UsbClass, TCHAR *Hide)
{
    USB_DK_HIDE_RULE Rule;

    if (!Controller_ParseRule(Type, VID, PID, BCD, UsbClass, Hide, Rule))
    {
        tcout << TEXT("Persistent hide rule parsing failed") << endl;
        return false;
    }

    auto res = Type == USBDK_HIDER_RULE_DEFAULT ?
        UsbDk_DeletePersistentHideRule(&Rule) :
        UsbDk_DeleteExtendedPersistentHideRule(&Rule, (ULONG)Rule.Type);
    return Controller_AnalyzeInstallResult(res, TEXT("Persistent hide rule removal"));
}

static void Controller_HideDevice(TCHAR *Type, TCHAR *VID, TCHAR *PID, TCHAR *BCD, TCHAR *UsbClass, TCHAR *Hide)
{
    USB_DK_HIDE_RULE Rule;

    if (!Controller_ParseRule(Type, VID, PID, BCD, UsbClass, Hide, Rule))
    {
        tcout << TEXT("Hide rule parsing failed") << endl;
        return;
    }

    auto hiderHandle = UsbDk_CreateHiderHandle();
    if (hiderHandle == INVALID_HANDLE_VALUE)
    {
        tcout << TEXT("Failed to open the hider device") << endl;
        return;
    }

    BOOL bResult = Rule.Type == USBDK_HIDER_RULE_DEFAULT ?
        UsbDk_AddHideRule(hiderHandle, &Rule) :
        UsbDk_AddExtendedHideRule(hiderHandle, &Rule, (ULONG)Rule.Type);

    if (bResult)
    {
        tcout << endl
              << TEXT("Hide rule loaded succesfully. ")
              << TEXT("Press ENTER to un-hide and continue.")
              << endl;
        getchar();
    }
    else
    {
        tcout << TEXT("Failed to load hide rule.") << endl;
    }

    if (UsbDk_ClearHideRules(hiderHandle))
    {
        tcout << TEXT("Hide rules cleared successfully.") << endl;
    }
    else
    {
        tcout << TEXT("Failed to clear hide rules.") << endl;
    }

    UsbDk_CloseHiderHandle(hiderHandle);
}

static int Controller_DeleteAllPersistentHideRules()
{
    ULONG done = 0, notDone = 0;
    auto res = UsbDk_DeleteAllPersistentRules(&done, &notDone);
    tcout << TEXT("Cleaning persistent rules: done ") << dec << done
        << TEXT(", not done ") << notDone << endl;
    return Controller_AnalyzeInstallResult(res, TEXT("Clean persistent hide rules"));
}

static bool Controller_ChdirToPackageFolder()
{
    TCHAR PackagePath[MAX_PATH];

    auto Length = GetModuleFileName(NULL, PackagePath, ARRAY_SIZE(PackagePath));

    if ((Length == 0) || (Length == ARRAY_SIZE(PackagePath)))
    {
        tcout << TEXT("Failed to get executable path.") << endl;
        return false;
    }

    if (!PathRemoveFileSpec(PackagePath))
    {
        tcout << TEXT("Failed to get package directory.") << endl;
        return false;
    }

    if (!SetCurrentDirectory(PackagePath))
    {
        auto Error = GetLastError();
        tcout << TEXT("Failed to make package directory current, error: ") << Error << TEXT(".") << endl;
        return false;
    }

    return true;
}

int __cdecl _tmain(int argc, TCHAR* argv[])
{
    if (argc > 1)
    {
        if (!Controller_ChdirToPackageFolder())
        {
            return -1;
        }

        if (_tcscmp(L"-h", argv[1]) == 0)
        {
            ShowUsage();
            return 0;
        }
        else if (_tcscmp(L"-i", argv[1]) == 0)
        {
            return Controller_InstallDriver();
        }
        else if (_tcscmp(L"-u", argv[1]) == 0)
        {
            return Controller_UninstallDriver();
        }
        else if (_tcscmp(L"-n", argv[1]) == 0)
        {
            return Controller_EnumerateDevices();
        }
        else if (_tcscmp(L"-r", argv[1]) == 0)
        {
            if (argc < 4)
            {
                ShowUsage();
                return -2;
            }

            return Controller_RedirectDevice(argv[2], argv[3]);
        }
        else if (_tcscmp(L"-H", argv[1]) == 0)
        {
            if (argc < 8)
            {
                ShowUsage();
                return -3;
            }
            Controller_HideDevice(argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
        }
        else if (_tcscmp(L"-P", argv[1]) == 0)
        {
            if (argc < 8)
            {
                ShowUsage();
                return -4;
            }
            return Controller_AddPersistentHideRule(argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
        }
        else if (_tcscmp(L"-D", argv[1]) == 0)
        {
            if (argc < 8)
            {
                ShowUsage();
                return -5;
            }
            return Controller_DeletePersistentHideRule(argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
        }
        else if (_tcscmp(L"-Z", argv[1]) == 0)
        {
            return Controller_DeleteAllPersistentHideRules();
        }
        else
        {
            ShowUsage();
            return -3;
        }
    }
    else
    {
        ShowUsage();
        return -4;
    }
}