| 1 | /* $Id: ModuleBase.cpp,v 1.4 2000-09-02 21:08:06 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * ModuleBase - Implementetation. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 1999-2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.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 | * @sketch | 
|---|
| 177 | * This is roughly what the original ldrOpenPath does: | 
|---|
| 178 | *      if !CLASS_GLOBAL or miniifs then | 
|---|
| 179 | *          ldrOpen(pachModName) | 
|---|
| 180 | *      else | 
|---|
| 181 | *          loop until no more libpath elements | 
|---|
| 182 | *              get next libpath element and add it to the modname. | 
|---|
| 183 | *              try open the modname | 
|---|
| 184 | *              if successfull then break the loop. | 
|---|
| 185 | *          endloop | 
|---|
| 186 | *      endif | 
|---|
| 187 | */ | 
|---|
| 188 | ULONG  ModuleBase::openPath(PCHAR pachFilename, USHORT cchFilename, ldrlv_t *pLdrLv, PULONG pful) /* (ldrOpenPath) */ | 
|---|
| 189 | { | 
|---|
| 190 | #ifdef RING0 | 
|---|
| 191 | printf("ModuleBase::openPath:\n"); | 
|---|
| 192 | return ldrOpenPath(pachFilename, cchFilename, pLdrLv, pful); | 
|---|
| 193 | #else | 
|---|
| 194 | NOREF(pachFilename); | 
|---|
| 195 | NOREF(cchFilename); | 
|---|
| 196 | NOREF(pLdrLv); | 
|---|
| 197 | NOREF(pful); | 
|---|
| 198 | return ERROR_NOT_SUPPORTED; | 
|---|
| 199 | #endif | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 |  | 
|---|
| 203 | #ifndef RING0 | 
|---|
| 204 |  | 
|---|
| 205 | /** | 
|---|
| 206 | * Optional method objects when in Ring-3: Write the virtual LX file to disk. | 
|---|
| 207 | * | 
|---|
| 208 | * A child my override this method, as this is simply a dummy stub. | 
|---|
| 209 | * | 
|---|
| 210 | * @returns   ERROR_NOT_SUPPORTED. | 
|---|
| 211 | *            Children: OS/2 styled return codes with the errorcodes specified in ModuleBase.h. | 
|---|
| 212 | * @param     pszFilename  Name of output file. This file should be overwritten if exists and created | 
|---|
| 213 | *                         if it don't exists. | 
|---|
| 214 | * @status    completely implemented. | 
|---|
| 215 | * @author    knut st. osmundsen | 
|---|
| 216 | */ | 
|---|
| 217 | ULONG  ModuleBase::writeFile(PCSZ pszFilename) | 
|---|
| 218 | { | 
|---|
| 219 | pszFilename = pszFilename; | 
|---|
| 220 | return ERROR_NOT_SUPPORTED; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | #endif | 
|---|
| 224 |  | 
|---|
| 225 |  | 
|---|
| 226 | /** | 
|---|
| 227 | * Compare a given filename/modulename with the name of this module. | 
|---|
| 228 | * @returns   TRUE:  Matching. | 
|---|
| 229 | *            FALSE: Non-matching. | 
|---|
| 230 | * @param     pszFilename  Filename/modulename to match with this module. | 
|---|
| 231 | * @sketch    IF filename without path THEN | 
|---|
| 232 | *            BEGIN | 
|---|
| 233 | *                IF no extention THEN | 
|---|
| 234 | *                    compare directly with modulename | 
|---|
| 235 | *                ELSE | 
|---|
| 236 | *                    compare input filename with the object filename without path. | 
|---|
| 237 | *            END | 
|---|
| 238 | *            ELSE | 
|---|
| 239 | *                compare input filename with the object filename | 
|---|
| 240 | *            return TRUE if equal : FALSE if not. | 
|---|
| 241 | * @status    completely implemented. | 
|---|
| 242 | * @author    knut st. osmundsen | 
|---|
| 243 | */ | 
|---|
| 244 | BOOL  ModuleBase::queryIsModuleName(PCSZ pszFilename) | 
|---|
| 245 | { | 
|---|
| 246 | #ifdef DEBUG | 
|---|
| 247 | if (!fInitTime) | 
|---|
| 248 | { | 
|---|
| 249 | printIPE(("queryIsModuleName should not be called during init!\n")); | 
|---|
| 250 | return FALSE; | 
|---|
| 251 | } | 
|---|
| 252 | #endif | 
|---|
| 253 | if (strchr(pszFilename, '\\') == NULL && strchr(pszFilename,'/') == NULL) | 
|---|
| 254 | {   /* use module name - no extention */ | 
|---|
| 255 | if (strchr(pszFilename, '.') == NULL) | 
|---|
| 256 | return stricmp(pszFilename, pszModuleName) == 0; | 
|---|
| 257 | else | 
|---|
| 258 | {   /* extention */ | 
|---|
| 259 | PCSZ psz = strchr(this->pszFilename, '\\'); | 
|---|
| 260 | if ((psz = strchr(this->pszFilename, '/')) == NULL) | 
|---|
| 261 | psz = this->pszFilename; | 
|---|
| 262 | else | 
|---|
| 263 | psz++; /* skip '\\' or '/' */ | 
|---|
| 264 | return stricmp(pszFilename, psz) == 0; | 
|---|
| 265 | } | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | /* TODO: relative name vs fullname. */ | 
|---|
| 269 | return stricmp(pszFilename, this->pszFilename) == 0; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 |  | 
|---|
| 273 | /** | 
|---|
| 274 | * Gets the fullpath filename. | 
|---|
| 275 | * @returns     Const ("Readonly") pointer to the filename. | 
|---|
| 276 | * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 277 | */ | 
|---|
| 278 | PCSZ ModuleBase::getFilename() | 
|---|
| 279 | { | 
|---|
| 280 | return pszFilename; | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 |  | 
|---|
| 284 | /** | 
|---|
| 285 | * Gets the modulename. | 
|---|
| 286 | * @returns     Const ("Readonly") pointer to the module name. | 
|---|
| 287 | * @author      knut st. osmundsen (knut.stange.osmundsen@pmsc.no) | 
|---|
| 288 | * @remark      Modulename is filename without path and extention. | 
|---|
| 289 | */ | 
|---|
| 290 | PCSZ ModuleBase::getModuleName() | 
|---|
| 291 | { | 
|---|
| 292 | return pszModuleName; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 |  | 
|---|
| 296 | /** | 
|---|
| 297 | * Output function for Modules. | 
|---|
| 298 | * @param     pszFormat    Pointer to format string. | 
|---|
| 299 | * @status    completely implemented; tested. | 
|---|
| 300 | * @author    knut st. osmundsen | 
|---|
| 301 | */ | 
|---|
| 302 | VOID ModuleBase::printf(PCSZ pszFormat, ...) | 
|---|
| 303 | { | 
|---|
| 304 | va_list arguments; | 
|---|
| 305 |  | 
|---|
| 306 | if (ulInfoLevel <= ModuleBase::ulInfoLevel) | 
|---|
| 307 | { | 
|---|
| 308 | va_start(arguments, pszFormat); | 
|---|
| 309 | vprintf2(pszFormat, arguments); | 
|---|
| 310 | va_end(arguments); | 
|---|
| 311 | } | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 |  | 
|---|
| 315 | #ifndef RING0 | 
|---|
| 316 | /** | 
|---|
| 317 | * | 
|---|
| 318 | * @returns   OS/2 return code. (NO_ERROR on success...) | 
|---|
| 319 | * @param     hFile     Handle to file. (RING0: Handle to SystemFileNumber (SFN).) | 
|---|
| 320 | * @param     ulOffset  Offset in the file to start reading at. | 
|---|
| 321 | * @param     pvBuffer  Pointer to output buffer. | 
|---|
| 322 | * @param     cbToRead  Count of bytes to read. | 
|---|
| 323 | * @sketch    Set Change filepointer to ulOffset. | 
|---|
| 324 | *            Read cbToRead into pvBufer. | 
|---|
| 325 | * @status    completely tested. | 
|---|
| 326 | * @author    knut st. osmundsen | 
|---|
| 327 | * @remark | 
|---|
| 328 | */ | 
|---|
| 329 | APIRET ReadAt(SFN hFile, ULONG ulOffset, PVOID pvBuffer, ULONG cbToRead) | 
|---|
| 330 | { | 
|---|
| 331 | ULONG cbRead, ulMoved; | 
|---|
| 332 | APIRET rc; | 
|---|
| 333 |  | 
|---|
| 334 | rc = DosSetFilePtr(hFile, ulOffset, FILE_BEGIN, &ulMoved); | 
|---|
| 335 | if (rc == NO_ERROR) | 
|---|
| 336 | rc = DosRead(hFile, pvBuffer, cbToRead, &cbRead); | 
|---|
| 337 | else | 
|---|
| 338 | printErr(("DosSetFilePtr(hfile, %#8x(%d),..) failed with rc = %d.\n", | 
|---|
| 339 | ulOffset, ulOffset, rc)); | 
|---|
| 340 |  | 
|---|
| 341 | return rc; | 
|---|
| 342 | } | 
|---|
| 343 | #endif | 
|---|
| 344 |  | 
|---|