| 1 | /* $Id: xx2lxmain.cpp,v 1.1 2001-03-11 16:37:17 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * Xx2Lx main program. (Ring 3 only!)
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 1999-2001 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 "elf.h"
|
|---|
| 32 | #include <malloc.h>
|
|---|
| 33 | #include <string.h>
|
|---|
| 34 | #include <stdlib.h>
|
|---|
| 35 | #include "OS2Krnl.h"
|
|---|
| 36 | #include "modulebase.h"
|
|---|
| 37 | #include "pe2lx.h"
|
|---|
| 38 | #include "elf2lx.h"
|
|---|
| 39 | #include "options.h"
|
|---|
| 40 | #include <stdio.h>
|
|---|
| 41 | #include <versionos2.h>
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | /*******************************************************************************
|
|---|
| 45 | * Global Variables *
|
|---|
| 46 | *******************************************************************************/
|
|---|
| 47 | char szBackupXxFilename[CCHMAXPATH]; /* too save stack/heap */
|
|---|
| 48 | struct options options = DEFAULT_OPTION_ASSIGMENTS;
|
|---|
| 49 |
|
|---|
| 50 | /*******************************************************************************
|
|---|
| 51 | * Internal Functions *
|
|---|
| 52 | *******************************************************************************/
|
|---|
| 53 | static void syntax();
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Main function of the Pe2Lx utillity.
|
|---|
| 58 | * @returns 0 on success.
|
|---|
| 59 | * 1 Help.
|
|---|
| 60 | * 2 Syntax error: Invalid argument, '<arg>'.
|
|---|
| 61 | * 3 Syntax error: Too many filenames.
|
|---|
| 62 | * 4 Syntax error: No Xx-file specified.
|
|---|
| 63 | * 80 Fatal error: Can't find Xx-file, <filename>.
|
|---|
| 64 | * 81 Fatal error: Failed to rename the Xx-file, <from> -> <to>
|
|---|
| 65 | * 82 Fatal error: Failed to open Xx-file, <filename>. rc=<rc>
|
|---|
| 66 | * 83 Fatal error: Failed to read file signature. rc=<rc from DosRead(..)>
|
|---|
| 67 | * 84 Fatal error: Failed to convertert the file. rc=<rc from init(..)>
|
|---|
| 68 | * 85 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 pszXxFilename = NULL;
|
|---|
| 79 | PCSZ pszLxFilename = 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 | default:
|
|---|
| 132 | printf("Syntax error: Invalid argument, '%s'\n", argv[argi]);
|
|---|
| 133 | syntax();
|
|---|
| 134 | return 2;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | else
|
|---|
| 138 | {
|
|---|
| 139 | if (pszXxFilename == NULL)
|
|---|
| 140 | {
|
|---|
| 141 | pszXxFilename = argv[argi];
|
|---|
| 142 | /* check if exists */
|
|---|
| 143 | rc = DosQueryPathInfo(pszXxFilename,FIL_QUERYFULLNAME,
|
|---|
| 144 | &szBackupXxFilename[0], sizeof(szBackupXxFilename));
|
|---|
| 145 | if (rc != NO_ERROR)
|
|---|
| 146 | {
|
|---|
| 147 | printf("Fatal error: Can't find Xx-file, '%s'.\n", pszXxFilename);
|
|---|
| 148 | return 80;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | else if (pszLxFilename == NULL)
|
|---|
| 152 | pszLxFilename = argv[argi];
|
|---|
| 153 | else
|
|---|
| 154 | {
|
|---|
| 155 | printf("Syntax error: Too many filenames!\n");
|
|---|
| 156 | syntax();
|
|---|
| 157 | return 3;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | } /* for */
|
|---|
| 161 |
|
|---|
| 162 | /* check if enough arguments */
|
|---|
| 163 | if (pszXxFilename == NULL)
|
|---|
| 164 | {
|
|---|
| 165 | printf("Syntax error: No Xx-file specified.\n\n");
|
|---|
| 166 | syntax();
|
|---|
| 167 | return 4;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /* rename files? */
|
|---|
| 171 | if (pszLxFilename == NULL)
|
|---|
| 172 | {
|
|---|
| 173 | char *pszExt = strrchr(pszXxFilename, '.');
|
|---|
| 174 | if (pszExt == NULL)
|
|---|
| 175 | {
|
|---|
| 176 | //printf("warning: Xx-file don't have an extention\n");
|
|---|
| 177 | strcpy(szBackupXxFilename, pszXxFilename);
|
|---|
| 178 | strcat(szBackupXxFilename, "wbk");
|
|---|
| 179 | }
|
|---|
| 180 | else
|
|---|
| 181 | {
|
|---|
| 182 | memset(szBackupXxFilename, 0, sizeof(szBackupXxFilename));
|
|---|
| 183 | if (stricmp(pszExt + 1, "exe") == 0)
|
|---|
| 184 | {
|
|---|
| 185 | strncpy(szBackupXxFilename, pszXxFilename, pszExt - pszXxFilename + 1);
|
|---|
| 186 | strcat(szBackupXxFilename, "exf");
|
|---|
| 187 | }
|
|---|
| 188 | else
|
|---|
| 189 | {
|
|---|
| 190 | strncpy(szBackupXxFilename, pszXxFilename, pszExt - pszXxFilename + 3);
|
|---|
| 191 | strcat(szBackupXxFilename, "k");
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 | rc = DosMove(pszXxFilename, szBackupXxFilename);
|
|---|
| 195 | if (rc != NO_ERROR)
|
|---|
| 196 | {
|
|---|
| 197 | printf("Fatal error: Failed to rename the Xx-file, %s -> %s\n",
|
|---|
| 198 | pszXxFilename, szBackupXxFilename);
|
|---|
| 199 | return 81;
|
|---|
| 200 | }
|
|---|
| 201 | printInf(("Backuped %s to %s\n", pszXxFilename, szBackupXxFilename));
|
|---|
| 202 |
|
|---|
| 203 | /* switch name */
|
|---|
| 204 | pszLxFilename = pszXxFilename;
|
|---|
| 205 | pszXxFilename = szBackupXxFilename;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /*
|
|---|
| 209 | * Open input file
|
|---|
| 210 | */
|
|---|
| 211 | HFILE hFileXx;
|
|---|
| 212 |
|
|---|
| 213 | rc = DosOpen(pszXxFilename, &hFileXx, &ulAction, 0UL,
|
|---|
| 214 | FILE_NORMAL,
|
|---|
| 215 | OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
|
|---|
| 216 | OPEN_FLAGS_RANDOMSEQUENTIAL | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
|
|---|
| 217 | NULL);
|
|---|
| 218 | if (rc == NO_ERROR)
|
|---|
| 219 | {
|
|---|
| 220 | ULONG ulSignature; /* Signature (four first bytes) of the file */
|
|---|
| 221 | ULONG cbRead; /* Signature bytes read - ignored */
|
|---|
| 222 |
|
|---|
| 223 | /*
|
|---|
| 224 | * Read signature.
|
|---|
| 225 | * If Elf signature Then create elf2lx object.
|
|---|
| 226 | * Else create pe2lx object.
|
|---|
| 227 | * Init the module.
|
|---|
| 228 | * Write LX File.
|
|---|
| 229 | * Dump virtual LX file
|
|---|
| 230 | */
|
|---|
| 231 | rc = DosRead(hFileXx, &ulSignature, sizeof(ulSignature), &cbRead);
|
|---|
| 232 | if (rc == NO_ERROR)
|
|---|
| 233 | {
|
|---|
| 234 | ModuleBase *pModule; /* Pointer to module. */
|
|---|
| 235 |
|
|---|
| 236 | if (ulSignature == ELFMAGICLSB)
|
|---|
| 237 | pModule = new Elf2Lx(hFileXx);
|
|---|
| 238 | else
|
|---|
| 239 | pModule = new Pe2Lx(hFileXx);
|
|---|
| 240 |
|
|---|
| 241 | rc = pModule->init(pszLxFilename);
|
|---|
| 242 | if (rc == NO_ERROR)
|
|---|
| 243 | {
|
|---|
| 244 | rc = pModule->writeFile(pszLxFilename);
|
|---|
| 245 | if (rc != NO_ERROR)
|
|---|
| 246 | {
|
|---|
| 247 | printf("Fatal error: Failed to write the Lx-file. rc=%d\n", rc);
|
|---|
| 248 | rc = 85;
|
|---|
| 249 | }
|
|---|
| 250 | pModule->dumpVirtualLxFile();
|
|---|
| 251 | }
|
|---|
| 252 | else
|
|---|
| 253 | {
|
|---|
| 254 | printf("Fatal error: Failed to convert the file. rc=%d\n", rc);
|
|---|
| 255 | rc = 84;
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 | else
|
|---|
| 259 | {
|
|---|
| 260 | printf("Fatal error: Failed to read file signature. rc=%d\n", rc);
|
|---|
| 261 | rc = 83;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | DosClose(hFileXx);
|
|---|
| 265 | }
|
|---|
| 266 | else
|
|---|
| 267 | {
|
|---|
| 268 | printf("Fatal error: Failed to open Xx-file, %s. rc=%d\n",
|
|---|
| 269 | pszXxFilename, rc);
|
|---|
| 270 | rc = 82;
|
|---|
| 271 | }
|
|---|
| 272 | return (int)rc;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Display syntax for this program.
|
|---|
| 278 | * @status completely implemented.
|
|---|
| 279 | * @author knut st. osmundsen
|
|---|
| 280 | */
|
|---|
| 281 | static void syntax()
|
|---|
| 282 | {
|
|---|
| 283 | printf("Syntax: xx2lx.exe [-W<0|1|2|3>] [-1<+|-|[*]>] <XxFile> [LxFile]\n"
|
|---|
| 284 | "\n"
|
|---|
| 285 | " Global options:\n"
|
|---|
| 286 | " -W<0|1|2|3|4> Message filter level.\n"
|
|---|
| 287 | " -W0: Output only severe and unrecoverable error messages.\n"
|
|---|
| 288 | " -W1: Output error, severe and unrecoverable error messages.\n"
|
|---|
| 289 | " -W2: Output warning, error, severe and unrecoverable error\n"
|
|---|
| 290 | " messages.\n"
|
|---|
| 291 | " -W3: Output nearly all messages.\n"
|
|---|
| 292 | " -W4: Output absolutely all messages.\n"
|
|---|
| 293 | " Default: -W3\n"
|
|---|
| 294 | "\n"
|
|---|
| 295 | " Pe2Lx options:\n"
|
|---|
| 296 | " -1<+|-|[*]> All-in-one-object fix.\n"
|
|---|
| 297 | " +: Fix applied when necessary.\n"
|
|---|
| 298 | " -: Disabled. Never applied.\n"
|
|---|
| 299 | " *: Forced. Applied every time.\n"
|
|---|
| 300 | " Default: -1*\n"
|
|---|
| 301 | "\n"
|
|---|
| 302 | " XxFile Input PE .\n"
|
|---|
| 303 | " LxFile Output Lx-file. If not specified the Xx-file is\n"
|
|---|
| 304 | " renamed and the Lx-file will use the original name\n"
|
|---|
| 305 | " of the Xx-file.\n"
|
|---|
| 306 | " Xx2Lx version 0.%02d\n",
|
|---|
| 307 | PE2LX_VERSION
|
|---|
| 308 | );
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 | #if 0
|
|---|
| 314 | /**
|
|---|
| 315 | * Debug - see how much of the stack that have been used.
|
|---|
| 316 | * Padd stack, and look in the debug storage view on program end.
|
|---|
| 317 | * @param
|
|---|
| 318 | * @status
|
|---|
| 319 | * @author knut st. osmundsen
|
|---|
| 320 | */
|
|---|
| 321 | static void initStack()
|
|---|
| 322 | {
|
|---|
| 323 | PTIB pTib;
|
|---|
| 324 | PPIB pPib;
|
|---|
| 325 |
|
|---|
| 326 | DosGetInfoBlocks(&pTib, &pPib);
|
|---|
| 327 | memset((PVOID)pTib->tib_pstack, 'k', (size_t)&pTib - 0x30 - (size_t)pTib->tib_pstack);
|
|---|
| 328 | }
|
|---|
| 329 | #endif
|
|---|