mirror of
				https://github.com/pbatard/rufus.git
				synced 2024-08-14 23:57:05 +00:00 
			
		
		
		
	[fat] improve FAT32 boot record setup
* write primary and secondary boot records * add offset provision for sector write * improved detection reports for MBR, FAT16 and FAT32
This commit is contained in:
		
							parent
							
								
									36fa9cee23
								
							
						
					
					
						commit
						0600005a09
					
				
					 4 changed files with 85 additions and 50 deletions
				
			
		|  | @ -71,9 +71,8 @@ int write_fat_32_br(FILE *fp, int bKeepLabel) | |||
| 	 ( write_data(fp, 0x0, br_fat32_0x0, sizeof(br_fat32_0x0)) && | ||||
| 	   /* BIOS Parameter Block should not be overwritten */ | ||||
| 	   write_data(fp, 0x52, br_fat32_0x52, sizeof(br_fat32_0x52)) && | ||||
| 	   /* except offsets 0x40 & 0xC40 which may have to be corrected */ | ||||
| 	   /* except offset 0x40 which may have to be corrected */ | ||||
| 	   write_data(fp, 0x40, &offset_x40, 1) && | ||||
| 	   write_data(fp, 0xc40, &offset_x40, 1) && | ||||
| 	   /* Cluster information is not overwritten, however, it would bo OK
 | ||||
| 	      to write 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff here. */ | ||||
| 	   write_data(fp, 0x3f0, br_fat32_0x3f0, sizeof(br_fat32_0x3f0)) ); | ||||
|  | @ -83,9 +82,8 @@ int write_fat_32_br(FILE *fp, int bKeepLabel) | |||
| 	   /* BIOS Parameter Block should not be overwritten */ | ||||
| 	   write_data(fp, 0x47, label_11_char, sizeof(label_11_char)) && | ||||
| 	   write_data(fp, 0x52, br_fat32_0x52, sizeof(br_fat32_0x52)) && | ||||
| 	  /* except offsets 0x40 & 0xC40 which may have to be corrected */ | ||||
| 	  /* except offset 0x40 which may have to be corrected */ | ||||
| 	   write_data(fp, 0x40, &offset_x40, 1) && | ||||
| 	   write_data(fp, 0xc40, &offset_x40, 1) && | ||||
| 	   /* Cluster information is not overwritten, however, it would bo OK
 | ||||
| 	      to write 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff here. */ | ||||
| 	   write_data(fp, 0x3f0, br_fat32_0x3f0, sizeof(br_fat32_0x3f0)) ); | ||||
|  |  | |||
|  | @ -71,7 +71,12 @@ int read_sectors(HANDLE hDrive, size_t SectorSize, | |||
|    return Size; | ||||
| } | ||||
| 
 | ||||
