1 | /* $Id: VBoxPeSetVersion.cpp 55435 2015-04-27 09:08:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Change the OS and SubSystem version to 4.0 (VS2010 trick).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #ifdef RT_OS_WINDOWS
|
---|
23 | # include <Windows.h>
|
---|
24 | #else
|
---|
25 | # include <iprt/stdint.h>
|
---|
26 |
|
---|
27 | typedef struct _IMAGE_DOS_HEADER
|
---|
28 | {
|
---|
29 | uint16_t e_magic;
|
---|
30 | uint16_t e_cblp;
|
---|
31 | uint16_t e_cp;
|
---|
32 | uint16_t e_crlc;
|
---|
33 | uint16_t e_cparhdr;
|
---|
34 | uint16_t e_minalloc;
|
---|
35 | uint16_t e_maxalloc;
|
---|
36 | uint16_t e_ss;
|
---|
37 | uint16_t e_sp;
|
---|
38 | uint16_t e_csum;
|
---|
39 | uint16_t e_ip;
|
---|
40 | uint16_t e_cs;
|
---|
41 | uint16_t e_lfarlc;
|
---|
42 | uint16_t e_ovno;
|
---|
43 | uint16_t e_res[4];
|
---|
44 | uint16_t e_oemid;
|
---|
45 | uint16_t e_oeminfo;
|
---|
46 | uint16_t e_res2[10];
|
---|
47 | int32_t e_lfanew;
|
---|
48 | } IMAGE_DOS_HEADER,*PIMAGE_DOS_HEADER;
|
---|
49 |
|
---|
50 | typedef struct _IMAGE_FILE_HEADER
|
---|
51 | {
|
---|
52 | uint16_t Machine;
|
---|
53 | uint16_t NumberOfSections;
|
---|
54 | uint32_t TimeDateStamp;
|
---|
55 | uint32_t PointerToSymbolTable;
|
---|
56 | uint32_t NumberOfSymbols;
|
---|
57 | uint16_t SizeOfOptionalHeader;
|
---|
58 | uint16_t Characteristics;
|
---|
59 | } IMAGE_FILE_HEADER,*PIMAGE_FILE_HEADER;
|
---|
60 |
|
---|
61 | typedef struct _IMAGE_DATA_DIRECTORY
|
---|
62 | {
|
---|
63 | uint32_t VirtualAddress;
|
---|
64 | uint32_t Size;
|
---|
65 | } IMAGE_DATA_DIRECTORY,*PIMAGE_DATA_DIRECTORY;
|
---|
66 |
|
---|
67 | # define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
|
---|
68 |
|
---|
69 | typedef struct _IMAGE_OPTIONAL_HEADER
|
---|
70 | {
|
---|
71 | uint16_t Magic;
|
---|
72 | uint8_t MajorLinkerVersion;
|
---|
73 | uint8_t MinorLinkerVersion;
|
---|
74 | uint32_t SizeOfCode;
|
---|
75 | uint32_t SizeOfInitializedData;
|
---|
76 | uint32_t SizeOfUninitializedData;
|
---|
77 | uint32_t AddressOfEntryPoint;
|
---|
78 | uint32_t BaseOfCode;
|
---|
79 | uint32_t BaseOfData;
|
---|
80 | uint32_t ImageBase;
|
---|
81 | uint32_t SectionAlignment;
|
---|
82 | uint32_t FileAlignment;
|
---|
83 | uint16_t MajorOperatingSystemVersion;
|
---|
84 | uint16_t MinorOperatingSystemVersion;
|
---|
85 | uint16_t MajorImageVersion;
|
---|
86 | uint16_t MinorImageVersion;
|
---|
87 | uint16_t MajorSubsystemVersion;
|
---|
88 | uint16_t MinorSubsystemVersion;
|
---|
89 | uint32_t Win32VersionValue;
|
---|
90 | uint32_t SizeOfImage;
|
---|
91 | uint32_t SizeOfHeaders;
|
---|
92 | uint32_t CheckSum;
|
---|
93 | uint16_t Subsystem;
|
---|
94 | uint16_t DllCharacteristics;
|
---|
95 | uint32_t SizeOfStackReserve;
|
---|
96 | uint32_t SizeOfStackCommit;
|
---|
97 | uint32_t SizeOfHeapReserve;
|
---|
98 | uint32_t SizeOfHeapCommit;
|
---|
99 | uint32_t LoaderFlags;
|
---|
100 | uint32_t NumberOfRvaAndSizes;
|
---|
101 | IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
|
---|
102 | } IMAGE_OPTIONAL_HEADER32,*PIMAGE_OPTIONAL_HEADER32;
|
---|
103 |
|
---|
104 | typedef struct _IMAGE_NT_HEADERS
|
---|
105 | {
|
---|
106 | uint32_t Signature;
|
---|
107 | IMAGE_FILE_HEADER FileHeader;
|
---|
108 | IMAGE_OPTIONAL_HEADER32 OptionalHeader;
|
---|
109 | } IMAGE_NT_HEADERS32,*PIMAGE_NT_HEADERS32;
|
---|
110 |
|
---|
111 | # define IMAGE_NT_SIGNATURE 0x00004550
|
---|
112 | # define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
|
---|
113 | # define IMAGE_FILE_MACHINE_I386 0x014c
|
---|
114 |
|
---|
115 | #endif
|
---|
116 | #include <stdio.h>
|
---|
117 | #include <string.h>
|
---|
118 |
|
---|
119 |
|
---|
120 | /** @todo Rewrite this so it can take options and print out error messages. */
|
---|
121 | int main(int argc, char **argv)
|
---|
122 | {
|
---|
123 | if (argc != 2)
|
---|
124 | return 30;
|
---|
125 | FILE *pFile = fopen(argv[1], "r+b");
|
---|
126 | if (!pFile)
|
---|
127 | return 1;
|
---|
128 | IMAGE_DOS_HEADER MzHdr;
|
---|
129 | if (fread(&MzHdr, sizeof(MzHdr), 1, pFile) != 1)
|
---|
130 | return 2;
|
---|
131 |
|
---|
132 | if (fseek(pFile, MzHdr.e_lfanew, SEEK_SET) != 0)
|
---|
133 | return 3;
|
---|
134 |
|
---|
135 | IMAGE_NT_HEADERS32 NtHdrs;
|
---|
136 | if (fread(&NtHdrs, sizeof(NtHdrs), 1, pFile) != 1)
|
---|
137 | return 4;
|
---|
138 | if (NtHdrs.Signature != IMAGE_NT_SIGNATURE)
|
---|
139 | return 5;
|
---|
140 | if (NtHdrs.FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
|
---|
141 | return 6;
|
---|
142 | if (NtHdrs.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
|
---|
143 | return 7;
|
---|
144 |
|
---|
145 | if (NtHdrs.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
|
---|
146 | return 7;
|
---|
147 |
|
---|
148 | IMAGE_NT_HEADERS32 NtHdrsNew = NtHdrs;
|
---|
149 | if (NtHdrsNew.OptionalHeader.MajorOperatingSystemVersion > 4)
|
---|
150 | {
|
---|
151 | NtHdrsNew.OptionalHeader.MajorOperatingSystemVersion = 4;
|
---|
152 | NtHdrsNew.OptionalHeader.MinorOperatingSystemVersion = 0;
|
---|
153 | }
|
---|
154 | if (NtHdrsNew.OptionalHeader.MajorSubsystemVersion > 4)
|
---|
155 | {
|
---|
156 | NtHdrsNew.OptionalHeader.MajorSubsystemVersion = 4;
|
---|
157 | NtHdrsNew.OptionalHeader.MinorSubsystemVersion = 0;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (memcmp(&NtHdrsNew, &NtHdrs, sizeof(NtHdrs)))
|
---|
161 | {
|
---|
162 | /** @todo calc checksum. */
|
---|
163 | NtHdrsNew.OptionalHeader.CheckSum = 0;
|
---|
164 |
|
---|
165 | if (fseek(pFile, MzHdr.e_lfanew, SEEK_SET) != 0)
|
---|
166 | return 10;
|
---|
167 | if (fwrite(&NtHdrsNew, sizeof(NtHdrsNew), 1, pFile) != 1)
|
---|
168 | return 11;
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (fclose(pFile) != 0)
|
---|
172 | return 29;
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|