From 206b70ea15dbcdd1bc85557e127932ddf4ebf48d Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 25 Feb 2023 16:37:23 +0100 Subject: [PATCH 01/10] driver: Only check OS version and set pool type once Signed-off-by: Tormod Volden --- libusb/src/driver/libusb_driver.c | 49 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/libusb/src/driver/libusb_driver.c b/libusb/src/driver/libusb_driver.c index 30f2c69..462c503 100644 --- a/libusb/src/driver/libusb_driver.c +++ b/libusb/src/driver/libusb_driver.c @@ -45,6 +45,8 @@ const char* attached_driver_wdf_list[] = static bool_t match_driver(PDEVICE_OBJECT deviceObject, const char* driverString); +static RTL_OSVERSIONINFOW os_version; +static POOL_TYPE alloc_pool; #ifdef DBG @@ -121,6 +123,26 @@ NTSTATUS DDKAPI DriverEntry(DRIVER_OBJECT *driver_object, driver_object->DriverExtension->AddDevice = add_device; driver_object->DriverUnload = unload; + /* Determine running OS version */ + os_version.dwOSVersionInfoSize = sizeof(os_version); + if (!NT_SUCCESS(RtlGetVersion(&os_version))) + { + os_version.dwMajorVersion = 5; + os_version.dwMinorVersion = 0; + } + + /* Windows 8 needs non-executable paged pool to pass verification test */ + if (os_version.dwMajorVersion > 6 || + (os_version.dwMajorVersion == 6 && os_version.dwMinorVersion >= 2)) + { + alloc_pool = 512; /* NonPagedPoolNx - Windows 8 and up */ + } + else + { + /* Windows 7 and below only has normal paged pool */ + alloc_pool = NonPagedPool; + } + return STATUS_SUCCESS; } @@ -987,30 +1009,5 @@ Return Value: PVOID allocate_pool(SIZE_T bytes) { - ULONG major; - ULONG minor; - NTSTATUS status; - RTL_OSVERSIONINFOW version; - /* Windwos 7 and below only has normal paged pool */ - POOL_TYPE pool = NonPagedPool; - - version.dwOSVersionInfoSize = sizeof(version); - status = RtlGetVersion(&version); - if(!NT_VERIFY(NT_SUCCESS(status))) - { - major = 5; - minor = 0; - } - else - { - major = version.dwMajorVersion; - minor = version.dwMinorVersion; - } - - /* Windwos 8 needs non-executable paged pool to pass verification test */ - if((major > 6) || ((major == 6) && (minor >= 2))) - { - pool = 512; /* NonPagedPoolNx - Windows 8 and up */ - } - return ExAllocatePool(pool, bytes); + return ExAllocatePool(alloc_pool, bytes); } From 48aa74d7c278ccc2aba849b280551b0b359934f7 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 26 Feb 2023 21:54:58 +0100 Subject: [PATCH 02/10] driver: Remove unused variables --- libusb/src/driver/get_interface.c | 1 - libusb/src/driver/libusb_driver.c | 1 - libusb/src/driver/set_configuration.c | 2 +- libusb/src/driver/set_interface.c | 2 -- libusb/src/driver/transfer.c | 2 -- 5 files changed, 1 insertion(+), 7 deletions(-) diff --git a/libusb/src/driver/get_interface.c b/libusb/src/driver/get_interface.c index b218281..d0b13d3 100644 --- a/libusb/src/driver/get_interface.c +++ b/libusb/src/driver/get_interface.c @@ -28,7 +28,6 @@ NTSTATUS get_interface(libusb_device_t *dev, { NTSTATUS status = STATUS_SUCCESS; URB urb; - PUSB_INTERFACE_DESCRIPTOR interface_descriptor; USBMSG("interface: %d timeout: %d\n", interface_number, timeout); diff --git a/libusb/src/driver/libusb_driver.c b/libusb/src/driver/libusb_driver.c index 462c503..fbe15b9 100644 --- a/libusb/src/driver/libusb_driver.c +++ b/libusb/src/driver/libusb_driver.c @@ -795,7 +795,6 @@ USB_INTERFACE_DESCRIPTOR* find_interface_desc_ex(USB_CONFIGURATION_DESCRIPTOR *c usb_descriptor_header_t *desc = (usb_descriptor_header_t *)config_desc; char *p = (char *)desc; - int lastInfNumber, lastAltNumber; int currentInfIndex; short InterfacesByIndex[LIBUSB_MAX_NUMBER_OF_INTERFACES][2]; diff --git a/libusb/src/driver/set_configuration.c b/libusb/src/driver/set_configuration.c index 2f94271..cb6fcef 100644 --- a/libusb/src/driver/set_configuration.c +++ b/libusb/src/driver/set_configuration.c @@ -30,7 +30,7 @@ NTSTATUS set_configuration(libusb_device_t *dev, USB_CONFIGURATION_DESCRIPTOR *configuration_descriptor = NULL; USB_INTERFACE_DESCRIPTOR *interface_descriptor = NULL; USBD_INTERFACE_LIST_ENTRY *interfaces = NULL; - int i, j, interface_number, desc_size, config_index, ret; + int i, j, interface_number, desc_size, config_index; // check if this config value is already set if ((configuration > 0) && dev->config.value == configuration) diff --git a/libusb/src/driver/set_interface.c b/libusb/src/driver/set_interface.c index 74b96eb..5fe8d1f 100644 --- a/libusb/src/driver/set_interface.c +++ b/libusb/src/driver/set_interface.c @@ -103,8 +103,6 @@ NTSTATUS set_interface_ex(libusb_device_t *dev, interface_request_t* interface_request, int timeout) { - NTSTATUS status = STATUS_SUCCESS; - USB_INTERFACE_DESCRIPTOR *interface_descriptor = NULL; USBMSG("interface-%s=%d alt-%s=%d timeout=%d\n", diff --git a/libusb/src/driver/transfer.c b/libusb/src/driver/transfer.c index eac01a7..886ae04 100644 --- a/libusb/src/driver/transfer.c +++ b/libusb/src/driver/transfer.c @@ -393,7 +393,6 @@ NTSTATUS large_transfer(IN libusb_device_t* dev, IN PMDL mdlAddress, IN int totalLength) { - PIO_STACK_LOCATION irpStack; BOOLEAN read; ULONG stageSize; ULONG numIrps; @@ -424,7 +423,6 @@ NTSTATUS large_transfer(IN libusb_device_t* dev, // // initialize vars // - irpStack = IoGetCurrentIrpStackLocation(irp); sequenceID = InterlockedIncrement(&sequence); subRequestContextArray = NULL; From 688d0e8020d802eb5c4601f13c69e657a805119d Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 26 Feb 2023 22:52:06 +0100 Subject: [PATCH 03/10] driver/ioctl: Add cast to silence compiler warning Signed-off-by: Tormod Volden --- libusb/src/driver/ioctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libusb/src/driver/ioctl.c b/libusb/src/driver/ioctl.c index f38a9ab..771d6f4 100644 --- a/libusb/src/driver/ioctl.c +++ b/libusb/src/driver/ioctl.c @@ -344,7 +344,7 @@ NTSTATUS dispatch_ioctl(libusb_device_t *dev, IRP *irp) } else { - status = get_configuration(dev, output_buffer, &ret, request->timeout); + status = get_configuration(dev, (unsigned char *)output_buffer, &ret, request->timeout); } break; @@ -370,7 +370,7 @@ NTSTATUS dispatch_ioctl(libusb_device_t *dev, IRP *irp) status = get_interface( dev, request->intf.interface_number, - output_buffer, + (unsigned char *)output_buffer, request->timeout); if (NT_SUCCESS(status)) From 9fc2ab0067bc1d491ac8943d7030a0cb3026d2a0 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 26 Feb 2023 22:59:28 +0100 Subject: [PATCH 04/10] driver/transfer: Silence warning for non-debug build Signed-off-by: Tormod Volden --- libusb/src/driver/transfer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libusb/src/driver/transfer.c b/libusb/src/driver/transfer.c index 886ae04..0ac2226 100644 --- a/libusb/src/driver/transfer.c +++ b/libusb/src/driver/transfer.c @@ -985,6 +985,7 @@ NTSTATUS large_transfer_complete(IN PDEVICE_OBJECT DeviceObjectIsNULL, subRequestByteCount = MmGetMdlByteCount(subUrb->UrbBulkOrInterruptTransfer.TransferBufferMDL); subRequestByteOffset = MmGetMdlByteOffset(subUrb->UrbBulkOrInterruptTransfer.TransferBufferMDL); information = subUrb->UrbBulkOrInterruptTransfer.TransferBufferLength; + (void) subRequestByteOffset; USBDBG("[%s #%d] offset=%d requested=%d transferred=%d\n", dispTransfer, sequenceID, From 58da356ba4b5c3127c3669673f19cfdb1f3bc289 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 26 Feb 2023 22:21:08 +0100 Subject: [PATCH 05/10] driver: Selected whitespace cleanup The inconsistent indentation was giving compiler warnings. Signed-off-by: Tormod Volden --- libusb/src/driver/libusb_driver.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libusb/src/driver/libusb_driver.c b/libusb/src/driver/libusb_driver.c index fbe15b9..58c108f 100644 --- a/libusb/src/driver/libusb_driver.c +++ b/libusb/src/driver/libusb_driver.c @@ -793,25 +793,25 @@ USB_INTERFACE_DESCRIPTOR* find_interface_desc_ex(USB_CONFIGURATION_DESCRIPTOR *c #define INTF_FIELD 0 #define ALTF_FIELD 1 - usb_descriptor_header_t *desc = (usb_descriptor_header_t *)config_desc; + usb_descriptor_header_t *desc = (usb_descriptor_header_t *)config_desc; char *p = (char *)desc; - int currentInfIndex; - short InterfacesByIndex[LIBUSB_MAX_NUMBER_OF_INTERFACES][2]; + int currentInfIndex; + short InterfacesByIndex[LIBUSB_MAX_NUMBER_OF_INTERFACES][2]; USB_INTERFACE_DESCRIPTOR *if_desc = NULL; - memset(InterfacesByIndex,0xFF,sizeof(InterfacesByIndex)); + memset(InterfacesByIndex,0xFF,sizeof(InterfacesByIndex)); if (!config_desc) return NULL; - size = size > config_desc->wTotalLength ? config_desc->wTotalLength : size; + size = size > config_desc->wTotalLength ? config_desc->wTotalLength : size; while (size && desc->length <= size) { if (desc->type == USB_INTERFACE_DESCRIPTOR_TYPE) { - // this is a new interface or alternate interface + // this is a new interface or alternate interface if_desc = (USB_INTERFACE_DESCRIPTOR *)desc; for (currentInfIndex=0; currentInfIndex Date: Sun, 26 Feb 2023 18:42:49 +0100 Subject: [PATCH 06/10] driver: Avoid extra level of GUID define macros The preprocessor introduces a space between L and the string: L "abc" and GCC will fail to recognize this as one token. Signed-off-by: Tormod Volden --- libusb/src/driver/lusb_defdi_guids.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libusb/src/driver/lusb_defdi_guids.h b/libusb/src/driver/lusb_defdi_guids.h index ce625ad..5bf6025 100644 --- a/libusb/src/driver/lusb_defdi_guids.h +++ b/libusb/src/driver/lusb_defdi_guids.h @@ -2,13 +2,11 @@ #define __LUSB_DEFDI_GUIDS_ #ifndef DEFINE_TO_STR -#define _DEFINE_TO_STR(x) #x -#define DEFINE_TO_STR(x) _DEFINE_TO_STR(x) +#define DEFINE_TO_STR(x) #x #endif #ifndef DEFINE_TO_STRW -#define _DEFINE_TO_STRW(x) L#x -#define DEFINE_TO_STRW(x) _DEFINE_TO_STRW(x) +#define DEFINE_TO_STRW(x) L ## #x #endif #define DEFINE_DEVICE_INTERFACE_GUID(BaseFieldName,BaseGuidName,L1,W1,W2,B1,B2,B3,B4,B5,B6,B7,B8) \ From 6903b0d60ed35e1f3172720aa0cc20764a1b7716 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 26 Feb 2023 19:14:01 +0100 Subject: [PATCH 07/10] driver: Update GCC build for MinGW 7.0.0 Many things have improved in MinGW over the years and less special-casing is needed. Due to many DDK header files referring to each other without the ddk/ path prefix, we must tell make where to find the include/ddk folder, e.g. (Ubuntu 20.04): make host_prefix=i686-w64-mingw32 ddk=/usr/i686-w64-mingw32 driver or make host_prefix=x86_64-w64-mingw32 ddk=/usr/x86_64-w64-mingw32 driver --- libusb/Makefile | 5 +++++ libusb/src/driver/driver_registry.c | 1 + libusb/src/driver/libusb_driver.h | 28 ++++++++-------------------- libusb/src/error.c | 10 +--------- 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/libusb/Makefile b/libusb/Makefile index 6c05d30..17a91ec 100644 --- a/libusb/Makefile +++ b/libusb/Makefile @@ -35,6 +35,9 @@ endif ifdef cflags DBG_DEFINE = $(cflags) endif +ifdef ddk + DDK_INCLUDE = -I$(ddk)/include/ddk +endif CC = $(host_prefix)gcc LD = $(host_prefix)ld @@ -87,8 +90,10 @@ LIBWDI_OBJECTS = $(LIBWDI_DIR)/logging.5.o \ INCLUDES = -I./src -I./src/driver -I. +driver: INCLUDES += $(DDK_INCLUDE) CFLAGS = -O2 -Wall -DWINVER=0x500 $(DBG_DEFINE) +CFLAGS += -Wno-unknown-pragmas -Wno-multichar WIN_CFLAGS = $(CFLAGS) -mwindows WINDRES_FLAGS = -I$(SRC_DIR) diff --git a/libusb/src/driver/driver_registry.c b/libusb/src/driver/driver_registry.c index 8ec14fc..3376bc0 100644 --- a/libusb/src/driver/driver_registry.c +++ b/libusb/src/driver/driver_registry.c @@ -19,6 +19,7 @@ #include "libusb_driver.h" #include "lusb_defdi_guids.h" +#include /* missing in mingw's ddk headers */ #ifndef PLUGPLAY_REGKEY_DEVICE diff --git a/libusb/src/driver/libusb_driver.h b/libusb/src/driver/libusb_driver.h index f2ca21c..7155814 100644 --- a/libusb/src/driver/libusb_driver.h +++ b/libusb/src/driver/libusb_driver.h @@ -24,17 +24,14 @@ //#define SKIP_DEVICES_WINUSB //#define SKIP_DEVICES_PICOPP +#include #ifdef __GNUC__ -#include -#include -#include -#include "usbdlib_gcc.h" +#define NTSTATUS LONG #else -#include #include +#endif #include "usbdi.h" #include "usbdlib.h" -#endif #include #include @@ -45,19 +42,6 @@ #include "error.h" #include "driver_api.h" -/* some missing defines */ -#ifdef __GNUC__ - -#define USBD_TRANSFER_DIRECTION_OUT 0 -#define USBD_TRANSFER_DIRECTION_BIT 0 -#define USBD_TRANSFER_DIRECTION_IN (1 << USBD_TRANSFER_DIRECTION_BIT) -#define USBD_SHORT_TRANSFER_OK_BIT 1 -#define USBD_SHORT_TRANSFER_OK (1 << USBD_SHORT_TRANSFER_OK_BIT) -#define USBD_START_ISO_TRANSFER_ASAP_BIT 2 -#define USBD_START_ISO_TRANSFER_ASAP (1 << USBD_START_ISO_TRANSFER_ASAP_BIT) - -#endif - #define SET_CONFIG_ACTIVE_CONFIG -258 #define USB_RECIP_DEVICE 0x00 @@ -81,7 +65,9 @@ #define LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT 5000 -#ifndef __GNUC__ +#ifdef __GNUC__ +#define DDKAPI __stdcall +#else #define DDKAPI #endif @@ -140,7 +126,9 @@ typedef int bool_t; #endif +#ifndef __GNUC__ #define USB_ENDPOINT_ADDRESS_MASK 0x0F +#endif #define USB_ENDPOINT_DIR_MASK 0x80 #define LBYTE(w) (w & 0xFF) #define HBYTE(w) ((w>>8) & 0xFF) diff --git a/libusb/src/error.c b/libusb/src/error.c index d31a437..be7de53 100644 --- a/libusb/src/error.c +++ b/libusb/src/error.c @@ -23,15 +23,7 @@ #include #if IS_DRIVER - #ifdef __GNUC__ - #define OBJ_KERNEL_HANDLE 0x00000200L - #include - #include - #include - #include "usbdlib_gcc.h" - #else - #include - #endif + #include #else #include #endif From d5bb41af486d4b87af248d430062e3cb7015a055 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Mon, 27 Feb 2023 00:23:50 +0100 Subject: [PATCH 08/10] driver/Makefile: Set correct DriverEntry for 64-bit build --- libusb/Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libusb/Makefile b/libusb/Makefile index 17a91ec..5956632 100644 --- a/libusb/Makefile +++ b/libusb/Makefile @@ -126,9 +126,14 @@ LIBWDI_DLL_LDFLAGS = -s -shared \ -Wl,--enable-stdcall-fixup \ -L. -lnewdev -lsetupapi -lole32 -DRIVER_LDFLAGS = -s -shared -Wl,--entry,_DriverEntry@8 \ +DRIVER_LDFLAGS = -s -shared -Wl,--entry,$(DRIVERENTRY) \ -nostartfiles -nostdlib -L. -lusbd -lntoskrnl -lhal +ifneq (,$(findstring x86, $(host_prefix))) +DRIVERENTRY = DriverEntry +else +DRIVERENTRY = _DriverEntry@8 +endif .PHONY: all all: dll filter test testwin From f0fdc4709f667fab0bbbfb506f4bef79eadc1cf9 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 26 Feb 2023 16:27:44 +0100 Subject: [PATCH 09/10] driver/dispatch: Add debug print on IRP_MJ_CREATE/IRP_MJ_CLOSE Signed-off-by: Tormod Volden --- libusb/src/driver/dispatch.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libusb/src/driver/dispatch.c b/libusb/src/driver/dispatch.c index 8eccb21..87bdbb3 100644 --- a/libusb/src/driver/dispatch.c +++ b/libusb/src/driver/dispatch.c @@ -58,6 +58,10 @@ NTSTATUS DDKAPI dispatch(DEVICE_OBJECT *device_object, IRP *irp) case IRP_MJ_CREATE: + USBDBG("IRP_MJ_CREATE: is-filter=%c %s\n", + dev->is_filter ? 'Y' : 'N', + dev->device_id); + if (dev->is_started) { // only one driver can act as power policy owner and @@ -79,6 +83,10 @@ NTSTATUS DDKAPI dispatch(DEVICE_OBJECT *device_object, IRP *irp) case IRP_MJ_CLOSE: + USBDBG("IRP_MJ_CLOSE: is-filter=%c %s\n", + dev->is_filter ? 'Y' : 'N', + dev->device_id); + /* release all interfaces bound to this file object */ release_all_interfaces(dev, stack_location->FileObject); return complete_irp(irp, STATUS_SUCCESS, 0); From 56bb42936e6d4868a216a2e1f665bf22af9cdcae Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Mon, 27 Feb 2023 09:19:46 +0100 Subject: [PATCH 10/10] library: Update GCC build for MinGW 7.0.0 Same includes for 32-bit as for 64-bit. Signed-off-by: Tormod Volden --- libusb/src/install.c | 10 ++-------- libusb/src/install_filter_win.c | 10 ++-------- libusb/src/registry.c | 10 ++-------- 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/libusb/src/install.c b/libusb/src/install.c index 36625dd..49ae5af 100644 --- a/libusb/src/install.c +++ b/libusb/src/install.c @@ -30,15 +30,9 @@ #include #include #include - -#ifdef __GNUC__ -#if defined(_WIN64) -#include -#else -#include -#endif -#else #include + +#ifndef __GNUC__ #define strlwr(p) _strlwr(p) #endif diff --git a/libusb/src/install_filter_win.c b/libusb/src/install_filter_win.c index 1a9d428..7388a34 100644 --- a/libusb/src/install_filter_win.c +++ b/libusb/src/install_filter_win.c @@ -20,15 +20,9 @@ #define INITGUID #include +#include -#ifdef __GNUC__ - #if defined(_WIN64) - #include - #else - #include - #endif -#else - #include +#ifndef __GNUC__ #define strlwr(p) _strlwr(p) #endif diff --git a/libusb/src/registry.c b/libusb/src/registry.c index 4431efe..8dddba0 100644 --- a/libusb/src/registry.c +++ b/libusb/src/registry.c @@ -23,15 +23,9 @@ #include #include #include - -#ifdef __GNUC__ -#if defined(_WIN64) -#include -#else -#include -#endif -#else #include + +#ifndef __GNUC__ #define strlwr(p) _strlwr(p) #endif