Update contrib.
1 // Copyright (c) 2003-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #include <e32std_private.h>
21 #include "crashflash.h"
22 #include <partitions.h>
23 #include <ftlcontrolio.h>
26 #define TRACE(a) RDebug::Print(a); PrintLine(a)
27 #define TRACE1(a,b) RDebug::Print(a,b); PrintLine(a,b)
28 #define TRACE2(a,b,c) RDebug::Print(a,b,c); PrintLine(a,b,c)
29 #define TRACE5(a,b,c,d,e,f) RDebug::Print(a,b,c,d,e,f); PrintLine(a,b,c,d,e,f)
34 #define TRACE5(a,b,c,d,e,f)
37 #ifndef _CRASHLOG_COMPR
38 _LIT(KCrashLogFileName, "?:\\crashlog.txt");
40 _LIT(KCrashLogCompFileName, "?:\\crashlog.gz");
41 _LIT(KCrashLogCompTruncatedFileName, "?:\\crashlog_truncated.gz");
42 #endif //_CRASHLOG_COMPR
44 _LIT8(KCrashLogSignatureStomp, "\x00\x00\x00\x00");
46 CConsoleBase* console = 0;
49 TLocalDriveCapsV4 gCaps;
50 TPckg<TLocalDriveCapsV4> gCapsBuf(gCaps);
53 LOCAL_C void CheckConsoleCreated()
57 TRAPD(r, console = Console::NewL(_L("crashread"),
58 TSize(KConsFullScreen,KConsFullScreen)));
59 __ASSERT_ALWAYS(r == KErrNone, User::Panic(_L("Could not create console"), 1));
63 LOCAL_C void PrintLine(TRefByValue<const TDesC> aFmt,...)
65 // Print to a console screen.
69 aBuf.AppendFormatList(aFmt, list);
70 CheckConsoleCreated();
72 console->Write(_L("\n\r"));
76 /** Read the signature from the flash and verify it is correct.
77 @return ETrue when signature found, EFalse otherwise
79 LOCAL_C TBool SignatureExistsL()
81 TBuf8<KCrashLogSignatureBytes> buf(0);
82 User::LeaveIfError(gLd.Read(KCrashLogSizeFieldBytes,KCrashLogSignatureBytes,buf));
84 if(buf.Compare(KCrashLogSignature) == 0)
92 LOCAL_C TInt LogSizeL()
94 TBuf8<KCrashLogSizeFieldBytes> buf(0);
95 User::LeaveIfError(gLd.Read(0,KCrashLogSizeFieldBytes,buf));
96 TInt size = *((TUint*)(buf.Ptr()));
97 size -= (KCrashLogHeaderSize);
101 #ifdef _CRASHLOG_COMPR
102 /** Read the log flags from the flash. Flags located after the log size and uncompressed size
103 @return The log flags byte
105 LOCAL_C TUint32 LogFlagsL()
107 TBuf8<KCrashLogFlagsFieldBytes> buf(0);
108 User::LeaveIfError(gLd.Read(KCrashLogSizeFieldBytes+KCrashLogUncompSizeFieldBytes+KCrashLogSignatureBytes,
109 KCrashLogFlagsFieldBytes,buf));
110 return *((TUint32*)buf.Ptr());
112 #endif //_CRASHLOG_COMPR
114 LOCAL_C TInt InvalidateSignature()
116 //On Nand we erase the block.
117 if(gCaps.iType == EMediaNANDFlash)
119 return gLd.Format(0,gCaps.iNumBytesMain * gCaps.iNumPagesPerBlock);
121 //On Nor we just stomp on the first 4 bytes of the signature
122 return gLd.Write(KCrashLogSizeFieldBytes,KCrashLogSignatureStomp);
126 @return KErrNone if no read errors, otherwise the last read error.
127 @leave if other errors occur.
128 @param aFileName Where the log wll be copied to
129 @param aStartPosition Where to begin reads within the flash section.
130 @param aLogSize The total amount to read.
132 TInt CopyToFileL(const TDesC& aFileName, const TInt aStartPosition, const TInt aLogSize)
134 // Connect to f32 and write out the file
137 User::LeaveIfError(fs.Connect());
138 CleanupClosePushL(fs);
139 User::LeaveIfError(file.Replace(fs, aFileName, EFileWrite));
140 CleanupClosePushL(file);
143 const TInt KBufferSize=32*1024;
144 HBufC8* buf = HBufC8::NewLC(KBufferSize);
145 TPtr8 ptr = buf->Des();
147 TInt readError = KErrNone;
148 for(TInt offset=0; offset<aLogSize; offset+=KBufferSize)
150 //don't read beyond end on final iteration.
151 const TInt readLength = Min(KBufferSize, aLogSize-offset);
154 TInt r = gLd.Read(aStartPosition+offset,readLength,ptr);
156 // in case of error store it, but attempt to continue.
162 User::LeaveIfError(file.Write(offset, ptr));
165 User::LeaveIfError(file.Flush());
166 CleanupStack::PopAndDestroy(buf);
167 CleanupStack::PopAndDestroy(&file);
168 CleanupStack::PopAndDestroy(&fs);
175 // check if command line argument is 'reset'
177 cl.CreateL(User::CommandLineLength());
178 cl.CleanupClosePushL();
179 User::CommandLine(cl);
180 TBool reset = (cl==_L("reset"));
181 CleanupStack::PopAndDestroy();
186 // 1) Find a crash log partition.
187 for(; i<KMaxLocalDrives; i++)
189 r = gLd.Connect(i,changed);
192 r = gLd.Caps(gCapsBuf);
195 //TRACE1(_L("Could not retrieve gCaps for drive: %d. Skipping to next..."),i);
198 if(gCaps.iPartitionType == (TUint16)KPartitionTypeSymbianCrashLog)
200 TRACE1(_L("Found Symbian crash log partition on drive: %d"),i);
201 CleanupClosePushL(gLd);
202 // 1) See if there is an existing crash log
203 TBool exists = SignatureExistsL();
206 TRACE(_L("Did not find an existing crash log signature on this crash log partition..."));
207 //There may be a second crash log partition. (nor or nand
208 //depending on ordering in variantmediadef.h). So we continue searching
209 CleanupStack::PopAndDestroy(&gLd);
212 TRACE1(_L("Found a crash log signature on drive: %d."),i);
213 //We've found a crash log partition with a signature on it.
218 //TRACE2(_L("Partition type on drive: %d is %d"),i,gCaps.iPartitionType);
222 if(i == KMaxLocalDrives)
224 TRACE(_L("No crash log partition found with valid crash log signature found. Exiting..."));
225 User::Leave(KErrNotFound);
228 // If we're doing a reset, don't try to read the crash log, just skip to stomping the signature
231 TUint8 systemDriveChar = (TUint8) RFs::GetSystemDriveChar();
232 #ifndef _CRASHLOG_COMPR
233 // Determine size of crash log and copy to file.
234 TInt logSize = LogSizeL();
235 TRACE1(_L("Reading crash log of %d bytes..."), logSize);
236 TBuf<sizeof(KCrashLogFileName)> crashLogFileName(KCrashLogFileName);
237 crashLogFileName[0] = systemDriveChar;
238 r = CopyToFileL(crashLogFileName, KCrashLogSizeFieldBytes+KCrashLogSignatureBytes, logSize);
242 TRACE1(_L("Crash log successfully written to: %S."), &crashLogFileName);
246 TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogFileName);
250 // 2) Read crash log header to get the compressed and uncompressed size of the log
251 // also need to read the flags to determine if the log had to be truncated and
252 // if the expected log format is found
253 const TUint32 logFlags = LogFlagsL();
255 // Extract byte offset from the end of the header to the start of the log data
256 const TInt logOff = logFlags>>KCrashLogFlagOffShift;
258 // Work out if the log had to be truncated
259 const TInt truncated = logFlags&KCrashLogFlagTruncated;
261 // Check the crashlog type flag is that expected - here we can only cope with GZIP compatible logs
262 if ((logFlags & (0xffffffff>>(32-KCrashLogFlagTypeBits))) != KCrashLogFlagGzip)
263 {// wrong log type so can't extract it
264 TRACE(_L("Crash Log data is stored in an incompatible data format so can't be read"));
268 // 2) Read the log data
269 const TInt logSize = LogSizeL()-logOff; // don't include any offset bytes
270 TRACE1(_L("Reading compressed crash log of %d bytes..."), logSize);
273 TRACE1(_L("Writing compressed crash log to file..."), logSize);
274 RBuf crashLogCompFileName;
277 crashLogCompFileName.CreateL(KCrashLogCompFileName);
281 crashLogCompFileName.CreateL(KCrashLogCompTruncatedFileName);
283 crashLogCompFileName.CleanupClosePushL();
285 crashLogCompFileName[0] = systemDriveChar;
286 r = CopyToFileL(crashLogCompFileName, KCrashLogHeaderSize+logOff, logSize);
292 TRACE1(_L("Crash log successfully written to: %S."), &crashLogCompFileName);
296 TRACE(_L("Crash log was truncated, some log data has been lost"));
297 TRACE1(_L("Crash log successfully written to: %S."), &crashLogCompFileName);
304 TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogCompFileName);
308 TRACE1(_L("Crash log written to %S but errors were encountered when reading, it may be incomplete or corrupt."), &crashLogCompFileName);
311 CleanupStack::PopAndDestroy(&crashLogCompFileName);
313 #endif //_CRASHLOG_COMPR
316 // 5) Stomp on the signature to mark it eligible to be overwritten
317 TRACE(_L("Overwriting existing signature to indicate crash log has been read..."));
318 User::LeaveIfError(InvalidateSignature());
320 CleanupStack::PopAndDestroy(&gLd);
324 TRACE(_L("Crash reader finished successfully."));
328 TRACE(_L("Crash reader finished but with errors."));
333 GLDEF_C TInt E32Main()
336 CTrapCleanup* cleanup=CTrapCleanup::New();
343 if (ret){} // stops compile warning