os/kernelhwsrv/kerneltest/f32test/ext/t_fatext.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2005-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
// f32test\ext\fat_ext.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
//! @SYMTestCaseID FSBASE-CR-JHAS-68YPX7
sl@0
    19
//! @SYMTestType CT
sl@0
    20
//! @SYMREQ CR JHAS-68YPX7
sl@0
    21
//! @SYMTestCaseDesc Test facility used by and bad disk handling test
sl@0
    22
//! @SYMTestStatus Implemented
sl@0
    23
//! @SYMTestActions Provided plug-in test extension for FAT
sl@0
    24
//! @SYMTestExpectedResults N/A
sl@0
    25
//! @SYMTestPriority Low
sl@0
    26
//! @SYMAuthor Ying Shi
sl@0
    27
//! @SYMCreationDate 20/05/2005
sl@0
    28
//! @See EFat and EFat32 components
sl@0
    29
//! @file f32test\ext\fat_ext.cpp
sl@0
    30
sl@0
    31
#include <e32math.h>
sl@0
    32
#include "t_fatext.h"
sl@0
    33
sl@0
    34
//--------------------------  CFatTestProxyDrive  --------------------------
sl@0
    35
sl@0
    36
CFatTestProxyDrive* CFatTestProxyDrive::NewL(CProxyDrive* aProxyDrive, CMountCB* aMount)
sl@0
    37
    {
sl@0
    38
    __PRINT(_L("CFatTestProxyDrive::NewL"));
sl@0
    39
    CFatTestProxyDrive* drive = new(ELeave) CFatTestProxyDrive(aProxyDrive,aMount);
sl@0
    40
    return(drive);
sl@0
    41
    }
sl@0
    42
sl@0
    43
CFatTestProxyDrive::CFatTestProxyDrive(CProxyDrive* aProxyDrive, CMountCB* aMount)
sl@0
    44
    : CTestProxyDrive(aProxyDrive,aMount)
sl@0
    45
    {
sl@0
    46
    __PRINT(_L("CFatTestProxyDrive::CFatTestProxyDrive"));
sl@0
    47
    InitL();
sl@0
    48
    }
sl@0
    49
sl@0
    50
TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt aMessageHandle,TInt aOffset,TInt aFlags)
sl@0
    51
    {
sl@0
    52
//    __PRINT(_L("CFatTestProxyDrive::Read"));
sl@0
    53
    if (CheckEvent(aPos,aLength))
sl@0
    54
        return KErrCorrupt;
sl@0
    55
sl@0
    56
    return CTestProxyDrive::Read(aPos,aLength,aTrg,aMessageHandle,aOffset,aFlags);
sl@0
    57
    }
sl@0
    58
sl@0
    59
TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt aMessageHandle,TInt aOffset)
sl@0
    60
    {
sl@0
    61
    return Read(aPos,aLength,aTrg,aMessageHandle,aOffset,0);
sl@0
    62
    }
sl@0
    63
sl@0
    64
TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,TDes8& aTrg)
sl@0
    65
    {
sl@0
    66
    return Read(aPos,aLength,&aTrg,KLocalMessageHandle,0,0);
sl@0
    67
    }
sl@0
    68
sl@0
    69
TInt CFatTestProxyDrive::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt aMessageHandle,TInt anOffset,TInt aFlags)
sl@0
    70
    {
sl@0
    71
//    __PRINT(_L("CFatTestProxyDrive::Write"));
sl@0
    72
    if (CheckEvent(aPos,aLength))
sl@0
    73
        return KErrCorrupt;
sl@0
    74
sl@0
    75
    return CTestProxyDrive::Write(aPos,aLength,aSrc,aMessageHandle,anOffset,aFlags);
sl@0
    76
    }
sl@0
    77
sl@0
    78
TInt CFatTestProxyDrive::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt aMessageHandle,TInt anOffset)
sl@0
    79
    {
sl@0
    80
    return Write(aPos,aLength,aSrc,aMessageHandle,anOffset,0);
sl@0
    81
    }
sl@0
    82
sl@0
    83
TInt CFatTestProxyDrive::Write(TInt64 aPos,const TDesC8& aSrc)
sl@0
    84
    {
sl@0
    85
    return Write(aPos,aSrc.Length(),&aSrc,KLocalMessageHandle,0,0);
sl@0
    86
    }
sl@0
    87
sl@0
    88
