NetBurner 3.1
fat.h
1 /*NB_REVISION*/
2 
3 #ifndef _FAT_H_
4 #define _FAT_H_
5 
6 /****************************************************************************
7  *
8  * Copyright (c) 2003-2006 by HCC Embedded
9  *
10  * This software is copyrighted by and is the sole property of
11  * HCC. All rights, title, ownership, or other interests
12  * in the software remain the property of HCC. This
13  * software may only be used in accordance with the corresponding
14  * license agreement. Any unauthorized use, duplication, transmission,
15  * distribution, or disclosure of this software is expressly forbidden.
16  *
17  * This Copyright notice may not be removed or modified without prior
18  * written consent of HCC.
19  *
20  * HCC reserves the right to modify this software without notice.
21  *
22  * HCC Embedded
23  * Budapest 1132
24  * Victor Hugo Utca 11-15
25  * Hungary
26  *
27  * Tel: +36 (1) 450 1302
28  * Fax: +36 (1) 450 1303
29  * http: www.hcc-embedded.com
30  * email: info@hcc-embedded.com
31  *
32  ***************************************************************************/
33 
34 //#ifdef __cplusplus
35 // extern "C" {
36 //#endif
37 
38 #include <effs_fat/api_f.h>
39 #include <effs_fat/port_f.h>
40 #include <effs_fat/udefs_f.h>
41 
42 #define NOR_DRV_NUM 0
43 #define STDRAM_DRV_NUM 1
44 #define MMC_DRV_NUM 2
45 #define CFC_DRV_NUM 3
46 #define HDD_DRV_NUM 3
47 #define FATRAM_DRV_NUM 4
48 #define ONBOARD_MMC_DRV_NUM 5
49 
67 #define f_mountfat f_initvolume
68 
69 #define F_SECTOR_SIZE 512
70 
71 typedef struct
72 {
73  unsigned char jump_code[3];
74  unsigned char OEM_name[8];
75  unsigned short bytes_per_sector;
76  unsigned char sector_per_cluster;
77  unsigned short reserved_sectors;
78  unsigned char number_of_FATs;
79  unsigned short max_root_entry;
80  unsigned short number_of_sectors_less32; /* <32M */
81  unsigned char media_descriptor;
82  unsigned short sector_per_FAT;
83  unsigned short sector_per_Track;
84  unsigned short number_of_heads;
85  unsigned long number_of_hidden_sectors;
86  unsigned long number_of_sectors;
87 
88  /* only on fat32 */
89  unsigned long sector_per_FAT32;
90  unsigned short extflags;
91  unsigned short fsversion;
92  unsigned long rootcluster;
93  unsigned short fsinfo;
94  unsigned short bkbootsec;
95  unsigned char reserved[12];
96 
97  /* fat12-fat16-fat32 */
98  unsigned short logical_drive_num;
99  unsigned char extended_signature;
100  unsigned long serial_number;
101  unsigned char volume_name[11];
102  unsigned char FAT_name[8];
103  unsigned char executable_marker[2];
104 } F_BOOTRECORD;
105 
106 /* number of sectors after mbr */
107 #define F_SPACE_AFTER_MBR 63
108 
109 /* media descriptor to be set in getphy function */
110 //#define F_MEDIADESC_REMOVABLE 0xf0
111 //#define F_MEDIADESC_FIX 0xf8
112 
113 #if F_LONGFILENAME
114 
115 typedef struct
116 {
117  W_CHAR name[261]; /* with zero term */
118  unsigned char ord;
119  unsigned char chksum;
120  unsigned char state;
121  unsigned long start;
122  unsigned long end;
123 } F_LFNINT;
124 
125 enum
126 {
127  /* 0 */ F_LFNSTATE_LFN, /* lfn is useable */
128  /* 1 */ F_LFNSTATE_SFN, /* lfn is useable, contains short filename */
129  /* 2 */ F_LFNSTATE_NEXT, /* lfn need more entry */
130  /* 3 */ F_LFNSTATE_INV /* invalid lfn */
131 };
132 
133 #endif /* F_LONGFILENAME */
134 
135 typedef struct
136 {
137  char name[F_MAXNAME]; /* 8+3 filename */
138  char ext[F_MAXEXT]; /* 8+3 extension */
139  unsigned char attr; /* 00ADVSHR */
140 
141  unsigned char ntres;
142  unsigned char crttimetenth;
143  unsigned char crttime[2];
144  unsigned char crtdate[2];
145  unsigned char lastaccessdate[2];
146 
147  unsigned char clusterhi[2]; /* FAT32 only */
148  unsigned char ctime[2];
149  unsigned char cdate[2];
150  unsigned char clusterlo[2]; /* fat12,fat16,fat32 */
151  unsigned char filesize[4];
152 } F_DIRENTRY;
153 
154 /* 1st char in 8+3 if entry is deleted*/
155 #define F_DELETED_CHAR ((char)0xe5)
156 
157 /* lower case name */
158 #define NTRES_LOW 0x08
159 
160 /* define for long filename entry in directory entry*/
161 #define F_ATTR_LFN (F_ATTR_VOLUME | F_ATTR_SYSTEM | F_ATTR_HIDDEN | F_ATTR_READONLY)
162 
163 #if F_LONGFILENAME
164 
165 typedef struct
166 {
167  unsigned char ord;
168 
169  unsigned char lfn_1;
170  unsigned char lfnhi_1;
171 
172  unsigned char lfn_2;
173  unsigned char lfnhi_2;
174 
175  unsigned char lfn_3;
176  unsigned char lfnhi_3;
177 
178  unsigned char lfn_4;
179  unsigned char lfnhi_4;
180 
181  unsigned char lfn_5;
182  unsigned char lfnhi_5;
183 
184  unsigned char attr; /* 00ADVSHR */
185  unsigned char type; /* always 0 */
186 
187  unsigned char chksum;
188 
189  unsigned char lfn_6;
190  unsigned char lfnhi_6;
191 
192  unsigned char lfn_7;
193  unsigned char lfnhi_7;
194 
195  unsigned char lfn_8;
196  unsigned char lfnhi_8;
197 
198  unsigned char lfn_9;
199  unsigned char lfnhi_9;
200 
201  unsigned char lfn_10;
202  unsigned char lfnhi_10;
203 
204  unsigned char lfn_11;
205  unsigned char lfnhi_11;
206 
207  unsigned char clusterlo[2]; /* fat12,fat16,fat32 */
208 
209  unsigned char lfn_12;
210  unsigned char lfnhi_12;
211 
212  unsigned char lfn_13;
213  unsigned char lfnhi_13;
214 
215 } F_LFN;
216 
217 #endif /* F_LONGFILENAME */
218 
219 /* definitions for FAT entry */
220 #define F_CLUSTER_FREE ((unsigned long)0x00000000)
221 #define F_CLUSTER_RESERVED ((unsigned long)0x0ffffff0)
222 #define F_CLUSTER_BAD ((unsigned long)0x0ffffff7)
223 #define F_CLUSTER_LAST ((unsigned long)0x0ffffff8)
224 #define F_CLUSTER_LASTF32R ((unsigned long)0x0fffffff)
225 
226 typedef struct
227 {
228  unsigned long sector; /* start sector */
229  unsigned long num; /* number of sectors */
230 } F_SECTOR;
231 
232 #if F_MAXFILES > 0xffff /* maximum number of files */
233 #error F_MAXFILES should be less than 65535
234 #elif F_MAXFILES > 0x7fff
235 #define F_MAXFILES_SHIFT 16
236 #elif F_MAXFILES > 0x3fff
237 #define F_MAXFILES_SHIFT 15
238 #elif F_MAXFILES > 0x1fff
239 #define F_MAXFILES_SHIFT 14
240 #elif F_MAXFILES > 0x0fff
241 #define F_MAXFILES_SHIFT 13
242 #elif F_MAXFILES > 0x07ff
243 #define F_MAXFILES_SHIFT 12
244 #elif F_MAXFILES > 0x03ff
245 #define F_MAXFILES_SHIFT 11
246 #elif F_MAXFILES > 0x01ff
247 #define F_MAXFILES_SHIFT 10
248 #elif F_MAXFILES > 0x00ff
249 #define F_MAXFILES_SHIFT 9
250 #elif F_MAXFILES > 0x007f
251 #define F_MAXFILES_SHIFT 8
252 #elif F_MAXFILES > 0x003f
253 #define F_MAXFILES_SHIFT 7
254 #elif F_MAXFILES > 0x001f
255 #define F_MAXFILES_SHIFT 6
256 #elif F_MAXFILES > 0x000f
257 #define F_MAXFILES_SHIFT 5
258 #else
259 #define F_MAXFILES_SHIFT 4
260 #endif
261 
262 /* definitions for FN_FILE internally used */
263 
264 typedef struct
265 {
266  int N;
267  char *ptr;
268 #ifdef USE_MALLOC
269  F_POS *pos;
270 #else
271  F_POS *pos;
272  F_POS posbuf[WR_DATACACHE_SIZE];
273 #endif
274 } t_WrDataCache;
275 
276 typedef struct FN_FILEINT FN_FILEINT;
277 
278 typedef struct FN_FILEINT
279 {
280  FN_FILE file;
281  long modified;
282  int drivenum; /* 0-A 1-B 2-C */
283  unsigned long abspos;
284  unsigned long relpos;
285  unsigned long filesize;
286  unsigned char data[F_SECTOR_SIZE];
287  int datawritten;
288  t_WrDataCache WrDataCache;
289  unsigned long startcluster;
290  F_POS pos;
291  F_POS dirpos;
292  long state;
293 #if F_MAXSEEKPOS
294  long seekpos[F_MAXSEEKPOS];
295  long seekprev[F_MAXSEEKPOS];
296  long seekshift;
297 #endif
298  FN_FILEINT *syncfile;
299  char mode;
300  char dummy[3];
301 } _FN_FILEINT;
302 
303 /* this bit signal if synchronization is required in append and read in state */
304 #define F_FILE_ST_SYNC 0x0001
305 #define F_FILE_ST_EOF 0x0002
306 
307 typedef struct
308 {
309  unsigned long clfree;
310  unsigned long clused;
311  unsigned long clbad;
312 } F_CLSPACE;
313 
314 #ifdef FATCACHE_ENABLE
315 typedef struct
316 {
317  unsigned long sector;
318  int modified;
319 } t_CacheDsc;
320 
321 typedef struct
322 {
323  int N;
324  t_CacheDsc *dsc;
325 #ifdef USE_MALLOC
326  unsigned char *data;
327 #else
328  t_CacheDsc dsc_array[FATCACHE_SIZE];
329  unsigned char data[FATCACHE_SIZE * F_SECTOR_SIZE];
330 #endif
331 } t_FatCache;
332 #endif
333 
334 typedef struct
335 {
336  long state;
337 
338  F_BOOTRECORD bootrecord;
339 
340  F_SECTOR firstfat;
341 
342  F_SECTOR root;
343  F_SECTOR data;
344 
345 #ifdef FATCACHE_ENABLE
346  t_FatCache fatcache;
347  unsigned char *fat;
348 #else
349  unsigned char fat[F_SECTOR_SIZE];
350 #endif
351  unsigned long fatsector;
352  long fatmodified;
353 
354 #if F_LONGFILENAME
355 #ifdef DIRCACHE_ENABLE
356 #ifdef USE_MALLOC
357  unsigned char *dircache;
358 #else
359  unsigned char dircache[DIRCACHE_SIZE * F_SECTOR_SIZE];
360 #endif
361  unsigned long dircache_start;
362  unsigned long dircache_size;
363 #endif
364 #endif
365  unsigned char direntry[F_SECTOR_SIZE];
366  unsigned long direntrysector;
367 
368  unsigned long lastalloccluster;
369 
370  W_CHAR *cwd;
371 
372  long mediatype;
373  F_CLSPACE clspace; /* calculated disk space */
374  char cspaceok;
375 #if defined FATBITFIELD_ENABLE && defined USE_MALLOC
376  unsigned char *fatbitfield;
377 #endif
378  int partition;
379  unsigned long sectorstart;
380  unsigned long sectornum;
381  F_PHY phy;
382 
383  F_DRIVER *driver;
384 
385  unsigned char sectorbuffer[F_SECTOR_SIZE];
386 
387 } F_VOLUME;
388 
389 #define F_FAT12_MAX_CLUSTER 0xFF0
390 #define F_FAT16_MAX_CLUSTER 0xFFF0
391 
392 typedef struct
393 {
394  F_VOLUME volumes[FN_MAXVOLUME]; /* volumes */
395  FN_FILEINT files[F_MAXFILES];
396  unsigned long drvbldnum; /* drive build number for file.reference */
397 } FN_FILESYSTEM;
398 
399 extern FN_FILESYSTEM f_filesystem;
400 
401 #if (!FN_CAPI_USED)
402 typedef struct
403 {
404  long ID; /* task id */
405  int f_curdrive; /* current drive */
406 
407  struct
408  {
409  W_CHAR cwd[FN_MAXPATH]; /* current working folders in this volume */
410  } f_vols[FN_MAXVOLUME];
411 
412  FN_MUTEX_TYPE *pmutex;
413  unsigned char current_bank;
414  int lasterror; /* last error in this task */
415 } F_MULTI;
416 #endif
417 
418 /* current file opening modes */
419 
420 enum
421 {
422  /* 0 */ FN_FILE_CLOSE,
423  /* 1 */ FN_FILE_RD,
424  /* 2 */ FN_FILE_WR,
425  /* 3 */ FN_FILE_A,
426  /* 4 */ FN_FILE_RDP,
427  /* 5 */ FN_FILE_WRP,
428  /* 6 */ FN_FILE_AP,
429  /* 7 */ FN_FILE_WRERR,
430  /* 8 */ FN_FILE_LOCKED
431 };
432 
433 #define FN_FILE_ABORT_FLAG 0x40 /* signal for file is aborted */
434 
435 /* current drive modes */
436 
437 enum
438 {
439  /* 0 */ F_STATE_NONE,
440  /* 1 */ F_STATE_NEEDMOUNT,
441  /* 2 */ F_STATE_WORKING
442 };
443 
444 /****************************************************************************
445  *
446  * externed functions
447  *
448  ***************************************************************************/
449 
450 // extern int fn_init(void);
451 // extern char *fn_getversion(void);
452 extern int fn_delvolume(F_MULTI *fm, int drvnumber);
453 extern int fn_get_volume_count(F_MULTI *fm);
454 extern int fn_get_volume_list(F_MULTI *fm, int *buf);
455 extern int fn_checkvolume(F_MULTI *fm, int drvnumber);
456 extern int fn_format(F_MULTI *fm, int drivenum, long fattype);
457 extern int fn_getcwd(F_MULTI *fm, char *buffer, int maxlen);
458 extern int fn_getdcwd(F_MULTI *fm, int drivenum, char *buffer, int maxlen);
459 extern int fn_chdrive(F_MULTI *fm, int drivenum);
460 extern int fn_getdrive(F_MULTI *fm);
461 extern int fn_getfreespace(F_MULTI *fm, int drivenum, FN_SPACE *pspace);
462 
463 extern int fn_chdir(F_MULTI *fm, const char *dirname);
464 extern int fn_mkdir(F_MULTI *fm, const char *dirname);
465 extern int fn_rmdir(F_MULTI *fm, const char *dirname);
466 
467 extern int fn_findfirst(F_MULTI *fm, const char *filename, FN_FIND *find);
468 extern int fn_findnext(F_MULTI *fm, FN_FIND *find);
469 extern int fn_rename(F_MULTI *fm, const char *filename, const char *newname);
470 extern int fn_move(F_MULTI *fm, const char *filename, const char *newname);
471 extern long fn_filelength(F_MULTI *fm, const char *filename);
472 
473 extern int fn_close(F_MULTI *fm, FN_FILE *filehandle);
474 extern int fn_flush(F_MULTI *fm, FN_FILE *file);
475 extern FN_FILE *fn_open(F_MULTI *fm, const char *filename, const char *mode);
476 extern FN_FILE *fn_truncate(F_MULTI *fm, const char *filename, unsigned long length);
477 
478 extern long fn_read(F_MULTI *fm, void *buf, long size, long size_st, FN_FILE *filehandle);
479 extern long fn_write(F_MULTI *fm, const void *buf, long size, long size_st, FN_FILE *filehandle);
480 
481 extern int fn_seek(F_MULTI *fm, FN_FILE *filehandle, long offset, long whence);
482 extern int fn_seteof(F_MULTI *fm, FN_FILE *filehandle);
483 
484 extern long fn_tell(F_MULTI *fm, FN_FILE *filehandle);
485 extern int fn_getc(F_MULTI *fm, FN_FILE *filehandle);
486 extern int fn_putc(F_MULTI *fm, int ch, FN_FILE *filehandle);
487 extern int fn_rewind(F_MULTI *fm, FN_FILE *filehandle);
488 extern int fn_eof(F_MULTI *, FN_FILE *filehandle);
489 
490 extern int fn_gettimedate(F_MULTI *fm, const char *filename, unsigned short *pctime, unsigned short *pcdate);
491 extern int fn_settimedate(F_MULTI *fm, const char *filename, unsigned short ctime, unsigned short cdate);
492 extern int fn_delete(F_MULTI *fm, const char *filename);
493 extern int fn_stat(F_MULTI *fm, const char *filename, F_STAT *stat);
494 
495 extern int fn_getattr(F_MULTI *fm, const char *filename, unsigned char *attr);
496 extern int fn_setattr(F_MULTI *fm, const char *filename, unsigned char attr);
497 
498 extern int fn_getlabel(F_MULTI *fm, int drivenum, char *label, long len);
499 extern int fn_setlabel(F_MULTI *fm, int drivenum, const char *label);
500 
501 extern int fn_get_oem(F_MULTI *fm, int drivenum, char *str, long maxlen);
502 
503 #ifdef HCC_UNICODE
504 extern int fn_wgetcwd(F_MULTI *fm, wchar *buffer, int maxlen);
505 extern int fn_wgetdcwd(F_MULTI *fm, int drivenum, wchar *buffer, int maxlen);
506 extern int fn_wchdir(F_MULTI *fm, const wchar *dirname);
507 extern int fn_wmkdir(F_MULTI *fm, const wchar *dirname);
508 extern int fn_wrmdir(F_MULTI *fm, const wchar *dirname);
509 extern int fn_wfindfirst(F_MULTI *fm, const wchar *filename, FN_WFIND *find);
510 extern int fn_wfindnext(F_MULTI *fm, FN_WFIND *find);
511 extern int fn_wrename(F_MULTI *fm, const wchar *filename, const wchar *newname);
512 extern int fn_wmove(F_MULTI *fm, const wchar *filename, const wchar *newname);
513 extern long fn_wfilelength(F_MULTI *fm, const wchar *filename);
514 extern FN_FILE *fn_wopen(F_MULTI *fm, const wchar *filename, const wchar *mode);
515 extern FN_FILE *fn_wtruncate(F_MULTI *fm, const wchar *filename, unsigned long length);
516 extern int fn_wstat(F_MULTI *fm, const wchar *filename, F_STAT *stat);
517 extern int fn_wgettimedate(F_MULTI *fm, const wchar *filename, unsigned short *pctime, unsigned short *pcdate);
518 extern int fn_wsettimedate(F_MULTI *fm, const wchar *filename, unsigned short ctime, unsigned short cdate);
519 extern int fn_wdelete(F_MULTI *fm, const wchar *filename);
520 extern int fn_wgetattr(F_MULTI *fm, const wchar *filename, unsigned char *attr);
521 extern int fn_wsetattr(F_MULTI *fm, const wchar *filename, unsigned char attr);
522 #endif
523 
524 #include "fat_m.h"
525 
526 #define _f_toupper(ch) (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
527 
528 #ifdef HCC_UNICODE
529 extern wchar *_towchar(wchar *nconv, const char *s);
530 #endif
531 extern int _f_addentry(F_VOLUME *vi, F_NAME *fsname, F_POS *pos, F_DIRENTRY **pde);
532 extern int _f_getdirsector(F_VOLUME *vi, unsigned long sector);
533 
534 //#ifdef __cplusplus
535 //}
536 //#endif
537 
538 /****************************************************************************
539  *
540  * end of fat.h
541  *
542  ***************************************************************************/
543 
544 #endif /* _FAT_H_ */
Embedded Flash File System API - FAT.