source: rtems/cpukit/libfs/src/dosfs/fat.h @ 0a896eb

4.104.114.95
Last change on this file since 0a896eb was 0a896eb, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/04/08 at 08:33:06

Use "bool" instead of "rtems_boolean|boolean".

  • Property mode set to 100644
File size: 17.0 KB
Line 
1/*
2 *  fat.h
3 *
4 *  Constants/data structures/prototypes for low-level operations on a volume
5 *  with FAT filesystem
6 *
7 *  Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
8 *  Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
9 *
10 *  The license and distribution terms for this file may be
11 *  found in the file LICENSE in this distribution or at
12 *  http://www.rtems.com/license/LICENSE.
13 *
14 *  @(#) $Id$
15 */
16
17#ifndef __DOSFS_FAT_H__
18#define __DOSFS_FAT_H__
19
20#include <string.h>
21
22#include <rtems/seterr.h>
23
24#include <rtems/score/cpu.h>
25#include <errno.h>
26#include <rtems/bdbuf.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#ifndef RC_OK
33#define RC_OK 0
34#endif
35
36/*
37 * Remember that all FAT file system on disk data structure is
38 * "little endian"!
39 * (derived from linux)
40 */
41/*
42 * Conversion from and to little-endian byte order. (no-op on i386/i486)
43 *
44 * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian,
45 * BE = big-endian, c: W = word (16 bits), L = longword (32 bits)
46 */
47
48#if (CPU_BIG_ENDIAN == TRUE)
49#    define CF_LE_W(v) CPU_swap_u16((uint16_t)(v))
50#    define CF_LE_L(v) CPU_swap_u32((uint32_t)(v))
51#    define CT_LE_W(v) CPU_swap_u16((uint16_t)(v))
52#    define CT_LE_L(v) CPU_swap_u32((uint32_t)(v))
53#else 
54#    define CF_LE_W(v) (v)
55#    define CF_LE_L(v) (v)
56#    define CT_LE_W(v) (v)
57#    define CT_LE_L(v) (v)
58#endif
59
60#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
61
62#define FAT_HASH_SIZE   2
63#define FAT_HASH_MODULE FAT_HASH_SIZE
64
65
66#define FAT_SECTOR512_SIZE     512 /* sector size (bytes) */
67#define FAT_SECTOR512_BITS       9 /* log2(SECTOR_SIZE) */
68
69/* maximum + 1 number of clusters for FAT12 */
70#define FAT_FAT12_MAX_CLN      4085
71
72/* maximum + 1 number of clusters for FAT16 */
73#define FAT_FAT16_MAX_CLN      65525
74
75#define FAT_FAT12              0x01
76#define FAT_FAT16              0x02
77#define FAT_FAT32              0x04
78
79#define FAT_UNDEFINED_VALUE     (uint32_t  )0xFFFFFFFF
80
81#define FAT_FAT12_EOC          0x0FF8
82#define FAT_FAT16_EOC          0xFFF8
83#define FAT_FAT32_EOC          (uint32_t  )0x0FFFFFF8
84
85#define FAT_FAT12_FREE         0x0000
86#define FAT_FAT16_FREE         0x0000
87#define FAT_FAT32_FREE         0x00000000
88
89#define FAT_GENFAT_EOC         (uint32_t  )0xFFFFFFFF
90#define FAT_GENFAT_FREE        (uint32_t  )0x00000000
91
92#define FAT_FAT12_SHIFT        0x04
93
94#define FAT_FAT12_MASK         0x00000FFF
95#define FAT_FAT16_MASK         0x0000FFFF
96#define FAT_FAT32_MASK         (uint32_t  )0x0FFFFFFF
97
98#define FAT_MAX_BPB_SIZE       90
99#define FAT_TOTAL_MBR_SIZE    512
100
101/* size of useful information in FSInfo sector */
102#define FAT_USEFUL_INFO_SIZE   12
103
104#define FAT_GET_ADDR(x, ofs)       ((uint8_t *)(x) + (ofs))
105
106#define FAT_GET_VAL8(x, ofs)       (uint8_t)(*((uint8_t *)(x) + (ofs)))
107
108#define FAT_GET_VAL16(x, ofs)                               \
109    (uint16_t)( (*((uint8_t *)(x) + (ofs))) |           \
110                  ((*((uint8_t *)(x) + (ofs) + 1)) << 8) )
111
112#define FAT_GET_VAL32(x, ofs)                                             \
113    (uint32_t)( (uint32_t)(*((uint8_t *)(x) + (ofs))) |             \
114                  ((uint32_t)(*((uint8_t *)(x) + (ofs) + 1)) << 8)  | \
115                  ((uint32_t)(*((uint8_t *)(x) + (ofs) + 2)) << 16) | \
116                  ((uint32_t)(*((uint8_t *)(x) + (ofs) + 3)) << 24) )
117                   
118#define FAT_SET_VAL8(x, ofs,val)                    \
119                 (*((uint8_t *)(x)+(ofs))=(uint8_t)(val))
120 
121#define FAT_SET_VAL16(x, ofs,val) do {              \
122                 FAT_SET_VAL8((x),(ofs),(val));     \
123                 FAT_SET_VAL8((x),(ofs)+1,(val)>>8);\
124                 } while (0)
125
126#define FAT_SET_VAL32(x, ofs,val) do {               \
127                 uint32_t val1 = val;                \
128                 FAT_SET_VAL16((x),(ofs),(val1)&&0xffff);\
129                 FAT_SET_VAL16((x),(ofs)+2,(val1)>>16);\
130                 } while (0)
131
132/* macros to access boot sector fields */
133#define FAT_GET_BR_JMPBOOT(x)                FAT_GET_VAL8( x,  0)
134#define FAT_SET_BR_JMPBOOT(x,val)            FAT_SET_VAL8( x,  0,val)
135
136#define FAT_GET_ADDR_BR_OEMNAME(x)           FAT_GET_ADDR( x,  3)
137#define FAT_BR_OEMNAME_SIZE              (8)
138
139#define FAT_GET_BR_BYTES_PER_SECTOR(x)       FAT_GET_VAL16(x, 11)
140#define FAT_SET_BR_BYTES_PER_SECTOR(x,val)   FAT_SET_VAL16(x, 11,val)
141
142#define FAT_GET_BR_SECTORS_PER_CLUSTER(x)    FAT_GET_VAL8( x, 13)
143#define FAT_SET_BR_SECTORS_PER_CLUSTER(x,val)FAT_SET_VAL8( x, 13,val)
144
145#define FAT_GET_BR_RESERVED_SECTORS_NUM(x)   FAT_GET_VAL16(x, 14)
146#define FAT_SET_BR_RESERVED_SECTORS_NUM(x,val) FAT_SET_VAL16(x, 14,val)
147
148#define FAT_GET_BR_FAT_NUM(x)                FAT_GET_VAL8( x, 16)
149#define FAT_SET_BR_FAT_NUM(x,val)            FAT_SET_VAL8( x, 16,val)
150
151#define FAT_GET_BR_FILES_PER_ROOT_DIR(x)     FAT_GET_VAL16(x, 17)
152#define FAT_SET_BR_FILES_PER_ROOT_DIR(x,val) FAT_SET_VAL16(x, 17,val)
153
154#define FAT_GET_BR_TOTAL_SECTORS_NUM16(x)    FAT_GET_VAL16(x, 19)
155#define FAT_SET_BR_TOTAL_SECTORS_NUM16(x,val)FAT_SET_VAL16(x, 19,val)
156
157#define FAT_GET_BR_MEDIA(x)                  FAT_GET_VAL8( x, 21)
158#define FAT_SET_BR_MEDIA(x,val)              FAT_SET_VAL8( x, 21,val)
159
160#define FAT_GET_BR_SECTORS_PER_FAT(x)        FAT_GET_VAL16(x, 22)
161#define FAT_SET_BR_SECTORS_PER_FAT(x,val)    FAT_SET_VAL16(x, 22,val)
162
163#define FAT_GET_BR_SECTORS_PER_TRACK(x)      FAT_GET_VAL16(x, 24)
164#define FAT_SET_BR_SECTORS_PER_TRACK(x,val)  FAT_SET_VAL16(x, 24,val)
165
166#define FAT_GET_BR_NUMBER_OF_HEADS(x)        FAT_GET_VAL16(x, 26)
167#define FAT_SET_BR_NUMBER_OF_HEADS(x,val)    FAT_SET_VAL16(x, 26,val)
168
169#define FAT_GET_BR_HIDDEN_SECTORS(x)         FAT_GET_VAL32(x, 28)
170#define FAT_SET_BR_HIDDEN_SECTORS(x,val)     FAT_SET_VAL32(x, 28,val)
171
172#define FAT_GET_BR_TOTAL_SECTORS_NUM32(x)    FAT_GET_VAL32(x, 32)
173#define FAT_SET_BR_TOTAL_SECTORS_NUM32(x,val) FAT_SET_VAL32(x, 32,val)
174  /* --- start of FAT12/16 specific fields */
175#define FAT_GET_BR_DRVNUM(x)                 FAT_GET_VAL8( x, 36)
176#define FAT_SET_BR_DRVNUM(x,val)             FAT_SET_VAL8( x, 36,val)
177
178#define FAT_GET_BR_RSVD1(x)                  FAT_GET_VAL8( x, 37)
179#define FAT_SET_BR_RSVD1(x,val)              FAT_SET_VAL8( x, 37,val)
180
181#define FAT_GET_BR_BOOTSIG(x)                FAT_GET_VAL8( x, 38)
182#define FAT_SET_BR_BOOTSIG(x,val)            FAT_SET_VAL8( x, 38,val)
183#define FAT_BR_BOOTSIG_VAL               (0x29)
184
185#define FAT_GET_BR_VOLID(x)                  FAT_GET_VAL32(x, 39)
186#define FAT_SET_BR_VOLID(x,val)              FAT_SET_VAL32(x, 39,val)
187
188#define FAT_GET_ADDR_BR_VOLLAB(x)            FAT_GET_ADDR (x, 43)
189#define FAT_BR_VOLLAB_SIZE               (11)
190
191#define FAT_GET_ADDR_BR_FILSYSTYPE(x)        FAT_GET_ADDR (x, 54)
192#define FAT_BR_FILSYSTYPE_SIZE           (8)
193  /* --- end of FAT12/16 specific fields */
194  /* --- start of FAT32 specific fields */
195#define FAT_GET_BR_SECTORS_PER_FAT32(x)      FAT_GET_VAL32(x, 36)
196#define FAT_SET_BR_SECTORS_PER_FAT32(x,val)  FAT_SET_VAL32(x, 36,val)
197
198#define FAT_GET_BR_EXT_FLAGS(x)              FAT_GET_VAL16(x, 40)
199#define FAT_SET_BR_EXT_FLAGS(x,val)          FAT_SET_VAL16(x, 40,val)
200
201#define FAT_GET_BR_FSVER(x)                  FAT_GET_VAL16(x, 42)
202#define FAT_SET_BR_FSVER(x,val)              FAT_SET_VAL16(x, 42,val)
203
204#define FAT_GET_BR_FAT32_ROOT_CLUSTER(x)     FAT_GET_VAL32(x, 44)
205#define FAT_SET_BR_FAT32_ROOT_CLUSTER(x,val) FAT_SET_VAL32(x, 44,val)
206
207#define FAT_GET_BR_FAT32_FS_INFO_SECTOR(x)   FAT_GET_VAL16(x, 48)
208#define FAT_SET_BR_FAT32_FS_INFO_SECTOR(x,val) FAT_SET_VAL16(x, 48,val)
209
210#define FAT_GET_BR_FAT32_BK_BOOT_SECTOR(x)   FAT_GET_VAL16(x, 50)
211#define FAT_SET_BR_FAT32_BK_BOOT_SECTOR(x,val)  FAT_SET_VAL16(x, 50,val)
212
213#define FAT_GET_ADDR_BR_FAT32_RESERVED(x)    FAT_GET_ADDR (x, 52)
214#define FAT_BR_FAT32_RESERVED_SIZE       (12)
215
216#define FAT_GET_BR_FAT32_DRVNUM(x)           FAT_GET_VAL8( x, 64)
217#define FAT_SET_BR_FAT32_DRVNUM(x,val)       FAT_SET_VAL8( x, 64,val)
218
219#define FAT_GET_BR_FAT32_RSVD1(x)            FAT_GET_VAL8( x, 65)
220#define FAT_SET_BR_FAT32_RSVD1(x,val)        FAT_SET_VAL8( x, 65,val)
221
222#define FAT_GET_BR_FAT32_BOOTSIG(x)          FAT_GET_VAL8( x, 66)
223#define FAT_SET_BR_FAT32_BOOTSIG(x,val)      FAT_SET_VAL8( x, 66,val)
224#define FAT_BR_FAT32_BOOTSIG_VAL         (0x29)
225
226#define FAT_GET_BR_FAT32_VOLID(x)            FAT_GET_VAL32(x, 67)
227#define FAT_SET_BR_FAT32_VOLID(x,val)        FAT_SET_VAL32(x, 67,val)
228
229#define FAT_GET_ADDR_BR_FAT32_VOLLAB(x)      FAT_GET_ADDR (x, 71)
230#define FAT_BR_FAT32_VOLLAB_SIZE         (11)
231
232#define FAT_GET_ADDR_BR_FAT32_FILSYSTYPE(x)  FAT_GET_ADDR (x, 82)
233#define FAT_BR_FAT32_FILSYSTYPE_SIZE     (8)
234  /* --- end of FAT32 specific fields */
235
236#define FAT_GET_BR_SIGNATURE(x)              FAT_GET_VAL16(x,510)
237#define FAT_SET_BR_SIGNATURE(x,val)          FAT_SET_VAL16(x,510,val)
238#define FAT_BR_SIGNATURE_VAL                (0xAA55)
239
240  /*
241   * FAT32 FSINFO description
242   */
243#define FAT_GET_FSINFO_LEAD_SIGNATURE(x)      FAT_GET_VAL32(x,  0)
244#define FAT_SET_FSINFO_LEAD_SIGNATURE(x,val)  FAT_SET_VAL32(x,  0,val)
245#define FAT_FSINFO_LEAD_SIGNATURE_VALUE   (0x41615252)
246
247#define FAT_GET_FSINFO_STRUC_SIGNATURE(x)     FAT_GET_VAL32(x,484)
248#define FAT_SET_FSINFO_STRUC_SIGNATURE(x,val) FAT_SET_VAL32(x,484,val)
249#define FAT_FSINFO_STRUC_SIGNATURE_VALUE  (0x61417272)
250
251#define FAT_GET_FSINFO_TRAIL_SIGNATURE(x)     FAT_GET_VAL32(x,508)
252#define FAT_SET_FSINFO_TRAIL_SIGNATURE(x,val) FAT_SET_VAL32(x,508,val)
253#define FAT_FSINFO_TRAIL_SIGNATURE_VALUE  (0x000055AA)
254/*
255 * I read FSInfo sector from offset 484 to access the information, so offsets
256 * of these fields a relative
257 */
258#define FAT_GET_FSINFO_FREE_CLUSTER_COUNT(x)      FAT_GET_VAL32(x, 4)
259#define FAT_SET_FSINFO_FREE_CLUSTER_COUNT(x,val)  FAT_SET_VAL32(x, 4,val)
260#define FAT_GET_FSINFO_NEXT_FREE_CLUSTER(x)       FAT_GET_VAL32(x, 8)
261#define FAT_SET_FSINFO_NEXT_FREE_CLUSTER(x,val)   FAT_SET_VAL32(x, 8,val)
262
263#define FAT_FSI_INFO                         484
264#define FAT_FSINFO_STRUCT_OFFSET             488
265#define FAT_FSINFO_FREE_CLUSTER_COUNT_OFFSET (FAT_FSINFO_STRUCT_OFFSET+0)
266
267#define FAT_FSINFO_NEXT_FREE_CLUSTER_OFFSET  (FAT_FSINFO_STRUCT_OFFSET+4)
268
269#define FAT_RSRVD_CLN                        0x02
270
271#define FAT_FSI_LEADSIG_SIZE                 0x04
272
273#define FAT_TOTAL_FSINFO_SIZE               512
274
275#define MS_BYTES_PER_CLUSTER_LIMIT           0x8000     /* 32K */
276
277#define FAT_BR_EXT_FLAGS_MIRROR              0x0080
278
279#define FAT_BR_EXT_FLAGS_FAT_NUM             0x000F
280
281#define FAT_BR_MEDIA_FIXED                  0xf8
282
283#define FAT_DIRENTRY_SIZE          32
284
285#define FAT_DIRENTRIES_PER_SEC512  16
286
287/*
288 * Volume descriptor
289 * Description of the volume the FAT filesystem is located on - generally
290 * the fields of the structure correspond to Boot Sector and BPB Structure
291 * fields
292 */
293typedef struct fat_vol_s
294{
295    uint16_t           bps;            /* bytes per sector */
296    uint8_t            sec_log2;       /* log2 of bps */
297    uint8_t            sec_mul;        /* log2 of 512bts sectors number per sector */
298    uint8_t            spc;            /* sectors per cluster */
299    uint8_t            spc_log2;       /* log2 of spc */
300    uint16_t           bpc;            /* bytes per cluster */
301    uint8_t            bpc_log2;       /* log2 of bytes per cluster */
302    uint8_t            fats;           /* number of FATs */
303    uint8_t            type;           /* FAT type */
304    uint32_t           mask;
305    uint32_t           eoc_val;
306    uint16_t           fat_loc;        /* FAT start */
307    uint32_t           fat_length;     /* sectors per FAT */
308    uint32_t           rdir_loc;       /* root directory start */
309    uint16_t           rdir_entrs;     /* files per root directory */
310    uint32_t           rdir_secs;      /* sectors per root directory */
311    uint32_t           rdir_size;      /* root directory size in bytes */
312    uint32_t           tot_secs;       /* total count of sectors */
313    uint32_t           data_fsec;      /* first data sector */
314    uint32_t           data_cls;       /* count of data clusters */
315    uint32_t           rdir_cl;        /* first cluster of the root directory */
316    uint16_t           info_sec;       /* FSInfo Sector Structure location */
317    uint32_t           free_cls;       /* last known free clusters count */
318    uint32_t           next_cl;        /* next free cluster number */
319    uint8_t            mirror;         /* mirroring enabla/disable */
320    uint32_t           afat_loc;       /* active FAT location */
321    uint8_t            afat;           /* the number of active FAT */
322    dev_t              dev;            /* device ID */
323    rtems_disk_device *dd;             /* disk device (see libblock) */
324    void              *private_data;   /* reserved */
325} fat_vol_t;
326
327
328typedef struct fat_cache_s
329{
330    uint32_t            blk_num;
331    bool                modified;
332    uint8_t             state;
333    rtems_bdbuf_buffer *buf;
334} fat_cache_t;
335
336/*
337 * This structure identifies the instance of the filesystem on the FAT
338 * ("fat-file") level.
339 */
340typedef struct fat_fs_info_s
341{
342    fat_vol_t            vol;           /* volume descriptor */
343    rtems_chain_control *vhash;         /* "vhash" of fat-file descriptors */
344    rtems_chain_control *rhash;         /* "rhash" of fat-file descriptors */
345    char                *uino;          /* array of unique ino numbers */
346    uint32_t             index;
347    uint32_t             uino_pool_size; /* size */
348    uint32_t             uino_base;
349    fat_cache_t          c;             /* cache */
350    uint8_t             *sec_buf; /* just placeholder for anything */
351} fat_fs_info_t;
352
353/*
354 * if the name we looking for is file we store not only first data cluster
355 * number, but and cluster number and offset for directory entry for this
356 * name
357 */
358typedef struct fat_auxiliary_s
359{
360    uint32_t   cln;
361    uint32_t   ofs;
362} fat_auxiliary_t;
363
364#define FAT_FAT_OFFSET(fat_type, cln)                  \
365    ((fat_type) & FAT_FAT12 ? ((cln) + ((cln) >> 1)) : \
366     (fat_type) & FAT_FAT16 ? ((cln) << 1)           : \
367     ((cln) << 2))
368
369#define FAT_CLUSTER_IS_ODD(n)  ((n) & 0x0001)
370
371#define FAT12_SHIFT      0x4    /* half of a byte */
372
373/* initial size of array of unique ino */
374#define FAT_UINO_POOL_INIT_SIZE  0x100
375
376/* cache support */
377#define FAT_CACHE_EMPTY   0x0
378#define FAT_CACHE_ACTUAL  0x1
379
380#define FAT_OP_TYPE_READ  0x1
381#define FAT_OP_TYPE_GET   0x2
382
383static inline uint32_t
384fat_cluster_num_to_sector_num(
385    rtems_filesystem_mount_table_entry_t *mt_entry,
386    uint32_t                              cln
387    )
388{
389    register fat_fs_info_t *fs_info = mt_entry->fs_info;
390
391    if ( (cln == 0) && (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)) )
392        return fs_info->vol.rdir_loc;
393
394    return (((cln - FAT_RSRVD_CLN) << fs_info->vol.spc_log2) +
395            fs_info->vol.data_fsec);
396}
397
398static inline uint32_t
399fat_cluster_num_to_sector512_num(
400    rtems_filesystem_mount_table_entry_t *mt_entry,
401    uint32_t                              cln
402    )
403{
404    fat_fs_info_t *fs_info = mt_entry->fs_info;
405
406    if (cln == 1)
407        return 1;
408
409    return (fat_cluster_num_to_sector_num(mt_entry, cln) <<
410            fs_info->vol.sec_mul);
411}
412
413static inline void
414fat_buf_mark_modified(fat_fs_info_t *fs_info)
415{
416    fs_info->c.modified = true;
417}
418
419int
420fat_buf_access(fat_fs_info_t *fs_info, uint32_t   blk, int op_type,
421               rtems_bdbuf_buffer **buf);
422
423int
424fat_buf_release(fat_fs_info_t *fs_info);
425
426ssize_t
427_fat_block_read(rtems_filesystem_mount_table_entry_t *mt_entry,
428                uint32_t                              start,
429                uint32_t                              offset,
430                uint32_t                              count,
431                void                                 *buff);
432
433ssize_t
434_fat_block_write(rtems_filesystem_mount_table_entry_t *mt_entry,
435                 uint32_t                              start,
436                 uint32_t                              offset,
437                 uint32_t                              count,
438                 const void                           *buff);
439
440ssize_t
441fat_cluster_read(rtems_filesystem_mount_table_entry_t *mt_entry,
442                  uint32_t                             cln,
443                  void                                *buff);
444
445ssize_t
446fat_cluster_write(rtems_filesystem_mount_table_entry_t *mt_entry,
447                   uint32_t                             cln,
448                   const void                          *buff);
449
450int
451fat_init_volume_info(rtems_filesystem_mount_table_entry_t *mt_entry);
452
453int
454fat_init_clusters_chain(rtems_filesystem_mount_table_entry_t *mt_entry,
455                        uint32_t                              start_cln);
456
457uint32_t
458fat_cluster_num_to_sector_num(rtems_filesystem_mount_table_entry_t *mt_entry,
459                              uint32_t                              cln);
460
461int
462fat_shutdown_drive(rtems_filesystem_mount_table_entry_t *mt_entry);
463
464
465uint32_t
466fat_get_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry);
467
468bool
469fat_ino_is_unique(rtems_filesystem_mount_table_entry_t *mt_entry,
470                  uint32_t                              ino);
471
472void
473fat_free_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry,
474                    uint32_t                              ino);
475
476int
477fat_fat32_update_fsinfo_sector(
478  rtems_filesystem_mount_table_entry_t *mt_entry,
479  uint32_t                              free_count,
480  uint32_t                              next_free
481  );
482
483#ifdef __cplusplus
484}
485#endif
486
487#endif /* __DOSFS_FAT_H__ */
Note: See TracBrowser for help on using the repository browser.