Skip to content

Commit

Permalink
[core] work around a Windows bug that may render a disk inaccessible …
Browse files Browse the repository at this point in the history
…on cleanup

* The root of the issue is that Windows IOCTL_DISK_CREATE_DISK does not properly
  zero all of the MBR/GPT/PBR structure with PARTITION_STYLE_RAW (which is what
  diskpart uses to clean a disk), and leaves plenty of partition artefacts behind.
* This means that, when an image with complex partitioning has been applied, such
  as Chromium/ChromeOS, you may end up with a drive that can not be repartitioned
  or reformatted in Windows (and this is completely independent of whether Rufus
  was used to perform these operations - For instance you will get the same issue
  if you use Win32DiskImager and diskpart + clean).
* The only option left for users then is to reset/repartition their drives in Linux
  or some other OS, as Windows' VDS becomes incapacitated to handle the drive, as
  the problem persists independently of reset/re-plug/Windows platform being used.
* To work around this, we ensure that we zero the MBR/GPT/PBR sectors BEFORE calling
  IOCTL_DISK_CREATE_DISK.
* Also move zeroing of the drive before partition reset.
* Closes pbatard#759
  • Loading branch information
pbatard committed May 21, 2016
1 parent 935679d commit 258a4f7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/drive.c
Expand Up @@ -1247,8 +1247,8 @@ BOOL RefreshDriveLayout(HANDLE hDrive)
return r;
}

/* Delete the disk partition table */
BOOL DeletePartitions(HANDLE hDrive)
/* Initialize disk for partitioning */
BOOL InitializeDisk(HANDLE hDrive)
{
BOOL r;
DWORD size;
Expand Down
2 changes: 1 addition & 1 deletion src/drive.h
Expand Up @@ -97,6 +97,6 @@ BOOL AltUnmountVolume(const char* drive_name);
char* AltMountVolume(const char* drive_name, uint8_t part_nr);
BOOL RemountVolume(char* drive_name);
BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL mbr_uefi_marker, uint8_t extra_partitions);
BOOL DeletePartitions(HANDLE hDrive);
BOOL InitializeDisk(HANDLE hDrive);
BOOL RefreshDriveLayout(HANDLE hDrive);
const char* GetPartitionType(BYTE Type);
32 changes: 17 additions & 15 deletions src/format.c
Expand Up @@ -1668,20 +1668,22 @@ DWORD WINAPI FormatThread(void* param)
UpdateProgress(OP_ANALYZE_MBR, -1.0f);
}

if (zero_drive) {
WriteDrive(hPhysicalDrive, NULL);
goto out;
}

// Zap any existing partitions. This helps prevent access errors.
// As this creates issues with FAT16 formatted MS drives, only do this for other filesystems
if ( (fs != FS_FAT16) && (!DeletePartitions(hPhysicalDrive)) ) {
// Note, Microsoft's way of cleaning partitions (IOCTL_DISK_CREATE_DISK, which is what we apply
// in InitializeDisk) is *NOT ENOUGH* to reset a disk and can render it inoperable for partitioning
// or formatting under Windows. See https://github.com/pbatard/rufus/issues/759 for details.
if ((!ClearMBRGPT(hPhysicalDrive, SelectedDrive.DiskSize, SectorSize, FALSE)) || (!InitializeDisk(hPhysicalDrive)) ) {
uprintf("Could not reset partitions\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_PARTITION_FAILURE;
goto out;
}
CreateThread(NULL, 0, CloseFormatPromptThread, NULL, 0, NULL);

if (zero_drive) {
WriteDrive(hPhysicalDrive, NULL);
goto out;
}

if (IsChecked(IDC_BADBLOCKS)) {
do {
// create a log file for bad blocks report. Since %USERPROFILE% may
Expand Down Expand Up @@ -1736,15 +1738,15 @@ DWORD WINAPI FormatThread(void* param)
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_CANCELLED;
goto out;
}
}

// Especially after destructive badblocks test, you must zero the MBR/GPT completely
// before repartitioning. Else, all kind of bad things happen.
if (!ClearMBRGPT(hPhysicalDrive, SelectedDrive.DiskSize, SectorSize, use_large_fat32)) {
uprintf("unable to zero MBR/GPT\n");
if (!IS_ERROR(FormatStatus))
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT;
goto out;
// Especially after destructive badblocks test, you must zero the MBR/GPT completely
// before repartitioning. Else, all kind of bad things happen.
if (!ClearMBRGPT(hPhysicalDrive, SelectedDrive.DiskSize, SectorSize, use_large_fat32)) {
uprintf("unable to zero MBR/GPT\n");
if (!IS_ERROR(FormatStatus))
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT;
goto out;
}
}

// Write an image file
Expand Down
10 changes: 5 additions & 5 deletions src/rufus.rc
Expand Up @@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 242, 376
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 2.9.935"
CAPTION "Rufus 2.9.936"
FONT 8, "Segoe UI Symbol", 400, 0, 0x0
BEGIN
LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8
Expand Down Expand Up @@ -320,8 +320,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 2,9,935,0
PRODUCTVERSION 2,9,935,0
FILEVERSION 2,9,936,0
PRODUCTVERSION 2,9,936,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -338,13 +338,13 @@ BEGIN
BEGIN
VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "2.9.935"
VALUE "FileVersion", "2.9.936"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "� 2011-2016 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "2.9.935"
VALUE "ProductVersion", "2.9.936"
END
END
BLOCK "VarFileInfo"
Expand Down

0 comments on commit 258a4f7

Please sign in to comment.