os/kernelhwsrv/kerneltest/e32test/power/halsettings.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) 2007-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 "hal.h"
sl@0
    17
#include <f32file.h>
sl@0
    18
sl@0
    19
/** HAL attributes data folder */
sl@0
    20
_LIT(KHalFilePath,"_:\\private\\102825B1\\");
sl@0
    21
/** HAL attributes data file name */
sl@0
    22
_LIT(KHalFileName,"HAL.DAT");
sl@0
    23
/** Buffer descriptor for holding complete HAL data file path and name. */
sl@0
    24
typedef TBuf<28> THalFileName; 
sl@0
    25
sl@0
    26
/**First 4 bytes in the HAL.DAT ('h' 'a' 'l' and version '0')
sl@0
    27
*/
sl@0
    28
const TUint32 typePrefix = 0x006C6168; 
sl@0
    29
sl@0
    30
/**
sl@0
    31
HALSettings HAL.DAT header class
sl@0
    32
This class is used to validate HAL.DAT file header
sl@0
    33
*/
sl@0
    34
class THalFileHeader
sl@0
    35
	{
sl@0
    36
	TUint32 iMachineUid;	//Machine UID	
sl@0
    37
	TUint32 iTypePrefix;	//HAL.DAT first 4 bytes 'h' 'a' 'l' and version '0'
sl@0
    38
	public:
sl@0
    39
	THalFileHeader(TUint32 aMachineUid, TUint32 aTypePrefix);
sl@0
    40
	TInt ValidateHeader();
sl@0
    41
	};   
sl@0
    42
sl@0
    43
/** Function to manage command line
sl@0
    44
*/	
sl@0
    45
TInt HALSettingsManager();
sl@0
    46
sl@0
    47
/** Function to Initialise HAL attribute
sl@0
    48
*/
sl@0
    49
TInt InitialiseHAL();
sl@0
    50
/** Function to Persist HAL attribute
sl@0
    51
*/
sl@0
    52
TInt PersistHAL();
sl@0
    53
sl@0
    54
/**
sl@0
    55
Constructor of THalFileHeader class
sl@0
    56
@param aMachineUid Machine Uid
sl@0
    57
@param aTypePrefix header 'h' 'a' 'l' and version no.
sl@0
    58
*/
sl@0
    59
THalFileHeader::THalFileHeader(TUint32 aMachineUid, TUint32 aTypePrefix)
sl@0
    60
: iMachineUid (aMachineUid), iTypePrefix(aTypePrefix){}
sl@0
    61
		
sl@0
    62
/**
sl@0
    63
Validate header of the hal.dat file.
sl@0
    64
@return KErrNone if successful otherwise KErrCorrupt.
sl@0
    65
*/
sl@0
    66
TInt THalFileHeader::ValidateHeader() 
sl@0
    67
	{
sl@0
    68
	TInt result;
sl@0
    69
	TInt machineUID;
sl@0
    70
	result = HAL::Get(HAL::EMachineUid,machineUID);	
sl@0
    71
	if (result != KErrNone)
sl@0
    72
		return result;	
sl@0
    73
	if(iTypePrefix != typePrefix && (TUint32)machineUID != iMachineUid)
sl@0
    74
		return KErrCorrupt;
sl@0
    75
	return result;
sl@0
    76
	}
sl@0
    77
sl@0
    78
/**
sl@0
    79
Get the path (drive & folder path) of the HAL data file.
sl@0
    80
@param aPathName On completion this will contain the result.
sl@0
    81
*/	
sl@0
    82
void GetSystemDrivePath(THalFileName& aPathName)
sl@0
    83
	{
sl@0
    84
	aPathName.Copy(KHalFilePath);
sl@0
    85
	aPathName[0] = static_cast<TUint16>('A' + (RFs::GetSystemDrive()));
sl@0
    86
	}
sl@0
    87
sl@0
    88
/**
sl@0
    89
Initialise the HAL.
sl@0
    90
Read the saved HAL file - containing a series of saved HAL attributes. If present, initialise
sl@0
    91
each attribute saved.
sl@0
    92
@return KErrNone if successful, otherwise any system wide error code.
sl@0
    93
*/
sl@0
    94
