os/kernelhwsrv/kerneltest/e32test/misc/flash.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-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 // e32test\misc\flash.cpp
    15 // 
    16 //
    17 
    18 #include <e32test.h>
    19 #include <f32file.h>
    20 #include "flash.h"
    21 
    22 RTest test(_L("FLASH"));
    23 TInt Mode;
    24 TFileName FileName;
    25 RFs Fs;
    26 RFile File;
    27 TUint32 StartAddr;
    28 TUint32 Size;
    29 
    30 const TUint KBufferSize=4096;
    31 TUint8 Buffer[KBufferSize];
    32 TUint8 Buffer2[KBufferSize];
    33 
    34 enum TMode
    35 	{
    36 	EDump,
    37 	EBlankCheck,
    38 	EErase,
    39 	EWrite,
    40 	EVerify,
    41 	};
    42 
    43 void Usage()
    44 	{
    45 	test.Printf(_L("Usage:\n"));
    46 	test.Printf(_L(" flash d <addr> <size> <file> - dump flash to file\n"));
    47 	test.Printf(_L(" flash b <addr> <size>        - blank check flash\n"));
    48 	test.Printf(_L(" flash e <addr> <size>        - erase flash\n"));
    49 	test.Printf(_L(" flash w <addr> <file>        - write file to flash\n"));
    50 	test.Printf(_L(" flash v <addr> <file>        - verify flash against file\n"));
    51 	}
    52 
    53 TInt ParseCommandLine()
    54 	{
    55 	TBuf<256> cmd;
    56 	User::CommandLine(cmd);
    57 	cmd.Trim();
    58 	if (cmd.Length()==0)
    59 		return KErrArgument;
    60 	TLex lex(cmd);
    61 	TPtrC mode_str=lex.NextToken();
    62 	switch (mode_str[0])
    63 		{
    64 		case 'D':
    65 		case 'd':
    66 			Mode=EDump; break;
    67 		case 'B':
    68 		case 'b':
    69 			Mode=EBlankCheck; break;
    70 		case 'E':
    71 		case 'e':
    72 			Mode=EErase; break;
    73 		case 'W':
    74 		case 'w':
    75 			Mode=EWrite; break;
    76 		case 'V':
    77 		case 'v':
    78 			Mode=EVerify; break;
    79 		default:
    80 			return KErrArgument;
    81 		}
    82 	lex.SkipSpace();
    83 	TInt r=lex.Val(StartAddr,EHex);
    84 	if (r!=KErrNone)
    85 		return r;
    86 	if (Mode==EDump || Mode==EBlankCheck || Mode==EErase)
    87 		{
    88 		lex.SkipSpace();
    89 		r=lex.Val(Size,EHex);
    90 		if (r!=KErrNone)
    91 			return r;
    92 		}
    93 	if (Mode==EDump || Mode==EWrite || Mode==EVerify)
    94 		{
    95 		lex.SkipSpace();
    96 		FileName=lex.NextToken();
    97 		if (FileName.Length()==0)
    98 			return KErrArgument;
    99 		if (Mode==EDump)
   100 			{
   101 			r=File.Replace(Fs,FileName,EFileWrite);
   102 			if (r!=KErrNone)
   103 				{
   104 				test.Printf(_L("\nCould not open file %S for write\n"),&FileName);
   105 				return KErrGeneral;
   106 				}
   107 			}
   108 		else
   109 			{
   110 			r=File.Open(Fs,FileName,EFileRead);
   111 			if (r!=KErrNone)
   112 				{
   113 				test.Printf(_L("\nCould not open file %S for read\n"),&FileName);
   114 				return KErrGeneral;
   115 				}
   116 			else
   117 				{
   118 				r=File.Size((TInt&)Size);
   119 				if (r!=KErrNone)
   120 					{
   121 					test.Printf(_L("\nError %d accessing file %S\n"),r,&FileName);
   122 					File.Close();
   123 					return KErrGeneral;
   124 					}
   125 				test.Printf(_L("File size %x\n"),Size);
   126 				}
   127 			}
   128 		}
   129 	return KErrNone;
   130 	}
   131 
   132 void DoCommand()
   133 	{
   134 	Flash* pF=Flash::New(StartAddr);
   135 	if (!pF)
   136 		{
   137 		test.Printf(_L("Problem with flash device\n"));
   138 		return;
   139 		}
   140 	TInt r=KErrNotSupported;
   141 	switch (Mode)
   142 		{
   143 		case EDump:
   144 			{
   145 			while (Size)
   146 				{
   147 				TUint size=Size;
   148 				if (size>KBufferSize)
   149 					size=KBufferSize;
   150 				r=pF->Read(StartAddr,size,Buffer);
   151 				if (r!=KErrNone)
   152 					break;
   153 				r=File.Write(TPtrC8(Buffer,size));
   154 				if (r!=KErrNone)
   155 					break;
   156 				StartAddr+=size;
   157 				Size-=size;
   158 				}
   159 			break;
   160 			}
   161 		case EBlankCheck:
   162 			{
   163 			r=pF->BlankCheck(StartAddr,Size);
   164 			if (r!=KErrNone)
   165 				{
   166 				test.Printf(_L("Error at address %08x\n"),StartAddr+r-1);
   167 				}
   168 			break;
   169 			}
   170 		case EErase:
   171 			r=pF->Erase(StartAddr,Size);
   172 			if (r!=KErrNone)
   173 				{
   174 				test.Printf(_L("Error at address %08x\n"),StartAddr+r-1);
   175 				}
   176 			break;
   177 		case EWrite:
   178 			{
   179 			while (Size)
   180 				{
   181 				test.Printf(_L("%08x\n"),StartAddr);
   182 				TUint size=Size;
   183 				if (size>KBufferSize)
   184 					size=KBufferSize;
   185 				TPtr8 p8(Buffer,0,size);
   186 				r=File.Read(p8);
   187 				if (r!=KErrNone)
   188 					break;
   189 				r=pF->Write(StartAddr,size,Buffer);
   190 				if (r!=KErrNone)
   191 					break;
   192 				StartAddr+=size;
   193 				Size-=size;
   194 				}
   195 			break;
   196 			}
   197 		case EVerify:
   198 			{
   199 			while (Size)
   200 				{
   201 				TUint size=Size;
   202 				if (size>KBufferSize)
   203 					size=KBufferSize;
   204 				r=pF->Read(StartAddr,size,Buffer);
   205 				if (r!=KErrNone)
   206 					break;
   207 				TPtr8 p8(Buffer2,0,size);
   208 				r=File.Read(p8);
   209 				if (r!=KErrNone)
   210 					break;
   211 				TUint i;
   212 				for (i=0; i<size; ++i)
   213 					{
   214 					if (Buffer2[i]!=Buffer[i])
   215 						{
   216 						test.Printf(_L("VERIFY ERROR: Flash addr %08x, flash %02x, file %02x\n"),
   217 									StartAddr+i,Buffer[i],Buffer2[i]);
   218 						return;
   219 						}
   220 					}
   221 				StartAddr+=size;
   222 				Size-=size;
   223 				}
   224 			break;
   225 			}
   226 		}
   227 	if (r!=KErrNone)
   228 		test.Printf(_L("Error %d occurred\n"),r);
   229 	delete pF;
   230 	}
   231 
   232 TInt E32Main()
   233 	{
   234 	test.Title();
   235 	test.Start(_L("Connect to F32"));
   236 	TInt r=Fs.Connect();
   237 	test(r==KErrNone);
   238 
   239 	r=ParseCommandLine();
   240 	if (r==KErrNone)
   241 		DoCommand();
   242 	else
   243 		Usage();
   244 
   245 	Fs.Close();
   246 	test.End();
   247 	return 0;
   248 	}