2014-05-17 23:37:01 +00:00
|
|
|
/*
|
|
|
|
* Rufus: The Reliable USB Formatting Utility
|
2016-04-26 12:57:07 +00:00
|
|
|
* Device detection and enumeration
|
2023-01-27 17:23:08 +00:00
|
|
|
* Copyright © 2014-2023 Pete Batard <pete@akeo.ie>
|
2014-05-17 23:37:01 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
|
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <windowsx.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <commctrl.h>
|
|
|
|
#include <setupapi.h>
|
2019-05-23 12:09:25 +00:00
|
|
|
#include <cfg.h>
|
2018-06-30 21:45:15 +00:00
|
|
|
#include <assert.h>
|
2014-05-17 23:37:01 +00:00
|
|
|
|
|
|
|
#include "rufus.h"
|
2016-02-20 22:52:32 +00:00
|
|
|
#include "missing.h"
|
2014-05-17 23:37:01 +00:00
|
|
|
#include "resource.h"
|
2022-06-14 22:29:33 +00:00
|
|
|
#include "settings.h"
|
2016-02-20 22:52:32 +00:00
|
|
|
#include "msapi_utf8.h"
|
2014-05-17 23:37:01 +00:00
|
|
|
#include "localization.h"
|
2016-02-20 22:52:32 +00:00
|
|
|
|
|
|
|
#include "drive.h"
|
2016-04-26 12:57:07 +00:00
|
|
|
#include "dev.h"
|
2014-05-17 23:37:01 +00:00
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
extern RUFUS_DRIVE rufus_drive[MAX_DRIVES];
|
2020-04-27 09:28:00 +00:00
|
|
|
extern BOOL enable_HDDs, enable_VHDs, use_fake_units, enable_vmdk, usb_debug;
|
|
|
|
extern BOOL list_non_usb_removable_drives, its_a_me_mario;
|
2014-05-17 23:37:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the VID, PID and current device speed
|
|
|
|
*/
|
2015-08-22 20:44:31 +00:00
|
|
|
static BOOL GetUSBProperties(char* parent_path, char* device_id, usb_device_props* props)
|
2014-05-17 23:37:01 +00:00
|
|
|
{
|
2015-08-22 20:44:31 +00:00
|
|
|
BOOL r = FALSE;
|
|
|
|
CONFIGRET cr;
|
2014-05-17 23:37:01 +00:00
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
|
|
|
DWORD size;
|
2014-05-18 23:56:17 +00:00
|
|
|
DEVINST device_inst;
|
2014-05-17 23:37:01 +00:00
|
|
|
USB_NODE_CONNECTION_INFORMATION_EX conn_info;
|
|
|
|
USB_NODE_CONNECTION_INFORMATION_EX_V2 conn_info_v2;
|
|
|
|
|
[mingw] use delay loading for DLLs that are subject to side loading
* This reverts much of commits f6ac559f4d0a9a2e5043a972442d272c2da71de3 and 19472668370aacec0dba9dda306601cfc4a4ed7e
so that we call the Windows APIs directly again, while ensuring that, by the time we load the DLLs,
sideloading mitigation has already been applied by the application.
* This is a continuation of #1877, and should help prevent re-introducing side-loading issues when we
link against new libraries, as well as allow us to drop some of the manual DLL hooking we've been
doing to prevent it, to clean up the code.
* Note that this is a bit more complex than what the stackoverflow post suggests, because we need to
create delayloaded libs for both 32-bit and 64-bit, which use a different calling convention and
therefore need to use different .def files. So there's a lot of gymkhana involved, with Makefiles
and whatnot, to get us there.
* Also simplify the use of CM_Get_DevNode_Registry_PropertyA() in dev.c since recent versions of
MinGW now have support for it.
* Also fix 2 small issues in net.c (potential overflow) and format.c (memory leak).
2022-04-12 10:09:29 +00:00
|
|
|
if ((parent_path == NULL) || (device_id == NULL) || (props == NULL))
|
2015-08-12 19:03:51 +00:00
|
|
|
goto out;
|
|
|
|
|
2015-08-22 20:44:31 +00:00
|
|
|
cr = CM_Locate_DevNodeA(&device_inst, device_id, 0);
|
|
|
|
if (cr != CR_SUCCESS) {
|
|
|
|
uprintf("Could not get device instance handle for '%s': CR error %d", device_id, cr);
|
2015-08-12 19:03:51 +00:00
|
|
|
goto out;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
|
2014-05-18 23:56:17 +00:00
|
|
|
props->port = 0;
|
|
|
|
size = sizeof(props->port);
|
[mingw] use delay loading for DLLs that are subject to side loading
* This reverts much of commits f6ac559f4d0a9a2e5043a972442d272c2da71de3 and 19472668370aacec0dba9dda306601cfc4a4ed7e
so that we call the Windows APIs directly again, while ensuring that, by the time we load the DLLs,
sideloading mitigation has already been applied by the application.
* This is a continuation of #1877, and should help prevent re-introducing side-loading issues when we
link against new libraries, as well as allow us to drop some of the manual DLL hooking we've been
doing to prevent it, to clean up the code.
* Note that this is a bit more complex than what the stackoverflow post suggests, because we need to
create delayloaded libs for both 32-bit and 64-bit, which use a different calling convention and
therefore need to use different .def files. So there's a lot of gymkhana involved, with Makefiles
and whatnot, to get us there.
* Also simplify the use of CM_Get_DevNode_Registry_PropertyA() in dev.c since recent versions of
MinGW now have support for it.
* Also fix 2 small issues in net.c (potential overflow) and format.c (memory leak).
2022-04-12 10:09:29 +00:00
|
|
|
cr = CM_Get_DevNode_Registry_PropertyA(device_inst, CM_DRP_ADDRESS, NULL, (PVOID)&props->port, &size, 0);
|
2015-08-22 20:44:31 +00:00
|
|
|
if (cr != CR_SUCCESS) {
|
|
|
|
uprintf("Could not get port for '%s': CR error %d", device_id, cr);
|
2015-08-12 19:03:51 +00:00
|
|
|
goto out;
|
2014-05-18 23:56:17 +00:00
|
|
|
}
|
|
|
|
|
2014-05-17 23:37:01 +00:00
|
|
|
handle = CreateFileA(parent_path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
2014-06-01 23:51:35 +00:00
|
|
|
uprintf("Could not open hub %s: %s", parent_path, WindowsErrorString());
|
2014-05-17 23:37:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
size = sizeof(conn_info);
|
2018-03-22 23:14:20 +00:00
|
|
|
memset(&conn_info, 0, size);
|
2014-05-17 23:37:01 +00:00
|
|
|
conn_info.ConnectionIndex = (ULONG)props->port;
|
|
|
|
// coverity[tainted_data_argument]
|
|
|
|
if (!DeviceIoControl(handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, &conn_info, size, &conn_info, size, &size, NULL)) {
|
2014-06-01 23:51:35 +00:00
|
|
|
uprintf("Could not get node connection information for '%s': %s", device_id, WindowsErrorString());
|
2014-05-17 23:37:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-01-12 11:47:28 +00:00
|
|
|
// Some poorly written proprietary Windows 7 USB 3.0 controller drivers (<cough>ASMedia<cough>)
|
|
|
|
// have a screwed up implementation of IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX that succeeds
|
|
|
|
// but returns zeroed data => Add a workaround so that we don't lose our VID:PID...
|
|
|
|
if ((conn_info.DeviceDescriptor.idVendor != 0) || (conn_info.DeviceDescriptor.idProduct != 0)) {
|
|
|
|
props->vid = conn_info.DeviceDescriptor.idVendor;
|
|
|
|
props->pid = conn_info.DeviceDescriptor.idProduct;
|
|
|
|
props->speed = conn_info.Speed + 1;
|
|
|
|
r = TRUE;
|
|
|
|
}
|
2014-05-17 23:37:01 +00:00
|
|
|
|
|
|
|
// In their great wisdom, Microsoft decided to BREAK the USB speed report between Windows 7 and Windows 8
|
|
|
|
if (nWindowsVersion >= WINDOWS_8) {
|
|
|
|
size = sizeof(conn_info_v2);
|
2018-03-22 23:14:20 +00:00
|
|
|
memset(&conn_info_v2, 0, size);
|
2014-05-17 23:37:01 +00:00
|
|
|
conn_info_v2.ConnectionIndex = (ULONG)props->port;
|
|
|
|
conn_info_v2.Length = size;
|
|
|
|
conn_info_v2.SupportedUsbProtocols.Usb300 = 1;
|
|
|
|
if (!DeviceIoControl(handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, &conn_info_v2, size, &conn_info_v2, size, &size, NULL)) {
|
2014-06-01 23:51:35 +00:00
|
|
|
uprintf("Could not get node connection information (V2) for device '%s': %s", device_id, WindowsErrorString());
|
2019-07-31 21:45:11 +00:00
|
|
|
} else if (conn_info_v2.Flags.DeviceIsOperatingAtSuperSpeedPlusOrHigher) {
|
|
|
|
props->speed = USB_SPEED_SUPER_PLUS;
|
2014-05-17 23:37:01 +00:00
|
|
|
} else if (conn_info_v2.Flags.DeviceIsOperatingAtSuperSpeedOrHigher) {
|
2019-07-31 21:45:11 +00:00
|
|
|
props->speed = USB_SPEED_SUPER;
|
|
|
|
} else if (conn_info_v2.Flags.DeviceIsSuperSpeedPlusCapableOrHigher) {
|
|
|
|
props->lower_speed = 2;
|
2014-05-18 23:56:17 +00:00
|
|
|
} else if (conn_info_v2.Flags.DeviceIsSuperSpeedCapableOrHigher) {
|
2019-07-31 21:45:11 +00:00
|
|
|
props->lower_speed = 1;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
safe_closehandle(handle);
|
2015-08-22 20:44:31 +00:00
|
|
|
return r;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 11:40:33 +00:00
|
|
|
/*
|
2019-05-23 12:09:25 +00:00
|
|
|
* Cycle the USB port of the selected device
|
2017-09-15 11:40:33 +00:00
|
|
|
*/
|
2019-05-23 12:09:25 +00:00
|
|
|
BOOL CyclePort(int index)
|
2017-09-15 11:40:33 +00:00
|
|
|
{
|
|
|
|
static uint64_t LastReset = 0;
|
|
|
|
BOOL r = FALSE;
|
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
|
|
|
DWORD size;
|
|
|
|
USB_CYCLE_PORT_PARAMS cycle_port;
|
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
assert(index < MAX_DRIVES);
|
2017-09-15 11:40:33 +00:00
|
|
|
// Wait at least 10 secs between resets
|
2017-11-13 14:29:48 +00:00
|
|
|
if (GetTickCount64() < LastReset + 10000ULL) {
|
2017-09-15 11:40:33 +00:00
|
|
|
uprintf("You must wait at least 10 seconds before trying to reset a device");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
if (rufus_drive[index].hub == NULL) {
|
2018-06-13 18:23:24 +00:00
|
|
|
uprintf("The device you are trying to reset does not appear to be a USB device...");
|
2017-09-15 11:40:33 +00:00
|
|
|
return FALSE;
|
2018-06-13 18:23:24 +00:00
|
|
|
}
|
2017-09-15 11:40:33 +00:00
|
|
|
|
2017-11-13 14:29:48 +00:00
|
|
|
LastReset = GetTickCount64();
|
2017-09-15 11:40:33 +00:00
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
handle = CreateFileA(rufus_drive[index].hub, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
2017-09-15 11:40:33 +00:00
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
2022-05-25 12:21:36 +00:00
|
|
|
uprintf("Could not open %s: %s", rufus_drive[index].hub, WindowsErrorString());
|
2017-09-15 11:40:33 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = sizeof(cycle_port);
|
2018-03-22 23:14:20 +00:00
|
|
|
memset(&cycle_port, 0, size);
|
2022-05-25 12:21:36 +00:00
|
|
|
cycle_port.ConnectionIndex = rufus_drive[index].port;
|
|
|
|
uprintf("Cycling port %d (reset) on %s", rufus_drive[index].port, rufus_drive[index].hub);
|
2019-05-23 12:09:25 +00:00
|
|
|
// As per https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/usbioctl/ni-usbioctl-ioctl_usb_hub_cycle_port
|
2017-09-15 11:40:33 +00:00
|
|
|
// IOCTL_USB_HUB_CYCLE_PORT is not supported on Windows 7, Windows Vista, and Windows Server 2008
|
|
|
|
if (!DeviceIoControl(handle, IOCTL_USB_HUB_CYCLE_PORT, &cycle_port, size, &cycle_port, size, &size, NULL)) {
|
|
|
|
uprintf(" Failed to cycle port: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
2018-06-13 18:23:24 +00:00
|
|
|
uprintf("Please wait for the device to re-appear...");
|
2017-09-15 11:40:33 +00:00
|
|
|
r = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
safe_closehandle(handle);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:09:25 +00:00
|
|
|
/*
|
|
|
|
* Forces a refresh by disabling and then re-enabling the device using SetupAPI.
|
|
|
|
* Returns the Windows error code from the operation.
|
2020-02-15 14:44:34 +00:00
|
|
|
* Note: In some circumstances, this may leave the device disabled after re-plug or reboot...
|
2019-05-23 12:09:25 +00:00
|
|
|
*/
|
|
|
|
int CycleDevice(int index)
|
|
|
|
{
|
|
|
|
BOOL found = FALSE, disabled = FALSE;
|
|
|
|
char device_instance_id[MAX_PATH];
|
|
|
|
DWORD i, size, ret = ERROR_DEV_NOT_EXIST;
|
|
|
|
LONG dev_status, problem_code;
|
|
|
|
HDEVINFO dev_info;
|
|
|
|
SP_DEVINFO_DATA dev_info_data;
|
|
|
|
SP_PROPCHANGE_PARAMS propchange_params;
|
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
assert(index < MAX_DRIVES);
|
|
|
|
if ((index < 0) || (safe_strlen(rufus_drive[index].id) < 8))
|
2019-05-23 12:09:25 +00:00
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
// Need DIGCF_ALLCLASSES else disabled devices won't be listed.
|
2019-07-31 21:45:11 +00:00
|
|
|
dev_info = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_DISK, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
|
2019-05-23 12:09:25 +00:00
|
|
|
if (dev_info == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf("Could not get classes for device cycling: %s", WindowsErrorString());
|
|
|
|
return ERROR_PATH_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
|
|
|
for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
|
|
|
|
memset(device_instance_id, 0, sizeof(device_instance_id));
|
|
|
|
size = sizeof(device_instance_id);
|
|
|
|
|
|
|
|
if (!SetupDiGetDeviceInstanceIdA(dev_info, &dev_info_data, device_instance_id, size, &size)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
if (safe_strcmp(rufus_drive[index].id, device_instance_id) != 0)
|
2019-05-23 12:09:25 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
found = TRUE;
|
|
|
|
|
|
|
|
// Detect if the device is already disabled
|
|
|
|
if (CM_Get_DevNode_Status(&dev_status, &problem_code, dev_info_data.DevInst, 0) == CR_SUCCESS)
|
|
|
|
disabled = (dev_status & DN_HAS_PROBLEM) && (problem_code == CM_PROB_DISABLED);
|
|
|
|
|
|
|
|
// Disable the device
|
|
|
|
if (!disabled) {
|
|
|
|
memset(&propchange_params, 0, sizeof(propchange_params));
|
|
|
|
propchange_params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
|
|
|
|
propchange_params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
|
2020-02-15 14:44:34 +00:00
|
|
|
propchange_params.Scope = DICS_FLAG_CONFIGSPECIFIC;
|
2019-05-23 12:09:25 +00:00
|
|
|
propchange_params.StateChange = DICS_DISABLE;
|
|
|
|
|
|
|
|
if (!SetupDiSetClassInstallParams(dev_info, &dev_info_data,
|
|
|
|
(SP_CLASSINSTALL_HEADER*)&propchange_params, sizeof(propchange_params))) {
|
|
|
|
uprintf("Could not cycle device (D1): %s", WindowsErrorString());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetupDiChangeState(dev_info, &dev_info_data)) {
|
|
|
|
uprintf("Could not cycle device (D2): %s", WindowsErrorString());
|
|
|
|
// If we failed to actually change the status, we must revert the properties
|
|
|
|
ret = ERROR_OPERATION_ABORTED;
|
|
|
|
}
|
|
|
|
Sleep(250);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-enable the device
|
|
|
|
memset(&propchange_params, 0, sizeof(propchange_params));
|
|
|
|
propchange_params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
|
|
|
|
propchange_params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
|
2020-02-15 14:44:34 +00:00
|
|
|
propchange_params.Scope = DICS_FLAG_CONFIGSPECIFIC;
|
2019-05-23 12:09:25 +00:00
|
|
|
propchange_params.StateChange = DICS_ENABLE;
|
|
|
|
if (!SetupDiSetClassInstallParams(dev_info, &dev_info_data,
|
|
|
|
(SP_CLASSINSTALL_HEADER*)&propchange_params, sizeof(propchange_params))) {
|
|
|
|
uprintf("Could not cycle device (E1): %s", WindowsErrorString());
|
|
|
|
ret = ERROR_OPERATION_ABORTED;
|
|
|
|
}
|
|
|
|
if (ret == ERROR_OPERATION_ABORTED)
|
|
|
|
break;
|
|
|
|
if (!SetupDiChangeState(dev_info, &dev_info_data)) {
|
|
|
|
uprintf("Could not cycle device (E2): %s", WindowsErrorString());
|
|
|
|
ret = ERROR_GEN_FAILURE;
|
|
|
|
} else {
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is great: The MS APIs may let you believe that disabling and reenabling was
|
|
|
|
// successful, but leave the device in an actual disabled state... So we can end up
|
|
|
|
// with zombie devices, that are effectively disabled, but that Windows still sees
|
|
|
|
// as enabled... So we need to detect this.
|
|
|
|
if (CM_Get_DevNode_Status(&dev_status, &problem_code, dev_info_data.DevInst, 0) == CR_SUCCESS) {
|
|
|
|
disabled = (dev_status & DN_HAS_PROBLEM) && (problem_code == CM_PROB_DISABLED);
|
|
|
|
if (disabled)
|
|
|
|
ret = ERROR_DEVICE_REINITIALIZATION_NEEDED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupDiDestroyDeviceInfoList(dev_info);
|
|
|
|
if (!found)
|
|
|
|
uprintf("Could not find a device to cycle!");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-27 23:36:28 +00:00
|
|
|
static __inline BOOL IsVHD(const char* buffer)
|
|
|
|
{
|
|
|
|
int i;
|
2015-01-25 00:56:38 +00:00
|
|
|
// List of the Hardware IDs of the VHD devices we know
|
2014-11-27 23:36:28 +00:00
|
|
|
const char* vhd_name[] = {
|
2014-12-20 17:30:12 +00:00
|
|
|
"Arsenal_________Virtual_",
|
|
|
|
"KernSafeVirtual_________",
|
|
|
|
"Msft____Virtual_Disk____",
|
2015-01-08 00:22:56 +00:00
|
|
|
"VMware__VMware_Virtual_S" // Enabled through a cheat mode, as this lists primary disks on VMWare instances
|
2014-11-27 23:36:28 +00:00
|
|
|
};
|
|
|
|
|
2015-01-08 00:22:56 +00:00
|
|
|
for (i = 0; i < (int)(ARRAYSIZE(vhd_name)-(enable_vmdk?0:1)); i++)
|
2014-12-03 18:44:00 +00:00
|
|
|
if (safe_strstr(buffer, vhd_name[i]) != NULL)
|
2014-11-27 23:36:28 +00:00
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-02-16 17:47:07 +00:00
|
|
|
static __inline BOOL IsRemovable(const char* buffer)
|
|
|
|
{
|
|
|
|
switch (*((DWORD*)buffer)) {
|
|
|
|
case CM_REMOVAL_POLICY_EXPECT_SURPRISE_REMOVAL:
|
|
|
|
case CM_REMOVAL_POLICY_EXPECT_ORDERLY_REMOVAL:
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 12:26:06 +00:00
|
|
|
static __inline void ToUpper(char* str)
|
|
|
|
{
|
2017-03-08 20:22:24 +00:00
|
|
|
size_t i;
|
2017-02-20 12:26:06 +00:00
|
|
|
for (i = 0; i < safe_strlen(str); i++)
|
2017-04-25 19:25:50 +00:00
|
|
|
str[i] = (char)toupper(str[i]);
|
2017-02-20 12:26:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 11:47:19 +00:00
|
|
|
BOOL GetOpticalMedia(IMG_SAVE* img_save)
|
|
|
|
{
|
|
|
|
static char str[MAX_PATH];
|
|
|
|
static char label[33];
|
|
|
|
int k;
|
|
|
|
BYTE geometry[256], *buffer = NULL;
|
|
|
|
PDISK_GEOMETRY_EX DiskGeometry = (PDISK_GEOMETRY_EX)(void*)geometry;
|
|
|
|
DWORD i, j, size, datatype;
|
|
|
|
HDEVINFO dev_info = NULL;
|
|
|
|
SP_DEVINFO_DATA dev_info_data;
|
|
|
|
SP_DEVICE_INTERFACE_DATA devint_data;
|
|
|
|
PSP_DEVICE_INTERFACE_DETAIL_DATA_A devint_detail_data;
|
|
|
|
HANDLE hDrive = INVALID_HANDLE_VALUE;
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
|
2019-07-31 21:45:11 +00:00
|
|
|
dev_info = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_CDROM, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
2016-12-06 11:47:19 +00:00
|
|
|
if (dev_info == INVALID_HANDLE_VALUE) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetClassDevs (Interface) failed: %s", WindowsErrorString());
|
2016-12-06 11:47:19 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
dev_info_data.cbSize = sizeof(dev_info_data);
|
|
|
|
for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
|
|
|
|
memset(str, 0, sizeof(str));
|
|
|
|
if (!SetupDiGetDeviceRegistryPropertyU(dev_info, &dev_info_data, SPDRP_FRIENDLYNAME,
|
|
|
|
&datatype, (LPBYTE)str, sizeof(str), &size)) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetDeviceRegistryProperty (Friendly Name) failed: %s", WindowsErrorString());
|
2017-08-10 18:43:04 +00:00
|
|
|
static_strcpy(str, "Generic Optical Drive");
|
2016-12-06 11:47:19 +00:00
|
|
|
}
|
|
|
|
uprintf("Found '%s' optical device", str);
|
|
|
|
devint_data.cbSize = sizeof(devint_data);
|
|
|
|
devint_detail_data = NULL;
|
|
|
|
for (j = 0; ; j++) {
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
safe_free(devint_detail_data);
|
|
|
|
safe_free(buffer);
|
|
|
|
|
2019-07-31 21:45:11 +00:00
|
|
|
if (!SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data, &GUID_DEVINTERFACE_CDROM, j, &devint_data)) {
|
2016-12-06 11:47:19 +00:00
|
|
|
if (GetLastError() != ERROR_NO_MORE_ITEMS) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiEnumDeviceInterfaces failed: %s", WindowsErrorString());
|
2016-12-06 11:47:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, NULL, 0, &size, NULL)) {
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
devint_detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA_A)calloc(1, size);
|
|
|
|
if (devint_detail_data == NULL) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("Unable to allocate data for SP_DEVICE_INTERFACE_DETAIL_DATA");
|
2016-12-06 11:47:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
devint_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
|
|
|
|
} else {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetDeviceInterfaceDetail (dummy) failed: %s", WindowsErrorString());
|
2016-12-06 11:47:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (devint_detail_data == NULL) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetDeviceInterfaceDetail (dummy) - no data was allocated");
|
2016-12-06 11:47:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, devint_detail_data, size, &size, NULL)) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetDeviceInterfaceDetail (actual) failed: %s", WindowsErrorString());
|
2016-12-06 11:47:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the size of the inserted media (if any)
|
|
|
|
hDrive = CreateFileA(devint_detail_data->DevicePath, GENERIC_READ,
|
|
|
|
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
|
|
|
if (hDrive == INVALID_HANDLE_VALUE)
|
|
|
|
continue;
|
|
|
|
if (!DeviceIoControl(hDrive, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
NULL, 0, geometry, sizeof(geometry), &size, NULL))
|
|
|
|
continue;
|
|
|
|
// Rewritable media usually has a one sector
|
|
|
|
if (DiskGeometry->DiskSize.QuadPart <= 4096)
|
|
|
|
continue;
|
|
|
|
// Read the label directly, since it's a massive PITA to get it from Windows
|
|
|
|
li.QuadPart = 0x8000LL;
|
|
|
|
buffer = malloc(2048);
|
|
|
|
if ((buffer != NULL) && (SetFilePointerEx(hDrive, li, NULL, FILE_BEGIN)) &&
|
|
|
|
ReadFile(hDrive, buffer, 2048, &size, NULL) && (size == 2048)) {
|
2017-01-04 17:23:53 +00:00
|
|
|
memcpy(label, &buffer[0x28], sizeof(label) - 1);
|
|
|
|
label[sizeof(label) - 1] = 0;
|
2017-03-08 20:22:24 +00:00
|
|
|
for (k = (int)strlen(label) - 1; (k >= 0) && (label[k] == 0x20); k--)
|
2016-12-06 11:47:19 +00:00
|
|
|
label[k] = 0;
|
|
|
|
img_save->Label = label;
|
|
|
|
}
|
2017-08-10 18:43:04 +00:00
|
|
|
static_strcpy(str, devint_detail_data->DevicePath);
|
2016-12-06 11:47:19 +00:00
|
|
|
img_save->DevicePath = str;
|
|
|
|
img_save->DeviceSize = DiskGeometry->DiskSize.QuadPart;
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
safe_free(devint_detail_data);
|
|
|
|
safe_free(buffer);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-09-06 16:47:39 +00:00
|
|
|
/* For debugging user reports of HDDs vs UFDs */
|
|
|
|
//#define FORCED_DEVICE
|
|
|
|
#ifdef FORCED_DEVICE
|
2021-04-23 11:03:43 +00:00
|
|
|
#define FORCED_VID 0x048D
|
2023-01-27 17:23:08 +00:00
|
|
|
#define FORCED_PID 0x4030
|
|
|
|
#define FORCED_NAME "HP iLO Internal SD-CARD USB Device"
|
2015-09-06 16:47:39 +00:00
|
|
|
#endif
|
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
void ClearDrives(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_DRIVES && rufus_drive[i].size != 0; i++) {
|
|
|
|
free(rufus_drive[i].id);
|
|
|
|
free(rufus_drive[i].name);
|
|
|
|
free(rufus_drive[i].display_name);
|
|
|
|
free(rufus_drive[i].label);
|
|
|
|
free(rufus_drive[i].hub);
|
|
|
|
}
|
|
|
|
memset(rufus_drive, 0, sizeof(rufus_drive));
|
|
|
|
}
|
|
|
|
|
2014-05-17 23:37:01 +00:00
|
|
|
/*
|
|
|
|
* Refresh the list of USB devices
|
|
|
|
*/
|
2016-04-26 12:57:07 +00:00
|
|
|
BOOL GetDevices(DWORD devnum)
|
2014-05-17 23:37:01 +00:00
|
|
|
{
|
2016-02-16 17:47:07 +00:00
|
|
|
// List of USB storage drivers we know - list may be incomplete!
|
|
|
|
const char* usbstor_name[] = {
|
|
|
|
// Standard MS USB storage driver
|
|
|
|
"USBSTOR",
|
|
|
|
// USB card readers, with proprietary drivers (Realtek,etc...)
|
|
|
|
// Mostly "guessed" from http://www.carrona.org/dvrref.php
|
|
|
|
"RTSUER", "CMIUCR", "EUCR",
|
|
|
|
// UASP Drivers *MUST* be listed after this, starting with "UASPSTOR"
|
|
|
|
// (which is Microsoft's native UASP driver for Windows 8 and later)
|
|
|
|
// as we use "UASPSTOR" as a delimiter
|
|
|
|
"UASPSTOR", "VUSBSTOR", "ETRONSTOR", "ASUSSTPT"
|
|
|
|
};
|
|
|
|
// These are the generic (non USB) storage enumerators we also test
|
|
|
|
const char* genstor_name[] = {
|
|
|
|
// Generic storage drivers (Careful now!)
|
|
|
|
"SCSI", // "STORAGE", // "STORAGE" is used by 'Storage Spaces" and stuff => DANGEROUS!
|
2016-06-02 20:20:29 +00:00
|
|
|
// Non-USB card reader drivers - This list *MUST* start with "SD" (delimiter)
|
2016-02-16 17:47:07 +00:00
|
|
|
// See http://itdoc.hitachi.co.jp/manuals/3021/30213B5200e/DMDS0094.HTM
|
2019-05-23 12:09:25 +00:00
|
|
|
// Also http://www.carrona.org/dvrref.php. NB: All members from this list should have
|
|
|
|
// been reported as enumerators by Rufus, when Enum Debug is enabled.
|
2019-03-27 12:21:41 +00:00
|
|
|
"SD", "PCISTOR", "RTSOR", "JMCR", "JMCF", "RIMMPTSK", "RIMSPTSK", "RISD", "RIXDPTSK",
|
2021-09-09 20:20:57 +00:00
|
|
|
"TI21SONY", "ESD7SK", "ESM7SK", "O2MD", "O2SD", "VIACR", "GLREADER"
|
2016-02-16 17:47:07 +00:00
|
|
|
};
|
2016-06-05 23:36:56 +00:00
|
|
|
// Oh, and we also have card devices (e.g. 'SCSI\DiskO2Micro_SD_...') under the SCSI enumerator...
|
|
|
|
const char* scsi_disk_prefix = "SCSI\\Disk";
|
|
|
|
const char* scsi_card_name[] = {
|
2017-08-13 20:03:01 +00:00
|
|
|
"_SD_", "_SDHC_", "_MMC_", "_MS_", "_MSPro_", "_xDPicture_", "_O2Media_"
|
2016-06-05 23:36:56 +00:00
|
|
|
};
|
2019-07-31 21:45:11 +00:00
|
|
|
const char* usb_speed_name[USB_SPEED_MAX] = { "USB", "USB 1.0", "USB 1.1", "USB 2.0", "USB 3.0", "USB 3.1" };
|
2019-07-15 11:35:22 +00:00
|
|
|
const char* windows_sandbox_vhd_label = "PortableBaseLayer";
|
2014-05-17 23:37:01 +00:00
|
|
|
// Hash table and String Array used to match a Device ID with the parent hub's Device Interface Path
|
|
|
|
htab_table htab_devid = HTAB_EMPTY;
|
|
|
|
StrArray dev_if_path;
|
|
|
|
char letter_name[] = " (?:)";
|
2016-02-16 17:47:07 +00:00
|
|
|
char drive_name[] = "?:\\";
|
2022-06-14 22:29:33 +00:00
|
|
|
char setting_name[32];
|
2015-03-18 21:34:28 +00:00
|
|
|
char uefi_togo_check[] = "?:\\EFI\\Rufus\\ntfs_x64.efi";
|
2017-08-11 10:43:37 +00:00
|
|
|
char scsi_card_name_copy[16];
|
2016-02-16 17:47:07 +00:00
|
|
|
BOOL r = FALSE, found = FALSE, post_backslash;
|
2014-05-17 23:37:01 +00:00
|
|
|
HDEVINFO dev_info = NULL;
|
|
|
|
SP_DEVINFO_DATA dev_info_data;
|
|
|
|
SP_DEVICE_INTERFACE_DATA devint_data;
|
|
|
|
PSP_DEVICE_INTERFACE_DETAIL_DATA_A devint_detail_data;
|
2016-02-21 19:58:48 +00:00
|
|
|
DEVINST parent_inst, grandparent_inst, device_inst;
|
2019-05-23 12:09:25 +00:00
|
|
|
DWORD size, i, j, k, l, data_type, drive_index;
|
2016-02-16 17:47:07 +00:00
|
|
|
DWORD uasp_start = ARRAYSIZE(usbstor_name), card_start = ARRAYSIZE(genstor_name);
|
|
|
|
ULONG list_size[ARRAYSIZE(usbstor_name)] = { 0 }, list_start[ARRAYSIZE(usbstor_name)] = { 0 }, full_list_size, ulFlags;
|
2014-05-17 23:37:01 +00:00
|
|
|
HANDLE hDrive;
|
|
|
|
LONG maxwidth = 0;
|
2022-05-25 12:21:36 +00:00
|
|
|
int s, u, v, score, drive_number, remove_drive, num_drives = 0;
|
|
|
|
char drive_letters[27], *device_id, *devid_list = NULL, display_msg[128];
|
|
|
|
char *p, *label, *display_name, buffer[MAX_PATH], str[MAX_PATH], device_instance_id[MAX_PATH], *method_str, *hub_path;
|
2022-06-14 22:29:33 +00:00
|
|
|
uint32_t ignore_vid_pid[MAX_IGNORE_USB];
|
2014-05-17 23:37:01 +00:00
|
|
|
usb_device_props props;
|
|
|
|
|
|
|
|
IGNORE_RETVAL(ComboBox_ResetContent(hDeviceList));
|
2022-05-25 12:21:36 +00:00
|
|
|
ClearDrives();
|
2014-05-17 23:37:01 +00:00
|
|
|
StrArrayCreate(&dev_if_path, 128);
|
2015-05-28 17:47:53 +00:00
|
|
|
// Add a dummy for string index zero, as this is what non matching hashes will point to
|
2016-12-13 14:21:51 +00:00
|
|
|
StrArrayAdd(&dev_if_path, "", TRUE);
|
2014-05-18 23:56:17 +00:00
|
|
|
|
|
|
|
device_id = (char*)malloc(MAX_PATH);
|
|
|
|
if (device_id == NULL)
|
|
|
|
goto out;
|
2014-05-17 23:37:01 +00:00
|
|
|
|
2019-07-31 21:45:11 +00:00
|
|
|
// Build a hash table associating a CM Device ID of a USB device with the SetupDI Device Interface Path
|
2014-05-17 23:37:01 +00:00
|
|
|
// of its parent hub - this is needed to retrieve the device speed
|
2019-07-31 21:45:11 +00:00
|
|
|
dev_info = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_USB_HUB, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
2014-05-17 23:37:01 +00:00
|
|
|
if (dev_info != INVALID_HANDLE_VALUE) {
|
2014-05-18 23:56:17 +00:00
|
|
|
if (htab_create(DEVID_HTAB_SIZE, &htab_devid)) {
|
2014-05-17 23:37:01 +00:00
|
|
|
dev_info_data.cbSize = sizeof(dev_info_data);
|
|
|
|
for (i=0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
|
2015-10-03 22:47:06 +00:00
|
|
|
uuprintf("Processing Hub %d:", i + 1);
|
2014-05-17 23:37:01 +00:00
|
|
|
devint_detail_data = NULL;
|
|
|
|
devint_data.cbSize = sizeof(devint_data);
|
|
|
|
// Only care about the first interface (MemberIndex 0)
|
2019-07-31 21:45:11 +00:00
|
|
|
if ( (SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data, &GUID_DEVINTERFACE_USB_HUB, 0, &devint_data))
|
2015-11-05 21:32:08 +00:00
|
|
|
&& (!SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, NULL, 0, &size, NULL))
|
|
|
|
&& (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
2014-05-17 23:37:01 +00:00
|
|
|
&& ((devint_detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA_A)calloc(1, size)) != NULL) ) {
|
|
|
|
devint_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
|
|
|
|
if (SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, devint_detail_data, size, &size, NULL)) {
|
2014-05-18 23:56:17 +00:00
|
|
|
|
|
|
|
// Find the Device IDs for all the children of this hub
|
2014-05-17 23:37:01 +00:00
|
|
|
if (CM_Get_Child(&device_inst, dev_info_data.DevInst, 0) == CR_SUCCESS) {
|
|
|
|
device_id[0] = 0;
|
2016-12-13 14:21:51 +00:00
|
|
|
s = StrArrayAdd(&dev_if_path, devint_detail_data->DevicePath, TRUE);
|
2015-10-03 22:47:06 +00:00
|
|
|
uuprintf(" Hub[%d] = '%s'", s, devint_detail_data->DevicePath);
|
2014-05-18 23:56:17 +00:00
|
|
|
if ((s>= 0) && (CM_Get_Device_IDA(device_inst, device_id, MAX_PATH, 0) == CR_SUCCESS)) {
|
2017-02-20 12:26:06 +00:00
|
|
|
ToUpper(device_id);
|
2014-05-17 23:37:01 +00:00
|
|
|
if ((k = htab_hash(device_id, &htab_devid)) != 0) {
|
2014-05-18 23:56:17 +00:00
|
|
|
htab_devid.table[k].data = (void*)(uintptr_t)s;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
2015-10-03 22:47:06 +00:00
|
|
|
uuprintf(" Found ID[%03d]: %s", k, device_id);
|
2014-05-17 23:37:01 +00:00
|
|
|
while (CM_Get_Sibling(&device_inst, device_inst, 0) == CR_SUCCESS) {
|
|
|
|
device_id[0] = 0;
|
2014-05-18 23:56:17 +00:00
|
|
|
if (CM_Get_Device_IDA(device_inst, device_id, MAX_PATH, 0) == CR_SUCCESS) {
|
2017-02-20 12:26:06 +00:00
|
|
|
ToUpper(device_id);
|
2014-05-17 23:37:01 +00:00
|
|
|
if ((k = htab_hash(device_id, &htab_devid)) != 0) {
|
2014-05-18 23:56:17 +00:00
|
|
|
htab_devid.table[k].data = (void*)(uintptr_t)s;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
2015-10-03 22:47:06 +00:00
|
|
|
uuprintf(" Found ID[%03d]: %s", k, device_id);
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(devint_detail_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SetupDiDestroyDeviceInfoList(dev_info);
|
|
|
|
}
|
2014-05-18 23:56:17 +00:00
|
|
|
free(device_id);
|
2014-05-17 23:37:01 +00:00
|
|
|
|
2014-05-18 23:56:17 +00:00
|
|
|
// Build a single list of Device IDs from all the storage enumerators we know of
|
2014-05-17 23:37:01 +00:00
|
|
|
full_list_size = 0;
|
2017-11-13 14:29:48 +00:00
|
|
|
ulFlags = CM_GETIDLIST_FILTER_SERVICE | CM_GETIDLIST_FILTER_PRESENT;
|
2016-02-16 17:47:07 +00:00
|
|
|
for (s=0; s<ARRAYSIZE(usbstor_name); s++) {
|
2014-05-18 23:56:17 +00:00
|
|
|
// Get a list of device IDs for all USB storage devices
|
|
|
|
// This will be used to find if a device is UASP
|
2016-02-16 17:47:07 +00:00
|
|
|
// Also compute the uasp_start index
|
|
|
|
if (strcmp(usbstor_name[s], "UASPSTOR") == 0)
|
|
|
|
uasp_start = s;
|
|
|
|
if (CM_Get_Device_ID_List_SizeA(&list_size[s], usbstor_name[s], ulFlags) != CR_SUCCESS)
|
2014-06-08 17:02:03 +00:00
|
|
|
list_size[s] = 0;
|
2014-05-17 23:37:01 +00:00
|
|
|
if (list_size[s] != 0)
|
|
|
|
full_list_size += list_size[s]-1; // remove extra NUL terminator
|
|
|
|
}
|
2016-02-16 17:47:07 +00:00
|
|
|
// Compute the card_start index
|
2022-06-14 22:29:33 +00:00
|
|
|
for (s = 0; s < ARRAYSIZE(genstor_name); s++) {
|
2016-02-16 17:47:07 +00:00
|
|
|
if (strcmp(genstor_name[s], "SD") == 0)
|
|
|
|
card_start = s;
|
|
|
|
}
|
2018-06-30 21:45:15 +00:00
|
|
|
|
2022-06-14 22:29:33 +00:00
|
|
|
// Build the list of USB devices we may want to ignore
|
|
|
|
for (s = 0; s < ARRAYSIZE(ignore_vid_pid); s++) {
|
|
|
|
static_sprintf(setting_name, "IgnoreUsb%02d", s + 1);
|
|
|
|
ignore_vid_pid[s] = ReadSetting32(setting_name);
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:45:15 +00:00
|
|
|
// Better safe than sorry. And yeah, we could have used arrays of
|
|
|
|
// arrays to avoid this, but it's more readable this way.
|
|
|
|
assert((uasp_start > 0) && (uasp_start < ARRAYSIZE(usbstor_name)));
|
|
|
|
assert((card_start > 0) && (card_start < ARRAYSIZE(genstor_name)));
|
|
|
|
|
2014-05-20 18:28:46 +00:00
|
|
|
devid_list = NULL;
|
|
|
|
if (full_list_size != 0) {
|
|
|
|
full_list_size += 1; // add extra NUL terminator
|
|
|
|
devid_list = (char*)malloc(full_list_size);
|
|
|
|
if (devid_list == NULL) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("Could not allocate Device ID list");
|
2016-02-16 17:47:07 +00:00
|
|
|
goto out;
|
2014-05-20 18:28:46 +00:00
|
|
|
}
|
2016-02-16 17:47:07 +00:00
|
|
|
for (s=0, i=0; s<ARRAYSIZE(usbstor_name); s++) {
|
2015-05-28 17:47:53 +00:00
|
|
|
list_start[s] = i;
|
2014-05-20 18:28:46 +00:00
|
|
|
if (list_size[s] > 1) {
|
2016-02-16 17:47:07 +00:00
|
|
|
if (CM_Get_Device_ID_ListA(usbstor_name[s], &devid_list[i], list_size[s], ulFlags) != CR_SUCCESS)
|
2014-06-08 17:02:03 +00:00
|
|
|
continue;
|
2015-06-26 21:49:32 +00:00
|
|
|
if (usb_debug) {
|
2016-02-16 17:47:07 +00:00
|
|
|
uprintf("Processing IDs belonging to '%s':", usbstor_name[s]);
|
2015-06-26 21:49:32 +00:00
|
|
|
for (device_id = &devid_list[i]; *device_id != 0; device_id += strlen(device_id) + 1)
|
|
|
|
uprintf(" %s", device_id);
|
|
|
|
}
|
2014-05-20 18:28:46 +00:00
|
|
|
// The list_size is sometimes larger than required thus we need to find the real end
|
|
|
|
for (i += list_size[s]; i > 2; i--) {
|
|
|
|
if ((devid_list[i-2] != '\0') && (devid_list[i-1] == '\0') && (devid_list[i] == '\0'))
|
|
|
|
break;
|
|
|
|
}
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 17:47:07 +00:00
|
|
|
// Now use SetupDi to enumerate all our disk storage devices
|
2019-07-31 21:45:11 +00:00
|
|
|
dev_info = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_DISK, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
2014-05-18 23:56:17 +00:00
|
|
|
if (dev_info == INVALID_HANDLE_VALUE) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetClassDevs (Interface) failed: %s", WindowsErrorString());
|
2014-05-18 23:56:17 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2014-05-17 23:37:01 +00:00
|
|
|
dev_info_data.cbSize = sizeof(dev_info_data);
|
2022-05-25 12:21:36 +00:00
|
|
|
for (i = 0; num_drives < MAX_DRIVES && SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
|
2014-05-17 23:37:01 +00:00
|
|
|
memset(buffer, 0, sizeof(buffer));
|
2016-02-16 17:47:07 +00:00
|
|
|
memset(&props, 0, sizeof(props));
|
2015-05-28 17:47:53 +00:00
|
|
|
method_str = "";
|
2017-09-15 11:40:33 +00:00
|
|
|
hub_path = NULL;
|
2014-05-17 23:37:01 +00:00
|
|
|
if (!SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_ENUMERATOR_NAME,
|
2019-05-23 12:09:25 +00:00
|
|
|
&data_type, (LPBYTE)buffer, sizeof(buffer), &size)) {
|
|
|
|
uprintf("SetupDiGetDeviceRegistryProperty (Enumerator Name) failed: %s", WindowsErrorString());
|
2014-05-17 23:37:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-16 17:47:07 +00:00
|
|
|
|
|
|
|
for (j = 0; j < ARRAYSIZE(usbstor_name); j++) {
|
|
|
|
if (safe_stricmp(buffer, usbstor_name[0]) == 0) {
|
|
|
|
props.is_USB = TRUE;
|
|
|
|
if ((j != 0) && (j < uasp_start))
|
|
|
|
props.is_CARD = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UASP drives are listed under SCSI, and we also have non USB card readers to populate
|
|
|
|
for (j = 0; j < ARRAYSIZE(genstor_name); j++) {
|
|
|
|
if (safe_stricmp(buffer, genstor_name[j]) == 0) {
|
|
|
|
props.is_SCSI = TRUE;
|
|
|
|
if (j >= card_start)
|
|
|
|
props.is_CARD = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uuprintf("Processing '%s' device:", buffer);
|
|
|
|
if ((!props.is_USB) && (!props.is_SCSI)) {
|
2021-09-09 20:20:57 +00:00
|
|
|
uuprintf(" Unsupported or disabled by policy");
|
2014-05-17 23:37:01 +00:00
|
|
|
continue;
|
2016-02-16 17:47:07 +00:00
|
|
|
}
|
2014-12-20 17:30:12 +00:00
|
|
|
|
|
|
|
// We can't use the friendly name to find if a drive is a VHD, as friendly name string gets translated
|
|
|
|
// according to your locale, so we poke the Hardware ID
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
props.is_VHD = SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_HARDWAREID,
|
2019-05-23 12:09:25 +00:00
|
|
|
&data_type, (LPBYTE)buffer, sizeof(buffer), &size) && IsVHD(buffer);
|
2016-06-05 23:36:56 +00:00
|
|
|
// Additional detection for SCSI card readers
|
|
|
|
if ((!props.is_CARD) && (safe_strnicmp(buffer, scsi_disk_prefix, sizeof(scsi_disk_prefix)-1) == 0)) {
|
|
|
|
for (j = 0; j < ARRAYSIZE(scsi_card_name); j++) {
|
2017-08-11 10:43:37 +00:00
|
|
|
static_strcpy(scsi_card_name_copy, scsi_card_name[j]);
|
|
|
|
if (safe_strstr(buffer, scsi_card_name_copy) != NULL) {
|
|
|
|
props.is_CARD = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Also test for "_SD&" instead of "_SD_" and so on to allow for devices like
|
|
|
|
// "SCSI\DiskRicoh_Storage_SD&REV_3.0" to be detected.
|
2022-01-05 11:57:26 +00:00
|
|
|
assert(strlen(scsi_card_name_copy) > 1);
|
2017-08-11 10:43:37 +00:00
|
|
|
scsi_card_name_copy[strlen(scsi_card_name_copy) - 1] = '&';
|
|
|
|
if (safe_strstr(buffer, scsi_card_name_copy) != NULL) {
|
2016-06-05 23:36:56 +00:00
|
|
|
props.is_CARD = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 17:47:07 +00:00
|
|
|
uuprintf(" Hardware ID: '%s'", buffer);
|
|
|
|
|
2019-05-23 12:09:25 +00:00
|
|
|
// Keep track of the Device Instance ID, which we'll need to "reset" the device
|
|
|
|
if (!SetupDiGetDeviceInstanceIdA(dev_info, &dev_info_data, device_instance_id,
|
|
|
|
sizeof(device_instance_id), &size)) {
|
|
|
|
uprintf("SetupDiGetDeviceInstanceId failed: %s", WindowsErrorString());
|
2022-01-05 11:57:26 +00:00
|
|
|
strcpy(device_instance_id, "<N/A>");
|
2019-05-23 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 17:47:07 +00:00
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
props.is_Removable = SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_REMOVAL_POLICY,
|
2019-05-23 12:09:25 +00:00
|
|
|
&data_type, (LPBYTE)buffer, sizeof(buffer), &size) && IsRemovable(buffer);
|
2014-12-20 17:30:12 +00:00
|
|
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
2016-06-05 19:21:16 +00:00
|
|
|
if (!SetupDiGetDeviceRegistryPropertyU(dev_info, &dev_info_data, SPDRP_FRIENDLYNAME,
|
2019-05-23 12:09:25 +00:00
|
|
|
&data_type, (LPBYTE)buffer, sizeof(buffer), &size)) {
|
|
|
|
uprintf("SetupDiGetDeviceRegistryProperty (Friendly Name) failed: %s", WindowsErrorString());
|
2014-05-17 23:37:01 +00:00
|
|
|
// We can afford a failure on this call - just replace the name with "USB Storage Device (Generic)"
|
2017-08-10 18:43:04 +00:00
|
|
|
static_strcpy(buffer, lmprintf(MSG_045));
|
2014-12-20 17:30:12 +00:00
|
|
|
} else if ((!props.is_VHD) && (devid_list != NULL)) {
|
2014-05-18 23:56:17 +00:00
|
|
|
// Get the properties of the device. We could avoid doing this lookup every time by keeping
|
2014-05-17 23:37:01 +00:00
|
|
|
// a lookup table, but there shouldn't be that many USB storage devices connected...
|
2016-02-16 17:47:07 +00:00
|
|
|
// NB: Each of these Device IDs should have a child, from which we get the Device Instance match.
|
2014-05-20 18:28:46 +00:00
|
|
|
for (device_id = devid_list; *device_id != 0; device_id += strlen(device_id) + 1) {
|
2016-02-16 17:47:07 +00:00
|
|
|
if (CM_Locate_DevNodeA(&parent_inst, device_id, 0) != CR_SUCCESS) {
|
|
|
|
uuprintf("Could not locate device node for '%s'", device_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (CM_Get_Child(&device_inst, parent_inst, 0) != CR_SUCCESS) {
|
|
|
|
uuprintf("Could not get children of '%s'", device_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (device_inst != dev_info_data.DevInst) {
|
|
|
|
// Try the siblings
|
2016-02-21 19:58:48 +00:00
|
|
|
while (CM_Get_Sibling(&device_inst, device_inst, 0) == CR_SUCCESS) {
|
|
|
|
if (device_inst == dev_info_data.DevInst) {
|
2016-02-16 17:47:07 +00:00
|
|
|
uuprintf("NOTE: Matched instance from sibling for '%s'", device_id);
|
|
|
|
break;
|
2015-08-22 20:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 17:47:07 +00:00
|
|
|
if (device_inst != dev_info_data.DevInst)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
post_backslash = FALSE;
|
|
|
|
method_str = "";
|
|
|
|
|
|
|
|
// If we're not dealing with the USBSTOR part of our list, then this is an UASP device
|
|
|
|
props.is_UASP = ((((uintptr_t)device_id)+2) >= ((uintptr_t)devid_list)+list_start[uasp_start]);
|
|
|
|
// Now get the properties of the device, and its Device ID, which we need to populate the properties
|
2017-02-20 12:26:06 +00:00
|
|
|
ToUpper(device_id);
|
2016-02-16 17:47:07 +00:00
|
|
|
j = htab_hash(device_id, &htab_devid);
|
|
|
|
uuprintf(" Matched with ID[%03d]: %s", j, device_id);
|
|
|
|
|
|
|
|
// Try to parse the current device_id string for VID:PID
|
|
|
|
// We'll use that if we can't get anything better
|
|
|
|
for (k = 0, l = 0; (k<strlen(device_id)) && (l<2); k++) {
|
|
|
|
// The ID is in the form USB_VENDOR_BUSID\VID_xxxx&PID_xxxx\...
|
|
|
|
if (device_id[k] == '\\')
|
|
|
|
post_backslash = TRUE;
|
|
|
|
if (!post_backslash)
|
|
|
|
continue;
|
|
|
|
if (device_id[k] == '_') {
|
|
|
|
props.pid = (uint16_t)strtoul(&device_id[k + 1], NULL, 16);
|
|
|
|
if (l++ == 0)
|
|
|
|
props.vid = props.pid;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
2016-02-16 17:47:07 +00:00
|
|
|
}
|
|
|
|
if (props.vid != 0)
|
|
|
|
method_str = "[ID]";
|
|
|
|
|
|
|
|
// If the hash didn't match a populated string in dev_if_path[] (htab_devid.table[j].data > 0),
|
|
|
|
// we might have an extra vendor driver in between (e.g. "ASUS USB 3.0 Boost Storage Driver"
|
|
|
|
// for UASP devices in ASUS "Turbo Mode" or "Apple Mobile Device USB Driver" for iPods)
|
|
|
|
// so try to see if we can match the grandparent.
|
|
|
|
if ( ((uintptr_t)htab_devid.table[j].data == 0)
|
|
|
|
&& (CM_Get_Parent(&grandparent_inst, parent_inst, 0) == CR_SUCCESS)
|
|
|
|
&& (CM_Get_Device_IDA(grandparent_inst, str, MAX_PATH, 0) == CR_SUCCESS) ) {
|
|
|
|
device_id = str;
|
|
|
|
method_str = "[GP]";
|
2017-02-20 12:26:06 +00:00
|
|
|
ToUpper(device_id);
|
2016-02-16 17:47:07 +00:00
|
|
|
j = htab_hash(device_id, &htab_devid);
|
|
|
|
uuprintf(" Matched with (GP) ID[%03d]: %s", j, device_id);
|
|
|
|
}
|
|
|
|
if ((uintptr_t)htab_devid.table[j].data > 0) {
|
|
|
|
uuprintf(" Matched with Hub[%d]: '%s'", (uintptr_t)htab_devid.table[j].data,
|
|
|
|
dev_if_path.String[(uintptr_t)htab_devid.table[j].data]);
|
2017-09-15 11:40:33 +00:00
|
|
|
if (GetUSBProperties(dev_if_path.String[(uintptr_t)htab_devid.table[j].data], device_id, &props)) {
|
2016-02-16 17:47:07 +00:00
|
|
|
method_str = "";
|
2017-09-15 11:40:33 +00:00
|
|
|
hub_path = dev_if_path.String[(uintptr_t)htab_devid.table[j].data];
|
|
|
|
}
|
2015-09-06 16:47:39 +00:00
|
|
|
#ifdef FORCED_DEVICE
|
2016-02-16 17:47:07 +00:00
|
|
|
props.vid = FORCED_VID;
|
|
|
|
props.pid = FORCED_PID;
|
2017-08-10 18:43:04 +00:00
|
|
|
static_strcpy(buffer, FORCED_NAME);
|
2015-09-06 16:47:39 +00:00
|
|
|
#endif
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
2016-02-16 17:47:07 +00:00
|
|
|
break;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (props.is_VHD) {
|
2015-05-28 17:47:53 +00:00
|
|
|
uprintf("Found VHD device '%s'", buffer);
|
2016-02-16 17:47:07 +00:00
|
|
|
} else if ((props.is_CARD) && ((!props.is_USB) || ((props.vid == 0) && (props.pid == 0)))) {
|
|
|
|
uprintf("Found card reader device '%s'", buffer);
|
2016-04-26 12:57:07 +00:00
|
|
|
} else if ((!props.is_USB) && (!props.is_UASP) && (props.is_Removable)) {
|
|
|
|
if (!list_non_usb_removable_drives) {
|
2016-06-05 23:36:56 +00:00
|
|
|
uprintf("Found non-USB removable device '%s' => Eliminated", buffer);
|
2016-04-26 12:57:07 +00:00
|
|
|
uuprintf("If you *REALLY* need, you can enable listing of this device with <Ctrl><Alt><F>");
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-05 23:36:56 +00:00
|
|
|
uprintf("Found non-USB removable device '%s'", buffer);
|
2014-05-17 23:37:01 +00:00
|
|
|
} else {
|
|
|
|
if ((props.vid == 0) && (props.pid == 0)) {
|
2016-02-16 17:47:07 +00:00
|
|
|
if (!props.is_USB) {
|
|
|
|
// If we have a non removable SCSI drive and couldn't get a VID:PID,
|
|
|
|
// we are most likely dealing with a system drive => eliminate it!
|
2016-04-26 12:57:07 +00:00
|
|
|
uuprintf("Found non-USB non-removable device '%s' => Eliminated", buffer);
|
2014-05-17 23:37:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-08-10 18:43:04 +00:00
|
|
|
static_strcpy(str, "????:????"); // Couldn't figure VID:PID
|
2014-05-17 23:37:01 +00:00
|
|
|
} else {
|
2022-06-14 22:29:33 +00:00
|
|
|
static_sprintf(str, "%04X:%04X", props.vid, props.pid);
|
2020-04-15 15:50:36 +00:00
|
|
|
// I *REALLY* don't want to erase the devices below by accident.
|
|
|
|
if (its_a_me_mario) {
|
|
|
|
if ((props.vid == 0x0525) && (props.pid == 0x622b))
|
|
|
|
continue;
|
|
|
|
if ((props.vid == 0x0781) && (props.pid == 0x75a0))
|
|
|
|
continue;
|
2022-06-24 13:27:02 +00:00
|
|
|
if ((props.vid == 0x10d6) && (props.pid == 0x1101))
|
|
|
|
continue;
|
2020-04-15 15:50:36 +00:00
|
|
|
}
|
2022-06-24 13:27:02 +00:00
|
|
|
// Also ignore USB devices that have been specifically flagged by the user
|
2022-06-14 22:29:33 +00:00
|
|
|
for (s = 0; s < ARRAYSIZE(ignore_vid_pid); s++) {
|
|
|
|
if ((props.vid == (ignore_vid_pid[s] >> 16)) && (props.pid == (ignore_vid_pid[s] & 0xffff))) {
|
|
|
|
uprintf("Ignoring '%s' (%s), per user settings", buffer, str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s < ARRAYSIZE(ignore_vid_pid))
|
|
|
|
continue;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
if (props.speed >= USB_SPEED_MAX)
|
|
|
|
props.speed = 0;
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("Found %s%s%s device '%s' (%s) %s", props.is_UASP?"UAS (":"",
|
2015-05-28 17:47:53 +00:00
|
|
|
usb_speed_name[props.speed], props.is_UASP?")":"", buffer, str, method_str);
|
2019-07-31 21:45:11 +00:00
|
|
|
if (props.lower_speed)
|
|
|
|
uprintf("NOTE: This device is a USB 3.%c device operating at lower speed...", '0' + props.lower_speed - 1);
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
devint_data.cbSize = sizeof(devint_data);
|
|
|
|
devint_detail_data = NULL;
|
2020-09-09 16:00:39 +00:00
|
|
|
for (j = 0; ; j++) {
|
2014-05-17 23:37:01 +00:00
|
|
|
safe_free(devint_detail_data);
|
|
|
|
|
2019-07-31 21:45:11 +00:00
|
|
|
if (!SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data, &GUID_DEVINTERFACE_DISK, j, &devint_data)) {
|
2014-05-17 23:37:01 +00:00
|
|
|
if(GetLastError() != ERROR_NO_MORE_ITEMS) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiEnumDeviceInterfaces failed: %s", WindowsErrorString());
|
2014-05-17 23:37:01 +00:00
|
|
|
} else {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("A device was eliminated because it didn't report itself as a disk");
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, NULL, 0, &size, NULL)) {
|
|
|
|
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
devint_detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA_A)calloc(1, size);
|
|
|
|
if (devint_detail_data == NULL) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("Unable to allocate data for SP_DEVICE_INTERFACE_DETAIL_DATA");
|
2014-05-17 23:37:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
devint_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
|
|
|
|
} else {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetDeviceInterfaceDetail (dummy) failed: %s", WindowsErrorString());
|
2014-05-17 23:37:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (devint_detail_data == NULL) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetDeviceInterfaceDetail (dummy) - no data was allocated");
|
2014-05-17 23:37:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, devint_detail_data, size, &size, NULL)) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("SetupDiGetDeviceInterfaceDetail (actual) failed: %s", WindowsErrorString());
|
2014-05-17 23:37:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:56:07 +00:00
|
|
|
hDrive = CreateFileA(devint_detail_data->DevicePath, GENERIC_READ|GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2014-05-17 23:37:01 +00:00
|
|
|
if(hDrive == INVALID_HANDLE_VALUE) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("Could not open '%s': %s", devint_detail_data->DevicePath, WindowsErrorString());
|
2014-05-17 23:37:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
drive_number = GetDriveNumber(hDrive, devint_detail_data->DevicePath);
|
2020-09-09 16:00:39 +00:00
|
|
|
CloseHandle(hDrive);
|
2014-05-17 23:37:01 +00:00
|
|
|
if (drive_number < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
drive_index = drive_number + DRIVE_INDEX_MIN;
|
|
|
|
if (!IsMediaPresent(drive_index)) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("Device eliminated because it appears to contain no media");
|
2014-05-17 23:37:01 +00:00
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-20 22:52:32 +00:00
|
|
|
if (GetDriveSize(drive_index) < (MIN_DRIVE_SIZE*MB)) {
|
2019-05-23 12:09:25 +00:00
|
|
|
uprintf("Device eliminated because it is smaller than %d MB", MIN_DRIVE_SIZE);
|
2016-02-19 12:10:17 +00:00
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
|
|
|
}
|
2014-05-17 23:37:01 +00:00
|
|
|
|
|
|
|
if (GetDriveLabel(drive_index, drive_letters, &label)) {
|
2016-02-16 17:47:07 +00:00
|
|
|
if ((props.is_SCSI) && (!props.is_UASP) && (!props.is_VHD)) {
|
|
|
|
if (!props.is_Removable) {
|
|
|
|
// Non removables should have been eliminated above, but since we
|
|
|
|
// are potentially dealing with system drives, better safe than sorry
|
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!list_non_usb_removable_drives) {
|
|
|
|
// Go over the mounted partitions and find if GetDriveType() says they are
|
|
|
|
// removable. If they are not removable, don't allow the drive to be listed
|
|
|
|
for (p = drive_letters; *p; p++) {
|
|
|
|
drive_name[0] = *p;
|
|
|
|
if (GetDriveTypeA(drive_name) != DRIVE_REMOVABLE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*p) {
|
|
|
|
uprintf("Device eliminated because it contains a mounted partition that is set as non-removable");
|
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((!enable_HDDs) && (!props.is_VHD) && (!props.is_CARD) &&
|
2014-05-17 23:37:01 +00:00
|
|
|
((score = IsHDD(drive_index, (uint16_t)props.vid, (uint16_t)props.pid, buffer)) > 0)) {
|
2016-02-16 17:47:07 +00:00
|
|
|
uprintf("Device eliminated because it was detected as a Hard Drive (score %d > 0)", score);
|
|
|
|
if (!list_non_usb_removable_drives)
|
|
|
|
uprintf("If this device is not a Hard Drive, please e-mail the author of this application");
|
2018-05-19 12:01:35 +00:00
|
|
|
uprintf("NOTE: You can enable the listing of Hard Drives under 'advanced drive properties'");
|
2014-05-17 23:37:01 +00:00
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
2021-09-09 20:20:57 +00:00
|
|
|
} else if ((!enable_HDDs) && (props.is_CARD) && (GetDriveSize(drive_index) > MAX_DEFAULT_LIST_CARD_SIZE * GB)) {
|
|
|
|
uprintf("Device eliminated because it was detected as a card larger than %d GB", MAX_DEFAULT_LIST_CARD_SIZE);
|
|
|
|
uprintf("To use such a card, check 'List USB Hard Drives' under 'advanced drive properties'");
|
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
2019-07-15 11:35:22 +00:00
|
|
|
// Windows 10 19H1 mounts a 'PortableBaseLayer' for its Windows Sandbox feature => unlist those
|
|
|
|
if (safe_strcmp(label, windows_sandbox_vhd_label) == 0) {
|
2020-04-27 09:28:00 +00:00
|
|
|
uprintf("Device eliminated because it is a Windows Sandbox VHD");
|
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (props.is_VHD && (!enable_VHDs)) {
|
|
|
|
uprintf("Device eliminated because listing of VHDs is disabled (Alt-G)");
|
2019-07-15 11:35:22 +00:00
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
|
|
|
}
|
2014-05-17 23:37:01 +00:00
|
|
|
|
|
|
|
// The empty string is returned for drives that don't have any volumes assigned
|
|
|
|
if (drive_letters[0] == 0) {
|
2022-05-25 12:21:36 +00:00
|
|
|
display_name = lmprintf(MSG_046, label, drive_number,
|
2014-05-17 23:37:01 +00:00
|
|
|
SizeToHumanReadable(GetDriveSize(drive_index), FALSE, use_fake_units));
|
|
|
|
} else {
|
2015-03-18 21:34:28 +00:00
|
|
|
// Find the UEFI:TOGO partition(s) (and eliminate them form our listing)
|
|
|
|
for (k=0; drive_letters[k]; k++) {
|
|
|
|
uefi_togo_check[0] = drive_letters[k];
|
|
|
|
if (PathFileExistsA(uefi_togo_check)) {
|
|
|
|
for (l=k; drive_letters[l]; l++)
|
|
|
|
drive_letters[l] = drive_letters[l+1];
|
|
|
|
k--;
|
|
|
|
}
|
|
|
|
}
|
2014-05-17 23:37:01 +00:00
|
|
|
// We have multiple volumes assigned to the same device (multiple partitions)
|
|
|
|
// If that is the case, use "Multiple Volumes" instead of the label
|
2022-05-25 12:21:36 +00:00
|
|
|
static_strcpy(display_msg, (((drive_letters[0] != 0) && (drive_letters[1] != 0))?
|
2015-08-05 21:36:22 +00:00
|
|
|
lmprintf(MSG_047):label));
|
2015-06-27 21:12:30 +00:00
|
|
|
for (k=0, remove_drive=0; drive_letters[k] && (!remove_drive); k++) {
|
2014-05-17 23:37:01 +00:00
|
|
|
// Append all the drive letters we detected
|
|
|
|
letter_name[2] = drive_letters[k];
|
|
|
|
if (right_to_left_mode)
|
2022-05-25 12:21:36 +00:00
|
|
|
static_strcat(display_msg, RIGHT_TO_LEFT_MARK);
|
|
|
|
static_strcat(display_msg, letter_name);
|
2015-06-27 21:12:30 +00:00
|
|
|
if (drive_letters[k] == (PathGetDriveNumberU(app_dir) + 'A'))
|
|
|
|
remove_drive = 1;
|
|
|
|
if (drive_letters[k] == (PathGetDriveNumberU(system_dir) + 'A'))
|
|
|
|
remove_drive = 2;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
2015-06-27 21:12:30 +00:00
|
|
|
// Make sure that we don't list any drive that should not be listed
|
|
|
|
if (remove_drive) {
|
2022-01-31 16:55:42 +00:00
|
|
|
uprintf("Removing %c: from the list: This is the %s!", toupper(drive_letters[--k]),
|
2015-06-27 21:12:30 +00:00
|
|
|
(remove_drive==1)?"disk from which " APPLICATION_NAME " is running":"system disk");
|
2014-05-17 23:37:01 +00:00
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
|
|
|
}
|
2022-05-25 12:21:36 +00:00
|
|
|
safe_sprintf(&display_msg[strlen(display_msg)], sizeof(display_msg) - strlen(display_msg),
|
2014-05-17 23:37:01 +00:00
|
|
|
"%s [%s]", (right_to_left_mode)?RIGHT_TO_LEFT_MARK:"", SizeToHumanReadable(GetDriveSize(drive_index), FALSE, use_fake_units));
|
2022-05-25 12:21:36 +00:00
|
|
|
display_name = display_msg;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
rufus_drive[num_drives].index = drive_index;
|
|
|
|
rufus_drive[num_drives].id = safe_strdup(device_instance_id);
|
|
|
|
rufus_drive[num_drives].name = safe_strdup(buffer);
|
|
|
|
rufus_drive[num_drives].display_name = safe_strdup(display_name);
|
|
|
|
rufus_drive[num_drives].label = safe_strdup(label);
|
|
|
|
rufus_drive[num_drives].size = GetDriveSize(drive_index);
|
|
|
|
assert(rufus_drive[num_drives].size != 0);
|
|
|
|
if (hub_path != NULL) {
|
|
|
|
rufus_drive[num_drives].hub = safe_strdup(hub_path);
|
|
|
|
rufus_drive[num_drives].port = props.port;
|
|
|
|
}
|
|
|
|
num_drives++;
|
|
|
|
if (num_drives >= MAX_DRIVES)
|
|
|
|
uprintf("Warning: Found more than %d drives - ignoring remaining ones...", MAX_DRIVES);
|
2014-05-17 23:37:01 +00:00
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SetupDiDestroyDeviceInfoList(dev_info);
|
|
|
|
|
2022-05-25 12:21:36 +00:00
|
|
|
// Reorder the drives by increasing size, using the "selection sort" algorithm
|
|
|
|
for (u = 0; u < num_drives - 1; u++) {
|
|
|
|
uint64_t min_drive_size = rufus_drive[u].size;
|
|
|
|
int min_index = u;
|
|
|
|
for (v = u + 1; v < num_drives; v++) {
|
|
|
|
if (rufus_drive[v].size < min_drive_size) {
|
|
|
|
min_drive_size = rufus_drive[v].size;
|
|
|
|
min_index = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (min_index != u) {
|
|
|
|
RUFUS_DRIVE tmp;
|
|
|
|
memcpy(&tmp, &rufus_drive[u], sizeof(RUFUS_DRIVE));
|
|
|
|
memcpy(&rufus_drive[u], &rufus_drive[min_index], sizeof(RUFUS_DRIVE));
|
|
|
|
memcpy(&rufus_drive[min_index], &tmp, sizeof(RUFUS_DRIVE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now populate the drive combo box
|
|
|
|
// NB: The combo box must have the UNSORTED attribute for indexes to remain the ones we assign
|
|
|
|
for (u = 0; u < num_drives; u++) {
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hDeviceList, ComboBox_AddStringU(hDeviceList, rufus_drive[u].display_name), rufus_drive[u].index));
|
|
|
|
maxwidth = max(maxwidth, GetEntryWidth(hDeviceList, rufus_drive[u].display_name));
|
|
|
|
}
|
2014-05-17 23:37:01 +00:00
|
|
|
// Adjust the Dropdown width to the maximum text size
|
|
|
|
SendMessage(hDeviceList, CB_SETDROPPEDWIDTH, (WPARAM)maxwidth, 0);
|
|
|
|
|
|
|
|
if (devnum >= DRIVE_INDEX_MIN) {
|
2019-05-23 12:09:25 +00:00
|
|
|
for (i = 0; i < ComboBox_GetCount(hDeviceList); i++) {
|
2014-05-17 23:37:01 +00:00
|
|
|
if ((DWORD)ComboBox_GetItemData(hDeviceList, i) == devnum) {
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
i = 0;
|
|
|
|
IGNORE_RETVAL(ComboBox_SetCurSel(hDeviceList, i));
|
|
|
|
SendMessage(hMainDialog, WM_COMMAND, (CBN_SELCHANGE<<16) | IDC_DEVICE, 0);
|
2014-05-18 23:56:17 +00:00
|
|
|
r = TRUE;
|
|
|
|
|
|
|
|
out:
|
2015-05-08 22:37:22 +00:00
|
|
|
// Set 'Start' as the selected button, so that tab selection works
|
|
|
|
SendMessage(hMainDialog, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hMainDialog, IDC_START), TRUE);
|
2014-05-17 23:37:01 +00:00
|
|
|
safe_free(devid_list);
|
|
|
|
StrArrayDestroy(&dev_if_path);
|
|
|
|
htab_destroy(&htab_devid);
|
2014-05-18 23:56:17 +00:00
|
|
|
return r;
|
2014-05-17 23:37:01 +00:00
|
|
|
}
|