os/kernelhwsrv/kerneltest/f32test/ext/t_fatext.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/ext/t_fatext.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,339 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// f32test\ext\fat_ext.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +//! @SYMTestCaseID FSBASE-CR-JHAS-68YPX7
    1.22 +//! @SYMTestType CT
    1.23 +//! @SYMREQ CR JHAS-68YPX7
    1.24 +//! @SYMTestCaseDesc Test facility used by and bad disk handling test
    1.25 +//! @SYMTestStatus Implemented
    1.26 +//! @SYMTestActions Provided plug-in test extension for FAT
    1.27 +//! @SYMTestExpectedResults N/A
    1.28 +//! @SYMTestPriority Low
    1.29 +//! @SYMAuthor Ying Shi
    1.30 +//! @SYMCreationDate 20/05/2005
    1.31 +//! @See EFat and EFat32 components
    1.32 +//! @file f32test\ext\fat_ext.cpp
    1.33 +
    1.34 +#include <e32math.h>
    1.35 +#include "t_fatext.h"
    1.36 +
    1.37 +//--------------------------  CFatTestProxyDrive  --------------------------
    1.38 +
    1.39 +CFatTestProxyDrive* CFatTestProxyDrive::NewL(CProxyDrive* aProxyDrive, CMountCB* aMount)
    1.40 +    {
    1.41 +    __PRINT(_L("CFatTestProxyDrive::NewL"));
    1.42 +    CFatTestProxyDrive* drive = new(ELeave) CFatTestProxyDrive(aProxyDrive,aMount);
    1.43 +    return(drive);
    1.44 +    }
    1.45 +
    1.46 +CFatTestProxyDrive::CFatTestProxyDrive(CProxyDrive* aProxyDrive, CMountCB* aMount)
    1.47 +    : CTestProxyDrive(aProxyDrive,aMount)
    1.48 +    {
    1.49 +    __PRINT(_L("CFatTestProxyDrive::CFatTestProxyDrive"));
    1.50 +    InitL();
    1.51 +    }
    1.52 +
    1.53 +TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt aMessageHandle,TInt aOffset,TInt aFlags)
    1.54 +    {
    1.55 +//    __PRINT(_L("CFatTestProxyDrive::Read"));
    1.56 +    if (CheckEvent(aPos,aLength))
    1.57 +        return KErrCorrupt;
    1.58 +
    1.59 +    return CTestProxyDrive::Read(aPos,aLength,aTrg,aMessageHandle,aOffset,aFlags);
    1.60 +    }
    1.61 +
    1.62 +TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt aMessageHandle,TInt aOffset)
    1.63 +    {
    1.64 +    return Read(aPos,aLength,aTrg,aMessageHandle,aOffset,0);
    1.65 +    }
    1.66 +
    1.67 +TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,TDes8& aTrg)
    1.68 +    {
    1.69 +    return Read(aPos,aLength,&aTrg,KLocalMessageHandle,0,0);
    1.70 +    }
    1.71 +
    1.72 +TInt CFatTestProxyDrive::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt aMessageHandle,TInt anOffset,TInt aFlags)
    1.73 +    {
    1.74 +//    __PRINT(_L("CFatTestProxyDrive::Write"));
    1.75 +    if (CheckEvent(aPos,aLength))
    1.76 +        return KErrCorrupt;
    1.77 +
    1.78 +    return CTestProxyDrive::Write(aPos,aLength,aSrc,aMessageHandle,anOffset,aFlags);
    1.79 +    }
    1.80 +
    1.81 +TInt CFatTestProxyDrive::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt aMessageHandle,TInt anOffset)
    1.82 +    {
    1.83 +    return Write(aPos,aLength,aSrc,aMessageHandle,anOffset,0);
    1.84 +    }
    1.85 +
    1.86 +TInt CFatTestProxyDrive::Write(TInt64 aPos,const TDesC8& aSrc)
    1.87 +    {
    1.88 +    return Write(aPos,aSrc.Length(),&aSrc,KLocalMessageHandle,0,0);
    1.89 +    }
    1.90 +
    1.91 +TInt CFatTestProxyDrive::Format(TFormatInfo& anInfo)
    1.92 +    {
    1.93 +    //__PRINT(_L("CFatTestProxyDrive::Format"));
    1.94 +    TInt len;
    1.95 +    TInt64 pos = ((TInt64)anInfo.i512ByteSectorsFormatted) << KDefaultSectorLog2;
    1.96 +    // base function call in order to get anInfo.iMaxBytesPerFormat
    1.97 +    // for the first time
    1.98 +    if (anInfo.iMaxBytesPerFormat == 0)
    1.99 +        {
   1.100 +        TInt r = CTestProxyDrive::Format(anInfo);
   1.101 +        len = anInfo.iMaxBytesPerFormat;
   1.102 +        if (CheckEvent(pos,len))
   1.103 +            {
   1.104 +            anInfo.i512ByteSectorsFormatted = 0;
   1.105 +            return KErrCorrupt;
   1.106 +            }
   1.107 +        return r;
   1.108 +        }
   1.109 +    len = anInfo.iMaxBytesPerFormat;
   1.110 +    if (CheckEvent(pos,len))
   1.111 +        return KErrCorrupt;
   1.112 +    return CTestProxyDrive::Format(anInfo);
   1.113 +    }
   1.114 +
   1.115 +TInt CFatTestProxyDrive::Format(TInt64 aPos,TInt aLength)
   1.116 +    {
   1.117 +    __PRINT(_L("CFatTestProxyDrive::Format"));
   1.118 +    if (CheckEvent(aPos,aLength))
   1.119 +        return KErrCorrupt;
   1.120 +
   1.121 +    return CTestProxyDrive::Format(aPos, aLength);
   1.122 +    }
   1.123 +
   1.124 +void CFatTestProxyDrive::DoInitL()
   1.125 +    {
   1.126 +    __PRINT(_L("CFatTestProxyDrive::DoInit"));
   1.127 +    if (!CheckMount())
   1.128 +        User::Leave(KErrNotReady);
   1.129 +
   1.130 +   
   1.131 +    iTotalSectors = iBootSector.VolumeTotalSectorNumber();
   1.132 +    }
   1.133 +
   1.134 +TInt CFatTestProxyDrive::DoControlIO(const RMessagePtr2& aMessage,TInt aCommand,TAny* aParam1,TAny* aParam2)
   1.135 +    {
   1.136 +    __PRINT(_L("CFatTestProxyDrive::DoControlIO"));
   1.137 +
   1.138 +	// read boot sector & update iFatType etc
   1.139 +	CheckMount();
   1.140 +
   1.141 +    TInt r = KErrNone;
   1.142 +
   1.143 +	// Make sure that the information is up to date.
   1.144 +    if ((r=ReadBootSector()) != KErrNone)
   1.145 +        {
   1.146 +        __PRINT1(_L("ReadBootSector error: %d"), r);
   1.147 +        return EFalse;
   1.148 +        }
   1.149 +
   1.150 +    switch(aCommand+EExtCustom)
   1.151 +        {
   1.152 +        case ESectorsPerCluster:
   1.153 +            r = aMessage.Write(2, TPckgBuf<TInt>(iBootSector.SectorsPerCluster()));
   1.154 +            break;
   1.155 +        case EFatType:
   1.156 +            r = aMessage.Write(2, TPckgBuf<TInt>(iBootSector.FatType()));
   1.157 +            break;
   1.158 +        
   1.159 +        case EGetDataPosition:
   1.160 +            {
   1.161 +                //-- obtain 1st data sector media position. This is actually a nasty hack;
   1.162 +                //-- we expect that the drive will be freshly formatted, thust the root dir is empty and the first file we create there
   1.163 +                //-- will occupy the certain place.
   1.164 +                TUint32 dataSec; 
   1.165 +                
   1.166 +                if(iBootSector.FatType() !=EFat32)
   1.167 +                   dataSec = iBootSector.FirstDataSector();
   1.168 +                else
   1.169 +                  {//-- for FAT32 we assume that the root dir takes exactly 1 cluster. Another dirty trick
   1.170 +                   dataSec = iBootSector.RootDirStartSector() + 1*iBootSector.SectorsPerCluster(); 
   1.171 +                  }
   1.172 +                __PRINT1(_L("EGetDataPosition, sec:%d"), dataSec);
   1.173 +                r = aMessage.Write(2, TPckgBuf<TInt>(dataSec << KDefaultSectorLog2));
   1.174 +            }
   1.175 +            break;
   1.176 +
   1.177 +        default:
   1.178 +            r = CBaseExtProxyDrive::ControlIO(aMessage,aCommand,aParam1,aParam2);
   1.179 +            __PRINT2(_L("Get unknown command %d error %d"), aCommand, r);
   1.180 +        }
   1.181 +    return r;
   1.182 +    }
   1.183 +
   1.184 +TBool CFatTestProxyDrive::DoCheckEvent(TInt64 aPos, TInt aLength)
   1.185 +    {
   1.186 +    //__PRINT2(_L("CFatTestProxyDrive::DoCheckEvent() pos:%d, len:%d"), (TUint32)aPos, aLength);
   1.187 +
   1.188 +    if (aPos<0 || aLength<=0 || (aPos>>KDefaultSectorLog2)>=iTotalSectors)
   1.189 +        return EFalse;
   1.190 +
   1.191 +    TInt begin = (TInt)(aPos >> KDefaultSectorLog2);
   1.192 +    TInt end = (TInt)((aPos+aLength-1) >> KDefaultSectorLog2);
   1.193 +    end = Min(end, iTotalSectors-1);
   1.194 +
   1.195 +    if (iEventType == ENext)
   1.196 +        {
   1.197 +        Mark(begin);
   1.198 +        iEventType = ENone;
   1.199 +        iLastErrorReason = TErrorInfo::EBadSector;
   1.200 +        iSuccessBytes = 0;
   1.201 +        return ETrue;
   1.202 +        }
   1.203 +
   1.204 +    if (iEventType == EDeterministic)
   1.205 +        {
   1.206 +        if (iCount <= end-begin+1)
   1.207 +            {
   1.208 +            iCount = 0;
   1.209 +            Mark(begin+iCount-1);
   1.210 +            iEventType = ENone;
   1.211 +            iLastErrorReason = TErrorInfo::EBadSector;
   1.212 +            iSuccessBytes = (iCount-1) << KDefaultSectorLog2;
   1.213 +            return ETrue;
   1.214 +            }
   1.215 +        else
   1.216 +            iCount -= end-begin+1;
   1.217 +        }
   1.218 +
   1.219 +    TInt i;
   1.220 +    for (i=begin; i<=end; i++)
   1.221 +        if (IsMarked(i))
   1.222 +            {
   1.223 +            __PRINT(_L("CFatTestProxyDrive::DoCheckEvent() Sector Marked as bad!"));
   1.224 +            iLastErrorReason = TErrorInfo::EBadSector;
   1.225 +            iSuccessBytes = (i-begin) << KDefaultSectorLog2;
   1.226 +            return ETrue;
   1.227 +            }
   1.228 +
   1.229 +    return EFalse;
   1.230 +    }
   1.231 +
   1.232 +TBool CFatTestProxyDrive::CheckMount()
   1.233 +    {
   1.234 +    __PRINT(_L("CFatTestProxyDrive::CheckMount"));
   1.235 +
   1.236 +    //-- read boot sector
   1.237 +    if (ReadBootSector() != KErrNone)
   1.238 +        {
   1.239 +        __PRINT(_L("ReadBootSector error: %d"));
   1.240 +        return EFalse;
   1.241 +        }
   1.242 +
   1.243 +    //-- validate boot sector
   1.244 +    if(!iBootSector.IsValid())
   1.245 +    {
   1.246 +        goto BadBootSector;
   1.247 +    }
   1.248 +
   1.249 +    if (iBootSector.FatType() == EFat32)   // fat 32
   1.250 +        {
   1.251 +        if (iBootSector.RootDirEntries() != 0   ||
   1.252 +            iBootSector.TotalSectors() != 0     ||
   1.253 +            iBootSector.HugeSectors() == 0      ||
   1.254 +            iBootSector.FatSectors32() == 0     ||
   1.255 +            iBootSector.RootClusterNum() < 2)
   1.256 +            {
   1.257 +                goto BadBootSector;
   1.258 +            }
   1.259 +        }
   1.260 +    else // fat16/12
   1.261 +        {
   1.262 +        if (iBootSector.RootDirEntries() == 0 ||
   1.263 +            (iBootSector.TotalSectors() == 0 && iBootSector.HugeSectors() == 0))
   1.264 +            {
   1.265 +                goto BadBootSector;
   1.266 +            }
   1.267 +        }
   1.268 +
   1.269 +    //-- boot sector is OK
   1.270 +    return ETrue;
   1.271 +
   1.272 +    //-- Invalid boot sector
   1.273 +    BadBootSector:
   1.274 +        
   1.275 +        __PRINT(_L("Boot sector is invalid! dump:"));
   1.276 +        iBootSector.PrintDebugInfo();
   1.277 +        return EFalse;
   1.278 +
   1.279 +
   1.280 +
   1.281 +    }
   1.282 +
   1.283 +TInt CFatTestProxyDrive::ReadBootSector()
   1.284 +    {
   1.285 +    __PRINT(_L("CFatTestProxyDrive::ReadBootSector"));
   1.286 +
   1.287 +    const TInt KBufSz = KSizeOfFatBootSector;
   1.288 +    
   1.289 +    TBuf8<KBufSz> bootSecBuf(KBufSz);
   1.290 +    TInt r = CTestProxyDrive::Read(0, KBufSz, bootSecBuf);
   1.291 +	if (r != KErrNone)
   1.292 +		return r;
   1.293 +
   1.294 +    //-- initialise TFatBootSector object
   1.295 +    iBootSector.Internalize(bootSecBuf);
   1.296 +
   1.297 +    return KErrNone;
   1.298 +    }
   1.299 +
   1.300 +
   1.301 +// --------------------------  CFatTestProxyDriveFactory  --------------------------
   1.302 +
   1.303 +/**
   1.304 +Factory class constructor
   1.305 +@internalTechnology
   1.306 +*/
   1.307 +CFatTestProxyDriveFactory::CFatTestProxyDriveFactory()
   1.308 +    {
   1.309 +    }
   1.310 +
   1.311 +/**
   1.312 +Factory class installer
   1.313 +@internalTechnology
   1.314 +*/
   1.315 +TInt CFatTestProxyDriveFactory::Install()
   1.316 +    {
   1.317 +    __PRINT(_L("CFatTestProxyDriveFactory::Install"));
   1.318 +    _LIT(KFatTestExt,"FatTest");
   1.319 +    return(SetName(&KFatTestExt));
   1.320 +    }
   1.321 +
   1.322 +/**
   1.323 +@internalTechnology
   1.324 +*/
   1.325 +CProxyDrive* CFatTestProxyDriveFactory::NewProxyDriveL(CProxyDrive* aProxy,CMountCB* aMount)
   1.326 +    {
   1.327 +    __PRINT(_L("CFatTestProxyDriveFactory::NewProxyDriveL"));
   1.328 +    return(CFatTestProxyDrive::NewL(aProxy,aMount));
   1.329 +    }
   1.330 +
   1.331 +
   1.332 +
   1.333 +/**
   1.334 +@internalTechnology
   1.335 +*/
   1.336 +extern "C" {
   1.337 +EXPORT_C CProxyDriveFactory* CreateFileSystem()
   1.338 +    {
   1.339 +    __PRINT(_L("CreateFileSystem"));
   1.340 +    return new CFatTestProxyDriveFactory();
   1.341 +    }
   1.342 +}