os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/cmassstoragemountcb.cpp
Update contrib.
1 // Copyright (c) 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 // CMassStorageMountCB implementation.
30 #include "cmassstoragefilesystem.h"
31 #include "drivemanager.h"
34 #include "cusbmassstoragecontroller.h"
35 #include "cmassstoragemountcb.h"
38 CMassStorageMountCB::CMassStorageMountCB(const TLunToDriveMap& aDriveMapping)
39 : iDriveMapping(aDriveMapping)
43 CMassStorageMountCB* CMassStorageMountCB::NewL(const TLunToDriveMap& aDriveMapping)
45 return new (ELeave) CMassStorageMountCB(aDriveMapping);
49 Checks that the drive number is supported.
51 @leave KErrNotReady The drive number is not supported.
53 TInt CMassStorageMountCB::CheckDriveNumberL()
55 __FNLOG("CMassStorageMountCB::CheckDriveNumberL");
57 driveNumber = Drive().DriveNumber();
58 if (!IsValidLocalDriveMapping(driveNumber))
60 __PRINT1(_L("CMassStorageMountCB::CheckDriveNumberL: Drive number %d not supported"), driveNumber);
61 User::Leave(KErrNotReady);
63 __PRINT1(_L("CMassStorageMountCB::CheckDriveNumberL: Drive number = %d"), driveNumber);
68 Registers the drive with the Mass Storage drive manager.
70 @leave KErrNotSupported The drive is not compatible with Mass Storage.
72 void CMassStorageMountCB::MountL(TBool /*aForceMount*/)
74 __FNLOG("CMassStorageMountCB::MountL");
77 CMassStorageFileSystem& msFsys = *reinterpret_cast<CMassStorageFileSystem*>(Drive().GetFSys());
79 TInt lun = DriveNumberToLun(Drive().DriveNumber());
83 // This is not a supported Mass Storage drive
84 User::Leave(KErrNotSupported);
87 TBusLocalDrive& localDrive = msFsys.iMediaChangedStatusList[lun].iLocalDrive;
89 TInt err = CreateLocalDrive(localDrive);
90 User::LeaveIfError(err);
92 CProxyDrive* proxyDrive = LocalDrive();
94 TLocalDriveCapsV2Buf caps;
95 err = localDrive.Caps(caps);
97 //Make sure file system is FAT and removable
100 err = KErrNotSupported;
101 if ((caps().iDriveAtt & KDriveAttRemovable) == KDriveAttRemovable)
103 if (caps().iType != EMediaNotPresent)
110 if (err != KErrNone && err != KErrNotReady)
112 __PRINT1(_L("CMassStorageMountCB::MountL: Drive is not compatible with Mass Storage, err=%d"), err);
116 __PRINT(_L("CMassStorageMountCB::MountL: Registering drive"));
117 // Set media changed to true so that Win2K doesn't used cached drive data
118 TBool& mediaChanged = msFsys.iMediaChangedStatusList[lun].iMediaChanged;
119 mediaChanged = ETrue;
120 msFsys.Controller().DriveManager().RegisterDriveL(*proxyDrive, mediaChanged, lun);
121 SetVolumeName(_L("MassStorage").AllocL());
125 Returns the LUN that corresponds to the specified drive number.
127 @param aDriveNumber The drive number.
129 TInt CMassStorageMountCB::DriveNumberToLun(TInt aDriveNumber)
131 __FNLOG("CMassStorageMountCB::DriveNumberToLun");
133 for (TInt i = 0; i < iDriveMapping.Count(); i++)
135 if (iDriveMapping[i] == aDriveNumber)
141 __PRINT2(_L("CMassStorageMountCB::DriveNumberToLun: Drive %d maps to LUN %d"), aDriveNumber, lun);
146 Deregisters the drive from the Drive Manager.
148 void CMassStorageMountCB::Dismounted()
150 __FNLOG("CMassStorageMountCB::Dismounted");
151 TInt driveNumber = -1;
152 TRAPD(err, driveNumber = CheckDriveNumberL());
157 __PRINT(_L("CMassStorageMountCB::Dismounted: Deregistering drive"));
158 CMassStorageFileSystem& msFsys = *reinterpret_cast<CMassStorageFileSystem*>(Drive().GetFSys());
159 msFsys.Controller().DriveManager().DeregisterDrive(DriveNumberToLun(driveNumber));
161 DismountedLocalDrive();
165 Unlocks the drive with the specified password, optionally storing the password for later use.
167 @param aPassword The password to use for unlocking the drive.
168 @param aStore True if the password is to be stored.
170 TInt CMassStorageMountCB::Unlock(TMediaPassword& aPassword, TBool aStore)
172 __FNLOG("CMassStorageMountCB::Unlock");
173 TInt driveNumber = -1;
174 TRAPD(err, driveNumber = CheckDriveNumberL());
179 TBusLocalDrive& localDrive=GetLocalDrive(driveNumber);
180 if(localDrive.Status() == KErrLocked)
182 localDrive.Status() = KErrNotReady;
184 TInt r = localDrive.Unlock(aPassword, aStore);
185 if(r == KErrNone && aStore)
193 Stores the password for the drive to the password file.
195 void CMassStorageMountCB::WritePasswordData()
197 __FNLOG("CMassStorageMountCB::WritePasswordData");
198 TBusLocalDrive& local=GetLocalDrive(Drive().DriveNumber());
199 TInt length = local.PasswordStoreLengthInBytes();
202 TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
203 mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
204 WriteToDisk(mediaPWrdFile,_L8(""));
207 HBufC8* hDes=HBufC8::New(length);
212 TPtr8 pDes=hDes->Des();
213 TInt r=local.ReadPasswordData(pDes);
216 TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
217 mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
218 WriteToDisk(mediaPWrdFile,pDes);
224 Make sure that the file system is fat.
226 TBool CMassStorageMountCB::ValidateBootSector()
228 __FNLOG("CMassStorageMountCB::ValidateBootSector");
230 TFatBootSector bootSector;
231 TInt r=ReadBootSector(bootSector);
232 __PRINT1(_L("CMassStorageMountCB::MountL - ReadBootSector returned %d"),r);
238 __PRINT(_L("\nBootSector info"));
239 __PRINT8BIT1(_L("FAT type = %S"),bootSector.FileSysType());
240 __PRINT8BIT1(_L("Vendor ID = %S"),bootSector.VendorId());
241 __PRINT1(_L("BytesPerSector %d"),bootSector.BytesPerSector());
242 __PRINT1(_L("SectorsPerCluster %d"),bootSector.SectorsPerCluster());
243 __PRINT1(_L("ReservedSectors %d"),bootSector.ReservedSectors());
244 __PRINT1(_L("NumberOfFats %d"),bootSector.NumberOfFats());
245 __PRINT1(_L("RootDirEntries %d"),bootSector.RootDirEntries());
246 __PRINT1(_L("Total Sectors = %d"),bootSector.TotalSectors());
247 __PRINT1(_L("MediaDescriptor = 0x%x"),bootSector.MediaDescriptor());
248 __PRINT1(_L("FatSectors %d"),bootSector.FatSectors());
249 __PRINT1(_L("SectorsPerTrack %d"),bootSector.SectorsPerTrack());
250 __PRINT1(_L("NumberOfHeads %d"),bootSector.NumberOfHeads());
251 __PRINT1(_L("HugeSectors %d"),bootSector.HugeSectors());
252 __PRINT1(_L("Fat32 Sectors %d"),bootSector.FatSectors32());
253 __PRINT1(_L("Fat32 Flags %d"),bootSector.FATFlags());
254 __PRINT1(_L("Fat32 Version Number %d"),bootSector.VersionNumber());
255 __PRINT1(_L("Root Cluster Number %d"),bootSector.RootClusterNum());
256 __PRINT1(_L("FSInfo Sector Number %d"),bootSector.FSInfoSectorNum());
257 __PRINT1(_L("Backup Boot Rec Sector Number %d"),bootSector.BkBootRecSector());
258 __PRINT1(_L("PhysicalDriveNumber %d"),bootSector.PhysicalDriveNumber());
259 __PRINT1(_L("ExtendedBootSignature %d"),bootSector.ExtendedBootSignature());
260 __PRINT1(_L("UniqueID %d"),bootSector.UniqueID());
261 __PRINT8BIT1(_L("VolumeLabel %S"),bootSector.VolumeLabel());
262 __PRINT8BIT1(_L("FileSysType %S\n"),bootSector.FileSysType());
264 iUniqueID=bootSector.UniqueID();
265 iIs16BitFat=bootSector.Is16BitFat();
267 iIs32BitFat=bootSector.Is32BitFat();
268 switch (DetermineFatType(bootSector))
271 iIs16BitFat = EFalse;
272 iIs32BitFat = EFalse;
276 iIs32BitFat = EFalse;
279 iIs16BitFat = EFalse;
286 TInt sectorsPerCluster=bootSector.SectorsPerCluster();
287 if (!IsPowerOfTwo(sectorsPerCluster))
290 TInt sectorSizeLog2=Log2(bootSector.BytesPerSector());
291 if (sectorSizeLog2<0 || !IsPowerOfTwo(bootSector.BytesPerSector()))
294 TInt firstFatSector=bootSector.ReservedSectors();
295 if (firstFatSector<1)
301 fatSizeInBytes=bootSector.FatSectors32()*bootSector.BytesPerSector();
302 if (fatSizeInBytes<bootSector.BytesPerSector())
307 fatSizeInBytes=bootSector.FatSectors()*bootSector.BytesPerSector();
308 if (fatSizeInBytes<bootSector.BytesPerSector())
311 TInt rootDirectorySector=firstFatSector+bootSector.FatSectors()*bootSector.NumberOfFats();
312 if (rootDirectorySector<3)
315 TInt rootDirSizeInBytes=bootSector.RootDirEntries()*KSizeOfFatDirEntry;
316 TInt numOfRootDirSectors=(rootDirSizeInBytes+(1<<sectorSizeLog2)-1)>>sectorSizeLog2;
317 TInt rootDirEnd=(rootDirectorySector+numOfRootDirSectors)<<sectorSizeLog2;
318 if (rootDirEnd<(4<<sectorSizeLog2))
323 TInt totalSectors=bootSector.TotalSectors();
325 totalSectors=bootSector.HugeSectors();
329 TInt numberOfFats=bootSector.NumberOfFats();
337 Read non aligned boot data from media into TFatBootSector structure
339 @param aBootSector refrence to TFatBootSector populate
340 @return Media read error code
342 TInt CMassStorageMountCB::ReadBootSector(TFatBootSector& aBootSector)
344 __FNLOG("CMassStorageMountCB::ReadBootSector");
346 TUint8 data[KSizeOfFatBootSector];
347 TPtr8 buf(&data[0],KSizeOfFatBootSector);
348 TInt r=LocalDrive()->Read(0,KSizeOfFatBootSector,buf);
351 __PRINT1(_L("LocalDrive::Read() failed - %d"),r);
354 // 0 TUint8 iJumpInstruction[3]
355 Mem::Copy(&aBootSector.iJumpInstruction,&data[pos],3);
357 // 3 TUint8 iVendorId[KVendorIdSize]
358 Mem::Copy(&aBootSector.iVendorId,&data[pos],KVendorIdSize);
360 // 11 TUint16 iBytesPerSector
361 Mem::Copy(&aBootSector.iBytesPerSector,&data[pos],2);
363 // 13 TUint8 sectorsPerCluster
364 Mem::Copy(&aBootSector.iSectorsPerCluster,&data[pos],1);
366 // 14 TUint16 iReservedSectors
367 Mem::Copy(&aBootSector.iReservedSectors,&data[pos],2);
369 // 16 TUint8 numberOfFats
370 Mem::Copy(&aBootSector.iNumberOfFats,&data[pos],1);
372 // 17 TUint16 iRootDirEntries
373 Mem::Copy(&aBootSector.iRootDirEntries,&data[pos],2);
375 // 19 TUint16 totalSectors
376 Mem::Copy(&aBootSector.iTotalSectors,&data[pos],2);
378 // 21 TUint8 iMediaDescriptor
379 Mem::Copy(&aBootSector.iMediaDescriptor,&data[pos],1);
381 // 22 TUint16 iFatSectors
382 Mem::Copy(&aBootSector.iFatSectors,&data[pos],2);
384 // 24 TUint16 iSectorsPerTrack
385 Mem::Copy(&aBootSector.iSectorsPerTrack,&data[pos],2);
387 // 26 TUint16 iNumberOfHeads
388 Mem::Copy(&aBootSector.iNumberOfHeads,&data[pos],2);
390 // 28 TUint32 iHiddenSectors
391 Mem::Copy(&aBootSector.iHiddenSectors,&data[pos],4);
393 // 32 TUint32 iHugeSectors
394 Mem::Copy(&aBootSector.iHugeSectors,&data[pos],4);
397 if(aBootSector.iRootDirEntries == 0) //indicates we have FAT32 volume
399 __PRINT(_L("\nFile system thinks Fat32"));
401 //36 TUint32 iFatSectors32
402 Mem::Copy(&aBootSector.iFatSectors32, &data[pos],4);
404 //40 TUint16 iFATFlags
405 Mem::Copy(&aBootSector.iFATFlags, &data[pos],2);
407 //42 TUint16 iVersionNumber
408 Mem::Copy(&aBootSector.iVersionNumber, &data[pos],2);
410 //44 TUint32 iRootClusterNum
411 Mem::Copy(&aBootSector.iRootClusterNum, &data[pos],4);
413 //48 TUint16 iFSInfoSectorNum
414 Mem::Copy(&aBootSector.iFSInfoSectorNum, &data[pos],2);
416 //50 TUint16 iBkBootRecSector
417 Mem::Copy(&aBootSector.iBkBootRecSector, &data[pos],2);
418 pos+=(2+12);//extra 12 for the reserved bytes
421 // 36|64 TUint8 iPhysicalDriveNumber
422 Mem::Copy(&aBootSector.iPhysicalDriveNumber,&data[pos],1);
424 // 37|65 TUint8 iReserved
425 Mem::Copy(&aBootSector.iReserved,&data[pos],1);
427 // 38|66 TUint8 iExtendedBootSignature
428 Mem::Copy(&aBootSector.iExtendedBootSignature,&data[pos],1);
430 // 39|67 TUint32 iUniqueID
431 Mem::Copy(&aBootSector.iUniqueID,&data[pos],4);
433 // 43|71 TUint8 iVolumeLabel[KVolumeLabelSize]
434 Mem::Copy(&aBootSector.iVolumeLabel,&data[pos],KVolumeLabelSize);
435 pos+=KVolumeLabelSize;
436 // 54|82 TUint8 iFileSysType[KFileSysTypeSize]
437 Mem::Copy(&aBootSector.iFileSysType,&data[pos],KFileSysTypeSize);
444 Work out if we have a FAT12|16|32 volume.
445 Returns 12, 16 or 32 as appropriate.
446 Returns 0 if can't be calculated (invalid values)
448 TInt CMassStorageMountCB::DetermineFatType(TFatBootSector& aBootSector)
450 TUint32 ressectors = aBootSector.ReservedSectors();
452 if (aBootSector.SectorsPerCluster() < 1)
455 if (aBootSector.RootDirEntries() != 0)
457 TUint32 rootdirbytes;
458 rootdirbytes = aBootSector.RootDirEntries() * 32 + aBootSector.BytesPerSector() - 1;
459 ressectors += rootdirbytes / aBootSector.BytesPerSector();
462 if (aBootSector.FatSectors() != 0)
463 ressectors += aBootSector.NumberOfFats() * aBootSector.FatSectors();
465 ressectors += aBootSector.NumberOfFats() * aBootSector.FatSectors32();
467 TUint32 totalsectors;
468 if (aBootSector.TotalSectors() != 0)
469 totalsectors = aBootSector.TotalSectors();
471 totalsectors = aBootSector.HugeSectors();
473 if (ressectors < 1 || totalsectors < 1)
477 datasec = totalsectors - ressectors;
479 TUint32 countofclusters;
480 countofclusters = datasec / aBootSector.SectorsPerCluster();
482 __PRINT1(_L("CFatMountCB: Count of clusters = %d\n"), countofclusters);
484 if (countofclusters < 4085)
488 else if (countofclusters < 65525)
498 TInt CMassStorageMountCB::ReMount()
500 RDebug::Printf("CMassStorageMountCB::ReMount()");
504 void CMassStorageMountCB::VolumeL(TVolumeInfo& /*aVolume*/) const
506 User::Leave(KErrNotReady);
509 void CMassStorageMountCB::SetVolumeL(TDes& /*aName*/)
511 User::Leave(KErrNotReady);
514 void CMassStorageMountCB::MkDirL(const TDesC& /*aName*/)
516 User::Leave(KErrNotReady);
519 void CMassStorageMountCB::RmDirL(const TDesC& /*aName*/)
521 User::Leave(KErrNotReady);
524 void CMassStorageMountCB::DeleteL(const TDesC& /*aName*/)
526 User::Leave(KErrNotReady);
529 void CMassStorageMountCB::RenameL(const TDesC& /*anOldName*/,const TDesC& /*anNewName*/)
531 User::Leave(KErrNotReady);
534 void CMassStorageMountCB::ReplaceL(const TDesC& /*anOldName*/,const TDesC& /*anNewName*/)
536 User::Leave(KErrNotReady);
539 void CMassStorageMountCB::EntryL(const TDesC& /*aName*/,TEntry& /*anEntry*/) const
541 User::Leave(KErrNotReady);
544 void CMassStorageMountCB::SetEntryL(const TDesC& /*aName*/,const TTime& /*aTime*/,TUint /*aSetAttMask*/,TUint /*aClearAttMask*/)
546 User::Leave(KErrNotReady);
549 void CMassStorageMountCB::FileOpenL(const TDesC& /*aName*/,TUint /*aMode*/,TFileOpen /*anOpen*/,CFileCB* /*aFile*/)
551 User::Leave(KErrNotReady);
554 void CMassStorageMountCB::DirOpenL(const TDesC& /*aName*/,CDirCB* /*aDir*/)
556 User::Leave(KErrNotReady);
560 void CMassStorageMountCB::RawReadL(TInt64 /*aPos*/,TInt /*aLength*/,const TAny* /*aTrg*/,TInt /*anOffset*/,const RMessagePtr2& /*aMessage*/) const
562 User::Leave(KErrNotReady);
565 void CMassStorageMountCB::RawWriteL(TInt64 /*aPos*/,TInt /*aLength*/,const TAny* /*aSrc*/,TInt /*anOffset*/,const RMessagePtr2& /*aMessage*/)
567 User::Leave(KErrNotReady);
571 void CMassStorageMountCB::GetShortNameL(const TDesC& /*aLongName*/,TDes& /*aShortName*/)
573 User::Leave(KErrNotReady);
576 void CMassStorageMountCB::GetLongNameL(const TDesC& /*aShorName*/,TDes& /*aLongName*/)
578 User::Leave(KErrNotReady);
582 TInt CMassStorageMountCB::ControlIO(const RMessagePtr2& aMessage,TInt aCommand,TAny* aParam1,TAny* aParam2)
587 if(aCommand>=(KMaxTInt/2))
588 return LocalDrive()->ControlIO(aMessage,aCommand-(KMaxTInt/2),aParam1,aParam2);
590 return KErrNotSupported;
593 TInt CMassStorageMountCB::ControlIO(const RMessagePtr2& /*aMessage*/,TInt /*aCommand*/,TAny* /*aParam1*/,TAny* /*aParam2*/)
594 {return(KErrNotSupported);}
597 void CMassStorageMountCB::ReadSectionL(const TDesC& /*aName*/,TInt /*aPos*/,TAny* /*aTrg*/,TInt /*aLength*/,const RMessagePtr2& /*aMessage*/)
599 User::Leave(KErrNotReady);
603 Returns ETrue if aNum is a power of two
605 TBool CMassStorageMountCB::IsPowerOfTwo(TInt aNum)
625 Returns the position of the highest bit in aNum or -1 if aNum == 0
627 TInt CMassStorageMountCB::Log2(TInt aNum)