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