Fix calculation of cluster amount for FAT16 filesystems in FDISK #70
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 if this is the only partition on the device or the last one. If there are more partitions after the one being written to, those could be overwritten.
This commit fixes this by adding an intermediate calculation that prevents the variable from overflowing.
Fixing existing partitions
A filesystem is at fault if the "big sector count" field in its boot sector (at offset 20h) is higher than the partition size as declared in its partition table entry. Below you can see an example: the partition size is in yellow and the bad sector count is in red.
To fix, it's enough to set the sector count as equal to the partition size.
If partitions aren't fixed it's recommended not to fill them to their maximum capacity, leave about 1MB free to be safe.