Changeset 70368 in vbox for trunk/src/bldprogs
- Timestamp:
- Dec 27, 2017 5:54:10 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bldprogs/VBoxPeSetVersion.cpp
r70348 r70368 1 1 /* $Id$ */ 2 2 /** @file 3 * IPRT - Change the OS and SubSystem version to 4.0 (VS2010 trick). 3 * IPRT - Change the OS and SubSystem version to value suitable for NT v3.1. 4 * 5 * Also make sure the IAT is writable, since NT v3.1 expects this. These are 6 * tricks necessary to make binaries created by newer Visual C++ linkers work 7 * on ancient NT version like W2K, NT4 and NT 3.x. 4 8 */ 5 9 … … 28 32 29 33 /********************************************************************************************************************************* 34 * Defined Constants And Macros * 35 *********************************************************************************************************************************/ 36 #define MK_VER(a_uHi, a_uLo) ( ((a_uHi) << 8) | (a_uLo)) 37 38 39 /********************************************************************************************************************************* 30 40 * Global Variables * 31 41 *********************************************************************************************************************************/ 32 42 static const char *g_pszFilename; 43 static unsigned g_cVerbosity = 0; 33 44 34 45 … … 45 56 46 57 47 static int UpdateFile(FILE *pFile, bool fNt31, PIMAGE_SECTION_HEADER *ppaShdr) 58 static void Info(unsigned iLevel, const char *pszFormat, ...) 59 { 60 if (iLevel <= g_cVerbosity) 61 { 62 va_list va; 63 va_start(va, pszFormat); 64 char szTmp[1024]; 65 _vsnprintf(szTmp, sizeof(szTmp), pszFormat, va); 66 va_end(va); 67 fprintf(stderr, "VBoxPeSetVersion: %s: info: %s\n", g_pszFilename, szTmp); 68 } 69 } 70 71 72 static int UpdateFile(FILE *pFile, unsigned uNtVersion, PIMAGE_SECTION_HEADER *ppaShdr) 48 73 { 49 74 /* … … 82 107 */ 83 108 IMAGE_NT_HEADERS32 NtHdrsNew = NtHdrs; 84 if (fNt31) 109 NtHdrsNew.OptionalHeader.MajorOperatingSystemVersion = uNtVersion >> 8; 110 NtHdrsNew.OptionalHeader.MinorOperatingSystemVersion = uNtVersion & 0xff; 111 NtHdrsNew.OptionalHeader.MajorSubsystemVersion = uNtVersion >> 8; 112 NtHdrsNew.OptionalHeader.MinorSubsystemVersion = uNtVersion & 0xff; 113 114 if (uNtVersion <= MK_VER(3, 50)) 85 115 { 86 116 NtHdrsNew.OptionalHeader.MajorOperatingSystemVersion = 1; 87 117 NtHdrsNew.OptionalHeader.MinorOperatingSystemVersion = 0; 88 89 NtHdrsNew.OptionalHeader.MajorSubsystemVersion = 3;90 NtHdrsNew.OptionalHeader.MinorSubsystemVersion = 10;91 }92 else93 {94 NtHdrsNew.OptionalHeader.MajorOperatingSystemVersion = 4;95 NtHdrsNew.OptionalHeader.MinorOperatingSystemVersion = 0;96 97 NtHdrsNew.OptionalHeader.MajorSubsystemVersion = 4;98 NtHdrsNew.OptionalHeader.MinorSubsystemVersion = 0;99 118 } 100 119 … … 103 122 /** @todo calc checksum. */ 104 123 NtHdrsNew.OptionalHeader.CheckSum = 0; 124 125 if ( NtHdrsNew.OptionalHeader.MajorOperatingSystemVersion != NtHdrs.OptionalHeader.MajorOperatingSystemVersion 126 || NtHdrsNew.OptionalHeader.MinorOperatingSystemVersion != NtHdrs.OptionalHeader.MinorOperatingSystemVersion) 127 Info(1,"OperatingSystemVersion %u.%u -> %u.%u", 128 NtHdrs.OptionalHeader.MajorOperatingSystemVersion, NtHdrs.OptionalHeader.MinorOperatingSystemVersion, 129 NtHdrsNew.OptionalHeader.MajorOperatingSystemVersion, NtHdrsNew.OptionalHeader.MinorOperatingSystemVersion); 130 if ( NtHdrsNew.OptionalHeader.MajorSubsystemVersion != NtHdrs.OptionalHeader.MajorSubsystemVersion 131 || NtHdrsNew.OptionalHeader.MinorSubsystemVersion != NtHdrs.OptionalHeader.MinorSubsystemVersion) 132 Info(1,"SubsystemVersion %u.%u -> %u.%u", 133 NtHdrs.OptionalHeader.MajorSubsystemVersion, NtHdrs.OptionalHeader.MinorSubsystemVersion, 134 NtHdrsNew.OptionalHeader.MajorSubsystemVersion, NtHdrsNew.OptionalHeader.MinorSubsystemVersion); 105 135 106 136 if (fseek(pFile, offNtHdrs, SEEK_SET) != 0) … … 113 143 * Make the IAT writable for NT 3.1. 114 144 */ 115 if ( fNt31145 if ( uNtVersion <= MK_VER(3, 10) 116 146 && NtHdrsNew.FileHeader.NumberOfSections > 0 117 147 && NtHdrsNew.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size > 0) … … 160 190 161 191 192 static int Usage(FILE *pOutput) 193 { 194 fprintf(pOutput, 195 "Usage: VBoxPeSetVersion [options] <PE-image>\n" 196 "Options:\n" 197 " -v, --verbose\n" 198 " Increases verbosity.\n" 199 " -q, --quiet\n" 200 " Quiet operation (default).\n" 201 " --nt31, --nt350, --nt351, --nt4, --w2k, --xp, --w2k3, --vista,\n" 202 " --w7, --w8, --w81, --w10\n" 203 " Which version to set. Default is --nt31.\n" 204 " --nt4\n" 205 " Set versions to NT 4.0\n" 206 " --nt4\n" 207 " Set versions to NT 4.0\n" 208 ); 209 return RTEXITCODE_SYNTAX; 210 } 211 212 162 213 /** @todo Rewrite this so it can take options and print out error messages. */ 163 214 int main(int argc, char **argv) … … 165 216 /* 166 217 * Parse arguments. 167 */ 168 if (argc != 2) 169 { 170 fprintf(stderr, "VBoxPeSetVersion: syntax error: Expected a only single argument!\n"); 218 * This stucks 219 */ 220 unsigned uNtVersion = 31; 221 const char *pszFilename = NULL; 222 bool fAcceptOptions = true; 223 for (int i = 1; i < argc; i++) 224 { 225 const char *psz = argv[i]; 226 if (fAcceptOptions && *psz == '-') 227 { 228 char ch = psz[1]; 229 psz += 2; 230 if (ch == '-') 231 { 232 if (!*psz) 233 { 234 fAcceptOptions = false; 235 continue; 236 } 237 238 if (strcmp(psz, "verbose") == 0) 239 ch = 'v'; 240 else if (strcmp(psz, "quiet") == 0) 241 ch = 'q'; 242 else if (strcmp(psz, "help") == 0) 243 ch = 'h'; 244 else if (strcmp(psz, "version") == 0) 245 ch = 'V'; 246 else 247 { 248 if (strcmp(psz, "nt31") == 0) 249 uNtVersion = MK_VER(3, 10); 250 else if (strcmp(psz, "nt350") == 0) 251 uNtVersion = MK_VER(3, 50); 252 else if (strcmp(psz, "nt351") == 0) 253 uNtVersion = MK_VER(3, 51); 254 else if (strcmp(psz, "nt4") == 0) 255 uNtVersion = MK_VER(4, 0); 256 else if (strcmp(psz, "w2k") == 0) 257 uNtVersion = MK_VER(5,0); 258 else if (strcmp(psz, "xp") == 0) 259 uNtVersion = MK_VER(5,1); 260 else if (strcmp(psz, "w2k3") == 0) 261 uNtVersion = MK_VER(5,2); 262 else if (strcmp(psz, "vista") == 0) 263 uNtVersion = MK_VER(6,0); 264 else if (strcmp(psz, "w7") == 0) 265 uNtVersion = MK_VER(6,1); 266 else if (strcmp(psz, "w8") == 0) 267 uNtVersion = MK_VER(6,2); 268 else if (strcmp(psz, "w81") == 0) 269 uNtVersion = MK_VER(6,3); 270 else if (strcmp(psz, "w10") == 0) 271 uNtVersion = MK_VER(10,0); 272 else 273 { 274 fprintf(stderr, "VBoxPeSetVersion: syntax error: Unknown option: --%s\n", psz); 275 return RTEXITCODE_SYNTAX; 276 } 277 continue; 278 } 279 psz = " "; 280 } 281 do 282 { 283 switch (ch) 284 { 285 case 'q': 286 g_cVerbosity = 0; 287 break; 288 case 'v': 289 g_cVerbosity++; 290 break; 291 case 'V': 292 printf("2.0\n"); 293 return RTEXITCODE_SUCCESS; 294 case 'h': 295 Usage(stdout); 296 return RTEXITCODE_SUCCESS; 297 default: 298 fprintf(stderr, "VBoxPeSetVersion: syntax error: Unknown option: -%c\n", ch ? ch : ' '); 299 return RTEXITCODE_SYNTAX; 300 } 301 } while ((ch = *psz++) != '\0'); 302 303 } 304 else if (!pszFilename) 305 pszFilename = psz; 306 else 307 { 308 fprintf(stderr, "VBoxPeSetVersion: syntax error: More than one PE-image specified!\n"); 309 return RTEXITCODE_SYNTAX; 310 } 311 } 312 313 if (!pszFilename) 314 { 315 fprintf(stderr, "VBoxPeSetVersion: syntax error: No PE-image specified!\n"); 171 316 return RTEXITCODE_SYNTAX; 172 317 } 173 const char *pszFilename = argv[1];174 318 g_pszFilename = pszFilename; 175 319 … … 182 326 { 183 327 PIMAGE_SECTION_HEADER paShdrs = NULL; 184 rcExit = UpdateFile(pFile, true /*fNt31*/, &paShdrs);328 rcExit = UpdateFile(pFile, uNtVersion, &paShdrs); 185 329 if (paShdrs) 186 330 free(paShdrs);
Note:
See TracChangeset
for help on using the changeset viewer.