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

4.104.114.84.95
Last change on this file since a5305f6b was a5305f6b, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/17/04 at 08:34:41

Remove stray white spaces.

  • Property mode set to 100644
File size: 15.6 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#ifdef __cplusplus
21extern "C" {
22#endif
23
24#include <string.h>
25
26#include <rtems/seterr.h>
27
28/* XXX: temporary hack :(( */
29#ifndef set_errno_and_return_minus_one
30#define set_errno_and_return_minus_one rtems_set_errno_and_return_minus_one
31#endif /* set_errno_and_return_minus_one */
32
33#include <rtems/score/cpu.h>
34#include <errno.h>
35#include <rtems/bdbuf.h>
36
37#ifndef RC_OK
38#define RC_OK 0x00000000
39#endif
40
41/*
42 * Remember that all FAT file system on disk data structure is
43 * "little endian"!
44 * (derived from linux)
45 */
46/*
47 * Conversion from and to little-endian byte order. (no-op on i386/i486)
48 *
49 * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian,
50 * BE = big-endian, c: W = word (16 bits), L = longword (32 bits)
51 */
52
53#if (CPU_BIG_ENDIAN == TRUE)
54#    define CF_LE_W(v) CPU_swap_u16((uint16_t  )v)
55#    define CF_LE_L(v) CPU_swap_u32((uint32_t  )v)
56#    define CT_LE_W(v) CPU_swap_u16((uint16_t  )v)
57#    define CT_LE_L(v) CPU_swap_u32((uint32_t  )v)
58#else
59#    define CF_LE_W(v) (v)
60#    define CF_LE_L(v) (v)
61#    define CT_LE_W(v) (v)
62#    define CT_LE_L(v) (v)
63#endif
64
65#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
66
67#define FAT_HASH_SIZE   2
68#define FAT_HASH_MODULE FAT_HASH_SIZE
69
70
71#define FAT_SECTOR512_SIZE     512 /* sector size (bytes) */
72#define FAT_SECTOR512_BITS       9 /* log2(SECTOR_SIZE) */
73
74/* maximum + 1 number of clusters for FAT12 */
75#define FAT_FAT12_MAX_CLN      4085
76
77/* maximum + 1 number of clusters for FAT16 */
78#define FAT_FAT16_MAX_CLN      65525
79
80#define FAT_FAT12              0x01
81#define FAT_FAT16              0x02
82#define FAT_FAT32              0x04
83
84#define FAT_UNDEFINED_VALUE     (uint32_t  )0xFFFFFFFF
85
86#define FAT_FAT12_EOC          0x0FF8
87#define FAT_FAT16_EOC          0xFFF8
88#define FAT_FAT32_EOC          (uint32_t  )0x0FFFFFF8
89
90#define FAT_FAT12_FREE         0x0000
91#define FAT_FAT16_FREE         0x0000
92#define FAT_FAT32_FREE         0x00000000
93
94#define FAT_GENFAT_EOC         (uint32_t  )0xFFFFFFFF
95#define FAT_GENFAT_FREE        (uint32_t  )0x00000000
96
97#define FAT_FAT12_SHIFT        0x04
98
99#define FAT_FAT12_MASK         0x00000FFF
100#define FAT_FAT16_MASK         0x0000FFFF
101#define FAT_FAT32_MASK         (uint32_t  )0x0FFFFFFF
102
103#define FAT_MAX_BPB_SIZE       90
104
105/* size of useful information in FSInfo sector */
106#define FAT_USEFUL_INFO_SIZE   12
107
108#define FAT_VAL8(x, ofs)       (uint8_t  )(*((uint8_t   *)(x) + (ofs)))
109
110#define FAT_VAL16(x, ofs)                                   \
111    (uint16_t  )( (*((uint8_t   *)(x) + (ofs))) |           \
112                  ((*((uint8_t   *)(x) + (ofs) + 1)) << 8) )
113
114#define FAT_VAL32(x, ofs)                                                 \
115    (uint32_t  )( (uint32_t  )(*((uint8_t   *)(x) + (ofs))) |             \
116                  ((uint32_t  )(*((uint8_t   *)(x) + (ofs) + 1)) << 8)  | \
117                  ((uint32_t  )(*((uint8_t   *)(x) + (ofs) + 2)) << 16) | \
118                  ((uint32_t  )(*((uint8_t   *)(x) + (ofs) + 3)) << 24) )
119
120/* macros to access boot sector fields */
121#define FAT_BR_BYTES_PER_SECTOR(x)       FAT_VAL16(x, 11)
122#define FAT_BR_SECTORS_PER_CLUSTER(x)    FAT_VAL8(x, 13)
123#define FAT_BR_RESERVED_SECTORS_NUM(x)   FAT_VAL16(x, 14)
124#define FAT_BR_FAT_NUM(x)                FAT_VAL8(x, 16)
125#define FAT_BR_FILES_PER_ROOT_DIR(x)     FAT_VAL16(x, 17)
126#define FAT_BR_TOTAL_SECTORS_NUM16(x)    FAT_VAL16(x, 19)
127#define FAT_BR_MEDIA(x)                  FAT_VAL8(x, 21)
128#define FAT_BR_SECTORS_PER_FAT(x)        FAT_VAL16(x, 22)
129#define FAT_BR_TOTAL_SECTORS_NUM32(x)    FAT_VAL32(x, 32)
130#define FAT_BR_SECTORS_PER_FAT32(x)      FAT_VAL32(x, 36)
131#define FAT_BR_EXT_FLAGS(x)              FAT_VAL16(x, 40)
132#define FAT_BR_FAT32_ROOT_CLUSTER(x)     FAT_VAL32(x, 44)
133#define FAT_BR_FAT32_FS_INFO_SECTOR(x)   FAT_VAL16(x, 48)
134#define FAT_FSINFO_LEAD_SIGNATURE(x)     FAT_VAL32(x, 0)
135/*
136 * I read FSInfo sector from offset 484 to access the information, so offsets
137 * of these fields a relative
138 */
139#define FAT_FSINFO_FREE_CLUSTER_COUNT(x) FAT_VAL32(x, 4)
140#define FAT_FSINFO_NEXT_FREE_CLUSTER(x)  FAT_VAL32(x, 8)
141
142#define FAT_FSINFO_FREE_CLUSTER_COUNT_OFFSET 488
143
144#define FAT_FSINFO_NEXT_FREE_CLUSTER_OFFSET  492
145
146#define FAT_RSRVD_CLN                        0x02
147
148#define FAT_FSINFO_LEAD_SIGNATURE_VALUE      0x41615252
149
150#define FAT_FSI_LEADSIG_SIZE                 0x04
151
152#define FAT_FSI_INFO                         484
153
154#define MS_BYTES_PER_CLUSTER_LIMIT           0x8000     /* 32K */
155
156#define FAT_BR_EXT_FLAGS_MIRROR              0x0080
157
158#define FAT_BR_EXT_FLAGS_FAT_NUM             0x000F
159
160
161#define FAT_DIRENTRY_SIZE          32
162
163#define FAT_DIRENTRIES_PER_SEC512  16
164
165/*
166 * Volume descriptor
167 * Description of the volume the FAT filesystem is located on - generally
168 * the fields of the structure corresponde to Boot Sector and BPB Srtucture
169 * fields
170 */
171typedef struct fat_vol_s
172{
173    uint16_t     bps;            /* bytes per sector */
174    uint8_t      sec_log2;       /* log2 of bps */
175    uint8_t      sec_mul;        /* log2 of 512bts sectors number per sector */
176    uint8_t      spc;            /* sectors per cluster */
177    uint8_t      spc_log2;       /* log2 of spc */
178    uint16_t     bpc;            /* bytes per cluster */
179    uint8_t      bpc_log2;       /* log2 of bytes per cluster */
180    uint8_t      fats;           /* number of FATs */
181    uint8_t      type;           /* FAT type */
182    uint32_t     mask;
183    uint32_t     eoc_val;
184    uint16_t     fat_loc;        /* FAT start */
185    uint32_t     fat_length;     /* sectors per FAT */
186    uint32_t     rdir_loc;       /* root directory start */
187    uint16_t     rdir_entrs;     /* files per root directory */
188    uint32_t     rdir_secs;      /* sectors per root directory */
189    uint32_t     rdir_size;      /* root directory size in bytes */
190    uint32_t     tot_secs;       /* total count of sectors */
191    uint32_t     data_fsec;      /* first data sector */
192    uint32_t     data_cls;       /* count of data clusters */
193    uint32_t     rdir_cl;        /* first cluster of the root directory */
194    uint16_t     info_sec;       /* FSInfo Sector Structure location */
195    uint32_t     free_cls;       /* last known free clusters count */
196    uint32_t     next_cl;        /* next free cluster number */
197    uint8_t      mirror;         /* mirroring enabla/disable */
198    uint32_t     afat_loc;       /* active FAT location */
199    uint8_t      afat;           /* the number of active FAT */
200    dev_t        dev;            /* device ID */
201    disk_device *dd;             /* disk device (see libblock) */
202    void        *private_data;   /* reserved */
203} fat_vol_t;
204
205
206typedef struct fat_cache_s
207{
208    uint32_t       blk_num;
209    rtems_boolean  modified;
210    uint8_t        state;
211    bdbuf_buffer   *buf;
212} fat_cache_t;
213
214/*
215 * This structure identifies the instance of the filesystem on the FAT
216 * ("fat-file") level.
217 */
218typedef struct fat_fs_info_s
219{
220    fat_vol_t      vol;           /* volume descriptor */
221    Chain_Control *vhash;         /* "vhash" of fat-file descriptors */
222    Chain_Control *rhash;         /* "rhash" of fat-file descriptors */
223    char          *uino;          /* array of unique ino numbers */
224    uint32_t       index;
225    uint32_t       uino_pool_size; /* size */
226    uint32_t       uino_base;
227    fat_cache_t    c;             /* cache */
228    uint8_t       *sec_buf; /* just placeholder for anything */
229} fat_fs_info_t;
230
231/*
232 * if the name we looking for is file we store not only first data cluster
233 * number, but and cluster number and offset for directory entry for this
234 * name
235 */
236typedef struct fat_auxiliary_s
237{
238    uint32_t   cln;
239    uint32_t   ofs;
240} fat_auxiliary_t;
241
242#define FAT_FAT_OFFSET(fat_type, cln)                  \
243    ((fat_type) & FAT_FAT12 ? ((cln) + ((cln) >> 1)) : \
244     (fat_type) & FAT_FAT16 ? ((cln) << 1)           : \
245     ((cln) << 2))
246
247#define FAT_CLUSTER_IS_ODD(n)  ((n) & 0x0001)
248
249#define FAT12_SHIFT      0x4    /* half of a byte */
250
251/* initial size of array of unique ino */
252#define FAT_UINO_POOL_INIT_SIZE  0x100
253
254/* cache support */
255#define FAT_CACHE_EMPTY   0x0
256#define FAT_CACHE_ACTUAL  0x1
257
258#define FAT_OP_TYPE_READ  0x1
259#define FAT_OP_TYPE_GET   0x2
260
261static inline uint32_t
262fat_cluster_num_to_sector_num(
263    rtems_filesystem_mount_table_entry_t *mt_entry,
264    uint32_t                              cln
265    )
266{
267    register fat_fs_info_t *fs_info = mt_entry->fs_info;
268
269    if ( (cln == 0) && (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)) )
270        return fs_info->vol.rdir_loc;
271
272    return (((cln - FAT_RSRVD_CLN) << fs_info->vol.spc_log2) +
273            fs_info->vol.data_fsec);
274}
275
276static inline uint32_t
277fat_cluster_num_to_sector512_num(
278    rtems_filesystem_mount_table_entry_t *mt_entry,
279    uint32_t                              cln
280    )
281{
282    fat_fs_info_t *fs_info = mt_entry->fs_info;
283
284    if (cln == 1)
285        return 1;
286
287    return (fat_cluster_num_to_sector_num(mt_entry, cln) <<
288            fs_info->vol.sec_mul);
289}
290
291static inline int
292fat_buf_access(fat_fs_info_t *fs_info, uint32_t   blk, int op_type,
293               bdbuf_buffer **buf)
294{
295    rtems_status_code sc = RTEMS_SUCCESSFUL;
296    uint8_t           i;
297    rtems_boolean     sec_of_fat;
298
299
300    if (fs_info->c.state == FAT_CACHE_EMPTY)
301    {
302        if (op_type == FAT_OP_TYPE_READ)
303            sc = rtems_bdbuf_read(fs_info->vol.dev, blk, &fs_info->c.buf);
304        else
305            sc = rtems_bdbuf_get(fs_info->vol.dev, blk, &fs_info->c.buf);
306        if (sc != RTEMS_SUCCESSFUL)
307            set_errno_and_return_minus_one(EIO);
308        fs_info->c.blk_num = blk;
309        fs_info->c.modified = 0;
310        fs_info->c.state = FAT_CACHE_ACTUAL;
311    }
312
313    sec_of_fat = ((fs_info->c.blk_num >= fs_info->vol.fat_loc) &&
314                  (fs_info->c.blk_num < fs_info->vol.rdir_loc));
315
316    if (fs_info->c.blk_num != blk)
317    {
318        if (fs_info->c.modified)
319        {
320            if (sec_of_fat && !fs_info->vol.mirror)
321                memcpy(fs_info->sec_buf, fs_info->c.buf->buffer,
322                       fs_info->vol.bps);
323
324            sc = rtems_bdbuf_release_modified(fs_info->c.buf);
325            fs_info->c.state = FAT_CACHE_EMPTY;
326            fs_info->c.modified = 0;
327            if (sc != RTEMS_SUCCESSFUL)
328                set_errno_and_return_minus_one(EIO);
329
330            if (sec_of_fat && !fs_info->vol.mirror)
331            {
332                bdbuf_buffer *b;
333
334                for (i = 1; i < fs_info->vol.fats; i++)
335                {
336                    sc = rtems_bdbuf_get(fs_info->vol.dev,
337                                         fs_info->c.blk_num +
338                                         fs_info->vol.fat_length * i,
339                                         &b);
340                    if ( sc != RTEMS_SUCCESSFUL)
341                        set_errno_and_return_minus_one(ENOMEM);
342                    memcpy(b->buffer, fs_info->sec_buf, fs_info->vol.bps);
343                    sc = rtems_bdbuf_release_modified(b);
344                    if ( sc != RTEMS_SUCCESSFUL)
345                        set_errno_and_return_minus_one(ENOMEM);
346                }
347            }
348        }
349        else
350        {
351            sc = rtems_bdbuf_release(fs_info->c.buf);
352            fs_info->c.state = FAT_CACHE_EMPTY;
353            if (sc != RTEMS_SUCCESSFUL)
354                set_errno_and_return_minus_one(EIO);
355
356        }
357        if (op_type == FAT_OP_TYPE_READ)
358            sc = rtems_bdbuf_read(fs_info->vol.dev, blk, &fs_info->c.buf);
359        else
360            sc = rtems_bdbuf_get(fs_info->vol.dev, blk, &fs_info->c.buf);
361        if (sc != RTEMS_SUCCESSFUL)
362            set_errno_and_return_minus_one(EIO);
363        fs_info->c.blk_num = blk;
364        fs_info->c.state = FAT_CACHE_ACTUAL;
365    }
366    *buf = fs_info->c.buf;
367    return RC_OK;
368}
369
370
371static inline int
372fat_buf_release(fat_fs_info_t *fs_info)
373{
374    rtems_status_code sc = RTEMS_SUCCESSFUL;
375    uint8_t           i;
376    rtems_boolean     sec_of_fat;
377
378    if (fs_info->c.state == FAT_CACHE_EMPTY)
379        return RC_OK;
380
381    sec_of_fat = ((fs_info->c.blk_num >= fs_info->vol.fat_loc) &&
382                  (fs_info->c.blk_num < fs_info->vol.rdir_loc));
383
384    if (fs_info->c.modified)
385    {
386        if (sec_of_fat && !fs_info->vol.mirror)
387            memcpy(fs_info->sec_buf, fs_info->c.buf->buffer, fs_info->vol.bps);
388
389        sc = rtems_bdbuf_release_modified(fs_info->c.buf);
390        if (sc != RTEMS_SUCCESSFUL)
391            set_errno_and_return_minus_one(EIO);
392        fs_info->c.modified = 0;
393
394        if (sec_of_fat && !fs_info->vol.mirror)
395        {
396            bdbuf_buffer *b;
397
398            for (i = 1; i < fs_info->vol.fats; i++)
399            {
400                sc = rtems_bdbuf_get(fs_info->vol.dev,
401                                     fs_info->c.blk_num +
402                                     fs_info->vol.fat_length * i,
403                                     &b);
404                if ( sc != RTEMS_SUCCESSFUL)
405                    set_errno_and_return_minus_one(ENOMEM);
406                memcpy(b->buffer, fs_info->sec_buf, fs_info->vol.bps);
407                sc = rtems_bdbuf_release_modified(b);
408                if ( sc != RTEMS_SUCCESSFUL)
409                    set_errno_and_return_minus_one(ENOMEM);
410            }
411        }
412    }
413    else
414    {
415        sc = rtems_bdbuf_release(fs_info->c.buf);
416        if (sc != RTEMS_SUCCESSFUL)
417            set_errno_and_return_minus_one(EIO);
418    }
419    fs_info->c.state = FAT_CACHE_EMPTY;
420    return RC_OK;
421}
422
423static inline void
424fat_buf_mark_modified(fat_fs_info_t *fs_info)
425{
426    fs_info->c.modified = TRUE;
427}
428
429
430
431ssize_t
432_fat_block_read(rtems_filesystem_mount_table_entry_t *mt_entry,
433                uint32_t                              start,
434                uint32_t                              offset,
435                uint32_t                              count,
436                void                                 *buff);
437
438ssize_t
439_fat_block_write(rtems_filesystem_mount_table_entry_t *mt_entry,
440                 uint32_t                              start,
441                 uint32_t                              offset,
442                 uint32_t                              count,
443                 const void                           *buff);
444
445ssize_t
446fat_cluster_read(rtems_filesystem_mount_table_entry_t *mt_entry,
447                  uint32_t                             cln,
448                  void                                *buff);
449
450ssize_t
451fat_cluster_write(rtems_filesystem_mount_table_entry_t *mt_entry,
452                   uint32_t                             cln,
453                   const void                          *buff);
454
455int
456fat_init_volume_info(rtems_filesystem_mount_table_entry_t *mt_entry);
457
458int
459fat_init_clusters_chain(rtems_filesystem_mount_table_entry_t *mt_entry,
460                        uint32_t                              start_cln);
461
462uint32_t
463fat_cluster_num_to_sector_num(rtems_filesystem_mount_table_entry_t *mt_entry,
464                              uint32_t                              cln);
465
466int
467fat_shutdown_drive(rtems_filesystem_mount_table_entry_t *mt_entry);
468
469
470uint32_t
471fat_get_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry);
472
473rtems_boolean
474fat_ino_is_unique(rtems_filesystem_mount_table_entry_t *mt_entry,
475                  uint32_t                              ino);
476
477void
478fat_free_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry,
479                    uint32_t                              ino);
480
481int
482fat_fat32_update_fsinfo_sector(
483  rtems_filesystem_mount_table_entry_t *mt_entry,
484  uint32_t                              free_count,
485  uint32_t                              next_free
486  );
487
488#ifdef __cplusplus
489}
490#endif
491
492#endif /* __DOSFS_FAT_H__ */
Note: See TracBrowser for help on using the repository browser.