TInt InitialiseHAL()
sl@0
    95
	{
sl@0
    96
	//File server to open the HAL.DAT file
sl@0
    97
	RFs fs;
sl@0
    98
	TInt result = fs.Connect(); 
sl@0
    99
	if (result != KErrNone)
sl@0
   100
		{
sl@0
   101
		return result;
sl@0
   102
		}
sl@0
   103
	//Get the system drive path
sl@0
   104
	THalFileName halFileName;
sl@0
   105
	GetSystemDrivePath(halFileName);
sl@0
   106
	halFileName.Append(KHalFileName);
sl@0
   107
	
sl@0
   108
	//Open the hal.dat file with EFileShare Exclusive mode to read HAL attributes
sl@0
   109
	RFile file;
sl@0
   110
	result = file.Open(fs,halFileName,EFileRead | EFileShareExclusive);
sl@0
   111
	if (result != KErrNone)
sl@0
   112
		{
sl@0
   113
		fs.Close();
sl@0
   114
		if ( result == KErrPathNotFound )
sl@0
   115
			result = KErrNone;
sl@0
   116
		return result;		
sl@0
   117
		}
sl@0
   118
		
sl@0
   119
	//Checking the file integrity (total size should always be multiples of 8) 	
sl@0
   120
	TInt size=0;
sl@0
   121
	result = file.Size(size);
sl@0
   122
	if (result != KErrNone || size <= (TInt)sizeof(THalFileHeader) || (size&7) != 0)
sl@0
   123
		{
sl@0
   124
		file.Close();
sl@0
   125
		fs.Close();
sl@0
   126
		return KErrCorrupt;	
sl@0
   127
		}
sl@0
   128
	//Allocate a buffer to read all HAL.DAT file
sl@0
   129
	TInt* pBuf=(TInt*)User::Alloc(size);
sl@0
   130
	if (!pBuf)
sl@0
   131
		{
sl@0
   132
		file.Close();
sl@0
   133
		fs.Close();
sl@0
   134
		return  KErrNoMemory;		
sl@0
   135
		}
sl@0
   136
	TPtr8 bptr((TUint8*)pBuf,size);
sl@0
   137
	
sl@0
   138
	//Read HAL.DAT to the allocated buffer	
sl@0
   139
	result = file.Read(bptr);
sl@0
   140
	if ( result == KErrNone)
sl@0
   141
		{
sl@0
   142
		const TInt* pD = pBuf;
sl@0
   143
		THalFileHeader header (*pD, *(pD+1));
sl@0
   144
		pD += 2; //first 8 bytes are header  
sl@0
   145
		
sl@0
   146
		//Checking the validity of the file header and if valid set all HAL attributes
sl@0
   147
		if ((result = header.ValidateHeader()) == KErrNone)
sl@0
   148
			{
sl@0
   149
			HAL::Set(HALData::EPersistStartupModeKernel, *pD);
sl@0
   150
			}
sl@0
   151
		}
sl@0
   152
	User::Free(pBuf);
sl@0
   153
	file.Close();
sl@0
   154
	fs.Close();
sl@0
   155
	return(result);	
sl@0
   156
	}
sl@0
   157
sl@0
   158
/**
sl@0
   159
Persist the HAL.
sl@0
   160
Gets all HAL attributes, and their properties 
sl@0
   161
then save attributes (which are meaningful and modifiable on this device) to hal.dat
sl@0
   162
@return KErrNone if successful, otherwise any system wide error code.
sl@0
   163
*/
sl@0
   164
