os/kernelhwsrv/userlibandfileserver/fileserver/smassstorage/cmassstoragemountcb.cpp
Update contrib.
1 // Copyright (c) 2004-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.
25 #include "cmassstoragemountcb.h"
26 #include "cmassstoragefilesystem.h"
27 #include "drivemanager.h"
28 #include "massstoragedebug.h"
29 #include "massstorageutil.h"
31 CMassStorageMountCB::CMassStorageMountCB(const RArray<TInt>& aDriveMapping)
32 : iDriveMapping(aDriveMapping)
36 CMassStorageMountCB* CMassStorageMountCB::NewL(const RArray<TInt>& aDriveMapping)
38 return new (ELeave) CMassStorageMountCB(aDriveMapping);
42 Checks that the drive number is supported.
44 @leave KErrNotReady The drive number is not supported.
46 TInt CMassStorageMountCB::CheckDriveNumberL()
48 __FNLOG("CMassStorageMountCB::CheckDriveNumberL");
50 driveNumber = Drive().DriveNumber();
51 if (!IsValidLocalDriveMapping(driveNumber))
53 __PRINT1(_L("CMassStorageMountCB::CheckDriveNumberL: Drive number %d not supported"), driveNumber);
54 User::Leave(KErrNotReady);
56 __PRINT1(_L("CMassStorageMountCB::CheckDriveNumberL: Drive number = %d"), driveNumber);
61 Registers the drive with the Mass Storage drive manager.
63 @leave KErrNotSupported The drive is not compatible with Mass Storage.
65 void CMassStorageMountCB::MountL(TBool /*aForceMount*/)
67 __FNLOG("CMassStorageMountCB::MountL");
70 CMassStorageFileSystem& msFsys = *reinterpret_cast<CMassStorageFileSystem*>(Drive().GetFSys());
72 TInt lun = DriveNumberToLun(Drive().DriveNumber());
76 // This is not a supported Mass Storage drive
77 User::Leave(KErrNotSupported);
80 TBusLocalDrive& localDrive = msFsys.iLocalDriveForMediaFlag[lun];
82 TInt err = CreateLocalDrive(localDrive);
83 User::LeaveIfError(err);
85 CProxyDrive* proxyDrive = LocalDrive();
87 TLocalDriveCapsV2Buf caps;
88 err = localDrive.Caps(caps);
89 //Make sure file system is FAT and removable
92 err = KErrNotSupported;
93 if ((caps().iDriveAtt & KDriveAttRemovable) == KDriveAttRemovable)
95 if (caps().iType != EMediaNotPresent)
102 if (err != KErrNone && err != KErrNotReady)
104 __PRINT1(_L("CMassStorageMountCB::MountL: Drive is not compatible with Mass Storage, err=%d"), err);
108 __PRINT(_L("CMassStorageMountCB::MountL: Registering drive"));
110 // Set media changed to true so that Win2K doesn't used cached drive data
111 (*msFsys.iMediaChanged)[lun] = ETrue;
113 msFsys.Controller().DriveManager().RegisterDrive(*proxyDrive, (*msFsys.iMediaChanged)[lun], lun);
115 SetVolumeName(_L("MassStorage").AllocL());
119 Returns the LUN that corresponds to the specified drive number.
121 @param aDriveNumber The drive number.
123 TInt CMassStorageMountCB::DriveNumberToLun(TInt aDriveNumber)
125 __FNLOG("CMassStorageMountCB::DriveNumberToLun");
127 for (TInt i = 0; i < iDriveMapping.Count(); i++)
129 if (iDriveMapping[i] == aDriveNumber)
135 __PRINT2(_L("CMassStorageMountCB::DriveNumberToLun: Drive %d maps to LUN %d"), aDriveNumber, lun);
140 Deregisters the drive from the Drive Manager.
142 void CMassStorageMountCB::Dismounted()
144 __FNLOG("CMassStorageMountCB::Dismounted");
145 TInt driveNumber = -1;
146 TRAPD(err, driveNumber = CheckDriveNumberL());
151 __PRINT(_L("CMassStorageMountCB::Dismounted: Deregistering drive"));
152 CMassStorageFileSystem& msFsys = *reinterpret_cast<CMassStorageFileSystem*>(Drive().GetFSys());
153 msFsys.Controller().DriveManager().DeregisterDrive(DriveNumberToLun(driveNumber));
155 DismountedLocalDrive();
159 Unlocks the drive with the specified password, optionally storing the password for later use.
161 @param aPassword The password to use for unlocking the drive.
162 @param aStore True if the password is to be stored.
164 TInt CMassStorageMountCB::Unlock(TMediaPassword& aPassword, TBool aStore)
166 __FNLOG("CMassStorageMountCB::Unlock");
167 TInt driveNumber = -1;
168 TRAPD(err, driveNumber = CheckDriveNumberL());
173 TBusLocalDrive& localDrive=GetLocalDrive(driveNumber);
174 if(localDrive.Status() == KErrLocked)
176 localDrive.Status() = KErrNotReady;
178 TInt r = localDrive.Unlock(aPassword, aStore);
179 if(r == KErrNone && aStore)
187 Stores the password for the drive to the password file.
189 void CMassStorageMountCB::WritePasswordData()
191 __FNLOG("CMassStorageMountCB::WritePasswordData");
192 TBusLocalDrive& local=GetLocalDrive(Drive().DriveNumber());
193 TInt length = local.PasswordStoreLengthInBytes();
196 TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
197 mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
198 WriteToDisk(mediaPWrdFile,_L8(""));
201 HBufC8* hDes=HBufC8::New(length);
206 TPtr8 pDes=hDes->Des();
207 TInt r=local.ReadPasswordData(pDes);
210 TBuf<sizeof(KMediaPWrdFile)> mediaPWrdFile(KMediaPWrdFile);
211 mediaPWrdFile[0] = (TUint8) RFs::GetSystemDriveChar();
212 WriteToDisk(mediaPWrdFile,pDes);
218 Make sure that the file system is fat.
220 TBool CMassStorageMountCB::ValidateBootSector()
222 __FNLOG("CMassStorageMountCB::ValidateBootSector");
224 TFatBootSector bootSector;
225 TInt r=ReadBootSector(bootSector);
226 __PRINT1(_L("CMassStorageMountCB::MountL - ReadBootSector returned %d"),r);
232 __PRINT(_L("\nBootSector info"));
233 __PRINT8BIT1(_L("FAT type = %S"),bootSector.FileSysType());
234 __PRINT8BIT1(_L("Vendor ID = %S"),bootSector.VendorId());
235 __PRINT1(_L("BytesPerSector %d"),bootSector.BytesPerSector());
236 __PRINT1(_L("SectorsPerCluster %d"),bootSector.SectorsPerCluster());
237 __PRINT1(_L("ReservedSectors %d"),bootSector.ReservedSectors());
238 __PRINT1(_L("NumberOfFats %d"),bootSector.NumberOfFats());
239 __PRINT1(_L("RootDirEntries %d"),bootSector.RootDirEntries());
240 __PRINT1(_L("Total Sectors = %d"),bootSector.TotalSectors());
241 __PRINT1(_L("MediaDescriptor = 0x%x"),bootSector.MediaDescriptor());
242 __PRINT1(_L("FatSectors %d"),bootSector.FatSectors());
243 __PRINT1(_L("SectorsPerTrack %d"),bootSector.SectorsPerTrack());
244 __PRINT1(_L("NumberOfHeads %d"),bootSector.NumberOfHeads());
245 __PRINT1(_L("HugeSectors %d"),bootSector.HugeSectors());
246 __PRINT1(_L("Fat32 Sectors %d"),bootSector.FatSectors32());
247 __PRINT1(_L("Fat32 Flags %d"),bootSector.FATFlags());
248 __PRINT1(_L("Fat32 Version Number %d"),bootSector.VersionNumber());
249 __PRINT1(_L("Root Cluster Number %d"),bootSector.RootClusterNum());
250 __PRINT1(_L("FSInfo Sector Number %d"),bootSector.FSInfoSectorNum());
251 __PRINT1(_L("Backup Boot Rec Sector Number %d"),bootSector.BkBootRecSector());
252 __PRINT1(_L("PhysicalDriveNumber %d"),bootSector.PhysicalDriveNumber());
253 __PRINT1(_L("ExtendedBootSignature %d"),bootSector.ExtendedBootSignature());
254 __PRINT1(_L("UniqueID %d"),bootSector.UniqueID());
255 __PRINT8BIT1(_L("VolumeLabel %S"),bootSector.VolumeLabel());
256 __PRINT8BIT1(_L("FileSysType %S\n"),bootSector.FileSysType());
258 iUniqueID=bootSector.UniqueID();
259 iIs16BitFat=bootSector.Is16BitFat();
261 iIs32BitFat=bootSector.Is32BitFat();
262 switch (DetermineFatType(bootSector))
265 iIs16BitFat = EFalse;
266 iIs32BitFat = EFalse;
270 iIs32BitFat = EFalse;
273 iIs16BitFat = EFalse;
280 TInt sectorsPerCluster=bootSector.SectorsPerCluster();
281 if (!IsPowerOfTwo(sectorsPerCluster))
284 TInt sectorSizeLog2=Log2(bootSector.BytesPerSector());
285 if (sectorSizeLog2<0 || !IsPowerOfTwo(bootSector.BytesPerSector()))
288 TInt firstFatSector=bootSector.ReservedSectors();
289 if (firstFatSector<1)
295 fatSizeInBytes=bootSector.FatSectors32()*bootSector.BytesPerSector();
296 if (fatSizeInBytes<bootSector.BytesPerSector())
301 fatSizeInBytes=bootSector.FatSectors()*bootSector.BytesPerSector();
302 if (fatSizeInBytes<bootSector.BytesPerSector())
305 TInt rootDirectorySector=firstFatSector+bootSector.FatSectors()*bootSector.NumberOfFats();
306 if (rootDirectorySector<3)
309 TInt rootDirSizeInBytes=bootSector.RootDirEntries()*KSizeOfFatDirEntry;
310 TInt numOfRootDirSectors=(rootDirSizeInBytes+(1<<sectorSizeLog2)-1)>>sectorSizeLog2;
311 TInt rootDirEnd=(rootDirectorySector+numOfRootDirSectors)<<sectorSizeLog2;
312 if (rootDirEnd<(4<<sectorSizeLog2))
317 TInt totalSectors=bootSector.TotalSectors();
319 totalSectors=bootSector.HugeSectors();
323 TInt numberOfFats=bootSector.NumberOfFats();
331 Read non aligned boot data from media into TFatBootSector structure
333 @param aBootSector refrence to TFatBootSector populate
334 @return Media read error code
336 TInt CMassStorageMountCB::ReadBootSector(TFatBootSector& aBootSector)
338 __FNLOG("CMassStorageMountCB::ReadBootSector");
340 TUint8 data[KSizeOfFatBootSector];
341 TPtr8 buf(&data[0],KSizeOfFatBootSector);
342 TInt r=LocalDrive()->Read(0,KSizeOfFatBootSector,buf);
345 __PRINT1(_L("LocalDrive::Read() failed - %d"),r);
348 // 0 TUint8 iJumpInstruction[3]
349 Mem::Copy(&aBootSector.iJumpInstruction,&data[pos],3);
351 // 3 TUint8 iVendorId[KVendorIdSize]
352 Mem::Copy(&aBootSector.iVendorId,&data[pos],KVendorIdSize);
354 // 11 TUint16 iBytesPerSector
355 Mem::Copy(&aBootSector.iBytesPerSector,&data[pos],2);
357 // 13 TUint8 sectorsPerCluster
358 Mem::Copy(&aBootSector.iSectorsPerCluster,&data[pos],1);
360 // 14 TUint16 iReservedSectors
361 Mem::Copy(&aBootSector.iReservedSectors,&data[pos],2);
363 // 16 TUint8 numberOfFats
364 Mem::Copy(&aBootSector.iNumberOfFats,&data[pos],1);
366 // 17 TUint16 iRootDirEntries
367 Mem::Copy(&aBootSector.iRootDirEntries,&data[pos],2);
369 // 19 TUint16 totalSectors
370 Mem::Copy(&aBootSector.iTotalSectors,&data[pos],2);
372 // 21 TUint8 iMediaDescriptor
373 Mem::Copy(&aBootSector.iMediaDescriptor,&data[pos],1);
375 // 22 TUint16 iFatSectors
376 Mem::Copy(&aBootSector.iFatSectors,&data[pos],2);
378 // 24 TUint16 iSectorsPerTrack
379 Mem::Copy(&aBootSector.iSectorsPerTrack,&data[pos],2);
381 // 26 TUint16 iNumberOfHeads
382 Mem::Copy(&aBootSector.iNumberOfHeads,&data[pos],2);
384 // 28 TUint32 iHiddenSectors
385 Mem::Copy(&aBootSector.iHiddenSectors,&data[pos],4);
387 // 32 TUint32 iHugeSectors
388 Mem::Copy(&aBootSector.iHugeSectors,&data[pos],4);
391 if(aBootSector.iRootDirEntries == 0) //indicates we have FAT32 volume
393 __PRINT(_L("\nFile system thinks Fat32"));
395 //36 TUint32 iFatSectors32
396 Mem::Copy(&aBootSector.iFatSectors32, &data[pos],4);
398 //40 TUint16 iFATFlags
399 Mem::Copy(&aBootSector.iFATFlags, &data[pos],2);
401 //42 TUint16 iVersionNumber
402 Mem::Copy(&aBootSector.iVersionNumber, &data[pos],2);
404 //44 TUint32 iRootClusterNum
405 Mem::Copy(&aBootSector.iRootClusterNum, &data[pos],4);
407 //48 TUint16 iFSInfoSectorNum
408 Mem::Copy(&aBootSector.iFSInfoSectorNum, &data[pos],2);
410 //50 TUint16 iBkBootRecSector
411 Mem::Copy(&aBootSector.iBkBootRecSector, &data[pos],2);
412 pos+=(2+12);//extra 12 for the reserved bytes
415 // 36|64 TUint8 iPhysicalDriveNumber
416 Mem::Copy(&aBootSector.iPhysicalDriveNumber,&data[pos],1);
418 // 37|65 TUint8 iReserved
419 Mem::Copy(&aBootSector.iReserved,&data[pos],1);
421 // 38|66 TUint8 iExtendedBootSignature
422 Mem::Copy(&aBootSector.iExtendedBootSignature,&data[pos],1);
424 // 39|67 TUint32 iUniqueID
425 Mem::Copy(&aBootSector.iUniqueID,&data[pos],4);
427 // 43|71 TUint8 iVolumeLabel[KVolumeLabelSize]
428 Mem::Copy(&aBootSector.iVolumeLabel,&data[pos],KVolumeLabelSize);
429 pos+=KVolumeLabelSize;
430 // 54|82 TUint8 iFileSysType[KFileSysTypeSize]
431 Mem::Copy(&aBootSector.iFileSysType,&data[pos],KFileSysTypeSize);
438 Work out if we have a FAT12|16|32 volume.
439 Returns 12, 16 or 32 as appropriate.
440 Returns 0 if can't be calculated (invalid values)
442 TInt CMassStorageMountCB::DetermineFatType(TFatBootSector& aBootSector)
444 TUint32 ressectors = aBootSector.ReservedSectors();
446 if (aBootSector.SectorsPerCluster() < 1)
449 if (aBootSector.RootDirEntries() != 0)
451 TUint32 rootdirbytes;
452 rootdirbytes = aBootSector.RootDirEntries() * 32 + aBootSector.BytesPerSector() - 1;
453 ressectors += rootdirbytes / aBootSector.BytesPerSector();
456 if (aBootSector.FatSectors() != 0)
457 ressectors += aBootSector.NumberOfFats() * aBootSector.FatSectors();
459 ressectors += aBootSector.NumberOfFats() * aBootSector.FatSectors32();
461 TUint32 totalsectors;
462 if (aBootSector.TotalSectors() != 0)
463 totalsectors = aBootSector.TotalSectors();
465 totalsectors = aBootSector.HugeSectors();
467 if (ressectors < 1 || totalsectors < 1)
471 datasec = totalsectors - ressectors;
473 TUint32 countofclusters;
474 countofclusters = datasec / aBootSector.SectorsPerCluster();
476 __PRINT1(_L("CFatMountCB: Count of clusters = %d\n"), countofclusters);
478 if (countofclusters < 4085)
482 else if (countofclusters < 65525)
492 TInt CMassStorageMountCB::ReMount()
497 void CMassStorageMountCB::VolumeL(TVolumeInfo& /*aVolume*/) const
499 User::Leave(KErrNotReady);
502 void CMassStorageMountCB::SetVolumeL(TDes& /*aName*/)
504 User::Leave(KErrNotReady);
507 void CMassStorageMountCB::MkDirL(const TDesC& /*aName*/)
509 User::Leave(KErrNotReady);
512 void CMassStorageMountCB::RmDirL(const TDesC& /*aName*/)
514 User::Leave(KErrNotReady);
517 void CMassStorageMountCB::DeleteL(const TDesC& /*aName*/)
519 User::Leave(KErrNotReady);
522 void CMassStorageMountCB::RenameL(const TDesC& /*anOldName*/,const TDesC& /*anNewName*/)
524 User::Leave(KErrNotReady);
527 void CMassStorageMountCB::ReplaceL(const TDesC& /*anOldName*/,const TDesC& /*anNewName*/)
529 User::Leave(KErrNotReady);
532 void CMassStorageMountCB::EntryL(const TDesC& /*aName*/,TEntry& /*anEntry*/) const
534 User::Leave(KErrNotReady);
537 void CMassStorageMountCB::SetEntryL(const TDesC& /*aName*/,const TTime& /*aTime*/,TUint /*aSetAttMask*/,TUint /*aClearAttMask*/)
539 User::Leave(KErrNotReady);
542 void CMassStorageMountCB::FileOpenL(const TDesC& /*aName*/,TUint /*aMode*/,TFileOpen /*anOpen*/,CFileCB* /*aFile*/)
544 User::Leave(KErrNotReady);
547 void CMassStorageMountCB::DirOpenL(const TDesC& /*aName*/,CDirCB* /*aDir*/)
549 User::Leave(KErrNotReady);
553 void CMassStorageMountCB::RawReadL(TInt64 /*aPos*/,TInt /*aLength*/,const TAny* /*aTrg*/,TInt /*anOffset*/,const RMessagePtr2& /*aMessage*/) const
555 User::Leave(KErrNotReady);
558 void CMassStorageMountCB::RawWriteL(TInt64 /*aPos*/,TInt /*aLength*/,const TAny* /*aSrc*/,TInt /*anOffset*/,const RMessagePtr2& /*aMessage*/)
560 User::Leave(KErrNotReady);
564 void CMassStorageMountCB::GetShortNameL(const TDesC& /*aLongName*/,TDes& /*aShortName*/)
566 User::Leave(KErrNotReady);
569 void CMassStorageMountCB::GetLongNameL(const TDesC& /*aShorName*/,TDes& /*aLongName*/)
571 User::Leave(KErrNotReady);
575 TInt CMassStorageMountCB::ControlIO(const RMessagePtr2& aMessage,TInt aCommand,TAny* aParam1,TAny* aParam2)
580 if(aCommand>=(KMaxTInt/2))
581 return LocalDrive()->ControlIO(aMessage,aCommand-(KMaxTInt/2),aParam1,aParam2);
583 return KErrNotSupported;
586 TInt CMassStorageMountCB::ControlIO(const RMessagePtr2& /*aMessage*/,TInt /*aCommand*/,TAny* /*aParam1*/,TAny* /*aParam2*/)
587 {return(KErrNotSupported);}
590 void CMassStorageMountCB::ReadSectionL(const TDesC& /*aName*/,TInt /*aPos*/,TAny* /*aTrg*/,TInt /*aLength*/,const RMessagePtr2& /*aMessage*/)
592 User::Leave(KErrNotReady);