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