os/kernelhwsrv/kerneltest/e32utils/crashread/crashread.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2003-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
//
sl@0
    15
sl@0
    16
#include <e32std.h>
sl@0
    17
#include <e32std_private.h>
sl@0
    18
#include <f32file.h>
sl@0
    19
#include <d32locd.h>
sl@0
    20
#include <e32cons.h>
sl@0
    21
#include "crashflash.h"
sl@0
    22
#include <partitions.h>
sl@0
    23
#include <ftlcontrolio.h>
sl@0
    24
sl@0
    25
#ifdef _DEBUG
sl@0
    26
#define TRACE(a) RDebug::Print(a); PrintLine(a)
sl@0
    27
#define TRACE1(a,b) RDebug::Print(a,b); PrintLine(a,b)
sl@0
    28
#define TRACE2(a,b,c) RDebug::Print(a,b,c); PrintLine(a,b,c)
sl@0
    29
#define TRACE5(a,b,c,d,e,f) RDebug::Print(a,b,c,d,e,f); PrintLine(a,b,c,d,e,f)
sl@0
    30
#else
sl@0
    31
#define TRACE(a) 
sl@0
    32
#define TRACE1(a,b) 
sl@0
    33
#define TRACE2(a,b,c) 
sl@0
    34
#define TRACE5(a,b,c,d,e,f)
sl@0
    35
#endif
sl@0
    36
sl@0
    37
#ifndef _CRASHLOG_COMPR
sl@0
    38
_LIT(KCrashLogFileName, "?:\\crashlog.txt");
sl@0
    39
#else
sl@0
    40
_LIT(KCrashLogCompFileName, "?:\\crashlog.gz");
sl@0
    41
_LIT(KCrashLogCompTruncatedFileName, "?:\\crashlog_truncated.gz");
sl@0
    42
#endif //_CRASHLOG_COMPR
sl@0
    43
sl@0
    44
_LIT8(KCrashLogSignatureStomp, "\x00\x00\x00\x00");
sl@0
    45
sl@0
    46
CConsoleBase* console = 0;
sl@0
    47
sl@0
    48
RLocalDrive gLd;
sl@0
    49
TLocalDriveCapsV4 gCaps;
sl@0
    50
TPckg<TLocalDriveCapsV4> gCapsBuf(gCaps);
sl@0
    51
sl@0
    52
#ifdef _DEBUG
sl@0
    53
LOCAL_C void CheckConsoleCreated()
sl@0
    54
	{
sl@0
    55
	if(!console)
sl@0
    56
		{
sl@0
    57
		TRAPD(r, console = Console::NewL(_L("crashread"), 
sl@0
    58
			TSize(KConsFullScreen,KConsFullScreen)));
sl@0
    59
		__ASSERT_ALWAYS(r == KErrNone, User::Panic(_L("Could not create console"), 1));
sl@0
    60
		}
sl@0
    61
	}
sl@0
    62
sl@0
    63
LOCAL_C void PrintLine(TRefByValue<const TDesC> aFmt,...)
sl@0
    64
	{
sl@0
    65
    // Print to a console screen.
sl@0
    66
    VA_LIST list;
sl@0
    67
    VA_START(list, aFmt);
sl@0
    68
    TBuf<0x100> aBuf;
sl@0
    69
    aBuf.AppendFormatList(aFmt, list);
sl@0
    70
    CheckConsoleCreated();
sl@0
    71
    console->Write(aBuf);
sl@0
    72
	console->Write(_L("\n\r"));
sl@0
    73
	}
sl@0
    74
#endif
sl@0
    75
sl@0
    76
/** Read the signature from the flash and verify it is correct.
sl@0
    77
	@return ETrue when signature found, EFalse otherwise
sl@0
    78
*/
sl@0
    79
LOCAL_C TBool SignatureExistsL()
sl@0
    80
	{
sl@0
    81
	TBuf8<KCrashLogSignatureBytes> buf(0);
sl@0
    82
	User::LeaveIfError(gLd.Read(KCrashLogSizeFieldBytes,KCrashLogSignatureBytes,buf));
sl@0
    83
sl@0
    84
	if(buf.Compare(KCrashLogSignature) == 0)
sl@0
    85
		{
sl@0
    86
		return ETrue;
sl@0
    87
		}
sl@0
    88
sl@0
    89
	return EFalse;
sl@0
    90
	}
sl@0
    91
sl@0
    92
