os/kernelhwsrv/kerneltest/e32test/pccd/t_pccdsr.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) 1998-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_pccdsr.cpp
sl@0
    15
// Stress test a single sector of Compact Flash card (ATA).
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
sl@0
    20
//#define USE_F32_ACCESS
sl@0
    21
#include <e32test.h>
sl@0
    22
#include <e32svr.h>
sl@0
    23
#include <e32hal.h>
sl@0
    24
#include <e32uid.h>
sl@0
    25
#if defined (USE_F32_ACCESS)
sl@0
    26
#include <f32fsys.h>
sl@0
    27
#include <f32file.h>
sl@0
    28
#endif
sl@0
    29
sl@0
    30
#define ATA_PDD_NAME _L("MEDATA")
sl@0
    31
sl@0
    32
const TInt KAtaSectorSize=512;
sl@0
    33
const TInt KMaxSectors=8;
sl@0
    34
const TInt KMaxRdWrBufLen=(KAtaSectorSize*KMaxSectors); // 4K
sl@0
    35
const TInt KHeapSize=0x4000;
sl@0
    36
const TInt KMaxErr=8;
sl@0
    37
sl@0
    38
#if defined (USE_F32_ACCESS)
sl@0
    39
GLDEF_D	RFs TheFs;
sl@0
    40
#else
sl@0
    41
LOCAL_D	TBusLocalDrive TheDrive;
sl@0
    42
LOCAL_D TBool ChangedFlag;
sl@0
    43
#endif
sl@0
    44
RTest test(_L("Local Drive Stress test"));
sl@0
    45
LOCAL_D TBuf8<KMaxRdWrBufLen> wrBufPat1,wrBufPat2,rdBuf;
sl@0
    46
sl@0
    47
enum TOper {ENone,EWrite,ERead,ECompare};
sl@0
    48
class TErrInfo
sl@0
    49
	{
sl@0
    50
public:
sl@0
    51
	TErrInfo();
sl@0
    52
public:
sl@0
    53
	TInt iError;
sl@0
    54
	TOper iOperation;
sl@0
    55
	TInt iCycle;
sl@0
    56
	};
sl@0
    57
sl@0
    58
class TResult
sl@0
    59
	{
sl@0
    60
public:
sl@0
    61
	TResult();
sl@0
    62
	void Display(CConsoleBase *aConsole, TInt aCycles);
sl@0
    63
	void Add(TInt anError,TOper anOperation,TInt aCycle);
sl@0
    64
public:
sl@0
    65
	TInt iTotalErrs;
sl@0
    66
	TErrInfo iFirstErr;
sl@0
    67
	TErrInfo iLastErrs[KMaxErr];
sl@0
    68
	TInt iNextFreeErr;
sl@0
    69
	TBool iHadAnError;
sl@0
    70
	};
sl@0
    71
sl@0
    72
sl@0
    73
LOCAL_C TUint OperationToChar(TOper anOperation)
sl@0
    74
//
sl@0
    75
// Convert operation enum to corresponding display character
sl@0
    76
//
sl@0
    77
	{
sl@0
    78
sl@0
    79
	switch(anOperation)
sl@0
    80
		{
sl@0
    81
		case EWrite:
sl@0
    82
			return('W');
sl@0
    83
		case ERead:
sl@0
    84
			return('R');
sl@0
    85
		case ECompare:
sl@0
    86
			return('C');
sl@0
    87
		default:
sl@0
    88
			return('?');
sl@0
    89
		}
sl@0
    90
	}
sl@0
    91
sl@0
    92
TErrInfo::TErrInfo()
sl@0
    93
//
sl@0
    94
// Constructor
sl@0
    95
//
sl@0
    96
	{
sl@0
    97
sl@0
    98
	iError=KErrNone;
sl@0
    99
	iOperation=ENone;
sl@0
   100
	iCycle=0;
sl@0
   101
	}
sl@0
   102
sl@0
   103
TResult::TResult()
sl@0
   104
