创建虚拟USB设备

发布于 2024-10-17 19:16:33 字数 439 浏览 5 评论 0原文

我是一个新手,学习如何为 USB 设备编写 WDM 设备驱动程序,发现现有的材料都太难理解(DDK 在线文档是最难阅读的文档之一,Oney 的 WDM 设备驱动程序书则不是)没有什么更好的了)。

所以,我有一个简单的问题。如果我想创建一个虚拟 USB 设备(例如,看起来像连接到 USB 端口的真实 USB 鼠标的虚拟 USB 鼠标)用于测试/学习,我应该从哪里开始。

到目前为止,我所了解的是 HIDClass 驱动程序 (hidclass.sys) 有一个用于 USB 总线 (hidusb.sys) 的微型驱动程序,用于执行所连接的 USB 硬件的枚举。那么,如果我想劫持硬件枚举过程并创建自己的虚拟硬件,我是否应该在某处包含一个过滤驱动程序来拦截一些与硬件枚举过程相关的IRP?

抱歉,如果上述内容完全没有意义,因为我仍处于学习阶段,这实际上是我认为可以帮助我更好地学习编写 USB 设备驱动程序的练习之一。

I am a newbie learning how to write WDM device drivers for USB devices and found that the materials available are all too hard to comprehend (the DDK online doc is one of the most difficult to read, and the WDM Device driver book by Oney isn't anything better).

So, I've got a simple question. Where do I start if I want to create a virtual USB device (for example, a virtual USB mouse which looks like a real USB mouse attached to a USB port) for testing/learning.

So far what I understand is the HIDClass driver (hidclass.sys) has a minidriver for the usb bus (hidusb.sys) that carries out the enumeration of attached USB hardware. So, if I want to hijack the hardware enumeration process and creates my own virtual hardware, should I include a filter driver somewhere to intercept some IRPs related to the hardware enumeration process?

Sorry if the above does not make sense at all since I am still in the learning stage and this is actually one of the exercise I think could help me learn about writing USB device drivers better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界和平 2024-10-24 19:16:33

Windows 使用即插即用架构。
当您插入 USB 设备时,它会向设备发送低级 USB 请求,然后根据设备的响应决定要加载的驱动程序。匹配是通过将供应商 ID、产品 ID 等与 inf 文件部分进行比较来完成的。驱动程序以编译后的 xxx.sys 和 xxx.inf 文件的形式出现,并加载到内核空间。 Windows 根据设备驱动程序附带的 *.inf 文件来决定加载哪个 xxx.sys。

这些文件具有如下部分:(

[Manufacturer]
%Manufacturer% = DeviceInstall

[DeviceInstall]
"some usb dev"=OTHER_SECTION_DEV, USB\Vid_XXXX&Pid_yyyy

# This is where windows learns to match this information
# to your device, using the product id (Pid) and the 
# vendor id (Vid) that Windows gets back during the
# low level USB DeviceDescriptor request

[OTHER_SECTION_DEV]
CopyFiles = xxx.sys, 10,system32\drivers

有关 inf 文件内容的更详细说明可以在 https://learn.microsoft.com/en-us/windows-hardware/drivers/install/inf-manufacturer-section)


详细查看USB枚举过程(使用USB记录器):

  • USB设备插入
  • USB总线驱动程序请求
    • 获取描述符(设备)
    • 获取描述符(配置)
    • GetDescriptor(String iSerialNumber),用作设备实例 ID
    • GetDescriptor(String iProduct),用于“新硬件已识别”弹出窗口
  • PNP(即插即用)管理器被告知:总线驱动程序添加了一个设备。
  • 然后,PNP 管理器通过使用 PNP 请求向总线驱动程序询问设备信息,要求:
    • DeviceID 字符串,代表 USB 供应商和产品 ID,
    • HardwareID 字符串,
    • CompatibleIDs 字符串,代表 USB 设备的接口类、子类和协议,以及
    • InstanceID 字符串,表示连接到计算机的具有相同兼容 ID 的所有实例集中此特定设备的 UID。

对于任何连接的 USB 设备,您可以使用设备管理器看到这些字符串:

  • 打开设备管理器(Windows 菜单 ->“设备管理器”,或控制面板 ->“系统”->“硬件”->“设备”) Manager”)
  • 然后使用“查看”菜单切换到“Device by Connection”
  • 打开“ACPI [...]”-> “PCI总线”/“PCI Express根联合体”-> “[...] USB [...] Host Controller”
  • 展开主机控制器下的任何条目,对于列出的任何设备,右键单击以获取其属性,打开“详细信息”选项卡,然后使用在属性下拉菜单中查找“硬件 Ids”、“兼容 Ids”、“设备实例 ID”、“匹配设备 Id”、“服务”等。

例如,我有一个 USB 存储设备,其 Device Id = usb\class_08&subclass_06&prot_50 连接起来,并且该字符串可以与第一次枚举后添加到已知设备列表中的 .inf 文件相匹配。该文件有一个字符串 Service = USBSTOR,因此我们知道 usbstor.sys 用于与此 USB 大容量存储设备交互。

让我们继续匹配过程。

  • PNP 管理器尝试确定设备是否已“安装”:
    • 它在注册表中搜索与“DeviceInstance ID”匹配的键,以查看哪个服务处理与该设备的接口。具体来说,它会在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB
    • 中搜索此内容

内容

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\Vid_0781&Pid_5406\0775555ACA54ADE3]
"Service"="USBSTOR"
  • 。来自 .inf 数据库的 PNP 请求和数据:
    • inf 数据库位于:C:\WINDOWS\inf\
    • 驱动程序 .sys 文件位于:C:\WINDOWS\system32\drivers
  • 如果 PNP 找不到匹配的字符串,则会提示您显示 xxx 的路径。 sys 和 xxx.inf