LOCAL_C TInt LogSizeL()
sl@0
    93
	{
sl@0
    94
	TBuf8<KCrashLogSizeFieldBytes> buf(0);
sl@0
    95
	User::LeaveIfError(gLd.Read(0,KCrashLogSizeFieldBytes,buf));
sl@0
    96
	TInt size = *((TUint*)(buf.Ptr()));
sl@0
    97
	size -= (KCrashLogHeaderSize);
sl@0
    98
	return size;
sl@0
    99
	}
sl@0
   100
sl@0
   101
#ifdef _CRASHLOG_COMPR	
sl@0
   102
/** Read the log flags from the flash.  Flags located after the log size and uncompressed size
sl@0
   103
	@return The log flags byte
sl@0
   104
*/
sl@0
   105
LOCAL_C TUint32 LogFlagsL()
sl@0
   106
	{
sl@0
   107
	TBuf8<KCrashLogFlagsFieldBytes> buf(0);
sl@0
   108
	User::LeaveIfError(gLd.Read(KCrashLogSizeFieldBytes+KCrashLogUncompSizeFieldBytes+KCrashLogSignatureBytes,
sl@0
   109
						KCrashLogFlagsFieldBytes,buf));
sl@0
   110
	return *((TUint32*)buf.Ptr());
sl@0
   111
	}
sl@0
   112
#endif //_CRASHLOG_COMPR
sl@0
   113
	
sl@0
   114
LOCAL_C TInt InvalidateSignature()
sl@0
   115
	{
sl@0
   116
	//On Nand we erase the block.
sl@0
   117
	if(gCaps.iType == EMediaNANDFlash)
sl@0
   118
		{
sl@0
   119
		return gLd.Format(0,gCaps.iNumBytesMain * gCaps.iNumPagesPerBlock);
sl@0
   120
		}
sl@0
   121
	//On Nor we just stomp on the first 4 bytes of the signature
sl@0
   122
	return gLd.Write(KCrashLogSizeFieldBytes,KCrashLogSignatureStomp);
sl@0
   123
	}
sl@0
   124
sl@0
   125
/**
sl@0
   126
@return KErrNone if no read errors, otherwise the last read error. 
sl@0
   127
@leave if other errors occur.	
sl@0
   128
@param aFileName Where the log wll be copied to
sl@0
   129
@param aStartPosition Where to begin reads within the flash section.
sl@0
   130
@param aLogSize The total amount to read.
sl@0
   131
*/
sl@0
   132
TInt CopyToFileL(const TDesC& aFileName, const TInt aStartPosition, const TInt aLogSize)
sl@0
   133
	{
sl@0
   134
	// Connect to f32 and write out the file
sl@0
   135
	RFs fs;
sl@0
   136
	RFile file;
sl@0
   137
	User::LeaveIfError(fs.Connect());
sl@0
   138
	CleanupClosePushL(fs);
sl@0
   139
	User::LeaveIfError(file.Replace(fs, aFileName, EFileWrite));
sl@0
   140
	CleanupClosePushL(file);
sl@0
   141
sl@0
   142
	//create buffer
sl@0
   143
	const TInt KBufferSize=32*1024;
sl@0
   144
	HBufC8* buf = HBufC8::NewLC(KBufferSize);
sl@0
   145
	TPtr8 ptr = buf->Des();
sl@0
   146
sl@0
   147
	TInt readError = KErrNone;
sl@0
   148
	for(TInt offset=0; offset<aLogSize; offset+=KBufferSize)
sl@0
   149
		{
sl@0
   150
		//don't read beyond end on final iteration.
sl@0
   151
		const TInt readLength = Min(KBufferSize, aLogSize-offset);
sl@0
   152
sl@0
   153
		ptr.SetLength(0);
sl@0
   154
		TInt r = gLd.Read(aStartPosition+offset,readLength,ptr);
sl@0
   155
sl@0
   156
		// in case of error store it, but attempt to continue.
sl@0
   157
		if (r!=KErrNone)
sl@0
   158
			{
sl@0
   159
			readError=r;
sl@0
   160
			}
sl@0
   161
sl@0
   162
		User::LeaveIfError(file.Write(offset, ptr));
sl@0
   163
		}
sl@0
   164
sl@0
   165
	User::LeaveIfError(file.Flush());
sl@0
   166
	CleanupStack::PopAndDestroy(buf);
sl@0
   167
	CleanupStack::PopAndDestroy(&file);
sl@0
   168
	CleanupStack::PopAndDestroy(&fs);
sl@0
   169
	return readError;
sl@0
   170
	}
sl@0
   171
sl@0
   172
sl@0
   173
LOCAL_C TInt MainL()
sl@0
   174
	{
sl@0
   175
	// check if command line argument is 'reset'
sl@0
   176
	RBuf cl;
sl@0
   177
	cl.CreateL(User::CommandLineLength());
sl@0
   178
	cl.CleanupClosePushL();
sl@0
   179
	User::CommandLine(cl);
sl@0
   180
	TBool reset = (cl==_L("reset"));
sl@0
   181
	CleanupStack::PopAndDestroy();
sl@0
   182
	
sl@0
   183
	TBool changed;
sl@0
   184
	TInt r = 0;
sl@0
   185
	TInt i=0;
sl@0
   186
	// 1) Find a crash log partition.
sl@0
   187
	for(; i<KMaxLocalDrives; i++)
sl@0
   188
		{
sl@0
   189
		r = gLd.Connect(i,changed);
sl@0
   190
		if(r == KErrNone)
sl@0
   191
			{
sl@0
   192
			r = gLd.Caps(gCapsBuf);
sl@0
   193
			if(r != KErrNone)
sl@0
   194
				{
sl@0
   195
				//TRACE1(_L("Could not retrieve gCaps for drive: %d.  Skipping to next..."),i);
sl@0
   196
				continue;
sl@0
   197
				}
sl@0
   198
			if(gCaps.iPartitionType == (TUint16)KPartitionTypeSymbianCrashLog)
sl@0
   199
				{
sl@0
   200
				TRACE1(_L("Found Symbian crash log partition on drive: %d"),i);
sl@0
   201
				CleanupClosePushL(gLd);
sl@0
   202
				// 1) See if there is an existing crash log
sl@0
   203
				TBool exists = SignatureExistsL();
sl@0
   204
				if(!exists)
sl@0
   205
					{
sl@0
   206
					TRACE(_L("Did not find an existing crash log signature on this crash log partition..."));
sl@0
   207
					//There may be a second crash log partition. (nor or nand
sl@0
   208
					//depending on ordering in variantmediadef.h).  So we continue searching
sl@0
   209
					CleanupStack::PopAndDestroy(&gLd);
sl@0
   210
					continue; 
sl@0
   211
					}
sl@0
   212
				TRACE1(_L("Found a crash log signature on drive: %d."),i);
sl@0
   213
				//We've found a crash log partition with a signature on it.
sl@0
   214
				break;
sl@0
   215
				}
sl@0
   216
			else
sl@0
   217
				{
sl@0
   218
				//TRACE2(_L("Partition type on drive: %d is %d"),i,gCaps.iPartitionType);
sl@0
   219
				}
sl@0
   220
			}
sl@0
   221
		}
sl@0
   222
	if(i == KMaxLocalDrives)
sl@0
   223
		{
sl@0
   224
		TRACE(_L("No crash log partition found with valid crash log signature found.  Exiting..."));
sl@0
   225
		User::Leave(KErrNotFound);
sl@0
   226
		}
sl@0
   227
sl@0
   228
	// If we're doing a reset, don't try to read the crash log, just skip to stomping the signature
sl@0
   229
	if(!reset)
sl@0
   230
		{
sl@0
   231
		TUint8 systemDriveChar = (TUint8) RFs::GetSystemDriveChar();
sl@0
   232
#ifndef _CRASHLOG_COMPR
sl@0
   233
		// Determine size of crash log and copy to file.
sl@0
   234
		TInt logSize = LogSizeL();
sl@0
   235
		TRACE1(_L("Reading crash log of %d bytes..."), logSize);
sl@0
   236
		TBuf<sizeof(KCrashLogFileName)> crashLogFileName(KCrashLogFileName);
sl@0
   237
		crashLogFileName[0] = systemDriveChar;
sl@0
   238
		r = CopyToFileL(crashLogFileName, KCrashLogSizeFieldBytes+KCrashLogSignatureBytes, logSize);
sl@0
   239
sl@0
   240
		if (r==KErrNone)
sl@0
   241
			{
sl@0
   242
			TRACE1(_L("Crash log successfully written to: %S."), &crashLogFileName);
sl@0
   243
			}
sl@0
   244
		else
sl@0
   245
			{
sl@0
   246
			TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogFileName);
sl@0
   247
			}
sl@0
   248
sl@0
   249
#else
sl@0
   250
		// 2) 	Read crash log header to get the compressed and uncompressed size of the log
sl@0
   251
		//		also need to read the flags to determine if the log had to be truncated and
sl@0
   252
		//		if the expected log format is found
sl@0
   253
		const TUint32 logFlags = LogFlagsL();
sl@0
   254
		
sl@0
   255
		// Extract byte offset from the end of the header to the start of the log data
sl@0
   256
		const TInt logOff = logFlags>>KCrashLogFlagOffShift;
sl@0
   257
		
sl@0
   258
		// Work out if the log had to be truncated
sl@0
   259
		const TInt truncated = logFlags&KCrashLogFlagTruncated;			
sl@0
   260
		
sl@0
   261
		// Check the crashlog type flag is that expected - here we can only cope with GZIP compatible logs
sl@0
   262
		if ((logFlags & (0xffffffff>>(32-KCrashLogFlagTypeBits))) != KCrashLogFlagGzip)
sl@0
   263
			{// wrong log type so can't extract it
sl@0
   264
			TRACE(_L("Crash Log data is stored in an incompatible data format so can't be read"));
sl@0
   265
			}
sl@0
   266
		else
sl@0
   267
			{
sl@0
   268
			// 2) Read the log data
sl@0
   269
			const TInt logSize = LogSizeL()-logOff; // don't include any offset bytes	
sl@0
   270
			TRACE1(_L("Reading compressed crash log of %d bytes..."), logSize);
sl@0
   271
sl@0
   272
			
sl@0
   273
			TRACE1(_L("Writing compressed crash log to file..."), logSize);
sl@0
   274
			RBuf crashLogCompFileName;
sl@0
   275
			if (!truncated)
sl@0
   276
				{
sl@0
   277
				crashLogCompFileName.CreateL(KCrashLogCompFileName);
sl@0
   278
				}
sl@0
   279
			else
sl@0
   280
				{
sl@0
   281
				crashLogCompFileName.CreateL(KCrashLogCompTruncatedFileName);
sl@0
   282
				}
sl@0
   283
			crashLogCompFileName.CleanupClosePushL();
sl@0
   284
sl@0
   285
			crashLogCompFileName[0] = systemDriveChar;
sl@0
   286
			r = CopyToFileL(crashLogCompFileName, KCrashLogHeaderSize+logOff, logSize);
sl@0
   287
				
sl@0
   288
			if (r==KErrNone)
sl@0
   289
				{
sl@0
   290
				if (!truncated)
sl@0
   291
					{
sl@0
   292
					TRACE1(_L("Crash log successfully written to: %S."), &crashLogCompFileName);
sl@0
   293
					}
sl@0
   294
				else
sl@0
   295
					{
sl@0
   296
					TRACE(_L("Crash log was truncated, some log data has been lost"));
sl@0
   297
					TRACE1(_L("Crash log successfully written to: %S."), &crashLogCompFileName);
sl@0
   298
					}						
sl@0
   299
				}
sl@0
   300
			else
sl@0
   301
				{
sl@0
   302
				if(!truncated)
sl@0
   303
					{
sl@0
   304
					TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogCompFileName);
sl@0
   305
					}
sl@0
   306
				else
sl@0
   307
					{
sl@0
   308
					TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogCompFileName);
sl@0
   309
					}
sl@0
   310
				}
