Basics of µC/FS FAT Utilization

There are three commonly used FAT types: FAT12, FAT16 and FAT32. FAT12 addresses 3 byte memory regions. This means you can have a maximum of 212 (minus a few reserved clusters for the FAT) uniquely addressed memory regions. FAT16 addresses 4 byte memory regions. This allows for a maximum of approximately 65,524 addressable regions. FAT32 addresses 8 byte memory regions. This allows for 232 uniquely addressable memory regions within the volume.

Clusters

Clusters are the smallest type of memory that can be allocated to a file. Clusters are only used for data areas containing folders or directories. Everything else is contained in “sectors”.

Cluster Types:

  • Clusters containing File content
  • Those containing directories are Directory Clusters

Sectors

This is the smallest logical unit for describing how large memory regions are. Clusters are made up of sectors, and depending on the configured sector size they can vary from one sector to many sectors within a cluster. You could think of this as a file systems unit of measure.

Directory Entries

A directory entry contains the starting cluster of a memory region used for navigating and identifying how the data is organized for this file or directory. However, it does not include its name or the actual data stored in the file/directory.

Root Directory

Typically the first cluster in the data area except for FAT32 which allows for it to be located anywhere in the data area. Used for locating all files by starting here.

Root-Directory

The Boot Sector

The boot sector starts at sector 0 and contains the FAT table(s). If you are not using redundancy checking there may only be one FAT#1 here rather than multiples.

Navigating a FAT File System

Example-Root-Directory-and-File-Content

Example Root Directory Content and Example File Content

Example Root Directory contains three files and one directory. Stepping into Folder 1 we see that it contains another file and a folder. The cluster number shows where they link to.

Stepping inside the next folder we see it also contains another file and folder. Its current directory marker is the same as it was when we were inside folder 1. So you can see here how navigating through the layers works.

How µC/FS uses FAT

The majority of this information is stored and transferred via the FS_FILE and FS_VOL data type. These data types are used by the application to modify and change the content of the volume. Any changes to the file or the volume itself are reflected in this data structure relating to the FAT.

The FS_FILE struct gets loaded with specific file data when it is in use. This keeps track of the sector and file size as well as a pointer to the volume that it is associated with.

The FS_VOL struct is a part of the file struct. Here, information about the volume the file is located in can also be found. Number of files, size of sectors, numer of directories and the state of the volume.

struct  fs_file {
    FS_TYPE         Type;              
    FS_ID           ID;                
    FS_STATE        State;             
    FS_CTR          RefCnt;            
    FS_CTR          RefreshCnt;        

    FS_FLAGS        AccessMode;        
    CPU_BOOLEAN     FlagErr;           
    CPU_BOOLEAN     FlagEOF;           
    FS_STATE        IO_State;          
    FS_FILE_SIZE    Size;              
    FS_FILE_SIZE    Pos;               

#if (FS_CFG_FILE_BUF_EN == DEF_ENABLED)
    FS_FILE_SIZE    BufStart;          
    CPU_SIZE_T      BufMaxPos;         
    CPU_SIZE_T      BufSize;           
    FS_FLAGS        BufMode;           
    FS_STATE        BufStatus;         
    void           *BufPtr;            
    FS_SEC_SIZE     BufSecSize;        
#endif

    FS_VOL         *VolPtr;            
    void           *DataPtr;           

#if (FS_CFG_CTR_STAT_EN == DEF_ENABLED)
    FS_CTR          StatRdCtr;         
    FS_CTR          StatWrCtr;         
#endif
};

FS_FILE Struct

struct  fs_vol {
    FS_TYPE            Type;      
    FS_STATE           State;     
    FS_CTR             RefCnt;    
    FS_CTR             RefreshCnt;

    FS_FLAGS           AccessMode;
    CPU_CHAR           Name[FS_CFG_MAX_VOL_NAME_LEN + 1u]; 
    FS_PARTITION_NBR   PartitionNbr;   
    FS_SEC_NBR         PartitionStart; 
    FS_SEC_QTY         PartitionSize;  
    FS_SEC_SIZE        SecSize;        

    FS_QTY             FileCnt;        
#ifdef FS_DIR_MODULE_PRESENT
    FS_QTY             DirCnt;         
#endif
    FS_DEV            *DevPtr;         
    void              *DataPtr;        
#ifdef FS_CACHE_MODULE_PRESENT
    FS_VOL_CACHE_API  *CacheAPI_Ptr;   
    void              *CacheDataPtr;   
#endif

#if (FS_CFG_CTR_STAT_EN == DEF_ENABLED)
    FS_CTR             StatRdSecCtr;   
    FS_CTR             StatWrSecCtr;   
#endif
};

FS_VOL Struct

File Allocation Table (FAT)

The FAT maps out the entire content of the file system in a condensed table for ease of navigation through the data region.

Each cell in the FAT can have several possible values:

  • 0x00–UnallocatedCell
  • 0xFF7, 0xFFF7 or 0xFFF FFF7 (FAT 12, 16, 32) – bad cluster
  • 0xFF8, 0xFFF8 or 0xFFF FFF8 – End of File Marker
  • All other values indicate a cluster has been allocated and indicate which other cells it is linked to.
0
FF F8
1
FF FF
2
FF F8
3
FF F8
4
05 00
5
08 00
6
00 00
7
00 00
8
09 00
9
0A 00
10
FF F8
11
0D 00
12
FF F8
13
FF F8
14
00 00
15
00 00
16
0F 00
17
00 00
18
00 00
19
00 00
20
00 00
21
00 00
22
00 00
23
00 00

In this example, we have a FAT with 24 cells. There are three files currently represented in this table:

  • File.txt - Cluster 3
  • Image.png – Cluster 4
  • Document.pdf – Cluster 16

File.txt starts and ends in cluster 3 because it has a value of 0xFF F8 which indicates an EOF.

Cluster3-File.txt

Image.png starts in Cluster 4 and continues through Clusters 5, 8, 9 and ends in 10 with an EoF.

Cluster4-Images.png

Document.pdf starts in 16 and continues to 15 where it is unallocated. Since this should be a completed file already we can conclude that the file is corrupted since there is no link to an end of file. This would not open correctly on the file system.

Cluster16-Document.pdf

Tags: ,

Questions or Comments?

Have a question or a suggestion for a future article?
Don't hesitate to contact us and let us know!
All comments and ideas are welcome.