Some silly FAT32 question

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa
Some silly FAT32 question

Post by linuxfreak » Mon Aug 13, 2012 10:01 pm

Hi all;
Okay, so I am trying to support FAT32 on my OS, but I also want to keep the driver as compatable with the current setup (I know I wil have to change it as the OS grows). Which brings me to my point, for the FAT32 driver to work, I need to use the _MOUNT_INFO struct. It has a field fatOffset which I need to calculate. How do I do that in FAT32? In these tutorials it is assumed that the fat offset is 1 -right after the bootloader(0), not bothering with any of the possible reserved sectors between bootloader and fat1. I would think that this is for simplicity or what? 'Cause we know we can only support 1 floppy at this point, which has no reserved sectors right? Or am I overcomplicating the plot here? :D

So basicaly all I need is an algorithm or whatever you want to call it, that would give me the offset of the first fat. And please correct me in any way if I am wron or if my assumptions for why fatOffset is only set to 1 in the tutorials are wrong.

Kind Regards:

halofreak1990
Posts:92
Joined:Thu May 27, 2010 8:54 pm
Location:Netherlands

Re: Some silly FAT32 question

Post by halofreak1990 » Tue Aug 14, 2012 10:01 am

Your assumption for the FAT offset in the tutorials is correct. Most floppies have only one reserved sector.
But to be certain, you could, of course assign fatOffset to be the value of

Code: Select all

bootsector->Bpb.ReservedSectors
As for FAT32, I believe http://en.wikipedia.org/wiki/File_Allocation_Table#BPB has information on where to get FAT32's offsets.

User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa

Re: Some silly FAT32 question

Post by linuxfreak » Tue Aug 14, 2012 3:27 pm

Hi:

I went through the Microsoft FAT32 white paper and implemented support accordingly, but for some reason I have missed the BPB_RsvdSecCnt field. Now that I check I didn't even add it to my Bpb struct. How silly, but thank you very much for solving 2 problems at once.

Regards:

User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa

Re: Some silly FAT32 question

Post by linuxfreak » Tue Aug 14, 2012 4:55 pm

Also, do you have any information regarding the way data is written or read on a FAT32 volume. I mean, FAT32 has no root directory, or rather, it is located in the data region of the disk. Does data get written to it the same way as in FAT12, except for the root directory being elsewhere. Does it form a tree structure eg.
Data Region->Root Directory->Files and folders or what?

Sorry I have never really programmed FAT32 before? The white paper does not specify anything on how to write or read from FAT32. It is also much different from FAT12 so it's kinda hard to modify the tutorials to work with FAT32. Also the fact that the root dir is in the data region confuses me. I will do my best to find info aswell.

Regards:

halofreak1990
Posts:92
Joined:Thu May 27, 2010 8:54 pm
Location:Netherlands

Re: Some silly FAT32 question

Post by halofreak1990 » Tue Aug 14, 2012 6:13 pm

Well, I haven't done any FAT32 programming either, but as far as I've read it, FAT12, FAT16 and FAT32 all share the same directory/file structures.
The (optional) FAT32 long file names are implemented using specially crafted directory entries. Wikipedia has some info on that, too.

User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa

Re: Some silly FAT32 question

Post by linuxfreak » Tue Aug 14, 2012 7:07 pm

Hello again:

Thanks, I have played araound a bit and have figured out almost all of the required fields in MOUNT_INFO for FAT32. They are:

NumberOfSectors: BpbExt.TotalSectors
FatOffset: Bpb.ReservedSectors(thanks again)
NumberRootEntries: NOT USED BY FAT32
RootOffsetSectors: BpbExt.RootCluster * Bpb.SectorsPerCluster
RootSize: I don't have this yet, as far as I can think, this is dynamic(can change)
FatSizeSectors: BpbExt.SectorsPerFat
FatSizeBytes: BpbExt.SectorsPerFat * Bpb.BytesPerSector
FatEntrySize: 32

If you have any suggestions, they are welcome. Also, do you think it would be enough to be able to read and write data to FAT32 volumes.

I am also reading up on how data is read and written on a FAT32 volume. Hopefully I can find something that will help.

Regards:

User avatar
Mike
Site Admin
Posts:465
Joined:Sat Oct 20, 2007 7:58 pm
Contact:

Re: Some silly FAT32 question

Post by Mike » Sun Aug 19, 2012 1:41 am

Hello,

Fat32 is very similar to fat12 in how it its designed. The only differences is that the root directory is stored in the data area (the extended BPB block can be used to get the starting cluster of the root directory--typically 2. Note that this is a cluster into the data area on disk), BPB is extended, FSInfo block, and the FAT entry is extended to 4 bytes (32 bits, although only 28 of the bits is ever used.)

In fat32, the root directory is itself a file. The extended BPB provides the starting cluster of the root directory in the data area. This is the first cluster of the root directory. To determine if this is the last cluster, the software needs to check the FAT for that cluster and test for EOF (0x0FFFFFF8) or the next cluster to load and the process continues. Note that the process of reading the root directory is the same as that of reading a file or other subdirectories.

Some important calculations to have:

Convert cluster to sector
Note that this basically just adds the cluster number to data area start
sector = (SectorsPerCluster * (clusterNumvber - 2)) + dataStartSector

Calculate FAT entry offset
This is specific to fat32
fatOffset = active_cluster * 4

Calculate cluster size
Note: Most hard disks have 1 cluster = 1 sector, but this is not always the case
clusterSize = bytesPerSector * SectorsPerCluster

Calculate FAT sector to load
Note: Fat32 FATs can get very large in size. It is recommended to load only portions of the FAT that need to be read rather then attempting to load the entire structure.
fatSector = firstFatSector + (fatOffset / clusterSize)

Think that's pretty much everything you need. The above calculations are simple but needed for loading clusters of files (and the root directory) so I decided to supply them here. Finally if requested, I may be able to provide our fat32 VBR to supplement the boot loader if requested.

User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa

Re: Some silly FAT32 question

Post by linuxfreak » Sun Aug 19, 2012 5:43 pm

Hello;

Thank you for the help. I appreciate it. I wouldn't mind if you post it at all. :o

Post Reply