kerneltest/e32test/pccd/t_atadr3.cpp
changeset 9 96e5fb8b040d
child 6 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     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 // e32test\pccd\t_atadr3.cpp
       
    15 // Test the Compact Flash card (ATA) media driver
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <e32svr.h>
       
    21 #include <e32hal.h>
       
    22 #include "u32std.h"
       
    23 #include "../misc/prbs.h"
       
    24 
       
    25 //#define __USE_MUTEX__
       
    26 //#define __DISABLE_KILLER__
       
    27 #define SYSTEM	ETrue
       
    28 
       
    29 const TInt KErrVerify=-100;
       
    30 
       
    31 const TInt KSectorSize=512;
       
    32 const TInt KSectorShift=9;
       
    33 const TInt KVerifyBlockSize=8;	// in sectors
       
    34 
       
    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;
       
    45 LOCAL_D TInt Kills=0;
       
    46 LOCAL_D TInt SectorsWritten=0;
       
    47 LOCAL_D TInt Aborts=0;
       
    48 LOCAL_D TUint WritePattern=0;
       
    49 
       
    50 #ifdef __USE_MUTEX__
       
    51 LOCAL_D RMutex Mutex;
       
    52 #define WAIT		Mutex.Wait()
       
    53 #define SIGNAL		Mutex.Signal()
       
    54 #else
       
    55 #define WAIT
       
    56 #define SIGNAL
       
    57 #endif
       
    58 
       
    59 inline TUint RoundDownToSector(TUint aPos)
       
    60 	{ return aPos&~0x1ff; }
       
    61 inline TUint RoundUpToSector(TUint aPos)
       
    62 	{ return (aPos+0x1ff)&~0x1ff; }
       
    63 
       
    64 LOCAL_C TInt WriteSectors(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
       
    65 	{
       
    66 	WAIT;
       
    67 	TInt r=KErrNotReady;
       
    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);
       
    72 	SIGNAL;
       
    73 	return r;
       
    74 	}
       
    75 
       
    76 LOCAL_C TInt ReadSectors(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
       
    77 	{
       
    78 	WAIT;
       
    79 	TInt r=KErrNotReady;
       
    80 	TInt pos=aSector<<KSectorShift;
       
    81 	TInt len=aCount*KSectorSize;
       
    82 	TPtr8 p(aBuf,0,len);
       
    83 	while (r==KErrNotReady || r==KErrAbort)
       
    84 		r=aDrive.Read(pos,len,p);
       
    85 	if (r==KErrNone && p.Length()!=len)
       
    86 		r=KErrUnderflow;
       
    87 	SIGNAL;
       
    88 	return r;
       
    89 	}
       
    90 
       
    91 LOCAL_C void GenerateTestPattern(TInt aSector, TUint aState, TUint8* aBuf)
       
    92 	{
       
    93 	TUint seed[2];
       
    94 	seed[1]=0;
       
    95 	seed[0]=TUint(aSector)^(aState<<16);
       
    96 	*aBuf++=TUint8(aState&0xff);
       
    97 	*aBuf++=TUint8((aState>>8)&0xff);
       
    98 	TInt i;
       
    99 	for (i=0; i<KSectorSize-2; i++)
       
   100 		*aBuf++=TUint8(Random(seed));
       
   101 	}
       
   102 
       
   103 LOCAL_C void Write(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
       
   104 	{
       
   105 	TInt n;
       
   106 	for (n=0; n<aCount; n++)
       
   107 		GenerateTestPattern(aSector+n,WritePattern,aBuf+n*KSectorSize);
       
   108 	TInt r=WriteSectors(aDrive,aSector,aCount,aBuf);
       
   109 	if (r!=KErrNone)
       
   110 		User::Panic(_L("WRITE"),r);
       
   111 	}
       
   112 
       
   113 LOCAL_C void Write(TBusLocalDrive& aDrive, TInt aSector, TInt aCount)
       
   114 	{
       
   115 	const TInt KMaxLen = KSectorSize * 8;
       
   116 	TInt len=aCount*KSectorSize;
       
   117 	test (len <= KMaxLen);
       
   118 	TUint8 buf[KMaxLen];
       
   119 	Write(aDrive,aSector,aCount,buf);
       
   120 	}
       
   121 
       
   122 LOCAL_C TInt Verify(TInt aSector, TInt aCount, TUint8* aBuf)
       
   123 	{
       
   124 	TUint32 buf[KSectorSize/4];
       
   125 	TUint8* pB=(TUint8*)buf;
       
   126 	while(aCount--)
       
   127 		{
       
   128 		TUint state=aBuf[0]|(aBuf[1]<<8);
       
   129 		GenerateTestPattern(aSector,state,pB);
       
   130 		if (Mem::Compare(aBuf,KSectorSize,pB,KSectorSize)!=0)
       
   131 			return KErrVerify;
       
   132 		++aSector;
       
   133 		aBuf+=KSectorSize;
       
   134 		}
       
   135 	return KErrNone;
       
   136 	}
       
   137 
       
   138 LOCAL_C TInt Verify(TBusLocalDrive& aDrive, TInt aSector, TInt aCount)
       
   139 	{
       
   140 	const TInt KMaxLen = KSectorSize * 8;
       
   141 	TInt len=aCount*KSectorSize;
       
   142 	test (len <= KMaxLen);
       
   143 	TUint8 buf[KMaxLen];
       
   144 	TInt r=ReadSectors(aDrive,aSector,aCount,buf);
       
   145 	if (r!=KErrNone)
       
   146 		return r;
       
   147 	return Verify(aSector,aCount,buf);
       
   148 	}
       
   149 
       
   150 LOCAL_C TInt WriterThread(TAny*)
       
   151 	{
       
   152 /* 
       
   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);
       
   156  */
       
   157 	TUint seed[2];
       
   158 	seed[0]=User::NTickCount();
       
   159 	seed[1]=0;
       
   160 	TUint32 buf[8*KSectorSize/4];
       
   161 	TUint8* pB=(TUint8*)buf;
       
   162 	TBool medChg=EFalse;
       
   163 	WriterDrive.Close();	// close handle from previous incarnation of this thread
       
   164 	TInt r=WriterDrive.Connect(DriveNumber,medChg);
       
   165 	if (r!=KErrNone)
       
   166 		User::Panic(_L("WRITER-CONNECT"),r);
       
   167 	FOREVER
       
   168 		{
       
   169 		TInt remain=DriveSizeInSectors-CurrentSector;
       
   170 		TInt n=Random(seed)&15;
       
   171 		if (n>8 || n==0)
       
   172 			n=1;
       
   173 		if (n>remain)
       
   174 			n=remain;
       
   175 		if (n==1)
       
   176 			{
       
   177 			Write(WriterDrive,CurrentSector,1,pB);
       
   178 			++WritePattern;
       
   179 			Write(WriterDrive,CurrentSector,1,pB+KSectorSize);
       
   180 			SectorsWritten+=2;
       
   181 			}
       
   182 		else
       
   183 			{
       
   184 			Write(WriterDrive,CurrentSector,n,pB);
       
   185 			SectorsWritten+=n;
       
   186 			}
       
   187 		CurrentSector+=n;
       
   188 		if (CurrentSector==DriveSizeInSectors)
       
   189 			{
       
   190 			CurrentSector=0;
       
   191 			++WritePattern;
       
   192 			}
       
   193 		}
       
   194 	}
       
   195 
       
   196 LOCAL_C TInt KillerThread(TAny*)
       
   197 	{
       
   198 /* 
       
   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);
       
   202  */
       
   203 	TUint seed[2];
       
   204 	seed[0]=0xadf85458;
       
   205 	seed[1]=0;
       
   206 	TBusLocalDrive drive;
       
   207 	TBool medChg=EFalse;
       
   208 	TInt r=drive.Connect(DriveNumber,medChg);
       
   209 	if (r!=KErrNone)
       
   210 		User::Panic(_L("KILLER-CONNECT"),r);
       
   211 	RTimer timer;
       
   212 	r=timer.CreateLocal();
       
   213 	if (r!=KErrNone)
       
   214 		User::Panic(_L("KILLER-TIMER"),r);
       
   215 	TInt action=0;
       
   216 	FOREVER
       
   217 		{
       
   218 		TUint x=Random(seed);
       
   219 		TUint ms=1000+(x&4095);
       
   220 		User::AfterHighRes(ms*1000);
       
   221 		switch (action)
       
   222 			{
       
   223 			case 0:
       
   224 #ifndef __DISABLE_KILLER__
       
   225 				drive.ForceMediaChange();
       
   226 #endif
       
   227 				++MediaChanges;
       
   228 				break;
       
   229 			case 1:
       
   230 				{
       
   231 #ifndef __DISABLE_KILLER__
       
   232 				TTime now;
       
   233 				now.HomeTime();
       
   234 				now+=TTimeIntervalSeconds(2);
       
   235 				TRequestStatus s;
       
   236 				timer.At(s,now);
       
   237 				UserHal::SwitchOff();
       
   238 				User::WaitForRequest(s);
       
   239 				++PowerDowns;
       
   240 #endif
       
   241 				break;
       
   242 				}
       
   243 			case 2:
       
   244 #ifndef __DISABLE_KILLER__
       
   245 				TheWriterThread.Kill(0);
       
   246 #endif
       
   247 				++Kills;
       
   248 #ifndef __DISABLE_KILLER__
       
   249 				++WritePattern;
       
   250 				Sem.Wait();
       
   251 				TheWriterThread.SetPriority(EPriorityNormal);
       
   252 #endif
       
   253 				break;
       
   254 			case 3:
       
   255 #ifndef __DISABLE_KILLER__
       
   256 				drive.ForceMediaChange();
       
   257 #endif
       
   258 				++MediaChanges;
       
   259 #ifndef __DISABLE_KILLER__
       
   260 				x=Random(seed);
       
   261 				ms=50+(x&511);
       
   262 				User::AfterHighRes(ms*1000);
       
   263 				TheWriterThread.Kill(0);
       
   264 #endif
       
   265 				++Kills;
       
   266 #ifndef __DISABLE_KILLER__
       
   267 				++WritePattern;
       
   268 				Sem.Wait();
       
   269 				TheWriterThread.SetPriority(EPriorityNormal);
       
   270 #endif
       
   271 				break;
       
   272 			case 4:
       
   273 				break;
       
   274 			}
       
   275 		if (++action==5)
       
   276 			action=0;
       
   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);
       
   282 		if (r!=KErrNone)
       
   283 			User::Panic(_L("VERIFY"),r);
       
   284 #endif
       
   285 		}
       
   286 	}
       
   287 
       
   288 LOCAL_C TInt CreateKillerThread()
       
   289 	{
       
   290 	TInt r=TheKillerThread.Create(_L("Killer"),KillerThread,0x2000,NULL,NULL);
       
   291 	if (r!=KErrNone)
       
   292 		return r;
       
   293 	TheKillerThread.SetPriority(EPriorityMore);
       
   294 	TheKillerThread.Resume();
       
   295 	return KErrNone;
       
   296 	}
       
   297 
       
   298 LOCAL_C TInt CreateWriterThread()
       
   299 	{
       
   300 	FOREVER
       
   301 		{
       
   302 		TInt r=TheWriterThread.Create(_L("Writer"),WriterThread,0x2000,NULL,NULL);
       
   303 		if (r==KErrNone)
       
   304 			break;
       
   305 		if (r!=KErrAlreadyExists)
       
   306 			return r;
       
   307 		test.Printf(_L("Writer thread still exists\n"));
       
   308 		User::After(200000);
       
   309 		}
       
   310 	TheWriterThread.SetPriority(EPriorityLess);
       
   311 	TheWriterThread.Resume();
       
   312 	return KErrNone;
       
   313 	}
       
   314 
       
   315 GLDEF_C TInt E32Main()
       
   316 	{
       
   317 /* 
       
   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);
       
   321  */
       
   322 	WriterDrive.SetHandle(0);
       
   323 	test.Title();
       
   324 	TInt drv=-1;
       
   325 	while (drv<0 || drv>=KMaxLocalDrives)
       
   326 		{
       
   327 		test.Printf(_L("\nSelect drive C-K: "));
       
   328 		TChar c=(TUint)test.Getch();
       
   329 		c.UpperCase();
       
   330 		if (c.IsAlpha())
       
   331 			drv=TUint(c)-'C';
       
   332 		else
       
   333 			drv=-1;
       
   334 		}
       
   335 	TBuf<1> b;
       
   336 	b.SetLength(1);
       
   337 	b[0]=(TText)(drv+'C');
       
   338 	test.Printf(_L("%S\n"),&b);
       
   339 
       
   340 	TBuf<80> buf=_L("Connect to drive ");
       
   341 	buf+=b;
       
   342 	test.Start(buf);
       
   343 	TBusLocalDrive drive;
       
   344 	TBool medChg=EFalse;
       
   345 	TInt r=drive.Connect(drv,medChg);
       
   346 	test(r==KErrNone);
       
   347 	DriveNumber=drv;
       
   348 
       
   349 	test.Next(_L("Get capabilities"));
       
   350 	TLocalDriveCapsV2 driveCaps;
       
   351 	TPckg<TLocalDriveCapsV2> capsPckg(driveCaps);
       
   352 	r=drive.Caps(capsPckg);
       
   353 	test(r==KErrNone);
       
   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("Battery state    = %d\n"),driveCaps.iBattery);
       
   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"));
       
   365 	test.Getch();
       
   366 
       
   367 #ifdef __USE_MUTEX__
       
   368 	test.Next(_L("Create mutex"));
       
   369 	r=Mutex.CreateLocal();
       
   370 	test(r==KErrNone);
       
   371 #endif
       
   372 
       
   373 	test.Next(_L("Initialise drive"));
       
   374 	TInt sector;
       
   375 	for (sector=0; sector<DriveSizeInSectors; sector+=8)
       
   376 		{
       
   377 		Write(drive,sector,8);
       
   378 		if ((sector&127)==0)
       
   379 			test.Printf(_L("."));
       
   380 		}
       
   381 	test.Printf(_L("\n"));
       
   382 	test.Next(_L("Verify drive"));
       
   383 	for (sector=0; sector<DriveSizeInSectors; sector+=8)
       
   384 		{
       
   385 		test(Verify(drive,sector,8)==KErrNone);
       
   386 		if ((sector&127)==0)
       
   387 			test.Printf(_L("."));
       
   388 		}
       
   389 
       
   390 	test.Printf(_L("\n\nPress ENTER to continue..."));
       
   391 	TKeyCode k=EKeyNull;
       
   392 	while (k!=EKeyEnter)
       
   393 		k=test.Getch();
       
   394 	test.Printf(_L("\n"));
       
   395 
       
   396 	test.Next(_L("Create semaphore"));
       
   397 	r=Sem.CreateLocal(0);
       
   398 	test(r==KErrNone);
       
   399 	test.Next(_L("Create writer thread"));
       
   400 	r=CreateWriterThread();
       
   401 	test(r==KErrNone);
       
   402 	TheWriterThread.SetPriority(EPriorityNormal);
       
   403 	test.Next(_L("Create killer thread"));
       
   404 	r=CreateKillerThread();
       
   405 	test(r==KErrNone);
       
   406 
       
   407 	TBool exit=EFalse;
       
   408 	sector=0;
       
   409 	TInt verifies=0;
       
   410 	TInt fails=0;
       
   411 	while (!exit)
       
   412 		{
       
   413 		r=Verify(drive,sector,KVerifyBlockSize);
       
   414 		if (r!=KErrNone)
       
   415 			{
       
   416 			++fails;
       
   417 			test.Printf(_L("Sector %d Fail %d\n"),sector,r);
       
   418 			}
       
   419 		else
       
   420 			++verifies;
       
   421 		sector+=KVerifyBlockSize;
       
   422 		if (sector==DriveSizeInSectors)
       
   423 			{
       
   424 			sector=0;
       
   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);
       
   426 			}
       
   427 		if ((sector&63)==0)
       
   428 			{
       
   429 			if (TheKillerThread.ExitType()!=EExitPending)
       
   430 				{
       
   431 				const TDesC& cat=TheKillerThread.ExitCategory();
       
   432 				test.Printf(_L("KillerThread exited %d,%d,%S\n"),TheKillerThread.ExitType(),
       
   433 														TheKillerThread.ExitReason(),&cat);
       
   434 				break;
       
   435 				}
       
   436 			TExitType xt=TheWriterThread.ExitType();
       
   437 			if (xt==EExitPanic)
       
   438 				{
       
   439 				const TDesC& cat=TheWriterThread.ExitCategory();
       
   440 				test.Printf(_L("WriterThread Panic %S %d\n"),&cat,TheWriterThread.ExitReason());
       
   441 				break;
       
   442 				}
       
   443 			if (xt!=EExitPending)
       
   444 				{
       
   445 				// restart writer thread
       
   446 				TheWriterThread.Close();
       
   447 				r=CreateWriterThread();
       
   448 				if (r!=KErrNone)
       
   449 					{
       
   450 					test.Printf(_L("Restart writer thread failed %d\n"),r);
       
   451 					break;
       
   452 					}
       
   453 				Sem.Signal();
       
   454 				}
       
   455 			}
       
   456 		}
       
   457 
       
   458 	buf=_L("Disconnect from drive ");
       
   459 	buf+=b;
       
   460 	test.Next(buf);
       
   461 	drive.Disconnect();
       
   462 	test.End();
       
   463 	return 0;
       
   464 	}