My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Classes | Macros | Typedefs | Functions | Variables
fat32.h File Reference
#include <sw_types.h>

Go to the source code of this file.

Classes

struct  lfn_entry
 Structure of the long file name slot. More...
 
struct  f_info
 Structure of a file containing its basic information required for many operations. More...
 
struct  file_stat
 Structure containing the basic details of a file/directory. More...
 

Macros

#define END_OF_CLUSTER   0x0FFFFFFF
 
#define END_OF_ROOT_CLUSTER   0X0FFFFFF8
 
#define MAX_DIR_ENTRY_CNT   16
 
#define FILE_SYS_TYPE_OFF   82
 
#define BYTES_PER_SEC_OFF   11
 
#define SEC_PER_CLUS_OFF   13
 
#define RES_SEC_CNT_OFF   14
 
#define FAT_CNT_OFF   16
 
#define TOT_SEC_CNT_OFF   32
 
#define SEC_PER_FAT   36
 
#define ROOT_DIR_STRT_CLUS_OFF   44
 
#define FS_INFOSECTOR_OFF   48
 
#define BACKUP_BOOT_SEC_OFF   50
 
#define NXT_FREE_CLUS_OFF   492
 
#define FILE_SYS_TYPE_LENGTH   8
 
#define SHRT_FILE_NAME_LEN   11
 
#define STRT_CLUS_LOW_OFF   26
 
#define STRT_CLUS_HIGH_OFF   20
 
#define FILE_SIZE_OFF   28
 
#define ATTR_OFF   11
 
#define FILE_STAT_LEN   21
 
#define CHECK_SUM_OFF   13
 
#define FILE_NAME_SHRT_LEN   8
 
#define FILE_NAME_EXTN_LEN   3
 
#define LONG_FILE_NAME_LEN   255
 
#define LOW_CLUSWORD_MASK   0x0000FFFF
 
#define HIGH_CLUSWORD_MASK   0xFFFF0000
 
#define LONG_FNAME_MASK   0x0F
 
#define LAST_ORD_FIELD_SEQ   0x40
 
#define LFN_END_MARK   0xFFFF
 
#define LFN_TERM_MARK   0x0000
 
#define LFN_FIRST_OFF   0x01
 
#define LFN_SIXTH_OFF   0x0E
 
#define LFN_TWELVETH_OFF   0x1C
 
#define LFN_FIRST_SET_CNT   5
 
#define LFN_SEC_SET_CNT   6
 
#define LFN_THIRD_SET_CNT   2
 
#define LFN_FIRST_SET_LEN   10
 
#define LFN_SEC_SET_LEN   12
 
#define LFN_THIRD_SET_LEN   4
 
#define LFN_EMPTY_LEN   2
 
#define LFN_LEN_PER_ENTRY   13
 
#define FNAME_EXTN_SEP_OFF   6
 
#define FNAME_SEQ_NUM_OFF   7
 
#define BYTES_PER_CLUSTER_ENTRY   4
 
#define DIR_ENTRY_LEN   32
 
#define VOL_ID_LEN   4
 
#define VOL_LABEL_LEN   11
 
#define RESERV_LEN   12
 
#define FS_VER_LEN   2
 
#define OEM_NAME_LEN   8
 
#define JUMP_INS_LEN   3
 
#define MAX_FAT_CNT   2
 
#define SPACE_VAL   32
 
#define FILE_READ   0x01
 
#define FILE_WRITE   0X02
 
#define FILE_CREATE_NEW   0x04
 
#define FILE_CREATE_ALWAYS   0x08
 
#define FILE_APPEND   0x10
 
#define ATTR_READ   0x01
 
#define ATTR_HIDDEN   0x02
 
#define ATTR_SYSTEM   0x04
 
#define ATTR_VOL_LABEL   0x08
 
#define ATTR_DIR   0x10
 
#define ATTR_ARCHIVE   0x20
 
#define ATTR_LONG_FNAME   0x0F
 
#define FREE_DIR_ENTRY   0x00
 
#define DEL_DIR_ENTRY   0xE5
 
#define DOT_DIR_ENTRY   0x2E
 
#define ASCII_DIFF   32
 
#define FILE_SEEK_SET   0
 
#define FILE_SEEK_CUR   1
 
#define FILE_SEEK_END   2
 
#define DELIMITER   '/'
 
#define EXTN_DELIMITER   '.'
 
#define TILDE   '~'
 
