1 #ifndef CYGONCE_FATFS_FATFS_H
2 #define CYGONCE_FATFS_FATFS_H
3 //==========================================================================
7 // FAT file system header
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.
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.
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
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.
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.
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.
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): Savin Zlobec <savin@elatec.si>
46 //####DESCRIPTIONEND####
48 //==========================================================================
50 #include <pkgconf/fs_fat.h>
52 #include <cyg/infra/cyg_type.h>
53 #include <cyg/io/io.h>
54 #include <cyg/fileio/fileio.h>
55 #include <blib/blib.h>
58 #include <sys/types.h>
67 // --------------------------------------------------------------------------
69 #define FATFS_HASH_TABLE_SIZE CYGNUM_FS_FAT_NODE_HASH_TABLE_SIZE
71 #define FATFS_NODE_POOL_SIZE CYGNUM_FS_FAT_NODE_POOL_SIZE
73 #ifdef CYGDBG_FS_FAT_NODE_CACHE_EXTRA_CHECKS
74 # define FATFS_NODE_CACHE_EXTRA_CHECKS 1
77 // --------------------------------------------------------------------------
79 // Node cache tracing support
80 //#define FATFS_TRACE_NODE_CACHE 1
82 // FAT dir entry operations tracing support
83 //#define FATFS_TRACE_DIR_ENTRY 1
85 // FAT clusters operations tracing support
86 //#define FATFS_TRACE_CLUSTER 1
88 // FAT data tracing support
89 //#define FATFS_TRACE_DATA 1
91 // FAT file operations tracing support
92 //#define FATFS_TRACE_FILE_OP 1
94 // FAT filesystem operations tracing support
95 //#define FATFS_TRACE_FS_OP 1
97 // --------------------------------------------------------------------------
99 typedef enum fatfs_type_e
106 typedef struct fatfs_data_pos_s
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
115 typedef struct fatfs_dir_entry_s
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
132 typedef struct fatfs_node_s
134 fatfs_dir_entry_t dentry; // Dir entry data
135 cyg_ucount32 refcnt; // Open file/current dir references
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
142 typedef struct fatfs_hash_table_s
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;
149 typedef struct fatfs_node_list_s
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
156 typedef struct fatfs_disk_s
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
173 cyg_io_handle_t dev_h; // Disk device handle
174 fatfs_node_t *root; // Root dir node
176 cyg_uint8 *bcache_mem; // Block cache memory base
177 cyg_blib_t blib; // Block cache and access library instance
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
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
188 // --------------------------------------------------------------------------
190 int fatfs_init(fatfs_disk_t *disk);
192 void fatfs_get_root_dir_entry(fatfs_disk_t *disk, fatfs_dir_entry_t *dentry);
194 bool fatfs_is_root_dir_dentry(fatfs_dir_entry_t *dentry);
196 int fatfs_get_disk_usage(fatfs_disk_t *disk,
197 cyg_uint32 *total_clusters,
198 cyg_uint32 *free_clusters);
200 int fatfs_initpos(fatfs_disk_t *disk,
201 fatfs_dir_entry_t *file,
202 fatfs_data_pos_t *pos);
204 int fatfs_setpos(fatfs_disk_t *disk,
205 fatfs_dir_entry_t *file,
206 fatfs_data_pos_t *pos,
209 cyg_uint32 fatfs_getpos(fatfs_disk_t *disk,
210 fatfs_dir_entry_t *file,
211 fatfs_data_pos_t *pos);
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);
218 int fatfs_write_dir_entry(fatfs_disk_t *disk, fatfs_dir_entry_t *dentry);
220 int fatfs_delete_file(fatfs_disk_t *disk, fatfs_dir_entry_t *file);
222 int fatfs_create_file(fatfs_disk_t *disk,
223 fatfs_dir_entry_t *dir,
226 fatfs_dir_entry_t *dentry);
228 int fatfs_create_dir(fatfs_disk_t *disk,
229 fatfs_dir_entry_t *dir,
232 fatfs_dir_entry_t *dentry);
234 int fatfs_trunc_file(fatfs_disk_t *disk, fatfs_dir_entry_t *file);
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,
243 int fatfs_read_data(fatfs_disk_t *disk,
244 fatfs_dir_entry_t *file,
245 fatfs_data_pos_t *pos,
249 int fatfs_write_data(fatfs_disk_t *disk,
250 fatfs_dir_entry_t *file,
251 fatfs_data_pos_t *pos,
255 // --------------------------------------------------------------------------
257 void fatfs_node_cache_init(fatfs_disk_t *disk);
259 void fatfs_node_cache_flush(fatfs_disk_t *disk);
261 fatfs_node_t *fatfs_node_alloc(fatfs_disk_t *disk, fatfs_dir_entry_t *dentry);
263 void fatfs_node_ref(fatfs_disk_t *disk, fatfs_node_t *node);
265 void fatfs_node_unref(fatfs_disk_t *disk, fatfs_node_t *node);
267 void fatfs_node_touch(fatfs_disk_t *disk, fatfs_node_t *node);
269 void fatfs_node_rehash(fatfs_disk_t *disk, fatfs_node_t *node);
271 void fatfs_node_free(fatfs_disk_t *disk, fatfs_node_t *node);
273 fatfs_node_t* fatfs_node_find(fatfs_disk_t *disk,
275 unsigned int namelen,
276 cyg_uint32 parent_cluster);
278 int fatfs_get_live_node_count(fatfs_disk_t *disk);
280 int fatfs_get_dead_node_count(fatfs_disk_t *disk);
282 // --------------------------------------------------------------------------
284 // These enable the definition of local versions of certain routines
285 // if the given packages are not present.
287 #ifndef CYGPKG_LIBC_I18N
289 __externC int toupper( int c );
293 #ifndef CYGFUN_LIBC_STRING_BSD_FUNCS
295 __externC int strcasecmp( const char *s1, const char *s2 );
297 __externC int strncasecmp( const char *s1, const char *s2, size_t n );
301 // --------------------------------------------------------------------------
303 #endif // CYGONCE_FATFS_FATFS_H
305 // --------------------------------------------------------------------------