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