userlibandfileserver/fileserver/smassstorage/cmassstoragemountcb.cpp
branchRCL_3
changeset 81 e7d2d738d3c2
parent 0 a41df078684a
child 286 48e57fb1237e
equal deleted inserted replaced
80:597aaf25e343 81:e7d2d738d3c2
    21 */
    21 */
    22 
    22 
    23 #include <f32fsys.h>
    23 #include <f32fsys.h>
    24 #include <f32file.h>
    24 #include <f32file.h>
    25 #include "cmassstoragemountcb.h"
    25 #include "cmassstoragemountcb.h"
       
    26 #include "cusbmassstoragecontroller.h"
    26 #include "cmassstoragefilesystem.h"
    27 #include "cmassstoragefilesystem.h"
    27 #include "drivemanager.h"
       
    28 #include "massstoragedebug.h"
    28 #include "massstoragedebug.h"
    29 #include "massstorageutil.h"
       
    30 
    29 
    31 CMassStorageMountCB::CMassStorageMountCB(const RArray<TInt>& aDriveMapping)
    30 CMassStorageMountCB::CMassStorageMountCB(const RArray<TInt>& aDriveMapping)
    32     : iDriveMapping(aDriveMapping)
    31     : iDriveMapping(aDriveMapping)
    33 	{
    32 	{
    34 	}
    33 	}
   212 		WriteToDisk(mediaPWrdFile,pDes);
   211 		WriteToDisk(mediaPWrdFile,pDes);
   213 		}
   212 		}
   214 	delete hDes;
   213 	delete hDes;
   215 	}
   214 	}
   216 
   215 
   217 /**
       
   218 Make sure that the file system is fat.
       
   219 */
       
   220 TBool CMassStorageMountCB::ValidateBootSector()
       
   221 	{
       
   222 	__FNLOG("CMassStorageMountCB::ValidateBootSector");
       
   223 
       
   224 	TFatBootSector bootSector;
       
   225 	TInt r=ReadBootSector(bootSector);
       
   226 	__PRINT1(_L("CMassStorageMountCB::MountL - ReadBootSector returned %d"),r);
       
   227 	if (r != KErrNone)
       
   228 		{
       
   229 		return EFalse;
       
   230 		}
       
   231 
       
   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());
       
   257 
       
   258     iUniqueID=bootSector.UniqueID();
       
   259 	iIs16BitFat=bootSector.Is16BitFat();
       
   260 
       
   261 	iIs32BitFat=bootSector.Is32BitFat();
       
   262 	switch (DetermineFatType(bootSector))
       
   263 		{
       
   264 		case 12:
       
   265 			iIs16BitFat = EFalse;
       
   266 			iIs32BitFat = EFalse;
       
   267 			break;
       
   268 		case 16:
       
   269 			iIs16BitFat = ETrue;
       
   270 			iIs32BitFat = EFalse;
       
   271 			break;
       
   272 		case 32:
       
   273 			iIs16BitFat = EFalse;
       
   274 			iIs32BitFat = ETrue;
       
   275 			break;
       
   276 		default:
       
   277 			return EFalse;
       
   278 		}
       
   279 
       
   280 	TInt sectorsPerCluster=bootSector.SectorsPerCluster();
       
   281 	if (!IsPowerOfTwo(sectorsPerCluster))
       
   282 		return EFalse;
       
   283 
       
   284 	TInt sectorSizeLog2=Log2(bootSector.BytesPerSector());
       
   285 	if (sectorSizeLog2<0 || !IsPowerOfTwo(bootSector.BytesPerSector()))
       
   286 		return EFalse;
       
   287 
       
   288 	TInt firstFatSector=bootSector.ReservedSectors();
       
   289 	if (firstFatSector<1)
       
   290 		return EFalse;
       
   291 
       
   292 	TInt fatSizeInBytes;
       
   293 	if(iIs32BitFat)
       
   294 		{
       
   295 		fatSizeInBytes=bootSector.FatSectors32()*bootSector.BytesPerSector();
       
   296 		if (fatSizeInBytes<bootSector.BytesPerSector())
       
   297 			return EFalse;
       
   298 		}
       
   299 	else
       
   300 		{
       
   301 		fatSizeInBytes=bootSector.FatSectors()*bootSector.BytesPerSector();
       
   302 		if (fatSizeInBytes<bootSector.BytesPerSector())
       
   303 			return EFalse;
       
   304 
       
   305 		TInt rootDirectorySector=firstFatSector+bootSector.FatSectors()*bootSector.NumberOfFats();
       
   306 		if (rootDirectorySector<3)
       
   307 			return EFalse;
       
   308 
       
   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))
       
   313 			return EFalse;
       
   314 		}
       
   315 
       
   316 
       
   317 	TInt totalSectors=bootSector.TotalSectors();
       
   318 	if (totalSectors==0)
       
   319 		totalSectors=bootSector.HugeSectors();
       
   320 	if (totalSectors<5)
       
   321 		return EFalse;
       
   322 
       
   323 	TInt numberOfFats=bootSector.NumberOfFats();
       
   324 	if (numberOfFats<1)
       
   325 		return EFalse;
       
   326 
       
   327 	return ETrue;
       
   328 	}
       
   329 
       
   330 /**
       
   331 Read non aligned boot data from media into TFatBootSector structure
       
   332 
       
   333 @param aBootSector refrence to TFatBootSector populate
       
   334 @return Media read error code
       
   335 */
       
   336 TInt CMassStorageMountCB::ReadBootSector(TFatBootSector& aBootSector)
       
   337 	{
       
   338 	__FNLOG("CMassStorageMountCB::ReadBootSector");
       
   339 	TInt pos=0;
       
   340 	TUint8 data[KSizeOfFatBootSector];
       
   341     TPtr8 buf(&data[0],KSizeOfFatBootSector);
       
   342     TInt r=LocalDrive()->Read(0,KSizeOfFatBootSector,buf);
       
   343 	if (r!=KErrNone)
       
   344 		{
       
   345 		__PRINT1(_L("LocalDrive::Read() failed - %d"),r);
       
   346 		return(r);
       
   347 		}
       
   348 //	0	TUint8 iJumpInstruction[3]
       
   349 	Mem::Copy(&aBootSector.iJumpInstruction,&data[pos],3);
       
   350 	pos+=3;
       
   351 // 3	TUint8 iVendorId[KVendorIdSize]
       
   352 	Mem::Copy(&aBootSector.iVendorId,&data[pos],KVendorIdSize);
       
   353 	pos+=KVendorIdSize;
       
   354 // 11	TUint16 iBytesPerSector
       
   355 	Mem::Copy(&aBootSector.iBytesPerSector,&data[pos],2);
       
   356 	pos+=2;
       
   357 // 13	TUint8 sectorsPerCluster
       
   358 	Mem::Copy(&aBootSector.iSectorsPerCluster,&data[pos],1);
       
   359 	pos+=1;
       
   360 // 14	TUint16 iReservedSectors
       
   361 	Mem::Copy(&aBootSector.iReservedSectors,&data[pos],2);
       
   362 	pos+=2;
       
   363 // 16	TUint8 numberOfFats
       
   364 	Mem::Copy(&aBootSector.iNumberOfFats,&data[pos],1);
       
   365 	pos+=1;
       
   366 // 17	TUint16 iRootDirEntries
       
   367 	Mem::Copy(&aBootSector.iRootDirEntries,&data[pos],2);
       
   368 	pos+=2;
       
   369 // 19	TUint16 totalSectors
       
   370 	Mem::Copy(&aBootSector.iTotalSectors,&data[pos],2);
       
   371 	pos+=2;
       
   372 // 21	TUint8 iMediaDescriptor
       
   373 	Mem::Copy(&aBootSector.iMediaDescriptor,&data[pos],1);
       
   374 	pos+=1;
       
   375 // 22	TUint16 iFatSectors
       
   376 	Mem::Copy(&aBootSector.iFatSectors,&data[pos],2);
       
   377 	pos+=2;
       
   378 // 24	TUint16 iSectorsPerTrack
       
   379 	Mem::Copy(&aBootSector.iSectorsPerTrack,&data[pos],2);
       
   380 	pos+=2;
       
   381 // 26	TUint16 iNumberOfHeads
       
   382 	Mem::Copy(&aBootSector.iNumberOfHeads,&data[pos],2);
       
   383 	pos+=2;
       
   384 // 28	TUint32 iHiddenSectors
       
   385 	Mem::Copy(&aBootSector.iHiddenSectors,&data[pos],4);
       
   386 	pos+=4;
       
   387 // 32	TUint32 iHugeSectors
       
   388 	Mem::Copy(&aBootSector.iHugeSectors,&data[pos],4);
       
   389 	pos+=4;
       
   390 
       
   391 	if(aBootSector.iRootDirEntries == 0)	//indicates we have FAT32 volume
       
   392 		{
       
   393 		__PRINT(_L("\nFile system thinks Fat32"));
       
   394 
       
   395 		//36 TUint32 iFatSectors32
       
   396 		Mem::Copy(&aBootSector.iFatSectors32, &data[pos],4);
       
   397 		pos+=4;
       
   398 		//40 TUint16 iFATFlags
       
   399 		Mem::Copy(&aBootSector.iFATFlags, &data[pos],2);
       
   400 		pos+=2;
       
   401 		//42 TUint16 iVersionNumber
       
   402 		Mem::Copy(&aBootSector.iVersionNumber, &data[pos],2);
       
   403 		pos+=2;
       
   404 		//44 TUint32 iRootClusterNum
       
   405 		Mem::Copy(&aBootSector.iRootClusterNum, &data[pos],4);
       
   406 		pos+=4;
       
   407 		//48 TUint16 iFSInfoSectorNum
       
   408 		Mem::Copy(&aBootSector.iFSInfoSectorNum, &data[pos],2);
       
   409 		pos+=2;
       
   410 		//50 TUint16 iBkBootRecSector
       
   411 		Mem::Copy(&aBootSector.iBkBootRecSector, &data[pos],2);
       
   412 		pos+=(2+12);//extra 12 for the reserved bytes
       
   413 		}
       
   414 
       
   415 // 36|64	TUint8 iPhysicalDriveNumber
       
   416 	Mem::Copy(&aBootSector.iPhysicalDriveNumber,&data[pos],1);
       
   417 	pos+=1;
       
   418 // 37|65	TUint8 iReserved
       
   419 	Mem::Copy(&aBootSector.iReserved,&data[pos],1);
       
   420 	pos+=1;
       
   421 // 38|66	TUint8 iExtendedBootSignature
       
   422 	Mem::Copy(&aBootSector.iExtendedBootSignature,&data[pos],1);
       
   423 	pos+=1;
       
   424 // 39|67	TUint32 iUniqueID
       
   425 	Mem::Copy(&aBootSector.iUniqueID,&data[pos],4);
       
   426 	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);
       
   432 // 62|90
       
   433 
       
   434 	return(KErrNone);
       
   435 	}
       
   436 
       
   437 /**
       
   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)
       
   441 */
       
   442 TInt CMassStorageMountCB::DetermineFatType(TFatBootSector& aBootSector)
       
   443 	{
       
   444 	TUint32 ressectors = aBootSector.ReservedSectors();
       
   445 
       
   446 	if (aBootSector.SectorsPerCluster() < 1)
       
   447 		return 0;
       
   448 
       
   449 	if (aBootSector.RootDirEntries() != 0)
       
   450 		{
       
   451 		TUint32 rootdirbytes;
       
   452 		rootdirbytes = aBootSector.RootDirEntries() * 32 + aBootSector.BytesPerSector() - 1;
       
   453 		ressectors += rootdirbytes / aBootSector.BytesPerSector();
       
   454 		}
       
   455 
       
   456 	if (aBootSector.FatSectors() != 0)
       
   457 		ressectors += aBootSector.NumberOfFats() * aBootSector.FatSectors();
       
   458 	else
       
   459 		ressectors += aBootSector.NumberOfFats() * aBootSector.FatSectors32();
       
   460 
       
   461 	TUint32 totalsectors;
       
   462 	if (aBootSector.TotalSectors() != 0)
       
   463 		totalsectors = aBootSector.TotalSectors();
       
   464 	else
       
   465 		totalsectors = aBootSector.HugeSectors();
       
   466 
       
   467 	if (ressectors < 1 || totalsectors < 1)
       
   468 		return 0;
       
   469 
       
   470 	TUint32 datasec;
       
   471 	datasec = totalsectors - ressectors;
       
   472 
       
   473 	TUint32 countofclusters;
       
   474 	countofclusters = datasec / aBootSector.SectorsPerCluster();
       
   475 
       
   476 	__PRINT1(_L("CFatMountCB: Count of clusters = %d\n"), countofclusters);
       
   477 
       
   478 	if (countofclusters < 4085)
       
   479 		{
       
   480 		return 12;
       
   481 		}
       
   482 	else if (countofclusters < 65525)
       
   483 		{
       
   484 		return 16;
       
   485 		}
       
   486 	else
       
   487 		{
       
   488 		return 32;
       
   489 		}
       
   490 	}
       
   491 
       
   492 TInt CMassStorageMountCB::ReMount()
   216 TInt CMassStorageMountCB::ReMount()
   493 	{
   217 	{
   494 	return KErrNotReady;
   218 	return KErrNotReady;
   495 	}
   219 	}
   496 
   220