| 1 | /* $Id: pe2lxmain.cpp,v 1.8 2001-09-28 07:43:03 sandervl Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * Pe2Lx main program. (Ring 3 only!)
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 1999 knut st. osmundsen
|
|---|
| 6 | *
|
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | /*******************************************************************************
|
|---|
| 12 | * Defined Constants And Macros *
|
|---|
| 13 | *******************************************************************************/
|
|---|
| 14 | #define FOR_EXEHDR 1
|
|---|
| 15 | #define INCL_BASE
|
|---|
| 16 | #define INCL_DOSFILEMGR
|
|---|
| 17 | #define INCL_DOSERRORS
|
|---|
| 18 | #define INCL_OS2KRNL_LDR
|
|---|
| 19 |
|
|---|
| 20 | #define DATA16_GLOBAL
|
|---|
| 21 | #define OUTPUT_COM2 0x2f8
|
|---|
| 22 |
|
|---|
| 23 | /*******************************************************************************
|
|---|
| 24 | * Header Files *
|
|---|
| 25 | *******************************************************************************/
|
|---|
| 26 | #include <os2.h>
|
|---|
| 27 | #include <peexe.h>
|
|---|
| 28 | #include <neexe.h>
|
|---|
| 29 | #include <newexe.h>
|
|---|
| 30 | #include <exe386.h>
|
|---|
| 31 | #include <malloc.h>
|
|---|
| 32 | #include <string.h>
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| 34 | #include <io.h>
|
|---|
| 35 | #include <fcntl.h>
|
|---|
| 36 | #include <sys/stat.h>
|
|---|
| 37 | #include "OS2Krnl.h"
|
|---|
| 38 | #include "modulebase.h"
|
|---|
| 39 | #include "pe2lx.h"
|
|---|
| 40 | #include "options.h"
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <versionos2.h>
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | /*******************************************************************************
|
|---|
| 46 | * Global Variables *
|
|---|
| 47 | *******************************************************************************/
|
|---|
| 48 | char szBackupWin32Filename[CCHMAXPATH]; /* too save stack/heap */
|
|---|
| 49 | struct options options = DEFAULT_OPTION_ASSIGMENTS;
|
|---|
| 50 |
|
|---|
| 51 | /*******************************************************************************
|
|---|
| 52 | * Internal Functions *
|
|---|
| 53 | *******************************************************************************/
|
|---|
| 54 | static void syntax();
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Main function of the Pe2Lx utillity.
|
|---|
| 59 | * @returns 0 on success.
|
|---|
| 60 | * 1 Help.
|
|---|
| 61 | * 2 Syntax error: Invalid argument, '<arg>'.
|
|---|
| 62 | * 3 Syntax error: Too many filenames.
|
|---|
| 63 | * 4 Syntax error: No Win32-file specified.
|
|---|
| 64 | * 80 Fatal error: Can't find Win32-file, <filename>.
|
|---|
| 65 | * 81 Fatal error: Failed to rename the Win32-file, <from> -> <to>
|
|---|
| 66 | * 82 Fatal error: Failed to open Win32-file, <filename>. rc=<rc>
|
|---|
| 67 | * 83 Fatal error: Failed to convertert the file. rc=<rc from init(..)>
|
|---|
| 68 | * 84 Fatal error: Failed to write the Lx-file. rc=<rc from writeFile(..)>
|
|---|
| 69 | * @param argc Count of arguments.
|
|---|
| 70 | * @param argv Array of argument pointers. argc entries.
|
|---|
| 71 | * @status completely implemented.
|
|---|
| 72 | * @author knut st. osmundsen
|
|---|
| 73 | */
|
|---|
| 74 | int main(int argc, char **argv)
|
|---|
| 75 | {
|
|---|
| 76 | APIRET rc;
|
|---|
| 77 | ULONG ulAction = 0;
|
|---|
| 78 | PCSZ pszWin32Filename = NULL;
|
|---|
| 79 | PCSZ pszOdin32Filename = NULL;
|
|---|
| 80 | PCSZ psz;
|
|---|
| 81 | int argi;
|
|---|
| 82 |
|
|---|
| 83 | /* special edition for Dave Evans */
|
|---|
| 84 | options.fPEOneObject = FLAGS_PEOO_DISABLED;
|
|---|
| 85 |
|
|---|
| 86 | /* read parameters */
|
|---|
| 87 | for (argi = 1; argi < argc; argi++)
|
|---|
| 88 | {
|
|---|
| 89 | /* check if option or filname */
|
|---|
| 90 | if (argv[argi][0] == '-' || argv[argi][0] == '/')
|
|---|
| 91 | { /* option */
|
|---|
| 92 | switch (argv[argi][1])
|
|---|
| 93 | {
|
|---|
| 94 | case '1': /* All-In-One-Object fix - temporary...- -1<-|+|*> */
|
|---|
| 95 | if (argv[argi][2] == '-')
|
|---|
| 96 | options.fPEOneObject = FLAGS_PEOO_DISABLED;
|
|---|
| 97 | else if (argv[argi][2] == '+')
|
|---|
| 98 | options.fPEOneObject = FLAGS_PEOO_ENABLED;
|
|---|
| 99 | else
|
|---|
| 100 | options.fPEOneObject = FLAGS_PEOO_FORCED;
|
|---|
| 101 | break;
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | case 'h': /* syntax help */
|
|---|
| 105 | case 'H':
|
|---|
| 106 | case '?':
|
|---|
| 107 | syntax();
|
|---|
| 108 | return 1;
|
|---|
| 109 |
|
|---|
| 110 | case 'w': /* warning level */
|
|---|
| 111 | case 'W':
|
|---|
| 112 | psz = argv[argi] + (argv[argi][2] == ':' || argv[argi][2] == '=') + 2;
|
|---|
| 113 | if (*psz >= '0' && *psz <= '4' && psz[1] == '\0')
|
|---|
| 114 | {
|
|---|
| 115 | switch (*psz)
|
|---|
| 116 | {
|
|---|
| 117 | case '0': ModuleBase::ulInfoLevel = ModuleBase::Quiet; break;
|
|---|
| 118 | case '1': ModuleBase::ulInfoLevel = ModuleBase::Error; break;
|
|---|
| 119 | case '2': ModuleBase::ulInfoLevel = ModuleBase::Warning; break;
|
|---|
| 120 | case '3': ModuleBase::ulInfoLevel = ModuleBase::Info; break;
|
|---|
| 121 | case '4': ModuleBase::ulInfoLevel = ModuleBase::InfoAll; break;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | else
|
|---|
| 125 | {
|
|---|
| 126 | printf("Syntax error: Incorrect use of argument '%.2s'.\n\n", argv[argi]);
|
|---|
| 127 | return 5;
|
|---|
| 128 | }
|
|---|
| 129 | break;
|
|---|
| 130 |
|
|---|
| 131 | case 'f': //ignore internal fixups
|
|---|
| 132 | case 'F':
|
|---|
| 133 | options.fSkipFixups = TRUE;
|
|---|
| 134 | break;
|
|---|
| 135 |
|
|---|
| 136 | case 'c':
|
|---|
| 137 | case 'C': //custom odin dll name
|
|---|
| 138 | if(!hasCustomExports()) {
|
|---|
| 139 | printf("Syntax error: export table file not specified (/O).\n\n");
|
|---|
| 140 | return 5;
|
|---|
| 141 | }
|
|---|
| 142 | options.pszCustomDll = &argv[argi][3];
|
|---|
| 143 | break;
|
|---|
| 144 | case 'o':
|
|---|
| 145 | case 'O':
|
|---|
| 146 | {
|
|---|
| 147 | int fileIn = open(&argv[argi][3], O_RDONLY, S_IREAD);
|
|---|
| 148 | int sizein = (int)_filelength(fileIn);
|
|---|
| 149 |
|
|---|
| 150 | options.pszCustomExports = (PSZ)malloc(sizein+1);
|
|---|
| 151 | memset(options.pszCustomExports, 0, sizein+1);
|
|---|
| 152 | read(fileIn, options.pszCustomExports, sizein);
|
|---|
| 153 | close(fileIn);
|
|---|
| 154 | break;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | default:
|
|---|
| 158 | printf("Syntax error: Invalid argument, '%s'\n", argv[argi]);
|
|---|
| 159 | syntax();
|
|---|
| 160 | return 2;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | else
|
|---|
| 164 | {
|
|---|
| 165 | if (pszWin32Filename == NULL)
|
|---|
| 166 | {
|
|---|
| 167 | pszWin32Filename = argv[argi];
|
|---|
| 168 | /* check if exists */
|
|---|
| 169 | rc = DosQueryPathInfo(pszWin32Filename,FIL_QUERYFULLNAME,
|
|---|
| 170 | &szBackupWin32Filename[0], sizeof(szBackupWin32Filename));
|
|---|
| 171 | if (rc != NO_ERROR)
|
|---|
| 172 | {
|
|---|
| 173 | printf("Fatal error: Can't find Win32-file, '%s'.\n", pszWin32Filename);
|
|---|
| 174 | return 80;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | else if (pszOdin32Filename == NULL)
|
|---|
| 178 | pszOdin32Filename = argv[argi];
|
|---|
| 179 | else
|
|---|
| 180 | {
|
|---|
| 181 | printf("Syntax error: Too many filenames!\n");
|
|---|
| 182 | syntax();
|
|---|
| 183 | return 3;
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | } /* for */
|
|---|
| 187 |
|
|---|
| 188 | /* check if enough arguments */
|
|---|
| 189 | if (pszWin32Filename == NULL)
|
|---|
| 190 | {
|
|---|
| 191 | printf("Syntax error: No Win32-file specified.\n\n");
|
|---|
| 192 | syntax();
|
|---|
| 193 | return 4;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /* rename files? */
|
|---|
| 197 | if (pszOdin32Filename == NULL)
|
|---|
| 198 | {
|
|---|
| 199 | char *pszExt = strrchr(pszWin32Filename, '.');
|
|---|
| 200 | if (pszExt == NULL)
|
|---|
| 201 | {
|
|---|
| 202 | printf("warning: Win32-file don't have an extention\n");
|
|---|
| 203 | strcpy(szBackupWin32Filename, pszWin32Filename);
|
|---|
| 204 | strcat(szBackupWin32Filename, "wbk");
|
|---|
| 205 | }
|
|---|
| 206 | else
|
|---|
| 207 | {
|
|---|
| 208 | memset(szBackupWin32Filename, 0, sizeof(szBackupWin32Filename));
|
|---|
| 209 | if (stricmp(pszExt + 1, "exe") == 0)
|
|---|
| 210 | {
|
|---|
| 211 | strncpy(szBackupWin32Filename, pszWin32Filename, pszExt - pszWin32Filename + 1);
|
|---|
| 212 | strcat(szBackupWin32Filename, "exf");
|
|---|
| 213 | }
|
|---|
| 214 | else
|
|---|
| 215 | {
|
|---|
| 216 | strncpy(szBackupWin32Filename, pszWin32Filename, pszExt - pszWin32Filename + 3);
|
|---|
| 217 | strcat(szBackupWin32Filename, "k");
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 | rc = DosMove(pszWin32Filename, szBackupWin32Filename);
|
|---|
| 221 | if (rc != NO_ERROR)
|
|---|
| 222 | {
|
|---|
| 223 | printf("Fatal error: Failed to rename the Win32-file, %s -> %s\n",
|
|---|
| 224 | pszWin32Filename, szBackupWin32Filename);
|
|---|
| 225 | return 81;
|
|---|
| 226 | }
|
|---|
| 227 | printInf(("Backuped %s to %s\n", pszWin32Filename, szBackupWin32Filename));
|
|---|
| 228 |
|
|---|
| 229 | /* switch name */
|
|---|
| 230 | pszOdin32Filename = pszWin32Filename;
|
|---|
| 231 | pszWin32Filename = szBackupWin32Filename;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /* open input file */
|
|---|
| 235 | HFILE hFileWin32;
|
|---|
| 236 |
|
|---|
| 237 | rc = DosOpen(pszWin32Filename, &hFileWin32, &ulAction, 0UL,
|
|---|
| 238 | FILE_NORMAL,
|
|---|
| 239 | OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
|
|---|
| 240 | OPEN_FLAGS_RANDOMSEQUENTIAL | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
|
|---|
| 241 | NULL);
|
|---|
| 242 | if (rc == NO_ERROR)
|
|---|
| 243 | {
|
|---|
| 244 | Pe2Lx pe2lx(hFileWin32);
|
|---|
| 245 | rc = pe2lx.init(pszOdin32Filename);
|
|---|
| 246 | if (rc == NO_ERROR)
|
|---|
| 247 | {
|
|---|
| 248 | rc = pe2lx.writeFile(pszOdin32Filename);
|
|---|
| 249 | if (rc != NO_ERROR)
|
|---|
| 250 | {
|
|---|
| 251 | printf("Fatal error: Failed to write the Lx-file. rc=%d\n", rc);
|
|---|
| 252 | rc = 84;
|
|---|
| 253 | }
|
|---|
| 254 | pe2lx.dumpVirtualLxFile();
|
|---|
| 255 | }
|
|---|
| 256 | else
|
|---|
| 257 | {
|
|---|
| 258 | printf("Fatal error: Failed to convertert the file. rc=%d\n", rc);
|
|---|
| 259 | rc = 83;
|
|---|
| 260 | }
|
|---|
| 261 | DosClose(hFileWin32);
|
|---|
| 262 | }
|
|---|
| 263 | else
|
|---|
| 264 | {
|
|---|
| 265 | printf("Fatal error: Failed to open Win32-file, %s. rc=%d\n",
|
|---|
| 266 | pszWin32Filename, rc);
|
|---|
| 267 | rc = 82;
|
|---|
| 268 | }
|
|---|
| 269 | return (int)rc;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 | /**
|
|---|
| 274 | * Display syntax for this program.
|
|---|
| 275 | * @status completely implemented.
|
|---|
| 276 | * @author knut st. osmundsen
|
|---|
| 277 | */
|
|---|
| 278 | static void syntax()
|
|---|
| 279 | {
|
|---|
| 280 | printf("Syntax: pe2lx.exe [-W<0|1|2|3>] [-1<+|-|[*]>] <Win32File> [Odin32File]\n"
|
|---|
| 281 | "\n"
|
|---|
| 282 | " -W<0|1|2|3|4> Message filter level.\n"
|
|---|
| 283 | " -W0: Output only severe and unrecoverable error messages.\n"
|
|---|
| 284 | " -W1: Output error, severe and unrecoverable error messages.\n"
|
|---|
| 285 | " -W2: Output warning, error, severe and unrecoverable error\n"
|
|---|
| 286 | " messages.\n"
|
|---|
| 287 | " -W3: Output nearly all messages.\n"
|
|---|
| 288 | " -W4: Output absolutely all messages.\n"
|
|---|
| 289 | " Default: -W3\n"
|
|---|
| 290 | " -1<+|-|[*]> All-in-one-object fix.\n"
|
|---|
| 291 | " +: Fix applied when necessary.\n"
|
|---|
| 292 | " -: Disabled. Never applied.\n"
|
|---|
| 293 | " *: Forced. Applied every time.\n"
|
|---|
| 294 | " Default: -1*\n"
|
|---|
| 295 | " Win32File Input Win32 Exe, Dll or other Win32 PE file.\n"
|
|---|
| 296 | " Odin32File Output Odin32-file. If not specified the Win32-file is\n"
|
|---|
| 297 | " renamed and the Odin32-file will use the original name\n"
|
|---|
| 298 | " of the Win32-file.\n"
|
|---|
| 299 | " Pe2Lx version 0.%02d\n",
|
|---|
| 300 | PE2LX_VERSION
|
|---|
| 301 | );
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 | #if 0
|
|---|
| 307 | /**
|
|---|
| 308 | * Debug - see how much of the stack that have been used.
|
|---|
| 309 | * Padd stack, and look in the debug storage view on program end.
|
|---|
| 310 | * @param
|
|---|
| 311 | * @status
|
|---|
| 312 | * @author knut st. osmundsen
|
|---|
| 313 | */
|
|---|
| 314 | static void initStack()
|
|---|
| 315 | {
|
|---|
| 316 | PTIB pTib;
|
|---|
| 317 | PPIB pPib;
|
|---|
| 318 |
|
|---|
| 319 | DosGetInfoBlocks(&pTib, &pPib);
|
|---|
| 320 | memset((PVOID)pTib->tib_pstack, 'k', (size_t)&pTib - 0x30 - (size_t)pTib->tib_pstack);
|
|---|
| 321 | }
|
|---|
| 322 | #endif
|
|---|