1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32utils/setcap/setcap.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,185 @@
1.4 +// Copyright (c) 1998-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 +// e32utils\setcap\setcap.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include "setcap.h"
1.22 +
1.23 +#include <e32std.h>
1.24 +#include <e32std_private.h>
1.25 +
1.26 +GLDEF_D TBool CapabilitySet;
1.27 +GLDEF_D SCapabilitySet Capability;
1.28 +GLDEF_D TBool SecureIdSet;
1.29 +GLDEF_D TSecureId SecureId;
1.30 +GLDEF_D TBool VendorIdSet;
1.31 +GLDEF_D TVendorId VendorId;
1.32 +
1.33 +#ifdef __TOOLS__
1.34 +#define Mem HMem
1.35 +#include "h_utl.h"
1.36 +#endif //__TOOLS__
1.37 +
1.38 +#ifndef __EPOC32__
1.39 +
1.40 +GLDEF_C TInt SetCap(HANDLE hFile)
1.41 + {
1.42 + DWORD ret;
1.43 + TText8 checkedUidBuf[sizeof(TCheckedUid)];
1.44 + ReadFile(hFile,checkedUidBuf,sizeof(TCheckedUid),&ret,NULL);
1.45 + if (ret!=sizeof(TCheckedUid))
1.46 + goto close;
1.47 +
1.48 + //Look at PE file for UID section
1.49 + {
1.50 + const TInt KPeHeaderAddrAddr=0x3c;
1.51 + const TInt KPeHeaderAddrSize=0x01;
1.52 + const TInt KNumberOfSectionsOffset=0x06;
1.53 + const TInt KNumberOfSectionsSize=0x02;
1.54 + const TInt KSectionTableOffset=0xf8;
1.55 + const TInt KSectionHeaderSize=0x28;
1.56 + const TInt KSectionNameLength=0x08;
1.57 + const TInt KPtrToRawDataOffset=0x14;
1.58 + const TInt KPtrToRawDataSize=0x04;
1.59 + const TText8 peText[4]={'P','E',0,0};
1.60 + const TText8 uidText[8]={'.','S','Y','M','B','I','A','N'};
1.61 +
1.62 + //Read address of start of PE header
1.63 + if (SetFilePointer(hFile,KPeHeaderAddrAddr,0,FILE_BEGIN)==0xFFFFFFFF)
1.64 + goto close;
1.65 + TInt peAddr=0;
1.66 + ReadFile(hFile,&peAddr,KPeHeaderAddrSize,&ret,NULL);
1.67 + if (ret!=KPeHeaderAddrSize)
1.68 + goto close;
1.69 +
1.70 + //Check it really is the start of PE header
1.71 + if (SetFilePointer(hFile,peAddr,0,FILE_BEGIN)==0xFFFFFFFF)
1.72 + goto close;
1.73 + TText8 text[4];
1.74 + ReadFile(hFile,text,4,&ret,NULL);
1.75 + if (*(TInt32*)text!=*(TInt32*)peText)
1.76 + goto close;
1.77 +
1.78 + //Read number of sections
1.79 + if (SetFilePointer(hFile,peAddr+KNumberOfSectionsOffset,0,FILE_BEGIN)==0xFFFFFFFF)
1.80 + goto close;
1.81 + TInt sections=0;
1.82 + ReadFile(hFile,§ions,KNumberOfSectionsSize,&ret,NULL);
1.83 + if (ret!=KNumberOfSectionsSize)
1.84 + goto close;
1.85 +
1.86 + //Go through section headers looking for UID section
1.87 + if (SetFilePointer(hFile,peAddr+KSectionTableOffset,0,FILE_BEGIN)==0xFFFFFFFF)
1.88 + goto close;
1.89 + TInt i=0;
1.90 + for(;i<sections;i++)
1.91 + {
1.92 + TText8 name[KSectionNameLength];
1.93 + ReadFile(hFile,name,KSectionNameLength,&ret,NULL);
1.94 + if (ret!=KSectionNameLength)
1.95 + goto close;
1.96 + if (*(TInt64*)name==*(TInt64*)uidText)
1.97 + break;
1.98 + if (SetFilePointer(hFile,KSectionHeaderSize-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
1.99 + goto close;
1.100 + }
1.101 + if (i==sections)
1.102 + goto close;
1.103 +
1.104 + //Read RVA/Offset
1.105 + if (SetFilePointer(hFile,KPtrToRawDataOffset-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
1.106 + goto close;
1.107 + TInt uidOffset;
1.108 + ReadFile(hFile,&uidOffset,KPtrToRawDataSize,&ret,NULL);
1.109 + if (ret!=KPtrToRawDataSize)
1.110 + goto close;
1.111 +
1.112 + //SYMBIAN Header
1.113 + if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
1.114 + goto close;
1.115 +
1.116 + TEmulatorImageHeader header;
1.117 +
1.118 + ReadFile(hFile,&header,sizeof(header),&ret,NULL);
1.119 + if (ret!=sizeof(header))
1.120 + goto close;
1.121 +
1.122 + // set new capabilities
1.123 + if (CapabilitySet)
1.124 + {
1.125 + header.iS.iCaps = Capability;
1.126 + }
1.127 +
1.128 + // set new secure id and vendor id if specified
1.129 + if (SecureIdSet)
1.130 + {
1.131 + header.iS.iSecureId = SecureId.iId;
1.132 + }
1.133 + if (VendorIdSet)
1.134 + {
1.135 + header.iS.iVendorId = VendorId.iId;
1.136 + }
1.137 +
1.138 + // save header values
1.139 + Capability = header.iS.iCaps;
1.140 + SecureId.iId = header.iS.iSecureId;
1.141 + VendorId.iId = header.iS.iVendorId;
1.142 +
1.143 + if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
1.144 + goto close;
1.145 +
1.146 + BOOL b = WriteFile(hFile,&header,sizeof(header),&ret,NULL);
1.147 + if (b==FALSE)
1.148 + goto close;
1.149 +
1.150 + CloseHandle(hFile);
1.151 + return KErrNone;
1.152 + }
1.153 +close:
1.154 + CloseHandle(hFile);
1.155 + return KErrGeneral;
1.156 + }
1.157 +
1.158 +#endif //__EPOC32__
1.159 +
1.160 +#ifndef __WINS__
1.161 +
1.162 +GLDEF_C TInt SetCap(E32ImageHeader* h)
1.163 + {
1.164 + TUint hdrfmt = h->HeaderFormat();
1.165 + if (hdrfmt < KImageHdrFmt_V)
1.166 + return KErrNotSupported;
1.167 + E32ImageHeaderV* v = (E32ImageHeaderV*)h;
1.168 + if (CapabilitySet)
1.169 + v->iS.iCaps = Capability;
1.170 + if (SecureIdSet)
1.171 + v->iS.iSecureId = SecureId.iId;
1.172 + if (VendorIdSet)
1.173 + v->iS.iVendorId = VendorId.iId;
1.174 +
1.175 + // save header values
1.176 + Capability = v->iS.iCaps;
1.177 + SecureId.iId = v->iS.iSecureId;
1.178 + VendorId.iId = v->iS.iVendorId;
1.179 +
1.180 + // regenerate CRC
1.181 + v->iHeaderCrc = KImageCrcInitialiser;
1.182 + TUint32 crc = 0;
1.183 + Mem::Crc32(crc, v, v->TotalSize());
1.184 + v->iHeaderCrc = crc;
1.185 + return KErrNone;
1.186 + }
1.187 +
1.188 +#endif //__WINS__