userlibandfileserver/fileserver/sfat/sl_fat16.cpp
branchRCL_3
changeset 80 597aaf25e343
parent 15 4122176ea935
--- a/userlibandfileserver/fileserver/sfat/sl_fat16.cpp	Sat Feb 20 00:10:51 2010 +0200
+++ b/userlibandfileserver/fileserver/sfat/sl_fat16.cpp	Fri Mar 12 15:50:11 2010 +0200
@@ -82,37 +82,49 @@
         iSectorsPerFat=MaxFat16Sectors();
         }
     
-    // Ensure cluster size is a multiple of the block size
-    TInt blockSizeInSectors = aCaps.iBlockSize >> iSectorSizeLog2;
-    __PRINT1(_L("blockSizeInSectors: %d"),blockSizeInSectors);
-    ASSERT(blockSizeInSectors == 0 || IsPowerOf2(blockSizeInSectors));
-    if (blockSizeInSectors != 0 && IsPowerOf2(blockSizeInSectors))
-        {
-        __PRINT1(_L("iSectorsPerCluster (old): %d"),iSectorsPerCluster);
-        AdjustClusterSize(blockSizeInSectors);
-        __PRINT1(_L("iSectorsPerCluster (new): %d"),iSectorsPerCluster);
-        }
-    
-    // Align first data sector on an erase block boundary if
-    // (1) the iEraseBlockSize is specified
-    // (2) the start of the partition is already aligned to an erase block boundary, 
-    //     i.e. iHiddenSectors is zero or a multiple of iEraseBlockSize
-    __PRINT1(_L("iHiddenSectors: %d"),iHiddenSectors);
-    TInt eraseblockSizeInSectors = aCaps.iEraseBlockSize >> iSectorSizeLog2;
-    __PRINT1(_L("eraseblockSizeInSectors: %d"),eraseblockSizeInSectors);
-    ASSERT(eraseblockSizeInSectors == 0 || IsPowerOf2(eraseblockSizeInSectors));    
-    ASSERT(eraseblockSizeInSectors == 0 || eraseblockSizeInSectors >= blockSizeInSectors);
-    if ((eraseblockSizeInSectors != 0) &&
-        (iHiddenSectors % eraseblockSizeInSectors == 0) &&  
-        (IsPowerOf2(eraseblockSizeInSectors)) &&
-        (eraseblockSizeInSectors >= blockSizeInSectors))
-        {
-        TInt r = AdjustFirstDataSectorAlignment(eraseblockSizeInSectors);
-        ASSERT(r == KErrNone);
-        (void) r;
-        }
-    __PRINT1(_L("iReservedSectors: %d"),iReservedSectors);
-    __PRINT1(_L("FirstDataSector: %d"), FirstDataSector());
+	const TFatType fatType = SuggestFatType();
+
+	// Ensure cluster size is a multiple of the block size
+	TInt blockSizeInSectors = aCaps.iBlockSize >> iSectorSizeLog2;
+	__PRINT1(_L("blockSizeInSectors: %d"),blockSizeInSectors);
+	ASSERT(blockSizeInSectors == 0 || IsPowerOf2(blockSizeInSectors));
+	if (blockSizeInSectors != 0 && IsPowerOf2(blockSizeInSectors))
+		{
+		__PRINT1(_L("iSectorsPerCluster    (old): %d"),iSectorsPerCluster);
+		AdjustClusterSize(blockSizeInSectors);
+		__PRINT1(_L("iSectorsPerCluster    (new): %d"),iSectorsPerCluster);
+		}
+
+
+	for (; iSectorsPerCluster>1; iSectorsPerCluster>>= 1)
+		{
+		// Align first data sector on an erase block boundary if
+		// (1) the iEraseBlockSize is specified
+		// (2) the start of the partition is already aligned to an erase block boundary, 
+		//     i.e. iHiddenSectors is zero or a multiple of iEraseBlockSize
+		__PRINT1(_L("iHiddenSectors: %d"),iHiddenSectors);
+		TInt eraseblockSizeInSectors = aCaps.iEraseBlockSize >> iSectorSizeLog2;
+		__PRINT1(_L("eraseblockSizeInSectors: %d"),eraseblockSizeInSectors);
+		ASSERT(eraseblockSizeInSectors == 0 || IsPowerOf2(eraseblockSizeInSectors));	
+		ASSERT(eraseblockSizeInSectors == 0 || eraseblockSizeInSectors >= blockSizeInSectors);
+		if ((eraseblockSizeInSectors != 0) &&
+			(iHiddenSectors % eraseblockSizeInSectors == 0) &&	
+			(IsPowerOf2(eraseblockSizeInSectors)) &&
+			(eraseblockSizeInSectors >= blockSizeInSectors))
+			{
+			TInt r = AdjustFirstDataSectorAlignment(eraseblockSizeInSectors);
+			ASSERT(r == KErrNone);
+			(void) r;
+			}
+		__PRINT1(_L("iReservedSectors: %d"),iReservedSectors);
+		__PRINT1(_L("FirstDataSector: %d"), FirstDataSector());
+
+		// If we've shrunk the number of clusters by so much that it's now invalid for this FAT type
+		// then we need to decrease the cluster size and try again, otherwise we're finshed.
+		if (SuggestFatType() == fatType)
+			break;
+		}
+	__PRINT1(_L("iSectorsPerCluster  (final): %d"),iSectorsPerCluster);
 
     return KErrNone;
     }
@@ -124,11 +136,11 @@
     }
 
 void CFatFormatCB::AdjustClusterSize(TInt aRecommendedSectorsPerCluster)
-    {
-    const TInt KMaxSecPerCluster = 64;  // 32K
-    while (aRecommendedSectorsPerCluster > iSectorsPerCluster && iSectorsPerCluster <= (KMaxSecPerCluster/2))
-        iSectorsPerCluster<<= 1;
-    }
+	{
+    const TInt KMaxSecPerCluster = 64;	// 32K
+	while (aRecommendedSectorsPerCluster > iSectorsPerCluster && iSectorsPerCluster <= (KMaxSecPerCluster/2))
+		iSectorsPerCluster<<= 1;
+	}
 
 // AdjustFirstDataSectorAlignment()
 // Attempts to align the first data sector on an erase block boundary by modifying the