First public contribution.
1 // Copyright (c) 1998-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.
14 // e32utils\setcap\main.cpp
16 // Makes a copy of an executable file and gives it the specified capabilities.
17 // It may also, optionally, modify the Secure or Vendor IDs.
18 // This runs under the Symbian OS - it is not a native PC utility.
19 // Command line syntax:
20 // SETCAP source_exe capability [-SID secureId] [-VID vendorId] [destination_path]
21 // source_exe Name and path of an executable file (default path is Z:\SYS\BIN\)
22 // capability Hexadecimal value for capabilities
23 // secureId Optional hexadecimal value of secure ID
24 // vendorId Optional hexadecimal value of vendor ID
25 // destination_path Optional name and path to copy the exe to
26 // (defaults to C:\SYS\BIN\source_exe_name)
28 // 1. The 'capability' command line argument is the hexadecimal value of the
29 // capabilities when they are represented as a bit-field. E.g. the 3 capabilities
30 // LocalServices, ReadUserData and WriteUserData would together have a value of:
31 // (1<<ECapabilityLocalServices) | (<<ECapabilityReadUserData) | (1<<ECapabilityWriteUserData)
32 // Which in hexadecimal is '1c000'
33 // If the value supplied includes capabilities which aren't supported by the current
34 // OS version, then these are ignored and not added to the file.
35 // 2. If the source executable is in ROM it must be a RAM executable image, not an
36 // execute-in-place image. I.e. its entry in an OBY file must start with "data=" and
38 // For OBY files generated automatically by "ABLD ROMFILE" this needs to be achieved by
39 // using lines similar to the following in the executables MMP file:
40 // ROMTARGET // Empty ROM path means don't include normal execute-in-place file
41 // RAMTARGET \sys\bin\ // Target path (in ROM) for RAM executable image
42 // 3. The Symbian OS only allows one binary file with a given name; the name doesn't
43 // include file path or extension. This means if SETCAP is used to make a copy of a
44 // binary which is already loaded then the copy will not get loaded when used with
45 // RProcess::Create(), instead the already loaded version will be used. To avoid this,
46 // use SETCAP to give the copy a different name. E.g. "SETCAP test.exe 1234 test2.exe"
59 TParse DestinationName;
69 r = MapEmulatedFileName(sName, SourceName.NameAndExt());
74 r = MapEmulatedFileName(dName, DestinationName.FullName());
78 if(!Emulator::CopyFile((LPCTSTR)sName.PtrZ(),(LPCTSTR)dName.PtrZ(),FALSE))
81 HANDLE hFile=Emulator::CreateFile((LPCTSTR)dName.PtrZ(),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
82 if (hFile==INVALID_HANDLE_VALUE)
99 r=Source.Open(Fs,SourceName.FullName(),EFileRead);
103 r = Destination.Replace(Fs,DestinationName.FullName(),EFileWrite);
108 const TInt KBufferSize = 0x10000;
110 buffer = (TUint8*)User::Alloc(KBufferSize);
114 TPtr8 p(buffer,KBufferSize,KBufferSize);
119 if(r!=KErrNone || p.Size()==0)
123 // first block contains header
124 if ((TUint)p.Size() < sizeof(E32ImageHeader))
129 E32ImageHeader* h = (E32ImageHeader*)buffer;
133 r = Destination.Write(p);
147 _LIT(KDefaultSourcePath,"z:\\sys\\bin\\");
148 _LIT(KDefaultDestinationPath,"?:\\sys\\bin\\");
149 _LIT(KSIDOption,"-SID");
150 _LIT(KVIDOption,"-VID");
152 TInt ParseCommandLine()
155 User::CommandLine(c);
159 if(SourceName.SetNoWild(l.NextToken(),0,&KDefaultSourcePath)!=KErrNone)
163 TLex cl(l.NextToken());
164 if(cl.Val((TInt64&)Capability,EHex)!=KErrNone)
167 // Mask out unsupported capabilities
169 all.SetAllSupported();
170 ((TCapabilitySet&)Capability).Intersection(all);
172 // We always update capabilities in the headers
173 CapabilitySet = ETrue;
176 SecureIdSet = EFalse;
177 VendorIdSet = EFalse;
181 nextToken.Set(l.NextToken());
182 if (nextToken == KSIDOption)
185 nextToken.Set(l.NextToken());
186 if (nextToken == KNullDesC)
189 if(sl.Val(SecureId.iId,EHex)!=KErrNone)
193 else if (nextToken == KVIDOption)
196 nextToken.Set(l.NextToken());
197 if (nextToken == KNullDesC)
200 if(sl.Val(VendorId.iId,EHex)!=KErrNone)
209 TPtrC s(SourceName.NameAndExt());
210 TBuf<sizeof(KDefaultDestinationPath)> defaultDestinationPath(KDefaultDestinationPath);
211 defaultDestinationPath[0] = (TUint8) RFs::GetSystemDriveChar();
213 if(DestinationName.SetNoWild(nextToken,&s,&defaultDestinationPath)!=KErrNone)
216 // Check we used all the arguments
217 if (l.NextToken() != KNullDesC)
228 // Turn off lazy dll unloading
230 if ((r=l.Connect())!=KErrNone)
232 r = l.CancelLazyDllUnload();
237 r = ParseCommandLine();
243 r = Fs.MkDirAll(DestinationName.FullName());
244 if(r==KErrNone || r==KErrAlreadyExists)