author | hgs |
Mon, 04 Oct 2010 12:15:59 +0100 | |
changeset 280 | 2bfb1feef9de |
parent 271 | dc268b18d709 |
child 291 | 206a6eaaeb71 |
permissions | -rw-r--r-- |
0 | 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\sl_fatmisc32.cpp |
|
15 |
// |
|
16 |
// |
|
17 |
||
18 |
#include "sl_std.h" |
|
19 |
#include "sl_cache.h" |
|
20 |
||
269 | 21 |
//------------------------------------------------------------------------------------------------------------------- |
0 | 22 |
/** |
23 |
Calculate the FAT size in sectors for a Fat32 volume |
|
24 |
||
25 |
@return The number of sectors |
|
26 |
*/ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
27 |
TUint CFatFormatCB::MaxFat32Sectors() const |
0 | 28 |
{ |
29 |
TUint32 calc1 = iMaxDiskSectors - iReservedSectors; |
|
30 |
TUint32 calc2 = (256 * iSectorsPerCluster) + iNumberOfFats; |
|
31 |
calc2 = calc2 >> 1; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
32 |
return ((calc1 + (calc2 - 1))/calc2); |
0 | 33 |
} |
34 |
||
35 |
||
36 |
const TUint KDefFatResvdSec = 1; ///< default number of FAT12/16 reserved sectors |
|
37 |
const TUint KDefFat32ResvdSec = 32; ///< default number of FAT32 reserved sectors |
|
38 |
||
39 |
//------------------------------------------------------------------------------------------------------------------- |
|
40 |
void Dump_TLDFormatInfo(const TLDFormatInfo& aInfo) |
|
41 |
{ |
|
42 |
(void)aInfo; |
|
43 |
#ifdef _DEBUG |
|
44 |
__PRINT(_L("----- TLDFormatInfo dump:")); |
|
45 |
__PRINT1(_L("iCapacity:%d"), aInfo.iCapacity); |
|
46 |
__PRINT1(_L("iSectorsPerCluster:%d"), aInfo.iSectorsPerCluster); |
|
47 |
__PRINT1(_L("iSectorsPerTrack:%d"), aInfo.iSectorsPerTrack); |
|
48 |
__PRINT1(_L("iFATBits:%d"), aInfo.iFATBits); |
|
49 |
__PRINT1(_L("iReservedSectors:%d"), aInfo.iReservedSectors); |
|
50 |
__PRINT1(_L("iFlags:%d"), aInfo.iFlags); |
|
51 |
__PRINT(_L("-----")); |
|
52 |
#endif |
|
53 |
} |
|
54 |
||
55 |
//------------------------------------------------------------------------------------------------------------------- |
|
56 |
||
57 |
TInt CFatFormatCB::FirstDataSector() const |
|
58 |
{ |
|
59 |
TInt rootDirSectors = (iRootDirEntries * KSizeOfFatDirEntry + (iBytesPerSector-1)) / iBytesPerSector; |
|
60 |
return iHiddenSectors + iReservedSectors + iNumberOfFats*iSectorsPerFat + rootDirSectors; |
|
61 |
} |
|
62 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
63 |
void CFatFormatCB::AdjustClusterSize(TUint aRecommendedSectorsPerCluster) |
0 | 64 |
{ |
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
const TUint KMaxSecPerCluster = 64; // 32K |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
|
0 | 67 |
while (aRecommendedSectorsPerCluster > iSectorsPerCluster && iSectorsPerCluster <= (KMaxSecPerCluster/2)) |
68 |
iSectorsPerCluster<<= 1; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
|
0 | 70 |
} |
71 |
||
72 |
// AdjustFirstDataSectorAlignment() |
|
73 |
// Attempts to align the first data sector on an erase block boundary by modifying the |
|
74 |
// number of reserved sectors. |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
75 |
TInt CFatFormatCB::AdjustFirstDataSectorAlignment(TUint aEraseBlockSizeInSectors) |
0 | 76 |
{ |
77 |
const TBool bFat16 = Is16BitFat(); |
|
78 |
const TBool bFat32 = Is32BitFat(); |
|
79 |
||
80 |
// Save these 2 values in the event of a convergence failure; this should |
|
81 |
// hopefully never happen, but we will cater for this in release mode to be safe, |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
TUint reservedSectorsSaved = iReservedSectors; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
TUint sectorsPerFatSaved = iSectorsPerFat; |
0 | 84 |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
TUint reservedSectorsOld = 0; |
0 | 86 |
|
87 |
// zero for FAT32 |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
TUint rootDirSectors = (iRootDirEntries * KSizeOfFatDirEntry + (iBytesPerSector-1)) / iBytesPerSector; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
TUint fatSectors = 0; |
0 | 90 |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
91 |
TUint KMaxIterations = 10; |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
92 |
TUint n; |
0 | 93 |
for (n=0; n<KMaxIterations && reservedSectorsOld != iReservedSectors; n++) |
94 |
{ |
|
95 |
reservedSectorsOld = iReservedSectors; |
|
96 |
||
97 |
iSectorsPerFat = bFat32 ? MaxFat32Sectors() : bFat16 ? MaxFat16Sectors() : MaxFat12Sectors(); |
|
98 |
||
99 |
fatSectors = iSectorsPerFat * iNumberOfFats; |
|
100 |
||
101 |
// calculate number of blocks |
|
102 |
TInt nBlocks = (iReservedSectors + fatSectors + rootDirSectors + aEraseBlockSizeInSectors-1) / aEraseBlockSizeInSectors; |
|
103 |
||
104 |
iReservedSectors = (nBlocks * aEraseBlockSizeInSectors) - rootDirSectors - fatSectors; |
|
105 |
} |
|
106 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
107 |
ASSERT(iReservedSectors >= (bFat32 ? KDefFat32ResvdSec : KDefFatResvdSec)); |
0 | 108 |
|
109 |
if ((FirstDataSector() & (aEraseBlockSizeInSectors-1)) == 0) |
|
110 |
{ |
|
111 |
return KErrNone; |
|
112 |
} |
|
113 |
else |
|
114 |
{ |
|
115 |
iReservedSectors = reservedSectorsSaved; |
|
116 |
iSectorsPerFat = sectorsPerFatSaved; |
|
117 |
return KErrGeneral; |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
||
122 |
//------------------------------------------------------------------------------------------------------------------- |
|
123 |
||
124 |
/** |
|
125 |
Create the boot sector on media for the volume. For FAT32 also creates a backup copy of the boot sector. |
|
126 |
||
127 |
@leave System wide error codes |
|
128 |
*/ |
|
129 |
void CFatFormatCB::CreateBootSectorL() |
|
130 |
{ |
|
131 |
__PRINT1(_L("CFatFormatCB::CreateBootSector() drive:%d"),DriveNumber()); |
|
132 |
||
269 | 133 |
_LIT8(KName_Fat12,"FAT12 "); ///< Name in BPB given to a Fat12 volume |
134 |
_LIT8(KName_Fat16,"FAT16 "); ///< Name in BPB given to a Fat16 volume |
|
135 |
_LIT8(KName_Fat32,"FAT32 "); ///< Name in BPB given to a Fat32 volume |
|
136 |
_LIT8(KDefaultVendorID, "EPOC"); ///< Vendor Name for BPB for any volume formated using a Symbian OS device |
|
137 |
||
0 | 138 |
const TBool bFat32 = Is32BitFat(); |
139 |
||
140 |
TFatBootSector bootSector; |
|
141 |
||
142 |
bootSector.SetVendorID(KDefaultVendorID); |
|
143 |
bootSector.SetBytesPerSector(iBytesPerSector); |
|
144 |
bootSector.SetSectorsPerCluster(iSectorsPerCluster); |
|
145 |
bootSector.SetReservedSectors(iReservedSectors); |
|
146 |
bootSector.SetNumberOfFats(iNumberOfFats); |
|
147 |
iCountOfClusters=iMaxDiskSectors/iSectorsPerCluster; |
|
148 |
if (!bFat32) |
|
149 |
{ |
|
150 |
if (iCountOfClusters>(TInt)KMaxTUint16) |
|
151 |
User::Leave(KErrTooBig); |
|
152 |
} |
|
153 |
||
154 |
bootSector.SetReservedByte(0); |
|
155 |
TTime timeID; |
|
269 | 156 |
timeID.HomeTime(); |
157 |
bootSector.SetUniqueID(I64LOW(timeID.Int64())); // Generate Volume UniqueID from time |
|
0 | 158 |
bootSector.SetVolumeLabel(_L8("")); |
269 | 159 |
|
160 |
//-- set a text string in BPB that corresponds to the FS type |
|
161 |
switch(FatType()) |
|
162 |
{ |
|
163 |
case EFat12: |
|
164 |
bootSector.SetFileSysType(KName_Fat12); |
|
165 |
break; |
|
166 |
||
167 |
case EFat16: |
|
168 |
bootSector.SetFileSysType(KName_Fat16); |
|
169 |
break; |
|
170 |
||
171 |
case EFat32: |
|
172 |
bootSector.SetFileSysType(KName_Fat32); |
|
173 |
break; |
|
174 |
||
175 |
default: |
|
176 |
ASSERT(0); |
|
177 |
User::Leave(KErrArgument); |
|
178 |
}; |
|
179 |
||
180 |
||
181 |
||
0 | 182 |
bootSector.SetJumpInstruction(); |
183 |
bootSector.SetMediaDescriptor(KBootSectorMediaDescriptor); |
|
184 |
bootSector.SetNumberOfHeads(iNumberOfHeads); |
|
185 |
bootSector.SetHiddenSectors(iHiddenSectors); |
|
186 |
bootSector.SetSectorsPerTrack(iSectorsPerTrack); |
|
187 |
bootSector.SetPhysicalDriveNumber(128); |
|
188 |
bootSector.SetExtendedBootSignature(0x29); |
|
189 |
||
190 |
if(bFat32) |
|
191 |
{ |
|
192 |
bootSector.SetFatSectors(0); |
|
193 |
bootSector.SetFatSectors32(iSectorsPerFat); |
|
194 |
bootSector.SetRootDirEntries(0); |
|
195 |
bootSector.SetTotalSectors(0); |
|
196 |
bootSector.SetHugeSectors(iMaxDiskSectors); |
|
197 |
bootSector.SetFATFlags(0); |
|
198 |
bootSector.SetVersionNumber(0x00); |
|
199 |
bootSector.SetRootClusterNum(iRootClusterNum); |
|
200 |
bootSector.SetFSInfoSectorNum(KFSInfoSectorNum); |
|
201 |
bootSector.SetBkBootRecSector(KBkBootSectorNum); |
|
202 |
} |
|
203 |
else//fat12 and 16 |
|
204 |
{ |
|
205 |
bootSector.SetFatSectors32(0); |
|
206 |
bootSector.SetFatSectors(iSectorsPerFat); |
|
207 |
bootSector.SetRootDirEntries(iRootDirEntries); |
|
208 |
||
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
209 |
if (iMaxDiskSectors<=KMaxTUint16) |
0 | 210 |
{ |
211 |
bootSector.SetTotalSectors(iMaxDiskSectors); |
|
212 |
bootSector.SetHugeSectors(0); |
|
213 |
} |
|
214 |
else |
|
215 |
{ |
|
216 |
bootSector.SetTotalSectors(0); |
|
217 |
bootSector.SetHugeSectors(iMaxDiskSectors); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
//-- write main boot sector to the first sector on media |
|
222 |
User::LeaveIfError(FatMount().DoWriteBootSector(KBootSectorNum*bootSector.BytesPerSector(), bootSector)); |
|
223 |
||
224 |
//-- for FAT32 write backup copy of the boot sector |
|
225 |
if(bFat32) |
|
226 |
{ |
|
227 |
User::LeaveIfError(FatMount().DoWriteBootSector(KBkBootSectorNum*bootSector.BytesPerSector(), bootSector)); |
|
228 |
} |
|
229 |
||
230 |
} |
|
231 |
||
232 |
//------------------------------------------------------------------------------------------------------------------- |
|
233 |
||
234 |
/** |
|
235 |
Format a disk section, called iteratively to erase whole of media, on last iteration |
|
236 |
creates an empty volume. If called with quick formatonly erases the Fat leaving the |
|
237 |
rest of the volume intact. |
|
238 |
||
239 |
@leave System wide error code |
|
240 |
*/ |
|
241 |
void CFatFormatCB::DoFormatStepL() |
|
242 |
{ |
|
243 |
if (iFormatInfo.iFormatIsCurrent==EFalse) |
|
244 |
{ // Only done first time through |
|
245 |
if (iMode & EForceErase) |
|
246 |
{ |
|
247 |
TInt r = FatMount().ErasePassword(); |
|
248 |
User::LeaveIfError(r); |
|
249 |
// CFatMountCB::ErasePassword() calls TBusLocalDrive::ForceRemount(), |
|
250 |
// so need to stop a remount from occurring in next call to : |
|
251 |
// TFsFormatNext::DoRequestL((), TDrive::CheckMount(). |
|
252 |
FatMount().Drive().SetChanged(EFalse); |
|
253 |
} |
|
254 |
||
255 |
RecordOldInfoL(); |
|
256 |
InitializeFormatDataL(); |
|
257 |
FatMount().DoDismount(); |
|
258 |
if (iVariableSize) |
|
259 |
FatMount().ReduceSizeL(0,I64LOW(FatMount().iSize)); |
|
260 |
} |
|
261 |
// |
|
262 |
// Blank disk if not EQuickFormat |
|
263 |
// |
|
264 |
if (!iVariableSize && !(iMode & EQuickFormat) && iCurrentStep) |
|
265 |
{ |
|
266 |
if (iFormatInfo.iFormatIsCurrent == EFalse) |
|
267 |
{//-- firstly invalidate sectors 0-6 inclusive, they may contain main boot sector, backup boot sector and FSInfo sector. |
|
268 |
DoZeroFillMediaL(0, (KBkBootSectorNum+1)*iBytesPerSector); |
|
269 |
} |
|
270 |
TInt ret=FatMount().LocalDrive()->Format(iFormatInfo); |
|
271 |
if (ret!=KErrNone && ret!=KErrEof) // Handle format error |
|
272 |
ret = HandleCorrupt(ret); |
|
273 |
||
274 |
if (ret!=KErrNone && ret!=KErrEof) // KErrEof could be set by LocalDrive()->Format() |
|
275 |
User::Leave(ret); |
|
276 |
||
277 |
if (ret==KErrNone) |
|
278 |
{ |
|
279 |
iCurrentStep = I64LOW( 100 - (100 * TInt64(iFormatInfo.i512ByteSectorsFormatted)) / iMaxDiskSectors ); |
|
280 |
if (iCurrentStep<=0) |
|
281 |
iCurrentStep=1; |
|
282 |
return; |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
// ReMount since MBR may have been rewritten and partition may have moved / changed size |
|
287 |
TInt ret = LocalDrive()->ForceRemount(0); |
|
288 |
if (ret != KErrNone && ret != KErrNotSupported) |
|
289 |
User::Leave(ret); |
|
290 |
||
291 |
// MBR may have changed, so need to re-read iHiddenSectors etc.before BPB is written |
|
292 |
InitializeFormatDataL(); |
|
293 |
||
294 |
// Translate bad sector number to cluster number which contains that sector |
|
295 |
// This only happens in full format, in quick format they are already cluster numbers |
|
296 |
if (!iVariableSize && !(iMode & EQuickFormat)) |
|
297 |
User::LeaveIfError(BadSectorToCluster()); |
|
298 |
||
299 |
//Check if root cluster is bad and update as required |
|
300 |
if(Is32BitFat() && !iVariableSize && (iMode & EQuickFormat)) |
|
301 |
{ |
|
302 |
if(iBadClusters.Find(iRootClusterNum) != KErrNotFound) |
|
303 |
{ |
|
304 |
iRootClusterNum++; |
|
305 |
while(iBadClusters.Find(iRootClusterNum) != KErrNotFound) |
|
306 |
{ |
|
307 |
iRootClusterNum++; |
|
308 |
} |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
// |
|
313 |
// Do the rest of the disk in one lump |
|
314 |
// |
|
315 |
iCurrentStep=0; |
|
316 |
||
317 |
//-- zero-fill media from position 0 to the FAT end, i.e main & backup boot sector, FSInfo and its copy and all FATs |
|
318 |
const TUint32 posFatEnd = ((iSectorsPerFat*iNumberOfFats) + iReservedSectors) * iBytesPerSector; //-- last FAT end position |
|
319 |
||
320 |
if (iVariableSize) |
|
321 |
FatMount().EnlargeL(posFatEnd); |
|
322 |
||
323 |
DoZeroFillMediaL(0, posFatEnd); |
|
324 |
||
325 |
if(Is32BitFat()) |
|
326 |
{//create an empty root directory entry here |
|
327 |
||
328 |
const TUint KFat32EntrySz = 4; //-- FAT32 entry size, bytes |
|
329 |
const TInt startFAT1 = iReservedSectors; //-- FAT1 start sector |
|
330 |
const TInt entryOffset = iRootClusterNum*KFat32EntrySz; //-- Root dir entry offset in the FAT, bytes |
|
331 |
||
332 |
TBuf8<KFat32EntrySz> EOF(KFat32EntrySz); |
|
333 |
EOF[0]=0xFF; |
|
334 |
EOF[1]=0xFF; |
|
335 |
EOF[2]=0xFF; |
|
336 |
EOF[3]=0x0F; |
|
337 |
||
338 |
//-- write EOF mark to the every FAT copy |
|
339 |
for(TInt i=0; i<iNumberOfFats; i++) |
|
340 |
{ |
|
341 |
const TInt rootDirEntryPos = iBytesPerSector*(startFAT1 + i*iSectorsPerFat) + entryOffset; |
|
342 |
User::LeaveIfError(LocalDrive()->Write(rootDirEntryPos, EOF)); |
|
343 |
} |
|
344 |
||
345 |
//-- zero-fill FAT32 root directory (just 1 cluster) |
|
346 |
const TInt firstDataSector = iReservedSectors + (iNumberOfFats * iSectorsPerFat); //+RootDirSectors (not required for fat32) |
|
347 |
const TInt firstSectorOfCluster = ((iRootClusterNum - KFatFirstSearchCluster) * iSectorsPerCluster) + firstDataSector; |
|
348 |
||
349 |
const TUint32 posRootDirStart = firstSectorOfCluster * iBytesPerSector; |
|
350 |
const TUint32 posRootDirEnd = posRootDirStart + iSectorsPerCluster*iBytesPerSector; |
|
351 |
||
352 |
DoZeroFillMediaL(posRootDirStart, posRootDirEnd); |
|
353 |
} |
|
354 |
else |
|
355 |
{//-- FAT12/16 |
|
356 |
//-- Zero fill root directory |
|
357 |
const TInt rootDirSector = iReservedSectors + (iNumberOfFats * iSectorsPerFat); |
|
358 |
const TInt rootDirSize = iRootDirEntries * KSizeOfFatDirEntry; //-- size in bytes |
|
359 |
||
360 |
const TUint32 posRootDirStart = rootDirSector * iBytesPerSector; |
|
361 |
const TUint32 posRootDirEnd = posRootDirStart + rootDirSize; |
|
362 |
||
363 |
const TInt numOfRootSectors=(rootDirSize%iBytesPerSector) ? (rootDirSize/iBytesPerSector+1) : (rootDirSize/iBytesPerSector); |
|
364 |
if (iVariableSize) |
|
365 |
FatMount().EnlargeL(iBytesPerSector*numOfRootSectors); |
|
366 |
||
367 |
DoZeroFillMediaL(posRootDirStart, posRootDirEnd); |
|
368 |
||
369 |
// Enlarge ram drive to take into account rounding of |
|
370 |
// data start to cluster boundary |
|
371 |
if(iVariableSize && iSectorsPerCluster!=1) |
|
372 |
{ |
|
373 |
const TInt firstFreeSector=rootDirSector+numOfRootSectors; |
|
374 |
const TInt firstFreeCluster=firstFreeSector%iSectorsPerCluster ? firstFreeSector/iSectorsPerCluster+1 : firstFreeSector/iSectorsPerCluster; |
|
375 |
const TInt alignedSector=firstFreeCluster*iSectorsPerCluster; |
|
376 |
if(alignedSector!=firstFreeSector) |
|
377 |
FatMount().EnlargeL((alignedSector-firstFreeSector)*iBytesPerSector); |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
//-- FAT[0] must contain media descriptor in the low byte, FAT[1] for fat16/32 may contain some flags |
|
382 |
TBuf8<8> startFat(8); |
|
383 |
startFat.Fill(0xFF); |
|
384 |
||
385 |
if(Is32BitFat()) //-- FAT32 |
|
386 |
{//-- FAT32 uses only low 28 bits in FAT entry. |
|
387 |
startFat[3] = 0x0F; |
|
388 |
startFat[7] = 0x0F; |
|
389 |
} |
|
390 |
else if(iVariableSize||Is16BitFat()) //-- FAT16 or RAM drive which is always FAT16 |
|
391 |
{ |
|
392 |
startFat.SetLength(4); |
|
393 |
} |
|
394 |
else //-- FAT12 |
|
395 |
{ |
|
396 |
startFat.SetLength(3); |
|
397 |
} |
|
398 |
||
399 |
startFat[0]=KBootSectorMediaDescriptor; |
|
400 |
||
401 |
//-- write FAT[0] and FAT[1] entries to all copies of FAT |
|
402 |
for(TInt i=0;i<iNumberOfFats;i++) |
|
403 |
{ |
|
404 |
User::LeaveIfError(LocalDrive()->Write(iBytesPerSector*(iReservedSectors+(iSectorsPerFat*i)),startFat)); |
|
405 |
} |
|
406 |
||
407 |
//-- create boot sectors |
|
408 |
CreateBootSectorL(); |
|
409 |
||
410 |
//-- create FSInfo sectors |
|
411 |
if (Is32BitFat()) |
|
412 |
{ |
|
413 |
CreateReservedBootSectorL(); |
|
414 |
CreateFSInfoSectorL(); |
|
415 |
} |
|
416 |
||
417 |
//-- here we have bad clusters numbers saved by the quick format |
|
418 |
//-- Interpret old bad cluster number to new cluster number and mark new bad clusters |
|
419 |
if (!iVariableSize && iBadClusters.Count()>0) |
|
420 |
{ |
|
421 |
||
422 |
//-- Here we need fully mounted volume, so mount it normally. |
|
423 |
FatMount().MountL(EFalse); |
|
424 |
||
425 |
iBadClusters.Sort(); |
|
426 |
TranslateL(); |
|
427 |
const TInt mark = FatMount().Is32BitFat() ? KBad_32Bit : (FatMount().Is16BitFat() ? KBad_16Bit : KBad_12Bit); |
|
428 |
||
429 |
for (TInt i=0; i<iBadClusters.Count(); ++i) |
|
430 |
FatMount().FAT().WriteL(iBadClusters[i], mark); |
|
431 |
||
432 |
FatMount().FAT().FlushL(); |
|
433 |
||
434 |
//-- indicate that the volume is "dirty" in order to the next Mount evend not to use FSInfo, which |
|
435 |
//-- contains incorrect value of free clusters because we already have bad ones saved. |
|
436 |
//-- This is a very rare condition. |
|
437 |
FatMount().SetVolumeCleanL(EFalse); |
|
438 |
||
439 |
#if defined(_DEBUG) |
|
440 |
TInt r=FatMount().CheckDisk(); |
|
441 |
__PRINT1(_L("CFatFormatCB::DoFormatStepL() CheckDisk res: %d"),r); |
|
442 |
#endif |
|
443 |
} |
|
444 |
else |
|
445 |
{ |
|
446 |
//-- We do not need to perform full mount in this case, the TDrive object will be marked as changed in ~CFormatCB and the |
|
447 |
//-- mount will be closed. Therefore on the first access to it it will be mounted normally. |
|
448 |
FatMount().MountL(ETrue); //-- force mount |
|
449 |
} |
|
450 |
||
451 |
__PRINT1(_L("CFatFormatCB::DoFormatStepL() Format complete drv:%d"), DriveNumber()); |
|
452 |
} |
|
453 |
||
454 |
||
455 |
//------------------------------------------------------------------------------------------------------------------- |
|
456 |
||
457 |
||
458 |
void CFatFormatCB::RecordOldInfoL() |
|
459 |
{ |
|
460 |
// Check if mount or disk is corrupt |
|
461 |
// This should be stored in member variable because FatMount is remounted |
|
462 |
// every time RFormat::Next() gets called thus FatMount().Initialised() |
|
463 |
// will be inconsistent with previous state. |
|
464 |
TLocalDriveCapsV3Buf caps; |
|
465 |
User::LeaveIfError(LocalDrive()->Caps(caps)); |
|
466 |
iVariableSize=((caps().iMediaAtt)&KMediaAttVariableSize) ? (TBool)ETrue : (TBool)EFalse; |
|
467 |
iDiskCorrupt = !FatMount().ConsistentState(); |
|
468 |
iBadClusters.Reset(); |
|
469 |
iBadSectors.Reset(); |
|
470 |
if (!iVariableSize && !iDiskCorrupt && (iMode&EQuickFormat)) |
|
471 |
{ |
|
472 |
iOldFirstFreeSector = FatMount().iFirstFreeByte>>FatMount().SectorSizeLog2(); |
|
473 |
iOldSectorsPerCluster = FatMount().SectorsPerCluster(); |
|
474 |
||
475 |
FatMount().FAT().InvalidateCacheL(); //-- invalidate whole FAT cache |
|
476 |
||
477 |
// Collect bad cluster information from current FAT table |
|
478 |
const TInt maxClusterNum = FatMount().UsableClusters() + KFatFirstSearchCluster; |
|
479 |
const TUint32 mark = FatMount().Is32BitFat() ? KBad_32Bit : (FatMount().Is16BitFat() ? KBad_16Bit : KBad_12Bit); |
|
480 |
||
481 |
for (TInt i=KFatFirstSearchCluster; i<maxClusterNum; ++i) |
|
482 |
{ |
|
483 |
if (FatMount().FAT().ReadL(i) == mark) |
|
484 |
iBadClusters.AppendL(i); |
|
485 |
} |
|
486 |
} |
|
487 |
} |
|
488 |
||
489 |
||
490 |
||
491 |
||
492 |
TInt CFatFormatCB::BadSectorToCluster() |
|
493 |
{ |
|
494 |
TInt sizeofFatAndRootDir; |
|
269 | 495 |
|
496 |
if(!Is32BitFat()) |
|
0 | 497 |
sizeofFatAndRootDir = iSectorsPerFat*iNumberOfFats + ((iRootDirEntries*KSizeOfFatDirEntry+(1<<iSectorSizeLog2)-1)>>iSectorSizeLog2); |
498 |
else |
|
499 |
sizeofFatAndRootDir = (iRootClusterNum-2) * iSectorsPerCluster; |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
500 |
|
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
501 |
TUint firstFreeSector = iReservedSectors + sizeofFatAndRootDir; |
0 | 502 |
|
503 |
// Check in rare case that corrupt in critical area |
|
504 |
// which includes bootsector, FAT table, (and root dir if not FAT32) |
|
505 |
TInt i, r; |
|
506 |
for (i=0; i<iBadSectors.Count(); ++i) |
|
507 |
{ |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
const TUint badSector = iBadSectors[i]; |
0 | 509 |
// Check in rare case that corrupt in critical area |
510 |
// which includes bootsector, FAT table, (and root dir if not FAT32) |
|
511 |
if (firstFreeSector > badSector) |
|
512 |
{ |
|
513 |
if (badSector == 0) // Boot sector corrupt |
|
514 |
return KErrCorrupt; |
|
269 | 515 |
|
516 |
if (Is32BitFat() && badSector==1) // FSInfo corrupt |
|
0 | 517 |
return KErrCorrupt; |
269 | 518 |
|
0 | 519 |
if (badSector < iReservedSectors) // Harmless in reserved area |
520 |
continue; |
|
521 |
// Extend reserved area to cover bad sector |
|
522 |
iReservedSectors = badSector + 1; |
|
523 |
firstFreeSector = iReservedSectors + sizeofFatAndRootDir; |
|
524 |
continue; |
|
525 |
} |
|
526 |
||
527 |
// Figure out bad cluster number and record it |
|
528 |
TUint cluster = (badSector-firstFreeSector)/iSectorsPerCluster+2; |
|
529 |
if (iBadClusters.Find(cluster) == KErrNotFound) |
|
530 |
{ |
|
531 |
if ((r=iBadClusters.Append(cluster)) != KErrNone) |
|
532 |
return r; |
|
269 | 533 |
|
534 |
if (Is32BitFat() && iRootClusterNum==cluster) |
|
0 | 535 |
iRootClusterNum++; |
536 |
} |
|
537 |
} |
|
538 |
return KErrNone; |
|
539 |
} |
|
540 |
||
541 |
/** |
|
542 |
Create the File system information sector and its backup copy on a disk. |
|
543 |
Note that CFatMountCB is still not in mounted state, so we can not rely on it. |
|
544 |
||
545 |
@leave System wide error codes |
|
546 |
*/ |
|
547 |
void CFatFormatCB::CreateFSInfoSectorL() |
|
548 |
{ |
|
549 |
__PRINT1(_L("CFatFormatCB::CreateFSInfoSectorL() drv:%d"), DriveNumber()); |
|
550 |
||
551 |
ASSERT(Is32BitFat()); //-- Actually, CFatMount shall be in a consistent state. |
|
552 |
||
553 |
TFSInfo fsInfo; |
|
554 |
TBuf8<KSizeOfFSInfo> fsInfoSecBuf; |
|
555 |
||
556 |
const TUint32 freeSectors = iMaxDiskSectors - (iReservedSectors + (iNumberOfFats * iSectorsPerFat)); |
|
557 |
const TUint32 freeClusters = (freeSectors / iSectorsPerCluster) - 1; //-- 1st cluster is taken by empty Root Dir on FAT32 |
|
558 |
const TUint32 nextFreeClust = iRootClusterNum+1; |
|
559 |
||
560 |
fsInfo.SetFreeClusterCount(freeClusters); |
|
561 |
fsInfo.SetNextFreeCluster(nextFreeClust); |
|
562 |
||
563 |
fsInfo.Externalize(fsInfoSecBuf); //-- put data to the sector buffer |
|
564 |
||
565 |
User::LeaveIfError(LocalDrive()->Write(KFSInfoSectorNum*iBytesPerSector, fsInfoSecBuf)); //-- main FSInfo Sector |
|
566 |
User::LeaveIfError(LocalDrive()->Write(KBkFSInfoSectorNum*iBytesPerSector, fsInfoSecBuf)); //-- Backup FSInfo Sector |
|
567 |
||
568 |
} |
|
569 |
||
570 |
/** |
|
571 |
Create the reserved boot sector and its backup copy on a disk. |
|
572 |
These are located at sectors 2 & 8 |
|
573 |
||
574 |
@leave System wide error codes |
|
575 |
*/ |
|
576 |
void CFatFormatCB::CreateReservedBootSectorL() |
|
577 |
{ |
|
578 |
__PRINT1(_L("CFatFormatCB::CreateReserveBootSectorL() drv:%d"), DriveNumber()); |
|
579 |
||
580 |
ASSERT(Is32BitFat()); |
|
581 |
||
582 |
TFatBootSector bootSector; |
|
583 |
||
584 |
User::LeaveIfError(FatMount().DoWriteBootSector(KReservedBootSectorNum*KDefaultSectorSize, bootSector)); |
|
585 |
User::LeaveIfError(FatMount().DoWriteBootSector(KBkReservedBootSectorNum*KDefaultSectorSize, bootSector)); |
|
586 |
} |
|
587 |
||
588 |
||
269 | 589 |
//------------------------------------------------------------------------------------------------------------------- |
590 |
||
591 |
/** |
|
592 |
Initialize the user-specific format parameters |
|
593 |
Tries to figure out number of FATs, SPC, etc. values passed from the user side. |
|
594 |
This method is called if the user has specified some formatting parameters, like SPC |
|
595 |
*/ |
|
271 | 596 |
TInt CFatFormatCB::ProcessVolParam_User(const TLocalDriveCapsV6& aCaps) |
269 | 597 |
{ |
271 | 598 |
(void)aCaps; |
599 |
||
269 | 600 |
__PRINT1(_L("CFatFormatCB::ProcessVolParam_User() sectors:%d"), iMaxDiskSectors); |
601 |
Dump_TLDFormatInfo(iSpecialInfo()); |
|
602 |
||
603 |
//-- KErrArgument will be returned if iSpecialInfo().iFATBits isn't one of EFB32, EFB16, EFB32 |
|
604 |
||
605 |
if(iSpecialInfo().iFlags & TLDFormatInfo::EOneFatTable) |
|
606 |
iNumberOfFats = 1; |
|
607 |
else if(iSpecialInfo().iFlags & TLDFormatInfo::ETwoFatTables) |
|
608 |
iNumberOfFats = 2; |
|
609 |
else if(Drive().IsRemovable()) |
|
610 |
iNumberOfFats = KNumberOfFatsExternal; |
|
611 |
else |
|
612 |
iNumberOfFats = KNumberOfFatsInternal; |
|
613 |
||
614 |
||
615 |
if(iSpecialInfo().iReservedSectors) |
|
616 |
iReservedSectors = iSpecialInfo().iReservedSectors; |
|
617 |
else |
|
618 |
iReservedSectors = KDefFatResvdSec; //-- the user hasn't specified reserved sectors count, use default (FAT12/16) |
|
619 |
||
620 |
//----------------------------------------- |
|
621 |
||
622 |
||
623 |
const TUint KMaxSecPerCluster = 64; |
|
624 |
const TUint KDefaultSecPerCluster= 8; //-- default value, if the iSpecialInfo().iSectorsPerCluster isn't specified |
|
625 |
||
626 |
iSectorsPerCluster = iSpecialInfo().iSectorsPerCluster; |
|
627 |
if(iSectorsPerCluster <= 0) |
|
628 |
{//-- default value, user hasn't specified TLDFormatInfo::iSectorsPerCluster |
|
629 |
iSectorsPerCluster = KDefaultSecPerCluster; //-- will be adjusted later |
|
630 |
} |
|
631 |
else |
|
632 |
{ |
|
633 |
iSectorsPerCluster = Min(1<<Log2(iSectorsPerCluster), KMaxSecPerCluster); |
|
634 |
} |
|
635 |
||
636 |
//----------------------------------------- |
|
637 |
||
638 |
if (iMaxDiskSectors < 4096) // < 2MB |
|
639 |
{ |
|
640 |
iSectorsPerCluster = 1; |
|
641 |
iRootDirEntries = 128; |
|
642 |
} |
|
643 |
else if (iMaxDiskSectors < 8192) // < 4MB |
|
644 |
{ |
|
645 |
iSectorsPerCluster = Min((TUint32)iSectorsPerCluster, (TUint32)2); |
|
646 |
iRootDirEntries = 256; |
|
647 |
} |
|
648 |
else if (iMaxDiskSectors < 32768) // < 16MB |
|
649 |
{ |
|
650 |
iSectorsPerCluster = Min((TUint32)iSectorsPerCluster, (TUint32)4); |
|
651 |
iRootDirEntries = 512; |
|
652 |
} |
|
653 |
else if (iMaxDiskSectors < 1048576) // < 512MB |
|
654 |
{ |
|
655 |
iSectorsPerCluster = Min((TUint32)iSectorsPerCluster, (TUint32)8); |
|
656 |
iRootDirEntries = 512; |
|
657 |
} |
|
658 |
else // FAT32 |
|
659 |
{ |
|
660 |
iRootDirEntries = 512; |
|
661 |
iSectorsPerCluster = Min((TUint32)iSectorsPerCluster, (TUint32)KMaxSecPerCluster); |
|
662 |
} |
|
0 | 663 |
|
664 |
||
269 | 665 |
//----------------------------------------- |
666 |
||
667 |
TLDFormatInfo::TFATBits fatBits = iSpecialInfo().iFATBits; |
|
668 |
if (fatBits == TLDFormatInfo::EFBDontCare) |
|
669 |
{//-- the user hasn't specified FAT type, need to work it out according to volume geometry |
|
670 |
const TFatType fatType = SuggestFatType(); |
|
671 |
switch(fatType) |
|
672 |
{ |
|
673 |
case EFat12: |
|
674 |
fatBits = TLDFormatInfo::EFB12; |
|
675 |
break; |
|
676 |
case EFat16: |
|
677 |
fatBits = TLDFormatInfo::EFB16; |
|
678 |
break; |
|
679 |
case EFat32: |
|
680 |
fatBits = TLDFormatInfo::EFB32; |
|
681 |
break; |
|
682 |
case EInvalid: |
|
683 |
ASSERT(0); |
|
684 |
} |
|
685 |
} |
|
686 |
||
687 |
TFatType reqFatType(EInvalid); //-- requested FAT type |
|
688 |
||
689 |
switch (fatBits) |
|
690 |
{ |
|
691 |
case TLDFormatInfo::EFB12: |
|
692 |
SetFatType(EFat12); |
|
693 |
iSectorsPerFat=MaxFat12Sectors(); |
|
694 |
reqFatType = EFat12; |
|
695 |
break; |
|
696 |
||
697 |
case TLDFormatInfo::EFB16: |
|
698 |
SetFatType(EFat16); |
|
699 |
iSectorsPerFat=MaxFat16Sectors(); |
|
700 |
reqFatType = EFat16; |
|
701 |
break; |
|
702 |
||
703 |
case TLDFormatInfo::EFB32: |
|
704 |
SetFatType(EFat32); |
|
705 |
iSectorsPerFat=MaxFat32Sectors(); |
|
706 |
||
707 |
iRootDirEntries = 0; |
|
708 |
iRootClusterNum = 2; |
|
709 |
||
710 |
if(iSpecialInfo().iReservedSectors == 0) |
|
711 |
iReservedSectors = KDefFat32ResvdSec; //-- user hasn't specified reserved sectors count, use default (FAT32) |
|
712 |
else |
|
713 |
iReservedSectors = iSpecialInfo().iReservedSectors; |
|
714 |
||
715 |
reqFatType = EFat32; |
|
716 |
break; |
|
717 |
||
718 |
default: |
|
719 |
__PRINT(_L("CFatFormatCB::InitFormatDataForFixedSizeDiskUser() Incorrect FAT type specifier!")); |
|
720 |
return KErrArgument; |
|
721 |
} |
|
722 |
||
723 |
//-- check if we can format the volume with requested FAT type |
|
724 |
const TFatType fatType = SuggestFatType(); |
|
725 |
if(fatType != reqFatType) |
|
726 |
{ |
|
727 |
//-- volume metrics don't correspond to the requested FAT type |
|
728 |
__PRINT(_L("CFatFormatCB::InitFormatDataForFixedSizeDiskUser() FAT type mismatch!")); |
|
729 |
return KErrArgument; |
|
730 |
} |
|
731 |
||
732 |
return KErrNone; |
|
733 |
} |
|
734 |
||
735 |
//------------------------------------------------------------------------------------------------------------------- |
|
736 |
/** |
|
737 |
Initialize format parameters from the information provided by the media driver. |
|
738 |
This method is mostly called for SD cards formatting |
|
739 |
*/ |
|
740 |
TInt CFatFormatCB::ProcessVolParam_Custom(const TLocalDriveCapsV6& aCaps) |
|
741 |
{ |
|
742 |
__PRINT(_L("CFatFormatCB::ProcessVolParam_Custom()")); |
|
743 |
||
271 | 744 |
ASSERT(aCaps.iExtraInfo); |
745 |
||
746 |
//-- the driver may have decided which file system should be used on the media. |
|
747 |
//-- E.g. for SD cards > 32GB exFAT must be used. |
|
748 |
if(aCaps.iFileSystemId != KDriveFileSysFAT) |
|
749 |
{//-- the media driver decides that other than FAT FS must be used on the media. |
|
750 |
__PRINT1(_L(" the media driver required other than FAT FS:%d"), aCaps.iFileSystemId); |
|
751 |
return KErrNotSupported; |
|
752 |
} |
|
753 |
||
754 |
||
269 | 755 |
//-- TLDFormatInfo structure is filled by the media driver, it decides the media formatting parameters |
756 |
const TLDFormatInfo& fmtInfo = aCaps.iFormatInfo; |
|
757 |
Dump_TLDFormatInfo(fmtInfo); |
|
758 |
||
759 |
if(fmtInfo.iFlags & TLDFormatInfo::EOneFatTable) |
|
760 |
iNumberOfFats = 1; |
|
761 |
else if(fmtInfo.iFlags & TLDFormatInfo::ETwoFatTables) |
|
762 |
iNumberOfFats = 2; |
|
763 |
else if(Drive().IsRemovable()) |
|
764 |
iNumberOfFats = KNumberOfFatsExternal; |
|
765 |
else |
|
766 |
iNumberOfFats = KNumberOfFatsInternal; |
|
767 |
||
768 |
iRootDirEntries=512; |
|
769 |
||
770 |
iSectorsPerCluster = fmtInfo.iSectorsPerCluster; |
|
771 |
iSectorsPerTrack = fmtInfo.iSectorsPerTrack; |
|
772 |
iNumberOfHeads = fmtInfo.iNumberOfSides; |
|
773 |
iReservedSectors = fmtInfo.iReservedSectors ? fmtInfo.iReservedSectors : KDefFatResvdSec; |
|
774 |
||
775 |
switch (fmtInfo.iFATBits) |
|
776 |
{ |
|
777 |
case TLDFormatInfo::EFB12: |
|
778 |
SetFatType(EFat12); |
|
779 |
iSectorsPerFat = MaxFat12Sectors(); |
|
780 |
break; |
|
781 |
||
782 |
case TLDFormatInfo::EFB16: |
|
783 |
SetFatType(EFat16); |
|
784 |
iSectorsPerFat = MaxFat16Sectors(); |
|
785 |
break; |
|
786 |
||
787 |
case TLDFormatInfo::EFB32: |
|
788 |
SetFatType(EFat32); |
|
789 |
iReservedSectors = fmtInfo.iReservedSectors ? fmtInfo.iReservedSectors : KDefFat32ResvdSec; |
|
790 |
iSectorsPerFat = MaxFat32Sectors(); |
|
791 |
iRootDirEntries = 0; |
|
792 |
iRootClusterNum = 2; |
|
793 |
break; |
|
794 |
||
795 |
default: |
|
796 |
{ |
|
797 |
TInt64 clusters64 = (fmtInfo.iCapacity / KDefaultSectorSize) / iSectorsPerCluster; |
|
798 |
TInt clusters = I64LOW(clusters64); |
|
799 |
if (clusters < 4085) |
|
800 |
{ |
|
801 |
SetFatType(EFat12); |
|
802 |
iSectorsPerFat = MaxFat12Sectors(); |
|
803 |
} |
|
804 |
else if(clusters < 65525) |
|
805 |
{ |
|
806 |
SetFatType(EFat16); |
|
807 |
iSectorsPerFat = MaxFat16Sectors(); |
|
808 |
} |
|
809 |
else |
|
810 |
{ |
|
811 |
SetFatType(EFat32); |
|
812 |
iReservedSectors = fmtInfo.iReservedSectors ? fmtInfo.iReservedSectors : KDefFat32ResvdSec; |
|
813 |
iSectorsPerFat = MaxFat32Sectors(); |
|
814 |
iRootDirEntries = 0; |
|
815 |
iRootClusterNum = 2; |
|
816 |
} |
|
817 |
} |
|
818 |
} |
|
819 |
||
820 |
return KErrNone; |
|
821 |
||
822 |
} |
|
823 |
||
824 |
//------------------------------------------------------------------------------------------------------------------- |
|
825 |
/** |
|
826 |
Initialize format parameters by defult. |
|
827 |
This method is called if the used has not specified any formatting parameters (all default) |
|
828 |
*/ |
|
829 |
TInt CFatFormatCB::ProcessVolParam_Default(const TLocalDriveCapsV6& aCaps) |
|
830 |
{ |
|
831 |
__PRINT1(_L("CFatFormatCB::ProcessVolParam_Default sectors:%d"), iMaxDiskSectors); |
|
832 |
||
833 |
if( Drive().IsRemovable() ) |
|
834 |
iNumberOfFats = KNumberOfFatsExternal; |
|
835 |
else |
|
836 |
iNumberOfFats = KNumberOfFatsInternal; |
|
837 |
||
838 |
iReservedSectors=KDefFatResvdSec; |
|
839 |
if (iMaxDiskSectors <=4084*1) // 2MB |
|
840 |
{ |
|
841 |
iRootDirEntries=128; |
|
842 |
iSectorsPerCluster=1; |
|
843 |
SetFatType(EFat12); |
|
844 |
iSectorsPerFat=MaxFat12Sectors(); |
|
845 |
} |
|
846 |
else if (iMaxDiskSectors<4084*2) // < 4MB (8168 sectors) |
|
847 |
{ |
|
848 |
iRootDirEntries=256; |
|
849 |
iSectorsPerCluster=2; |
|
850 |
SetFatType(EFat12); |
|
851 |
iSectorsPerFat=MaxFat12Sectors(); |
|
852 |
} |
|
853 |
else if (iMaxDiskSectors<4084*4) // < 8MB (16336 sectors) |
|
854 |
{ |
|
855 |
iRootDirEntries=512; |
|
856 |
iSectorsPerCluster=4; |
|
857 |
SetFatType(EFat12); |
|
858 |
iSectorsPerFat=MaxFat12Sectors(); |
|
859 |
} |
|
860 |
else if (iMaxDiskSectors<4084*8) // < 16MB (32672 sectors) |
|
861 |
{ |
|
862 |
iRootDirEntries=512; |
|
863 |
iSectorsPerCluster=8; |
|
864 |
SetFatType(EFat12); |
|
865 |
iSectorsPerFat=MaxFat12Sectors(); |
|
866 |
} |
|
867 |
else if(iMaxDiskSectors<1048576) // >= 16Mb - FAT16 < (1048576) 512MB |
|
868 |
{ |
|
869 |
SetFatType(EFat16); |
|
870 |
TUint minSectorsPerCluster=(iMaxDiskSectors+KMaxFAT16Entries-1)/KMaxFAT16Entries; |
|
871 |
iRootDirEntries=512; |
|
872 |
iSectorsPerCluster=1; |
|
873 |
||
874 |
while (minSectorsPerCluster>iSectorsPerCluster) |
|
875 |
iSectorsPerCluster<<=1; |
|
876 |
||
877 |
iSectorsPerFat=MaxFat16Sectors(); |
|
878 |
} |
|
879 |
else //use FAT32 |
|
880 |
{ |
|
881 |
SetFatType(EFat32); |
|
882 |
iRootDirEntries=0; //this is always the case for fat32 |
|
883 |
||
884 |
if(iMaxDiskSectors < 16777216) //8GB in 512byte sectors |
|
885 |
iSectorsPerCluster=8; |
|
886 |
else if(iMaxDiskSectors < 33554432) //16GB in 512byte sectors |
|
887 |
iSectorsPerCluster=16; |
|
888 |
else if(iMaxDiskSectors < 67108864) //32GB in 512byte sectors |
|
889 |
iSectorsPerCluster=32; |
|
890 |
else |
|
891 |
iSectorsPerCluster=64; //Anything >= 32GB uses a 32K cluster size |
|
892 |
||
893 |
iReservedSectors=KDefFat32ResvdSec; |
|
894 |
iRootClusterNum=2; //As recomended in the document |
|
895 |
iSectorsPerFat=MaxFat32Sectors(); |
|
896 |
||
897 |
} |
|
898 |
||
899 |
const TFatType fatType = SuggestFatType(); |
|
900 |
||
901 |
// Ensure cluster size is a multiple of the block size |
|
902 |
TInt blockSizeInSectors = aCaps.iBlockSize >> iSectorSizeLog2; |
|
903 |
__PRINT1(_L("blockSizeInSectors: %d"),blockSizeInSectors); |
|
904 |
ASSERT(blockSizeInSectors == 0 || IsPowerOf2(blockSizeInSectors)); |
|
905 |
if (blockSizeInSectors != 0 && IsPowerOf2(blockSizeInSectors)) |
|
906 |
{ |
|
907 |
__PRINT1(_L("iSectorsPerCluster (old): %d"),iSectorsPerCluster); |
|
908 |
AdjustClusterSize(blockSizeInSectors); |
|
909 |
__PRINT1(_L("iSectorsPerCluster (new): %d"),iSectorsPerCluster); |
|
910 |
} |
|
911 |
||
912 |
||
913 |
for (; iSectorsPerCluster>1; iSectorsPerCluster>>= 1) |
|
914 |
{ |
|
915 |
// Align first data sector on an erase block boundary if |
|
916 |
// (1) the iEraseBlockSize is specified |
|
917 |
// (2) the start of the partition is already aligned to an erase block boundary, |
|
918 |
// i.e. iHiddenSectors is zero or a multiple of iEraseBlockSize |
|
919 |
__PRINT1(_L("iHiddenSectors: %d"),iHiddenSectors); |
|
920 |
TInt eraseblockSizeInSectors = aCaps.iEraseBlockSize >> iSectorSizeLog2; |
|
921 |
__PRINT1(_L("eraseblockSizeInSectors: %d"),eraseblockSizeInSectors); |
|
922 |
ASSERT(eraseblockSizeInSectors == 0 || IsPowerOf2(eraseblockSizeInSectors)); |
|
923 |
ASSERT(eraseblockSizeInSectors == 0 || eraseblockSizeInSectors >= blockSizeInSectors); |
|
924 |
if ((eraseblockSizeInSectors != 0) && |
|
925 |
(iHiddenSectors % eraseblockSizeInSectors == 0) && |
|
926 |
(IsPowerOf2(eraseblockSizeInSectors)) && |
|
927 |
(eraseblockSizeInSectors >= blockSizeInSectors)) |
|
928 |
{ |
|
929 |
TInt r = AdjustFirstDataSectorAlignment(eraseblockSizeInSectors); |
|
930 |
ASSERT(r == KErrNone); |
|
931 |
(void) r; |
|
932 |
} |
|
933 |
__PRINT1(_L("iReservedSectors: %d"),iReservedSectors); |
|
934 |
__PRINT1(_L("FirstDataSector: %d"), FirstDataSector()); |
|
935 |
||
936 |
// If we've shrunk the number of clusters by so much that it's now invalid for this FAT type |
|
937 |
// then we need to decrease the cluster size and try again, otherwise we're finshed. |
|
938 |
if (SuggestFatType() == fatType) |
|
939 |
break; |
|
940 |
} |
|
941 |
__PRINT1(_L("iSectorsPerCluster (final): %d"),iSectorsPerCluster); |
|
942 |
||
943 |
return KErrNone; |
|
944 |
} |
|
945 |
||
946 |
//------------------------------------------------------------------------------------------------------------------- |
|
947 |
/** |
|
948 |
Initialize the format parameters for a variable sized disk (RAM drive) |
|
949 |
||
950 |
@return standard error code |
|
951 |
*/ |
|
271 | 952 |
TInt CFatFormatCB::ProcessVolParam_RamDisk(const TLocalDriveCapsV6& aCaps) |
269 | 953 |
{ |
954 |
__PRINT1(_L("CFatFormatCB::ProcessVolParam_RamDisk() sectors:%d"), iMaxDiskSectors); |
|
955 |
||
271 | 956 |
if(aCaps.iFileSystemId != KDriveFileSysFAT) |
957 |
{//-- RAM media driver must set this value correctly. |
|
958 |
__PRINT1(_L("RAM media driver required other than FAT FS:%d"), aCaps.iFileSystemId); |
|
959 |
ASSERT(0); |
|
960 |
return KErrNotSupported; |
|
961 |
} |
|
962 |
||
269 | 963 |
iNumberOfFats = 2; // 1 FAT 1 Indirection table (FIT) |
964 |
iReservedSectors= 1; |
|
965 |
iRootDirEntries = 2*(4*KDefaultSectorSize)/sizeof(SFatDirEntry); |
|
966 |
TUint minSectorsPerCluster=(iMaxDiskSectors+KMaxFAT16Entries-1)/KMaxFAT16Entries; |
|
967 |
iSectorsPerCluster=1; |
|
968 |
||
969 |
while(minSectorsPerCluster > iSectorsPerCluster) |
|
970 |
iSectorsPerCluster<<=1; |
|
971 |
||
972 |
||
973 |
iSectorsPerFat=MaxFat16Sectors(); |
|
974 |
__PRINT1(_L("iSectorsPerCluster = %d"),iSectorsPerCluster); |
|
975 |
__PRINT1(_L("iSectorsPerFat = %d"),iSectorsPerFat); |
|
976 |
||
977 |
SetFatType(EFat16); |
|
978 |
||
979 |
return KErrNone; |
|
980 |
} |
|
981 |
||
982 |
||
983 |
||
984 |
||
985 |
||
986 |
||
987 |
||
988 |
||
989 |
||
990 |
||
991 |
||
992 |
||
993 |
||
994 |
||
995 |
||
996 |
||
997 |
||
998 |
||
999 |
||
1000 |
||
1001 |
||
1002 |
||
1003 |
||
1004 |