| 1 | /* $Id: ModuleBase.cpp,v 1.3 2000-02-27 02:17: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 "malloc.h" /* win32k malloc. Not C library! */
|
|---|
| 28 |
|
|---|
| 29 | #include <string.h> /* C library string.h. */
|
|---|
| 30 | #include <stdarg.h> /* C library stdarg.h. */
|
|---|
| 31 |
|
|---|
| 32 | #include "vprintf.h" /* win32k printf and vprintf. Not C library! */
|
|---|
| 33 | #include "dev32.h" /* 32-Bit part of the device driver. (SSToDS) */
|
|---|
| 34 | #include "OS2Krnl.h" /* kernel structs. (SFN) */
|
|---|
| 35 | #include "modulebase.h" /* ModuleBase class definitions, ++. */
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | /*******************************************************************************
|
|---|
| 39 | * Global Variables *
|
|---|
| 40 | *******************************************************************************/
|
|---|
| 41 | /**
|
|---|
| 42 | * Current output message detail level; default: ModuleBase::Info, -W3.
|
|---|
| 43 | */
|
|---|
| 44 | ULONG ModuleBase::ulInfoLevel = ModuleBase::Info;
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Constructor - Initializes the object to init state and sets the filehandle.
|
|---|
| 50 | * @param hFile System filenumber of the exectuable file.
|
|---|
| 51 | * @status completely implemented.
|
|---|
| 52 | * @author knut st. osmundsen
|
|---|
| 53 | */
|
|---|
| 54 | ModuleBase::ModuleBase(SFN hFile) : hFile(hFile), pszFilename(NULL), pszModuleName(NULL)
|
|---|
| 55 | {
|
|---|
| 56 | #ifdef DEBUG
|
|---|
| 57 | fInitTime = TRUE;
|
|---|
| 58 | #endif
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Destructor frees all memory allocated by this object.
|
|---|
| 64 | * @status completely implemented.
|
|---|
| 65 | * @author knut st. osmundsen
|
|---|
| 66 | */
|
|---|
| 67 | ModuleBase::~ModuleBase()
|
|---|
| 68 | {
|
|---|
| 69 | if (pszFilename != NULL)
|
|---|
| 70 | free(pszFilename);
|
|---|
| 71 | if (pszModuleName != NULL)
|
|---|
| 72 | free(pszModuleName);
|
|---|
| 73 | #ifdef DEBUG
|
|---|
| 74 | fInitTime = (BOOL)-1;
|
|---|
| 75 | pszFilename = (PSZ)0xFFFFBEEF;
|
|---|
| 76 | pszModuleName = (PSZ)0xFFFFBEEF;
|
|---|
| 77 | #endif
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * This functions sets the filename and modulename for the ModuleBase object.
|
|---|
| 84 | *
|
|---|
| 85 | * The children of this class should override this method and upon return of it
|
|---|
| 86 | * the object should be a fully initialized virtual LX file. The object will
|
|---|
| 87 | * then not be in init mode any longer (fInitTime is FALSE).
|
|---|
| 88 | *
|
|---|
| 89 | * @returns NO_ERROR on success.
|
|---|
| 90 | * ERROR_NOT_ENOUGH_MEMORY
|
|---|
| 91 | * ERROR_INTERNAL_PROCESSING_ERROR
|
|---|
| 92 | * @param pszFilename Module filename.
|
|---|
| 93 | * @precond Called in init-mode.
|
|---|
| 94 | * @sketch DEBUG: Verify initmode.
|
|---|
| 95 | * Duplicate filename.
|
|---|
| 96 | * Derive the modulename from the filename.
|
|---|
| 97 | * return successfully.
|
|---|
| 98 | * @status Completely implemented; tested.
|
|---|
| 99 | * @author knut st. osmundsen
|
|---|
| 100 | * @remark The object is still in initmode when returing.
|
|---|
| 101 | */
|
|---|
| 102 | ULONG ModuleBase::init(PCSZ pszFilename)
|
|---|
| 103 | {
|
|---|
| 104 | PCSZ psz;
|
|---|
| 105 | int i;
|
|---|
| 106 |
|
|---|
| 107 | #ifdef DEBUG
|
|---|
| 108 | /* check that were called in initmode. */
|
|---|
| 109 | if (!fInitTime)
|
|---|
| 110 | return ERROR_INTERNAL_PROCESSING_ERROR;
|
|---|
| 111 | #endif
|
|---|
| 112 |
|
|---|
| 113 | /* Duplicate filename. */
|
|---|
| 114 | this->pszFilename = (char*)malloc(strlen(pszFilename) + 1);
|
|---|
| 115 | if (this->pszFilename == NULL)
|
|---|
| 116 | return ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 117 | strcpy(this->pszFilename, pszFilename);
|
|---|
| 118 |
|
|---|
| 119 | /* Derive the modulename. */
|
|---|
| 120 | if ((psz = strrchr(pszFilename, '\\') + 1) == (PCHAR)1)
|
|---|
| 121 | if ((psz = strrchr(pszFilename, '/') + 1) == (PCHAR)1)
|
|---|
| 122 | psz = pszFilename;
|
|---|
| 123 | pszModuleName = strchr(psz, '.');
|
|---|
| 124 | i = pszModuleName != NULL ? pszModuleName - psz : strlen(psz);
|
|---|
| 125 | pszModuleName = (PSZ)malloc(i + 1);
|
|---|
| 126 | if (pszModuleName == NULL)
|
|---|
| 127 | return ERROR_NOT_ENOUGH_MEMORY;
|
|---|
| 128 | strncpy(pszModuleName, psz, i);
|
|---|
| 129 | pszModuleName[i] = '\0';
|
|---|
| 130 |
|
|---|
| 131 | /* return successfully */
|
|---|
| 132 | return NO_ERROR;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * Applies relocation fixups to a page which is being loaded.
|
|---|
| 139 | * @returns NO_ERROR on success?
|
|---|
| 140 | * error code on error?
|
|---|
| 141 | * @param pMTE Pointer Module Table Entry.
|
|---|
| 142 | * @param iObject Index into the object table. (0-based)
|
|---|
| 143 | * @param iPageTable Index into the page table. (0-based)
|
|---|
| 144 | * @param pvPage Pointer to the page which is being loaded.
|
|---|
| 145 | * @param ulPageAddress Address of page.
|
|---|
| 146 | * @param pvPTDA Pointer to Per Task Data Aera
|
|---|
| 147 | * @remark Stub.
|
|---|
| 148 | */
|
|---|
| 149 | ULONG ModuleBase::applyFixups(PMTE pMTE, ULONG iObject, ULONG iPageTable, PVOID pvPage,
|
|---|
| 150 | ULONG ulPageAddress, PVOID pvPTDA)
|
|---|
| 151 | {
|
|---|
| 152 | NOREF(pMTE);
|
|---|
| 153 | NOREF(iObject);
|
|---|
| 154 | NOREF(iPageTable);
|
|---|
| 155 | NOREF(pvPage);
|
|---|
| 156 | NOREF(ulPageAddress);
|
|---|
| 157 | NOREF(pvPTDA);
|
|---|
| 158 | return NO_ERROR;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | #ifndef RING0
|
|---|
| 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * Optional method objects when in Ring-3: Write the virtual LX file to disk.
|
|---|
| 167 | *
|
|---|
| 168 | * A child my override this method, as this is simply a dummy stub.
|
|---|
| 169 | *
|
|---|
| 170 | * @returns ERROR_NOT_SUPPORTED.
|
|---|
| 171 | * Children: OS/2 styled return codes with the errorcodes specified in ModuleBase.h.
|
|---|
| 172 | * @param pszFilename Name of output file. This file should be overwritten if exists and created
|
|---|
| 173 | * if it don't exists.
|
|---|
| 174 | * @status completely implemented.
|
|---|
| 175 | * @author knut st. osmundsen
|
|---|
| 176 | */
|
|---|
| 177 | ULONG ModuleBase::writeFile(PCSZ pszFilename)
|
|---|
| 178 | {
|
|---|
| 179 | pszFilename = pszFilename;
|
|---|
| 180 | return ERROR_NOT_SUPPORTED;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | #endif
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 | /**
|
|---|
| 187 | * Compare a given filename/modulename with the name of this module.
|
|---|
| 188 | * @returns TRUE: Matching.
|
|---|
| 189 | * FALSE: Non-matching.
|
|---|
| 190 | * @param pszFilename Filename/modulename to match with this module.
|
|---|
| 191 | * @sketch IF filename without path THEN
|
|---|
| 192 | * BEGIN
|
|---|
| 193 | * IF no extention THEN
|
|---|
| 194 | * compare directly with modulename
|
|---|
| 195 | * ELSE
|
|---|
| 196 | * compare input filename with the object filename without path.
|
|---|
| 197 | * END
|
|---|
| 198 | * ELSE
|
|---|
| 199 | * compare input filename with the object filename
|
|---|
| 200 | * return TRUE if equal : FALSE if not.
|
|---|
| 201 | * @status completely implemented.
|
|---|
| 202 | * @author knut st. osmundsen
|
|---|
| 203 | */
|
|---|
| 204 | BOOL ModuleBase::queryIsModuleName(PCSZ pszFilename)
|
|---|
| 205 | {
|
|---|
| 206 | #ifdef DEBUG
|
|---|
| 207 | if (!fInitTime)
|
|---|
| 208 | {
|
|---|
| 209 | printIPE(("queryIsModuleName should not be called during init!\n"));
|
|---|
| 210 | return FALSE;
|
|---|
| 211 | }
|
|---|
| 212 | #endif
|
|---|
| 213 | if (strchr(pszFilename, '\\') == NULL && strchr(pszFilename,'/') == NULL)
|
|---|
| 214 | { /* use module name - no extention */
|
|---|
| 215 | if (strchr(pszFilename, '.') == NULL)
|
|---|
| 216 | return stricmp(pszFilename, pszModuleName) == 0;
|
|---|
| 217 | else
|
|---|
| 218 | { /* extention */
|
|---|
| 219 | PCSZ psz = strchr(this->pszFilename, '\\');
|
|---|
| 220 | if ((psz = strchr(this->pszFilename, '/')) == NULL)
|
|---|
| 221 | psz = this->pszFilename;
|
|---|
| 222 | else
|
|---|
| 223 | psz++; /* skip '\\' or '/' */
|
|---|
| 224 | return stricmp(pszFilename, psz) == 0;
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /* TODO: relative name vs fullname. */
|
|---|
| 229 | return stricmp(pszFilename, this->pszFilename) == 0;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 | /**
|
|---|
| 234 | * Output function for Modules.
|
|---|
| 235 | * @param pszFormat Pointer to format string.
|
|---|
| 236 | * @status completely implemented; tested.
|
|---|
| 237 | * @author knut st. osmundsen
|
|---|
| 238 | */
|
|---|
| 239 | VOID ModuleBase::printf(PCSZ pszFormat, ...)
|
|---|
| 240 | {
|
|---|
| 241 | va_list arguments;
|
|---|
| 242 |
|
|---|
| 243 | if (ulInfoLevel <= ModuleBase::ulInfoLevel)
|
|---|
| 244 | {
|
|---|
| 245 | va_start(arguments, pszFormat);
|
|---|
| 246 | vprintf2(pszFormat, arguments);
|
|---|
| 247 | va_end(arguments);
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 | #ifndef RING0
|
|---|
| 253 | /**
|
|---|
| 254 | *
|
|---|
| 255 | * @returns OS/2 return code. (NO_ERROR on success...)
|
|---|
| 256 | * @param hFile Handle to file. (RING0: Handle to SystemFileNumber (SFN).)
|
|---|
| 257 | * @param ulOffset Offset in the file to start reading at.
|
|---|
| 258 | * @param pvBuffer Pointer to output buffer.
|
|---|
| 259 | * @param cbToRead Count of bytes to read.
|
|---|
| 260 | * @sketch Set Change filepointer to ulOffset.
|
|---|
| 261 | * Read cbToRead into pvBufer.
|
|---|
| 262 | * @status completely tested.
|
|---|
| 263 | * @author knut st. osmundsen
|
|---|
| 264 | * @remark
|
|---|
| 265 | */
|
|---|
| 266 | APIRET ReadAt(SFN hFile, ULONG ulOffset, PVOID pvBuffer, ULONG cbToRead)
|
|---|
| 267 | {
|
|---|
| 268 | ULONG cbRead, ulMoved;
|
|---|
| 269 | APIRET rc;
|
|---|
| 270 |
|
|---|
| 271 | rc = DosSetFilePtr(hFile, ulOffset, FILE_BEGIN, &ulMoved);
|
|---|
| 272 | if (rc == NO_ERROR)
|
|---|
| 273 | rc = DosRead(hFile, pvBuffer, cbToRead, &cbRead);
|
|---|
| 274 | else
|
|---|
| 275 | printErr(("DosSetFilePtr(hfile, %#8x(%d),..) failed with rc = %d.",
|
|---|
| 276 | ulOffset, ulOffset, rc));
|
|---|
| 277 |
|
|---|
| 278 | return rc;
|
|---|
| 279 | }
|
|---|
| 280 | #endif
|
|---|
| 281 |
|
|---|