TInt CFatTestProxyDrive::Format(TFormatInfo& anInfo)
sl@0
    89
    {
sl@0
    90
    //__PRINT(_L("CFatTestProxyDrive::Format"));
sl@0
    91
    TInt len;
sl@0
    92
    TInt64 pos = ((TInt64)anInfo.i512ByteSectorsFormatted) << KDefaultSectorLog2;
sl@0
    93
    // base function call in order to get anInfo.iMaxBytesPerFormat
sl@0
    94
    // for the first time
sl@0
    95
    if (anInfo.iMaxBytesPerFormat == 0)
sl@0
    96
        {
sl@0
    97
        TInt r = CTestProxyDrive::Format(anInfo);
sl@0
    98
        len = anInfo.iMaxBytesPerFormat;
sl@0
    99
        if (CheckEvent(pos,len))
sl@0
   100
            {
sl@0
   101
            anInfo.i512ByteSectorsFormatted = 0;
sl@0
   102
            return KErrCorrupt;
sl@0
   103
            }
sl@0
   104
        return r;
sl@0
   105
        }
sl@0
   106
    len = anInfo.iMaxBytesPerFormat;
sl@0
   107
    if (CheckEvent(pos,len))
sl@0
   108
        return KErrCorrupt;
sl@0
   109
    return CTestProxyDrive::Format(anInfo);
sl@0
   110
    }
sl@0
   111
sl@0
   112
TInt CFatTestProxyDrive::Format(TInt64 aPos,TInt aLength)
sl@0
   113
    {
sl@0
   114
    __PRINT(_L("CFatTestProxyDrive::Format"));
sl@0
   115
    if (CheckEvent(aPos,aLength))
sl@0
   116
        return KErrCorrupt;
sl@0
   117
sl@0
   118
    return CTestProxyDrive::Format(aPos, aLength);
sl@0
   119
    }
sl@0
   120
sl@0
   121
void CFatTestProxyDrive::DoInitL()
sl@0
   122
    {
sl@0
   123
    __PRINT(_L("CFatTestProxyDrive::DoInit"));
sl@0
   124
    if (!CheckMount())
sl@0
   125
        User::Leave(KErrNotReady);
sl@0
   126
sl@0
   127
   
sl@0
   128
    iTotalSectors = iBootSector.VolumeTotalSectorNumber();
sl@0
   129
    }
sl@0
   130
sl@0
   131
TInt CFatTestProxyDrive::DoControlIO(const RMessagePtr2& aMessage,TInt aCommand,TAny* aParam1,TAny* aParam2)
sl@0
   132
    {
sl@0
   133
    __PRINT(_L("CFatTestProxyDrive::DoControlIO"));
sl@0
   134
sl@0
   135
	// read boot sector & update iFatType etc
sl@0
   136
	CheckMount();
sl@0
   137
sl@0
   138
    TInt r = KErrNone;
sl@0
   139
sl@0
   140
	// Make sure that the information is up to date.
sl@0
   141
    if ((r=ReadBootSector()) != KErrNone)
sl@0
   142
        {
sl@0
   143
        __PRINT1(_L("ReadBootSector error: %d"), r);
sl@0
   144
        return EFalse;
sl@0
   145
        }
sl@0
   146
sl@0
   147
    switch(aCommand+EExtCustom)
sl@0
   148
        {
sl@0
   149
        case ESectorsPerCluster:
sl@0
   150
            r = aMessage.Write(2, TPckgBuf<TInt>(iBootSector.SectorsPerCluster()));
sl@0
   151
            break;
sl@0
   152
        case EFatType:
sl@0
   153
            r = aMessage.Write(2, TPckgBuf<TInt>(iBootSector.FatType()));
sl@0
   154
            break;
sl@0
   155
        
sl@0
   156
        case EGetDataPosition:
sl@0
   157
            {
sl@0
   158
                //-- obtain 1st data sector media position. This is actually a nasty hack;
sl@0
   159
                //-- we expect that the drive will be freshly formatted, thust the root dir is empty and the first file we create there
sl@0
   160
                //-- will occupy the certain place.
sl@0
   161
                TUint32 dataSec; 
sl@0
   162
                
sl@0
   163
                if(iBootSector.FatType() !=EFat32)
sl@0
   164
                   dataSec = iBootSector.FirstDataSector();
sl@0
   165
                else
sl@0
   166
                  {//-- for FAT32 we assume that the root dir takes exactly 1 cluster. Another dirty trick
sl@0
   167
                   dataSec = iBootSector.RootDirStartSector() + 1*iBootSector.SectorsPerCluster(); 
sl@0
   168
                  }
sl@0
   169
                __PRINT1(_L("EGetDataPosition, sec:%d"), dataSec);
sl@0
   170
                r = aMessage.Write(2, TPckgBuf<TInt>(dataSec << KDefaultSectorLog2));
sl@0
   171
            }
sl@0
   172
            break;
sl@0
   173
sl@0
   174
        default:
sl@0
   175
            r = CBaseExtProxyDrive::ControlIO(aMessage,aCommand,aParam1,aParam2);
sl@0
   176
            __PRINT2(_L("Get unknown command %d error %d"), aCommand, r);
sl@0
   177
        }
sl@0
   178
    return r;
sl@0
   179
    }
