Update contrib.
1 // Copyright (c) 1998-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 // f32\sfat\sl_check.cpp
18 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 //!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
23 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 #include "sl_scandrv.h"
29 const TInt KMaxBufferSize=8192;
32 TBool CCheckFatTable::IsEof16Bit(TInt aCluster) const
34 return(aCluster>=0xFFF8 && aCluster<=0xFFFF);
37 TBool CCheckFatTable::IsEof12Bit(TInt aCluster) const
39 return(aCluster>=0xFF8 && aCluster<=0xFFF);
42 TInt CCheckFatTable::MaxFatIndex() const
44 __ASSERT_DEBUG((TUint)iMaxFatIndex>=KFatFirstSearchCluster, Fault(ECheckFatIndexZero));
49 CCheckFatTable* CCheckFatTable::NewL(CFatMountCB* aOwner)
51 // Create a CCheckFatTable
54 CCheckFatTable* fatTable;
55 fatTable=new(ELeave) CCheckFatTable(aOwner);
60 CCheckFatTable::CCheckFatTable(CFatMountCB* aOwner)
68 CCheckFatTable::~CCheckFatTable()
73 User::Free(iCheckFat);
77 void CCheckFatTable::InitializeL()
79 // Initialize the check fat table
82 __PRINT(_L("CCheckFatTable::InitializeL"));
84 TInt fatSize=iOwner->FatSizeInBytes();
87 iCheckFat=(TUint8*)User::AllocL(fatSize);
89 iCheckFat=(TUint8*)User::ReAllocL(iCheckFat,fatSize);
90 Mem::FillZ(iCheckFat,fatSize);
91 iMaxFatIndex=iOwner->UsableClusters()+1;
92 if(iOwner->Is16BitFat())
94 __ASSERT_ALWAYS(iMaxFatIndex>0 && iMaxFatIndex<EOF_16Bit && !IsEof16Bit(iMaxFatIndex),User::Leave(KErrCorrupt));
98 __ASSERT_ALWAYS(iMaxFatIndex>0 && iMaxFatIndex<EOF_12Bit && !IsEof12Bit(iMaxFatIndex),User::Leave(KErrCorrupt));
100 WriteMediaDescriptor();
102 __PRINT2(_L("fatSize=%d,iCheckFat=0x%x"),fatSize,iCheckFat);
107 @return ETrue if found errors in FAT
109 TBool CCheckFatTable::FlushL()
111 // Flush iCheckFat to the media, comparing each sector to corresponding
112 // sector in all fats (cf.CFixedCache::FlushL)
115 TBool bErrFound = EFalse;
117 __PRINT(_L("CCheckFatTable::FlushL()"));
118 HBufC8* hBuf=HBufC8::New(KMaxBufferSize);
120 hBuf=HBufC8::NewL(KMaxBufferSize/4);
121 CleanupStack::PushL(hBuf);
123 TUint8* ptr=(TUint8*)hBuf->Ptr();
124 TInt maxSize=hBuf->Des().MaxSize();
126 TPtr8 fatBuffer(ptr,maxSize);
127 TInt fatSize=iOwner->FatSizeInBytes();
128 TInt remainder=fatSize;
129 TInt offset=iOwner->StartOfFatInBytes();
130 TUint8* dataPtr=iCheckFat;
131 TFatDriveInterface& drive = iOwner->DriveInterface();
132 TInt fatNumber=iOwner->NumberOfFats();
136 TInt s=Min(fatBuffer.MaxSize(),remainder);
137 TInt fatCount=fatNumber;
141 TInt fatOffset=offset+fatPos;
142 User::LeaveIfError(drive.ReadCritical(fatOffset,s,fatBuffer));
144 TInt offset2=fatOffset;
145 TUint8* dataPtr2=dataPtr;
149 TInt s2=Min(rem2,512);
150 TInt r=Mem::Compare(dataPtr2,s2,fatBuffer.Ptr()+bufOffset,s2);
154 TPtrC8 dataBuf(dataPtr2,s2);
155 User::LeaveIfError(drive.WriteCritical(offset2,dataBuf));
170 CleanupStack::PopAndDestroy();
175 void CCheckFatTable::WriteMediaDescriptor()
177 // Write media descriptor to first byte and 0xFF to
178 // remaining bytes of first two entries
181 __PRINT(_L("CCheckFatTable::WriteMediaDescriptor"));
182 iCheckFat[0]=KBootSectorMediaDescriptor;
185 if (iOwner->Is16BitFat())
189 TInt CCheckFatTable::PosInBytes(TInt aFatIndex) const
191 // Return number of bytes into the fat
195 if (iOwner->Is16BitFat())
196 fatPosInBytes=aFatIndex<<1;
198 // this is used since 8-bit access will be used for reading/writing
199 fatPosInBytes=(aFatIndex*3>>1);
200 return(fatPosInBytes);
204 TInt CCheckFatTable::PosInIndex(TInt aBytePos) const
206 // Return index given byte position in fat
209 if(iOwner->Is16BitFat())
212 return((aBytePos<<1)/3);
216 TInt CCheckFatTable::ReadL(TInt aFatIndex) const
218 // Read a value from the check fat
221 __ASSERT_ALWAYS((TUint32)aFatIndex >=KFatFirstSearchCluster && aFatIndex<=MaxFatIndex(),User::Leave(KErrCorrupt));
223 if(iOwner->Is16BitFat())
224 clusterVal=*(TUint16*)(iCheckFat+PosInBytes(aFatIndex));
227 TUint8* pCluster=iCheckFat+PosInBytes(aFatIndex);
228 clusterVal=pCluster[0]|(pCluster[1]<<8);
229 TBool oddCluster=(aFatIndex)&1;
238 void CCheckFatTable::WriteL(TInt aFatIndex,TInt aValue)
240 // Write a value to the check fat
243 if(iOwner->Is16BitFat())
244 __ASSERT_ALWAYS((TUint32)aFatIndex>=KFatFirstSearchCluster && aFatIndex<=MaxFatIndex() && aValue>=0 && aValue<=0xFFFF,User::Leave(KErrCorrupt));
246 __ASSERT_ALWAYS((TUint32)aFatIndex>=KFatFirstSearchCluster && aFatIndex<=MaxFatIndex() && aValue>=0 && aValue<=0xFFF,User::Leave(KErrCorrupt));
247 TUint8* p=(TUint8*)(iCheckFat+PosInBytes(aFatIndex));
248 if (iOwner->Is16BitFat())
250 *(TUint16*)p=(TUint16)aValue;
254 TBool odd=(aFatIndex)&1;
262 fatVal|=(TUint8)(aValue&0xFF);
264 p[1]=(TUint8)(aValue>>8);
268 p[0]=(TUint8)(aValue&0xFF);
271 fatVal|=(TUint8)(aValue>>8);
278 TBool CCheckFatTable::GetNextClusterL(TInt& aCluster) const
280 // Get the next cluster in the chain from the check fat.
283 __PRINT(_L("CCheckFatTable::GetNextClusterL"));
285 TInt nextCluster=ReadL(aCluster);
286 if (iOwner->Is16BitFat())
287 ret=!IsEof16Bit(nextCluster);
289 ret=!IsEof12Bit(nextCluster);
291 aCluster=nextCluster;
295 void CCheckFatTable::WriteFatEntryEofFL(TInt aCluster)
297 // Write EOF to aCluster
300 __PRINT(_L("CCheckFatTable::WriteFatEntryEofF"));
301 if (iOwner->Is16BitFat())
302 WriteL(aCluster,EOF_16Bit);
304 WriteL(aCluster,EOF_12Bit);