sl@0: // Copyright (c) 1998-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: // e32test\misc\flash.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include "flash.h" sl@0: sl@0: RTest test(_L("FLASH")); sl@0: TInt Mode; sl@0: TFileName FileName; sl@0: RFs Fs; sl@0: RFile File; sl@0: TUint32 StartAddr; sl@0: TUint32 Size; sl@0: sl@0: const TUint KBufferSize=4096; sl@0: TUint8 Buffer[KBufferSize]; sl@0: TUint8 Buffer2[KBufferSize]; sl@0: sl@0: enum TMode sl@0: { sl@0: EDump, sl@0: EBlankCheck, sl@0: EErase, sl@0: EWrite, sl@0: EVerify, sl@0: }; sl@0: sl@0: void Usage() sl@0: { sl@0: test.Printf(_L("Usage:\n")); sl@0: test.Printf(_L(" flash d - dump flash to file\n")); sl@0: test.Printf(_L(" flash b - blank check flash\n")); sl@0: test.Printf(_L(" flash e - erase flash\n")); sl@0: test.Printf(_L(" flash w - write file to flash\n")); sl@0: test.Printf(_L(" flash v - verify flash against file\n")); sl@0: } sl@0: sl@0: TInt ParseCommandLine() sl@0: { sl@0: TBuf<256> cmd; sl@0: User::CommandLine(cmd); sl@0: cmd.Trim(); sl@0: if (cmd.Length()==0) sl@0: return KErrArgument; sl@0: TLex lex(cmd); sl@0: TPtrC mode_str=lex.NextToken(); sl@0: switch (mode_str[0]) sl@0: { sl@0: case 'D': sl@0: case 'd': sl@0: Mode=EDump; break; sl@0: case 'B': sl@0: case 'b': sl@0: Mode=EBlankCheck; break; sl@0: case 'E': sl@0: case 'e': sl@0: Mode=EErase; break; sl@0: case 'W': sl@0: case 'w': sl@0: Mode=EWrite; break; sl@0: case 'V': sl@0: case 'v': sl@0: Mode=EVerify; break; sl@0: default: sl@0: return KErrArgument; sl@0: } sl@0: lex.SkipSpace(); sl@0: TInt r=lex.Val(StartAddr,EHex); sl@0: if (r!=KErrNone) sl@0: return r; sl@0: if (Mode==EDump || Mode==EBlankCheck || Mode==EErase) sl@0: { sl@0: lex.SkipSpace(); sl@0: r=lex.Val(Size,EHex); sl@0: if (r!=KErrNone) sl@0: return r; sl@0: } sl@0: if (Mode==EDump || Mode==EWrite || Mode==EVerify) sl@0: { sl@0: lex.SkipSpace(); sl@0: FileName=lex.NextToken(); sl@0: if (FileName.Length()==0) sl@0: return KErrArgument; sl@0: if (Mode==EDump) sl@0: { sl@0: r=File.Replace(Fs,FileName,EFileWrite); sl@0: if (r!=KErrNone) sl@0: { sl@0: test.Printf(_L("\nCould not open file %S for write\n"),&FileName); sl@0: return KErrGeneral; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: r=File.Open(Fs,FileName,EFileRead); sl@0: if (r!=KErrNone) sl@0: { sl@0: test.Printf(_L("\nCould not open file %S for read\n"),&FileName); sl@0: return KErrGeneral; sl@0: } sl@0: else sl@0: { sl@0: r=File.Size((TInt&)Size); sl@0: if (r!=KErrNone) sl@0: { sl@0: test.Printf(_L("\nError %d accessing file %S\n"),r,&FileName); sl@0: File.Close(); sl@0: return KErrGeneral; sl@0: } sl@0: test.Printf(_L("File size %x\n"),Size); sl@0: } sl@0: } sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: void DoCommand() sl@0: { sl@0: Flash* pF=Flash::New(StartAddr); sl@0: if (!pF) sl@0: { sl@0: test.Printf(_L("Problem with flash device\n")); sl@0: return; sl@0: } sl@0: TInt r=KErrNotSupported; sl@0: switch (Mode) sl@0: { sl@0: case EDump: sl@0: { sl@0: while (Size) sl@0: { sl@0: TUint size=Size; sl@0: if (size>KBufferSize) sl@0: size=KBufferSize; sl@0: r=pF->Read(StartAddr,size,Buffer); sl@0: if (r!=KErrNone) sl@0: break; sl@0: r=File.Write(TPtrC8(Buffer,size)); sl@0: if (r!=KErrNone) sl@0: break; sl@0: StartAddr+=size; sl@0: Size-=size; sl@0: } sl@0: break; sl@0: } sl@0: case EBlankCheck: sl@0: { sl@0: r=pF->BlankCheck(StartAddr,Size); sl@0: if (r!=KErrNone) sl@0: { sl@0: test.Printf(_L("Error at address %08x\n"),StartAddr+r-1); sl@0: } sl@0: break; sl@0: } sl@0: case EErase: sl@0: r=pF->Erase(StartAddr,Size); sl@0: if (r!=KErrNone) sl@0: { sl@0: test.Printf(_L("Error at address %08x\n"),StartAddr+r-1); sl@0: } sl@0: break; sl@0: case EWrite: sl@0: { sl@0: while (Size) sl@0: { sl@0: test.Printf(_L("%08x\n"),StartAddr); sl@0: TUint size=Size; sl@0: if (size>KBufferSize) sl@0: size=KBufferSize; sl@0: TPtr8 p8(Buffer,0,size); sl@0: r=File.Read(p8); sl@0: if (r!=KErrNone) sl@0: break; sl@0: r=pF->Write(StartAddr,size,Buffer); sl@0: if (r!=KErrNone) sl@0: break; sl@0: StartAddr+=size; sl@0: Size-=size; sl@0: } sl@0: break; sl@0: } sl@0: case EVerify: sl@0: { sl@0: while (Size) sl@0: { sl@0: TUint size=Size; sl@0: if (size>KBufferSize) sl@0: size=KBufferSize; sl@0: r=pF->Read(StartAddr,size,Buffer); sl@0: if (r!=KErrNone) sl@0: break; sl@0: TPtr8 p8(Buffer2,0,size); sl@0: r=File.Read(p8); sl@0: if (r!=KErrNone) sl@0: break; sl@0: TUint i; sl@0: for (i=0; i