diff --git a/src/boot_sector.rs b/src/boot_sector.rs index 4d179eb..778d5bc 100644 --- a/src/boot_sector.rs +++ b/src/boot_sector.rs @@ -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; // 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) - let reserved_sectors: u16 = if fat_type == FatType::Fat32 { 4 } else { 1 }; + // This implementation uses only 8. This is enough to fit in two boot sectors (main and backup) with additional + // 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 is_fat32 = fat_type == FatType::Fat32; diff --git a/src/fs.rs b/src/fs.rs index 5b1006d..1b07dad 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -913,6 +913,7 @@ pub fn format_volume(mut disk: T, options: FormatVolumeOptions write_zeros_until_end_of_sector(&mut disk, bytes_per_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())))?; boot.serialize(&mut disk)?; write_zeros_until_end_of_sector(&mut disk, bytes_per_sector)?; diff --git a/tests/format.rs b/tests/format.rs index 296cd47..43eabcb 100644 --- a/tests/format.rs +++ b/tests/format.rs @@ -14,6 +14,14 @@ const TEST_STR: &str = "Hi there Rust programmer!\n"; type FileSystem = fatfs::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 entries = root_dir.iter().map(|r| r.unwrap()).collect::>(); assert_eq!(entries.len(), 0);