1 // Copyright (c) 2006-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 // Performs basic operations required by all test steps |
|
15 // |
|
16 // |
|
17 |
|
18 #include "basetestfat32base.h" |
|
19 #include "sl_bpb1.h" |
|
20 #include "basetedefs.h" |
|
21 #include <f32file.h> |
|
22 |
|
23 static RRawDisk TheDisk; |
|
24 static TFatBootSector TheBootSector; |
|
25 |
|
26 TDiskType gDiskType = EFatUnknown; |
|
27 TInt gBytesPerCluster; |
|
28 TInt gEntriesPerCluster; |
|
29 TInt gDataStartBytes; |
|
30 TInt gRootDirSectors; |
|
31 TInt gTotalSectors; |
|
32 TInt gRootDirStart; |
|
33 TInt gRootSector; |
|
34 TInt gRootCluster; |
|
35 TInt gFatStartBytes; |
|
36 TInt gFatTestEntries; |
|
37 TInt gFatSizeSectors; |
|
38 TInt gFirstDataSector; |
|
39 TInt gFirstDataCluster; |
|
40 TInt gMaxDataCluster; |
|
41 TInt gClusterCount; |
|
42 TInt gEndOfChain; |
|
43 |
|
44 /** |
|
45 Class Constructor |
|
46 */ |
|
47 CBaseTestFat32Base::CBaseTestFat32Base() |
|
48 { |
|
49 SetTestStepName(KTestStepBase); |
|
50 } |
|
51 |
|
52 /** |
|
53 Class Destructor |
|
54 */ |
|
55 CBaseTestFat32Base::~CBaseTestFat32Base() |
|
56 { |
|
57 } |
|
58 |
|
59 |
|
60 /** |
|
61 Base step to all test steps which carries out the followng actions: |
|
62 1. Gets the drive letter |
|
63 2. Checks that it is not a RAM drive |
|
64 3. Obtains the size of the disk |
|
65 4. Ensures that the volume is either FAT or FAT32 |
|
66 5. Sets the session path |
|
67 6. Ensures that the number of sectors per cluster is correct |
|
68 7. Calculates the cluster count of the volume. |
|
69 |
|
70 @return EPass if test passes and EFail if test fails |
|
71 */ |
|
72 |
|
73 TVerdict CBaseTestFat32Base::doTestStepPreambleL() |
|
74 { |
|
75 SetTestStepResult(EFail); |
|
76 RDebug::Printf("Starting next test step..."); |
|
77 CheckDebug(); |
|
78 ParseCommandArguments(); |
|
79 iTheFs.Connect(); |
|
80 TInt drvNum = CurrentDrive(); |
|
81 if (!IsFileSystemFAT32() && !IsFileSystemFAT(iTheFs,drvNum)) |
|
82 { |
|
83 _LIT(KNotFat, "Test requires FAT filesystem"); |
|
84 INFO_PRINTF1(KNotFat); |
|
85 return TestStepResult(); |
|
86 } |
|
87 |
|
88 |
|
89 // check this is not the internal ram drive and get the size of the disk |
|
90 TVolumeInfo volumeInfo; |
|
91 TInt res = iTheFs.Volume(volumeInfo, CurrentDrive()); |
|
92 TBool isRamDrive = volumeInfo.iDrive.iMediaAtt&KMediaAttVariableSize; |
|
93 iDiskSize = volumeInfo.iSize; |
|
94 |
|
95 // Obtain the Size of the disk from the media driver |
|
96 TBusLocalDrive mmcDrive; |
|
97 TBool changeFlag; |
|
98 changeFlag=EFalse; |
|
99 TInt r = mmcDrive.Connect(1,changeFlag); |
|
100 TTime startTime; |
|
101 startTime.HomeTime(); |
|
102 TLocalDriveCapsV2 information; |
|
103 TPckg<TLocalDriveCapsV2> pckg(information); |
|
104 r = mmcDrive.Caps(pckg); |
|
105 iDriveSize = information.iSize; |
|
106 |
|
107 |
|
108 // Set the Session Path |
|
109 _LIT(Ksp, "%c:\\"); |
|
110 TBuf<4> sessionPath; |
|
111 sessionPath.Format(Ksp, (TUint)iDriveToTest); |
|
112 r = iTheFs.SetSessionPath(sessionPath); |
|
113 r = iTheFs.SessionPath(iSessionPath); |
|
114 |
|
115 |
|
116 |
|
117 CalculateClusCount(); |
|
118 |
|
119 // Check the Sectors per cluster and Cluster count |
|
120 r = CheckSecPerClus(); |
|
121 if (r != KErrNone) |
|
122 { |
|
123 INFO_PRINTF1(_L("WARNING: Sector Per Cluster check was incorrect - Card should have special format requirements")); |
|
124 iMedia = 1; |
|
125 } |
|
126 else |
|
127 { |
|
128 INFO_PRINTF1(_L("Sector Per Cluster check is correct")); |
|
129 iMedia = 0; |
|
130 } |
|
131 |
|
132 if (isRamDrive) |
|
133 { |
|
134 SetTestStepResult(EAbort); |
|
135 _LIT(KRamNotValid, "Tests not valid on internal ram drive "); |
|
136 INFO_PRINTF1(KRamNotValid); |
|
137 return TestStepResult(); |
|
138 } |
|
139 else |
|
140 SetTestStepResult(EPass); |
|
141 return TestStepResult(); |
|
142 } |
|
143 |
|
144 /** |
|
145 Get the drive letter from the ini file |
|
146 */ |
|
147 void CBaseTestFat32Base::ParseCommandArguments(void) |
|
148 { |
|
149 TBufC<2> driveLetter; |
|
150 TPtrC16 letter = driveLetter; |
|
151 _LIT(KLetter,"DriveLetter"); |
|
152 _LIT(KCommon,"Common"); |
|
153 TBool alright = GetStringFromConfig(KCommon, KLetter, letter); |
|
154 if (alright) |
|
155 { |
|
156 iDriveToTest = letter[0]; |
|
157 iDriveToTest.UpperCase(); |
|
158 } |
|
159 |
|
160 } |
|
161 |
|
162 /** |
|
163 Sets the debug register value for debug builds if specified in the ini file |
|
164 */ |
|
165 void CBaseTestFat32Base::CheckDebug() |
|
166 { |
|
167 TInt debug; |
|
168 _LIT(KDebug,"SetDebug"); |
|
169 _LIT(KCommon,"Common"); |
|
170 TBool alright = GetIntFromConfig(KCommon, KDebug, debug); |
|
171 if (alright) |
|
172 { |
|
173 if (debug == 1) |
|
174 { |
|
175 #ifdef _DEBUG |
|
176 iTheFs.SetDebugRegister(KFSYS); |
|
177 #else |
|
178 _LIT(KNotDebug,"This is not a Debug build"); |
|
179 INFO_PRINTF1(KNotDebug); |
|
180 #endif |
|
181 } |
|
182 else |
|
183 { |
|
184 _LIT(KSpecifyDebug,"Debugging not specified in ini file"); |
|
185 INFO_PRINTF1(KSpecifyDebug); |
|
186 } |
|
187 } |
|
188 |
|
189 } |
|
190 /** |
|
191 Converts the drive letter to the drive number |
|
192 |
|
193 @return driveNum The number of the drive |
|
194 */ |
|
195 TInt CBaseTestFat32Base::CurrentDrive() |
|
196 { |
|
197 TInt driveNum; |
|
198 TInt ret = iTheFs.CharToDrive(iDriveToTest,driveNum); |
|
199 return(driveNum); |
|
200 } |
|
201 |
|
202 |
|
203 /** |
|
204 Check to see whether the volume is FAT32 |
|
205 |
|
206 @return TRUE if is Fat32 else FALSE |
|
207 */ |
|
208 TBool CBaseTestFat32Base:: IsFileSystemFAT32() |
|
209 { |
|
210 // _LIT(KFat32Name,"Fat32"); |
|
211 if(TheBootSector.RootDirEntries() == 0) |
|
212 { |
|
213 gEndOfChain = 0x0FFFFFFF; |
|
214 _LIT(KIsFat32, "Is Fat32 filesystem"); |
|
215 INFO_PRINTF1(KIsFat32); |
|
216 return TRUE; |
|
217 } |
|
218 else |
|
219 { |
|
220 _LIT(KIsNotFat32, "Is Not Fat32 filesystem"); |
|
221 INFO_PRINTF1(KIsNotFat32); |
|
222 return FALSE; |
|
223 } |
|
224 } |
|
225 |
|
226 /** |
|
227 Check to see whether the volume is FAT |
|
228 |
|
229 @return TRUE if is Fat else FALSE |
|
230 */ |
|
231 TBool CBaseTestFat32Base::IsFileSystemFAT(RFs &aFsSession,TInt aDrive) |
|
232 { |
|
233 _LIT(KFatName,"Fat"); |
|
234 TFileName f; |
|
235 TInt r = aFsSession.FileSystemName(f,aDrive); |
|
236 if (r != KErrNone) |
|
237 { |
|
238 _LIT(KNoFatName, "Unable to get file system name"); |
|
239 INFO_PRINTF1(KNoFatName); |
|
240 return FALSE; |
|
241 } |
|
242 return (f.CompareF(KFatName) == 0); |
|
243 } |
|
244 |
|
245 |
|
246 /** |
|
247 Check that the sectors per cluster is correct for the disk size used |
|
248 |
|
249 @return KErrNone if Sectors Per Cluster is correct |
|
250 */ |
|
251 TInt CBaseTestFat32Base::CheckSecPerClus() |
|
252 { |
|
253 TInt64 diskSizeSec = iDiskSize / 512; |
|
254 if (diskSizeSec <= 16777216) |
|
255 { |
|
256 if (iBPB_SecPerClus == 8) |
|
257 return KErrNone; |
|
258 else return -1; |
|
259 } |
|
260 else if (diskSizeSec <= 33554432) |
|
261 { |
|
262 if (iBPB_SecPerClus == 16) |
|
263 return KErrNone; |
|
264 else return -1; |
|
265 } |
|
266 else if (diskSizeSec <= 67108864) |
|
267 { |
|
268 if (iBPB_SecPerClus == 32) |
|
269 return KErrNone; |
|
270 else return -1; |
|
271 } |
|
272 else if (diskSizeSec <= 0xFFFFFFFF) |
|
273 { |
|
274 if (iBPB_SecPerClus == 64) |
|
275 return KErrNone; |
|
276 else return -1; |
|
277 } |
|
278 return KErrNone; |
|
279 } |
|
280 |
|
281 |
|
282 /** |
|
283 Calculate the cluster count of the volume |
|
284 |
|
285 @return KErrNone Successful |
|
286 */ |
|
287 TInt CBaseTestFat32Base::CalculateClusCount() |
|
288 { |
|
289 TInt r; |
|
290 r = TheDisk.Open(iTheFs, CurrentDrive()); |
|
291 if (r != KErrNone) |
|
292 { |
|
293 _LIT(KErrorRead, "Cannot open the raw disk - r=%d"); |
|
294 INFO_PRINTF2(KErrorRead, r); |
|
295 return r; |
|
296 } |
|
297 |
|
298 r = ReadField(4, 32, &iBPB_TotSec32); |
|
299 if (r != KErrNone) |
|
300 { |
|
301 _LIT(KErrorRead, "Cannot get the field value BPB_TotSec32 - r=%d"); |
|
302 INFO_PRINTF2(KErrorRead, r); |
|
303 return r; |
|
304 } |
|
305 r = ReadField(2, 14, &iBPB_ResvdSecCnt); |
|
306 if (r != KErrNone) |
|
307 { |
|
308 _LIT(KErrorReadField, "Cannot get the field value BPB_ResvdSecCnt - r=%d"); |
|
309 INFO_PRINTF2(KErrorReadField, r); |
|
310 return r; |
|
311 } |
|
312 r = ReadField(1, 16, &iBPB_NumFATs); |
|
313 if (r != KErrNone) |
|
314 { |
|
315 _LIT(KErrorReadField, "Cannot get the field value BPB_NumFATs - r=%d"); |
|
316 INFO_PRINTF2(KErrorReadField, r); |
|
317 return r; |
|
318 } |
|
319 r = ReadField(4, 36, &iBPB_FATSz32); |
|
320 if (r != KErrNone) |
|
321 { |
|
322 _LIT(KErrorReadField, "Cannot get the field value BPB_FATSz32 - r=%d"); |
|
323 INFO_PRINTF2(KErrorReadField, r); |
|
324 return r; |
|
325 } |
|
326 r = ReadField(1, 13, &iBPB_SecPerClus); |
|
327 if (r != KErrNone) |
|
328 { |
|
329 _LIT(KErrorReadField, "Cannot get the field value BPB_SecPerClus - r=%d"); |
|
330 INFO_PRINTF2(KErrorReadField, r); |
|
331 return r; |
|
332 } |
|
333 iClusterCount = (iBPB_TotSec32-(iBPB_ResvdSecCnt+(iBPB_NumFATs*iBPB_FATSz32)))/(iBPB_SecPerClus); |
|
334 _LIT(KClusterCountValue, "The cluster count of the volume is %d"); |
|
335 INFO_PRINTF2(KClusterCountValue, iClusterCount); |
|
336 TheDisk.Close(); |
|
337 return KErrNone; |
|
338 } |
|
339 |
|
340 /** |
|
341 Convert the field to the correct endien format |
|
342 |
|
343 @param aLen The length of the field being read |
|
344 @param aBuffer The original endien format of the field |
|
345 @param aField the correct endien format of the field |
|
346 |
|
347 @return KErrNone Successful |
|
348 */ |
|
349 TInt CBaseTestFat32Base::Convert(TInt aLen, TUint8 *aBuffer, TUint32 *aField) |
|
350 { |
|
351 TInt i = aLen-1; |
|
352 *aField = 0; |
|
353 while(i >= 0) |
|
354 { |
|
355 *aField = *aField << 8; |
|
356 *aField = *aField + aBuffer[i]; |
|
357 i--; |
|
358 } |
|
359 return KErrNone; |
|
360 } |
|
361 |
|
362 /** |
|
363 Read the entire field |
|
364 |
|
365 @param aLen The length of the field being read |
|
366 @param aOffSt The position at which to start reading |
|
367 @param aName The entire field to read |
|
368 |
|
369 @return KErrNone Successful |
|
370 */ |
|
371 TInt CBaseTestFat32Base::ReadField(TInt aLen, TInt aOffSet, TUint32 *aName) |
|
372 { |
|
373 TInt r; |
|
374 TUint8 aField[4]; |
|
375 TPtr8 bufPtr(&aField[0], aLen); |
|
376 r=TheDisk.Read(aOffSet, bufPtr); |
|
377 if (r != KErrNone) |
|
378 { |
|
379 _LIT(KErrorRead, "Cannot read the raw disk - r=%d"); |
|
380 INFO_PRINTF2(KErrorRead, r); |
|
381 return r; |
|
382 } |
|
383 r = Convert(aLen, aField,aName); |
|
384 return r; |
|
385 } |
|
386 |
|
387 |
|
388 /** |
|
389 Get the number of bytes per sector |
|
390 |
|
391 @param aSecPerCluster The number of sectors per cluster |
|
392 |
|
393 @return The number of bytes per cluster |
|
394 */ |
|
395 TInt64 getBytesPerCluster(TUint32 aSecPerClus) |
|
396 { |
|
397 TInt64 res = aSecPerClus * 512; |
|
398 return res; |
|
399 } |
|
400 |
|
401 /** |
|
402 Convert the cluster number to byte offset on disk |
|
403 |
|
404 @param aCluster The cluster number |
|
405 |
|
406 @return The offset in bytes |
|
407 */ |
|
408 TInt64 CBaseTestFat32Base::ClusterToByte(TInt aCluster) |
|
409 { |
|
410 TInt64 FirstDataSector = (iBPB_ResvdSecCnt + (iBPB_NumFATs * iBPB_FATSz32)); |
|
411 TInt64 pos; |
|
412 pos = ((aCluster - 2) * iBPB_SecPerClus + FirstDataSector) * 512 ; |
|
413 return pos; |
|
414 } |
|
415 |
|
416 /** |
|
417 Return number of bytes into the FAT |
|
418 |
|
419 @param aFatIndex The Fat index |
|
420 |
|
421 @return The Fat position in bytes |
|
422 */ |
|
423 TInt CBaseTestFat32Base::PosInBytes(TInt aFatIndex) |
|
424 { |
|
425 TInt fatPosInBytes = -1; |
|
426 if(IsFileSystemFAT32()) |
|
427 { |
|
428 gDiskType = EFat32; |
|
429 } |
|
430 switch (gDiskType) |
|
431 { |
|
432 case EFat32: |
|
433 fatPosInBytes=aFatIndex<<2; |
|
434 break; |
|
435 case EFat16: |
|
436 fatPosInBytes=aFatIndex<<1; |
|
437 break; |
|
438 case EFat12: |
|
439 fatPosInBytes=(aFatIndex*3>>1); |
|
440 break; |
|
441 default: |
|
442 break; |
|
443 } |
|
444 return(fatPosInBytes); |
|
445 } |
|
446 |
|
447 |
|