First public contribution.
1 // Copyright (c) 2007-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.
19 /** HAL attributes data folder */
20 _LIT(KHalFilePath,"_:\\private\\102825B1\\");
21 /** HAL attributes data file name */
22 _LIT(KHalFileName,"HAL.DAT");
23 /** Buffer descriptor for holding complete HAL data file path and name. */
24 typedef TBuf<28> THalFileName;
26 /**First 4 bytes in the HAL.DAT ('h' 'a' 'l' and version '0')
28 const TUint32 typePrefix = 0x006C6168;
31 HALSettings HAL.DAT header class
32 This class is used to validate HAL.DAT file header
36 TUint32 iMachineUid; //Machine UID
37 TUint32 iTypePrefix; //HAL.DAT first 4 bytes 'h' 'a' 'l' and version '0'
39 THalFileHeader(TUint32 aMachineUid, TUint32 aTypePrefix);
40 TInt ValidateHeader();
43 /** Function to manage command line
45 TInt HALSettingsManager();
47 /** Function to Initialise HAL attribute
50 /** Function to Persist HAL attribute
55 Constructor of THalFileHeader class
56 @param aMachineUid Machine Uid
57 @param aTypePrefix header 'h' 'a' 'l' and version no.
59 THalFileHeader::THalFileHeader(TUint32 aMachineUid, TUint32 aTypePrefix)
60 : iMachineUid (aMachineUid), iTypePrefix(aTypePrefix){}
63 Validate header of the hal.dat file.
64 @return KErrNone if successful otherwise KErrCorrupt.
66 TInt THalFileHeader::ValidateHeader()
70 result = HAL::Get(HAL::EMachineUid,machineUID);
71 if (result != KErrNone)
73 if(iTypePrefix != typePrefix && (TUint32)machineUID != iMachineUid)
79 Get the path (drive & folder path) of the HAL data file.
80 @param aPathName On completion this will contain the result.
82 void GetSystemDrivePath(THalFileName& aPathName)
84 aPathName.Copy(KHalFilePath);
85 aPathName[0] = static_cast<TUint16>('A' + (RFs::GetSystemDrive()));
90 Read the saved HAL file - containing a series of saved HAL attributes. If present, initialise
92 @return KErrNone if successful, otherwise any system wide error code.
96 //File server to open the HAL.DAT file
98 TInt result = fs.Connect();
99 if (result != KErrNone)
103 //Get the system drive path
104 THalFileName halFileName;
105 GetSystemDrivePath(halFileName);
106 halFileName.Append(KHalFileName);
108 //Open the hal.dat file with EFileShare Exclusive mode to read HAL attributes
110 result = file.Open(fs,halFileName,EFileRead | EFileShareExclusive);
111 if (result != KErrNone)
114 if ( result == KErrPathNotFound )
119 //Checking the file integrity (total size should always be multiples of 8)
121 result = file.Size(size);
122 if (result != KErrNone || size <= (TInt)sizeof(THalFileHeader) || (size&7) != 0)
128 //Allocate a buffer to read all HAL.DAT file
129 TInt* pBuf=(TInt*)User::Alloc(size);
136 TPtr8 bptr((TUint8*)pBuf,size);
138 //Read HAL.DAT to the allocated buffer
139 result = file.Read(bptr);
140 if ( result == KErrNone)
142 const TInt* pD = pBuf;
143 THalFileHeader header (*pD, *(pD+1));
144 pD += 2; //first 8 bytes are header
146 //Checking the validity of the file header and if valid set all HAL attributes
147 if ((result = header.ValidateHeader()) == KErrNone)
149 HAL::Set(HALData::EPersistStartupModeKernel, *pD);
160 Gets all HAL attributes, and their properties
161 then save attributes (which are meaningful and modifiable on this device) to hal.dat
162 @return KErrNone if successful, otherwise any system wide error code.
167 TInt result = HAL::Get(HALData::EPersistStartupModeKernel, value);
168 if ( result != KErrNone )
174 if ( result != KErrNone )
178 THalFileName halFile;
179 GetSystemDrivePath(halFile);
181 // Ensure directory \private\SID exists in target drive
182 result = fs.MkDirAll(halFile);
183 if (result != KErrNone )
184 if(result != KErrAlreadyExists )
191 // Gets the machine's unique ID
192 result=HAL::Get(HAL::EMachineUid, muid);
193 if ( result != KErrNone )
199 //Allocating a buffer with size of header and data (HAL attributes)
201 result = buf.ReAlloc(sizeof(THalFileHeader) + 8);
202 if(result != KErrNone)
208 //Appending header and hal attributes to the allocated buffer
209 THalFileHeader header (muid,typePrefix);
210 buf.Append((const TUint8*)&header,sizeof(THalFileHeader));
211 buf.Append((const TUint8*)&value, 8);
213 //Saving HAL setting to a temp file after that rename it to HAL.DAT
216 result = file.Temp(fs,halFile,tempFile,EFileWrite|EFileShareExclusive);
218 if ( result == KErrNone )
220 result = file.Write(buf);
221 if ( result == KErrNone )
223 halFile.Append(KHalFileName);
224 fs.Delete(halFile); // ignore if error
225 result = file.Rename(halFile);
235 HAL Settings Manager.
236 Manages the request for initialise and persist hal settings through command line
237 For initialise it checks SID of the process send the request and command line parameter.
238 If the SID = SID of EStart and command line is "INITIALISE" it initialise hal settings.
239 For persistence it only checks the command line = "PERSIST"
240 @return KErrNone if successful, otherwise any system wide error code.
242 TInt HALSettingsManager()
244 const TInt KMaxArgumentLength = 10;
245 const TInt KEStartSID = 0x10272C04; //SID of EStart
246 _LIT(KHalInitialise,"INITIALISE");
247 _LIT(KHalPersist,"PERSIST");
249 if (User::CommandLineLength() > KMaxArgumentLength)
251 TBuf<KMaxArgumentLength> args;
252 User::CommandLine(args);
255 //Initialise or Persist HAL depending on command line arguments
256 if (args.CompareF(KHalInitialise) == 0)
258 if(User::CreatorSecureId() != KEStartSID)
259 return KErrPermissionDenied;
260 result = InitialiseHAL();
262 else if (args.CompareF(KHalPersist) == 0)
264 result = PersistHAL();
273 GLDEF_C TInt E32Main()
277 TInt result = HALSettingsManager();