How to calculate the minimum number of bytes required for the minimum FAT32 partition?

1

From experimentation with newfs_msdos, I determined that the minimum number of bytes required to create a FAT32 partition is 34089472. One byte less and the program complains:

$ newfs_msdos -F 32 -c 1 -S 512 -C 34089471 tm
newfs_msdos: 65524 clusters too few clusters for FAT32, need 65525

$ newfs_msdos -F 32 -c 1 -S 512 -C 34089472 tm
tm: 65525 sectors in 65525 FAT32 clusters (512 bytes/cluster)
BytesPerSec=512 SecPerClust=1 ResSectors=32 FATs=2 Media=0xf0 SecPerTrack=63 Heads=255 HiddenSecs=0 HugeSectors=66581 FATsecs=512 RootCluster=2 FSInfo=1 Backup=2

My question is: how would I have calculated 34089472?

From the FAT32 Wikipedia page, I can see a few metrics:

FAT32 requirements : 1 sector on each copy of FAT for every 128 clusters

FAT32 minimum : 1 sector per cluster × 65,525 clusters = 33,548,800 bytes (32,762.5 KB)

To me, this meant that at 512 bytes per sector, 65525 clusters and (65525/128 = 512) extra sectors overhead means that it should be

(65525 + 512) * 512 = 33810944

But, that's wrong. It's actually

(65525 + 1056) * 512 = 34089472

Where does this 1056 come from?


On a related note, FAT16 behaves the same way. You actually need 2124800 bytes instead of 2091520 (from above referenced Wikipedia page) + overhead. Why?

Roxy

Posted 2018-12-15T16:27:42.177

Reputation: 237

Answers

0

Where does this 1056 come from?

You haven't included:

  1. The boot block 0x20 bytes = 32 bytes.

  2. Two copies of the FAT (file allocation table), one is a backup, 2 * 512 = 1024.

Adding them up:

1024 + 32 = 1056

See A tutorial on the FAT file system for more information.

DavidPostill

Posted 2018-12-15T16:27:42.177

Reputation: 118 938

The OP's 1056 is a count of sectors. You seem to be counting just bytes. IOW using numbers without units is dangerous! – sawdust – 2018-12-16T01:04:12.783

0

My question is: how would I have calculated 34089472?

You're dealing with a block device that performs all data transfers, reads, writes, and allocations in fixed-sized blocks, specifically units of 512-byte sectors.
So the first calculation should be a conversion to a sector quantity:

34089472 bytes / 512 bytes per sector = 66581 sectors

To me, this meant that at 512 bytes per sector, 65525 clusters and (65525/128 = 512) extra sectors overhead

You have an unstated assumption that you're using one sector per cluster.

The wikipedia article that you cite already has the answer, but apparently you overlooked it.

There are 32 sectors for the reserved area (that includes the boot sector) (typically 32 sectors on FAT32 filesystems).
There are two FATs of 512 sectors each (or a total of 1024 sectors).
There is no root directory for FAT32.
There are 65525 sectors for 65525 clusters in the data region.

That's a total of 66581 sectors.


On a related note, FAT16 behaves the same way. You actually need 2124800 bytes instead of 2091520 (from above referenced Wikipedia page) + overhead. Why?

Again a conversion to a sector quantity:

2124800 bytes / 512 bytes per sector = 4150 sectors 

There are 32 sectors for the reserved area (that includes the boot sector) (apparently it's also 32 sectors on FAT16 filesystems). (Back in the day this would be whatever number of sectors to put the FAT at the start of a track.)
Assuming that you're using one sector per cluster and the minimum of 4085 clusters, there are two FATs of 16 sectors each (or a total of 32 sectors).
There is 1 sector (or cluster) for the root directory for FAT16.
There are 4085 sectors for 4085 clusters in the data region.

That's a total of 4150 sectors.

sawdust

Posted 2018-12-15T16:27:42.177

Reputation: 14 697