os/kernelhwsrv/kerneltest/e32test/pccd/t_atadr3.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\pccd\t_atadr3.cpp
sl@0
    15
// Test the Compact Flash card (ATA) media driver
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include <e32test.h>
sl@0
    20
#include <e32svr.h>
sl@0
    21
#include <e32hal.h>
sl@0
    22
#include "u32std.h"
sl@0
    23
#include "../misc/prbs.h"
sl@0
    24
sl@0
    25
//#define __USE_MUTEX__
sl@0
    26
//#define __DISABLE_KILLER__
sl@0
    27
#define SYSTEM	ETrue
sl@0
    28
sl@0
    29
const TInt KErrVerify=-100;
sl@0
    30
sl@0
    31
const TInt KSectorSize=512;
sl@0
    32
const TInt KSectorShift=9;
sl@0
    33
const TInt KVerifyBlockSize=8;	// in sectors
sl@0
    34
sl@0
    35
LOCAL_D TBusLocalDrive WriterDrive;
sl@0
    36
LOCAL_D RTest test(_L("T_ATADR3"));
sl@0
    37
LOCAL_D TInt DriveNumber;
sl@0
    38
LOCAL_D TInt DriveSizeInSectors;
sl@0
    39
LOCAL_D RThread TheWriterThread;
sl@0
    40
LOCAL_D RThread TheKillerThread;
sl@0
    41
LOCAL_D RSemaphore Sem;
sl@0
    42
LOCAL_D TInt CurrentSector=0;
sl@0
    43
LOCAL_D TInt MediaChanges=0;
sl@0
    44
LOCAL_D TInt PowerDowns=0;
sl@0
    45
LOCAL_D TInt Kills=0;
sl@0
    46
LOCAL_D TInt SectorsWritten=0;
sl@0
    47
LOCAL_D TInt Aborts=0;
sl@0
    48
LOCAL_D TUint WritePattern=0;
sl@0
    49
sl@0
    50
#ifdef __USE_MUTEX__
sl@0
    51
LOCAL_D RMutex Mutex;
sl@0
    52
#define WAIT		Mutex.Wait()
sl@0
    53
#define SIGNAL		Mutex.Signal()
sl@0
    54
#else
sl@0
    55
#define WAIT
sl@0
    56
#define SIGNAL
sl@0
    57
#endif
sl@0
    58
sl@0
    59
inline TUint RoundDownToSector(TUint aPos)
sl@0
    60
	{ return aPos&~0x1ff; }
sl@0
    61
inline TUint RoundUpToSector(TUint aPos)
sl@0
    62
	{ return (aPos+0x1ff)&~0x1ff; }
sl@0
    63
sl@0
    64
