source: soft/giet_vm/giet_fat32/fat32.h @ 658

Last change on this file since 658 was 658, checked in by guerin, 9 years ago

fat32: implement _fat_opendir, _fat_closedir, _fat_readdir

  • Property svn:executable set to *
File size: 13.5 KB
Line 
1////////////////////////////////////////////////////////////////////////////////
2// File     : fat32.h
3// Date     : 01/09/2013
4// Author   : Alain Greiner / Marco Jankovic
5// Copyright (c) UPMC-LIP6
6////////////////////////////////////////////////////////////////////////////////
7
8#ifndef _FAT32_H
9#define _FAT32_H
10
11#include "giet_config.h"
12#include "kernel_locks.h"
13
14/*************** Partition Boot Sector Format **********************************/
15//                                     offset |  length
16#define BS_JMPBOOT                          0 ,  3
17#define BS_OEMNAME                          3 ,  8
18#define BPB_BYTSPERSEC                     11 ,  2
19#define BPB_SECPERCLUS                     13 ,  1
20#define BPB_RSVDSECCNT                     14 ,  2
21#define BPB_NUMFATS                        16 ,  1
22#define BPB_ROOTENTCNT                     17 ,  2
23#define BPB_TOTSEC16                       19 ,  2
24#define BPB_MEDIA                          21 ,  1
25#define BPB_FATSZ16                        22 ,  2
26#define BPB_SECPERTRK                      24 ,  2
27#define BPB_NUMHEADS                       26 ,  2
28#define BPB_HIDDSEC                        28 ,  4
29#define BPB_TOTSEC32                       32 ,  4
30#define BPB_PARTITION_TABLE               446 , 64
31
32// FAT 32
33#define BPB_FAT32_FATSZ32                  36 ,  4
34#define BPB_FAT32_EXTFLAGS                 40 ,  2
35#define BPB_FAT32_FSVER                    42 ,  2
36#define BPB_FAT32_ROOTCLUS                 44 ,  4
37#define BPB_FAT32_FSINFO                   48 ,  2
38#define BPB_FAT32_BKBOOTSEC                50 ,  2
39#define BS_FAT32_DRVNUM                    64 ,  1
40#define BS_FAT32_BOOTSIG                   66 ,  1
41#define BS_FAT32_VOLID                     67 ,  4
42#define BS_FAT32_VOLLAB                    71 , 11
43#define BS_FAT32_FILSYSTYPE                82 ,  8
44
45// Partitions
46#define FIRST_PARTITION_ACTIVE            446 ,  8
47#define FIRST_PARTITION_BEGIN_LBA         454 ,  4
48#define FIRST_PARTITION_SIZE              458 ,  4
49#define SECOND_PARTITION_ACTIVE           462 ,  8
50#define SECOND_PARTITION_BEGIN_LBA        470 ,  4
51#define SECOND_PARTITION_SIZE             474 ,  4
52#define THIRD_PARTITION_ACTIVE            478 ,  8
53#define THIRD_PARTITION_BEGIN_LBA         486 ,  4
54#define THIRD_PARTITION_SIZE              490 ,  4
55#define FOURTH_PARTITION_ACTIVE           494 ,  8
56#define FOURTH_PARTITION_BEGIN_LBA        502 ,  4
57#define FOURTH_PARTITION_SIZE             506 ,  4   
58/*******************************************************************************/
59
60#define MBR_SIGNATURE_POSITION            510 , 2
61#define MBR_SIGNATURE_VALUE               0xAA55 
62
63/************** FAT_FS_INFO SECTOR  ********************************************/
64#define FS_SIGNATURE_VALUE_1              0x52526141
65#define FS_SIGNATURE_VALUE_2              0x72724161
66#define FS_SIGNATURE_VALUE_3              0x000055AA 
67#define FS_SIGNATURE_POSITION_1           0   , 4 
68#define FS_SIGNATURE_POSITION_2           484 , 4
69#define FS_SIGNATURE_POSITION_3           508 , 4 
70#define FS_FREE_CLUSTERS                  488 , 4
71#define FS_FREE_CLUSTER_HINT              492 , 4
72/*******************************************************************************/
73
74#define DIR_ENTRY_SIZE          32
75                   
76#define NAME_MAX_SIZE           31
77
78/******* Directory Entry Structure (32 bytes) **********************************/
79//                            offset | length
80#define DIR_NAME                   0 , 11   // dir_entry name
81#define DIR_ATTR                  11 ,  1   // attributes
82#define DIR_NTRES                 12 ,  1   // reserved for the OS       
83#define DIR_CRT_TIMES_TENTH       13 ,  1
84#define DIR_FST_CLUS_HI           20 ,  2   // cluster index 16 MSB bits
85#define DIR_WRT_TIME              22 ,  2   // time of last write
86#define DIR_WRT_DATE              24 ,  2   // date of last write
87#define DIR_FST_CLUS_LO           26 ,  2   // cluster index 16 LSB bit
88#define DIR_FILE_SIZE             28 ,  4   // file size (up to 4 giga bytes)
89/*******************************************************************************/
90
91/******* LFN Directory Entry Structure  (32 bytes) *****************************/
92//                            offset | length
93#define LDIR_ORD                   0 ,  1   // Sequence number (from 0x01 to 0x0f)   
94#define LDIR_NAME_1                1 , 10   // name broken into 3 parts
95#define LDIR_ATTR                 11 ,  1   // attributes (must be 0x0F)
96#define LDIR_TYPE                 12 ,  1   // directory type (must be 0x00)
97#define LDIR_CHKSUM               13 ,  1   // checksum of name in short dir 
98#define LDIR_NAME_2               14 , 12
99#define LDIR_RSVD                 26 ,  2   // artifact of previous fat (must be 0)
100#define LDIR_NAME_3               28 ,  4   
101/*******************************************************************************/
102
103/***********************  DIR_ATTR values  (attributes) ************************/
104#define ATTR_READ_ONLY            0x01
105#define ATTR_HIDDEN               0x02
106#define ATTR_SYSTEM               0x04
107#define ATTR_VOLUME_ID            0x08
108#define ATTR_DIRECTORY            0x10
109#define ATTR_ARCHIVE              0x20
110#define ATTR_LONG_NAME_MASK       0x0f      // READ_ONLY|HIDDEN|SYSTEM|VOLUME_ID
111/*******************************************************************************/
112
113/********************* DIR_ORD special values **********************************/
114#define FREE_ENTRY                0xE5     // this entry is free in the directory
115#define NO_MORE_ENTRY             0x00     // no more entry in the directory
116/*******************************************************************************/
117
118/******************** CLuster Index Special Values *****************************/
119#define FREE_CLUSTER              0x00000000
120#define RESERVED_CLUSTER          0x00000001
121#define BAD_CLUSTER               0x0FFFFFF7
122#define END_OF_CHAIN_CLUSTER_MIN  0x0ffffff8
123#define END_OF_CHAIN_CLUSTER_MAX  0x0fffffff
124/*******************************************************************************/
125
126#define FAT_INITIALISED         0xBABEF00D
127
128#ifndef _FAT32_SHARED
129#define _FAT32_SHARED
130typedef struct fat_file_info_s
131{
132    unsigned int size;
133    unsigned int offset;
134    unsigned int is_dir;
135}   fat_file_info_t;
136
137typedef struct fat_dirent_s
138{
139    unsigned int cluster;
140    unsigned int size;
141    unsigned int is_dir;
142    char name[36];
143}   fat_dirent_t;
144
145#define SEEK_SET                0
146#define SEEK_CUR                1
147
148#define O_RDONLY                0x01
149#define O_TRUNC                 0x10
150#define O_CREATE                0x20
151#endif // _FAT32_SHARED
152
153/********************************************************************************
154  This struct defines a non terminal node in a 64-tree implementing a File-Cache
155  associated to an open file, or the Fat-Cache, associated to the FAT itself.
156  Each node has 64 children, and we use the void* type, because the children
157  can be non-terminal (fat_cache_node_t) or terminal (fat_cache_cluster_t).
158********************************************************************************/
159
160typedef struct fat_cache_node_s
161{
162    void*              children[64];           // pointers on the 64 children
163}   fat_cache_node_t;
164
165
166/********************************************************************************
167  This struct defines a cluster descriptor, that is a leaf cell in a 64-tree.
168  Each cluster descriptor contains a pointer on a 4K bytes buffer, and the
169  lba on block device.
170********************************************************************************/
171
172typedef struct fat_cache_desc_s
173{
174    unsigned int       lba;                      // cluster lba on block device
175    unsigned int       dirty;                    // modified if non zero
176    unsigned char*     buffer;                   // pointer on the 4 Kbytes buffer
177}   fat_cache_desc_t;
178
179
180/********************************************************************************
181  This struct defines a file/directory inode / size = 64 bytes
182********************************************************************************/
183
184typedef struct fat_inode_s
185{
186    struct fat_inode_s*  parent;                 // parent directory inode
187    struct fat_inode_s*  next;                   // next inode in same directory
188    struct fat_inode_s*  child;                  // first children inode (dir only)
189    fat_cache_node_t*    cache;                  // pointer on the file_cache root
190    unsigned int         cluster;                // first cluster index in FAT
191    unsigned int         size;                   // number of bytes (file only)
192    unsigned int         count;                  // number open if file / 0 if dir
193    unsigned short       dentry;                 // directory entry index in parent
194    unsigned char        levels;                 // number of levels in file_cache
195    unsigned char        is_dir;                 // directory if non zero
196    char                 name[32];               // file  directory name
197}   fat_inode_t;
198
199/********************************************************************************
200  This struct defines a file descriptor (handler) / size = 16 bytes
201********************************************************************************/
202
203typedef struct fat_file_desc_s
204{
205    unsigned int         seek;                   // current offset value (bytes)
206    fat_inode_t*         inode;                  // pointer on inode
207    char                 allocated;              // file descriptor allocated
208    char                 read_only;              // write protected
209    char                 reserved[6];            // reserved
210}   fat_file_desc_t;
211
212/********************************************************************************
213  This struct defines a FAT32 File system descriptor
214 *******************************************************************************/
215
216typedef struct fat_desc_s
217{
218    unsigned char       block_buffer[512];       // one block buffer (for FS_INFO)
219    fat_file_desc_t     fd[GIET_OPEN_FILES_MAX]; // file descriptors array
220    spin_lock_t         fat_lock;                // global lock protecting FAT
221    fat_inode_t*        inode_tree_root;         // Inode-Tree root pointer
222    fat_cache_node_t*   fat_cache_root;          // Fat_Cache root pointer
223    unsigned int        fat_cache_levels;        // number of levels in Fat-Cache
224    unsigned int        block_buffer_lba;        // lba of block in block_buffer
225    unsigned int        initialised;             // 0xBABEF00D when FAT initialised
226    unsigned int        sector_size;             // must be 512
227    unsigned int        cluster_size;            // must be 4096
228    unsigned int        fat_lba;                 // lba of first FAT sector
229    unsigned int        fat_sectors;             // number of sectors in FAT region
230    unsigned int        data_lba;                // lba of first DATA sector 
231    unsigned int        data_sectors;            // number of sectors inf DATA region
232    unsigned int        fs_info_lba;             // lba of fs_info
233    unsigned int        first_free_cluster;      // free cluster with smallest index
234    unsigned int        free_clusters_number;    // total number of free clusters
235}   fat_desc_t;
236
237
238/*********************** Extern Functions  *************************************/
239
240extern int _fat_init();         
241
242extern int _fat_open( char*        pathname,               // path from root
243                      unsigned int flags );                // O_CREATE/O_RDONLY
244
245extern int _fat_close( unsigned int fd_id );               // file descriptor
246
247extern int _fat_file_info( unsigned int     fd_id,         // file descriptor
248                           fat_file_info_t* info );        // file info struct
249
250extern int _fat_read( unsigned int fd_id,                  // file descriptor 
251                      void*        buffer,                 // destination buffer
252                      unsigned int count );                // number of bytes
253
254extern int _fat_write( unsigned int fd_id,                 // file descriptor
255                       void*        buffer,                        // source buffer
256                       unsigned int count );               // number of bytes
257
258extern int _fat_lseek( unsigned int fd_id,                 // file descriptor
259                       unsigned int offset,                // new offset value
260                       unsigned int whence );              // command type
261
262extern int _fat_remove( char*        pathname,             // path from root
263                        unsigned int should_be_dir );      // for checking
264
265extern int _fat_rename( char*  old_path,                   // path from root
266                        char*  new_path );                 // path from root
267
268extern int _fat_mkdir( char* pathname );                   // path from root
269
270extern int _fat_opendir( char* pathname );                 // path from root
271
272extern int _fat_closedir( unsigned int fd_id );            // file descriptor
273
274extern int _fat_readdir( unsigned int  fd_id,              // file descriptor
275                         fat_dirent_t* entry );            // directory entry
276
277extern int _fat_list( char* pathname );                    // path from root
278
279extern int _fat_load_no_cache( char*        pathname,      // path from root
280                               unsigned int buffer_vbase,  // buffer base
281                               unsigned int buffer_size ); // buffer size
282
283/*******************************************************************************/
284
285
286#endif
287
288
289// Local Variables:
290// tab-width: 4
291// c-basic-offset: 4
292// c-file-offsets:((innamespace . 0)(inline-open . 0))
293// indent-tabs-mode: nil
294// End:
295// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
296
Note: See TracBrowser for help on using the repository browser.