userlibandfileserver/fileserver/sfat/fat_config.cpp
changeset 9 96e5fb8b040d
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    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
       
    17 // 
       
    18 //
       
    19 
       
    20 /**
       
    21  @file
       
    22 */
       
    23 
       
    24 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    25 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    26 //!!
       
    27 //!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
       
    28 //!!
       
    29 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    30 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    31 
       
    32 #include <f32fsys.h>
       
    33 
       
    34 #include "common_constants.h"
       
    35 #include "sl_std.h"
       
    36 #include "fat_config.h"
       
    37 
       
    38 //########################################################################################################################
       
    39 //-- parameters that can be defined in estart.txt file under [Drive%] section
       
    40 
       
    41 //=======================================================================================================================
       
    42 //-- FAT volumes mounting related settings
       
    43 //=======================================================================================================================
       
    44 
       
    45 //-- if this parameter is not 0, ScanDrive will skip finalised volume. Applicable for FAT16/32
       
    46 //-- e.g:   FAT_ScanDrv_SkipFinalisedVolume 1
       
    47 _LIT8(KPN_ScanDrvSkipFinalisedVolume,"FAT_ScanDrv_SkipFinalisedVolume");  //-- parameter key name
       
    48 static const TUint32 KDef_ScanDrvSkipFinalisedVolume = 0; //-- parameter default value
       
    49 
       
    50 //-- if this parameter is not 0, enables asynchronous mounting FAT32 volumes. 
       
    51 //-- At present it means enabling/disabling FAT32 background scanning for free clusters. Applicable for FAT32 only
       
    52 //-- e.g:   FAT_FAT32_AsynchMount=0
       
    53 _LIT8(KPN_FAT32_AsynchMount, "FAT_FAT32_AsynchMount"); 
       
    54 static const TUint32 KDef_FAT32_AsynchMount = 1;
       
    55 
       
    56 //-- Using FSInfo sector data during FAT32 mount to find out number of free clusters etc.
       
    57 //-- e.g:   FAT_FAT32_UseFsInfo=1
       
    58 _LIT8(KPN_FAT32_UseFsInfoOnMount, "FAT_FAT32_UseFsInfo"); 
       
    59 static const TUint32 KDef_FAT32_UseFsInfoOnMount = 1;
       
    60 
       
    61 //-- Using FAT bit supercache for fast free cluster lookup
       
    62 //-- e.g:   FAT_FAT32_UseBitSupercache 0
       
    63 _LIT8(KPN_FAT32_UseFatBitSupercache, "FAT_FAT32_UseBitSupercache"); 
       
    64 static const TUint32 KDef_FAT32_UseFatBitSupercache = 1;
       
    65 
       
    66 
       
    67 //-- if this parameter is not 0, "clean shutdown mask" bit in FAT16[1] will be used during volume finalisation.
       
    68 //-- Otherwise, FAT16[1] will not be affected during finalisation
       
    69 //-- e.g:   KDef_FAT16_UseCleanShutDownBit 0
       
    70 _LIT8(KPN_FAT16_UseCleanShutDownBit,"FAT16_UseCleanShutDownBit");  //-- parameter key name
       
    71 static const TUint32 KDef_FAT16_UseCleanShutDownBit = 1;           //-- parameter default value
       
    72 
       
    73 
       
    74 //=======================================================================================================================
       
    75 //-- FAT directory cache settings
       
    76 //=======================================================================================================================
       
    77 //-- FAT_DirCache <CacheSizeKB>,<Log2(max page size)>
       
    78 //-- e.g:   FAT_DirCache 16,12
       
    79 _LIT8(KPN_FAT_DirCache, "FAT_DirCache"); 
       
    80 
       
    81 static const TUint32 KDef_KDirCacheSizeKB = 16;      //-- default value for the total directory cache size, kilobytes. 16K by default
       
    82 static const TUint32 KDef_MaxDirCachePageSzLog2 = 12;//-- default value for directory cache single page maximal size Log2, 2^12 (4K) by default
       
    83 
       
    84 
       
    85 //=======================================================================================================================
       
    86 //-- FAT cache settings
       
    87 //=======================================================================================================================
       
    88 
       
    89 //-- Fat16 fixed cache
       
    90 //-- FAT_FAT16FixedCache <Log2(read granularity)>,<Log2(Write granularity)> 
       
    91 //-- e.g:   FAT_FAT16FixedCache 12,9 
       
    92 _LIT8(KPN_FAT_FAT16FixedCache, "FAT_FAT16FixedCache"); 
       
    93 static const TUint32 KDef_FAT16_FixedCache_Read_Granularity_LOG2 = 12;  //-- read granularity  Log2, 4K
       
    94 static const TUint32 KDef_FAT16_FixedCache_Write_Granularity_LOG2 = 9;  //-- write granularity Log2, 512 bytes    
       
    95 
       
    96 
       
    97 //-- Fat32 LRU cache
       
    98 //-- FAT_FAT32LruCache <Size KB>,<Log2(read granularity)>,<Log2(Write granularity)> 
       
    99 //-- e.g:   FAT_FAT32LruCache 128,12,9
       
   100 _LIT8(KPN_FAT_FAT32LruCache, "FAT_FAT32LruCache"); 
       
   101 
       
   102 
       
   103 static const TUint32 KDef_FAT32_LruCache_SizeKB = 128;                  //-- LRU cache size, 128KB
       
   104 static const TUint32 KDef_FAT32_LruCache_Read_Granularity_LOG2 = 12;    //-- read granularity  Log2, 4K     
       
   105 static const TUint32 KDef_FAT32_LruCache_Write_Granularity_LOG2 = 9;    //-- write granularity Log2, 512 bytes      
       
   106 
       
   107 
       
   108 //=======================================================================================================================
       
   109 //-- FAT leaf directory cache settings
       
   110 //=======================================================================================================================
       
   111 //-- A leaf directory cache for Fat volumes
       
   112 _LIT8(KPN_FAT_LeafDirCache, "FAT_LeafDirCacheSize"); 
       
   113 static const TUint32 KDef_KLeafDirCacheSize = 1;    //-- default number of the most recently visited leaf dirs to be cached
       
   114 
       
   115 //=======================================================================================================================
       
   116 //-- New directory cache settings
       
   117 //=======================================================================================================================
       
   118 //-- New directory cache uses the global cache memory manager for dynamic size allocation
       
   119 _LIT8(KPN_FAT_DynamicDirCacheMin, "FAT_DirCacheSizeMin"); 
       
   120 _LIT8(KPN_FAT_DynamicDirCacheMax, "FAT_DirCacheSizeMax"); 
       
   121 static const TUint32 KDef_DynamicDirCacheMin = 128;     // default minimum fat dir cache size in KB
       
   122 static const TUint32 KDef_DynamicDirCacheMax = 128;     // default maximum fat dir cache size in KB
       
   123 //-- default value for directory cache single page maximal size Log2, 2^14 (16K) by default
       
   124 static const TUint32 KDef_MaxDynamicDirCachePageSzLog2 = 14;
       
   125 
       
   126 
       
   127 
       
   128 
       
   129 
       
   130 
       
   131 
       
   132 
       
   133 //########################################################################################################################
       
   134 
       
   135 
       
   136 
       
   137 //-----------------------------------------------------------------------------
       
   138 /**
       
   139     Extract a token from the comma-separated tokens string.
       
   140 
       
   141     @param  aSrc        source string descriptor
       
   142     @param  aTokenNo    token number to extract (the 1st token is number 0)
       
   143     @param  aToken      on success there will be extracted token
       
   144     
       
   145     @return ETrue on success and an extracted token in aToken
       
   146 */
       
   147 static TBool GetCSV_Token(const TDesC8& aSrc, TUint aTokenNo, TPtrC8& aToken)
       
   148     {
       
   149 
       
   150     const TChar chDelim = ','; //-- token delimiter, comma
       
   151     TInt nRes;
       
   152     
       
   153     //-- 1. find the beginning of the token we are looking for
       
   154     TPtrC8 ptrCurrToken(aSrc);
       
   155     for(TUint i=0; i<aTokenNo; ++i)
       
   156         {
       
   157         const TInt len = ptrCurrToken.Length();
       
   158 
       
   159         nRes = ptrCurrToken.Locate(chDelim);
       
   160         if(nRes == KErrNotFound)
       
   161             return EFalse; //-- did tot find the required token
       
   162 
       
   163         ptrCurrToken.Set(ptrCurrToken.Right(len-nRes-1));
       
   164         }
       
   165 
       
   166     //-- 2. set the end position of the token we found
       
   167     aToken.Set(ptrCurrToken);
       
   168 
       
   169     nRes = ptrCurrToken.Locate(chDelim);
       
   170     if(nRes != KErrNotFound)
       
   171         {
       
   172         aToken.Set(ptrCurrToken.Left(nRes));
       
   173         }
       
   174 
       
   175     return ETrue;
       
   176     }
       
   177 
       
   178 
       
   179 //-----------------------------------------------------------------------------
       
   180 /**
       
   181     Extract a token from the comma-separated tokens string and try to convert it into TInt
       
   182 
       
   183     @param  aSrc        source string descriptor
       
   184     @param  aTokenNo    token number to extract (the 1st token is number 0)
       
   185     @param  aVal        On success will contain the value represented by the token
       
   186 
       
   187     @return ETrue if the token is extracted and converted to TInt OK, in this case aVal contains value
       
   188 */
       
   189 static TBool GetIntTokenVal(const TDesC8& aSrc, TUint aTokenNo, TInt& aVal)
       
   190 {
       
   191     TPtrC8      ptrToken;
       
   192     TLex8       lex;
       
   193     TInt        val;
       
   194 
       
   195     if(!GetCSV_Token(aSrc, aTokenNo, ptrToken))
       
   196         return EFalse;
       
   197 
       
   198     lex.Assign(ptrToken);
       
   199     lex.SkipSpace();
       
   200     
       
   201     if(lex.Val(val) != KErrNone)
       
   202         return EFalse;
       
   203 
       
   204     aVal = val;
       
   205     return ETrue;
       
   206 }
       
   207 
       
   208 static TBool GetIntTokenVal(const TDesC8& aSrc, TUint aTokenNo, TUint16& aVal)
       
   209 {
       
   210     TInt  val;
       
   211     if(!GetIntTokenVal(aSrc, aTokenNo, val))
       
   212         return EFalse;
       
   213 
       
   214     aVal = (TUint16)val;
       
   215     return ETrue;
       
   216 }
       
   217 
       
   218 
       
   219 //-----------------------------------------------------------------------------
       
   220 TFatConfig::TFatConfig()
       
   221     {
       
   222     iInitialised = EFalse;
       
   223     }
       
   224 
       
   225 //-----------------------------------------------------------------------------
       
   226 
       
   227 /**
       
   228     reads FAT parameters from estart.txt file.
       
   229     @param  aDrvNumber a valid drive number
       
   230     @param  aForceRead if ETrue parameters will be forcedly re-read.
       
   231 */
       
   232 void TFatConfig::ReadConfig(TInt aDrvNumber, TBool aForceRead /*=EFalse*/)
       
   233     {
       
   234     if(aForceRead)
       
   235         iInitialised = EFalse;
       
   236 
       
   237     if(iInitialised)
       
   238         return;
       
   239 
       
   240     //__PRINT1(_L("#>- TFatConfig::ReadConfig() drive:%d\n"),aDrvNumber);
       
   241     ASSERT(aDrvNumber >= EDriveA && aDrvNumber <= EDriveZ);
       
   242     
       
   243     //-- make a section name, like "DriveX"
       
   244     TBuf8<32> section;
       
   245     section.Format(_L8("Drive%c"), 'A'+aDrvNumber);
       
   246     
       
   247     //-- read values from estart.txt and cache them
       
   248     iScanDrvSkipFinalisedVolume = ReadUint(section, KPN_ScanDrvSkipFinalisedVolume, KDef_ScanDrvSkipFinalisedVolume);
       
   249     iFAT32_AsynchMount          = ReadUint(section, KPN_FAT32_AsynchMount,          KDef_FAT32_AsynchMount);
       
   250     iFAT32_UseFSInfoOnMount     = ReadUint(section, KPN_FAT32_UseFsInfoOnMount,     KDef_FAT32_UseFsInfoOnMount);
       
   251     iFAT32_UseBitSupercache     = ReadUint(section, KPN_FAT32_UseFatBitSupercache,  KDef_FAT32_UseFatBitSupercache);
       
   252     iFAT16_UseCleanShutDownBit  = ReadUint(section, KPN_FAT16_UseCleanShutDownBit, KDef_FAT16_UseCleanShutDownBit);
       
   253 
       
   254     // If leaf dir cache is supported, read the configuration from estart.txt file
       
   255     iLeafDirCacheSize           = ReadUint(section, KPN_FAT_LeafDirCache,           KDef_KLeafDirCacheSize);
       
   256     ProcessDynamicDirCacheParams(section);  //-- read dynamic dir cache parameters;
       
   257 
       
   258     ProcessDirCacheParams(section); //-- read FAT directory cache parameters
       
   259     ProcessFatCacheParams(section); //-- read FAT cache parameters
       
   260 
       
   261     iInitialised = ETrue;
       
   262 
       
   263     DumpParameters(); //-- print out parameters in debug mode
       
   264     }
       
   265 
       
   266 
       
   267 //-----------------------------------------------------------------------------
       
   268 /** 
       
   269     process dynamic directory cache parameters 
       
   270     @param aSection section name, like "DriveX"
       
   271 */
       
   272 void TFatConfig::ProcessDynamicDirCacheParams(const TDesC8& aSection)
       
   273     {
       
   274     // we have to process the data in this file as the default values are all static variables, which means
       
   275     //  their scope is limited within this file only.
       
   276     iDynamicDirCacheMaxPageSizeLog2 = KDef_MaxDynamicDirCachePageSzLog2; 
       
   277     iDynamicDirCacheSizeMinKB = ReadUint(aSection, KPN_FAT_DynamicDirCacheMin, KDef_DynamicDirCacheMin);
       
   278     iDynamicDirCacheSizeMaxKB = ReadUint(aSection, KPN_FAT_DynamicDirCacheMax, KDef_DynamicDirCacheMax);
       
   279 
       
   280     // if less than default values, set to default values
       
   281     if (iDynamicDirCacheSizeMinKB < KDef_DynamicDirCacheMin)
       
   282         iDynamicDirCacheSizeMinKB = KDef_DynamicDirCacheMin;
       
   283     if (iDynamicDirCacheSizeMaxKB < KDef_DynamicDirCacheMax)
       
   284         iDynamicDirCacheSizeMaxKB = KDef_DynamicDirCacheMax;
       
   285 
       
   286     // validate settings for those values that the default values does not apply onto them
       
   287     __ASSERT_ALWAYS(iDynamicDirCacheSizeMinKB <= iDynamicDirCacheSizeMaxKB, Fault(EFatBadParameter));
       
   288     }
       
   289 
       
   290 /** 
       
   291     process directory cache parameters 
       
   292     @param aSection section name, like "DriveX"
       
   293 */
       
   294 void TFatConfig::ProcessDirCacheParams(const TDesC8& aSection)
       
   295     {
       
   296     TBuf8<128>  buf;
       
   297 
       
   298     //-- set default values.
       
   299     iDirCacheSizeKB = KDef_KDirCacheSizeKB;
       
   300     iDirCacheMaxPageSizeLog2 = KDef_MaxDirCachePageSzLog2;
       
   301 
       
   302     //-- read a string containing DirCache parameters
       
   303     //-- it looks like this: FAT_DirCache cacheSzKB, maxPageSzBytes
       
   304     if(!F32Properties::GetString(aSection, KPN_FAT_DirCache, buf))
       
   305         return;
       
   306     
       
   307     GetIntTokenVal(buf, 0, iDirCacheSizeKB);          //-- 1. extract directory cache size in KB. this is token 0
       
   308     GetIntTokenVal(buf, 1, iDirCacheMaxPageSizeLog2); //-- 2. extract directory cache max page size in bytes Log2. this is token 1  
       
   309     }
       
   310 
       
   311 //-----------------------------------------------------------------------------
       
   312 /** 
       
   313     process fat cache parameters 
       
   314     @param aSection section name, like "DriveX"
       
   315 */
       
   316 void TFatConfig::ProcessFatCacheParams(const TDesC8& aSection)
       
   317 {
       
   318     TBuf8<128>  buf;
       
   319     //-- set default values.
       
   320     iFat16FixedCacheReadGrLog2 = KDef_FAT16_FixedCache_Read_Granularity_LOG2;
       
   321     iFat16FixedCacheWriteGrLog2 = KDef_FAT16_FixedCache_Write_Granularity_LOG2;
       
   322 
       
   323     iFat32LRUCacheSizeKB = KDef_FAT32_LruCache_SizeKB;
       
   324     iFat32LRUCacheReadGrLog2 = KDef_FAT32_LruCache_Read_Granularity_LOG2;
       
   325     iFat32LRUCacheWriteGrLog2 = KDef_FAT32_LruCache_Write_Granularity_LOG2;
       
   326 
       
   327     //-- read a string containing FAT16 fixed cache parameters
       
   328     if(F32Properties::GetString(aSection, KPN_FAT_FAT16FixedCache, buf))
       
   329     {
       
   330         GetIntTokenVal(buf, 0, iFat16FixedCacheReadGrLog2);  //-- 1. extract Log2(FAT16 fixed cache read granularity)
       
   331         GetIntTokenVal(buf, 1, iFat16FixedCacheWriteGrLog2); //-- 2. extract Log2(FAT16 fixed cache write granularity)
       
   332     }
       
   333 
       
   334     //-- read a string containing FAT32 LRU cache parameters
       
   335     if(F32Properties::GetString(aSection, KPN_FAT_FAT32LruCache, buf))
       
   336     {
       
   337         GetIntTokenVal(buf, 0, iFat32LRUCacheSizeKB);        //-- 1. extract FAT32 LRU cache size, Kbytes
       
   338         GetIntTokenVal(buf, 1, iFat32LRUCacheReadGrLog2);    //-- 2. extract Log2(FAT32 LRU cache read granularity)
       
   339         GetIntTokenVal(buf, 2, iFat32LRUCacheWriteGrLog2);   //-- 3. extract Log2(FAT32 LRU cache write granularity)
       
   340     }
       
   341 }
       
   342 
       
   343 
       
   344 //-----------------------------------------------------------------------------
       
   345 /**
       
   346     Just a helper method. Reads TUint32 value from the estart.txt
       
   347     
       
   348     @param  aSection        section name, e.g. [DriveD]
       
   349     @param  aKeyName        Key name
       
   350     @param  aDefaultValue   Default value to return in parameter isn't found
       
   351     @return a value from config file or default aDefaultValue
       
   352 */
       
   353 TUint32 TFatConfig::ReadUint(const TDesC8& aSection, const TDesC8& aKeyName, TUint32 aDefaultValue) const
       
   354     {
       
   355     TInt32 val = aDefaultValue;
       
   356     // coverity[check_return] coverity[unchecked_value]
       
   357     F32Properties::GetInt(aSection, aKeyName, val);
       
   358 
       
   359     return val;
       
   360     }
       
   361 
       
   362 //-----------------------------------------------------------------------------
       
   363 /** Debug method, prints out the parameters*/
       
   364 void TFatConfig::DumpParameters() const
       
   365     {
       
   366 #ifdef _DEBUG
       
   367 
       
   368     ASSERT(iInitialised);
       
   369     __PRINT(_L("#>- TFatConfig parameters:\n"));
       
   370 
       
   371     DoDumpUintParam(KPN_ScanDrvSkipFinalisedVolume, iScanDrvSkipFinalisedVolume);
       
   372     DoDumpUintParam(KPN_FAT32_AsynchMount, iFAT32_AsynchMount);
       
   373     DoDumpUintParam(KPN_FAT32_UseFsInfoOnMount, iFAT32_UseFSInfoOnMount);
       
   374     DoDumpUintParam(KPN_FAT32_UseFatBitSupercache, iFAT32_UseBitSupercache);
       
   375     DoDumpUintParam(KPN_FAT16_UseCleanShutDownBit, iFAT16_UseCleanShutDownBit);
       
   376 
       
   377     DoDumpUintParam(_L8("FAT_DirCache Size, KB"), iDirCacheSizeKB);
       
   378     DoDumpUintParam(_L8("FAT_DirCache MaxPage Size Log2"), iDirCacheMaxPageSizeLog2);
       
   379 
       
   380     DoDumpUintParam(_L8("FAT_16Cache RdGr Log2"), iFat16FixedCacheReadGrLog2);
       
   381     DoDumpUintParam(_L8("FAT_16Cache WrGr Log2"), iFat16FixedCacheWriteGrLog2);
       
   382 
       
   383     DoDumpUintParam(_L8("FAT_32Cache Size, KB"),  iFat32LRUCacheSizeKB);
       
   384     DoDumpUintParam(_L8("FAT_32Cache RdGr Log2"), iFat32LRUCacheReadGrLog2);
       
   385     DoDumpUintParam(_L8("FAT_32Cache WrGr Log2"), iFat32LRUCacheWriteGrLog2);
       
   386 
       
   387     __PRINT(_L("#>------ end -------<#\n\n"));
       
   388 
       
   389 #endif
       
   390     }
       
   391 
       
   392 void TFatConfig::DoDumpUintParam(const TDesC8& aKeyName, TUint32 aParam) const
       
   393     {
       
   394 #ifdef _DEBUG
       
   395     TBuf<100> buf;
       
   396 
       
   397     buf.Copy(aKeyName);
       
   398     buf.Insert(0,_L("#>-  "));
       
   399     buf.AppendFormat(_L(": %d"), aParam);
       
   400     __PRINT(buf);
       
   401 #else
       
   402     (void)aKeyName; (void)aParam; //-- get rid of warning
       
   403 
       
   404 #endif
       
   405     }
       
   406 
       
   407 
       
   408 
       
   409 
       
   410 
       
   411 
       
   412 
       
   413 
       
   414 
       
   415 
       
   416 
       
   417 
       
   418 
       
   419 
       
   420 
       
   421 
       
   422 
       
   423 
       
   424 
       
   425 
       
   426 
       
   427 
       
   428 
       
   429 
       
   430 
       
   431