From 28f99b237bdbfa7792137ca307b3194691609c73 Mon Sep 17 00:00:00 2001 From: Fredrik Orderud Date: Thu, 26 Sep 2024 12:33:08 +0200 Subject: [PATCH] [FwUpdateCfu] Fix broken PID/VID filtering in FwUpdateCfu::GetVersion method Fixes #63. I'm struggling to get FwUpdateCfu.exe version protocolCfgExample.cfg to work against the CfuVirtualHid driver in this repo. The main problem appear to be faulty VID/PID filtering in the FwUpdateCfu::GetVersion method where the method looks for either a VID_xxxx&PID_yyyy or VID_xxxx substring in the DevicePath. The observed DevicePath for CfuVirtualHid on my test machine is \\?\HID#HID_DEVICE_SYSTEM_VHF#2&b84b253&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}, which doesn't contain the VID or PID. The VID/PID filtering mechanism therefore appear to be flawed to me. I think the VID/PID filtering should instead be performed against a HIDD_ATTRIBUTES struct obtained through HidD_GetAttributes after opening the device. --- .../FwUpdate.cpp | 49 +++++++++---------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/Tools/ComponentFirmwareUpdateStandAloneToolSample/FwUpdate.cpp b/Tools/ComponentFirmwareUpdateStandAloneToolSample/FwUpdate.cpp index abb533a..0a156b3 100644 --- a/Tools/ComponentFirmwareUpdateStandAloneToolSample/FwUpdate.cpp +++ b/Tools/ComponentFirmwareUpdateStandAloneToolSample/FwUpdate.cpp @@ -182,18 +182,32 @@ Return Value: NTSTATUS status; HRESULT hr = S_OK; device.hDevice = INVALID_HANDLE_VALUE; - - // Check that the VID/PID matches. - wchar_t vidPidFilterString[256] = { 0 }; - memset(&VerReport, 0, sizeof(VerReport)); + HIDD_ATTRIBUTES attr = { 0 }; + // Open a handle to the device. + device.hDevice = CreateFileW( + DevicePath, + GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); + if (device.hDevice == INVALID_HANDLE_VALUE) + { + wprintf(L"INVALID_HANDLE_VALUE %s", DevicePath); + hr = HRESULT_FROM_WIN32(GetLastError()); + goto Exit; + } + if (!HidD_GetAttributes(device.hDevice, &attr)) { + hr = HRESULT_FROM_WIN32(GetLastError()); + goto Exit; + } // Filter on both if both set - if (ProtocolSettings.Vid && ProtocolSettings.Pid) + if (ProtocolSettings.Vid && ProtocolSettings.Pid) { - swprintf(vidPidFilterString, 256, L"VID_%04X&PID_%04X", - ProtocolSettings.Vid, ProtocolSettings.Pid); - if (!wcsstr(DevicePath, vidPidFilterString)) + if ((attr.VendorID != ProtocolSettings.Vid) || (attr.ProductID != ProtocolSettings.Pid)) { // The device found doesn't match the vid and pid //wprintf(L"The device found does not match the VID/PID\n"); @@ -205,8 +219,7 @@ Return Value: // Filter on vid only (vid is mandatory) else { - swprintf(vidPidFilterString, 256, L"VID_%04X", ProtocolSettings.Vid); - if (!wcsstr(DevicePath, vidPidFilterString)) + if (attr.VendorID != ProtocolSettings.Vid) { // The device found doesn't match the vid hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND); @@ -214,22 +227,6 @@ Return Value: } } - // Open a handle to the device. - device.hDevice = CreateFileW( - DevicePath, - GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); - if (device.hDevice == INVALID_HANDLE_VALUE) - { - wprintf(L"INVALID_HANDLE_VALUE %s", DevicePath); - hr = HRESULT_FROM_WIN32(GetLastError()); - goto Exit; - } - // Try to get the device's preparsed HID data. if (!HidD_GetPreparsedData(device.hDevice, &device.PreparsedData)) {