First public contribution.
1 // Copyright (c) 1996-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 // f32\sfat\fat_config.cpp
15 // Implementation of the class that reads FAT filesystem configuration parameters from the estart.txt file,
16 // caches and provides access to them
26 #include "common_constants.h"
28 #include "fat_config.h"
30 //########################################################################################################################
31 //-- parameters that can be defined in estart.txt file under [Drive%] section
33 //=======================================================================================================================
34 //-- FAT volumes mounting related settings
35 //=======================================================================================================================
37 //-- if this parameter is not 0, ScanDrive will skip finalised volume. Applicable for FAT16/32
38 //-- e.g: FAT_ScanDrv_SkipFinalisedVolume 1
39 _LIT8(KPN_ScanDrvSkipFinalisedVolume,"FAT_ScanDrv_SkipFinalisedVolume"); //-- parameter key name
40 static const TUint32 KDef_ScanDrvSkipFinalisedVolume = 0; //-- parameter default value
42 //-- if this parameter is not 0, enables asynchronous mounting FAT32 volumes.
43 //-- At present it means enabling/disabling FAT32 background scanning for free clusters. Applicable for FAT32 only
44 //-- e.g: FAT_FAT32_AsynchMount=0
45 _LIT8(KPN_FAT32_AsynchMount, "FAT_FAT32_AsynchMount");
46 static const TUint32 KDef_FAT32_AsynchMount = 1;
48 //-- Using FSInfo sector data during FAT32 mount to find out number of free clusters etc.
49 //-- e.g: FAT_FAT32_UseFsInfo=1
50 _LIT8(KPN_FAT32_UseFsInfoOnMount, "FAT_FAT32_UseFsInfo");
51 static const TUint32 KDef_FAT32_UseFsInfoOnMount = 1;
53 //-- Using FAT bit supercache for fast free cluster lookup
54 //-- e.g: FAT_FAT32_UseBitSupercache 0
55 _LIT8(KPN_FAT32_UseFatBitSupercache, "FAT_FAT32_UseBitSupercache");
56 static const TUint32 KDef_FAT32_UseFatBitSupercache = 1;
59 //-- if this parameter is not 0, "clean shutdown mask" bit in FAT16[1] will be used during volume finalisation.
60 //-- Otherwise, FAT16[1] will not be affected during finalisation
61 //-- e.g: KDef_FAT16_UseCleanShutDownBit 0
62 _LIT8(KPN_FAT16_UseCleanShutDownBit,"FAT16_UseCleanShutDownBit"); //-- parameter key name
63 static const TUint32 KDef_FAT16_UseCleanShutDownBit = 1; //-- parameter default value
66 //=======================================================================================================================
67 //-- FAT directory cache settings
68 //=======================================================================================================================
69 //-- FAT_DirCache <CacheSizeKB>,<Log2(max page size)>
70 //-- e.g: FAT_DirCache 16,12
71 _LIT8(KPN_FAT_DirCache, "FAT_DirCache");
73 static const TUint32 KDef_KDirCacheSizeKB = 16; //-- default value for the total directory cache size, kilobytes. 16K by default
74 static const TUint32 KDef_MaxDirCachePageSzLog2 = 12;//-- default value for directory cache single page maximal size Log2, 2^12 (4K) by default
77 //=======================================================================================================================
78 //-- FAT cache settings
79 //=======================================================================================================================
81 //-- Fat16 fixed cache
82 //-- FAT_FAT16FixedCache <Log2(read granularity)>,<Log2(Write granularity)>
83 //-- e.g: FAT_FAT16FixedCache 12,9
84 _LIT8(KPN_FAT_FAT16FixedCache, "FAT_FAT16FixedCache");
85 static const TUint32 KDef_FAT16_FixedCache_Read_Granularity_LOG2 = 12; //-- read granularity Log2, 4K
86 static const TUint32 KDef_FAT16_FixedCache_Write_Granularity_LOG2 = 9; //-- write granularity Log2, 512 bytes
90 //-- FAT_FAT32LruCache <Size KB>,<Log2(read granularity)>,<Log2(Write granularity)>
91 //-- e.g: FAT_FAT32LruCache 128,12,9
92 _LIT8(KPN_FAT_FAT32LruCache, "FAT_FAT32LruCache");
95 static const TUint32 KDef_FAT32_LruCache_SizeKB = 128; //-- LRU cache size, 128KB
96 static const TUint32 KDef_FAT32_LruCache_Read_Granularity_LOG2 = 12; //-- read granularity Log2, 4K
97 static const TUint32 KDef_FAT32_LruCache_Write_Granularity_LOG2 = 9; //-- write granularity Log2, 512 bytes
100 //=======================================================================================================================
101 //-- FAT leaf directory cache settings
102 //=======================================================================================================================
103 //-- A leaf directory cache for Fat volumes
104 _LIT8(KPN_FAT_LeafDirCache, "FAT_LeafDirCacheSize");
105 static const TUint32 KDef_KLeafDirCacheSize = 1; //-- default number of the most recently visited leaf dirs to be cached
107 //=======================================================================================================================
108 //-- New directory cache settings
109 //=======================================================================================================================
110 //-- New directory cache uses the global cache memory manager for dynamic size allocation
111 _LIT8(KPN_FAT_DynamicDirCacheMin, "FAT_DirCacheSizeMin");
112 _LIT8(KPN_FAT_DynamicDirCacheMax, "FAT_DirCacheSizeMax");
113 static const TUint32 KDef_DynamicDirCacheMin = 128; // default minimum fat dir cache size in KB
114 static const TUint32 KDef_DynamicDirCacheMax = 256; // default maximum fat dir cache size in KB
115 static const TUint32 KDef_MaxDynamicDirCachePageSzLog2 = 14; // default value for directory cache single page
116 // maximal size Log2, 2^14 (16K) by default
125 //########################################################################################################################
129 //-----------------------------------------------------------------------------
131 Extract a token from the comma-separated tokens string.
133 @param aSrc source string descriptor
134 @param aTokenNo token number to extract (the 1st token is number 0)
135 @param aToken on success there will be extracted token
137 @return ETrue on success and an extracted token in aToken
139 static TBool GetCSV_Token(const TDesC8& aSrc, TUint aTokenNo, TPtrC8& aToken)
142 const TChar chDelim = ','; //-- token delimiter, comma
145 //-- 1. find the beginning of the token we are looking for
146 TPtrC8 ptrCurrToken(aSrc);
147 for(TUint i=0; i<aTokenNo; ++i)
149 const TInt len = ptrCurrToken.Length();
151 nRes = ptrCurrToken.Locate(chDelim);
152 if(nRes == KErrNotFound)
153 return EFalse; //-- did tot find the required token
155 ptrCurrToken.Set(ptrCurrToken.Right(len-nRes-1));
158 //-- 2. set the end position of the token we found
159 aToken.Set(ptrCurrToken);
161 nRes = ptrCurrToken.Locate(chDelim);
162 if(nRes != KErrNotFound)
164 aToken.Set(ptrCurrToken.Left(nRes));
171 //-----------------------------------------------------------------------------
173 Extract a token from the comma-separated tokens string and try to convert it into TInt
175 @param aSrc source string descriptor
176 @param aTokenNo token number to extract (the 1st token is number 0)
177 @param aVal On success will contain the value represented by the token
179 @return ETrue if the token is extracted and converted to TInt OK, in this case aVal contains value
181 static TBool GetIntTokenVal(const TDesC8& aSrc, TUint aTokenNo, TInt& aVal)
187 if(!GetCSV_Token(aSrc, aTokenNo, ptrToken))
190 lex.Assign(ptrToken);
193 if(lex.Val(val) != KErrNone)
200 static TBool GetIntTokenVal(const TDesC8& aSrc, TUint aTokenNo, TUint16& aVal)
203 if(!GetIntTokenVal(aSrc, aTokenNo, val))
211 //-----------------------------------------------------------------------------
212 TFatConfig::TFatConfig()
214 iInitialised = EFalse;
217 //-----------------------------------------------------------------------------
220 reads FAT parameters from estart.txt file.
221 @param aDrvNumber a valid drive number
222 @param aForceRead if ETrue parameters will be forcedly re-read.
224 void TFatConfig::ReadConfig(TInt aDrvNumber, TBool aForceRead /*=EFalse*/)
227 iInitialised = EFalse;
232 //__PRINT1(_L("#>- TFatConfig::ReadConfig() drive:%d\n"),aDrvNumber);
233 ASSERT(aDrvNumber >= EDriveA && aDrvNumber <= EDriveZ);
235 //-- make a section name, like "DriveX"
237 section.Format(_L8("Drive%c"), 'A'+aDrvNumber);
239 //-- read values from estart.txt and cache them
240 iScanDrvSkipFinalisedVolume = ReadUint(section, KPN_ScanDrvSkipFinalisedVolume, KDef_ScanDrvSkipFinalisedVolume);
241 iFAT32_AsynchMount = ReadUint(section, KPN_FAT32_AsynchMount, KDef_FAT32_AsynchMount);
242 iFAT32_UseFSInfoOnMount = ReadUint(section, KPN_FAT32_UseFsInfoOnMount, KDef_FAT32_UseFsInfoOnMount);
243 iFAT32_UseBitSupercache = ReadUint(section, KPN_FAT32_UseFatBitSupercache, KDef_FAT32_UseFatBitSupercache);
244 iFAT16_UseCleanShutDownBit = ReadUint(section, KPN_FAT16_UseCleanShutDownBit, KDef_FAT16_UseCleanShutDownBit);
246 // If leaf dir cache is supported, read the configuration from estart.txt file
247 iLeafDirCacheSize = ReadUint(section, KPN_FAT_LeafDirCache, KDef_KLeafDirCacheSize);
248 ProcessDynamicDirCacheParams(section); //-- read dynamic dir cache parameters;
250 ProcessDirCacheParams(section); //-- read FAT directory cache parameters
251 ProcessFatCacheParams(section); //-- read FAT cache parameters
253 iInitialised = ETrue;
255 DumpParameters(); //-- print out parameters in debug mode
259 //-----------------------------------------------------------------------------
261 process dynamic directory cache parameters
262 @param aSection section name, like "DriveX"
264 void TFatConfig::ProcessDynamicDirCacheParams(const TDesC8& aSection)
266 // we have to process the data in this file as the default values are all static variables, which means
267 // their scope is limited within this file only.
268 iDynamicDirCacheMaxPageSizeLog2 = KDef_MaxDynamicDirCachePageSzLog2;
269 iDynamicDirCacheSizeMinKB = ReadUint(aSection, KPN_FAT_DynamicDirCacheMin, KDef_DynamicDirCacheMin);
270 iDynamicDirCacheSizeMaxKB = ReadUint(aSection, KPN_FAT_DynamicDirCacheMax, KDef_DynamicDirCacheMax);
272 // if less than default values, set to default values
273 if (iDynamicDirCacheSizeMinKB < KDef_DynamicDirCacheMin)
274 iDynamicDirCacheSizeMinKB = KDef_DynamicDirCacheMin;
275 if (iDynamicDirCacheSizeMaxKB < KDef_DynamicDirCacheMax)
276 iDynamicDirCacheSizeMaxKB = KDef_DynamicDirCacheMax;
278 // validate settings for those values that the default values does not apply onto them
279 __ASSERT_ALWAYS(iDynamicDirCacheSizeMinKB <= iDynamicDirCacheSizeMaxKB, Fault(EFatBadParameter));
283 process directory cache parameters
284 @param aSection section name, like "DriveX"
286 void TFatConfig::ProcessDirCacheParams(const TDesC8& aSection)
290 //-- set default values.
291 iDirCacheSizeKB = KDef_KDirCacheSizeKB;
292 iDirCacheMaxPageSizeLog2 = KDef_MaxDirCachePageSzLog2;
294 //-- read a string containing DirCache parameters
295 //-- it looks like this: FAT_DirCache cacheSzKB, maxPageSzBytes
296 if(!F32Properties::GetString(aSection, KPN_FAT_DirCache, buf))
299 GetIntTokenVal(buf, 0, iDirCacheSizeKB); //-- 1. extract directory cache size in KB. this is token 0
300 GetIntTokenVal(buf, 1, iDirCacheMaxPageSizeLog2); //-- 2. extract directory cache max page size in bytes Log2. this is token 1
303 //-----------------------------------------------------------------------------
305 process fat cache parameters
306 @param aSection section name, like "DriveX"
308 void TFatConfig::ProcessFatCacheParams(const TDesC8& aSection)
311 //-- set default values.
312 iFat16FixedCacheReadGrLog2 = KDef_FAT16_FixedCache_Read_Granularity_LOG2;
313 iFat16FixedCacheWriteGrLog2 = KDef_FAT16_FixedCache_Write_Granularity_LOG2;
315 iFat32LRUCacheSizeKB = KDef_FAT32_LruCache_SizeKB;
316 iFat32LRUCacheReadGrLog2 = KDef_FAT32_LruCache_Read_Granularity_LOG2;
317 iFat32LRUCacheWriteGrLog2 = KDef_FAT32_LruCache_Write_Granularity_LOG2;
319 //-- read a string containing FAT16 fixed cache parameters
320 if(F32Properties::GetString(aSection, KPN_FAT_FAT16FixedCache, buf))
322 GetIntTokenVal(buf, 0, iFat16FixedCacheReadGrLog2); //-- 1. extract Log2(FAT16 fixed cache read granularity)
323 GetIntTokenVal(buf, 1, iFat16FixedCacheWriteGrLog2); //-- 2. extract Log2(FAT16 fixed cache write granularity)
326 //-- read a string containing FAT32 LRU cache parameters
327 if(F32Properties::GetString(aSection, KPN_FAT_FAT32LruCache, buf))
329 GetIntTokenVal(buf, 0, iFat32LRUCacheSizeKB); //-- 1. extract FAT32 LRU cache size, Kbytes
330 GetIntTokenVal(buf, 1, iFat32LRUCacheReadGrLog2); //-- 2. extract Log2(FAT32 LRU cache read granularity)
331 GetIntTokenVal(buf, 2, iFat32LRUCacheWriteGrLog2); //-- 3. extract Log2(FAT32 LRU cache write granularity)
336 //-----------------------------------------------------------------------------
338 Just a helper method. Reads TUint32 value from the estart.txt
340 @param aSection section name, e.g. [DriveD]
341 @param aKeyName Key name
342 @param aDefaultValue Default value to return in parameter isn't found
343 @return a value from config file or default aDefaultValue
345 TUint32 TFatConfig::ReadUint(const TDesC8& aSection, const TDesC8& aKeyName, TUint32 aDefaultValue) const
347 TInt32 val = aDefaultValue;
348 // coverity[check_return] coverity[unchecked_value]
349 F32Properties::GetInt(aSection, aKeyName, val);
354 //-----------------------------------------------------------------------------
355 /** Debug method, prints out the parameters*/
356 void TFatConfig::DumpParameters() const
360 ASSERT(iInitialised);
361 __PRINT(_L("#>- TFatConfig parameters:\n"));
363 DoDumpUintParam(KPN_ScanDrvSkipFinalisedVolume, iScanDrvSkipFinalisedVolume);
364 DoDumpUintParam(KPN_FAT32_AsynchMount, iFAT32_AsynchMount);
365 DoDumpUintParam(KPN_FAT32_UseFsInfoOnMount, iFAT32_UseFSInfoOnMount);
366 DoDumpUintParam(KPN_FAT32_UseFatBitSupercache, iFAT32_UseBitSupercache);
367 DoDumpUintParam(KPN_FAT16_UseCleanShutDownBit, iFAT16_UseCleanShutDownBit);
369 DoDumpUintParam(_L8("FAT_DirCache Size, KB"), iDirCacheSizeKB);
370 DoDumpUintParam(_L8("FAT_DirCache MaxPage Size Log2"), iDirCacheMaxPageSizeLog2);
372 DoDumpUintParam(_L8("FAT_16Cache RdGr Log2"), iFat16FixedCacheReadGrLog2);
373 DoDumpUintParam(_L8("FAT_16Cache WrGr Log2"), iFat16FixedCacheWriteGrLog2);
375 DoDumpUintParam(_L8("FAT_32Cache Size, KB"), iFat32LRUCacheSizeKB);
376 DoDumpUintParam(_L8("FAT_32Cache RdGr Log2"), iFat32LRUCacheReadGrLog2);
377 DoDumpUintParam(_L8("FAT_32Cache WrGr Log2"), iFat32LRUCacheWriteGrLog2);
379 __PRINT(_L("#>------ end -------<#\n\n"));
384 void TFatConfig::DoDumpUintParam(const TDesC8& aKeyName, TUint32 aParam) const
390 buf.Insert(0,_L("#>- "));
391 buf.AppendFormat(_L(": %d"), aParam);
394 (void)aKeyName; (void)aParam; //-- get rid of warning