Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:16

0001 /**
0002  * @file
0003  *
0004  * @brief Constants/Data Structures/Prototypes on a Volume with FAT Filesystem
0005  *
0006  * @ingroup DOSFS
0007  *
0008  * Constants/Data Structures/Prototypes for Low-Level
0009  * Operations on a Volume with FAT Filesystem
0010  */
0011 
0012 /*
0013  *
0014  *  Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
0015  *  Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
0016  *
0017  *  The license and distribution terms for this file may be
0018  *  found in the file LICENSE in this distribution or at
0019  *  http://www.rtems.org/license/LICENSE.
0020  */
0021 
0022 #ifndef __DOSFS_FAT_H__
0023 #define __DOSFS_FAT_H__
0024 
0025 #include <sys/param.h>
0026 #include <sys/endian.h>
0027 #include <string.h>
0028 
0029 #include <rtems/seterr.h>
0030 
0031 #include <errno.h>
0032 #include <rtems/bdbuf.h>
0033 
0034 #ifdef __cplusplus
0035 extern "C" {
0036 #endif
0037 
0038 /**
0039  * @addtogroup DOSFS
0040  *
0041  * @{
0042  */
0043 
0044 #ifndef RC_OK
0045 #define RC_OK 0
0046 #endif
0047 
0048 /*
0049  * Remember that all FAT file system on disk data structure is
0050  * "little endian"!
0051  * (derived from linux)
0052  */
0053 /*
0054  * Conversion from and to little-endian byte order. (no-op on i386/i486)
0055  *
0056  * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian,
0057  * BE = big-endian, c: W = word (16 bits), L = longword (32 bits)
0058  */
0059 #define CF_LE_W(v) le16toh(v)
0060 #define CF_LE_L(v) le32toh(v)
0061 #define CT_LE_W(v) htole16(v)
0062 #define CT_LE_L(v) htole32(v)
0063 
0064 #define FAT_HASH_SIZE   2
0065 #define FAT_HASH_MODULE FAT_HASH_SIZE
0066 
0067 
0068 #define FAT_SECTOR512_SIZE     512 /* sector size (bytes) */
0069 #define FAT_SECTOR512_BITS       9 /* log2(SECTOR_SIZE) */
0070 
0071 /* maximum + 1 number of clusters for FAT12 */
0072 #define FAT_FAT12_MAX_CLN      4085
0073 
0074 /* maximum + 1 number of clusters for FAT16 */
0075 #define FAT_FAT16_MAX_CLN      65525
0076 
0077 #define FAT_FAT12              0x01
0078 #define FAT_FAT16              0x02
0079 #define FAT_FAT32              0x04
0080 
0081 #define FAT_UNDEFINED_VALUE     (uint32_t)0xFFFFFFFF
0082 
0083 #define FAT_FAT12_EOC          0x0FF8
0084 #define FAT_FAT16_EOC          0xFFF8
0085 #define FAT_FAT32_EOC          (uint32_t)0x0FFFFFF8
0086 
0087 #define FAT_FAT12_FREE         0x0000
0088 #define FAT_FAT16_FREE         0x0000
0089 #define FAT_FAT32_FREE         0x00000000
0090 
0091 #define FAT_GENFAT_EOC         (uint32_t)0xFFFFFFFF
0092 #define FAT_GENFAT_FREE        (uint32_t)0x00000000
0093 
0094 #define FAT_FAT12_SHIFT        0x04
0095 
0096 #define FAT_FAT12_MASK         0x00000FFF
0097 #define FAT_FAT16_MASK         0x0000FFFF
0098 #define FAT_FAT32_MASK         (uint32_t)0x0FFFFFFF
0099 
0100 #define FAT_MAX_BPB_SIZE       90
0101 #define FAT_TOTAL_MBR_SIZE    512
0102 
0103 /* size of useful information in FSInfo sector */
0104 #define FAT_USEFUL_INFO_SIZE   12
0105 
0106 #define FAT_GET_ADDR(x, ofs)       ((uint8_t *)(x) + (ofs))
0107 
0108 #define FAT_GET_VAL8(x, ofs)       (uint8_t)(*((uint8_t *)(x) + (ofs)))
0109 
0110 #define FAT_GET_VAL16(x, ofs)                               \
0111     (uint16_t)( (*((uint8_t *)(x) + (ofs))) |           \
0112                   ((*((uint8_t *)(x) + (ofs) + 1)) << 8) )
0113 
0114 #define FAT_GET_VAL32(x, ofs)                                             \
0115     (uint32_t)( (uint32_t)(*((uint8_t *)(x) + (ofs))) |             \
0116                   ((uint32_t)(*((uint8_t *)(x) + (ofs) + 1)) << 8)  | \
0117                   ((uint32_t)(*((uint8_t *)(x) + (ofs) + 2)) << 16) | \
0118                   ((uint32_t)(*((uint8_t *)(x) + (ofs) + 3)) << 24) )
0119 
0120 #define FAT_SET_VAL8(x, ofs,val)                    \
0121                  (*((uint8_t *)(x)+(ofs))=(uint8_t)(val))
0122 
0123 #define FAT_SET_VAL16(x, ofs,val) do {              \
0124                  FAT_SET_VAL8((x),(ofs),(val));     \
0125                  FAT_SET_VAL8((x),(ofs)+1,(val)>>8);\
0126                  } while (0)
0127 
0128 #define FAT_SET_VAL32(x, ofs,val) do {               \
0129                  uint32_t val1 = val;                \
0130                  FAT_SET_VAL16((x),(ofs),(val1)&0xffff);\
0131                  FAT_SET_VAL16((x),(ofs)+2,(val1)>>16);\
0132                  } while (0)
0133 
0134 /* macros to access boot sector fields */
0135 #define FAT_GET_BR_JMPBOOT(x)                FAT_GET_VAL8( x,  0)
0136 #define FAT_SET_BR_JMPBOOT(x,val)            FAT_SET_VAL8( x,  0,val)
0137 
0138 #define FAT_GET_ADDR_BR_OEMNAME(x)           FAT_GET_ADDR( x,  3)
0139 #define FAT_BR_OEMNAME_SIZE              (8)
0140 
0141 #define FAT_GET_BR_BYTES_PER_SECTOR(x)       FAT_GET_VAL16(x, 11)
0142 #define FAT_SET_BR_BYTES_PER_SECTOR(x,val)   FAT_SET_VAL16(x, 11,val)
0143 
0144 #define FAT_GET_BR_SECTORS_PER_CLUSTER(x)    FAT_GET_VAL8( x, 13)
0145 #define FAT_SET_BR_SECTORS_PER_CLUSTER(x,val)FAT_SET_VAL8( x, 13,val)
0146 
0147 #define FAT_GET_BR_RESERVED_SECTORS_NUM(x)   FAT_GET_VAL16(x, 14)
0148 #define FAT_SET_BR_RESERVED_SECTORS_NUM(x,val) FAT_SET_VAL16(x, 14,val)
0149 
0150 #define FAT_GET_BR_FAT_NUM(x)                FAT_GET_VAL8( x, 16)
0151 #define FAT_SET_BR_FAT_NUM(x,val)            FAT_SET_VAL8( x, 16,val)
0152 
0153 #define FAT_GET_BR_FILES_PER_ROOT_DIR(x)     FAT_GET_VAL16(x, 17)
0154 #define FAT_SET_BR_FILES_PER_ROOT_DIR(x,val) FAT_SET_VAL16(x, 17,val)
0155 
0156 #define FAT_GET_BR_TOTAL_SECTORS_NUM16(x)    FAT_GET_VAL16(x, 19)
0157 #define FAT_SET_BR_TOTAL_SECTORS_NUM16(x,val)FAT_SET_VAL16(x, 19,val)
0158 
0159 #define FAT_GET_BR_MEDIA(x)                  FAT_GET_VAL8( x, 21)
0160 #define FAT_SET_BR_MEDIA(x,val)              FAT_SET_VAL8( x, 21,val)
0161 
0162 #define FAT_GET_BR_SECTORS_PER_FAT(x)        FAT_GET_VAL16(x, 22)
0163 #define FAT_SET_BR_SECTORS_PER_FAT(x,val)    FAT_SET_VAL16(x, 22,val)
0164 
0165 #define FAT_GET_BR_SECTORS_PER_TRACK(x)      FAT_GET_VAL16(x, 24)
0166 #define FAT_SET_BR_SECTORS_PER_TRACK(x,val)  FAT_SET_VAL16(x, 24,val)
0167 
0168 #define FAT_GET_BR_NUMBER_OF_HEADS(x)        FAT_GET_VAL16(x, 26)
0169 #define FAT_SET_BR_NUMBER_OF_HEADS(x,val)    FAT_SET_VAL16(x, 26,val)
0170 
0171 #define FAT_GET_BR_HIDDEN_SECTORS(x)         FAT_GET_VAL32(x, 28)
0172 #define FAT_SET_BR_HIDDEN_SECTORS(x,val)     FAT_SET_VAL32(x, 28,val)
0173 
0174 #define FAT_GET_BR_TOTAL_SECTORS_NUM32(x)    FAT_GET_VAL32(x, 32)
0175 #define FAT_SET_BR_TOTAL_SECTORS_NUM32(x,val) FAT_SET_VAL32(x, 32,val)
0176   /* --- start of FAT12/16 specific fields */
0177 #define FAT_GET_BR_DRVNUM(x)                 FAT_GET_VAL8( x, 36)
0178 #define FAT_SET_BR_DRVNUM(x,val)             FAT_SET_VAL8( x, 36,val)
0179 
0180 #define FAT_GET_BR_RSVD1(x)                  FAT_GET_VAL8( x, 37)
0181 #define FAT_SET_BR_RSVD1(x,val)              FAT_SET_VAL8( x, 37,val)
0182 
0183 #define FAT_GET_BR_BOOTSIG(x)                FAT_GET_VAL8( x, 38)
0184 #define FAT_SET_BR_BOOTSIG(x,val)            FAT_SET_VAL8( x, 38,val)
0185 #define FAT_BR_BOOTSIG_VAL               (0x29)
0186 
0187 #define FAT_GET_BR_VOLID(x)                  FAT_GET_VAL32(x, 39)
0188 #define FAT_SET_BR_VOLID(x,val)              FAT_SET_VAL32(x, 39,val)
0189 
0190 #define FAT_GET_ADDR_BR_VOLLAB(x)            FAT_GET_ADDR (x, 43)
0191 #define FAT_BR_VOLLAB_SIZE               (11)
0192 
0193 #define FAT_GET_ADDR_BR_FILSYSTYPE(x)        FAT_GET_ADDR (x, 54)
0194 #define FAT_BR_FILSYSTYPE_SIZE           (8)
0195   /* --- end of FAT12/16 specific fields */
0196   /* --- start of FAT32 specific fields */
0197 #define FAT_GET_BR_SECTORS_PER_FAT32(x)      FAT_GET_VAL32(x, 36)
0198 #define FAT_SET_BR_SECTORS_PER_FAT32(x,val)  FAT_SET_VAL32(x, 36,val)
0199 
0200 #define FAT_GET_BR_EXT_FLAGS(x)              FAT_GET_VAL16(x, 40)
0201 #define FAT_SET_BR_EXT_FLAGS(x,val)          FAT_SET_VAL16(x, 40,val)
0202 
0203 #define FAT_GET_BR_FSVER(x)                  FAT_GET_VAL16(x, 42)
0204 #define FAT_SET_BR_FSVER(x,val)              FAT_SET_VAL16(x, 42,val)
0205 
0206 #define FAT_GET_BR_FAT32_ROOT_CLUSTER(x)     FAT_GET_VAL32(x, 44)
0207 #define FAT_SET_BR_FAT32_ROOT_CLUSTER(x,val) FAT_SET_VAL32(x, 44,val)
0208 
0209 #define FAT_GET_BR_FAT32_FS_INFO_SECTOR(x)   FAT_GET_VAL16(x, 48)
0210 #define FAT_SET_BR_FAT32_FS_INFO_SECTOR(x,val) FAT_SET_VAL16(x, 48,val)
0211 
0212 #define FAT_GET_BR_FAT32_BK_BOOT_SECTOR(x)   FAT_GET_VAL16(x, 50)
0213 #define FAT_SET_BR_FAT32_BK_BOOT_SECTOR(x,val)  FAT_SET_VAL16(x, 50,val)
0214 
0215 #define FAT_GET_ADDR_BR_FAT32_RESERVED(x)    FAT_GET_ADDR (x, 52)
0216 #define FAT_BR_FAT32_RESERVED_SIZE       (12)
0217 
0218 #define FAT_GET_BR_FAT32_DRVNUM(x)           FAT_GET_VAL8( x, 64)
0219 #define FAT_SET_BR_FAT32_DRVNUM(x,val)       FAT_SET_VAL8( x, 64,val)
0220 
0221 #define FAT_GET_BR_FAT32_RSVD1(x)            FAT_GET_VAL8( x, 65)
0222 #define FAT_SET_BR_FAT32_RSVD1(x,val)        FAT_SET_VAL8( x, 65,val)
0223 
0224 #define FAT_GET_BR_FAT32_BOOTSIG(x)          FAT_GET_VAL8( x, 66)
0225 #define FAT_SET_BR_FAT32_BOOTSIG(x,val)      FAT_SET_VAL8( x, 66,val)
0226 #define FAT_BR_FAT32_BOOTSIG_VAL         (0x29)
0227 
0228 #define FAT_GET_BR_FAT32_VOLID(x)            FAT_GET_VAL32(x, 67)
0229 #define FAT_SET_BR_FAT32_VOLID(x,val)        FAT_SET_VAL32(x, 67,val)
0230 
0231 #define FAT_GET_ADDR_BR_FAT32_VOLLAB(x)      FAT_GET_ADDR (x, 71)
0232 #define FAT_BR_FAT32_VOLLAB_SIZE         (11)
0233 
0234 #define FAT_GET_ADDR_BR_FAT32_FILSYSTYPE(x)  FAT_GET_ADDR (x, 82)
0235 #define FAT_BR_FAT32_FILSYSTYPE_SIZE     (8)
0236   /* --- end of FAT32 specific fields */
0237 
0238 #define FAT_GET_BR_SIGNATURE(x)              FAT_GET_VAL16(x,510)
0239 #define FAT_SET_BR_SIGNATURE(x,val)          FAT_SET_VAL16(x,510,val)
0240 #define FAT_BR_SIGNATURE_VAL                (0xAA55)
0241 
0242   /*
0243    * FAT32 FSINFO description
0244    */
0245 #define FAT_GET_FSINFO_LEAD_SIGNATURE(x)      FAT_GET_VAL32(x,  0)
0246 #define FAT_SET_FSINFO_LEAD_SIGNATURE(x,val)  FAT_SET_VAL32(x,  0,val)
0247 #define FAT_FSINFO_LEAD_SIGNATURE_VALUE   (0x41615252)
0248 
0249 #define FAT_GET_FSINFO_STRUC_SIGNATURE(x)     FAT_GET_VAL32(x,484)
0250 #define FAT_SET_FSINFO_STRUC_SIGNATURE(x,val) FAT_SET_VAL32(x,484,val)
0251 #define FAT_FSINFO_STRUC_SIGNATURE_VALUE  (0x61417272)
0252 
0253 #define FAT_GET_FSINFO_TRAIL_SIGNATURE(x)     FAT_GET_VAL32(x,508)
0254 #define FAT_SET_FSINFO_TRAIL_SIGNATURE(x,val) FAT_SET_VAL32(x,508,val)
0255 #define FAT_FSINFO_TRAIL_SIGNATURE_VALUE  (0xAA550000)
0256 /*
0257  * I read FSInfo sector from offset 484 to access the information, so offsets
0258  * of these fields a relative
0259  */
0260 #define FAT_GET_FSINFO_FREE_CLUSTER_COUNT(x)      FAT_GET_VAL32(x, 4)
0261 #define FAT_SET_FSINFO_FREE_CLUSTER_COUNT(x,val)  FAT_SET_VAL32(x, 4,val)
0262 #define FAT_GET_FSINFO_NEXT_FREE_CLUSTER(x)       FAT_GET_VAL32(x, 8)
0263 #define FAT_SET_FSINFO_NEXT_FREE_CLUSTER(x,val)   FAT_SET_VAL32(x, 8,val)
0264 
0265 #define FAT_FSI_INFO                         484
0266 #define FAT_FSINFO_STRUCT_OFFSET             488
0267 #define FAT_FSINFO_FREE_CLUSTER_COUNT_OFFSET (FAT_FSINFO_STRUCT_OFFSET+0)
0268 
0269 #define FAT_FSINFO_NEXT_FREE_CLUSTER_OFFSET  (FAT_FSINFO_STRUCT_OFFSET+4)
0270 
0271 #define FAT_RSRVD_CLN                        0x02
0272 
0273 #define FAT_FSI_LEADSIG_SIZE                 0x04
0274 
0275 #define FAT_TOTAL_FSINFO_SIZE               512
0276 
0277 #define MS_BYTES_PER_CLUSTER_LIMIT           0x10000    /* 64K */
0278 #define MS_BYTES_PER_CLUSTER_LIMIT_FAT12     0x1000     /*  4K */
0279 
0280 #define FAT_BR_EXT_FLAGS_MIRROR              0x0080
0281 
0282 #define FAT_BR_EXT_FLAGS_FAT_NUM             0x000F
0283 
0284 #define FAT_BR_MEDIA_FIXED                  0xf8
0285 
0286 #define FAT_DIRENTRY_SIZE          32
0287 
0288 #define FAT_DIRENTRIES_PER_SEC512  16
0289 
0290 /*
0291  * Volume descriptor
0292  * Description of the volume the FAT filesystem is located on - generally
0293  * the fields of the structure correspond to Boot Sector and BPB Structure
0294  * fields
0295  */
0296 typedef struct fat_vol_s
0297 {
0298     uint16_t           bps;            /* bytes per sector */
0299     uint8_t            sec_log2;       /* log2 of bps */
0300     uint8_t            sec_mul;        /* log2 of 512bts sectors number per sector */
0301     uint8_t            spc;            /* sectors per cluster */
0302     uint8_t            spc_log2;       /* log2 of spc */
0303     uint32_t           bpc;            /* bytes per cluster */
0304     uint8_t            bpc_log2;       /* log2 of bytes per cluster */
0305     uint8_t            sectors_per_block;    /* sectors per bdbuf block */
0306     uint32_t           bytes_per_block;      /* number of bytes for the bduf block device handling */
0307     uint8_t            bytes_per_block_log2; /* log2 of bytes_per_block */
0308     uint8_t            fats;           /* number of FATs */
0309     uint8_t            type;           /* FAT type */
0310     uint32_t           mask;
0311     uint32_t           eoc_val;
0312     uint16_t           fat_loc;        /* FAT start */
0313     uint32_t           fat_length;     /* sectors per FAT */
0314     uint32_t           rdir_loc;       /* root directory start */
0315     uint16_t           rdir_entrs;     /* files per root directory */
0316     uint32_t           rdir_secs;      /* sectors per root directory */
0317     uint32_t           rdir_size;      /* root directory size in bytes */
0318     uint32_t           tot_secs;       /* total count of sectors */
0319     uint32_t           data_fsec;      /* first data sector */
0320     uint32_t           data_cls;       /* count of data clusters */
0321     uint32_t           rdir_cl;        /* first cluster of the root directory */
0322     uint16_t           info_sec;       /* FSInfo Sector Structure location */
0323     uint32_t           free_cls;       /* last known free clusters count */
0324     uint32_t           free_cls_in_fs_info; /* last known free clusters count
0325                                                in FS info sector */
0326     uint32_t           next_cl;        /* next free cluster number */
0327     uint32_t           next_cl_in_fs_info; /* next free cluster number in FS
0328                                               info sector */
0329     uint8_t            mirror;         /* mirroring enabla/disable */
0330     uint32_t           afat_loc;       /* active FAT location */
0331     uint8_t            afat;           /* the number of active FAT */
0332     int                fd;             /* the disk device file descriptor */
0333     rtems_disk_device *dd;             /* disk device (see libblock) */
0334     dev_t              dev;            /* device identifier of disk */
0335     void              *private_data;   /* reserved */
0336 } fat_vol_t;
0337 
0338 
0339 typedef struct fat_cache_s
0340 {
0341     uint32_t            blk_num;
0342     bool                modified;
0343     uint8_t             state;
0344     rtems_bdbuf_buffer *buf;
0345 } fat_cache_t;
0346 
0347 /*
0348  * This structure identifies the instance of the filesystem on the FAT
0349  * ("fat-file") level.
0350  */
0351 typedef struct fat_fs_info_s
0352 {
0353     fat_vol_t            vol;           /* volume descriptor */
0354     rtems_chain_control *vhash;         /* "vhash" of fat-file descriptors */
0355     rtems_chain_control *rhash;         /* "rhash" of fat-file descriptors */
0356     char                *uino;          /* array of unique ino numbers */
0357     uint32_t             index;
0358     uint32_t             uino_pool_size; /* size */
0359     uint32_t             uino_base;
0360     fat_cache_t          c;             /* cache */
0361     uint8_t             *sec_buf; /* just placeholder for anything */
0362 } fat_fs_info_t;
0363 
0364 /*
0365  * FAT position is a the cluster and the offset into the
0366  * cluster.
0367  */
0368 typedef struct fat_pos_s
0369 {
0370     uint32_t   cln;
0371     uint32_t   ofs;
0372 } fat_pos_t;
0373 
0374 /*
0375  * If the name we looking for is file we store not only first data cluster
0376  * number, but and cluster number and offset for directory entry for this
0377  * name. We also add the LFN start offset so we can delete it the whole
0378  * file name. We can then use this to delete the file.
0379  */
0380 typedef struct fat_dir_pos_s
0381 {
0382     fat_pos_t  sname;
0383     fat_pos_t  lname;
0384 } fat_dir_pos_t;
0385 
0386 /*
0387  * Set the long name entries to this value for a short file name.
0388  */
0389 #define FAT_FILE_SHORT_NAME (0xffffffff)
0390 
0391 #define FAT_FAT_OFFSET(fat_type, cln)                  \
0392     ((fat_type) & FAT_FAT12 ? ((cln) + ((cln) >> 1)) : \
0393      (fat_type) & FAT_FAT16 ? ((cln) << 1)           : \
0394      ((cln) << 2))
0395 
0396 #define FAT_CLUSTER_IS_ODD(n)  ((n) & 0x0001)
0397 
0398 #define FAT12_SHIFT      0x4    /* half of a byte */
0399 
0400 /* initial size of array of unique ino */
0401 #define FAT_UINO_POOL_INIT_SIZE  0x100
0402 
0403 /* cache support */
0404 #define FAT_CACHE_EMPTY   0x0
0405 #define FAT_CACHE_ACTUAL  0x1
0406 
0407 #define FAT_OP_TYPE_READ  0x1
0408 #define FAT_OP_TYPE_GET   0x2
0409 
0410 static inline void
0411 fat_dir_pos_init(
0412     fat_dir_pos_t *dir_pos
0413     )
0414 {
0415   dir_pos->sname.cln = 0;
0416   dir_pos->sname.ofs = 0;
0417   dir_pos->lname.cln = FAT_FILE_SHORT_NAME;
0418   dir_pos->lname.ofs = FAT_FILE_SHORT_NAME;
0419 }
0420 
0421 static inline uint32_t
0422 fat_cluster_num_to_sector_num(
0423     const fat_fs_info_t *fs_info,
0424     uint32_t             cln
0425     )
0426 {
0427     if ( (cln == 0) && (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)) )
0428         return fs_info->vol.rdir_loc;
0429 
0430     return (((cln - FAT_RSRVD_CLN) << fs_info->vol.spc_log2) +
0431             fs_info->vol.data_fsec);
0432 }
0433 
0434 static inline uint32_t
0435 fat_cluster_num_to_sector512_num(
0436     const fat_fs_info_t *fs_info,
0437     uint32_t             cln
0438     )
0439 {
0440     if (cln == 1)
0441         return 1;
0442 
0443     return (fat_cluster_num_to_sector_num(fs_info, cln) <<
0444             fs_info->vol.sec_mul);
0445 }
0446 
0447 static inline uint32_t
0448  fat_block_num_to_cluster_num (const fat_fs_info_t *fs_info,
0449                                const uint32_t block_number)
0450 {
0451   return block_number >> (fs_info->vol.bpc_log2 - fs_info->vol.bytes_per_block_log2);
0452 }
0453 
0454 static inline uint32_t
0455  fat_block_num_to_sector_num (const fat_fs_info_t *fs_info,
0456                               const uint32_t block_number)
0457 {
0458   return block_number << (fs_info->vol.bytes_per_block_log2 - fs_info->vol.sec_log2);
0459 }
0460 
0461 static inline uint32_t
0462  fat_sector_num_to_block_num (const fat_fs_info_t *fs_info,
0463                               const uint32_t sector_number)
0464 {
0465   return sector_number >> (fs_info->vol.bytes_per_block_log2 - fs_info->vol.sec_log2);
0466 }
0467 
0468 static inline uint32_t
0469  fat_sector_offset_to_block_offset (const fat_fs_info_t *fs_info,
0470                                     const uint32_t sector,
0471                                     const uint32_t sector_offset)
0472 {
0473   return sector_offset +
0474            ((sector -
0475               fat_block_num_to_sector_num (fs_info,
0476                   fat_sector_num_to_block_num (fs_info, sector)))
0477             << fs_info->vol.sec_log2);
0478 }
0479 
0480 static inline void
0481 fat_buf_mark_modified(fat_fs_info_t *fs_info)
0482 {
0483     fs_info->c.modified = true;
0484 }
0485 
0486 int
0487 fat_buf_access(fat_fs_info_t  *fs_info,
0488                uint32_t        sec_num,
0489                int             op_type,
0490                uint8_t       **sec_buf);
0491 
0492 int
0493 fat_buf_release(fat_fs_info_t *fs_info);
0494 
0495 ssize_t
0496 _fat_block_read(fat_fs_info_t                        *fs_info,
0497                 uint32_t                              start,
0498                 uint32_t                              offset,
0499                 uint32_t                              count,
0500                 void                                 *buff);
0501 
0502 void
0503 fat_block_peek(fat_fs_info_t                        *fs_info,
0504                const uint32_t                        blk,
0505                const uint32_t                        blk_cnt);
0506 
0507 ssize_t
0508 fat_cluster_write(fat_fs_info_t                    *fs_info,
0509                     uint32_t                          start_cln,
0510                     uint32_t                          offset,
0511                     uint32_t                          count,
0512                     const void                       *buff);
0513 
0514 ssize_t
0515 fat_sector_write(fat_fs_info_t                        *fs_info,
0516                  uint32_t                              start,
0517                  uint32_t                              offset,
0518                  uint32_t                              count,
0519                  const void                           *buff);
0520 
0521 ssize_t
0522 fat_cluster_set(fat_fs_info_t                        *fs_info,
0523                   uint32_t                              start,
0524                   uint32_t                              offset,
0525                   uint32_t                              count,
0526                   uint8_t                               pattern);
0527 
0528 
0529 int
0530 fat_init_volume_info(fat_fs_info_t *fs_info, const char *device);
0531 
0532 int
0533 fat_init_clusters_chain(fat_fs_info_t                        *fs_info,
0534                         uint32_t                              start_cln);
0535 
0536 int
0537 fat_shutdown_drive(fat_fs_info_t *fs_info);
0538 
0539 
0540 uint32_t
0541 fat_get_unique_ino(fat_fs_info_t *fs_info);
0542 
0543 bool
0544 fat_ino_is_unique(fat_fs_info_t                        *fs_info,
0545                   uint32_t                              ino);
0546 
0547 void
0548 fat_free_unique_ino(fat_fs_info_t                        *fs_info,
0549                     uint32_t                              ino);
0550 
0551 int
0552 fat_sync(fat_fs_info_t *fs_info);
0553 
0554 /** @} */
0555 
0556 #ifdef __cplusplus
0557 }
0558 #endif
0559 #endif /* __DOSFS_FAT_H__ */