fix: Make backup boot-sector part of reserved region when formatting volume

Change number of reserved sectors to 8.
Previously backup boot-sector was over-written by FAT formatting code soon
after it was initialized.
This commit is contained in:
Rafał Harabień 2018-12-12 22:10:00 +01:00
parent f2863e8f46
commit b4455a86a8
3 changed files with 12 additions and 2 deletions

View File

@ -513,8 +513,9 @@ fn format_bpb(options: &FormatVolumeOptions) -> io::Result<(BiosParameterBlock,
let sectors_per_cluster = (bytes_per_cluster / bytes_per_sector as u32) as u8; let sectors_per_cluster = (bytes_per_cluster / bytes_per_sector as u32) as u8;
// Note: most of implementations use 32 reserved sectors for FAT32 but it's wasting of space // Note: most of implementations use 32 reserved sectors for FAT32 but it's wasting of space
// We use 4 because there are two boot sectors and one FS Info sector (1 sector remains unused) // This implementation uses only 8. This is enough to fit in two boot sectors (main and backup) with additional
let reserved_sectors: u16 = if fat_type == FatType::Fat32 { 4 } else { 1 }; // bootstrap code and one FSInfo sector. It also makes FAT alligned to 4096 which is a nice number.
let reserved_sectors: u16 = if fat_type == FatType::Fat32 { 8 } else { 1 };
let fats = 2u8; let fats = 2u8;
let is_fat32 = fat_type == FatType::Fat32; let is_fat32 = fat_type == FatType::Fat32;

View File

@ -913,6 +913,7 @@ pub fn format_volume<T: ReadWriteSeek>(mut disk: T, options: FormatVolumeOptions
write_zeros_until_end_of_sector(&mut disk, bytes_per_sector)?; write_zeros_until_end_of_sector(&mut disk, bytes_per_sector)?;
// backup boot sector // backup boot sector
debug_assert!(boot.bpb.backup_boot_sector() < boot.bpb.reserved_sectors());
disk.seek(SeekFrom::Start(boot.bpb.bytes_from_sectors(boot.bpb.backup_boot_sector())))?; disk.seek(SeekFrom::Start(boot.bpb.bytes_from_sectors(boot.bpb.backup_boot_sector())))?;
boot.serialize(&mut disk)?; boot.serialize(&mut disk)?;
write_zeros_until_end_of_sector(&mut disk, bytes_per_sector)?; write_zeros_until_end_of_sector(&mut disk, bytes_per_sector)?;

View File

@ -14,6 +14,14 @@ const TEST_STR: &str = "Hi there Rust programmer!\n";
type FileSystem = fatfs::FileSystem<BufStream<io::Cursor<Vec<u8>>>>; type FileSystem = fatfs::FileSystem<BufStream<io::Cursor<Vec<u8>>>>;
fn basic_fs_test(fs: &FileSystem) { fn basic_fs_test(fs: &FileSystem) {
let stats = fs.stats().expect("stats");
if fs.fat_type() == fatfs::FatType::Fat32 {
// On FAT32 one cluster is allocated for root directory
assert_eq!(stats.total_clusters(), stats.free_clusters() + 1);
} else {
assert_eq!(stats.total_clusters(), stats.free_clusters());
}
let root_dir = fs.root_dir(); let root_dir = fs.root_dir();
let entries = root_dir.iter().map(|r| r.unwrap()).collect::<Vec<_>>(); let entries = root_dir.iter().map(|r| r.unwrap()).collect::<Vec<_>>();
assert_eq!(entries.len(), 0); assert_eq!(entries.len(), 0);