os/kernelhwsrv/kerneltest/e32test/pccd/t_media.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_media.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 "u32std.h"
sl@0
    22
#include "../misc/prbs.h"
sl@0
    23
sl@0
    24
const TInt KSectorSize=512;
sl@0
    25
sl@0
    26
LOCAL_D RTest test(_L("T_MEDIA"));
sl@0
    27
LOCAL_D TBusLocalDrive TheDrive;
sl@0
    28
LOCAL_D TBool MediaChange=EFalse;
sl@0
    29
LOCAL_D TUint Seed[2];
sl@0
    30
sl@0
    31
LOCAL_D TUint8 Background1[64*KSectorSize];
sl@0
    32
LOCAL_D TUint8 Background2[64*KSectorSize];
sl@0
    33
LOCAL_D TUint8 Foreground1[64*KSectorSize];
sl@0
    34
LOCAL_D TUint8 Foreground2[64*KSectorSize];
sl@0
    35
LOCAL_D TUint8 VerifyBuffer[64*KSectorSize];
sl@0
    36
sl@0
    37
inline TUint RoundDownToSector(TUint aPos)
sl@0
    38
	{ return aPos&~0x1ff; }
sl@0
    39
inline TUint RoundUpToSector(TUint aPos)
sl@0
    40
	{ return (aPos+0x1ff)&~0x1ff; }
sl@0
    41
sl@0
    42
LOCAL_C void TestPattern(TUint8* aBuf, TInt aLength)
sl@0
    43
	{
sl@0
    44
	while(aLength--)
sl@0
    45
		*aBuf++=(TUint8)Random(Seed);
sl@0
    46
	}
sl@0
    47
sl@0
    48
LOCAL_C void Write(TUint aPos, TInt aLength, const TUint8* aBuffer)
sl@0
    49
	{
sl@0
    50
	TPtrC8 p(aBuffer,aLength);
sl@0
    51
	TInt r=TheDrive.Write(aPos,p);
sl@0
    52
	if (r!=KErrNone)
sl@0
    53
		{
sl@0
    54
		test.Printf(_L("Write failed with error %d\n"),r);
sl@0
    55
		test.Printf(_L("Pos=%08x, Length=%x\n"),aPos,aLength);
sl@0
    56
		test(0);
sl@0
    57
		}
sl@0
    58
	}
sl@0
    59
sl@0
    60
LOCAL_C void DebugDump(TUint aPos, TInt aLength, const TUint8* aBuf, const TDesC& aTitle)
sl@0
    61
	{
sl@0
    62
	RDebug::Print(aTitle);
sl@0
    63
	TUint end=aPos+aLength;
sl@0
    64
	TInt i;
sl@0
    65
	TInt j=0;
sl@0
    66
	while(aPos<end)
sl@0
    67
		{
sl@0
    68
		TBuf<80> buf;
sl@0
    69
		buf.NumFixedWidthUC(aPos,EHex,8);
sl@0
    70
		buf+=_L(": ");
sl@0
    71
		for (i=0; i<16; i++)
sl@0
    72
			{
sl@0
    73
			buf.AppendNumFixedWidthUC(aBuf[j+i],EHex,2);
sl@0
    74
			buf+=_L(" ");
sl@0
    75
			}
sl@0
    76
		RDebug::Print(buf);
sl@0
    77
		aPos+=16;
sl@0
    78
		j+=16;
sl@0
    79
		if ((aPos&(KSectorSize-1))==0)
sl@0
    80
			RDebug::Print(_L(""));
sl@0
    81
		}
sl@0
    82
	}
sl@0
    83
sl@0
    84
