edrdos icon indicating copy to clipboard operation
edrdos copied to clipboard

Sub-optimal FAT size calculation for default BPB

Open boeckmann opened this issue 2 years ago • 2 comments

The kernel counts the FAT area as data sectors while calculating the FAT size, leading to a sub-optimal FAT size.

boeckmann avatar Dec 16 '23 20:12 boeckmann

I derived more accurate formulas for the calculation of the FAT table sizes than currently used in the source. Under the assumtion, that a single sector is 512 bytes and two FATs are created, the general formula is

$$fatsecs= \frac{(totalsec-reserved+1)/2+secperclust}{t \cdot secperclust+ 1} + 1$$

Where $t = 170$ for FAT12, $t = 128$ for FAT16 and $t= 64$ for FAT32. For FAT12 and FAT16, the size of the root directory in sectors has to be added to the reserved sectors.

This formula is overflow-save under 32-bit integer arithmetic provided that the total number of sectors does not exceed 32-bit integer range. The formula is reasonable easy to implement and should not return sector values more than one sector too large.

I will use this formula when I remove the overflow bug https://github.com/SvarDOS/edrdos/issues/135.

boeckmann avatar Feb 15 '25 21:02 boeckmann

I implemented the above formula for the FAT32 BPBs via commit https://github.com/SvarDOS/edrdos/commit/9699c65b6eee7d32986cd5374fbc2238902a6b5c. Seems to work as expected. I have not yet changed the FAT12/16 BPB creation...

The following document explains how I derived the formula: https://github.com/boeckmann/fatdocs/blob/main/fatcalc.pdf

boeckmann avatar Feb 17 '25 20:02 boeckmann