NetBurner 3.5.6
PDF Version
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: [email protected]
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
71typedef 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
115typedef 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
125enum
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
135typedef 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
165typedef 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
226typedef 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
264typedef struct
265{
266 uint16_t N;
267 uint16_t siz;
268 char *ptr;
269#ifdef USE_MALLOC
270 F_POS *pos;
271#else
272 F_POS *pos;
273 F_POS posbuf[WR_DATACACHE_SIZE];
274#endif
275} t_WrDataCache;
276
277typedef struct FN_FILEINT FN_FILEINT;
278
279typedef struct FN_FILEINT
280{
281 FN_FILE file;
282 long modified;
283 int drivenum; /* 0-A 1-B 2-C */
284 unsigned long abspos;
285 unsigned long relpos;
286 unsigned long filesize;
287 unsigned char data[F_SECTOR_SIZE];
288 int datawritten;
289 t_WrDataCache WrDataCache;
290 unsigned long startcluster;
291 F_POS pos;
292 F_POS dirpos;
293 long state;
294#if F_MAXSEEKPOS
295 long seekpos[F_MAXSEEKPOS];
296 long seekprev[F_MAXSEEKPOS];
297 long seekshift;
298#endif
299 FN_FILEINT *syncfile;
300 char mode;
301 char dummy[3];
302} _FN_FILEINT;
303
304/* this bit signal if synchronization is required in append and read in state */
305#define F_FILE_ST_SYNC 0x0001
306#define F_FILE_ST_EOF 0x0002
307
308typedef struct
309{
310 unsigned long clfree;
311 unsigned long clused;
312 unsigned long clbad;
313} F_CLSPACE;
314
315#ifdef FATCACHE_ENABLE
316typedef struct
317{
318 unsigned long sector;
319 int modified;
320} t_CacheDsc;
321
322typedef struct
323{
324 int N;
325 t_CacheDsc *dsc;
326#ifdef USE_MALLOC
327 unsigned char *data;
328#else
329 t_CacheDsc dsc_array[FATCACHE_SIZE];
330 unsigned char data[FATCACHE_SIZE * F_SECTOR_SIZE];
331#endif
332} t_FatCache;
333#endif
334
335typedef struct
336{
337 long state;
338
339 F_BOOTRECORD bootrecord;
340
341 F_SECTOR firstfat;
342
343 F_SECTOR root;
344 F_SECTOR data;
345
346#ifdef FATCACHE_ENABLE
347 t_FatCache fatcache;
348 unsigned char *fat;
349#else
350 unsigned char fat[F_SECTOR_SIZE];
351#endif
352 unsigned long fatsector;
353 long fatmodified;
354
355#if F_LONGFILENAME
356#ifdef DIRCACHE_ENABLE
357#ifdef USE_MALLOC
358 unsigned char *dircache;
359#else
360 unsigned char dircache[DIRCACHE_SIZE * F_SECTOR_SIZE];
361#endif
362 unsigned long dircache_start;
363 unsigned long dircache_size;
364#endif
365#endif
366 unsigned char direntry[F_SECTOR_SIZE];
367 unsigned long direntrysector;
368
369 unsigned long lastalloccluster;
370
371 W_CHAR *cwd;
372
373 long mediatype;
374 F_CLSPACE clspace; /* calculated disk space */
375 char cspaceok;
376#if defined FATBITFIELD_ENABLE && defined USE_MALLOC
377 unsigned char *fatbitfield;
378#endif
379 int partition;
380 unsigned long sectorstart;
381 unsigned long sectornum;
382 F_PHY phy;
383
384 F_DRIVER *driver;
385
386 unsigned char sectorbuffer[F_SECTOR_SIZE];
387
388} F_VOLUME;
389
390#define F_FAT12_MAX_CLUSTER 0xFF0
391#define F_FAT16_MAX_CLUSTER 0xFFF0
392
393typedef struct
394{
395 F_VOLUME volumes[FN_MAXVOLUME]; /* volumes */
396 FN_FILEINT files[F_MAXFILES];
397 unsigned long drvbldnum; /* drive build number for file.reference */
398} FN_FILESYSTEM;
399
400extern FN_FILESYSTEM f_filesystem;
401
402#if (!FN_CAPI_USED)
403typedef struct
404{
405 long ID; /* task id */
406 int f_curdrive; /* current drive */
407
408 struct
409 {
410 W_CHAR cwd[FN_MAXPATH]; /* current working folders in this volume */
411 } f_vols[FN_MAXVOLUME];
412
413 FN_MUTEX_TYPE *pmutex;
414 unsigned char current_bank;
415 int lasterror; /* last error in this task */
416} F_MULTI;
417#endif
418
419/* current file opening modes */
420
421enum
422{
423 /* 0 */ FN_FILE_CLOSE,
424 /* 1 */ FN_FILE_RD,
425 /* 2 */ FN_FILE_WR,
426 /* 3 */ FN_FILE_A,
427 /* 4 */ FN_FILE_RDP,
428 /* 5 */ FN_FILE_WRP,
429 /* 6 */ FN_FILE_AP,
430 /* 7 */ FN_FILE_WRERR,
431 /* 8 */ FN_FILE_LOCKED
432};
433
434#define FN_FILE_ABORT_FLAG 0x40 /* signal for file is aborted */
435
436/* current drive modes */
437
438enum
439{
440 /* 0 */ F_STATE_NONE,
441 /* 1 */ F_STATE_NEEDMOUNT,
442 /* 2 */ F_STATE_WORKING
443};
444
445/****************************************************************************
446 *
447 * externed functions
448 *
449 ***************************************************************************/
450
451// extern int fn_init(void);
452// extern char *fn_getversion(void);
453extern int fn_delvolume(F_MULTI *fm, int drvnumber);
454extern int fn_get_volume_count(F_MULTI *fm);
455extern int fn_get_volume_list(F_MULTI *fm, int *buf);
456extern int fn_checkvolume(F_MULTI *fm, int drvnumber);
457extern int fn_format(F_MULTI *fm, int drivenum, long fattype);
458extern int fn_getcwd(F_MULTI *fm, char *buffer, int maxlen);
459extern int fn_getdcwd(F_MULTI *fm, int drivenum, char *buffer, int maxlen);
460extern int fn_chdrive(F_MULTI *fm, int drivenum);
461extern int fn_getdrive(F_MULTI *fm);
462extern int fn_getfreespace(F_MULTI *fm, int drivenum, FN_SPACE *pspace);
463
464extern int fn_chdir(F_MULTI *fm, const char *dirname);
465extern int fn_mkdir(F_MULTI *fm, const char *dirname);
466extern int fn_rmdir(F_MULTI *fm, const char *dirname);
467
468extern int fn_findfirst(F_MULTI *fm, const char *filename, FN_FIND *find);
469extern int fn_findnext(F_MULTI *fm, FN_FIND *find);
470extern int fn_rename(F_MULTI *fm, const char *filename, const char *newname);
471extern int fn_move(F_MULTI *fm, const char *filename, const char *newname);
472extern long fn_filelength(F_MULTI *fm, const char *filename);
473
474extern int fn_close(F_MULTI *fm, FN_FILE *filehandle);
475extern int fn_flush(F_MULTI *fm, FN_FILE *file);
476extern FN_FILE *fn_open(F_MULTI *fm, const char *filename, const char *mode);
477extern FN_FILE *fn_truncate(F_MULTI *fm, const char *filename, unsigned long length);
478
479extern long fn_read(F_MULTI *fm, void *buf, long size, long size_st, FN_FILE *filehandle);
480extern long fn_write(F_MULTI *fm, const void *buf, long size, long size_st, FN_FILE *filehandle);
481
482extern int fn_seek(F_MULTI *fm, FN_FILE *filehandle, long offset, long whence);
483extern int fn_seteof(F_MULTI *fm, FN_FILE *filehandle);
484
485extern long fn_tell(F_MULTI *fm, FN_FILE *filehandle);
486extern int fn_getc(F_MULTI *fm, FN_FILE *filehandle);
487extern int fn_putc(F_MULTI *fm, int ch, FN_FILE *filehandle);
488extern int fn_rewind(F_MULTI *fm, FN_FILE *filehandle);
489extern int fn_eof(F_MULTI *, FN_FILE *filehandle);
490
491extern int fn_gettimedate(F_MULTI *fm, const char *filename, unsigned short *pctime, unsigned short *pcdate);
492extern int fn_settimedate(F_MULTI *fm, const char *filename, unsigned short ctime, unsigned short cdate);
493extern int fn_delete(F_MULTI *fm, const char *filename);
494extern int fn_stat(F_MULTI *fm, const char *filename, F_STAT *stat);
495
496extern int fn_getattr(F_MULTI *fm, const char *filename, unsigned char *attr);
497extern int fn_setattr(F_MULTI *fm, const char *filename, unsigned char attr);
498
499extern int fn_getlabel(F_MULTI *fm, int drivenum, char *label, long len);
500extern int fn_setlabel(F_MULTI *fm, int drivenum, const char *label);
501
502extern int fn_get_oem(F_MULTI *fm, int drivenum, char *str, long maxlen);
503
504#ifdef HCC_UNICODE
505extern int fn_wgetcwd(F_MULTI *fm, wchar *buffer, int maxlen);
506extern int fn_wgetdcwd(F_MULTI *fm, int drivenum, wchar *buffer, int maxlen);
507extern int fn_wchdir(F_MULTI *fm, const wchar *dirname);
508extern int fn_wmkdir(F_MULTI *fm, const wchar *dirname);
509extern int fn_wrmdir(F_MULTI *fm, const wchar *dirname);
510extern int fn_wfindfirst(F_MULTI *fm, const wchar *filename, FN_WFIND *find);
511extern int fn_wfindnext(F_MULTI *fm, FN_WFIND *find);
512extern int fn_wrename(F_MULTI *fm, const wchar *filename, const wchar *newname);
513extern int fn_wmove(F_MULTI *fm, const wchar *filename, const wchar *newname);
514extern long fn_wfilelength(F_MULTI *fm, const wchar *filename);
515extern FN_FILE *fn_wopen(F_MULTI *fm, const wchar *filename, const wchar *mode);
516extern FN_FILE *fn_wtruncate(F_MULTI *fm, const wchar *filename, unsigned long length);
517extern int fn_wstat(F_MULTI *fm, const wchar *filename, F_STAT *stat);
518extern int fn_wgettimedate(F_MULTI *fm, const wchar *filename, unsigned short *pctime, unsigned short *pcdate);
519extern int fn_wsettimedate(F_MULTI *fm, const wchar *filename, unsigned short ctime, unsigned short cdate);
520extern int fn_wdelete(F_MULTI *fm, const wchar *filename);
521extern int fn_wgetattr(F_MULTI *fm, const wchar *filename, unsigned char *attr);
522extern int fn_wsetattr(F_MULTI *fm, const wchar *filename, unsigned char attr);
523#endif
524
525#include "fat_m.h"
526
527#define _f_toupper(ch) (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
528
529#ifdef HCC_UNICODE
530extern wchar *_towchar(wchar *nconv, const char *s);
531#endif
532extern int _f_addentry(F_VOLUME *vi, F_NAME *fsname, F_POS *pos, F_DIRENTRY **pde);
533extern int _f_getdirsector(F_VOLUME *vi, unsigned long sector);
534
535//#ifdef __cplusplus
536//}
537//#endif
538
539/****************************************************************************
540 *
541 * end of fat.h
542 *
543 ***************************************************************************/
544
545#endif /* _FAT_H_ */