os/kernelhwsrv/kerneltest/e32test/power/halsettings.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/power/halsettings.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,282 @@
     1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "hal.h"
    1.20 +#include <f32file.h>
    1.21 +
    1.22 +/** HAL attributes data folder */
    1.23 +_LIT(KHalFilePath,"_:\\private\\102825B1\\");
    1.24 +/** HAL attributes data file name */
    1.25 +_LIT(KHalFileName,"HAL.DAT");
    1.26 +/** Buffer descriptor for holding complete HAL data file path and name. */
    1.27 +typedef TBuf<28> THalFileName; 
    1.28 +
    1.29 +/**First 4 bytes in the HAL.DAT ('h' 'a' 'l' and version '0')
    1.30 +*/
    1.31 +const TUint32 typePrefix = 0x006C6168; 
    1.32 +
    1.33 +/**
    1.34 +HALSettings HAL.DAT header class
    1.35 +This class is used to validate HAL.DAT file header
    1.36 +*/
    1.37 +class THalFileHeader
    1.38 +	{
    1.39 +	TUint32 iMachineUid;	//Machine UID	
    1.40 +	TUint32 iTypePrefix;	//HAL.DAT first 4 bytes 'h' 'a' 'l' and version '0'
    1.41 +	public:
    1.42 +	THalFileHeader(TUint32 aMachineUid, TUint32 aTypePrefix);
    1.43 +	TInt ValidateHeader();
    1.44 +	};   
    1.45 +
    1.46 +/** Function to manage command line
    1.47 +*/	
    1.48 +TInt HALSettingsManager();
    1.49 +
    1.50 +/** Function to Initialise HAL attribute
    1.51 +*/
    1.52 +TInt InitialiseHAL();
    1.53 +/** Function to Persist HAL attribute
    1.54 +*/
    1.55 +TInt PersistHAL();
    1.56 +
    1.57 +/**
    1.58 +Constructor of THalFileHeader class
    1.59 +@param aMachineUid Machine Uid
    1.60 +@param aTypePrefix header 'h' 'a' 'l' and version no.
    1.61 +*/
    1.62 +THalFileHeader::THalFileHeader(TUint32 aMachineUid, TUint32 aTypePrefix)
    1.63 +: iMachineUid (aMachineUid), iTypePrefix(aTypePrefix){}
    1.64 +		
    1.65 +/**
    1.66 +Validate header of the hal.dat file.
    1.67 +@return KErrNone if successful otherwise KErrCorrupt.
    1.68 +*/
    1.69 +TInt THalFileHeader::ValidateHeader() 
    1.70 +	{
    1.71 +	TInt result;
    1.72 +	TInt machineUID;
    1.73 +	result = HAL::Get(HAL::EMachineUid,machineUID);	
    1.74 +	if (result != KErrNone)
    1.75 +		return result;	
    1.76 +	if(iTypePrefix != typePrefix && (TUint32)machineUID != iMachineUid)
    1.77 +		return KErrCorrupt;
    1.78 +	return result;
    1.79 +	}
    1.80 +
    1.81 +/**
    1.82 +Get the path (drive & folder path) of the HAL data file.
    1.83 +@param aPathName On completion this will contain the result.
    1.84 +*/	
    1.85 +void GetSystemDrivePath(THalFileName& aPathName)
    1.86 +	{
    1.87 +	aPathName.Copy(KHalFilePath);
    1.88 +	aPathName[0] = static_cast<TUint16>('A' + (RFs::GetSystemDrive()));
    1.89 +	}
    1.90 +
    1.91 +/**
    1.92 +Initialise the HAL.
    1.93 +Read the saved HAL file - containing a series of saved HAL attributes. If present, initialise
    1.94 +each attribute saved.
    1.95 +@return KErrNone if successful, otherwise any system wide error code.
    1.96 +*/
    1.97 +TInt InitialiseHAL()
    1.98 +	{
    1.99 +	//File server to open the HAL.DAT file
   1.100 +	RFs fs;
   1.101 +	TInt result = fs.Connect(); 
   1.102 +	if (result != KErrNone)
   1.103 +		{
   1.104 +		return result;
   1.105 +		}
   1.106 +	//Get the system drive path
   1.107 +	THalFileName halFileName;
   1.108 +	GetSystemDrivePath(halFileName);
   1.109 +	halFileName.Append(KHalFileName);
   1.110 +	
   1.111 +	//Open the hal.dat file with EFileShare Exclusive mode to read HAL attributes
   1.112 +	RFile file;
   1.113 +	result = file.Open(fs,halFileName,EFileRead | EFileShareExclusive);
   1.114 +	if (result != KErrNone)
   1.115 +		{
   1.116 +		fs.Close();
   1.117 +		if ( result == KErrPathNotFound )
   1.118 +			result = KErrNone;
   1.119 +		return result;		
   1.120 +		}
   1.121 +		
   1.122 +	//Checking the file integrity (total size should always be multiples of 8) 	
   1.123 +	TInt size=0;
   1.124 +	result = file.Size(size);
   1.125 +	if (result != KErrNone || size <= (TInt)sizeof(THalFileHeader) || (size&7) != 0)
   1.126 +		{
   1.127 +		file.Close();
   1.128 +		fs.Close();
   1.129 +		return KErrCorrupt;	
   1.130 +		}
   1.131 +	//Allocate a buffer to read all HAL.DAT file
   1.132 +	TInt* pBuf=(TInt*)User::Alloc(size);
   1.133 +	if (!pBuf)
   1.134 +		{
   1.135 +		file.Close();
   1.136 +		fs.Close();
   1.137 +		return  KErrNoMemory;		
   1.138 +		}
   1.139 +	TPtr8 bptr((TUint8*)pBuf,size);
   1.140 +	
   1.141 +	//Read HAL.DAT to the allocated buffer	
   1.142 +	result = file.Read(bptr);
   1.143 +	if ( result == KErrNone)
   1.144 +		{
   1.145 +		const TInt* pD = pBuf;
   1.146 +		THalFileHeader header (*pD, *(pD+1));
   1.147 +		pD += 2; //first 8 bytes are header  
   1.148 +		
   1.149 +		//Checking the validity of the file header and if valid set all HAL attributes
   1.150 +		if ((result = header.ValidateHeader()) == KErrNone)
   1.151 +			{
   1.152 +			HAL::Set(HALData::EPersistStartupModeKernel, *pD);
   1.153 +			}
   1.154 +		}
   1.155 +	User::Free(pBuf);
   1.156 +	file.Close();
   1.157 +	fs.Close();
   1.158 +	return(result);	
   1.159 +	}
   1.160 +
   1.161 +/**
   1.162 +Persist the HAL.
   1.163 +Gets all HAL attributes, and their properties 
   1.164 +then save attributes (which are meaningful and modifiable on this device) to hal.dat
   1.165 +@return KErrNone if successful, otherwise any system wide error code.
   1.166 +*/
   1.167 +TInt PersistHAL()
   1.168 +	{
   1.169 +	TInt value;
   1.170 +	TInt result = HAL::Get(HALData::EPersistStartupModeKernel, value);
   1.171 +	if ( result != KErrNone )
   1.172 +		{
   1.173 +		return result;
   1.174 +		}
   1.175 +	RFs fs;
   1.176 +	result=fs.Connect();
   1.177 +	if ( result != KErrNone )
   1.178 +		{
   1.179 +		return result;	
   1.180 +		}
   1.181 +	THalFileName halFile;
   1.182 +	GetSystemDrivePath(halFile);
   1.183 +	
   1.184 +	// Ensure directory \private\SID exists in target drive
   1.185 +	result = fs.MkDirAll(halFile);
   1.186 +	if (result != KErrNone )
   1.187 +		if(result != KErrAlreadyExists )
   1.188 +			{
   1.189 +			fs.Close();
   1.190 +			return 	result;
   1.191 +			}
   1.192 +	TInt muid=0;
   1.193 +	
   1.194 +	// Gets the machine's unique ID
   1.195 +	result=HAL::Get(HAL::EMachineUid, muid);
   1.196 +	if ( result != KErrNone )
   1.197 +		{
   1.198 +		fs.Close();
   1.199 +		return 	result;	
   1.200 +		}
   1.201 +		
   1.202 +	//Allocating a buffer with size of header and data (HAL attributes)	
   1.203 +	RBuf8 buf;
   1.204 +	result = buf.ReAlloc(sizeof(THalFileHeader) + 8);
   1.205 +	if(result != KErrNone)
   1.206 +		{
   1.207 +		fs.Close();
   1.208 +		return 	result;		
   1.209 +		}
   1.210 +		
   1.211 +	//Appending header and hal attributes to the allocated buffer		
   1.212 +	THalFileHeader header (muid,typePrefix);
   1.213 +	buf.Append((const TUint8*)&header,sizeof(THalFileHeader));
   1.214 +	buf.Append((const TUint8*)&value, 8);
   1.215 +	
   1.216 +	//Saving HAL setting to a temp file after that rename it to HAL.DAT
   1.217 +	RFile file;
   1.218 +	TFileName tempFile;
   1.219 +	result = file.Temp(fs,halFile,tempFile,EFileWrite|EFileShareExclusive);
   1.220 +
   1.221 +	if ( result == KErrNone )
   1.222 +		{
   1.223 +		result = file.Write(buf);
   1.224 +		if ( result == KErrNone )
   1.225 +			{
   1.226 +			halFile.Append(KHalFileName); 
   1.227 +			fs.Delete(halFile); // ignore if error 
   1.228 +			result = file.Rename(halFile);
   1.229 +			}
   1.230 +		file.Close();	
   1.231 +		}	
   1.232 +	buf.Close();
   1.233 +	fs.Close();
   1.234 +	return result;
   1.235 +	}
   1.236 +	
   1.237 +/**
   1.238 +HAL Settings Manager.
   1.239 +Manages the request for initialise and persist hal settings through command line
   1.240 +For initialise it checks SID of the process send the request and command line parameter.
   1.241 +If the SID = SID of EStart and command line is "INITIALISE" it initialise hal settings.
   1.242 +For persistence it only checks the command line = "PERSIST"
   1.243 +@return KErrNone if successful, otherwise any system wide error code.
   1.244 +*/
   1.245 +TInt HALSettingsManager()
   1.246 +	{
   1.247 +	const TInt KMaxArgumentLength = 10;
   1.248 +	const TInt KEStartSID = 0x10272C04; //SID of EStart
   1.249 +	_LIT(KHalInitialise,"INITIALISE");
   1.250 +	_LIT(KHalPersist,"PERSIST");
   1.251 +
   1.252 +	if (User::CommandLineLength() >  KMaxArgumentLength)
   1.253 +		return KErrArgument;
   1.254 +	TBuf<KMaxArgumentLength> args;
   1.255 +	User::CommandLine(args);
   1.256 +	TInt result;
   1.257 +	
   1.258 +	//Initialise or Persist HAL depending on command line arguments
   1.259 +	if (args.CompareF(KHalInitialise) == 0)
   1.260 +		{
   1.261 +		if(User::CreatorSecureId() != KEStartSID)
   1.262 +				return KErrPermissionDenied;	
   1.263 +		result = InitialiseHAL();	
   1.264 +		}
   1.265 +	else if (args.CompareF(KHalPersist) == 0)
   1.266 +		{
   1.267 +		result = PersistHAL();
   1.268 +		}
   1.269 +	else
   1.270 +		{
   1.271 +		return KErrArgument;
   1.272 +		}
   1.273 +	return result;
   1.274 +	}
   1.275 +
   1.276 +GLDEF_C TInt E32Main()		   
   1.277 +	{
   1.278 +	__UHEAP_MARK;
   1.279 +	
   1.280 +	TInt result = HALSettingsManager();
   1.281 +
   1.282 +	__UHEAP_MARKEND;
   1.283 +	
   1.284 +	return result;
   1.285 +	}