sl@0: // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // @file sl@0: // various FAT utilities header file sl@0: // sl@0: // sl@0: sl@0: sl@0: sl@0: #ifndef TEST_FAT_UTILS_HEADER sl@0: #define TEST_FAT_UTILS_HEADER sl@0: sl@0: #include "f32_test_utils.h" sl@0: sl@0: sl@0: namespace Fat_Test_Utils sl@0: { sl@0: using namespace F32_Test_Utils; sl@0: sl@0: sl@0: #include "filesystem_fat.h" sl@0: using namespace FileSystem_FAT; sl@0: typedef TFatSubType TFatType; sl@0: sl@0: sl@0: sl@0: //############################################################################# sl@0: //# FAT-specific declarations sl@0: //############################################################################# sl@0: sl@0: sl@0: sl@0: TFatType GetFatType(RFs &aFs, TInt aDrive); sl@0: sl@0: //----------------------------------------------------------------------------- sl@0: sl@0: const TInt KDefaultSectorLog2 = 9; ///< Log2 of default sector size for FAT sl@0: const TInt KDefaultSectorSize = 1 << KDefaultSectorLog2; ///< Default sector size for FAT, 512 bytes sl@0: sl@0: //----------------------------------------------------------------------------- sl@0: sl@0: class TFatBootSector; sl@0: class TFSInfo; sl@0: sl@0: TInt ReadBootSector(RFs &aFs, TInt aDrive, TInt64 aMediaPos, TFatBootSector& aBootSector); sl@0: TInt WriteBootSector(RFs &aFs, TInt aDrive, TInt64 aMediaPos, const TFatBootSector& aBootSector); sl@0: sl@0: TInt ReadFSInfoSector(RFs &aFs, TInt aDrive, TInt64 aMediaPos, TFSInfo& aFsInfo); sl@0: TInt WriteFSInfoSector(RFs &aFs, TInt aDrive, TInt64 aMediaPos, const TFSInfo& aFsInfo); sl@0: sl@0: //----------------------------------------------------------------------------- sl@0: sl@0: sl@0: /** FAT formatting parameters structure */ sl@0: struct TFatFormatParam sl@0: { sl@0: TFatFormatParam(); sl@0: sl@0: TFatType iFatType; ///< explicit FAT type sl@0: TUint iSecPerCluster; ///< Sectors per cluster; 0 means "default" sl@0: TUint iReservedSectors; ///< Reserved Sectors; 0 means "default" sl@0: }; sl@0: sl@0: sl@0: TInt FormatFatDrive(RFs &aFs, TInt aDrive, TBool aQuickFormat, const TFatFormatParam* apFmtParams=NULL, TBool aForceErase = EFalse); sl@0: sl@0: sl@0: //----------------------------------------------------------------------------- sl@0: const TInt KFatFirstSearchCluser =2; ///< FAT usable clusters start from 2; FAT[0] and FAT[1] are reserved sl@0: const TInt KFatDirNameSize =11; ///< Dos directory/File name length sl@0: const TInt KFatDirReserved1Size =8; ///< Size of reserved area one in a directory enrty sl@0: const TInt KVolumeLabelSize =11; ///< Volume lable size sl@0: const TInt KFileSysTypeSize =8; ///< File system type parameter size sl@0: const TInt KVendorIdSize =8; ///< Vendor ID parameter size sl@0: const TInt KVFatEntryAttribute =0x0F; ///< VFat entry attribute setting sl@0: const TInt KBootSectorSignature =0xAA55;///< File system Boot sector signiture sl@0: const TUint8 KDotEntryByte =0x2e; ///< Dot value for self and parent pointer directory entries sl@0: const TUint8 KBlankSpace =0x20; ///< Blank space in a directory entry sl@0: const TInt KSizeOfFatDirEntry =32; ///< Size in bytes of a Fat directry entry sl@0: const TInt KSizeOfFatDirEntryLog2 =5; ///< Log2 of size in bytes of a Fat directry entry sl@0: const TInt KFat16VolumeLabelPos =43; ///< Position of volume lable in BPB for Fat12/16 sl@0: const TInt KFat32VolumeLabelPos =71; ///< Position of volume lable in BPB for Fat32 sl@0: const TInt KReservedIdOldEntry =1; ///< used for TFatDirEntry::SetReserved2(..) sl@0: const TInt KReservedIdNewEntry =0; sl@0: const TInt KSizeOfFatBootSector =90; ///< Size in bytes of Boot sector parameter block (BPB) sl@0: sl@0: const TUint32 KBootSectorNum =0; ///< Main Boot Sector number (always 0) sl@0: const TUint32 KFSInfoSectorNum =1; ///< Main FSInfo sector number. This is a default value. The actual value shall be taken from the BPB sl@0: sl@0: const TUint32 KBkBootSectorNum =6; ///< Backup Boot Sector number (must be 6 by default) sl@0: const TUint32 KBkFSInfoSectorNum =KBkBootSectorNum+1; ///< Backup FSInfo sector number, follows the backup bpb sector sl@0: sl@0: typedef TBuf8 TShortName; ///< Buffer type fot short names in dos entries sl@0: sl@0: //############################################################################# sl@0: //# FAT/FAT32 Boot sector support sl@0: //############################################################################# sl@0: sl@0: /** sl@0: Boot sector parameter block, enables access to all file system parameters. sl@0: Supports FAT12/16/32; code is taken from sl_bpb.h sl@0: */ sl@0: class TFatBootSector sl@0: { sl@0: public: sl@0: //-- simple getters / setters sl@0: const TPtrC8 VendorId() const; sl@0: TUint16 BytesPerSector() const; sl@0: TInt SectorsPerCluster() const; sl@0: TInt ReservedSectors() const; sl@0: TInt NumberOfFats() const; sl@0: TInt RootDirEntries() const; sl@0: TInt TotalSectors() const; sl@0: TUint8 MediaDescriptor() const; sl@0: TInt FatSectors() const; sl@0: TInt SectorsPerTrack() const; sl@0: TInt NumberOfHeads() const; sl@0: TInt HiddenSectors() const; sl@0: TInt HugeSectors() const; sl@0: TInt PhysicalDriveNumber() const; sl@0: TInt ExtendedBootSignature() const; sl@0: TUint32 UniqueID() const; sl@0: const TPtrC8 VolumeLabel() const; sl@0: const TPtrC8 FileSysType() const; sl@0: TInt BootSectorSignature() const; sl@0: TUint32 FatSectors32() const; sl@0: TUint16 FATFlags() const; sl@0: TUint16 VersionNumber() const; sl@0: TUint32 RootClusterNum() const; sl@0: TUint16 FSInfoSectorNum() const; sl@0: TUint16 BkBootRecSector() const; sl@0: sl@0: void SetJumpInstruction(); sl@0: void SetVendorID(const TDesC8& aDes); sl@0: void SetBytesPerSector(TInt aBytesPerSector); sl@0: void SetSectorsPerCluster(TInt aSectorsPerCluster); sl@0: void SetReservedSectors(TInt aReservedSectors); sl@0: void SetNumberOfFats(TInt aNumberOfFats); sl@0: void SetRootDirEntries(TInt aRootDirEntries); sl@0: void SetTotalSectors(TInt aTotalSectors); sl@0: void SetMediaDescriptor(TUint8 aMediaDescriptor); sl@0: void SetFatSectors(TInt aFatSectors); sl@0: void SetSectorsPerTrack(TInt aSectorsPerTrack); sl@0: void SetNumberOfHeads(TInt aNumberOfHeads); sl@0: void SetHiddenSectors(TUint32 aHiddenSectors); sl@0: void SetHugeSectors(TUint32 aTotalSectors); sl@0: void SetPhysicalDriveNumber(TInt aPhysicalDriveNumber); sl@0: void SetReservedByte(TUint8 aReservedByte); sl@0: void SetExtendedBootSignature(TInt anExtendedBootSignature); sl@0: void SetUniqueID(TUint32 anUniqueID); sl@0: void SetVolumeLabel(const TDesC8& aDes); sl@0: void SetFileSysType(const TDesC8& aDes); sl@0: void SetFatSectors32(TUint32 aFatSectors32); sl@0: void SetFATFlags(TUint16 aFATFlags); sl@0: void SetVersionNumber(TUint16 aVersionNumber); sl@0: void SetRootClusterNum(TUint32 aRootCusterNum); sl@0: void SetFSInfoSectorNum(TUint16 aFSInfoSectorNum); sl@0: void SetBkBootRecSector(TUint16 aBkBootRecSector); sl@0: sl@0: public: sl@0: TFatBootSector(); sl@0: sl@0: void Initialise(); sl@0: TBool IsValid() const; sl@0: TFatType FatType(void) const; sl@0: sl@0: void Internalize(const TDesC8& aBuf); sl@0: void Externalize(TDes8& aBuf) const; sl@0: void PrintDebugInfo() const; sl@0: sl@0: //-- more advanced API, works for all FAT types sl@0: TInt FirstFatSector() const; sl@0: TInt RootDirStartSector() const; sl@0: TInt FirstDataSector() const; sl@0: sl@0: TUint32 VolumeTotalSectorNumber() const; sl@0: TUint32 TotalFatSectors() const; sl@0: TUint32 RootDirSectors() const; sl@0: sl@0: TBool operator==(const TFatBootSector& aRhs); sl@0: sl@0: protected: sl@0: sl@0: TUint8 iJumpInstruction[3]; ///< +0 Jump instruction used for bootable volumes sl@0: TUint8 iVendorId[KVendorIdSize]; ///< +3 Vendor ID of the file system that formatted the volume sl@0: TUint16 iBytesPerSector; ///< +11/0x0b Bytes per sector sl@0: TUint8 iSectorsPerCluster; ///< +13/0x0d Sectors per cluster ratio sl@0: TUint16 iReservedSectors; ///< +14/0x0e Number of reserved sectors on the volume sl@0: TUint8 iNumberOfFats; ///< +16/0x10 Number of Fats on the volume sl@0: TUint16 iRootDirEntries; ///< +17/0x11 Number of entries allowed in the root directory, specific to Fat12/16, zero for FAT32 sl@0: TUint16 iTotalSectors; ///< +19/0x13 Total sectors on the volume, zero for FAT32 sl@0: TUint8 iMediaDescriptor; ///< +12/0x15 Media descriptor sl@0: TUint16 iFatSectors; ///< +22/0x16 Sectors used for the Fat table, zero for FAT32 sl@0: TUint16 iSectorsPerTrack; ///< +24/0x18 Sectors per track sl@0: TUint16 iNumberOfHeads; ///< +26/0x1a Number of heads sl@0: TUint32 iHiddenSectors; ///< +28/0x1c Number of hidden sectors in the volume sl@0: TUint32 iHugeSectors; ///< +32/0x20 Total sectors in the volume, Used if totalSectors > 65535 sl@0: TUint32 iFatSectors32; ///< +36/0x24 Start of additional elements @ offset 36 for FAT32, Sectors in Fat table for 32 bit volume sl@0: TUint16 iFATFlags; ///< +40/0x28 Fat flags sl@0: TUint16 iVersionNumber; ///< +42/0x2a Version number of the file system sl@0: TUint32 iRootClusterNum; ///< +44/0x2c Cluster number of the root directory sl@0: TUint16 iFSInfoSectorNum; ///< +48/0x30 Sector number containing the FSIInfo structure sl@0: TUint16 iBkBootRecSector; ///< +50/0x32 Backup boot sector sl@0: TUint8 iReserved2[12]; ///< +52/0x34 Reserved space, End of Fat32 Only parameters section sl@0: TUint8 iPhysicalDriveNumber; ///< +64/0x40 Physical drive number, not used in Symbian OS sl@0: TUint8 iReserved; ///< +65/0x41 Reserved byte sl@0: TUint8 iExtendedBootSignature; ///< +66/0x42 Extended boot signiture sl@0: TUint32 iUniqueID; ///< +67/0x43 Unique volume ID sl@0: TUint8 iVolumeLabel[KVolumeLabelSize]; ///< +71/0x47 The volume's label sl@0: TUint8 iFileSysType[KFileSysTypeSize]; ///< +82/0x52 File system type sl@0: }; sl@0: sl@0: sl@0: sl@0: sl@0: //############################################################################# sl@0: //# FAT32 FSInfo sector support sl@0: //############################################################################# sl@0: sl@0: sl@0: const TUint32 KSizeOfFSInfo = 0x200; ///< Size in bytes of the FSInfo structures sl@0: const TUint32 KFSInfoReserved1Size= 480; ///< Size of first reserved area in FSInfo sl@0: const TUint32 KFSInfoReserved2Size= 12; ///< Size of second reserved area in FSInfo sl@0: sl@0: //----------------------------------------------------------------------------- sl@0: sl@0: /** sl@0: TFSinfo hold the File system information comprising the free cluster count sl@0: and next free cluster. It is found in the sector after the BPB and contains sl@0: several identification signitures along with resverved space. It is not sl@0: mandatory to support this feature. sl@0: */ sl@0: class TFSInfo sl@0: { sl@0: sl@0: public: sl@0: TFSInfo(); sl@0: sl@0: void Initialise(); sl@0: TBool IsValid() const; sl@0: void Internalize(const TDesC8& aBuf); sl@0: void Externalize(TDes8& aBuf) const; sl@0: void PrintDebugInfo() const; sl@0: sl@0: public: sl@0: sl@0: TUint32 FreeClusterCount() const; sl@0: TUint32 NextFreeCluster() const; sl@0: sl@0: void SetFreeClusterCount(TUint32 aFreeCount); sl@0: void SetNextFreeCluster(TUint32 aNextCluster); sl@0: sl@0: TBool operator==(const TFSInfo& aRhs); sl@0: sl@0: protected: sl@0: TUint32 iLeadSig; ///< +0 lead in signature, should always equal 0x41615252 sl@0: TUint8 iReserved1[KFSInfoReserved1Size]; ///< +4 First reserved region of 480 bytes sl@0: TUint32 iStructureSig; ///< +484/0x1e4 Structure signature, should equal 0x61417272 sl@0: TUint32 iFreeCount; ///< +488/0x1e8 last known free cluster count sl@0: TUint32 iNextFree; ///< +492/0x1ec hint to file system as to where to start looking for free clusters sl@0: TUint8 iReserved2[KFSInfoReserved2Size]; ///< +496/0x1f0 Second reserved region of 12 bytes sl@0: TUint32 iTrainlingSig; ///< +508/0x1fc Trailing Signature (Bytes 510 and 511 = 55 and AA respectively) sl@0: }; sl@0: sl@0: sl@0: sl@0: //############################################################################# sl@0: //# FAT directory entries support sl@0: //############################################################################# sl@0: sl@0: const TInt KMaxFatFileName=12; sl@0: const TInt KMaxFatFileNameWithoutExt=8; sl@0: const TInt KMaxDuplicateShortName=0xFF; sl@0: const TInt KMaxVFatEntryName=13; sl@0: sl@0: //-- some helper functions sl@0: TTime DosTimeToTTime(TInt aDosTime,TInt aDosDate); sl@0: TInt DosTimeFromTTime(const TTime& aTime); sl@0: TInt DosDateFromTTime(const TTime& aTime); sl@0: TBuf8<12> DosNameToStdFormat(const TDesC8& aDosName); sl@0: TBuf8<12> DosNameFromStdFormat(const TDesC8& aStdFormatName); sl@0: TInt NumberOfVFatEntries(TInt aNameLength); sl@0: TUint8 CalculateShortNameCheckSum(const TDesC8& aShortName); sl@0: sl@0: sl@0: /** sl@0: Fat DOS directory entry structure sl@0: */ sl@0: struct SFatDirEntry sl@0: { sl@0: TUint8 iName[KFatDirNameSize]; ///< :0 File/Directory name sl@0: TUint8 iAttributes; ///< :11 File/Directory attributes sl@0: TUint8 iReserved1[2]; ///< :12 2 reserved bytes(in our implementation), some versions of Windows may use them sl@0: TUint16 iTimeC; ///< :14 Creation time sl@0: TUint16 iDateC; ///< :16 Creation date sl@0: 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 sl@0: TUint16 iStartClusterHi; ///< :20 High 16 bits of the File/Directory cluster number (Fat32 only) sl@0: TUint16 iTime; ///< :22 last write access time sl@0: TUint16 iDate; ///< :24 last write access date sl@0: TUint16 iStartClusterLo; ///< :26 Low 16 bits of the File/Directory cluster number sl@0: TUint32 iSize; ///< :28 File/Directory size in bytes sl@0: }; sl@0: sl@0: sl@0: //------------------------------------------------------------------------------------------------------------------- sl@0: sl@0: /** sl@0: Provides access to the Fat directory entry parameters sl@0: */ sl@0: class TFatDirEntry sl@0: { sl@0: public: sl@0: TFatDirEntry(); sl@0: void InitZ(); sl@0: sl@0: const TPtrC8 Name() const; sl@0: TInt Attributes() const; sl@0: TTime Time() const; sl@0: TInt StartCluster() const; sl@0: TUint32 Size() const; sl@0: TBool IsErased() const; sl@0: TBool IsCurrentDirectory() const; sl@0: TBool IsParentDirectory() const; sl@0: TBool IsEndOfDirectory() const; sl@0: TBool IsGarbage() const; sl@0: void SetName(const TDesC8& aDes); sl@0: void SetAttributes(TInt anAtt); sl@0: void SetTime(TTime aTime); sl@0: void SetCreateTime(TTime aTime); sl@0: void SetStartCluster(TInt aStartCluster); sl@0: void SetSize(TUint32 aFilesize); sl@0: void SetErased(); sl@0: void SetCurrentDirectory(); sl@0: void SetParentDirectory(); sl@0: void SetEndOfDirectory(); sl@0: //TUint RuggedFatEntryId() const; sl@0: //void SetRuggedFatEntryId(TUint16 aId); sl@0: sl@0: public: sl@0: void InitializeAsVFat(TUint8 aCheckSum); sl@0: void SetVFatEntry(const TDesC& aName,TInt aRemainderLen); sl@0: void ReadVFatEntry(TDes16& aVBuf) const; sl@0: TBool IsLongNameStart() const; sl@0: TBool IsVFatEntry() const; sl@0: TInt NumFollowing() const; sl@0: TUint8 CheckSum() const; sl@0: sl@0: sl@0: public: sl@0: TUint8 iData[KSizeOfFatDirEntry]; ///< The directory entry data sl@0: }; sl@0: sl@0: sl@0: sl@0: }//namespace Fat_Test_Utils sl@0: sl@0: sl@0: #endif //TEST_FAT_UTILS_HEADER sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: sl@0: