| 1 | /* $Id: myldrOpen.cpp,v 1.2 1999-10-14 01:25:38 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * myldrOpen - _ldrOpen.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 1998-1999 knut st. osmundsen
|
|---|
| 6 | *
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | /*******************************************************************************
|
|---|
| 11 | * Defined Constants And Macros *
|
|---|
| 12 | *******************************************************************************/
|
|---|
| 13 | #define INCL_DOSERRORS
|
|---|
| 14 | #define INCL_NOPMAPI
|
|---|
| 15 |
|
|---|
| 16 | /*******************************************************************************
|
|---|
| 17 | * Header Files *
|
|---|
| 18 | *******************************************************************************/
|
|---|
| 19 | #include <os2.h>
|
|---|
| 20 |
|
|---|
| 21 | #include "malloc.h"
|
|---|
| 22 | #include <memory.h>
|
|---|
| 23 | #include <stdlib.h>
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | #include "log.h"
|
|---|
| 27 | #include <peexe.h>
|
|---|
| 28 | #include <exe386.h>
|
|---|
| 29 | #include "OS2Krnl.h"
|
|---|
| 30 | #include "pe2lx.h"
|
|---|
| 31 | #include "ldr.h"
|
|---|
| 32 | #include "ldrCalls.h"
|
|---|
| 33 |
|
|---|
| 34 | #include "elf.h"
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * _ldrOpen override.
|
|---|
| 38 | * @returns Return code.
|
|---|
| 39 | * @param phFile Pointer to file handler. Holds filehandle on output.
|
|---|
| 40 | * @param pszFilename Pointer to filename.
|
|---|
| 41 | * @parma param3 Probably some flags.
|
|---|
| 42 | */
|
|---|
| 43 | ULONG LDRCALL myldrOpen(PSFN phFile, char *pszFilename, ULONG param3)
|
|---|
| 44 | {
|
|---|
| 45 | ULONG rc;
|
|---|
| 46 | int i;
|
|---|
| 47 |
|
|---|
| 48 | rc = _ldrOpen(phFile, pszFilename, param3);
|
|---|
| 49 |
|
|---|
| 50 | if (rc == NO_ERROR)
|
|---|
| 51 | kprintf(("_ldrOpen: phFile=%#.4x, flags=%#.8x, pszFn=%s\n", *phFile, param3, pszFilename));
|
|---|
| 52 |
|
|---|
| 53 | if (rc == NO_ERROR)
|
|---|
| 54 | {
|
|---|
| 55 | static char achBuffer[sizeof(IMAGE_DOS_HEADER)];
|
|---|
| 56 | PIMAGE_DOS_HEADER pMzHdr = (PIMAGE_DOS_HEADER)&achBuffer[0];
|
|---|
| 57 | char *pach = &achBuffer[0];
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * See if this is an recognizable module format.
|
|---|
| 61 | * This costs up to two disk reads!
|
|---|
| 62 | */
|
|---|
| 63 | rc = _ldrRead(*phFile, 0UL, pMzHdr, 0UL, sizeof(*pMzHdr), NULL);
|
|---|
| 64 | if (rc == NO_ERROR)
|
|---|
| 65 | {
|
|---|
| 66 | if (pMzHdr->e_magic == IMAGE_DOS_SIGNATURE &&
|
|---|
| 67 | pMzHdr->e_lfanew > sizeof(IMAGE_DOS_HEADER) && pMzHdr->e_lfanew < 0x04000000UL) /* Larger than 64 bytes and less that 64MB. */
|
|---|
| 68 | { /* MZ header found */
|
|---|
| 69 | /* read */
|
|---|
| 70 | rc = _ldrRead(*phFile, pMzHdr->e_lfanew, pMzHdr, 0UL, 4UL, NULL);
|
|---|
| 71 | if (rc == NO_ERROR && *(PULONG)pach == IMAGE_NT_SIGNATURE)
|
|---|
| 72 | { /* PE signature found */
|
|---|
| 73 | PPENODE pNode;
|
|---|
| 74 |
|
|---|
| 75 | kprintf(("_ldrOpen: PE executable!(?)\n"));
|
|---|
| 76 | #pragma info(none)
|
|---|
| 77 | if (/* invoke pe.exe or do conversion now? */ 1)
|
|---|
| 78 | { /* pe2lx - win32k */
|
|---|
| 79 | #pragma info(restore)
|
|---|
| 80 | pNode = allocateNode();
|
|---|
| 81 | if (pNode != NULL)
|
|---|
| 82 | {
|
|---|
| 83 | pNode->pPe2Lx = new Pe2Lx(*phFile);
|
|---|
| 84 | if (pNode->pPe2Lx != NULL)
|
|---|
| 85 | {
|
|---|
| 86 | rc = pNode->pPe2Lx->init(pszFilename);
|
|---|
| 87 | if (rc == NO_ERROR)
|
|---|
| 88 | {
|
|---|
| 89 | kprintf(("_ldrOpen: Successfully init of Pe2Lx object.\n"));
|
|---|
| 90 | rc = insertNode(pNode);
|
|---|
| 91 | if (rc != NO_ERROR)
|
|---|
| 92 | {
|
|---|
| 93 | kprintf(("_ldrOpen: Failed to insert PeNode into tree. rc=%d\n"));
|
|---|
| 94 | delete pNode->pPe2Lx;
|
|---|
| 95 | pNode->pPe2Lx = NULL;
|
|---|
| 96 | freeNode(pNode);
|
|---|
| 97 | SetState(*phFile, HSTATE_OUR);
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | else
|
|---|
| 101 | {
|
|---|
| 102 | kprintf(("_ldrOpen: Failed to init Pe2Lx object. rc=%d\n"));
|
|---|
| 103 | delete pNode->pPe2Lx;
|
|---|
| 104 | pNode->pPe2Lx = NULL;
|
|---|
| 105 | freeNode(pNode);
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | else
|
|---|
| 109 | {
|
|---|
| 110 | kprintf(("_ldrOpen: Failed to allocate Pe2Lx object.\n"));
|
|---|
| 111 | freeNode(pNode);
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | else
|
|---|
| 115 | kprintf(("_ldrOpen: Failed to allocate node.\n"));
|
|---|
| 116 | }
|
|---|
| 117 | else
|
|---|
| 118 | { /* pe.exe */
|
|---|
| 119 | kprintf(("_ldrOpen: pe.exe - opening\n"));
|
|---|
| 120 | _ldrClose(*phFile);
|
|---|
| 121 | rc = _ldrOpen(phFile, "pe.exe", param3); /* path....! problems! */
|
|---|
| 122 | kprintf(("_ldrOpen: pe.exe - open returned with rc = %d\n", rc));
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | rc = NO_ERROR;
|
|---|
| 126 | }
|
|---|
| 127 | else
|
|---|
| 128 | {
|
|---|
| 129 | if (*pach == '#')
|
|---|
| 130 | {
|
|---|
| 131 | /* unix styled script...? must be more than 64 bytes long.... */
|
|---|
| 132 | char *pszStart = pach+1;
|
|---|
| 133 | char *pszEnd;
|
|---|
| 134 | kprintf(("_ldrOpen: unix script?\n"));
|
|---|
| 135 | /* skip blanks */
|
|---|
| 136 | while (*pszStart != '\0' && (*pszStart == ' ' || *pszStart == '\t'))
|
|---|
| 137 | pszStart++;
|
|---|
| 138 | if (*pszStart != '\0' && *pszStart != '\r' && *pszStart != '\n')
|
|---|
| 139 | { /* find end-of-word */
|
|---|
| 140 | while (*pszEnd != '\0' && *pszEnd != '\n' && *pszEnd != '\r'
|
|---|
| 141 | && *pszEnd != '\t' && *pszEnd != ' ')
|
|---|
| 142 | pszEnd++;
|
|---|
| 143 | *pszEnd = '\0';
|
|---|
| 144 | kprintf(("_ldrOpen: unix script - opening %s\n", pszStart));
|
|---|
| 145 | _ldrClose(*phFile);
|
|---|
| 146 | rc = _ldrOpen(phFile, pszStart, param3);
|
|---|
| 147 | kprintf(("_ldrOpen: unix script - open returned with rc = %d\n", rc));
|
|---|
| 148 | }
|
|---|
| 149 | else
|
|---|
| 150 | kprintf(("_ldrOpen: unix script - unexpected end of line/file. (line: %.10s\n", pach));
|
|---|
| 151 | }
|
|---|
| 152 | else if (pach[0] == ELFMAG0 && pach[1] == ELFMAG1 && pach[2] == ELFMAG2 && pach[3] == ELFMAG3)
|
|---|
| 153 | {
|
|---|
| 154 | /* ELF signature found */
|
|---|
| 155 | kprintf(("_ldrOpen: ELF executable! - not implemented yet!\n"));
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | else
|
|---|
| 160 | {
|
|---|
| 161 | kprintf(("_ldrOpen: _ldrRead failed with rc=%d when reading DosHdr.\n", rc));
|
|---|
| 162 | rc = NO_ERROR;
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | return rc;
|
|---|
| 166 | }
|
|---|