My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
fat32.h
Go to the documentation of this file.
1 /*
2  * OpenVirtualization:
3  * For additional details and support contact developer@sierraware.com.
4  * Additional documentation can be found at www.openvirtualization.org
5  *
6  * Copyright (C) 2010-2014 SierraWare
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23 #ifndef __FAT32_H_
24 /*
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  *
28  * Header file for fat32 filesystem
29  */
30 #include<sw_types.h>
31 
32 #define END_OF_CLUSTER 0x0FFFFFFF
33 #define END_OF_ROOT_CLUSTER 0X0FFFFFF8
34 #define MAX_DIR_ENTRY_CNT 16
35 #define FILE_SYS_TYPE_OFF 82
36 #define BYTES_PER_SEC_OFF 11
37 #define SEC_PER_CLUS_OFF 13
38 #define RES_SEC_CNT_OFF 14
39 #define FAT_CNT_OFF 16
40 #define TOT_SEC_CNT_OFF 32
41 #define SEC_PER_FAT 36
42 #define ROOT_DIR_STRT_CLUS_OFF 44
43 #define FS_INFOSECTOR_OFF 48
44 #define BACKUP_BOOT_SEC_OFF 50
45 #define NXT_FREE_CLUS_OFF 492
46 #define FILE_SYS_TYPE_LENGTH 8
47 #define SHRT_FILE_NAME_LEN 11
48 #define STRT_CLUS_LOW_OFF 26
49 #define STRT_CLUS_HIGH_OFF 20
50 #define FILE_SIZE_OFF 28
51 #define ATTR_OFF 11
52 #define FILE_STAT_LEN 21
53 #define CHECK_SUM_OFF 13
54 #define FILE_NAME_SHRT_LEN 8
55 #define FILE_NAME_EXTN_LEN 3
56 #define LONG_FILE_NAME_LEN 255
57 #define LOW_CLUSWORD_MASK 0x0000FFFF
58 #define HIGH_CLUSWORD_MASK 0xFFFF0000
59 #define LONG_FNAME_MASK 0x0F
60 #define LAST_ORD_FIELD_SEQ 0x40
61 #define LFN_END_MARK 0xFFFF
62 #define LFN_TERM_MARK 0x0000
63 #define LFN_FIRST_OFF 0x01
64 #define LFN_SIXTH_OFF 0x0E
65 #define LFN_TWELVETH_OFF 0x1C
66 #define LFN_FIRST_SET_CNT 5
67 #define LFN_SEC_SET_CNT 6
68 #define LFN_THIRD_SET_CNT 2
69 #define LFN_FIRST_SET_LEN 10
70 #define LFN_SEC_SET_LEN 12
71 #define LFN_THIRD_SET_LEN 4
72 #define LFN_EMPTY_LEN 2
73 #define LFN_LEN_PER_ENTRY 13
74 #define FNAME_EXTN_SEP_OFF 6
75 #define FNAME_SEQ_NUM_OFF 7
76 #define BYTES_PER_CLUSTER_ENTRY 4
77 #define DIR_ENTRY_LEN 32
78 #define VOL_ID_LEN 4
79 #define VOL_LABEL_LEN 11
80 #define RESERV_LEN 12
81 #define FS_VER_LEN 2
82 #define OEM_NAME_LEN 8
83 #define JUMP_INS_LEN 3
84 #define MAX_FAT_CNT 2
85 #define SPACE_VAL 32
86 #define FILE_READ 0x01
87 #define FILE_WRITE 0X02
88 #define FILE_CREATE_NEW 0x04
89 #define FILE_CREATE_ALWAYS 0x08
90 #define FILE_APPEND 0x10
91 #define ATTR_READ 0x01
92 #define ATTR_HIDDEN 0x02
93 #define ATTR_SYSTEM 0x04
94 #define ATTR_VOL_LABEL 0x08
95 #define ATTR_DIR 0x10
96 #define ATTR_ARCHIVE 0x20
97 #define ATTR_LONG_FNAME 0x0F
98 #define FREE_DIR_ENTRY 0x00
99 #define DEL_DIR_ENTRY 0xE5
100 #define DOT_DIR_ENTRY 0x2E
101 #define ASCII_DIFF 32
102 #define FILE_SEEK_SET 0
103 #define FILE_SEEK_CUR 1
104 #define FILE_SEEK_END 2
105 #define DELIMITER '/'
106 #define EXTN_DELIMITER '.'
107 #define TILDE '~'
108 #define FULL_SHRT_NAME_LEN 13
109 
114 typedef struct __attribute__((__packed__))
115 {
116  u8 jump[JUMP_INS_LEN];
117  u8 OEM_name[OEM_NAME_LEN];
118  u16 bytes_per_sec;
119  u8 sec_per_clus;
120  u16 reserved_sec_cnt;
121  u8 fat_cnt;
122  u16 root_dir_max_cnt;
123  u16 tot_sectors;
124  u8 media_desc;
125  u16 sec_per_fat_fat16;
126  u16 sec_per_track;
127  u16 number_of_heads;
128  u32 hidden_sec_cnt;
129  u32 tol_sector_cnt;
130  u32 sectors_per_fat;
131  u16 ext_flags;
132  u8 fs_version[FS_VER_LEN];
133  u32 root_dir_strt_cluster;
134  u16 fs_info_sector;
135  u16 backup_boot_sector;
136  u8 reserved[RESERV_LEN];
137  u8 drive_number;
138  u8 reserved1;
139  u8 boot_sig;
140  u8 volume_id[VOL_ID_LEN];
141  u8 volume_label[VOL_LABEL_LEN];
142  u8 file_system_type[FILE_SYS_TYPE_LENGTH];
144 
145 
150 typedef struct __attribute__((__packed__))
151 {
152  u8 name[FILE_NAME_SHRT_LEN];
153  u8 extn[FILE_NAME_EXTN_LEN];
154  u8 attr;
155  u8 reserved;
156  u8 crt_time_tenth;
157  u16 crt_time;
158  u16 crt_date;
159  u16 lst_access_date;
160  u16 strt_clus_hword;
161  u16 lst_mod_time;
162  u16 lst_mod_date;
163  u16 strt_clus_lword;
164  u32 size;
166 
171 typedef struct
172 {
174  u8 fname0_4[LFN_FIRST_SET_LEN];
175  u8 flag;
177  u8 chksum;
178  u8 fname6_11[LFN_SEC_SET_LEN];
179  u8 empty[LFN_EMPTY_LEN];
180  u8 fname12_13[LFN_THIRD_SET_LEN];
181 } lfn_entry;
182 
183 struct f_info;
184 typedef struct f_info file_info;
190 struct f_info
191 {
192  int fd;
194  const char *file_path;
199  u8 mode;
203 };
204 
205 
210 typedef struct
211 {
212  char name[FULL_SHRT_NAME_LEN];
213  const char *lname;
214  u32 size;
215  u16 time;
216  u16 date;
217  u8 attr;
218 } file_stat;
219 
228 int mount_file_system(u8 *dev);
229 
238 bool chk_fat32_file_system(u8 *file_sys_type);
239 
249 void cal_region_offsets(boot_sector *bs, u8 *dev);
250 
263 int file_open(const char *file_path,int flags);
264 
265 
287 bool get_dir_entry(char *fname,dir_entry **entry,u8 *strt,
288  u32 strt_clus,bool is_lfn);
289 
290 
305 int lfn_entry_to_name(lfn_entry *entry,char *lfname,int *index);
306 
315 u8 * cluster_to_memory_addr(u32 cluster);
316 
330 int retrieve_file_info(file_info *finfo,dir_entry *entry,u8 mode,
331  u8 *dir_offset,const char *path);
341 u32 get_fat_table_entry(u32 cluster);
342 
350 void convert_to_uppercase(char * file_name);
351 
362 int find_depth(const char *file_path);
363 
383 int create_file(char *lfname,char *shrt_name,u32 clus,dir_entry **entry);
384 
385 
398 int file_read(int fd,char * buffer,int bytes_to_read);
399 
413 int file_write(int fd, const char *buffer,int bytes_to_write);
414 
421 u32 allocate_cluster();
422 
432 void write_fat_table(u32 cluster,u32 value);
433 
442 void get_short_file_name(char * lfname,char *sfname,char seq_num);
443 
459 void fill_lng_fname_entries(char *fname,u8 *entry,u8 chk_sum,int count);
470 u8 calc_check_sum(u8 * dir_entry);
471 
472 
482 int file_close(int fd);
483 
501 int file_seek(int fd,int offset,int whence);
502 
518 int get_seek_off(file_info * file_ptr,u32 strt_cluster,int offset);
519 
536 int get_long_file_name(char *fname,u8 **dir_strt,int *count);
537 
551 int file_rename(const char *old,const char *new);
552 
562 int create_dir(const char *path);
570 int chk_file_lock(const char *path);
578 file_stat * read_dir(int fd);
579 
588 int open_dir(const char *path);
589 
597 int cal_req_dir_cnt(int file_name_len);
598 
610 int file_truncate(const char* tr_pf,int tr_lgh);
611 
621 int file_remove(const char *fl_name);
622 
631 int file_unlink(const char *ul_name);
632 
641 int list_dir(const char *path);
642 
643 #endif
#define OEM_NAME_LEN
Definition: fat32.h:82
dir_entry
Definition: fat32.h:165
u8 ord_field
Definition: fat32.h:173
int file_read(int fd, char *buffer, int bytes_to_read)
Function to read the contents of a file.
Definition: fat32.c:508
#define FILE_NAME_EXTN_LEN
Definition: fat32.h:55
u8 * cluster_to_memory_addr(u32 cluster)
Function to get the corresponding physical address from the given cluster.
Definition: fat32.c:1158
int file_unlink(const char *ul_name)
file_remove - Deletes the file whose name is specified in filename
Definition: fat32.c:1534
u32 allocate_cluster()
Function to find an unused cluster from the FAT table.
Definition: fat32.c:1134
#define JUMP_INS_LEN
Definition: fat32.h:83
u32 bytes_traversed
Definition: fat32.h:200
u8 calc_check_sum(u8 *dir_entry)
Function to calculate the checksum for the long file name based on its short name entry...
Definition: fat32.c:1391
u8 * dir_entry_offset
Definition: fat32.h:201
u16 time
Definition: fat32.h:215
int create_dir(const char *path)
Function to create a directory.
Definition: fat32.c:980
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.
Definition: fat32.c:707
u32 size
Definition: fat32.h:214
void get_short_file_name(char *lfname, char *sfname, char seq_num)
Function to extract the short file name given its long file name.
Definition: fat32.c:1322
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 ...
Definition: fat32.c:466
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.
Definition: fat32.c:389
u32 get_fat_table_entry(u32 cluster)
Function to retrieve the next cluster value in a cluster chain from the FAT table.
Definition: fat32.c:1174
u8 reserved
Definition: fat32.h:176
u8 attr
Definition: fat32.h:217
int file_close(int fd)
Close the opened file based on the file descriptor and free the file structure pointer.
Definition: fat32.c:660
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 ...
Definition: fat32.c:1431
u32 cur_cluster
Definition: fat32.h:196
u32 strt_cluster
Definition: fat32.h:195
int mount_file_system(u8 *dev)
Mount the FAT file system and retrieve its basic information.
Definition: fat32.c:61
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 correspo...
Definition: fat32.c:311
void convert_to_uppercase(char *file_name)
Function to convert the incoming short file name to its corresponding upper case letters.
Definition: fat32.c:1205
int file_open(const char *file_path, int flags)
Function to open a specific file mentioned in the path based on the incoming modes.
Definition: fat32.c:150
Structure containing the basic details of a file/directory.
Definition: fat32.h:210
int file_write(int fd, const char *buffer, int bytes_to_write)
Function to write into the given file with the contents given.
Definition: fat32.c:575
#define LFN_EMPTY_LEN
Definition: fat32.h:72
file_info * next
Definition: fat32.h:202
const char * lname
Definition: fat32.h:213
#define VOL_ID_LEN
Definition: fat32.h:78
dir_entry * entry
Definition: fat32.h:193
#define FULL_SHRT_NAME_LEN
Definition: fat32.h:108
int file_rename(const char *old, const char *new)
Function to rename a file/directory.
Definition: fat32.c:940
#define LFN_THIRD_SET_LEN
Definition: fat32.h:71
u32 file_size
Definition: fat32.h:198
void write_fat_table(u32 cluster, u32 value)
Function to write into the FAT table after assigning an empty cluster to a file.
Definition: fat32.c:1191
#define LFN_FIRST_SET_LEN
Definition: fat32.h:69
#define RESERV_LEN
Definition: fat32.h:80
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.
Definition: fat32.c:801
int cal_req_dir_cnt(int file_name_len)
Definition: fat32.c:1408
u16 date
Definition: fat32.h:216
int file_remove(const char *fl_name)
file_remove - Deletes the file whose name is specified in filename
Definition: fat32.c:1515
#define VOL_LABEL_LEN
Definition: fat32.h:79
int fd
Definition: fat32.h:192
bool chk_fat32_file_system(u8 *file_sys_type)
Verify the type of FAT file system.
Definition: fat32.c:86
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 op...
Definition: fat32.c:870
int chk_file_lock(const char *path)
Definition: fat32.c:1365
s32int count
Definition: virtual_keyboard.c:40
u8 chksum
Definition: fat32.h:177
int open_dir(const char *path)
Function to open a directory.
Definition: fat32.c:999
#define FILE_NAME_SHRT_LEN
Definition: fat32.h:54
const char * file_path
Definition: fat32.h:194
Structure of a file containing its basic information required for many operations.
Definition: fat32.h:190
void cal_region_offsets(boot_sector *bs, u8 *dev)
Function to calculate all the region offsets.
Definition: fat32.c:110
u16 cur_offset
Definition: fat32.h:197
u8 * backup_boot_sector
Definition: fat32.c:36
#define FILE_SYS_TYPE_LENGTH
Definition: fat32.h:46
int lfn_entry_to_name(lfn_entry *entry, char *lfname, int *index)
Function to get the long file name correspoding to a directory slot.
Definition: fat32.c:428
file_stat * read_dir(int fd)
Definition: fat32.c:1045
int list_dir(const char *path)
Function to list the directory contents.
Definition: fat32.c:1014
boot_sector
Definition: fat32.h:143
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 t...
Definition: fat32.c:1252
Structure of the long file name slot.
Definition: fat32.h:171
#define LFN_SEC_SET_LEN
Definition: fat32.h:70
int find_depth(const char *file_path)
Function to calculate the number of iterations needed to reach the file from the given starting path...
Definition: fat32.c:1225
Definition: cpu_mmu.h:126
struct __attribute__((__packed__))
Structure of the boot sector.
Definition: fat32.h:114
u8 mode
Definition: fat32.h:199
#define FS_VER_LEN
Definition: fat32.h:81
u8 flag
Definition: fat32.h:175