| 1 | /* $Id: myldrRead.cpp,v 1.1 1999-09-06 02:20:01 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * myldrRead - _ldrRead. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 1998-1999 knut st. osmundsen | 
|---|
| 6 | * | 
|---|
| 7 | */ | 
|---|
| 8 |  | 
|---|
| 9 | /******************************************************************************* | 
|---|
| 10 | *   Defined Constants And Macros                                               * | 
|---|
| 11 | *******************************************************************************/ | 
|---|
| 12 | #define INCL_DOSERRORS | 
|---|
| 13 | #define INCL_NOPMAPI | 
|---|
| 14 |  | 
|---|
| 15 | /******************************************************************************* | 
|---|
| 16 | *   Header Files                                                               * | 
|---|
| 17 | *******************************************************************************/ | 
|---|
| 18 | #include <os2.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include "pefile.h" | 
|---|
| 21 | #include "lx.h" | 
|---|
| 22 | #include "log.h" | 
|---|
| 23 | #include "dev32.h" | 
|---|
| 24 | #include "ldr.h" | 
|---|
| 25 | #include "ldrCalls.h" | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | ULONG LDRCALL myldrRead( | 
|---|
| 30 | SFN hFile, | 
|---|
| 31 | ULONG ulOffset, | 
|---|
| 32 | PVOID pBuffer, | 
|---|
| 33 | ULONG ulFlags , | 
|---|
| 34 | ULONG ulBytesToRead, | 
|---|
| 35 | PMTE pMTE | 
|---|
| 36 | ) | 
|---|
| 37 | { | 
|---|
| 38 | ULONG   rc; | 
|---|
| 39 | ULONG   ulRead; | 
|---|
| 40 | int     i; | 
|---|
| 41 | PPENODE pNode; | 
|---|
| 42 |  | 
|---|
| 43 | /* check if this is an PE-file */ | 
|---|
| 44 | if (GetState(hFile) == HSTATE_PE) | 
|---|
| 45 | { | 
|---|
| 46 | pNode = getNodePtr(hFile); | 
|---|
| 47 | if (pNode != NULL) | 
|---|
| 48 | { | 
|---|
| 49 | /* I would love to have a pointer to the MTE */ | 
|---|
| 50 | if (pNode->pMTE == NULL && pMTE != NULL) | 
|---|
| 51 | pNode->pMTE == pMTE; | 
|---|
| 52 |  | 
|---|
| 53 | /*debug*/ | 
|---|
| 54 | if (ulFlags != 0) | 
|---|
| 55 | kprintf(("_ldrRead: Warning ulFlags = 0x%x (!= 0)\n", ulFlags)); | 
|---|
| 56 | rc = pNode->lxfile.read(ulOffset, pBuffer, ulBytesToRead, (PULONG)SSToDS(&ulRead), ulFlags); | 
|---|
| 57 | return rc; | 
|---|
| 58 | } | 
|---|
| 59 | else | 
|---|
| 60 | kprintf(("_ldrRead:  DON'T PANIC! - I can't get Node ptr!\n")); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | rc = _ldrRead( hFile, ulOffset, pBuffer, ulFlags, ulBytesToRead, pMTE ); | 
|---|
| 65 |  | 
|---|
| 66 | //  kprintf(("_ldrRead:  hF=%+04x off=%+08x pB=%+08x fl=%+08x cb=%+04x pMTE=%+08x rc=%d\n",hFile,ulOffset,pBuffer,ulFlags,ulBytesToRead,pMTE,rc)); | 
|---|
| 67 |  | 
|---|
| 68 | /* check if file is uncertain - try make it certain */ | 
|---|
| 69 | if (GetState(hFile) == HSTATE_CHECK) | 
|---|
| 70 | { | 
|---|
| 71 | i = findUncertainEntry(hFile); | 
|---|
| 72 | if (!ahUncertain[i].fMZ && ulOffset == 0 && ulBytesToRead >= 0x40) | 
|---|
| 73 | { | 
|---|
| 74 | /* Stage 1: */ | 
|---|
| 75 | if (((PUSHORT)pBuffer)[0] == IMAGE_DOS_SIGNATURE) | 
|---|
| 76 | {   /* MZ */ | 
|---|
| 77 | ahUncertain[i].offsetNEHdr = *(PULONG)((ULONG)pBuffer+0x3c); /*Need more verification???*/ | 
|---|
| 78 | ahUncertain[i].fMZ = 1; | 
|---|
| 79 | //kprintf(("_ldrRead:  Yeah! Detected MZ file - offset of NEHdr=%#x.\n", ahUncertain[i].offsetNEHdr)); | 
|---|
| 80 | } | 
|---|
| 81 | else | 
|---|
| 82 | {   /* CERTAIN: not a PE-file */ | 
|---|
| 83 | SetState(hFile, HSTATE_NOT_PE); | 
|---|
| 84 | freeUncertainEntry(hFile); | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 | else | 
|---|
| 88 | { | 
|---|
| 89 | if (ahUncertain[i].fMZ && ulOffset == ahUncertain[i].offsetNEHdr && ulBytesToRead == sizeof(struct e32_exe)) | 
|---|
| 90 | { | 
|---|
| 91 | /* Stage 2: */ | 
|---|
| 92 | if (LXFile::isPEFile((PIMAGE_SIG_FILE_HEADER)pBuffer)) | 
|---|
| 93 | { | 
|---|
| 94 | kprintf(("_ldrRead:  Yeah! Detected PE file.\n")); | 
|---|
| 95 | if (!fQAppType) | 
|---|
| 96 | { | 
|---|
| 97 | pNode = allocateNode(); | 
|---|
| 98 | if (pNode != NULL) | 
|---|
| 99 | { | 
|---|
| 100 | /* setup node */ | 
|---|
| 101 | pNode->hFile = hFile;   //key - but yes it is duplicateded in lxfile | 
|---|
| 102 | pNode->pMTE = pMTE; | 
|---|
| 103 | insertNode(pNode); | 
|---|
| 104 | SetState(hFile, HSTATE_PE); | 
|---|
| 105 | pNode->lxfile.SetModuleName(ahUncertain[i].pszName); | 
|---|
| 106 |  | 
|---|
| 107 | /* initiate lxfile */ | 
|---|
| 108 | if (pNode->lxfile.init(hFile, (PIMAGE_SIG_FILE_HEADER)pBuffer, ahUncertain[i].offsetNEHdr)) | 
|---|
| 109 | { | 
|---|
| 110 | /* init ok - do read */ | 
|---|
| 111 | ULONG read; | 
|---|
| 112 | /*debug*/ | 
|---|
| 113 | if (ulFlags != 0) | 
|---|
| 114 | kprintf(("_ldrRead: Warning ulFlags = 0x%x (!= 0)\n", ulFlags)); | 
|---|
| 115 | rc = pNode->lxfile.read(ulOffset, pBuffer, ulBytesToRead, (PULONG)SSToDS(&read), ulFlags); | 
|---|
| 116 | } | 
|---|
| 117 | else | 
|---|
| 118 | { | 
|---|
| 119 | /* init failed - complain */ | 
|---|
| 120 | kprintf(("_ldrRead:  opps! lxfile.init() failed!\n")); | 
|---|
| 121 | } | 
|---|
| 122 | } | 
|---|
| 123 | else | 
|---|
| 124 | { | 
|---|
| 125 | /* to not enter an unstable state - free all and mark as no pefile*/ | 
|---|
| 126 | SetState(hFile, HSTATE_NOT_PE); | 
|---|
| 127 | kprintf(("_ldrRead:  oops! could not allocate a Node for this file - why?\n")); | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 | else | 
|---|
| 131 | { | 
|---|
| 132 | kprintf(("_ldrRead: Building an QAppType-Header!\n")); | 
|---|
| 133 | LXFile::buildQAppTypeHeader((struct e32_exe*)pBuffer); | 
|---|
| 134 | SetState(hFile, HSTATE_NOT_PE); | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 | else | 
|---|
| 138 | { | 
|---|
| 139 | /* CERTAIN: not a pefile! */ | 
|---|
| 140 | SetState(hFile,HSTATE_NOT_PE); | 
|---|
| 141 | } | 
|---|
| 142 | freeUncertainEntry(hFile); | 
|---|
| 143 | } | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | return rc; | 
|---|
| 148 | } | 
|---|