#define FULL_SHRT_NAME_LEN   13
 

Typedefs

typedef struct f_info file_info
 

Functions

struct __attribute__ ((__packed__))
 Structure of the boot sector. More...
 
int mount_file_system (u8 *dev)
 Mount the FAT file system and retrieve its basic information. More...
 
bool chk_fat32_file_system (u8 *file_sys_type)
 Verify the type of FAT file system. More...
 
void cal_region_offsets (boot_sector *bs, u8 *dev)
 Function to calculate all the region offsets. More...
 
int file_open (const char *file_path, int flags)
 Function to open a specific file mentioned in the path based on the incoming modes. More...
 
bool get_dir_entry (char *fname, dir_entry **entry, u8 *strt, u32 strt_clus, bool is_lfn)
 Function to check whether a given file name exists in the file system and if so retrieve the corresponding short file name directory entry. More...
 
int lfn_entry_to_name (lfn_entry *entry, char *lfname, int *index)
 Function to get the long file name correspoding to a directory slot. More...
 
u8 * cluster_to_memory_addr (u32 cluster)
 Function to get the corresponding physical address from the given cluster. More...
 
int retrieve_file_info (file_info *finfo, dir_entry *entry, u8 mode, u8 *dir_offset, const char *path)
 Function to assign a file descriptor and necessary information to a file structure inorder to pursue subsequent operations. More...
 
u32 get_fat_table_entry (u32 cluster)
 Function to retrieve the next cluster value in a cluster chain from the FAT table. More...
 
void convert_to_uppercase (char *file_name)
 Function to convert the incoming short file name to its corresponding upper case letters. More...
 
int find_depth (const char *file_path)
 Function to calculate the number of iterations needed to reach the file from the given starting path. More...
 
int create_file (char *lfname, char *shrt_name, u32 clus, dir_entry **entry)
 Function to create a file if the file doesnt exists and the create flag is set. More...
 
int file_read (int fd, char *buffer, int bytes_to_read)
 Function to read the contents of a file. More...
 
int file_write (int fd, const char *buffer, int bytes_to_write)
 Function to write into the given file with the contents given. More...
 
u32 allocate_cluster ()
 Function to find an unused cluster from the FAT table. More...
 
void write_fat_table (u32 cluster, u32 value)
 Function to write into the FAT table after assigning an empty cluster to a file. More...
 
void get_short_file_name (char *lfname, char *sfname, char seq_num)
 Function to extract the short file name given its long file name. More...
 
void fill_lng_fname_entries (char *fname, u8 *entry, u8 chk_sum, int count)
 Function to put the long file name entries into the respective allocated region. More...
 
u8 calc_check_sum (u8 *dir_entry)
 Function to calculate the checksum for the long file name based on its short name entry. More...
 
int file_close (int fd)
 Close the opened file based on the file descriptor and free the file structure pointer. More...
 
int file_seek (int fd, int offset, int whence)
 Function to seek the opened file pointer to the desired location so that subsequent read and write operations are performed from the new value. More...
 
int get_seek_off (file_info *file_ptr, u32 strt_cluster, int offset)
 Function to move the current cluster and its offset value to the value specified in the offset from the starting cluster provided. More...
 
int get_long_file_name (char *fname, u8 **dir_strt, int *count)
 Function to retrieve the long file name given its end of the long file name entry. More...
 
int file_rename (const char *old, const char *new)
 Function to rename a file/directory. More...
 
int create_dir (const char *path)
 Function to create a directory. More...
 
int chk_file_lock (const char *path)
 
file_statread_dir (int fd)
 
int open_dir (const char *path)
 Function to open a directory. More...
 
int cal_req_dir_cnt (int file_name_len)
 
int file_truncate (const char *tr_pf, int tr_lgh)
 file_truncate - function cause the regular file named by path to be truncated to a size of precisely length bytes More...
 
int file_remove (const char *fl_name)
 file_remove - Deletes the file whose name is specified in filename More...
 
int file_unlink (const char *ul_name)
 file_remove - Deletes the file whose name is specified in filename More...
 
int list_dir (const char *path)
 Function to list the directory contents. More...
 

Variables

 boot_sector
 
 dir_entry
 

Macro Definition Documentation

