Skip to content

mindentropy/mdkfat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project involves development of a FAT32 filesystem for small microcontrollers.
This will be originally developed for the MDK OS to read FAT32 filesytem.

Development of this project will be done outside of any tree and debugging will be
done using the standard libraries. To integrate this project into an existing 
microcontroller project appropriate changes has to be done for the microcontroller/microprocessor
environment.



Understanding of the FAT Filesystem.
===================================
MBR: Master boot record.
The Master boot record is 512 bytes. The master boot record ends with the 
signature 0x55 0xAA.
The 64 bytes behind the signature contains the start of the partition table
entry. There are 4 partition table entries each of 16 bytes.

The following diagram explains this:

Master Boot Record:
    +-------------------+ 
    |                   |
    |                   |
    |                   |
    |                   |
    | Boot code of 446  |
    |           bytes   |
    |                   |
    |                   |
    |                   |
    |                   |
    |                   |
    |                   |
    |                   |
    |          _________|
    |_________|_________|
    |P1_______|_________|
    |P2_______|_________|
    |P3_______|_________|
    |P4       |0x55 0xAA|
    +-------------------+

P1,P2,P3 and P4 contains the partition table of 16 bytes each.

Observations with the partition scheme
--------------------------------------

I am seeing multiple boot signature 0x55 0xAA (at least 3) with the 
MBR partition data entry being empty. Why?

If you take a peek at the create_testfs_image shell script you will find that 
I am creating a image without any partitions. The image is just one big filesystem.
Hence there are no partition data present.

In the case of multiple boot signatures please see Microsoft FAT32 specifications Pg22.
In it you will find that the Microsoft FAT32 "boot sector" is actually three 512-byte sectors
long. Also there is a copy of all these sectors starting at the BPB_BkBootSec sector. The 
BPB_BkBootSec is the backup boot sector. This is a feature of the FAT32 filesystem. The
idea behind this is if sector 0 is overwritten or corrupted by some means the whole filesystem
cannot be read making it a single point of failure. To prevent this a backup copy of the 
boot sector information is kept at BPB_BkBootSec which is usually 6 and some of the systems are
"hardwired" to read sector 6.


Calculations:
============
1) Getting to the root directory sector:
---------------------------------------
RootDirSector = BPB_RsvdSecCnt + (BPB_NumFATS *  BPB_FATSz32)
To get to the root directory sector we need to get past the "Reserved Sector Count"
in sectors plus the "Number of FATS" multiplied by the "Size per FAT" in sectors.

2) Getting to the FAT structure:
-------------------------------
FAT_Structure = BPD_RsvdSecCnt;

3) Finding the contents of the FAT given a cluster number N.
------------------------------------------------------------

FATType == FAT32 then FATOffset = N * 4; where N is the cluster number.

Next go to the particular sector. What does this mean? The FAT table spans
multiple sectors. You need to go the particular sector as below:
ThisFATSecNum = BPB_RsvdSecCnt + (FATOffset/BP_BytsPerSec);

Next in that sector go the particular offset:
ThisFATEntOffset = (FATOffset % BPB_BytsPerSec);

The FAT is arranged as clusters i.e.

C1 C2 C3 C4 ..... Cn

So if the first sector is kept in an array then the Sector[0] is C1 Sector[1] is C2
Sector[n] is Cn.

Here n is the FatEntOffset as shown above.

4) Getting to the First cluster of the file:
-------------------------------------------
We first get to the First Data Sector 'FirstDataSector'

FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATS * FATSz) + RootDirSectors;

Here FATSz will be the Size of FAT.

The FirstDataSector will take me to the RootDirectoryEntry.
Next we find have to travel to the First Sector of the file.

FirstSectorofCluster = ((N-2) * BPB_SecPerClus) + FirstDataSector

About

FAT32 Filesystem for microcontrollers.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published