1
1
Fork 0
mirror of https://github.com/pbatard/rufus.git synced 2024-08-14 23:57:05 +00:00

[togo] move ESP to the beginning of the drive on systems that allow it

This commit is contained in:
Pete Batard 2020-09-10 17:48:09 +01:00
parent b3caf638b6
commit 1e56c8812e
No known key found for this signature in database
GPG key ID: 38E0CF5E69EDD671
2 changed files with 40 additions and 19 deletions

View file

@ -73,7 +73,7 @@ PF_TYPE_DECL(NTAPI, NTSTATUS, NtQueryVolumeInformationFile, (HANDLE, PIO_STATUS_
*/ */
RUFUS_DRIVE_INFO SelectedDrive; RUFUS_DRIVE_INFO SelectedDrive;
extern BOOL installed_uefi_ntfs, write_as_esp; extern BOOL installed_uefi_ntfs, write_as_esp;
extern int nWindowsVersion; extern int nWindowsVersion, nWindowsBuildNumber;
uint64_t partition_offset[3]; uint64_t partition_offset[3];
uint64_t persistence_size = 0; uint64_t persistence_size = 0;
@ -1575,7 +1575,7 @@ BOOL MountVolume(char* drive_name, char *volume_name)
} }
uprintf("Retrying after dismount..."); uprintf("Retrying after dismount...");
if (!DeleteVolumeMountPointA(drive_name)) if (!DeleteVolumeMountPointA(drive_name))
uprintf("Warning: Could not delete volume mountpoint: %s", WindowsErrorString()); uprintf("Warning: Could not delete volume mountpoint '%s': %s", drive_name, WindowsErrorString());
if (SetVolumeMountPointA(drive_name, volume_name)) if (SetVolumeMountPointA(drive_name, volume_name))
return TRUE; return TRUE;
if ((GetLastError() == ERROR_DIR_NOT_EMPTY) && if ((GetLastError() == ERROR_DIR_NOT_EMPTY) &&
@ -1608,7 +1608,7 @@ char* AltMountVolume(DWORD DriveIndex, uint64_t PartitionOffset, BOOL bSilent)
goto out; goto out;
} }
// Can't use a regular volume GUID for ESPs... // Can't use a regular volume GUID for ESPs...
volume_name = AltGetLogicalName(DriveIndex, PartitionOffset, FALSE, TRUE); volume_name = AltGetLogicalName(DriveIndex, PartitionOffset, FALSE, FALSE);
if ((volume_name == NULL) || (strncmp(volume_name, groot_name, groot_len) != 0)) { if ((volume_name == NULL) || (strncmp(volume_name, groot_name, groot_len) != 0)) {
suprintf("Unexpected volume name: '%s'", volume_name); suprintf("Unexpected volume name: '%s'", volume_name);
goto out; goto out;
@ -1716,7 +1716,11 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m
DRIVE_LAYOUT_INFORMATION_EX4 DriveLayoutEx = {0}; DRIVE_LAYOUT_INFORMATION_EX4 DriveLayoutEx = {0};
BOOL r; BOOL r;
DWORD i, size, bufsize, pn = 0; DWORD i, size, bufsize, pn = 0;
LONGLONG main_part_size_in_sectors, extra_part_size_in_tracks = 0, esp_size; LONGLONG main_part_size_in_sectors, extra_part_size_in_tracks = 0;
// Go for a 260 MB sized ESP by default to keep everyone happy, including 4K sector users:
// https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/configure-uefigpt-based-hard-drive-partitions
// and folks using MacOS: https://github.com/pbatard/rufus/issues/979
LONGLONG esp_size = 260 * MB;
PrintInfoDebug(0, MSG_238, PartitionTypeName[partition_style]); PrintInfoDebug(0, MSG_238, PartitionTypeName[partition_style]);
@ -1753,6 +1757,32 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m
((bytes_per_track + (ClusterSize - 1)) / ClusterSize) * ClusterSize; ((bytes_per_track + (ClusterSize - 1)) / ClusterSize) * ClusterSize;
} }
// Having the ESP up front may help (and is the Microsoft recommended way) but this
// is only achievable if we can mount more than one partition at once, which means
// either fixed drive or Windows 10 1703 or later.
if (((SelectedDrive.MediaType == FixedMedia) || (nWindowsBuildNumber > 15000)) &&
(extra_partitions & XP_ESP)) {
assert(partition_style == PARTITION_STYLE_GPT);
extra_part_name = L"EFI System Partition";
DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart = esp_size;
DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = PARTITION_GENERIC_ESP;
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));
// 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());
SelectedDrive.PartitionOffset[pn] = DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart;
SelectedDrive.PartitionSize[pn] = DriveLayoutEx.PartitionEntry[pn].PartitionLength.QuadPart;
partition_offset[PI_ESP] = SelectedDrive.PartitionOffset[pn];
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_ESP);
}
// If required, set the MSR partition (GPT only - must be created before the data part) // If required, set the MSR partition (GPT only - must be created before the data part)
if (extra_partitions & XP_MSR) { if (extra_partitions & XP_MSR) {
assert(partition_style == PARTITION_STYLE_GPT); assert(partition_style == PARTITION_STYLE_GPT);
@ -1778,7 +1808,7 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m
// Set our main data partition // Set our main data partition
if (write_as_esp) { if (write_as_esp) {
// Align ESP to 64 MB while leaving at least 32 MB free space // Align ESP to 64 MB while leaving at least 32 MB free space
esp_size = (((img_report.projected_size / MB) + 96) / 64) * 64 * MB; esp_size = max(esp_size, ((((LONGLONG)img_report.projected_size / MB) + 96) / 64) * 64 * MB);
main_part_size_in_sectors = (esp_size - DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart) / main_part_size_in_sectors = (esp_size - DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart) /
SelectedDrive.SectorSize; SelectedDrive.SectorSize;
} else { } else {
@ -1790,15 +1820,6 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m
// Adjust the size according to extra partitions (which we always align to a track) // Adjust the size according to extra partitions (which we always align to a track)
if (extra_partitions & XP_ESP) { if (extra_partitions & XP_ESP) {
extra_part_name = L"EFI System"; 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)
esp_size = 300 * MB;
else
esp_size = 1200 * MB; // That'll teach you to have a nonstandard disk!
extra_part_size_in_tracks = (esp_size + bytes_per_track - 1) / bytes_per_track; extra_part_size_in_tracks = (esp_size + bytes_per_track - 1) / bytes_per_track;
} else if (extra_partitions & XP_UEFI_NTFS) { } else if (extra_partitions & XP_UEFI_NTFS) {
extra_part_name = L"UEFI:NTFS"; extra_part_name = L"UEFI:NTFS";

View file

@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 232, 326 IDD_DIALOG DIALOGEX 12, 12, 232, 326
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 3.12.1697" CAPTION "Rufus 3.12.1698"
FONT 9, "Segoe UI Symbol", 400, 0, 0x0 FONT 9, "Segoe UI Symbol", 400, 0, 0x0
BEGIN BEGIN
LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP
@ -395,8 +395,8 @@ END
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,12,1697,0 FILEVERSION 3,12,1698,0
PRODUCTVERSION 3,12,1697,0 PRODUCTVERSION 3,12,1698,0
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -414,13 +414,13 @@ BEGIN
VALUE "Comments", "https://rufus.ie" VALUE "Comments", "https://rufus.ie"
VALUE "CompanyName", "Akeo Consulting" VALUE "CompanyName", "Akeo Consulting"
VALUE "FileDescription", "Rufus" VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "3.12.1697" VALUE "FileVersion", "3.12.1698"
VALUE "InternalName", "Rufus" VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2020 Pete Batard (GPL v3)" VALUE "LegalCopyright", "© 2011-2020 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html" VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html"
VALUE "OriginalFilename", "rufus-3.12.exe" VALUE "OriginalFilename", "rufus-3.12.exe"
VALUE "ProductName", "Rufus" VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "3.12.1697" VALUE "ProductVersion", "3.12.1698"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"