LOCAL_C void Verify(TUint aPos, TInt aLength, const TUint8* aRef)
sl@0
    85
	{
sl@0
    86
	TPtr8 p(VerifyBuffer,0,64*KSectorSize);
sl@0
    87
	TInt r=TheDrive.Read(aPos,aLength,p);
sl@0
    88
	if (r!=KErrNone)
sl@0
    89
		{
sl@0
    90
		test.Printf(_L("Read failed with error %d\n"),r);
sl@0
    91
		test.Printf(_L("Pos=%08x, Length=%x\n"),aPos,aLength);
sl@0
    92
		test(0);
sl@0
    93
		}
sl@0
    94
	if (p.Length()!=aLength)
sl@0
    95
		{
sl@0
    96
		test.Printf(_L("Incorrect length after read: Was %08x Expected %08x\n"),p.Length(),aLength);
sl@0
    97
		test.Printf(_L("Pos=%08x, Length=%x\n"),aPos,aLength);
sl@0
    98
		test(0);
sl@0
    99
		}
sl@0
   100
	r=Mem::Compare(VerifyBuffer,aLength,aRef,aLength);
sl@0
   101
	if (r==0)
sl@0
   102
		return;
sl@0
   103
	TInt i=0;
sl@0
   104
	while(i<aLength && VerifyBuffer[i]==aRef[i])
sl@0
   105
		i++;
sl@0
   106
	test.Printf(_L("Verify error: aPos=%08x, aLength=%08x\n"),aPos,aLength);
sl@0
   107
	test.Printf(_L("First difference at offset %x\n"),i);
sl@0
   108
	test.Printf(_L("Press <ENTER> for debug dump "));
sl@0
   109
	TInt k=test.Getch();
sl@0
   110
	if (k==EKeyEnter)
sl@0
   111
		{
sl@0
   112
		DebugDump(aPos,aLength,VerifyBuffer,_L("Actual:"));
sl@0
   113
		DebugDump(aPos,aLength,aRef,_L("Expected:"));
sl@0
   114
		}
sl@0
   115
	test(0);
sl@0
   116
	}
sl@0
   117
sl@0
   118
LOCAL_C void DoTest(TUint aBasePos, TInt anOffset, TInt aSize)
sl@0
   119
	{
sl@0
   120
	TBuf<80> buf;
sl@0
   121
	buf.Format(_L("Offset %3x Size %04x"),anOffset,aSize);
sl@0
   122
	test.Next(buf);
sl@0
   123
	TUint block1=aBasePos;
sl@0
   124
	TUint block2=aBasePos+64*KSectorSize;
sl@0
   125
	TUint totalSectorSize=RoundUpToSector(anOffset+aSize);
sl@0
   126
	TestPattern(Background1,totalSectorSize);
sl@0
   127
	TestPattern(Background2,totalSectorSize);
sl@0
   128
	TestPattern(Foreground1,totalSectorSize);
sl@0
   129
	TestPattern(Foreground2,totalSectorSize);
sl@0
   130
	Write(block1,totalSectorSize,Background1);
sl@0
   131
	Write(block2,totalSectorSize,Background2);
sl@0
   132
	Verify(block1,totalSectorSize,Background1);
sl@0
   133
	Verify(block2,totalSectorSize,Background2);
sl@0
   134
	Write(block1+anOffset,aSize,Foreground1);
sl@0
   135
	Write(block2+anOffset,aSize,Foreground2);
sl@0
   136
	Mem::Copy(Background1+anOffset,Foreground1,aSize);
sl@0
   137
	Mem::Copy(Background2+anOffset,Foreground2,aSize);
sl@0
   138
	Verify(block1,totalSectorSize,Background1);
sl@0
   139
	Verify(block2,totalSectorSize,Background2);
sl@0
   140
	}
sl@0
   141
sl@0
   142