#define ASCII_DIFF   32
#define ATTR_ARCHIVE   0x20
#define ATTR_DIR   0x10
#define ATTR_HIDDEN   0x02
#define ATTR_LONG_FNAME   0x0F
#define ATTR_OFF   11
#define ATTR_READ   0x01
#define ATTR_SYSTEM   0x04
#define ATTR_VOL_LABEL   0x08
#define BACKUP_BOOT_SEC_OFF   50
#define BYTES_PER_CLUSTER_ENTRY   4
#define BYTES_PER_SEC_OFF   11
#define CHECK_SUM_OFF   13
#define DEL_DIR_ENTRY   0xE5
#define DELIMITER   '/'
#define DIR_ENTRY_LEN   32
#define DOT_DIR_ENTRY   0x2E
#define END_OF_CLUSTER   0x0FFFFFFF
#define END_OF_ROOT_CLUSTER   0X0FFFFFF8
#define EXTN_DELIMITER   '.'
#define FAT_CNT_OFF   16
#define FILE_APPEND   0x10
#define FILE_CREATE_ALWAYS   0x08
#define FILE_CREATE_NEW   0x04
#define FILE_NAME_EXTN_LEN   3
#define FILE_NAME_SHRT_LEN   8
#define FILE_READ   0x01
#define FILE_SEEK_CUR   1
#define FILE_SEEK_END   2
#define FILE_SEEK_SET   0
#define FILE_SIZE_OFF   28
#define FILE_STAT_LEN   21
#define FILE_SYS_TYPE_LENGTH   8
#define FILE_SYS_TYPE_OFF   82
#define FILE_WRITE   0X02
#define FNAME_EXTN_SEP_OFF   6
#define FNAME_SEQ_NUM_OFF   7
#define FREE_DIR_ENTRY   0x00
#define FS_INFOSECTOR_OFF   48
#define FS_VER_LEN   2
#define FULL_SHRT_NAME_LEN   13
#define HIGH_CLUSWORD_MASK   0xFFFF0000
#define JUMP_INS_LEN   3
#define LAST_ORD_FIELD_SEQ   0x40
#define LFN_EMPTY_LEN   2
#define LFN_END_MARK   0xFFFF
#define LFN_FIRST_OFF   0x01
#define LFN_FIRST_SET_CNT   5
#define LFN_FIRST_SET_LEN   10
#define LFN_LEN_PER_ENTRY   13
#define LFN_SEC_SET_CNT   6
#define LFN_SEC_SET_LEN   12
#define LFN_SIXTH_OFF   0x0E
#define LFN_TERM_MARK   0x0000
#define LFN_THIRD_SET_CNT   2
#define LFN_THIRD_SET_LEN   4
#define LFN_TWELVETH_OFF   0x1C
#define LONG_FILE_NAME_LEN   255
#define LONG_FNAME_MASK   0x0F
#define LOW_CLUSWORD_MASK   0x0000FFFF
#define MAX_DIR_ENTRY_CNT   16
#define MAX_FAT_CNT   2
#define NXT_FREE_CLUS_OFF   492
#define OEM_NAME_LEN   8
#define RES_SEC_CNT_OFF   14
#define RESERV_LEN   12
#define ROOT_DIR_STRT_CLUS_OFF   44
#define SEC_PER_CLUS_OFF   13
#define SEC_PER_FAT   36
#define SHRT_FILE_NAME_LEN   11
#define SPACE_VAL   32
#define STRT_CLUS_HIGH_OFF   20
#define STRT_CLUS_LOW_OFF   26
#define TILDE   '~'
#define TOT_SEC_CNT_OFF   32
#define VOL_ID_LEN   4
#define VOL_LABEL_LEN   11

Typedef Documentation

typedef struct f_info file_info

Function Documentation

struct __attribute__ ( (__packed__)  )

Structure of the boot sector.

Structure of the 32 byte directory entry.

u32 allocate_cluster ( )

Function to find an unused cluster from the FAT table.

Returns
Newly allocated cluster for the file
void cal_region_offsets ( boot_sector bs,
u8 *  dev 
)

Function to calculate all the region offsets.

Parameters
bsPointer to the boot sector structure which holds the information of the boot sector of the file system
devPointer to the start of the file system
int cal_req_dir_cnt ( int  file_name_len)
Parameters
file_name_len
Returns
u8 calc_check_sum ( u8 *  dir_entry)

Function to calculate the checksum for the long file name based on its short name entry.

Parameters
dir_entryPointer to the start of the short file name corresponding to a long file name
Returns
checksum value of the short file name
bool chk_fat32_file_system ( u8 *  file_sys_type)

