sysstatemgmt/systemstarter/src/resourcefilereader2.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2005-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 "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 //
       
    15 
       
    16 #include <barsread.h>
       
    17 #include <e32uid.h>
       
    18 #include <e32modes.h>
       
    19 #include <startup.hrh>
       
    20 #include <hal.h>
       
    21 #include <hal_data.h>
       
    22 #include <startupproperties.h>
       
    23 #include "resourcefilereader2.h"
       
    24 #include "SystemStartupStateInfo.h"
       
    25 #include "StartupCommand.h"
       
    26 #include "startuputilprovider.h"
       
    27 #include "BootModeMapping.h"
       
    28 #include "appstarter2.h"
       
    29 #include "DllStarter.h"
       
    30 #include "StartupSplashScreen.h"
       
    31 #include "initapparcserver2.h"
       
    32 #include "SystemStartupDllInfo.h"
       
    33 #include "multiplewait2.h"
       
    34 #include "amastarter.h"
       
    35 
       
    36 #include "SysStartDebug.h"
       
    37 #include "sysstartpanic.h"
       
    38 
       
    39 const TUint KEntryPointIndex = 1;
       
    40 
       
    41 /** Read splash screen information from the resource file and return an object
       
    42 that can start/kill it
       
    43 */
       
    44 MStartupCommand* CResourceFileReader::ReadSplashScreenL(TResourceReader& aReader)
       
    45 	{
       
    46 	TBool kill = static_cast<TUint16>(aReader.ReadUint16());
       
    47 	HBufC* path = aReader.ReadTPtrC().AllocLC();
       
    48 	CStartupSplashScreen* command = CStartupSplashScreen::NewL(!kill, path);
       
    49 	CleanupStack::Pop(path);	// "command" takes ownership of path
       
    50 	return command;
       
    51 	}
       
    52 		
       
    53 /** Read app arc server initialisation information from the resource file
       
    54 and return an object that can start it
       
    55 */
       
    56 MStartupCommand* CResourceFileReader::ReadAppArcInitL()
       
    57 	{
       
    58 	return CInitAppArcServer::NewL(*iStartupUtilProvider);
       
    59 	}
       
    60 
       
    61 /** Read in the fields of the MULTIPLE_WAIT command in the SSC. 
       
    62 Create a CMultipleWait object containing the list of deferred commands encountered
       
    63 since the last MULTIPLE_WAIT command.   
       
    64 */ 
       
    65 MStartupCommand* CResourceFileReader::ReadMultipleWaitInfoL(TResourceReader& aReader, CommandList& aDeferredList)
       
    66 	{
       
    67 	// Read values from the SSC
       
    68 	TInt timeout = aReader.ReadInt32();
       
    69 	TInt fail = static_cast<TUint16>(aReader.ReadUint16());
       
    70 	
       
    71 	// Create a CMultipleWait object to hold the data to be used during 
       
    72 	// the command execution.
       
    73 	CMultipleWait* waitCommand = CMultipleWait::NewL(aDeferredList, *iStartupUtilProvider);
       
    74 	
       
    75 	// Populate the command with the values just read from the SSC.
       
    76 	waitCommand->SetTimeout(timeout);
       
    77 	
       
    78 	// Check that the fail_on_error value is valid.	
       
    79 	if ((fail != EIgnoreCommandFailure) && (fail != EPanicOnCommandFailure))   
       
    80 		{	
       
    81 		DEBUGPRINT2(_L("SysStart: Invalid fail_on_error value of %d"), fail);
       
    82 		PanicNow(KPanicResourceFileReader, EInvalidMultipleWaitFailOnError);
       
    83 		} 
       
    84 	waitCommand->SetFailOnError(fail);
       
    85 	 	
       
    86  	return waitCommand; 
       
    87 	}
       
    88 	
       
    89 /**
       
    90 */
       
    91 MStartupCommand* CResourceFileReader::ReadAmaInfoL(TResourceReader& aReader)
       
    92 	{
       
    93 	TUid id = TUid::Uid(aReader.ReadUint32());
       
    94 
       
    95 	CAmaStarter *command = CAmaStarter::NewL(id);
       
    96 	
       
    97 	return command;	
       
    98 	}
       
    99 	
       
   100 /** Read DLL information from the resource file and store in an object to be 
       
   101 used during dll function invocation
       
   102 */
       
   103 MStartupCommand* CResourceFileReader::ReadDllInfoL(TResourceReader& aReader)
       
   104 { 
       
   105 	// Read the main START_DLL_INFO fields from the SSC (static start-up 
       
   106 	// configuration) file.
       
   107 	TPtrC dllName = aReader.ReadTPtrC();
       
   108 	TUint8 ordinal = aReader.ReadUint8();
       
   109  	TInt fail = static_cast<TBool>(aReader.ReadInt16());
       
   110 	TUint8 retries = static_cast<TUint8>(aReader.ReadUint8());
       
   111 
       
   112 	// Additional validation
       
   113 	if ((fail != EIgnoreCommandFailure) && (fail != EPanicOnCommandFailure))   
       
   114 		{	
       
   115 		DEBUGPRINT2(_L("SysStart: Invalid fail_on_error value of %d"), fail);
       
   116 		PanicNow(KPanicResourceFileReader, EInvalidDLLFailOnError);
       
   117 		}  
       
   118  
       
   119  	if (ordinal == 0)
       
   120  		{
       
   121  		DEBUGPRINT1(_L("Invalid ordinal value of 0"));
       
   122 		PanicNow(KPanicResourceFileReader, EInvalidOrdinal0);
       
   123  		}
       
   124  		
       
   125 	// Store the information to be used later during function invocation
       
   126 	CSystemStartupDllInfo* dllInfo = CSystemStartupDllInfo::NewLC();	
       
   127 	dllInfo->SetDllNameL(dllName); 
       
   128 	dllInfo->SetOrdinal( ordinal);  
       
   129 	dllInfo->SetFailOnError(fail);
       
   130  	dllInfo->SetNoOfRetries(retries); 
       
   131   
       
   132   	// Read the DLL custom data link from the START_DLL_INFO struct
       
   133  	TUint32 dllDataLink = aReader.ReadUint32();
       
   134  
       
   135  	// Validate data link value
       
   136  	if (dllDataLink == 0)
       
   137  		{
       
   138  		DEBUGPRINT1(_L("Invalid custom_dll_data_link value of 0"));
       
   139 		PanicNow(KPanicResourceFileReader, EInvalidDataLink0);
       
   140  		}
       
   141  		
       
   142  	// Store the data found under the dllDataLink structure into a buffer
       
   143  	// for custom processing by licensee (See CDllStarter::Execute)
       
   144   	dllInfo->SetDllBuffer(iResourceFile.AllocReadLC(dllDataLink));
       
   145   		
       
   146   	// The above read a resource into a heap descriptor and put a pointer 
       
   147   	// to that descriptor onto the cleanup stack. Therefore this must now be 
       
   148   	// popped off the stack.
       
   149 	CleanupStack::Pop();
       
   150 	
       
   151 	// Create a command using the DLL info retrieved.
       
   152 	CDllStarter* command = CDllStarter::NewL(dllInfo);
       
   153 		
       
   154 	// Pop dllInfo of the stack
       
   155 	CleanupStack::Pop(dllInfo);
       
   156 
       
   157 	return command;
       
   158 	}
       
   159  
       
   160 //
       
   161 // Standard Symbian factory functions/destructor
       
   162 //
       
   163 
       
   164 CResourceFileReader* CResourceFileReader::NewL(TInt aBootMode, RFs& aFs)
       
   165 	{
       
   166 	CResourceFileReader* self = CResourceFileReader::NewLC(aBootMode, aFs);
       
   167 	CleanupStack::Pop(self);
       
   168 	return self;
       
   169 	}
       
   170 
       
   171 CResourceFileReader* CResourceFileReader::NewLC(TInt aBootMode , RFs& aFs)
       
   172 	{
       
   173 	TFileName resourceFile;
       
   174 	TBootModeMapping mapping;
       
   175 	
       
   176 	mapping.GetResourceFileName(aBootMode, resourceFile, aFs);
       
   177 	
       
   178 	DEBUGPRINT2(_L("SysStart: using resource file %S"), &resourceFile);
       
   179 
       
   180 	CResourceFileReader* self = new (ELeave) CResourceFileReader(aBootMode, aFs);
       
   181 	CleanupStack::PushL(self);
       
   182 	self->ConstructL(resourceFile);
       
   183 	return self;
       
   184 	}
       
   185 
       
   186 CResourceFileReader::~CResourceFileReader()
       
   187 	{
       
   188 	iResourceFile.Close();
       
   189 	delete iStartupUtilProvider;	
       
   190 	}
       
   191 
       
   192 //
       
   193 // Public functions.
       
   194 //
       
   195 
       
   196 /** Gets information about the next start-up state. This involves reading the state information
       
   197 and constructing the command list for this state.
       
   198 */
       
   199 MStartupStateInfo* CResourceFileReader::GetStateInfoL()
       
   200 	{
       
   201 	return ((iState < NULL) ? NULL : ReadStateInformationL());
       
   202 	}
       
   203 
       
   204 
       
   205 //
       
   206 // Private functions
       
   207 //
       
   208 
       
   209 CResourceFileReader::CResourceFileReader(TInt aBootMode, RFs& aFs)
       
   210 	: iFs(aFs), iBootMode(aBootMode)
       
   211 	{
       
   212 	iMaxStartupMode = -1;
       
   213 	}
       
   214 
       
   215 void CResourceFileReader::ConstructL(const TDesC& aResourceFile)
       
   216 	{
       
   217     iResourceFile.OpenL(iFs, aResourceFile);
       
   218     iStartupUtilProvider = CStartupUtilProvider::NewL();    
       
   219 	}
       
   220  
       
   221 void CResourceFileReader::FindFirstStateEntryL()
       
   222 	{
       
   223 	HBufC8* dataBuffer = iResourceFile.AllocReadLC(KEntryPointIndex);
       
   224 	TResourceReader reader;
       
   225 	reader.SetBuffer(dataBuffer);
       
   226 	iState = reader.ReadInt32();
       
   227 	CleanupStack::PopAndDestroy(dataBuffer);
       
   228 	}
       
   229 
       
   230 /** Read state information from the resource file
       
   231 */
       
   232 CSystemStartupStateInfo* CResourceFileReader::ReadStateInformationL()
       
   233 	{
       
   234 	if (iState == 0)
       
   235 		{
       
   236 		FindFirstStateEntryL();
       
   237 		}
       
   238 		
       
   239 	CSystemStartupStateInfo* stateInfo = CSystemStartupStateInfo::NewLC();
       
   240 
       
   241 	HBufC8* dataBuffer = iResourceFile.AllocReadLC(iState);
       
   242 	TResourceReader reader;
       
   243 	reader.SetBuffer(dataBuffer);
       
   244 
       
   245 	TUint8 id = reader.ReadUint8();
       
   246 	
       
   247 	// Validate state id field. State id can be any int except for the 
       
   248 	// currently reserved values.
       
   249 	if ((id == EReservedStartUpState1) || (id == EReservedStartUpState2) ||
       
   250 	    (id == EReservedStartUpState4) || (id == EReservedStartUpState5)) 
       
   251 		{
       
   252  		DEBUGPRINT2(_L("Invalid state id of %d"), id);
       
   253 		PanicNow(KPanicResourceFileReader, EInvalidStateId);
       
   254 		}  	
       
   255 
       
   256 	// Store the state id value.
       
   257 	stateInfo->SetStateId(id);
       
   258 
       
   259 	TPtrC stateName = reader.ReadTPtrC();
       
   260 	stateInfo->SetName(stateName);
       
   261 
       
   262 	TUint32 commandListId = reader.ReadUint32();
       
   263 	
       
   264     iState = reader.ReadInt32();	// Link to next state.
       
   265  
       
   266  	// The retries field specifies how many times to re-attempt 
       
   267  	// state transition in the case of failure
       
   268 	TNoOfRetries noOfRetries = static_cast<TNoOfRetries>(reader.ReadUint16()); 
       
   269 
       
   270 	// Validate field and store
       
   271 	if ((noOfRetries != ERetry0) && (noOfRetries != ERetry1))
       
   272 		{
       
   273  		DEBUGPRINT2(_L("Invalid no_of_retries_on_failure value of %d"), noOfRetries);
       
   274 		PanicNow(KPanicResourceFileReader, EInvalidNoOfRetries);
       
   275 		}  
       
   276 	stateInfo->SetNoOfRetries(noOfRetries);
       
   277 	 
       
   278 	// The next field specifies what should be done on a state transition 
       
   279 	// failure - ignore or panic  
       
   280 	TActionOnStateTransitionFailure actionOnStateTransitionFailure = 
       
   281 	        static_cast<TActionOnStateTransitionFailure>(reader.ReadUint16());  
       
   282 
       
   283 	// Validate field and store.
       
   284 	if ((actionOnStateTransitionFailure != EIgnoreFailure) && (actionOnStateTransitionFailure != EPanicOnFailure))
       
   285 		{
       
   286  		DEBUGPRINT2(_L("Invalid action_on_failure value of %d"), actionOnStateTransitionFailure);
       
   287 		PanicNow(KPanicResourceFileReader, EInvalidActionOnFailure);
       
   288 		}  
       
   289 	stateInfo->SetActionOnStateTransitionFailure(actionOnStateTransitionFailure);
       
   290     
       
   291     CleanupStack::PopAndDestroy(dataBuffer);
       
   292 
       
   293     if (iState == 0)
       
   294     	{
       
   295     	// Just read the last state info - make sure that we don't try to
       
   296     	// read any more.
       
   297     	iState = -1;
       
   298     	}
       
   299      
       
   300  	CommandList commandList = ReadCommandListL(commandListId);
       
   301 	stateInfo->SetCommandList(commandList);
       
   302 	CleanupStack::Pop(stateInfo);
       
   303 
       
   304     return stateInfo;
       
   305 	}
       
   306 	
       
   307 /** Read the command list from the resource file
       
   308 */
       
   309 CommandList CResourceFileReader::ReadCommandListL(TUint32 aCommandListId)
       
   310 	{
       
   311 	CommandList commandList; 
       
   312 	
       
   313 	// Some of the commands will be deferred apps or processes (i.e. it is not 
       
   314 	// checked that they have completed until a MULTIPLE_WAIT command is issued). 
       
   315 	// Create a list of these commands so they can be referred to from a 
       
   316 	// CMultipleWait object.
       
   317 	CommandList deferredList;
       
   318 		
       
   319 	HBufC8* dataBuffer = iResourceFile.AllocReadLC(aCommandListId);
       
   320 	TResourceReader reader;
       
   321 	reader.SetBuffer(dataBuffer);
       
   322 
       
   323 	TUint16 commandListCount = reader.ReadUint16();
       
   324 
       
   325 	for(TUint i = 0; i < commandListCount; ++i)
       
   326 		{
       
   327 		const TStartupCommandType type = static_cast<TStartupCommandType>(reader.ReadUint16());	
       
   328 		MStartupCommand* command = NULL;
       
   329 		switch (type)
       
   330 			{
       
   331 		case EStartupApp:					
       
   332 		case EStartupProcess:
       
   333 		case EStartupApp2:					
       
   334 		case EStartupProcess2:			
       
   335 			{
       
   336 			reader.Rewind(sizeof(TUint16)); //CStartupProperties need to read the struct from start
       
   337 			CStartupProperties* appInfo = CStartupProperties::NewLC(reader);
       
   338 			if (appInfo->RecoveryMethod() == ERestartOSWithMode)
       
   339 				{
       
   340 				ValidateRestartMode(appInfo->RestartMode());
       
   341 				}
       
   342 			command = CAppStarter::NewL(appInfo, *iStartupUtilProvider);
       
   343 			CleanupStack::Pop(appInfo); // ownership passed above
       
   344 			CleanupStack::PushL(command);
       
   345 			 
       
   346 			// If the command has a start_method of EDeferredWaitForStart
       
   347 			// in the SSC then it needs to be added to the current deferred
       
   348 			// list. This will be passed to a CMultipleWait object once a 
       
   349 			// MULTIPLE_WAIT command is encountered.
       
   350 			if (command && (appInfo->StartMethod() == EDeferredWaitForStart))
       
   351 				{  
       
   352 				User::LeaveIfError(deferredList.Append(command)); 				
       
   353 				}
       
   354 			break;
       
   355 			}
       
   356  		
       
   357 		case EStartupSplashScreen:
       
   358 			command = ReadSplashScreenL(reader);
       
   359 			CleanupStack::PushL(command);
       
   360 			break;
       
   361 
       
   362 		case EInitAppArcServer:
       
   363 			command = ReadAppArcInitL();
       
   364 			CleanupStack::PushL(command);
       
   365 			break;
       
   366 
       
   367 		case EStartupDLL:
       
   368 			command = ReadDllInfoL(reader);
       
   369 			CleanupStack::PushL(command);
       
   370 			break;
       
   371 			
       
   372 		case EMultipleWait:
       
   373 		
       
   374 			// A MULTIPLE_WAIT command should only be provisioned in the SSC
       
   375 			// if there are preceeding deferred commands.
       
   376 			if (deferredList.Count() == 0)
       
   377 				{		
       
   378 		 		DEBUGPRINT1(_L("SysStart: Resource File Reader extra multiple wait command in SSC"));
       
   379 				PanicNow(KPanicResourceFileReader, EExtraMultipleWaitCommand);
       
   380 				}
       
   381 			// A list has been built up of any processes/apps with a start_method of
       
   382 			// EDeferredWaitForStart. Pass this list as parameter so that a 
       
   383 			// CMultipleWait object can be created which has access to this list.			  
       
   384 			command = ReadMultipleWaitInfoL(reader, deferredList);
       
   385 			CleanupStack::PushL(command);
       
   386 			
       
   387 			// As processing continues more deferred command may be encountered
       
   388 			// along with a subsequent MULTIPLE_WAIT struct. Reset the deferred list
       
   389 			// so that the next set of deferred commands can be stored.
       
   390 			deferredList.Reset();   
       
   391 			break;
       
   392 			
       
   393 		case EStartupAMAStarter:
       
   394 			command = ReadAmaInfoL(reader);
       
   395 			CleanupStack::PushL(command);
       
   396 			break;
       
   397 		
       
   398 		default:
       
   399 	 		DEBUGPRINT2(_L("SysStart: Resource File Reader unknown command type %d"), type);
       
   400 			PanicNow(KPanicResourceFileReader, EInvalidCommandType);
       
   401 			break;
       
   402 			}
       
   403 			
       
   404 		if (command)
       
   405 			{
       
   406 			User::LeaveIfError(commandList.Append(command));
       
   407 			CleanupStack::Pop(command);
       
   408 			}
       
   409 		}
       
   410 	
       
   411 	// Any deferred commands must be followed by a MULTIPLE_WAIT command within
       
   412 	// the same state. Therefore at this stage all commands in the state have been 
       
   413 	// processed so the deferred list should be empty.
       
   414 	
       
   415 	if (deferredList.Count() != 0)
       
   416 		{		
       
   417 		PanicNow(KPanicResourceFileReader, EMissingMultipleWaitCommand);
       
   418 		}
       
   419 
       
   420 	deferredList.Reset();	
       
   421     CleanupStack::PopAndDestroy(dataBuffer);
       
   422 	return commandList;
       
   423 	}
       
   424 
       
   425 void CResourceFileReader::ValidateRestartMode(TInt aStartupMode)
       
   426 	{
       
   427 #ifndef __WINSCW__ // HAL::Get(EMaximumRestartStartupModes...) is only supported in H4	
       
   428 	// check that the startup mode is valid, first retrieve max startup mode if not retrieved yet
       
   429 	if (iMaxStartupMode == -1)
       
   430 		{
       
   431 		TInt err = HAL::Get(HALData::EMaximumRestartStartupModes, iMaxStartupMode);
       
   432 		if (err != KErrNone)
       
   433 			{
       
   434 	 		DEBUGPRINT2(_L("SysStart: Failed to get max startup mode err = %d"), err);
       
   435 			PanicNow(KPanicResourceFileReader, EMissingMaxStartupMode);
       
   436 			}
       
   437 		DEBUGPRINT2(_L("SysStart: HAL max startup mode = %d"), iMaxStartupMode);
       
   438 		}
       
   439 
       
   440 	if (aStartupMode > iMaxStartupMode)
       
   441 		{
       
   442  		DEBUGPRINT2(_L("SysStart: Invalid failure_recovery_startup_mode %d"), aStartupMode);
       
   443 		PanicNow(KPanicResourceFileReader, EInvalidStartupMode);
       
   444 		}
       
   445 #else
       
   446 	(void)aStartupMode;
       
   447 #endif
       
   448 	}