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
|
2016-01-07 15:49:58 +00:00
|
|
|
* Copyright © 2011-2016 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 "rufus.h"
|
2016-02-20 22:52:32 +00:00
|
|
|
#include "missing.h"
|
|
|
|
#include "resource.h"
|
|
|
|
#include "msapi_utf8.h"
|
|
|
|
#include "localization.h"
|
|
|
|
|
2015-08-10 22:19:57 +00:00
|
|
|
#include "file.h"
|
2014-01-05 01:39:41 +00:00
|
|
|
#include "drive.h"
|
2013-01-22 02:40:43 +00:00
|
|
|
#include "sys_types.h"
|
2014-01-05 01:39:41 +00:00
|
|
|
#include "br.h"
|
|
|
|
#include "fat16.h"
|
|
|
|
#include "fat32.h"
|
|
|
|
#include "ntfs.h"
|
2011-12-01 17:54:35 +00:00
|
|
|
|
2014-08-07 00:45:46 +00:00
|
|
|
#if !defined(PARTITION_BASIC_DATA_GUID)
|
2015-11-05 22:54:38 +00:00
|
|
|
const GUID PARTITION_BASIC_DATA_GUID =
|
2015-01-19 01:23:32 +00:00
|
|
|
{ 0xebd0a0a2L, 0xb9e5, 0x4433, {0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7} };
|
|
|
|
#endif
|
|
|
|
#if !defined(PARTITION_MSFT_RESERVED_GUID)
|
|
|
|
const GUID PARTITION_MSFT_RESERVED_GUID =
|
|
|
|
{ 0xe3c9e316L, 0x0b5c, 0x4db8, {0x81, 0x7d, 0xf9, 0x2d, 0xf0, 0x02, 0x15, 0xae} };
|
2014-08-07 00:45:46 +00:00
|
|
|
#endif
|
2015-01-20 02:56:17 +00:00
|
|
|
#if !defined(PARTITION_SYSTEM_GUID)
|
|
|
|
const GUID PARTITION_SYSTEM_GUID =
|
|
|
|
{ 0xc12a7328L, 0xf81f, 0x11d2, {0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b} };
|
|
|
|
#endif
|
2014-08-07 00:45:46 +00:00
|
|
|
|
2011-12-01 17:54:35 +00:00
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
|
|
|
RUFUS_DRIVE_INFO SelectedDrive;
|
|
|
|
|
2014-11-11 19:17:39 +00:00
|
|
|
/*
|
|
|
|
* The following methods get or set the AutoMount setting (which is different from AutoRun)
|
|
|
|
* Rufus needs AutoMount to be set as the format process may fail for fixed drives otherwise.
|
|
|
|
* See https://github.com/pbatard/rufus/issues/386.
|
|
|
|
*
|
|
|
|
* Reverse engineering diskpart and mountvol indicates that the former uses the IVdsService
|
2015-11-05 22:54:38 +00:00
|
|
|
* ClearFlags()/SetFlags() to set VDS_SVF_AUTO_MOUNT_OFF whereas mountvol on uses
|
2014-11-11 19:17:39 +00:00
|
|
|
* IOCTL_MOUNTMGR_SET_AUTO_MOUNT on "\\\\.\\MountPointManager".
|
|
|
|
* As the latter is MUCH simpler this is what we'll use too
|
|
|
|
*/
|
|
|
|
BOOL SetAutoMount(BOOL enable)
|
|
|
|
{
|
|
|
|
HANDLE hMountMgr;
|
|
|
|
DWORD size;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
hMountMgr = CreateFileA(MOUNTMGR_DOS_DEVICE_NAME, GENERIC_READ|GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (hMountMgr == NULL)
|
|
|
|
return FALSE;
|
|
|
|
ret = DeviceIoControl(hMountMgr, IOCTL_MOUNTMGR_SET_AUTO_MOUNT, &enable, sizeof(enable), NULL, 0, &size, NULL);
|
|
|
|
CloseHandle(hMountMgr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL GetAutoMount(BOOL* enabled)
|
|
|
|
{
|
|
|
|
HANDLE hMountMgr;
|
|
|
|
DWORD size;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (enabled == NULL)
|
|
|
|
return FALSE;
|
|
|
|
hMountMgr = CreateFileA(MOUNTMGR_DOS_DEVICE_NAME, GENERIC_READ|GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (hMountMgr == NULL)
|
|
|
|
return FALSE;
|
|
|
|
ret = DeviceIoControl(hMountMgr, IOCTL_MOUNTMGR_QUERY_AUTO_MOUNT, NULL, 0, enabled, sizeof(*enabled), &size, NULL);
|
|
|
|
CloseHandle(hMountMgr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
/*
|
|
|
|
* Working with drive indexes quite risky (left unchecked,inadvertently passing 0 as
|
|
|
|
* index would return a handle to C:, which we might then proceed to unknowingly
|
|
|
|
* clear the MBR of!), so we mitigate the risk by forcing our indexes to belong to
|
|
|
|
* the specific range [DRIVE_INDEX_MIN; DRIVE_INDEX_MAX].
|
|
|
|
*/
|
|
|
|
#define CheckDriveIndex(DriveIndex) do { \
|
|
|
|
if ((DriveIndex < DRIVE_INDEX_MIN) || (DriveIndex > DRIVE_INDEX_MAX)) { \
|
2016-07-07 17:13:01 +00:00
|
|
|
uprintf("ERROR: Bad index value %d. Please check the code!", DriveIndex); \
|
2013-04-07 23:10:58 +00:00
|
|
|
goto out; \
|
|
|
|
} \
|
|
|
|
DriveIndex -= DRIVE_INDEX_MIN; } while (0)
|
|
|
|
|
2011-12-01 17:54:35 +00:00
|
|
|
/*
|
|
|
|
* Open a drive or volume with optional write and lock access
|
2013-04-07 23:10:58 +00:00
|
|
|
* Return INVALID_HANDLE_VALUE (/!\ which is DIFFERENT from NULL /!\) on failure.
|
2011-12-01 17:54:35 +00:00
|
|
|
*/
|
2017-04-08 20:58:10 +00:00
|
|
|
static HANDLE GetHandle(char* Path, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare)
|
2011-12-01 17:54:35 +00:00
|
|
|
{
|
2013-06-25 17:39:07 +00:00
|
|
|
int i;
|
2017-07-13 20:53:44 +00:00
|
|
|
BYTE access_mask = 0;
|
2017-05-08 15:58:26 +00:00
|
|
|
DWORD size, EndTime;
|
2011-12-01 17:54:35 +00:00
|
|
|
HANDLE hDrive = INVALID_HANDLE_VALUE;
|
2017-04-08 20:58:10 +00:00
|
|
|
char DevPath[MAX_PATH];
|
2011-12-01 17:54:35 +00:00
|
|
|
|
2017-04-08 20:58:10 +00:00
|
|
|
if ((safe_strlen(Path) < 5) || (Path[0] != '\\') || (Path[1] != '\\') || (Path[3] != '\\'))
|
2013-04-07 23:10:58 +00:00
|
|
|
goto out;
|
2017-04-08 20:58:10 +00:00
|
|
|
|
2017-04-27 22:06:42 +00:00
|
|
|
// Resolve a device path, so that we can look for that handle in case of access issues.
|
2017-04-08 20:58:10 +00:00
|
|
|
if (QueryDosDeviceA(&Path[4], DevPath, sizeof(DevPath)) == 0)
|
|
|
|
strcpy(DevPath, "???");
|
|
|
|
|
2017-02-16 14:13:30 +00:00
|
|
|
for (i = 0; i < DRIVE_ACCESS_RETRIES; i++) {
|
2017-04-08 20:58:10 +00:00
|
|
|
// Try without FILE_SHARE_WRITE (unless specifically requested) so that
|
2017-02-16 14:13:30 +00:00
|
|
|
// we won't be bothered by the OS or other apps when we set up our data.
|
|
|
|
// However this means we might have to wait for an access gap...
|
|
|
|
// We keep FILE_SHARE_READ though, as this shouldn't hurt us any, and is
|
|
|
|
// required for enumeration.
|
|
|
|
hDrive = CreateFileA(Path, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0),
|
2017-05-01 23:56:07 +00:00
|
|
|
FILE_SHARE_READ|(bWriteShare?FILE_SHARE_WRITE:0),
|
2017-04-08 20:58:10 +00:00
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2017-02-16 14:13:30 +00:00
|
|
|
if (hDrive != INVALID_HANDLE_VALUE)
|
|
|
|
break;
|
|
|
|
if ((GetLastError() != ERROR_SHARING_VIOLATION) && (GetLastError() != ERROR_ACCESS_DENIED))
|
|
|
|
break;
|
2017-04-08 20:58:10 +00:00
|
|
|
if (i == 0) {
|
2017-05-03 13:48:24 +00:00
|
|
|
uprintf("Waiting for access on %s [%s]...", Path, DevPath);
|
2017-05-01 23:56:07 +00:00
|
|
|
} else if (!bWriteShare && (i > DRIVE_ACCESS_RETRIES/3)) {
|
2017-05-03 13:48:24 +00:00
|
|
|
// If we can't seem to get a hold of the drive for some time, try to enable FILE_SHARE_WRITE...
|
2017-04-09 20:05:10 +00:00
|
|
|
uprintf("Warning: Could not obtain exclusive rights. Retrying with write sharing enabled...");
|
2017-04-08 20:58:10 +00:00
|
|
|
bWriteShare = TRUE;
|
2017-05-03 13:48:24 +00:00
|
|
|
// Try to report the process that is locking the drive
|
2017-07-13 20:53:44 +00:00
|
|
|
// We also use bit 6 as a flag to indicate that SearchProcess was called.
|
|
|
|
access_mask = SearchProcess(DevPath, TRUE, TRUE, FALSE) | 0x40;
|
2017-04-08 20:58:10 +00:00
|
|
|
}
|
2017-02-16 14:13:30 +00:00
|
|
|
Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
|
|
|
|
}
|
2013-04-07 23:10:58 +00:00
|
|
|
if (hDrive == INVALID_HANDLE_VALUE) {
|
2017-05-03 13:48:24 +00:00
|
|
|
uprintf("Could not open %s: %s\n", Path, WindowsErrorString());
|
2013-04-07 23:10:58 +00:00
|
|
|
goto out;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
if (bWriteAccess) {
|
2017-05-03 13:48:24 +00:00
|
|
|
uprintf("Opened %s for %s write access", Path, bWriteShare?"shared":"exclusive");
|
2013-04-07 23:10:58 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 17:39:07 +00:00
|
|
|
if (bLockDrive) {
|
2013-11-23 22:39:54 +00:00
|
|
|
if (DeviceIoControl(hDrive, FSCTL_ALLOW_EXTENDED_DASD_IO, NULL, 0, NULL, 0, &size, NULL)) {
|
|
|
|
uprintf("I/O boundary checks disabled\n");
|
|
|
|
}
|
|
|
|
|
2017-05-03 13:48:24 +00:00
|
|
|
uprintf("Requesting lock...");
|
2017-05-08 15:58:26 +00:00
|
|
|
EndTime = GetTickCount() + DRIVE_ACCESS_TIMEOUT;
|
|
|
|
do {
|
2013-06-25 17:39:07 +00:00
|
|
|
if (DeviceIoControl(hDrive, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &size, NULL))
|
|
|
|
goto out;
|
|
|
|
if (IS_ERROR(FormatStatus)) // User cancel
|
|
|
|
break;
|
2017-05-08 15:58:26 +00:00
|
|
|
Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES);
|
|
|
|
} while (GetTickCount() < EndTime);
|
2013-06-25 17:39:07 +00:00
|
|
|
// If we reached this section, either we didn't manage to get a lock or the user cancelled
|
2017-05-03 13:48:24 +00:00
|
|
|
uprintf("Could not lock access to %s: %s", Path, WindowsErrorString());
|
2017-07-13 20:53:44 +00:00
|
|
|
// See if we can report the processes are accessing the drive
|
|
|
|
if (!IS_ERROR(FormatStatus) && (access_mask == 0))
|
|
|
|
access_mask = SearchProcess(DevPath, TRUE, TRUE, FALSE);
|
|
|
|
// Try to continue if the only access rights we saw were for read-only
|
|
|
|
if ((access_mask & 0x07) != 0x01)
|
|
|
|
safe_closehandle(hDrive);
|
2013-04-07 23:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return hDrive;
|
|
|
|
}
|
|
|
|
|
2015-11-05 22:54:38 +00:00
|
|
|
/*
|
2013-04-07 23:10:58 +00:00
|
|
|
* Return the path to access the physical drive, or NULL on error.
|
|
|
|
* The string is allocated and must be freed (to ensure concurrent access)
|
|
|
|
*/
|
|
|
|
char* GetPhysicalName(DWORD DriveIndex)
|
|
|
|
{
|
|
|
|
BOOL success = FALSE;
|
|
|
|
char physical_name[24];
|
|
|
|
|
|
|
|
CheckDriveIndex(DriveIndex);
|
2016-05-30 16:49:32 +00:00
|
|
|
safe_sprintf(physical_name, sizeof(physical_name), "\\\\.\\PHYSICALDRIVE%lu", DriveIndex);
|
2013-04-07 23:10:58 +00:00
|
|
|
success = TRUE;
|
|
|
|
out:
|
|
|
|
return (success)?safe_strdup(physical_name):NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-05 22:54:38 +00:00
|
|
|
/*
|
2013-04-07 23:10:58 +00:00
|
|
|
* Return a handle to the physical drive identified by DriveIndex
|
|
|
|
*/
|
2017-05-01 23:56:07 +00:00
|
|
|
HANDLE GetPhysicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare)
|
2013-04-07 23:10:58 +00:00
|
|
|
{
|
|
|
|
HANDLE hPhysical = INVALID_HANDLE_VALUE;
|
|
|
|
char* PhysicalPath = GetPhysicalName(DriveIndex);
|
2017-05-01 23:56:07 +00:00
|
|
|
hPhysical = GetHandle(PhysicalPath, bLockDrive, bWriteAccess, bWriteShare);
|
2013-04-07 23:10:58 +00:00
|
|
|
safe_free(PhysicalPath);
|
|
|
|
return hPhysical;
|
|
|
|
}
|
|
|
|
|
2013-11-17 01:39:43 +00:00
|
|
|
/*
|
|
|
|
* Return the first GUID volume name for the associated drive or NULL if not found
|
|
|
|
* See http://msdn.microsoft.com/en-us/library/cc542456.aspx
|
|
|
|
* The returned string is allocated and must be freed
|
|
|
|
*/
|
2013-06-25 17:39:07 +00:00
|
|
|
char* GetLogicalName(DWORD DriveIndex, BOOL bKeepTrailingBackslash, BOOL bSilent)
|
2013-04-07 23:10:58 +00:00
|
|
|
{
|
|
|
|
BOOL success = FALSE;
|
|
|
|
char volume_name[MAX_PATH];
|
|
|
|
HANDLE hDrive = INVALID_HANDLE_VALUE, hVolume = INVALID_HANDLE_VALUE;
|
|
|
|
size_t len;
|
|
|
|
char path[MAX_PATH];
|
2017-01-05 15:42:24 +00:00
|
|
|
VOLUME_DISK_EXTENTS_REDEF DiskExtents;
|
2013-04-07 23:10:58 +00:00
|
|
|
DWORD size;
|
2013-11-17 01:39:43 +00:00
|
|
|
UINT drive_type;
|
2013-04-07 23:10:58 +00:00
|
|
|
int i, j;
|
|
|
|
static const char* ignore_device[] = { "\\Device\\CdRom", "\\Device\\Floppy" };
|
2013-05-19 22:02:16 +00:00
|
|
|
static const char* volume_start = "\\\\?\\";
|
2013-04-07 23:10:58 +00:00
|
|
|
|
|
|
|
CheckDriveIndex(DriveIndex);
|
|
|
|
|
|
|
|
for (i=0; hDrive == INVALID_HANDLE_VALUE; i++) {
|
|
|
|
if (i == 0) {
|
|
|
|
hVolume = FindFirstVolumeA(volume_name, sizeof(volume_name));
|
|
|
|
if (hVolume == INVALID_HANDLE_VALUE) {
|
2013-06-25 17:39:07 +00:00
|
|
|
suprintf("Could not access first GUID volume: %s\n", WindowsErrorString());
|
2013-04-07 23:10:58 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!FindNextVolumeA(hVolume, volume_name, sizeof(volume_name))) {
|
|
|
|
if (GetLastError() != ERROR_NO_MORE_FILES) {
|
2013-06-25 17:39:07 +00:00
|
|
|
suprintf("Could not access next GUID volume: %s\n", WindowsErrorString());
|
2013-04-07 23:10:58 +00:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
2013-04-07 23:10:58 +00:00
|
|
|
|
|
|
|
// Sanity checks
|
|
|
|
len = safe_strlen(volume_name);
|
2013-05-19 22:02:16 +00:00
|
|
|
if ((len <= 1) || (safe_strnicmp(volume_name, volume_start, 4) != 0) || (volume_name[len-1] != '\\')) {
|
2013-06-25 17:39:07 +00:00
|
|
|
suprintf("'%s' is not a GUID volume name\n", volume_name);
|
2013-04-07 23:10:58 +00:00
|
|
|
continue;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
2013-04-07 23:10:58 +00:00
|
|
|
|
|
|
|
drive_type = GetDriveTypeA(volume_name);
|
2013-11-14 01:21:50 +00:00
|
|
|
if ((drive_type != DRIVE_REMOVABLE) && (drive_type != DRIVE_FIXED))
|
2013-04-07 23:10:58 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
volume_name[len-1] = 0;
|
|
|
|
|
|
|
|
if (QueryDosDeviceA(&volume_name[4], path, sizeof(path)) == 0) {
|
2013-06-25 17:39:07 +00:00
|
|
|
suprintf("Failed to get device path for GUID volume '%s': %s\n", volume_name, WindowsErrorString());
|
2013-04-07 23:10:58 +00:00
|
|
|
continue;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
2013-04-07 23:10:58 +00:00
|
|
|
|
|
|
|
for (j=0; (j<ARRAYSIZE(ignore_device)) &&
|
2014-05-09 21:05:25 +00:00
|
|
|
(_strnicmp(path, ignore_device[j], safe_strlen(ignore_device[j])) != 0); j++);
|
2013-04-07 23:10:58 +00:00
|
|
|
if (j < ARRAYSIZE(ignore_device)) {
|
2013-06-25 17:39:07 +00:00
|
|
|
suprintf("Skipping GUID volume for '%s'\n", path);
|
2013-04-07 23:10:58 +00:00
|
|
|
continue;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 19:17:39 +00:00
|
|
|
hDrive = CreateFileA(volume_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2013-04-07 23:10:58 +00:00
|
|
|
if (hDrive == INVALID_HANDLE_VALUE) {
|
2013-06-25 17:39:07 +00:00
|
|
|
suprintf("Could not open GUID volume '%s': %s\n", volume_name, WindowsErrorString());
|
2013-04-07 23:10:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-02-02 19:47:08 +00:00
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
if ((!DeviceIoControl(hDrive, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0,
|
|
|
|
&DiskExtents, sizeof(DiskExtents), &size, NULL)) || (size <= 0)) {
|
2013-06-25 17:39:07 +00:00
|
|
|
suprintf("Could not get Disk Extents: %s\n", WindowsErrorString());
|
2013-04-07 23:10:58 +00:00
|
|
|
safe_closehandle(hDrive);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
if ((DiskExtents.NumberOfDiskExtents >= 1) && (DiskExtents.Extents[0].DiskNumber == DriveIndex)) {
|
|
|
|
if (bKeepTrailingBackslash)
|
|
|
|
volume_name[len-1] = '\\';
|
|
|
|
success = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 19:47:08 +00:00
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
out:
|
|
|
|
if (hVolume != INVALID_HANDLE_VALUE)
|
|
|
|
FindVolumeClose(hVolume);
|
|
|
|
return (success)?safe_strdup(volume_name):NULL;
|
|
|
|
}
|
2011-12-01 17:54:35 +00:00
|
|
|
|
2013-06-25 17:39:07 +00:00
|
|
|
/* Wait for a logical drive to reappear - Used when a drive has just been repartitioned */
|
|
|
|
BOOL WaitForLogical(DWORD DriveIndex)
|
|
|
|
{
|
2017-04-11 21:15:05 +00:00
|
|
|
DWORD EndTime;
|
2013-06-25 17:39:07 +00:00
|
|
|
char* LogicalPath = NULL;
|
|
|
|
|
2017-04-11 21:15:05 +00:00
|
|
|
// GetLogicalName() calls may be slow, so use the system time to
|
|
|
|
// make sure we don't spend more than DRIVE_ACCESS_TIMEOUT in wait.
|
|
|
|
EndTime = GetTickCount() + DRIVE_ACCESS_TIMEOUT;
|
|
|
|
do {
|
2013-06-25 17:39:07 +00:00
|
|
|
LogicalPath = GetLogicalName(DriveIndex, FALSE, TRUE);
|
|
|
|
if (LogicalPath != NULL) {
|
|
|
|
free(LogicalPath);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (IS_ERROR(FormatStatus)) // User cancel
|
|
|
|
return FALSE;
|
|
|
|
Sleep(DRIVE_ACCESS_TIMEOUT/DRIVE_ACCESS_RETRIES);
|
2017-04-11 21:15:05 +00:00
|
|
|
} while (GetTickCount() < EndTime);
|
2013-06-25 17:39:07 +00:00
|
|
|
uprintf("Timeout while waiting for logical drive\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-11-05 22:54:38 +00:00
|
|
|
/*
|
2013-06-25 01:55:25 +00:00
|
|
|
* Obtain a handle to the first logical volume on the disk identified by DriveIndex
|
|
|
|
* Returns INVALID_HANDLE_VALUE on error or NULL if no logical path exists (typical
|
|
|
|
* of unpartitioned drives)
|
2013-04-07 23:10:58 +00:00
|
|
|
*/
|
2017-04-08 20:58:10 +00:00
|
|
|
HANDLE GetLogicalHandle(DWORD DriveIndex, BOOL bLockDrive, BOOL bWriteAccess, BOOL bWriteShare)
|
2013-04-07 23:10:58 +00:00
|
|
|
{
|
|
|
|
HANDLE hLogical = INVALID_HANDLE_VALUE;
|
2013-06-25 17:39:07 +00:00
|
|
|
char* LogicalPath = GetLogicalName(DriveIndex, FALSE, FALSE);
|
|
|
|
|
2013-06-25 01:55:25 +00:00
|
|
|
if (LogicalPath == NULL) {
|
2013-06-25 17:39:07 +00:00
|
|
|
uprintf("No logical drive found (unpartitioned?)\n");
|
2013-06-25 01:55:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-08 20:58:10 +00:00
|
|
|
hLogical = GetHandle(LogicalPath, bLockDrive, bWriteAccess, bWriteShare);
|
2014-05-09 21:05:25 +00:00
|
|
|
free(LogicalPath);
|
2013-04-07 23:10:58 +00:00
|
|
|
return hLogical;
|
|
|
|
}
|
|
|
|
|
2014-02-27 19:53:53 +00:00
|
|
|
/*
|
|
|
|
* Who would have thought that Microsoft would make it so unbelievably hard to
|
|
|
|
* get the frickin' device number for a drive? You have to use TWO different
|
|
|
|
* methods to have a chance to get it!
|
|
|
|
*/
|
|
|
|
int GetDriveNumber(HANDLE hDrive, char* path)
|
|
|
|
{
|
|
|
|
STORAGE_DEVICE_NUMBER_REDEF DeviceNumber;
|
|
|
|
VOLUME_DISK_EXTENTS_REDEF DiskExtents;
|
|
|
|
DWORD size;
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
if (!DeviceIoControl(hDrive, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0,
|
|
|
|
&DiskExtents, sizeof(DiskExtents), &size, NULL) || (size <= 0) || (DiskExtents.NumberOfDiskExtents < 1) ) {
|
|
|
|
// DiskExtents are NO_GO (which is the case for external USB HDDs...)
|
|
|
|
if(!DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0,
|
|
|
|
&DeviceNumber, sizeof(DeviceNumber), &size, NULL ) || (size <= 0)) {
|
|
|
|
uprintf("Could not get device number for device %s: %s", path, WindowsErrorString());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
r = (int)DeviceNumber.DeviceNumber;
|
|
|
|
} else if (DiskExtents.NumberOfDiskExtents >= 2) {
|
|
|
|
uprintf("Ignoring drive '%s' as it spans multiple disks (RAID?)", path);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
r = (int)DiskExtents.Extents[0].DiskNumber;
|
|
|
|
}
|
|
|
|
if (r >= MAX_DRIVES) {
|
|
|
|
uprintf("Device Number for device %s is too big (%d) - ignoring device", path, r);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
/*
|
2014-01-14 20:17:07 +00:00
|
|
|
* Returns the drive letters for all volumes located on the drive identified by DriveIndex,
|
2013-11-17 01:39:43 +00:00
|
|
|
* as well as the drive type. This is used as base for the 2 function calls that follow.
|
2013-04-07 23:10:58 +00:00
|
|
|
*/
|
2014-01-14 20:17:07 +00:00
|
|
|
static BOOL _GetDriveLettersAndType(DWORD DriveIndex, char* drive_letters, UINT* drive_type)
|
2013-04-07 23:10:58 +00:00
|
|
|
{
|
|
|
|
DWORD size;
|
2014-02-27 19:53:53 +00:00
|
|
|
BOOL r = FALSE;
|
2013-04-07 23:10:58 +00:00
|
|
|
HANDLE hDrive = INVALID_HANDLE_VALUE;
|
2013-11-17 01:39:43 +00:00
|
|
|
UINT _drive_type;
|
2014-02-27 21:40:12 +00:00
|
|
|
int i = 0, drive_number;
|
2014-09-08 17:23:50 +00:00
|
|
|
char *drive, drives[26*4 + 1]; /* "D:\", "E:\", etc., plus one NUL */
|
2013-04-07 23:10:58 +00:00
|
|
|
char logical_drive[] = "\\\\.\\#:";
|
2013-11-14 01:21:50 +00:00
|
|
|
|
2014-01-14 20:17:07 +00:00
|
|
|
if (drive_letters != NULL)
|
|
|
|
drive_letters[0] = 0;
|
2013-11-17 01:39:43 +00:00
|
|
|
if (drive_type != NULL)
|
|
|
|
*drive_type = DRIVE_UNKNOWN;
|
2013-04-07 23:10:58 +00:00
|
|
|
CheckDriveIndex(DriveIndex);
|
|
|
|
|
2014-09-08 17:23:50 +00:00
|
|
|
// This call is weird... The buffer needs to have an extra NUL, but you're
|
|
|
|
// supposed to provide the size without the extra NUL. And the returned size
|
|
|
|
// does not include the NUL either *EXCEPT* if your buffer is too small...
|
|
|
|
// But then again, this doesn't hold true if you have a 105 byte buffer and
|
|
|
|
// pass a 4*26=104 size, as the the call will return 105 (i.e. *FAILURE*)
|
|
|
|
// instead of 104 as it should => screw Microsoft: We'll include the NUL
|
|
|
|
// always, as each drive string is at least 4 chars long anyway.
|
2013-04-07 23:10:58 +00:00
|
|
|
size = GetLogicalDriveStringsA(sizeof(drives), drives);
|
|
|
|
if (size == 0) {
|
|
|
|
uprintf("GetLogicalDriveStrings failed: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (size > sizeof(drives)) {
|
2014-09-08 17:23:50 +00:00
|
|
|
uprintf("GetLogicalDriveStrings: Buffer too small (required %d vs. %d)\n", size, sizeof(drives));
|
2013-04-07 23:10:58 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-02-25 19:13:49 +00:00
|
|
|
r = TRUE; // Required to detect drives that don't have volumes assigned
|
2013-04-07 23:10:58 +00:00
|
|
|
for (drive = drives ;*drive; drive += safe_strlen(drive)+1) {
|
|
|
|
if (!isalpha(*drive))
|
|
|
|
continue;
|
|
|
|
*drive = (char)toupper((int)*drive);
|
|
|
|
if (*drive < 'C') {
|
|
|
|
continue;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
2013-04-07 23:10:58 +00:00
|
|
|
|
2016-02-16 17:47:07 +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#issuecomment-3785956
|
2013-11-17 01:39:43 +00:00
|
|
|
_drive_type = GetDriveTypeA(drive);
|
2013-11-14 01:21:50 +00:00
|
|
|
|
2013-11-17 01:39:43 +00:00
|
|
|
if ((_drive_type != DRIVE_REMOVABLE) && (_drive_type != DRIVE_FIXED))
|
2013-04-07 23:10:58 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
safe_sprintf(logical_drive, sizeof(logical_drive), "\\\\.\\%c:", drive[0]);
|
2014-11-11 19:17:39 +00:00
|
|
|
hDrive = CreateFileA(logical_drive, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2011-12-01 17:54:35 +00:00
|
|
|
if (hDrive == INVALID_HANDLE_VALUE) {
|
2015-01-20 02:56:17 +00:00
|
|
|
// uprintf("Warning: could not open drive %c: %s\n", drive[0], WindowsErrorString());
|
2013-04-07 23:10:58 +00:00
|
|
|
continue;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
2013-04-07 23:10:58 +00:00
|
|
|
|
2014-02-27 21:40:12 +00:00
|
|
|
drive_number = GetDriveNumber(hDrive, logical_drive);
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
if (drive_number == DriveIndex) {
|
2014-02-25 19:13:49 +00:00
|
|
|
r = TRUE;
|
2014-01-14 20:17:07 +00:00
|
|
|
if (drive_letters != NULL)
|
|
|
|
drive_letters[i++] = *drive;
|
|
|
|
// The drive type should be the same for all volumes, so we can overwrite
|
2013-11-17 01:39:43 +00:00
|
|
|
if (drive_type != NULL)
|
|
|
|
*drive_type = _drive_type;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
out:
|
2014-01-14 20:17:07 +00:00
|
|
|
if (drive_letters != NULL)
|
|
|
|
drive_letters[i] = 0;
|
2013-11-14 01:21:50 +00:00
|
|
|
return r;
|
2013-04-07 23:10:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 01:39:43 +00:00
|
|
|
// Could have used a #define, but this is clearer
|
2014-01-14 20:17:07 +00:00
|
|
|
BOOL GetDriveLetters(DWORD DriveIndex, char* drive_letters)
|
2013-11-17 01:39:43 +00:00
|
|
|
{
|
2014-01-14 20:17:07 +00:00
|
|
|
return _GetDriveLettersAndType(DriveIndex, drive_letters, NULL);
|
2013-11-17 01:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// There's already a GetDriveType in the Windows API
|
|
|
|
UINT GetDriveTypeFromIndex(DWORD DriveIndex)
|
|
|
|
{
|
|
|
|
UINT drive_type;
|
2014-01-14 20:17:07 +00:00
|
|
|
_GetDriveLettersAndType(DriveIndex, NULL, &drive_type);
|
2013-11-17 01:39:43 +00:00
|
|
|
return drive_type;
|
|
|
|
}
|
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
/*
|
|
|
|
* Return the next unused drive letter from the system
|
|
|
|
*/
|
|
|
|
char GetUnusedDriveLetter(void)
|
|
|
|
{
|
|
|
|
DWORD size;
|
2014-09-08 17:23:50 +00:00
|
|
|
char drive_letter = 'Z'+1, *drive, drives[26*4 + 1]; /* "D:\", "E:\", etc., plus one NUL */
|
2013-04-07 23:10:58 +00:00
|
|
|
|
|
|
|
size = GetLogicalDriveStringsA(sizeof(drives), drives);
|
|
|
|
if (size == 0) {
|
|
|
|
uprintf("GetLogicalDriveStrings failed: %s\n", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (size > sizeof(drives)) {
|
2014-09-08 17:23:50 +00:00
|
|
|
uprintf("GetLogicalDriveStrings: Buffer too small (required %d vs. %d)\n", size, sizeof(drives));
|
2011-12-01 17:54:35 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
for (drive_letter = 'C'; drive_letter < 'Z'; drive_letter++) {
|
|
|
|
for (drive = drives ;*drive; drive += safe_strlen(drive)+1) {
|
|
|
|
if (!isalpha(*drive))
|
|
|
|
continue;
|
|
|
|
if (drive_letter == (char)toupper((int)*drive))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!*drive)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-01 17:54:35 +00:00
|
|
|
out:
|
2014-01-14 20:17:07 +00:00
|
|
|
return (drive_letter>'Z')?0:drive_letter;
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the drive letter and volume label
|
2013-04-07 23:10:58 +00:00
|
|
|
* If the drive doesn't have a volume assigned, space is returned for the letter
|
2011-12-01 17:54:35 +00:00
|
|
|
*/
|
2014-01-14 20:17:07 +00:00
|
|
|
BOOL GetDriveLabel(DWORD DriveIndex, char* letters, char** label)
|
2011-12-01 17:54:35 +00:00
|
|
|
{
|
2013-04-07 23:10:58 +00:00
|
|
|
HANDLE hPhysical;
|
2012-03-09 01:38:52 +00:00
|
|
|
DWORD size;
|
2016-06-26 20:24:16 +00:00
|
|
|
static char VolumeLabel[MAX_PATH + 1];
|
|
|
|
char DrivePath[] = "#:\\", AutorunPath[] = "#:\\autorun.inf", *AutorunLabel = NULL;
|
2011-12-01 17:54:35 +00:00
|
|
|
|
|
|
|
*label = STR_NO_LABEL;
|
|
|
|
|
2014-01-14 20:17:07 +00:00
|
|
|
if (!GetDriveLetters(DriveIndex, letters))
|
2013-11-14 01:21:50 +00:00
|
|
|
return FALSE;
|
2014-01-14 20:17:07 +00:00
|
|
|
if (letters[0] == 0) {
|
2013-11-14 01:21:50 +00:00
|
|
|
// Drive without volume assigned - always enabled
|
|
|
|
return TRUE;
|
2013-02-10 21:54:47 +00:00
|
|
|
}
|
2014-01-14 20:17:07 +00:00
|
|
|
// We only care about an autorun.inf if we have a single volume
|
|
|
|
AutorunPath[0] = letters[0];
|
2016-06-26 20:24:16 +00:00
|
|
|
DrivePath[0] = letters[0];
|
2011-12-01 17:54:35 +00:00
|
|
|
|
2012-03-01 19:19:12 +00:00
|
|
|
// Try to read an extended label from autorun first. Fallback to regular label if not found.
|
2012-03-09 01:38:52 +00:00
|
|
|
// In the case of card readers with no card, users can get an annoying popup asking them
|
|
|
|
// to insert media. Use IOCTL_STORAGE_CHECK_VERIFY to prevent this
|
2017-05-01 23:56:07 +00:00
|
|
|
hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE, TRUE);
|
2012-03-09 01:38:52 +00:00
|
|
|
if (DeviceIoControl(hPhysical, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, NULL, 0, &size, NULL))
|
2012-11-12 01:53:34 +00:00
|
|
|
AutorunLabel = get_token_data_file("label", AutorunPath);
|
2012-03-09 01:38:52 +00:00
|
|
|
else if (GetLastError() == ERROR_NOT_READY)
|
2014-01-14 20:17:07 +00:00
|
|
|
uprintf("Ignoring autorun.inf label for drive %c: %s\n", letters[0],
|
2012-03-09 01:38:52 +00:00
|
|
|
(HRESULT_CODE(GetLastError()) == ERROR_NOT_READY)?"No media":WindowsErrorString());
|
|
|
|
safe_closehandle(hPhysical);
|
2012-03-01 19:19:12 +00:00
|
|
|
if (AutorunLabel != NULL) {
|
2014-01-14 20:17:07 +00:00
|
|
|
uprintf("Using autorun.inf label for drive %c: '%s'\n", letters[0], AutorunLabel);
|
2014-05-09 21:05:25 +00:00
|
|
|
safe_strcpy(VolumeLabel, sizeof(VolumeLabel), AutorunLabel);
|
2012-03-01 19:19:12 +00:00
|
|
|
safe_free(AutorunLabel);
|
|
|
|
*label = VolumeLabel;
|
2016-06-26 20:24:16 +00:00
|
|
|
} else if (GetVolumeInformationU(DrivePath, VolumeLabel, ARRAYSIZE(VolumeLabel),
|
|
|
|
NULL, NULL, NULL, NULL, 0) && (VolumeLabel[0] != 0)) {
|
2012-02-21 19:46:28 +00:00
|
|
|
*label = VolumeLabel;
|
2016-06-26 20:24:16 +00:00
|
|
|
} else {
|
|
|
|
duprintf("Failed to read label: %s", WindowsErrorString());
|
2011-12-01 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-12-11 02:19:38 +00:00
|
|
|
|
2013-11-17 01:39:43 +00:00
|
|
|
/*
|
|
|
|
* Return the drive size
|
|
|
|
*/
|
|
|
|
uint64_t GetDriveSize(DWORD DriveIndex)
|
|
|
|
{
|
|
|
|
BOOL r;
|
|
|
|
HANDLE hPhysical;
|
|
|
|
DWORD size;
|
2014-03-29 00:17:41 +00:00
|
|
|
BYTE geometry[256];
|
|
|
|
PDISK_GEOMETRY_EX DiskGeometry = (PDISK_GEOMETRY_EX)(void*)geometry;
|
2013-11-17 01:39:43 +00:00
|
|
|
|
2017-05-01 23:56:07 +00:00
|
|
|
hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE, TRUE);
|
2013-11-17 01:39:43 +00:00
|
|
|
if (hPhysical == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
r = DeviceIoControl(hPhysical, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
NULL, 0, geometry, sizeof(geometry), &size, NULL);
|
|
|
|
safe_closehandle(hPhysical);
|
|
|
|
if (!r || size <= 0)
|
|
|
|
return 0;
|
|
|
|
return DiskGeometry->DiskSize.QuadPart;
|
|
|
|
}
|
|
|
|
|
2013-12-20 18:32:10 +00:00
|
|
|
/*
|
|
|
|
* GET_DRIVE_GEOMETRY is used to tell if there is an actual media
|
|
|
|
*/
|
|
|
|
BOOL IsMediaPresent(DWORD DriveIndex)
|
|
|
|
{
|
|
|
|
BOOL r;
|
|
|
|
HANDLE hPhysical;
|
|
|
|
DWORD size;
|
|
|
|
BYTE geometry[128];
|
|
|
|
|
2017-05-01 23:56:07 +00:00
|
|
|
hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE, TRUE);
|
2013-12-20 18:32:10 +00:00
|
|
|
r = DeviceIoControl(hPhysical, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
2015-09-19 16:00:51 +00:00
|
|
|
NULL, 0, geometry, sizeof(geometry), &size, NULL) && (size > 0);
|
2013-12-20 18:32:10 +00:00
|
|
|
safe_closehandle(hPhysical);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-05-09 20:57:27 +00:00
|
|
|
const struct {int (*fn)(FILE *fp); char* str;} known_mbr[] = {
|
|
|
|
{ is_dos_mbr, "DOS/NT/95A" },
|
|
|
|
{ is_dos_f2_mbr, "DOS/NT/95A (F2)" },
|
|
|
|
{ is_95b_mbr, "Windows 95B/98/98SE/ME" },
|
|
|
|
{ is_2000_mbr, "Windows 2000/XP/2003" },
|
|
|
|
{ is_vista_mbr, "Windows Vista" },
|
|
|
|
{ is_win7_mbr, "Windows 7" },
|
|
|
|
{ is_rufus_mbr, "Rufus" },
|
|
|
|
{ is_syslinux_mbr, "Syslinux" },
|
2014-05-15 20:17:12 +00:00
|
|
|
{ is_reactos_mbr, "ReactOS" },
|
2016-01-07 15:49:58 +00:00
|
|
|
{ is_kolibrios_mbr, "KolibriOS" },
|
|
|
|
{ is_grub4dos_mbr, "Grub4DOS" },
|
2014-11-14 23:40:00 +00:00
|
|
|
{ is_grub2_mbr, "Grub 2.0" },
|
2016-01-07 15:49:58 +00:00
|
|
|
{ is_zero_mbr_not_including_disk_signature_or_copy_protect, "Zeroed" },
|
2014-02-09 02:54:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Returns TRUE if the drive seems bootable, FALSE otherwise
|
|
|
|
BOOL AnalyzeMBR(HANDLE hPhysicalDrive, const char* TargetName)
|
2014-01-05 01:39:41 +00:00
|
|
|
{
|
2014-02-09 02:54:07 +00:00
|
|
|
const char* mbr_name = "Master Boot Record";
|
2015-08-10 22:19:57 +00:00
|
|
|
FAKE_FD fake_fd = { 0 };
|
|
|
|
FILE* fp = (FILE*)&fake_fd;
|
2014-02-09 02:54:07 +00:00
|
|
|
int i;
|
2014-01-05 01:39:41 +00:00
|
|
|
|
2015-08-10 22:19:57 +00:00
|
|
|
fake_fd._handle = (char*)hPhysicalDrive;
|
2016-05-23 11:19:11 +00:00
|
|
|
set_bytes_per_sector(SelectedDrive.SectorSize);
|
2014-01-05 01:39:41 +00:00
|
|
|
|
2015-08-10 22:19:57 +00:00
|
|
|
if (!is_br(fp)) {
|
2014-02-09 02:54:07 +00:00
|
|
|
uprintf("%s does not have an x86 %s\n", TargetName, mbr_name);
|
2014-01-05 01:39:41 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-02-09 02:54:07 +00:00
|
|
|
for (i=0; i<ARRAYSIZE(known_mbr); i++) {
|
2015-08-10 22:19:57 +00:00
|
|
|
if (known_mbr[i].fn(fp)) {
|
2014-02-09 02:54:07 +00:00
|
|
|
uprintf("%s has a %s %s\n", TargetName, known_mbr[i].str, mbr_name);
|
2014-05-09 20:57:27 +00:00
|
|
|
return TRUE;
|
2014-02-09 02:54:07 +00:00
|
|
|
}
|
2014-01-05 01:39:41 +00:00
|
|
|
}
|
2014-02-09 02:54:07 +00:00
|
|
|
|
|
|
|
uprintf("%s has an unknown %s\n", TargetName, mbr_name);
|
2014-01-05 01:39:41 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-05-09 20:57:27 +00:00
|
|
|
const struct {int (*fn)(FILE *fp); char* str;} known_pbr[] = {
|
|
|
|
{ entire_fat_16_br_matches, "FAT16 DOS" },
|
|
|
|
{ entire_fat_16_fd_br_matches, "FAT16 FreeDOS" },
|
|
|
|
{ entire_fat_16_ros_br_matches, "FAT16 ReactOS" },
|
|
|
|
{ entire_fat_32_br_matches, "FAT32 DOS" },
|
|
|
|
{ entire_fat_32_nt_br_matches, "FAT32 NT" },
|
|
|
|
{ entire_fat_32_fd_br_matches, "FAT32 FreeDOS" },
|
|
|
|
{ entire_fat_32_ros_br_matches, "FAT32 ReactOS" },
|
2014-05-15 20:17:12 +00:00
|
|
|
{ entire_fat_32_kos_br_matches, "FAT32 KolibriOS" },
|
2014-05-09 20:57:27 +00:00
|
|
|
};
|
|
|
|
|
2014-01-05 01:39:41 +00:00
|
|
|
BOOL AnalyzePBR(HANDLE hLogicalVolume)
|
|
|
|
{
|
2014-05-09 20:57:27 +00:00
|
|
|
const char* pbr_name = "Partition Boot Record";
|
2015-08-10 22:19:57 +00:00
|
|
|
FAKE_FD fake_fd = { 0 };
|
|
|
|
FILE* fp = (FILE*)&fake_fd;
|
2014-05-09 20:57:27 +00:00
|
|
|
int i;
|
2014-01-05 01:39:41 +00:00
|
|
|
|
2015-08-10 22:19:57 +00:00
|
|
|
fake_fd._handle = (char*)hLogicalVolume;
|
2016-05-23 11:19:11 +00:00
|
|
|
set_bytes_per_sector(SelectedDrive.SectorSize);
|
2014-01-05 01:39:41 +00:00
|
|
|
|
2015-08-10 22:19:57 +00:00
|
|
|
if (!is_br(fp)) {
|
2014-05-09 20:57:27 +00:00
|
|
|
uprintf("Volume does not have an x86 %s\n", pbr_name);
|
2014-01-05 01:39:41 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-05-09 20:57:27 +00:00
|
|
|
|
2015-08-10 22:19:57 +00:00
|
|
|
if (is_fat_16_br(fp) || is_fat_32_br(fp)) {
|
2014-05-09 20:57:27 +00:00
|
|
|
for (i=0; i<ARRAYSIZE(known_pbr); i++) {
|
2015-08-10 22:19:57 +00:00
|
|
|
if (known_pbr[i].fn(fp)) {
|
2014-05-09 20:57:27 +00:00
|
|
|
uprintf("Drive has a %s %s\n", known_pbr[i].str, pbr_name);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2014-01-05 01:39:41 +00:00
|
|
|
}
|
2014-05-09 20:57:27 +00:00
|
|
|
uprintf("Volume has an unknown FAT16 or FAT32 %s\n", pbr_name);
|
2014-01-05 01:39:41 +00:00
|
|
|
} else {
|
2014-05-09 20:57:27 +00:00
|
|
|
uprintf("Volume has an unknown %s\n", pbr_name);
|
2014-01-05 01:39:41 +00:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-24 21:30:11 +00:00
|
|
|
/*
|
|
|
|
* Fill the drive properties (size, FS, etc)
|
2014-08-07 00:45:46 +00:00
|
|
|
* Returns TRUE if the drive has a partition that can be mounted in Windows, FALSE otherwise
|
2013-01-24 21:30:11 +00:00
|
|
|
*/
|
2014-08-07 00:45:46 +00:00
|
|
|
BOOL GetDrivePartitionData(DWORD DriveIndex, char* FileSystemName, DWORD FileSystemNameSize, BOOL bSilent)
|
2013-01-24 21:30:11 +00:00
|
|
|
{
|
2014-08-07 00:45:46 +00:00
|
|
|
// MBR partition types that can be mounted in Windows
|
|
|
|
const uint8_t mbr_mountable[] = { 0x01, 0x04, 0x06, 0x07, 0x0b, 0x0c, 0x0e, 0xef };
|
2015-03-16 20:34:04 +00:00
|
|
|
BOOL r, ret = FALSE, isUefiNtfs = FALSE;
|
2013-04-07 23:10:58 +00:00
|
|
|
HANDLE hPhysical;
|
2013-01-24 21:30:11 +00:00
|
|
|
DWORD size;
|
2015-01-28 23:22:11 +00:00
|
|
|
BYTE geometry[256] = {0}, layout[4096] = {0}, part_type;
|
2014-03-29 00:17:41 +00:00
|
|
|
PDISK_GEOMETRY_EX DiskGeometry = (PDISK_GEOMETRY_EX)(void*)geometry;
|
|
|
|
PDRIVE_LAYOUT_INFORMATION_EX DriveLayout = (PDRIVE_LAYOUT_INFORMATION_EX)(void*)layout;
|
2013-04-07 23:10:58 +00:00
|
|
|
char* volume_name;
|
|
|
|
char tmp[256];
|
2016-08-21 12:06:15 +00:00
|
|
|
DWORD i, j, big_floppy = FALSE;
|
2013-01-24 21:30:11 +00:00
|
|
|
|
2015-01-28 23:22:11 +00:00
|
|
|
if (FileSystemName == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-08-07 00:45:46 +00:00
|
|
|
SelectedDrive.nPartitions = 0;
|
2013-07-08 23:14:29 +00:00
|
|
|
// Populate the filesystem data
|
|
|
|
FileSystemName[0] = 0;
|
|
|
|
volume_name = GetLogicalName(DriveIndex, TRUE, FALSE);
|
|
|
|
if ((volume_name == NULL) || (!GetVolumeInformationA(volume_name, NULL, 0, NULL, NULL, NULL, FileSystemName, FileSystemNameSize))) {
|
2014-08-07 00:45:46 +00:00
|
|
|
suprintf("No volume information for drive 0x%02x\n", DriveIndex);
|
2013-07-08 23:14:29 +00:00
|
|
|
}
|
|
|
|
safe_free(volume_name);
|
|
|
|
|
2017-05-01 23:56:07 +00:00
|
|
|
hPhysical = GetPhysicalHandle(DriveIndex, FALSE, FALSE, TRUE);
|
2013-04-07 23:10:58 +00:00
|
|
|
if (hPhysical == INVALID_HANDLE_VALUE)
|
2014-01-14 20:17:07 +00:00
|
|
|
return 0;
|
2013-01-24 21:30:11 +00:00
|
|
|
|
2013-11-17 01:39:43 +00:00
|
|
|
r = DeviceIoControl(hPhysical, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
2013-01-24 21:30:11 +00:00
|
|
|
NULL, 0, geometry, sizeof(geometry), &size, NULL);
|
|
|
|
if (!r || size <= 0) {
|
2014-08-07 00:45:46 +00:00
|
|
|
suprintf("Could not get geometry for drive 0x%02x: %s\n", DriveIndex, WindowsErrorString());
|
2013-04-07 23:10:58 +00:00
|
|
|
safe_closehandle(hPhysical);
|
2014-01-14 20:17:07 +00:00
|
|
|
return 0;
|
2013-01-24 21:30:11 +00:00
|
|
|
}
|
2016-05-23 11:19:11 +00:00
|
|
|
SelectedDrive.DiskSize = DiskGeometry->DiskSize.QuadPart;
|
|
|
|
SelectedDrive.SectorSize = DiskGeometry->Geometry.BytesPerSector;
|
2016-07-07 17:13:01 +00:00
|
|
|
SelectedDrive.FirstDataSector = MAXDWORD;
|
2016-05-23 11:19:11 +00:00
|
|
|
if (SelectedDrive.SectorSize < 512) {
|
2015-01-20 02:56:17 +00:00
|
|
|
suprintf("Warning: Drive 0x%02x reports a sector size of %d - Correcting to 512 bytes.\n",
|
2016-05-23 11:19:11 +00:00
|
|
|
DriveIndex, SelectedDrive.SectorSize);
|
|
|
|
SelectedDrive.SectorSize = 512;
|
2014-08-03 18:34:01 +00:00
|
|
|
}
|
2016-05-23 11:19:11 +00:00
|
|
|
SelectedDrive.SectorsPerTrack = DiskGeometry->Geometry.SectorsPerTrack;
|
|
|
|
SelectedDrive.MediaType = DiskGeometry->Geometry.MediaType;
|
|
|
|
|
2017-01-11 01:12:00 +00:00
|
|
|
suprintf("Disk type: %s, Sector Size: %d bytes\n", (SelectedDrive.MediaType == FixedMedia)?"FIXED":"Removable",
|
2016-05-23 11:19:11 +00:00
|
|
|
SelectedDrive.SectorSize);
|
2015-01-28 23:22:11 +00:00
|
|
|
suprintf("Cylinders: %" PRIi64 ", TracksPerCylinder: %d, SectorsPerTrack: %d\n",
|
2013-01-24 21:30:11 +00:00
|
|
|
DiskGeometry->Geometry.Cylinders, DiskGeometry->Geometry.TracksPerCylinder, DiskGeometry->Geometry.SectorsPerTrack);
|
|
|
|
|
2015-11-05 22:54:38 +00:00
|
|
|
r = DeviceIoControl(hPhysical, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
|
2013-01-24 21:30:11 +00:00
|
|
|
NULL, 0, layout, sizeof(layout), &size, NULL );
|
|
|
|
if (!r || size <= 0) {
|
2014-08-07 00:45:46 +00:00
|
|
|
suprintf("Could not get layout for drive 0x%02x: %s\n", DriveIndex, WindowsErrorString());
|
2014-05-09 21:05:25 +00:00
|
|
|
safe_closehandle(hPhysical);
|
2014-01-14 20:17:07 +00:00
|
|
|
return 0;
|
2013-01-24 21:30:11 +00:00
|
|
|
}
|
|
|
|
|
2014-12-20 00:22:00 +00:00
|
|
|
#if defined(__GNUC__)
|
2015-09-19 16:00:51 +00:00
|
|
|
// GCC 4.9 bugs us about the fact that MS defined an expandable array as array[1]
|
2014-12-20 00:22:00 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
|
|
|
#endif
|
2013-01-24 21:30:11 +00:00
|
|
|
switch (DriveLayout->PartitionStyle) {
|
|
|
|
case PARTITION_STYLE_MBR:
|
|
|
|
SelectedDrive.PartitionType = PARTITION_STYLE_MBR;
|
|
|
|
for (i=0; i<DriveLayout->PartitionCount; i++) {
|
|
|
|
if (DriveLayout->PartitionEntry[i].Mbr.PartitionType != PARTITION_ENTRY_UNUSED) {
|
2014-08-07 00:45:46 +00:00
|
|
|
SelectedDrive.nPartitions++;
|
2013-01-24 21:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-21 12:06:15 +00:00
|
|
|
// Detect drives that are using the whole disk as a single partition
|
|
|
|
if ((DriveLayout->PartitionEntry[0].Mbr.PartitionType != PARTITION_ENTRY_UNUSED) &&
|
|
|
|
(DriveLayout->PartitionEntry[0].StartingOffset.QuadPart == 0LL)) {
|
2016-08-31 12:01:39 +00:00
|
|
|
suprintf("Partition type: SFD (Super Floppy Disk) or Unpartitioned");
|
2016-08-21 12:06:15 +00:00
|
|
|
big_floppy = TRUE;
|
|
|
|
} else {
|
|
|
|
suprintf("Partition type: MBR, NB Partitions: %d\n", SelectedDrive.nPartitions);
|
|
|
|
SelectedDrive.has_mbr_uefi_marker = (DriveLayout->Mbr.Signature == MBR_UEFI_MARKER);
|
|
|
|
suprintf("Disk ID: 0x%08X %s\n", DriveLayout->Mbr.Signature, SelectedDrive.has_mbr_uefi_marker ? "(UEFI target)" : "");
|
|
|
|
AnalyzeMBR(hPhysical, "Drive");
|
|
|
|
}
|
2013-01-24 21:30:11 +00:00
|
|
|
for (i=0; i<DriveLayout->PartitionCount; i++) {
|
|
|
|
if (DriveLayout->PartitionEntry[i].Mbr.PartitionType != PARTITION_ENTRY_UNUSED) {
|
|
|
|
part_type = DriveLayout->PartitionEntry[i].Mbr.PartitionType;
|
2015-03-16 20:34:04 +00:00
|
|
|
isUefiNtfs = (i == 1) && (part_type == 0xef) &&
|
2016-04-28 08:09:43 +00:00
|
|
|
(DriveLayout->PartitionEntry[i].PartitionLength.QuadPart <= 1*MB);
|
2016-08-21 12:06:15 +00:00
|
|
|
suprintf("Partition %d%s:\n", i+(big_floppy?0:1), isUefiNtfs?" (UEFI:NTFS)":"");
|
2014-08-07 00:45:46 +00:00
|
|
|
for (j=0; j<ARRAYSIZE(mbr_mountable); j++) {
|
|
|
|
if (part_type == mbr_mountable[j]) {
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-28 23:22:11 +00:00
|
|
|
// NB: MinGW's gcc 4.9.2 broke "%lld" printout on XP so we use the inttypes.h "PRI##" qualifiers
|
2016-08-21 12:06:15 +00:00
|
|
|
suprintf(" Type: %s (0x%02x)\r\n Size: %s (%" PRIi64 " bytes)\r\n Start Sector: %" PRIi64 ", Boot: %s",
|
|
|
|
((part_type==0x07||big_floppy)&&(FileSystemName[0]!=0))?FileSystemName:GetPartitionType(part_type), big_floppy?0:part_type,
|
2014-03-01 00:09:40 +00:00
|
|
|
SizeToHumanReadable(DriveLayout->PartitionEntry[i].PartitionLength.QuadPart, TRUE, FALSE),
|
2016-07-07 17:13:01 +00:00
|
|
|
DriveLayout->PartitionEntry[i].PartitionLength.QuadPart,
|
|
|
|
DriveLayout->PartitionEntry[i].StartingOffset.QuadPart / SelectedDrive.SectorSize,
|
2016-08-21 12:06:15 +00:00
|
|
|
DriveLayout->PartitionEntry[i].Mbr.BootIndicator?"Yes":"No");
|
2016-07-07 17:13:01 +00:00
|
|
|
SelectedDrive.FirstDataSector = min(SelectedDrive.FirstDataSector,
|
|
|
|
(DWORD)(DriveLayout->PartitionEntry[i].StartingOffset.QuadPart / SelectedDrive.SectorSize));
|
2015-03-16 20:34:04 +00:00
|
|
|
if ((part_type == RUFUS_EXTRA_PARTITION_TYPE) || (isUefiNtfs))
|
2014-12-20 00:22:00 +00:00
|
|
|
// This is a partition Rufus created => we can safely ignore it
|
2015-01-20 21:50:24 +00:00
|
|
|
--SelectedDrive.nPartitions;
|
2013-01-24 21:30:11 +00:00
|
|
|
if (part_type == 0xee) // Flag a protective MBR for non GPT platforms (XP)
|
|
|
|
SelectedDrive.has_protective_mbr = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PARTITION_STYLE_GPT:
|
|
|
|
SelectedDrive.PartitionType = PARTITION_STYLE_GPT;
|
2014-08-07 00:45:46 +00:00
|
|
|
suprintf("Partition type: GPT, NB Partitions: %d\n", DriveLayout->PartitionCount);
|
|
|
|
suprintf("Disk GUID: %s\n", GuidToString(&DriveLayout->Gpt.DiskId));
|
2015-01-28 23:22:11 +00:00
|
|
|
suprintf("Max parts: %d, Start Offset: %" PRIi64 ", Usable = %" PRIi64 " bytes\n",
|
2013-01-24 21:30:11 +00:00
|
|
|
DriveLayout->Gpt.MaxPartitionCount, DriveLayout->Gpt.StartingUsableOffset.QuadPart, DriveLayout->Gpt.UsableLength.QuadPart);
|
|
|
|
for (i=0; i<DriveLayout->PartitionCount; i++) {
|
2014-08-07 00:45:46 +00:00
|
|
|
SelectedDrive.nPartitions++;
|
2013-01-24 21:30:11 +00:00
|
|
|
tmp[0] = 0;
|
|
|
|
wchar_to_utf8_no_alloc(DriveLayout->PartitionEntry[i].Gpt.Name, tmp, sizeof(tmp));
|
2015-01-16 01:51:24 +00:00
|
|
|
suprintf("Partition %d:\r\n Type: %s\r\n Name: '%s'\n", i+1,
|
2013-01-24 21:30:11 +00:00
|
|
|
GuidToString(&DriveLayout->PartitionEntry[i].Gpt.PartitionType), tmp);
|
2015-01-28 23:22:11 +00:00
|
|
|
suprintf(" ID: %s\r\n Size: %s (%" PRIi64 " bytes)\r\n Start Sector: %" PRIi64 ", Attributes: 0x%016" PRIX64 "\n",
|
2016-07-07 17:13:01 +00:00
|
|
|
GuidToString(&DriveLayout->PartitionEntry[i].Gpt.PartitionId),
|
|
|
|
SizeToHumanReadable(DriveLayout->PartitionEntry[i].PartitionLength.QuadPart, TRUE, FALSE),
|
|
|
|
DriveLayout->PartitionEntry[i].PartitionLength,
|
|
|
|
DriveLayout->PartitionEntry[i].StartingOffset.QuadPart / SelectedDrive.SectorSize,
|
2013-01-24 21:30:11 +00:00
|
|
|
DriveLayout->PartitionEntry[i].Gpt.Attributes);
|
2016-07-07 17:13:01 +00:00
|
|
|
SelectedDrive.FirstDataSector = min(SelectedDrive.FirstDataSector,
|
|
|
|
(DWORD)(DriveLayout->PartitionEntry[i].StartingOffset.QuadPart / SelectedDrive.SectorSize));
|
2015-01-20 21:50:24 +00:00
|
|
|
// Don't register the partitions that we don't care about destroying
|
2015-03-16 20:34:04 +00:00
|
|
|
if ( (strcmp(tmp, "UEFI:NTFS") == 0) ||
|
2015-01-20 21:50:24 +00:00
|
|
|
(CompareGUID(&DriveLayout->PartitionEntry[i].Gpt.PartitionType, &PARTITION_MSFT_RESERVED_GUID)) ||
|
|
|
|
(CompareGUID(&DriveLayout->PartitionEntry[i].Gpt.PartitionType, &PARTITION_SYSTEM_GUID)) )
|
|
|
|
--SelectedDrive.nPartitions;
|
2014-08-07 00:45:46 +00:00
|
|
|
if ( (memcmp(&PARTITION_BASIC_DATA_GUID, &DriveLayout->PartitionEntry[i].Gpt.PartitionType, sizeof(GUID)) == 0) &&
|
|
|
|
(nWindowsVersion >= WINDOWS_VISTA) )
|
|
|
|
ret = TRUE;
|
2013-01-24 21:30:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SelectedDrive.PartitionType = PARTITION_STYLE_MBR;
|
2014-08-07 00:45:46 +00:00
|
|
|
suprintf("Partition type: RAW\n");
|
2013-01-24 21:30:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-12-20 00:22:00 +00:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic warning "-Warray-bounds"
|
|
|
|
#endif
|
2013-04-07 23:10:58 +00:00
|
|
|
safe_closehandle(hPhysical);
|
2013-01-24 21:30:11 +00:00
|
|
|
|
2014-08-07 00:45:46 +00:00
|
|
|
return ret;
|
2013-07-08 23:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Flush file data
|
|
|
|
*/
|
|
|
|
static BOOL FlushDrive(char drive_letter)
|
|
|
|
{
|
|
|
|
HANDLE hDrive = INVALID_HANDLE_VALUE;
|
|
|
|
BOOL r = FALSE;
|
|
|
|
char logical_drive[] = "\\\\.\\#:";
|
|
|
|
|
|
|
|
logical_drive[4] = drive_letter;
|
|
|
|
hDrive = CreateFileA(logical_drive, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
|
2014-11-11 19:17:39 +00:00
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2013-07-08 23:14:29 +00:00
|
|
|
if (hDrive == INVALID_HANDLE_VALUE) {
|
|
|
|
uprintf("Failed to open %c: for flushing: %s\n", drive_letter, WindowsErrorString());
|
|
|
|
goto out;
|
2013-01-24 21:30:11 +00:00
|
|
|
}
|
2013-07-08 23:14:29 +00:00
|
|
|
r = FlushFileBuffers(hDrive);
|
|
|
|
if (r == FALSE)
|
|
|
|
uprintf("Failed to flush %c: %s\n", drive_letter, WindowsErrorString());
|
2013-01-24 21:30:11 +00:00
|
|
|
|
2013-07-08 23:14:29 +00:00
|
|
|
out:
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
return r;
|
2013-01-24 21:30:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 23:10:58 +00:00
|
|
|
/*
|
|
|
|
* Unmount of volume using the DISMOUNT_VOLUME ioctl
|
|
|
|
*/
|
|
|
|
BOOL UnmountVolume(HANDLE hDrive)
|
2011-12-11 02:19:38 +00:00
|
|
|
{
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
if (!DeviceIoControl(hDrive, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &size, NULL)) {
|
2014-05-20 18:28:46 +00:00
|
|
|
uprintf("Could not unmount drive: %s\n", WindowsErrorString());
|
2011-12-11 02:19:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-01-22 02:40:43 +00:00
|
|
|
|
2013-07-08 23:14:29 +00:00
|
|
|
/*
|
|
|
|
* Mount the volume identified by drive_guid to mountpoint drive_name
|
|
|
|
*/
|
|
|
|
BOOL MountVolume(char* drive_name, char *drive_guid)
|
|
|
|
{
|
|
|
|
char mounted_guid[52]; // You need at least 51 characters on XP
|
2013-12-10 19:30:12 +00:00
|
|
|
char mounted_letter[16] = {0};
|
|
|
|
DWORD size;
|
|
|
|
|
2015-06-24 19:00:20 +00:00
|
|
|
if (drive_name[0] == '?')
|
|
|
|
return FALSE;
|
|
|
|
|
2013-12-10 19:30:12 +00:00
|
|
|
// For fixed disks, Windows may already have remounted the volume, but with a different letter
|
|
|
|
// than the one we want. If that's the case, we need to unmount first.
|
|
|
|
if ( (GetVolumePathNamesForVolumeNameA(drive_guid, mounted_letter, sizeof(mounted_letter), &size))
|
2014-01-11 00:19:03 +00:00
|
|
|
&& (size > 1) && (mounted_letter[0] != drive_name[0]) ) {
|
|
|
|
uprintf("Volume is already mounted, but as %c: instead of %c: - Unmounting...\n", mounted_letter[0], drive_name[0]);
|
2013-12-10 19:30:12 +00:00
|
|
|
if (!DeleteVolumeMountPointA(mounted_letter))
|
|
|
|
uprintf("Failed to unmount volume: %s", WindowsErrorString());
|
2015-08-28 22:12:55 +00:00
|
|
|
// Also delete the destination mountpoint if needed (Don't care about errors)
|
|
|
|
DeleteVolumeMountPointA(drive_name);
|
2013-12-10 19:30:12 +00:00
|
|
|
Sleep(200);
|
|
|
|
}
|
2013-07-08 23:14:29 +00:00
|
|
|
|
|
|
|
if (!SetVolumeMountPointA(drive_name, drive_guid)) {
|
|
|
|
// If the OS was faster than us at remounting the drive, this operation can fail
|
|
|
|
// with ERROR_DIR_NOT_EMPTY. If that's the case, just check that mountpoints match
|
|
|
|
if (GetLastError() == ERROR_DIR_NOT_EMPTY) {
|
|
|
|
if (!GetVolumeNameForVolumeMountPointA(drive_name, mounted_guid, sizeof(mounted_guid))) {
|
|
|
|
uprintf("%s already mounted, but volume GUID could not be checked: %s\n",
|
|
|
|
drive_name, WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (safe_strcmp(drive_guid, mounted_guid) != 0) {
|
|
|
|
uprintf("%s already mounted, but volume GUID doesn't match:\r\n expected %s, got %s\n",
|
|
|
|
drive_name, drive_guid, mounted_guid);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
uprintf("%s was already mounted as %s\n", drive_guid, drive_name);
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-01-20 02:56:17 +00:00
|
|
|
/*
|
|
|
|
* Mount partition #part_nr, residing on the same disk as drive_name to an available
|
|
|
|
* drive letter. Returns the newly allocated drive string.
|
|
|
|
* We need to do this because, for instance, EFI System partitions are not assigned
|
|
|
|
* Volume GUIDs by the OS, and we need to have a letter assigned, for when we invoke
|
|
|
|
* bcdtool for Windows To Go. All in all, the process looks like this:
|
|
|
|
* 1. F: = \Device\HarddiskVolume9 (SINGLE LOOKUP)
|
|
|
|
* 2. Harddisk5Partition1 = \Device\HarddiskVolume9 (FULL LOOKUP)
|
|
|
|
* 3. Harddisk5Partition2 = \Device\HarddiskVolume10 (SINGLE LOOKUP)
|
|
|
|
* 4. DefineDosDevice(letter, \Device\HarddiskVolume10)
|
|
|
|
*/
|
|
|
|
char* AltMountVolume(const char* drive_name, uint8_t part_nr)
|
|
|
|
{
|
|
|
|
static char mounted_drive[] = "?:";
|
2015-01-23 02:26:41 +00:00
|
|
|
const DWORD bufsize = 65536;
|
2015-01-20 02:56:17 +00:00
|
|
|
char *buffer = NULL, *p, target[2][MAX_PATH], *ret = NULL;
|
2015-01-23 02:26:41 +00:00
|
|
|
size_t i;
|
2015-01-20 02:56:17 +00:00
|
|
|
|
|
|
|
mounted_drive[0] = GetUnusedDriveLetter();
|
|
|
|
if (mounted_drive[0] == 0) {
|
|
|
|
uprintf("Could not find an unused drive letter");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
target[0][0] = 0;
|
|
|
|
// Convert our drive letter to something like "\Device\HarddiskVolume9"
|
2015-01-20 21:50:24 +00:00
|
|
|
if (!QueryDosDeviceA(drive_name, target[0], MAX_PATH) || (strlen(target[0]) == 0)) {
|
|
|
|
uprintf("Could not get the DOS volume name for '%s': %s", drive_name, WindowsErrorString());
|
2015-01-20 02:56:17 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now parse the whole DOS device list to find the 'Harddisk#Partition#' that matches the above
|
|
|
|
// TODO: realloc if someone ever manages to burst through 64K of DOS devices
|
|
|
|
buffer = malloc(bufsize);
|
|
|
|
if (buffer == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
buffer[0] = 0;
|
|
|
|
if (!QueryDosDeviceA(NULL, buffer, bufsize)) {
|
|
|
|
uprintf("Could not get the DOS device list: %s", WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
while (strlen(p) != 0) {
|
|
|
|
if ((strncmp("Harddisk", p, 8) == 0) && (strstr(&p[9], "Partition") != NULL)) {
|
|
|
|
target[1][0] = 0;
|
|
|
|
if (QueryDosDeviceA(p, target[1], MAX_PATH) && (strlen(target[1]) != 0))
|
|
|
|
if ((strcmp(target[1], target[0]) == 0) && (p[1] != ':'))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p += strlen(p) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = strlen(p);
|
|
|
|
if (i == 0) {
|
|
|
|
uprintf("Could not find partition mapping for %s", target[0]);
|
|
|
|
goto out;
|
|
|
|
}
|
2015-11-05 22:54:38 +00:00
|
|
|
|
2015-01-20 02:56:17 +00:00
|
|
|
while ((--i > 0) && (isdigit(p[i])));
|
|
|
|
p[++i] = '0' + part_nr;
|
|
|
|
p[++i] = 0;
|
|
|
|
|
|
|
|
target[0][0] = 0;
|
2015-01-20 21:50:24 +00:00
|
|
|
if (!QueryDosDeviceA(p, target[0], MAX_PATH) || (strlen(target[0]) == 0)) {
|
|
|
|
uprintf("Could not find the DOS volume name for partition '%s': %s", p, WindowsErrorString());
|
2015-01-20 02:56:17 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DefineDosDeviceA(DDD_RAW_TARGET_PATH | DDD_NO_BROADCAST_SYSTEM, mounted_drive, target[0])) {
|
|
|
|
uprintf("Could not mount '%s' to '%s': %s", target[0], mounted_drive, WindowsErrorString());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
uprintf("Successfully mounted '%s' (USB partition %d) as '%s'", target[0], part_nr, mounted_drive);
|
|
|
|
ret = mounted_drive;
|
|
|
|
|
|
|
|
out:
|
|
|
|
safe_free(buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmount a volume that was mounted by AltmountVolume()
|
|
|
|
*/
|
|
|
|
BOOL AltUnmountVolume(const char* drive_name)
|
|
|
|
{
|
2015-01-20 21:50:24 +00:00
|
|
|
if (drive_name == NULL)
|
|
|
|
return FALSE;
|
2015-01-20 02:56:17 +00:00
|
|
|
if (!DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_NO_BROADCAST_SYSTEM, drive_name, NULL)) {
|
|
|
|
uprintf("Could not unmount '%s': %s", drive_name, WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
uprintf("Successfully unmounted '%s'", drive_name);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-07-08 23:14:29 +00:00
|
|
|
/*
|
|
|
|
* Issue a complete remount of the volume
|
|
|
|
*/
|
|
|
|
BOOL RemountVolume(char* drive_name)
|
|
|
|
{
|
|
|
|
char drive_guid[51];
|
|
|
|
|
|
|
|
// UDF requires a sync/flush, and it's also a good idea for other FS's
|
|
|
|
FlushDrive(drive_name[0]);
|
|
|
|
if (GetVolumeNameForVolumeMountPointA(drive_name, drive_guid, sizeof(drive_guid))) {
|
|
|
|
if (DeleteVolumeMountPointA(drive_name)) {
|
|
|
|
Sleep(200);
|
|
|
|
if (MountVolume(drive_name, drive_guid)) {
|
2017-05-03 13:48:24 +00:00
|
|
|
uprintf("Successfully remounted %s on %C:", drive_guid, drive_name[0]);
|
2013-07-08 23:14:29 +00:00
|
|
|
} else {
|
2017-05-03 13:48:24 +00:00
|
|
|
uprintf("Failed to remount %s on %C:", drive_guid, drive_name[0]);
|
2013-07-08 23:14:29 +00:00
|
|
|
// This will leave the drive inaccessible and must be flagged as an error
|
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_CANT_REMOUNT_VOLUME);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2017-05-03 13:48:24 +00:00
|
|
|
uprintf("Could not remount %C: %s", drive_name[0], WindowsErrorString());
|
2013-07-08 23:14:29 +00:00
|
|
|
// Try to continue regardless
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-22 02:40:43 +00:00
|
|
|
/* MinGW is unhappy about accessing partitions beside the first unless we redef */
|
|
|
|
typedef struct _DRIVE_LAYOUT_INFORMATION_EX4 {
|
|
|
|
DWORD PartitionStyle;
|
|
|
|
DWORD PartitionCount;
|
|
|
|
union {
|
|
|
|
DRIVE_LAYOUT_INFORMATION_MBR Mbr;
|
|
|
|
DRIVE_LAYOUT_INFORMATION_GPT Gpt;
|
|
|
|
} Type;
|
|
|
|
PARTITION_INFORMATION_EX PartitionEntry[4];
|
|
|
|
} DRIVE_LAYOUT_INFORMATION_EX4,*PDRIVE_LAYOUT_INFORMATION_EX4;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a partition table
|
2013-01-27 20:56:57 +00:00
|
|
|
* See http://technet.microsoft.com/en-us/library/cc739412.aspx for some background info
|
|
|
|
* NB: if you modify the MBR outside of using the Windows API, Windows still uses the cached
|
2013-06-25 01:55:25 +00:00
|
|
|
* copy it got from the last IOCTL, and ignores your changes until you replug the drive
|
|
|
|
* or issue an IOCTL_DISK_UPDATE_PROPERTIES.
|
|
|
|
*/
|
2015-01-20 02:56:17 +00:00
|
|
|
BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL mbr_uefi_marker, uint8_t extra_partitions)
|
2013-01-22 02:40:43 +00:00
|
|
|
{
|
|
|
|
const char* PartitionTypeName[2] = { "MBR", "GPT" };
|
2014-12-20 00:22:00 +00:00
|
|
|
unsigned char* buffer;
|
2016-04-28 08:09:43 +00:00
|
|
|
size_t uefi_ntfs_size = 0;
|
2013-01-22 02:40:43 +00:00
|
|
|
CREATE_DISK CreateDisk = {PARTITION_STYLE_RAW, {{0}}};
|
|
|
|
DRIVE_LAYOUT_INFORMATION_EX4 DriveLayoutEx = {0};
|
|
|
|
BOOL r;
|
2015-01-19 01:23:32 +00:00
|
|
|
DWORD i, size, bufsize, pn = 0;
|
2015-01-20 21:50:24 +00:00
|
|
|
LONGLONG main_part_size_in_sectors, extra_part_size_in_tracks = 0, ms_efi_size;
|
2016-05-23 11:19:11 +00:00
|
|
|
const LONGLONG bytes_per_track = ((LONGLONG)SelectedDrive.SectorsPerTrack) * SelectedDrive.SectorSize;
|
2013-01-22 02:40:43 +00:00
|
|
|
|
2015-01-01 23:39:28 +00:00
|
|
|
PrintInfoDebug(0, MSG_238, PartitionTypeName[partition_style]);
|
2015-01-19 01:23:32 +00:00
|
|
|
|
2016-04-28 08:09:43 +00:00
|
|
|
if (extra_partitions & XP_UEFI_NTFS) {
|
2015-03-16 20:34:04 +00:00
|
|
|
uefi_ntfs_size = GetResourceSize(hMainInstance, MAKEINTRESOURCEA(IDR_UEFI_NTFS), _RT_RCDATA, "uefi-ntfs.img");
|
|
|
|
if (uefi_ntfs_size == 0)
|
2015-01-22 22:31:34 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-22 02:40:43 +00:00
|
|
|
|
2015-01-19 01:23:32 +00:00
|
|
|
// Compute the start offset of our first partition
|
2013-01-22 02:40:43 +00:00
|
|
|
if ((partition_style == PARTITION_STYLE_GPT) || (!IsChecked(IDC_EXTRA_PARTITION))) {
|
|
|
|
// Go with the MS 1 MB wastage at the beginning...
|
2016-02-20 22:52:32 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart = MB;
|
2013-01-22 02:40:43 +00:00
|
|
|
} else {
|
|
|
|
// Align on Cylinder
|
2015-01-20 21:50:24 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart = bytes_per_track;
|
2013-01-22 02:40:43 +00:00
|
|
|
}
|
2015-01-19 01:23:32 +00:00
|
|
|
|
|
|
|
// If required, set the MSR partition (GPT only - must be created before the data part)
|
2015-01-20 02:56:17 +00:00
|
|
|
if ((partition_style == PARTITION_STYLE_GPT) && (extra_partitions & XP_MSR)) {
|
2015-01-19 01:23:32 +00:00
|
|
|
uprintf("Adding MSR partition");
|
2016-02-20 22:52:32 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart = 128*MB;
|
2015-01-19 01:23:32 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = PARTITION_MSFT_RESERVED_GUID;
|
|
|
|
IGNORE_RETVAL(CoCreateGuid(&DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionId));
|
2015-02-13 19:51:30 +00:00
|
|
|
// coverity[strcpy_overrun]
|
2015-01-19 01:23:32 +00:00
|
|
|
wcscpy(DriveLayoutEx.PartitionEntry[pn].Gpt.Name, L"Microsoft reserved partition");
|
2015-01-20 21:50:24 +00:00
|
|
|
|
|
|
|
// We must zero the beginning of this partition, else we get FAT leftovers and stuff
|
|
|
|
if (SetFilePointerEx(hDrive, DriveLayoutEx.PartitionEntry[pn].StartingOffset, NULL, FILE_BEGIN)) {
|
|
|
|
bufsize = 65536; // 64K should be enough for everyone
|
|
|
|
buffer = calloc(bufsize, 1);
|
|
|
|
if (buffer != NULL) {
|
2016-01-14 17:43:02 +00:00
|
|
|
if (!WriteFileWithRetry(hDrive, buffer, bufsize, &size, WRITE_RETRIES))
|
2015-01-20 21:50:24 +00:00
|
|
|
uprintf(" Could not zero MSR: %s", WindowsErrorString());
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
}
|
2015-01-26 23:20:39 +00:00
|
|
|
|
|
|
|
pn++;
|
|
|
|
DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart = DriveLayoutEx.PartitionEntry[pn-1].StartingOffset.QuadPart +
|
|
|
|
DriveLayoutEx.PartitionEntry[pn-1].PartitionLength.QuadPart;
|
2015-01-19 01:23:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set our main data partition
|
2015-01-20 02:56:17 +00:00
|
|
|
main_part_size_in_sectors = (SelectedDrive.DiskSize - DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart) /
|
2015-01-19 01:23:32 +00:00
|
|
|
// Need 33 sectors at the end for secondary GPT
|
2016-05-23 11:19:11 +00:00
|
|
|
SelectedDrive.SectorSize - ((partition_style == PARTITION_STYLE_GPT)?33:0);
|
2015-01-22 22:31:34 +00:00
|
|
|
if (main_part_size_in_sectors <= 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-01-20 21:50:24 +00:00
|
|
|
// Adjust the size according to extra partitions (which we always align to a track)
|
2015-01-20 02:56:17 +00:00
|
|
|
if (extra_partitions) {
|
2015-01-20 21:50:24 +00:00
|
|
|
uprintf("Adding extra partition");
|
|
|
|
if (extra_partitions & XP_EFI) {
|
|
|
|
// The size of the EFI partition depends on the minimum size we're able to format in FAT32,
|
|
|
|
// which in turn depends on the cluster size used, which in turn depends on the disk sector size.
|
2016-05-23 11:19:11 +00:00
|
|
|
if (SelectedDrive.SectorSize <= 1024)
|
2016-02-20 22:52:32 +00:00
|
|
|
ms_efi_size = 100*MB;
|
2016-05-23 11:19:11 +00:00
|
|
|
else if (SelectedDrive.SectorSize <= 4096)
|
2016-02-20 22:52:32 +00:00
|
|
|
ms_efi_size = 300*MB;
|
2015-01-20 21:50:24 +00:00
|
|
|
else
|
2016-02-20 22:52:32 +00:00
|
|
|
ms_efi_size = 1200*MB; // That'll teach you to have a nonstandard disk!
|
2015-01-20 21:50:24 +00:00
|
|
|
extra_part_size_in_tracks = (ms_efi_size + bytes_per_track - 1) / bytes_per_track;
|
2015-03-16 20:34:04 +00:00
|
|
|
} else if (extra_partitions & XP_UEFI_NTFS)
|
2016-06-16 16:29:15 +00:00
|
|
|
extra_part_size_in_tracks = (max(MIN_EXTRA_PART_SIZE, uefi_ntfs_size) + bytes_per_track - 1) / bytes_per_track;
|
2015-01-20 21:50:24 +00:00
|
|
|
else if (extra_partitions & XP_COMPAT)
|
2015-01-20 02:56:17 +00:00
|
|
|
extra_part_size_in_tracks = 1; // One track for the extra partition
|
2015-01-28 23:22:11 +00:00
|
|
|
uprintf("Reserved %" PRIi64" tracks (%s) for extra partition", extra_part_size_in_tracks,
|
2015-01-20 21:50:24 +00:00
|
|
|
SizeToHumanReadable(extra_part_size_in_tracks * bytes_per_track, TRUE, FALSE));
|
2016-05-23 11:19:11 +00:00
|
|
|
main_part_size_in_sectors = ((main_part_size_in_sectors / SelectedDrive.SectorsPerTrack) -
|
|
|
|
extra_part_size_in_tracks) * SelectedDrive.SectorsPerTrack;
|
2015-01-20 02:56:17 +00:00
|
|
|
if (main_part_size_in_sectors <= 0)
|
2014-12-20 00:22:00 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-05-23 11:19:11 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart = main_part_size_in_sectors * SelectedDrive.SectorSize;
|
2015-01-19 01:23:32 +00:00
|
|
|
if (partition_style == PARTITION_STYLE_MBR) {
|
|
|
|
DriveLayoutEx.PartitionEntry[pn].Mbr.BootIndicator = IsChecked(IDC_BOOT);
|
|
|
|
switch (file_system) {
|
|
|
|
case FS_FAT16:
|
|
|
|
DriveLayoutEx.PartitionEntry[pn].Mbr.PartitionType = 0x0e; // FAT16 LBA
|
|
|
|
break;
|
|
|
|
case FS_NTFS:
|
|
|
|
case FS_EXFAT:
|
|
|
|
case FS_UDF:
|
|
|
|
case FS_REFS:
|
|
|
|
DriveLayoutEx.PartitionEntry[pn].Mbr.PartitionType = 0x07;
|
|
|
|
break;
|
|
|
|
case FS_FAT32:
|
|
|
|
DriveLayoutEx.PartitionEntry[pn].Mbr.PartitionType = 0x0c; // FAT32 LBA
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
uprintf("Unsupported file system\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = PARTITION_BASIC_DATA_GUID;
|
|
|
|
IGNORE_RETVAL(CoCreateGuid(&DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionId));
|
|
|
|
wcscpy(DriveLayoutEx.PartitionEntry[pn].Gpt.Name, L"Microsoft Basic Data");
|
|
|
|
}
|
|
|
|
pn++;
|
|
|
|
|
|
|
|
// Set the optional extra partition
|
2015-01-20 02:56:17 +00:00
|
|
|
if (extra_partitions) {
|
2015-01-19 01:23:32 +00:00
|
|
|
// Should end on a track boundary
|
|
|
|
DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart = DriveLayoutEx.PartitionEntry[pn-1].StartingOffset.QuadPart +
|
2015-01-20 02:56:17 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn-1].PartitionLength.QuadPart;
|
2015-03-16 20:34:04 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart = (extra_partitions & XP_UEFI_NTFS)?uefi_ntfs_size:
|
2016-05-23 11:19:11 +00:00
|
|
|
extra_part_size_in_tracks * SelectedDrive.SectorsPerTrack * SelectedDrive.SectorSize;
|
2015-01-19 01:23:32 +00:00
|
|
|
if (partition_style == PARTITION_STYLE_GPT) {
|
2015-03-16 20:34:04 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = (extra_partitions & XP_UEFI_NTFS)?
|
2015-03-11 21:43:08 +00:00
|
|
|
PARTITION_BASIC_DATA_GUID:PARTITION_SYSTEM_GUID;
|
2015-01-19 01:23:32 +00:00
|
|
|
IGNORE_RETVAL(CoCreateGuid(&DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionId));
|
2015-03-16 20:34:04 +00:00
|
|
|
wcscpy(DriveLayoutEx.PartitionEntry[pn].Gpt.Name, (extra_partitions & XP_UEFI_NTFS)?L"UEFI:NTFS":L"EFI system partition");
|
2015-01-19 01:23:32 +00:00
|
|
|
} else {
|
2015-03-16 20:34:04 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].Mbr.PartitionType = (extra_partitions & XP_UEFI_NTFS)?0xef:RUFUS_EXTRA_PARTITION_TYPE;
|
2015-01-20 21:50:24 +00:00
|
|
|
if (extra_partitions & XP_COMPAT)
|
|
|
|
// Set the one track compatibility partition to be all hidden sectors
|
2016-05-23 11:19:11 +00:00
|
|
|
DriveLayoutEx.PartitionEntry[pn].Mbr.HiddenSectors = SelectedDrive.SectorsPerTrack;
|
2015-01-19 01:23:32 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 20:34:04 +00:00
|
|
|
// We need to write the UEFI:NTFS partition before we refresh the disk
|
|
|
|
if (extra_partitions & XP_UEFI_NTFS) {
|
|
|
|
uprintf("Writing UEFI:NTFS partition...");
|
2015-01-19 01:23:32 +00:00
|
|
|
if (!SetFilePointerEx(hDrive, DriveLayoutEx.PartitionEntry[pn].StartingOffset, NULL, FILE_BEGIN)) {
|
|
|
|
uprintf("Unable to set position");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-03-16 20:34:04 +00:00
|
|
|
buffer = GetResource(hMainInstance, MAKEINTRESOURCEA(IDR_UEFI_NTFS), _RT_RCDATA, "uefi-ntfs.img", &bufsize, FALSE);
|
2015-01-19 01:23:32 +00:00
|
|
|
if (buffer == NULL) {
|
2015-03-16 20:34:04 +00:00
|
|
|
uprintf("Could not access uefi-ntfs.img");
|
2015-01-19 01:23:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-01-14 17:43:02 +00:00
|
|
|
if(!WriteFileWithRetry(hDrive, buffer, bufsize, &size, WRITE_RETRIES)) {
|
|
|
|
uprintf("Write error: %s", WindowsErrorString());
|
2015-01-19 01:23:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pn++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the remaining partition data
|
|
|
|
for (i = 0; i < pn; i++) {
|
|
|
|
DriveLayoutEx.PartitionEntry[i].PartitionNumber = i+1;
|
|
|
|
DriveLayoutEx.PartitionEntry[i].PartitionStyle = partition_style;
|
|
|
|
DriveLayoutEx.PartitionEntry[i].RewritePartition = TRUE;
|
|
|
|
}
|
2013-01-22 02:40:43 +00:00
|
|
|
|
|
|
|
switch (partition_style) {
|
|
|
|
case PARTITION_STYLE_MBR:
|
|
|
|
CreateDisk.PartitionStyle = PARTITION_STYLE_MBR;
|
2013-01-27 20:56:57 +00:00
|
|
|
// If MBR+UEFI is selected, write an UEFI marker in lieu of the regular MBR signature.
|
|
|
|
// This helps us reselect the partition scheme option that was used when creating the
|
|
|
|
// drive in Rufus. As far as I can tell, Windows doesn't care much if this signature
|
|
|
|
// isn't unique for USB drives.
|
2016-02-24 16:10:54 +00:00
|
|
|
CreateDisk.Mbr.Signature = mbr_uefi_marker?MBR_UEFI_MARKER:(DWORD)_GetTickCount64();
|
2013-01-22 02:40:43 +00:00
|
|
|
|
|
|
|
DriveLayoutEx.PartitionStyle = PARTITION_STYLE_MBR;
|
|
|
|
DriveLayoutEx.PartitionCount = 4; // Must be multiple of 4 for MBR
|
|
|
|
DriveLayoutEx.Type.Mbr.Signature = CreateDisk.Mbr.Signature;
|
|
|
|
// TODO: CHS fixup (32 sectors/track) through a cheat mode, if requested
|
|
|
|
// NB: disk geometry is computed by BIOS & co. by finding a match between LBA and CHS value of first partition
|
|
|
|
// ms-sys's write_partition_number_of_heads() and write_partition_start_sector_number() can be used if needed
|
|
|
|
break;
|
|
|
|
case PARTITION_STYLE_GPT:
|
2013-07-05 21:20:46 +00:00
|
|
|
// TODO: (?) As per MSDN: "When specifying a GUID partition table (GPT) as the PARTITION_STYLE of the CREATE_DISK
|
|
|
|
// structure, an application should wait for the MSR partition arrival before sending the IOCTL_DISK_SET_DRIVE_LAYOUT_EX
|
|
|
|
// control code. For more information about device notification, see RegisterDeviceNotification."
|
|
|
|
|
2013-01-22 02:40:43 +00:00
|
|
|
CreateDisk.PartitionStyle = PARTITION_STYLE_GPT;
|
|
|
|
IGNORE_RETVAL(CoCreateGuid(&CreateDisk.Gpt.DiskId));
|
|
|
|
CreateDisk.Gpt.MaxPartitionCount = MAX_GPT_PARTITIONS;
|
|
|
|
|
|
|
|
DriveLayoutEx.PartitionStyle = PARTITION_STYLE_GPT;
|
2015-01-19 01:23:32 +00:00
|
|
|
DriveLayoutEx.PartitionCount = pn;
|
2014-10-28 19:16:35 +00:00
|
|
|
// At the very least, a GPT disk has 34 reserved sectors at the beginning and 33 at the end.
|
2016-05-23 11:19:11 +00:00
|
|
|
DriveLayoutEx.Type.Gpt.StartingUsableOffset.QuadPart = 34 * SelectedDrive.SectorSize;
|
|
|
|
DriveLayoutEx.Type.Gpt.UsableLength.QuadPart = SelectedDrive.DiskSize - (34+33) * SelectedDrive.SectorSize;
|
2013-01-22 02:40:43 +00:00
|
|
|
DriveLayoutEx.Type.Gpt.MaxPartitionCount = MAX_GPT_PARTITIONS;
|
|
|
|
DriveLayoutEx.Type.Gpt.DiskId = CreateDisk.Gpt.DiskId;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you don't call IOCTL_DISK_CREATE_DISK, the next call will fail
|
|
|
|
size = sizeof(CreateDisk);
|
2015-01-19 01:23:32 +00:00
|
|
|
r = DeviceIoControl(hDrive, IOCTL_DISK_CREATE_DISK, (BYTE*)&CreateDisk, size, NULL, 0, &size, NULL );
|
2013-01-22 02:40:43 +00:00
|
|
|
if (!r) {
|
2013-04-07 23:10:58 +00:00
|
|
|
uprintf("Could not reset disk: %s\n", WindowsErrorString());
|
2013-01-22 02:40:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-01-19 01:23:32 +00:00
|
|
|
size = sizeof(DriveLayoutEx) - ((partition_style == PARTITION_STYLE_GPT)?((4-pn)*sizeof(PARTITION_INFORMATION_EX)):0);
|
|
|
|
r = DeviceIoControl(hDrive, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, (BYTE*)&DriveLayoutEx, size, NULL, 0, &size, NULL );
|
2013-01-22 02:40:43 +00:00
|
|
|
if (!r) {
|
2013-04-07 23:10:58 +00:00
|
|
|
uprintf("Could not set drive layout: %s\n", WindowsErrorString());
|
2013-01-22 02:40:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-08-07 00:45:46 +00:00
|
|
|
|
2015-01-20 21:50:24 +00:00
|
|
|
if (!RefreshDriveLayout(hDrive))
|
2013-06-25 01:55:25 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-07 00:45:46 +00:00
|
|
|
BOOL RefreshDriveLayout(HANDLE hDrive)
|
|
|
|
{
|
|
|
|
BOOL r;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
// Diskpart does call the following IOCTL this after updating the partition table, so we do too
|
|
|
|
r = DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &size, NULL );
|
|
|
|
if (!r)
|
|
|
|
uprintf("Could not refresh drive layout: %s\n", WindowsErrorString());
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-05-21 15:34:34 +00:00
|
|
|
/* Initialize disk for partitioning */
|
|
|
|
BOOL InitializeDisk(HANDLE hDrive)
|
2013-07-05 21:20:46 +00:00
|
|
|
{
|
|
|
|
BOOL r;
|
|
|
|
DWORD size;
|
|
|
|
CREATE_DISK CreateDisk = {PARTITION_STYLE_RAW, {{0}}};
|
|
|
|
|
2015-01-01 23:39:28 +00:00
|
|
|
PrintInfoDebug(0, MSG_239);
|
2013-07-05 21:20:46 +00:00
|
|
|
|
|
|
|
size = sizeof(CreateDisk);
|
|
|
|
r = DeviceIoControl(hDrive, IOCTL_DISK_CREATE_DISK,
|
|
|
|
(BYTE*)&CreateDisk, size, NULL, 0, &size, NULL );
|
|
|
|
if (!r) {
|
|
|
|
uprintf("Could not delete drive layout: %s\n", WindowsErrorString());
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = DeviceIoControl(hDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &size, NULL );
|
|
|
|
if (!r) {
|
|
|
|
uprintf("Could not refresh drive layout: %s\n", WindowsErrorString());
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
return FALSE;
|
2017-02-14 12:44:25 +00:00
|
|
|
}
|
2013-07-05 21:20:46 +00:00
|
|
|
|
2017-02-14 12:44:25 +00:00
|
|
|
return TRUE;
|
2013-07-05 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 02:40:43 +00:00
|
|
|
/*
|
|
|
|
* Convert a partition type to its human readable form using
|
|
|
|
* (slightly modified) entries from GNU fdisk
|
|
|
|
*/
|
|
|
|
const char* GetPartitionType(BYTE Type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<ARRAYSIZE(msdos_systypes); i++) {
|
|
|
|
if (msdos_systypes[i].type == Type)
|
|
|
|
return msdos_systypes[i].name;
|
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|