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