sl@0: // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Simple app to dump out the contents of a drive sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "user_config.h" sl@0: sl@0: RTest test( _L("TF_DUMP") ); sl@0: sl@0: sl@0: const TInt KBytesPerLine = 32; sl@0: sl@0: sl@0: class CDumper : public CBase sl@0: { sl@0: public: sl@0: ~CDumper(); sl@0: sl@0: void CreateL(); sl@0: void DoDump(); sl@0: sl@0: private: sl@0: void Add3Bytes( const TUint8* aBytes ); sl@0: sl@0: private: sl@0: TBusLocalDrive iDrive; sl@0: TBool iDriveOpened; sl@0: TBool iDriveSize; sl@0: sl@0: TBuf iLineBuf; sl@0: }; sl@0: sl@0: sl@0: CDumper::~CDumper() sl@0: { sl@0: if( iDriveOpened ) sl@0: { sl@0: iDrive.Disconnect(); sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: void CDumper::CreateL() sl@0: { sl@0: // sl@0: // Load the device driver sl@0: // sl@0: #ifdef UNMOUNT_DRIVE sl@0: RFs fs; sl@0: test( KErrNone == fs.Connect() ); sl@0: #if 0 sl@0: // XXX not EKA2 sl@0: test( KErrNone == fs.SetDefaultPath( _L("Z:\\") ) ); sl@0: #endif sl@0: TFullName name; sl@0: fs.FileSystemName( name, KLffsLogicalDriveNumber ); sl@0: if( name.Length() > 0 ) sl@0: { sl@0: test.Printf( _L("Unmounting drive") ); sl@0: test( KErrNone == fs.DismountFileSystem( _L("Lffs"), KLffsLogicalDriveNumber) ); sl@0: User::After( 2000000 ); sl@0: test.Printf( _L("Drive unmounted") ); sl@0: } sl@0: fs.Close(); sl@0: #endif sl@0: sl@0: // sl@0: // Open a TBusLogicalDevice to it sl@0: // sl@0: test.Printf( _L("Opening media channel") ); sl@0: TBool changedFlag = EFalse; sl@0: User::LeaveIfError( iDrive.Connect( KDriveNumber, changedFlag ) ); sl@0: iDriveOpened = ETrue; sl@0: sl@0: // sl@0: // Get size of Flash drive sl@0: // sl@0: TLocalDriveCapsV2Buf info; sl@0: iDrive.Caps(info); sl@0: iDriveSize = I64LOW(info().iSize); sl@0: sl@0: sl@0: iLineBuf.Zero(); sl@0: test.Printf( _L("Drive size is 0x%x bytes"), iDriveSize ); sl@0: } sl@0: sl@0: sl@0: void CDumper::Add3Bytes( const TUint8* aBytes ) sl@0: { sl@0: TUint8 buf[4]; // produces four output bytes sl@0: sl@0: buf[0] = 0x30 + (aBytes[0] >> 2); // first 6 bits sl@0: buf[1] = 0x30 + ( ((aBytes[0] << 4)&0x30) | (aBytes[1] >> 4)); sl@0: buf[2] = 0x30 + ( ((aBytes[1] << 2)&0x3C) | (aBytes[2] >> 6)); sl@0: buf[3] = 0x30 + (aBytes[2] & 0x3F); // last 6 bits sl@0: sl@0: TInt avail = iLineBuf.MaxLength() - iLineBuf.Length(); sl@0: if( avail > 4 ) sl@0: { sl@0: avail = 4; sl@0: } sl@0: sl@0: TUint8* p = &buf[0]; sl@0: for( TInt i = 0; i < avail; i++ ) sl@0: { sl@0: iLineBuf.Append( *p++ ); sl@0: } sl@0: if( avail < 4 ) sl@0: { sl@0: test.Printf( iLineBuf ); sl@0: iLineBuf.Zero(); sl@0: for( TInt i = 4 - avail; i > 0; i-- ) sl@0: { sl@0: iLineBuf.Append( *p++ ); sl@0: } sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: void CDumper::DoDump() sl@0: { sl@0: TBuf8<300> buf; sl@0: sl@0: TInt remain = iDriveSize; sl@0: TInt64 offset = 0; sl@0: while( remain > 0 ) sl@0: { sl@0: TInt readLen = Min( remain, 300 ); sl@0: TInt r = iDrive.Read( offset, readLen, buf ); sl@0: if( KErrNone != r ) sl@0: { sl@0: RDebug::Print( _L("Error: drive read failed %d"), r ); sl@0: return; sl@0: } sl@0: TUint8* p = (TUint8*)buf.Ptr(); sl@0: TInt remainder = readLen % 3; sl@0: if( remainder != 0 ) sl@0: { sl@0: // leftover bytes, must be end of media, pad buffer sl@0: buf.AppendFill( 0x0, 3 - remainder ); sl@0: readLen += 3 - remainder; // round up sl@0: } sl@0: for( TInt i = readLen / 3; i > 0; i-- ) sl@0: { sl@0: Add3Bytes( p ); sl@0: p += 3; sl@0: } sl@0: offset += readLen; sl@0: remain -= readLen; sl@0: } sl@0: test.Printf( iLineBuf ); // remaining bit sl@0: } sl@0: sl@0: sl@0: TInt E32Main() sl@0: { sl@0: test.Title(); sl@0: test.Start(_L("Dumping Flash contents....")); sl@0: sl@0: CDumper dumper; sl@0: TRAPD( ret, dumper.CreateL() ); sl@0: if( KErrNone == ret ) sl@0: { sl@0: dumper.DoDump(); sl@0: } sl@0: return KErrNone; sl@0: }