kerneltest/f32test/fileutils/inc/fat_utils.h
changeset 43 96e5fb8b040d
child 81 e7d2d738d3c2
equal deleted inserted replaced
-1:000000000000 43:96e5fb8b040d
       
     1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // @file
       
    15 // various FAT utilities header file
       
    16 // 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #ifndef TEST_FAT_UTILS_HEADER
       
    22 #define TEST_FAT_UTILS_HEADER
       
    23 
       
    24 #include "f32_test_utils.h"
       
    25 
       
    26 
       
    27 namespace Fat_Test_Utils
       
    28 {
       
    29 using namespace F32_Test_Utils;
       
    30 
       
    31 
       
    32 #include "filesystem_fat.h"
       
    33 using namespace FileSystem_FAT;
       
    34 typedef TFatSubType TFatType;
       
    35 
       
    36 
       
    37 
       
    38 //#############################################################################
       
    39 //#     FAT-specific declarations
       
    40 //#############################################################################
       
    41 
       
    42 
       
    43 
       
    44 TFatType GetFatType(RFs &aFs, TInt aDrive);
       
    45 
       
    46 //-----------------------------------------------------------------------------
       
    47 
       
    48 const TInt KDefaultSectorLog2 = 9; ///< Log2 of default sector size for FAT
       
    49 const TInt KDefaultSectorSize = 1 << KDefaultSectorLog2; ///< Default sector size for FAT, 512 bytes
       
    50 
       
    51 //-----------------------------------------------------------------------------
       
    52 
       
    53 class TFatBootSector;
       
    54 class TFSInfo;
       
    55 
       
    56 TInt  ReadBootSector(RFs &aFs, TInt aDrive, TInt64 aMediaPos, TFatBootSector& aBootSector);
       
    57 TInt  WriteBootSector(RFs &aFs, TInt aDrive, TInt64 aMediaPos, const TFatBootSector& aBootSector);
       
    58 
       
    59 TInt  ReadFSInfoSector(RFs &aFs, TInt aDrive, TInt64 aMediaPos, TFSInfo& aFsInfo);
       
    60 TInt  WriteFSInfoSector(RFs &aFs, TInt aDrive, TInt64 aMediaPos, const TFSInfo& aFsInfo);
       
    61 
       
    62 //-----------------------------------------------------------------------------
       
    63 
       
    64 
       
    65 /** FAT formatting parameters structure */
       
    66 struct TFatFormatParam
       
    67 {
       
    68     TFatFormatParam();
       
    69         
       
    70     TFatType iFatType;          ///< explicit FAT type
       
    71     TUint    iSecPerCluster;    ///< Sectors per cluster; 0 means "default"
       
    72     TUint    iReservedSectors;  ///< Reserved Sectors; 0 means "default"
       
    73 };
       
    74 
       
    75 
       
    76 TInt FormatFatDrive(RFs &aFs, TInt aDrive, TBool aQuickFormat, const TFatFormatParam* apFmtParams=NULL, TBool aForceErase = EFalse); 
       
    77 
       
    78 
       
    79 //-----------------------------------------------------------------------------
       
    80 const TInt KFatFirstSearchCluser    =2;     ///< FAT usable clusters start from 2; FAT[0] and FAT[1] are reserved
       
    81 const TInt KFatDirNameSize			=11;    ///< Dos directory/File name length
       
    82 const TInt KFatDirReserved1Size		=8;     ///< Size of reserved area one in a directory enrty
       
    83 const TInt KVolumeLabelSize			=11;    ///< Volume lable size
       
    84 const TInt KFileSysTypeSize			=8;     ///< File system type parameter size
       
    85 const TInt KVendorIdSize			=8;     ///< Vendor ID parameter size
       
    86 const TInt KVFatEntryAttribute		=0x0F;  ///< VFat entry attribute setting
       
    87 const TInt KBootSectorSignature		=0xAA55;///< File system Boot sector signiture
       
    88 const TUint8 KDotEntryByte			=0x2e;  ///< Dot value for self and parent pointer directory entries
       
    89 const TUint8 KBlankSpace			=0x20;  ///< Blank space in a directory entry
       
    90 const TInt KSizeOfFatDirEntry		=32;    ///< Size in bytes of a Fat directry entry 
       
    91 const TInt KSizeOfFatDirEntryLog2	=5;     ///< Log2 of size in bytes of a Fat directry entry 
       
    92 const TInt KFat16VolumeLabelPos 	=43;    ///< Position of volume lable in BPB for Fat12/16
       
    93 const TInt KFat32VolumeLabelPos		=71;    ///< Position of volume lable in BPB for Fat32
       
    94 const TInt KReservedIdOldEntry		=1;	    ///< used for TFatDirEntry::SetReserved2(..)
       
    95 const TInt KReservedIdNewEntry		=0;
       
    96 const TInt KSizeOfFatBootSector     =90;    ///< Size in bytes of Boot sector parameter block (BPB) 
       
    97 
       
    98 const TUint32 KBootSectorNum        =0;     ///< Main Boot Sector number (always 0)
       
    99 const TUint32 KFSInfoSectorNum      =1;     ///< Main FSInfo sector number. This is a default value. The actual value shall be taken from the BPB
       
   100 
       
   101 const TUint32 KBkBootSectorNum      =6;     ///< Backup Boot Sector number (must be 6 by default)
       
   102 const TUint32 KBkFSInfoSectorNum    =KBkBootSectorNum+1;     ///< Backup FSInfo sector number, follows the backup bpb sector
       
   103 
       
   104 typedef TBuf8<KFatDirNameSize> TShortName;  ///< Buffer type fot short names in dos entries
       
   105 
       
   106 //#############################################################################
       
   107 //#                 FAT/FAT32 Boot sector support
       
   108 //#############################################################################
       
   109 
       
   110 /**
       
   111 Boot sector parameter block, enables access to all file system parameters.
       
   112 Supports FAT12/16/32; code is taken from sl_bpb.h
       
   113 */
       
   114 class TFatBootSector
       
   115 	{
       
   116 public:
       
   117     //-- simple getters / setters
       
   118 	const TPtrC8 VendorId() const;
       
   119 	TUint16 BytesPerSector() const;
       
   120 	TInt SectorsPerCluster() const;
       
   121 	TInt ReservedSectors() const;
       
   122 	TInt NumberOfFats() const;
       
   123 	TInt RootDirEntries() const;
       
   124 	TInt TotalSectors() const;
       
   125 	TUint8 MediaDescriptor() const;
       
   126 	TInt FatSectors() const;
       
   127 	TInt SectorsPerTrack() const;
       
   128 	TInt NumberOfHeads() const;
       
   129 	TInt HiddenSectors() const;
       
   130 	TInt HugeSectors() const;
       
   131 	TInt PhysicalDriveNumber() const;
       
   132 	TInt ExtendedBootSignature() const;
       
   133 	TUint32 UniqueID() const;
       
   134 	const TPtrC8 VolumeLabel() const;
       
   135 	const TPtrC8 FileSysType() const;
       
   136 	TInt BootSectorSignature() const;
       
   137 	TUint32 FatSectors32() const;	
       
   138 	TUint16 FATFlags() const;		
       
   139 	TUint16 VersionNumber() const;		
       
   140 	TUint32 RootClusterNum() const;	
       
   141 	TUint16 FSInfoSectorNum() const;
       
   142 	TUint16 BkBootRecSector() const;
       
   143 
       
   144 	void SetJumpInstruction();
       
   145 	void SetVendorID(const TDesC8& aDes);
       
   146 	void SetBytesPerSector(TInt aBytesPerSector);
       
   147 	void SetSectorsPerCluster(TInt aSectorsPerCluster);
       
   148 	void SetReservedSectors(TInt aReservedSectors);
       
   149 	void SetNumberOfFats(TInt aNumberOfFats);
       
   150 	void SetRootDirEntries(TInt aRootDirEntries);
       
   151 	void SetTotalSectors(TInt aTotalSectors);
       
   152 	void SetMediaDescriptor(TUint8 aMediaDescriptor);
       
   153 	void SetFatSectors(TInt aFatSectors);
       
   154 	void SetSectorsPerTrack(TInt aSectorsPerTrack);
       
   155 	void SetNumberOfHeads(TInt aNumberOfHeads);
       
   156 	void SetHiddenSectors(TUint32 aHiddenSectors);
       
   157 	void SetHugeSectors(TUint32 aTotalSectors);
       
   158 	void SetPhysicalDriveNumber(TInt aPhysicalDriveNumber);
       
   159 	void SetReservedByte(TUint8 aReservedByte);
       
   160 	void SetExtendedBootSignature(TInt anExtendedBootSignature);
       
   161 	void SetUniqueID(TUint32 anUniqueID);
       
   162 	void SetVolumeLabel(const TDesC8& aDes);
       
   163 	void SetFileSysType(const TDesC8& aDes);
       
   164 	void SetFatSectors32(TUint32	aFatSectors32);	
       
   165 	void SetFATFlags(TUint16 aFATFlags);		
       
   166 	void SetVersionNumber(TUint16	aVersionNumber);		
       
   167 	void SetRootClusterNum(TUint32 aRootCusterNum);	
       
   168 	void SetFSInfoSectorNum(TUint16 aFSInfoSectorNum);
       
   169 	void SetBkBootRecSector(TUint16 aBkBootRecSector);
       
   170 
       
   171 public:
       
   172     TFatBootSector();
       
   173 
       
   174     void Initialise();
       
   175     TBool IsValid() const;
       
   176     TFatType FatType(void) const;
       
   177 
       
   178     void Internalize(const TDesC8& aBuf);
       
   179     void Externalize(TDes8& aBuf) const;
       
   180 	void PrintDebugInfo() const;
       
   181 
       
   182     //-- more advanced API, works for all FAT types
       
   183     TInt FirstFatSector() const;
       
   184 	TInt RootDirStartSector() const;
       
   185 	TInt FirstDataSector() const;
       
   186     
       
   187     TUint32 VolumeTotalSectorNumber() const;
       
   188     TUint32 TotalFatSectors() const;
       
   189     TUint32 RootDirSectors() const;
       
   190 
       
   191     TBool operator==(const TFatBootSector& aRhs);
       
   192 
       
   193 protected:
       
   194 
       
   195     TUint8  iJumpInstruction[3];            ///< +0         Jump instruction used for bootable volumes
       
   196     TUint8  iVendorId[KVendorIdSize];       ///< +3         Vendor ID of the file system that formatted the volume
       
   197     TUint16 iBytesPerSector;                ///< +11/0x0b   Bytes per sector 
       
   198     TUint8  iSectorsPerCluster;             ///< +13/0x0d   Sectors per cluster ratio
       
   199     TUint16 iReservedSectors;               ///< +14/0x0e   Number of reserved sectors on the volume
       
   200     TUint8  iNumberOfFats;                  ///< +16/0x10   Number of Fats on the volume
       
   201     TUint16 iRootDirEntries;	            ///< +17/0x11   Number of entries allowed in the root directory, specific to Fat12/16, zero for FAT32
       
   202     TUint16 iTotalSectors;                  ///< +19/0x13   Total sectors on the volume, zero for FAT32
       
   203     TUint8  iMediaDescriptor;               ///< +12/0x15   Media descriptor
       
   204     TUint16 iFatSectors;                    ///< +22/0x16   Sectors used for the Fat table, zero for FAT32
       
   205     TUint16 iSectorsPerTrack;               ///< +24/0x18   Sectors per track
       
   206     TUint16 iNumberOfHeads;                 ///< +26/0x1a   Number of heads 
       
   207     TUint32 iHiddenSectors;                 ///< +28/0x1c   Number of hidden sectors in the volume
       
   208     TUint32 iHugeSectors;                   ///< +32/0x20   Total sectors in the volume, Used if totalSectors > 65535
       
   209 	TUint32	iFatSectors32;                  ///< +36/0x24   Start of additional elements @ offset 36 for FAT32, Sectors in Fat table for 32 bit volume
       
   210 	TUint16 iFATFlags;                      ///< +40/0x28   Fat flags
       
   211 	TUint16	iVersionNumber;		            ///< +42/0x2a   Version number of the file system
       
   212 	TUint32 iRootClusterNum;                ///< +44/0x2c   Cluster number of the root directory
       
   213 	TUint16 iFSInfoSectorNum;               ///< +48/0x30   Sector number containing the FSIInfo structure
       
   214 	TUint16 iBkBootRecSector;               ///< +50/0x32   Backup boot sector
       
   215 	TUint8	iReserved2[12];                 ///< +52/0x34   Reserved space, End of Fat32 Only parameters section
       
   216 	TUint8  iPhysicalDriveNumber;           ///< +64/0x40   Physical drive number, not used in Symbian OS
       
   217     TUint8  iReserved;                      ///< +65/0x41   Reserved byte
       
   218     TUint8  iExtendedBootSignature;         ///< +66/0x42   Extended boot signiture
       
   219     TUint32 iUniqueID;                      ///< +67/0x43   Unique volume ID
       
   220     TUint8  iVolumeLabel[KVolumeLabelSize]; ///< +71/0x47   The volume's label
       
   221 	TUint8  iFileSysType[KFileSysTypeSize]; ///< +82/0x52   File system type
       
   222 	};
       
   223 
       
   224 
       
   225 
       
   226 
       
   227 //#############################################################################
       
   228 //#                     FAT32 FSInfo sector support
       
   229 //#############################################################################
       
   230 
       
   231 
       
   232 const TUint32	KSizeOfFSInfo	    = 0x200; ///< Size in bytes of the FSInfo structures
       
   233 const TUint32	KFSInfoReserved1Size= 480;   ///< Size of first reserved area in FSInfo
       
   234 const TUint32	KFSInfoReserved2Size= 12;    ///< Size of second reserved area in FSInfo
       
   235 
       
   236 //-----------------------------------------------------------------------------
       
   237 
       
   238 /**
       
   239 TFSinfo hold the File system information comprising the free cluster count
       
   240 and next free cluster. It is found in the sector after the BPB and contains
       
   241 several identification signitures along with resverved space. It is not
       
   242 mandatory to support this feature.
       
   243 */
       
   244 class TFSInfo
       
   245 	{
       
   246 
       
   247 public:
       
   248 	TFSInfo();
       
   249 
       
   250     void  Initialise();  
       
   251     TBool IsValid() const;
       
   252     void Internalize(const TDesC8& aBuf);
       
   253     void Externalize(TDes8& aBuf) const;
       
   254 	void PrintDebugInfo() const;
       
   255 
       
   256 public:
       
   257 
       
   258     TUint32 FreeClusterCount() const; 
       
   259 	TUint32 NextFreeCluster() const;
       
   260 
       
   261 	void SetFreeClusterCount(TUint32 aFreeCount);
       
   262 	void SetNextFreeCluster(TUint32 aNextCluster);
       
   263 
       
   264     TBool operator==(const TFSInfo& aRhs);
       
   265 
       
   266 protected:
       
   267 	TUint32 iLeadSig;                           ///< +0         lead in signature, should always equal 0x41615252
       
   268 	TUint8	iReserved1[KFSInfoReserved1Size];   ///< +4         First reserved region of 480 bytes
       
   269 	TUint32 iStructureSig;                      ///< +484/0x1e4 Structure signature, should equal 0x61417272
       
   270 	TUint32 iFreeCount;                         ///< +488/0x1e8 last known free cluster count
       
   271 	TUint32 iNextFree;                          ///< +492/0x1ec hint to file system as to where to start looking for free clusters
       
   272 	TUint8	iReserved2[KFSInfoReserved2Size];   ///< +496/0x1f0 Second reserved region of 12 bytes
       
   273 	TUint32	iTrainlingSig;                      ///< +508/0x1fc Trailing Signature (Bytes 510 and 511 = 55 and AA respectively)
       
   274 	};
       
   275 
       
   276 
       
   277 
       
   278 //#############################################################################
       
   279 //#                     FAT directory entries support
       
   280 //#############################################################################
       
   281 
       
   282 const TInt KMaxFatFileName=12;
       
   283 const TInt KMaxFatFileNameWithoutExt=8;
       
   284 const TInt KMaxDuplicateShortName=0xFF;
       
   285 const TInt KMaxVFatEntryName=13;
       
   286 
       
   287 //-- some helper functions
       
   288 TTime DosTimeToTTime(TInt aDosTime,TInt aDosDate);
       
   289 TInt DosTimeFromTTime(const TTime& aTime);
       
   290 TInt DosDateFromTTime(const TTime& aTime);
       
   291 TBuf8<12> DosNameToStdFormat(const TDesC8& aDosName);
       
   292 TBuf8<12> DosNameFromStdFormat(const TDesC8& aStdFormatName);
       
   293 TInt NumberOfVFatEntries(TInt aNameLength);
       
   294 TUint8 CalculateShortNameCheckSum(const TDesC8& aShortName);
       
   295 
       
   296 
       
   297 /**
       
   298     Fat DOS directory entry structure
       
   299 */
       
   300 struct SFatDirEntry
       
   301     {
       
   302     TUint8  iName[KFatDirNameSize]; ///< :0  File/Directory name
       
   303     TUint8  iAttributes;            ///< :11 File/Directory attributes
       
   304     TUint8  iReserved1[2];          ///< :12 2 reserved bytes(in our implementation), some versions of Windows may use them
       
   305     TUint16 iTimeC;                 ///< :14 Creation time
       
   306     TUint16 iDateC;                 ///< :16 Creation date
       
   307     TUint16 iReserved2;             ///< :18 2 reserved bytes(in our implementation), FAT specs say that this is "last access date". Rugged FAT uses them as a special entry ID
       
   308     TUint16 iStartClusterHi;        ///< :20 High 16 bits of the File/Directory cluster number (Fat32 only)
       
   309     TUint16 iTime;                  ///< :22 last write access time 
       
   310     TUint16 iDate;                  ///< :24 last write access date 
       
   311     TUint16 iStartClusterLo;        ///< :26 Low 16 bits of the File/Directory cluster number 
       
   312     TUint32 iSize;                  ///< :28 File/Directory size in bytes
       
   313     };
       
   314 
       
   315 
       
   316 //-------------------------------------------------------------------------------------------------------------------
       
   317 
       
   318 /**
       
   319 Provides access to the Fat directory entry parameters
       
   320 */
       
   321 class TFatDirEntry
       
   322     {
       
   323 public:
       
   324     TFatDirEntry();
       
   325     void InitZ();
       
   326 
       
   327     const TPtrC8 Name() const;
       
   328     TInt Attributes() const;
       
   329     TTime Time() const;
       
   330     TInt StartCluster() const;
       
   331     TUint32 Size() const;
       
   332     TBool IsErased() const;
       
   333     TBool IsCurrentDirectory() const;
       
   334     TBool IsParentDirectory() const;
       
   335     TBool IsEndOfDirectory() const;
       
   336     TBool IsGarbage() const;
       
   337     void SetName(const TDesC8& aDes);
       
   338     void SetAttributes(TInt anAtt);
       
   339     void SetTime(TTime aTime);
       
   340     void SetCreateTime(TTime aTime);
       
   341     void SetStartCluster(TInt aStartCluster);
       
   342     void SetSize(TUint32 aFilesize);
       
   343     void SetErased();
       
   344     void SetCurrentDirectory();
       
   345     void SetParentDirectory();
       
   346     void SetEndOfDirectory();
       
   347     //TUint RuggedFatEntryId() const;
       
   348     //void  SetRuggedFatEntryId(TUint16 aId);
       
   349 
       
   350 public:
       
   351     void InitializeAsVFat(TUint8 aCheckSum);
       
   352     void SetVFatEntry(const TDesC& aName,TInt aRemainderLen);
       
   353     void ReadVFatEntry(TDes16& aVBuf) const;
       
   354     TBool IsLongNameStart() const;
       
   355     TBool IsVFatEntry() const;
       
   356     TInt NumFollowing() const;
       
   357     TUint8 CheckSum() const;
       
   358 
       
   359 
       
   360 public:
       
   361     TUint8 iData[KSizeOfFatDirEntry]; ///< The directory entry data
       
   362     };
       
   363 
       
   364 
       
   365 
       
   366 }//namespace Fat_Test_Utils
       
   367 
       
   368 
       
   369 #endif //TEST_FAT_UTILS_HEADER
       
   370 
       
   371 
       
   372 
       
   373 
       
   374 
       
   375 
       
   376 
       
   377 
       
   378 
       
   379 
       
   380 
       
   381 
       
   382 
       
   383 
       
   384 
       
   385 
       
   386 
       
   387 
       
   388 
       
   389 
       
   390 
       
   391 
       
   392 
       
   393 
       
   394 
       
   395 
       
   396 
       
   397 
       
   398 
       
   399 
       
   400 
       
   401 
       
   402 
       
   403