├── .appveyor.yml ├── .editorconfig ├── .gitignore ├── ARCHITECTURE ├── Documentation ├── CrashDumps.txt ├── DriverSigning.txt ├── Hider.txt ├── LICENSE ├── Tracing.txt ├── Uninstallation.txt ├── UsbInfo.txt └── WdfLogDumping.txt ├── LICENSE ├── MAINTAINERS ├── README.md ├── Tools ├── AppVeyor │ └── before-build.cmd ├── Driver.Initial.props ├── Installer │ ├── UsbDkFiles.wxi │ ├── UsbDkInstaller.wxs │ └── buildmsi.bat ├── LICENSE ├── RegSettingsForWdfLog.reg ├── Trace │ ├── LICENSE │ ├── UsbDk-Autologger-Off.reg │ ├── UsbDk-Autologger-On.reg │ ├── UsbDk.ctl │ ├── UsbDkLogman.bat │ ├── UsbDkTextLog.bat │ ├── UsbDkTrace.bat │ └── UsbDkTraceToFile.bat ├── versions.h ├── vs_cmdline.vbs └── vs_run.bat ├── UsbDk Package ├── LICENSE ├── UsbDk Package.vcxproj └── UsbDk Package.vcxproj.filters ├── UsbDk.sln ├── UsbDk ├── Alloc.h ├── ControlDevice.cpp ├── ControlDevice.h ├── DeviceAccess.cpp ├── DeviceAccess.h ├── Driver.cpp ├── Driver.h ├── FilterDevice.cpp ├── FilterDevice.h ├── FilterStrategy.cpp ├── FilterStrategy.h ├── HideRulesRegPublic.h ├── HiderDevice.cpp ├── HiderDevice.h ├── HiderStrategy.cpp ├── HiderStrategy.h ├── Irp.cpp ├── Irp.h ├── LICENSE ├── MemoryBuffer.cpp ├── MemoryBuffer.h ├── Public.h ├── RedirectorStrategy.cpp ├── RedirectorStrategy.h ├── RegText.cpp ├── RegText.h ├── Registry.cpp ├── Registry.h ├── Resource.rc ├── Trace.h ├── Urb.cpp ├── Urb.h ├── UsbDk.vcxproj ├── UsbDk.vcxproj.filters ├── UsbDk.vcxproj.user ├── UsbDkCompat.cpp ├── UsbDkCompat.h ├── UsbDkData.h ├── UsbDkDataHider.h ├── UsbDkDataHiderPublic.h ├── UsbDkNames.h ├── UsbDkUtil.cpp ├── UsbDkUtil.h ├── UsbTarget.cpp ├── UsbTarget.h ├── WdfDevice.cpp ├── WdfDevice.h ├── WdfRequest.cpp ├── WdfRequest.h ├── WdfWorkitem.cpp ├── WdfWorkitem.h ├── stdafx.cpp └── stdafx.h ├── UsbDkController ├── LICENSE ├── Resource.rc ├── UsbDkController.cpp ├── UsbDkController.vcxproj ├── UsbDkController.vcxproj.filters ├── stdafx.cpp ├── stdafx.h └── targetver.h ├── UsbDkHelper ├── DeviceMgr.cpp ├── DeviceMgr.h ├── DriverAccess.cpp ├── DriverAccess.h ├── DriverFile.cpp ├── DriverFile.h ├── Exception.cpp ├── Exception.h ├── GuidGen.cpp ├── GuidGen.h ├── Installer.cpp ├── Installer.h ├── LICENSE ├── RedirectorAccess.cpp ├── RedirectorAccess.h ├── RegAccess.cpp ├── RegAccess.h ├── Resource.rc ├── RuleManager.cpp ├── RuleManager.h ├── ServiceManager.cpp ├── ServiceManager.h ├── UsbDk.inf ├── UsbDkCompat.h ├── UsbDkHelper.cpp ├── UsbDkHelper.h ├── UsbDkHelper.vcxproj ├── UsbDkHelper.vcxproj.filters ├── UsbDkHelperHider.h ├── UsbDkHlprCompat.h ├── UsbDk_XP.inf ├── Utils.cpp ├── Utils.h ├── WdfCoinstaller.cpp ├── WdfCoinstaller.h ├── dllmain.cpp ├── stdafx.cpp ├── stdafx.h ├── targetver.h ├── tstrings.cpp └── tstrings.h ├── UsbDkInstHelper ├── LICENSE ├── Resource.rc ├── UsbDkInstHelper.cpp ├── UsbDkInstHelper.vcxproj ├── UsbDkInstHelper.vcxproj.filters ├── stdafx.cpp ├── stdafx.h └── targetver.h └── buildAll.bat /.appveyor.yml: -------------------------------------------------------------------------------- 1 | image: Visual Studio 2015 2 | 3 | version: build-{build}-{branch} 4 | 5 | before_build: 6 | - tools\AppVeyor\before-build.cmd 7 | 8 | build_script: buildAll.bat 9 | 10 | skip_commits: 11 | message: /\[ci skip\]/ 12 | 13 | notifications: 14 | - provider: Email 15 | to: devel@daynix.com 16 | on_build_success: false 17 | on_build_failure: false 18 | on_build_status_changed: true 19 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig file, see editorconfig.org 2 | 3 | # This is top level folder, don't look up the tree 4 | root = true 5 | 6 | # Windows-style newlines, yeah! 7 | # Indentation = 4 spaces, trim trailing spaces 8 | [{*.cpp,*.h,*.c}] 9 | end_of_line = crlf 10 | insert_final_newline = true 11 | indent_style = space 12 | indent_size = 4 13 | trim_trailing_whitespace = true 14 | 15 | #[*] 16 | # defs for all other file types go here... 17 | 18 | ### 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.suo 2 | *.sdf 3 | Win*Debug 4 | Win*Debug_NoSign 5 | Win*Release 6 | *.opensdf 7 | *.ipch 8 | build*.log 9 | resource.h 10 | Resource.aps 11 | Install 12 | Install_debug 13 | XP Debug 14 | XP Debug_NoSign 15 | XP Release 16 | XPDebug 17 | XPDebug_NoSign 18 | XPRelease 19 | x64 20 | -------------------------------------------------------------------------------- /Documentation/CrashDumps.txt: -------------------------------------------------------------------------------- 1 | Crash dump types 2 | ============================================================================== 3 | 4 | Three types of crash dumps can be saved on Windows: 5 | 1) Mini dumps: These are small (64 - 256KB) files. Due to their size, a lot of 6 | them can be saved, but they are not particularly useful, especially without 7 | the executable files that were running at crash time. 8 | 2) Kernel dump: this is a dump of the region in memory which is occupied by 9 | the kernel. The size of such dump is usually few tenths of MB. These dumps are 10 | very useful, because their size is small, and they contain the more relevant 11 | parts of the memory. 12 | 3) Full dump: this is the dump of the entire RAM at the time of the crash. 13 | While it contains all possible data, its size is equal to that of the RAM, 14 | which is usually quite large. 15 | 16 | 17 | Crash dump creation 18 | ============================================================================== 19 | 20 | The crash dump (and crash handling) options on Windows are accessible at: 21 | --- 22 | Right click on "My Computer" -> "Properties" -> "Advanced system settings" 23 | -> "Startup and Recovery" -> "Settings". 24 | --- 25 | In that menu, you would probably want to: 26 | * De-select the "Automatically restart" option (so that the PC won't restart 27 | automatically after a crash). 28 | * Choose "Kernel memory dump" from the drop-down menu. 29 | * You can leave the dump file location at the default value, but may change 30 | that if needed. 31 | * You can choose whether to overwrite the last dump file on the next crash. 32 | The default behavior is to overwrite, unless it's a minidump. 33 | 34 | On the next crash (blue screen) a crash dump will be saved according to your 35 | choices. 36 | 37 | * The dump files are usually located in: 38 | * %windir%\memory.dmp 39 | * %windir%\Minidump\Minidump.dmp 40 | 41 | * The option to permanently preserve old dumps can be also set by writing a 42 | DWORD "AlwaysKeepMemoryDump" with value 1 to the registry key: 43 | --- 44 | HKLM\System\CurrentControlSet\Control\CrashControl 45 | --- 46 | 47 | 48 | Crash dumps on demand 49 | ============================================================================== 50 | 51 | Crashes can also be initiated manually, to examine a status of a driver. This 52 | is done via the registry, by setting a DWORD "CrashOnCtrlScroll" with the 53 | value of 1 to the following two keys: 54 | --- 55 | HKLM\System\CurrentControlSet\Services\i8042prt\Parameters 56 | HKLM\System\CurrentControlSet\Services\kbdhid\Parameters 57 | --- 58 | After setting these values, you can press Ctrl+ScrollLock+ScrollLock to get 59 | a MANUALLY_INITIATED_CRASH (0x000000E2). 60 | 61 | If a keyboard can not be accessed, a crash can be initiated using the 62 | Non-Maskable Interrupt (NMI) switch (or NMI signal on a VM). 63 | To configure that, write a DWORD "NMICrashDump" with the value 1 to the 64 | following registry key: 65 | --- 66 | HKLM\System\CurrentControlSet\Control\CrashControl 67 | --- 68 | 69 | * Note: On some machines automatic system recovery should be disabled, and NMI 70 | should be enabled in the BIOS for this to work. 71 | 72 | 73 | Sharing crash dumps with developers 74 | ============================================================================== 75 | 76 | * Because of the large file size, and high compressibility, please compress 77 | the dump files before sending them. 78 | * To analyze a crash that was supposedly caused by UsbDk, please provide the 79 | UsbDk binaries you were using, the program database (PDB) files, and the 80 | source code - in case of using an unofficial build. 81 | -------------------------------------------------------------------------------- /Documentation/DriverSigning.txt: -------------------------------------------------------------------------------- 1 | Windows Driver Signing 2 | ====================================== 3 | 4 | Windows device installation uses digital signatures to verify the integrity 5 | of driver packages and to verify the identity of the vendor (software publisher) 6 | who provides the driver packages. In addition, the kernel-mode code signing policy 7 | for 64-bit versions of Windows Vista and later versions of Windows specifies that 8 | a kernel-mode driver must be signed for the driver to load. 9 | 10 | Signing Modes 11 | ====================================== 12 | 13 | There are 2 Windows driver signing modes - production and test signing. 14 | 15 | Production signing process requires a Software Publisher Certificate (SPC) 16 | obtained from a third-party certificate authority (CA) that is authorized by 17 | Microsoft to issue such certificates. 18 | 19 | Test signing process uses a self-generated test signing certificate 20 | and requires test signing mode enabled on a target computer in order to 21 | load a test signed driver. 22 | 23 | UsbDk Driver Signing 24 | ====================================== 25 | 26 | All official UsbDk releases are production signed, this means that they will 27 | load successfully on any Windows PC running supported OS version without additional 28 | configuration efforts. 29 | 30 | As for custom builds of UsbDk drivers they are either unsigned (Release configurations) 31 | of self-signed (Debug configurations). 32 | 33 | Running Custom Builds of UsbDk Drivers 34 | ====================================== 35 | 36 | On 32-bit systems all custom builds of UsbDk drivers should run with no additional 37 | configuration efforts. 38 | 39 | In order to run a custom UsbDk build make sure that driver is built in Debug 40 | Configuration and test signing mode is enabled on the target system. 41 | 42 | In order to enable test signing mode run (as administrator): 43 | 44 | Bcdedit.exe -set TESTSIGNING ON 45 | 46 | and reboot the system. 47 | 48 | In order to disable test signing mode, run 49 | 50 | Bcdedit.exe -set TESTSIGNING OFF 51 | 52 | and reboot the system. 53 | 54 | More information on driver signing 55 | ====================================== 56 | 57 | 1. Driver Signing 58 | https://msdn.microsoft.com/en-us/library/windows/hardware/ff544865(v=vs.85).aspx 59 | 2. Signing Drivers during Development and Test 60 | https://msdn.microsoft.com/en-us/library/windows/hardware/ff552275(v=vs.85).aspx 61 | 3. The TESTSIGNING Boot Configuration Option 62 | https://msdn.microsoft.com/en-us/library/windows/hardware/ff553484(v=vs.85).aspx 63 | -------------------------------------------------------------------------------- /Documentation/Hider.txt: -------------------------------------------------------------------------------- 1 | Short description of Device Hider feature: 2 | The interface is described in UsbDkHelperHider.h 3 | It includes basic and extended interfaces: 4 | 5 | 1. Basic interface: 6 | UsbDk_AddPersistentHideRule 7 | UsbDk_DeletePersistentHideRule 8 | UsbDk_AddHideRule 9 | UsbDk_ClearHideRules 10 | 11 | Rule structure includes: 12 | Class 13 | BCD 14 | Vendor ID 15 | Product ID 16 | Hide specifier (0 or 1), 0 is terminal, 1 continues for next rule 17 | 18 | Class describes device class as specified in device descriptor. 19 | Checkers for these rules do not check classes of interfaces in multifunction devices, 20 | i.e. 21 | class match = (device class == rule class) or (rule class == -1) 22 | 23 | 2. Extended interface 24 | UsbDk_AddExtendedHideRule 25 | UsbDk_AddExtendedPersistentHideRule 26 | UsbDk_DeleteExtendedPersistentHideRule 27 | 28 | These API calls receive the same rule structure as basic API and 29 | an additional parameter 'Rule Type', which can be 30 | 0 (USBDK_HIDER_RULE_DEFAULT) or 1 (USBDK_HIDER_RULE_DETERMINATIVE_TYPES) 31 | 32 | Using extended API with USBDK_HIDER_RULE_DEFAULT produces exactly the 33 | same result as using basic API. 34 | 35 | Using USBDK_HIDER_RULE_DETERMINATIVE_TYPES forces different processing 36 | of hiding rules: 37 | 38 | Rule structure includes the same fields: 39 | Class (bitmask of device types to match) 40 | BCD 41 | Vendor ID 42 | Product ID 43 | Hide specifier (0 or 1), 0 is terminal, 1 continues for next rule 44 | 45 | Device type for each specific device is determined from device class as specified 46 | in device descriptor and 'class' field in all the interface descriptors of all 47 | device's configurations. 48 | 49 | Supported classes are: 50 | USB_DEVICE_CLASS_AUDIO (0x1) -> USB_DEVICE_CLASS_AUDIO 51 | 52 | USB_DEVICE_CLASS_COMMUNICATIONS(0x2) 53 | USB_DEVICE_CLASS_CDC_DATA(0xA) 54 | USB_DEVICE_CLASS_WIRELESS_CONTROLLER(0xE0) -> USB_DEVICE_CLASS_COMMUNICATIONS 55 | 56 | USB_DEVICE_CLASS_PRINTER(0x7) -> USB_DEVICE_CLASS_PRINTER 57 | USB_DEVICE_CLASS_STORAGE(0x8) -> USB_DEVICE_CLASS_STORAGE 58 | USB_DEVICE_CLASS_VIDEO(0xE) -> USB_DEVICE_CLASS_VIDEO 59 | USB_DEVICE_CLASS_AUDIO_VIDEO(0x10) -> USB_DEVICE_CLASS_AUDIO, USB_DEVICE_CLASS_VIDEO 60 | USB_DEVICE_CLASS_HUMAN_INTERFACE(0x3) -> USB_DEVICE_CLASS_HUMAN_INTERFACE 61 | 62 | All other classes -> OTHER (31) 63 | 64 | Following classes are determinative (if the device belongs to one 65 | of determinative types, all device types except of 66 | determinative one are removed from device type bitmask): 67 | The order of determinative types is: 68 | USB_DEVICE_CLASS_PRINTER 69 | USB_DEVICE_CLASS_COMMUNICATIONS 70 | USB_DEVICE_CLASS_AUDIO 71 | USB_DEVICE_CLASS_VIDEO 72 | 73 | Checkers for these rules check resulting set of device type and 74 | device types of interfaces in multifunction devices, i.e. 75 | Class match = (device type bitmask) & (rule class bitmask) != 0 76 | 77 | Example of usage: 78 | Rule.BCD = Rule.VID = Rule.PID = -1; 79 | Rule.Hide = 1; 80 | Rule.Class = (1 << USB_DEVICE_CLASS_STORAGE) | (1 << USB_DEVICE_CLASS_PRINTER); 81 | UsbDk_AddExtendedPersistentHideRule(&Rule, USBDK_HIDER_RULE_DETERMINATIVE_TYPES); 82 | This call will Hide all the device that determined as being mass storage 83 | devices or printer devices. 84 | -------------------------------------------------------------------------------- /Documentation/Uninstallation.txt: -------------------------------------------------------------------------------- 1 | UsbDk Uninstallation: 2 | 3 | 1.UsbDk can be easily uninstalled from programs and features as follows: 4 | 1.1. Go to control panel 5 | 1.2. Go to Programs 6 | 1.3. Open Programs and features 7 | 1.4. Select UsbDk Runtime Libraries 8 | 1.5. Click on the Uninstall button 9 | 1.6. A window should prompt stating "are you sure you want to uninstall this UsbDk Runtime Libraries", select yes 10 | 11 | 2.Another way to Uninstall UsbDk is using UsbDkController as follows: 12 | 2.1. Locate UsbDkController.exe, it should reside in "c:\Program Files\UsbDk Runtime Library\". 13 | 2.2. Run UsbDkController.exe with -u flag. 14 | 15 | UsbDk uninstallation when keyboard and mouse are stuck: 16 | 17 | UsbDk driver can cause the mouse and keyboard to get stuck, this occurs due to installing an unsigned UsbDk 18 | version and can be resolved by booting Windows in "disable driver signature enforcement" mode from the 19 | "Advanced Boot Options" menu and then uninstalling UsbDk as documented above. 20 | 21 | How to get into "Advanced Boot Options" menu: 22 | 23 | There are multiple ways for different Windows OSes: 24 | 25 | Windows 8, 8.1 & 10: 26 | 1. Boot to "Advanced Startup Options" Using Command Prompt 27 | 1.1. Open command Prompt as an Administrator 28 | 1.2. Run the following command: shutdown /r /t 0 /o 29 | 30 | Windows 7: 31 | 2. Boot to "Advanced Startup Options" During boot: 32 | 2.1. Reboot the PC 33 | 2.2. During the early boot process start pressing F8 34 | 35 | All Oses: 36 | 3. Boot to "Advanced Startup Options" from a Hard Reboot (useful if both mouse and keyboard are stuck): 37 | 3.1. Press and hold the power button until it powers off 38 | 3.2. Press the power button to turn it on 39 | 3.3 Repeat the steps above until the "Windows logo screen" or "Please wait" during boot show on. Note that this it may take up to 3 attempts. 40 | 3.4 When displayed, click on "See advanced repair options". 41 | -------------------------------------------------------------------------------- /Documentation/UsbInfo.txt: -------------------------------------------------------------------------------- 1 | Collecting Full USB Stack Information 2 | ============================================================================== 3 | 4 | 1. Dumping USB tree 5 | 6 | - Obtain the latest USBView tool from Microsoft: 7 | 8 | https://msdn.microsoft.com/en-us/library/windows/hardware/ff560019%28v=vs.85%29.aspx 9 | 10 | - Run it as following: 11 | 12 | usbview /q /f /saveall:MyUsbTree.txt 13 | usbview /q /f /savexml:MyUsbTree.xml 14 | 15 | - USB tree dump will be stored in MyUsbTree.txt and MyUsbTree.xml files. 16 | -------------------------------------------------------------------------------- /Documentation/WdfLogDumping.txt: -------------------------------------------------------------------------------- 1 | 1) Download and install the latest version of UsbDk, if you haven't done so 2 | already. It is available from: 3 | 4 | http://www.spice-space.org/download.html 5 | 6 | 2) Download WDK suitable for your system: 7 | 8 | Windows 7 and newer versions: 9 | 10 | https://msdn.microsoft.com/en-us/windows/hardware/hh852365.aspx 11 | 12 | Look for the link named "Download WDK 8.1 Update", or whatever the latest 13 | version is. It is advised to use the latest version always. 14 | 15 | Windows XP/Server 2003: 16 | 17 | https://www.microsoft.com/en-us/download/details.aspx?id=11800 18 | 19 | Download Windows Driver Kit Version 7.1.0, newer versions will not work. 20 | 21 | 3) Enable local kernel-mode debugging: 22 | 23 | * On Windows Vista/Server 2008 and later: 24 | 25 | a) Open a Command Prompt window as the Administrator. 26 | b) Run "bcdedit /debug on". 27 | c) On Windows 8 and later, run "bcdedit /dbgsettings local". 28 | d) Reboot the computer. 29 | 30 | * On Windows 2000/XP/Server 2003: 31 | 32 | a) Open the Command Prompt. 33 | b) Run "bootcfg", and note the ID of the targeted operating system. 34 | c) Run "bootcfg /debug ON /ID X" (REPLACE X WITH ID NOTED BEFORE). 35 | d) Reboot the computer. 36 | 37 | 4) Set logging parameters for UsbDk: run the "Tools/RegSettingsForWdfLog.reg" 38 | file from UsbDk source tree on the intended machine. 39 | 40 | 5) Reboot the machine. 41 | 42 | 6) Start the WinDbg debugger, that was installed as a part of WDK: 43 | 44 | a) Open a Command Prompt window as the Administrator. 45 | b) Change directory to where WinDbg is installed. FOR EXAMPLE: 46 | 'cd "C:\Program Files (x86)\Windows Kits\8.1\Debuggers\x64"' 47 | (or 'cd C:\WinDDK\7600.16385.1\Debuggers' on Windows XP) 48 | c) Run WinDbg connected to the local machine: "windbg -kl" 49 | d) Load the WDF debugger extension: 50 | "!load wdfkd.dll" 51 | e) Load the system symbols: 52 | ".symfix+" 53 | ".reload" 54 | f) Specify the WDF .tmf file. FOR EXAMPLE: 55 | Windows 7 and later: 56 | "!wdftmffile C:\temp\Wudf01011.tmf" 57 | Windows XP: 58 | "!wdftmffile C:\WinDDK\7600.16385.1\Tools\tracing\i386\Wudf01009.tmf" 59 | 60 | * The default path to this file on later versions than XP/2003 looks like: 61 | For 64-bit systems: 62 | "C:\Program Files (x86)\Windows Kits\8.1\Tools\x64\Tracing", 63 | For 32-bit systems: 64 | "C:\Program Files (x86)\Windows Kits\8.1\Tools\x86\Tracing", 65 | but it is necessary to copy it to some folder with no spaces in its 66 | name, such as "C:\temp\", in the example above. 67 | 68 | * Choose file for correct WDF version: 69 | WDF 1.11 (Wudf01011.tmf) for Windows 7/Server 2008 R2 70 | WDF 1.09 (Wudf01009.tmf) fir Windows XP/Server 2003/Server 2003 R2 71 | 72 | 7) Dump the log with "!wdflogdump .sys". FOR EXAMPLE: 73 | "!wdflogdump usbdk.sys" 74 | -------------------------------------------------------------------------------- /MAINTAINERS: -------------------------------------------------------------------------------- 1 | Maintained by Daynix Computing 2 | ------------------------------ 3 | M: Yuri Benditovich 4 | M: Yuri Benditovich 5 | M: Yan Vugenfirer 6 | M: Yan Vugenfirer 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://ci.appveyor.com/api/projects/status/p3s6bdbx8mq8o0hu?svg=true)](https://ci.appveyor.com/api/projects/status/p3s6bdbx8mq8o0hu?svg=true) 2 | 3 | # UsbDk 4 | 5 | UsbDk (USB Development Kit) is a open-source library for Windows meant 6 | to provide user mode applications with direct and exclusive access to 7 | USB devices by detaching those from Windows PNP manager and device drivers 8 | and providing user mode with API for USB-specific operations on the device. 9 | 10 | The library is intended to be as generic as possible, support all types of 11 | USB devices, bulk and isochronous transfers, composite devices etc. 12 | 13 | Library supports all Windows OS versions starting from Windows XP/2003. 14 | 15 | ## Documentation 16 | 17 | * See ARCHITECTURE document in the source tree root. 18 | * See Documentation folder in the source tree root. 19 | * See UsbDkHelper\UsbDkHelper.h UsbDkHelper\UsbDkHelperHider.h for API documentation 20 | 21 | ## Building 22 | 23 | **Tools required:** 24 | 25 | * Visual Studio 2015/Visual Studio 2015 Express update 3 or newer 26 | * WDK 10 27 | * Windows 10 SDK 28 | * Wix Toolset V3.8 (for building MSI installer) 29 | * WDK 7.1 (for Windows XP/2003/Vista/2008 builds) 30 | 31 | ***Compilation*** 32 | 33 | Just open UsbDk.sln from the source tree root in Visual Studio 2015 and compile 34 | desired configuration. 35 | 36 | ## Installing and running 37 | 38 | Use UsbDkController.exe to install/uninstall and verify basic operation. 39 | Run UsbDkController.exe without parameters for command line options. 40 | 41 | ## Known issues 42 | 43 | * Installation on 64-bit versions of Windows 7 fails if security update 44 | [3033929](https://technet.microsoft.com/en-us/library/security/3033929) 45 | is not installed. Reason: UsbDk driver is signed by SHA-256 certificate. Without this update 46 | Windows 7 does not recognize the signature properly and fails to load the driver. 47 | -------------------------------------------------------------------------------- /Tools/AppVeyor/before-build.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | pushd "%~dp0" 3 | curl -L -s https://drive.google.com/uc?id=17ZK6a1kxhTR-YOkWpAOqNJZA7UBUGkmu --output Microsoft.DriverKit.Build.Tasks.14.0.dll 4 | copy /y Microsoft.DriverKit.Build.Tasks.14.0.dll "C:\Program Files (x86)\Windows Kits\10\build\bin" 5 | popd 6 | -------------------------------------------------------------------------------- /Tools/Driver.Initial.props: -------------------------------------------------------------------------------- 1 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | <_NT_TARGET_VERSION>0x0A00 20 | 21 | 22 | <_NT_TARGET_VERSION>0x0603 23 | 24 | 25 | <_NT_TARGET_VERSION>0x0602 26 | 27 | 28 | <_NT_TARGET_VERSION>0x0601 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Tools/Installer/UsbDkFiles.wxi: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Tools/Installer/buildmsi.bat: -------------------------------------------------------------------------------- 1 | SETLOCAL EnableExtensions EnableDelayedExpansion 2 | 3 | IF [%UsbDkVersion%] == [] SET UsbDkVersion=99.99.90 4 | IF [%UsbDkVersion%] == [".."] SET UsbDkVersion=99.99.90 5 | 6 | if [%1] EQU [NOSIGN] (SET DEBUG_CFG=Debug_NoSign) ELSE (SET DEBUG_CFG=Debug) 7 | 8 | pushd ..\..\Install_Debug\x86 9 | 10 | del *.msi *.wixobj *.wixpdb 11 | 12 | "%WIX%bin\candle.exe" ..\..\Tools\Installer\UsbDkInstaller.wxs -out UsbDk_Debug.wixobj -dUsbDkVersion=%UsbDkVersion% -dConfig=%DEBUG_CFG% 13 | if !ERRORLEVEL! NEQ 0 exit /B 1 14 | "%WIX%bin\light.exe" UsbDk_Debug.wixobj -out UsbDk_Debug_%UsbDkVersion%_x86.msi -sw1076 15 | if !ERRORLEVEL! NEQ 0 exit /B 1 16 | 17 | popd 18 | 19 | pushd ..\..\Install_Debug\x64 20 | 21 | del *.msi *.wixobj *.wixpdb 22 | 23 | "%WIX%bin\candle.exe" ..\..\Tools\Installer\UsbDkInstaller.wxs -out UsbDk_Debug.wixobj -dUsbDkVersion=%UsbDkVersion% -dConfig=%DEBUG_CFG% -dUsbDk64Bit=1 24 | if !ERRORLEVEL! NEQ 0 exit /B 1 25 | "%WIX%bin\light.exe" UsbDk_Debug.wixobj -out UsbDk_Debug_%UsbDkVersion%_x64.msi -sw1076 26 | if !ERRORLEVEL! NEQ 0 exit /B 1 27 | 28 | popd 29 | 30 | pushd ..\..\Install\x86 31 | 32 | del *.msi *.wixobj *.wixpdb 33 | 34 | "%WIX%bin\candle.exe" ..\..\Tools\Installer\UsbDkInstaller.wxs -out UsbDk.wixobj -dUsbDkVersion=%UsbDkVersion% -dConfig=Release 35 | if !ERRORLEVEL! NEQ 0 exit /B 1 36 | "%WIX%bin\light.exe" UsbDk.wixobj -out UsbDk_%UsbDkVersion%_x86.msi -sw1076 37 | if !ERRORLEVEL! NEQ 0 exit /B 1 38 | 39 | popd 40 | 41 | pushd ..\..\Install\x64 42 | 43 | del *.msi *.wixobj *.wixpdb 44 | 45 | "%WIX%bin\candle.exe" ..\..\Tools\Installer\UsbDkInstaller.wxs -out UsbDk.wixobj -dUsbDkVersion=%UsbDkVersion% -dConfig=Release -dUsbDk64Bit=1 46 | if !ERRORLEVEL! NEQ 0 exit /B 1 47 | "%WIX%bin\light.exe" UsbDk.wixobj -out UsbDk_%UsbDkVersion%_x64.msi -sw1076 48 | if !ERRORLEVEL! NEQ 0 exit /B 1 49 | 50 | popd 51 | 52 | echo SUCCEEDED -------------------------------------------------------------------------------- /Tools/RegSettingsForWdfLog.reg: -------------------------------------------------------------------------------- 1 | Windows Registry Editor Version 5.00 2 | 3 | [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\UsbDk\Parameters\Wdf] 4 | "VerifierOn"=dword:00000001 5 | "VerboseOn"=dword:00000001 6 | "LogPages"=dword:00000010 7 | "VerifyDownLevel"=dword:00000001 8 | "EnhancedVerifierOptions"=dword:00000001 9 | -------------------------------------------------------------------------------- /Tools/Trace/UsbDk-Autologger-Off.reg: -------------------------------------------------------------------------------- 1 | Windows Registry Editor Version 5.00 2 | 3 | [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WMI\Autologger\UsbDk] 4 | "Start"=dword:00000000 5 | "Guid"="{88e1661f-48b6-410f-b096-ba84e9f0656f}" 6 | 7 | [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WMI\Autologger\UsbDk\{88e1661f-48b6-410f-b096-ba84e9f0656f}] 8 | "Enabled"=dword:00000000 9 | -------------------------------------------------------------------------------- /Tools/Trace/UsbDk-Autologger-On.reg: -------------------------------------------------------------------------------- 1 | Windows Registry Editor Version 5.00 2 | 3 | [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WMI\Autologger\UsbDk] 4 | "Start"=dword:00000001 5 | "Guid"="{88e1661f-48b6-410f-b096-ba84e9f0656f}" 6 | "FlushTimer"=dword:00000001 7 | 8 | [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WMI\Autologger\UsbDk\{88e1661f-48b6-410f-b096-ba84e9f0656f}] 9 | "Enabled"=dword:00000001 10 | "EnableFlags"=dword:7fffffff 11 | "EnableLevel"=dword:00000006 12 | -------------------------------------------------------------------------------- /Tools/Trace/UsbDk.ctl: -------------------------------------------------------------------------------- 1 | 88e1661f,48b6,410f,b096,ba84e9f0656f UsbDkTraceGuid -------------------------------------------------------------------------------- /Tools/Trace/UsbDkLogman.bat: -------------------------------------------------------------------------------- 1 | echo off 2 | pushd "%~dp0" 3 | logman stop usbdkm -ets >nul 2>&1 4 | logman delete usbdkm -ets >nul 2>&1 5 | logman create trace usbdkm -o usbdk.etl -ow -ets 6 | logman update usbdkm -p {88e1661f-48b6-410f-b096-ba84e9f0656f} 0x7fffffff 6 -ets 7 | echo Recording started. 8 | echo Reproduce the problem, then press ENTER 9 | pause > nul 10 | logman stop usbdkm -ets 11 | dir usbdk.etl 12 | echo Please collect usbdk.etl file now 13 | pause 14 | popd 15 | -------------------------------------------------------------------------------- /Tools/Trace/UsbDkTextLog.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | tracepdb -f UsbDk.pdb 4 | tracefmt.exe -display -p . UsbDkTrace.etl 5 | -------------------------------------------------------------------------------- /Tools/Trace/UsbDkTrace.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | traceview.exe -start UsbDk -rt -flag 0x7FFFFFFF -level 6 -ft 1 -pdb UsbDk.pdb -guid UsbDk.ctl 4 | 5 | mkdir WPP 6 | start traceview.exe -process -rt UsbDk -display -ods -o WPP\\UsbDk.txt -pdb UsbDk.pdb -guid UsbDk.ctl 7 | start dbgview.exe 8 | 9 | pause 10 | 11 | traceview.exe -stop UsbDk 12 | -------------------------------------------------------------------------------- /Tools/Trace/UsbDkTraceToFile.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | traceview.exe -start UsbDk -rt -flag 0x7FFFFFFF -level 6 -ft 1 -guid UsbDk.ctl -f UsbDkTrace.etl 4 | 5 | echo . 6 | echo Tracing is in progress. Press ENTER to stop it. 7 | echo . 8 | 9 | pause 10 | 11 | traceview.exe -stop UsbDk 12 | -------------------------------------------------------------------------------- /Tools/versions.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include 25 | #include 26 | 27 | #if (7-USBDK_MAJOR_VERSION-7) == 14 28 | #undef USBDK_MAJOR_VERSION 29 | #define USBDK_MAJOR_VERSION 0 30 | #endif 31 | 32 | #if (7-USBDK_MINOR_VERSION-7) == 14 33 | #undef USBDK_MINOR_VERSION 34 | #define USBDK_MINOR_VERSION 0 35 | #endif 36 | 37 | #if (7-USBDK_BUILD_NUMBER-7) == 14 38 | #undef USBDK_BUILD_NUMBER 39 | #define USBDK_BUILD_NUMBER 0 40 | #endif 41 | 42 | #ifdef VER_COMPANYNAME_STR 43 | #undef VER_COMPANYNAME_STR 44 | #endif 45 | #ifdef VER_LEGALTRADEMARKS_STR 46 | #undef VER_LEGALTRADEMARKS_STR 47 | #endif 48 | #ifdef VER_PRODUCTBUILD 49 | #undef VER_PRODUCTBUILD 50 | #endif 51 | #ifdef VER_PRODUCTBUILD_QFE 52 | #undef VER_PRODUCTBUILD_QFE 53 | #endif 54 | #ifdef VER_PRODUCTNAME_STR 55 | #undef VER_PRODUCTNAME_STR 56 | #endif 57 | #ifdef VER_PRODUCTMAJORVERSION 58 | #undef VER_PRODUCTMAJORVERSION 59 | #endif 60 | #ifdef VER_PRODUCTMINORVERSION 61 | #undef VER_PRODUCTMINORVERSION 62 | #endif 63 | 64 | #define VER_COMPANYNAME_STR "Red Hat Inc." 65 | #define VER_LEGALTRADEMARKS_STR "" 66 | #define VER_LEGALCOPYRIGHT_STR "Copyright (C) 2014 Red Hat Inc." 67 | 68 | #define VER_PRODUCTMAJORVERSION USBDK_MAJOR_VERSION 69 | #define VER_PRODUCTMINORVERSION USBDK_MINOR_VERSION 70 | #define VER_PRODUCTBUILD USBDK_BUILD_NUMBER 71 | #define VER_PRODUCTBUILD_QFE 0 72 | 73 | #define VER_LANGNEUTRAL 74 | 75 | #define VER_PRODUCTNAME_STR "Red Hat USB Development Kit" 76 | 77 | #include "common.ver" 78 | -------------------------------------------------------------------------------- /Tools/vs_cmdline.vbs: -------------------------------------------------------------------------------- 1 | Dim strCmdLine, strTemp 2 | Set WshShell = Wscript.CreateObject("Wscript.Shell") 3 | 4 | On Error Resume Next 5 | strCmdLine = WshShell.RegRead("HKLM\SOFTWARE\Microsoft\VisualStudio\14.0\InstallDir") 6 | ' In case of error assume WoW64 case 7 | If Err <> 0 Then 8 | On Error Goto 0 9 | strCmdLine = WshShell.RegRead("HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0\InstallDir") 10 | End If 11 | 12 | On Error Goto 0 13 | strCmdLine = chr(34) + strCmdLine + "devenv.com" + chr(34) 14 | For i = 0 to (Wscript.Arguments.Count - 1) 15 | strTemp = Wscript.Arguments(i) 16 | If InStr(strTemp, " ") Or InStr(strTemp, "|") Then 17 | strCmdLine = strCmdLine + " " + chr(34) + strTemp + chr(34) 18 | Else 19 | strCmdLine = strCmdLine + " " + strTemp 20 | End If 21 | Next 22 | 23 | WScript.Echo strCmdLine + vbCrLf 24 | -------------------------------------------------------------------------------- /Tools/vs_run.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | for /f "tokens=*" %%a in ( 4 | 'cscript.exe /nologo "%~dp0\vs_cmdline.vbs" %*' 5 | ) do ( 6 | SET vs_cmd=%%a 7 | ) 8 | 9 | IF NOT DEFINED vs_cmd ( 10 | echo Visual Studio not found 11 | EXIT /b 1 12 | ) 13 | 14 | SET vs_cmd_no_quotes="%vs_cmd:"=%" 15 | IF "vs_cmd_no_quotes" == "" ( 16 | echo Visual Studio not found 17 | EXIT /b 2 18 | ) 19 | 20 | %vs_cmd% 21 | if %ERRORLEVEL% GEQ 1 ( 22 | echo Build with Visual Studio FAILED 23 | EXIT /b 3 24 | ) 25 | 26 | EXIT /b 0 27 | -------------------------------------------------------------------------------- /UsbDk Package/UsbDk Package.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {8E41214B-6785-4CFE-B992-037D68949A14} 6 | inf;inv;inx;mof;mc; 7 | 8 | 9 | -------------------------------------------------------------------------------- /UsbDk/DeviceAccess.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "Alloc.h" 27 | #include "RegText.h" 28 | #include "MemoryBuffer.h" 29 | #include "Public.h" 30 | 31 | 32 | class CWdmDeviceAccess 33 | { 34 | public: 35 | CWdmDeviceAccess(PDEVICE_OBJECT WdmDevice) 36 | : m_DevObj(WdmDevice) 37 | { ObReferenceObjectWithTag(m_DevObj, 'DMHR'); } 38 | 39 | ~CWdmDeviceAccess() 40 | { ObDereferenceObjectWithTag(m_DevObj, 'DMHR'); } 41 | 42 | CWdmDeviceAccess(const CWdmDeviceAccess&) = delete; 43 | CWdmDeviceAccess& operator= (const CWdmDeviceAccess&) = delete; 44 | 45 | NTSTATUS QueryForInterface(const GUID &, __out INTERFACE &, USHORT intfSize, USHORT intfVer, __in_opt PVOID intfCtx = nullptr); 46 | 47 | enum : ULONG 48 | { 49 | NO_ADDRESS = (ULONG)-1 50 | }; 51 | 52 | ULONG GetAddress(); 53 | CRegText *GetDeviceID() { return new CRegSz(QueryBusID(BusQueryDeviceID)); } 54 | CRegText *GetInstanceID() { return new CRegSz(QueryBusID(BusQueryInstanceID)); } 55 | bool QueryPowerData(CM_POWER_DATA& powerData); 56 | protected: 57 | PDEVICE_OBJECT m_DevObj; 58 | 59 | private: 60 | PWCHAR QueryBusID(BUS_QUERY_ID_TYPE idType); 61 | NTSTATUS QueryCapabilities(DEVICE_CAPABILITIES &Capabilities); 62 | 63 | static PWCHAR MakeNonPagedDuplicate(BUS_QUERY_ID_TYPE idType, PWCHAR idData); 64 | static SIZE_T GetIdBufferLength(BUS_QUERY_ID_TYPE idType, PWCHAR idData); 65 | }; 66 | 67 | class CWdmUsbDeviceAccess : public CWdmDeviceAccess 68 | { 69 | public: 70 | CWdmUsbDeviceAccess(PDEVICE_OBJECT WdmDevice) 71 | : CWdmDeviceAccess(WdmDevice) 72 | { } 73 | 74 | NTSTATUS Reset(bool ForceD0); 75 | NTSTATUS GetDeviceDescriptor(USB_DEVICE_DESCRIPTOR &Descriptor); 76 | NTSTATUS GetConfigurationDescriptor(UCHAR Index, USB_CONFIGURATION_DESCRIPTOR &Descriptor, size_t Length); 77 | 78 | CWdmUsbDeviceAccess(const CWdmUsbDeviceAccess&) = delete; 79 | CWdmUsbDeviceAccess& operator= (const CWdmUsbDeviceAccess&) = delete; 80 | }; 81 | 82 | #if !TARGET_OS_WIN_XP 83 | class CWdmUSBD 84 | { 85 | public: 86 | CWdmUSBD(PDRIVER_OBJECT Driver, PDEVICE_OBJECT TargetDevice) 87 | : m_Driver(Driver) 88 | , m_TargetDevice(TargetDevice) 89 | {} 90 | 91 | ~CWdmUSBD(); 92 | 93 | bool Create(); 94 | 95 | bool IsSuperSpeed() const 96 | { return IsCapable(GUID_USB_CAPABILITY_DEVICE_CONNECTION_SUPER_SPEED_COMPATIBLE); } 97 | 98 | bool IsHighSpeed() const 99 | { return IsCapable(GUID_USB_CAPABILITY_DEVICE_CONNECTION_HIGH_SPEED_COMPATIBLE); } 100 | 101 | private: 102 | bool IsCapable(const GUID& CapabilityGuid) const 103 | { 104 | auto status = USBD_QueryUsbCapability(m_USBDHandle, &CapabilityGuid, 0, nullptr, nullptr); 105 | ASSERT(status != STATUS_INVALID_PARAMETER); 106 | return NT_SUCCESS(status); 107 | } 108 | 109 | PDRIVER_OBJECT m_Driver; 110 | PDEVICE_OBJECT m_TargetDevice; 111 | 112 | PDEVICE_OBJECT m_USBDDevice = nullptr; 113 | PDEVICE_OBJECT m_AttachmentPoint = nullptr; 114 | 115 | USBD_HANDLE m_USBDHandle = nullptr; 116 | }; 117 | #endif 118 | 119 | bool UsbDkGetWdmDeviceIdentity(const PDEVICE_OBJECT PDO, 120 | CObjHolder *DeviceID, 121 | CObjHolder *InstanceID = nullptr); 122 | 123 | USB_DK_DEVICE_SPEED UsbDkWdmUsbDeviceGetSpeed(PDEVICE_OBJECT PDO, PDRIVER_OBJECT DriverObject); 124 | 125 | template 126 | void UsbDkBuildDescriptorRequest(URB &Urb, UCHAR Type, UCHAR Index, TBuffer &Buffer, ULONG BufferLength = sizeof(TBuffer)) 127 | { 128 | UsbBuildGetDescriptorRequest(&Urb, sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST), 129 | Type, Index, 0, 130 | &Buffer, nullptr, BufferLength, 131 | nullptr); 132 | } 133 | 134 | NTSTATUS UsbDkSendUrbSynchronously(PDEVICE_OBJECT Target, URB &Urb); 135 | -------------------------------------------------------------------------------- /UsbDk/Driver.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "driver.h" 26 | #include "ControlDevice.h" 27 | #include "FilterDevice.h" 28 | #include "driver.tmh" 29 | 30 | #ifdef ALLOC_PRAGMA 31 | #pragma alloc_text (INIT, DriverEntry) 32 | #endif 33 | 34 | NTSTATUS 35 | DriverEntry( 36 | _In_ PDRIVER_OBJECT DriverObject, 37 | _In_ PUNICODE_STRING RegistryPath 38 | ) 39 | { 40 | WDF_DRIVER_CONFIG config; 41 | NTSTATUS status; 42 | WDF_OBJECT_ATTRIBUTES attributes; 43 | 44 | // 45 | // Initialize WPP Tracing 46 | // 47 | WPP_INIT_TRACING( DriverObject, RegistryPath ); 48 | 49 | TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); 50 | 51 | // 52 | // Register a cleanup callback so that we can call WPP_CLEANUP when 53 | // the framework driver object is deleted during driver unload. 54 | // 55 | WDF_OBJECT_ATTRIBUTES_INIT(&attributes); 56 | attributes.EvtCleanupCallback = UsbDkEvtDriverContextCleanup; 57 | 58 | WDF_DRIVER_CONFIG_INIT(&config, 59 | UsbDkEvtDeviceAdd); 60 | 61 | config.EvtDriverUnload = DriverUnload; 62 | 63 | CDriverParamsRegistryPath::CreateFrom(RegistryPath); 64 | 65 | WDFDRIVER Driver; 66 | status = WdfDriverCreate(DriverObject, 67 | RegistryPath, 68 | &attributes, 69 | &config, 70 | &Driver); 71 | 72 | if (!NT_SUCCESS(status)) { 73 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfDriverCreate failed %!STATUS!", status); 74 | WPP_CLEANUP(DriverObject); 75 | return status; 76 | } 77 | 78 | if (!CUsbDkControlDevice::Allocate()) 79 | { 80 | return STATUS_INSUFFICIENT_RESOURCES; 81 | } 82 | 83 | TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); 84 | 85 | return STATUS_SUCCESS; 86 | } 87 | 88 | VOID 89 | DriverUnload(IN WDFDRIVER Driver) 90 | { 91 | UNREFERENCED_PARAMETER(Driver); 92 | 93 | PAGED_CODE(); 94 | 95 | TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); 96 | 97 | CUsbDkControlDevice::Deallocate(); 98 | 99 | CDriverParamsRegistryPath::Destroy(); 100 | 101 | return; 102 | } 103 | 104 | NTSTATUS 105 | UsbDkEvtDeviceAdd( 106 | _In_ WDFDRIVER Driver, 107 | _Inout_ PWDFDEVICE_INIT DeviceInit 108 | ) 109 | { 110 | PAGED_CODE(); 111 | 112 | TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); 113 | 114 | auto FilterDevice = new CUsbDkFilterDevice(); 115 | if (FilterDevice == nullptr) 116 | { 117 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC! Failed to allocate filter device"); 118 | return STATUS_INSUFFICIENT_RESOURCES; 119 | } 120 | 121 | auto status = FilterDevice->Create(DeviceInit); 122 | if (!NT_SUCCESS(status)) 123 | { 124 | FilterDevice->Release(); 125 | return status; 126 | } 127 | 128 | status = FilterDevice->AttachToStack(Driver); 129 | 130 | TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit %!STATUS!", status); 131 | 132 | return status; 133 | } 134 | 135 | VOID 136 | UsbDkEvtDriverContextCleanup( 137 | _In_ WDFOBJECT DriverObject 138 | ) 139 | { 140 | UNREFERENCED_PARAMETER(DriverObject); 141 | 142 | PAGED_CODE (); 143 | 144 | TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); 145 | 146 | // 147 | // Stop WPP Tracing 148 | // 149 | WPP_CLEANUP( WdfGetDriver() ); 150 | 151 | } 152 | -------------------------------------------------------------------------------- /UsbDk/Driver.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "trace.h" 25 | 26 | // 27 | // WDFDRIVER Events 28 | // 29 | 30 | extern "C" 31 | DRIVER_INITIALIZE DriverEntry; 32 | 33 | EVT_WDF_DRIVER_UNLOAD DriverUnload; 34 | EVT_WDF_DRIVER_DEVICE_ADD UsbDkEvtDeviceAdd; 35 | EVT_WDF_OBJECT_CONTEXT_CLEANUP UsbDkEvtDriverContextCleanup; 36 | -------------------------------------------------------------------------------- /UsbDk/FilterStrategy.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "FilterStrategy.h" 26 | #include "trace.h" 27 | #include "FilterStrategy.tmh" 28 | #include "FilterDevice.h" 29 | #include "ControlDevice.h" 30 | #include "WdfRequest.h" 31 | 32 | NTSTATUS CUsbDkFilterStrategy::PNPPreProcess(PIRP Irp) 33 | { 34 | IoSkipCurrentIrpStackLocation(Irp); 35 | return WdfDeviceWdmDispatchPreprocessedIrp(m_Owner->WdfObject(), Irp); 36 | } 37 | 38 | void CUsbDkFilterStrategy::ForwardRequest(WDFREQUEST Request) 39 | { 40 | CWdfRequest WdfRequest(Request); 41 | 42 | auto status = WdfRequest.SendAndForget(m_Owner->IOTarget()); 43 | if (!NT_SUCCESS(status)) 44 | { 45 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_FILTERSTRATEGY, "%!FUNC! failed %!STATUS!", status); 46 | } 47 | } 48 | 49 | void CUsbDkFilterStrategy::IoInCallerContext(WDFDEVICE Device, WDFREQUEST Request) 50 | { 51 | auto status = WdfDeviceEnqueueRequest(Device, Request); 52 | if (!NT_SUCCESS(status)) 53 | { 54 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_FILTERSTRATEGY, "%!FUNC! failed to enqueue request: %!STATUS!", status); 55 | WdfRequestComplete(Request, status); 56 | } 57 | } 58 | 59 | NTSTATUS CUsbDkFilterStrategy::Create(CUsbDkFilterDevice *Owner) 60 | { 61 | m_Owner = Owner; 62 | 63 | m_ControlDevice = CUsbDkControlDevice::Reference(Owner->GetDriverHandle()); 64 | if (m_ControlDevice == nullptr) 65 | { 66 | return STATUS_INSUFFICIENT_RESOURCES; 67 | } 68 | 69 | return STATUS_SUCCESS; 70 | } 71 | 72 | void CUsbDkFilterStrategy::Delete() 73 | { 74 | if (m_ControlDevice != nullptr) 75 | { 76 | CUsbDkControlDevice::Release(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /UsbDk/FilterStrategy.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "Alloc.h" 27 | #include "UsbDkUtil.h" 28 | 29 | class CUsbDkFilterDevice; 30 | class CUsbDkChildDevice; 31 | class CUsbDkControlDevice; 32 | class CWdfRequest; 33 | 34 | class CUsbDkFilterStrategy 35 | { 36 | public: 37 | virtual NTSTATUS Create(CUsbDkFilterDevice *Owner); 38 | virtual void Delete(); 39 | virtual NTSTATUS PNPPreProcess(PIRP Irp); 40 | virtual void IoInCallerContext(WDFDEVICE Device, WDFREQUEST Request); 41 | 42 | virtual void IoDeviceControl(WDFREQUEST Request, 43 | size_t /*OutputBufferLength*/, size_t /*InputBufferLength*/, 44 | ULONG /*IoControlCode*/) 45 | { ForwardRequest(Request); } 46 | 47 | virtual void IoDeviceControlConfig(WDFREQUEST Request, 48 | size_t /*OutputBufferLength*/, size_t /*InputBufferLength*/, 49 | ULONG /*IoControlCode*/) 50 | { ForwardRequest(Request); } 51 | 52 | virtual NTSTATUS MakeAvailable() = 0; 53 | 54 | typedef CWdmList TChildrenList; 55 | 56 | virtual TChildrenList& Children() 57 | { return m_Children; } 58 | 59 | static size_t GetRequestContextSize() 60 | { return 0; } 61 | 62 | CUsbDkControlDevice* GetControlDevice() 63 | { return m_ControlDevice; } 64 | 65 | virtual void OnClose(){} 66 | 67 | protected: 68 | CUsbDkFilterDevice *m_Owner = nullptr; 69 | CUsbDkControlDevice *m_ControlDevice = nullptr; 70 | 71 | template 72 | NTSTATUS PostProcessOnSuccess(PIRP Irp, PostProcessFuncT PostProcessFunc) 73 | { 74 | return PostProcess(Irp, [PostProcessFunc](PIRP Irp, NTSTATUS Status) -> NTSTATUS 75 | { return NT_SUCCESS(Status) ? PostProcessFunc(Irp) : Status; }); 76 | } 77 | 78 | template 79 | NTSTATUS PostProcessOnFailure(PIRP Irp, PostProcessFuncT PostProcessFunc) 80 | { 81 | return PostProcess(Irp, [PostProcessFunc](PIRP Irp, NTSTATUS Status) -> NTSTATUS 82 | { return !NT_SUCCESS(Status) ? PostProcessFunc(Irp, Status) : Status; }); 83 | } 84 | 85 | template 86 | NTSTATUS PostProcess(PIRP Irp, PostProcessFuncT PostProcessFunc) 87 | { 88 | IoCopyCurrentIrpStackLocationToNext(Irp); 89 | 90 | auto status = CIrp::ForwardAndWait(Irp, [this, Irp]() 91 | { return WdfDeviceWdmDispatchPreprocessedIrp(m_Owner->WdfObject(), Irp); }); 92 | 93 | status = PostProcessFunc(Irp, status); 94 | 95 | Irp->IoStatus.Status = status; 96 | IoCompleteRequest(Irp, IO_NO_INCREMENT); 97 | return status; 98 | } 99 | 100 | NTSTATUS CompleteWithStatus(PIRP Irp, NTSTATUS Status) 101 | { 102 | Irp->IoStatus.Status = Status; 103 | IoCompleteRequest(Irp, IO_NO_INCREMENT); 104 | return Status; 105 | } 106 | 107 | private: 108 | void ForwardRequest(WDFREQUEST Request); 109 | TChildrenList m_Children; 110 | }; 111 | 112 | class CUsbDkNullFilterStrategy : public CUsbDkFilterStrategy 113 | { 114 | public: 115 | virtual NTSTATUS MakeAvailable() override 116 | { return STATUS_SUCCESS; } 117 | }; 118 | -------------------------------------------------------------------------------- /UsbDk/HideRulesRegPublic.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #define USBDK_PARAMS_SUBKEY_NAME TEXT("Parameters") 27 | #define USBDK_HIDE_RULES_SUBKEY_NAME TEXT("HideRules") 28 | 29 | #define USBDK_HIDE_RULE_SHOULD_HIDE TEXT("ShouldHide") 30 | #define USBDK_HIDE_RULE_VID TEXT("VID") 31 | #define USBDK_HIDE_RULE_PID TEXT("PID") 32 | #define USBDK_HIDE_RULE_BCD TEXT("BCD") 33 | #define USBDK_HIDE_RULE_CLASS TEXT("Class") 34 | #define USBDK_HIDE_RULE_TYPE TEXT("Type") 35 | 36 | #define USBDK_HIDE_RULES_PATH TEXT("SYSTEM\\CurrentControlSet\\Services\\") \ 37 | USBDK_DRIVER_NAME TEXT("\\") \ 38 | USBDK_PARAMS_SUBKEY_NAME TEXT("\\") \ 39 | USBDK_HIDE_RULES_SUBKEY_NAME TEXT("\\") 40 | 41 | #define USBDK_REG_HIDE_RULE_MATCH_ALL ULONG(-1) 42 | 43 | static inline ULONG64 HideRuleUlongMaskFromRegistry(DWORD Value) 44 | { 45 | return (Value == USBDK_REG_HIDE_RULE_MATCH_ALL) ? USB_DK_HIDE_RULE_MATCH_ALL 46 | : Value; 47 | } 48 | 49 | static inline ULONG64 HideRuleBoolFromRegistry(DWORD Value) 50 | { 51 | return !!Value; 52 | } 53 | -------------------------------------------------------------------------------- /UsbDk/HiderDevice.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | **********************************************************************/ 22 | 23 | #pragma once 24 | 25 | #include "WdfDevice.h" 26 | #include "Alloc.h" 27 | #include "UsbDkUtil.h" 28 | 29 | class CWdfRequest; 30 | 31 | class CUsbDkHiderDeviceQueue : public CWdfDefaultQueue 32 | { 33 | public: 34 | CUsbDkHiderDeviceQueue() 35 | : CWdfDefaultQueue(WdfIoQueueDispatchSequential, WdfExecutionLevelPassive) 36 | {} 37 | 38 | private: 39 | virtual void SetCallbacks(WDF_IO_QUEUE_CONFIG &QueueConfig) override; 40 | static void DeviceControl(WDFQUEUE Queue, 41 | WDFREQUEST Request, 42 | size_t OutputBufferLength, 43 | size_t InputBufferLength, 44 | ULONG IoControlCode); 45 | 46 | CUsbDkHiderDeviceQueue(const CUsbDkHiderDeviceQueue&) = delete; 47 | CUsbDkHiderDeviceQueue& operator= (const CUsbDkHiderDeviceQueue&) = delete; 48 | }; 49 | 50 | class CUsbDkHiderDevice : private CWdfControlDevice, public CAllocatable 51 | { 52 | public: 53 | CUsbDkHiderDevice() {} 54 | NTSTATUS Create(WDFDRIVER Driver); 55 | NTSTATUS Register(); 56 | void Delete() 57 | { CWdfControlDevice::Delete(); } 58 | WDFDRIVER DriverHandle() const 59 | { return m_Driver; } 60 | 61 | private: 62 | static void ContextCleanup(_In_ WDFOBJECT DeviceObject); 63 | CUsbDkHiderDeviceQueue m_DeviceQueue; 64 | WDFDRIVER m_Driver = nullptr; 65 | 66 | friend class CUsbDkHiderDeviceInit; 67 | }; 68 | 69 | typedef struct _USBDK_HIDER_DEVICE_EXTENSION { 70 | 71 | CUsbDkHiderDevice *UsbDkHider; 72 | 73 | _USBDK_HIDER_DEVICE_EXTENSION(const _USBDK_HIDER_DEVICE_EXTENSION&) = delete; 74 | _USBDK_HIDER_DEVICE_EXTENSION& operator= (const _USBDK_HIDER_DEVICE_EXTENSION&) = delete; 75 | 76 | } USBDK_HIDER_DEVICE_EXTENSION, *PUSBDK_HIDER_DEVICE_EXTENSION; 77 | WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(USBDK_HIDER_DEVICE_EXTENSION, UsbDkHiderGetContext); 78 | -------------------------------------------------------------------------------- /UsbDk/HiderStrategy.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | **********************************************************************/ 22 | 23 | #pragma once 24 | 25 | #include "FilterStrategy.h" 26 | 27 | class CUsbDkHiderStrategy : public CUsbDkNullFilterStrategy 28 | { 29 | public: 30 | virtual NTSTATUS Create(CUsbDkFilterDevice *Owner) override; 31 | virtual void Delete() override; 32 | 33 | virtual NTSTATUS PNPPreProcess(PIRP Irp) override; 34 | virtual NTSTATUS MakeAvailable() override 35 | { 36 | m_RemovalStopWatch.Start(); 37 | return STATUS_SUCCESS; 38 | } 39 | 40 | private: 41 | void PatchDeviceID(PIRP Irp); 42 | NTSTATUS PatchDeviceText(PIRP Irp); 43 | bool ShouldDenyRemoval() const; 44 | 45 | CStopWatch m_RemovalStopWatch; 46 | }; 47 | -------------------------------------------------------------------------------- /UsbDk/Irp.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "Irp.h" 26 | 27 | CIrp::~CIrp() 28 | { 29 | if (m_Irp != nullptr) 30 | { 31 | DestroyIrp(); 32 | ReleaseTarget(); 33 | } 34 | } 35 | 36 | void CIrp::DestroyIrp() 37 | { 38 | ASSERT(m_Irp != nullptr); 39 | 40 | IoFreeIrp(m_Irp); 41 | m_Irp = nullptr; 42 | } 43 | 44 | void CIrp::ReleaseTarget() 45 | { 46 | ASSERT(m_TargetDevice != nullptr); 47 | 48 | ObDereferenceObject(m_TargetDevice); 49 | m_TargetDevice = nullptr; 50 | } 51 | 52 | void CIrp::Destroy() 53 | { 54 | DestroyIrp(); 55 | ReleaseTarget(); 56 | } 57 | 58 | NTSTATUS CIrp::Create(PDEVICE_OBJECT TargetDevice) 59 | { 60 | ASSERT(m_TargetDevice == nullptr); 61 | ASSERT(m_Irp == nullptr); 62 | 63 | m_Irp = IoAllocateIrp(TargetDevice->StackSize, FALSE); 64 | if (m_Irp == nullptr) 65 | { 66 | return STATUS_INSUFFICIENT_RESOURCES; 67 | } 68 | 69 | m_Irp->IoStatus.Status = STATUS_NOT_SUPPORTED; 70 | 71 | ObReferenceObject(TargetDevice); 72 | m_TargetDevice = TargetDevice; 73 | return STATUS_SUCCESS; 74 | } 75 | 76 | CIoControlIrp::~CIoControlIrp() 77 | { 78 | if (m_TargetDevice != nullptr) 79 | { 80 | Destroy(); 81 | } 82 | } 83 | 84 | void CIoControlIrp::Destroy() 85 | { 86 | ASSERT(m_TargetDevice != nullptr); 87 | 88 | ObDereferenceObject(m_TargetDevice); 89 | m_TargetDevice = nullptr; 90 | } 91 | 92 | NTSTATUS CIoControlIrp::Create(PDEVICE_OBJECT TargetDevice, 93 | ULONG IoControlCode, 94 | bool IsInternal, 95 | PVOID InputBuffer, 96 | ULONG InputBufferLength, 97 | PVOID OutputBuffer, 98 | ULONG OutputBufferLength) 99 | { 100 | m_Irp = IoBuildDeviceIoControlRequest(IoControlCode, 101 | TargetDevice, 102 | InputBuffer, 103 | InputBufferLength, 104 | OutputBuffer, 105 | OutputBufferLength, 106 | IsInternal ? TRUE : FALSE, 107 | m_Event, 108 | &m_IoControlStatus); 109 | 110 | if (m_Irp == nullptr) 111 | { 112 | return STATUS_INSUFFICIENT_RESOURCES; 113 | } 114 | 115 | ObReferenceObject(TargetDevice); 116 | m_TargetDevice = TargetDevice; 117 | return STATUS_SUCCESS; 118 | } 119 | 120 | NTSTATUS CIoControlIrp::SendSynchronously() 121 | { 122 | m_Event.Clear(); 123 | 124 | auto res = IoCallDriver(m_TargetDevice, m_Irp); 125 | if (res == STATUS_PENDING) 126 | { 127 | KeWaitForSingleObject(&m_Event, Executive, KernelMode, FALSE, nullptr); 128 | return m_IoControlStatus.Status; 129 | } 130 | 131 | return res; 132 | } 133 | -------------------------------------------------------------------------------- /UsbDk/Irp.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "Alloc.h" 27 | #include "UsbDkUtil.h" 28 | 29 | class CIrpBase : public CAllocatable 30 | { 31 | public: 32 | template 33 | void Configure(ConfiguratorT Configurator) 34 | { Configurator(IoGetNextIrpStackLocation(m_Irp)); } 35 | 36 | protected: 37 | CIrpBase() 38 | {} 39 | 40 | CIrpBase(const CIrpBase&) = delete; 41 | CIrpBase& operator= (const CIrpBase&) = delete; 42 | 43 | PIRP m_Irp = nullptr; 44 | PDEVICE_OBJECT m_TargetDevice = nullptr; 45 | }; 46 | 47 | class CIrp : public CIrpBase 48 | { 49 | public: 50 | CIrp() {}; 51 | ~CIrp(); 52 | 53 | NTSTATUS Create(PDEVICE_OBJECT TargetDevice); 54 | void Destroy(); 55 | 56 | NTSTATUS SendSynchronously() 57 | { return ForwardAndWait(m_Irp, m_TargetDevice); } 58 | 59 | static NTSTATUS ForwardAndWait(PIRP Irp, PDEVICE_OBJECT Target) 60 | { return ForwardAndWait(Irp, [Irp, Target](){ return IoCallDriver(Target, Irp); }); } 61 | 62 | template 63 | static NTSTATUS ForwardAndWait(PIRP Irp, SendFuncT SendFunc) 64 | { 65 | VERIFY_IS_IRQL_PASSIVE_LEVEL(); // because of wait 66 | CWdmEvent Event; 67 | IoSetCompletionRoutine(Irp, 68 | [](PDEVICE_OBJECT, PIRP Irp, PVOID Context) -> NTSTATUS 69 | { 70 | if (Irp->PendingReturned) 71 | { 72 | static_cast(Context)->Set(); 73 | } 74 | return STATUS_MORE_PROCESSING_REQUIRED; 75 | }, 76 | &Event, 77 | TRUE, TRUE, TRUE); 78 | 79 | auto status = SendFunc(); 80 | if (status == STATUS_PENDING) 81 | { 82 | Event.Wait(); 83 | return Irp->IoStatus.Status; 84 | } 85 | 86 | return status; 87 | } 88 | 89 | 90 | template 91 | void ReadResult(ReaderT Reader) 92 | { Reader(m_Irp->IoStatus.Information); } 93 | 94 | CIrp(const CIrp&) = delete; 95 | CIrp& operator= (const CIrp&) = delete; 96 | 97 | private: 98 | void DestroyIrp(); 99 | void ReleaseTarget(); 100 | }; 101 | 102 | class CIoControlIrp : public CIrpBase 103 | { 104 | public: 105 | CIoControlIrp() {} 106 | ~CIoControlIrp(); 107 | 108 | NTSTATUS Create(PDEVICE_OBJECT TargetDevice, 109 | ULONG IoControlCode, 110 | bool IsInternal = true, 111 | PVOID InputBuffer = nullptr, 112 | ULONG InputBufferLength = 0, 113 | PVOID OutputBuffer = nullptr, 114 | ULONG OutputBufferLength = 0); 115 | void Destroy(); 116 | NTSTATUS SendSynchronously(); 117 | 118 | CIoControlIrp(const CIoControlIrp&) = delete; 119 | CIoControlIrp& operator= (const CIoControlIrp&) = delete; 120 | 121 | private: 122 | CWdmEvent m_Event; 123 | IO_STATUS_BLOCK m_IoControlStatus; 124 | }; 125 | -------------------------------------------------------------------------------- /UsbDk/MemoryBuffer.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "MemoryBuffer.h" 26 | -------------------------------------------------------------------------------- /UsbDk/MemoryBuffer.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "Alloc.h" 27 | 28 | template 29 | class CPreAllocatedWdfMemoryBufferT 30 | { 31 | public: 32 | CPreAllocatedWdfMemoryBufferT(WDFMEMORY MemObj) 33 | : m_MemObj(MemObj) 34 | { 35 | if (MemObj != WDF_NO_HANDLE) 36 | { 37 | m_Ptr = reinterpret_cast(WdfMemoryGetBuffer(MemObj, &m_Size)); 38 | } 39 | else 40 | { 41 | m_Ptr = nullptr; 42 | m_Size = 0; 43 | } 44 | } 45 | operator T* () const { return m_Ptr; } 46 | T *operator ->() { return m_Ptr; } 47 | T* Ptr() const { return m_Ptr; } 48 | size_t Size() const { return m_Size; } 49 | size_t ArraySize() const { return m_Size / sizeof(T); } 50 | 51 | CPreAllocatedWdfMemoryBufferT(const CPreAllocatedWdfMemoryBufferT&) = delete; 52 | CPreAllocatedWdfMemoryBufferT& operator= (const CPreAllocatedWdfMemoryBufferT&) = delete; 53 | 54 | private: 55 | WDFMEMORY m_MemObj; 56 | T* m_Ptr; 57 | size_t m_Size; 58 | }; 59 | 60 | typedef CPreAllocatedWdfMemoryBufferT CPreAllocatedWdfMemoryBuffer; 61 | 62 | class CWdmMemoryBuffer final : public CAllocatable 63 | { 64 | public: 65 | CWdmMemoryBuffer(PVOID Buffer = nullptr, SIZE_T Size = 0) 66 | { 67 | m_Ptr = Buffer; 68 | m_Size = Size; 69 | } 70 | 71 | NTSTATUS Create(SIZE_T Size, POOL_TYPE PoolType) 72 | { 73 | m_Ptr = ExAllocatePoolWithTag(PoolType, Size, 'BMHR'); 74 | if (m_Ptr == nullptr) 75 | { 76 | return STATUS_INSUFFICIENT_RESOURCES; 77 | } 78 | 79 | m_Size = Size; 80 | return STATUS_SUCCESS; 81 | } 82 | 83 | NTSTATUS Recreate(SIZE_T Size, POOL_TYPE PoolType) 84 | { 85 | Destroy(); 86 | return (Size != 0) ? Create(Size, PoolType) : STATUS_SUCCESS; 87 | } 88 | 89 | ~CWdmMemoryBuffer() 90 | { Destroy(); } 91 | 92 | void Destroy() 93 | { 94 | if (m_Ptr != nullptr) 95 | { 96 | ExFreePool(m_Ptr); 97 | m_Ptr = nullptr; 98 | m_Size = 0; 99 | } 100 | } 101 | 102 | PVOID Ptr() const { return m_Ptr; } 103 | SIZE_T Size() const { return m_Size; } 104 | 105 | CWdmMemoryBuffer(const CWdmMemoryBuffer&) = delete; 106 | CWdmMemoryBuffer& operator= (const CWdmMemoryBuffer&) = delete; 107 | 108 | private: 109 | PVOID m_Ptr; 110 | size_t m_Size; 111 | }; 112 | -------------------------------------------------------------------------------- /UsbDk/Public.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #if !defined(MAX_DEVICE_ID_LEN) 27 | #define MAX_DEVICE_ID_LEN (200) 28 | #endif 29 | 30 | #include "UsbDkData.h" 31 | #include "UsbDkNames.h" 32 | 33 | #define USBDK_DEVICE_TYPE 50000 34 | 35 | // UsbDk Control Device IOCTLs 36 | #define IOCTL_USBDK_COUNT_DEVICES \ 37 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x851, METHOD_BUFFERED, FILE_READ_ACCESS )) 38 | #define IOCTL_USBDK_ENUM_DEVICES \ 39 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x852, METHOD_BUFFERED, FILE_READ_ACCESS )) 40 | #define IOCTL_USBDK_ADD_REDIRECT \ 41 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x854, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS )) 42 | #define IOCTL_USBDK_GET_CONFIG_DESCRIPTOR \ 43 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x855, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)) 44 | #define IOCTL_USBDK_UPDATE_REG_PARAMETERS \ 45 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x858, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)) 46 | 47 | // UsbDk Hider device IOCTLs 48 | #define IOCTL_USBDK_ADD_HIDE_RULE \ 49 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x856, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS )) 50 | #define IOCTL_USBDK_CLEAR_HIDE_RULES \ 51 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x857, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS )) 52 | 53 | //UsbDk redirector device IOCTLs 54 | #define IOCTL_USBDK_DEVICE_ABORT_PIPE \ 55 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x952, METHOD_BUFFERED, FILE_WRITE_ACCESS )) 56 | #define IOCTL_USBDK_DEVICE_SET_ALTSETTING \ 57 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x953, METHOD_BUFFERED, FILE_WRITE_ACCESS )) 58 | #define IOCTL_USBDK_DEVICE_RESET_DEVICE \ 59 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x954, METHOD_BUFFERED, FILE_WRITE_ACCESS )) 60 | #define IOCTL_USBDK_DEVICE_RESET_PIPE \ 61 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x955, METHOD_BUFFERED, FILE_WRITE_ACCESS )) 62 | #define IOCTL_USBDK_DEVICE_WRITE_PIPE \ 63 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x956, METHOD_BUFFERED, FILE_WRITE_ACCESS )) 64 | #define IOCTL_USBDK_DEVICE_READ_PIPE \ 65 | ULONG(CTL_CODE( USBDK_DEVICE_TYPE, 0x957, METHOD_BUFFERED, FILE_READ_ACCESS )) 66 | 67 | typedef struct tag_USBDK_ALTSETTINGS_IDXS 68 | { 69 | ULONG64 InterfaceIdx; 70 | ULONG64 AltSettingIdx; 71 | } USBDK_ALTSETTINGS_IDXS, *PUSBDK_ALTSETTINGS_IDXS; 72 | -------------------------------------------------------------------------------- /UsbDk/RedirectorStrategy.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "HiderStrategy.h" 27 | #include "UsbTarget.h" 28 | #include "WdfDevice.h" 29 | #include "Public.h" 30 | 31 | class CRegText; 32 | 33 | class CUsbDkRedirectorQueueData : public CWdfDefaultQueue 34 | { 35 | public: 36 | CUsbDkRedirectorQueueData() 37 | : CWdfDefaultQueue(WdfIoQueueDispatchParallel, WdfExecutionLevelDispatch) 38 | {} 39 | 40 | private: 41 | virtual void SetCallbacks(WDF_IO_QUEUE_CONFIG &QueueConfig) override; 42 | CUsbDkRedirectorQueueData(const CUsbDkRedirectorQueueData&) = delete; 43 | CUsbDkRedirectorQueueData& operator= (const CUsbDkRedirectorQueueData&) = delete; 44 | }; 45 | 46 | class CUsbDkRedirectorQueueConfig : public CWdfSpecificQueue 47 | { 48 | public: 49 | CUsbDkRedirectorQueueConfig() 50 | : CWdfSpecificQueue(WdfIoQueueDispatchSequential, WdfExecutionLevelPassive) 51 | {} 52 | 53 | private: 54 | virtual void SetCallbacks(WDF_IO_QUEUE_CONFIG &QueueConfig) override; 55 | CUsbDkRedirectorQueueConfig(const CUsbDkRedirectorQueueConfig&) = delete; 56 | CUsbDkRedirectorQueueConfig& operator= (const CUsbDkRedirectorQueueConfig&) = delete; 57 | }; 58 | 59 | class CRedirectorRequest; 60 | 61 | class CUsbDkRedirectorStrategy : public CUsbDkHiderStrategy 62 | { 63 | public: 64 | virtual NTSTATUS Create(CUsbDkFilterDevice *Owner) override; 65 | virtual NTSTATUS MakeAvailable() override; 66 | virtual void IoInCallerContext(WDFDEVICE Device, WDFREQUEST Request) override; 67 | 68 | virtual void IoDeviceControl(WDFREQUEST Request, 69 | size_t OutputBufferLength, 70 | size_t InputBufferLength, 71 | ULONG IoControlCode) override; 72 | 73 | virtual void IoDeviceControlConfig(WDFREQUEST Request, 74 | size_t OutputBufferLength, 75 | size_t InputBufferLength, 76 | ULONG IoControlCode) override; 77 | 78 | virtual void OnClose() override; 79 | 80 | void SetDeviceID(CRegText *DevID) 81 | { m_DeviceID = DevID; } 82 | 83 | void SetInstanceID(CRegText *InstID) 84 | { m_InstanceID = InstID; } 85 | 86 | static size_t GetRequestContextSize(); 87 | 88 | private: 89 | void DoControlTransfer(CRedirectorRequest &WdfRequest, WDFMEMORY DataBuffer); 90 | void WritePipe(WDFREQUEST Request); 91 | void ReadPipe(WDFREQUEST Request); 92 | 93 | static void CompleteTransferRequest(CRedirectorRequest &Request, 94 | NTSTATUS Status, 95 | USBD_STATUS UsbdStatus, 96 | size_t BytesTransferred); 97 | 98 | template 99 | static NTSTATUS IoInCallerContextRW(CRedirectorRequest &WdfRequest, 100 | TLockerFunc LockerFunc); 101 | 102 | static NTSTATUS IoInCallerContextRWControlTransfer(CRedirectorRequest &WdfRequest, 103 | const USB_DK_TRANSFER_REQUEST &TransferRequest); 104 | 105 | template 106 | static NTSTATUS IoInCallerContextRWIsoTransfer(CRedirectorRequest &WdfRequest, 107 | const USB_DK_TRANSFER_REQUEST &TransferRequest, 108 | TLockerFunc LockerFunc); 109 | 110 | static void IsoRWCompletion(WDFREQUEST Request, WDFIOTARGET Target, PWDF_REQUEST_COMPLETION_PARAMS Params, WDFCONTEXT Context); 111 | 112 | static void TraceTransferError(const CRedirectorRequest &WdfRequest, 113 | NTSTATUS Status, 114 | USBD_STATUS UsbdStatus); 115 | 116 | CWdfUsbTarget m_Target; 117 | 118 | CUsbDkRedirectorQueueData m_IncomingDataQueue; 119 | CUsbDkRedirectorQueueConfig m_IncomingConfigQueue; 120 | 121 | CObjHolder m_DeviceID; 122 | CObjHolder m_InstanceID; 123 | }; 124 | -------------------------------------------------------------------------------- /UsbDk/RegText.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "Trace.h" 26 | #include "RegText.h" 27 | #include "regtext.tmh" 28 | 29 | SIZE_T CRegMultiSz::GetBufferLength(PWCHAR Data) 30 | { 31 | SIZE_T Size = 0; 32 | 33 | if (Data != nullptr) 34 | { 35 | SIZE_T stringLen; 36 | 37 | do 38 | { 39 | stringLen = wcslen(Data); 40 | Data += stringLen + 1; 41 | Size += (stringLen + 1) * sizeof(WCHAR); 42 | } while (stringLen != 0); 43 | } 44 | 45 | return Size; 46 | } 47 | 48 | bool CRegText::Match(PCWSTR String) const 49 | { 50 | for (auto idData : *this) 51 | { 52 | if (!wcscmp(idData, String)) 53 | { 54 | return true; 55 | } 56 | } 57 | return false; 58 | } 59 | 60 | bool CRegText::MatchPrefix(PCWSTR String) const 61 | { 62 | for (auto idData : *this) 63 | { 64 | if (!wcsncmp(idData, String, wcslen(String))) 65 | { 66 | return true; 67 | } 68 | } 69 | return false; 70 | } 71 | 72 | void CRegText::Dump() const 73 | { 74 | for (auto idData : *this) 75 | { 76 | TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_REGTEXT, "%!FUNC! ID: %S", idData); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /UsbDk/RegText.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "Alloc.h" 27 | #include "MemoryBuffer.h" 28 | 29 | class CRegText : public CAllocatable 30 | { 31 | public: 32 | class iterator 33 | { 34 | public: 35 | iterator(PWCHAR Ptr) : m_Ptr(Ptr) {} 36 | iterator operator++() { m_Ptr += wcslen(m_Ptr) + 1; return m_Ptr; } 37 | bool operator!=(const iterator& other) const { return m_Ptr != other.m_Ptr; } 38 | PWCHAR operator *() const { return m_Ptr; } 39 | 40 | private: 41 | PWCHAR m_Ptr; 42 | }; 43 | 44 | virtual ~CRegText() {} 45 | 46 | iterator begin() const { return static_cast (m_Data.Ptr()); } 47 | iterator end() const { return reinterpret_cast(static_cast(m_Data.Ptr()) + m_Data.Size()); } 48 | bool empty() const { return (m_Data.Size() == 0); } 49 | 50 | bool Match(PCWSTR String) const; 51 | bool MatchPrefix(PCWSTR String) const; 52 | void Dump() const; 53 | 54 | CRegText(PVOID Buffer, SIZE_T Size) 55 | : m_Data(Buffer, Size) {} 56 | 57 | private: 58 | CWdmMemoryBuffer m_Data; 59 | SIZE_T m_currPos = 0; 60 | }; 61 | 62 | class CRegSz : public CRegText 63 | { 64 | public: 65 | CRegSz(PWCHAR Data) 66 | : CRegText(static_cast(Data), GetBufferLength(Data)) 67 | {} 68 | static SIZE_T GetBufferLength(PWCHAR Data) { return (Data != nullptr) ? (wcslen(Data) + 1) * sizeof(WCHAR) : 0; } 69 | }; 70 | 71 | class CRegMultiSz : public CRegText 72 | { 73 | public: 74 | CRegMultiSz(PWCHAR Data) 75 | : CRegText(static_cast(Data), GetBufferLength(Data)) 76 | {} 77 | static SIZE_T GetBufferLength(PWCHAR Data); 78 | }; 79 | -------------------------------------------------------------------------------- /UsbDk/Registry.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Kirill Moizik 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "Alloc.h" 26 | #include "MemoryBuffer.h" 27 | #include "Trace.h" 28 | #include "Registry.h" 29 | #include "Registry.tmh" 30 | 31 | NTSTATUS CRegKey::Open(const CRegKey &ParentKey, const UNICODE_STRING &RegPath) 32 | { 33 | OBJECT_ATTRIBUTES KeyAttributes; 34 | 35 | InitializeObjectAttributes(&KeyAttributes, 36 | const_cast(&RegPath), 37 | OBJ_KERNEL_HANDLE, ParentKey.m_Key, nullptr); 38 | 39 | auto status = ZwOpenKey(&m_Key, KEY_READ, &KeyAttributes); 40 | if (!NT_SUCCESS(status)) 41 | { 42 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_REGISTRY, 43 | "%!FUNC! Failed to open registry key %wZ, parent 0x%p (status: %!STATUS!)", &RegPath, ParentKey.m_Key, status); 44 | } 45 | 46 | return status; 47 | } 48 | 49 | NTSTATUS CRegKey::QuerySubkeyInfo(ULONG Index, 50 | KEY_INFORMATION_CLASS InfoClass, 51 | CWdmMemoryBuffer &Buffer) 52 | { 53 | ULONG BytesNeeded = 0; 54 | NTSTATUS status = STATUS_BUFFER_TOO_SMALL; 55 | 56 | while ((status == STATUS_BUFFER_OVERFLOW) || (status == STATUS_BUFFER_TOO_SMALL)) 57 | { 58 | status = Buffer.Recreate(BytesNeeded, PagedPool); 59 | if (NT_SUCCESS(status)) 60 | { 61 | status = ZwEnumerateKey(m_Key, 62 | Index, 63 | InfoClass, 64 | Buffer.Ptr(), 65 | static_cast(Buffer.Size()), 66 | &BytesNeeded); 67 | } 68 | } 69 | 70 | if (!NT_SUCCESS(status) && (status != STATUS_NO_MORE_ENTRIES)) 71 | { 72 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_REGISTRY, 73 | "%!FUNC! Failed to query subkey #%d info, info class %d (status: %!STATUS!)", Index, InfoClass, status); 74 | } 75 | 76 | return status; 77 | } 78 | 79 | NTSTATUS CRegKey::QueryValueInfo(const UNICODE_STRING &ValueName, 80 | KEY_VALUE_INFORMATION_CLASS InfoClass, 81 | CWdmMemoryBuffer &Buffer) 82 | { 83 | ULONG BytesNeeded = 0; 84 | NTSTATUS status = STATUS_BUFFER_TOO_SMALL; 85 | 86 | while ((status == STATUS_BUFFER_OVERFLOW) || (status == STATUS_BUFFER_TOO_SMALL)) 87 | { 88 | status = Buffer.Recreate(BytesNeeded, PagedPool); 89 | if (NT_SUCCESS(status)) 90 | { 91 | status = ZwQueryValueKey(m_Key, 92 | const_cast(&ValueName), 93 | InfoClass, 94 | Buffer.Ptr(), 95 | static_cast(Buffer.Size()), 96 | &BytesNeeded); 97 | } 98 | } 99 | 100 | if (status != STATUS_SUCCESS) 101 | { 102 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_REGISTRY, 103 | "%!FUNC! Failed to query value %wZ, info class %d (status: %!STATUS!)", &ValueName, InfoClass, status); 104 | } 105 | 106 | return status; 107 | } 108 | -------------------------------------------------------------------------------- /UsbDk/Registry.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Kirill Moizik 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | class CRegKey 27 | { 28 | public: 29 | NTSTATUS Open(const CRegKey &ParentKey, const UNICODE_STRING &RegPath); 30 | NTSTATUS Open(const UNICODE_STRING &RegPath) 31 | { return Open(CRegKey(), RegPath); } 32 | 33 | template 34 | NTSTATUS ForEachSubKey(TFunctor Functor) 35 | { 36 | auto status = STATUS_SUCCESS; 37 | for (ULONG Index = 0; status == STATUS_SUCCESS; Index++) 38 | { 39 | CWdmMemoryBuffer InfoBuffer; 40 | status = QuerySubkeyInfo(Index, KeyBasicInformation, InfoBuffer); 41 | if (NT_SUCCESS(status)) 42 | { 43 | auto Info = reinterpret_cast(InfoBuffer.Ptr()); 44 | CStringHolder Name; 45 | Name.Attach(Info->Name, static_cast(Info->NameLength)); 46 | 47 | Functor(Name); 48 | } 49 | } 50 | 51 | if (status == STATUS_NO_MORE_ENTRIES) 52 | { 53 | return STATUS_SUCCESS; 54 | } 55 | 56 | return status; 57 | } 58 | 59 | ~CRegKey() 60 | { 61 | if (m_Key != nullptr) 62 | { 63 | ZwClose(m_Key); 64 | } 65 | } 66 | 67 | protected: 68 | NTSTATUS QuerySubkeyInfo(ULONG Index, 69 | KEY_INFORMATION_CLASS InfoClass, 70 | CWdmMemoryBuffer &Buffer); 71 | 72 | NTSTATUS QueryValueInfo(const UNICODE_STRING &ValueName, 73 | KEY_VALUE_INFORMATION_CLASS InfoClass, 74 | CWdmMemoryBuffer &Buffer); 75 | 76 | HANDLE m_Key = nullptr; 77 | }; 78 | -------------------------------------------------------------------------------- /UsbDk/Resource.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daynix/UsbDk/af46b7ee3302413d2533c492fac2aa154cfd3d70/UsbDk/Resource.rc -------------------------------------------------------------------------------- /UsbDk/Trace.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | // 25 | // Define the tracing flags. 26 | // 27 | // Tracing GUID - 88e1661f-48b6-410f-b096-ba84e9f0656f 28 | // 29 | 30 | #define WPP_CONTROL_GUIDS \ 31 | WPP_DEFINE_CONTROL_GUID( \ 32 | UsbDkTraceGuid, (88e1661f,48b6,410f,b096,ba84e9f0656f), \ 33 | \ 34 | WPP_DEFINE_BIT(MYDRIVER_ALL_INFO) \ 35 | WPP_DEFINE_BIT(TRACE_DRIVER) \ 36 | WPP_DEFINE_BIT(TRACE_DEVICE) \ 37 | WPP_DEFINE_BIT(TRACE_FILTER) \ 38 | WPP_DEFINE_BIT(TRACE_DEVACCESS) \ 39 | WPP_DEFINE_BIT(TRACE_REGTEXT) \ 40 | WPP_DEFINE_BIT(TRACE_CONTROLDEVICE) \ 41 | WPP_DEFINE_BIT(TRACE_HIDERDEVICE) \ 42 | WPP_DEFINE_BIT(TRACE_FILTERDEVICE) \ 43 | WPP_DEFINE_BIT(TRACE_WDFDEVICE) \ 44 | WPP_DEFINE_BIT(TRACE_REDIRECTOR) \ 45 | WPP_DEFINE_BIT(TRACE_HIDER) \ 46 | WPP_DEFINE_BIT(TRACE_UTILS) \ 47 | WPP_DEFINE_BIT(TRACE_USBTARGET) \ 48 | WPP_DEFINE_BIT(TRACE_FILTERSTRATEGY) \ 49 | WPP_DEFINE_BIT(TRACE_URB) \ 50 | WPP_DEFINE_BIT(TRACE_REGISTRY) \ 51 | WPP_DEFINE_BIT(TRACE_WDFREQUEST) \ 52 | ) 53 | 54 | #define WPP_FLAG_LEVEL_LOGGER(flag, level) \ 55 | WPP_LEVEL_LOGGER(flag) 56 | 57 | #define WPP_FLAG_LEVEL_ENABLED(flag, level) \ 58 | (WPP_LEVEL_ENABLED(flag) && \ 59 | WPP_CONTROL(WPP_BIT_ ## flag).Level >= level) 60 | 61 | #define WPP_LEVEL_FLAGS_LOGGER(lvl,flags) \ 62 | WPP_LEVEL_LOGGER(flags) 63 | 64 | #define WPP_LEVEL_FLAGS_ENABLED(lvl, flags) \ 65 | (WPP_LEVEL_ENABLED(flags) && WPP_CONTROL(WPP_BIT_ ## flags).Level >= lvl) 66 | 67 | // 68 | // This comment block is scanned by the trace preprocessor to define our 69 | // Trace function. 70 | // 71 | // begin_wpp config 72 | // FUNC Trace{FLAG=MYDRIVER_ALL_INFO}(LEVEL, MSG, ...); 73 | // FUNC TraceEvents(LEVEL, FLAGS, MSG, ...); 74 | // CUSTOM_TYPE(devprop, ItemEnum(DEVICE_REGISTRY_PROPERTY)); 75 | // CUSTOM_TYPE(devid, ItemEnum(BUS_QUERY_ID_TYPE)); 76 | // CUSTOM_TYPE(pipetype, ItemEnum(_WDF_USB_PIPE_TYPE)); 77 | // CUSTOM_TYPE(usbdktransfertype, ItemEnum(USB_DK_TRANSFER_TYPE)); 78 | // CUSTOM_TYPE(usbdktransferdirection, ItemEnum(UsbDkTransferDirection)); 79 | // end_wpp 80 | // 81 | -------------------------------------------------------------------------------- /UsbDk/Urb.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "Urb.h" 26 | #include "Alloc.h" 27 | #include "UsbDkUtil.h" 28 | #include "Trace.h" 29 | #include "Urb.tmh" 30 | 31 | NTSTATUS CIsochronousUrb::FillOffsetsArray(size_t NumberOfPackets, PULONG64 PacketSizes, size_t TransferBufferSize) 32 | { 33 | ULONG CurrOffset = 0; 34 | 35 | for (size_t i = 0; i < NumberOfPackets; i++) 36 | { 37 | m_Urb->UrbIsochronousTransfer.IsoPacket[i].Offset = CurrOffset; 38 | CurrOffset += static_cast(PacketSizes[i]); 39 | } 40 | 41 | m_Urb->UrbIsochronousTransfer.TransferBufferLength = CurrOffset; 42 | 43 | return (TransferBufferSize < m_Urb->UrbIsochronousTransfer.TransferBufferLength) ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS; 44 | } 45 | 46 | NTSTATUS CIsochronousUrb::Create(Direction TransferDirection, PVOID TransferBuffer, size_t TransferBufferSize, size_t NumberOfPackets, PULONG64 PacketSizes) 47 | { 48 | WDF_OBJECT_ATTRIBUTES attributes; 49 | WDF_OBJECT_ATTRIBUTES_INIT(&attributes); 50 | attributes.ParentObject = m_Parent; 51 | 52 | auto status = WdfUsbTargetDeviceCreateIsochUrb(m_TargetDevice, &attributes, static_cast(NumberOfPackets), &m_UrbMemoryHandle, &m_Urb); 53 | if (!NT_SUCCESS(status)) 54 | { 55 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_URB, "%!FUNC! failed: %!STATUS!", status); 56 | return status; 57 | } 58 | 59 | auto UrbSize = GET_ISO_URB_SIZE(NumberOfPackets); 60 | if (UrbSize > USHORT_MAX) 61 | { 62 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_URB, "%!FUNC! failed: too much packets"); 63 | return STATUS_BUFFER_OVERFLOW; 64 | } 65 | 66 | m_Urb->UrbIsochronousTransfer.Hdr.Length = static_cast(UrbSize); 67 | 68 | m_Urb->UrbIsochronousTransfer.Hdr.Function = URB_FUNCTION_ISOCH_TRANSFER; 69 | m_Urb->UrbIsochronousTransfer.PipeHandle = WdfUsbTargetPipeWdmGetPipeHandle(m_TargetPipe); 70 | m_Urb->UrbIsochronousTransfer.TransferFlags = TransferDirection | USBD_START_ISO_TRANSFER_ASAP; 71 | 72 | m_Urb->UrbIsochronousTransfer.TransferBuffer = TransferBuffer; 73 | m_Urb->UrbIsochronousTransfer.NumberOfPackets = static_cast(NumberOfPackets); 74 | 75 | if (TransferDirection == URB_DIRECTION_OUT) 76 | { 77 | // initialize Length with initial packet length 78 | // USB controller driver may override it (Win7) 79 | for (size_t i = 0; i < NumberOfPackets; i++) 80 | { 81 | m_Urb->UrbIsochronousTransfer.IsoPacket[i].Length = static_cast(PacketSizes[i]); 82 | } 83 | } 84 | 85 | return FillOffsetsArray(NumberOfPackets, PacketSizes, TransferBufferSize); 86 | } 87 | -------------------------------------------------------------------------------- /UsbDk/Urb.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | class CIsochronousUrb 27 | { 28 | public: 29 | CIsochronousUrb(WDFUSBDEVICE TargetDevice, WDFUSBPIPE TargetPipe, WDFOBJECT ParentObject) 30 | : m_TargetDevice(TargetDevice) 31 | , m_TargetPipe(TargetPipe) 32 | , m_Parent(ParentObject) 33 | {} 34 | 35 | typedef enum 36 | { 37 | URB_DIRECTION_IN = USBD_TRANSFER_DIRECTION_IN, 38 | URB_DIRECTION_OUT = USBD_TRANSFER_DIRECTION_OUT 39 | } Direction; 40 | 41 | NTSTATUS Create(Direction TransferDirection, PVOID Transferbuffer, size_t TransferBufferSize, size_t NumberOfPackets, PULONG64 PacketSizes); 42 | operator WDFMEMORY() const { return m_UrbMemoryHandle; } 43 | 44 | private: 45 | NTSTATUS FillOffsetsArray(size_t NumberOfPackets, PULONG64 PacketSizes, size_t TransferBufferSize); 46 | 47 | WDFUSBDEVICE m_TargetDevice; 48 | WDFUSBPIPE m_TargetPipe; 49 | WDFOBJECT m_Parent; 50 | PURB m_Urb = nullptr; 51 | WDFMEMORY m_UrbMemoryHandle = WDF_NO_HANDLE; 52 | 53 | CIsochronousUrb(const CIsochronousUrb&) = delete; 54 | CIsochronousUrb& operator= (const CIsochronousUrb&) = delete; 55 | }; 56 | -------------------------------------------------------------------------------- /UsbDk/UsbDk.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Off 5 | 6 | 7 | Off 8 | 9 | 10 | Off 11 | 12 | 13 | Off 14 | 15 | 16 | Off 17 | 18 | 19 | Off 20 | 21 | 22 | Off 23 | 24 | 25 | Off 26 | 27 | 28 | Off 29 | 30 | 31 | Off 32 | 33 | 34 | Off 35 | 36 | 37 | Off 38 | 39 | 40 | Off 41 | 42 | 43 | Off 44 | 45 | 46 | Off 47 | 48 | 49 | Off 50 | 51 | 52 | Off 53 | 54 | 55 | Off 56 | 57 | 58 | Off 59 | 60 | 61 | Off 62 | 63 | -------------------------------------------------------------------------------- /UsbDk/UsbDkCompat.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | 26 | #if TARGET_OS_WIN_XP 27 | NTSTATUS WdfUsbTargetDeviceCreateIsochUrb(WDFUSBDEVICE UsbDevice, PWDF_OBJECT_ATTRIBUTES Attributes, ULONG NumberOfIsochPackets, 28 | WDFMEMORY* UrbMemory, PURB *Urb) 29 | { 30 | UNREFERENCED_PARAMETER(UsbDevice); 31 | 32 | size_t size = GET_ISO_URB_SIZE(NumberOfIsochPackets); 33 | auto status = WdfMemoryCreate(Attributes, USBDK_NON_PAGED_POOL, 'SBSU', size, UrbMemory, (PVOID*)Urb); 34 | if (NT_SUCCESS(status)) 35 | { 36 | RtlZeroMemory(*Urb, size); 37 | } 38 | 39 | return status; 40 | } 41 | 42 | extern "C" 43 | { 44 | 45 | #ifdef _WIN64 46 | NTSTATUS __guard_check_icall_fptr(...) 47 | { 48 | return STATUS_SUCCESS; 49 | } 50 | 51 | NTSTATUS __guard_dispatch_icall_fptr(...) 52 | { 53 | return STATUS_SUCCESS; 54 | } 55 | #else 56 | NTSTATUS _cdecl __guard_check_icall_fptr(...) 57 | { 58 | return STATUS_SUCCESS; 59 | } 60 | #endif 61 | } 62 | #endif 63 | -------------------------------------------------------------------------------- /UsbDk/UsbDkCompat.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #if TARGET_OS_WIN_XP 27 | 28 | #define _In_ 29 | #define _In_opt_ 30 | #define _Out_ 31 | #define _Out_opt_ 32 | #define _Inout_ 33 | #define _IRQL_requires_same_ 34 | #define _IRQL_requires_max_(X) 35 | #define _Inout_updates_bytes_(X) 36 | #define _Must_inspect_result_ 37 | #define _Outptr_opt_result_bytebuffer_(X) 38 | 39 | #define ObReferenceObjectWithTag(O, T) ObReferenceObject(O) 40 | #define ObDereferenceObjectWithTag(O, T) ObDereferenceObject(O) 41 | 42 | typedef struct _WDF_USB_DEVICE_CREATE_CONFIG 43 | { 44 | } WDF_USB_DEVICE_CREATE_CONFIG, PWDF_USB_DEVICE_CREATE_CONFIG; 45 | 46 | #define WDF_USB_DEVICE_CREATE_CONFIG_INIT(X, Y) \ 47 | UNREFERENCED_PARAMETER(X) 48 | 49 | #define WdfUsbTargetDeviceCreateWithParameters(Device, Config, Attributes, UsbDevice) \ 50 | WdfUsbTargetDeviceCreate(Device, Attributes, UsbDevice) 51 | 52 | #define wcsncpy_s(T, S, N) wcsncpy(T, S, N) 53 | 54 | NTSTATUS WdfUsbTargetDeviceCreateIsochUrb(WDFUSBDEVICE UsbDevice, PWDF_OBJECT_ATTRIBUTES Attributes, ULONG NumberOfIsochPackets, 55 | WDFMEMORY* UrbMemory, PURB *Urb); 56 | 57 | #define _When_(expr, annotes) __allowed(on_parameter_or_return) 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /UsbDk/UsbDkData.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | typedef struct tag_USB_DK_DEVICE_ID 27 | { 28 | WCHAR DeviceID[MAX_DEVICE_ID_LEN]; 29 | WCHAR InstanceID[MAX_DEVICE_ID_LEN]; 30 | } USB_DK_DEVICE_ID, *PUSB_DK_DEVICE_ID; 31 | 32 | static inline 33 | void UsbDkFillIDStruct(USB_DK_DEVICE_ID *ID, PCWCHAR DeviceID, PCWCHAR InstanceID) 34 | { 35 | wcsncpy_s(ID->DeviceID, DeviceID, MAX_DEVICE_ID_LEN); 36 | wcsncpy_s(ID->InstanceID, InstanceID, MAX_DEVICE_ID_LEN); 37 | } 38 | 39 | typedef struct tag_USB_DK_DEVICE_INFO 40 | { 41 | USB_DK_DEVICE_ID ID; 42 | ULONG64 FilterID; 43 | ULONG64 Port; 44 | ULONG64 Speed; 45 | USB_DEVICE_DESCRIPTOR DeviceDescriptor; 46 | } USB_DK_DEVICE_INFO, *PUSB_DK_DEVICE_INFO; 47 | 48 | typedef struct tag_USB_DK_CONFIG_DESCRIPTOR_REQUEST 49 | { 50 | USB_DK_DEVICE_ID ID; 51 | ULONG64 Index; 52 | } USB_DK_CONFIG_DESCRIPTOR_REQUEST, *PUSB_DK_CONFIG_DESCRIPTOR_REQUEST; 53 | 54 | typedef struct tag_USB_DK_ISO_TRANSFER_RESULT 55 | { 56 | ULONG64 ActualLength; 57 | ULONG64 TransferResult; 58 | } USB_DK_ISO_TRANSFER_RESULT, *PUSB_DK_ISO_TRANSFER_RESULT; 59 | 60 | typedef struct tag_USB_DK_GEN_TRANSFER_RESULT 61 | { 62 | ULONG64 BytesTransferred; 63 | ULONG64 UsbdStatus; // USBD_STATUS code 64 | } USB_DK_GEN_TRANSFER_RESULT, *PUSB_DK_GEN_TRANSFER_RESULT; 65 | 66 | typedef struct tag_USB_DK_TRANSFER_RESULT 67 | { 68 | USB_DK_GEN_TRANSFER_RESULT GenResult; 69 | PVOID64 IsochronousResultsArray; // array of USB_DK_ISO_TRANSFER_RESULT 70 | } USB_DK_TRANSFER_RESULT, *PUSB_DK_TRANSFER_RESULT; 71 | 72 | typedef struct tag_USB_DK_TRANSFER_REQUEST 73 | { 74 | ULONG64 EndpointAddress; 75 | PVOID64 Buffer; 76 | ULONG64 BufferLength; 77 | ULONG64 TransferType; 78 | ULONG64 IsochronousPacketsArraySize; 79 | PVOID64 IsochronousPacketsArray; 80 | 81 | USB_DK_TRANSFER_RESULT Result; 82 | } USB_DK_TRANSFER_REQUEST, *PUSB_DK_TRANSFER_REQUEST; 83 | 84 | typedef enum 85 | { 86 | TransferFailure = 0, 87 | TransferSuccess, 88 | TransferSuccessAsync 89 | } TransferResult; 90 | 91 | typedef enum 92 | { 93 | NoSpeed = 0, 94 | LowSpeed, 95 | FullSpeed, 96 | HighSpeed, 97 | SuperSpeed 98 | } USB_DK_DEVICE_SPEED; 99 | 100 | typedef enum 101 | { 102 | ControlTransferType, 103 | BulkTransferType, 104 | InterruptTransferType, 105 | IsochronousTransferType 106 | } USB_DK_TRANSFER_TYPE; 107 | -------------------------------------------------------------------------------- /UsbDk/UsbDkDataHider.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Kirill Moizik 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "UsbDkDataHiderPublic.h" 27 | 28 | #ifdef __cplusplus 29 | class USB_DK_HIDE_RULE : public USB_DK_HIDE_RULE_PUBLIC 30 | { 31 | public: 32 | USB_DK_HIDE_RULE(PUSB_DK_HIDE_RULE_PUBLIC PublicRule = NULL, ULONG RuleType = USBDK_HIDER_RULE_DEFAULT) : 33 | Type(RuleType) 34 | { 35 | if (PublicRule) 36 | { 37 | Hide = PublicRule->Hide; 38 | Class = PublicRule->Class; 39 | VID = PublicRule->VID; 40 | PID = PublicRule->PID; 41 | BCD = PublicRule->BCD; 42 | } 43 | } 44 | ULONG64 Type; 45 | }; 46 | 47 | typedef USB_DK_HIDE_RULE *PUSB_DK_HIDE_RULE; 48 | #endif 49 | -------------------------------------------------------------------------------- /UsbDk/UsbDkDataHiderPublic.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Kirill Moizik 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #define USB_DK_HIDE_RULE_MATCH_ALL ((ULONG64)(-1)) 27 | 28 | typedef struct tag_USB_DK_HIDE_RULE_PUBLIC 29 | { 30 | ULONG64 Hide; 31 | ULONG64 Class; 32 | ULONG64 VID; 33 | ULONG64 PID; 34 | ULONG64 BCD; 35 | } USB_DK_HIDE_RULE_PUBLIC, *PUSB_DK_HIDE_RULE_PUBLIC; 36 | 37 | #define USBDK_HIDER_RULE_DEFAULT 0 38 | #define USBDK_HIDER_RULE_DETERMINATIVE_TYPES 1 39 | -------------------------------------------------------------------------------- /UsbDk/UsbDkNames.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #define USBDK_DRIVER_NAME TEXT("UsbDk") 27 | #define USBDK_DRIVER_FILE_NAME USBDK_DRIVER_NAME TEXT(".sys") 28 | #define USBDK_DRIVER_INF_NAME USBDK_DRIVER_NAME TEXT(".inf") 29 | #define USBDK_DEVICE_NAME TEXT("\\Device\\") USBDK_DRIVER_NAME 30 | #define USBDK_DOSDEVICE_NAME TEXT("\\DosDevices\\") USBDK_DRIVER_NAME 31 | #define USBDK_USERMODE_NAME TEXT("\\\\.\\") USBDK_DRIVER_NAME 32 | 33 | #define _USBDK_HIDER_NAME TEXT("UsbDkHider") 34 | #define USBDK_HIDER_DEVICE_NAME TEXT("\\Device\\") _USBDK_HIDER_NAME 35 | #define USBDK_DOS_HIDER_DEVICE_NAME TEXT("\\DosDevices\\") _USBDK_HIDER_NAME 36 | #define USBDK_USERMODE_HIDER_NAME TEXT("\\\\.\\") _USBDK_HIDER_NAME 37 | -------------------------------------------------------------------------------- /UsbDk/WdfRequest.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | #include "stdafx.h" 24 | #include "Trace.h" 25 | #include "WdfRequest.tmh" 26 | #include "WdfRequest.h" 27 | 28 | NTSTATUS CWdfRequest::SendAndForget(WDFIOTARGET Target) 29 | { 30 | WDF_REQUEST_SEND_OPTIONS options; 31 | WDF_REQUEST_SEND_OPTIONS_INIT(&options, WDF_REQUEST_SEND_OPTION_SEND_AND_FORGET); 32 | 33 | NTSTATUS status = STATUS_SUCCESS; 34 | 35 | if (WdfRequestSend(m_Request, Target, &options)) 36 | { 37 | Detach(); 38 | } 39 | else 40 | { 41 | status = WdfRequestGetStatus(m_Request); 42 | SetStatus(status); 43 | } 44 | 45 | return status; 46 | } 47 | 48 | NTSTATUS CWdfRequest::ForwardToIoQueue(WDFQUEUE Destination) 49 | { 50 | NTSTATUS status = WdfRequestForwardToIoQueue(m_Request, Destination); 51 | 52 | if (NT_SUCCESS(status)) 53 | { 54 | Detach(); 55 | } 56 | else 57 | { 58 | SetStatus(status); 59 | } 60 | 61 | return status; 62 | } 63 | 64 | NTSTATUS CWdfRequest::SendWithCompletion(WDFIOTARGET Target, PFN_WDF_REQUEST_COMPLETION_ROUTINE CompletionFunc, WDFCONTEXT CompletionContext) 65 | { 66 | auto status = STATUS_SUCCESS; 67 | 68 | WdfRequestSetCompletionRoutine(m_Request, CompletionFunc, CompletionContext); 69 | if (WdfRequestSend(m_Request, Target, WDF_NO_SEND_OPTIONS)) 70 | { 71 | Detach(); 72 | } 73 | else 74 | { 75 | status = WdfRequestGetStatus(m_Request); 76 | SetStatus(status); 77 | } 78 | 79 | return status; 80 | } 81 | 82 | NTSTATUS CWdfRequest::LockUserBufferForRead(PVOID Ptr, size_t Length, WDFMEMORY &Buffer) const 83 | { 84 | if (Length == 0) 85 | { 86 | Buffer = WDF_NO_HANDLE; 87 | return STATUS_SUCCESS; 88 | } 89 | 90 | auto status = WdfRequestProbeAndLockUserBufferForRead(m_Request, Ptr, Length, &Buffer); 91 | 92 | if (!NT_SUCCESS(status)) 93 | { 94 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_WDFREQUEST, 95 | "%!FUNC! Failed for address %p, %llu bytes. Error %!STATUS!\n", 96 | Ptr, Length, status); 97 | } 98 | 99 | return status; 100 | } 101 | 102 | NTSTATUS CWdfRequest::LockUserBufferForWrite(PVOID Ptr, size_t Length, WDFMEMORY &Buffer) const 103 | { 104 | if (Length == 0) 105 | { 106 | Buffer = WDF_NO_HANDLE; 107 | return STATUS_SUCCESS; 108 | } 109 | 110 | auto status = WdfRequestProbeAndLockUserBufferForWrite(m_Request, Ptr, Length, &Buffer); 111 | 112 | if (!NT_SUCCESS(status)) 113 | { 114 | TraceEvents(TRACE_LEVEL_ERROR, TRACE_WDFREQUEST, 115 | "%!FUNC! Failed for address %p, %llu bytes. Error %!STATUS!\n", 116 | Ptr, Length, status); 117 | } 118 | 119 | return status; 120 | } 121 | -------------------------------------------------------------------------------- /UsbDk/WdfWorkitem.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "WdfWorkitem.h" 26 | 27 | CWdfWorkitem::~CWdfWorkitem() 28 | { 29 | if (m_hWorkItem != WDF_NO_HANDLE) 30 | { 31 | WdfObjectDelete(m_hWorkItem); 32 | } 33 | } 34 | 35 | typedef struct tag_WdfWorkItemContext 36 | { 37 | CWdfWorkitem* workItem; 38 | } WdfWorkItemContext; 39 | 40 | WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(WdfWorkItemContext, GetWdfWorkItemContext); 41 | 42 | VOID CWdfWorkitem::Callback(_In_ WDFWORKITEM WorkItem) 43 | { 44 | auto* ctx = GetWdfWorkItemContext(WorkItem); 45 | ctx->workItem->m_Payload(ctx->workItem->m_PayloadCtx); 46 | } 47 | 48 | NTSTATUS CWdfWorkitem::Create(WDFOBJECT parent) 49 | { 50 | WDF_OBJECT_ATTRIBUTES attributes; 51 | WDF_WORKITEM_CONFIG workitemConfig; 52 | 53 | WDF_OBJECT_ATTRIBUTES_INIT(&attributes); 54 | WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&attributes, WdfWorkItemContext); 55 | attributes.ParentObject = parent; 56 | WDF_WORKITEM_CONFIG_INIT(&workitemConfig, Callback); 57 | 58 | auto status = WdfWorkItemCreate(&workitemConfig, &attributes, &m_hWorkItem); 59 | auto ctx = GetWdfWorkItemContext(m_hWorkItem); 60 | ctx->workItem = this; 61 | 62 | return status; 63 | } 64 | 65 | VOID CWdfWorkitem::Enqueue() 66 | { 67 | ASSERT(m_hWorkItem != WDF_NO_HANDLE); 68 | 69 | WdfWorkItemEnqueue(m_hWorkItem); 70 | } 71 | -------------------------------------------------------------------------------- /UsbDk/WdfWorkitem.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "Alloc.h" 27 | 28 | class CWdfWorkitem : public CAllocatable 29 | { 30 | private: 31 | typedef VOID(*PayloadFunc)(PVOID Ctx); 32 | 33 | public: 34 | CWdfWorkitem(PayloadFunc payload, PVOID payloadCtx) 35 | : m_Payload(payload) 36 | , m_PayloadCtx(payloadCtx) 37 | {} 38 | 39 | CWdfWorkitem(const CWdfWorkitem&) = delete; 40 | CWdfWorkitem& operator= (const CWdfWorkitem&) = delete; 41 | 42 | ~CWdfWorkitem(); 43 | 44 | NTSTATUS Create(WDFOBJECT parent); 45 | VOID Enqueue(); 46 | 47 | private: 48 | WDFWORKITEM m_hWorkItem = WDF_NO_HANDLE; 49 | static VOID Callback(_In_ WDFWORKITEM WorkItem); 50 | 51 | PayloadFunc m_Payload; 52 | PVOID m_PayloadCtx; 53 | }; 54 | -------------------------------------------------------------------------------- /UsbDk/stdafx.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | -------------------------------------------------------------------------------- /UsbDk/stdafx.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if WINVER < 0x0600 4 | #define TARGET_OS_WIN_XP (1) 5 | #endif 6 | 7 | #if TARGET_OS_WIN_XP 8 | #define WINDOWS_ENABLE_CPLUSPLUS 9 | #pragma warning(push,3) 10 | #endif 11 | 12 | extern "C" 13 | { 14 | #include 15 | #include 16 | #include 17 | 18 | #if !TARGET_OS_WIN_XP 19 | #include 20 | #include 21 | #include 22 | #else 23 | #define USB_DEVICE_CLASS_AUDIO 0x01 24 | #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02 25 | #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03 26 | #define USB_DEVICE_CLASS_PRINTER 0x07 27 | #define USB_DEVICE_CLASS_STORAGE 0x08 28 | #define USB_DEVICE_CLASS_HUB 0x09 29 | #define USB_DEVICE_CLASS_CDC_DATA 0x0A 30 | #define USB_DEVICE_CLASS_VIDEO 0x0E 31 | #define USB_DEVICE_CLASS_AUDIO_VIDEO 0x10 32 | #define USB_DEVICE_CLASS_WIRELESS_CONTROLLER 0xE0 33 | #endif 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | } 40 | 41 | #if TARGET_OS_WIN_XP 42 | #pragma warning(pop) 43 | #endif 44 | 45 | #if !TARGET_OS_WIN_XP && (NTDDI_VERSION >= NTDDI_WIN8) 46 | #define USBDK_NON_PAGED_POOL NonPagedPoolNx 47 | #else 48 | #define USBDK_NON_PAGED_POOL NonPagedPool 49 | #endif 50 | 51 | #include "UsbDkCompat.h" 52 | -------------------------------------------------------------------------------- /UsbDkController/Resource.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daynix/UsbDk/af46b7ee3302413d2533c492fac2aa154cfd3d70/UsbDkController/Resource.rc -------------------------------------------------------------------------------- /UsbDkController/UsbDkController.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | 37 | 38 | Resource Files 39 | 40 | 41 | -------------------------------------------------------------------------------- /UsbDkController/stdafx.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | -------------------------------------------------------------------------------- /UsbDkController/stdafx.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "targetver.h" 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef _USING_V110_SDK71_ 37 | #include 38 | #else 39 | #include "UsbDkCompat.h" 40 | #endif 41 | 42 | #include "tstrings.h" 43 | -------------------------------------------------------------------------------- /UsbDkController/targetver.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | // Including SDKDDKVer.h defines the highest available Windows platform. 27 | 28 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 29 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 30 | 31 | #include 32 | -------------------------------------------------------------------------------- /UsbDkHelper/DeviceMgr.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "DeviceMgr.h" 26 | 27 | #include 28 | 29 | bool DeviceMgr::ResetDeviceByClass(const GUID &ClassGuid) 30 | { 31 | auto hDevInfo = SetupDiGetClassDevsEx(&ClassGuid, nullptr, nullptr, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT, nullptr, nullptr, nullptr); 32 | if (hDevInfo == INVALID_HANDLE_VALUE) 33 | { 34 | throw UsbDkDeviceMgrException(TEXT("SetupDiGetClassDevsEx() failed!!")); 35 | } 36 | 37 | SP_DEVINFO_DATA devInfo; 38 | devInfo.cbSize = sizeof(devInfo); 39 | for (DWORD devIndex = 0; SetupDiEnumDeviceInfo(hDevInfo, devIndex, &devInfo); devIndex++) 40 | { 41 | if (!ResetDevice(hDevInfo, &devInfo)) 42 | { 43 | return false; 44 | } 45 | } 46 | 47 | SetupDiDestroyDeviceInfoList(hDevInfo); 48 | 49 | return true; 50 | } 51 | 52 | bool DeviceMgr::ResetDevice(HDEVINFO devs, PSP_DEVINFO_DATA devInfo) 53 | { 54 | SP_PROPCHANGE_PARAMS pcParams; 55 | pcParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER); 56 | pcParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; 57 | pcParams.StateChange = DICS_PROPCHANGE; 58 | pcParams.Scope = DICS_FLAG_CONFIGSPECIFIC; 59 | pcParams.HwProfile = 0; 60 | 61 | if (!SetupDiSetClassInstallParams(devs, devInfo, &pcParams.ClassInstallHeader, sizeof(pcParams)) || 62 | !SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, devs, devInfo)) 63 | { 64 | return false; 65 | } 66 | else 67 | { 68 | SP_DEVINSTALL_PARAMS devInstallParams; 69 | devInstallParams.cbSize = sizeof(devInstallParams); 70 | if (SetupDiGetDeviceInstallParams(devs, devInfo, &devInstallParams) && (devInstallParams.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT))) 71 | { 72 | return false; 73 | } 74 | } 75 | 76 | return true; 77 | } 78 | -------------------------------------------------------------------------------- /UsbDkHelper/DeviceMgr.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include 27 | #include "UsbDkHelper.h" 28 | 29 | #define DEVICE_MANAGER_EXCEPTION_STRING TEXT("DeviceMgr exception: ") 30 | 31 | class UsbDkDeviceMgrException : public UsbDkW32ErrorException 32 | { 33 | public: 34 | UsbDkDeviceMgrException() : UsbDkW32ErrorException(DEVICE_MANAGER_EXCEPTION_STRING){} 35 | UsbDkDeviceMgrException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(DEVICE_MANAGER_EXCEPTION_STRING) + lpzMessage){} 36 | UsbDkDeviceMgrException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(DEVICE_MANAGER_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} 37 | UsbDkDeviceMgrException(tstring errMsg) : UsbDkW32ErrorException(tstring(DEVICE_MANAGER_EXCEPTION_STRING) + errMsg){} 38 | UsbDkDeviceMgrException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(DEVICE_MANAGER_EXCEPTION_STRING) + errMsg, dwErrorCode){} 39 | }; 40 | 41 | class DeviceMgr 42 | { 43 | public: 44 | static bool ResetDeviceByClass(const GUID &ClassGuid); 45 | private: 46 | static bool ResetDevice(HDEVINFO devs, PSP_DEVINFO_DATA devInfo); 47 | }; 48 | -------------------------------------------------------------------------------- /UsbDkHelper/DriverAccess.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "DriverAccess.h" 26 | #include "Public.h" 27 | 28 | 29 | void UsbDkDriverAccess::GetDevicesList(PUSB_DK_DEVICE_INFO &DevicesArray, ULONG &DeviceNumber) 30 | { 31 | DevicesArray = nullptr; 32 | DWORD bytesReturned; 33 | 34 | unique_ptr Result; 35 | 36 | do 37 | { 38 | // get number of devices 39 | Ioctl(IOCTL_USBDK_COUNT_DEVICES, false, nullptr, 0, 40 | &DeviceNumber, sizeof(DeviceNumber)); 41 | 42 | if (DeviceNumber == 0) 43 | { 44 | DevicesArray = nullptr; 45 | return; 46 | } 47 | 48 | // allocate storage for device list 49 | Result.reset(new USB_DK_DEVICE_INFO[DeviceNumber]); 50 | 51 | } while (!Ioctl(IOCTL_USBDK_ENUM_DEVICES, true, nullptr, 0, 52 | Result.get(), DeviceNumber * sizeof(USB_DK_DEVICE_INFO), 53 | &bytesReturned)); 54 | 55 | DeviceNumber = bytesReturned / sizeof(USB_DK_DEVICE_INFO); 56 | DevicesArray = Result.release(); 57 | } 58 | 59 | void UsbDkDriverAccess::ReleaseConfigurationDescriptor(PUSB_CONFIGURATION_DESCRIPTOR Descriptor) 60 | { 61 | delete[] Descriptor; 62 | } 63 | 64 | void UsbDkDriverAccess::ReleaseDevicesList(PUSB_DK_DEVICE_INFO DevicesArray) 65 | { 66 | delete[] DevicesArray; 67 | } 68 | 69 | PUSB_CONFIGURATION_DESCRIPTOR UsbDkDriverAccess::GetConfigurationDescriptor(USB_DK_CONFIG_DESCRIPTOR_REQUEST &Request, ULONG &Length) 70 | { 71 | USB_CONFIGURATION_DESCRIPTOR ShortDescriptor; 72 | 73 | Ioctl(IOCTL_USBDK_GET_CONFIG_DESCRIPTOR, true, &Request, sizeof(Request), 74 | &ShortDescriptor, sizeof(ShortDescriptor)); 75 | 76 | Length = ShortDescriptor.wTotalLength; 77 | 78 | auto FullDescriptor = reinterpret_cast(new BYTE[Length]); 79 | 80 | Ioctl(IOCTL_USBDK_GET_CONFIG_DESCRIPTOR, false, &Request, sizeof(Request), 81 | FullDescriptor, Length); 82 | 83 | return FullDescriptor; 84 | } 85 | 86 | void UsbDkDriverAccess::UpdateRegistryParameters() 87 | { 88 | Ioctl(IOCTL_USBDK_UPDATE_REG_PARAMETERS); 89 | } 90 | 91 | HANDLE UsbDkDriverAccess::AddRedirect(USB_DK_DEVICE_ID &DeviceID) 92 | { 93 | ULONG64 RedirectorHandle; 94 | SendIoctlWithDeviceId(IOCTL_USBDK_ADD_REDIRECT, DeviceID, &RedirectorHandle); 95 | return reinterpret_cast(RedirectorHandle); 96 | } 97 | 98 | void UsbDkHiderAccess::AddHideRule(const USB_DK_HIDE_RULE &Rule) 99 | { 100 | Ioctl(IOCTL_USBDK_ADD_HIDE_RULE, false, const_cast(&Rule), sizeof(Rule)); 101 | } 102 | 103 | void UsbDkHiderAccess::ClearHideRules() 104 | { 105 | Ioctl(IOCTL_USBDK_CLEAR_HIDE_RULES); 106 | } 107 | -------------------------------------------------------------------------------- /UsbDkHelper/DriverAccess.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "UsbDkData.h" 27 | #include "UsbDkDataHider.h" 28 | #include "DriverFile.h" 29 | #include "UsbDkNames.h" 30 | 31 | #define DRIVER_ACCESS_EXCEPTION_STRING TEXT("Driver operation error. ") 32 | 33 | class UsbDkDriverAccessException : public UsbDkW32ErrorException 34 | { 35 | public: 36 | UsbDkDriverAccessException() : UsbDkW32ErrorException(DRIVER_ACCESS_EXCEPTION_STRING){} 37 | UsbDkDriverAccessException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(DRIVER_ACCESS_EXCEPTION_STRING) + lpzMessage){} 38 | UsbDkDriverAccessException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(DRIVER_ACCESS_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} 39 | UsbDkDriverAccessException(tstring errMsg) : UsbDkW32ErrorException(tstring(DRIVER_ACCESS_EXCEPTION_STRING) + errMsg){} 40 | UsbDkDriverAccessException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(DRIVER_ACCESS_EXCEPTION_STRING) + errMsg, dwErrorCode){} 41 | }; 42 | class UsbDkDriverAccess : public UsbDkDriverFile 43 | { 44 | public: 45 | UsbDkDriverAccess() 46 | : UsbDkDriverFile(USBDK_USERMODE_NAME) 47 | {} 48 | 49 | void GetDevicesList(PUSB_DK_DEVICE_INFO &DevicesArray, ULONG &NumberDevice); 50 | PUSB_CONFIGURATION_DESCRIPTOR GetConfigurationDescriptor(USB_DK_CONFIG_DESCRIPTOR_REQUEST &Request, ULONG &Length); 51 | void UpdateRegistryParameters(); 52 | static void ReleaseDevicesList(PUSB_DK_DEVICE_INFO DevicesArray); 53 | static void ReleaseConfigurationDescriptor(PUSB_CONFIGURATION_DESCRIPTOR Descriptor); 54 | 55 | HANDLE AddRedirect(USB_DK_DEVICE_ID &DeviceID); 56 | 57 | private: 58 | template 59 | void SendIoctlWithDeviceId(DWORD ControlCode, USB_DK_DEVICE_ID &Id, TOutputObj* Output = nullptr) 60 | { 61 | Ioctl(ControlCode, false, &Id, sizeof(Id), 62 | Output, (Output != nullptr) ? sizeof(*Output) : 0); 63 | } 64 | }; 65 | 66 | class UsbDkHiderAccess : public UsbDkDriverFile 67 | { 68 | public: 69 | UsbDkHiderAccess() 70 | : UsbDkDriverFile(USBDK_USERMODE_HIDER_NAME) 71 | {} 72 | 73 | void AddHideRule(const USB_DK_HIDE_RULE &Rule); 74 | void ClearHideRules(); 75 | }; 76 | -------------------------------------------------------------------------------- /UsbDkHelper/DriverFile.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "DriverFile.h" 26 | 27 | 28 | UsbDkDriverFile::UsbDkDriverFile(LPCTSTR lpFileName, bool bOverlapped) 29 | { 30 | m_bOverlapped = bOverlapped; 31 | 32 | m_hDriver = CreateFile(lpFileName, 33 | GENERIC_READ | GENERIC_WRITE, 34 | 0, 35 | nullptr, 36 | OPEN_EXISTING, 37 | FILE_ATTRIBUTE_NORMAL | (bOverlapped ? FILE_FLAG_OVERLAPPED : 0), 38 | nullptr); 39 | 40 | if (m_hDriver == INVALID_HANDLE_VALUE) 41 | { 42 | throw UsbDkDriverFileException(tstring(TEXT("Failed to open device symlink ")) + lpFileName); 43 | } 44 | } 45 | 46 | TransferResult UsbDkDriverFile::Ioctl(DWORD Code, 47 | bool ShortBufferOk, 48 | LPVOID InBuffer, 49 | DWORD InBufferSize, 50 | LPVOID OutBuffer, 51 | DWORD OutBufferSize, 52 | LPDWORD BytesReturned, 53 | LPOVERLAPPED Overlapped) 54 | { 55 | DWORD InternalBytesReturned; 56 | LPDWORD InternalBytesReturnedPtr = (BytesReturned != nullptr) ? BytesReturned : &InternalBytesReturned; 57 | if (!DeviceIoControl(m_hDriver, Code, 58 | InBuffer, InBufferSize, 59 | OutBuffer, OutBufferSize, 60 | InternalBytesReturnedPtr, Overlapped)) 61 | { 62 | auto err = GetLastError(); 63 | if (m_bOverlapped && (err == ERROR_IO_PENDING)) 64 | { 65 | // If driver was open without FILE_FLAG_OVERLAPPED, DeviceIoControl can't return ERROR_IO_PENDING, 66 | // so the caller of Ioctl can check return result as boolean 67 | return TransferSuccessAsync; 68 | } 69 | if (ShortBufferOk && (err == ERROR_MORE_DATA)) 70 | { 71 | return TransferFailure; 72 | } 73 | 74 | throw UsbDkDriverFileException(TEXT("DeviceIoControl failed")); 75 | } 76 | 77 | return TransferSuccess; 78 | } 79 | 80 | TransferResult UsbDkDriverFile::Read(LPVOID Buffer, 81 | DWORD BufferSize, 82 | LPDWORD BytesRead, 83 | LPOVERLAPPED Overlapped) 84 | { 85 | if (!ReadFile(m_hDriver, Buffer, BufferSize, BytesRead, Overlapped)) 86 | { 87 | if (m_bOverlapped && (GetLastError() == ERROR_IO_PENDING)) 88 | { 89 | return TransferSuccessAsync; 90 | } 91 | throw UsbDkDriverFileException(TEXT("ReadFile failed")); 92 | } 93 | return TransferSuccess; 94 | } 95 | 96 | TransferResult UsbDkDriverFile::Write(LPVOID Buffer, 97 | DWORD BufferSize, 98 | LPDWORD BytesWritten, 99 | LPOVERLAPPED Overlapped) 100 | { 101 | if (!WriteFile(m_hDriver, Buffer, BufferSize, BytesWritten, Overlapped)) 102 | { 103 | if (m_bOverlapped && (GetLastError() == ERROR_IO_PENDING)) 104 | { 105 | return TransferSuccessAsync; 106 | } 107 | throw UsbDkDriverFileException(TEXT("WriteFile failed")); 108 | } 109 | return TransferSuccess; 110 | } 111 | -------------------------------------------------------------------------------- /UsbDkHelper/DriverFile.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "UsbDkData.h" 27 | 28 | #define DRIVER_FILE_EXCEPTION_STRING TEXT("Driver file operation error. ") 29 | 30 | class UsbDkDriverFileException : public UsbDkW32ErrorException 31 | { 32 | public: 33 | UsbDkDriverFileException() : UsbDkW32ErrorException(DRIVER_FILE_EXCEPTION_STRING){} 34 | UsbDkDriverFileException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(DRIVER_FILE_EXCEPTION_STRING) + lpzMessage){} 35 | UsbDkDriverFileException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(DRIVER_FILE_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} 36 | UsbDkDriverFileException(tstring errMsg) : UsbDkW32ErrorException(tstring(DRIVER_FILE_EXCEPTION_STRING) + errMsg){} 37 | UsbDkDriverFileException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(DRIVER_FILE_EXCEPTION_STRING) + errMsg, dwErrorCode){} 38 | }; 39 | class UsbDkDriverFile 40 | { 41 | public: 42 | UsbDkDriverFile(LPCTSTR lpFileName, bool bOverlapped = false); 43 | UsbDkDriverFile(HANDLE ObjectHandle, bool bOverlapped = false) 44 | : m_bOverlapped(bOverlapped), m_hDriver(ObjectHandle) 45 | {} 46 | virtual ~UsbDkDriverFile() 47 | { CloseHandle(m_hDriver); } 48 | 49 | TransferResult Ioctl(DWORD Code, 50 | bool ShortBufferOk = false, 51 | LPVOID InBuffer = nullptr, 52 | DWORD InBufferSize = 0, 53 | LPVOID OutBuffer = nullptr, 54 | DWORD OutBufferSize = 0, 55 | LPDWORD BytesReturned = nullptr, 56 | LPOVERLAPPED Overlapped = nullptr); 57 | 58 | TransferResult Read(LPVOID Buffer, 59 | DWORD BufferSize, 60 | LPDWORD BytesRead, 61 | LPOVERLAPPED Overlapped = nullptr); 62 | 63 | TransferResult Write(LPVOID Buffer, 64 | DWORD BufferSize, 65 | LPDWORD BytesWritten, 66 | LPOVERLAPPED Overlapped = nullptr); 67 | 68 | protected: 69 | HANDLE m_hDriver; 70 | 71 | private: 72 | bool m_bOverlapped; 73 | }; 74 | -------------------------------------------------------------------------------- /UsbDkHelper/Exception.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "tstrings.h" 27 | 28 | using std::exception; 29 | 30 | class UsbDkException : public exception 31 | { 32 | public: 33 | UsbDkException(); 34 | UsbDkException(LPCTSTR lpzMessage); 35 | UsbDkException(const tstring &Message); 36 | UsbDkException(const UsbDkException &Other); 37 | virtual ~UsbDkException(); 38 | 39 | virtual const char *what() const; 40 | virtual LPCTSTR twhat() const; 41 | 42 | protected: 43 | void SetMessage(const tstring &Message); 44 | 45 | private: 46 | tstring m_Message; 47 | string m_MBCSMessage; 48 | }; 49 | 50 | class UsbDkNumErrorException : public UsbDkException 51 | { 52 | public: 53 | UsbDkNumErrorException(LPCTSTR lpzDescription, DWORD dwErrorCode); 54 | UsbDkNumErrorException(const tstring &Description, DWORD dwErrorCode); 55 | UsbDkNumErrorException(const UsbDkNumErrorException& Other); 56 | 57 | DWORD GetErrorCode(void) const { return m_dwErrorCode; } 58 | 59 | protected: 60 | DWORD m_dwErrorCode; 61 | }; 62 | 63 | class UsbDkCRTErrorException : public UsbDkNumErrorException 64 | { 65 | public: 66 | UsbDkCRTErrorException(int nErrorCode = errno); 67 | UsbDkCRTErrorException(LPCTSTR lpzDescription, int nErrorCode = errno); 68 | UsbDkCRTErrorException(const tstring &Description, int nErrorCode = errno); 69 | UsbDkCRTErrorException(const UsbDkCRTErrorException &Other); 70 | 71 | protected: 72 | static tstring GetErrorString(DWORD dwErrorCode); 73 | }; 74 | 75 | #ifdef WIN32 76 | class UsbDkW32ErrorException : public UsbDkNumErrorException 77 | { 78 | public: 79 | UsbDkW32ErrorException(DWORD dwErrorCode = GetLastError()); 80 | UsbDkW32ErrorException(LPCTSTR lpzDescription, DWORD dwErrorCode = GetLastError()); 81 | UsbDkW32ErrorException(const tstring &Description, DWORD dwErrorCode = GetLastError()); 82 | UsbDkW32ErrorException(const UsbDkW32ErrorException &Other); 83 | 84 | protected: 85 | static tstring GetErrorString(DWORD dwErrorCode); 86 | }; 87 | #endif 88 | -------------------------------------------------------------------------------- /UsbDkHelper/GuidGen.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "GuidGen.h" 3 | 4 | CGuid::CGuid() 5 | { 6 | UUID Uuid; 7 | auto res = UuidCreate(&Uuid); 8 | if ((res != RPC_S_OK) && (res != RPC_S_UUID_LOCAL_ONLY)) 9 | { 10 | throw GuidGenException(res); 11 | } 12 | 13 | RPC_TSTR UuidStr; 14 | res = UuidToString(&Uuid, &UuidStr); 15 | if (res != RPC_S_OK) 16 | { 17 | throw GuidGenException(res); 18 | } 19 | 20 | m_GuidStr = reinterpret_cast(UuidStr); 21 | RpcStringFree(&UuidStr); 22 | } -------------------------------------------------------------------------------- /UsbDkHelper/GuidGen.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #define GUID_EXCEPTION_STRING TEXT("GUID creation failure.") 3 | 4 | class GuidGenException : public UsbDkNumErrorException 5 | { 6 | public: 7 | explicit GuidGenException(RPC_STATUS dwErrorCode) 8 | : UsbDkNumErrorException(GUID_EXCEPTION_STRING, dwErrorCode) 9 | {} 10 | }; 11 | 12 | class CGuid 13 | { 14 | public: 15 | CGuid(); 16 | 17 | operator const tstring&() const 18 | { return m_GuidStr; } 19 | 20 | operator LPCTSTR() const 21 | {return m_GuidStr.c_str(); } 22 | private: 23 | tstring m_GuidStr; 24 | }; 25 | -------------------------------------------------------------------------------- /UsbDkHelper/Installer.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "Exception.h" 27 | #include "WdfCoinstaller.h" 28 | #include "UsbDkHelper.h" 29 | #include "RegAccess.h" 30 | 31 | #define INSTALLER_EXCEPTION_STRING TEXT("UsbDkInstaller exception: ") 32 | 33 | class UsbDkInstallerFailedException : public UsbDkW32ErrorException 34 | { 35 | public: 36 | UsbDkInstallerFailedException() : UsbDkW32ErrorException(INSTALLER_EXCEPTION_STRING){} 37 | UsbDkInstallerFailedException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(INSTALLER_EXCEPTION_STRING) + lpzMessage){} 38 | UsbDkInstallerFailedException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(INSTALLER_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} 39 | UsbDkInstallerFailedException(tstring errMsg) : UsbDkW32ErrorException(tstring(INSTALLER_EXCEPTION_STRING) + errMsg){} 40 | UsbDkInstallerFailedException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(INSTALLER_EXCEPTION_STRING) + errMsg, dwErrorCode){} 41 | }; 42 | 43 | class UsbDkInstallerAbortedException : public UsbDkInstallerFailedException 44 | { 45 | public: 46 | UsbDkInstallerAbortedException() : UsbDkInstallerFailedException() {} 47 | }; 48 | 49 | class UsbDkInstaller 50 | { 51 | public: 52 | 53 | UsbDkInstaller(); 54 | 55 | bool Install(bool &NeedRollBack); 56 | void Uninstall(); 57 | 58 | protected: 59 | private: 60 | 61 | WdfCoinstaller m_wdfCoinstaller; 62 | ServiceManager m_scManager; 63 | UsbDkRegAccess m_regAccess; 64 | 65 | tstring CopyDriver(); 66 | void DeleteDriver(); 67 | tstring buildDriverPath(const tstring &DriverFileName); 68 | tstring buildInfFilePath(); 69 | 70 | void addUsbDkToRegistry(); 71 | void removeUsbDkFromRegistry(); 72 | void buildStringListFromVector(tstringlist &filtersList, vector &valVector); 73 | void buildNewListWithoutEement(tstringlist &newfiltersList, tstringlist &filtersList, tstring element); 74 | void buildMultiStringVectorFromList(vector &valVector, tstringlist &newfiltersList); 75 | void validatePlatform(); 76 | bool isWow64B(); 77 | void verifyDriverCanStart(); 78 | 79 | }; 80 | -------------------------------------------------------------------------------- /UsbDkHelper/RedirectorAccess.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "RedirectorAccess.h" 26 | #include "Public.h" 27 | 28 | TransferResult UsbDkRedirectorAccess::TransactPipe(USB_DK_TRANSFER_REQUEST &Request, 29 | DWORD OpCode, 30 | LPOVERLAPPED Overlapped) 31 | { 32 | static DWORD BytesTransferredDummy; 33 | 34 | auto res = Ioctl(OpCode, false, 35 | &Request, sizeof(Request), 36 | &Request.Result.GenResult, sizeof(Request.Result.GenResult), 37 | &BytesTransferredDummy, Overlapped); 38 | 39 | return res; 40 | } 41 | 42 | TransferResult UsbDkRedirectorAccess::ReadPipe(USB_DK_TRANSFER_REQUEST &Request, 43 | LPOVERLAPPED Overlapped) 44 | { 45 | return TransactPipe(Request, IOCTL_USBDK_DEVICE_READ_PIPE, Overlapped); 46 | } 47 | 48 | TransferResult UsbDkRedirectorAccess::WritePipe(USB_DK_TRANSFER_REQUEST &Request, 49 | LPOVERLAPPED Overlapped) 50 | { 51 | return TransactPipe(Request, IOCTL_USBDK_DEVICE_WRITE_PIPE, Overlapped); 52 | } 53 | 54 | void UsbDkRedirectorAccess::AbortPipe(ULONG64 PipeAddress) 55 | { 56 | IoctlSync(IOCTL_USBDK_DEVICE_ABORT_PIPE, false, &PipeAddress, sizeof(PipeAddress)); 57 | } 58 | 59 | void UsbDkRedirectorAccess::ResetPipe(ULONG64 PipeAddress) 60 | { 61 | IoctlSync(IOCTL_USBDK_DEVICE_RESET_PIPE, false, &PipeAddress, sizeof(PipeAddress)); 62 | } 63 | 64 | void UsbDkRedirectorAccess::SetAltsetting(ULONG64 InterfaceIdx, ULONG64 AltSettingIdx) 65 | { 66 | USBDK_ALTSETTINGS_IDXS AltSetting; 67 | AltSetting.InterfaceIdx = InterfaceIdx; 68 | AltSetting.AltSettingIdx = AltSettingIdx; 69 | 70 | IoctlSync(IOCTL_USBDK_DEVICE_SET_ALTSETTING, false, &AltSetting, sizeof(AltSetting)); 71 | } 72 | 73 | void UsbDkRedirectorAccess::ResetDevice() 74 | { 75 | IoctlSync(IOCTL_USBDK_DEVICE_RESET_DEVICE); 76 | } 77 | 78 | bool UsbDkRedirectorAccess::IoctlSync(DWORD Code, 79 | bool ShortBufferOk, 80 | LPVOID InBuffer, 81 | DWORD InBufferSize, 82 | LPVOID OutBuffer, 83 | DWORD OutBufferSize, 84 | LPDWORD BytesReturned) 85 | { 86 | UsbDkHandleHolder Event(CreateEvent(nullptr, FALSE, FALSE, nullptr)); 87 | if (!Event) 88 | { 89 | throw UsbDkDriverFileException(TEXT("CreateEvent failed")); 90 | } 91 | 92 | OVERLAPPED Overlapped; 93 | Overlapped.hEvent = Event; 94 | auto res = Ioctl(Code, ShortBufferOk, InBuffer, InBufferSize, OutBuffer, OutBufferSize, BytesReturned, &Overlapped); 95 | 96 | switch (res) 97 | { 98 | case TransferSuccessAsync: 99 | DWORD NumberOfBytesTransferred; 100 | if (!GetOverlappedResult(m_hDriver, &Overlapped, &NumberOfBytesTransferred, TRUE)) 101 | { 102 | throw UsbDkDriverFileException(TEXT("GetOverlappedResult failed")); 103 | } 104 | return true; 105 | case TransferSuccess: 106 | return true; 107 | case TransferFailure: 108 | return false; 109 | } 110 | 111 | return false; 112 | } 113 | -------------------------------------------------------------------------------- /UsbDkHelper/RedirectorAccess.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "UsbDkData.h" 27 | #include "DriverFile.h" 28 | #include "UsbDkNames.h" 29 | 30 | #define REDIRECTOR_ACCESS_EXCEPTION_STRING TEXT("Redirection operation error. ") 31 | 32 | class UsbDkRedirectorAccessException : public UsbDkW32ErrorException 33 | { 34 | public: 35 | UsbDkRedirectorAccessException() : UsbDkW32ErrorException(REDIRECTOR_ACCESS_EXCEPTION_STRING){} 36 | UsbDkRedirectorAccessException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(REDIRECTOR_ACCESS_EXCEPTION_STRING) + lpzMessage){} 37 | UsbDkRedirectorAccessException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(REDIRECTOR_ACCESS_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} 38 | UsbDkRedirectorAccessException(tstring errMsg) : UsbDkW32ErrorException(tstring(REDIRECTOR_ACCESS_EXCEPTION_STRING) + errMsg){} 39 | UsbDkRedirectorAccessException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(REDIRECTOR_ACCESS_EXCEPTION_STRING) + errMsg, dwErrorCode){} 40 | }; 41 | class UsbDkRedirectorAccess : public UsbDkDriverFile 42 | { 43 | public: 44 | UsbDkRedirectorAccess(HANDLE ObjectHandle) 45 | :UsbDkDriverFile(ObjectHandle, true) 46 | {} 47 | 48 | TransferResult ReadPipe(USB_DK_TRANSFER_REQUEST &Request, LPOVERLAPPED Overlapped); 49 | TransferResult WritePipe(USB_DK_TRANSFER_REQUEST &Request, LPOVERLAPPED Overlapped); 50 | void AbortPipe(ULONG64 PipeAddress); 51 | void ResetPipe(ULONG64 PipeAddress); 52 | void SetAltsetting(ULONG64 InterfaceIdx, ULONG64 AltSettingIdx); 53 | void ResetDevice(); 54 | 55 | HANDLE GetSystemHandle() const 56 | { return m_hDriver; } 57 | 58 | private: 59 | 60 | TransferResult TransactPipe(USB_DK_TRANSFER_REQUEST &Request, 61 | DWORD OpCode, 62 | LPOVERLAPPED Overlapped); 63 | 64 | bool IoctlSync(DWORD Code, 65 | bool ShortBufferOk = false, 66 | LPVOID InBuffer = nullptr, 67 | DWORD InBufferSize = 0, 68 | LPVOID OutBuffer = nullptr, 69 | DWORD OutBufferSize = 0, 70 | LPDWORD BytesReturned = nullptr); 71 | }; 72 | -------------------------------------------------------------------------------- /UsbDkHelper/Resource.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daynix/UsbDk/af46b7ee3302413d2533c492fac2aa154cfd3d70/UsbDkHelper/Resource.rc -------------------------------------------------------------------------------- /UsbDkHelper/RuleManager.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "UsbDkDataHider.h" 3 | #include "UsbDkNames.h" 4 | #include "HideRulesRegPublic.h" 5 | #include "RegAccess.h" 6 | #include "RuleManager.h" 7 | #include "GuidGen.h" 8 | 9 | CRulesManager::CRulesManager() 10 | : m_RegAccess(HKEY_LOCAL_MACHINE, USBDK_HIDE_RULES_PATH) 11 | {} 12 | 13 | static bool operator == (const USB_DK_HIDE_RULE& r1, const USB_DK_HIDE_RULE& r2) 14 | { 15 | return (r1.VID == r2.VID) && 16 | (r1.PID == r2.PID) && 17 | (r1.BCD == r2.BCD) && 18 | (r1.Class == r2.Class) && 19 | (r1.Hide == r2.Hide) && 20 | (r1.Type == r2.Type); 21 | } 22 | 23 | DWORD CRulesManager::ReadDword(LPCTSTR RuleName, LPCTSTR ValueName) const 24 | { 25 | DWORD RawValue; 26 | 27 | if (!m_RegAccess.ReadDWord(ValueName, &RawValue, RuleName)) 28 | { 29 | tstring ErrorText = tstring(TEXT("Failed to read rule ")) + ValueName; 30 | throw UsbDkRuleManagerException(ErrorText, ERROR_FUNCTION_FAILED); 31 | } 32 | 33 | return RawValue; 34 | } 35 | 36 | void CRulesManager::WriteDword(const tstring &RuleName, LPCTSTR ValueName, ULONG Value) 37 | { 38 | if (!m_RegAccess.WriteValue(ValueName, Value, RuleName.c_str())) 39 | { 40 | tstring ErrorText = tstring(TEXT("Failed to write rule ")) + ValueName; 41 | throw UsbDkRuleManagerException(ErrorText, ERROR_FUNCTION_FAILED); 42 | } 43 | } 44 | 45 | ULONG64 CRulesManager::ReadDwordMask(LPCTSTR RuleName, LPCTSTR ValueName) const 46 | { 47 | return HideRuleUlongMaskFromRegistry(ReadDword(RuleName, ValueName)); 48 | } 49 | 50 | ULONG64 CRulesManager::ReadBool(LPCTSTR RuleName, LPCTSTR ValueName) const 51 | { 52 | return HideRuleBoolFromRegistry(ReadDword(RuleName, ValueName)); 53 | } 54 | 55 | void CRulesManager::ReadRule(LPCTSTR RuleName, USB_DK_HIDE_RULE &Rule) const 56 | { 57 | Rule.Type = ReadDword(RuleName, USBDK_HIDE_RULE_TYPE); 58 | Rule.Hide = ReadBool(RuleName, USBDK_HIDE_RULE_SHOULD_HIDE); 59 | Rule.VID = ReadDwordMask(RuleName, USBDK_HIDE_RULE_VID); 60 | Rule.PID = ReadDwordMask(RuleName, USBDK_HIDE_RULE_PID); 61 | Rule.BCD = ReadDwordMask(RuleName, USBDK_HIDE_RULE_BCD); 62 | Rule.Class = ReadDwordMask(RuleName, USBDK_HIDE_RULE_CLASS); 63 | } 64 | 65 | template 66 | bool CRulesManager::FindRule(const USB_DK_HIDE_RULE &Rule, TFunctor Functor) 67 | { 68 | for (const auto &SubKey : m_RegAccess) 69 | { 70 | try 71 | { 72 | USB_DK_HIDE_RULE ExistingRule; 73 | ReadRule(SubKey, ExistingRule); 74 | 75 | if (Rule == ExistingRule) 76 | { 77 | Functor(SubKey); 78 | return true; 79 | } 80 | } 81 | catch (const UsbDkRuleManagerException &e) 82 | { 83 | auto ErrorText = tstring(TEXT("Error while processing rule ")) + 84 | SubKey + TEXT(": ") + string2tstring(e.what()); 85 | OutputDebugString(ErrorText.c_str()); 86 | } 87 | } 88 | 89 | return false; 90 | } 91 | 92 | bool CRulesManager::RuleExists(const USB_DK_HIDE_RULE &Rule) 93 | { 94 | return FindRule(Rule, [](LPCTSTR){}); 95 | } 96 | 97 | void CRulesManager::AddRule(const USB_DK_HIDE_RULE &Rule) 98 | { 99 | if (RuleExists(Rule)) 100 | { 101 | throw UsbDkRuleManagerException(TEXT("Rule already exists"), ERROR_FILE_EXISTS); 102 | } 103 | 104 | CGuid RuleName; 105 | 106 | if (!m_RegAccess.AddKey(RuleName)) 107 | { 108 | throw UsbDkRuleManagerException(TEXT("Failed to create rule key"), ERROR_FUNCTION_FAILED); 109 | } 110 | 111 | WriteDword(RuleName, USBDK_HIDE_RULE_TYPE, static_cast(Rule.Type)); 112 | WriteDword(RuleName, USBDK_HIDE_RULE_SHOULD_HIDE, static_cast(Rule.Hide)); 113 | WriteDword(RuleName, USBDK_HIDE_RULE_VID, static_cast(Rule.VID)); 114 | WriteDword(RuleName, USBDK_HIDE_RULE_PID, static_cast(Rule.PID)); 115 | WriteDword(RuleName, USBDK_HIDE_RULE_BCD, static_cast(Rule.BCD)); 116 | WriteDword(RuleName, USBDK_HIDE_RULE_CLASS, static_cast(Rule.Class)); 117 | } 118 | 119 | void CRulesManager::DeleteRule(const USB_DK_HIDE_RULE &Rule) 120 | { 121 | tstring RuleName; 122 | 123 | if (FindRule(Rule, [&RuleName](LPCTSTR Name){ RuleName = Name; })) 124 | { 125 | if (!m_RegAccess.DeleteKey(RuleName.c_str())) 126 | { 127 | throw UsbDkRuleManagerException(TEXT("Failed to delete rule key"), ERROR_FUNCTION_FAILED); 128 | } 129 | } 130 | } 131 | 132 | ULONG CRulesManager::DeleteAllRules(ULONG& notDeleted) 133 | { 134 | ULONG deleted = 0; 135 | notDeleted = 0; 136 | vector subkeys; 137 | 138 | for (const auto &SubKey : m_RegAccess) 139 | subkeys.push_back(SubKey); 140 | 141 | while (subkeys.size()) 142 | { 143 | m_RegAccess.DeleteKey(subkeys.front().c_str()) ? deleted++ : notDeleted++; 144 | subkeys.erase(subkeys.begin()); 145 | } 146 | return deleted; 147 | } 148 | -------------------------------------------------------------------------------- /UsbDkHelper/RuleManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #define RULE_MANAGER_EXCEPTION_STRING TEXT("RuleManager exception: ") 3 | 4 | class UsbDkRuleManagerException : public UsbDkW32ErrorException 5 | { 6 | public: 7 | UsbDkRuleManagerException() : UsbDkW32ErrorException(RULE_MANAGER_EXCEPTION_STRING){} 8 | UsbDkRuleManagerException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(RULE_MANAGER_EXCEPTION_STRING) + lpzMessage){} 9 | UsbDkRuleManagerException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(RULE_MANAGER_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} 10 | UsbDkRuleManagerException(tstring errMsg) : UsbDkW32ErrorException(tstring(RULE_MANAGER_EXCEPTION_STRING) + errMsg){} 11 | UsbDkRuleManagerException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(RULE_MANAGER_EXCEPTION_STRING) + errMsg, dwErrorCode){} 12 | }; 13 | 14 | class CRulesManager 15 | { 16 | public: 17 | CRulesManager(); 18 | 19 | void AddRule(const USB_DK_HIDE_RULE &Rule); 20 | void DeleteRule(const USB_DK_HIDE_RULE &Rule); 21 | ULONG DeleteAllRules(ULONG& notDeleted); 22 | private: 23 | template 24 | bool FindRule(const USB_DK_HIDE_RULE &Rule, TFunctor Functor); 25 | bool RuleExists(const USB_DK_HIDE_RULE &Rule); 26 | 27 | DWORD ReadDword(LPCTSTR RuleName, LPCTSTR ValueName) const; 28 | ULONG64 ReadDwordMask(LPCTSTR RuleName, LPCTSTR ValueName) const; 29 | ULONG64 ReadBool(LPCTSTR RuleName, LPCTSTR ValueName) const; 30 | void WriteDword(const tstring &RuleName, LPCTSTR ValueName, ULONG Value); 31 | 32 | void ReadRule(LPCTSTR RuleName, USB_DK_HIDE_RULE &Rule) const; 33 | 34 | UsbDkRegAccess m_RegAccess; 35 | }; 36 | -------------------------------------------------------------------------------- /UsbDkHelper/ServiceManager.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | 26 | 27 | ServiceManager::ServiceManager() 28 | :m_schSCManager(OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS)) 29 | { 30 | if (!m_schSCManager) 31 | { 32 | throw UsbDkServiceManagerFailedException(TEXT("OpenSCManager failed")); 33 | } 34 | } 35 | 36 | void ServiceManager::CreateServiceObject(const tstring &ServiceName, const tstring &ServicePath) 37 | { 38 | assert(m_schSCManager); 39 | 40 | SCMHandleHolder schService(CreateService(m_schSCManager, ServiceName.c_str(), ServiceName.c_str(), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, 41 | SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, ServicePath.c_str(), nullptr, nullptr, nullptr, nullptr, nullptr)); 42 | if (!schService) 43 | { 44 | throw UsbDkServiceManagerFailedException(TEXT("CreateService failed")); 45 | } 46 | } 47 | 48 | void ServiceManager::DeleteServiceObject(const tstring &ServiceName) 49 | { 50 | SCMHandleHolder schService(OpenServiceObject(ServiceName)); 51 | WaitForServiceStop(schService); 52 | if (!DeleteService(schService)) 53 | { 54 | throw UsbDkServiceManagerFailedException(TEXT("DeleteService failed")); 55 | } 56 | } 57 | 58 | void ServiceManager::StartServiceObject(const tstring &ServiceName) 59 | { 60 | SCMHandleHolder schService(OpenServiceObject(ServiceName)); 61 | 62 | if (!StartService(SC_HANDLE(schService), 0, NULL)) 63 | { 64 | throw UsbDkServiceManagerFailedException(TEXT("StartService failed")); 65 | } 66 | return; 67 | } 68 | 69 | void ServiceManager::StopServiceObject(const tstring & ServiceName) 70 | { 71 | SCMHandleHolder schService(OpenServiceObject(ServiceName)); 72 | WaitForServiceStop(schService); 73 | } 74 | 75 | void ServiceManager::WaitForServiceStop(const SCMHandleHolder &schService) 76 | { 77 | static const DWORD SERVICE_STOP_WAIT_QUANTUM = 50; 78 | static const DWORD SERVICE_STOP_ITERATIONS = 20000 / 50; //Total timeout is 20 seconds 79 | 80 | SERVICE_STATUS_PROCESS ssp; 81 | DWORD iterationNumber = 0; 82 | 83 | do 84 | { 85 | Sleep(SERVICE_STOP_WAIT_QUANTUM); 86 | 87 | DWORD bytesNeeded; 88 | if (!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &bytesNeeded)) 89 | { 90 | throw UsbDkServiceManagerFailedException(TEXT("QueryServiceStatusEx failed")); 91 | } 92 | } while ((ssp.dwCurrentState != SERVICE_STOPPED) && (iterationNumber++ < SERVICE_STOP_ITERATIONS)); 93 | } 94 | 95 | SC_HANDLE ServiceManager::OpenServiceObject(const tstring & ServiceName) 96 | { 97 | assert(m_schSCManager); 98 | 99 | SC_HANDLE sch = OpenService(m_schSCManager, ServiceName.c_str(), SERVICE_ALL_ACCESS); 100 | if (!sch) 101 | { 102 | auto err = GetLastError(); 103 | throw UsbDkServiceManagerFailedException(TEXT("OpenService failed"), err); 104 | } 105 | return sch; 106 | } 107 | -------------------------------------------------------------------------------- /UsbDkHelper/ServiceManager.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #define SERVICE_MANAGER_EXCEPTION_STRING TEXT("ServiceManager exception: ") 27 | 28 | class UsbDkServiceManagerFailedException : public UsbDkW32ErrorException 29 | { 30 | public: 31 | UsbDkServiceManagerFailedException() : UsbDkW32ErrorException(SERVICE_MANAGER_EXCEPTION_STRING){} 32 | UsbDkServiceManagerFailedException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(SERVICE_MANAGER_EXCEPTION_STRING) + lpzMessage){} 33 | UsbDkServiceManagerFailedException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(SERVICE_MANAGER_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} 34 | UsbDkServiceManagerFailedException(tstring errMsg) : UsbDkW32ErrorException(tstring(SERVICE_MANAGER_EXCEPTION_STRING) + errMsg){} 35 | UsbDkServiceManagerFailedException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(SERVICE_MANAGER_EXCEPTION_STRING) + errMsg, dwErrorCode){} 36 | }; 37 | 38 | class ServiceManager 39 | { 40 | public: 41 | ServiceManager(); 42 | 43 | void CreateServiceObject(const tstring &ServiceName, const tstring &ServicePath); 44 | void DeleteServiceObject(const tstring &ServiceName); 45 | void StartServiceObject(const tstring &ServiceName); 46 | void StopServiceObject(const tstring &ServiceName); 47 | 48 | private: 49 | typedef UsbDkHandleHolder SCMHandleHolder; 50 | 51 | static void WaitForServiceStop(const SCMHandleHolder &schService); 52 | SC_HANDLE OpenServiceObject(const tstring & ServiceName); 53 | 54 | SCMHandleHolder m_schSCManager; 55 | }; 56 | -------------------------------------------------------------------------------- /UsbDkHelper/UsbDk.inf: -------------------------------------------------------------------------------- 1 | ; UsbDk filter/redirector driver INF file 2 | ; 3 | ; Copyright (c) 2013 Red Hat, Inc. 4 | ; 5 | ; Authors: 6 | ; Dmitry Fleytman 7 | ; 8 | 9 | [Version] 10 | Signature="$WINDOWS NT$" 11 | 12 | [UsbDk.NT.Wdf] 13 | KmdfService = UsbDk, UsbDk_Service_kmdfInst 14 | 15 | [UsbDk_Service_kmdfInst] 16 | KmdfLibraryVersion = 1.11 17 | -------------------------------------------------------------------------------- /UsbDkHelper/UsbDkCompat.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include 27 | 28 | typedef struct _USB_CONFIGURATION_DESCRIPTOR { 29 | UCHAR bLength; 30 | UCHAR bDescriptorType; 31 | USHORT wTotalLength; 32 | UCHAR bNumInterfaces; 33 | UCHAR bConfigurationValue; 34 | UCHAR iConfiguration; 35 | UCHAR bmAttributes; 36 | UCHAR MaxPower; 37 | } USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR; 38 | 39 | typedef struct _USB_DEVICE_DESCRIPTOR { 40 | UCHAR bLength; 41 | UCHAR bDescriptorType; 42 | USHORT bcdUSB; 43 | UCHAR bDeviceClass; 44 | UCHAR bDeviceSubClass; 45 | UCHAR bDeviceProtocol; 46 | UCHAR bMaxPacketSize0; 47 | USHORT idVendor; 48 | USHORT idProduct; 49 | USHORT bcdDevice; 50 | UCHAR iManufacturer; 51 | UCHAR iProduct; 52 | UCHAR iSerialNumber; 53 | UCHAR bNumConfigurations; 54 | } USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR; 55 | 56 | #include 57 | 58 | C_ASSERT(sizeof(USB_CONFIGURATION_DESCRIPTOR) == 9); 59 | -------------------------------------------------------------------------------- /UsbDkHelper/UsbDkHelper.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files 41 | 42 | 43 | Source Files 44 | 45 | 46 | Source Files 47 | 48 | 49 | Source Files 50 | 51 | 52 | Source Files 53 | 54 | 55 | Source Files 56 | 57 | 58 | Source Files 59 | 60 | 61 | Source Files 62 | 63 | 64 | Source Files 65 | 66 | 67 | 68 | 69 | Header Files 70 | 71 | 72 | Header Files 73 | 74 | 75 | Header Files 76 | 77 | 78 | Header Files 79 | 80 | 81 | Header Files 82 | 83 | 84 | Header Files 85 | 86 | 87 | Header Files 88 | 89 | 90 | Header Files 91 | 92 | 93 | Header Files 94 | 95 | 96 | Header Files 97 | 98 | 99 | Header Files 100 | 101 | 102 | Header Files 103 | 104 | 105 | Header Files 106 | 107 | 108 | Header Files 109 | 110 | 111 | Header Files 112 | 113 | 114 | Header Files 115 | 116 | 117 | Header Files 118 | 119 | 120 | Header Files 121 | 122 | 123 | 124 | 125 | Resource Files 126 | 127 | 128 | -------------------------------------------------------------------------------- /UsbDkHelper/UsbDkHlprCompat.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include 27 | 28 | typedef struct _USB_CONFIGURATION_DESCRIPTOR { 29 | UCHAR bLength; 30 | UCHAR bDescriptorType; 31 | USHORT wTotalLength; 32 | UCHAR bNumInterfaces; 33 | UCHAR bConfigurationValue; 34 | UCHAR iConfiguration; 35 | UCHAR bmAttributes; 36 | UCHAR MaxPower; 37 | } USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR; 38 | 39 | typedef struct _USB_DEVICE_DESCRIPTOR { 40 | UCHAR bLength; 41 | UCHAR bDescriptorType; 42 | USHORT bcdUSB; 43 | UCHAR bDeviceClass; 44 | UCHAR bDeviceSubClass; 45 | UCHAR bDeviceProtocol; 46 | UCHAR bMaxPacketSize0; 47 | USHORT idVendor; 48 | USHORT idProduct; 49 | USHORT bcdDevice; 50 | UCHAR iManufacturer; 51 | UCHAR iProduct; 52 | UCHAR iSerialNumber; 53 | UCHAR bNumConfigurations; 54 | } USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR; 55 | 56 | #include 57 | 58 | /* 3ABF6F2D-71C4-462a-8A92-1E6861E6AF27 */ 59 | DEFINE_GUID(GUID_DEVINTERFACE_USB_HOST_CONTROLLER, 0x3abf6f2d, 0x71c4, 0x462a, 0x8a, 0x92, 0x1e, \ 60 | 0x68, 0x61, 0xe6, 0xaf, 0x27); 61 | -------------------------------------------------------------------------------- /UsbDkHelper/UsbDk_XP.inf: -------------------------------------------------------------------------------- 1 | ; UsbDk filter/redirector driver INF file 2 | ; 3 | ; Copyright (c) 2013 Red Hat, Inc. 4 | ; 5 | ; Authors: 6 | ; Dmitry Fleytman 7 | ; 8 | 9 | [Version] 10 | Signature="$WINDOWS NT$" 11 | 12 | [UsbDk.NT.Wdf] 13 | KmdfService = UsbDk, UsbDk_Service_kmdfInst 14 | 15 | [UsbDk_Service_kmdfInst] 16 | KmdfLibraryVersion = 1.09 17 | -------------------------------------------------------------------------------- /UsbDkHelper/Utils.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | 26 | void UsbDkHandleHolder::Close() 27 | { 28 | CloseServiceHandle(m_Handle); 29 | } 30 | 31 | void UsbDkHandleHolder::Close() 32 | { 33 | CloseHandle(m_Handle); 34 | } 35 | -------------------------------------------------------------------------------- /UsbDkHelper/Utils.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | template 27 | class UsbDkHandleHolder 28 | { 29 | public: 30 | UsbDkHandleHolder(HandleT Handle) 31 | : m_Handle(Handle) 32 | {} 33 | 34 | ~UsbDkHandleHolder() 35 | { 36 | if (m_Handle != nullptr) 37 | { 38 | Close(); 39 | } 40 | } 41 | 42 | operator bool() const { return m_Handle != nullptr; } 43 | operator HandleT() const { return m_Handle; } 44 | 45 | private: 46 | void Close(); 47 | HandleT m_Handle; 48 | }; 49 | -------------------------------------------------------------------------------- /UsbDkHelper/WdfCoinstaller.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "WdfCoinstaller.h" 26 | #include "tstrings.h" 27 | 28 | 29 | #define WDF_SECTION_NAME TEXT("UsbDk.NT.Wdf") 30 | #if !TARGET_OS_WIN_XP 31 | #define COINSTALLER_VERSION TEXT("01011") 32 | #else 33 | #define COINSTALLER_VERSION TEXT("01009") 34 | #endif 35 | 36 | WdfCoinstaller::WdfCoinstaller() 37 | { 38 | loadWdfCoinstaller(); 39 | } 40 | 41 | WdfCoinstaller::~WdfCoinstaller() 42 | { 43 | freeWdfCoinstallerLibrary(); 44 | } 45 | 46 | void WdfCoinstaller::loadWdfCoinstaller() 47 | { 48 | TCHAR currDir[MAX_PATH]; 49 | if (GetCurrentDirectory(MAX_PATH, currDir) == 0) 50 | { 51 | throw UsbDkWdfCoinstallerFailedException(TEXT("GetCurrentDirectory() failed")); 52 | } 53 | 54 | tstringstream coinstallerFullPath; 55 | coinstallerFullPath << currDir << TEXT("\\WdfCoInstaller") << COINSTALLER_VERSION << TEXT(".dll"); 56 | 57 | m_wdfCoinstallerLibrary = LoadLibrary(coinstallerFullPath.str().c_str()); 58 | 59 | if (nullptr == m_wdfCoinstallerLibrary) 60 | { 61 | throw UsbDkWdfCoinstallerFailedException(tstring(TEXT("LoadLibrary(")) + coinstallerFullPath.str() + TEXT(") failed")); 62 | } 63 | 64 | try 65 | { 66 | m_pfnWdfPreDeviceInstallEx = getCoinstallerFunction("WdfPreDeviceInstallEx"); 67 | m_pfnWdfPostDeviceInstall = getCoinstallerFunction("WdfPostDeviceInstall"); 68 | m_pfnWdfPreDeviceRemove = getCoinstallerFunction("WdfPreDeviceRemove"); 69 | m_pfnWdfPostDeviceRemove = getCoinstallerFunction("WdfPostDeviceRemove"); 70 | } 71 | catch (...) 72 | { 73 | freeWdfCoinstallerLibrary(); 74 | throw; 75 | } 76 | } 77 | 78 | bool WdfCoinstaller::PreDeviceInstallEx(const tstring &infFilePath) 79 | { 80 | WDF_COINSTALLER_INSTALL_OPTIONS clientOptions; 81 | WDF_COINSTALLER_INSTALL_OPTIONS_INIT(&clientOptions); 82 | 83 | ULONG res = m_pfnWdfPreDeviceInstallEx(infFilePath.c_str(), WDF_SECTION_NAME, &clientOptions); 84 | 85 | if (res != ERROR_SUCCESS) 86 | { 87 | if (res == ERROR_SUCCESS_REBOOT_REQUIRED) 88 | { 89 | return false; 90 | } 91 | else 92 | { 93 | throw UsbDkWdfCoinstallerFailedException(TEXT("WdfPreDeviceInstallEx() failed"), res); 94 | } 95 | } 96 | 97 | return true; 98 | } 99 | 100 | void WdfCoinstaller::PostDeviceInstall(const tstring &infFilePath) 101 | { 102 | ULONG res = m_pfnWdfPostDeviceInstall(infFilePath.c_str(), WDF_SECTION_NAME); 103 | if (ERROR_SUCCESS != res) 104 | { 105 | throw UsbDkWdfCoinstallerFailedException(TEXT("WdfPostDeviceInstall() failed"), res); 106 | } 107 | } 108 | 109 | void WdfCoinstaller::PreDeviceRemove(const tstring &infFilePath) 110 | { 111 | ULONG res = m_pfnWdfPreDeviceRemove(infFilePath.c_str(), WDF_SECTION_NAME); 112 | if (ERROR_SUCCESS != res) 113 | { 114 | throw UsbDkWdfCoinstallerFailedException(TEXT("WdfPreDeviceRemove() failed"), res); 115 | } 116 | } 117 | 118 | void WdfCoinstaller::PostDeviceRemove(const tstring &infFilePath) 119 | { 120 | ULONG res = m_pfnWdfPostDeviceRemove(infFilePath.c_str(), WDF_SECTION_NAME); 121 | if (ERROR_SUCCESS != res) 122 | { 123 | throw UsbDkWdfCoinstallerFailedException(TEXT("WdfPostDeviceRemove() failed"), res); 124 | } 125 | } 126 | 127 | void WdfCoinstaller::freeWdfCoinstallerLibrary() 128 | { 129 | if (m_wdfCoinstallerLibrary) 130 | { 131 | FreeLibrary(m_wdfCoinstallerLibrary); 132 | m_wdfCoinstallerLibrary = nullptr; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /UsbDkHelper/WdfCoinstaller.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include 27 | 28 | #define WDF_COINSTALLER_EXCEPTION_STRING TEXT("WdfCoinstaller exception: ") 29 | 30 | class UsbDkWdfCoinstallerFailedException : public UsbDkW32ErrorException 31 | { 32 | public: 33 | UsbDkWdfCoinstallerFailedException() : UsbDkW32ErrorException(WDF_COINSTALLER_EXCEPTION_STRING){} 34 | UsbDkWdfCoinstallerFailedException(LPCTSTR lpzMessage) : UsbDkW32ErrorException(tstring(WDF_COINSTALLER_EXCEPTION_STRING) + lpzMessage){} 35 | UsbDkWdfCoinstallerFailedException(LPCTSTR lpzMessage, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(WDF_COINSTALLER_EXCEPTION_STRING) + lpzMessage, dwErrorCode){} 36 | UsbDkWdfCoinstallerFailedException(tstring errMsg) : UsbDkW32ErrorException(tstring(WDF_COINSTALLER_EXCEPTION_STRING) + errMsg){} 37 | UsbDkWdfCoinstallerFailedException(tstring errMsg, DWORD dwErrorCode) : UsbDkW32ErrorException(tstring(WDF_COINSTALLER_EXCEPTION_STRING) + errMsg, dwErrorCode){} 38 | }; 39 | class WdfCoinstaller 40 | { 41 | public: 42 | 43 | WdfCoinstaller(); 44 | virtual ~WdfCoinstaller(); 45 | 46 | bool PreDeviceInstallEx(const tstring &infFilePath); 47 | void PostDeviceInstall(const tstring &infFilePath); 48 | void PreDeviceRemove(const tstring &infFilePath); 49 | void PostDeviceRemove(const tstring &infFilePath); 50 | 51 | protected: 52 | private: 53 | HMODULE m_wdfCoinstallerLibrary; 54 | 55 | PFN_WDFPREDEVICEINSTALLEX m_pfnWdfPreDeviceInstallEx; 56 | PFN_WDFPOSTDEVICEINSTALL m_pfnWdfPostDeviceInstall; 57 | PFN_WDFPREDEVICEREMOVE m_pfnWdfPreDeviceRemove; 58 | PFN_WDFPOSTDEVICEREMOVE m_pfnWdfPostDeviceRemove; 59 | 60 | template< typename CallbackType> 61 | CallbackType getCoinstallerFunction(char *functionName) 62 | { 63 | auto ptfFunction = (CallbackType)GetProcAddress(m_wdfCoinstallerLibrary, functionName); 64 | if (ptfFunction == nullptr) 65 | { 66 | throw UsbDkWdfCoinstallerFailedException(TEXT("GetProcAddress() failed")); 67 | } 68 | 69 | return ptfFunction; 70 | } 71 | 72 | void loadWdfCoinstaller(); 73 | void freeWdfCoinstallerLibrary(); 74 | }; 75 | -------------------------------------------------------------------------------- /UsbDkHelper/dllmain.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | 26 | BOOL APIENTRY DllMain( HMODULE /*hModule*/, 27 | DWORD ul_reason_for_call, 28 | LPVOID /*lpReserved*/ 29 | ) 30 | { 31 | switch (ul_reason_for_call) 32 | { 33 | case DLL_PROCESS_ATTACH: 34 | case DLL_THREAD_ATTACH: 35 | case DLL_THREAD_DETACH: 36 | case DLL_PROCESS_DETACH: 37 | break; 38 | } 39 | return TRUE; 40 | } 41 | -------------------------------------------------------------------------------- /UsbDkHelper/stdafx.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | // stdafx.cpp : source file that includes just the standard includes 25 | // netco.pch will be the pre-compiled header 26 | // stdafx.obj will contain the pre-compiled type information 27 | 28 | #include "stdafx.h" 29 | -------------------------------------------------------------------------------- /UsbDkHelper/stdafx.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "targetver.h" 27 | 28 | #ifndef WIN32_LEAN_AND_MEAN 29 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 30 | #endif 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #ifndef _USING_V110_SDK71_ 41 | #include 42 | #include 43 | #else 44 | #define TARGET_OS_WIN_XP (1) 45 | #include "UsbDkHlprCompat.h" 46 | #endif 47 | 48 | #include 49 | #include 50 | #include 51 | #include 52 | 53 | //new behavior: elements of array 'array' will be default initialized 54 | #pragma warning(disable : 4351) 55 | 56 | #include "tstrings.h" 57 | #include "Exception.h" 58 | #include "Utils.h" 59 | #include "ServiceManager.h" 60 | 61 | using namespace std; 62 | -------------------------------------------------------------------------------- /UsbDkHelper/targetver.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | // Including SDKDDKVer.h defines the highest available Windows platform. 27 | 28 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 29 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 30 | 31 | #include 32 | -------------------------------------------------------------------------------- /UsbDkHelper/tstrings.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | #include "tstrings.h" 26 | 27 | string __wstring2string(const wstring& str) 28 | { 29 | size_t buf_length = str.length() + 1; 30 | char* buf = new (std::nothrow) char[buf_length]; 31 | size_t nCount; 32 | 33 | if(nullptr == buf) 34 | { 35 | return string(); 36 | } 37 | 38 | if( 0 != wcstombs_s(&nCount, buf, buf_length, str.c_str(), buf_length) ) 39 | {// Return empty string in case of failure 40 | buf[0] = 0; 41 | } 42 | 43 | string result(buf); 44 | delete [] buf; 45 | return result; 46 | } 47 | 48 | wstring __string2wstring(const string& str) 49 | { 50 | size_t buf_length = str.length() + 1; 51 | wchar_t* buf = new (std::nothrow) wchar_t[buf_length]; 52 | size_t nCount; 53 | 54 | if(nullptr == buf) 55 | { 56 | return wstring(); 57 | } 58 | 59 | if( 0 != mbstowcs_s(&nCount, buf, buf_length, str.c_str(), buf_length) ) 60 | {// Return empty string in case of failure 61 | buf[0] = 0; 62 | } 63 | 64 | wstring result(buf); 65 | delete [] buf; 66 | return result; 67 | } 68 | -------------------------------------------------------------------------------- /UsbDkHelper/tstrings.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #pragma warning(push, 3) 27 | #pragma warning(disable:4995) //name was marked as #pragma deprecated 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #pragma warning(pop) 35 | 36 | using std::string; 37 | using std::wstring; 38 | using std::stringstream; 39 | using std::wstringstream; 40 | using std::wcout; 41 | using std::cout; 42 | using std::list; 43 | using std::allocator; 44 | 45 | string __wstring2string(const wstring& str); 46 | wstring __string2wstring(const string& str); 47 | 48 | #if defined(UNICODE) 49 | typedef RPC_WSTR RPC_TSTR; 50 | typedef wstring tstring; 51 | typedef wstringstream tstringstream; 52 | # define tcout wcout 53 | # define tstring2string(str) __wstring2string(str) 54 | # define string2tstring(str) __string2wstring(str) 55 | # define tstring2wstring(str) (str) 56 | # define wstring2tstring(str) (str) 57 | # define to_tstring to_wstring 58 | #else 59 | typedef RPC_CSTR RPC_TSTR; 60 | typedef string tstring; 61 | typedef stringstream tstringstream; 62 | # define tcout cout 63 | # define tstring2string(str) (str) 64 | # define string2tstring(str) (str) 65 | # define tstring2wstring(str) __string2wstring(str) 66 | # define wstring2tstring(str) __wstring2string(str) 67 | # define to_tstring to_string 68 | #endif 69 | 70 | typedef list> tstringlist; 71 | 72 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) 73 | #define TBUF_SIZEOF(a) ARRAY_SIZE(a) 74 | -------------------------------------------------------------------------------- /UsbDkInstHelper/Resource.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daynix/UsbDk/af46b7ee3302413d2533c492fac2aa154cfd3d70/UsbDkInstHelper/Resource.rc -------------------------------------------------------------------------------- /UsbDkInstHelper/UsbDkInstHelper.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | 26 | #include "UsbDkHelper.h" 27 | 28 | using namespace std; 29 | 30 | static BOOL suppressInstallMessageBox = FALSE; 31 | 32 | static int Controller_InstallDriver() 33 | { 34 | //Clean up any previous versions before reinstalling 35 | UsbDk_UninstallDriver(); 36 | 37 | auto res = UsbDk_InstallDriver(); 38 | switch (res) 39 | { 40 | case InstallSuccess: 41 | return 0; 42 | case InstallFailure: 43 | return 1; 44 | case InstallAborted: 45 | if (!suppressInstallMessageBox) 46 | { 47 | MessageBox(NULL, 48 | TEXT("Failed to start the driver on the system, installation aborted!\nPlease make sure you are installing a signed version of UsbDk or else try to disable \"driver signature enforcement\" on the system"), 49 | TEXT("UsbDk Runtime Libraries Installer"), MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND | MB_SYSTEMMODAL); 50 | } 51 | else 52 | { 53 | OutputDebugString(TEXT("UsbDkInstHelper: Installation aborted")); 54 | } 55 | return 4; 56 | case InstallSuccessNeedReboot: 57 | if (!suppressInstallMessageBox) 58 | { 59 | MessageBox(NULL, 60 | TEXT("Please restart your computer to complete the installation"), 61 | TEXT("UsbDk Runtime Libraries Installer"), MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND | MB_SYSTEMMODAL); 62 | } 63 | else 64 | { 65 | OutputDebugString(TEXT("UsbDkInstHelper: reboot required to complete the installation")); 66 | } 67 | return 0; 68 | default: 69 | assert(0); 70 | return 3; 71 | } 72 | } 73 | 74 | static int Controller_UninstallDriver() 75 | { 76 | return UsbDk_UninstallDriver() ? 0 : 1; 77 | } 78 | 79 | static bool _ChdirToPackageFolder() 80 | { 81 | TCHAR PackagePath[MAX_PATH]; 82 | 83 | auto Length = GetModuleFileName(NULL, PackagePath, ARRAY_SIZE(PackagePath)); 84 | 85 | if ((Length == 0) || (Length == ARRAY_SIZE(PackagePath))) 86 | { 87 | OutputDebugString(TEXT("UsbDkInstHelper: Failed to get executable path.")); 88 | return false; 89 | } 90 | 91 | if (!PathRemoveFileSpec(PackagePath)) 92 | { 93 | OutputDebugString(TEXT("UsbDkInstHelper: Failed to get package directory.")); 94 | return false; 95 | } 96 | 97 | if (!SetCurrentDirectory(PackagePath)) 98 | { 99 | OutputDebugString(TEXT("UsbDkInstHelper: Failed to make package directory current")); 100 | return false; 101 | } 102 | 103 | return true; 104 | } 105 | 106 | int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 107 | { 108 | UNREFERENCED_PARAMETER(hInstance); 109 | UNREFERENCED_PARAMETER(hPrevInstance); 110 | UNREFERENCED_PARAMETER(lpCmdLine); 111 | UNREFERENCED_PARAMETER(nCmdShow); 112 | 113 | if (!_ChdirToPackageFolder()) 114 | { 115 | return -1; 116 | } 117 | 118 | if (lpCmdLine[0] == 'i') 119 | { 120 | if (lpCmdLine[1] == 'n' || lpCmdLine[1] == 'N') 121 | { 122 | suppressInstallMessageBox = TRUE; 123 | } 124 | OutputDebugString(TEXT("UsbDkInstHelper: Install")); 125 | return Controller_InstallDriver(); 126 | } 127 | 128 | if (lpCmdLine[0] == 'u') 129 | { 130 | OutputDebugString(TEXT("UsbDkInstHelper: Uninstall")); 131 | return Controller_UninstallDriver(); 132 | } 133 | 134 | OutputDebugString(TEXT("UsbDkInstHelper: Wrong command line")); 135 | return -2; 136 | } 137 | -------------------------------------------------------------------------------- /UsbDkInstHelper/UsbDkInstHelper.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | 37 | 38 | Resource Files 39 | 40 | 41 | -------------------------------------------------------------------------------- /UsbDkInstHelper/stdafx.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #include "stdafx.h" 25 | -------------------------------------------------------------------------------- /UsbDkInstHelper/stdafx.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | #include "targetver.h" 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #ifndef _USING_V110_SDK71_ 37 | #include 38 | #else 39 | #include "UsbDkCompat.h" 40 | #endif 41 | 42 | #include "tstrings.h" 43 | -------------------------------------------------------------------------------- /UsbDkInstHelper/targetver.h: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | * Copyright (c) 2013-2014 Red Hat, Inc. 3 | * 4 | * Developed by Daynix Computing LTD. 5 | * 6 | * Authors: 7 | * Dmitry Fleytman 8 | * Pavel Gurvich 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); 11 | * you may not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | **********************************************************************/ 23 | 24 | #pragma once 25 | 26 | // Including SDKDDKVer.h defines the highest available Windows platform. 27 | 28 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 29 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 30 | 31 | #include 32 | -------------------------------------------------------------------------------- /buildAll.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | SETLOCAL EnableExtensions EnableDelayedExpansion 4 | 5 | set _f=UsbDk 6 | if [%1] EQU [MSIONLY] goto BUILD_MSI 7 | if [%2] EQU [NOSIGN] (SET DEBUG_CFG=Debug_NoSign) ELSE (SET DEBUG_CFG=Debug) 8 | 9 | del *.log 10 | 11 | for %%x in (Win7, Win8, Win8.1, Win10, XP) do ( 12 | for %%y in (%DEBUG_CFG%, Release) do ( 13 | for %%z in (win32, x64) do ( 14 | call tools\vs_run.bat UsbDk.sln /Rebuild "%%x %%y|%%z" /Out build%%y_%%x_%%z.log 15 | if !ERRORLEVEL! NEQ 0 exit /B 1 16 | ) 17 | ) 18 | ) 19 | 20 | pushd Install 21 | call :maketmf Release 22 | if !ERRORLEVEL! NEQ 0 exit /B 1 23 | popd 24 | 25 | pushd Install_Debug 26 | call :maketmf %DEBUG_CFG% 27 | if !ERRORLEVEL! NEQ 0 exit /B 1 28 | popd 29 | 30 | if [%1] EQU [NOMSI] goto NOMSI 31 | goto BUILD_MSI 32 | 33 | :maketmf 34 | del *.tmf *.mof 35 | call :make1tmf x64\Win10%1 36 | call :make1tmf x86\Win10%1 37 | call :make1tmf x64\Win8.1%1 38 | call :make1tmf x86\Win8.1%1 39 | call :make1tmf x64\Win8%1 40 | call :make1tmf x86\Win8%1 41 | call :make1tmf x64\Win7%1 42 | call :make1tmf x86\Win7%1 43 | call :make1tmf x64\XP%1 44 | call :make1tmf x86\XP%1 45 | goto :eof 46 | 47 | :make1tmf 48 | pushd %1 49 | echo Making TMF in %1 50 | "C:\Program Files (x86)\Windows Kits\10\bin\x86\tracepdb.exe" -s -o .\%_f%.tmf 51 | popd 52 | type %1\%_f%.tmf >> %_f%.tmf 53 | del %1\%_f%.??f 54 | goto :eof 55 | 56 | :BUILD_MSI 57 | 58 | pushd Tools\Installer 59 | 60 | SET UsbDkVersion="%USBDK_MAJOR_VERSION%.%USBDK_MINOR_VERSION%.%USBDK_BUILD_NUMBER%" 61 | buildmsi.bat %2 62 | 63 | popd 64 | 65 | :NOMSI 66 | 67 | ENDLOCAL 68 | --------------------------------------------------------------------------------