os/kernelhwsrv/kerneltest/e32test/pccd/t_atadr3.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/pccd/t_atadr3.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,464 @@
     1.4 +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\pccd\t_atadr3.cpp
    1.18 +// Test the Compact Flash card (ATA) media driver
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#include <e32test.h>
    1.23 +#include <e32svr.h>
    1.24 +#include <e32hal.h>
    1.25 +#include "u32std.h"
    1.26 +#include "../misc/prbs.h"
    1.27 +
    1.28 +//#define __USE_MUTEX__
    1.29 +//#define __DISABLE_KILLER__
    1.30 +#define SYSTEM	ETrue
    1.31 +
    1.32 +const TInt KErrVerify=-100;
    1.33 +
    1.34 +const TInt KSectorSize=512;
    1.35 +const TInt KSectorShift=9;
    1.36 +const TInt KVerifyBlockSize=8;	// in sectors
    1.37 +
    1.38 +LOCAL_D TBusLocalDrive WriterDrive;
    1.39 +LOCAL_D RTest test(_L("T_ATADR3"));
    1.40 +LOCAL_D TInt DriveNumber;
    1.41 +LOCAL_D TInt DriveSizeInSectors;
    1.42 +LOCAL_D RThread TheWriterThread;
    1.43 +LOCAL_D RThread TheKillerThread;
    1.44 +LOCAL_D RSemaphore Sem;
    1.45 +LOCAL_D TInt CurrentSector=0;
    1.46 +LOCAL_D TInt MediaChanges=0;
    1.47 +LOCAL_D TInt PowerDowns=0;
    1.48 +LOCAL_D TInt Kills=0;
    1.49 +LOCAL_D TInt SectorsWritten=0;
    1.50 +LOCAL_D TInt Aborts=0;
    1.51 +LOCAL_D TUint WritePattern=0;
    1.52 +
    1.53 +#ifdef __USE_MUTEX__
    1.54 +LOCAL_D RMutex Mutex;
    1.55 +#define WAIT		Mutex.Wait()
    1.56 +#define SIGNAL		Mutex.Signal()
    1.57 +#else
    1.58 +#define WAIT
    1.59 +#define SIGNAL
    1.60 +#endif
    1.61 +
    1.62 +inline TUint RoundDownToSector(TUint aPos)
    1.63 +	{ return aPos&~0x1ff; }
    1.64 +inline TUint RoundUpToSector(TUint aPos)
    1.65 +	{ return (aPos+0x1ff)&~0x1ff; }
    1.66 +
    1.67 +LOCAL_C TInt WriteSectors(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
    1.68 +	{
    1.69 +	WAIT;
    1.70 +	TInt r=KErrNotReady;
    1.71 +	TInt pos=aSector<<KSectorShift;
    1.72 +	TPtrC8 p(aBuf,aCount*KSectorSize);
    1.73 +	while (r==KErrNotReady || (r==KErrAbort && (++Aborts,1)))
    1.74 +		r=aDrive.Write(pos,p);
    1.75 +	SIGNAL;
    1.76 +	return r;
    1.77 +	}
    1.78 +
    1.79 +LOCAL_C TInt ReadSectors(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
    1.80 +	{
    1.81 +	WAIT;
    1.82 +	TInt r=KErrNotReady;
    1.83 +	TInt pos=aSector<<KSectorShift;
    1.84 +	TInt len=aCount*KSectorSize;
    1.85 +	TPtr8 p(aBuf,0,len);
    1.86 +	while (r==KErrNotReady || r==KErrAbort)
    1.87 +		r=aDrive.Read(pos,len,p);
    1.88 +	if (r==KErrNone && p.Length()!=len)
    1.89 +		r=KErrUnderflow;
    1.90 +	SIGNAL;
    1.91 +	return r;
    1.92 +	}
    1.93 +
    1.94 +LOCAL_C void GenerateTestPattern(TInt aSector, TUint aState, TUint8* aBuf)
    1.95 +	{
    1.96 +	TUint seed[2];
    1.97 +	seed[1]=0;
    1.98 +	seed[0]=TUint(aSector)^(aState<<16);
    1.99 +	*aBuf++=TUint8(aState&0xff);
   1.100 +	*aBuf++=TUint8((aState>>8)&0xff);
   1.101 +	TInt i;
   1.102 +	for (i=0; i<KSectorSize-2; i++)
   1.103 +		*aBuf++=TUint8(Random(seed));
   1.104 +	}
   1.105 +
   1.106 +LOCAL_C void Write(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
   1.107 +	{
   1.108 +	TInt n;
   1.109 +	for (n=0; n<aCount; n++)
   1.110 +		GenerateTestPattern(aSector+n,WritePattern,aBuf+n*KSectorSize);
   1.111 +	TInt r=WriteSectors(aDrive,aSector,aCount,aBuf);
   1.112 +	if (r!=KErrNone)
   1.113 +		User::Panic(_L("WRITE"),r);
   1.114 +	}
   1.115 +
   1.116 +LOCAL_C void Write(TBusLocalDrive& aDrive, TInt aSector, TInt aCount)
   1.117 +	{
   1.118 +	const TInt KMaxLen = KSectorSize * 8;
   1.119 +	TInt len=aCount*KSectorSize;
   1.120 +	test (len <= KMaxLen);
   1.121 +	TUint8 buf[KMaxLen];
   1.122 +	Write(aDrive,aSector,aCount,buf);
   1.123 +	}
   1.124 +
   1.125 +LOCAL_C TInt Verify(TInt aSector, TInt aCount, TUint8* aBuf)
   1.126 +	{
   1.127 +	TUint32 buf[KSectorSize/4];
   1.128 +	TUint8* pB=(TUint8*)buf;
   1.129 +	while(aCount--)
   1.130 +		{
   1.131 +		TUint state=aBuf[0]|(aBuf[1]<<8);
   1.132 +		GenerateTestPattern(aSector,state,pB);
   1.133 +		if (Mem::Compare(aBuf,KSectorSize,pB,KSectorSize)!=0)
   1.134 +			return KErrVerify;
   1.135 +		++aSector;
   1.136 +		aBuf+=KSectorSize;
   1.137 +		}
   1.138 +	return KErrNone;
   1.139 +	}
   1.140 +
   1.141 +LOCAL_C TInt Verify(TBusLocalDrive& aDrive, TInt aSector, TInt aCount)
   1.142 +	{
   1.143 +	const TInt KMaxLen = KSectorSize * 8;
   1.144 +	TInt len=aCount*KSectorSize;
   1.145 +	test (len <= KMaxLen);
   1.146 +	TUint8 buf[KMaxLen];
   1.147 +	TInt r=ReadSectors(aDrive,aSector,aCount,buf);
   1.148 +	if (r!=KErrNone)
   1.149 +		return r;
   1.150 +	return Verify(aSector,aCount,buf);
   1.151 +	}
   1.152 +
   1.153 +LOCAL_C TInt WriterThread(TAny*)
   1.154 +	{
   1.155 +/* 
   1.156 + * SetSystem() API was removed by __SECURE_API__	
   1.157 + *	"Systemize" will be implemented later calling a LDD which will set thread's "system" flag
   1.158 +	RThread().SetSystem(SYSTEM);
   1.159 + */
   1.160 +	TUint seed[2];
   1.161 +	seed[0]=User::NTickCount();
   1.162 +	seed[1]=0;
   1.163 +	TUint32 buf[8*KSectorSize/4];
   1.164 +	TUint8* pB=(TUint8*)buf;
   1.165 +	TBool medChg=EFalse;
   1.166 +	WriterDrive.Close();	// close handle from previous incarnation of this thread
   1.167 +	TInt r=WriterDrive.Connect(DriveNumber,medChg);
   1.168 +	if (r!=KErrNone)
   1.169 +		User::Panic(_L("WRITER-CONNECT"),r);
   1.170 +	FOREVER
   1.171 +		{
   1.172 +		TInt remain=DriveSizeInSectors-CurrentSector;
   1.173 +		TInt n=Random(seed)&15;
   1.174 +		if (n>8 || n==0)
   1.175 +			n=1;
   1.176 +		if (n>remain)
   1.177 +			n=remain;
   1.178 +		if (n==1)
   1.179 +			{
   1.180 +			Write(WriterDrive,CurrentSector,1,pB);
   1.181 +			++WritePattern;
   1.182 +			Write(WriterDrive,CurrentSector,1,pB+KSectorSize);
   1.183 +			SectorsWritten+=2;
   1.184 +			}
   1.185 +		else
   1.186 +			{
   1.187 +			Write(WriterDrive,CurrentSector,n,pB);
   1.188 +			SectorsWritten+=n;
   1.189 +			}
   1.190 +		CurrentSector+=n;
   1.191 +		if (CurrentSector==DriveSizeInSectors)
   1.192 +			{
   1.193 +			CurrentSector=0;
   1.194 +			++WritePattern;
   1.195 +			}
   1.196 +		}
   1.197 +	}
   1.198 +
   1.199 +LOCAL_C TInt KillerThread(TAny*)
   1.200 +	{
   1.201 +/* 
   1.202 + * SetSystem() API was removed by __SECURE_API__	
   1.203 + *	"Systemize" will be implemented later calling a LDD which will set thread's "system" flag
   1.204 + 	RThread().SetSystem(SYSTEM);
   1.205 + */
   1.206 +	TUint seed[2];
   1.207 +	seed[0]=0xadf85458;
   1.208 +	seed[1]=0;
   1.209 +	TBusLocalDrive drive;
   1.210 +	TBool medChg=EFalse;
   1.211 +	TInt r=drive.Connect(DriveNumber,medChg);
   1.212 +	if (r!=KErrNone)
   1.213 +		User::Panic(_L("KILLER-CONNECT"),r);
   1.214 +	RTimer timer;
   1.215 +	r=timer.CreateLocal();
   1.216 +	if (r!=KErrNone)
   1.217 +		User::Panic(_L("KILLER-TIMER"),r);
   1.218 +	TInt action=0;
   1.219 +	FOREVER
   1.220 +		{
   1.221 +		TUint x=Random(seed);
   1.222 +		TUint ms=1000+(x&4095);
   1.223 +		User::AfterHighRes(ms*1000);
   1.224 +		switch (action)
   1.225 +			{
   1.226 +			case 0:
   1.227 +#ifndef __DISABLE_KILLER__
   1.228 +				drive.ForceMediaChange();
   1.229 +#endif
   1.230 +				++MediaChanges;
   1.231 +				break;
   1.232 +			case 1:
   1.233 +				{
   1.234 +#ifndef __DISABLE_KILLER__
   1.235 +				TTime now;
   1.236 +				now.HomeTime();
   1.237 +				now+=TTimeIntervalSeconds(2);
   1.238 +				TRequestStatus s;
   1.239 +				timer.At(s,now);
   1.240 +				UserHal::SwitchOff();
   1.241 +				User::WaitForRequest(s);
   1.242 +				++PowerDowns;
   1.243 +#endif
   1.244 +				break;
   1.245 +				}
   1.246 +			case 2:
   1.247 +#ifndef __DISABLE_KILLER__
   1.248 +				TheWriterThread.Kill(0);
   1.249 +#endif
   1.250 +				++Kills;
   1.251 +#ifndef __DISABLE_KILLER__
   1.252 +				++WritePattern;
   1.253 +				Sem.Wait();
   1.254 +				TheWriterThread.SetPriority(EPriorityNormal);
   1.255 +#endif
   1.256 +				break;
   1.257 +			case 3:
   1.258 +#ifndef __DISABLE_KILLER__
   1.259 +				drive.ForceMediaChange();
   1.260 +#endif
   1.261 +				++MediaChanges;
   1.262 +#ifndef __DISABLE_KILLER__
   1.263 +				x=Random(seed);
   1.264 +				ms=50+(x&511);
   1.265 +				User::AfterHighRes(ms*1000);
   1.266 +				TheWriterThread.Kill(0);
   1.267 +#endif
   1.268 +				++Kills;
   1.269 +#ifndef __DISABLE_KILLER__
   1.270 +				++WritePattern;
   1.271 +				Sem.Wait();
   1.272 +				TheWriterThread.SetPriority(EPriorityNormal);
   1.273 +#endif
   1.274 +				break;
   1.275 +			case 4:
   1.276 +				break;
   1.277 +			}
   1.278 +		if (++action==5)
   1.279 +			action=0;
   1.280 +#ifndef __DISABLE_KILLER__
   1.281 +		TInt curr=CurrentSector;
   1.282 +		TInt remain=DriveSizeInSectors-curr;
   1.283 +		TInt n=(remain<8)?remain:8;
   1.284 +		r=Verify(drive,curr,n);
   1.285 +		if (r!=KErrNone)
   1.286 +			User::Panic(_L("VERIFY"),r);
   1.287 +#endif
   1.288 +		}
   1.289 +	}
   1.290 +
   1.291 +LOCAL_C TInt CreateKillerThread()
   1.292 +	{
   1.293 +	TInt r=TheKillerThread.Create(_L("Killer"),KillerThread,0x2000,NULL,NULL);
   1.294 +	if (r!=KErrNone)
   1.295 +		return r;
   1.296 +	TheKillerThread.SetPriority(EPriorityMore);
   1.297 +	TheKillerThread.Resume();
   1.298 +	return KErrNone;
   1.299 +	}
   1.300 +
   1.301 +LOCAL_C TInt CreateWriterThread()
   1.302 +	{
   1.303 +	FOREVER
   1.304 +		{
   1.305 +		TInt r=TheWriterThread.Create(_L("Writer"),WriterThread,0x2000,NULL,NULL);
   1.306 +		if (r==KErrNone)
   1.307 +			break;
   1.308 +		if (r!=KErrAlreadyExists)
   1.309 +			return r;
   1.310 +		test.Printf(_L("Writer thread still exists\n"));
   1.311 +		User::After(200000);
   1.312 +		}
   1.313 +	TheWriterThread.SetPriority(EPriorityLess);
   1.314 +	TheWriterThread.Resume();
   1.315 +	return KErrNone;
   1.316 +	}
   1.317 +
   1.318 +GLDEF_C TInt E32Main()
   1.319 +	{
   1.320 +/* 
   1.321 + * SetSystem() API was removed by __SECURE_API__	
   1.322 + *	"Systemize" will be implemented later calling a LDD which will set thread's "system" flag
   1.323 +	RThread().SetSystem(SYSTEM);
   1.324 + */
   1.325 +	WriterDrive.SetHandle(0);
   1.326 +	test.Title();
   1.327 +	TInt drv=-1;
   1.328 +	while (drv<0 || drv>=KMaxLocalDrives)
   1.329 +		{
   1.330 +		test.Printf(_L("\nSelect drive C-K: "));
   1.331 +		TChar c=(TUint)test.Getch();
   1.332 +		c.UpperCase();
   1.333 +		if (c.IsAlpha())
   1.334 +			drv=TUint(c)-'C';
   1.335 +		else
   1.336 +			drv=-1;
   1.337 +		}
   1.338 +	TBuf<1> b;
   1.339 +	b.SetLength(1);
   1.340 +	b[0]=(TText)(drv+'C');
   1.341 +	test.Printf(_L("%S\n"),&b);
   1.342 +
   1.343 +	TBuf<80> buf=_L("Connect to drive ");
   1.344 +	buf+=b;
   1.345 +	test.Start(buf);
   1.346 +	TBusLocalDrive drive;
   1.347 +	TBool medChg=EFalse;
   1.348 +	TInt r=drive.Connect(drv,medChg);
   1.349 +	test(r==KErrNone);
   1.350 +	DriveNumber=drv;
   1.351 +
   1.352 +	test.Next(_L("Get capabilities"));
   1.353 +	TLocalDriveCapsV2 driveCaps;
   1.354 +	TPckg<TLocalDriveCapsV2> capsPckg(driveCaps);
   1.355 +	r=drive.Caps(capsPckg);
   1.356 +	test(r==KErrNone);
   1.357 +	TUint driveSize=I64LOW(driveCaps.iSize);
   1.358 +	DriveSizeInSectors=(driveSize&~0xfff)>>KSectorShift;	// round down to multiple of 8 sectors
   1.359 +	test.Printf(_L("Drive size       = %08x (%dK)\n"),driveSize,driveSize>>10);
   1.360 +	test.Printf(_L("Media type       = %d\n"),driveCaps.iType);
   1.361 +	test.Printf(_L("Connection Bus   = %d\n"),driveCaps.iConnectionBusType);
   1.362 +	test.Printf(_L("Drive attributes = %08x\n"),driveCaps.iDriveAtt);
   1.363 +	test.Printf(_L("Media attributes = %08x\n"),driveCaps.iMediaAtt);
   1.364 +	test.Printf(_L("Base address     = %08x\n"),driveCaps.iBaseAddress);
   1.365 +	test.Printf(_L("File system ID   = %08x\n"),driveCaps.iFileSystemId);
   1.366 +	test.Printf(_L("Hidden sectors   = %08x\n"),driveCaps.iHiddenSectors);
   1.367 +	test.Printf(_L("Press any key...\n"));
   1.368 +	test.Getch();
   1.369 +
   1.370 +#ifdef __USE_MUTEX__
   1.371 +	test.Next(_L("Create mutex"));
   1.372 +	r=Mutex.CreateLocal();
   1.373 +	test(r==KErrNone);
   1.374 +#endif
   1.375 +
   1.376 +	test.Next(_L("Initialise drive"));
   1.377 +	TInt sector;
   1.378 +	for (sector=0; sector<DriveSizeInSectors; sector+=8)
   1.379 +		{
   1.380 +		Write(drive,sector,8);
   1.381 +		if ((sector&127)==0)
   1.382 +			test.Printf(_L("."));
   1.383 +		}
   1.384 +	test.Printf(_L("\n"));
   1.385 +	test.Next(_L("Verify drive"));
   1.386 +	for (sector=0; sector<DriveSizeInSectors; sector+=8)
   1.387 +		{
   1.388 +		test(Verify(drive,sector,8)==KErrNone);
   1.389 +		if ((sector&127)==0)
   1.390 +			test.Printf(_L("."));
   1.391 +		}
   1.392 +
   1.393 +	test.Printf(_L("\n\nPress ENTER to continue..."));
   1.394 +	TKeyCode k=EKeyNull;
   1.395 +	while (k!=EKeyEnter)
   1.396 +		k=test.Getch();
   1.397 +	test.Printf(_L("\n"));
   1.398 +
   1.399 +	test.Next(_L("Create semaphore"));
   1.400 +	r=Sem.CreateLocal(0);
   1.401 +	test(r==KErrNone);
   1.402 +	test.Next(_L("Create writer thread"));
   1.403 +	r=CreateWriterThread();
   1.404 +	test(r==KErrNone);
   1.405 +	TheWriterThread.SetPriority(EPriorityNormal);
   1.406 +	test.Next(_L("Create killer thread"));
   1.407 +	r=CreateKillerThread();
   1.408 +	test(r==KErrNone);
   1.409 +
   1.410 +	TBool exit=EFalse;
   1.411 +	sector=0;
   1.412 +	TInt verifies=0;
   1.413 +	TInt fails=0;
   1.414 +	while (!exit)
   1.415 +		{
   1.416 +		r=Verify(drive,sector,KVerifyBlockSize);
   1.417 +		if (r!=KErrNone)
   1.418 +			{
   1.419 +			++fails;
   1.420 +			test.Printf(_L("Sector %d Fail %d\n"),sector,r);
   1.421 +			}
   1.422 +		else
   1.423 +			++verifies;
   1.424 +		sector+=KVerifyBlockSize;
   1.425 +		if (sector==DriveSizeInSectors)
   1.426 +			{
   1.427 +			sector=0;
   1.428 +			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);
   1.429 +			}
   1.430 +		if ((sector&63)==0)
   1.431 +			{
   1.432 +			if (TheKillerThread.ExitType()!=EExitPending)
   1.433 +				{
   1.434 +				const TDesC& cat=TheKillerThread.ExitCategory();
   1.435 +				test.Printf(_L("KillerThread exited %d,%d,%S\n"),TheKillerThread.ExitType(),
   1.436 +														TheKillerThread.ExitReason(),&cat);
   1.437 +				break;
   1.438 +				}
   1.439 +			TExitType xt=TheWriterThread.ExitType();
   1.440 +			if (xt==EExitPanic)
   1.441 +				{
   1.442 +				const TDesC& cat=TheWriterThread.ExitCategory();
   1.443 +				test.Printf(_L("WriterThread Panic %S %d\n"),&cat,TheWriterThread.ExitReason());
   1.444 +				break;
   1.445 +				}
   1.446 +			if (xt!=EExitPending)
   1.447 +				{
   1.448 +				// restart writer thread
   1.449 +				TheWriterThread.Close();
   1.450 +				r=CreateWriterThread();
   1.451 +				if (r!=KErrNone)
   1.452 +					{
   1.453 +					test.Printf(_L("Restart writer thread failed %d\n"),r);
   1.454 +					break;
   1.455 +					}
   1.456 +				Sem.Signal();
   1.457 +				}
   1.458 +			}
   1.459 +		}
   1.460 +
   1.461 +	buf=_L("Disconnect from drive ");
   1.462 +	buf+=b;
   1.463 +	test.Next(buf);
   1.464 +	drive.Disconnect();
   1.465 +	test.End();
   1.466 +	return 0;
   1.467 +	}