TInt PersistHAL()
sl@0
   165
	{
sl@0
   166
	TInt value;
sl@0
   167
	TInt result = HAL::Get(HALData::EPersistStartupModeKernel, value);
sl@0
   168
	if ( result != KErrNone )
sl@0
   169
		{
sl@0
   170
		return result;
sl@0
   171
		}
sl@0
   172
	RFs fs;
sl@0
   173
	result=fs.Connect();
sl@0
   174
	if ( result != KErrNone )
sl@0
   175
		{
sl@0
   176
		return result;	
sl@0
   177
		}
sl@0
   178
	THalFileName halFile;
sl@0
   179
	GetSystemDrivePath(halFile);
sl@0
   180
	
sl@0
   181
	// Ensure directory \private\SID exists in target drive
sl@0
   182
	result = fs.MkDirAll(halFile);
sl@0
   183
	if (result != KErrNone )
sl@0
   184
		if(result != KErrAlreadyExists )
sl@0
   185
			{
sl@0
   186
			fs.Close();
sl@0
   187
			return 	result;
sl@0
   188
			}
sl@0
   189
	TInt muid=0;
sl@0
   190
	
sl@0
   191
	// Gets the machine's unique ID
sl@0
   192
	result=HAL::Get(HAL::EMachineUid, muid);
sl@0
   193
	if ( result != KErrNone )
sl@0
   194
		{
sl@0
   195
		fs.Close();
sl@0
   196
		return 	result;	
sl@0
   197
		}
sl@0
   198
		
sl@0
   199
	//Allocating a buffer with size of header and data (HAL attributes)	
sl@0
   200
	RBuf8 buf;
sl@0
   201
	result = buf.ReAlloc(sizeof(THalFileHeader) + 8);
sl@0
   202
	if(result != KErrNone)
sl@0
   203
		{
sl@0
   204
		fs.Close();
sl@0
   205
		return 	result;		
sl@0
   206
		}
sl@0
   207
		
sl@0
   208
	//Appending header and hal attributes to the allocated buffer		
sl@0
   209
	THalFileHeader header (muid,typePrefix);
sl@0
   210
	buf.Append((const TUint8*)&header,sizeof(THalFileHeader));
sl@0
   211
	buf.Append((const TUint8*)&value, 8);
sl@0
   212
	
sl@0
   213
	//Saving HAL setting to a temp file after that rename it to HAL.DAT
sl@0
   214
	RFile file;
sl@0
   215
	TFileName tempFile;
sl@0
   216
	result = file.Temp(fs,halFile,tempFile,EFileWrite|EFileShareExclusive);
sl@0
   217
sl@0
   218
	if ( result == KErrNone )
sl@0
   219
		{
sl@0
   220
		result = file.Write(buf);
sl@0
   221
		if ( result == KErrNone )
sl@0
   222
			{
sl@0
   223
			halFile.Append(KHalFileName); 
sl@0
   224
			fs.Delete(halFile); // ignore if error 
sl@0
   225
			result = file.Rename(halFile);
sl@0
   226
			}
sl@0
   227
		file.Close();	
sl@0
   228
		}	
sl@0
   229
	buf.Close();
sl@0
   230
	fs.Close();
sl@0
   231
	return result;
sl@0
   232
	}
sl@0
   233
	
sl@0
   234
/**
sl@0
   235
HAL Settings Manager.
sl@0
   236
Manages the request for initialise and persist hal settings through command line
sl@0
   237
For initialise it checks SID of the process send the request and command line parameter.
sl@0
   238
If the SID = SID of EStart and command line is "INITIALISE" it initialise hal settings.
sl@0
   239
For persistence it only checks the command line = "PERSIST"
sl@0
   240
@return KErrNone if successful, otherwise any system wide error code.
sl@0
   241
*/
sl@0
   242
TInt HALSettingsManager()
sl@0
   243
	{
sl@0
   244
	const TInt KMaxArgumentLength = 10;
sl@0
   245
	const TInt KEStartSID = 0x10272C04; //SID of EStart
sl@0
   246
	_LIT(KHalInitialise,"INITIALISE");
sl@0
   247
	_LIT(KHalPersist,"PERSIST");
sl@0
   248
sl@0
   249
	if (User::CommandLineLength() >  KMaxArgumentLength)
sl@0
   250
		return KErrArgument;
sl@0
   251
	TBuf<KMaxArgumentLength> args;
sl@0
   252
	User::CommandLine(args);
sl@0
   253
	TInt result;
sl@0
   254
	
sl@0
   255
	//Initialise or Persist HAL depending on command line arguments
sl@0
   256
	if (args.CompareF(KHalInitialise) == 0)
sl@0
   257
		{
sl@0
   258
		if(User::CreatorSecureId() != KEStartSID)
sl@0
   259
				return KErrPermissionDenied;	
sl@0
   260
		result = InitialiseHAL();	
sl@0
   261
		}
sl@0
   262
	else if (args.CompareF(KHalPersist) == 0)
sl@0
   263
		{
sl@0
   264
		result = PersistHAL();
sl@0
   265
		}
sl@0
   266
	else
sl@0
   267
		{
sl@0
   268
		return KErrArgument;
sl@0
   269
		}
sl@0
   270
	return result;
sl@0
   271
	}
sl@0
   272
sl@0
   273
GLDEF_C TInt E32Main()		   
sl@0
   274
	{
sl@0
   275
	__UHEAP_MARK;
sl@0
   276
	
sl@0
   277
	TInt result = HALSettingsManager();
sl@0
   278
sl@0
   279
	__UHEAP_MARKEND;
sl@0
   280
	
sl@0
   281
	return result;
sl@0
   282
	}