os/kernelhwsrv/kerneltest/e32test/lffs/tf_dump.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2001-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 // Simple app to dump out the contents of a drive
    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_DUMP") );
    25 
    26 
    27 const TInt KBytesPerLine = 32;
    28 
    29 
    30 class CDumper : public CBase
    31 	{
    32 	public:
    33 		~CDumper();
    34 
    35 		void CreateL();
    36 		void DoDump();
    37 
    38 	private:
    39 		void Add3Bytes( const TUint8* aBytes );
    40 
    41 	private:
    42 		TBusLocalDrive	iDrive;
    43 		TBool			iDriveOpened;
    44 		TBool			iDriveSize;
    45 
    46 		TBuf<KBytesPerLine>	iLineBuf;
    47 	};
    48 
    49 
    50 CDumper::~CDumper()
    51 	{
    52 	if( iDriveOpened )
    53 		{
    54 		iDrive.Disconnect();
    55 		}
    56 	}
    57 
    58 
    59 
    60 void CDumper::CreateL()
    61 	{
    62 	//
    63 	// Load the device driver
    64 	//
    65 #ifdef UNMOUNT_DRIVE
    66 	RFs fs;
    67 	test( KErrNone == fs.Connect() );
    68 #if 0
    69 	// XXX not EKA2
    70 	test( KErrNone == fs.SetDefaultPath( _L("Z:\\") ) );
    71 #endif
    72 	TFullName name;
    73 	fs.FileSystemName( name, KLffsLogicalDriveNumber );
    74 	if( name.Length() > 0 )
    75 		{
    76 		test.Printf( _L("Unmounting drive") );
    77 		test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) );
    78 		User::After( 2000000 );
    79 		test.Printf( _L("Drive unmounted") );
    80 		}
    81 	fs.Close();
    82 #endif
    83 
    84 	//
    85 	// Open a TBusLogicalDevice to it
    86 	//
    87 	test.Printf( _L("Opening media channel") );
    88 	TBool changedFlag = EFalse;
    89 	User::LeaveIfError( iDrive.Connect( KDriveNumber, changedFlag ) );
    90 	iDriveOpened = ETrue;
    91 
    92 	//
    93 	// Get size of Flash drive
    94 	//
    95 	TLocalDriveCapsV2Buf info;
    96     iDrive.Caps(info);
    97 	iDriveSize = I64LOW(info().iSize);
    98 
    99 
   100 	iLineBuf.Zero();
   101 	test.Printf( _L("Drive size is 0x%x bytes"), iDriveSize );
   102 	}
   103 
   104 
   105 void CDumper::Add3Bytes( const TUint8* aBytes )
   106 	{
   107 	TUint8	buf[4];		// produces four output bytes
   108 
   109 	buf[0] = 0x30 + (aBytes[0] >> 2);	// first 6 bits
   110 	buf[1] = 0x30 + ( ((aBytes[0] << 4)&0x30) | (aBytes[1] >> 4));
   111 	buf[2] = 0x30 + ( ((aBytes[1] << 2)&0x3C) | (aBytes[2] >> 6));
   112 	buf[3] = 0x30 + (aBytes[2] & 0x3F);	// last 6 bits
   113 
   114 	TInt avail = iLineBuf.MaxLength() - iLineBuf.Length();
   115 	if( avail > 4 )
   116 		{
   117 		avail = 4;
   118 		}
   119 
   120 	TUint8* p = &buf[0];
   121 	for( TInt i = 0; i < avail; i++ )
   122 		{
   123 		iLineBuf.Append( *p++ );
   124 		}
   125 	if( avail < 4 )
   126 		{
   127 		test.Printf( iLineBuf );
   128 		iLineBuf.Zero();
   129 		for( TInt i = 4 - avail; i > 0; i-- )
   130 			{
   131 			iLineBuf.Append( *p++ );
   132 			}
   133 		}
   134 	}
   135 
   136 
   137 
   138 void CDumper::DoDump()
   139 	{
   140 	TBuf8<300> buf;
   141 
   142 	TInt remain = iDriveSize;
   143 	TInt64 offset = 0;
   144 	while( remain > 0 )
   145 		{
   146 		TInt readLen = Min( remain, 300 );
   147 		TInt r = iDrive.Read( offset, readLen, buf );
   148 		if( KErrNone != r )
   149 			{
   150 			RDebug::Print( _L("Error: drive read failed %d"), r );
   151 			return;
   152 			}
   153 		TUint8* p = (TUint8*)buf.Ptr();
   154 		TInt remainder = readLen % 3;
   155 		if( remainder != 0 )
   156 			{
   157 			// leftover bytes, must be end of media, pad buffer
   158 			buf.AppendFill( 0x0, 3 - remainder );
   159 			readLen += 3 - remainder;	// round up
   160 			}
   161 		for( TInt i = readLen / 3; i > 0; i-- )
   162 			{
   163 			Add3Bytes( p );
   164 			p += 3;
   165 			}
   166 		offset += readLen;
   167 		remain -= readLen;
   168 		}
   169 	test.Printf( iLineBuf );	// remaining bit
   170 	}
   171 
   172 
   173 TInt E32Main()
   174 	{
   175 	test.Title();
   176 	test.Start(_L("Dumping Flash contents...."));
   177 
   178 	CDumper dumper;
   179 	TRAPD( ret, dumper.CreateL() );
   180 	if( KErrNone == ret )
   181 		{
   182 		dumper.DoDump();
   183 		}
   184 	return KErrNone;
   185 	}