From 8085a2846d0cd84e0829e205a39c036b096bf8a7 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Sun, 25 Oct 2020 12:31:30 +0000 Subject: [PATCH] [misc] fix small issues * SetAutoMount()/GetAutoMount() should check for INVALID_HANDLE_VALUE and not NULL. Also we don't actually need to open MOUNTMGR_DOS_DEVICE_NAME rw to issue an IOCTL. * ToggleEsp() failed to exit properly when an ESP offset was specified. * Introduce PI_MAX to explicitly set the size of the partition_information table. * write_sectors() has write retry, so there's no need to perform one on top of it. * When we exit FormatThread(), GetLogicalName() should attempt to look for the the main partition and be silent. --- src/drive.c | 18 ++++++++++-------- src/drive.h | 3 ++- src/format.c | 36 ++++++++++-------------------------- src/rufus.rc | 10 +++++----- 4 files changed, 27 insertions(+), 40 deletions(-) diff --git a/src/drive.c b/src/drive.c index 2ca6405e..866701c9 100644 --- a/src/drive.c +++ b/src/drive.c @@ -74,7 +74,7 @@ PF_TYPE_DECL(NTAPI, NTSTATUS, NtQueryVolumeInformationFile, (HANDLE, PIO_STATUS_ RUFUS_DRIVE_INFO SelectedDrive; extern BOOL installed_uefi_ntfs, write_as_esp; extern int nWindowsVersion, nWindowsBuildNumber; -uint64_t partition_offset[3]; +uint64_t partition_offset[PI_MAX]; uint64_t persistence_size = 0; /* @@ -93,9 +93,8 @@ BOOL SetAutoMount(BOOL enable) 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) + hMountMgr = CreateFileA(MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hMountMgr == INVALID_HANDLE_VALUE) return FALSE; ret = DeviceIoControl(hMountMgr, IOCTL_MOUNTMGR_SET_AUTO_MOUNT, &enable, sizeof(enable), NULL, 0, &size, NULL); CloseHandle(hMountMgr); @@ -110,9 +109,8 @@ BOOL GetAutoMount(BOOL* enabled) 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) + hMountMgr = CreateFileA(MOUNTMGR_DOS_DEVICE_NAME, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hMountMgr == INVALID_HANDLE_VALUE) return FALSE; ret = DeviceIoControl(hMountMgr, IOCTL_MOUNTMGR_QUERY_AUTO_MOUNT, NULL, 0, enabled, sizeof(*enabled), &size, NULL); CloseHandle(hMountMgr); @@ -1292,8 +1290,10 @@ BOOL ToggleEsp(DWORD DriveIndex, uint64_t PartitionOffset) } } else { for (i = 0, j = 0; i < DriveLayout->PartitionCount; i++) { - if (DriveLayout->PartitionEntry[i].StartingOffset.QuadPart == PartitionOffset) + if (DriveLayout->PartitionEntry[i].StartingOffset.QuadPart == PartitionOffset) { DriveLayout->PartitionEntry[i].Gpt.PartitionType = PARTITION_GENERIC_ESP; + break; + } } } if (i >= DriveLayout->PartitionCount) { @@ -1853,6 +1853,8 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m } else { assert(FALSE); } + // NB: Because we already subtracted the backup GPT size from the main partition size and + // this extra partition is indexed on main size, it does not overflow into the backup GPT. main_part_size_in_sectors = ((main_part_size_in_sectors / SelectedDrive.SectorsPerTrack) - extra_part_size_in_tracks) * SelectedDrive.SectorsPerTrack; } diff --git a/src/drive.h b/src/drive.h index 1c5ccf39..8a35d86a 100644 --- a/src/drive.h +++ b/src/drive.h @@ -41,6 +41,7 @@ #define PI_MAIN 0 #define PI_ESP 1 #define PI_CASPER 2 +#define PI_MAX 3 // The following should match VDS_FSOF_FLAGS as much as possible #define FP_FORCE 0x00000001 @@ -362,7 +363,7 @@ typedef struct { } ClusterSize[FS_MAX]; } RUFUS_DRIVE_INFO; extern RUFUS_DRIVE_INFO SelectedDrive; -extern uint64_t partition_offset[3]; +extern uint64_t partition_offset[PI_MAX]; BOOL SetAutoMount(BOOL enable); BOOL GetAutoMount(BOOL* enabled); diff --git a/src/format.c b/src/format.c index c703544b..53f3d8ae 100644 --- a/src/format.c +++ b/src/format.c @@ -705,7 +705,7 @@ out: static BOOL ClearMBRGPT(HANDLE hPhysicalDrive, LONGLONG DiskSize, DWORD SectorSize, BOOL add1MB) { BOOL r = FALSE; - uint64_t i, j, last_sector = DiskSize/SectorSize, num_sectors_to_clear; + uint64_t i, last_sector = DiskSize/SectorSize, num_sectors_to_clear; unsigned char* pBuf = (unsigned char*) calloc(SectorSize, 1); PrintInfoDebug(0, MSG_224); @@ -729,32 +729,16 @@ static BOOL ClearMBRGPT(HANDLE hPhysicalDrive, LONGLONG DiskSize, DWORD SectorSi uprintf("Erasing %d sectors", num_sectors_to_clear); for (i = 0; i < num_sectors_to_clear; i++) { - for (j = 1; j <= WRITE_RETRIES; j++) { - CHECK_FOR_USER_CANCEL; - if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) == SectorSize) - break; - if (j >= WRITE_RETRIES) - goto out; - uprintf("Retrying in %d seconds...", WRITE_TIMEOUT / 1000); - // Don't sit idly but use the downtime to check for conflicting processes... - Sleep(CheckDriveAccess(WRITE_TIMEOUT, FALSE)); - } + CHECK_FOR_USER_CANCEL; + if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) != SectorSize) + goto out; } for (i = last_sector - MAX_SECTORS_TO_CLEAR; i < last_sector; i++) { - for (j = 1; j <= WRITE_RETRIES; j++) { - CHECK_FOR_USER_CANCEL; - if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) == SectorSize) - break; - if (j >= WRITE_RETRIES) { - // Windows seems to be an ass about keeping a lock on a backup GPT, - // so we try to be lenient about not being able to clear it. - uprintf("Warning: Failed to clear backup GPT..."); - r = TRUE; - goto out; - } - uprintf("Retrying in %d seconds...", WRITE_TIMEOUT / 1000); - Sleep(CheckDriveAccess(WRITE_TIMEOUT, FALSE)); - } + CHECK_FOR_USER_CANCEL; + // Windows seems to be an ass about keeping a lock on a backup GPT, + // so we try to be lenient about not being able to clear it. + if (write_sectors(hPhysicalDrive, SectorSize, i, 1, pBuf) != SectorSize) + break; } r = TRUE; @@ -2195,7 +2179,7 @@ out: safe_unlockclose(hLogicalVolume); safe_unlockclose(hPhysicalDrive); // This can take a while if (IS_ERROR(FormatStatus)) { - volume_name = GetLogicalName(DriveIndex, 0, TRUE, FALSE); + volume_name = GetLogicalName(DriveIndex, partition_offset[PI_MAIN], TRUE, TRUE); if (volume_name != NULL) { if (MountVolume(drive_name, volume_name)) uprintf("Re-mounted volume as %C: after error", drive_name[0]); diff --git a/src/rufus.rc b/src/rufus.rc index 31978497..36bef8aa 100644 --- a/src/rufus.rc +++ b/src/rufus.rc @@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL IDD_DIALOG DIALOGEX 12, 12, 232, 326 STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_ACCEPTFILES -CAPTION "Rufus 3.12.1711" +CAPTION "Rufus 3.12.1712" FONT 9, "Segoe UI Symbol", 400, 0, 0x0 BEGIN LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP @@ -395,8 +395,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 3,12,1711,0 - PRODUCTVERSION 3,12,1711,0 + FILEVERSION 3,12,1712,0 + PRODUCTVERSION 3,12,1712,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -414,13 +414,13 @@ BEGIN VALUE "Comments", "https://rufus.ie" VALUE "CompanyName", "Akeo Consulting" VALUE "FileDescription", "Rufus" - VALUE "FileVersion", "3.12.1711" + VALUE "FileVersion", "3.12.1712" VALUE "InternalName", "Rufus" VALUE "LegalCopyright", "© 2011-2020 Pete Batard (GPL v3)" VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html" VALUE "OriginalFilename", "rufus-3.12.exe" VALUE "ProductName", "Rufus" - VALUE "ProductVersion", "3.12.1711" + VALUE "ProductVersion", "3.12.1712" END END BLOCK "VarFileInfo"