Page 1 of 2
FAT32 sector usage
Posted: Sat Apr 23, 2005 8:08 pm
by Kemp
I've been reading through the FAT32 specs and the numbers don't seem to add up. Firstly, why does it require 32 reserved sectors? I only count about 5 actual structures that use a sector each. Also, the first data cluster is cluster 2 and with 8 sectors per cluster that means the data area starts at sector 16, so the reserved sectors extend for an extra 16 sectors into the data area. Who wrote this thing anyway? (metaphorical

)
Re:FAT32 sector usage
Posted: Sun Apr 24, 2005 12:41 am
by B.E
You forgetting the FAT it self.
this will get you the first data sector:
If(BPB_FATSz16 != 0)
FATSz = BPB_FATSz16;
Else
FATSz = BPB_FATSz32;
FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz) + RootDirSectors;
Re:FAT32 sector usage
Posted: Sun Apr 24, 2005 6:01 am
by Kemp
Ok, let's see if I have this straight.
[pre]
Sector Area Contents
0 Boot sector
1 FSInfo
6 Backup boot sector
16-31 Data All reserved for FAT tables
[/pre]
Is that correct? (Using the standard recommended values in the BPB of course)
The BPB defines the root drectory cluster number, which is normally 2. This places the root directory on top of the fat tables. This is where I get confused also. I always thought of the fat tables as being the map that shows which areas of the disk are already used and etc, whereas the docs refer to the fat as being the root directory list.
Re:FAT32 sector usage
Posted: Sun Apr 24, 2005 4:26 pm
by B.E
this directly copied from the spec:
Given any valid data cluster number N, the sector number of the first sector of that cluster (again relative to sector 0 of the FAT volume) is computed as follows:
FirstSectorofCluster = ((N ? 2) * BPB_SecPerClus) + FirstDataSector;
so if FirstDataSector is 32 and if BPB_SecPerClus equal to 8 then the root directory would be the
Code: Select all
FirstSectorofCluster = ((2 -2) * 8) + 32;
= 32;
Re:FAT32 sector usage
Posted: Sun Apr 24, 2005 5:34 pm
by Kemp
I have that document right here and I know how you work out things like the first sector for a particular cluster. What I want to know is what the sectors are actually used for as I can't find any reason for there to be so many reserved ones.
Re:FAT32 sector usage
Posted: Sun Apr 24, 2005 6:45 pm
by B.E
The following section it the same specs explains why
What are the two reserved clusters at the start of the FAT for? The first reserved cluster, FAT[ 0 ], contains the BPB_Media byte value in its low 8 bits, and all other bits are set to 1. For example, if the BPB_Media value is 0xF8, for FAT12 FAT[ 0 ] = 0x0FF8, for FAT16 FAT[ 0 ] = 0xFFF8, and for FAT32 FAT[ 0 ] = 0x0FFFFFF8. The second reserved cluster, FAT[ 1 ], is set by FORMAT to the EOC mark. On FAT12 volumes, it is not used and is simply always contains an EOC mark. For FAT16 and FAT32, the file system driver may use the high two bits of the FAT[1] entry for dirty volume flags (all other bits, are always left set to 1). Note that the bit location is different for FAT16 and FAT32, because they are the high 2 bits of the entry.
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 5:51 am
by Kemp
That's one of the other bits that didn't make much sense to me when I read it. A cluster is 8 sectors usually, each one being 512 bytes, which means that they use 4kb of storage for storing an 8 bit value. Plus if those thwo clusters are used for storing that then that means that the FAT isn't in the reserved area. I'm really tempted to just not bother supporting FAT32, they must have been smoking some good s**t when they designed the thing.
And B.E., If I wanted to read quotes from the whitepaper I would read the thing over again, I come to this forum for a human's perspective on things. Does no-one here have a simple "this sector is used for this structure" type table? I'd imagine it would be useful to a lot of people.
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 6:47 am
by Pype.Clicker
Kemp wrote:
That's one of the other bits that didn't make much sense to me when I read it. A cluster is 8 sectors usually, each one being 512 bytes, which means that they use 4kb of storage for storing an 8 bit value.
Well, it looks like the value is stored in the FAT itself, not on the sectors ... I guess 'smart' formatting tools will make the 'first sector' of the data part coded such that no space is actually lost by that convention.
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 7:00 am
by Solar
Kemp wrote:A cluster is 8 sectors usually, each one being 512 bytes, which means that they use 4kb of storage for storing an 8 bit value.
Footprint for recovery tools that try to repair a screwed-up FAT partition?
(Just wild guessing / drawing from half-forgotten memories, I didn't delve into FAT that much myself.)
And B.E., If I wanted to read quotes from the whitepaper I would read the thing over again, I come to this forum for a human's perspective on things.
Some would claim that you have to be distinctly inhuman to do OS development.
Which part of the whitepaper information remained unclear to you? Which additional information do you want?
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 7:01 am
by distantvoices
cluster management info is stored in the FAT as Pype states, so I daresay, there aren't 512 bytes wasted.
btw, has someone a good cup o' coffee for me? I'm dead tired today.
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 7:28 am
by Pype.Clicker
beyond infinity wrote:
btw, has someone a good cup o' coffee for me? I'm dead tired today.
Here you get it. It should be hot'n'Streamy in about 58 mins.
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 8:52 am
by Kemp
My question was basically "What are all those sectors used for?". There are a lot more reserved than I can find a reason for (both in and before the data area). As I noted before, I know where the boot sector, file system info, and backup boot sectors go so that's 3 out of 16 sectors used at the beginning, but I can't find reference to the other 13 sectors being used for anything.
Then there's the 16 reserved sectors in the data area, which is where one of my issues with how things are named comes in. There is the root directory entry (which they appear to call the FAT table in the whitepaper) which has its obvious function, and I thought there was an area that basically has a list that says "If you're loading a file from X at the moment then the next part is at Y", which I always called the FAT table (yeah, I know the "table" at the end is redundant, but it doesn't sound right to me otherwise

).
So to summarise, two basic questions:
1) What are all the extra sectors at the start of the disk used for (if anything)?
2) What are the reserved sectors in the data area used for? (though mainly, why do I have these freaky pre-conceptions about what should be there)
I'm gonna keep following number 2 up myself with the info I have and I'll hopefully figure it out properly, I don't expect to be spoon-fed how to do things, sorry if I've appeared that way.
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 9:24 am
by Candy
Kemp wrote:
My question was basically "What are all those sectors used for?". There are a lot more reserved than I can find a reason for (both in and before the data area). As I noted before, I know where the boot sector, file system info, and backup boot sectors go so that's 3 out of 16 sectors used at the beginning, but I can't find reference to the other 13 sectors being used for anything.
Then there's the 16 reserved sectors in the data area, which is where one of my issues with how things are named comes in. There is the root directory entry (which they appear to call the FAT table in the whitepaper) which has its obvious function, and I thought there was an area that basically has a list that says "If you're loading a file from X at the moment then the next part is at Y", which I always called the FAT table (yeah, I know the "table" at the end is redundant, but it doesn't sound right to me otherwise

).
So to summarise, two basic questions:
1) What are all the extra sectors at the start of the disk used for (if anything)?
2) What are the reserved sectors in the data area used for? (though mainly, why do I have these freaky pre-conceptions about what should be there)
I'm gonna keep following number 2 up myself with the info I have and I'll hopefully figure it out properly, I don't expect to be spoon-fed how to do things, sorry if I've appeared that way.
Up to now I've been at school, being required to work and all. I couldn't type a long list of stuff that I recall.
Now I'm home. Prepare.
FAT32 is like FAT12 in most areas, except that its file table IS counted in the cluster chain and is a normal cluster chain, where the FAT12 one (as well as the FAT16 one) isn't. FAT32 is technically FAT28 though.
So, from the start you have:
<reserved sectors> <FAT1> <FAT2> ! <file table> <other stuff>
The exclamation mark is where cluster 2 begins. It's called cluster 2 since they needed some 3 bytes of info in the FAT and in FAT12, that used up the first two entries. In fat32 they scaled it to eat 8 bytes, without doing more. Still, that eats the first two cluster entries in the FAT.
Each cluster is between 512 and 32768 bytes large. In NT4 FAT16 systems you could use 65536 but it was very unsafe, since all other OSes didn't support it. The clusters start from the first sector after the FAT, which is sized according to a dword in the header. Note, they may be oversized. Before the two FATs is a block of reserved sectors, which may be used for anything your OS wants to (if it owns the disk). Strictly speaking, it's for a boot loader, extended information, backup boot sector, root sector or sth. Microsoft backs up sector #0 of the partition in sector #6. There is also, iirc, a bit of extra header info in sector #1.
Hope this helps, Candy
[edit] Yep, they're clusters, not cylinders. [/edit]
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 9:29 am
by Pype.Clicker
Candy wrote:
Still, that eats the first two cylinder entries in the FAT.
Each cylinder is between 512 and 32768 bytes large.
Hope this helps, Candy
uh? shouldn't we search&replace "cylinder" ==> "cluster" from that point ?
Re:FAT32 sector usage
Posted: Mon Apr 25, 2005 9:45 am
by Kemp
Woooo, thanks for that Candy, it was quite helpful. So I can have
Sector 0 = Boot loader
Sector 1 = FSInfo
Sector 6 = Backup boot sector
and use any others I want for my second stage? I'm doing a proper loader that grabs the right file rather than assuming it'll be the first one on the disk, and 420 bytes isn't quite enough for that ;D Wanted to make sure there weren't any unofficial uses for the other sectors that I should watch out for.
Just to check I'm not missing something:
root sector or sth
What exactly do you mean by that?
Thanks again, and if you think of anything else feel free to keep it coming
