Skip to content

Commit bf7ec93

Browse files
committed
Fix calculation of cluster amount for FAT16 filesystems in FDISK (#70)
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.
1 parent 7b9c723 commit bf7ec93

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

source/kernel/bank5/fdisk2.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,9 @@ int CalculateFatFileSystemParametersFat16(ulong fileSystemSizeInK, dosFilesystem
413413

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

418-
if((sectorsPerFat & 0x3FF) == 0) {
419-
sectorsPerFat >>= 8;
420-
} else {
421-
sectorsPerFat >>= 8;
418+
if(((clusterCount + 2) & 0x3FF) != 0) {
422419
sectorsPerFat++;
423420
}
424421

0 commit comments

Comments
 (0)