//
sl@0
   105
// Constructor
sl@0
   106
//
sl@0
   107
	{
sl@0
   108
sl@0
   109
	iNextFreeErr=0;
sl@0
   110
	iTotalErrs=0;
sl@0
   111
	iHadAnError=EFalse;
sl@0
   112
	}
sl@0
   113
sl@0
   114
void TResult::Display(CConsoleBase *aConsole, TInt aCycles)
sl@0
   115
//
sl@0
   116
// Display test results
sl@0
   117
//
sl@0
   118
	{
sl@0
   119
sl@0
   120
	TInt xStartPos=0;
sl@0
   121
	TInt yStartPos=7;
sl@0
   122
sl@0
   123
	aConsole->SetPos(xStartPos,yStartPos);
sl@0
   124
	test.Printf(_L("CYCLES-> %07d   ERRORS-> %d"),aCycles,iTotalErrs);
sl@0
   125
sl@0
   126
	aConsole->SetPos(xStartPos,yStartPos+1);
sl@0
   127
	test.Printf(_L("FIRST ERROR-> "));
sl@0
   128
	if (iHadAnError)
sl@0
   129
		test.Printf(_L("Error:%d Oper:%c Cycle:%07d"),iFirstErr.iError,OperationToChar(iFirstErr.iOperation),iFirstErr.iCycle);
sl@0
   130
sl@0
   131
	aConsole->SetPos(xStartPos,yStartPos+2);
sl@0
   132
	test.Printf(_L("LAST ERRORS->"));
sl@0
   133
	if (iHadAnError)
sl@0
   134
		{
sl@0
   135
		TInt i;
sl@0
   136
		aConsole->SetPos(xStartPos+3,yStartPos+3);
sl@0
   137
		test.Printf(_L("Error:  "));
sl@0
   138
		for (i=0;(i<KMaxErr && iLastErrs[i].iOperation!=ENone);i++)
sl@0
   139
			test.Printf(_L("% 7d,"),iLastErrs[i].iError);
sl@0
   140
sl@0
   141
		aConsole->SetPos(xStartPos+3,yStartPos+4);
sl@0
   142
		test.Printf(_L("Oper:   "));
sl@0
   143
		for (i=0;(i<KMaxErr && iLastErrs[i].iOperation!=ENone);i++)
sl@0
   144
			test.Printf(_L("      %c,"),OperationToChar(iLastErrs[i].iOperation));
sl@0
   145
 
sl@0
   146
		aConsole->SetPos(xStartPos+3,yStartPos+5);
sl@0
   147
		test.Printf(_L("Cycle:  "));
sl@0
   148
		for (i=0;(i<KMaxErr && iLastErrs[i].iOperation!=ENone);i++)
sl@0
   149
			test.Printf(_L("% 7d,"),iLastErrs[i].iCycle);
sl@0
   150
		}
sl@0
   151
sl@0
   152
	test.Printf(_L("\r\n"));
sl@0
   153
	}
sl@0
   154
sl@0
   155
void TResult::Add(TInt anError,TOper anOperation,TInt aCycle)
sl@0
   156
//
sl@0
   157
// Add a test result
sl@0
   158
//
sl@0
   159
	{
sl@0
   160
sl@0
   161
	iTotalErrs++;
sl@0
   162
	if (!iHadAnError)
sl@0
   163
		{
sl@0
   164
		iFirstErr.iError=anError;
sl@0
   165
		iFirstErr.iOperation=anOperation;
sl@0
   166
		iFirstErr.iCycle=aCycle;
sl@0
   167
		iHadAnError=ETrue;
sl@0
   168
		}
sl@0
   169
	if (iNextFreeErr>=KMaxErr)
sl@0
   170
		{
sl@0
   171
		for (TInt i=0;i<(KMaxErr-1);i++)
sl@0
   172
			iLastErrs[i]=iLastErrs[i+1];
sl@0
   173
		iNextFreeErr=(KMaxErr-1);
sl@0
   174
		}
sl@0
   175
	iLastErrs[iNextFreeErr].iError=anError;
sl@0
   176
	iLastErrs[iNextFreeErr].iOperation=anOperation;
sl@0
   177
	iLastErrs[iNextFreeErr].iCycle=aCycle;
sl@0
   178
	iNextFreeErr++;
sl@0
   179
	}
