Main Page   Data Structures   File List   Data Fields   Globals  

/fat.h

Go to the documentation of this file.
00001 /*! \file fat.h \brief FAT16/32 file system driver. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'fat.h'
00005 // Title        : FAT16/32 file system driver
00006 // Author       : Pascal Stang
00007 // Date         : 11/07/2000
00008 // Revised      : 12/12/2000
00009 // Version      : 0.3
00010 // Target MCU   : ATmega103 (should work for Atmel AVR Series)
00011 // Editor Tabs  : 4
00012 //
00013 // NOTE: This code is currently below version 1.0, and therefore is considered
00014 // to be lacking in some functionality or documentation, or may not be fully
00015 // tested.  Nonetheless, you can expect most functions to work.
00016 //
00017 // This code is distributed under the GNU Public License
00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00019 //
00020 //*****************************************************************************
00021 
00022 #ifndef FAT_H
00023 #define FAT_H
00024 
00025 #include "global.h"
00026 
00027 
00028 // Some useful cluster numbers
00029 #define MSDOSFSROOT     0               // cluster 0 means the root dir
00030 #define CLUST_FREE      0               // cluster 0 also means a free cluster
00031 #define MSDOSFSFREE     CLUST_FREE
00032 #define CLUST_FIRST     2               // first legal cluster number
00033 #define CLUST_RSRVD     0xfffffff6      // reserved cluster range
00034 #define CLUST_BAD       0xfffffff7      // a cluster with a defect
00035 #define CLUST_EOFS      0xfffffff8      // start of eof cluster range
00036 #define CLUST_EOFE      0xffffffff      // end of eof cluster range
00037 
00038 #define FAT12_MASK      0x00000fff      // mask for 12 bit cluster numbers
00039 #define FAT16_MASK      0x0000ffff      // mask for 16 bit cluster numbers
00040 #define FAT32_MASK      0x0fffffff      // mask for FAT32 cluster numbers
00041 
00042 
00043 // Partition Type used in the partition record
00044 #define PART_TYPE_UNKNOWN       0x00
00045 #define PART_TYPE_FAT12         0x01
00046 #define PART_TYPE_XENIX         0x02
00047 #define PART_TYPE_DOSFAT16      0x04
00048 #define PART_TYPE_EXTDOS        0x05
00049 #define PART_TYPE_FAT16         0x06
00050 #define PART_TYPE_NTFS          0x07
00051 #define PART_TYPE_FAT32         0x0B
00052 #define PART_TYPE_FAT32LBA      0x0C
00053 #define PART_TYPE_FAT16LBA      0x0E
00054 #define PART_TYPE_EXTDOSLBA     0x0F
00055 #define PART_TYPE_ONTRACK       0x33
00056 #define PART_TYPE_NOVELL        0x40
00057 #define PART_TYPE_PCIX          0x4B
00058 #define PART_TYPE_PHOENIXSAVE   0xA0
00059 #define PART_TYPE_CPM           0xDB
00060 #define PART_TYPE_DBFS          0xE0
00061 #define PART_TYPE_BBT           0xFF
00062 
00063 struct partrecord // length 16 bytes
00064 {           
00065     BYTE    prIsActive;                 // 0x80 indicates active partition
00066     BYTE    prStartHead;                // starting head for partition
00067     WORD    prStartCylSect;             // starting cylinder and sector
00068     BYTE    prPartType;                 // partition type (see above)
00069     BYTE    prEndHead;                  // ending head for this partition
00070     WORD    prEndCylSect;               // ending cylinder and sector
00071     DWORD   prStartLBA;                 // first LBA sector for this partition
00072     DWORD   prSize;                     // size of this partition (bytes or sectors ?)
00073 };
00074 
00075         
00076 struct partsector
00077 {
00078     CHAR    psPartCode[512-64-2];       // pad so struct is 512b
00079     BYTE    psPart[64];                 // four partition records (64 bytes)
00080     BYTE    psBootSectSig0;             // two signature bytes (2 bytes)
00081     BYTE    psBootSectSig1;
00082 #define BOOTSIG0        0x55
00083 #define BOOTSIG1        0xaa
00084 };
00085 
00086 
00087 
00088 // Format of a boot sector.  This is the first sector on a DOS floppy disk
00089 // or the first sector of a partition on a hard disk.  But, it is not the
00090 // first sector of a partitioned hard disk.
00091 struct bootsector33 {
00092     BYTE    bsJump[3];                  // jump inst E9xxxx or EBxx90
00093     CHAR    bsOemName[8];               // OEM name and version
00094     CHAR    bsBPB[19];                  // BIOS parameter block
00095     CHAR    bsDriveNumber;              // drive number (0x80)
00096     CHAR    bsBootCode[479];            // pad so struct is 512b
00097     BYTE    bsBootSectSig0;             // boot sector signature byte 0x55
00098     BYTE    bsBootSectSig1;             // boot sector signature byte 0xAA
00099 #define BOOTSIG0        0x55
00100 #define BOOTSIG1        0xaa
00101 };
00102 
00103 struct extboot {
00104     CHAR    exDriveNumber;              // drive number (0x80)
00105     CHAR    exReserved1;                // reserved
00106     CHAR    exBootSignature;            // ext. boot signature (0x29)
00107 #define EXBOOTSIG       0x29
00108     CHAR    exVolumeID[4];              // volume ID number
00109     CHAR    exVolumeLabel[11];          // volume label
00110     CHAR    exFileSysType[8];           // fs type (FAT12 or FAT16)
00111 };
00112 
00113 struct bootsector50 {
00114     BYTE    bsJump[3];                  // jump inst E9xxxx or EBxx90
00115     CHAR    bsOemName[8];               // OEM name and version
00116     CHAR    bsBPB[25];                  // BIOS parameter block
00117     CHAR    bsExt[26];                  // Bootsector Extension
00118     CHAR    bsBootCode[448];            // pad so structure is 512b
00119     BYTE    bsBootSectSig0;             // boot sector signature byte 0x55 
00120     BYTE    bsBootSectSig1;             // boot sector signature byte 0xAA
00121 #define BOOTSIG0        0x55
00122 #define BOOTSIG1        0xaa
00123 };
00124 
00125 struct bootsector710 {
00126     BYTE    bsJump[3];                  // jump inst E9xxxx or EBxx90
00127     CHAR    bsOEMName[8];               // OEM name and version
00128     CHAR    bsBPB[53];                  // BIOS parameter block
00129     CHAR    bsExt[26];                  // Bootsector Extension
00130     CHAR    bsBootCode[418];            // pad so structure is 512b
00131     BYTE    bsBootSectSig2;             // 2 & 3 are only defined for FAT32?
00132     BYTE    bsBootSectSig3;
00133     BYTE    bsBootSectSig0;             // boot sector signature byte 0x55
00134     BYTE    bsBootSectSig1;             // boot sector signature byte 0xAA
00135 #define BOOTSIG0        0x55
00136 #define BOOTSIG1        0xaa
00137 #define BOOTSIG2        0
00138 #define BOOTSIG3        0
00139 };
00140 
00141 
00142 /***************************************************************/
00143 /***************************************************************/
00144 
00145 // BIOS Parameter Block (BPB) for DOS 3.3
00146 struct bpb33 {
00147         WORD    bpbBytesPerSec; // bytes per sector
00148         BYTE    bpbSecPerClust; // sectors per cluster
00149         WORD    bpbResSectors;  // number of reserved sectors
00150         BYTE    bpbFATs;        // number of FATs
00151         WORD    bpbRootDirEnts; // number of root directory entries
00152         WORD    bpbSectors;     // total number of sectors
00153         BYTE    bpbMedia;       // media descriptor
00154         WORD    bpbFATsecs;     // number of sectors per FAT
00155         WORD    bpbSecPerTrack; // sectors per track
00156         WORD    bpbHeads;       // number of heads
00157         WORD    bpbHiddenSecs;  // number of hidden sectors
00158 };
00159 
00160 // BPB for DOS 5.0
00161 // The difference is bpbHiddenSecs is a short for DOS 3.3,
00162 // and bpbHugeSectors is not present in the DOS 3.3 bpb.
00163 struct bpb50 {
00164         WORD    bpbBytesPerSec; // bytes per sector
00165         BYTE    bpbSecPerClust; // sectors per cluster
00166         WORD    bpbResSectors;  // number of reserved sectors
00167         BYTE    bpbFATs;        // number of FATs
00168         WORD    bpbRootDirEnts; // number of root directory entries
00169         WORD    bpbSectors;     // total number of sectors
00170         BYTE    bpbMedia;       // media descriptor
00171         WORD    bpbFATsecs;     // number of sectors per FAT
00172         WORD    bpbSecPerTrack; // sectors per track
00173         WORD    bpbHeads;       // number of heads
00174         DWORD   bpbHiddenSecs;  // # of hidden sectors
00175 // 3.3 compat ends here
00176         DWORD   bpbHugeSectors; // # of sectors if bpbSectors == 0
00177 };
00178 
00179 // BPB for DOS 7.10 (FAT32)
00180 // This one has a few extensions to bpb50.
00181 struct bpb710 {
00182         WORD    bpbBytesPerSec; // bytes per sector
00183         BYTE    bpbSecPerClust; // sectors per cluster
00184         WORD    bpbResSectors;  // number of reserved sectors
00185         BYTE    bpbFATs;        // number of FATs
00186         WORD    bpbRootDirEnts; // number of root directory entries
00187         WORD    bpbSectors;     // total number of sectors
00188         BYTE    bpbMedia;       // media descriptor
00189         WORD    bpbFATsecs;     // number of sectors per FAT
00190         WORD    bpbSecPerTrack; // sectors per track
00191         WORD    bpbHeads;       // number of heads
00192         DWORD   bpbHiddenSecs;  // # of hidden sectors
00193 // 3.3 compat ends here
00194         DWORD   bpbHugeSectors; // # of sectors if bpbSectors == 0
00195 // 5.0 compat ends here
00196         DWORD     bpbBigFATsecs;// like bpbFATsecs for FAT32
00197         WORD      bpbExtFlags;  // extended flags:
00198 #define FATNUM    0xf           // mask for numbering active FAT
00199 #define FATMIRROR 0x80          // FAT is mirrored (like it always was)
00200         WORD      bpbFSVers;    // filesystem version
00201 #define FSVERS    0             // currently only 0 is understood
00202         DWORD     bpbRootClust; // start cluster for root directory
00203         WORD      bpbFSInfo;    // filesystem info structure sector
00204         WORD      bpbBackup;    // backup boot sector
00205         // There is a 12 byte filler here, but we ignore it
00206 };
00207 
00208 
00209 
00210 
00211 // ***************************************************************
00212 // * byte versions of the above structs                          *
00213 // ***************************************************************
00214 
00215 
00216 // BIOS Parameter Block (BPB) for DOS 3.3
00217 struct byte_bpb33 {
00218         CHAR bpbBytesPerSec[2];     // bytes per sector
00219         CHAR bpbSecPerClust;        // sectors per cluster
00220         CHAR bpbResSectors[2];      // number of reserved sectors
00221         CHAR bpbFATs;               // number of FATs
00222         CHAR bpbRootDirEnts[2];     // number of root directory entries
00223         CHAR bpbSectors[2];         // total number of sectors
00224         CHAR bpbMedia;              // media descriptor
00225         CHAR bpbFATsecs[2];         // number of sectors per FAT
00226         CHAR bpbSecPerTrack[2];     // sectors per track
00227         CHAR bpbHeads[2];           // number of heads
00228         CHAR bpbHiddenSecs[2];      // number of hidden sectors
00229 };
00230 
00231 // BPB for DOS 5.0
00232 // The difference is bpbHiddenSecs is a short for DOS 3.3,
00233 // and bpbHugeSectors is not in the 3.3 bpb.
00234 struct byte_bpb50 {
00235         CHAR bpbBytesPerSec[2];     // bytes per sector
00236         CHAR bpbSecPerClust;        // sectors per cluster
00237         CHAR bpbResSectors[2];      // number of reserved sectors
00238         CHAR bpbFATs;               // number of FATs
00239         CHAR bpbRootDirEnts[2];     // number of root directory entries
00240         CHAR bpbSectors[2];         // total number of sectors
00241         CHAR bpbMedia;              // media descriptor
00242         CHAR bpbFATsecs[2];         // number of sectors per FAT
00243         CHAR bpbSecPerTrack[2];     // sectors per track
00244         CHAR bpbHeads[2];           // number of heads
00245         CHAR bpbHiddenSecs[4];      // number of hidden sectors
00246         CHAR bpbHugeSectors[4];     // # of sectors if bpbSectors == 0
00247 };
00248 
00249 // BPB for DOS 7.10 (FAT32).
00250 // This one has a few extensions to bpb50.
00251 struct byte_bpb710 {
00252         BYTE bpbBytesPerSec[2];     // bytes per sector
00253         BYTE bpbSecPerClust;        // sectors per cluster
00254         BYTE bpbResSectors[2];      // number of reserved sectors
00255         BYTE bpbFATs;               // number of FATs
00256         BYTE bpbRootDirEnts[2];     // number of root directory entries
00257         BYTE bpbSectors[2];         // total number of sectors
00258         BYTE bpbMedia;              // media descriptor
00259         BYTE bpbFATsecs[2];         // number of sectors per FAT
00260         BYTE bpbSecPerTrack[2];     // sectors per track
00261         BYTE bpbHeads[2];           // number of heads
00262         BYTE bpbHiddenSecs[4];      // # of hidden sectors
00263         BYTE bpbHugeSectors[4];     // # of sectors if bpbSectors == 0
00264         BYTE bpbBigFATsecs[4];      // like bpbFATsecs for FAT32
00265         BYTE bpbExtFlags[2];        // extended flags:
00266         BYTE bpbFSVers[2];          // filesystem version
00267         BYTE bpbRootClust[4];       // start cluster for root directory
00268         BYTE bpbFSInfo[2];          // filesystem info structure sector
00269         BYTE bpbBackup[2];          // backup boot sector
00270         // There is a 12 byte filler here, but we ignore it
00271 };
00272 
00273 // FAT32 FSInfo block.
00274 struct fsinfo {
00275         BYTE fsisig1[4];
00276         BYTE fsifill1[480];
00277         BYTE fsisig2[4];
00278         BYTE fsinfree[4];
00279         BYTE fsinxtfree[4];
00280         BYTE fsifill2[12];
00281         BYTE fsisig3[4];
00282         BYTE fsifill3[508];
00283         BYTE fsisig4[4];
00284 };
00285 
00286 
00287 /***************************************************************/
00288 /***************************************************************/
00289 
00290 
00291 // Structure of a dos directory entry.
00292 struct direntry {
00293         BYTE        deName[8];      // filename, blank filled
00294 #define SLOT_EMPTY      0x00            // slot has never been used
00295 #define SLOT_E5         0x05            // the real value is 0xe5
00296 #define SLOT_DELETED    0xe5            // file in this slot deleted
00297         BYTE        deExtension[3]; // extension, blank filled
00298         BYTE        deAttributes;   // file attributes
00299 #define ATTR_NORMAL     0x00            // normal file
00300 #define ATTR_READONLY   0x01            // file is readonly
00301 #define ATTR_HIDDEN     0x02            // file is hidden
00302 #define ATTR_SYSTEM     0x04            // file is a system file
00303 #define ATTR_VOLUME     0x08            // entry is a volume label
00304 #define ATTR_LONG_FILENAME  0x0f        // this is a long filename entry                
00305 #define ATTR_DIRECTORY  0x10            // entry is a directory name
00306 #define ATTR_ARCHIVE    0x20            // file is new or modified
00307         BYTE        deLowerCase;    // NT VFAT lower case flags
00308 #define LCASE_BASE      0x08            // filename base in lower case
00309 #define LCASE_EXT       0x10            // filename extension in lower case
00310         BYTE        deCHundredth;   // hundredth of seconds in CTime
00311         BYTE        deCTime[2];     // create time
00312         BYTE        deCDate[2];     // create date
00313         BYTE        deADate[2];     // access date
00314         WORD        deHighClust;    // high bytes of cluster number
00315         BYTE        deMTime[2];     // last update time
00316         BYTE        deMDate[2];     // last update date
00317         WORD        deStartCluster; // starting cluster of file
00318         DWORD       deFileSize;     // size of file in bytes
00319 };
00320 
00321 // number of directory entries in one sector
00322 #define DIRENTRIES_PER_SECTOR   0x10
00323 
00324 // Structure of a Win95 long name directory entry
00325 struct winentry {
00326         BYTE        weCnt;
00327 #define WIN_LAST        0x40
00328 #define WIN_CNT         0x3f
00329         BYTE        wePart1[10];
00330         BYTE        weAttributes;
00331 #define ATTR_WIN95      0x0f
00332         BYTE        weReserved1;
00333         BYTE        weChksum;
00334         BYTE        wePart2[12];
00335         WORD        weReserved2;
00336         BYTE        wePart3[4];
00337 };
00338 
00339 #define WIN_CHARS   13      // Number of chars per winentry
00340 
00341 // Maximum filename length in Win95
00342 // Note: Must be < sizeof(dirent.d_name)
00343 #define WIN_MAXLEN      255
00344 
00345 // This is the format of the contents of the deTime field in the direntry
00346 // structure.
00347 // We don't use bitfields because we don't know how compilers for
00348 // arbitrary machines will lay them out.
00349 #define DT_2SECONDS_MASK        0x1F    // seconds divided by 2
00350 #define DT_2SECONDS_SHIFT       0
00351 #define DT_MINUTES_MASK         0x7E0   // minutes
00352 #define DT_MINUTES_SHIFT        5
00353 #define DT_HOURS_MASK           0xF800  // hours
00354 #define DT_HOURS_SHIFT          11
00355 
00356 // This is the format of the contents of the deDate field in the direntry
00357 // structure.
00358 #define DD_DAY_MASK             0x1F    // day of month
00359 #define DD_DAY_SHIFT            0
00360 #define DD_MONTH_MASK           0x1E0   // month
00361 #define DD_MONTH_SHIFT          5
00362 #define DD_YEAR_MASK            0xFE00  // year - 1980
00363 #define DD_YEAR_SHIFT           9
00364 
00365 // Prototypes
00366 unsigned char fatInit( unsigned char device);
00367 unsigned int fatClusterSize(void);
00368 unsigned long fatGetDirEntry(unsigned int entry, unsigned int count);
00369 unsigned long fatGetFilesize(void);
00370 char* fatGetFilename(void);
00371 char* fatGetDirname(void);
00372 void fatLoadCluster(unsigned long cluster, unsigned char *buffer);
00373 unsigned long fatNextCluster(unsigned long cluster);
00374 
00375 #endif

Generated on Fri Aug 1 10:42:41 2003 for Procyon AVRlib by doxygen1.2.18