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\setcap.cpp
21 #include <e32std_private.h>
23 GLDEF_D TBool CapabilitySet;
24 GLDEF_D SCapabilitySet Capability;
25 GLDEF_D TBool SecureIdSet;
26 GLDEF_D TSecureId SecureId;
27 GLDEF_D TBool VendorIdSet;
28 GLDEF_D TVendorId VendorId;
37 GLDEF_C TInt SetCap(HANDLE hFile)
40 TText8 checkedUidBuf[sizeof(TCheckedUid)];
41 ReadFile(hFile,checkedUidBuf,sizeof(TCheckedUid),&ret,NULL);
42 if (ret!=sizeof(TCheckedUid))
45 //Look at PE file for UID section
47 const TInt KPeHeaderAddrAddr=0x3c;
48 const TInt KPeHeaderAddrSize=0x01;
49 const TInt KNumberOfSectionsOffset=0x06;
50 const TInt KNumberOfSectionsSize=0x02;
51 const TInt KSectionTableOffset=0xf8;
52 const TInt KSectionHeaderSize=0x28;
53 const TInt KSectionNameLength=0x08;
54 const TInt KPtrToRawDataOffset=0x14;
55 const TInt KPtrToRawDataSize=0x04;
56 const TText8 peText[4]={'P','E',0,0};
57 const TText8 uidText[8]={'.','S','Y','M','B','I','A','N'};
59 //Read address of start of PE header
60 if (SetFilePointer(hFile,KPeHeaderAddrAddr,0,FILE_BEGIN)==0xFFFFFFFF)
63 ReadFile(hFile,&peAddr,KPeHeaderAddrSize,&ret,NULL);
64 if (ret!=KPeHeaderAddrSize)
67 //Check it really is the start of PE header
68 if (SetFilePointer(hFile,peAddr,0,FILE_BEGIN)==0xFFFFFFFF)
71 ReadFile(hFile,text,4,&ret,NULL);
72 if (*(TInt32*)text!=*(TInt32*)peText)
75 //Read number of sections
76 if (SetFilePointer(hFile,peAddr+KNumberOfSectionsOffset,0,FILE_BEGIN)==0xFFFFFFFF)
79 ReadFile(hFile,§ions,KNumberOfSectionsSize,&ret,NULL);
80 if (ret!=KNumberOfSectionsSize)
83 //Go through section headers looking for UID section
84 if (SetFilePointer(hFile,peAddr+KSectionTableOffset,0,FILE_BEGIN)==0xFFFFFFFF)
89 TText8 name[KSectionNameLength];
90 ReadFile(hFile,name,KSectionNameLength,&ret,NULL);
91 if (ret!=KSectionNameLength)
93 if (*(TInt64*)name==*(TInt64*)uidText)
95 if (SetFilePointer(hFile,KSectionHeaderSize-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
102 if (SetFilePointer(hFile,KPtrToRawDataOffset-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
105 ReadFile(hFile,&uidOffset,KPtrToRawDataSize,&ret,NULL);
106 if (ret!=KPtrToRawDataSize)
110 if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
113 TEmulatorImageHeader header;
115 ReadFile(hFile,&header,sizeof(header),&ret,NULL);
116 if (ret!=sizeof(header))
119 // set new capabilities
122 header.iS.iCaps = Capability;
125 // set new secure id and vendor id if specified
128 header.iS.iSecureId = SecureId.iId;
132 header.iS.iVendorId = VendorId.iId;
135 // save header values
136 Capability = header.iS.iCaps;
137 SecureId.iId = header.iS.iSecureId;
138 VendorId.iId = header.iS.iVendorId;
140 if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
143 BOOL b = WriteFile(hFile,&header,sizeof(header),&ret,NULL);
159 GLDEF_C TInt SetCap(E32ImageHeader* h)
161 TUint hdrfmt = h->HeaderFormat();
162 if (hdrfmt < KImageHdrFmt_V)
163 return KErrNotSupported;
164 E32ImageHeaderV* v = (E32ImageHeaderV*)h;
166 v->iS.iCaps = Capability;
168 v->iS.iSecureId = SecureId.iId;
170 v->iS.iVendorId = VendorId.iId;
172 // save header values
173 Capability = v->iS.iCaps;
174 SecureId.iId = v->iS.iSecureId;
175 VendorId.iId = v->iS.iVendorId;
178 v->iHeaderCrc = KImageCrcInitialiser;
180 Mem::Crc32(crc, v, v->TotalSize());