Verify the type of FAT file system.

Parameters
file_sys_typePointer to the location where the type of file system is stored
Returns
true:indicates FAT32 file system false:indicates not FAT32 file system
int chk_file_lock ( const char *  path)
Parameters
path
Returns
u8* cluster_to_memory_addr ( u32  cluster)

Function to get the corresponding physical address from the given cluster.

Parameters
clusterCluster value for which the address is to be obtained
Returns
Memory address of the incoming cluster
void convert_to_uppercase ( char *  file_name)

Function to convert the incoming short file name to its corresponding upper case letters.

Parameters
file_nameFile name whose values are to be converted
int create_dir ( const char *  path)

Function to create a directory.

Parameters
pathPointer to the start of the path which represents the directory hierarchy
Returns
On successful creation:File descriptor of the directory -1:If the creation failed or if it already exists
int create_file ( char *  lfname,
char *  shrt_name,
u32  clus,
dir_entry **  entry 
)

Function to create a file if the file doesnt exists and the create flag is set.

Parameters
lfnamePointer to the start of the long file. NULL in case of short file name creation
shrt_namePointer to the start of the short file name. In case of long file names the corresponding short file name is found out.
clusStarting cluster of the directory in which the file is going to be created
entryPointer to a pointer of the directory entry structure in which the short file name directory entry content is to be stored
Returns
On successful creation returns zero
int file_close ( int  fd)

Close the opened file based on the file descriptor and free the file structure pointer.

Parameters
fdFile descriptor of the file which needs to be closed
Returns
Zero on successful closing of the file
int file_open ( const char *  file_path,
int  flags 
)

Function to open a specific file mentioned in the path based on the incoming modes.

Parameters
file_pathPointer to the location of the file path string
flagsFlags indicating the modes in which the file is to be opened
Returns
Non-zero:File descriptor of the opened file Zero :File open is unsuccessful
int file_read ( int  fd,
char *  buffer,
int  bytes_to_read 
)

Function to read the contents of a file.

Parameters
fdFile descriptor of the file in which read operation needs to be performed
bufferPointer to the start of the buffer where the contents read are stored
bytes_to_readNumber of bytes to be read from the file
Returns
Number of bytes successfully read
int file_remove ( const char *  fl_name)

file_remove - Deletes the file whose name is specified in filename

Parameters
fl_name- string containing the name of the file to be deleted
Returns
- if the file is successfully deleted, a zero value is returned. On failure, a non-zero value is returned
int file_rename ( const char *  old,
const char *  new 
)

Function to rename a file/directory.

Parameters
oldConstant pointer to the start of the path where the file which needs to be renamed is located
newConstant pointer to the start of the path where the new name of the file/directory is situated
Returns
zero on successful renaming -1 if the new name already exists
int file_seek ( int  fd,
int  offset,
int  whence 
)

Function to seek the opened file pointer to the desired location so that subsequent read and write operations are performed from the new value.

Parameters
fdFile descriptor of the file in which the seek operation needs to be performed
offsetOffset bytes indicating the number of bytes which needs to be moved
whenceFlag denoting the position from which the seek operation needs to be performed
Returns
Number of bytes traversed from the beginning of the file to the current position
int file_truncate ( const char *  tr_pf,
int  tr_lgh 
)

file_truncate - function cause the regular file named by path to be truncated to a size of precisely length bytes

Parameters
tr_pf- truncates the file whose name is specified in filename
tr_lgh- The desired truncate size of the file in bytes
Returns
- If the file contents is successfully truncated, a zero value is returned.On failure, non-Zero is returned
int file_unlink ( const char *  ul_name)

file_remove - Deletes the file whose name is specified in filename

Parameters
ul_name- string containing the name of the file to be deleted
Returns
if the file is successfully deleted, a zero value is returned. On failure, a non-zero value is returned

file_remove - Deletes the file whose name is specified in filename

Parameters
ul_name- string containing the name of the file to be deleted
Returns
- if the file is successfully deleted, a zero value is returned. On failure, a non-zero value is returned
int file_write ( int  fd,
const char *  buffer,
int  bytes_to_write 
)

Function to write into the given file with the contents given.

Parameters
fdFile descriptor of the file
bufferPointer to the start of the contents which needs to be written into the file
bytes_to_writeIndicates the number of bytes which needs to be written into the file
Returns
Integer representing number of bytes successfully written into the file
void fill_lng_fname_entries ( char *  fname,
u8 *  entry,
u8  chk_sum,
int  count 
)