sl@0
   180
sl@0
   181
TBool CFatTestProxyDrive::DoCheckEvent(TInt64 aPos, TInt aLength)
sl@0
   182
    {
sl@0
   183
    //__PRINT2(_L("CFatTestProxyDrive::DoCheckEvent() pos:%d, len:%d"), (TUint32)aPos, aLength);
sl@0
   184
sl@0
   185
    if (aPos<0 || aLength<=0 || (aPos>>KDefaultSectorLog2)>=iTotalSectors)
sl@0
   186
        return EFalse;
sl@0
   187
sl@0
   188
    TInt begin = (TInt)(aPos >> KDefaultSectorLog2);
sl@0
   189
    TInt end = (TInt)((aPos+aLength-1) >> KDefaultSectorLog2);
sl@0
   190
    end = Min(end, iTotalSectors-1);
sl@0
   191
sl@0
   192
    if (iEventType == ENext)
sl@0
   193
        {
sl@0
   194
        Mark(begin);
sl@0
   195
        iEventType = ENone;
sl@0
   196
        iLastErrorReason = TErrorInfo::EBadSector;
sl@0
   197
        iSuccessBytes = 0;
sl@0
   198
        return ETrue;
sl@0
   199
        }
sl@0
   200
sl@0
   201
    if (iEventType == EDeterministic)
sl@0
   202
        {
sl@0
   203
        if (iCount <= end-begin+1)
sl@0
   204
            {
sl@0
   205
            iCount = 0;
sl@0
   206
            Mark(begin+iCount-1);
sl@0
   207
            iEventType = ENone;
sl@0
   208
            iLastErrorReason = TErrorInfo::EBadSector;
sl@0
   209
            iSuccessBytes = (iCount-1) << KDefaultSectorLog2;
sl@0
   210
            return ETrue;
sl@0
   211
            }
sl@0
   212
        else
sl@0
   213
            iCount -= end-begin+1;
sl@0
   214
        }
sl@0
   215
sl@0
   216
    TInt i;
sl@0
   217
    for (i=begin; i<=end; i++)
sl@0
   218
        if (IsMarked(i))
sl@0
   219
            {
sl@0
   220
            __PRINT(_L("CFatTestProxyDrive::DoCheckEvent() Sector Marked as bad!"));
sl@0
   221
            iLastErrorReason = TErrorInfo::EBadSector;
sl@0
   222
            iSuccessBytes = (i-begin) << KDefaultSectorLog2;
sl@0
   223
            return ETrue;
sl@0
   224
            }
sl@0
   225
sl@0
   226
    return EFalse;
sl@0
   227
    }
sl@0
   228
sl@0
   229
