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

[core] fix write error when using non sector aligned DD images

* Closes #410
This commit is contained in:
Pete Batard 2014-12-03 21:28:24 +00:00
parent efd22d1fe3
commit ea28080e0a
3 changed files with 27 additions and 19 deletions

View file

@ -1272,7 +1272,8 @@ DWORD WINAPI FormatThread(void* param)
{ {
int i, r, pt, bt, fs, dt; int i, r, pt, bt, fs, dt;
BOOL s, ret, use_large_fat32; BOOL s, ret, use_large_fat32;
DWORD rSize, wSize, LastRefresh = 0, DriveIndex = (DWORD)(uintptr_t)param; const DWORD SectorSize = SelectedDrive.Geometry.BytesPerSector;
DWORD rSize, wSize, BufSize, LastRefresh = 0, DriveIndex = (DWORD)(uintptr_t)param;
HANDLE hPhysicalDrive = INVALID_HANDLE_VALUE; HANDLE hPhysicalDrive = INVALID_HANDLE_VALUE;
HANDLE hLogicalVolume = INVALID_HANDLE_VALUE; HANDLE hLogicalVolume = INVALID_HANDLE_VALUE;
HANDLE hSourceImage = INVALID_HANDLE_VALUE; HANDLE hSourceImage = INVALID_HANDLE_VALUE;
@ -1280,7 +1281,7 @@ DWORD WINAPI FormatThread(void* param)
FILE* log_fd; FILE* log_fd;
LARGE_INTEGER li; LARGE_INTEGER li;
uint64_t wb; uint64_t wb;
uint8_t *buffer = NULL; uint8_t *buffer = NULL, *aligned_buffer;
char *bb_msg, *guid_volume = NULL; char *bb_msg, *guid_volume = NULL;
char drive_name[] = "?:\\"; char drive_name[] = "?:\\";
char drive_letters[27]; char drive_letters[27];
@ -1393,12 +1394,12 @@ DWORD WINAPI FormatThread(void* param)
fflush(log_fd); fflush(log_fd);
} }
if (!BadBlocks(hPhysicalDrive, SelectedDrive.DiskSize, if (!BadBlocks(hPhysicalDrive, SelectedDrive.DiskSize, SectorSize,
SelectedDrive.Geometry.BytesPerSector, ComboBox_GetCurSel(hNBPasses)+1, &report, log_fd)) { ComboBox_GetCurSel(hNBPasses)+1, &report, log_fd)) {
uprintf("Bad blocks: Check failed.\n"); uprintf("Bad blocks: Check failed.\n");
if (!IS_ERROR(FormatStatus)) if (!IS_ERROR(FormatStatus))
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_BADBLOCKS_FAILURE); FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_BADBLOCKS_FAILURE);
ClearMBRGPT(hPhysicalDrive, SelectedDrive.DiskSize, SelectedDrive.Geometry.BytesPerSector, FALSE); ClearMBRGPT(hPhysicalDrive, SelectedDrive.DiskSize, SectorSize, FALSE);
fclose(log_fd); fclose(log_fd);
_unlink(logfile); _unlink(logfile);
goto out; goto out;
@ -1431,7 +1432,7 @@ DWORD WINAPI FormatThread(void* param)
// Especially after destructive badblocks test, you must zero the MBR/GPT completely // Especially after destructive badblocks test, you must zero the MBR/GPT completely
// before repartitioning. Else, all kind of bad things happen. // before repartitioning. Else, all kind of bad things happen.
if (!ClearMBRGPT(hPhysicalDrive, SelectedDrive.DiskSize, SelectedDrive.Geometry.BytesPerSector, use_large_fat32)) { if (!ClearMBRGPT(hPhysicalDrive, SelectedDrive.DiskSize, SectorSize, use_large_fat32)) {
uprintf("unable to zero MBR/GPT\n"); uprintf("unable to zero MBR/GPT\n");
if (!IS_ERROR(FormatStatus)) if (!IS_ERROR(FormatStatus))
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT;
@ -1454,18 +1455,22 @@ DWORD WINAPI FormatThread(void* param)
} }
uprintf("Writing Image..."); uprintf("Writing Image...");
buffer = (uint8_t*)malloc(DD_BUFFER_SIZE); // Our buffer size must be a multiple of the sector size
BufSize = ((DD_BUFFER_SIZE + SectorSize - 1) / SectorSize) * SectorSize;
buffer = (uint8_t*)malloc(BufSize + SectorSize); // +1 sector for align
if (buffer == NULL) { if (buffer == NULL) {
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_ENOUGH_MEMORY; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_NOT_ENOUGH_MEMORY;
uprintf("could not allocate DD buffer"); uprintf("could not allocate DD buffer");
goto out; goto out;
} }
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747.aspx does buffer sector alignment
aligned_buffer = ((void *) ((((uintptr_t)(buffer)) + (SectorSize) - 1) & (~(((uintptr_t)(SectorSize)) - 1))));
// Don't bother trying for something clever, using double buffering overlapped and whatnot: // Don't bother trying for something clever, using double buffering overlapped and whatnot:
// With Windows' default optimizations, sync read + sync write for sequential operations // With Windows' default optimizations, sync read + sync write for sequential operations
// will be as fast, if not faster, than whatever async scheme you can come up with. // will be as fast, if not faster, than whatever async scheme you can come up with.
for (wb = 0; ; wb += wSize) { for (wb = 0, wSize = 0; ; wb += wSize) {
s = ReadFile(hSourceImage, buffer, DD_BUFFER_SIZE, &rSize, NULL); s = ReadFile(hSourceImage, aligned_buffer, BufSize, &rSize, NULL);
if (!s) { if (!s) {
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_READ_FAULT; FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_READ_FAULT;
uprintf("read error: %s", WindowsErrorString()); uprintf("read error: %s", WindowsErrorString());
@ -1483,9 +1488,12 @@ DWORD WINAPI FormatThread(void* param)
if (wb + rSize > iso_report.projected_size) { if (wb + rSize > iso_report.projected_size) {
rSize = (DWORD)(iso_report.projected_size - wb); rSize = (DWORD)(iso_report.projected_size - wb);
} }
// WriteFile fails unless the size is a multiple of sector size
if (rSize % SectorSize != 0)
rSize = ((rSize + SectorSize -1) / SectorSize) * SectorSize;
for (i=0; i<WRITE_RETRIES; i++) { for (i=0; i<WRITE_RETRIES; i++) {
CHECK_FOR_USER_CANCEL; CHECK_FOR_USER_CANCEL;
s = WriteFile(hPhysicalDrive, buffer, rSize, &wSize, NULL); s = WriteFile(hPhysicalDrive, aligned_buffer, rSize, &wSize, NULL);
if ((s) && (wSize == rSize)) if ((s) && (wSize == rSize))
break; break;
if (s) if (s)

View file

@ -61,7 +61,7 @@
#define UDF_FORMAT_WARN 20 // Duration (in seconds) above which we warn about long UDF formatting times #define UDF_FORMAT_WARN 20 // Duration (in seconds) above which we warn about long UDF formatting times
#define MAX_FAT32_SIZE 2.0f // Threshold above which we disable FAT32 formatting (in TB) #define MAX_FAT32_SIZE 2.0f // Threshold above which we disable FAT32 formatting (in TB)
#define FAT32_CLUSTER_THRESHOLD 1.011f // For FAT32, cluster size changes don't occur at power of 2 boundaries but sligthly above #define FAT32_CLUSTER_THRESHOLD 1.011f // For FAT32, cluster size changes don't occur at power of 2 boundaries but sligthly above
#define DD_BUFFER_SIZE 65536 // Size of the buffer we use for DD operations #define DD_BUFFER_SIZE 65536 // Minimum size of the buffer we use for DD operations
#define WHITE RGB(255,255,255) #define WHITE RGB(255,255,255)
#define SEPARATOR_GREY RGB(223,223,223) #define SEPARATOR_GREY RGB(223,223,223)
#define RUFUS_URL "http://rufus.akeo.ie" #define RUFUS_URL "http://rufus.akeo.ie"

View file

@ -32,7 +32,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 242, 329 IDD_DIALOG DIALOGEX 12, 12, 242, 329
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
CAPTION "Rufus 1.5.0.553" CAPTION "Rufus 1.5.0.554"
FONT 8, "Segoe UI", 400, 0, 0x1 FONT 8, "Segoe UI", 400, 0, 0x1
BEGIN BEGIN
DEFPUSHBUTTON "Start",IDC_START,127,291,50,14 DEFPUSHBUTTON "Start",IDC_START,127,291,50,14
@ -164,7 +164,7 @@ END
IDD_DIALOG_XP DIALOGEX 12, 12, 242, 329 IDD_DIALOG_XP DIALOGEX 12, 12, 242, 329
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
CAPTION "Rufus 1.5.0.553" CAPTION "Rufus 1.5.0.554"
FONT 8, "MS Shell Dlg", 400, 0, 0x1 FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN BEGIN
DEFPUSHBUTTON "Start",IDC_START,127,291,50,14 DEFPUSHBUTTON "Start",IDC_START,127,291,50,14
@ -297,7 +297,7 @@ END
IDD_DIALOG_RTL DIALOGEX 12, 12, 242, 329 IDD_DIALOG_RTL DIALOGEX 12, 12, 242, 329
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_RTLREADING | WS_EX_APPWINDOW | WS_EX_LAYOUTRTL EXSTYLE WS_EX_RTLREADING | WS_EX_APPWINDOW | WS_EX_LAYOUTRTL
CAPTION "Rufus 1.5.0.553" CAPTION "Rufus 1.5.0.554"
FONT 8, "Segoe UI", 400, 0, 0x1 FONT 8, "Segoe UI", 400, 0, 0x1
BEGIN BEGIN
DEFPUSHBUTTON "Start",IDC_START,127,291,50,14 DEFPUSHBUTTON "Start",IDC_START,127,291,50,14
@ -437,7 +437,7 @@ END
IDD_DIALOG_RTL_XP DIALOGEX 12, 12, 242, 329 IDD_DIALOG_RTL_XP DIALOGEX 12, 12, 242, 329
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_RTLREADING | WS_EX_APPWINDOW | WS_EX_LAYOUTRTL EXSTYLE WS_EX_RTLREADING | WS_EX_APPWINDOW | WS_EX_LAYOUTRTL
CAPTION "Rufus 1.5.0.553" CAPTION "Rufus 1.5.0.554"
FONT 8, "MS Shell Dlg", 400, 0, 0x1 FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN BEGIN
DEFPUSHBUTTON "Start",IDC_START,127,291,50,14 DEFPUSHBUTTON "Start",IDC_START,127,291,50,14
@ -702,8 +702,8 @@ END
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,5,0,553 FILEVERSION 1,5,0,554
PRODUCTVERSION 1,5,0,553 PRODUCTVERSION 1,5,0,554
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -720,13 +720,13 @@ BEGIN
BEGIN BEGIN
VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)" VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)"
VALUE "FileDescription", "Rufus" VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "1.5.0.553" VALUE "FileVersion", "1.5.0.554"
VALUE "InternalName", "Rufus" VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2014 Pete Batard (GPL v3)" VALUE "LegalCopyright", "© 2011-2014 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html" VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe" VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus" VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "1.5.0.553" VALUE "ProductVersion", "1.5.0.554"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"