| 1 | /* $Id: ModuleBase.cpp,v 1.6 2000-12-11 06:53:53 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * ModuleBase - Implementetation. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 1999-2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 6 | * | 
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 8 | * | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 | /******************************************************************************* | 
|---|
| 12 | *   Defined Constants And Macros                                               * | 
|---|
| 13 | *******************************************************************************/ | 
|---|
| 14 | #define INCL_DOSERRORS                  /* DOS Error codes. */ | 
|---|
| 15 | #ifdef RING0 | 
|---|
| 16 | #define INCL_NOAPI                  /* RING0: No apis. */ | 
|---|
| 17 | #else /*RING3*/ | 
|---|
| 18 | #define INCL_DOSFILEMGR             /* RING3: DOS File api. */ | 
|---|
| 19 | #endif | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | /******************************************************************************* | 
|---|
| 23 | *   Header Files                                                               * | 
|---|
| 24 | *******************************************************************************/ | 
|---|
| 25 | #include <os2.h>                        /* OS/2 header file. */ | 
|---|
| 26 |  | 
|---|
| 27 | #include "devSegDf.h"                   /* Win32k segment definitions. */ | 
|---|
| 28 | #include "malloc.h"                     /* Win32k malloc. Not C library! */ | 
|---|
| 29 |  | 
|---|
| 30 | #include <string.h>                     /* C library string.h. */ | 
|---|
| 31 | #include <stdarg.h>                     /* C library stdarg.h. */ | 
|---|
| 32 |  | 
|---|
| 33 | #include "vprintf.h"                    /* win32k printf and vprintf. Not C library! */ | 
|---|
| 34 | #include "dev32.h"                      /* 32-Bit part of the device driver. (SSToDS) */ | 
|---|
| 35 | #include "OS2Krnl.h"                    /* kernel structs.  (SFN) */ | 
|---|
| 36 | #include "ldrCalls.h"                   /* ldrOpenPath and ldrlv_t. */ | 
|---|
| 37 | #include "modulebase.h"                 /* ModuleBase class definitions, ++. */ | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | /******************************************************************************* | 
|---|
| 41 | *   Global Variables                                                           * | 
|---|
| 42 | *******************************************************************************/ | 
|---|
| 43 | /** | 
|---|
| 44 | * Current output message detail level; default: ModuleBase::Info, -W3. | 
|---|
| 45 | */ | 
|---|
| 46 | ULONG ModuleBase::ulInfoLevel = ModuleBase::Info; | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | /** | 
|---|
| 51 | * Constructor - Initializes the object to init state and sets the filehandle. | 
|---|
| 52 | * @param     hFile   System filenumber of the exectuable file. | 
|---|
| 53 | * @status    completely implemented. | 
|---|
| 54 | * @author    knut st. osmundsen | 
|---|
| 55 | */ | 
|---|
| 56 | ModuleBase::ModuleBase(SFN hFile) : hFile(hFile), pszFilename(NULL), pszModuleName(NULL) | 
|---|
| 57 | { | 
|---|
| 58 | #ifdef DEBUG | 
|---|
| 59 | fInitTime = TRUE; | 
|---|
| 60 | #endif | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | /** | 
|---|
| 65 | * Destructor frees all memory allocated by this object. | 
|---|
| 66 | * @status    completely implemented. | 
|---|
| 67 | * @author    knut st. osmundsen | 
|---|
| 68 | */ | 
|---|
| 69 | ModuleBase::~ModuleBase() | 
|---|
| 70 | { | 
|---|
| 71 | if (pszFilename != NULL) | 
|---|
| 72 | free(pszFilename); | 
|---|
| 73 | if (pszModuleName != NULL) | 
|---|
| 74 | free(pszModuleName); | 
|---|
| 75 | #ifdef DEBUG | 
|---|
| 76 | fInitTime = (BOOL)-1; | 
|---|
| 77 | pszFilename = (PSZ)0xFFFFBEEF; | 
|---|
| 78 | pszModuleName = (PSZ)0xFFFFBEEF; | 
|---|
| 79 | #endif | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | /** | 
|---|
| 85 | * This functions sets the filename and modulename for the ModuleBase object. | 
|---|
| 86 | * | 
|---|
| 87 | * The children of this class should override this method and upon return of it | 
|---|
| 88 | * the object should be a fully initialized virtual LX file. The object will | 
|---|
| 89 | * then not be in init mode any longer (fInitTime is FALSE). | 
|---|
| 90 | * | 
|---|
| 91 | * @returns   NO_ERROR on success. | 
|---|
| 92 | *            ERROR_NOT_ENOUGH_MEMORY | 
|---|
| 93 | *            ERROR_INTERNAL_PROCESSING_ERROR | 
|---|
| 94 | * @param     pszFilename  Module filename. | 
|---|
| 95 | * @precond   Called in init-mode. | 
|---|
| 96 | * @sketch    DEBUG: Verify initmode. | 
|---|
| 97 | *            Duplicate filename. | 
|---|
| 98 | *            Derive the modulename from the filename. | 
|---|
| 99 | *            return successfully. | 
|---|
| 100 | * @status    Completely implemented; tested. | 
|---|
| 101 | * @author    knut st. osmundsen | 
|---|
| 102 | * @remark    The object is still in initmode when returing. | 
|---|
| 103 | */ | 
|---|
| 104 | ULONG ModuleBase::init(PCSZ pszFilename) | 
|---|
| 105 | { | 
|---|
| 106 | PCSZ psz; | 
|---|
| 107 | int   i; | 
|---|
| 108 |  | 
|---|
| 109 | #ifdef DEBUG | 
|---|
| 110 | /* check that were called in initmode. */ | 
|---|
| 111 | if (!fInitTime) | 
|---|
| 112 | return ERROR_INTERNAL_PROCESSING_ERROR; | 
|---|
| 113 | #endif | 
|---|
| 114 |  | 
|---|
| 115 | /* Duplicate filename. */ | 
|---|
| 116 | this->pszFilename = (char*)malloc(strlen(pszFilename) + 1); | 
|---|
| 117 | if (this->pszFilename == NULL) | 
|---|
| 118 | return ERROR_NOT_ENOUGH_MEMORY; | 
|---|
| 119 | strcpy(this->pszFilename, pszFilename); | 
|---|
| 120 |  | 
|---|
| 121 | /* Derive the modulename. */ | 
|---|
| 122 | if ((psz = strrchr(pszFilename, '\\') + 1) == (PCHAR)1) | 
|---|
| 123 | if ((psz = strrchr(pszFilename, '/') + 1) == (PCHAR)1) | 
|---|
| 124 | psz = pszFilename; | 
|---|
| 125 | pszModuleName = strchr(psz, '.'); | 
|---|
| 126 | i = pszModuleName != NULL ? pszModuleName - psz : strlen(psz); | 
|---|
| 127 | pszModuleName = (PSZ)malloc(i + 1); | 
|---|
| 128 | if (pszModuleName == NULL) | 
|---|
| 129 | return ERROR_NOT_ENOUGH_MEMORY; | 
|---|
| 130 | strncpy(pszModuleName, psz, i); | 
|---|
| 131 | pszModuleName[i] = '\0'; | 
|---|
| 132 |  | 
|---|
| 133 | /* return successfully */ | 
|---|
| 134 | return NO_ERROR; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 |  | 
|---|
| 139 | /** | 
|---|
| 140 | * Applies relocation fixups to a page which is being loaded. | 
|---|
| 141 | * @returns    NO_ERROR on success? | 
|---|
| 142 | *             error code on error? | 
|---|
| 143 | * @param      pMTE           Pointer Module Table Entry. | 
|---|
| 144 | * @param      iObject        Index into the object table. (0-based) | 
|---|
| 145 | * @param      iPageTable     Index into the page table. (0-based) | 
|---|
| 146 | * @param      pvPage         Pointer to the page which is being loaded. | 
|---|
| 147 | * @param      ulPageAddress  Address of page. | 
|---|
| 148 | * @param      pvPTDA         Pointer to Per Task Data Aera | 
|---|
| 149 | * @remark     Stub. | 
|---|
| 150 | */ | 
|---|
| 151 | ULONG  ModuleBase::applyFixups(PMTE pMTE, ULONG iObject, ULONG iPageTable, PVOID pvPage, | 
|---|
| 152 | ULONG ulPageAddress, PVOID pvPTDA) | 
|---|
| 153 | { | 
|---|
| 154 | NOREF(pMTE); | 
|---|
| 155 | NOREF(iObject); | 
|---|
| 156 | NOREF(iPageTable); | 
|---|
| 157 | NOREF(pvPage); | 
|---|
| 158 | NOREF(ulPageAddress); | 
|---|
| 159 | NOREF(pvPTDA); | 
|---|
| 160 | return NO_ERROR; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 | /** | 
|---|
| 165 | * openPath - opens file eventually searching loader specific paths. | 
|---|
| 166 | * | 
|---|
| 167 | * This base implementation simply calls ldrOpenPath. | 
|---|
| 168 | * This method is only called for DLLs. DosLoadModule and Imports. | 
|---|
| 169 | * | 
|---|
| 170 | * @returns   OS2 return code. | 
|---|
| 171 | *            pLdrLv->lv_sfn  is set to filename handle. | 
|---|
| 172 | * @param     pachFilename  Pointer to filename. Not zero terminated! | 
|---|
| 173 | * @param     cchFilename   Filename length. | 
|---|
| 174 | * @param     pLdrLv        Loader local variables? (Struct from KERNEL.SDF) | 
|---|
| 175 | * @param     pful          Pointer to flags which are passed on to ldrOpen. | 
|---|
| 176 | * @param     lLibPath      New parameter in build 14053(/8266?) | 
|---|
| 177 | *                          ldrGetMte calls with 1 | 
|---|
| 178 | *                          ldrOpenNewExe calls with 3 | 
|---|
| 179 | *                          This is compared to the initial libpath index. | 
|---|
| 180 | *                              The libpath index is: | 
|---|
| 181 | *                                  BEGINLIBPATH    1 | 
|---|
| 182 | *                                  LIBPATH         2 | 
|---|
| 183 | *                                  ENDLIBPATH      3 | 
|---|
| 184 | *                              The initial libpath index is either 1 or 2. | 
|---|
| 185 | *                          Currently we'll ignore it. (I don't know why ldrGetMte calls ldrOpenPath...) | 
|---|
| 186 | * @sketch | 
|---|
| 187 | * This is roughly what the original ldrOpenPath does: | 
|---|
| 188 | *      Save pTCBCur->TCBFailErr. | 
|---|
| 189 | *      if !CLASS_GLOBAL or miniifs then | 
|---|
| 190 | *          ldrOpen(pachFilename) | 
|---|
| 191 | *      else | 
|---|
| 192 | *          if beglibpath != NULL then path = 1 else path = 2 | 
|---|
| 193 | *          if (lLibPath < path) | 
|---|
| 194 | *              return ERROR_FILE_NOT_FOUND; (2) | 
|---|
| 195 | *          Allocate buffer. | 
|---|
| 196 | *          loop until no more libpath elements | 
|---|
| 197 | *              get next libpath element and add it to the modname. | 
|---|
| 198 | *              try open the modname | 
|---|
| 199 | *              if successfull then break the loop. | 
|---|
| 200 | *          endloop | 
|---|
| 201 | *          Free buffer. | 
|---|
| 202 | *      endif | 
|---|
| 203 | *      Restore pTCBCur->TCBFailErr. | 
|---|
| 204 | */ | 
|---|
| 205 | ULONG  ModuleBase::openPath(PCHAR pachFilename, USHORT cchFilename, ldrlv_t *pLdrLv, PULONG pful, ULONG lLibPath) /* (ldrOpenPath) */ | 
|---|
| 206 | { | 
|---|
| 207 | #ifdef RING0 | 
|---|
| 208 | printf("ModuleBase::openPath:\n"); | 
|---|
| 209 | return ldrOpenPath(pachFilename, cchFilename, pLdrLv, pful, lLibPath); | 
|---|
| 210 | #else | 
|---|
| 211 | NOREF(pachFilename); | 
|---|
| 212 | NOREF(cchFilename); | 
|---|
| 213 | NOREF(pLdrLv); | 
|---|
| 214 | NOREF(pful); | 
|---|
| 215 | return ERROR_NOT_SUPPORTED; | 
|---|
| 216 | #endif | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 |  | 
|---|
| 220 | #ifndef RING0 | 
|---|
| 221 |  | 
|---|
| 222 | /** | 
|---|
| 223 | * Optional method objects when in Ring-3: Write the virtual LX file to disk. | 
|---|
| 224 | * | 
|---|
| 225 | * A child my override this method, as this is simply a dummy stub. | 
|---|
| 226 | * | 
|---|
| 227 | * @returns   ERROR_NOT_SUPPORTED. | 
|---|
| 228 | *            Children: OS/2 styled return codes with the errorcodes specified in ModuleBase.h. | 
|---|
| 229 | * @param     pszFilename  Name of output file. This file should be overwritten if exists and created | 
|---|
| 230 | *                         if it don't exists. | 
|---|
| 231 | * @status    completely implemented. | 
|---|
| 232 | * @author    knut st. osmundsen | 
|---|
| 233 | */ | 
|---|
| 234 | ULONG  ModuleBase::writeFile(PCSZ pszFilename) | 
|---|
| 235 | { | 
|---|
| 236 | pszFilename = pszFilename; | 
|---|
| 237 | return ERROR_NOT_SUPPORTED; | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | #endif | 
|---|
| 241 |  | 
|---|
| 242 |  | 
|---|
| 243 | /** | 
|---|
| 244 | * Compare a given filename/modulename with the name of this module. | 
|---|
| 245 | * @returns   TRUE:  Matching. | 
|---|
| 246 | *            FALSE: Non-matching. | 
|---|
| 247 | * @param     pszFilename  Filename/modulename to match with this module. | 
|---|
| 248 | * @sketch    IF filename without path THEN | 
|---|
| 249 | *            BEGIN | 
|---|
| 250 | *                IF no extention THEN | 
|---|
| 251 | *                    compare directly with modulename | 
|---|
| 252 | *                ELSE | 
|---|
| 253 | *                    compare input filename with the object filename without path. | 
|---|
| 254 | *            END | 
|---|
| 255 | *            ELSE | 
|---|
| 256 | *                compare input filename with the object filename | 
|---|
| 257 | *            return TRUE if equal : FALSE if not. | 
|---|
| 258 | * @status    completely implemented. | 
|---|
| 259 | * @author    knut st. osmundsen | 
|---|
| 260 | */ | 
|---|
| 261 | BOOL  ModuleBase::queryIsModuleName(PCSZ pszFilename) | 
|---|
| 262 | { | 
|---|
| 263 | #ifdef DEBUG | 
|---|
| 264 | if (!fInitTime) | 
|---|
| 265 | { | 
|---|
| 266 | printIPE(("queryIsModuleName should not be called during init!\n")); | 
|---|
| 267 | return FALSE; | 
|---|
| 268 | } | 
|---|
| 269 | #endif | 
|---|
| 270 | if (strchr(pszFilename, '\\') == NULL && strchr(pszFilename,'/') == NULL) | 
|---|
| 271 | {   /* use module name - no extention */ | 
|---|
| 272 | if (strchr(pszFilename, '.') == NULL) | 
|---|
| 273 | return stricmp(pszFilename, pszModuleName) == 0; | 
|---|
| 274 | else | 
|---|
| 275 | {   /* extention */ | 
|---|
| 276 | PCSZ psz = strchr(this->pszFilename, '\\'); | 
|---|
| 277 | if ((psz = strchr(this->pszFilename, '/')) == NULL) | 
|---|
| 278 | psz = this->pszFilename; | 
|---|
| 279 | else | 
|---|
| 280 | psz++; /* skip '\\' or '/' */ | 
|---|
| 281 | return stricmp(pszFilename, psz) == 0; | 
|---|
| 282 | } | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | /* TODO: relative name vs fullname. */ | 
|---|
| 286 | return stricmp(pszFilename, this->pszFilename) == 0; | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 |  | 
|---|
| 290 | /** | 
|---|
| 291 | * Gets the fullpath filename. | 
|---|
| 292 | * @returns     Const ("Readonly") pointer to the filename. | 
|---|
| 293 | * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 294 | */ | 
|---|
| 295 | PCSZ ModuleBase::getFilename() | 
|---|
| 296 | { | 
|---|
| 297 | return pszFilename; | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 |  | 
|---|
| 301 | /** | 
|---|
| 302 | * Gets the modulename. | 
|---|
| 303 | * @returns     Const ("Readonly") pointer to the module name. | 
|---|
| 304 | * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 305 | * @remark      Modulename is filename without path and extention. | 
|---|
| 306 | */ | 
|---|
| 307 | PCSZ ModuleBase::getModuleName() | 
|---|
| 308 | { | 
|---|
| 309 | return pszModuleName; | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 |  | 
|---|
| 313 | /** | 
|---|
| 314 | * Output function for Modules. | 
|---|
| 315 | * @param     pszFormat    Pointer to format string. | 
|---|
| 316 | * @status    completely implemented; tested. | 
|---|
| 317 | * @author    knut st. osmundsen | 
|---|
| 318 | */ | 
|---|
| 319 | VOID ModuleBase::printf(PCSZ pszFormat, ...) | 
|---|
| 320 | { | 
|---|
| 321 | va_list arguments; | 
|---|
| 322 |  | 
|---|
| 323 | if (ulInfoLevel <= ModuleBase::ulInfoLevel) | 
|---|
| 324 | { | 
|---|
| 325 | va_start(arguments, pszFormat); | 
|---|
| 326 | vprintf2(pszFormat, arguments); | 
|---|
| 327 | va_end(arguments); | 
|---|
| 328 | } | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 |  | 
|---|
| 332 | #ifndef RING0 | 
|---|
| 333 | /** | 
|---|
| 334 | * | 
|---|
| 335 | * @returns   OS/2 return code. (NO_ERROR on success...) | 
|---|
| 336 | * @param     hFile     Handle to file. (RING0: Handle to SystemFileNumber (SFN).) | 
|---|
| 337 | * @param     ulOffset  Offset in the file to start reading at. | 
|---|
| 338 | * @param     pvBuffer  Pointer to output buffer. | 
|---|
| 339 | * @param     cbToRead  Count of bytes to read. | 
|---|
| 340 | * @sketch    Set Change filepointer to ulOffset. | 
|---|
| 341 | *            Read cbToRead into pvBufer. | 
|---|
| 342 | * @status    completely tested. | 
|---|
| 343 | * @author    knut st. osmundsen | 
|---|
| 344 | * @remark | 
|---|
| 345 | */ | 
|---|
| 346 | APIRET ReadAt(SFN hFile, ULONG ulOffset, PVOID pvBuffer, ULONG cbToRead) | 
|---|
| 347 | { | 
|---|
| 348 | ULONG cbRead, ulMoved; | 
|---|
| 349 | APIRET rc; | 
|---|
| 350 |  | 
|---|
| 351 | rc = DosSetFilePtr(hFile, ulOffset, FILE_BEGIN, &ulMoved); | 
|---|
| 352 | if (rc == NO_ERROR) | 
|---|
| 353 | rc = DosRead(hFile, pvBuffer, cbToRead, &cbRead); | 
|---|
| 354 | else | 
|---|
| 355 | printErr(("DosSetFilePtr(hfile, %#8x(%d),..) failed with rc = %d.\n", | 
|---|
| 356 | ulOffset, ulOffset, rc)); | 
|---|
| 357 |  | 
|---|
| 358 | return rc; | 
|---|
| 359 | } | 
|---|
| 360 | #endif | 
|---|
| 361 |  | 
|---|