2011-12-01 17:54:35 +00:00
|
|
|
/*
|
2011-12-05 11:36:02 +00:00
|
|
|
* Rufus: The Reliable USB Formatting Utility
|
2011-12-01 17:54:35 +00:00
|
|
|
* Drive access function calls
|
2012-01-02 23:55:47 +00:00
|
|
|
* Copyright (c) 2011-2012 Pete Batard <pete@akeo.ie>
|
2011-12-01 17:54:35 +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/>.
|
|
|
|
*/
|
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "msapi_utf8.h"
|
|
|
|
#include "rufus.h"
|
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
|
|
|
RUFUS_DRIVE_INFO SelectedDrive;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a drive or volume with optional write and lock access
|
|
|
|
* Returns INVALID_HANDLE_VALUE (/!\ which is DIFFERENT from NULL /!\) on failure.
|
|
|
|
* This call is quite risky (left unchecked, inadvertently passing 0 as index would
|
|
|
|
* return a handle to C:, which we might then proceed to unknowingly repartition!),
|
|
|
|
* so we apply the following mitigation factors:
|
|
|
|
* - Valid indexes must belong to a specific range [DRIVE_INDEX_MIN; DRIVE_INDEX_MAX]
|
|
|
|
* - When opening for write access, we lock the volume. If that fails, which would
|
|
|
|
* typically be the case on C:\ or any other drive in use, we report failure
|
|
|
|
* - We report the full path of any drive that was successfully opened for write acces
|
|
|
|
*/
|
|
|
|
HANDLE GetDriveHandle(DWORD DriveIndex, char* DriveLetter, BOOL bWriteAccess, BOOL bLockDrive)
|
|
|
|
{
|
|
|
|
BOOL r;
|
|
|
|
DWORD size;
|
|
|
|
HANDLE hDrive = INVALID_HANDLE_VALUE;
|
|
|
|
STORAGE_DEVICE_NUMBER_REDEF device_number = {0};
|
2012-02-02 19:47:08 +00:00
|
|
|
UINT drive_type;
|
2011-12-01 17:54:35 +00:00
|
|
|
char drives[26*4]; /* "D:\", "E:\", etc. */
|
|
|
|
char *drive = drives;
|
|
|
|
char logical_drive[] = "\\\\.\\#:";
|
|
|
|
char physical_drive[24];
|
|
|
|
|
|
|
|
if ((DriveIndex < DRIVE_INDEX_MIN) || (DriveIndex > DRIVE_INDEX_MAX)) {
|
|
|
|
uprintf("WARNING: Bad index value. Please check the code!\n");
|
|
|
|
}
|
|
|
|
DriveIndex -= DRIVE_INDEX_MIN;
|
|
|
|
|
|
|
|
// If no drive letter is requested, open a phyical drive
|
|
|
|
if (DriveLetter == NULL) {
|
|
|
|
safe_sprintf(physical_drive, sizeof(physical_drive), "\\\\.\\PHYSICALDRIVE%d", DriveIndex);
|
|
|
|
hDrive = CreateFileA(physical_drive, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0),
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
|
|
|
|
if (hDrive == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf("Could not open drive %s: %s\n", physical_drive, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (bWriteAccess) {
|
|
|
|
uprintf("Caution: Opened %s drive for write access\n", physical_drive);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*DriveLetter = ' ';
|
|
|
|
size = GetLogicalDriveStringsA(sizeof(drives), drives);
|
|
|
|
if (size == 0) {
|
|
|
|
uprintf("GetLogicalDriveStrings failed: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (size > sizeof(drives)) {
|
|
|
|
uprintf("GetLogicalDriveStrings: buffer too small (required %d vs %d)\n", size, sizeof(drives));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
hDrive = INVALID_HANDLE_VALUE;
|
|
|
|
for ( ;*drive; drive += safe_strlen(drive)+1) {
|
|
|
|
if (!isalpha(*drive))
|
|
|
|
continue;
|
|
|
|
*drive = (char)toupper((int)*drive);
|
|
|
|
if (*drive < 'C') {
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-02 19:47:08 +00:00
|
|
|
|
|
|
|
/* IOCTL_STORAGE_GET_DEVICE_NUMBER's STORAGE_DEVICE_NUMBER.DeviceNumber is
|
|
|
|
not unique! An HDD, a DVD and probably other drives can have the same
|
|
|
|
value there => Use GetDriveType() to filter out unwanted devices.
|
|
|
|
See https://github.com/pbatard/rufus/issues/32 for details. */
|
|
|
|
drive_type = GetDriveTypeA(drive);
|
|
|
|
if ((drive_type != DRIVE_REMOVABLE) && (drive_type != DRIVE_FIXED))
|
|
|
|
continue;
|
|
|
|
|
2011-12-01 17:54:35 +00:00
|
|
|
safe_sprintf(logical_drive, sizeof(logical_drive), "\\\\.\\%c:", drive[0]);
|
|
|
|
hDrive = CreateFileA(logical_drive, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0),
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
|
|
|
|
if (hDrive == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf("Warning: could not open drive %c: %s\n", drive[0], WindowsErrorString());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL,
|
|
|
|
0, &device_number, sizeof(device_number), &size, NULL);
|
|
|
|
if ((!r) || (size <= 0)) {
|
|
|
|
uprintf("IOCTL_STORAGE_GET_DEVICE_NUMBER failed for device %s: %s\n",
|
|
|
|
logical_drive, WindowsErrorString());
|
|
|
|
} else if (device_number.DeviceNumber == DriveIndex) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
}
|
|
|
|
if (hDrive == INVALID_HANDLE_VALUE) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (bWriteAccess) {
|
|
|
|
uprintf("Caution: Opened %s drive for write access\n", logical_drive);
|
|
|
|
}
|
|
|
|
*DriveLetter = *drive?*drive:' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((bLockDrive) && (!DeviceIoControl(hDrive, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &size, NULL))) {
|
2011-12-16 23:24:16 +00:00
|
|
|
uprintf("Could not get exclusive access to %s %s\n", logical_drive, WindowsErrorString());
|
2011-12-01 17:54:35 +00:00
|
|
|
safe_closehandle(hDrive);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return hDrive;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the drive letter and volume label
|
|
|
|
*/
|
|
|
|
BOOL GetDriveLabel(DWORD DriveIndex, char* letter, char** label)
|
|
|
|
{
|
|
|
|
HANDLE hDrive;
|
2012-02-21 19:46:28 +00:00
|
|
|
wchar_t wDrivePath[] = L"#:\\";
|
|
|
|
wchar_t wVolumeLabel[MAX_PATH+1];
|
|
|
|
static char VolumeLabel[MAX_PATH+1];
|
2011-12-01 17:54:35 +00:00
|
|
|
|
|
|
|
*label = STR_NO_LABEL;
|
|
|
|
|
2012-02-21 19:46:28 +00:00
|
|
|
hDrive = GetDriveHandle(DriveIndex, letter, FALSE, FALSE);
|
2011-12-01 17:54:35 +00:00
|
|
|
if (hDrive == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
safe_closehandle(hDrive);
|
2012-02-21 19:46:28 +00:00
|
|
|
wDrivePath[0] = *letter;
|
2011-12-01 17:54:35 +00:00
|
|
|
|
2012-02-21 19:46:28 +00:00
|
|
|
if (GetVolumeInformationW(wDrivePath, wVolumeLabel, sizeof(wVolumeLabel),
|
|
|
|
NULL, NULL, NULL, NULL, 0) && *wVolumeLabel) {
|
|
|
|
wchar_to_utf8_no_alloc(wVolumeLabel, VolumeLabel, sizeof(VolumeLabel));
|
|
|
|
*label = VolumeLabel;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-12-11 02:19:38 +00:00
|
|
|
|
|
|
|
BOOL UnmountDrive(HANDLE hDrive)
|
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
if (!DeviceIoControl(hDrive, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &size, NULL)) {
|
|
|
|
uprintf("Could not ummount drive: %s\n", WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|