From c601aed054825654bd511d4b5c687d5bca6dd2c5 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Sat, 6 Jun 2020 22:19:20 +0100 Subject: [PATCH] [core] add option to write a small enough ISO to an ESP * This is mostly aimed at Debian 11 netinst on the Raspberry Pi 4 * Only available for regular UEFI ISOs if GPT and FAT are selected (no MBR ESPs). * Also fix a MinGW warning in GetUnusedDriveLetter() --- res/loc/ChangeLog.txt | 2 ++ res/loc/rufus.loc | 4 ++++ src/drive.c | 35 ++++++++++++++++++++-------------- src/format.c | 4 ++-- src/rufus.c | 44 +++++++++++++++++++++++++++++++++++-------- src/rufus.h | 2 ++ src/rufus.rc | 10 +++++----- 7 files changed, 72 insertions(+), 29 deletions(-) diff --git a/res/loc/ChangeLog.txt b/res/loc/ChangeLog.txt index 1fc49ffb..853cb4c5 100644 --- a/res/loc/ChangeLog.txt +++ b/res/loc/ChangeLog.txt @@ -9,6 +9,8 @@ o v3.* - *UPDATED* MSG_068 "Error while partitioning drive." -> "Could not partition drive." You can test this new message with - - *NEW* MSG_308 "VHD detection" + // TODO: Add a test ISO for this. + - *NEW* MSG_310 "The ISO you have selected uses UEFI and is small enough to be written as (...)" o v3.5 (2019.03.12) The following 3 messages can be tested by creating a UEFI:NTFS drive in Rufus ('Show advanced drive properties' must be enabled diff --git a/res/loc/rufus.loc b/res/loc/rufus.loc index a2e62c31..39a826ff 100644 --- a/res/loc/rufus.loc +++ b/res/loc/rufus.loc @@ -566,6 +566,10 @@ t MSG_305 "Use this option to indicate whether you want to use this device to in t MSG_306 "Fast-zeroing drive: %s" t MSG_307 "This may take a while" t MSG_308 "VHD detection" +t MSG_309 "ISO → ESP" +t MSG_310 "The ISO you have selected uses UEFI and is small enough to be written as an EFI System Partition (ESP). " + "Writing to an ESP, instead of writing to a generic data partition occupying the whole disk, can be preferable " + "for some types of installations.\n\nPlease select the mode that you want to use to write this image:" ######################################################################### l "ar-SA" "Arabic (العربية)" 0x0401, 0x0801, 0x0c01, 0x1001, 0x1401, 0x1801, 0x1c01, 0x2001, 0x2401, 0x2801, 0x2c01, 0x3001, 0x3401, 0x3801, 0x3c01, 0x4001 diff --git a/src/drive.c b/src/drive.c index db94c7bd..88dddcb8 100644 --- a/src/drive.c +++ b/src/drive.c @@ -71,7 +71,7 @@ PF_TYPE_DECL(NTAPI, NTSTATUS, NtQueryVolumeInformationFile, (HANDLE, PIO_STATUS_ * Globals */ RUFUS_DRIVE_INFO SelectedDrive; -extern BOOL installed_uefi_ntfs; +extern BOOL installed_uefi_ntfs, write_as_esp; uint64_t partition_offset[3]; uint64_t persistence_size = 0; @@ -913,7 +913,7 @@ UINT GetDriveTypeFromIndex(DWORD DriveIndex) } /* - * Return the next unused drive letter from the system + * Return the next unused drive letter from the system or NUL on error. */ char GetUnusedDriveLetter(void) { @@ -923,11 +923,11 @@ char GetUnusedDriveLetter(void) size = GetLogicalDriveStringsA(sizeof(drives), drives); if (size == 0) { uprintf("GetLogicalDriveStrings failed: %s", WindowsErrorString()); - goto out; + return 0; } if (size > sizeof(drives)) { uprintf("GetLogicalDriveStrings: Buffer too small (required %d vs. %d)", size, sizeof(drives)); - goto out; + return 0; } for (drive_letter = 'C'; drive_letter <= 'Z'; drive_letter++) { @@ -941,7 +941,6 @@ char GetUnusedDriveLetter(void) break; } -out: return (drive_letter > 'Z') ? 0 : drive_letter; } @@ -1685,7 +1684,8 @@ static BOOL ClearPartition(HANDLE hDrive, LARGE_INTEGER offset, DWORD size) BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL mbr_uefi_marker, uint8_t extra_partitions) { const char* PartitionTypeName[] = { "MBR", "GPT", "SFD" }; - const wchar_t *extra_part_name = L"", *main_part_name = L"Main Data Partition"; + const wchar_t *extra_part_name = L"", *main_part_name = write_as_esp ? L"EFI System Partition" : L"Main Data Partition"; + const LONGLONG main_part_size = write_as_esp ? MAX_ISO_TO_ESP_SIZE * MB : SelectedDrive.DiskSize; const LONGLONG bytes_per_track = ((LONGLONG)SelectedDrive.SectorsPerTrack) * SelectedDrive.SectorSize; const DWORD size_to_clear = MAX_SECTORS_TO_CLEAR * SelectedDrive.SectorSize; uint8_t* buffer; @@ -1694,7 +1694,7 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m DRIVE_LAYOUT_INFORMATION_EX4 DriveLayoutEx = {0}; BOOL r; DWORD i, size, bufsize, pn = 0; - LONGLONG main_part_size_in_sectors, extra_part_size_in_tracks = 0, ms_esp_size; + LONGLONG main_part_size_in_sectors, extra_part_size_in_tracks = 0, esp_size; PrintInfoDebug(0, MSG_238, PartitionTypeName[partition_style]); @@ -1743,9 +1743,16 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m } // Set our main data partition - main_part_size_in_sectors = (SelectedDrive.DiskSize - DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart) / - // Need 33 sectors at the end for secondary GPT - SelectedDrive.SectorSize - ((partition_style == PARTITION_STYLE_GPT)?33:0); + if (write_as_esp) { + // Align ESP to 64 MB while leaving at least 32 MB free space + esp_size = (((img_report.projected_size / MB) + 96) / 64) * 64 * MB; + main_part_size_in_sectors = (esp_size - DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart) / + SelectedDrive.SectorSize; + } else { + main_part_size_in_sectors = (main_part_size - DriveLayoutEx.PartitionEntry[pn].StartingOffset.QuadPart) / + // Need 33 sectors at the end for secondary GPT + 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_ESP) { @@ -1756,10 +1763,10 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m // 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; + 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; + 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; } 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; @@ -1812,7 +1819,7 @@ BOOL CreatePartition(HANDLE hDrive, int partition_style, int file_system, BOOL m return FALSE; } } else { - DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = PARTITION_MICROSOFT_DATA; + DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionType = write_as_esp ? PARTITION_GENERIC_ESP : PARTITION_MICROSOFT_DATA; IGNORE_RETVAL(CoCreateGuid(&DriveLayoutEx.PartitionEntry[pn].Gpt.PartitionId)); wcsncpy(DriveLayoutEx.PartitionEntry[pn].Gpt.Name, main_part_name, ARRAYSIZE(DriveLayoutEx.PartitionEntry[pn].Gpt.Name)); } diff --git a/src/format.c b/src/format.c index e8835956..3ae188da 100644 --- a/src/format.c +++ b/src/format.c @@ -66,7 +66,7 @@ extern uint32_t dur_mins, dur_secs; extern uint32_t wim_nb_files, wim_proc_files, wim_extra_files; static int actual_fs_type, wintogo_index = -1, wininst_index = 0; extern BOOL force_large_fat32, enable_ntfs_compression, lock_drive, zero_drive, fast_zeroing, enable_file_indexing, write_as_image; -extern BOOL use_vds; +extern BOOL use_vds, write_as_esp; uint8_t *grub2_buf = NULL, *sec_buf = NULL; long grub2_len; @@ -1997,7 +1997,7 @@ DWORD WINAPI FormatThread(void* param) if (fs_type < FS_EXT2) ToValidLabel(label, (fs_type == FS_FAT16) || (fs_type == FS_FAT32) || (fs_type == FS_EXFAT)); ClusterSize = (DWORD)ComboBox_GetItemData(hClusterSize, ComboBox_GetCurSel(hClusterSize)); - if (ClusterSize < 0x200) + if ((ClusterSize < 0x200) || (write_as_esp)) ClusterSize = 0; // 0 = default cluster size Flags = FP_FORCE; diff --git a/src/rufus.c b/src/rufus.c index 257cf1b1..ea5296ca 100755 --- a/src/rufus.c +++ b/src/rufus.c @@ -116,7 +116,7 @@ BOOL enable_HDDs = FALSE, enable_VHDs = TRUE, enable_ntfs_compression = FALSE, n BOOL advanced_mode_device, advanced_mode_format, allow_dual_uefi_bios, detect_fakes, enable_vmdk, force_large_fat32, usb_debug; BOOL use_fake_units, preserve_timestamps = FALSE, fast_zeroing = FALSE, app_changed_size = FALSE; BOOL zero_drive = FALSE, list_non_usb_removable_drives = FALSE, enable_file_indexing, large_drive = FALSE; -BOOL write_as_image = FALSE, installed_uefi_ntfs = FALSE, enable_fido = FALSE, use_vds = FALSE; +BOOL write_as_image = FALSE, write_as_esp = FALSE, installed_uefi_ntfs = FALSE, enable_fido = FALSE, use_vds = FALSE; float fScale = 1.0f; int dialog_showing = 0, selection_default = BT_IMAGE, windows_to_go_selection = 0, persistence_unit_selection = -1; int default_fs, fs_type, boot_type, partition_type, target_type; // file system, boot type, partition type, target type @@ -2287,6 +2287,7 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA target_type = (int)ComboBox_GetItemData(hTargetSystem, ComboBox_GetCurSel(hTargetSystem)); fs_type = (int)ComboBox_GetItemData(hFileSystem, ComboBox_GetCurSel(hFileSystem)); write_as_image = FALSE; + write_as_esp = FALSE; installed_uefi_ntfs = FALSE; // Disable all controls except Cancel EnableControls(FALSE, FALSE); @@ -2681,17 +2682,44 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA if (persistence_size == 0) { char* iso_image = lmprintf(MSG_036); char* dd_image = lmprintf(MSG_095); - char* choices[2] = { lmprintf(MSG_276, iso_image), lmprintf(MSG_277, dd_image) }; - i = SelectionDialog(lmprintf(MSG_274), lmprintf(MSG_275, iso_image, dd_image, iso_image, dd_image), - choices, 2); - if (i < 0) // Cancel - goto aborted_start; - else if (i == 2) - write_as_image = TRUE; + char* esp_image = lmprintf(MSG_309); + // If the ISO is small enough to be written as an ESP and we are using GPT add the ISO → ESP option + if ((img_report.projected_size < MAX_ISO_TO_ESP_SIZE * MB) && HAS_REGULAR_EFI(img_report) && + (partition_type == PARTITION_STYLE_GPT) && IS_FAT(fs_type)) { + char* choices[3] = { lmprintf(MSG_276, iso_image), lmprintf(MSG_277, esp_image), lmprintf(MSG_277, dd_image) }; + i = SelectionDialog(lmprintf(MSG_274), lmprintf(MSG_275, iso_image, dd_image, iso_image, dd_image), + choices, 3); + if (i < 0) // Cancel + goto aborted_start; + else if (i == 2) + write_as_esp = TRUE; + else if (i == 3) + write_as_image = TRUE; + } else { + char* choices[2] = { lmprintf(MSG_276, iso_image), lmprintf(MSG_277, dd_image) }; + i = SelectionDialog(lmprintf(MSG_274), lmprintf(MSG_275, iso_image, dd_image, iso_image, dd_image), + choices, 2); + if (i < 0) // Cancel + goto aborted_start; + else if (i == 2) + write_as_image = TRUE; + } } } else { write_as_image = TRUE; } + } else if ((img_report.projected_size < MAX_ISO_TO_ESP_SIZE * MB) && HAS_REGULAR_EFI(img_report) && + (partition_type == PARTITION_STYLE_GPT) && IS_FAT(fs_type)) { + // The ISO is small enough to be written as an ESP and we are using GPT + // so ask the users if they want to write it as an ESP. + char* iso_image = lmprintf(MSG_036); + char* esp_image = lmprintf(MSG_309); + char* choices[2] = { lmprintf(MSG_276, iso_image), lmprintf(MSG_277, esp_image) }; + i = SelectionDialog(lmprintf(MSG_274), lmprintf(MSG_310), choices, 2); + if (i < 0) // Cancel + goto aborted_start; + else if (i == 2) + write_as_esp = TRUE; } } diff --git a/src/rufus.h b/src/rufus.h index 4cbe2851..baa08b91 100644 --- a/src/rufus.h +++ b/src/rufus.h @@ -72,6 +72,7 @@ #define MAX_GUID_STRING_LENGTH 40 #define MAX_PARTITIONS 16 // Maximum number of partitions we handle #define MAX_ESP_TOGGLE 8 // Maximum number of entries we record to toggle GPT ESP back and forth +#define MAX_ISO_TO_ESP_SIZE 512 // Maximum size we allow for the ISO → ESP option (in MB) #define MAX_SECTORS_TO_CLEAR 128 // nb sectors to zap when clearing the MBR/GPT (must be >34) #define MAX_WININST 4 // Max number of install[.wim|.esd] we can handle on an image #define MBR_UEFI_MARKER 0x49464555 // 'U', 'E', 'F', 'I', as a 32 bit little endian longword @@ -303,6 +304,7 @@ enum checksum_type { #define HAS_BOOTMGR_BIOS(r) (r.has_bootmgr) #define HAS_BOOTMGR_EFI(r) (r.has_bootmgr_efi) #define HAS_BOOTMGR(r) (HAS_BOOTMGR_BIOS(r) || HAS_BOOTMGR_EFI(r)) +#define HAS_REGULAR_EFI(r) (r.has_efi & 0x7E) #define HAS_WININST(r) (r.wininst_index != 0) #define HAS_WINPE(r) (((r.winpe & WINPE_I386) == WINPE_I386)||((r.winpe & WINPE_AMD64) == WINPE_AMD64)||((r.winpe & WINPE_MININT) == WINPE_MININT)) #define HAS_WINDOWS(r) (HAS_BOOTMGR(r) || (r.uses_minint) || HAS_WINPE(r)) diff --git a/src/rufus.rc b/src/rufus.rc index 855d6540..c630a5fc 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.11.1668" +CAPTION "Rufus 3.11.1669" 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,11,1668,0 - PRODUCTVERSION 3,11,1668,0 + FILEVERSION 3,11,1669,0 + PRODUCTVERSION 3,11,1669,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.11.1668" + VALUE "FileVersion", "3.11.1669" 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.11.exe" VALUE "ProductName", "Rufus" - VALUE "ProductVersion", "3.11.1668" + VALUE "ProductVersion", "3.11.1669" END END BLOCK "VarFileInfo"