Update contrib.
1 // Copyright (c) 1996-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_drv.cpp
18 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 //!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
23 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
30 const TInt KMaxRecoverableRetries=10;
31 const TInt KMaxCriticalRetries=10;
34 //---------------------------------------------------------------------------------------------------------------------------------------
37 TFatDriveInterface::TFatDriveInterface()
43 Initialise the interface object.
44 @param aMount the CFatMountCB that owns this object
46 TBool TFatDriveInterface::Init(CFatMountCB* aMount)
50 aMount->LocalDrive()->SetMount(aMount);
51 return iProxyDrive.Init(aMount->LocalDrive());
57 void TFatDriveInterface::Close()
60 iMount->LocalDrive()->SetMount(NULL);
64 //---------------------------------------------------------------------------------------------------------------------------------------
67 Read data from the media via CProxyDrive interface.
68 This is non-critical read: on error Non-critical notifier is involved
70 @param aPos absolute media position
71 @param aLength how many bytes to read
72 @param aTrg data descriptor
74 @return KErrNone - success
75 @return KErrNotReady - non-critical error
76 @return KErrCorrupt - an illegal write is detected
77 @return KErrBadPower - failure due to low power
80 TInt TFatDriveInterface::ReadNonCritical(TInt64 aPos, TInt aLength, TDes8& aTrg) const
83 TInt cntRetry = KMaxRecoverableRetries;
85 //__PRINT2(_L("#=+++ Read_nc1: pos:%LU, len:%u"), aPos, aLength);
89 nRes = iProxyDrive.Read(aPos,aLength,aTrg);
93 __PRINT4(_L("TFatDriveInterface::ReadNonCritical() failure! drv:%d Posl=%LU len=%d retval=%d"), iMount->DriveNumber(), aPos, aLength, nRes);
101 nRes = HandleRecoverableError(nRes);
109 //---------------------------------------------------------------------------------------------------------------------------------------
112 Read data from the media via CProxyDrive interface.
113 This is non-critical read: on error Non-critical notifier is involved
115 @param aPos absolute media position
116 @param aLength how many bytes to read
117 @param aTrg data descriptor
121 @return KErrNone - success
122 @return KErrNotReady - non-critical error
123 @return KErrCorrupt - an illegal write is detected
124 @return KErrBadPower - failure due to low power
127 TInt TFatDriveInterface::ReadNonCritical(TInt64 aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2 &aMessage,TInt anOffset) const
129 //__PRINT2(_L("#=+++ Read_nc2: pos:%LU, len:%u"), aPos, aLength);
131 TInt nRes = KErrNone;
132 TInt cntRetry = KMaxRecoverableRetries;
136 nRes = iProxyDrive.Read(aPos, aLength, aTrg, aMessage, anOffset);
140 __PRINT4(_L("TFatDriveInterface::ReadNonCritical() Failure! drv:%d aPosl=%d len=%d anOffset=%d"), iMount->DriveNumber(), aPos,aLength, anOffset);
148 nRes = HandleRecoverableError(nRes);
156 //---------------------------------------------------------------------------------------------------------------------------------------
159 Read data from the media via CProxyDrive interface with a critical notifier.
160 This method shall be used to read critical filesystem data, such as directory entries, FAT data.
162 @param aPos absolute media position
163 @param aLength how many bytes to read
164 @param aTrg data descriptor
166 @return KErrNone - success
167 @return KErrNotReady - non-critical error
168 @return KErrCorrupt - an illegal write is detected
169 @return KErrAbort - user aborted read
171 TInt TFatDriveInterface::ReadCritical(TInt64 aPos,TInt aLength,TDes8& aTrg) const
173 //__PRINT2(_L("#=+++ Read_C: pos:%LU, len:%u"), aPos, aLength);
175 TInt nRes = KErrNone;
179 nRes = iProxyDrive.Read(aPos, aLength, aTrg);
183 __PRINT4(_L("TFatDriveInterface::ReadCritical() Error! drv:%d Posl=%LU len=%d retval=%d"), iMount->DriveNumber(), aPos, aLength, nRes);
185 nRes=HandleCriticalError(nRes);
193 //---------------------------------------------------------------------------------------------------------------------------------------
196 Write data to the media via CProxyDrive interface.
198 @param aPos absolute media position
199 @param aLength how many bytes to write
200 @param aSrc pointer to the data
204 @return KErrNone - success
205 @return KErrNotReady - non-critical error
206 @return KErrBadPower - write not attempted due to low batteries
207 @return KErrCorrupt - an illegal write is detected
208 @return KErrAccessDenied - write to protected media
210 TInt TFatDriveInterface::WriteNonCritical(TInt64 aPos, TInt aLength, const TAny* aSrc, const RMessagePtr2 &aMessage, TInt anOffset)
212 //__PRINT2(_L("#=+++ Write_NC: pos:%LU, len:%u"), aPos, aLength);
215 TInt nRes = KErrNone;
216 TInt cntRetry = KMaxRecoverableRetries;
220 iMount->OpenMountForWrite(); //-- make a callback to CFatMountCB to perform some actions on 1st write.
221 nRes = iProxyDrive.Write(aPos, aLength, aSrc, aMessage, anOffset);
225 __PRINT4(_L("TFatDriveInterface::WriteNonCritical() failure! drv:%d, Pos=%LU len=%d anOffset=%d"), iMount->DriveNumber(), aPos, aLength, anOffset);
233 nRes = HandleRecoverableError(nRes);
242 //---------------------------------------------------------------------------------------------------------------------------------------
245 Write data to the media via CProxyDrive interface. On error this method can invoke a critical notifier.
246 This method is intended to be called for the filesstem critical data, i.e. FAT metadata, such as directory entries,
249 @param aPos absolute media position
250 @param aSrc descriptor with the source data
252 @return KErrNone - success
253 @return KErrNotReady - non-critical error
254 @return KErrBadPower - write not attempted due to low batteries
255 @return KErrCorrupt - an illegal write is detected
256 @return KErrAccessDenied - write to protected media
258 TInt TFatDriveInterface::WriteCritical(TInt64 aPos, const TDesC8& aSrc)
260 //__PRINT2(_L("#=+++ Write_C: pos:%LU, len:%u"), aPos, aSrc.Length());
262 TInt nRes = KErrNone;
266 TBool simulatedWriteFailure = EFalse; //-- if true it means that the write failure has been simulated
268 //-- debug interface to simulate write failure
269 if(iMount->IsWriteFail())
271 if(iMount->WriteFailCount() != 0)
273 iMount->DecWriteFailCount();
276 {//-- simulate write failure
277 if(iMount->WriteFailError()==-99)
278 UserSvr::ResetMachine(EStartupWarmReset);
281 //-- invalidate caches, because actual write to the drive isn't going to happen
282 if(iMount->RawDisk().DirCacheInterface())
283 iMount->RawDisk().DirCacheInterface()->InvalidateCache();
285 iMount->SetWriteFail(EFalse);
287 TRAP_IGNORE(iMount->RawDisk().InvalidateUidCache()); //-- invalidate whole UID data cache
288 TRAP_IGNORE(iMount->FAT().InvalidateCacheL()); //-- invalidate whole FAT cache
290 iMount->InvalidateLeafDirCache();
292 nRes = iMount->WriteFailError();
293 simulatedWriteFailure = ETrue; //-- won't perform actual write later
294 __PRINT4(_L("TFatDriveInterface::WriteCritical() Simulating write failure. drv:%d, aPos=%LU len=%d Code=%d"), iMount->DriveNumber(), aPos,aSrc.Length(),nRes);
298 }//if(iMount->IsWriteFail())
300 if(!simulatedWriteFailure)
303 //-- try to write data until success or user gives up
306 for(TInt i=0; i<KMaxCriticalRetries; i++)
308 iMount->OpenMountForWrite(); //-- make a callback to CFatMountCB to perform some actions on 1st write.
309 nRes=iProxyDrive.Write(aPos,aSrc);
314 //-- write error occured
315 __PRINT4(_L("TFatDriveInterface::WriteCritical() failure! drv:%d, aPos=%LU len=%d retval=%d"), iMount->DriveNumber(), aPos,aSrc.Length(),nRes);
317 nRes=HandleCriticalError(nRes);
323 }// if(!simulatedWriteFailure)
330 Get Last Error Info from the proxy drive
332 @param aErrorInfo data descriptor for the error info.
333 @return KErrNone - success, interrogate aErrorInfo for further info
334 @return KErrNotSupported - media driver does not support
336 TInt TFatDriveInterface::GetLastErrorInfo(TDes8& aErrorInfo) const
338 return iProxyDrive.GetLastErrorInfo(aErrorInfo);
342 //---------------------------------------------------------------------------------------------------------------------------------------
345 Handle critical error
347 @param aResult result from the media driver (error code)
349 @return ERetry - Attempt operation again
350 @return KErrAbort - User aborted notifier
351 @return KErrAccessDenied - media is read only
352 @return KErrCorrupt - cf-card is corrupt
354 TInt TFatDriveInterface::HandleCriticalError(TInt aResult) const
356 __PRINT2(_L("TFatDriveInterface::HandleCriticalError drv:%d, code:%d"), iMount->DriveNumber(),aResult);
358 TLocaleMessage line1;
359 TLocaleMessage line2;
363 if (aResult==KErrLocked)
369 if (aResult==KErrAccessDenied)
375 if (aResult==KErrArgument || aResult==KErrBadDescriptor)
381 if (iMount->Drive().IsChanged())
382 {//-- check if the media we accessing is the same as it used to be
383 if(iMount->CheckVolumeTheSame())
384 {//-- the media is the same
385 if(!IsDriveWriteProtected())
387 iMount->Drive().SetChanged(EFalse);
394 if (aResult==KErrAbort && !iMount->Drive().IsChanged())
400 if (aResult==KErrBadPower)
402 line1=EFileServer_LowPowerLine1;
403 line2=EFileServer_LowPowerLine2;
405 else if (iMount->Drive().IsChanged())
407 line1=EFileServer_PutTheCardBackLine1;
408 line2=EFileServer_PutTheCardBackLine2;
412 line1=EFileServer_DiskErrorLine1;
413 line2=EFileServer_DiskErrorLine2;
421 TInt ret=iMount->Notifier()->Notify(TLocaleMessageText(line1),
422 TLocaleMessageText(line2),
423 TLocaleMessageText(EFileServer_Button1),
424 TLocaleMessageText(EFileServer_Button2),
431 if (iMount->Drive().IsChanged())
434 // Without this code, retry will indiscriminately write over whatever disk happens to be present.
435 // However if the write error is to the bootsector remounting will always fail because the boot
436 // sector will have changed and hence the disk is useless.
438 if(!iMount->CheckVolumeTheSame())
439 continue; //-- the media isn't the same as originally mounted; continue asking
441 if(IsDriveWriteProtected())
442 continue; //-- still can not write to the drive
445 iMount->Drive().SetChanged(EFalse);
456 //---------------------------------------------------------------------------------------------------------------------------------------
459 Handle recoverable error
461 @param aResult result from the media driver (error code)
463 @return ERetry - retry write
464 @return KErrCorrupt - media is corrupt
465 @return KErrBadPower - low power failure
466 @return KErrNotReady - non-critical error
468 TInt TFatDriveInterface::HandleRecoverableError(TInt aResult) const
470 __PRINT2(_L("TFatDriveInterface::HandleRecoverableError drv:%d, code:%d"), iMount->DriveNumber(),aResult);
472 if (aResult==KErrAccessDenied)
473 return(KErrAccessDenied);
474 if (aResult == KErrLocked)
476 if (aResult==KErrArgument || aResult==KErrBadDescriptor)
478 if (aResult==KErrBadPower)
479 return(KErrBadPower);
480 if (aResult==KErrDied) // client thread died
482 if (iMount->Drive().IsChanged())
485 if(! iMount->CheckVolumeTheSame())
486 {//-- the media is different now.
489 else if(!IsRecoverableRemount())
491 return KErrAccessDenied;
497 /** @return true if the mount can be remounted for a recoverable error */
498 TBool TFatDriveInterface::IsRecoverableRemount() const
500 if(IsDriveWriteProtected()&&(iMount->Drive().IsWriteableResource()||iMount->Drive().IsCurrentWriteFunction()))
505 /** return true if the media is write protected */
506 TBool TFatDriveInterface::IsDriveWriteProtected() const
508 TLocalDriveCapsV2Buf localDriveCaps;
509 TInt r=iProxyDrive.Caps(localDriveCaps);
514 return((localDriveCaps().iMediaAtt&KMediaAttWriteProtected)!=0);
519 //---------------------------------------------------------------------------------------------------------------------------------------
521 TFatDriveInterface::XProxyDriveWrapper::XProxyDriveWrapper()
524 TInt nRes = iLock.CreateLocal();
525 ASSERT(nRes == KErrNone);
530 TFatDriveInterface::XProxyDriveWrapper::~XProxyDriveWrapper()
536 Initialise interface wrapper.
537 @param aProxyDrive pointer to the raw drive access interface
538 @return true on success
540 TBool TFatDriveInterface::XProxyDriveWrapper::Init(CProxyDrive* aProxyDrive)
543 if(!iLock.Handle()) //-- the mutex must have been created by constructor
546 iLocalDrive = aProxyDrive;
550 //-- see original TFatDriveInterface methods
552 TInt TFatDriveInterface::XProxyDriveWrapper::Read(TInt64 aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2 &aMessage,TInt anOffset) const
554 EnterCriticalSection();
555 TInt nRes = iLocalDrive->Read(aPos, aLength, aTrg, aMessage.Handle(), anOffset);
556 LeaveCriticalSection();
560 TInt TFatDriveInterface::XProxyDriveWrapper::Read(TInt64 aPos,TInt aLength,TDes8& aTrg) const
562 EnterCriticalSection();
563 TInt nRes = iLocalDrive->Read(aPos, aLength, aTrg);
564 LeaveCriticalSection();
568 TInt TFatDriveInterface::XProxyDriveWrapper::Write(TInt64 aPos,TInt aLength,const TAny* aSrc,const RMessagePtr2 &aMessage,TInt anOffset)
570 EnterCriticalSection();
571 TInt nRes = iLocalDrive->Write(aPos, aLength, aSrc, aMessage.Handle(), anOffset);
572 LeaveCriticalSection();
576 TInt TFatDriveInterface::XProxyDriveWrapper::Write(TInt64 aPos, const TDesC8& aSrc)
578 EnterCriticalSection();
579 TInt nRes = iLocalDrive->Write(aPos, aSrc);
580 LeaveCriticalSection();
584 TInt TFatDriveInterface::XProxyDriveWrapper::GetLastErrorInfo(TDes8& aErrorInfo) const
586 EnterCriticalSection();
587 TInt nRes = iLocalDrive->GetLastErrorInfo(aErrorInfo);
588 LeaveCriticalSection();
592 TInt TFatDriveInterface::XProxyDriveWrapper::Caps(TDes8& anInfo) const
594 EnterCriticalSection();
595 TInt nRes = iLocalDrive->Caps(anInfo);
596 LeaveCriticalSection();