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