2011-11-13 20:53:23 +00:00
|
|
|
/*
|
2011-11-21 17:06:17 +00:00
|
|
|
* Rufus: The Resourceful USB Formatting Utility
|
2011-11-13 20:53:23 +00:00
|
|
|
* Copyright (c) 2011 Pete Batard <pete@akeo.ie>
|
2011-11-18 01:58:08 +00:00
|
|
|
*
|
|
|
|
* Device enumeration based in part on TestUSBDriveEject.cpp by ahmd:
|
|
|
|
* http://www.codeguru.com/forum/showpost.php?p=1951973&postcount=7
|
2011-11-13 20:53:23 +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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-21 20:12:23 +00:00
|
|
|
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
|
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
2011-11-13 20:53:23 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <windowsx.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2011-11-20 22:49:55 +00:00
|
|
|
#include <ctype.h>
|
2011-11-24 02:24:50 +00:00
|
|
|
#include <math.h>
|
2011-11-13 20:53:23 +00:00
|
|
|
#include <commctrl.h>
|
2011-11-17 01:43:06 +00:00
|
|
|
#include <setupapi.h>
|
2011-11-20 02:34:15 +00:00
|
|
|
#include <winioctl.h>
|
2011-11-27 23:40:28 +00:00
|
|
|
#include <process.h>
|
2011-11-26 00:25:04 +00:00
|
|
|
#include <dbt.h>
|
2011-11-20 22:49:55 +00:00
|
|
|
|
2011-11-18 21:46:34 +00:00
|
|
|
// http://git.kernel.org/?p=fs/ext2/e2fsprogs.git;a=blob;f=misc/badblocks.c
|
2011-11-23 12:27:51 +00:00
|
|
|
// http://ms-sys.sourceforge.net/
|
|
|
|
// http://thestarman.pcministry.com/asm/mbr/MSWIN41.htm
|
2011-11-27 23:40:28 +00:00
|
|
|
// http://www.c-jump.com/CIS24/Slides/FAT/lecture.html#F01_0130_sector_assignments
|
2011-11-23 12:27:51 +00:00
|
|
|
|
2011-11-13 20:53:23 +00:00
|
|
|
#include "msapi_utf8.h"
|
|
|
|
#include "resource.h"
|
2011-11-19 19:08:23 +00:00
|
|
|
#include "rufus.h"
|
2011-11-25 13:29:35 +00:00
|
|
|
#include "sys_types.h"
|
2011-11-13 20:53:23 +00:00
|
|
|
|
2011-11-21 17:06:17 +00:00
|
|
|
#if !defined(GUID_DEVINTERFACE_DISK)
|
|
|
|
const GUID GUID_DEVINTERFACE_DISK = { 0x53f56307L, 0xb6bf, 0x11d0, {0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b} };
|
|
|
|
#endif
|
|
|
|
|
2011-11-27 23:40:28 +00:00
|
|
|
// For MinGW
|
|
|
|
#ifndef PBS_MARQUEE
|
|
|
|
#define PBS_MARQUEE 0x08
|
|
|
|
#endif
|
|
|
|
#ifndef PBM_SETMARQUEE
|
|
|
|
#define PBM_SETMARQUEE (WM_USER+10)
|
|
|
|
#endif
|
|
|
|
|
2011-11-13 20:53:23 +00:00
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
2011-11-21 17:06:17 +00:00
|
|
|
HINSTANCE hMainInstance;
|
|
|
|
HWND hMainDialog;
|
|
|
|
char szFolderPath[MAX_PATH];
|
|
|
|
HWND hStatus;
|
|
|
|
float fScale = 1.0f;
|
|
|
|
|
2011-11-24 02:24:50 +00:00
|
|
|
BOOL bBootable;
|
|
|
|
BOOL bQuickFormat;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
DWORD DeviceNumber;
|
|
|
|
LONGLONG DiskSize;
|
|
|
|
LONGLONG PartitionSize;
|
2011-11-25 01:30:41 +00:00
|
|
|
DISK_GEOMETRY Geometry;
|
2011-11-24 02:24:50 +00:00
|
|
|
DWORD FirstSector;
|
2011-11-25 01:30:41 +00:00
|
|
|
enum _FSType FSType;
|
2011-11-24 02:24:50 +00:00
|
|
|
} SelectedDrive;
|
|
|
|
|
2011-11-28 20:05:34 +00:00
|
|
|
static HWND hDeviceList, hCapacity, hFileSystem, hClusterSize, hLabel;
|
2011-11-24 23:49:42 +00:00
|
|
|
static HWND hDeviceTooltip = NULL, hFSTooltip = NULL;
|
2011-11-24 02:24:50 +00:00
|
|
|
static StrArray DriveID, DriveLabel;
|
2011-11-28 12:37:58 +00:00
|
|
|
static DWORD FormatStatus;
|
2011-11-17 01:43:06 +00:00
|
|
|
|
2011-11-19 19:08:23 +00:00
|
|
|
#ifdef RUFUS_DEBUG
|
2011-11-21 17:06:17 +00:00
|
|
|
void _uprintf(const char *format, ...)
|
2011-11-17 01:43:06 +00:00
|
|
|
{
|
|
|
|
char buf[4096], *p = buf;
|
|
|
|
va_list args;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
n = safe_vsnprintf(p, sizeof(buf)-3, format, args); // buf-3 is room for CR/LF/NUL
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
p += (n < 0)?sizeof(buf)-3:n;
|
|
|
|
|
|
|
|
while((p>buf) && (isspace(p[-1])))
|
|
|
|
*--p = '\0';
|
|
|
|
|
|
|
|
*p++ = '\r';
|
|
|
|
*p++ = '\n';
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
OutputDebugStringA(buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-21 17:06:17 +00:00
|
|
|
|
2011-11-23 12:27:51 +00:00
|
|
|
void StatusPrintf(const char *format, ...)
|
|
|
|
{
|
|
|
|
char buf[256], *p = buf;
|
|
|
|
va_list args;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
n = safe_vsnprintf(p, sizeof(buf)-1, format, args); // room for NUL
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
p += (n < 0)?sizeof(buf)-1:n;
|
|
|
|
|
|
|
|
while((p>buf) && (isspace(p[-1])))
|
|
|
|
*--p = '\0';
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
SetDlgItemTextU(hMainDialog, IDC_STATUS, buf);
|
|
|
|
}
|
|
|
|
|
2011-11-17 01:43:06 +00:00
|
|
|
/*
|
2011-11-25 13:29:35 +00:00
|
|
|
* Convert a partition type to its human readable form using
|
|
|
|
* (slightly modified) entries from GNU fdisk
|
2011-11-17 01:43:06 +00:00
|
|
|
*/
|
2011-11-21 17:06:17 +00:00
|
|
|
static const char* GetPartitionType(BYTE Type)
|
2011-11-17 01:43:06 +00:00
|
|
|
{
|
2011-11-25 13:29:35 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i<ARRAYSIZE(msdos_systypes); i++) {
|
|
|
|
if (msdos_systypes[i].type == Type)
|
|
|
|
return msdos_systypes[i].name;
|
2011-11-17 01:43:06 +00:00
|
|
|
}
|
2011-11-25 13:29:35 +00:00
|
|
|
return "Unknown";
|
2011-11-17 01:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-11-24 20:02:14 +00:00
|
|
|
* Open a drive with optional write access - return a drive HANDLE and the drive letter
|
2011-11-23 12:27:51 +00:00
|
|
|
* 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 fail the call
|
|
|
|
* - We report the full path of any drive that was successfully opened for write acces
|
2011-11-17 01:43:06 +00:00
|
|
|
*/
|
2011-11-23 12:27:51 +00:00
|
|
|
static BOOL GetDriveHandle(DWORD DriveIndex, HANDLE* hDrive, char* DriveLetter, BOOL bWriteAccess)
|
2011-11-17 01:43:06 +00:00
|
|
|
{
|
2011-11-18 01:58:08 +00:00
|
|
|
BOOL r;
|
2011-11-17 01:43:06 +00:00
|
|
|
DWORD size;
|
2011-11-20 22:49:55 +00:00
|
|
|
STORAGE_DEVICE_NUMBER_REDEF device_number = {0};
|
2011-11-23 12:27:51 +00:00
|
|
|
char drives[26*4]; /* "D:\", "E:\", etc. */
|
2011-11-17 01:43:06 +00:00
|
|
|
char *drive = drives;
|
|
|
|
char drive_name[] = "\\\\.\\#:";
|
|
|
|
|
2011-11-23 12:27:51 +00:00
|
|
|
if ((DriveIndex < DRIVE_INDEX_MIN) || (DriveIndex > DRIVE_INDEX_MAX)) {
|
|
|
|
uprintf("WARNING: Bad index value. Please check your code!\n");
|
|
|
|
}
|
|
|
|
DriveIndex -= DRIVE_INDEX_MIN;
|
|
|
|
|
2011-11-17 01:43:06 +00:00
|
|
|
size = GetLogicalDriveStringsA(sizeof(drives), drives);
|
|
|
|
if (size == 0) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("GetLogicalDriveStrings failed: %s\n", WindowsErrorString());
|
2011-11-18 21:46:34 +00:00
|
|
|
return FALSE;
|
2011-11-17 01:43:06 +00:00
|
|
|
}
|
|
|
|
if (size > sizeof(drives)) {
|
|
|
|
uprintf("GetLogicalDriveStrings: buffer too small (required %d vs %d)\n", size, sizeof(drives));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-11-18 21:46:34 +00:00
|
|
|
*hDrive = INVALID_HANDLE_VALUE;
|
2011-11-17 01:43:06 +00:00
|
|
|
for ( ;*drive; drive += safe_strlen(drive)+1) {
|
2011-11-27 23:40:28 +00:00
|
|
|
if (!isalpha(*drive))
|
|
|
|
continue;
|
|
|
|
*drive = (char)toupper((int)*drive);
|
2011-11-17 01:43:06 +00:00
|
|
|
if (*drive < 'C') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
safe_sprintf(drive_name, sizeof(drive_name), "\\\\.\\%c:", drive[0]);
|
2011-11-23 12:27:51 +00:00
|
|
|
*hDrive = CreateFileA(drive_name, GENERIC_READ|(bWriteAccess?GENERIC_WRITE:0),
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
|
2011-11-17 01:43:06 +00:00
|
|
|
if (hDrive == INVALID_HANDLE_VALUE) {
|
2011-11-23 12:27:51 +00:00
|
|
|
uprintf("Could not open drive %c: %s\n", drive[0], WindowsErrorString());
|
2011-11-17 01:43:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-11-23 12:27:51 +00:00
|
|
|
|
2011-11-18 21:46:34 +00:00
|
|
|
r = DeviceIoControl(*hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL,
|
2011-11-20 22:49:55 +00:00
|
|
|
0, &device_number, sizeof(device_number), &size, NULL);
|
2011-11-17 01:43:06 +00:00
|
|
|
if ((!r) || (size <= 0)) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("IOCTL_STORAGE_GET_DEVICE_NUMBER (GetDriveHandle) failed: %s\n", WindowsErrorString());
|
2011-11-18 21:46:34 +00:00
|
|
|
safe_closehandle(*hDrive);
|
|
|
|
break;
|
2011-11-17 01:43:06 +00:00
|
|
|
}
|
2011-11-23 12:27:51 +00:00
|
|
|
if (device_number.DeviceNumber == DriveIndex) {
|
|
|
|
if (bWriteAccess) {
|
|
|
|
if (!DeviceIoControl(*hDrive, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &size, NULL)) {
|
|
|
|
uprintf("Could not get exclusive access to %s: %s\n", drive_name, WindowsErrorString());
|
|
|
|
safe_closehandle(*hDrive);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-11-28 12:37:58 +00:00
|
|
|
uprintf("Caution: Opened %s drive for write access\n", drive_name);
|
2011-11-23 12:27:51 +00:00
|
|
|
}
|
2011-11-17 01:43:06 +00:00
|
|
|
break;
|
2011-11-23 12:27:51 +00:00
|
|
|
}
|
|
|
|
safe_closehandle(*hDrive);
|
2011-11-17 01:43:06 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 21:46:34 +00:00
|
|
|
if (DriveLetter != NULL) {
|
2011-11-28 12:37:58 +00:00
|
|
|
*DriveLetter = *drive?*drive:' ';
|
2011-11-18 21:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (*hDrive != INVALID_HANDLE_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-11-21 17:06:17 +00:00
|
|
|
* Return the drive letter and volume label
|
2011-11-18 21:46:34 +00:00
|
|
|
*/
|
2011-11-24 02:24:50 +00:00
|
|
|
static BOOL GetDriveLabel(DWORD DriveIndex, char* letter, char** label)
|
2011-11-18 21:46:34 +00:00
|
|
|
{
|
|
|
|
HANDLE hDrive;
|
|
|
|
char DrivePath[] = "#:\\";
|
2011-11-24 02:24:50 +00:00
|
|
|
static char volume_label[MAX_PATH+1];
|
2011-11-18 21:46:34 +00:00
|
|
|
|
2011-11-24 02:24:50 +00:00
|
|
|
*label = STR_NO_LABEL;
|
2011-11-18 21:46:34 +00:00
|
|
|
|
2011-11-24 02:24:50 +00:00
|
|
|
if (!GetDriveHandle(DriveIndex, &hDrive, DrivePath, FALSE))
|
2011-11-18 21:46:34 +00:00
|
|
|
return FALSE;
|
|
|
|
safe_closehandle(hDrive);
|
2011-11-21 17:06:17 +00:00
|
|
|
*letter = DrivePath[0];
|
2011-11-18 21:46:34 +00:00
|
|
|
|
2011-11-23 12:39:47 +00:00
|
|
|
if (GetVolumeInformationA(DrivePath, volume_label, sizeof(volume_label), NULL, NULL, NULL, NULL, 0) && *volume_label) {
|
2011-11-21 17:06:17 +00:00
|
|
|
*label = volume_label;
|
2011-11-18 21:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-28 20:05:34 +00:00
|
|
|
static void SetClusterSizes(enum _FSType FSType)
|
|
|
|
{
|
|
|
|
IGNORE_RETVAL(ComboBox_ResetContent(hClusterSize));
|
|
|
|
// Don't ask me - just following the MS standard here
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "Default allocation size"), 0x8000));
|
|
|
|
// TODO set value above according to FS selected and default cluster sizes from
|
|
|
|
// http://support.microsoft.com/kb/140365
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "512 bytes"), 0x200));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "1024 bytes"), 0x400));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "2048 bytes"), 0x800));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "4096 bytes"), 0x1000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "8192 bytes"), 0x2000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "16 kilobytes"), 0x4000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "32 kilobytes"), 0x8000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "64 kilobytes"), 0x10000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "128 kilobytes"), 0x20000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "256 kilobytes"), 0x40000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "512 kilobytes"), 0x80000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "1024 kilobytes"), 0x100000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "2048 kilobytes"), 0x200000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "4096 kilobytes"), 0x400000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "8192 kilobytes"), 0x800000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "16 megabytes"), 0x1000000));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, "32 megabytes"), 0x2000000));
|
|
|
|
// TODO set value according to disk props
|
|
|
|
IGNORE_RETVAL(ComboBox_SetCurSel(hClusterSize, 0));
|
|
|
|
}
|
|
|
|
|
2011-11-18 21:46:34 +00:00
|
|
|
/*
|
2011-11-24 02:24:50 +00:00
|
|
|
* Fill the drive properties (size, FS, etc)
|
2011-11-18 21:46:34 +00:00
|
|
|
*/
|
2011-11-24 02:24:50 +00:00
|
|
|
static BOOL GetDriveInfo(void)
|
2011-11-18 21:46:34 +00:00
|
|
|
{
|
|
|
|
BOOL r;
|
|
|
|
HANDLE hDrive;
|
|
|
|
DWORD size;
|
2011-11-21 17:06:17 +00:00
|
|
|
BYTE geometry[128], layout[1024];
|
2011-11-20 03:29:08 +00:00
|
|
|
void* disk_geometry = (void*)geometry;
|
2011-11-21 17:06:17 +00:00
|
|
|
void* drive_layout = (void*)layout;
|
|
|
|
PDISK_GEOMETRY_EX DiskGeometry = (PDISK_GEOMETRY_EX)disk_geometry;
|
|
|
|
PDRIVE_LAYOUT_INFORMATION_EX DriveLayout = (PDRIVE_LAYOUT_INFORMATION_EX)drive_layout;
|
2011-11-25 01:30:41 +00:00
|
|
|
char DrivePath[] = "#:\\", tmp[128], fs_type[32];
|
2011-11-21 17:06:17 +00:00
|
|
|
DWORD i, nb_partitions = 0;
|
2011-11-18 21:46:34 +00:00
|
|
|
|
2011-11-24 02:24:50 +00:00
|
|
|
SelectedDrive.DiskSize = 0;
|
2011-11-18 21:46:34 +00:00
|
|
|
|
2011-11-24 02:24:50 +00:00
|
|
|
if (!GetDriveHandle(SelectedDrive.DeviceNumber, &hDrive, DrivePath, FALSE))
|
2011-11-18 21:46:34 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
r = DeviceIoControl(hDrive, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
NULL, 0, geometry, sizeof(geometry), &size, NULL );
|
|
|
|
if (!r || size <= 0) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("IOCTL_DISK_GET_DRIVE_GEOMETRY_EX failed: %s\n", WindowsErrorString());
|
2011-11-18 21:46:34 +00:00
|
|
|
safe_closehandle(hDrive);
|
|
|
|
return FALSE;
|
2011-11-17 01:43:06 +00:00
|
|
|
}
|
2011-11-24 02:24:50 +00:00
|
|
|
SelectedDrive.DiskSize = DiskGeometry->DiskSize.QuadPart;
|
2011-11-25 01:30:41 +00:00
|
|
|
memcpy(&SelectedDrive.Geometry, &DiskGeometry->Geometry, sizeof(DISK_GEOMETRY));
|
2011-11-23 12:27:51 +00:00
|
|
|
uprintf("Cylinders: %lld, TracksPerCylinder: %d, SectorsPerTrack: %d, BytesPerSector: %d\n",
|
|
|
|
DiskGeometry->Geometry.Cylinders, DiskGeometry->Geometry.TracksPerCylinder,
|
|
|
|
DiskGeometry->Geometry.SectorsPerTrack, DiskGeometry->Geometry.BytesPerSector);
|
2011-11-21 17:06:17 +00:00
|
|
|
|
|
|
|
r = DeviceIoControl(hDrive, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
|
|
|
|
NULL, 0, layout, sizeof(layout), &size, NULL );
|
|
|
|
if (!r || size <= 0) {
|
|
|
|
uprintf("IOCTL_DISK_GET_DRIVE_LAYOUT_EX failed: %s\n", WindowsErrorString());
|
|
|
|
} else {
|
2011-11-24 23:49:42 +00:00
|
|
|
DestroyTooltip(hFSTooltip);
|
|
|
|
hFSTooltip = NULL;
|
2011-11-21 17:06:17 +00:00
|
|
|
switch (DriveLayout->PartitionStyle) {
|
|
|
|
case PARTITION_STYLE_MBR:
|
|
|
|
for (i=0; i<DriveLayout->PartitionCount; i++) {
|
|
|
|
if (DriveLayout->PartitionEntry[i].Mbr.PartitionType != PARTITION_ENTRY_UNUSED) {
|
|
|
|
uprintf("Partition #%d:\n", ++nb_partitions);
|
2011-11-24 23:49:42 +00:00
|
|
|
if (hFSTooltip == NULL) {
|
2011-11-28 12:37:58 +00:00
|
|
|
// TODO: provide all partitions FS on tooltip, not just the one
|
2011-11-26 00:25:04 +00:00
|
|
|
safe_sprintf(tmp, sizeof(tmp), "Current file system: %s (0x%02x)",
|
2011-11-24 23:49:42 +00:00
|
|
|
GetPartitionType(DriveLayout->PartitionEntry[i].Mbr.PartitionType),
|
|
|
|
DriveLayout->PartitionEntry[i].Mbr.PartitionType);
|
|
|
|
hFSTooltip = CreateTooltip(hFileSystem, tmp, -1);
|
|
|
|
}
|
2011-11-26 00:25:04 +00:00
|
|
|
uprintf(" Type: %s (0x%02x)\n Boot: %s\n Recognized: %s\n Hidden Sectors: %d\n",
|
2011-11-21 17:06:17 +00:00
|
|
|
GetPartitionType(DriveLayout->PartitionEntry[i].Mbr.PartitionType),
|
|
|
|
DriveLayout->PartitionEntry[i].Mbr.PartitionType,
|
|
|
|
DriveLayout->PartitionEntry[i].Mbr.BootIndicator?"Yes":"No",
|
|
|
|
DriveLayout->PartitionEntry[i].Mbr.RecognizedPartition?"Yes":"No",
|
|
|
|
DriveLayout->PartitionEntry[i].Mbr.HiddenSectors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uprintf("Partition type: MBR, NB Partitions: %d\n", nb_partitions);
|
|
|
|
break;
|
|
|
|
case PARTITION_STYLE_GPT:
|
|
|
|
uprintf("Partition type: GPT, NB Partitions: %d\n", DriveLayout->PartitionCount);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
uprintf("Partition type: RAW\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-11-18 21:46:34 +00:00
|
|
|
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
|
2011-11-25 01:30:41 +00:00
|
|
|
SelectedDrive.FSType = FS_DEFAULT;
|
|
|
|
|
2011-11-24 23:49:42 +00:00
|
|
|
if (GetVolumeInformationA(DrivePath, NULL, 0, NULL, NULL, NULL,
|
2011-11-25 01:30:41 +00:00
|
|
|
fs_type, sizeof(fs_type))) {
|
2011-11-24 23:49:42 +00:00
|
|
|
// re-select existing FS if it's one we know
|
2011-11-25 01:30:41 +00:00
|
|
|
for (SelectedDrive.FSType=FS_FAT16; SelectedDrive.FSType<FS_MAX; SelectedDrive.FSType++) {
|
2011-11-24 23:49:42 +00:00
|
|
|
tmp[0] = 0;
|
2011-11-25 01:30:41 +00:00
|
|
|
IGNORE_RETVAL(ComboBox_GetLBTextU(hFileSystem, SelectedDrive.FSType, tmp));
|
|
|
|
if (safe_strcmp(fs_type, tmp) == 0) {
|
|
|
|
IGNORE_RETVAL(ComboBox_SetCurSel(hFileSystem, SelectedDrive.FSType));
|
2011-11-24 23:49:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-11-25 01:30:41 +00:00
|
|
|
if (SelectedDrive.FSType == FS_MAX)
|
|
|
|
SelectedDrive.FSType = FS_DEFAULT;
|
2011-11-19 01:30:20 +00:00
|
|
|
}
|
2011-11-25 01:30:41 +00:00
|
|
|
IGNORE_RETVAL(ComboBox_SetCurSel(hFileSystem, SelectedDrive.FSType));
|
2011-11-28 20:05:34 +00:00
|
|
|
SetClusterSizes(SelectedDrive.FSType);
|
2011-11-19 01:30:20 +00:00
|
|
|
|
2011-11-18 21:46:34 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-21 17:06:17 +00:00
|
|
|
/*
|
|
|
|
* Populate the UI properties
|
|
|
|
*/
|
2011-11-24 02:24:50 +00:00
|
|
|
static BOOL PopulateProperties(int ComboIndex)
|
2011-11-18 21:46:34 +00:00
|
|
|
{
|
|
|
|
double HumanReadableSize;
|
2011-11-24 02:24:50 +00:00
|
|
|
char capacity[64];
|
|
|
|
char *suffix[] = { "KB", "MB", "GB", "TB", "PB"};
|
|
|
|
char proposed_label[16], no_label[] = STR_NO_LABEL;
|
2011-11-18 21:46:34 +00:00
|
|
|
int i;
|
|
|
|
|
2011-11-18 23:41:28 +00:00
|
|
|
IGNORE_RETVAL(ComboBox_ResetContent(hCapacity));
|
2011-11-19 01:30:20 +00:00
|
|
|
IGNORE_RETVAL(ComboBox_ResetContent(hFileSystem));
|
2011-11-28 20:05:34 +00:00
|
|
|
IGNORE_RETVAL(ComboBox_ResetContent(hClusterSize));
|
2011-11-24 02:24:50 +00:00
|
|
|
SetWindowTextA(hLabel, "");
|
2011-11-24 23:49:42 +00:00
|
|
|
DestroyTooltip(hDeviceTooltip);
|
2011-11-25 13:29:35 +00:00
|
|
|
DestroyTooltip(hFSTooltip);
|
2011-11-24 23:49:42 +00:00
|
|
|
hDeviceTooltip = NULL;
|
2011-11-25 13:29:35 +00:00
|
|
|
hFSTooltip = NULL;
|
2011-11-24 02:24:50 +00:00
|
|
|
memset(&SelectedDrive, 0, sizeof(SelectedDrive));
|
|
|
|
|
|
|
|
if (ComboIndex < 0) {
|
2011-11-18 23:41:28 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-24 23:49:42 +00:00
|
|
|
// Populate the FileSystem values
|
|
|
|
IGNORE_RETVAL(ComboBox_AddStringU(hFileSystem, "FAT"));
|
|
|
|
IGNORE_RETVAL(ComboBox_AddStringU(hFileSystem, "FAT32"));
|
|
|
|
IGNORE_RETVAL(ComboBox_AddStringU(hFileSystem, "NTFS"));
|
|
|
|
|
2011-11-24 02:24:50 +00:00
|
|
|
SelectedDrive.DeviceNumber = (DWORD)ComboBox_GetItemData(hDeviceList, ComboIndex);
|
|
|
|
if (!GetDriveInfo())
|
2011-11-18 21:46:34 +00:00
|
|
|
return FALSE;
|
2011-11-17 01:43:06 +00:00
|
|
|
|
2011-11-24 02:24:50 +00:00
|
|
|
HumanReadableSize = (double)SelectedDrive.DiskSize;
|
2011-11-18 21:46:34 +00:00
|
|
|
for (i=0; i<ARRAYSIZE(suffix); i++) {
|
|
|
|
HumanReadableSize /= 1024.0;
|
|
|
|
if (HumanReadableSize < 512.0) {
|
|
|
|
safe_sprintf(capacity, sizeof(capacity), "%0.2f %s", HumanReadableSize, suffix[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IGNORE_RETVAL(ComboBox_AddStringU(hCapacity, capacity));
|
|
|
|
IGNORE_RETVAL(ComboBox_SetCurSel(hCapacity, 0));
|
2011-11-24 23:49:42 +00:00
|
|
|
hDeviceTooltip = CreateTooltip(hDeviceList, DriveID.Table[ComboIndex], -1);
|
2011-11-24 20:02:14 +00:00
|
|
|
|
2011-11-24 02:24:50 +00:00
|
|
|
// If no existing label is available, propose one according to the size (eg: "256MB", "8GB")
|
|
|
|
if (safe_strcmp(no_label, DriveLabel.Table[ComboIndex]) == 0) {
|
2011-11-24 20:02:14 +00:00
|
|
|
if (HumanReadableSize < 1.0) {
|
|
|
|
HumanReadableSize *= 1024.0;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
// If we're beneath the tolerance, round proposed label to an integer, if not, show one decimal point
|
|
|
|
if (fabs(HumanReadableSize / ceil(HumanReadableSize) - 1.0) < PROPOSEDLABEL_TOLERANCE) {
|
|
|
|
safe_sprintf(proposed_label, sizeof(proposed_label), "%0.0f%s", ceil(HumanReadableSize), suffix[i]);
|
|
|
|
} else {
|
|
|
|
safe_sprintf(proposed_label, sizeof(proposed_label), "%0.1f%s", HumanReadableSize, suffix[i]);
|
2011-11-24 02:24:50 +00:00
|
|
|
}
|
2011-11-24 20:02:14 +00:00
|
|
|
SetWindowTextA(hLabel, proposed_label);
|
2011-11-24 02:24:50 +00:00
|
|
|
} else {
|
|
|
|
SetWindowTextA(hLabel, DriveLabel.Table[ComboIndex]);
|
|
|
|
}
|
|
|
|
|
2011-11-18 21:46:34 +00:00
|
|
|
return TRUE;
|
2011-11-17 01:43:06 +00:00
|
|
|
}
|
2011-11-13 20:53:23 +00:00
|
|
|
|
2011-11-28 01:32:18 +00:00
|
|
|
static BOOL WriteSectors(HANDLE hDrive, size_t SectorSize, size_t StartSector, size_t nSectors, void* Buf, size_t BufSize)
|
2011-11-23 12:27:51 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER ptr;
|
|
|
|
DWORD Size;
|
|
|
|
|
|
|
|
if (SectorSize * nSectors > BufSize) {
|
|
|
|
uprintf("WriteSectors: Buffer is too small\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr.QuadPart = StartSector*SectorSize;
|
|
|
|
if (!SetFilePointerEx(hDrive, ptr, NULL, FILE_BEGIN)) {
|
|
|
|
uprintf("WriteSectors: Could not access sector %lld - %s\n", StartSector, WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-11-24 20:23:01 +00:00
|
|
|
if ((!WriteFile(hDrive, Buf, (DWORD)BufSize, &Size, NULL)) || (Size != BufSize)) {
|
2011-11-23 12:27:51 +00:00
|
|
|
uprintf("WriteSectors: Write error - %s\n", WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-28 01:32:18 +00:00
|
|
|
static BOOL ReadSectors(HANDLE hDrive, size_t SectorSize, size_t StartSector, size_t nSectors, void* Buf, size_t BufSize)
|
2011-11-23 12:27:51 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER ptr;
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
if (SectorSize * nSectors > BufSize) {
|
|
|
|
uprintf("ReadSectors: Buffer is too small\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr.QuadPart = StartSector*SectorSize;
|
|
|
|
if (!SetFilePointerEx(hDrive, ptr, NULL, FILE_BEGIN)) {
|
|
|
|
uprintf("ReadSectors: Could not access sector %lld - %s\n", StartSector, WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-11-24 20:23:01 +00:00
|
|
|
if ((!ReadFile(hDrive, Buf, (DWORD)BufSize, &size, NULL)) || (size != BufSize)) {
|
2011-11-23 12:27:51 +00:00
|
|
|
uprintf("ReadSectors: Write error - %s\n", WindowsErrorString());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a partition table
|
|
|
|
*/
|
2011-11-28 01:32:18 +00:00
|
|
|
static BOOL CreatePartition(HANDLE hDrive)
|
2011-11-23 12:27:51 +00:00
|
|
|
{
|
|
|
|
BYTE layout[sizeof(DRIVE_LAYOUT_INFORMATION_EX) + 3*sizeof(PARTITION_INFORMATION_EX)] = {0};
|
|
|
|
PDRIVE_LAYOUT_INFORMATION_EX DriveLayoutEx = (PDRIVE_LAYOUT_INFORMATION_EX)layout;
|
|
|
|
BOOL r;
|
|
|
|
DWORD size;
|
|
|
|
|
2011-11-26 00:25:04 +00:00
|
|
|
StatusPrintf("Partitioning...");
|
2011-11-23 12:27:51 +00:00
|
|
|
DriveLayoutEx->PartitionStyle = PARTITION_STYLE_MBR;
|
|
|
|
DriveLayoutEx->PartitionCount = 4; // Must be multiple of 4 for MBR
|
|
|
|
DriveLayoutEx->Mbr.Signature = GetTickCount();
|
|
|
|
DriveLayoutEx->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;
|
2011-11-25 01:30:41 +00:00
|
|
|
DriveLayoutEx->PartitionEntry[0].StartingOffset.QuadPart =
|
|
|
|
SelectedDrive.Geometry.BytesPerSector * SelectedDrive.Geometry.SectorsPerTrack;
|
|
|
|
DriveLayoutEx->PartitionEntry[0].PartitionLength.QuadPart = SelectedDrive.DiskSize -
|
|
|
|
DriveLayoutEx->PartitionEntry[0].StartingOffset.QuadPart;
|
2011-11-23 12:27:51 +00:00
|
|
|
DriveLayoutEx->PartitionEntry[0].PartitionNumber = 1;
|
|
|
|
DriveLayoutEx->PartitionEntry[0].RewritePartition = TRUE;
|
2011-11-25 01:30:41 +00:00
|
|
|
DriveLayoutEx->PartitionEntry[0].Mbr.HiddenSectors = SelectedDrive.Geometry.SectorsPerTrack;
|
|
|
|
switch (ComboBox_GetCurSel(hFileSystem)) {
|
|
|
|
case FS_FAT16:
|
|
|
|
DriveLayoutEx->PartitionEntry[0].Mbr.PartitionType = 0x0e;
|
|
|
|
break;
|
|
|
|
case FS_NTFS:
|
|
|
|
DriveLayoutEx->PartitionEntry[0].Mbr.PartitionType = 0x07;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DriveLayoutEx->PartitionEntry[0].Mbr.PartitionType = 0x0c;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-24 20:26:20 +00:00
|
|
|
// For the remaining partitions, PartitionStyle & PartitionType have already
|
|
|
|
// been zeroed => set to MBR/unused
|
2011-11-23 12:27:51 +00:00
|
|
|
|
|
|
|
r = DeviceIoControl(hDrive, IOCTL_DISK_SET_DRIVE_LAYOUT_EX,
|
|
|
|
layout, sizeof(layout), NULL, 0, &size, NULL );
|
|
|
|
if (!r) {
|
|
|
|
uprintf("IOCTL_DISK_SET_DRIVE_LAYOUT_EX failed: %s\n", WindowsErrorString());
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-26 00:25:04 +00:00
|
|
|
/*
|
|
|
|
* FormatEx callback. Return FALSE to halt operations
|
|
|
|
*/
|
2011-11-28 01:32:18 +00:00
|
|
|
static BOOLEAN __stdcall FormatExCallback(FILE_SYSTEM_CALLBACK_COMMAND Command, DWORD Action, PVOID Data)
|
2011-11-26 00:25:04 +00:00
|
|
|
{
|
|
|
|
DWORD* percent;
|
|
|
|
int task_number = 0;
|
|
|
|
|
2011-11-28 12:37:58 +00:00
|
|
|
if (IS_ERROR(FormatStatus))
|
|
|
|
return FALSE;
|
2011-11-28 01:32:18 +00:00
|
|
|
|
2011-11-26 00:25:04 +00:00
|
|
|
switch(Command) {
|
|
|
|
case FCC_PROGRESS:
|
|
|
|
percent = (DWORD*)Data;
|
2011-11-27 23:40:28 +00:00
|
|
|
PostMessage(hMainDialog, UM_FORMAT_PROGRESS, (WPARAM)*percent, (LPARAM)0);
|
2011-11-26 00:25:04 +00:00
|
|
|
uprintf("%d percent completed.\n", *percent);
|
|
|
|
break;
|
|
|
|
case FCC_STRUCTURE_PROGRESS: // No progress on quick format
|
2011-11-28 12:37:58 +00:00
|
|
|
uprintf("Format task %d/? completed.\n", ++task_number);
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
case FCC_DONE:
|
|
|
|
if(*(BOOLEAN*)Data == FALSE) {
|
|
|
|
uprintf("Error while formatting.\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_GEN_FAILURE;
|
2011-11-26 00:25:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FCC_INCOMPATIBLE_FILE_SYSTEM:
|
|
|
|
uprintf("Incompatible File System\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INCOMPATIBLE_FS;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
case FCC_ACCESS_DENIED:
|
|
|
|
uprintf("Access denied\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_ACCESS_DENIED;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
case FCC_MEDIA_WRITE_PROTECTED:
|
|
|
|
uprintf("Media is write protected\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_PROTECT;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
case FCC_VOLUME_IN_USE:
|
|
|
|
uprintf("Volume is in use\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_DEVICE_IN_USE;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
case FCC_CANT_QUICK_FORMAT:
|
|
|
|
uprintf("Cannot quick format this volume\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_CANT_QUICK_FORMAT;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
case FCC_BAD_LABEL:
|
|
|
|
uprintf("Bad label\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INVALID_LABEL;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
case FCC_OUTPUT:
|
|
|
|
uprintf("%s\n", ((PTEXTOUTPUT)Data)->Output);
|
|
|
|
break;
|
|
|
|
case FCC_CLUSTER_SIZE_TOO_BIG:
|
2011-11-28 12:37:58 +00:00
|
|
|
case FCC_CLUSTER_SIZE_TOO_SMALL:
|
2011-11-28 20:05:34 +00:00
|
|
|
uprintf("Unsupported cluster size\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INVALID_CLUSTER_SIZE;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
case FCC_VOLUME_TOO_BIG:
|
2011-11-28 12:37:58 +00:00
|
|
|
case FCC_VOLUME_TOO_SMALL:
|
|
|
|
uprintf("Volume is too %s\n", FCC_VOLUME_TOO_BIG?"big":"small");
|
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_INVALID_VOLUME_SIZE;
|
2011-11-26 00:25:04 +00:00
|
|
|
case FCC_NO_MEDIA_IN_DRIVE:
|
2011-11-28 12:37:58 +00:00
|
|
|
uprintf("No media in drive\n");
|
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NO_MEDIA_IN_DRIVE;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
uprintf("FormatExCallback: received unhandled command %X\n", Command);
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_SUPPORTED;
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-11-28 12:37:58 +00:00
|
|
|
return (!IS_ERROR(FormatStatus));
|
2011-11-26 00:25:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 23:40:28 +00:00
|
|
|
/*
|
|
|
|
* Call on fmifs.dll's FormatEx() to format the drive
|
|
|
|
*/
|
2011-11-28 01:32:18 +00:00
|
|
|
static BOOL FormatDrive(char DriveLetter)
|
2011-11-26 00:25:04 +00:00
|
|
|
{
|
|
|
|
BOOL r = FALSE;
|
|
|
|
PF_DECL(FormatEx);
|
|
|
|
WCHAR wDriveRoot[] = L"?:\\";
|
|
|
|
WCHAR wFSType[32];
|
|
|
|
WCHAR wLabel[128];
|
|
|
|
|
|
|
|
wDriveRoot[0] = (WCHAR)DriveLetter;
|
|
|
|
StatusPrintf("Formatting...");
|
|
|
|
PF_INIT_OR_OUT(FormatEx, fmifs);
|
|
|
|
|
|
|
|
// TODO: properly set MediaType
|
2011-11-26 00:32:06 +00:00
|
|
|
GetWindowTextW(hFileSystem, wFSType, ARRAYSIZE(wFSType));
|
|
|
|
GetWindowTextW(hLabel, wLabel, ARRAYSIZE(wLabel));
|
2011-11-28 20:05:34 +00:00
|
|
|
uprintf("Using cluster size: %d bytes\n", ComboBox_GetItemData(hClusterSize, ComboBox_GetCurSel(hClusterSize)));
|
2011-11-26 00:25:04 +00:00
|
|
|
pfFormatEx(wDriveRoot, RemovableMedia, wFSType, wLabel,
|
2011-11-28 20:05:34 +00:00
|
|
|
IsChecked(IDC_QUICKFORMAT), ComboBox_GetItemData(hClusterSize, ComboBox_GetCurSel(hClusterSize)), FormatExCallback);
|
2011-11-28 12:37:58 +00:00
|
|
|
if (!IS_ERROR(FormatStatus)) {
|
2011-11-26 00:25:04 +00:00
|
|
|
uprintf("Format completed.\n");
|
|
|
|
r = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-11-27 23:40:28 +00:00
|
|
|
/*
|
|
|
|
* Create a separate thread for the formatting operation
|
|
|
|
*/
|
2011-11-28 01:32:18 +00:00
|
|
|
static void __cdecl FormatThread(void* param)
|
2011-11-23 12:27:51 +00:00
|
|
|
{
|
2011-11-27 23:40:28 +00:00
|
|
|
DWORD num = (DWORD)(uintptr_t)param;
|
2011-11-23 12:27:51 +00:00
|
|
|
HANDLE hDrive;
|
2011-11-27 23:40:28 +00:00
|
|
|
BOOL r;
|
|
|
|
char drive_name[] = "?:";
|
2011-11-26 00:25:04 +00:00
|
|
|
int i;
|
2011-11-23 12:27:51 +00:00
|
|
|
|
|
|
|
if (!GetDriveHandle(num, &hDrive, NULL, TRUE)) {
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED;
|
2011-11-27 23:40:28 +00:00
|
|
|
goto out;
|
2011-11-23 12:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r = CreatePartition(hDrive);
|
2011-11-26 00:25:04 +00:00
|
|
|
safe_closehandle(hDrive);
|
2011-11-27 23:40:28 +00:00
|
|
|
if (!r) {
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_PARTITION_FAILURE;
|
2011-11-27 23:40:28 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2011-11-26 00:25:04 +00:00
|
|
|
|
|
|
|
// Make sure we can reopen the drive before trying to format it
|
|
|
|
for (i=0; i<10; i++) {
|
|
|
|
Sleep(500);
|
2011-11-27 23:40:28 +00:00
|
|
|
if (GetDriveHandle(num, &hDrive, drive_name, FALSE)) {
|
2011-11-26 00:25:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i >= 10) {
|
2011-11-27 23:40:28 +00:00
|
|
|
uprintf("Unable to reopen drive post partitioning\n");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_OPEN_FAILED;
|
2011-11-27 23:40:28 +00:00
|
|
|
goto out;
|
2011-11-26 00:25:04 +00:00
|
|
|
}
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
|
2011-11-27 23:40:28 +00:00
|
|
|
if (!FormatDrive(drive_name[0])) {
|
2011-11-28 12:37:58 +00:00
|
|
|
// Error will be set by FormatDrive() in FormatStatus
|
|
|
|
uprintf("Format error: 0x%08X\n", FormatStatus);
|
2011-11-27 23:40:28 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2011-11-23 12:27:51 +00:00
|
|
|
|
2011-11-27 23:40:28 +00:00
|
|
|
if (IsChecked(IDC_DOSSTARTUP)) {
|
|
|
|
// TODO: check return value & set bootblock
|
|
|
|
ExtractMSDOS(drive_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
PostMessage(hMainDialog, UM_FORMAT_COMPLETED, 0, 0);
|
|
|
|
_endthread();
|
2011-11-23 12:27:51 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:53:23 +00:00
|
|
|
/*
|
2011-11-21 17:06:17 +00:00
|
|
|
* Refresh the list of USB devices
|
2011-11-13 20:53:23 +00:00
|
|
|
*/
|
2011-11-17 01:43:06 +00:00
|
|
|
static BOOL GetUSBDevices(void)
|
|
|
|
{
|
|
|
|
BOOL r;
|
|
|
|
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;
|
2011-11-20 22:49:55 +00:00
|
|
|
STORAGE_DEVICE_NUMBER_REDEF device_number;
|
2011-11-17 01:43:06 +00:00
|
|
|
DWORD size, i, j, datatype;
|
|
|
|
HANDLE hDrive;
|
|
|
|
char drive_letter;
|
2011-11-18 02:00:19 +00:00
|
|
|
char *label, entry[MAX_PATH], buffer[MAX_PATH];
|
2011-11-20 02:34:15 +00:00
|
|
|
const char* usbstor_name = "USBSTOR";
|
2011-11-17 01:43:06 +00:00
|
|
|
|
|
|
|
IGNORE_RETVAL(ComboBox_ResetContent(hDeviceList));
|
2011-11-21 20:12:23 +00:00
|
|
|
StrArrayClear(&DriveID);
|
2011-11-24 02:24:50 +00:00
|
|
|
StrArrayClear(&DriveLabel);
|
2011-11-17 01:43:06 +00:00
|
|
|
|
|
|
|
dev_info = SetupDiGetClassDevsA(&GUID_DEVINTERFACE_DISK, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
|
|
|
|
if (dev_info == INVALID_HANDLE_VALUE) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("SetupDiGetClassDevs (Interface) failed: %d\n", WindowsErrorString());
|
2011-11-17 01:43:06 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_info_data.cbSize = sizeof(dev_info_data);
|
|
|
|
for (i=0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
if (!SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_ENUMERATOR_NAME,
|
|
|
|
&datatype, (LPBYTE)buffer, sizeof(buffer), &size)) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("SetupDiGetDeviceRegistryProperty (Enumerator Name) failed: %d\n", WindowsErrorString());
|
2011-11-17 01:43:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-20 02:34:15 +00:00
|
|
|
if (safe_strcmp(buffer, usbstor_name) != 0)
|
2011-11-17 01:43:06 +00:00
|
|
|
continue;
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
if (!SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_FRIENDLYNAME,
|
|
|
|
&datatype, (LPBYTE)buffer, sizeof(buffer), &size)) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("SetupDiGetDeviceRegistryProperty (Friendly Name) failed: %d\n", WindowsErrorString());
|
2011-11-17 01:43:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-11-23 12:27:51 +00:00
|
|
|
uprintf("Found drive '%s'\n", buffer);
|
2011-11-17 01:43:06 +00:00
|
|
|
|
|
|
|
devint_data.cbSize = sizeof(devint_data);
|
2011-11-18 01:58:08 +00:00
|
|
|
hDrive = INVALID_HANDLE_VALUE;
|
2011-11-17 01:43:06 +00:00
|
|
|
devint_detail_data = NULL;
|
2011-11-18 01:58:08 +00:00
|
|
|
for (j=0; ;j++) {
|
|
|
|
safe_closehandle(hDrive);
|
|
|
|
safe_free(devint_detail_data);
|
|
|
|
|
2011-11-17 01:43:06 +00:00
|
|
|
if (!SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data, &GUID_DEVINTERFACE_DISK, j, &devint_data)) {
|
|
|
|
if(GetLastError() != ERROR_NO_MORE_ITEMS) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("SetupDiEnumDeviceInterfaces failed: %s\n", WindowsErrorString());
|
2011-11-17 01:43:06 +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) {
|
|
|
|
uprintf("unable to allocate data for SP_DEVICE_INTERFACE_DETAIL_DATA\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
devint_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
|
|
|
|
} else {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("SetupDiGetDeviceInterfaceDetail (dummy) failed: %s\n", WindowsErrorString());
|
2011-11-17 01:43:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!SetupDiGetDeviceInterfaceDetailA(dev_info, &devint_data, devint_detail_data, size, &size, NULL)) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("SetupDiGetDeviceInterfaceDetail (actual) failed: %s\n", WindowsErrorString());
|
2011-11-17 01:43:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
hDrive = CreateFileA(devint_detail_data->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if(hDrive == INVALID_HANDLE_VALUE) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("could not open '%s': %s\n", devint_detail_data->DevicePath, WindowsErrorString());
|
2011-11-17 01:43:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-18 21:46:34 +00:00
|
|
|
memset(&device_number, 0, sizeof(device_number));
|
2011-11-17 01:43:06 +00:00
|
|
|
r = DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER,
|
2011-11-18 21:46:34 +00:00
|
|
|
NULL, 0, &device_number, sizeof(device_number), &size, NULL );
|
2011-11-18 01:58:08 +00:00
|
|
|
if (!r || size <= 0) {
|
2011-11-21 17:06:17 +00:00
|
|
|
uprintf("IOCTL_STORAGE_GET_DEVICE_NUMBER (GetUSBDevices) failed: %s\n", WindowsErrorString());
|
2011-11-18 01:58:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-11-17 01:43:06 +00:00
|
|
|
|
2011-11-23 12:27:51 +00:00
|
|
|
if (GetDriveLabel(device_number.DeviceNumber + DRIVE_INDEX_MIN, &drive_letter, &label)) {
|
2011-11-24 02:24:50 +00:00
|
|
|
// Must ensure that the combo box is UNSORTED for indexes to be the same
|
|
|
|
StrArrayAdd(&DriveID, buffer);
|
|
|
|
StrArrayAdd(&DriveLabel, label);
|
|
|
|
safe_sprintf(entry, sizeof(entry), "%s (%c:)", label, drive_letter);
|
2011-11-23 12:27:51 +00:00
|
|
|
IGNORE_RETVAL(ComboBox_SetItemData(hDeviceList, ComboBox_AddStringU(hDeviceList, entry),
|
|
|
|
device_number.DeviceNumber + DRIVE_INDEX_MIN));
|
2011-11-24 02:24:50 +00:00
|
|
|
safe_closehandle(hDrive);
|
|
|
|
safe_free(devint_detail_data);
|
|
|
|
break;
|
2011-11-17 01:43:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IGNORE_RETVAL(ComboBox_SetCurSel(hDeviceList, 0));
|
2011-11-21 17:06:17 +00:00
|
|
|
PostMessage(hMainDialog, WM_COMMAND, (CBN_SELCHANGE<<16) | IDC_DEVICE, 0);
|
2011-11-18 23:41:28 +00:00
|
|
|
|
2011-11-17 01:43:06 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-28 01:32:18 +00:00
|
|
|
/* Toggle controls according to operation */
|
|
|
|
static void EnableControls(BOOL bEnable)
|
|
|
|
{
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_DEVICE), bEnable);
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_CAPACITY), bEnable);
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_FILESYSTEM), bEnable);
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_ALLOCSIZE), bEnable);
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_LABEL), bEnable);
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_QUICKFORMAT), bEnable);
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_DOSSTARTUP), bEnable);
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_ABOUT), bEnable);
|
|
|
|
EnableWindow(GetDlgItem(hMainDialog, IDC_START), bEnable);
|
|
|
|
SetDlgItemTextA(hMainDialog, IDCANCEL, bEnable?"Close":"Cancel");
|
|
|
|
}
|
|
|
|
|
2011-11-17 01:43:06 +00:00
|
|
|
/*
|
2011-11-21 17:06:17 +00:00
|
|
|
* Main dialog callback
|
|
|
|
*/
|
2011-11-18 01:58:08 +00:00
|
|
|
static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
|
2011-11-13 20:53:23 +00:00
|
|
|
{
|
2011-11-21 17:06:17 +00:00
|
|
|
HDC hDC;
|
|
|
|
DRAWITEMSTRUCT* pDI;
|
2011-11-21 20:12:23 +00:00
|
|
|
int nDeviceIndex;
|
2011-11-27 23:40:28 +00:00
|
|
|
DWORD DeviceNum;
|
2011-11-24 02:24:50 +00:00
|
|
|
char str[MAX_PATH], tmp[128];
|
2011-11-27 23:40:28 +00:00
|
|
|
static uintptr_t format_thid = -1L;
|
|
|
|
static HWND hProgress;
|
|
|
|
static LONG ProgressStyle = 0;
|
2011-11-21 17:06:17 +00:00
|
|
|
|
2011-11-13 20:53:23 +00:00
|
|
|
switch (message) {
|
|
|
|
|
|
|
|
case WM_DEVICECHANGE:
|
2011-11-28 01:32:18 +00:00
|
|
|
if ( (format_thid == -1L) &&
|
|
|
|
((wParam == DBT_DEVICEARRIVAL) || (wParam == DBT_DEVICEREMOVECOMPLETE)) ) {
|
2011-11-26 00:25:04 +00:00
|
|
|
GetUSBDevices();
|
|
|
|
return (INT_PTR)TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2011-11-13 20:53:23 +00:00
|
|
|
|
|
|
|
case WM_INITDIALOG:
|
2011-11-21 17:06:17 +00:00
|
|
|
hMainDialog = hDlg;
|
2011-11-17 01:43:06 +00:00
|
|
|
hDeviceList = GetDlgItem(hDlg, IDC_DEVICE);
|
2011-11-18 21:46:34 +00:00
|
|
|
hCapacity = GetDlgItem(hDlg, IDC_CAPACITY);
|
2011-11-19 01:30:20 +00:00
|
|
|
hFileSystem = GetDlgItem(hDlg, IDC_FILESYSTEM);
|
2011-11-28 20:05:34 +00:00
|
|
|
hClusterSize = GetDlgItem(hDlg, IDC_CLUSTERSIZE);
|
2011-11-24 02:24:50 +00:00
|
|
|
hLabel = GetDlgItem(hDlg, IDC_LABEL);
|
2011-11-27 23:40:28 +00:00
|
|
|
hProgress = GetDlgItem(hDlg, IDC_PROGRESS);
|
2011-11-21 17:06:17 +00:00
|
|
|
// High DPI scaling
|
|
|
|
hDC = GetDC(hDlg);
|
|
|
|
fScale = GetDeviceCaps(hDC, LOGPIXELSX) / 96.0f;
|
|
|
|
ReleaseDC(hDlg, hDC);
|
|
|
|
// Create the status line
|
|
|
|
CreateStatusBar();
|
|
|
|
// Display the version in the right area of the status bar
|
|
|
|
SendMessageA(GetDlgItem(hDlg, IDC_STATUS), SB_SETTEXTA, SBT_OWNERDRAW | 1, (LPARAM)APP_VERSION);
|
2011-11-27 23:40:28 +00:00
|
|
|
// We'll switch the progressbar to marquee and back => keep a copy of current style
|
|
|
|
ProgressStyle = GetWindowLong(hProgress, GWL_STYLE);
|
2011-11-26 00:25:04 +00:00
|
|
|
// Create the string array
|
2011-11-24 02:24:50 +00:00
|
|
|
StrArrayCreate(&DriveID, MAX_DRIVES);
|
|
|
|
StrArrayCreate(&DriveLabel, MAX_DRIVES);
|
2011-11-26 00:25:04 +00:00
|
|
|
// Set the quick format checkbox
|
|
|
|
CheckDlgButton(hDlg, IDC_QUICKFORMAT, BST_CHECKED);
|
2011-11-17 01:43:06 +00:00
|
|
|
GetUSBDevices();
|
2011-11-13 20:53:23 +00:00
|
|
|
return (INT_PTR)TRUE;
|
|
|
|
|
2011-11-21 17:06:17 +00:00
|
|
|
// Change the colour of the version text in the status bar
|
|
|
|
case WM_DRAWITEM:
|
|
|
|
if (wParam == IDC_STATUS) {
|
|
|
|
pDI = (DRAWITEMSTRUCT*)lParam;
|
|
|
|
SetBkMode(pDI->hDC, TRANSPARENT);
|
|
|
|
SetTextColor(pDI->hDC, GetSysColor(COLOR_3DSHADOW));
|
|
|
|
pDI->rcItem.top += (int)(2.0f * fScale);
|
|
|
|
pDI->rcItem.left += (int)(4.0f * fScale);
|
|
|
|
DrawTextExA(pDI->hDC, APP_VERSION, -1, &pDI->rcItem, DT_LEFT, NULL);
|
|
|
|
return (INT_PTR)TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-11-13 20:53:23 +00:00
|
|
|
case WM_COMMAND:
|
|
|
|
switch(LOWORD(wParam)) {
|
2011-11-21 20:12:23 +00:00
|
|
|
case IDOK: // close application
|
|
|
|
case IDCANCEL:
|
2011-11-28 01:32:18 +00:00
|
|
|
if (format_thid != -1L) {
|
|
|
|
if (MessageBoxA(hMainDialog, "Cancelling may leave the device in an UNUSABLE state.\r\n"
|
|
|
|
"If you are sure you want to cancel, click YES. Otherwise, click NO.",
|
2011-11-28 12:37:58 +00:00
|
|
|
RUFUS_CANCELBOX_TITLE, MB_YESNO|MB_ICONWARNING) == IDYES) {
|
|
|
|
// Operation may have completed in the meantime
|
2011-11-28 01:32:18 +00:00
|
|
|
if (format_thid != -1L) {
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_CANCELLED;
|
2011-11-28 01:32:18 +00:00
|
|
|
StatusPrintf("Cancelling - please wait...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (INT_PTR)TRUE;
|
|
|
|
}
|
2011-11-21 20:12:23 +00:00
|
|
|
PostQuitMessage(0);
|
|
|
|
StrArrayDestroy(&DriveID);
|
2011-11-24 02:24:50 +00:00
|
|
|
StrArrayDestroy(&DriveLabel);
|
2011-11-21 20:25:32 +00:00
|
|
|
DestroyAllTooltips();
|
2011-11-21 20:12:23 +00:00
|
|
|
EndDialog(hDlg, 0);
|
|
|
|
break;
|
2011-11-21 17:06:17 +00:00
|
|
|
case IDC_ABOUT:
|
|
|
|
CreateAboutBox();
|
|
|
|
break;
|
2011-11-24 02:24:50 +00:00
|
|
|
case IDC_DEVICE:
|
2011-11-18 23:41:28 +00:00
|
|
|
switch (HIWORD(wParam)) {
|
|
|
|
case CBN_SELCHANGE:
|
2011-11-24 02:24:50 +00:00
|
|
|
StatusPrintf("%d device%s found.", ComboBox_GetCount(hDeviceList),
|
|
|
|
(ComboBox_GetCount(hDeviceList)!=1)?"s":"");
|
|
|
|
PopulateProperties(ComboBox_GetCurSel(hDeviceList));
|
2011-11-18 23:41:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-11-27 23:40:28 +00:00
|
|
|
break;
|
2011-11-23 12:27:51 +00:00
|
|
|
case IDC_START:
|
2011-11-27 23:40:28 +00:00
|
|
|
if (format_thid != -1L) {
|
|
|
|
return (INT_PTR)TRUE;
|
|
|
|
}
|
2011-11-23 12:27:51 +00:00
|
|
|
nDeviceIndex = ComboBox_GetCurSel(hDeviceList);
|
|
|
|
if (nDeviceIndex != CB_ERR) {
|
2011-11-24 02:24:50 +00:00
|
|
|
GetWindowTextA(hDeviceList, tmp, sizeof(tmp));
|
2011-11-28 01:32:18 +00:00
|
|
|
safe_sprintf(str, sizeof(str), "WARNING: ALL DATA ON DEVICE %s\r\nWILL BE DESTROYED.\r\n"
|
|
|
|
"To continue with this operation, click OK. To quit click CANCEL.", tmp);
|
2011-11-24 02:24:50 +00:00
|
|
|
if (MessageBoxA(hMainDialog, str, "Rufus", MB_OKCANCEL|MB_ICONWARNING) == IDOK) {
|
2011-11-28 01:32:18 +00:00
|
|
|
// Disable all controls except cancel
|
|
|
|
EnableControls(FALSE);
|
2011-11-27 23:40:28 +00:00
|
|
|
// Handle marquee progress bar on quickformat
|
|
|
|
SetWindowLongPtr(hProgress, GWL_STYLE, ProgressStyle | (IsChecked(IDC_QUICKFORMAT)?PBS_MARQUEE:0));
|
|
|
|
if (IsChecked(IDC_QUICKFORMAT)) {
|
|
|
|
SendMessage(hProgress, PBM_SETMARQUEE, TRUE, 0);
|
|
|
|
}
|
|
|
|
DeviceNum = (DWORD)ComboBox_GetItemData(hDeviceList, nDeviceIndex);
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = 0;
|
2011-11-27 23:40:28 +00:00
|
|
|
format_thid = _beginthread(FormatThread, 0, (void*)(uintptr_t)DeviceNum);
|
|
|
|
if (format_thid == -1L) {
|
|
|
|
uprintf("Unable to start formatting thread");
|
2011-11-28 12:37:58 +00:00
|
|
|
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_CANT_START_THREAD;
|
|
|
|
PostMessage(hMainDialog, UM_FORMAT_COMPLETED, 0, 0);
|
2011-11-27 23:40:28 +00:00
|
|
|
}
|
2011-11-24 02:24:50 +00:00
|
|
|
}
|
2011-11-23 12:27:51 +00:00
|
|
|
}
|
2011-11-27 23:40:28 +00:00
|
|
|
break;
|
2011-11-13 20:53:23 +00:00
|
|
|
default:
|
|
|
|
return (INT_PTR)FALSE;
|
|
|
|
}
|
|
|
|
return (INT_PTR)TRUE;
|
|
|
|
|
|
|
|
case WM_CLOSE:
|
2011-11-28 01:32:18 +00:00
|
|
|
if (format_thid != -1L) {
|
|
|
|
return (INT_PTR)TRUE;
|
|
|
|
}
|
2011-11-13 20:53:23 +00:00
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
|
2011-11-26 00:25:04 +00:00
|
|
|
case UM_FORMAT_PROGRESS:
|
2011-11-27 23:40:28 +00:00
|
|
|
SendMessage(hProgress, PBM_SETPOS, wParam, lParam);
|
2011-11-26 00:25:04 +00:00
|
|
|
return (INT_PTR)TRUE;
|
|
|
|
|
2011-11-27 23:40:28 +00:00
|
|
|
case UM_FORMAT_COMPLETED:
|
|
|
|
format_thid = -1L;
|
2011-11-28 12:37:58 +00:00
|
|
|
// Close the cancel MessageBox if active
|
|
|
|
SendMessage(FindWindowA(MAKEINTRESOURCEA(32770), RUFUS_CANCELBOX_TITLE), WM_COMMAND, IDNO, 0);
|
2011-11-28 01:32:18 +00:00
|
|
|
if (IsChecked(IDC_QUICKFORMAT)) {
|
2011-11-27 23:40:28 +00:00
|
|
|
SendMessage(hProgress, PBM_SETMARQUEE, FALSE, 0);
|
2011-11-28 01:32:18 +00:00
|
|
|
SetWindowLongPtr(hProgress, GWL_STYLE, ProgressStyle);
|
|
|
|
// This is the only way to achieve instantenous progress transition
|
|
|
|
SendMessage(hProgress, PBM_SETRANGE, 0, 101<<16);
|
|
|
|
SendMessage(hProgress, PBM_SETPOS, 101, 0);
|
|
|
|
SendMessage(hProgress, PBM_SETRANGE, 0, 100<<16);
|
|
|
|
}
|
2011-11-28 12:37:58 +00:00
|
|
|
SendMessage(hProgress, PBM_SETPOS, FormatStatus?0:100, 0);
|
|
|
|
StatusPrintf(!IS_ERROR(FormatStatus)?"DONE":
|
|
|
|
((SCODE_CODE(FormatStatus)==ERROR_CANCELLED)?"Cancelled":"FAILED"));
|
2011-11-28 01:32:18 +00:00
|
|
|
EnableControls(TRUE);
|
2011-11-27 23:40:28 +00:00
|
|
|
return (INT_PTR)TRUE;
|
2011-11-13 20:53:23 +00:00
|
|
|
}
|
|
|
|
return (INT_PTR)FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Application Entrypoint
|
|
|
|
*/
|
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
|
|
|
{
|
|
|
|
HANDLE mutex = NULL;
|
|
|
|
HWND hDlg = NULL;
|
|
|
|
MSG msg;
|
|
|
|
|
2011-11-19 19:08:23 +00:00
|
|
|
uprintf("*** RUFUS INIT ***\n");
|
2011-11-17 01:43:06 +00:00
|
|
|
|
2011-11-13 20:53:23 +00:00
|
|
|
// Prevent 2 applications from running at the same time
|
2011-11-19 19:08:23 +00:00
|
|
|
mutex = CreateMutexA(NULL, TRUE, "Global/RUFUS");
|
2011-11-13 20:53:23 +00:00
|
|
|
if ((mutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS))
|
|
|
|
{
|
2011-11-19 19:08:23 +00:00
|
|
|
MessageBoxA(NULL, "Another Rufus application is running.\n"
|
2011-11-13 20:53:23 +00:00
|
|
|
"Please close the first application before running another one.",
|
|
|
|
"Other instance detected", MB_ICONSTOP);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save instance of the application for further reference
|
2011-11-18 23:41:28 +00:00
|
|
|
hMainInstance = hInstance;
|
2011-11-13 20:53:23 +00:00
|
|
|
|
|
|
|
// Initialize COM for folder selection
|
|
|
|
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
|
|
|
|
|
|
|
|
// Create the main Window
|
2011-11-18 01:58:08 +00:00
|
|
|
if ( (hDlg = CreateDialogA(hInstance, MAKEINTRESOURCEA(IDD_DIALOG), NULL, MainCallback)) == NULL ) {
|
2011-11-13 20:53:23 +00:00
|
|
|
MessageBoxA(NULL, "Could not create Window", "DialogBox failure", MB_ICONSTOP);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
CenterDialog(hDlg);
|
|
|
|
ShowWindow(hDlg, SW_SHOWNORMAL);
|
|
|
|
UpdateWindow(hDlg);
|
|
|
|
|
|
|
|
// Do our own event processing
|
|
|
|
while(GetMessage(&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
CloseHandle(mutex);
|
2011-11-19 19:08:23 +00:00
|
|
|
uprintf("*** RUFUS EXIT ***\n");
|
2011-11-13 20:53:23 +00:00
|
|
|
|
2011-11-21 20:12:23 +00:00
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
_CrtDumpMemoryLeaks();
|
|
|
|
#endif
|
|
|
|
|
2011-11-13 20:53:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|