LOCAL_C TInt WriteSectors(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
sl@0
    65
	{
sl@0
    66
	WAIT;
sl@0
    67
	TInt r=KErrNotReady;
sl@0
    68
	TInt pos=aSector<<KSectorShift;
sl@0
    69
	TPtrC8 p(aBuf,aCount*KSectorSize);
sl@0
    70
	while (r==KErrNotReady || (r==KErrAbort && (++Aborts,1)))
sl@0
    71
		r=aDrive.Write(pos,p);
sl@0
    72
	SIGNAL;
sl@0
    73
	return r;
sl@0
    74
	}
sl@0
    75
sl@0
    76
LOCAL_C TInt ReadSectors(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
sl@0
    77
	{
sl@0
    78
	WAIT;
sl@0
    79
	TInt r=KErrNotReady;
sl@0
    80
	TInt pos=aSector<<KSectorShift;
sl@0
    81
	TInt len=aCount*KSectorSize;
sl@0
    82
	TPtr8 p(aBuf,0,len);
sl@0
    83
	while (r==KErrNotReady || r==KErrAbort)
sl@0
    84
		r=aDrive.Read(pos,len,p);
sl@0
    85
	if (r==KErrNone && p.Length()!=len)
sl@0
    86
		r=KErrUnderflow;
sl@0
    87
	SIGNAL;
sl@0
    88
	return r;
sl@0
    89
	}
sl@0
    90
sl@0
    91
LOCAL_C void GenerateTestPattern(TInt aSector, TUint aState, TUint8* aBuf)
sl@0
    92
	{
sl@0
    93
	TUint seed[2];
sl@0
    94
	seed[1]=0;
sl@0
    95
	seed[0]=TUint(aSector)^(aState<<16);
sl@0
    96
	*aBuf++=TUint8(aState&0xff);
sl@0
    97
	*aBuf++=TUint8((aState>>8)&0xff);
sl@0
    98
	TInt i;
sl@0
    99
	for (i=0; i<KSectorSize-2; i++)
sl@0
   100
		*aBuf++=TUint8(Random(seed));
sl@0
   101
	}
sl@0
   102
sl@0
   103
LOCAL_C void Write(TBusLocalDrive& aDrive, TInt aSector, TInt aCount, TUint8* aBuf)
sl@0
   104
	{
sl@0
   105
	TInt n;
sl@0
   106
	for (n=0; n<aCount; n++)
sl@0
   107
		GenerateTestPattern(aSector+n,WritePattern,aBuf+n*KSectorSize);
sl@0
   108
	TInt r=WriteSectors(aDrive,aSector,aCount,aBuf);
sl@0
   109
	if (r!=KErrNone)
sl@0
   110
		User::Panic(_L("WRITE"),r);
sl@0
   111
	}
sl@0
   112
sl@0
   113
LOCAL_C void Write(TBusLocalDrive& aDrive, TInt aSector, TInt aCount)
sl@0
   114
	{
sl@0
   115
	const TInt KMaxLen = KSectorSize * 8;
sl@0
   116
	TInt len=aCount*KSectorSize;
sl@0
   117
	test (len <= KMaxLen);
sl@0
   118
	TUint8 buf[KMaxLen];
sl@0
   119
	Write(aDrive,aSector,aCount,buf);
sl@0
   120
	}
sl@0
   121
sl@0
   122
LOCAL_C TInt Verify(TInt aSector, TInt aCount, TUint8* aBuf)
sl@0
   123
	{
sl@0
   124
	TUint32 buf[KSectorSize/4];
sl@0
   125
	TUint8* pB=(TUint8*)buf;
sl@0
   126
	while(aCount--)
sl@0
   127
		{
sl@0
   128
		TUint state=aBuf[0]|(aBuf[1]<<8);
sl@0
   129
		GenerateTestPattern(aSector,state,pB);
sl@0
   130
		if (Mem::Compare(aBuf,KSectorSize,pB,KSectorSize)!=0)
sl@0
   131
			return KErrVerify;
sl@0
   132
		++aSector;
sl@0
   133
		aBuf+=KSectorSize;
sl@0
   134
		}
sl@0
   135
	return KErrNone;
sl@0
   136
	}
sl@0
   137
sl@0
   138
LOCAL_C TInt Verify(TBusLocalDrive& aDrive, TInt aSector, TInt aCount)
sl@0
   139
	{
sl@0
   140
	const TInt KMaxLen = KSectorSize * 8;
sl@0
   141
	TInt len=aCount*KSectorSize;
sl@0
   142
	test (len <= KMaxLen);
sl@0
   143
	TUint8 buf[KMaxLen];
sl@0
   144
	TInt r=ReadSectors(aDrive,aSector,aCount,buf);
sl@0
   145
	if (r!=KErrNone)
sl@0
   146
		return r;
sl@0
   147
	return Verify(aSector,aCount,buf);
sl@0
   148
	}
sl@0
   149
sl@0
   150
LOCAL_C TInt WriterThread(TAny*)
sl@0
   151
	{
sl@0
   152
/* 
sl@0
   153
 * SetSystem() API was removed by __SECURE_API__	
sl@0
   154
 *	"Systemize" will be implemented later calling a LDD which will set thread's "system" flag
sl@0
   155
	RThread().SetSystem(SYSTEM);
sl@0
   156
 */
sl@0
   157
	TUint seed[2];
sl@0
   158
	seed[0]=User::NTickCount();
sl@0
   159
	seed[1]=0;
sl@0
   160
	TUint32 buf[8*KSectorSize/4];
sl@0
   161
	TUint8* pB=(TUint8*)buf;
sl@0
   162
	TBool medChg=EFalse;
sl@0
   163
	WriterDrive.Close();	// close handle from previous incarnation of this thread
sl@0
   164
	TInt r=WriterDrive.Connect(DriveNumber,medChg);
sl@0
   165
	if (r!=KErrNone)
sl@0
   166
		User::Panic(_L("WRITER-CONNECT"),r);
sl@0
   167
	FOREVER
sl@0
   168
		{
sl@0
   169
		TInt remain=DriveSizeInSectors-CurrentSector;
sl@0
   170
		TInt n=Random(seed)&15;
sl@0
   171
		if (n>8 || n==0)
sl@0
   172
			n=1;
sl@0
   173
		if (n>remain)
sl@0
   174
			n=remain;
sl@0
   175
		if (n==1)
sl@0
   176
			{
sl@0
   177
			Write(WriterDrive,CurrentSector,1,pB);
sl@0
   178
			++WritePattern;
sl@0
   179
			Write(WriterDrive,CurrentSector,1,pB+KSectorSize);
sl@0
   180
			SectorsWritten+=2;
sl@0
   181
			}
sl@0
   182
		else
sl@0
   183
			{
sl@0
   184
			Write(WriterDrive,CurrentSector,n,pB);
sl@0
   185
			SectorsWritten+=n;
sl@0
   186
			}
sl@0
   187
		CurrentSector+=n;
sl@0
   188
		if (CurrentSector==DriveSizeInSectors)
sl@0
   189
			{
sl@0
   190
			CurrentSector=0;
sl@0
   191
			++WritePattern;
sl@0
   192
			}
sl@0
   193
		}
sl@0
   194
	}
sl@0
   195
sl@0
   196
LOCAL_C TInt KillerThread(TAny*)
sl@0
   197
	{
sl@0
   198
/* 
sl@0
   199
 * SetSystem() API was removed by __SECURE_API__	
sl@0
   200
 *	"Systemize" will be implemented later calling a LDD which will set thread's "system" flag
sl@0
   201
 	RThread().SetSystem(SYSTEM);
sl@0
   202
 */
sl@0
   203
	TUint seed[2];
sl@0
   204
	seed[0]=0xadf85458;
sl@0
   205
	seed[1]=0;
sl@0
   206
	TBusLocalDrive drive;
sl@0
   207
	TBool medChg=EFalse;
sl@0
   208
	TInt r=drive.Connect(DriveNumber,medChg);
sl@0
   209
	if (r!=KErrNone)
sl@0
   210
		User::Panic(_L("KILLER-CONNECT"),r);
sl@0
   211
	RTimer timer;
sl@0
   212
	r=timer.CreateLocal();
sl@0
   213
	if (r!=KErrNone)
sl@0
   214
		User::Panic(_L("KILLER-TIMER"),r);
sl@0
   215
	TInt action=0;
sl@0
   216
	FOREVER
sl@0
   217
		{
sl@0
   218
		TUint x=Random(seed);
sl@0
   219
		TUint ms=1000+(x&4095);
sl@0
   220
		User::AfterHighRes(ms*1000);
sl@0
   221
		switch (action)
sl@0
   222
			{
sl@0
   223
			case 0:
sl@0
   224
#ifndef __DISABLE_KILLER__
sl@0
   225
				drive.ForceMediaChange();
sl@0
   226
#endif
sl@0
   227
				++MediaChanges;
sl@0
   228
				break;
sl@0
   229
			case 1:
sl@0
   230
				{
sl@0
   231
#ifndef __DISABLE_KILLER__
sl@0
   232
				TTime now;
sl@0
   233
				now.HomeTime();
sl@0
   234
				now+=TTimeIntervalSeconds(2);
sl@0
   235
				TRequestStatus s;
sl@0
   236
				timer.At(s,now);
sl@0
   237
				UserHal::SwitchOff();
sl@0
   238
				User::WaitForRequest(s);
sl@0
   239
				++PowerDowns;
sl@0
   240
#endif
sl@0
   241
				break;
sl@0
   242
				}
sl@0
   243
			case 2:
sl@0
   244
#ifndef __DISABLE_KILLER__
sl@0
   245
				TheWriterThread.Kill(0);
sl@0
   246
#endif
sl@0
   247
				++Kills;
sl@0
   248
#ifndef __DISABLE_KILLER__
sl@0
   249
				++WritePattern;
sl@0
   250
				Sem.Wait();
sl@0
   251
				TheWriterThread.SetPriority(EPriorityNormal);
sl@0
   252
#endif
sl@0
   253
				break;
sl@0
   254
			case 3:
sl@0
   255
#ifndef __DISABLE_KILLER__
sl@0
   256
				drive.ForceMediaChange();
sl@0
   257
#endif
sl@0
   258
				++MediaChanges;
sl@0
   259
#ifndef __DISABLE_KILLER__
sl@0
   260
				x=Random(seed);
sl@0
   261
				ms=50+(x&511);
sl@0
   262
				User::AfterHighRes(ms*1000);
sl@0
   263
				TheWriterThread.Kill(0);
sl@0
   264
#endif
sl@0
   265
				++Kills;
sl@0
   266
#ifndef __DISABLE_KILLER__
sl@0
   267
				++WritePattern;
sl@0
   268
				Sem.Wait();
sl@0
   269
				TheWriterThread.SetPriority(EPriorityNormal);
sl@0
   270
#endif
sl@0
   271
				break;
sl@0
   272
			case 4:
sl@0
   273
				break;
sl@0
   274
			}
sl@0
   275
		if (++action==5)
sl@0
   276
			action=0;
sl@0
   277
#ifndef __DISABLE_KILLER__
sl@0
   278
		TInt curr=CurrentSector;
sl@0
   279
		TInt remain=DriveSizeInSectors-curr;
sl@0
   280
		TInt n=(remain<8)?remain:8;
sl@0
   281
		r=Verify(drive,curr,n);
sl@0
   282
		if (r!=KErrNone)
sl@0
   283
			User::Panic(_L("VERIFY"),r);
sl@0
   284
#endif
sl@0
   285
		}
sl@0
   286
	}
sl@0
   287
sl@0
   288
LOCAL_C TInt CreateKillerThread()
sl@0
   289
	{
sl@0
   290
	TInt r=TheKillerThread.Create(_L("Killer"),KillerThread,0x2000,NULL,NULL);
sl@0
   291
	if (r!=KErrNone)
sl@0
   292
		return r;
sl@0
   293
	TheKillerThread.SetPriority(EPriorityMore);
sl@0
   294
	TheKillerThread.Resume();
sl@0
   295
	return KErrNone;
sl@0
   296
	}
sl@0
   297
sl@0
   298
LOCAL_C TInt CreateWriterThread()
sl@0
   299
	{
sl@0
   300
	FOREVER
sl@0
   301
		{
sl@0
   302
		TInt r=TheWriterThread.Create(_L("Writer"),WriterThread,0x2000,NULL,NULL);
sl@0
   303
		if (r==KErrNone)
sl@0
   304
			break;
sl@0
   305
		if (r!=KErrAlreadyExists)
sl@0
   306
			return r;
sl@0
   307
		test.Printf(_L("Writer thread still exists\n"));
sl@0
   308
		User::After(200000);
sl@0
   309
		}
sl@0
   310
	TheWriterThread.SetPriority(EPriorityLess);
sl@0
   311
	TheWriterThread.Resume();
sl@0
   312
	return KErrNone;
sl@0
   313
	}
sl@0
   314
sl@0
   315
GLDEF_C TInt E32Main()
sl@0
   316
	{
sl@0
   317
/* 
sl@0
   318
 * SetSystem() API was removed by __SECURE_API__	
sl@0
   319
 *	"Systemize" will be implemented later calling a LDD which will set thread's "system" flag
sl@0
   320
	RThread().SetSystem(SYSTEM);
sl@0
   321
 */
sl@0
   322
	WriterDrive.SetHandle(0);
sl@0
   323
	test.Title();
sl@0
   324
	TInt drv=-1;
sl@0
   325
	while (drv<0 || drv>=KMaxLocalDrives)
sl@0
   326
		{
sl@0
   327
		test.Printf(_L("\nSelect drive C-K: "));
sl@0
   328
		TChar c=(TUint)test.Getch();
sl@0
   329
		c.UpperCase();
sl@0
   330
		if (c.IsAlpha())
sl@0
   331
			drv=TUint(c)-'C';
sl@0
   332
		else
sl@0
   333
			drv=-1;
sl@0
   334
		}
sl@0
   335
	TBuf<1> b;
sl@0
   336
	b.SetLength(1);
sl@0
   337
	b[0]=(TText)(drv+'C');
sl@0
   338
	test.Printf(_L("%S\n"),&b);
sl@0
   339
sl@0
   340
	TBuf<80> buf=_L("Connect to drive ");
sl@0
   341
	buf+=b;
sl@0
   342
	test.Start(buf);
sl@0
   343
	TBusLocalDrive drive;
sl@0
   344
	TBool medChg=EFalse;
sl@0
   345
	TInt r=drive.Connect(drv,medChg);
sl@0
   346
	test(r==KErrNone);
sl@0
   347
	DriveNumber=drv;
sl@0
   348
sl@0
   349
	test.Next(_L("Get capabilities"));
sl@0
   350
	TLocalDriveCapsV2 driveCaps;
sl@0
   351
	TPckg<TLocalDriveCapsV2> capsPckg(driveCaps);
sl@0
   352
	r=drive.Caps(capsPckg);
sl@0
   353
	test(r==KErrNone);
sl@0
   354
	TUint driveSize=I64LOW(driveCaps.iSize);
sl@0
   355
	DriveSizeInSectors=(driveSize&~0xfff)>>KSectorShift;	// round down to multiple of 8 sectors
sl@0
   356
	test.Printf(_L("Drive size       = %08x (%dK)\n"),driveSize,driveSize>>10);
sl@0
   357
	test.Printf(_L("Media type       = %d\n"),driveCaps.iType);
sl@0
   358
	test.Printf(_L("Connection Bus   = %d\n"),driveCaps.iConnectionBusType);
sl@0
   359
	test.Printf(_L("Drive attributes = %08x\n"),driveCaps.iDriveAtt);
sl@0
   360
	test.Printf(_L("Media attributes = %08x\n"),driveCaps.iMediaAtt);
sl@0
   361
	test.Printf(_L("Base address     = %08x\n"),driveCaps.iBaseAddress);
sl@0
   362
	test.Printf(_L("File system ID   = %08x\n"),driveCaps.iFileSystemId);
sl@0
   363
	test.Printf(_L("Hidden sectors   = %08x\n"),driveCaps.iHiddenSectors);
sl@0
   364
	test.Printf(_L("Press any key...\n"));
sl@0
   365
	test.Getch();
sl@0
   366
sl@0
   367
#ifdef __USE_MUTEX__
sl@0
   368
	test.Next(_L("Create mutex"));
sl@0
   369
	r=Mutex.CreateLocal();
sl@0
   370
	test(r==KErrNone);
sl@0
   371
#endif
sl@0
   372
sl@0
   373
	test.Next(_L("Initialise drive"));
sl@0
   374
	TInt sector;
sl@0
   375
	for (sector=0; sector<DriveSizeInSectors; sector+=8)
sl@0
   376
		{
sl@0
   377
		Write(drive,sector,8);
sl@0
   378
		if ((sector&127)==0)
sl@0
   379
			test.Printf(_L("."));
sl@0
   380
		}
sl@0
   381
	test.Printf(_L("\n"));
sl@0
   382
	test.Next(_L("Verify drive"));
sl@0
   383
	for (sector=0; sector<DriveSizeInSectors; sector+=8)
sl@0
   384
		{
sl@0
   385
		test(Verify(drive,sector,8)==KErrNone);
sl@0
   386
		if ((sector&127)==0)
sl@0
   387
			test.Printf(_L("."));
sl@0
   388
		}
sl@0
   389
sl@0
   390
	test.Printf(_L("\n\nPress ENTER to continue..."));
sl@0
   391
	TKeyCode k=EKeyNull;
sl@0
   392
	while (k!=EKeyEnter)
sl@0
   393
		k=test.Getch();
sl@0
   394
	test.Printf(_L("\n"));
sl@0
   395
sl@0
   396
	test.Next(_L("Create semaphore"));
sl@0
   397
	r=Sem.CreateLocal(0);
sl@0
   398
	test(r==KErrNone);
sl@0
   399
	test.Next(_L("Create writer thread"));
sl@0
   400
	r=CreateWriterThread();
sl@0
   401
	test(r==KErrNone);
sl@0
   402
	TheWriterThread.SetPriority(EPriorityNormal);
sl@0
   403
	test.Next(_L("Create killer thread"));
sl@0
   404
	r=CreateKillerThread();
sl@0
   405
	test(r==KErrNone);
sl@0
   406
sl@0
   407
	TBool exit=EFalse;
sl@0
   408
	sector=0;
sl@0
   409
	TInt verifies=0;
sl@0
   410
	TInt fails=0;
sl@0
   411
	while (!exit)
sl@0
   412
		{
sl@0
   413
		r=Verify(drive,sector,KVerifyBlockSize);
sl@0
   414
		if (r!=KErrNone)
sl@0
   415
			{
sl@0
   416
			++fails;
sl@0
   417
			test.Printf(_L("Sector %d Fail %d\n"),sector,r);
sl@0
   418
			}
sl@0
   419
		else
sl@0
   420
			++verifies;
sl@0
   421
		sector+=KVerifyBlockSize;
sl@0
   422
		if (sector==DriveSizeInSectors)
sl@0
   423
			{
sl@0
   424
			sector=0;
sl@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);
sl@0
   426
			}
sl@0
   427
		if ((sector&63)==0)
sl@0
   428
			{
sl@0
   429
			if (TheKillerThread.ExitType()!=EExitPending)
sl@0
   430
				{
sl@0
   431
				const TDesC& cat=TheKillerThread.ExitCategory();
sl@0
   432
				test.Printf(_L("KillerThread exited %d,%d,%S\n"),TheKillerThread.ExitType(),
sl@0
   433
														TheKillerThread.ExitReason(),&cat);
sl@0
   434
				break;
sl@0
   435
				}
sl@0
   436
			TExitType xt=TheWriterThread.ExitType();
sl@0
   437
			if (xt==EExitPanic)
sl@0
   438
				{
sl@0
   439
				const TDesC& cat=TheWriterThread.ExitCategory();
sl@0
   440
				test.Printf(_L("WriterThread Panic %S %d\n"),&cat,TheWriterThread.ExitReason());
sl@0
   441
				break;
sl@0
   442
				}
sl@0
   443
			if (xt!=EExitPending)
sl@0
   444
				{
sl@0
   445
				// restart writer thread
sl@0
   446
				TheWriterThread.Close();
sl@0
   447
				r=CreateWriterThread();
sl@0
   448
				if (r!=KErrNone)
sl@0
   449
					{
sl@0
   450
					test.Printf(_L("Restart writer thread failed %d\n"),r);
sl@0
   451
					break;
sl@0
   452
					}
sl@0
   453
				Sem.Signal();
sl@0
   454
				}
sl@0
   455
			}
sl@0
   456
		}
sl@0
   457
sl@0
   458
	buf=_L("Disconnect from drive ");
sl@0
   459
	buf+=b;
sl@0
   460
	test.Next(buf);
sl@0
   461
	drive.Disconnect();
sl@0
   462
	test.End();
sl@0
   463
	return 0;
sl@0
   464
	}