]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/fat/v2_0/src/fatfs.h
Initial revision
[karo-tx-redboot.git] / packages / fs / fat / v2_0 / src / fatfs.h
1 #ifndef CYGONCE_FATFS_FATFS_H
2 #define CYGONCE_FATFS_FATFS_H
3 //==========================================================================
4 //
5 //      fatfs.h
6 //
7 //      FAT file system header 
8 //
9 //==========================================================================
10 //####ECOSGPLCOPYRIGHTBEGIN####
11 // -------------------------------------------
12 // This file is part of eCos, the Embedded Configurable Operating System.
13 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc. 
14 //
15 // eCos is free software; you can redistribute it and/or modify it under
16 // the terms of the GNU General Public License as published by the Free
17 // Software Foundation; either version 2 or (at your option) any later version.
18 //
19 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
20 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22 // for more details.
23 //
24 // You should have received a copy of the GNU General Public License along
25 // with eCos; if not, write to the Free Software Foundation, Inc.,
26 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 //
28 // As a special exception, if other files instantiate templates or use macros
29 // or inline functions from this file, or you compile this file and link it
30 // with other works to produce a work based on this file, this file does not
31 // by itself cause the resulting work to be covered by the GNU General Public
32 // License. However the source code for this file must still be made available
33 // in accordance with section (3) of the GNU General Public License.
34 //
35 // This exception does not invalidate any other reasons why a work based on
36 // this file might be covered by the GNU General Public License.
37 //
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):           Savin Zlobec <savin@elatec.si> 
44 // Date:                2003-06-29
45 //
46 //####DESCRIPTIONEND####
47 //
48 //==========================================================================
49
50 #include <pkgconf/fs_fat.h>
51
52 #include <cyg/infra/cyg_type.h>
53 #include <cyg/io/io.h>
54 #include <cyg/fileio/fileio.h>
55 #include <blib/blib.h>
56
57 #include <unistd.h>
58 #include <sys/types.h>
59 #include <fcntl.h>
60 #include <sys/stat.h>
61 #include <errno.h>
62 #include <dirent.h>
63
64 #include <stdlib.h>
65 #include <string.h>
66
67 // --------------------------------------------------------------------------
68
69 #define FATFS_HASH_TABLE_SIZE CYGNUM_FS_FAT_NODE_HASH_TABLE_SIZE 
70
71 #define FATFS_NODE_POOL_SIZE  CYGNUM_FS_FAT_NODE_POOL_SIZE
72
73 #ifdef CYGDBG_FS_FAT_NODE_CACHE_EXTRA_CHECKS
74 # define FATFS_NODE_CACHE_EXTRA_CHECKS 1
75 #endif
76
77 // --------------------------------------------------------------------------
78
79 // Node cache tracing support
80 //#define FATFS_TRACE_NODE_CACHE 1
81
82 // FAT dir entry operations tracing support 
83 //#define FATFS_TRACE_DIR_ENTRY 1
84
85 // FAT clusters operations tracing support 
86 //#define FATFS_TRACE_CLUSTER 1
87
88 // FAT data tracing support 
89 //#define FATFS_TRACE_DATA 1
90
91 // FAT file operations tracing support 
92 //#define FATFS_TRACE_FILE_OP 1
93
94 // FAT filesystem operations tracing support 
95 //#define FATFS_TRACE_FS_OP 1
96
97 // --------------------------------------------------------------------------
98
99 typedef enum fatfs_type_e
100 {
101     FATFS_FAT12 = 0,    
102     FATFS_FAT16,         
103     FATFS_FAT32         
104 } fatfs_type_t;
105
106 typedef struct fatfs_data_pos_s
107 {
108     cyg_uint32 cluster;      // Cluster number
109     cyg_uint32 cluster_snum; // Cluster file seq number 
110                              // (0 - first cluster of file,
111                              //  1 - second cluster of file, ...)  
112     cyg_uint32 cluster_pos;  // Position inside cluster
113 } fatfs_data_pos_t;
114
115 typedef struct fatfs_dir_entry_s
116 {
117     char              filename[12+1]; // File name
118     mode_t            mode;           // Node type
119     size_t            size;           // Size of file in bytes
120     time_t            ctime;          // Creation timestamp
121     time_t            atime;          // Last access timestamp
122     time_t            mtime;          // Last write timestamp
123     cyg_uint8         priv_data;      // Private data
124     cyg_uint32        cluster;        // First cluster number
125     cyg_uint32        parent_cluster; // First cluster of parent dentry
126     fatfs_data_pos_t  disk_pos;       // Position of dir entry on disk
127 #ifdef CYGCFG_FS_FAT_USE_ATTRIBUTES
128     cyg_fs_attrib_t    attrib;     // Attribute bits for DOS compatability
129 #endif //CYGCFG_FS_FAT_USE_ATTRIBUTES
130 } fatfs_dir_entry_t;
131
132 typedef struct fatfs_node_s
133 {
134     fatfs_dir_entry_t    dentry;     // Dir entry data
135     cyg_ucount32         refcnt;     // Open file/current dir references
136
137     struct fatfs_node_s *list_prev;  // Next node in list
138     struct fatfs_node_s *list_next;  // Prev node in list
139     struct fatfs_node_s *hash_next;  // Next node in hash
140 } fatfs_node_t;
141
142 typedef struct fatfs_hash_table_s 
143 {
144     cyg_uint32     size;                         // Number of slots
145     cyg_uint32     n;                            // Number of nodes 
146     fatfs_node_t  *nodes[FATFS_HASH_TABLE_SIZE]; // Nodes slots
147 } fatfs_hash_table_t;
148
149 typedef struct fatfs_node_list_s
150 {
151     cyg_uint32    size;          // Number of nodes in list
152     fatfs_node_t *first;         // First node in list
153     fatfs_node_t *last;          // Last node in list
154 } fatfs_node_list_t;
155
156 typedef struct fatfs_disk_s
157 {
158     cyg_uint32    sector_size;          // Sector size in bytes
159     cyg_uint32    sector_size_log2;     // Sector size log2
160     cyg_uint32    cluster_size;         // Cluster size in bytes
161     cyg_uint32    cluster_size_log2;    // Cluster size log2 
162     cyg_uint32    fat_tbl_pos;          // Position of the first FAT table
163     cyg_uint32    fat_tbl_size;         // FAT table size in bytes
164     cyg_uint32    fat_tbl_nents;        // Number of entries in FAT table
165     cyg_uint32    fat_tbls_num;         // Number of FAT tables
166     cyg_uint32    fat_root_dir_pos;     // Position of the root dir
167     cyg_uint32    fat_root_dir_size;    // Root dir size in bytes 
168     cyg_uint32    fat_root_dir_nents;   // Max number of entries in root dir
169     cyg_uint32    fat_root_dir_cluster; // Cluster number of root dir (FAT32) 
170     cyg_uint32    fat_data_pos;         // Position of data area
171     fatfs_type_t  fat_type;             // Type of FAT - 12, 16 or 32 
172     
173     cyg_io_handle_t  dev_h;           // Disk device handle
174     fatfs_node_t    *root;            // Root dir node
175
176     cyg_uint8       *bcache_mem;      // Block cache memory base
177     cyg_blib_t       blib;            // Block cache and access library instance
178
179     fatfs_node_t  node_pool_base[FATFS_NODE_POOL_SIZE]; // Node pool base   
180     fatfs_node_t *node_pool[FATFS_NODE_POOL_SIZE];      // Node pool 
181     cyg_uint32    node_pool_free_cnt;                   // Node pool free cnt
182     
183     fatfs_node_list_t  live_nlist;    // List of nodes with refcnt > 0
184     fatfs_node_list_t  dead_nlist;    // List of nodes with refcnt == 0
185     fatfs_hash_table_t node_hash;     // Hash of nodes in live and dead lists
186 } fatfs_disk_t;
187
188 // --------------------------------------------------------------------------
189
190 int  fatfs_init(fatfs_disk_t *disk);
191
192 void fatfs_get_root_dir_entry(fatfs_disk_t *disk, fatfs_dir_entry_t *dentry);
193
194 bool fatfs_is_root_dir_dentry(fatfs_dir_entry_t *dentry);
195
196 int  fatfs_get_disk_usage(fatfs_disk_t *disk,
197                           cyg_uint32   *total_clusters,
198                           cyg_uint32   *free_clusters);
199
200 int  fatfs_initpos(fatfs_disk_t      *disk,
201                    fatfs_dir_entry_t *file,
202                    fatfs_data_pos_t  *pos);
203
204 int  fatfs_setpos(fatfs_disk_t      *disk,
205                   fatfs_dir_entry_t *file,
206                   fatfs_data_pos_t  *pos,
207                   cyg_uint32         offset);
208
209 cyg_uint32 fatfs_getpos(fatfs_disk_t      *disk, 
210                         fatfs_dir_entry_t *file,
211                         fatfs_data_pos_t  *pos);
212
213 int  fatfs_read_dir_entry(fatfs_disk_t      *disk,
214                           fatfs_dir_entry_t *dir,
215                           fatfs_data_pos_t  *pos,
216                           fatfs_dir_entry_t *dentry);
217
218 int  fatfs_write_dir_entry(fatfs_disk_t *disk, fatfs_dir_entry_t *dentry);
219
220 int  fatfs_delete_file(fatfs_disk_t *disk, fatfs_dir_entry_t *file);
221
222 int  fatfs_create_file(fatfs_disk_t      *disk, 
223                        fatfs_dir_entry_t *dir, 
224                        const char        *name,
225                        int                namelen,
226                        fatfs_dir_entry_t *dentry);
227
228 int  fatfs_create_dir(fatfs_disk_t      *disk, 
229                       fatfs_dir_entry_t *dir, 
230                       const char        *name,
231                       int                namelen,
232                       fatfs_dir_entry_t *dentry);
233
234 int  fatfs_trunc_file(fatfs_disk_t *disk, fatfs_dir_entry_t *file);
235
236 int  fatfs_rename_file(fatfs_disk_t      *disk,
237                        fatfs_dir_entry_t *dir1,
238                        fatfs_dir_entry_t *target,
239                        fatfs_dir_entry_t *dir2,
240                        const char        *name,
241                        int                namelen);
242
243 int  fatfs_read_data(fatfs_disk_t      *disk,
244                      fatfs_dir_entry_t *file,
245                      fatfs_data_pos_t  *pos,
246                      void              *data,
247                      cyg_uint32        *len);
248
249 int  fatfs_write_data(fatfs_disk_t      *disk,
250                       fatfs_dir_entry_t *file,
251                       fatfs_data_pos_t  *pos,
252                       void              *data,
253                       cyg_uint32        *len);
254
255 // --------------------------------------------------------------------------
256
257 void fatfs_node_cache_init(fatfs_disk_t *disk);
258
259 void fatfs_node_cache_flush(fatfs_disk_t *disk);
260
261 fatfs_node_t *fatfs_node_alloc(fatfs_disk_t *disk, fatfs_dir_entry_t *dentry);
262
263 void fatfs_node_ref(fatfs_disk_t *disk, fatfs_node_t *node);
264
265 void fatfs_node_unref(fatfs_disk_t *disk, fatfs_node_t *node);
266
267 void fatfs_node_touch(fatfs_disk_t *disk, fatfs_node_t *node);
268
269 void fatfs_node_rehash(fatfs_disk_t *disk, fatfs_node_t *node);
270
271 void fatfs_node_free(fatfs_disk_t *disk, fatfs_node_t *node);
272
273 fatfs_node_t* fatfs_node_find(fatfs_disk_t *disk, 
274                               const char   *name, 
275                               unsigned int  namelen, 
276                               cyg_uint32    parent_cluster);
277
278 int  fatfs_get_live_node_count(fatfs_disk_t *disk);
279
280 int  fatfs_get_dead_node_count(fatfs_disk_t *disk);
281
282 // --------------------------------------------------------------------------
283 // Support routines
284 // These enable the definition of local versions of certain routines
285 // if the given packages are not present.
286
287 #ifndef CYGPKG_LIBC_I18N
288
289 __externC int toupper( int c );
290
291 #endif
292
293 #ifndef CYGFUN_LIBC_STRING_BSD_FUNCS
294
295 __externC int strcasecmp( const char *s1, const char *s2 );
296
297 __externC int strncasecmp( const char *s1, const char *s2, size_t n );
298
299 #endif
300
301 // --------------------------------------------------------------------------
302
303 #endif // CYGONCE_FATFS_FATFS_H
304
305 // --------------------------------------------------------------------------
306 // EOF fatfs.h