e32tools/e32lib/setcap/setcap.cpp
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     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 "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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32utils\setcap\setcap.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "setcap.h"
       
    19 
       
    20 #include <e32std.h>
       
    21 
       
    22 GLDEF_D TBool CapabilitySet;
       
    23 GLDEF_D SCapabilitySet Capability;
       
    24 GLDEF_D TBool SecureIdSet;
       
    25 GLDEF_D TSecureId SecureId;
       
    26 GLDEF_D TBool VendorIdSet;
       
    27 GLDEF_D TVendorId VendorId;
       
    28 
       
    29 #ifdef __TOOLS__
       
    30 #define		Mem		HMem
       
    31 #include "h_utl.h"
       
    32 #endif //__TOOLS__
       
    33 
       
    34 #if !defined(__EPOC32__) && !defined(__LINUX__)
       
    35 
       
    36 GLDEF_C TInt SetCap(HANDLE hFile)
       
    37 	{
       
    38 	DWORD ret;
       
    39 	TText8 checkedUidBuf[sizeof(TCheckedUid)];
       
    40 	ReadFile(hFile,checkedUidBuf,sizeof(TCheckedUid),&ret,NULL);
       
    41 	if (ret!=sizeof(TCheckedUid))
       
    42 		goto close;
       
    43 
       
    44 	//Look at PE file for UID section
       
    45 	{
       
    46 	const TInt KPeHeaderAddrAddr=0x3c;
       
    47 	const TInt KPeHeaderAddrSize=0x01;
       
    48 	const TInt KNumberOfSectionsOffset=0x06;
       
    49 	const TInt KNumberOfSectionsSize=0x02;
       
    50 	const TInt KSectionTableOffset=0xf8;
       
    51 	const TInt KSectionHeaderSize=0x28;
       
    52 	const TInt KSectionNameLength=0x08;
       
    53 	const TInt KPtrToRawDataOffset=0x14;
       
    54 	const TInt KPtrToRawDataSize=0x04;
       
    55 	const TText8 peText[4]={'P','E',0,0};
       
    56 	const TText8 uidText[8]={'.','S','Y','M','B','I','A','N'};
       
    57 		
       
    58 	//Read address of start of PE header
       
    59 	if (SetFilePointer(hFile,KPeHeaderAddrAddr,0,FILE_BEGIN)==0xFFFFFFFF)
       
    60 		goto close;
       
    61 	TInt peAddr=0;
       
    62 	ReadFile(hFile,&peAddr,KPeHeaderAddrSize,&ret,NULL);
       
    63 	if (ret!=KPeHeaderAddrSize)
       
    64 		goto close;
       
    65 		
       
    66 	//Check it really is the start of PE header
       
    67 	if (SetFilePointer(hFile,peAddr,0,FILE_BEGIN)==0xFFFFFFFF)
       
    68 		goto close;
       
    69 	TText8 text[4];
       
    70 	ReadFile(hFile,text,4,&ret,NULL);
       
    71 	if (*(TInt32*)text!=*(TInt32*)peText)
       
    72 		goto close;
       
    73 		
       
    74 	//Read number of sections
       
    75 	if (SetFilePointer(hFile,peAddr+KNumberOfSectionsOffset,0,FILE_BEGIN)==0xFFFFFFFF)
       
    76 		goto close;
       
    77 	TInt sections=0;
       
    78 	ReadFile(hFile,&sections,KNumberOfSectionsSize,&ret,NULL);
       
    79 	if (ret!=KNumberOfSectionsSize)
       
    80 		goto close;
       
    81 
       
    82 	//Go through section headers looking for UID section
       
    83 	if (SetFilePointer(hFile,peAddr+KSectionTableOffset,0,FILE_BEGIN)==0xFFFFFFFF)
       
    84 		goto close;
       
    85 	TInt i=0;
       
    86 	for(;i<sections;i++)
       
    87 		{
       
    88 		TText8 name[KSectionNameLength];
       
    89 		ReadFile(hFile,name,KSectionNameLength,&ret,NULL);
       
    90 		if (ret!=KSectionNameLength)
       
    91 			goto close;
       
    92 		if (*(TInt64*)name==*(TInt64*)uidText)
       
    93 			break;
       
    94 		if (SetFilePointer(hFile,KSectionHeaderSize-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
       
    95 			goto close;
       
    96 		}
       
    97 	if (i==sections)
       
    98 		goto close;
       
    99 
       
   100 	//Read RVA/Offset
       
   101 	if (SetFilePointer(hFile,KPtrToRawDataOffset-KSectionNameLength,0,FILE_CURRENT)==0xFFFFFFFF)
       
   102 		goto close;
       
   103 	TInt uidOffset;
       
   104 	ReadFile(hFile,&uidOffset,KPtrToRawDataSize,&ret,NULL);
       
   105 	if (ret!=KPtrToRawDataSize)
       
   106 		goto close;
       
   107 
       
   108 	//SYMBIAN Header
       
   109 	if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
       
   110 		goto close;
       
   111 
       
   112 	TEmulatorImageHeader header;
       
   113 
       
   114 	ReadFile(hFile,&header,sizeof(header),&ret,NULL);
       
   115 	if (ret!=sizeof(header))
       
   116 		goto close;
       
   117 
       
   118 	// set new capabilities
       
   119 	if (CapabilitySet)
       
   120 		{
       
   121 		header.iS.iCaps = Capability;
       
   122 		}
       
   123 
       
   124 	// set new secure id and vendor id if specified
       
   125 	if (SecureIdSet)
       
   126 		{
       
   127 		header.iS.iSecureId = SecureId.iId;
       
   128 		}
       
   129 	if (VendorIdSet)
       
   130 		{
       
   131 		header.iS.iVendorId = VendorId.iId;
       
   132 		}
       
   133 
       
   134 	// save header values
       
   135 	Capability = header.iS.iCaps;
       
   136 	SecureId.iId = header.iS.iSecureId;
       
   137 	VendorId.iId = header.iS.iVendorId;
       
   138 
       
   139 	if (SetFilePointer(hFile,uidOffset,0,FILE_BEGIN)==0xFFFFFFFF)
       
   140 		goto close;
       
   141 
       
   142 	BOOL b = WriteFile(hFile,&header,sizeof(header),&ret,NULL);
       
   143 	if (b==FALSE)
       
   144 		goto close;
       
   145 
       
   146 	CloseHandle(hFile);
       
   147 	return KErrNone;
       
   148 	}
       
   149 close:
       
   150 	CloseHandle(hFile);
       
   151 	return KErrGeneral;
       
   152 	}
       
   153 
       
   154 #endif //__EPOC32__
       
   155 
       
   156 #ifndef __WINS__
       
   157 
       
   158 GLDEF_C TInt SetCap(E32ImageHeader* h)
       
   159 	{
       
   160 	TUint hdrfmt = h->HeaderFormat();
       
   161 	if (hdrfmt < KImageHdrFmt_V)
       
   162 		return KErrNotSupported;
       
   163 	E32ImageHeaderV* v = (E32ImageHeaderV*)h;
       
   164 	if (CapabilitySet)
       
   165 		v->iS.iCaps = Capability;
       
   166 	if (SecureIdSet)
       
   167 		v->iS.iSecureId = SecureId.iId;
       
   168 	if (VendorIdSet)
       
   169 		v->iS.iVendorId = VendorId.iId;	
       
   170 
       
   171 	// save header values
       
   172 	Capability = v->iS.iCaps;
       
   173 	SecureId.iId = v->iS.iSecureId;
       
   174 	VendorId.iId = v->iS.iVendorId;
       
   175 
       
   176 	// regenerate CRC
       
   177 	v->iHeaderCrc = KImageCrcInitialiser;
       
   178 	TUint32 crc = 0;
       
   179 	Mem::Crc32(crc, v, v->TotalSize());
       
   180 	v->iHeaderCrc = crc;
       
   181 	return KErrNone;
       
   182 	}
       
   183 
       
   184 #endif //__WINS__