[format] fixed corruption issue when MBR is garbage

* after a bb check, MBR could become garbage
* when that occurs, Windows might interpret a bad partition table and prevent MBR from being properly written
* force a complete zeroing of the whole MBR before partitioning to fix this
* also added error code for bb
This commit is contained in:
Pete Batard 2011-12-06 18:11:38 +00:00
parent 5c2242beaa
commit 480986b0ae
7 changed files with 39 additions and 7 deletions

View File

@ -189,3 +189,9 @@ int write_zero_mbr(FILE *fp)
write_data(fp, 0x0, mbr_zero_0x0, sizeof(mbr_zero_0x0)) &&
write_data(fp, 0x1FE, aucRef, sizeof(aucRef));
} /* write_zero_mbr */
int clear_mbr(FILE *fp)
{
unsigned char buf[512] = { 0 };
return write_data(fp, 0x0, buf, sizeof(buf));
} /* clear_mbr */

View File

@ -212,6 +212,16 @@ static BOOL AnalyzeMBR(HANDLE hPhysicalDrive)
return TRUE;
}
static BOOL ClearMBR(HANDLE hPhysicalDrive)
{
FILE fake_fd;
fake_fd._ptr = (char*)hPhysicalDrive;
fake_fd._bufsiz = SelectedDrive.Geometry.BytesPerSector;
return clear_mbr(&fake_fd);
}
/*
* Process the Master Boot Record
*/
@ -335,11 +345,20 @@ void __cdecl FormatThread(void* param)
SelectedDrive.Geometry.BytesPerSector, BADBLOCKS_RW)) {
// TODO: report block failure number, etc
uprintf("Bad blocks check failed.\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_BADBLOCKS);
goto out;
}
safe_unlockclose(hLogicalVolume);
}
// Especially after destructive badblocks test, you must zero the MBR completely
// before repartitioning. Else, all kind of bad things happen
if (!ClearMBR(hPhysicalDrive)) {
uprintf("unable to zero MBR\n");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT;
goto out;
}
if (!CreatePartition(hPhysicalDrive)) {
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_PARTITION_FAILURE;
goto out;

View File

@ -72,4 +72,8 @@ int write_syslinux_mbr(FILE *fp);
FALSE */
int write_zero_mbr(FILE *fp);
/* Completely clears (zeroes) master boot record, returns TRUE on success, otherwise
FALSE */
int clear_mbr(FILE *fp);
#endif

View File

@ -742,7 +742,7 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA
if (IsChecked(IDC_QUICKFORMAT)) {
SendMessage(hProgress, PBM_SETMARQUEE, FALSE, 0);
SetWindowLongPtr(hProgress, GWL_STYLE, ProgressStyle);
// This is the only way to achieve instantenous progress transition
// This is the only way to achieve instantanenous progress transition
SendMessage(hProgress, PBM_SETRANGE, 0, 101<<16);
SendMessage(hProgress, PBM_SETPOS, 101, 0);
SendMessage(hProgress, PBM_SETRANGE, 0, 100<<16);

View File

@ -183,3 +183,4 @@ typedef struct {
#define ERROR_INVALID_CLUSTER_SIZE 0x1203
#define ERROR_INVALID_VOLUME_SIZE 0x1204
#define ERROR_CANT_START_THREAD 0x1205
#define ERROR_BADBLOCKS 0x1206

View File

@ -30,7 +30,7 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 206, 278
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "Rufus v1.0.2.79"
CAPTION "Rufus v1.0.2.80"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "Start",IDC_START,94,236,50,14
@ -65,7 +65,7 @@ BEGIN
DEFPUSHBUTTON "OK",IDOK,231,175,50,14,WS_GROUP
CONTROL "<a href=""https://github.com/pbatard/rufus/wiki/Rufus"">https://github.com/pbatard/rufus</a>",IDC_ABOUT_RUFUS_URL,
"SysLink",WS_TABSTOP,46,47,114,9
LTEXT "Version 1.0.2 (Build 79)",IDC_STATIC,46,19,78,8
LTEXT "Version 1.0.2 (Build 80)",IDC_STATIC,46,19,78,8
PUSHBUTTON "License...",IDC_ABOUT_LICENSE,46,175,50,14,WS_GROUP
EDITTEXT IDC_ABOUT_COPYRIGHTS,46,107,235,63,ES_MULTILINE | ES_READONLY | WS_VSCROLL
LTEXT "Report bugs or request enhancements at:",IDC_STATIC,46,66,187,8
@ -164,8 +164,8 @@ END
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,2,79
PRODUCTVERSION 1,0,2,79
FILEVERSION 1,0,2,80
PRODUCTVERSION 1,0,2,80
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
@ -182,13 +182,13 @@ BEGIN
BEGIN
VALUE "CompanyName", "akeo.ie"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "1.0.2.79"
VALUE "FileVersion", "1.0.2.80"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "1.0.2.79"
VALUE "ProductVersion", "1.0.2.80"
END
END
BLOCK "VarFileInfo"

View File

@ -192,6 +192,8 @@ const char* StrError(DWORD error_code)
return "Cancelled by user";
case ERROR_CANT_START_THREAD:
return "Unable to create formatting thread";
case ERROR_BADBLOCKS:
return "Bad blocks detected";
default:
uprintf("StrError: hit default - %08X\n", error_code);
SetLastError(error_code);