sl@0
   311
			CleanupStack::PopAndDestroy(&crashLogCompFileName);
sl@0
   312
			}
sl@0
   313
#endif //_CRASHLOG_COMPR			
sl@0
   314
		}
sl@0
   315
sl@0
   316
	// 5) Stomp on the signature to mark it eligible to be overwritten
sl@0
   317
	TRACE(_L("Overwriting existing signature to indicate crash log has been read..."));
sl@0
   318
	User::LeaveIfError(InvalidateSignature());
sl@0
   319
sl@0
   320
	CleanupStack::PopAndDestroy(&gLd);
sl@0
   321
sl@0
   322
	if (r==KErrNone)
sl@0
   323
		{
sl@0
   324
		TRACE(_L("Crash reader finished successfully."));
sl@0
   325
		}
sl@0
   326
	else
sl@0
   327
		{
sl@0
   328
		TRACE(_L("Crash reader finished but with errors."));
sl@0
   329
		}
sl@0
   330
	return KErrNone;
sl@0
   331
	}
sl@0
   332
sl@0
   333
GLDEF_C TInt E32Main()
sl@0
   334
	{
sl@0
   335
	__UHEAP_MARK;
sl@0
   336
	CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
   337
	TRAPD(ret, MainL());
sl@0
   338
	if(console)
sl@0
   339
		{
sl@0
   340
		console->Getch();
sl@0
   341
		delete console;
sl@0
   342
		}
sl@0
   343
	if (ret){} // stops compile warning
sl@0
   344
	delete cleanup;
sl@0
   345
	__UHEAP_MARKEND;
sl@0
   346
	return KErrNone;
sl@0
   347
	}