Skip to content

Commit

Permalink
Fix calculation of cluster amount for FAT16 filesystems in FDISK (#70)
Browse files Browse the repository at this point in the history
The routine that calculates the amount of clusters for a new FAT16
filesystem in FDSIK has a bug that appears when creating a filesystem
with a maximum amount of clusters possible: a 16-bit variable overflows
and turns into 0 when it should actually be 65536.

This causes the sector count in the boot sector to be higher than
the actual size of the filesystem, and thus when writing to an almost
full partition a "Disk error writing" error may appear.

This commit fixes this by adding an intermediate calculation that
prevents the variable from overflowing.
  • Loading branch information
Konamiman committed Aug 23, 2020
1 parent 7b9c723 commit bf7ec93
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions source/kernel/bank5/fdisk2.c
Expand Up @@ -413,12 +413,9 @@ int CalculateFatFileSystemParametersFat16(ulong fileSystemSizeInK, dosFilesystem

dataSectorsCount = (fileSystemSizeInK * 2) - (FAT16_ROOT_DIR_ENTRIES / DIR_ENTRIES_PER_SECTOR) - 1;
clusterCount = dataSectorsCount >> sectorsPerClusterPower;
sectorsPerFat = clusterCount + 2;
sectorsPerFat = (clusterCount + 2) >> 8;

if((sectorsPerFat & 0x3FF) == 0) {
sectorsPerFat >>= 8;
} else {
sectorsPerFat >>= 8;
if(((clusterCount + 2) & 0x3FF) != 0) {
sectorsPerFat++;
}

Expand Down

0 comments on commit bf7ec93

Please sign in to comment.