对于编写驱动程序,我的建议是:

  1. 不要从实现 HID(人机接口设备)设备开始,因为您可能会导致 Windows 使用您的鼠标或键盘的自定义驱动程序而不是原始驱动程序,这将禁用您的驱动程序鼠标或键盘,非常危险。
  2. 不要将驱动程序加载到您的开发机器中:
    1. 使用虚拟机并在其中安装驱动程序。为虚拟机设置内核调试器: http://www.codeproject.com/KB /winsdk/KernelModeDebuggerSetup.asp
    2. 或在其他测试机器上加载驱动程序。
  3. USB驱动程序的良好学习平台是“OSR USB-FX2学习套件”

Windows uses a Plug and Play Architecture.
When you insert a USB device, It sends low level USB request to the device and then based on the response from a device decides what driver to load. Matching is done by comparing vendor id, product id and etc to inf files sections. Drivers come in the form of a compiled xxx.sys with xxx.inf file and is loaded to kernel space. Windows decides which xxx.sys to load based on the *.inf file that comes with the device's driver.

These files have sections like this:

[Manufacturer]
%Manufacturer% = DeviceInstall

[DeviceInstall]
"some usb dev"=OTHER_SECTION_DEV, USB\Vid_XXXX&Pid_yyyy

# This is where windows learns to match this information
# to your device, using the product id (Pid) and the 
# vendor id (Vid) that Windows gets back during the
# low level USB DeviceDescriptor request

[OTHER_SECTION_DEV]
CopyFiles = xxx.sys, 10,system32\drivers

