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