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

4.104.114.84.95
Last change on this file since a29d2e7 was c3bb0619, checked in by Ralf Corsepius <ralf.corsepius@…>, on 01/19/05 at 02:31:15

Fix typos in comment.

  • Property mode set to 100644
File size: 21.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#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#define FAT_TOTAL_MBR_SIZE    512
105
106/* size of useful information in FSInfo sector */
107#define FAT_USEFUL_INFO_SIZE   12
108
109#define FAT_GET_ADDR(x, ofs)       ((uint8_t *)(x) + (ofs))
110
111#define FAT_GET_VAL8(x, ofs)       (uint8_t)(*((uint8_t *)(x) + (ofs)))
112
113#define FAT_GET_VAL16(x, ofs)                               \
114    (uint16_t)( (*((uint8_t *)(x) + (ofs))) |           \
115                  ((*((uint8_t *)(x) + (ofs) + 1)) << 8) )
116
117#define FAT_GET_VAL32(x, ofs)                                             \
118    (uint32_t)( (uint32_t)(*((uint8_t *)(x) + (ofs))) |             \
119                  ((uint32_t)(*((uint8_t *)(x) + (ofs) + 1)) << 8)  | \
120                  ((uint32_t)(*((uint8_t *)(x) + (ofs) + 2)) << 16) | \
121                  ((uint32_t)(*((uint8_t *)(x) + (ofs) + 3)) << 24) )
122                   
123#define FAT_SET_VAL8(x, ofs,val)                    \
124                 (*((uint8_t *)(x)+(ofs))=(uint8_t)(val))
125 
126#define FAT_SET_VAL16(x, ofs,val) do {              \
127                 FAT_SET_VAL8((x),(ofs),(val));     \
128                 FAT_SET_VAL8((x),(ofs)+1,(val)>>8);\
129                 } while (0)
130
131#define FAT_SET_VAL32(x, ofs,val) do {               \
132                 FAT_SET_VAL16((x),(ofs),(val));     \
133                 FAT_SET_VAL16((x),(ofs)+2,(val)>>16);\
134                 } while (0)
135
136/* macros to access boot sector fields */
137#define FAT_GET_BR_JMPBOOT(x)                FAT_GET_VAL8( x,  0)
138#define FAT_SET_BR_JMPBOOT(x,val)            FAT_SET_VAL8( x,  0,val)
139
140#define FAT_GET_ADDR_BR_OEMNAME(x)           FAT_GET_ADDR( x,  3)
141#define FAT_BR_OEMNAME_SIZE              (8)
142
143#define FAT_GET_BR_BYTES_PER_SECTOR(x)       FAT_GET_VAL16(x, 11)
144#define FAT_SET_BR_BYTES_PER_SECTOR(x,val)   FAT_SET_VAL16(x, 11,val)
145
146#define FAT_GET_BR_SECTORS_PER_CLUSTER(x)    FAT_GET_VAL8( x, 13)
147#define FAT_SET_BR_SECTORS_PER_CLUSTER(x,val)FAT_SET_VAL8( x, 13,val)
148
149#define FAT_GET_BR_RESERVED_SECTORS_NUM(x)   FAT_GET_VAL16(x, 14)
150#define FAT_SET_BR_RESERVED_SECTORS_NUM(x,val) FAT_SET_VAL16(x, 14,val)
151
152#define FAT_GET_BR_FAT_NUM(x)                FAT_GET_VAL8( x, 16)
153#define FAT_SET_BR_FAT_NUM(x,val)            FAT_SET_VAL8( x, 16,val)
154
155#define FAT_GET_BR_FILES_PER_ROOT_DIR(x)     FAT_GET_VAL16(x, 17)
156#define FAT_SET_BR_FILES_PER_ROOT_DIR(x,val) FAT_SET_VAL16(x, 17,val)
157
158#define FAT_GET_BR_TOTAL_SECTORS_NUM16(x)    FAT_GET_VAL16(x, 19)
159#define FAT_SET_BR_TOTAL_SECTORS_NUM16(x,val)FAT_SET_VAL16(x, 19,val)
160
161#define FAT_GET_BR_MEDIA(x)                  FAT_GET_VAL8( x, 21)
162#define FAT_SET_BR_MEDIA(x,val)              FAT_SET_VAL8( x, 21,val)
163
164#define FAT_GET_BR_SECTORS_PER_FAT(x)        FAT_GET_VAL16(x, 22)
165#define FAT_SET_BR_SECTORS_PER_FAT(x,val)    FAT_SET_VAL16(x, 22,val)
166
167#define FAT_GET_BR_SECTORS_PER_TRACK(x)      FAT_GET_VAL16(x, 24)
168#define FAT_SET_BR_SECTORS_PER_TRACK(x,val)  FAT_SET_VAL16(x, 24,val)
169
170#define FAT_GET_BR_NUMBER_OF_HEADS(x)        FAT_GET_VAL16(x, 26)
171#define FAT_SET_BR_NUMBER_OF_HEADS(x,val)    FAT_SET_VAL16(x, 26,val)
172
173#define FAT_GET_BR_HIDDEN_SECTORS(x)         FAT_GET_VAL32(x, 28)
174#define FAT_SET_BR_HIDDEN_SECTORS(x,val)     FAT_SET_VAL32(x, 28,val)
175
176#define FAT_GET_BR_TOTAL_SECTORS_NUM32(x)    FAT_GET_VAL32(x, 32)
177#define FAT_SET_BR_TOTAL_SECTORS_NUM32(x,val) FAT_SET_VAL32(x, 32,val)
178  /* --- start of FAT12/16 specific fields */
179#define FAT_GET_BR_DRVNUM(x)                 FAT_GET_VAL8( x, 36)
180#define FAT_SET_BR_DRVNUM(x,val)             FAT_SET_VAL8( x, 36,val)
181
182#define FAT_GET_BR_RSVD1(x)                  FAT_GET_VAL8( x, 37)
183#define FAT_SET_BR_RSVD1(x,val)              FAT_SET_VAL8( x, 37,val)
184
185#define FAT_GET_BR_BOOTSIG(x)                FAT_GET_VAL8( x, 38)
186#define FAT_SET_BR_BOOTSIG(x,val)            FAT_SET_VAL8( x, 38,val)
187#define FAT_BR_BOOTSIG_VAL               (0x29)
188
189#define FAT_GET_BR_VOLID(x)                  FAT_GET_VAL32(x, 39)
190#define FAT_SET_BR_VOLID(x,val)              FAT_SET_VAL32(x, 39,val)
191
192#define FAT_GET_ADDR_BR_VOLLAB(x)            FAT_GET_ADDR (x, 43)
193#define FAT_BR_VOLLAB_SIZE               (11)
194
195#define FAT_GET_ADDR_BR_FILSYSTYPE(x)        FAT_GET_ADDR (x, 54)
196#define FAT_BR_FILSYSTYPE_SIZE           (8)
197  /* --- end of FAT12/16 specific fields */
198  /* --- start of FAT32 specific fields */
199#define FAT_GET_BR_SECTORS_PER_FAT32(x)      FAT_GET_VAL32(x, 36)
200#define FAT_SET_BR_SECTORS_PER_FAT32(x,val)  FAT_SET_VAL32(x, 36,val)
201
202#define FAT_GET_BR_EXT_FLAGS(x)              FAT_GET_VAL16(x, 40)
203#define FAT_SET_BR_EXT_FLAGS(x,val)          FAT_SET_VAL16(x, 40,val)
204
205#define FAT_GET_BR_FSVER(x)                  FAT_GET_VAL16(x, 42)
206#define FAT_SET_BR_FSVER(x,val)              FAT_SET_VAL16(x, 42,val)
207
208#define FAT_GET_BR_FAT32_ROOT_CLUSTER(x)     FAT_GET_VAL32(x, 44)
209#define FAT_SET_BR_FAT32_ROOT_CLUSTER(x,val) FAT_SET_VAL32(x, 44,val)
210
211#define FAT_GET_BR_FAT32_FS_INFO_SECTOR(x)   FAT_GET_VAL16(x, 48)
212#define FAT_SET_BR_FAT32_FS_INFO_SECTOR(x,val) FAT_SET_VAL16(x, 48,val)
213
214#define FAT_GET_BR_FAT32_BK_BOOT_SECTOR(x)   FAT_GET_VAL16(x, 50)
215#define FAT_SET_BR_FAT32_BK_BOOT_SECTOR(x,val)  FAT_SET_VAL16(x, 50,val)
216
217#define FAT_GET_ADDR_BR_FAT32_RESERVED(x)    FAT_GET_ADDR (x, 52)
218#define FAT_BR_FAT32_RESERVED_SIZE       (12)
219
220#define FAT_GET_BR_FAT32_DRVNUM(x)           FAT_GET_VAL8( x, 64)
221#define FAT_SET_BR_FAT32_DRVNUM(x,val)       FAT_SET_VAL8( x, 64,val)
222
223#define FAT_GET_BR_FAT32_RSVD1(x)            FAT_GET_VAL8( x, 65)
224#define FAT_SET_BR_FAT32_RSVD1(x,val)        FAT_SET_VAL8( x, 65,val)
225
226#define FAT_GET_BR_FAT32_BOOTSIG(x)          FAT_GET_VAL8( x, 66)
227#define FAT_SET_BR_FAT32_BOOTSIG(x,val)      FAT_SET_VAL8( x, 66,val)
228#define FAT_BR_FAT32_BOOTSIG_VAL         (0x29)
229
230#define FAT_GET_BR_FAT32_VOLID(x)            FAT_GET_VAL32(x, 67)
231#define FAT_SET_BR_FAT32_VOLID(x,val)        FAT_SET_VAL32(x, 67,val)
232
233#define FAT_GET_ADDR_BR_FAT32_VOLLAB(x)      FAT_GET_ADDR (x, 71)
234#define FAT_BR_FAT32_VOLLAB_SIZE         (11)
235
236#define FAT_GET_ADDR_BR_FAT32_FILSYSTYPE(x)  FAT_GET_ADDR (x, 82)
237#define FAT_BR_FAT32_FILSYSTYPE_SIZE     (8)
238  /* --- end of FAT32 specific fields */
239
240#define FAT_GET_BR_SIGNATURE(x)              FAT_GET_VAL16(x,510)
241#define FAT_SET_BR_SIGNATURE(x,val)          FAT_SET_VAL16(x,510,val)
242#define FAT_BR_SIGNATURE_VAL                (0xAA55)
243
244  /*
245   * FAT32 FSINFO description
246   */
247#define FAT_GET_FSINFO_LEAD_SIGNATURE(x)      FAT_GET_VAL32(x,  0)
248#define FAT_SET_FSINFO_LEAD_SIGNATURE(x,val)  FAT_SET_VAL32(x,  0,val)
249#define FAT_FSINFO_LEAD_SIGNATURE_VALUE   (0x41615252)
250
251#define FAT_GET_FSINFO_STRUC_SIGNATURE(x)     FAT_GET_VAL32(x,484)
252#define FAT_SET_FSINFO_STRUC_SIGNATURE(x,val) FAT_SET_VAL32(x,484,val)
253#define FAT_FSINFO_STRUC_SIGNATURE_VALUE  (0x61417272)
254
255#define FAT_GET_FSINFO_TRAIL_SIGNATURE(x)     FAT_GET_VAL32(x,508)
256#define FAT_SET_FSINFO_TRAIL_SIGNATURE(x,val) FAT_SET_VAL32(x,508,val)
257#define FAT_FSINFO_TRAIL_SIGNATURE_VALUE  (0x000055AA)
258/*
259 * I read FSInfo sector from offset 484 to access the information, so offsets
260 * of these fields a relative
261 */
262#define FAT_GET_FSINFO_FREE_CLUSTER_COUNT(x)      FAT_GET_VAL32(x, 4)
263#define FAT_SET_FSINFO_FREE_CLUSTER_COUNT(x,val)  FAT_SET_VAL32(x, 4,val)
264#define FAT_GET_FSINFO_NEXT_FREE_CLUSTER(x)       FAT_GET_VAL32(x, 8)
265#define FAT_SET_FSINFO_NEXT_FREE_CLUSTER(x,val)   FAT_SET_VAL32(x, 8,val)
266
267#define FAT_FSI_INFO                         484
268#define FAT_FSINFO_STRUCT_OFFSET             488
269#define FAT_FSINFO_FREE_CLUSTER_COUNT_OFFSET (FAT_FSINFO_STRUCT_OFFSET+0)
270
271#define FAT_FSINFO_NEXT_FREE_CLUSTER_OFFSET  (FAT_FSINFO_STRUCT_OFFSET+4)
272
273#define FAT_RSRVD_CLN                        0x02
274
275#define FAT_FSI_LEADSIG_SIZE                 0x04
276
277#define FAT_TOTAL_FSINFO_SIZE               512
278
279#define MS_BYTES_PER_CLUSTER_LIMIT           0x8000     /* 32K */
280
281#define FAT_BR_EXT_FLAGS_MIRROR              0x0080
282
283#define FAT_BR_EXT_FLAGS_FAT_NUM             0x000F
284
285#define FAT_BR_MEDIA_FIXED                  0xf8
286
287#define FAT_DIRENTRY_SIZE          32
288
289#define FAT_DIRENTRIES_PER_SEC512  16
290
291/*
292 * Volume descriptor
293 * Description of the volume the FAT filesystem is located on - generally
294 * the fields of the structure correspond to Boot Sector and BPB Structure
295 * fields
296 */
297typedef struct fat_vol_s
298{
299    uint16_t     bps;            /* bytes per sector */
300    uint8_t      sec_log2;       /* log2 of bps */
301    uint8_t      sec_mul;        /* log2 of 512bts sectors number per sector */
302    uint8_t      spc;            /* sectors per cluster */
303    uint8_t      spc_log2;       /* log2 of spc */
304    uint16_t     bpc;            /* bytes per cluster */
305    uint8_t      bpc_log2;       /* log2 of bytes per cluster */
306    uint8_t      fats;           /* number of FATs */
307    uint8_t      type;           /* FAT type */
308    uint32_t     mask;
309    uint32_t     eoc_val;
310    uint16_t     fat_loc;        /* FAT start */
311    uint32_t     fat_length;     /* sectors per FAT */
312    uint32_t     rdir_loc;       /* root directory start */
313    uint16_t     rdir_entrs;     /* files per root directory */
314    uint32_t     rdir_secs;      /* sectors per root directory */
315    uint32_t     rdir_size;      /* root directory size in bytes */
316    uint32_t     tot_secs;       /* total count of sectors */
317    uint32_t     data_fsec;      /* first data sector */
318    uint32_t     data_cls;       /* count of data clusters */
319    uint32_t     rdir_cl;        /* first cluster of the root directory */
320    uint16_t     info_sec;       /* FSInfo Sector Structure location */
321    uint32_t     free_cls;       /* last known free clusters count */
322    uint32_t     next_cl;        /* next free cluster number */
323    uint8_t      mirror;         /* mirroring enabla/disable */
324    uint32_t     afat_loc;       /* active FAT location */
325    uint8_t      afat;           /* the number of active FAT */
326    dev_t        dev;            /* device ID */
327    disk_device *dd;             /* disk device (see libblock) */
328    void        *private_data;   /* reserved */
329} fat_vol_t;
330
331
332typedef struct fat_cache_s
333{
334    uint32_t       blk_num;
335    rtems_boolean  modified;
336    uint8_t        state;
337    bdbuf_buffer   *buf;
338} fat_cache_t;
339
340/*
341 * This structure identifies the instance of the filesystem on the FAT
342 * ("fat-file") level.
343 */
344typedef struct fat_fs_info_s
345{
346    fat_vol_t      vol;           /* volume descriptor */
347    Chain_Control *vhash;         /* "vhash" of fat-file descriptors */
348    Chain_Control *rhash;         /* "rhash" of fat-file descriptors */
349    char          *uino;          /* array of unique ino numbers */
350    uint32_t       index;
351    uint32_t       uino_pool_size; /* size */
352    uint32_t       uino_base;
353    fat_cache_t    c;             /* cache */
354    uint8_t       *sec_buf; /* just placeholder for anything */
355} fat_fs_info_t;
356
357/*
358 * if the name we looking for is file we store not only first data cluster
359 * number, but and cluster number and offset for directory entry for this
360 * name
361 */
362typedef struct fat_auxiliary_s
363{
364    uint32_t   cln;
365    uint32_t   ofs;
366} fat_auxiliary_t;
367
368#define FAT_FAT_OFFSET(fat_type, cln)                  \
369    ((fat_type) & FAT_FAT12 ? ((cln) + ((cln) >> 1)) : \
370     (fat_type) & FAT_FAT16 ? ((cln) << 1)           : \
371     ((cln) << 2))
372
373#define FAT_CLUSTER_IS_ODD(n)  ((n) & 0x0001)
374
375#define FAT12_SHIFT      0x4    /* half of a byte */
376
377/* initial size of array of unique ino */
378#define FAT_UINO_POOL_INIT_SIZE  0x100
379
380/* cache support */
381#define FAT_CACHE_EMPTY   0x0
382#define FAT_CACHE_ACTUAL  0x1
383
384#define FAT_OP_TYPE_READ  0x1
385#define FAT_OP_TYPE_GET   0x2
386
387static inline uint32_t
388fat_cluster_num_to_sector_num(
389    rtems_filesystem_mount_table_entry_t *mt_entry,
390    uint32_t                              cln
391    )
392{
393    register fat_fs_info_t *fs_info = mt_entry->fs_info;
394
395    if ( (cln == 0) && (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)) )
396        return fs_info->vol.rdir_loc;
397
398    return (((cln - FAT_RSRVD_CLN) << fs_info->vol.spc_log2) +
399            fs_info->vol.data_fsec);
400}
401
402static inline uint32_t
403fat_cluster_num_to_sector512_num(
404    rtems_filesystem_mount_table_entry_t *mt_entry,
405    uint32_t                              cln
406    )
407{
408    fat_fs_info_t *fs_info = mt_entry->fs_info;
409
410    if (cln == 1)
411        return 1;
412
413    return (fat_cluster_num_to_sector_num(mt_entry, cln) <<
414            fs_info->vol.sec_mul);
415}
416
417static inline int
418fat_buf_access(fat_fs_info_t *fs_info, uint32_t   blk, int op_type,
419               bdbuf_buffer **buf)
420{
421    rtems_status_code sc = RTEMS_SUCCESSFUL;
422    uint8_t           i;
423    rtems_boolean     sec_of_fat;
424
425
426    if (fs_info->c.state == FAT_CACHE_EMPTY)
427    {
428        if (op_type == FAT_OP_TYPE_READ)
429            sc = rtems_bdbuf_read(fs_info->vol.dev, blk, &fs_info->c.buf);
430        else
431            sc = rtems_bdbuf_get(fs_info->vol.dev, blk, &fs_info->c.buf);
432        if (sc != RTEMS_SUCCESSFUL)
433            set_errno_and_return_minus_one(EIO);
434        fs_info->c.blk_num = blk;
435        fs_info->c.modified = 0;
436        fs_info->c.state = FAT_CACHE_ACTUAL;
437    }
438
439    sec_of_fat = ((fs_info->c.blk_num >= fs_info->vol.fat_loc) &&
440                  (fs_info->c.blk_num < fs_info->vol.rdir_loc));
441
442    if (fs_info->c.blk_num != blk)
443    {
444        if (fs_info->c.modified)
445        {
446            if (sec_of_fat && !fs_info->vol.mirror)
447                memcpy(fs_info->sec_buf, fs_info->c.buf->buffer,
448                       fs_info->vol.bps);
449
450            sc = rtems_bdbuf_release_modified(fs_info->c.buf);
451            fs_info->c.state = FAT_CACHE_EMPTY;
452            fs_info->c.modified = 0;
453            if (sc != RTEMS_SUCCESSFUL)
454                set_errno_and_return_minus_one(EIO);
455
456            if (sec_of_fat && !fs_info->vol.mirror)
457            {
458                bdbuf_buffer *b;
459
460                for (i = 1; i < fs_info->vol.fats; i++)
461                {
462                    sc = rtems_bdbuf_get(fs_info->vol.dev,
463                                         fs_info->c.blk_num +
464                                         fs_info->vol.fat_length * i,
465                                         &b);
466                    if ( sc != RTEMS_SUCCESSFUL)
467                        set_errno_and_return_minus_one(ENOMEM);
468                    memcpy(b->buffer, fs_info->sec_buf, fs_info->vol.bps);
469                    sc = rtems_bdbuf_release_modified(b);
470                    if ( sc != RTEMS_SUCCESSFUL)
471                        set_errno_and_return_minus_one(ENOMEM);
472                }
473            }
474        }
475        else
476        {
477            sc = rtems_bdbuf_release(fs_info->c.buf);
478            fs_info->c.state = FAT_CACHE_EMPTY;
479            if (sc != RTEMS_SUCCESSFUL)
480                set_errno_and_return_minus_one(EIO);
481
482        }
483        if (op_type == FAT_OP_TYPE_READ)
484            sc = rtems_bdbuf_read(fs_info->vol.dev, blk, &fs_info->c.buf);
485        else
486            sc = rtems_bdbuf_get(fs_info->vol.dev, blk, &fs_info->c.buf);
487        if (sc != RTEMS_SUCCESSFUL)
488            set_errno_and_return_minus_one(EIO);
489        fs_info->c.blk_num = blk;
490        fs_info->c.state = FAT_CACHE_ACTUAL;
491    }
492    *buf = fs_info->c.buf;
493    return RC_OK;
494}
495
496
497static inline int
498fat_buf_release(fat_fs_info_t *fs_info)
499{
500    rtems_status_code sc = RTEMS_SUCCESSFUL;
501    uint8_t           i;
502    rtems_boolean     sec_of_fat;
503
504    if (fs_info->c.state == FAT_CACHE_EMPTY)
505        return RC_OK;
506
507    sec_of_fat = ((fs_info->c.blk_num >= fs_info->vol.fat_loc) &&
508                  (fs_info->c.blk_num < fs_info->vol.rdir_loc));
509
510    if (fs_info->c.modified)
511    {
512        if (sec_of_fat && !fs_info->vol.mirror)
513            memcpy(fs_info->sec_buf, fs_info->c.buf->buffer, fs_info->vol.bps);
514
515        sc = rtems_bdbuf_release_modified(fs_info->c.buf);
516        if (sc != RTEMS_SUCCESSFUL)
517            set_errno_and_return_minus_one(EIO);
518        fs_info->c.modified = 0;
519
520        if (sec_of_fat && !fs_info->vol.mirror)
521        {
522            bdbuf_buffer *b;
523
524            for (i = 1; i < fs_info->vol.fats; i++)
525            {
526                sc = rtems_bdbuf_get(fs_info->vol.dev,
527                                     fs_info->c.blk_num +
528                                     fs_info->vol.fat_length * i,
529                                     &b);
530                if ( sc != RTEMS_SUCCESSFUL)
531                    set_errno_and_return_minus_one(ENOMEM);
532                memcpy(b->buffer, fs_info->sec_buf, fs_info->vol.bps);
533                sc = rtems_bdbuf_release_modified(b);
534                if ( sc != RTEMS_SUCCESSFUL)
535                    set_errno_and_return_minus_one(ENOMEM);
536            }
537        }
538    }
539    else
540    {
541        sc = rtems_bdbuf_release(fs_info->c.buf);
542        if (sc != RTEMS_SUCCESSFUL)
543            set_errno_and_return_minus_one(EIO);
544    }
545    fs_info->c.state = FAT_CACHE_EMPTY;
546    return RC_OK;
547}
548
549static inline void
550fat_buf_mark_modified(fat_fs_info_t *fs_info)
551{
552    fs_info->c.modified = TRUE;
553}
554
555
556
557ssize_t
558_fat_block_read(rtems_filesystem_mount_table_entry_t *mt_entry,
559                uint32_t                              start,
560                uint32_t                              offset,
561                uint32_t                              count,
562                void                                 *buff);
563
564ssize_t
565_fat_block_write(rtems_filesystem_mount_table_entry_t *mt_entry,
566                 uint32_t                              start,
567                 uint32_t                              offset,
568                 uint32_t                              count,
569                 const void                           *buff);
570
571ssize_t
572fat_cluster_read(rtems_filesystem_mount_table_entry_t *mt_entry,
573                  uint32_t                             cln,
574                  void                                *buff);
575
576ssize_t
577fat_cluster_write(rtems_filesystem_mount_table_entry_t *mt_entry,
578                   uint32_t                             cln,
579                   const void                          *buff);
580
581int
582fat_init_volume_info(rtems_filesystem_mount_table_entry_t *mt_entry);
583
584int
585fat_init_clusters_chain(rtems_filesystem_mount_table_entry_t *mt_entry,
586                        uint32_t                              start_cln);
587
588uint32_t
589fat_cluster_num_to_sector_num(rtems_filesystem_mount_table_entry_t *mt_entry,
590                              uint32_t                              cln);
591
592int
593fat_shutdown_drive(rtems_filesystem_mount_table_entry_t *mt_entry);
594
595
596uint32_t
597fat_get_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry);
598
599rtems_boolean
600fat_ino_is_unique(rtems_filesystem_mount_table_entry_t *mt_entry,
601                  uint32_t                              ino);
602
603void
604fat_free_unique_ino(rtems_filesystem_mount_table_entry_t *mt_entry,
605                    uint32_t                              ino);
606
607int
608fat_fat32_update_fsinfo_sector(
609  rtems_filesystem_mount_table_entry_t *mt_entry,
610  uint32_t                              free_count,
611  uint32_t                              next_free
612  );
613
614#ifdef __cplusplus
615}
616#endif
617
618#endif /* __DOSFS_FAT_H__ */
Note: See TracBrowser for help on using the repository browser.