Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\pccd\t_atadr3.cpp
15 // Test the Compact Flash card (ATA) media driver
23 #include "../misc/prbs.h"
25 //#define __USE_MUTEX__
26 //#define __DISABLE_KILLER__
29 const TInt KErrVerify=-100;
31 const TInt KSectorSize=512;
32 const TInt KSectorShift=9;
33 const TInt KVerifyBlockSize=8; // in sectors
35 LOCAL_D TBusLocalDrive WriterDrive;
36 LOCAL_D RTest test(_L("T_ATADR3"));
37 LOCAL_D TInt DriveNumber;
38 LOCAL_D TInt DriveSizeInSectors;
39 LOCAL_D RThread TheWriterThread;
40 LOCAL_D RThread TheKillerThread;
41 LOCAL_D RSemaphore Sem;
42 LOCAL_D TInt CurrentSector=0;
43 LOCAL_D TInt MediaChanges=0;
44 LOCAL_D TInt PowerDowns=0;
46 LOCAL_D TInt SectorsWritten=0;
47 LOCAL_D TInt Aborts=0;
48 LOCAL_D TUint WritePattern=0;
52 #define WAIT Mutex.Wait()
53 #define SIGNAL Mutex.Signal()
59 inline TUint RoundDownToSector(TUint aPos)
60 { return aPos&~0x1ff; }
61 inline TUint RoundUpToSector(TUint aPos)
62 { return (aPos+0x1ff)&~0x1ff; }
64 LOCAL_C TInt WriteSectors(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
68 TInt pos=aSector<<KSectorShift;
69 TPtrC8 p(aBuf,aCount*KSectorSize);
70 while (r==KErrNotReady || (r==KErrAbort && (++Aborts,1)))
71 r=aDrive.Write(pos,p);
76 LOCAL_C TInt ReadSectors(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
80 TInt pos=aSector<<KSectorShift;
81 TInt len=aCount*KSectorSize;
83 while (r==KErrNotReady || r==KErrAbort)
84 r=aDrive.Read(pos,len,p);
85 if (r==KErrNone && p.Length()!=len)
91 LOCAL_C void GenerateTestPattern(TInt aSector, TUint aState, TUint8* aBuf)
95 seed[0]=TUint(aSector)^(aState<<16);
96 *aBuf++=TUint8(aState&0xff);
97 *aBuf++=TUint8((aState>>8)&0xff);
99 for (i=0; i<KSectorSize-2; i++)
100 *aBuf++=TUint8(Random(seed));
103 LOCAL_C void Write(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
106 for (n=0; n<aCount; n++)
107 GenerateTestPattern(aSector+n,WritePattern,aBuf+n*KSectorSize);
108 TInt r=WriteSectors(aDrive,aSector,aCount,aBuf);
110 User::Panic(_L("WRITE"),r);
113 LOCAL_C void Write(TBusLocalDrive& aDrive, TInt aSector, TInt aCount)
115 const TInt KMaxLen = KSectorSize * 8;
116 TInt len=aCount*KSectorSize;
117 test (len <= KMaxLen);
119 Write(aDrive,aSector,aCount,buf);
122 LOCAL_C TInt Verify(TInt aSector, TInt aCount, TUint8* aBuf)
124 TUint32 buf[KSectorSize/4];
125 TUint8* pB=(TUint8*)buf;
128 TUint state=aBuf[0]|(aBuf[1]<<8);
129 GenerateTestPattern(aSector,state,pB);
130 if (Mem::Compare(aBuf,KSectorSize,pB,KSectorSize)!=0)
138 LOCAL_C TInt Verify(TBusLocalDrive& aDrive, TInt aSector, TInt aCount)
140 const TInt KMaxLen = KSectorSize * 8;
141 TInt len=aCount*KSectorSize;
142 test (len <= KMaxLen);
144 TInt r=ReadSectors(aDrive,aSector,aCount,buf);
147 return Verify(aSector,aCount,buf);
150 LOCAL_C TInt WriterThread(TAny*)
153 * SetSystem() API was removed by __SECURE_API__
154 * "Systemize" will be implemented later calling a LDD which will set thread's "system" flag
155 RThread().SetSystem(SYSTEM);
158 seed[0]=User::NTickCount();
160 TUint32 buf[8*KSectorSize/4];
161 TUint8* pB=(TUint8*)buf;
163 WriterDrive.Close(); // close handle from previous incarnation of this thread
164 TInt r=WriterDrive.Connect(DriveNumber,medChg);
166 User::Panic(_L("WRITER-CONNECT"),r);
169 TInt remain=DriveSizeInSectors-CurrentSector;
170 TInt n=Random(seed)&15;
177 Write(WriterDrive,CurrentSector,1,pB);
179 Write(WriterDrive,CurrentSector,1,pB+KSectorSize);
184 Write(WriterDrive,CurrentSector,n,pB);
188 if (CurrentSector==DriveSizeInSectors)
196 LOCAL_C TInt KillerThread(TAny*)
199 * SetSystem() API was removed by __SECURE_API__
200 * "Systemize" will be implemented later calling a LDD which will set thread's "system" flag
201 RThread().SetSystem(SYSTEM);
206 TBusLocalDrive drive;
208 TInt r=drive.Connect(DriveNumber,medChg);
210 User::Panic(_L("KILLER-CONNECT"),r);
212 r=timer.CreateLocal();
214 User::Panic(_L("KILLER-TIMER"),r);
218 TUint x=Random(seed);
219 TUint ms=1000+(x&4095);
220 User::AfterHighRes(ms*1000);
224 #ifndef __DISABLE_KILLER__
225 drive.ForceMediaChange();
231 #ifndef __DISABLE_KILLER__
234 now+=TTimeIntervalSeconds(2);
237 UserHal::SwitchOff();
238 User::WaitForRequest(s);
244 #ifndef __DISABLE_KILLER__
245 TheWriterThread.Kill(0);
248 #ifndef __DISABLE_KILLER__
251 TheWriterThread.SetPriority(EPriorityNormal);
255 #ifndef __DISABLE_KILLER__
256 drive.ForceMediaChange();
259 #ifndef __DISABLE_KILLER__
262 User::AfterHighRes(ms*1000);
263 TheWriterThread.Kill(0);
266 #ifndef __DISABLE_KILLER__
269 TheWriterThread.SetPriority(EPriorityNormal);
277 #ifndef __DISABLE_KILLER__
278 TInt curr=CurrentSector;
279 TInt remain=DriveSizeInSectors-curr;
280 TInt n=(remain<8)?remain:8;
281 r=Verify(drive,curr,n);
283 User::Panic(_L("VERIFY"),r);
288 LOCAL_C TInt CreateKillerThread()
290 TInt r=TheKillerThread.Create(_L("Killer"),KillerThread,0x2000,NULL,NULL);
293 TheKillerThread.SetPriority(EPriorityMore);
294 TheKillerThread.Resume();
298 LOCAL_C TInt CreateWriterThread()
302 TInt r=TheWriterThread.Create(_L("Writer"),WriterThread,0x2000,NULL,NULL);
305 if (r!=KErrAlreadyExists)
307 test.Printf(_L("Writer thread still exists\n"));
310 TheWriterThread.SetPriority(EPriorityLess);
311 TheWriterThread.Resume();
315 GLDEF_C TInt E32Main()
318 * SetSystem() API was removed by __SECURE_API__
319 * "Systemize" will be implemented later calling a LDD which will set thread's "system" flag
320 RThread().SetSystem(SYSTEM);
322 WriterDrive.SetHandle(0);
325 while (drv<0 || drv>=KMaxLocalDrives)
327 test.Printf(_L("\nSelect drive C-K: "));
328 TChar c=(TUint)test.Getch();
337 b[0]=(TText)(drv+'C');
338 test.Printf(_L("%S\n"),&b);
340 TBuf<80> buf=_L("Connect to drive ");
343 TBusLocalDrive drive;
345 TInt r=drive.Connect(drv,medChg);
349 test.Next(_L("Get capabilities"));
350 TLocalDriveCapsV2 driveCaps;
351 TPckg<TLocalDriveCapsV2> capsPckg(driveCaps);
352 r=drive.Caps(capsPckg);
354 TUint driveSize=I64LOW(driveCaps.iSize);
355 DriveSizeInSectors=(driveSize&~0xfff)>>KSectorShift; // round down to multiple of 8 sectors
356 test.Printf(_L("Drive size = %08x (%dK)\n"),driveSize,driveSize>>10);
357 test.Printf(_L("Media type = %d\n"),driveCaps.iType);
358 test.Printf(_L("Connection Bus = %d\n"),driveCaps.iConnectionBusType);
359 test.Printf(_L("Drive attributes = %08x\n"),driveCaps.iDriveAtt);
360 test.Printf(_L("Media attributes = %08x\n"),driveCaps.iMediaAtt);
361 test.Printf(_L("Base address = %08x\n"),driveCaps.iBaseAddress);
362 test.Printf(_L("File system ID = %08x\n"),driveCaps.iFileSystemId);
363 test.Printf(_L("Hidden sectors = %08x\n"),driveCaps.iHiddenSectors);
364 test.Printf(_L("Press any key...\n"));
368 test.Next(_L("Create mutex"));
369 r=Mutex.CreateLocal();
373 test.Next(_L("Initialise drive"));
375 for (sector=0; sector<DriveSizeInSectors; sector+=8)
377 Write(drive,sector,8);
379 test.Printf(_L("."));
381 test.Printf(_L("\n"));
382 test.Next(_L("Verify drive"));
383 for (sector=0; sector<DriveSizeInSectors; sector+=8)
385 test(Verify(drive,sector,8)==KErrNone);
387 test.Printf(_L("."));
390 test.Printf(_L("\n\nPress ENTER to continue..."));
394 test.Printf(_L("\n"));
396 test.Next(_L("Create semaphore"));
397 r=Sem.CreateLocal(0);
399 test.Next(_L("Create writer thread"));
400 r=CreateWriterThread();
402 TheWriterThread.SetPriority(EPriorityNormal);
403 test.Next(_L("Create killer thread"));
404 r=CreateKillerThread();
413 r=Verify(drive,sector,KVerifyBlockSize);
417 test.Printf(_L("Sector %d Fail %d\n"),sector,r);
421 sector+=KVerifyBlockSize;
422 if (sector==DriveSizeInSectors)
425 test.Printf(_L("W %d VER %d FAIL %d MC %d PD %d K %d A %d\n"),SectorsWritten,verifies,fails,MediaChanges,PowerDowns,Kills,Aborts);
429 if (TheKillerThread.ExitType()!=EExitPending)
431 const TDesC& cat=TheKillerThread.ExitCategory();
432 test.Printf(_L("KillerThread exited %d,%d,%S\n"),TheKillerThread.ExitType(),
433 TheKillerThread.ExitReason(),&cat);
436 TExitType xt=TheWriterThread.ExitType();
439 const TDesC& cat=TheWriterThread.ExitCategory();
440 test.Printf(_L("WriterThread Panic %S %d\n"),&cat,TheWriterThread.ExitReason());
443 if (xt!=EExitPending)
445 // restart writer thread
446 TheWriterThread.Close();
447 r=CreateWriterThread();
450 test.Printf(_L("Restart writer thread failed %d\n"),r);
458 buf=_L("Disconnect from drive ");