From bf7ec933ab1a7cebf02e98a0e94aac47c8498db0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9stor=20Soriano?= Date: Sun, 23 Aug 2020 18:00:25 +0200 Subject: [PATCH] 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. --- source/kernel/bank5/fdisk2.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/source/kernel/bank5/fdisk2.c b/source/kernel/bank5/fdisk2.c index 757432eb..5a20b297 100644 --- a/source/kernel/bank5/fdisk2.c +++ b/source/kernel/bank5/fdisk2.c @@ -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++; }