userlibandfileserver/fileserver/sfat/sl_fmt.cpp
changeset 15 4122176ea935
parent 0 a41df078684a
equal deleted inserted replaced
0:a41df078684a 15:4122176ea935
    13 // Description:
    13 // Description:
    14 // f32\sfat\sl_fmt.cpp
    14 // f32\sfat\sl_fmt.cpp
    15 // 
    15 // 
    16 //
    16 //
    17 
    17 
       
    18 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    19 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    20 //!!
       
    21 //!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
       
    22 //!!
       
    23 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    24 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    25 
    18 #include "sl_std.h"
    26 #include "sl_std.h"
    19 #include <e32hal.h>
    27 #include <e32hal.h>
    20 
    28 
    21 
    29 
    22 
    30 
    24 //
    32 //
    25 // Returns the total available ram from UserHal:: or sets an
    33 // Returns the total available ram from UserHal:: or sets an
    26 // arbitrary limit upon the WINS ramdisk.
    34 // arbitrary limit upon the WINS ramdisk.
    27 //
    35 //
    28 static TInt64 GetRamDiskSizeInBytes()
    36 static TInt64 GetRamDiskSizeInBytes()
    29 	{
    37     {
    30 
    38 
    31 #if defined(__EPOC32__)
    39 #if defined(__EPOC32__)
    32 	TMemoryInfoV1Buf memInfo;
    40     TMemoryInfoV1Buf memInfo;
    33 	UserHal::MemoryInfo(memInfo);
    41     UserHal::MemoryInfo(memInfo);
    34 	TUint max = memInfo().iTotalRamInBytes; // not really the correct max
    42     TUint max = memInfo().iTotalRamInBytes; // not really the correct max
    35 	return max;
    43     return max;
    36 #else
    44 #else
    37     const TInt KArbitraryWinsRamDiskSize=0x400000;  //-- Default size for a Ram drive, 4MB
    45     const TInt KArbitraryWinsRamDiskSize=0x400000;  //-- Default size for a Ram drive, 4MB
    38 	return(KArbitraryWinsRamDiskSize);
    46     return(KArbitraryWinsRamDiskSize);
    39 #endif
    47 #endif
    40 	}
    48     }
    41 
    49 
    42 CFatFormatCB::CFatFormatCB()
    50 CFatFormatCB::CFatFormatCB()
    43 	{
    51     {
    44 	__PRINT1(_L("CFatFormatCB::CFatFormatCB() [%x]"),this);
    52     __PRINT1(_L("CFatFormatCB::CFatFormatCB() [%x]"),this);
    45     }
    53     }
    46 
    54 
    47 CFatFormatCB::~CFatFormatCB()
    55 CFatFormatCB::~CFatFormatCB()
    48 	{
    56     {
    49 	__PRINT1(_L("CFatFormatCB::~CFatFormatCB() [%x]"),this);
    57     __PRINT1(_L("CFatFormatCB::~CFatFormatCB() [%x]"),this);
    50     iBadSectors.Close();
    58     iBadSectors.Close();
    51 	iBadClusters.Close();
    59     iBadClusters.Close();
    52 	}
    60     }
    53 
    61 
    54 TInt CFatFormatCB::MaxFat16Sectors() const
    62 TInt CFatFormatCB::MaxFat16Sectors() const
    55 //
    63 //
    56 // Calculate the size of a 16 bit FAT
    64 // Calculate the size of a 16 bit FAT
    57 //
    65 //
    58 	{
    66     {
    59 	
    67     
    60 	TInt fatSizeInBytes=(2*iMaxDiskSectors)/iSectorsPerCluster+(iBytesPerSector-1);
    68     TInt fatSizeInBytes=(2*iMaxDiskSectors)/iSectorsPerCluster+(iBytesPerSector-1);
    61 	return(fatSizeInBytes/iBytesPerSector);
    69     return(fatSizeInBytes/iBytesPerSector);
    62 	}
    70     }
    63 
    71 
    64 TInt CFatFormatCB::MaxFat12Sectors() const
    72 TInt CFatFormatCB::MaxFat12Sectors() const
    65 //
    73 //
    66 // Calculate the size of a 12 bit FAT
    74 // Calculate the size of a 12 bit FAT
    67 //
    75 //
    68 	{
    76     {
    69 	
    77     
    70 	TInt maxDiskClusters=iMaxDiskSectors/iSectorsPerCluster;
    78     TInt maxDiskClusters=iMaxDiskSectors/iSectorsPerCluster;
    71 	TInt fatSizeInBytes=maxDiskClusters+(maxDiskClusters>>1)+(iBytesPerSector-1);
    79     TInt fatSizeInBytes=maxDiskClusters+(maxDiskClusters>>1)+(iBytesPerSector-1);
    72 	return(fatSizeInBytes/iBytesPerSector);
    80     return(fatSizeInBytes/iBytesPerSector);
    73 	}
    81     }
    74 
    82 
    75 //-------------------------------------------------------------------------------------------------------------------
    83 //-------------------------------------------------------------------------------------------------------------------
    76 /**
    84 /**
    77     Fill a media range from aStartPos to aEndPos with zeroes.
    85     Fill a media range from aStartPos to aEndPos with zeroes.
    78     @param  aStartPos   start media position
    86     @param  aStartPos   start media position
   111     }
   119     }
   112 
   120 
   113 //-------------------------------------------------------------------------------------------------------------------
   121 //-------------------------------------------------------------------------------------------------------------------
   114 
   122 
   115 static TInt DiskSizeInSectorsL(TInt64 aSizeInBytes)
   123 static TInt DiskSizeInSectorsL(TInt64 aSizeInBytes)
   116 	{
   124     {
   117     const TInt64 totalSectors64=aSizeInBytes>>KDefSectorSzLog2;
   125     const TInt64 totalSectors64=aSizeInBytes>>KDefSectorSzLog2;
   118 	const TInt   totalSectors32=I64LOW(totalSectors64);
   126     const TInt   totalSectors32=I64LOW(totalSectors64);
   119     __PRINT2(_L("Disk size:%LU, max disk sectors:%d"),aSizeInBytes, totalSectors32);
   127     __PRINT2(_L("Disk size:%LU, max disk sectors:%d"),aSizeInBytes, totalSectors32);
   120     return totalSectors32;
   128     return totalSectors32;
   121 	}
   129     }
   122 
   130 
   123 
   131 
   124 /**
   132 /**
   125     suggest FAT type according to the FAT volume metrics
   133     suggest FAT type according to the FAT volume metrics
   126     @return calculated FAT type
   134     @return calculated FAT type
   142 
   150 
   143 /**
   151 /**
   144     Initialize format data.
   152     Initialize format data.
   145 */
   153 */
   146 void CFatFormatCB::InitializeFormatDataL()
   154 void CFatFormatCB::InitializeFormatDataL()
   147 	{
   155     {
   148       
   156       
   149 	__PRINT1(_L("CFatFormatCB::InitializeFormatDataL() drv:%d"), Drive().DriveNumber());
   157     __PRINT1(_L("CFatFormatCB::InitializeFormatDataL() drv:%d"), Drive().DriveNumber());
   150 	TLocalDriveCapsV6Buf caps;
   158     TLocalDriveCapsV6Buf caps;
   151 	User::LeaveIfError(LocalDrive()->Caps(caps));
   159     User::LeaveIfError(LocalDrive()->Caps(caps));
   152 	iVariableSize=((caps().iMediaAtt)&KMediaAttVariableSize) ? (TBool)ETrue : (TBool)EFalse;
   160     iVariableSize=((caps().iMediaAtt)&KMediaAttVariableSize) ? (TBool)ETrue : (TBool)EFalse;
   153 
   161 
   154 	iBytesPerSector=KDefaultSectorSize;
   162     iBytesPerSector=KDefaultSectorSize;
   155 	iSectorSizeLog2 = Log2(iBytesPerSector);
   163     iSectorSizeLog2 = Log2(iBytesPerSector);
   156 	iHiddenSectors=caps().iHiddenSectors;	
   164     iHiddenSectors=caps().iHiddenSectors;   
   157 	iNumberOfHeads=2;
   165     iNumberOfHeads=2;
   158 	iSectorsPerTrack=16;
   166     iSectorsPerTrack=16;
   159 	
   167     
   160     if (iVariableSize)
   168     if (iVariableSize)
   161 		{// Variable size implies ram disk
   169         {// Variable size implies ram disk
   162 		iMaxDiskSectors=DiskSizeInSectorsL(GetRamDiskSizeInBytes());
   170         iMaxDiskSectors=DiskSizeInSectorsL(GetRamDiskSizeInBytes());
   163 		InitFormatDataForVariableSizeDisk(iMaxDiskSectors);
   171         InitFormatDataForVariableSizeDisk(iMaxDiskSectors);
   164 		}
   172         }
   165 	else
   173     else
   166 		{//-- fixed-size media
   174         {//-- fixed-size media
   167         iMaxDiskSectors=DiskSizeInSectorsL(caps().iSize);
   175         iMaxDiskSectors=DiskSizeInSectorsL(caps().iSize);
   168 		
   176         
   169         __PRINT3(_L("::InitializeFormatDataL() iMode:0x%x, ilen:%d, extrai:%d"), iMode, iSpecialInfo.Length(), caps().iExtraInfo);
   177         __PRINT3(_L("::InitializeFormatDataL() iMode:0x%x, ilen:%d, extrai:%d"), iMode, iSpecialInfo.Length(), caps().iExtraInfo);
   170 
   178 
   171         if(iMode & ESpecialFormat)
   179         if(iMode & ESpecialFormat)
   172 		    {
   180             {
   173 		    if(iSpecialInfo.Length())
   181             if(iSpecialInfo.Length())
   174 			    {
   182                 {
   175                 if (caps().iExtraInfo)  // conflict between user and media
   183                 if (caps().iExtraInfo)  // conflict between user and media
   176                     User::Leave(KErrNotSupported);
   184                     User::Leave(KErrNotSupported);
   177 			    else  // User-specified
   185                 else  // User-specified
   178                     User::LeaveIfError(InitFormatDataForFixedSizeDiskUser(iMaxDiskSectors));
   186                     User::LeaveIfError(InitFormatDataForFixedSizeDiskUser(iMaxDiskSectors));
   179                 }
   187                 }
   180     		else
   188             else
   181     		    {
   189                 {
   182                 if (caps().iExtraInfo)
   190                 if (caps().iExtraInfo)
   183                     User::LeaveIfError(InitFormatDataForFixedSizeDiskCustom(caps().iFormatInfo));
   191                     User::LeaveIfError(InitFormatDataForFixedSizeDiskCustom(caps().iFormatInfo));
   184                 else
   192                 else
   185     			    User::LeaveIfError(InitFormatDataForFixedSizeDiskNormal(iMaxDiskSectors, caps()));
   193                     User::LeaveIfError(InitFormatDataForFixedSizeDiskNormal(iMaxDiskSectors, caps()));
   186                 }
   194                 }
   187 		    }
   195             }
   188         else //if(iMode & ESpecialFormat)
   196         else //if(iMode & ESpecialFormat)
   189             {
   197             {
   190             // Normal format with default values
   198             // Normal format with default values
   191             //  - Media with special format requirements will always use them
   199             //  - Media with special format requirements will always use them
   192             //    even without the ESpecialFormat option.
   200             //    even without the ESpecialFormat option.
   193             if(caps().iExtraInfo)
   201             if(caps().iExtraInfo)
   194 	            User::LeaveIfError(InitFormatDataForFixedSizeDiskCustom(caps().iFormatInfo));
   202                 User::LeaveIfError(InitFormatDataForFixedSizeDiskCustom(caps().iFormatInfo));
   195             else
   203             else
   196 	            User::LeaveIfError(InitFormatDataForFixedSizeDiskNormal(iMaxDiskSectors, caps()));
   204                 User::LeaveIfError(InitFormatDataForFixedSizeDiskNormal(iMaxDiskSectors, caps()));
   197 		    }
   205             }
   198         
   206         
   199         } //else(iVariableSize)
   207         } //else(iVariableSize)
   200 	}
   208     }
   201 
   209 
   202 /**
   210 /**
   203     Initialize the format parameters for a variable sized disk
   211     Initialize the format parameters for a variable sized disk
   204     
   212     
   205     @param  aDiskSizeInSectors volume size in sectors
   213     @param  aDiskSizeInSectors volume size in sectors
   206     @return standard error code
   214     @return standard error code
   207 */
   215 */
   208 TInt  CFatFormatCB::InitFormatDataForVariableSizeDisk(TInt aDiskSizeInSectors)
   216 TInt  CFatFormatCB::InitFormatDataForVariableSizeDisk(TInt aDiskSizeInSectors)
   209 	{
   217     {
   210 	iNumberOfFats=2; // 1 FAT 1 Indirection table (FIT)
   218     iNumberOfFats=2; // 1 FAT 1 Indirection table (FIT)
   211 	iReservedSectors=1;
   219     iReservedSectors=1;
   212 	iRootDirEntries=2*(4*KDefaultSectorSize)/sizeof(SFatDirEntry);
   220     iRootDirEntries=2*(4*KDefaultSectorSize)/sizeof(SFatDirEntry);
   213 	TInt minSectorsPerCluster=(aDiskSizeInSectors+KMaxFAT16Entries-1)/KMaxFAT16Entries;
   221     TInt minSectorsPerCluster=(aDiskSizeInSectors+KMaxFAT16Entries-1)/KMaxFAT16Entries;
   214 	iSectorsPerCluster=1;
   222     iSectorsPerCluster=1;
   215 	while (minSectorsPerCluster>iSectorsPerCluster)
   223     while (minSectorsPerCluster>iSectorsPerCluster)
   216 		iSectorsPerCluster<<=1;
   224         iSectorsPerCluster<<=1;
   217 	__PRINT1(_L("iSectorsPerCluster = %d"),iSectorsPerCluster);
   225     __PRINT1(_L("iSectorsPerCluster = %d"),iSectorsPerCluster);
   218 	iSectorsPerFat=MaxFat16Sectors();
   226     iSectorsPerFat=MaxFat16Sectors();
   219 	__PRINT1(_L("iSectorsPerFat = %d"),iSectorsPerFat);
   227     __PRINT1(_L("iSectorsPerFat = %d"),iSectorsPerFat);
   220 	iFileSystemName=KFileSystemName16;
   228     iFileSystemName=KFileSystemName16;
   221 
   229 
   222 	return KErrNone;
   230     return KErrNone;
   223 	}
   231     }
   224 
   232 
   225 TInt CFatFormatCB::HandleCorrupt(TInt aError)
   233 TInt CFatFormatCB::HandleCorrupt(TInt aError)
   226 //
   234 //
   227 // Handle disk corrupt during format. It needs media driver's support.
   235 // Handle disk corrupt during format. It needs media driver's support.
   228 // Media driver should handle DLocalDrive::EGetLastErrorInfo request in
   236 // Media driver should handle DLocalDrive::EGetLastErrorInfo request in
   229 // its Request function, filling in proper error information.
   237 // its Request function, filling in proper error information.
   230 // @see TErrorInfo
   238 // @see TErrorInfo
   231 //
   239 //
   232     {
   240     {
   233 	__PRINT2(_L("CFatFormatCB::HandleCorrupt(%d) drv:%d"), aError, Drive().DriveNumber());
   241     __PRINT2(_L("CFatFormatCB::HandleCorrupt(%d) drv:%d"), aError, Drive().DriveNumber());
   234 
   242 
   235     TPckgBuf<TErrorInfo> info;
   243     TPckgBuf<TErrorInfo> info;
   236 	TInt r = LocalDrive()->GetLastErrorInfo(info);
   244     TInt r = LocalDrive()->GetLastErrorInfo(info);
   237     
   245     
   238     if(r != KErrNone)
   246     if(r != KErrNone)
   239         {
   247         {
   240         __PRINT1(_L("....GetLastErrorInfo() err:%d"), r);
   248         __PRINT1(_L("....GetLastErrorInfo() err:%d"), r);
   241         }
   249         }
   242 
   250 
   243     if (r == KErrNotSupported)
   251     if (r == KErrNotSupported)
   244 		return KErrCorrupt;
   252         return KErrCorrupt;
   245     else if (r != KErrNone)
   253     else if (r != KErrNone)
   246         return r;
   254         return r;
   247 
   255 
   248     __PRINT3(_L("....TErrorInfo iReasonCode:%d, iErrorPos:%LU, iOtherInfo:%d"), info().iReasonCode, info().iErrorPos, info().iOtherInfo);
   256     __PRINT3(_L("....TErrorInfo iReasonCode:%d, iErrorPos:%LU, iOtherInfo:%d"), info().iReasonCode, info().iErrorPos, info().iOtherInfo);
   249 	
   257     
   250     // if no error reported by GetLastErrorInfo(), return the original error
   258     // if no error reported by GetLastErrorInfo(), return the original error
   251 	if (info().iReasonCode == KErrNone)
   259     if (info().iReasonCode == KErrNone)
   252 		return aError;
   260         return aError;
   253 
   261 
   254     if (info().iReasonCode!=KErrNone && info().iReasonCode!=TErrorInfo::EBadSector)
   262     if (info().iReasonCode!=KErrNone && info().iReasonCode!=TErrorInfo::EBadSector)
   255         return info().iReasonCode;
   263         return info().iReasonCode;
   256 
   264 
   257     // First bad sector met
   265     // First bad sector met