| 1 | /* $Id: winexepeldr.cpp,v 1.15 2001-02-24 12:59:44 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 PE loader Exe class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 | #define INCL_DOSFILEMGR          /* File Manager values      */ | 
|---|
| 13 | #define INCL_DOSERRORS           /* DOS Error values         */ | 
|---|
| 14 | #define INCL_DOSPROCESS          /* DOS Process values       */ | 
|---|
| 15 | #define INCL_DOSMISC             /* DOS Miscellanous values  */ | 
|---|
| 16 | #define INCL_WIN | 
|---|
| 17 | #include <os2wrap.h>    //Odin32 OS/2 api wrappers | 
|---|
| 18 | #include <stdio.h> | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 | #include <stdlib.h> | 
|---|
| 21 | #include <iostream.h> | 
|---|
| 22 | #include <fstream.h> | 
|---|
| 23 | #include <misc.h> | 
|---|
| 24 | #include <win32type.h> | 
|---|
| 25 | #include <win32api.h> | 
|---|
| 26 | #include <winexepeldr.h> | 
|---|
| 27 | #include <wprocess.h> | 
|---|
| 28 | #include <pefile.h> | 
|---|
| 29 |  | 
|---|
| 30 | #include "conwin.h"          // Windows Header for console only | 
|---|
| 31 | #include "console.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "exceptions.h" | 
|---|
| 34 | #include "exceptutil.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "cio.h" | 
|---|
| 37 | #include "oslibmisc.h" | 
|---|
| 38 |  | 
|---|
| 39 | #define DBG_LOCALLOG    DBG_winexepeldr | 
|---|
| 40 | #include "dbglocal.h" | 
|---|
| 41 |  | 
|---|
| 42 | extern char szErrorTitle[]; | 
|---|
| 43 | extern char szErrorModule[]; | 
|---|
| 44 |  | 
|---|
| 45 | BOOL fPeLoader = FALSE; | 
|---|
| 46 |  | 
|---|
| 47 | //****************************************************************************** | 
|---|
| 48 | //Called by ring 3 pe loader to create win32 executable | 
|---|
| 49 | //PE.EXE command line options: | 
|---|
| 50 | //   /OPT:[x1=y,x2=z,..] | 
|---|
| 51 | //   x = CURDIR    -> set current directory to y | 
|---|
| 52 | //   (not other options available at this time) | 
|---|
| 53 | //****************************************************************************** | 
|---|
| 54 | BOOL WIN32API CreateWin32PeLdrExe(char *szFileName, char *szCmdLine, | 
|---|
| 55 | char *peoptions, | 
|---|
| 56 | ULONG reservedMem, BOOL fConsoleApp, | 
|---|
| 57 | BOOL  fVioConsole) | 
|---|
| 58 | { | 
|---|
| 59 | APIRET  rc; | 
|---|
| 60 | PPIB   ppib; | 
|---|
| 61 | PTIB   ptib; | 
|---|
| 62 | WINEXCEPTION_FRAME exceptFrame; | 
|---|
| 63 | Win32PeLdrExe *WinExe; | 
|---|
| 64 | char   *szFullCmdLine; | 
|---|
| 65 |  | 
|---|
| 66 | fPeLoader = TRUE; | 
|---|
| 67 |  | 
|---|
| 68 | WinExe = new Win32PeLdrExe(szFileName, fConsoleApp); | 
|---|
| 69 |  | 
|---|
| 70 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 71 | if(rc) { | 
|---|
| 72 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 73 | delete WinExe; | 
|---|
| 74 | return FALSE; | 
|---|
| 75 | } | 
|---|
| 76 | //Handle special pe cmd line options here (/OPT:[x1=y,x2=z,..]) | 
|---|
| 77 | if(peoptions) { | 
|---|
| 78 | char *option; | 
|---|
| 79 |  | 
|---|
| 80 | option = strchr(peoptions, '['); | 
|---|
| 81 | if(option) { | 
|---|
| 82 | option++; | 
|---|
| 83 | option = strstr(option, "CURDIR="); | 
|---|
| 84 | if(option) { | 
|---|
| 85 | char *curdir, *tmp; | 
|---|
| 86 | int   curdirlength; | 
|---|
| 87 |  | 
|---|
| 88 | option += 7; | 
|---|
| 89 | tmp = option; | 
|---|
| 90 | while(*tmp != ']' && *tmp != ',' && *tmp != 0) { | 
|---|
| 91 | tmp++; | 
|---|
| 92 | } | 
|---|
| 93 | curdirlength = (int)(tmp-option); | 
|---|
| 94 | curdir = (char *)malloc(curdirlength+1); | 
|---|
| 95 | memcpy(curdir, option, curdirlength); | 
|---|
| 96 | curdir[curdirlength] = 0; | 
|---|
| 97 | SetCurrentDirectoryA((LPCSTR)curdir); | 
|---|
| 98 | free(curdir); | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
| 102 | //exe length + space + (possibly) 2x'"' + cmd line length + 0 terminator | 
|---|
| 103 | szFullCmdLine = (char *)malloc(strlen(szFileName) + 3 + strlen(szCmdLine) + 1); | 
|---|
| 104 | //Enclose executable name in quotes if it (or it's directory) contains spaces | 
|---|
| 105 | if(strchr(szFileName, ' ') != NULL) { | 
|---|
| 106 | sprintf(szFullCmdLine, "\"%s\"", szFileName); | 
|---|
| 107 | } | 
|---|
| 108 | else  strcpy(szFullCmdLine, szFileName); | 
|---|
| 109 | strcat(szFullCmdLine, " "); | 
|---|
| 110 | strcat(szFullCmdLine, szCmdLine); | 
|---|
| 111 | InitCommandLine(szFullCmdLine); | 
|---|
| 112 | dprintf(("Cmd line: %s", szFullCmdLine)); | 
|---|
| 113 | free(szFullCmdLine); | 
|---|
| 114 |  | 
|---|
| 115 | if(getenv("WIN32_IOPL2")) { | 
|---|
| 116 | io_init1(); | 
|---|
| 117 | } | 
|---|
| 118 | //Init console before loading executable as dlls might want to print | 
|---|
| 119 | //something on the console while being loaded | 
|---|
| 120 | if(WinExe->isConsoleApp()) | 
|---|
| 121 | { | 
|---|
| 122 | dprintf(("Console application!\n")); | 
|---|
| 123 |  | 
|---|
| 124 | APIRET rc = iConsoleInit(fVioConsole);                /* initialize console subsystem */ | 
|---|
| 125 | if (rc != NO_ERROR)                                  /* check for errors */ | 
|---|
| 126 | dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc)); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | OS2SetExceptionHandler(&exceptFrame); | 
|---|
| 130 | if(WinExe->init(reservedMem) == FALSE) | 
|---|
| 131 | { | 
|---|
| 132 | if(szErrorModule[0] != 0) { | 
|---|
| 133 | char szErrorMsg[128]; | 
|---|
| 134 |  | 
|---|
| 135 | sprintf(szErrorMsg, "Can't execute %s due to bad or missing %s", OSLibStripPath(szFileName), szErrorModule); | 
|---|
| 136 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 137 | } | 
|---|
| 138 | delete WinExe; | 
|---|
| 139 | OS2UnsetExceptionHandler(&exceptFrame); | 
|---|
| 140 | return FALSE; | 
|---|
| 141 | } | 
|---|
| 142 | OS2UnsetExceptionHandler(&exceptFrame); | 
|---|
| 143 |  | 
|---|
| 144 | WinExe->start(); | 
|---|
| 145 |  | 
|---|
| 146 | delete WinExe; | 
|---|
| 147 |  | 
|---|
| 148 | return TRUE; | 
|---|
| 149 | } | 
|---|
| 150 | //****************************************************************************** | 
|---|
| 151 | //****************************************************************************** | 
|---|
| 152 | Win32PeLdrExe::Win32PeLdrExe(char *szFileName, BOOL fConsoleApp) : | 
|---|
| 153 | Win32ImageBase(-1), | 
|---|
| 154 | Win32ExeBase(-1), | 
|---|
| 155 | Win32PeLdrImage(szFileName, TRUE) | 
|---|
| 156 | { | 
|---|
| 157 | dprintf(("Win32PeLdrExe ctor: %s", szFileName)); | 
|---|
| 158 | this->fConsoleApp = fConsoleApp; | 
|---|
| 159 |  | 
|---|
| 160 | //SvL: set temporary full path here as console init needs it | 
|---|
| 161 | setFullPath(szFileName); | 
|---|
| 162 | } | 
|---|
| 163 | //****************************************************************************** | 
|---|
| 164 | //****************************************************************************** | 
|---|
| 165 | Win32PeLdrExe::~Win32PeLdrExe() | 
|---|
| 166 | { | 
|---|
| 167 | fExitProcess = TRUE; | 
|---|
| 168 | } | 
|---|
| 169 | //****************************************************************************** | 
|---|
| 170 | //****************************************************************************** | 
|---|
| 171 | BOOL Win32PeLdrExe::init(ULONG reservedMem) | 
|---|
| 172 | { | 
|---|
| 173 | BOOL rc; | 
|---|
| 174 |  | 
|---|
| 175 | rc = Win32PeLdrImage::init(reservedMem); | 
|---|
| 176 | return rc; | 
|---|
| 177 | } | 
|---|
| 178 | //****************************************************************************** | 
|---|
| 179 | //****************************************************************************** | 
|---|