Function to put the long file name entries into the respective allocated region.

Parameters
fnamePointer to the start of the long file name
entryStarting memory location to the short file name directory entry
chk_sumCalculated check sum of the short file name corresponding to the long file name
countCounter indicating the number of 32 byte entries needed for the whole file name to get accommodated
int find_depth ( const char *  file_path)

Function to calculate the number of iterations needed to reach the file from the given starting path.

Parameters
file_pathPointer to the starting of the file path
Returns
Count of directories which resides before the file/directory from the given path
bool get_dir_entry ( char *  fname,
dir_entry **  entry,
u8 *  strt,
u32  strt_clus,
bool  is_lfn 
)

Function to check whether a given file name exists in the file system and if so retrieve the corresponding short file name directory entry.

Parameters
fnamePointer to the start of the file name which needs to be searched
entryPointer to a pointer of the directory entry structure in which the short file name directory entry content is to be stored
strtPointer to the start of the region where the file/directory needs to be searched
strt_clusStarting cluster of the directory in which the file is assumed to be present
is_lfnBoolean value which indicates whether the file is short or long file
Returns
true: if the file/dir is present false:if it is not present in the respected path
u32 get_fat_table_entry ( u32  cluster)

Function to retrieve the next cluster value in a cluster chain from the FAT table.

Parameters
clusterCluster whose next cluster value is to be obtained
Returns
Corresponding FAT table entry for the given cluster
int get_long_file_name ( char *  fname,
u8 **  dir_strt,
int *  count 
)

Function to retrieve the long file name given its end of the long file name entry.

Parameters
fnamePointer to a location where the retrieved long file name starts
dir_strtPointer to the memory location where the last component of the long file name resides
countCount representing the number of directory entries needed for the long file name
Returns
Length of the long file name
int get_seek_off ( file_info file_ptr,
u32  strt_cluster,
int  offset 
)

Function to move the current cluster and its offset value to the value specified in the offset from the starting cluster provided.

Parameters
file_ptrFile structure pointer pointing to the opened file
strt_clusterStarting cluster value from which the seek operation has to be performed
offsetOffset bytes for the seek operation
Returns
Number of bytes traversed from the beginning of the file
void get_short_file_name ( char *  lfname,
char *  sfname,
char  seq_num 
)

Function to extract the short file name given its long file name.

Parameters
lfname: Pointer to the start of the long file name string
sfname: Pointer to the start of the short file name in which this function extracts and stored the short file name value
seq_num: sequence number
int lfn_entry_to_name ( lfn_entry entry,
char *  lfname,
int *  index 
)

Function to get the long file name correspoding to a directory slot.

Parameters
entryPointer to the slot where a long file component resides
lfnamePointer to the start of the long file name
indexPointer to the offset of the long file name where the retrieved name from the given slot is going to be stored
Returns
0:if the file name doesnt end in the given slot 1:if the file name ends in the given slot
int list_dir ( const char *  path)

Function to list the directory contents.

Parameters
pathPointer to the start of the path representing the directory hierarchy
Returns
Number of directories/files present in the given directory
int mount_file_system ( u8 *  dev)

Mount the FAT file system and retrieve its basic information.

Parameters
devPointer to the start of the file system image
Returns
0:File system is FAT32 and the mount process is successful -1:Mount is unsuccessful
int open_dir ( const char *  path)

Function to open a directory.

Parameters
pathPointer to the start of the path representing the directory hierarchy
Returns
file descriptor assigned to the directory
file_stat* read_dir ( int  fd)
Parameters
fd
Returns
int retrieve_file_info ( file_info finfo,
dir_entry entry,
u8  mode,
u8 *  dir_offset,
const char *  path 
)

Function to assign a file descriptor and necessary information to a file structure inorder to pursue subsequent operations.

Parameters
finfo: Pointer to a file structure where the file information is stored
entry: Pointer to the directory entry of the file/directory
mode: Mode in which the file is requested to be opened
dir_offset: directory offset
path: path
Returns
returns : File descriptor of the file
void write_fat_table ( u32  cluster,
u32  value 
)

Function to write into the FAT table after assigning an empty cluster to a file.

Parameters
clusterCluster number for which the entry has to be made
valueNext cluster value of the cluster number provided

Variable Documentation

boot_sector
dir_entry