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