sl@0
   180
sl@0
   181
GLDEF_C TInt E32Main()
sl@0
   182
    {
sl@0
   183
	TBuf<64> b;
sl@0
   184
sl@0
   185
	test.Title();
sl@0
   186
	TDriveInfoV1Buf diBuf;
sl@0
   187
	UserHal::DriveInfo(diBuf);
sl@0
   188
	TDriveInfoV1 &di=diBuf();
sl@0
   189
	test.Printf(_L("Select Local Drive (C-%c): "),'C'+(di.iTotalSupportedDrives-1));
sl@0
   190
	TChar c;
sl@0
   191
	TInt drv;
sl@0
   192
	FOREVER
sl@0
   193
		{
sl@0
   194
		c=(TUint)test.Getch();
sl@0
   195
		c.UpperCase();
sl@0
   196
		drv=((TUint)c)-'C';
sl@0
   197
		if (drv>=0&&drv<di.iTotalSupportedDrives)
sl@0
   198
			break;
sl@0
   199
		}
sl@0
   200
	test.Printf(_L("%c:\r\n"),'C'+drv);
sl@0
   201
sl@0
   202
	TInt rdWrLen;
sl@0
   203
#if !defined (USE_F32_ACCESS)
sl@0
   204
	test.Printf(_L("Select total sectors to write(1-8): "));
sl@0
   205
	FOREVER
sl@0
   206
		{
sl@0
   207
		c=(TUint)test.Getch();
sl@0
   208
		rdWrLen=((TUint)c)-'0';
sl@0
   209
		if (rdWrLen>=1&&rdWrLen<=8)
sl@0
   210
			break;
sl@0
   211
		}
sl@0
   212
	test.Printf(_L("%dSector(s)\r\n"),rdWrLen);
sl@0
   213
	rdWrLen*=KAtaSectorSize;
sl@0
   214
#else
sl@0
   215
	rdWrLen=(KAtaSectorSize*2)+1;
sl@0
   216
#endif
sl@0
   217
sl@0
   218
	b.Format(_L("Init test on drive %c:"),'C'+drv);
sl@0
   219
	test.Start(b);
sl@0
   220
sl@0
   221
	TInt r;
sl@0
   222
#if defined (USE_F32_ACCESS)
sl@0
   223
	r=TheFs.Connect();
sl@0
   224
	test(r==KErrNone);
sl@0
   225
sl@0
   226
	RFile f;
sl@0
   227
	b.Format(_L("%c:\\TEMP.BIN"),'C'+drv);
sl@0
   228
	r=f.Replace(TheFs,b,EFileShareAny|EFileStream|EFileWrite);
sl@0
   229
	test(r==KErrNone);
sl@0
   230
	b.Format(_L("Start testing (%c:\\TEMP.BIN):"),'C'+drv);
sl@0
   231
#else
sl@0
   232
	r=User::LoadPhysicalDevice(ATA_PDD_NAME);
sl@0
   233
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   234
sl@0
   235
	ChangedFlag=EFalse;
sl@0
   236
	TheDrive.Connect(drv,ChangedFlag);
sl@0
   237
sl@0
   238
	TLocalDriveCapsV2Buf info;
sl@0
   239
	test(TheDrive.Caps(info)==KErrNone);
sl@0
   240
	test(info().iType==EMediaHardDisk);
sl@0
   241
	TInt trgPos=I64LOW(info().iSize)-rdWrLen;	  // Hammer the very end of the disk
sl@0
   242
	b.Format(_L("Start testing (sector %xH):"),trgPos/KAtaSectorSize);
sl@0
   243
#endif
sl@0
   244
sl@0
   245
	test.Next(b);
sl@0
   246
	wrBufPat1.SetLength(rdWrLen);
sl@0
   247
	TInt j;
sl@0
   248
	for (j=0;j<rdWrLen;j++)
sl@0
   249
		wrBufPat1[j]=(TUint8)j;
sl@0
   250
	wrBufPat2.SetLength(rdWrLen);
sl@0
   251
	for (j=0;j<rdWrLen;j++)
sl@0
   252
		wrBufPat2[j]=(TUint8)((rdWrLen-1)-j);
sl@0
   253
sl@0
   254
	TInt cycles=0;
sl@0
   255
	TResult results;
sl@0
   256
	TBool toggleTest=EFalse;
sl@0
   257
sl@0
   258
	TRequestStatus kStat;
sl@0
   259
	test.Console()->Read(kStat);
sl@0
   260
	FOREVER
sl@0
   261
		{
sl@0
   262
		if ((cycles%10)==0)
sl@0
   263
			results.Display(test.Console(),cycles);
sl@0
   264
sl@0
   265
		TInt res;
sl@0
   266
#if defined (USE_F32_ACCESS)
sl@0
   267
		TInt len=(toggleTest)?rdWrLen:1;
sl@0
   268
sl@0
   269
		wrBufPat1.SetLength(len);
sl@0
   270
		if ((res=f.SetSize(len))==KErrNone)
sl@0
   271
			res=f.Write(0,wrBufPat1);		// Write test (Pos=0)
sl@0
   272
		if (res!=KErrNone)
sl@0
   273
			results.Add(res,EWrite,cycles);
sl@0
   274
sl@0
   275
		// Read test
sl@0
   276
		rdBuf.Fill(0,len);
sl@0
   277
		res=f.Read(0,rdBuf,len);
sl@0
   278
		if (res!=KErrNone)
sl@0
   279
			results.Add(res,ERead,cycles);
sl@0
   280
		if (rdBuf.Compare(wrBufPat1)!=0)
sl@0
   281
			results.Add(0,ECompare,cycles);
sl@0
   282
#else
sl@0
   283
		TDes8* wrBuf=(toggleTest)?&wrBufPat2:&wrBufPat1; // Change pattern written
sl@0
   284
		
sl@0
   285
		res=TheDrive.Write(trgPos,*wrBuf);	// Write test
sl@0
   286
		if (res!=KErrNone)
sl@0
   287
			results.Add(res,EWrite,cycles);
sl@0
   288
		
sl@0
   289
		rdBuf.Fill(0,rdWrLen);
sl@0
   290
		res=TheDrive.Read(trgPos,rdWrLen,rdBuf);	// Read test
sl@0
   291
		if (res!=KErrNone)
sl@0
   292
			results.Add(res,ERead,cycles);
sl@0
   293
		if (rdBuf.Compare(*wrBuf)!=0)
sl@0
   294
			results.Add(0,ECompare,cycles);
sl@0
   295
#endif
sl@0
   296
		cycles++;
sl@0
   297
		toggleTest^=0x01;
sl@0
   298
sl@0
   299
		if (kStat!=KRequestPending)
sl@0
   300
			{
sl@0
   301
			TKeyCode c=test.Console()->KeyCode();
sl@0
   302
			if (c==EKeySpace)
sl@0
   303
				break;
sl@0
   304
			test.Console()->Read(kStat);
sl@0
   305
			}
sl@0
   306
		}
sl@0
   307
sl@0
   308
	test.Next(_L("Close"));
sl@0
   309
#if defined (USE_F32_ACCESS)
sl@0
   310
	f.Close();
sl@0
   311
	TheFs.Close();
sl@0
   312
#else
sl@0
   313
	TheDrive.Disconnect();
sl@0
   314
#endif
sl@0
   315
sl@0
   316
	test.End();
sl@0
   317
	return(0);
sl@0
   318
	}
sl@0
   319