| /* Use a bastardized fp that contains a Windows handle and the sector size */ | ||||
| /*
 | ||||
|  * The following calls use a bastardized fp on Windows that contains: | ||||
|  * fp->_ptr: a Windows handle | ||||
|  * fp->_bufsiz: the sector size | ||||
|  * fp->_cnt: a file offset | ||||
|  */ | ||||
| int contains_data(FILE *fp, size_t Position, | ||||
|                   const void *pData, size_t Len) | ||||
| { | ||||
|  | @ -79,6 +84,7 @@ int contains_data(FILE *fp, size_t Position, | |||
|    HANDLE hDrive = (HANDLE)fp->_ptr; | ||||
|    size_t SectorSize = (size_t)fp->_bufsiz; | ||||
|    size_t StartSector, EndSector, NumSectors; | ||||
|    Position += (size_t)fp->_cnt; | ||||
| 
 | ||||
|    StartSector = Position/SectorSize; | ||||
|    EndSector   = (Position+Len+SectorSize-1)/SectorSize; | ||||
|  | @ -107,6 +113,7 @@ int write_data(FILE *fp, size_t Position, | |||
|    HANDLE hDrive = (HANDLE)fp->_ptr; | ||||
|    size_t SectorSize = (size_t)fp->_bufsiz; | ||||
|    size_t StartSector, EndSector, NumSectors; | ||||
|    Position += (size_t)fp->_cnt; | ||||
| 
 | ||||
|    StartSector = Position/SectorSize; | ||||
|    EndSector   = (Position+Len+SectorSize-1)/SectorSize; | ||||
|  |  | |||
							
								
								
									
										104
									
								
								src/format.c
									
										
									
									
									
								
							
							
						
						
									
										104
									
								
								src/format.c
									
										
									
									
									
								
							|  | @ -179,58 +179,69 @@ out: | |||
| 
 | ||||
| static BOOL AnalyzeMBR(HANDLE hPhysicalDrive) | ||||
| { | ||||
| 	FILE fake_fd; | ||||
| 	FILE fake_fd = { 0 }; | ||||
| 
 | ||||
| 	fake_fd._ptr = (char*)hPhysicalDrive; | ||||
| 	fake_fd._bufsiz = SelectedDrive.Geometry.BytesPerSector; | ||||
| 
 | ||||
| 	// TODO: Apply this detection before partitioning
 | ||||
| 	if (is_br(&fake_fd)) { | ||||
| 		uprintf("Drive has an x86 boot sector\n"); | ||||
| 	} else{ | ||||
| 		uprintf("Drive is missing an x86 boot sector!\n"); | ||||
| 	if (!is_br(&fake_fd)) { | ||||
| 		uprintf("Drive does not have an x86 master boot record\n"); | ||||
| 		return FALSE; | ||||
| 	} | ||||
| 	// TODO: Add/Eliminate FAT12?
 | ||||
| 	if (is_fat_16_br(&fake_fd) || is_fat_32_br(&fake_fd)) { | ||||
| 		if (entire_fat_16_br_matches(&fake_fd)) { | ||||
| 			uprintf("Exact FAT16 DOS boot record match\n"); | ||||
| 		} else if (entire_fat_16_fd_br_matches(&fake_fd)) { | ||||
| 			uprintf("Exact FAT16 FreeDOS boot record match\n"); | ||||
| 		} else if (entire_fat_32_br_matches(&fake_fd)) { | ||||
| 			uprintf("Exact FAT32 DOS boot record match\n"); | ||||
| 		} else if (entire_fat_32_nt_br_matches(&fake_fd)) { | ||||
| 			uprintf("Exact FAT32 NT boot record match\n"); | ||||
| 		} else if (entire_fat_32_fd_br_matches(&fake_fd)) { | ||||
| 			uprintf("Exactly FAT32 FreeDOS boot record match\n"); | ||||
| 		} else { | ||||
| 			uprintf("Unknown FAT16 or FAT32 boot record\n"); | ||||
| 		} | ||||
| 	} else if (is_dos_mbr(&fake_fd)) { | ||||
| 		uprintf("Microsoft DOS/NT/95A master boot record match\n"); | ||||
| 	if (is_dos_mbr(&fake_fd)) { | ||||
| 		uprintf("Drive has a Microsoft DOS/NT/95A master boot record\n"); | ||||
| 	} else if (is_dos_f2_mbr(&fake_fd)) { | ||||
| 		uprintf("Microsoft DOS/NT/95A master boot record with the undocumented\n"); | ||||
| 		uprintf("F2 instruction match\n"); | ||||
| 		uprintf("Drive has a Microsoft DOS/NT/95A master boot record " | ||||
| 			"with the undocumented F2 instruction\n"); | ||||
| 	} else if (is_95b_mbr(&fake_fd)) { | ||||
| 		uprintf("Microsoft 95B/98/98SE/ME master boot record match\n"); | ||||
| 		uprintf("Drive has a Microsoft 95B/98/98SE/ME master boot record\n"); | ||||
| 	} else if (is_2000_mbr(&fake_fd)) { | ||||
| 		uprintf("Microsoft 2000/XP/2003 master boot record match\n"); | ||||
| 		uprintf("Drive has a Microsoft 2000/XP/2003 master boot record\n"); | ||||
| 	} else if (is_vista_mbr(&fake_fd)) { | ||||
| 		uprintf("Microsoft Vista master boot record match\n"); | ||||
| 		uprintf("Drive has a Microsoft Vista master boot record\n"); | ||||
| 	} else if (is_win7_mbr(&fake_fd)) { | ||||
| 		uprintf("Microsoft 7 master boot record match\n"); | ||||
| 		uprintf("Drive has a Microsoft 7 master boot record\n"); | ||||
| 	} else if (is_zero_mbr(&fake_fd)) { | ||||
| 		uprintf("Zeroed non-bootable master boot record match\n"); | ||||
| 		uprintf("Drive has a zeroed non-bootable master boot record\n"); | ||||
| 	} else { | ||||
| 		uprintf("Unknown boot record\n"); | ||||
| 	} | ||||
| 	return TRUE; | ||||
| } | ||||
| 
 | ||||
| static BOOL AnalyzePBR(HANDLE hLogicalVolume) | ||||
| { | ||||
| 	FILE fake_fd = { 0 }; | ||||
| 
 | ||||
| 	fake_fd._ptr = (char*)hLogicalVolume; | ||||
| 	fake_fd._bufsiz = SelectedDrive.Geometry.BytesPerSector; | ||||
| 
 | ||||
| 	// TODO: Add/Eliminate FAT12?
 | ||||
| 	if (!is_br(&fake_fd)) { | ||||
| 		uprintf("Volume does not have an x86 partition boot record\n"); | ||||
| 		return FALSE; | ||||
| 	} | ||||
| 	if (is_fat_16_br(&fake_fd) || is_fat_32_br(&fake_fd)) { | ||||
| 		if (entire_fat_16_br_matches(&fake_fd)) { | ||||
| 			uprintf("Drive has a FAT16 DOS partition boot record\n"); | ||||
| 		} else if (entire_fat_16_fd_br_matches(&fake_fd)) { | ||||
| 			uprintf("Drive has a FAT16 FreeDOS partition boot record\n"); | ||||
| 		} else if (entire_fat_32_br_matches(&fake_fd)) { | ||||
| 			uprintf("Drive has a FAT32 DOS partition boot record\n"); | ||||
| 		} else if (entire_fat_32_nt_br_matches(&fake_fd)) { | ||||
| 			uprintf("Drive has a FAT32 NT partition boot record\n"); | ||||
| 		} else if (entire_fat_32_fd_br_matches(&fake_fd)) { | ||||
| 			uprintf("Drive has a FAT32 FreeDOS partition boot record\n"); | ||||
| 		} else { | ||||
| 			uprintf("Drive has a unknown FAT16 or FAT32 partition boot record\n"); | ||||
| 		} | ||||
| 	} | ||||
| 	return TRUE; | ||||
| } | ||||
| 
 | ||||
| static BOOL ClearMBR(HANDLE hPhysicalDrive) | ||||
| { | ||||
| 	FILE fake_fd; | ||||
| 	FILE fake_fd = { 0 }; | ||||
| 
 | ||||
| 	fake_fd._ptr = (char*)hPhysicalDrive; | ||||
| 	fake_fd._bufsiz = SelectedDrive.Geometry.BytesPerSector; | ||||
|  | @ -246,7 +257,7 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive) | |||
| 	unsigned char* buf = NULL; | ||||
| 	size_t SecSize = SelectedDrive.Geometry.BytesPerSector; | ||||
| 	size_t nSecs = (0x200 + SecSize -1) / SecSize; | ||||
| 	FILE fake_fd; | ||||
| 	FILE fake_fd = { 0 }; | ||||
| 
 | ||||
| 	if (!AnalyzeMBR(hPhysicalDrive)) return FALSE; | ||||
| 
 | ||||
|  | @ -308,18 +319,34 @@ out: | |||
|  */ | ||||
| static BOOL WritePBR(HANDLE hLogicalVolume) | ||||
| { | ||||
| 	FILE fake_fd; | ||||
| 	int i; | ||||
| 	FILE fake_fd = { 0 }; | ||||
| 
 | ||||
| 	fake_fd._ptr = (char*)hLogicalVolume; | ||||
| 	fake_fd._bufsiz = SelectedDrive.Geometry.BytesPerSector; | ||||
| 
 | ||||
| 	switch (ComboBox_GetItemData(hFileSystem, ComboBox_GetCurSel(hFileSystem))) { | ||||
| 	case FS_FAT16: | ||||
| 		if (write_fat_16_br(&fake_fd, 0)) | ||||
| 		if (!is_fat_16_fs(&fake_fd)) { | ||||
| 			uprintf("New volume does not have a FAT16 boot sector\n"); | ||||
| 			break; | ||||
| 		} | ||||
| 		uprintf("Confirmed new volume has a FAT16 boot sector\n"); | ||||
| 		if (!write_fat_16_br(&fake_fd, 0)) | ||||
| 			break; | ||||
| 		return TRUE; | ||||
| 	case FS_FAT32: | ||||
| 		// TODO: we may have to validate the 0xc40 offset of second FS boot sector
 | ||||
| 		if (write_fat_32_br(&fake_fd, 0)) | ||||
| 		for (i=0; i<2; i++) { | ||||
| 			if (!is_fat_32_fs(&fake_fd)) { | ||||
| 				uprintf("New volume does not have a %s FAT32 boot sector\n", i?"secondary":"primary"); | ||||
| 				break; | ||||
| 			} | ||||
| 			uprintf("Confirmed new volume has a %s FAT32 boot sector\n", i?"secondary":"primary"); | ||||
| 			uprintf("Setting %s FAT32 boot sector for DOS boot...\n", i?"secondary":"primary"); | ||||
| 			if (!write_fat_32_br(&fake_fd, 0)) | ||||
| 				break; | ||||
| 			fake_fd._cnt += 6 * (int)SelectedDrive.Geometry.BytesPerSector; | ||||
| 		} | ||||
| 		return TRUE; | ||||
| 	default: | ||||
| 		uprintf("unsupported FS for FS BR processing\n"); | ||||
|  | @ -359,6 +386,9 @@ void __cdecl FormatThread(void* param) | |||
| 	} | ||||
| 	UnmountDrive(hLogicalVolume); | ||||
| 
 | ||||
| 	AnalyzeMBR(hPhysicalDrive); | ||||
| 	AnalyzePBR(hLogicalVolume); | ||||
| 
 | ||||
| 	if (IsChecked(IDC_BADBLOCKS)) { | ||||
| 		do { | ||||
| 			if (!BadBlocks(hPhysicalDrive, SelectedDrive.DiskSize, | ||||
|  | @ -367,7 +397,7 @@ void __cdecl FormatThread(void* param) | |||
| 				if (!FormatStatus) | ||||
| 					FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)| | ||||
| 						APPERR(ERROR_BADBLOCKS_FAILURE); | ||||
| 				// TODO: should probably ClearMBR here as well
 | ||||
| 				ClearMBR(hPhysicalDrive); | ||||
| 				goto out; | ||||
| 			} | ||||
| 			uprintf("Check completed, %u bad block%s found. (%d/%d/%d errors)\n", | ||||
|  |  | |||
							
								
								
									
										12
									
								
								src/rufus.rc
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								src/rufus.rc
									
										
									
									
									
								
							|  | @ -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.3.93" | ||||
| CAPTION "Rufus v1.0.3.94" | ||||
| FONT 8, "MS Shell Dlg", 400, 0, 0x1 | ||||
| BEGIN | ||||
|     DEFPUSHBUTTON   "Start",IDC_START,94,236,50,14 | ||||
|  | @ -64,7 +64,7 @@ BEGIN | |||
|     DEFPUSHBUTTON   "OK",IDOK,231,175,50,14,WS_GROUP | ||||
|     CONTROL         "<a href=""http://rufus.akeo.ie"">http://rufus.akeo.ie</a>",IDC_ABOUT_RUFUS_URL, | ||||
|                     "SysLink",WS_TABSTOP,46,47,114,9 | ||||
|     LTEXT           "Version 1.0.3 (Build 93)",IDC_STATIC,46,19,78,8 | ||||
|     LTEXT           "Version 1.0.3 (Build 94)",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 | ||||
|  | @ -163,8 +163,8 @@ END | |||
| // | ||||
| 
 | ||||
| VS_VERSION_INFO VERSIONINFO | ||||
|  FILEVERSION 1,0,3,93 | ||||
|  PRODUCTVERSION 1,0,3,93 | ||||
|  FILEVERSION 1,0,3,94 | ||||
|  PRODUCTVERSION 1,0,3,94 | ||||
|  FILEFLAGSMASK 0x3fL | ||||
| #ifdef _DEBUG | ||||
|  FILEFLAGS 0x1L | ||||
|  | @ -181,13 +181,13 @@ BEGIN | |||
|         BEGIN | ||||
|             VALUE "CompanyName", "akeo.ie" | ||||
|             VALUE "FileDescription", "Rufus" | ||||
|             VALUE "FileVersion", "1.0.3.93" | ||||
|             VALUE "FileVersion", "1.0.3.94" | ||||
|             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.3.93" | ||||
|             VALUE "ProductVersion", "1.0.3.94" | ||||
|         END | ||||
|     END | ||||
|     BLOCK "VarFileInfo" | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue