os/kernelhwsrv/kerneltest/e32test/lffs/tf_suspend.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) 2003-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
// Tests erasing of Flash while forcing suspend-resume cycles
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32std.h>
sl@0
    19
#include <e32std_private.h>
sl@0
    20
#include <e32svr.h>
sl@0
    21
#include <e32test.h>
sl@0
    22
#include "user_config.h"
sl@0
    23
sl@0
    24
RTest test( _L("TF_SUSPEND") );
sl@0
    25
sl@0
    26
sl@0
    27
sl@0
    28
class CEraser
sl@0
    29
	{
sl@0
    30
	public:
sl@0
    31
		enum TFunction
sl@0
    32
			{
sl@0
    33
			EEraseBlock
sl@0
    34
			};
sl@0
    35
sl@0
    36
	public:
sl@0
    37
		~CEraser();
sl@0
    38
		void CreateL();
sl@0
    39
		void Stop();
sl@0
    40
		void WaitForReady();
sl@0
    41
sl@0
    42
		inline void WaitForDone()
sl@0
    43
			{
sl@0
    44
			WaitForReady();
sl@0
    45
			iWaitingSignal.Signal();	// resignal, ready for next Start()
sl@0
    46
			};
sl@0
    47
sl@0
    48
		void EraseBlock( TUint32 aOffset, TUint aLength );
sl@0
    49
sl@0
    50
	private:
sl@0
    51
		void Panic( TInt aPanicNum );
sl@0
    52
		void Start( TFunction aFunction );
sl@0
    53
sl@0
    54
		static TInt EraserThread( TAny* aParam );
sl@0
    55
sl@0
    56
		void DoEraseBlock();
sl@0
    57
sl@0
    58
	private:
sl@0
    59
		RThread		iThread;
sl@0
    60
sl@0
    61
		//
sl@0
    62
		// Shared between main & eraser thread
sl@0
    63
		//
sl@0
    64
		TFunction	iRequestedFunction;
sl@0
    65
		RSemaphore	iGoSignal;
sl@0
    66
		RSemaphore	iWaitingSignal;
sl@0
    67
		TBool		iStop;
sl@0
    68
sl@0
    69
		//
sl@0
    70
		// These are local to the eraser thread
sl@0
    71
		//
sl@0
    72
		TUint		iOffset;
sl@0
    73
		TUint		iLength;
sl@0
    74
		TBusLocalDrive	iDrive;
sl@0
    75
	};
sl@0
    76
sl@0
    77
sl@0
    78
sl@0
    79
CEraser::~CEraser()
sl@0
    80
	{
sl@0
    81
	iThread.Terminate( KErrNone );
sl@0
    82
	iThread.Close();
sl@0
    83
	iGoSignal.Close();
sl@0
    84
	iWaitingSignal.Close();
sl@0
    85
	}
sl@0
    86
sl@0
    87
void CEraser::Panic( TInt aPanicNum )
sl@0
    88
	{
sl@0
    89
	_LIT( KPanicCat, "ERASE-T" );
sl@0
    90
	User::Panic( KPanicCat, aPanicNum );
sl@0
    91
	RProcess().Panic( KPanicCat, aPanicNum );
sl@0
    92
	}
sl@0
    93
sl@0
    94
sl@0
    95
void CEraser::CreateL()
sl@0
    96
	//
sl@0
    97
	// Create new thread and wait for it to become ready
sl@0
    98
	//
sl@0
    99
	{
sl@0
   100
	iGoSignal.CreateLocal( 0 );	// initially blocked
sl@0
   101
	iWaitingSignal.CreateLocal( 0 );	// initially blocked
sl@0
   102
	iStop = EFalse;
sl@0
   103
	User::LeaveIfError( iThread.Create( _L("ERASER"), EraserThread, 2048, 2048, 65536, this ) );
sl@0
   104
	test.Printf( _L("Eraser thread created\n") );
sl@0
   105
	
sl@0
   106
	iThread.Resume();
sl@0
   107
	
sl@0
   108
	test.Printf( _L("Waiting for thread to become ready\n") );
sl@0
   109
	WaitForReady();
sl@0
   110
	iWaitingSignal.Signal();
sl@0
   111
	}
sl@0
   112
sl@0
   113
void CEraser::Start( TFunction aFunction )
sl@0
   114
	//
sl@0
   115
	// Start the suspender thread executing function aFunction
sl@0
   116
	//
sl@0
   117
	{
sl@0
   118
	iStop = EFalse;
sl@0
   119
	WaitForReady();
sl@0
   120
	iRequestedFunction = aFunction;
sl@0
   121
	iGoSignal.Signal();
sl@0
   122
	}
sl@0
   123
sl@0
   124
void CEraser::Stop()
sl@0
   125
	//
sl@0
   126
	// Stop the thread
sl@0
   127
	//
sl@0
   128
	{
sl@0
   129
	iStop = ETrue;
sl@0
   130
	}
sl@0
   131
sl@0
   132
void CEraser::WaitForReady()
sl@0
   133
	{
sl@0
   134
	iWaitingSignal.Wait();
sl@0
   135
	}
sl@0
   136
sl@0
   137
void CEraser::EraseBlock( TUint32 aOffset, TUint aLength )
sl@0
   138
	//
sl@0
   139
	// Execute a block erase
sl@0
   140
	//
sl@0
   141
	{
sl@0
   142
	iOffset = aOffset;
sl@0
   143
	iLength = aLength;
sl@0
   144
	Start( EEraseBlock );
sl@0
   145
	}
sl@0
   146
sl@0
   147
sl@0
   148
TInt CEraser::EraserThread( TAny* aParam )
sl@0
   149
	//
sl@0
   150
	// The thread which executes suspend functions
sl@0
   151
	//
sl@0
   152
	{
sl@0
   153
	RDebug::Print( _L("Eraser thread starts") );
sl@0
   154
sl@0
   155
	CEraser& self = *reinterpret_cast<CEraser*>(aParam);
sl@0
   156
sl@0
   157
	//
sl@0
   158
	// Open our own TBusLogicalDevice channel
sl@0
   159
	//
sl@0
   160
	TBool changedFlag;
sl@0
   161
	if( KErrNone != self.iDrive.Connect( KDriveNumber, changedFlag ) )
sl@0
   162
		{
sl@0
   163
		self.Panic( 1 );
sl@0
   164
		}
sl@0
   165
sl@0
   166
	RDebug::Print( _L("Eraser thread connected to drive") );
sl@0
   167
	
sl@0
   168
	while( !self.iStop )
sl@0
   169
		{
sl@0
   170
		//
sl@0
   171
		// Signal that we are ready for a request
sl@0
   172
		//
sl@0
   173
		RDebug::Print( _L("Eraser thread waiting...") );
sl@0
   174
		self.iWaitingSignal.Signal();
sl@0
   175
sl@0
   176
		//
sl@0
   177
		// Wait for a request
sl@0
   178
		//
sl@0
   179
		self.iGoSignal.Wait();
sl@0
   180
		RDebug::Print( _L("Eraser thread go (%d)"), self.iRequestedFunction );
sl@0
   181
sl@0
   182
		switch( self.iRequestedFunction )
sl@0
   183
			{
sl@0
   184
			case EEraseBlock:
sl@0
   185
				self.DoEraseBlock();
sl@0
   186
				break;
sl@0
   187
sl@0
   188
			default:
sl@0
   189
				self.Panic( 0 );
sl@0
   190
			}
sl@0
   191
sl@0
   192
		}
sl@0
   193
sl@0
   194
	self.iDrive.Disconnect();
sl@0
   195
	return KErrNone;
sl@0
   196
	}
sl@0
   197
sl@0
   198
void CEraser::DoEraseBlock()
sl@0
   199
	//
sl@0
   200
	// Issue an erase
sl@0
   201
	//
sl@0
   202
	{
sl@0
   203
	RDebug::Print( _L("Eraser starting erase...") );
sl@0
   204
	
sl@0
   205
	TInt64 offs( iOffset );
sl@0
   206
	TInt r = iDrive.Format( offs, iLength );
sl@0
   207
	
sl@0
   208
	if( KErrNone != r )
sl@0
   209
		{
sl@0
   210
		RDebug::Print( _L("Eraser: FAIL: erase request returns %d"), r );
sl@0
   211
		Panic( 2 );
sl@0
   212
		}
sl@0
   213
	}
sl@0
   214
sl@0
   215
sl@0
   216
sl@0
   217
class CSuspendTest : public CBase
sl@0
   218
	{
sl@0
   219
	public:
sl@0
   220
		~CSuspendTest();
sl@0
   221
sl@0
   222
		void CreateL();
sl@0
   223
sl@0
   224
		void DoTest();
sl@0
   225
sl@0
   226
	private:
sl@0
   227
sl@0
   228
		TInt EraseOneBlock( TInt aBlockNumber );
sl@0
   229
		TInt ZeroFillBlock( TInt aBlockNumber );
sl@0
   230
		TBool ValidateBlock( TInt aBlockNumber, TUint32 aFillWord );
sl@0
   231
		TInt ZeroAllBlocks();
sl@0
   232
		TBool ValidateAllBlocks( TUint32 aFillWord );
sl@0
   233
sl@0
   234
		void DoImmediateSuspendTest();
sl@0
   235
sl@0
   236
	private:
sl@0
   237
		TBusLocalDrive	iDrive;
sl@0
   238
		TBool			iDriveOpened;
sl@0
   239
sl@0
   240
		CEraser*		iEraser;
sl@0
   241
sl@0
   242
		TInt			iFlashSize;
sl@0
   243
		TInt			iBlockSize;
sl@0
   244
		TInt			iBlockCount;
sl@0
   245
sl@0
   246
		TBuf8<512>		iReadBuffer;
sl@0
   247
	};
sl@0
   248
sl@0
   249
sl@0
   250
CSuspendTest::~CSuspendTest()
sl@0
   251
	{
sl@0
   252
	if( iDriveOpened )
sl@0
   253
		{
sl@0
   254
		iDrive.Disconnect();
sl@0
   255
		}
sl@0
   256
sl@0
   257
	delete iEraser;
sl@0
   258
	}
sl@0
   259
sl@0
   260
sl@0
   261
sl@0
   262
void CSuspendTest::CreateL()
sl@0
   263
	{
sl@0
   264
	//
sl@0
   265
	// Create the eraser thread
sl@0
   266
	//
sl@0
   267
	iEraser = new(ELeave) CEraser;
sl@0
   268
	iEraser->CreateL();
sl@0
   269
sl@0
   270
	//
sl@0
   271
	// Load the device drivers
sl@0
   272
	//
sl@0
   273
	TInt r;
sl@0
   274
#ifndef SKIP_PDD_LOAD
sl@0
   275
	test.Printf( _L("Loading %S\n"), &KLfsDriverName );
sl@0
   276
	r = User::LoadPhysicalDevice( KLfsDriverName );
sl@0
   277
	test( KErrNone == r || KErrAlreadyExists == r );
sl@0
   278
#endif
sl@0
   279
sl@0
   280
#ifdef UNMOUNT_DRIVE
sl@0
   281
	RFs fs;
sl@0
   282
	test( KErrNone == fs.Connect() );
sl@0
   283
#if 0
sl@0
   284
	// XXX - not EKA2
sl@0
   285
	test( KErrNone == fs.SetDefaultPath( _L("Z:\\") ) );
sl@0
   286
#endif
sl@0
   287
	TFullName name;
sl@0
   288
	fs.FileSystemName( name, KLffsLogicalDriveNumber );
sl@0
   289
	if( name.Length() > 0 )
sl@0
   290
		{
sl@0
   291
		test.Printf( _L("Unmounting drive") );
sl@0
   292
		test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) );
sl@0
   293
		User::After( 2000000 );
sl@0
   294
		test.Printf( _L("Drive unmounted") );
sl@0
   295
		}
sl@0
   296
	fs.Close();
sl@0
   297
#endif
sl@0
   298
sl@0
   299
	//
sl@0
   300
	// Open a TBusLogicalDevice to the driver
sl@0
   301
	//
sl@0
   302
	test.Printf( _L("Opening media channel\n") );
sl@0
   303
	TBool changedFlag = EFalse;
sl@0
   304
	r = iDrive.Connect( KDriveNumber, changedFlag );
sl@0
   305
	User::LeaveIfError( r );
sl@0
   306
	iDriveOpened = ETrue;
sl@0
   307
sl@0
   308
	//
sl@0
   309
	// Get size of Flash drive, block size, block count
sl@0
   310
	//
sl@0
   311
	TLocalDriveCapsV2Buf info;
sl@0
   312
    iDrive.Caps(info);
sl@0
   313
	iFlashSize = I64LOW(info().iSize);
sl@0
   314
	iBlockSize = info().iEraseBlockSize;
sl@0
   315
	iBlockCount = iFlashSize / iBlockSize;
sl@0
   316
sl@0
   317
	test.Printf( _L("Flash size is 0x%x bytes\n"), iFlashSize );
sl@0
   318
	test.Printf( _L("Block size is 0x%x bytes\n"), iBlockSize );
sl@0
   319
	test.Printf( _L("Block count is %d\n"), iBlockCount );
sl@0
   320
sl@0
   321
	test.Printf( _L("CreateL complete\n") );
sl@0
   322
	}
sl@0
   323
sl@0
   324
sl@0
   325
void CSuspendTest::DoTest()
sl@0
   326
	//
sl@0
   327
	// Main test dispatcher
sl@0
   328
	//
sl@0
   329
	{
sl@0
   330
	DoImmediateSuspendTest();
sl@0
   331
	}
sl@0
   332
sl@0
   333
sl@0
   334
TInt CSuspendTest::EraseOneBlock( TInt aBlockNumber )
sl@0
   335
	//
sl@0
   336
	// Erases block aBlockNumber on Flash
sl@0
   337
	//
sl@0
   338
	{
sl@0
   339
	TInt blockBaseOffset = aBlockNumber * iBlockSize;
sl@0
   340
sl@0
   341
	test.Printf( _L("Erasing block %d (offs=0x%x)\n"), aBlockNumber, blockBaseOffset );
sl@0
   342
	
sl@0
   343
	TInt r = iDrive.Format( blockBaseOffset, iBlockSize );
sl@0
   344
sl@0
   345
	test.Printf( _L("... block erased, rv=%d\n"), r );
sl@0
   346
	return r;
sl@0
   347
	}
sl@0
   348
sl@0
   349
sl@0
   350
TBool CSuspendTest::ValidateBlock( TInt aBlockNumber, TUint32 aFillWord )
sl@0
   351
	//
sl@0
   352
	// Checks that every word in block aBlockNumber has the value aFillWord
sl@0
   353
	//
sl@0
   354
	{
sl@0
   355
	TUint offset = aBlockNumber * iBlockSize;
sl@0
   356
	test.Printf( _L("Validating block %d (offs=0x%x)\n"), aBlockNumber, offset );
sl@0
   357
	
sl@0
   358
	TBool failed = EFalse;
sl@0
   359
	const TInt readBufLen = iReadBuffer.MaxLength();
sl@0
   360
sl@0
   361
	for( TInt len = iBlockSize; len > 0 && !failed ;)
sl@0
   362
		{
sl@0
   363
		TInt r = iDrive.Read( offset, readBufLen, iReadBuffer );
sl@0
   364
		if( r != KErrNone )
sl@0
   365
			{
sl@0
   366
			test.Printf( _L("... FAIL: read failed (%d) at offset 0x%x\n"), r, offset );
sl@0
   367
			test( KErrNone == r );
sl@0
   368
			}
sl@0
   369
		test( iReadBuffer.Length() == readBufLen );
sl@0
   370
sl@0
   371
		TUint32* p = (TUint32*)iReadBuffer.Ptr();
sl@0
   372
		for( TInt i = 0; i < readBufLen; i += 4 )
sl@0
   373
			{
sl@0
   374
			if( aFillWord != *p )
sl@0
   375
				{
sl@0
   376
				failed = ETrue;
sl@0
   377
				test.Printf( _L("... FAILED: word @ offs=0x%x, read=0x%x, expected=0x%x\n"), 
sl@0
   378
								offset+i, p[0], aFillWord );
sl@0
   379
				break;
sl@0
   380
				}
sl@0
   381
			++p;
sl@0
   382
			}
sl@0
   383
		offset += readBufLen;
sl@0
   384
		len -= readBufLen;
sl@0
   385
		}
sl@0
   386
	
sl@0
   387
	return !failed;
sl@0
   388
	}
sl@0
   389
sl@0
   390
sl@0
   391
TInt CSuspendTest::ZeroFillBlock( TInt aBlockNumber )
sl@0
   392
	//
sl@0
   393
	// Zero-fills and entire block
sl@0
   394
	// The requires that writing works
sl@0
   395
	//
sl@0
   396
	{
sl@0
   397
	test.Printf( _L("Zero-filling block %d\n"), aBlockNumber );
sl@0
   398
sl@0
   399
	//
sl@0
   400
	// Create a buffer full of zeros
sl@0
   401
	//
sl@0
   402
	const TInt KZeroBufSize = 512;
sl@0
   403
sl@0
   404
	TBuf8<KZeroBufSize> buf;
sl@0
   405
	buf.FillZ( buf.MaxLength() );
sl@0
   406
sl@0
   407
	//
sl@0
   408
	// Write the data out to the Flash
sl@0
   409
	//
sl@0
   410
	TInt writeCount = iBlockSize / KZeroBufSize;
sl@0
   411
	TInt r = KErrNone;
sl@0
   412
	TInt blockBaseOffset = aBlockNumber * iBlockSize;
sl@0
   413
	for( ; (writeCount > 0) && (KErrNone == r); writeCount-- )
sl@0
   414
		{
sl@0
   415
		r = iDrive.Write( blockBaseOffset, buf );
sl@0
   416
		if( r != KErrNone )
sl@0
   417
			{
sl@0
   418
			test.Printf( _L("... FAIL: write failed (%d) at offset 0x%x\n"), blockBaseOffset );
sl@0
   419
			}
sl@0
   420
		blockBaseOffset += KZeroBufSize;
sl@0
   421
		}
sl@0
   422
sl@0
   423
	return r;
sl@0
   424
	}
sl@0
   425
sl@0
   426
sl@0
   427
TInt CSuspendTest::ZeroAllBlocks()
sl@0
   428
	//
sl@0
   429
	// Writes zeros to all blocks
sl@0
   430
	//
sl@0
   431
	{
sl@0
   432
	test.Printf( _L("Zeroing all blocks\n") );
sl@0
   433
sl@0
   434
	TInt r = KErrNone;
sl@0
   435
	for( TInt i = 0; (i < iBlockCount) && (KErrNone == r); i++ )
sl@0
   436
		{
sl@0
   437
		r = ZeroFillBlock( i );
sl@0
   438
		}
sl@0
   439
sl@0
   440
	return r;
sl@0
   441
	}
