From 936bd9beb027cfe11989257ba015942d5d4ca99d Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Tue, 13 Aug 2019 09:04:31 +0100 Subject: [PATCH] [core] revert to having the ESP as the last partition * This is to avoid Microsoft's appalling refresh of the partition layout, which can result in partitions not being assigned a volume GUID. * Mostly reverts a change that was applied in 1c39a80d72ce3f6914925ad18518857063dfa413. * Also add some more enum output and bail if we can't get a logical drive. * Closes #1351 --- src/drive.c | 84 ++++++++++++++++++++++++---------------------------- src/format.c | 7 +++-- src/rufus.rc | 10 +++---- 3 files changed, 48 insertions(+), 53 deletions(-) diff --git a/src/drive.c b/src/drive.c index 8766e074..b7c0fa52 100644 --- a/src/drive.c +++ b/src/drive.c @@ -287,6 +287,7 @@ char* GetLogicalName(DWORD DriveIndex, DWORD PartitionIndex, BOOL bKeepTrailingB static const char* ignore_device[] = { "\\Device\\CdRom", "\\Device\\Floppy" }; static const char* volume_start = "\\\\?\\"; char *ret = NULL, volume_name[MAX_PATH], path[MAX_PATH]; + BOOL bPrintHeader = TRUE; HANDLE hDrive = INVALID_HANDLE_VALUE, hVolume = INVALID_HANDLE_VALUE; VOLUME_DISK_EXTENTS_REDEF DiskExtents; DWORD size; @@ -378,7 +379,13 @@ char* GetLogicalName(DWORD DriveIndex, DWORD PartitionIndex, BOOL bKeepTrailingB volume_name[len - 1] = '\\'; found_offset[found_name.Index] = DiskExtents.Extents[0].StartingOffset.QuadPart; StrArrayAdd(&found_name, volume_name, TRUE); - // uprintf("GOT %s @%lld", volume_name, DiskExtents.Extents[0].StartingOffset.QuadPart); + if (!bSilent) { + if (bPrintHeader) { + bPrintHeader = FALSE; + uuprintf("Windows volumes from this device:"); + } + uuprintf("● %s @%lld", volume_name, DiskExtents.Extents[0].StartingOffset.QuadPart); + } } if (found_name.Index == 0) @@ -731,7 +738,7 @@ BOOL WaitForLogical(DWORD DriveIndex, DWORD PartitionIndex) free(LogicalPath); if (IS_ERROR(FormatStatus)) // User cancel return FALSE; - Sleep(DRIVE_ACCESS_TIMEOUT/DRIVE_ACCESS_RETRIES); + Sleep(DRIVE_ACCESS_TIMEOUT / DRIVE_ACCESS_RETRIES); } while (GetTickCount64() < EndTime); uprintf("Timeout while waiting for logical drive"); return FALSE; @@ -1540,45 +1547,13 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart = bytes_per_track; } - // If required, set the ESP (which Microsoft wants to be the first) - if (extra_partitions & XP_ESP) { - extra_part_name = L"EFI System Partition"; - // The size of the ESP 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. - // Plus some people are complaining that the *OFFICIAL MINIMUM SIZE* as documented by Microsoft at - // https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/configure-uefigpt-based-hard-drive-partitions - // is too small. See: https://github.com/pbatard/rufus/issues/979 - if (SelectedDrive.SectorSize <= 4096) - ms_esp_size = 300 * MB; - else - ms_esp_size = 1200 * MB; // That'll teach you to have a nonstandard disk! - extra_part_size_in_tracks = (ms_esp_size + bytes_per_track - 1) / bytes_per_track; - DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart = extra_part_size_in_tracks * bytes_per_track; - uprintf("● Creating %S Partition (offset: %lld, size: %s)", extra_part_name, DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart, - SizeToHumanReadable(DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart, TRUE, FALSE)); - if (partition_style == PARTITION_STYLE_GPT) { - DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = PARTITION_SYSTEM_GUID; - IGNORE_RETVAL(CoCreateGuid(&DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionId)); - wcscpy(DriveLayoutEx.PartitionEntry[pn].Gpt.Name, extra_part_name); - } else { - DriveLayoutEx.PartitionEntry[pn].Mbr.PartitionType = 0xef; - } - // Zero the first sectors from this partition to avoid file system caching issues - if (!ClearPartition(hDrive, DriveLayoutEx.PartitionEntry[pn].StartingOffset, size_to_clear)) - uprintf("Could not zero %S: %s", extra_part_name, WindowsErrorString()); - pn++; - partition_index[PI_ESP] = pn; - DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart = DriveLayoutEx.PartitionEntry[pn - 1].StartingOffset.QuadPart + - DriveLayoutEx.PartitionEntry[pn - 1].PartitionLength.QuadPart; - } - // If required, set the MSR partition (GPT only - must be created before the data part) if (extra_partitions & XP_MSR) { assert (partition_style == PARTITION_STYLE_GPT); extra_part_name = L"Microsoft Reserved Partition"; DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart = 128*MB; DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = PARTITION_MSFT_RESERVED_GUID; - uprintf("● Creating %S Partition (offset: %lld, size: %s)", extra_part_name, DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart, + uprintf("● Creating %S (offset: %lld, size: %s)", extra_part_name, DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart, SizeToHumanReadable(DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart, TRUE, FALSE)); IGNORE_RETVAL(CoCreateGuid(&DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionId)); wcsncpy(DriveLayoutEx.PartitionEntry[pn].Gpt.Name, extra_part_name, ARRAYSIZE(DriveLayoutEx.PartitionEntry[pn].Gpt.Name)); @@ -1588,9 +1563,9 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m pn++; DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart = DriveLayoutEx.PartitionEntry[pn-1].StartingOffset.QuadPart + DriveLayoutEx.PartitionEntry[pn-1].PartitionLength.QuadPart; + // Clear the extra partition we processed + extra_partitions &= ~(XP_MSR); } - // Clear the extra partitions we processed - extra_partitions &= ~(XP_ESP|XP_MSR); // Set our main data partition main_part_size_in_sectors = (SelectedDrive.DiskSize - DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart) / @@ -1598,7 +1573,19 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m SelectedDrive.SectorSize - ((partition_style == PARTITION_STYLE_GPT)?33:0); if (extra_partitions) { // Adjust the size according to extra partitions (which we always align to a track) - if (extra_partitions & XP_UEFI_NTFS) { + if (extra_partitions & XP_ESP) { + extra_part_name = L"EFI System"; + // The size of the ESP 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. + // Plus some people are complaining that the *OFFICIAL MINIMUM SIZE* as documented by Microsoft at + // https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/configure-uefigpt-based-hard-drive-partitions + // is too small. See: https://github.com/pbatard/rufus/issues/979 + if (SelectedDrive.SectorSize <= 4096) + ms_esp_size = 300 * MB; + else + ms_esp_size = 1200 * MB; // That'll teach you to have a nonstandard disk! + extra_part_size_in_tracks = (ms_esp_size + bytes_per_track - 1) / bytes_per_track; + } else if (extra_partitions & XP_UEFI_NTFS) { extra_part_name = L"UEFI:NTFS"; extra_part_size_in_tracks = (max(MIN_EXTRA_PART_SIZE, uefi_ntfs_size) + bytes_per_track - 1) / bytes_per_track; } else if ((extra_partitions & XP_CASPER)) { @@ -1659,9 +1646,10 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m // Set the optional extra partition if (extra_partitions) { - // Don't forget to set our peristent partition index! if (extra_partitions & XP_CASPER) partition_index[PI_CASPER] = pn + 1; + else if (extra_partitions & XP_ESP) + partition_index[PI_ESP] = pn + 1; // Should end on a track boundary DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart = DriveLayoutEx.PartitionEntry[pn-1].StartingOffset.QuadPart + DriveLayoutEx.PartitionEntry[pn-1].PartitionLength.QuadPart; @@ -1670,11 +1658,12 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m uprintf("● Creating %S Partition (offset: %lld, size: %s)", extra_part_name, DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart, SizeToHumanReadable(DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart, TRUE, FALSE)); if (partition_style == PARTITION_STYLE_GPT) { - DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = PARTITION_BASIC_DATA_GUID; + DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = (extra_partitions & XP_ESP) ? PARTITION_SYSTEM_GUID : PARTITION_BASIC_DATA_GUID; IGNORE_RETVAL(CoCreateGuid(&DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionId)); - wcsncpy(DriveLayoutEx.PartitionEntry[pn].Gpt.Name, extra_part_name, ARRAYSIZE(DriveLayoutEx.PartitionEntry[pn].Gpt.Name)); + wcsncpy(DriveLayoutEx.PartitionEntry[pn].Gpt.Name, (extra_partitions & XP_ESP) ? L"EFI System Partition" : extra_part_name, + ARRAYSIZE(DriveLayoutEx.PartitionEntry[pn].Gpt.Name)); } else { - if (extra_partitions & XP_UEFI_NTFS) { + if (extra_partitions & (XP_UEFI_NTFS | XP_ESP)) { DriveLayoutEx.PartitionEntry[pn].Mbr.PartitionType = 0xef; } else if (extra_partitions & XP_CASPER) { DriveLayoutEx.PartitionEntry[pn].Mbr.PartitionType = 0x83; @@ -1732,7 +1721,7 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m // ms-sys's write_partition_number_of_heads() and write_partition_start_sector_number() can be used if needed break; case PARTITION_STYLE_GPT: - // TODO: (?) As per MSDN: "When specifying a GUID partition table (GPT) as the PARTITION_STYLE of the CREATE_DISK + // TODO: (HOW?!?!?) 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." @@ -1750,16 +1739,19 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m break; } - // If you don't call IOCTL_DISK_CREATE_DISK, the next call will fail + // If you don't call IOCTL_DISK_CREATE_DISK, the IOCTL_DISK_SET_DRIVE_LAYOUT_EX call will fail size = sizeof(CreateDisk); - r = DeviceIoControl(hDrive, IOCTL_DISK_CREATE_DISK, (BYTE*)&CreateDisk, size, NULL, 0, &size, NULL ); + r = DeviceIoControl(hDrive, IOCTL_DISK_CREATE_DISK, (BYTE*)&CreateDisk, size, NULL, 0, &size, NULL); if (!r) { uprintf("Could not reset disk: %s", WindowsErrorString()); return FALSE; } + // "The goggles, they do nothing!" + RefreshDriveLayout(hDrive); + 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 ); + r = DeviceIoControl(hDrive, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, (BYTE*)&DriveLayoutEx, size, NULL, 0, &size, NULL); if (!r) { uprintf("Could not set drive layout: %s", WindowsErrorString()); return FALSE; diff --git a/src/format.c b/src/format.c index 8d32c59c..8dba48af 100644 --- a/src/format.c +++ b/src/format.c @@ -2679,8 +2679,11 @@ DWORD WINAPI FormatThread(void* param) // Wait for the logical drive we just created to appear uprintf("Waiting for logical drive to reappear..."); Sleep(200); - if (!WaitForLogical(DriveIndex, partition_index[PI_MAIN])) - uprintf("Logical drive was not found!"); // We try to continue even if this fails, just in case + if (!WaitForLogical(DriveIndex, partition_index[PI_MAIN])) { + uprintf("Logical drive was not found - aborting"); + FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_NO_VOLUME_ID; + goto out; + } CHECK_FOR_USER_CANCEL; // Format Casper partition if required. Do it before we format anything with diff --git a/src/rufus.rc b/src/rufus.rc index 6971d20f..e58fe93c 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.7.1558" +CAPTION "Rufus 3.7.1559" FONT 9, "Segoe UI Symbol", 400, 0, 0x0 BEGIN LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP @@ -394,8 +394,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 3,7,1558,0 - PRODUCTVERSION 3,7,1558,0 + FILEVERSION 3,7,1559,0 + PRODUCTVERSION 3,7,1559,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -413,13 +413,13 @@ BEGIN VALUE "Comments", "https://akeo.ie" VALUE "CompanyName", "Akeo Consulting" VALUE "FileDescription", "Rufus" - VALUE "FileVersion", "3.7.1558" + VALUE "FileVersion", "3.7.1559" VALUE "InternalName", "Rufus" VALUE "LegalCopyright", " 2011-2019 Pete Batard (GPL v3)" VALUE "LegalTrademarks", "https://www.gnu.org/copyleft/gpl.html" VALUE "OriginalFilename", "rufus-3.7.exe" VALUE "ProductName", "Rufus" - VALUE "ProductVersion", "3.7.1558" + VALUE "ProductVersion", "3.7.1559" END END BLOCK "VarFileInfo"