GLDEF_C TInt E32Main()
sl@0
   143
	{
sl@0
   144
	Seed[0]=0xadf85458;
sl@0
   145
	Seed[1]=0;
sl@0
   146
	test.Title();
sl@0
   147
	
sl@0
   148
	TChar driveToTest;
sl@0
   149
sl@0
   150
	// Get the list of drives
sl@0
   151
	TDriveInfoV1Buf diBuf;
sl@0
   152
	UserHal::DriveInfo(diBuf);
sl@0
   153
	TDriveInfoV1 &di=diBuf();
sl@0
   154
	TInt driveCount = di.iTotalSupportedDrives;
sl@0
   155
	
sl@0
   156
	test.Printf(_L("\nDRIVES USED AT PRESENT :\r\n"));
sl@0
   157
	for (TInt i=0; i < driveCount; i++)
sl@0
   158
		{
sl@0
   159
		TBool flag=EFalse;
sl@0
   160
		RLocalDrive d;
sl@0
   161
		TInt r=d.Connect(i,flag);
sl@0
   162
		//Not all the drives are used at present
sl@0
   163
		if (r == KErrNotSupported)
sl@0
   164
			continue;
sl@0
   165
sl@0
   166
		test.Printf(_L("%d : DRIVE NAME  :%- 16S\r\n"), i, &di.iDriveName[i]);
sl@0
   167
		}
sl@0
   168
sl@0
   169
	test.Printf(_L("\n<<<Hit required drive number to continue>>>\r\n"));
sl@0
   170
sl@0
   171
	driveToTest=(TUint)test.Getch();
sl@0
   172
	
sl@0
   173
	TInt driveNumber=((TUint)driveToTest) - '0';
sl@0
   174
sl@0
   175
	TBuf<0x100> buf;
sl@0
   176
	buf.Format(_L("Connect to local drive (%d)"),driveNumber);
sl@0
   177
	test.Start(buf);
sl@0
   178
	
sl@0
   179
	TInt r=TheDrive.Connect(driveNumber,MediaChange);
sl@0
   180
	test(r==KErrNone);
sl@0
   181
	
sl@0
   182
	test.Next(_L("Get capabilities"));
sl@0
   183
	TLocalDriveCapsV2 driveCaps;
sl@0
   184
	TPckg<TLocalDriveCapsV2> capsPckg(driveCaps);
sl@0
   185
	r=TheDrive.Caps(capsPckg);
sl@0
   186
	test(r==KErrNone);
sl@0
   187
	TUint driveSize=I64LOW(driveCaps.iSize);
sl@0
   188
	test.Printf(_L("Drive size       = %08x (%dK)\n"),driveSize,driveSize>>10);
sl@0
   189
	test.Printf(_L("Media type       = %d\n"),driveCaps.iType);
sl@0
   190
	test.Printf(_L("Connection Bus   = %d\n"),driveCaps.iConnectionBusType);
sl@0
   191
	test.Printf(_L("Drive attributes = %08x\n"),driveCaps.iDriveAtt);
sl@0
   192
	test.Printf(_L("Media attributes = %08x\n"),driveCaps.iMediaAtt);
sl@0
   193
	test.Printf(_L("Base address     = %08x\n"),driveCaps.iBaseAddress);
sl@0
   194
	test.Printf(_L("File system ID   = %08x\n"),driveCaps.iFileSystemId);
sl@0
   195
	test.Printf(_L("Hidden sectors   = %08x\n"),driveCaps.iHiddenSectors);
sl@0
   196
	test.Printf(_L("Press any key...\n"));
sl@0
   197
	test.Getch();
sl@0
   198
	TUint basePos=RoundDownToSector(driveSize)-128*KSectorSize;
sl@0
   199
	test.Printf(_L("Base position    = %08x\n"),basePos);
sl@0
   200
sl@0
   201
	TInt offset;
sl@0
   202
	TInt size;
sl@0
   203
	for (size=KSectorSize/4; size<=23*KSectorSize/2; size+=KSectorSize/4)
sl@0
   204
		{
sl@0
   205
		for (offset=0; offset<KSectorSize; offset+=KSectorSize/2)
sl@0
   206
			{
sl@0
   207
			DoTest(basePos,offset,size);
sl@0
   208
			}
sl@0
   209
		}
sl@0
   210
sl@0
   211
	for (size=12*KSectorSize; size<=33*KSectorSize; size+=KSectorSize/2)
sl@0
   212
		{
sl@0
   213
		for (offset=0; offset<KSectorSize; offset+=KSectorSize/2)
sl@0
   214
			{
sl@0
   215
			DoTest(basePos,offset,size);
sl@0
   216
			}
sl@0
   217
		}
sl@0
   218
sl@0
   219
	buf.Format(_L("Disconnect from local drive (%d)"),driveNumber);
sl@0
   220
	test.Next(buf);
sl@0
   221
	TheDrive.Disconnect();
sl@0
   222
	test.End();
sl@0
   223
	return 0;
sl@0
   224
	}