sl@0
   442
sl@0
   443
TBool CSuspendTest::ValidateAllBlocks( TUint32 aFillWord )
sl@0
   444
	//
sl@0
   445
	// Checks that all blocks contain aFillWord
sl@0
   446
	//
sl@0
   447
	{
sl@0
   448
	test.Printf( _L("Validating all blocks\n") );
sl@0
   449
sl@0
   450
	TBool failed = EFalse;
sl@0
   451
	for( TInt i = 0; (i < iBlockCount) && (!failed); i++ )
sl@0
   452
		{
sl@0
   453
		failed = !ValidateBlock( i, aFillWord );
sl@0
   454
		}
sl@0
   455
sl@0
   456
	return !failed;
sl@0
   457
	}
sl@0
   458
sl@0
   459
sl@0
   460
void CSuspendTest::DoImmediateSuspendTest()
sl@0
   461
	//
sl@0
   462
	// For each block issues an erase and then immediately
sl@0
   463
	// requests a read on another block. Waits for erase to
sl@0
   464
	// finish and validates it
sl@0
   465
	//
sl@0
   466
	{
sl@0
   467
	test.Next( _L("Immediate suspend test") );
sl@0
   468
	test( KErrNone == ZeroAllBlocks() );
sl@0
   469
	test( ValidateAllBlocks( 0 ) );
sl@0
   470
sl@0
   471
	//
sl@0
   472
	// We repeat the test for each block, erasing block n and reading from
sl@0
   473
	// block (n+1) modulo iBlockCount
sl@0
   474
	//
sl@0
   475
	for( TInt eraseBlock = 0; eraseBlock < iBlockCount; eraseBlock++ )
sl@0
   476
		{
sl@0
   477
		TUint32 readBlock = (eraseBlock + 1) % iBlockCount;
sl@0
   478
		TUint32 erasePos = eraseBlock * iBlockSize;
sl@0
   479
		TInt readPos = readBlock * iBlockSize;
sl@0
   480
sl@0
   481
		TBuf8<32> buf;
sl@0
   482
sl@0
   483
		//
sl@0
   484
		// Start the erase
sl@0
   485
		//
sl@0
   486
		iEraser->EraseBlock( erasePos, iBlockSize );
sl@0
   487
sl@0
   488
		//
sl@0
   489
		// Do a read immediately
sl@0
   490
		//
sl@0
   491
		test.Printf( _L("main thread requesting read") );
sl@0
   492
		test( KErrNone == iDrive.Read( readPos, buf.MaxLength(), buf ) );
sl@0
   493
sl@0
   494
		//
sl@0
   495
		// Wait for erase to finish
sl@0
   496
		//
sl@0
   497
		test.Printf( _L("main thread waiting for erase to finish...") );
sl@0
   498
		iEraser->WaitForDone();
sl@0
   499
sl@0
   500
		//
sl@0
   501
		// Now check that the block was erased
sl@0
   502
		//
sl@0
   503
		test( ValidateBlock( eraseBlock, 0xFFFFFFFF ) );
sl@0
   504
		
sl@0
   505
		}
sl@0
   506
sl@0
   507
	}
sl@0
   508
sl@0
   509
sl@0
   510
sl@0
   511
sl@0
   512
TInt E32Main()
sl@0
   513
	{
sl@0
   514
	test.Title();
sl@0
   515
	test.Start(_L("Testing media erase+suspend operations"));
sl@0
   516
sl@0
   517
	CSuspendTest suspendTest;
sl@0
   518
	TRAPD( ret, suspendTest.CreateL() );
sl@0
   519
	if( KErrNone == ret )
sl@0
   520
		{
sl@0
   521
		suspendTest.DoTest();
sl@0
   522
		}
sl@0
   523
sl@0
   524
	test.End();
sl@0
   525
	return KErrNone;
sl@0
   526
	}