(a more detailed description on what's in inf files can be found over on https://learn.microsoft.com/en-us/windows-hardware/drivers/install/inf-manufacturer-section)


A detailed look at the USB enumeration process (Use USB Logger):

  • USB Device Plugged
  • USB Bus Driver Request
    • GetDescriptor(Device)
    • GetDescriptor(Configuration)
    • GetDescriptor(String iSerialNumber), used as Device Instance ID
    • GetDescriptor(String iProduct), used in the "new Hardware been identified" popups
  • The PNP (Plug and Play) manager is informed that a device was added by the bus drivers.
  • The PNP manager then asks the bus driver for device information by using a PNP request, asking for:
    • DeviceID string, representing the USB Vendor and Product ID,
    • HardwareIDs string,
    • CompatibleIDs string, representing USB device' Interface Class, Subclass and Protocol, and
    • InstanceID string, representing the uid for this particular device within the set of all instances with the same compatible id hooked up to the computer.

For any connected USB device you can see these strings using the Device Manager:

  • Open the Device Manager (windows menu -> "device manager", or control panel -> "System" -> "Hardware" -> "Device Manager")
  • then use the "view" menu to switch to "Device by Connection"
  • open "ACPI [...]" -> "PCI bus"/"PCI Express Root Complex" -> "[...] USB [...] Host Controller"
  • expand any of the entries under the host controller, and for any of the devices listed, right click to get their properties, open the "details" tab, and then use the property pulldown menu to find "Hardware Ids", "Compatible Ids", "Device Instance ID", "Matching Device Id", "Service", etc.

For example, I have a USB storage device with Device Id = usb\class_08&subclass_06&prot_50 hooked up, and this string can be matched to an .inf file that was added to the list of known devices after first enumeration. This file has a string Service = USBSTOR, and so we know that usbstor.sys is used to interface with this USB Mass Storage Device.

Let's continue with matching process.

  • The PNP Manager tries to determine whether Device was already "installed":
    • It search the registry for a key matching the "DeviceInstance ID" to see which service handles interfacing with this device. Specifically, it searches for this in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB

For disk on key, you can see something like:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\Vid_0781&Pid_5406\0775555ACA54ADE3]
"Service"="USBSTOR"
  • The PNP Manager then loads the associated driver based on a match between the strings in PNP requests and data from the .inf database:
    • inf database located under: C:\WINDOWS\inf\
    • drivers .sys files located: C:\WINDOWS\system32\drivers
  • If PNP can't find matching string, you will get prompt to show a path to xxx.sys and xxx.inf

For writing drivers my advice is:

  1. Don't start with implementing HID (human interface device) devices, because you can cause windows to use your custom driver for you mouse or keyboard instead of original driver, this will disable your mouse or keyboard, very dangerous.
  2. Don't load drivers into your dev machine:
    1. use a virtual machine and install your drivers there. Set up a kernel debugger for your virtual machine: http://www.codeproject.com/KB/winsdk/KernelModeDebuggerSetup.asp
    2. or load drivers on other test machine.
  3. Good learning platform for USB drivers is "OSR USB-FX2 Learning Kit"
策马西风 2024-10-24 19:16:33

您可以使用 USB/IP 项目来模拟任何您想要的设备。在我的博客中,我演示了如何使用 USB/IP 项目在 python 中模拟 USB 鼠标设备:
http://break -the-system.blogspot.com/2014/08/emulated-usb-devices-in-python-with-no.html

它不会帮助您了解如何创建虚拟USB设备(该过程已完成在 USB/IP 驱动程序中,您可以读取代码),但它将创建虚拟 USB HID 设备,您可以使用发送到 USB 驱动程序的 HID 参数。

You can use the USB/IP project to emulate any device that you want. In my blog I demonstrated how to emulate USB Mouse device in python using the USB/IP project:
http://breaking-the-system.blogspot.com/2014/08/emulating-usb-devices-in-python-with-no.html

It wont help you to understand how to create the virtual USB device (the process is done in the USB/IP driver, you could read the code), but it will create the virtual USB HID device and you could play with the HID arguments sent to the USB driver.

悲凉≈ 2024-10-24 19:16:33

提供您自己的总线类型和枚举器不是更有意义吗?

Wouldn't it make more sense to provide your own bus type and enumerator?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文