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

[misc] improve ToggleEsp() call to take an offset

* Also fix a MinGW warning
This commit is contained in:
Pete Batard 2020-08-15 14:57:48 +01:00
parent ba406843f4
commit 3f0d574657
No known key found for this signature in database
GPG key ID: 38E0CF5E69EDD671
4 changed files with 69 additions and 54 deletions

View file

@ -73,6 +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;
uint64_t partition_offset[3]; uint64_t partition_offset[3];
uint64_t persistence_size = 0; uint64_t persistence_size = 0;
@ -1198,7 +1199,7 @@ static BOOL ClearEspInfo(uint8_t index)
* Needed because Windows 10 doesn't mount ESPs by default, and also * Needed because Windows 10 doesn't mount ESPs by default, and also
* doesn't let usermode apps (such as File Explorer) access mounted ESPs. * doesn't let usermode apps (such as File Explorer) access mounted ESPs.
*/ */
BOOL ToggleEsp(DWORD DriveIndex) BOOL ToggleEsp(DWORD DriveIndex, uint64_t PartitionOffset)
{ {
char *volume_name, mount_point[] = DEFAULT_ESP_MOUNT_POINT; char *volume_name, mount_point[] = DEFAULT_ESP_MOUNT_POINT;
BOOL r, ret = FALSE, found = FALSE; BOOL r, ret = FALSE, found = FALSE;
@ -1208,7 +1209,7 @@ BOOL ToggleEsp(DWORD DriveIndex)
GUID* guid; GUID* guid;
PDRIVE_LAYOUT_INFORMATION_EX DriveLayout = (PDRIVE_LAYOUT_INFORMATION_EX)(void*)layout; PDRIVE_LAYOUT_INFORMATION_EX DriveLayout = (PDRIVE_LAYOUT_INFORMATION_EX)(void*)layout;
if (nWindowsVersion < WINDOWS_10) { if ((PartitionOffset == 0) && (nWindowsVersion < WINDOWS_10)) {
uprintf("ESP toggling is only available for Windows 10 or later"); uprintf("ESP toggling is only available for Windows 10 or later");
return FALSE; return FALSE;
} }
@ -1223,11 +1224,13 @@ BOOL ToggleEsp(DWORD DriveIndex)
uprintf("Could not get layout for drive 0x%02x: %s", DriveIndex, WindowsErrorString()); uprintf("Could not get layout for drive 0x%02x: %s", DriveIndex, WindowsErrorString());
goto out; goto out;
} }
// TODO: Handle MBR
if (DriveLayout->PartitionStyle != PARTITION_STYLE_GPT) { if (DriveLayout->PartitionStyle != PARTITION_STYLE_GPT) {
uprintf("ESP toggling is only available for GPT drives"); uprintf("ESP toggling is only available for GPT drives");
goto out; goto out;
} }
if (PartitionOffset == 0) {
// See if the current drive contains an ESP // See if the current drive contains an ESP
for (i = 0, j = 0; i < DriveLayout->PartitionCount; i++) { for (i = 0, j = 0; i < DriveLayout->PartitionCount; i++) {
if (CompareGUID(&DriveLayout->PartitionEntry[i].Gpt.PartitionType, &PARTITION_GENERIC_ESP)) { if (CompareGUID(&DriveLayout->PartitionEntry[i].Gpt.PartitionType, &PARTITION_GENERIC_ESP)) {
@ -1268,6 +1271,16 @@ BOOL ToggleEsp(DWORD DriveIndex)
goto out; goto out;
DriveLayout->PartitionEntry[i].Gpt.PartitionType = PARTITION_GENERIC_ESP; DriveLayout->PartitionEntry[i].Gpt.PartitionType = PARTITION_GENERIC_ESP;
} }
} else {
for (i = 0, j = 0; i < DriveLayout->PartitionCount; i++) {
if (DriveLayout->PartitionEntry[i].StartingOffset.QuadPart == PartitionOffset)
DriveLayout->PartitionEntry[i].Gpt.PartitionType = PARTITION_GENERIC_ESP;
}
}
if (i >= DriveLayout->PartitionCount) {
uprintf("No partition to toggle");
goto out;
}
DriveLayout->PartitionEntry[i].RewritePartition = TRUE; // Just in case DriveLayout->PartitionEntry[i].RewritePartition = TRUE; // Just in case
r = DeviceIoControl(hPhysical, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, (BYTE*)DriveLayout, size, NULL, 0, &size, NULL); r = DeviceIoControl(hPhysical, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, (BYTE*)DriveLayout, size, NULL, 0, &size, NULL);
@ -1276,6 +1289,7 @@ BOOL ToggleEsp(DWORD DriveIndex)
goto out; goto out;
} }
RefreshDriveLayout(hPhysical); RefreshDriveLayout(hPhysical);
if (PartitionOffset == 0) {
if (CompareGUID(&DriveLayout->PartitionEntry[i].Gpt.PartitionType, &PARTITION_GENERIC_ESP)) { if (CompareGUID(&DriveLayout->PartitionEntry[i].Gpt.PartitionType, &PARTITION_GENERIC_ESP)) {
// We successfully reverted ESP from Basic Data -> Delete stored ESP info // We successfully reverted ESP from Basic Data -> Delete stored ESP info
ClearEspInfo((uint8_t)j); ClearEspInfo((uint8_t)j);
@ -1285,6 +1299,7 @@ BOOL ToggleEsp(DWORD DriveIndex)
IGNORE_RETVAL(MountVolume(mount_point, volume_name)); IGNORE_RETVAL(MountVolume(mount_point, volume_name));
free(volume_name); free(volume_name);
} }
}
ret = TRUE; ret = TRUE;
out: out:
@ -1521,8 +1536,8 @@ BOOL UnmountVolume(HANDLE hDrive)
BOOL MountVolume(char* drive_name, char *volume_name) BOOL MountVolume(char* drive_name, char *volume_name)
{ {
char mounted_guid[52]; char mounted_guid[52];
char mounted_letter[27] = { 0 };
#if defined(WINDOWS_IS_NOT_BUGGY) #if defined(WINDOWS_IS_NOT_BUGGY)
char mounted_letter[27] = { 0 };
DWORD size; DWORD size;
#endif #endif

View file

@ -400,4 +400,4 @@ BOOL CyclePort(int index);
int CycleDevice(int index); int CycleDevice(int index);
BOOL RefreshLayout(DWORD DriveIndex); BOOL RefreshLayout(DWORD DriveIndex);
BOOL GetOpticalMedia(IMG_SAVE* img_save); BOOL GetOpticalMedia(IMG_SAVE* img_save);
BOOL ToggleEsp(DWORD DriveIndex); BOOL ToggleEsp(DWORD DriveIndex, uint64_t PartitionOffset);

View file

@ -3640,7 +3640,7 @@ relaunch:
if ((msg.message == WM_SYSKEYDOWN) && (msg.wParam == 'P')) { if ((msg.message == WM_SYSKEYDOWN) && (msg.wParam == 'P')) {
int index = ComboBox_GetCurSel(hDeviceList); int index = ComboBox_GetCurSel(hDeviceList);
DWORD DeviceNum = (DWORD)ComboBox_GetItemData(hDeviceList, index); DWORD DeviceNum = (DWORD)ComboBox_GetItemData(hDeviceList, index);
if (ToggleEsp(DeviceNum)) if (ToggleEsp(DeviceNum, 0))
CyclePort(index); CyclePort(index);
continue; continue;
} }

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.1694" CAPTION "Rufus 3.12.1695"
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,1694,0 FILEVERSION 3,12,1695,0
PRODUCTVERSION 3,12,1694,0 PRODUCTVERSION 3,12,1695,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.1694" VALUE "FileVersion", "3.12.1695"
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.1694" VALUE "ProductVersion", "3.12.1695"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"