[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Info-mtools] Bug: mformat does not format FAT32 correctly

From: Pali Rohár
Subject: [Info-mtools] Bug: mformat does not format FAT32 correctly
Date: Mon, 4 Dec 2017 19:29:53 +0100
User-agent: NeoMutt/20170113 (1.7.2)


I observed 3 problems when I tried to create FAT32 disk image with
mformat tool:

1) mformat is not able to format disk images without specifying C/H/S
geometry. Nowadays there is no hard disk or SSD disk which use C/H/S
geometry and therefore asking user for such thing does not make sense.

So what I used is to specify 64 heads and 32 sectors per track just to
easily calculates number of tracks on disk. For disk with logical sector
size as 512 bytes (which is probably for all HDD and SSD), number of
tracks would be size of disk in megabytes (64*32*512 = 1 megabyte).

So mformat parameters: -h 64 -s 32 -t SIZE_IN_MB
Probably it is also needed to specify (for hard disks): -m 0xf8

If you do not enter this C/H/S geometry mformat just show error message:

mformat: Unknown geometry (You must tell the complete geometry of the
disk, either in /etc/mtools.conf or on the command line)

I think that except this error message, mformat for hard drives should
use above C/H/S auto-calculation.

2) mformat refuse to create FAT32 if disk size is less then 257 MB.

Test to reproduce (-F means to format as FAT32):

  $ truncate -s 100M fat.img
  $ mformat -F -h 64 -s 32 -t 100 -m 0xf8 :: -i fat.img

mformay show just error message:

Too few clusters for this fat size. Please choose a 16-bit fat in your
/etc/mtools.conf or .mtoolsrc file

Fix is to set size of cluster (in number of sectors) to 1. So mformat
parameter: -c 1

It is pity that mformat cannot calculate cluster size for smaller FAT32
disks (when size is less then 257 MB). Note that mformat can calculate
it correctly for FAT16 (when -F is not used).

3) Cluster size is not calculated correctly according to Microsoft FAT
specification. This also answer why there is a bug 2).

Function which calculate cluster size is named calc_cluster_size and can
be found in mformat.c source code. Relevant part for FAT32 is:

        switch(abs(fat_bits)) {
                case 32:
                        Fs->cluster_size = 8;
                        /* According to
                         * Micro$oft does not support FAT32 with less than 4K

It means that cluster size is hardcoded to 8 sectors for FAT32. This is
also reason for problem 3). For FAT16 there is autodetection code. I do
not know what that comment mean or what it try to refer as link is dead.

So I would like to know, why is hardcoded 8 sectors as cluster size for
FAT32? I suspect this is a bug which needs fixing. At least to have
working mformat in FAT32 mode for small disk (or disk images).

Because, in Microsoft FAT32 specification (fatgen103.doc) on page 20 for
FAT32 is written calculation table. For disk size there is cluster size
(in number of sectors), see table below:

32.5 MB - 260 MB   cluster_size =  1
 260 MB -   8 GB   cluster_size =  8
   8 GB -  16 GB   cluster_size = 16
  16 GB -  32 GB   cluster_size = 32
  32 GB -   2 TB   cluster_size = 64

Note that in that Microsoft FAT specification is written: "The rest of
this section is totally specific to drives that have 512 bytes per
sector.". So there is need to recalculate values when (logical) sector
size is not 512 bytes.

Pali Rohár

reply via email to

[Prev in Thread] Current Thread [Next in Thread]