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