TBool CFatTestProxyDrive::CheckMount()
sl@0
   230
    {
sl@0
   231
    __PRINT(_L("CFatTestProxyDrive::CheckMount"));
sl@0
   232
sl@0
   233
    //-- read boot sector
sl@0
   234
    if (ReadBootSector() != KErrNone)
sl@0
   235
        {
sl@0
   236
        __PRINT(_L("ReadBootSector error: %d"));
sl@0
   237
        return EFalse;
sl@0
   238
        }
sl@0
   239
sl@0
   240
    //-- validate boot sector
sl@0
   241
    if(!iBootSector.IsValid())
sl@0
   242
    {
sl@0
   243
        goto BadBootSector;
sl@0
   244
    }
sl@0
   245
sl@0
   246
    if (iBootSector.FatType() == EFat32)   // fat 32
sl@0
   247
        {
sl@0
   248
        if (iBootSector.RootDirEntries() != 0   ||
sl@0
   249
            iBootSector.TotalSectors() != 0     ||
sl@0
   250
            iBootSector.HugeSectors() == 0      ||
sl@0
   251
            iBootSector.FatSectors32() == 0     ||
sl@0
   252
            iBootSector.RootClusterNum() < 2)
sl@0
   253
            {
sl@0
   254
                goto BadBootSector;
sl@0
   255
            }
sl@0
   256
        }
sl@0
   257
    else // fat16/12
sl@0
   258
        {
sl@0
   259
        if (iBootSector.RootDirEntries() == 0 ||
sl@0
   260
            (iBootSector.TotalSectors() == 0 && iBootSector.HugeSectors() == 0))
sl@0
   261
            {
sl@0
   262
                goto BadBootSector;
sl@0
   263
            }
sl@0
   264
        }
sl@0
   265
sl@0
   266
    //-- boot sector is OK
sl@0
   267
    return ETrue;
sl@0
   268
sl@0
   269
    //-- Invalid boot sector
sl@0
   270
    BadBootSector:
sl@0
   271
        
sl@0
   272
        __PRINT(_L("Boot sector is invalid! dump:"));
sl@0
   273
        iBootSector.PrintDebugInfo();
sl@0
   274
        return EFalse;
sl@0
   275
sl@0
   276
sl@0
   277
sl@0
   278
    }
sl@0
   279
sl@0
   280
TInt CFatTestProxyDrive::ReadBootSector()
sl@0
   281
    {
sl@0
   282
    __PRINT(_L("CFatTestProxyDrive::ReadBootSector"));
sl@0
   283
sl@0
   284
    const TInt KBufSz = KSizeOfFatBootSector;
sl@0
   285
    
sl@0
   286
    TBuf8<KBufSz> bootSecBuf(KBufSz);
sl@0
   287
    TInt r = CTestProxyDrive::Read(0, KBufSz, bootSecBuf);
sl@0
   288
	if (r != KErrNone)
sl@0
   289
		return r;
sl@0
   290
sl@0
   291
    //-- initialise TFatBootSector object
sl@0
   292
    iBootSector.Internalize(bootSecBuf);
sl@0
   293
sl@0
   294
    return KErrNone;
sl@0
   295
    }
sl@0
   296
sl@0
   297
sl@0
   298
// --------------------------  CFatTestProxyDriveFactory  --------------------------
sl@0
   299
sl@0
   300
/**
sl@0
   301
Factory class constructor
sl@0
   302
@internalTechnology
sl@0
   303
*/
sl@0
   304
CFatTestProxyDriveFactory::CFatTestProxyDriveFactory()
sl@0
   305
    {
sl@0
   306
    }
sl@0
   307
sl@0
   308
/**
sl@0
   309
Factory class installer
sl@0
   310
@internalTechnology
sl@0
   311
*/
sl@0
   312
TInt CFatTestProxyDriveFactory::Install()
sl@0
   313
    {
sl@0
   314
    __PRINT(_L("CFatTestProxyDriveFactory::Install"));
sl@0
   315
    _LIT(KFatTestExt,"FatTest");
sl@0
   316
    return(SetName(&KFatTestExt));
sl@0
   317
    }
sl@0
   318
sl@0
   319
/**
sl@0
   320
@internalTechnology
sl@0
   321
*/
sl@0
   322
CProxyDrive* CFatTestProxyDriveFactory::NewProxyDriveL(CProxyDrive* aProxy,CMountCB* aMount)
sl@0
   323
    {
sl@0
   324
    __PRINT(_L("CFatTestProxyDriveFactory::NewProxyDriveL"));
sl@0
   325
    return(CFatTestProxyDrive::NewL(aProxy,aMount));
sl@0
   326
    }
sl@0
   327
sl@0
   328
sl@0
   329
sl@0
   330
/**
sl@0
   331
@internalTechnology
sl@0
   332
*/
sl@0
   333
extern "C" {
sl@0
   334
EXPORT_C CProxyDriveFactory* CreateFileSystem()
sl@0
   335
    {
sl@0
   336
    __PRINT(_L("CreateFileSystem"));
sl@0
   337
    return new CFatTestProxyDriveFactory();
sl@0
   338
    }
sl@0
   339
}