From 2dd538b7cbbe82039e3257f81d5a633e25ae1da2 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Mon, 13 Apr 2020 17:05:33 +0100 Subject: [PATCH] [core] fix MBR protective message overwriting the primary GPT * We distractedly chose to populate the message from our protective MBR for GPT/UEFI-only boot media into the 4KB that directly followed the MBR, which of course is space that is being used by the primary GPT. * This resulted on systems having to fall back to using the secondary GPT, which not all appear to be designed to do. * Alter the code to ensure the protective message is written at LBA 34, after the primary GPT. * Closes #1507 --- res/mbr/Makefile | 6 ++- res/mbr/mbr.bin | Bin 512 -> 0 bytes res/mbr/msg.S | 8 ++-- res/mbr/msg.bin | Bin 512 -> 512 bytes res/mbr/msg.txt | 4 +- res/mbr/readme.txt | 3 +- src/format.c | 21 ++++++----- src/ms-sys/inc/mbr_msg_rufus.h | 65 ++++++++++++++++----------------- src/rufus.rc | 10 ++--- 9 files changed, 60 insertions(+), 57 deletions(-) delete mode 100644 res/mbr/mbr.bin diff --git a/res/mbr/Makefile b/res/mbr/Makefile index 35a5dd73..982c15ff 100644 --- a/res/mbr/Makefile +++ b/res/mbr/Makefile @@ -53,9 +53,11 @@ dis: $(TEST_TARGET).out # Run the MBR in a Bochs environment (append msg.txt in subsequent blocks) test: $(TEST_TARGET).bin @test -s $(BOCHS) || { echo "Error: $(BOCHS) was not found on this system"; exit 1; } - @cat $(TEST_TARGET).bin msg.txt > disk.img + @cp $(TEST_TARGET).bin disk.img + @truncate -c -s 17K disk.img + @cat msg.txt >> disk.img @truncate -c -s 10M disk.img - -@$(BOCHS) -f bochsrc.bxrc -q + @-$(BOCHS) -f bochsrc.bxrc -q %.out: %.o mbr.ld @echo "[LD] $@" diff --git a/res/mbr/mbr.bin b/res/mbr/mbr.bin deleted file mode 100644 index 2e433d1a01386eb81581964a7d8da1a5d77be48e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 512 zcmZ?tcJ=>bc;Hvxg*^;4zdN6GK9^(byRloCrJLQOll|ZWHr)g4eFt_jFnwMUkYP}B zy6;As;qAV|j=LHeUUV`P&4F%_0liw3*NwG8##`?l1+*vxQ@ZE~$pgWaTBhSHlrBagA| zt8L6^VAxeuyRWYHMJhuSyZkGL8U}_JxePBV7=ZS5iwA9L==Nty>yGDm!OBp4m0kXH z!WPHft3uBT7wI(q4qa8M_Wd9uLy1D7T$aXKqJzxjy3 zJo&?kyBTVBFfyGLKK!d&MlG$IJ*|^Pty5gB$Dir<;a|HI7}6Hz)vBd+vj08|@<0jO z7KXE8kG>pUW5ZCwxrJ{xJHuIl*Pjjt=q=b++wiNQl%?a)Yc`8RrA(U|A`bu31AD53 zOYa}wxAd20Q5ibx{X|t^xo{lfFy< diff --git a/res/mbr/msg.S b/res/mbr/msg.S index dd94c487..cc8ba508 100644 --- a/res/mbr/msg.S +++ b/res/mbr/msg.S @@ -31,6 +31,7 @@ /********************************************************************************/ MBR_ADDR = 0x7c00 MBR_RESERVED = 0x1b8 # Start of the reserved section (partition table, etc.) +MSG_SECTOR = 0x22 # First sector of the message (must be after the GPT) NB_SECTORS = 0x08 # Number of sectors to read PT_MAX = 0x04 # Number of partition entries in the partition table PT_ENTRY_SIZE = 0x10 # Size of a partition entry in the partition table @@ -109,10 +110,9 @@ read_sectors: # Copy the next sectors into RAM ext: # http://en.wikipedia.org/wiki/INT_13H#INT_13h_AH.3D42h:_Extended_Read_Sectors_From_Drive xor eax, eax push eax # bits 32-63 of sector address - inc eax - push eax # bits 0-31 of sector address + push MSG_SECTOR # bits 0-31 of sector address push es # destination segment - push 0x0000 # destination address + push eax # destination address (0) push NB_SECTORS # number of sectors to be read push 0x0010 # size of DAP struct mov si, sp # DAP address (= stack) @@ -125,7 +125,7 @@ ext: # http://en.wikipedia.org/wiki/INT_13H#INT_13h_AH.3D42h:_Extended_Read_Sect no_ext: # http://en.wikipedia.org/wiki/INT_13H#INT_13h_AH.3D02h:_Read_Sectors_From_Drive mov ax, 0x0200 + NB_SECTORS - mov cx, 0x0002 # Sector address (starts at 1) + mov cx, (MSG_SECTOR + 1) # Sector address (starts at 1) mov dx, 0x0080 # Drive ID xor bx, bx # Destination address in ES int 0x13 diff --git a/res/mbr/msg.bin b/res/mbr/msg.bin index b350c71f0fa165bad91643333d8a79cfafb61f0c..bd005b02d5b5e8185d58eab461d35054f29fcd4e 100644 GIT binary patch delta 76 zcmZo*X<(V)psU{aJ9Jg4;`f7$3?1V@%Y=d&$NXNBiC9}$@Mns)~W g(@y1y`I6k?wJ*#W3=j0(*q6L \09MBR\07. * \09Target system\07 -> \09BIOS (...)\07 diff --git a/res/mbr/readme.txt b/res/mbr/readme.txt index 9d752947..ebfe6211 100644 --- a/res/mbr/readme.txt +++ b/res/mbr/readme.txt @@ -15,7 +15,8 @@ the partition table lists the disk ID for the first partition as 0x81, then it will be swapped for 0x80. An additional MBR (msg.S) also exists in this directory, that can be used to -display an ASCII message contained in the sectors directly following the MBR. +display an ASCII message contained in the sectors following the primary GPT +(LBA sectors 34 and later). This can be used, for instance, to display a notice for media that cannot be booted in BIOS/Legacy mode. diff --git a/src/format.c b/src/format.c index bf17a11a..38cc2204 100644 --- a/src/format.c +++ b/src/format.c @@ -906,7 +906,7 @@ out: static BOOL WriteSBR(HANDLE hPhysicalDrive) { // TODO: Do we need anything special for 4K sectors? - DWORD size, max_size, mbr_size = 0x200; + DWORD size, max_size, br_size = 0x200; int r, sub_type = boot_type; unsigned char* buf = NULL; FAKE_FD fake_fd = { 0 }; @@ -914,10 +914,6 @@ static BOOL WriteSBR(HANDLE hPhysicalDrive) fake_fd._handle = (char*)hPhysicalDrive; set_bytes_per_sector(SelectedDrive.SectorSize); - // Ensure that we have sufficient space for the SBR - max_size = IsChecked(IDC_OLD_BIOS_FIXES) ? - (DWORD)(SelectedDrive.SectorsPerTrack * SelectedDrive.SectorSize) : 1 * MB; - max_size -= mbr_size; // Syslinux has precedence over Grub if ((boot_type == BT_IMAGE) && (!HAS_SYSLINUX(img_report))) { if (img_report.has_grub4dos) @@ -932,12 +928,12 @@ static BOOL WriteSBR(HANDLE hPhysicalDrive) case BT_GRUB4DOS: uprintf("Writing Grub4Dos SBR"); buf = GetResource(hMainInstance, MAKEINTRESOURCEA(IDR_GR_GRUB_GRLDR_MBR), _RT_RCDATA, "grldr.mbr", &size, FALSE); - if ((buf == NULL) || (size <= mbr_size)) { + if ((buf == NULL) || (size <= br_size)) { uprintf("grldr.mbr is either not present or too small"); return FALSE; } - buf = &buf[mbr_size]; - size -= mbr_size; + buf = &buf[br_size]; + size -= br_size; break; case BT_GRUB2: if (grub2_buf != NULL) { @@ -957,6 +953,7 @@ static BOOL WriteSBR(HANDLE hPhysicalDrive) case BT_NON_BOOTABLE: uprintf("Writing protective message SBR"); size = 4 * KB; + br_size = 17 * KB; // 34 sectors are reserved for protective MBR + primary GPT buf = GetResource(hMainInstance, MAKEINTRESOURCEA(IDR_SBR_MSG), _RT_RCDATA, "msg.txt", &size, TRUE); if (buf == NULL) { uprintf("Could not access message"); @@ -968,11 +965,15 @@ static BOOL WriteSBR(HANDLE hPhysicalDrive) return TRUE; } - if (size > max_size) { + // Ensure that we have sufficient space for the SBR + max_size = IsChecked(IDC_OLD_BIOS_FIXES) ? + (DWORD)(SelectedDrive.SectorsPerTrack * SelectedDrive.SectorSize) : 1 * MB; + if (br_size + size > max_size) { uprintf(" SBR size is too large - You may need to uncheck 'Add fixes for old BIOSes'."); return FALSE; } - r = write_data(fp, mbr_size, buf, (uint64_t)size); + + r = write_data(fp, br_size, buf, (uint64_t)size); safe_free(grub2_buf); if (sub_type == BT_NON_BOOTABLE) safe_free(buf); diff --git a/src/ms-sys/inc/mbr_msg_rufus.h b/src/ms-sys/inc/mbr_msg_rufus.h index 3c5948cd..eb8fa6db 100644 --- a/src/ms-sys/inc/mbr_msg_rufus.h +++ b/src/ms-sys/inc/mbr_msg_rufus.h @@ -1,6 +1,6 @@ /* * Rufus message MBR - Displays an ASCII text message contained in the - * next 4 KB of sectors that follow. + * 4 KB of sectors starting at LBA 34 (i.e. after the primary GPT if any). * See https://github.com/pbatard/rufus/tree/master/res/mbr * Copyright © 2019-2020 Pete Batard */ @@ -11,36 +11,35 @@ unsigned char mbr_msg_rufus_0x0[] = { 0x89, 0x07, 0xC1, 0xE0, 0x06, 0x8E, 0xC0, 0xB7, 0x07, 0xB8, 0x7F, 0x00, 0xCD, 0x10, 0x31, 0xDB, 0x31, 0xC9, 0xBA, 0x4F, 0x18, 0xB8, 0x00, 0x06, 0xCD, 0x10, 0x31, 0xD2, 0xB4, 0x02, 0xCD, 0x10, 0xB4, 0x41, 0xBB, 0xAA, - 0x55, 0xCD, 0x13, 0x72, 0x29, 0x81, 0xFB, 0x55, 0xAA, 0x75, 0x23, 0xF7, - 0xC1, 0x01, 0x00, 0x74, 0x1D, 0x66, 0x31, 0xC0, 0x66, 0x50, 0x66, 0x40, - 0x66, 0x50, 0x06, 0x6A, 0x00, 0x6A, 0x08, 0x6A, 0x10, 0x89, 0xE6, 0xB4, - 0x42, 0xCD, 0x13, 0x9F, 0x83, 0xC4, 0x10, 0x9E, 0xEB, 0x0D, 0xB8, 0x08, - 0x02, 0xB9, 0x02, 0x00, 0xBA, 0x80, 0x00, 0x31, 0xDB, 0xCD, 0x13, 0xBB, - 0x07, 0x00, 0x72, 0x0B, 0x31, 0xF6, 0x8C, 0xC0, 0x8E, 0xD8, 0xE8, 0x3F, - 0x00, 0xEB, 0x06, 0xBE, 0x19, 0x7D, 0xE8, 0x37, 0x00, 0x31, 0xC0, 0x8E, - 0xD8, 0xBE, 0x65, 0x7D, 0xE8, 0x2D, 0x00, 0xE8, 0x1D, 0x00, 0xB4, 0x01, - 0xCD, 0x16, 0x75, 0x08, 0xB4, 0x02, 0xCD, 0x16, 0x24, 0x04, 0x74, 0xF2, - 0x31, 0xC0, 0x8E, 0xD8, 0xB8, 0x34, 0x12, 0xA3, 0x73, 0x04, 0xEA, 0x00, - 0x00, 0xFF, 0xFF, 0xB4, 0x01, 0xCD, 0x16, 0x74, 0x06, 0xB4, 0x00, 0xCD, - 0x16, 0xE2, 0xF4, 0xC3, 0xAC, 0x3C, 0x00, 0x74, 0x4F, 0x3C, 0x0D, 0x74, - 0xF7, 0x3C, 0x0A, 0x75, 0x10, 0x53, 0xB4, 0x03, 0xCD, 0x10, 0xFE, 0xC6, - 0xB2, 0x00, 0xB4, 0x02, 0xCD, 0x10, 0x5B, 0xEB, 0xE3, 0x3C, 0x5C, 0x75, - 0x1C, 0xB1, 0x02, 0xAC, 0x3C, 0x46, 0x7F, 0xD8, 0x2C, 0x30, 0x3C, 0x09, - 0x7E, 0x02, 0x2C, 0x07, 0xC0, 0xE3, 0x04, 0x24, 0x0F, 0x08, 0xC3, 0xFE, - 0xC9, 0x75, 0xE8, 0xEB, 0xC3, 0xB9, 0x01, 0x00, 0xB4, 0x09, 0xCD, 0x10, - 0x53, 0x31, 0xDB, 0xB4, 0x03, 0xCD, 0x10, 0xFE, 0xC2, 0xB4, 0x02, 0xCD, - 0x10, 0x5B, 0xEB, 0xAC, 0xC3, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, - 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x5C, 0x30, 0x34, 0x2A, - 0x2A, 0x2A, 0x20, 0x45, 0x52, 0x52, 0x4F, 0x52, 0x3A, 0x20, 0x54, 0x48, - 0x49, 0x53, 0x20, 0x4D, 0x45, 0x44, 0x49, 0x41, 0x20, 0x43, 0x41, 0x4E, - 0x4E, 0x4F, 0x54, 0x20, 0x42, 0x4F, 0x4F, 0x54, 0x20, 0x49, 0x4E, 0x20, - 0x4C, 0x45, 0x47, 0x41, 0x43, 0x59, 0x20, 0x4D, 0x4F, 0x44, 0x45, 0x20, - 0x2A, 0x2A, 0x2A, 0x5C, 0x30, 0x37, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x0D, - 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, - 0x20, 0x20, 0x5C, 0x37, 0x30, 0x50, 0x6C, 0x65, 0x61, 0x73, 0x65, 0x20, - 0x72, 0x65, 0x6D, 0x6F, 0x76, 0x65, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, - 0x6D, 0x65, 0x64, 0x69, 0x61, 0x20, 0x61, 0x6E, 0x64, 0x20, 0x70, 0x72, - 0x65, 0x73, 0x73, 0x20, 0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20, - 0x74, 0x6F, 0x20, 0x72, 0x65, 0x62, 0x6F, 0x6F, 0x74, 0x5C, 0x30, 0x37, - 0x00, + 0x55, 0xCD, 0x13, 0x72, 0x27, 0x81, 0xFB, 0x55, 0xAA, 0x75, 0x21, 0xF7, + 0xC1, 0x01, 0x00, 0x74, 0x1B, 0x66, 0x31, 0xC0, 0x66, 0x50, 0x6A, 0x22, + 0x06, 0x66, 0x50, 0x6A, 0x08, 0x6A, 0x10, 0x89, 0xE6, 0xB4, 0x42, 0xCD, + 0x13, 0x9F, 0x83, 0xC4, 0x10, 0x9E, 0xEB, 0x0D, 0xB8, 0x08, 0x02, 0xB9, + 0x23, 0x00, 0xBA, 0x80, 0x00, 0x31, 0xDB, 0xCD, 0x13, 0xBB, 0x07, 0x00, + 0x72, 0x0B, 0x31, 0xF6, 0x8C, 0xC0, 0x8E, 0xD8, 0xE8, 0x3F, 0x00, 0xEB, + 0x06, 0xBE, 0x17, 0x7D, 0xE8, 0x37, 0x00, 0x31, 0xC0, 0x8E, 0xD8, 0xBE, + 0x63, 0x7D, 0xE8, 0x2D, 0x00, 0xE8, 0x1D, 0x00, 0xB4, 0x01, 0xCD, 0x16, + 0x75, 0x08, 0xB4, 0x02, 0xCD, 0x16, 0x24, 0x04, 0x74, 0xF2, 0x31, 0xC0, + 0x8E, 0xD8, 0xB8, 0x34, 0x12, 0xA3, 0x73, 0x04, 0xEA, 0x00, 0x00, 0xFF, + 0xFF, 0xB4, 0x01, 0xCD, 0x16, 0x74, 0x06, 0xB4, 0x00, 0xCD, 0x16, 0xE2, + 0xF4, 0xC3, 0xAC, 0x3C, 0x00, 0x74, 0x4F, 0x3C, 0x0D, 0x74, 0xF7, 0x3C, + 0x0A, 0x75, 0x10, 0x53, 0xB4, 0x03, 0xCD, 0x10, 0xFE, 0xC6, 0xB2, 0x00, + 0xB4, 0x02, 0xCD, 0x10, 0x5B, 0xEB, 0xE3, 0x3C, 0x5C, 0x75, 0x1C, 0xB1, + 0x02, 0xAC, 0x3C, 0x46, 0x7F, 0xD8, 0x2C, 0x30, 0x3C, 0x09, 0x7E, 0x02, + 0x2C, 0x07, 0xC0, 0xE3, 0x04, 0x24, 0x0F, 0x08, 0xC3, 0xFE, 0xC9, 0x75, + 0xE8, 0xEB, 0xC3, 0xB9, 0x01, 0x00, 0xB4, 0x09, 0xCD, 0x10, 0x53, 0x31, + 0xDB, 0xB4, 0x03, 0xCD, 0x10, 0xFE, 0xC2, 0xB4, 0x02, 0xCD, 0x10, 0x5B, + 0xEB, 0xAC, 0xC3, 0x0D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x5C, 0x30, 0x34, 0x2A, 0x2A, 0x2A, + 0x20, 0x45, 0x52, 0x52, 0x4F, 0x52, 0x3A, 0x20, 0x54, 0x48, 0x49, 0x53, + 0x20, 0x4D, 0x45, 0x44, 0x49, 0x41, 0x20, 0x43, 0x41, 0x4E, 0x4E, 0x4F, + 0x54, 0x20, 0x42, 0x4F, 0x4F, 0x54, 0x20, 0x49, 0x4E, 0x20, 0x4C, 0x45, + 0x47, 0x41, 0x43, 0x59, 0x20, 0x4D, 0x4F, 0x44, 0x45, 0x20, 0x2A, 0x2A, + 0x2A, 0x5C, 0x30, 0x37, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x0D, 0x0A, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x5C, 0x37, 0x30, 0x50, 0x6C, 0x65, 0x61, 0x73, 0x65, 0x20, 0x72, 0x65, + 0x6D, 0x6F, 0x76, 0x65, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x6D, 0x65, + 0x64, 0x69, 0x61, 0x20, 0x61, 0x6E, 0x64, 0x20, 0x70, 0x72, 0x65, 0x73, + 0x73, 0x20, 0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x74, 0x6F, + 0x20, 0x72, 0x65, 0x62, 0x6F, 0x6F, 0x74, 0x5C, 0x30, 0x37, 0x00, 0x00, }; diff --git a/src/rufus.rc b/src/rufus.rc index edaf2b36..57a3cca6 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.10.1639" +CAPTION "Rufus 3.10.1640" 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,10,1639,0 - PRODUCTVERSION 3,10,1639,0 + FILEVERSION 3,10,1640,0 + PRODUCTVERSION 3,10,1640,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.10.1639" + VALUE "FileVersion", "3.10.1640" 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.10.exe" VALUE "ProductName", "Rufus" - VALUE "ProductVersion", "3.10.1639" + VALUE "ProductVersion", "3.10.1640" END END BLOCK "VarFileInfo"