First public contribution.
1 // Copyright (c) 2005-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\ext\fat_ext.cpp
18 //! @SYMTestCaseID FSBASE-CR-JHAS-68YPX7
20 //! @SYMREQ CR JHAS-68YPX7
21 //! @SYMTestCaseDesc Test facility used by and bad disk handling test
22 //! @SYMTestStatus Implemented
23 //! @SYMTestActions Provided plug-in test extension for FAT
24 //! @SYMTestExpectedResults N/A
25 //! @SYMTestPriority Low
26 //! @SYMAuthor Ying Shi
27 //! @SYMCreationDate 20/05/2005
28 //! @See EFat and EFat32 components
29 //! @file f32test\ext\fat_ext.cpp
34 //-------------------------- CFatTestProxyDrive --------------------------
36 CFatTestProxyDrive* CFatTestProxyDrive::NewL(CProxyDrive* aProxyDrive, CMountCB* aMount)
38 __PRINT(_L("CFatTestProxyDrive::NewL"));
39 CFatTestProxyDrive* drive = new(ELeave) CFatTestProxyDrive(aProxyDrive,aMount);
43 CFatTestProxyDrive::CFatTestProxyDrive(CProxyDrive* aProxyDrive, CMountCB* aMount)
44 : CTestProxyDrive(aProxyDrive,aMount)
46 __PRINT(_L("CFatTestProxyDrive::CFatTestProxyDrive"));
50 TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt aMessageHandle,TInt aOffset,TInt aFlags)
52 // __PRINT(_L("CFatTestProxyDrive::Read"));
53 if (CheckEvent(aPos,aLength))
56 return CTestProxyDrive::Read(aPos,aLength,aTrg,aMessageHandle,aOffset,aFlags);
59 TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt aMessageHandle,TInt aOffset)
61 return Read(aPos,aLength,aTrg,aMessageHandle,aOffset,0);
64 TInt CFatTestProxyDrive::Read(TInt64 aPos,TInt aLength,TDes8& aTrg)
66 return Read(aPos,aLength,&aTrg,KLocalMessageHandle,0,0);
69 TInt CFatTestProxyDrive::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt aMessageHandle,TInt anOffset,TInt aFlags)
71 // __PRINT(_L("CFatTestProxyDrive::Write"));
72 if (CheckEvent(aPos,aLength))
75 return CTestProxyDrive::Write(aPos,aLength,aSrc,aMessageHandle,anOffset,aFlags);
78 TInt CFatTestProxyDrive::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt aMessageHandle,TInt anOffset)
80 return Write(aPos,aLength,aSrc,aMessageHandle,anOffset,0);
83 TInt CFatTestProxyDrive::Write(TInt64 aPos,const TDesC8& aSrc)
85 return Write(aPos,aSrc.Length(),&aSrc,KLocalMessageHandle,0,0);
88 TInt CFatTestProxyDrive::Format(TFormatInfo& anInfo)
90 //__PRINT(_L("CFatTestProxyDrive::Format"));
92 TInt64 pos = ((TInt64)anInfo.i512ByteSectorsFormatted) << KDefaultSectorLog2;
93 // base function call in order to get anInfo.iMaxBytesPerFormat
95 if (anInfo.iMaxBytesPerFormat == 0)
97 TInt r = CTestProxyDrive::Format(anInfo);
98 len = anInfo.iMaxBytesPerFormat;
99 if (CheckEvent(pos,len))
101 anInfo.i512ByteSectorsFormatted = 0;
106 len = anInfo.iMaxBytesPerFormat;
107 if (CheckEvent(pos,len))
109 return CTestProxyDrive::Format(anInfo);
112 TInt CFatTestProxyDrive::Format(TInt64 aPos,TInt aLength)
114 __PRINT(_L("CFatTestProxyDrive::Format"));
115 if (CheckEvent(aPos,aLength))
118 return CTestProxyDrive::Format(aPos, aLength);
121 void CFatTestProxyDrive::DoInitL()
123 __PRINT(_L("CFatTestProxyDrive::DoInit"));
125 User::Leave(KErrNotReady);
128 iTotalSectors = iBootSector.VolumeTotalSectorNumber();
131 TInt CFatTestProxyDrive::DoControlIO(const RMessagePtr2& aMessage,TInt aCommand,TAny* aParam1,TAny* aParam2)
133 __PRINT(_L("CFatTestProxyDrive::DoControlIO"));
135 // read boot sector & update iFatType etc
140 // Make sure that the information is up to date.
141 if ((r=ReadBootSector()) != KErrNone)
143 __PRINT1(_L("ReadBootSector error: %d"), r);
147 switch(aCommand+EExtCustom)
149 case ESectorsPerCluster:
150 r = aMessage.Write(2, TPckgBuf<TInt>(iBootSector.SectorsPerCluster()));
153 r = aMessage.Write(2, TPckgBuf<TInt>(iBootSector.FatType()));
156 case EGetDataPosition:
158 //-- obtain 1st data sector media position. This is actually a nasty hack;
159 //-- we expect that the drive will be freshly formatted, thust the root dir is empty and the first file we create there
160 //-- will occupy the certain place.
163 if(iBootSector.FatType() !=EFat32)
164 dataSec = iBootSector.FirstDataSector();
166 {//-- for FAT32 we assume that the root dir takes exactly 1 cluster. Another dirty trick
167 dataSec = iBootSector.RootDirStartSector() + 1*iBootSector.SectorsPerCluster();
169 __PRINT1(_L("EGetDataPosition, sec:%d"), dataSec);
170 r = aMessage.Write(2, TPckgBuf<TInt>(dataSec << KDefaultSectorLog2));
175 r = CBaseExtProxyDrive::ControlIO(aMessage,aCommand,aParam1,aParam2);
176 __PRINT2(_L("Get unknown command %d error %d"), aCommand, r);
181 TBool CFatTestProxyDrive::DoCheckEvent(TInt64 aPos, TInt aLength)
183 //__PRINT2(_L("CFatTestProxyDrive::DoCheckEvent() pos:%d, len:%d"), (TUint32)aPos, aLength);
185 if (aPos<0 || aLength<=0 || (aPos>>KDefaultSectorLog2)>=iTotalSectors)
188 TInt begin = (TInt)(aPos >> KDefaultSectorLog2);
189 TInt end = (TInt)((aPos+aLength-1) >> KDefaultSectorLog2);
190 end = Min(end, iTotalSectors-1);
192 if (iEventType == ENext)
196 iLastErrorReason = TErrorInfo::EBadSector;
201 if (iEventType == EDeterministic)
203 if (iCount <= end-begin+1)
206 Mark(begin+iCount-1);
208 iLastErrorReason = TErrorInfo::EBadSector;
209 iSuccessBytes = (iCount-1) << KDefaultSectorLog2;
213 iCount -= end-begin+1;
217 for (i=begin; i<=end; i++)
220 __PRINT(_L("CFatTestProxyDrive::DoCheckEvent() Sector Marked as bad!"));
221 iLastErrorReason = TErrorInfo::EBadSector;
222 iSuccessBytes = (i-begin) << KDefaultSectorLog2;
229 TBool CFatTestProxyDrive::CheckMount()
231 __PRINT(_L("CFatTestProxyDrive::CheckMount"));
233 //-- read boot sector
234 if (ReadBootSector() != KErrNone)
236 __PRINT(_L("ReadBootSector error: %d"));
240 //-- validate boot sector
241 if(!iBootSector.IsValid())
246 if (iBootSector.FatType() == EFat32) // fat 32
248 if (iBootSector.RootDirEntries() != 0 ||
249 iBootSector.TotalSectors() != 0 ||
250 iBootSector.HugeSectors() == 0 ||
251 iBootSector.FatSectors32() == 0 ||
252 iBootSector.RootClusterNum() < 2)
259 if (iBootSector.RootDirEntries() == 0 ||
260 (iBootSector.TotalSectors() == 0 && iBootSector.HugeSectors() == 0))
266 //-- boot sector is OK
269 //-- Invalid boot sector
272 __PRINT(_L("Boot sector is invalid! dump:"));
273 iBootSector.PrintDebugInfo();
280 TInt CFatTestProxyDrive::ReadBootSector()
282 __PRINT(_L("CFatTestProxyDrive::ReadBootSector"));
284 const TInt KBufSz = KSizeOfFatBootSector;
286 TBuf8<KBufSz> bootSecBuf(KBufSz);
287 TInt r = CTestProxyDrive::Read(0, KBufSz, bootSecBuf);
291 //-- initialise TFatBootSector object
292 iBootSector.Internalize(bootSecBuf);
298 // -------------------------- CFatTestProxyDriveFactory --------------------------
301 Factory class constructor
304 CFatTestProxyDriveFactory::CFatTestProxyDriveFactory()
309 Factory class installer
312 TInt CFatTestProxyDriveFactory::Install()
314 __PRINT(_L("CFatTestProxyDriveFactory::Install"));
315 _LIT(KFatTestExt,"FatTest");
316 return(SetName(&KFatTestExt));
322 CProxyDrive* CFatTestProxyDriveFactory::NewProxyDriveL(CProxyDrive* aProxy,CMountCB* aMount)
324 __PRINT(_L("CFatTestProxyDriveFactory::NewProxyDriveL"));
325 return(CFatTestProxyDrive::NewL(aProxy,aMount));
334 EXPORT_C CProxyDriveFactory* CreateFileSystem()
336 __PRINT(_L("CreateFileSystem"));
337 return new CFatTestProxyDriveFactory();