Page 1 of 1
How to (re)format a FAT drive?
Posted: Thu May 24, 2018 3:40 am
by fano1
Good morning!
Cosmos OS has good FAT support now you can read, write files, create directories and so on and all this using the normal C# System.IO classes... the missing part now is to format the HDD itself!
The HDD contains already a FAT32 file system with some files that I think was created in Windows so I've not need (for now) to init a totally empty drive but simply to (re)format it.
For now I'd like to support the quick and slow format how you could do on Windows but it is not clear to me what is the difference between these two and what operations I've to do.
Thanks for your help!
Re: How to (re)format a FAT drive?
Posted: Thu May 24, 2018 4:08 am
by iansjack
Re: How to (re)format a FAT drive?
Posted: Thu May 24, 2018 6:53 am
by fano1
OK the article says regarding quick format:
the format removes files from the partition, but does not scan the disk for bad sectors
it means that I should simply find the root directory and recursively remove all files / directory in it?
I don't think this will be a lot "quick"... I tough "quick format" simply removed the "references" to the files from the root directory and make appear all cluster as not in use (filling the FAT table with a special value I suppose).
Re: How to (re)format a FAT drive?
Posted: Thu May 24, 2018 7:23 am
by iansjack
"Removing the files" does mean removing the references to them in the directory, not wiping the data from the disk and reinitializing any meta-data (such as the FAT in this case). As far as the file system is concerned the files are removed (as far as recovery utilities are concerned they are not necessarily removed). It's a very quick operation.
Re: How to (re)format a FAT drive?
Posted: Fri May 25, 2018 2:12 am
by fano1
Let's take this as example:
I'll need to use File.Delete("Kudzu.txt") and Directory.Delete("Dir Testing", true) or something more low level? Directory.Delete(String path, bool recursive) obviously deletes all files and directories inside "Dir Testing" if they are a lot it will take a little time.
In the case of Cosmos we are tallking of 700 MB drive and I think less that 3 MB used so anything it will be "quick".
It is a lot of time that I've do not do a "quick" format of 300 GB drive really... I do not remember how "quick" is.
Re: How to (re)format a FAT drive?
Posted: Fri May 25, 2018 2:41 am
by alexfru
In a quick format you should zero out the data in the root directory and mark all allocated clusters in the FAT as available.
On FAT32 the latter should exclude the clusters belonging to the root directory because it's variable-length and is stored as a chain of clusters similarly to any other file or non-root directory. You may also want to shrink the root directory on FAT32 to just one cluster if it's larger than that.
Also, AFAIR, on FAT32 there are two special values somewhere (boot sectors?) that tell how many clusters are free and which cluster to examine first when looking for a free one. You should update the former and you may also update the latter.
IOW, you need to reset the metadata for the FS to appear empty. You don't need to actually wipe data in all clusters. And you definitely shouldn't low-level-format each sector (that's what the full format does and that's why it's not quick).
P.S. quick format should make sure the counts, sizes, CHS/LBA and cluster values and so on are valid in the boot sectors and FAT cluster chains. Perhaps, an easier implementation should start by examining these (failing if something's horribly wrong) and then simply recreate everything (the root directory and the cluster chains, however, it should remember which clusters were marked bad and preserve those bad marks).
Re: How to (re)format a FAT drive?
Posted: Fri May 25, 2018 5:19 pm
by StudlyCaps
So I quickly checked Wikipedia and there are three things you need to do to quick format FAT. In the file system information block, set the number of free clusters (offset 0x1E4) and the last allocated cluster (0x1E8) to 0xFFFFFFFF, then zero out the file allocation table, except the first two clusters (leave them be).
Technically you don't need to set the file system block info, because it's use is optional.