| 1 | /* $Id: d16init.c,v 1.9 2000-12-11 06:53:48 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * d16init - init routines for both drivers. | 
|---|
| 4 | * | 
|---|
| 5 | * IMPORTANT! Code and data defined here will be discarded after init is | 
|---|
| 6 | *            compleated. CodeEnd and DataEnd should not be set here.? | 
|---|
| 7 | * | 
|---|
| 8 | * Copyright (c) 1999-2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | /******************************************************************************* | 
|---|
| 15 | *   Defined Constants                                                          * | 
|---|
| 16 | *******************************************************************************/ | 
|---|
| 17 | #define INCL_16 | 
|---|
| 18 | #define INCL_DOSFILEMGR | 
|---|
| 19 | #define INCL_DOSDEVICES | 
|---|
| 20 | #define INCL_DOSMISC | 
|---|
| 21 | #define INCL_DOSERRORS | 
|---|
| 22 | #define INCL_NOPMAPI | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | /******************************************************************************* | 
|---|
| 26 | *   Header Files                                                               * | 
|---|
| 27 | *******************************************************************************/ | 
|---|
| 28 | #include <os2.h> | 
|---|
| 29 | #include <devhdr.h> | 
|---|
| 30 | #include <devcmd.h> | 
|---|
| 31 | #include <strat2.h> | 
|---|
| 32 | #include <reqpkt.h> | 
|---|
| 33 | #include <dhcalls.h> | 
|---|
| 34 |  | 
|---|
| 35 | #include <string.h> | 
|---|
| 36 | #include <memory.h> | 
|---|
| 37 |  | 
|---|
| 38 | #include "devSegDf.h" | 
|---|
| 39 | #undef  DATA16_INIT | 
|---|
| 40 | #define DATA16_INIT | 
|---|
| 41 | #undef  CODE16_INIT | 
|---|
| 42 | #define CODE16_INIT | 
|---|
| 43 | #include "probkrnl.h" | 
|---|
| 44 | #include "dev1632.h" | 
|---|
| 45 | #include "dev16.h" | 
|---|
| 46 | #include "vprntf16.h" | 
|---|
| 47 | #include "log.h" | 
|---|
| 48 | #include "options.h" | 
|---|
| 49 | #include "errors.h" | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | /** | 
|---|
| 53 | * init function - device 0. | 
|---|
| 54 | * Does nothing else than setting the device_help variable and | 
|---|
| 55 | * fill the request packet. | 
|---|
| 56 | * @returns   Status word. | 
|---|
| 57 | * @param     pRpIn   Pointer to input request packet.  Actually the same memory as pRpOut but another struct. | 
|---|
| 58 | * @param     pRpOut  Pointer to output request packet. Actually the same memory as pRpIn  but another struct. | 
|---|
| 59 | * @remark    pRpIn and pRpOut points to the same memory. | 
|---|
| 60 | */ | 
|---|
| 61 | USHORT NEAR dev0Init(PRPINITIN pRpIn, PRPINITOUT pRpOut) | 
|---|
| 62 | { | 
|---|
| 63 | APIRET  rc; | 
|---|
| 64 | Device_Help = pRpIn->DevHlpEP; | 
|---|
| 65 |  | 
|---|
| 66 | /* | 
|---|
| 67 | * Does this work at Ring-3 (inittime)? | 
|---|
| 68 | * If this work we could be saved from throuble! | 
|---|
| 69 | */ | 
|---|
| 70 | rc = initGetDosTableData(); | 
|---|
| 71 | if (rc != NO_ERROR) | 
|---|
| 72 | printf16("win32k - elf$: initGetDosTableData failed with rc=%d\n", rc); | 
|---|
| 73 |  | 
|---|
| 74 | pRpOut->BPBArray = NULL; | 
|---|
| 75 | pRpOut->CodeEnd = (USHORT)&CODE16_INITSTART; | 
|---|
| 76 | pRpOut->DataEnd = (USHORT)&DATA16_INITSTART; | 
|---|
| 77 | pRpOut->Unit     = 0; | 
|---|
| 78 | pRpOut->rph.Status = STATUS_DONE; | 
|---|
| 79 | return STATUS_DONE; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | /** | 
|---|
| 84 | * init function - device 1. | 
|---|
| 85 | * We will send an IOCtl request to the elf$ (device 0) which will | 
|---|
| 86 | * perform the Ring-0 initiation of the driver. | 
|---|
| 87 | * @returns   Status word. | 
|---|
| 88 | * @param     pRpIn   Pointer to input request packet.  Actually the same memory as pRpOut but another struct. | 
|---|
| 89 | * @param     pRpOut  Pointer to output request packet. Actually the same memory as pRpIn  but another struct. | 
|---|
| 90 | * @remark    pRpIn and pRpOut points to the same memory. | 
|---|
| 91 | */ | 
|---|
| 92 | USHORT NEAR dev1Init(PRPINITIN pRpIn, PRPINITOUT pRpOut) | 
|---|
| 93 | { | 
|---|
| 94 | APIRET          rc; | 
|---|
| 95 | D16R0INITPARAM  param; | 
|---|
| 96 | D16R0INITDATA   data = {0}; | 
|---|
| 97 | HFILE           hDev0 = 0; | 
|---|
| 98 | USHORT          usAction = 0; | 
|---|
| 99 | NPSZ            npszErrMsg = NULL; | 
|---|
| 100 | NPSZ            npszErrMsg2 = NULL; | 
|---|
| 101 |  | 
|---|
| 102 | /* | 
|---|
| 103 | * Probe kernel data. | 
|---|
| 104 | */ | 
|---|
| 105 | rc = ProbeKernel(pRpIn); | 
|---|
| 106 | if (rc == NO_ERROR) | 
|---|
| 107 | { | 
|---|
| 108 | /* | 
|---|
| 109 | * Open and send a Ring-0 init packet to elf$. | 
|---|
| 110 | * If this succeeds win32k$ init succeeds. | 
|---|
| 111 | */ | 
|---|
| 112 | rc = DosOpen("\\dev\\elf$", &hDev0, &usAction, 0UL, FILE_NORMAL, | 
|---|
| 113 | OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS, | 
|---|
| 114 | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY, | 
|---|
| 115 | 0UL); | 
|---|
| 116 | if (rc == NO_ERROR) | 
|---|
| 117 | { | 
|---|
| 118 | param.pRpInitIn = pRpIn; | 
|---|
| 119 | rc = DosDevIOCtl(&data, ¶m, D16_IOCTL_RING0INIT, D16_IOCTL_CAT, hDev0); | 
|---|
| 120 | if (rc == NO_ERROR) | 
|---|
| 121 | { | 
|---|
| 122 | if (data.usRcInit32 == NO_ERROR) | 
|---|
| 123 | { | 
|---|
| 124 | if (!options.fQuiet) | 
|---|
| 125 | printf16("Win32k.sys succesfully initiated!\n"); | 
|---|
| 126 | pRpOut->Status = pRpOut->rph.Status = STATUS_DONE; | 
|---|
| 127 | } | 
|---|
| 128 | else | 
|---|
| 129 | { | 
|---|
| 130 | /* set correct error message and rc */ | 
|---|
| 131 | rc = data.usRcInit32; | 
|---|
| 132 | npszErrMsg = "Ring-0 initiation failed. rc=%x\n"; | 
|---|
| 133 | if (rc >= ERROR_D32_FIRST && rc <= ERROR_D32_LAST) | 
|---|
| 134 | npszErrMsg2 = GetErrorMsg(data.usRcInit32 + ERROR_PROB_SYM_D32_FIRST - ERROR_D32_FIRST); | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 | else | 
|---|
| 138 | npszErrMsg = "Ring-0 init: DosDevIOCtl failed. rc=%d\n"; | 
|---|
| 139 | DosClose(hDev0); | 
|---|
| 140 | } | 
|---|
| 141 | else | 
|---|
| 142 | npszErrMsg = "Ring-0 init: DosOpen failed. rc=%d\n"; | 
|---|
| 143 | } | 
|---|
| 144 | else | 
|---|
| 145 | npszErrMsg = ""; /* ProbeKrnl do its own complaining, but we need something here to indicate failure. */ | 
|---|
| 146 |  | 
|---|
| 147 | /* | 
|---|
| 148 | * Fill return data. | 
|---|
| 149 | */ | 
|---|
| 150 | pRpOut->CodeEnd = (USHORT)&CODE16_INITSTART; | 
|---|
| 151 | pRpOut->DataEnd = (USHORT)&DATA16_INITSTART; | 
|---|
| 152 | pRpOut->BPBArray= NULL; | 
|---|
| 153 | pRpOut->Unit    = 0; | 
|---|
| 154 |  | 
|---|
| 155 | /* | 
|---|
| 156 | * Any errors?, if so complain! | 
|---|
| 157 | */ | 
|---|
| 158 | if (npszErrMsg) | 
|---|
| 159 | { | 
|---|
| 160 | printf16(npszErrMsg, rc); | 
|---|
| 161 | if (npszErrMsg2) | 
|---|
| 162 | printf16("%s\n", npszErrMsg2); | 
|---|
| 163 | pRpOut->Status = pRpOut->rph.Status = STATUS_DONE | STERR | ERROR_I24_GEN_FAILURE; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | /* Init is completed. */ | 
|---|
| 167 | fInitTime = FALSE; | 
|---|
| 168 |  | 
|---|
| 169 | /* successful return */ | 
|---|
| 170 | return pRpOut->rph.Status; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | /** | 
|---|
| 176 | * R0 16-bit initiation. | 
|---|
| 177 | * This gets TKSSBase, thunks parameters and calls R0 32-bit initiation function. | 
|---|
| 178 | * @returns     Status word. We don't fail on R0Init32 but forwards the result | 
|---|
| 179 | *              using the usRcInit32. | 
|---|
| 180 | * @param       pRp     Generic IO Control request packet. | 
|---|
| 181 | */ | 
|---|
| 182 | USHORT NEAR  R0Init16(PRP_GENIOCTL pRp) | 
|---|
| 183 | { | 
|---|
| 184 | USHORT usRc = STATUS_DONE; | 
|---|
| 185 | APIRET rc; | 
|---|
| 186 |  | 
|---|
| 187 | /* First we're to get the DosTable2 stuff. */ | 
|---|
| 188 | rc = initGetDosTableData(); | 
|---|
| 189 | if (rc == NO_ERROR) | 
|---|
| 190 | { | 
|---|
| 191 | ULONG cPages; | 
|---|
| 192 | char  hLockParm[12] = {0}; | 
|---|
| 193 | ULONG ulLinParm; | 
|---|
| 194 | char  hLockData[12] = {0}; | 
|---|
| 195 | ULONG ulLinData; | 
|---|
| 196 |  | 
|---|
| 197 | /* | 
|---|
| 198 | * Thunk the request packet and lock userdata. | 
|---|
| 199 | */ | 
|---|
| 200 | if (!DevHelp_VirtToLin(SELECTOROF(pRp->ParmPacket), OFFSETOF(pRp->ParmPacket), &ulLinParm) | 
|---|
| 201 | && | 
|---|
| 202 | !DevHelp_VirtToLin(SELECTOROF(pRp->DataPacket), OFFSETOF(pRp->DataPacket), &ulLinData) | 
|---|
| 203 | ) | 
|---|
| 204 | { | 
|---|
| 205 | if (!(rc = DevHelp_VMLock(VMDHL_LONG | VMDHL_WRITE, | 
|---|
| 206 | ulLinParm, sizeof(D16R0INITPARAM), | 
|---|
| 207 | (LIN)~0UL, SSToDS_16(&hLockParm[0]), &cPages)) | 
|---|
| 208 | && | 
|---|
| 209 | !DevHelp_VMLock(VMDHL_LONG | VMDHL_WRITE, | 
|---|
| 210 | ulLinData, sizeof(D16R0INITDATA), | 
|---|
| 211 | (LIN)~0UL, SSToDS_16(&hLockData[0]), &cPages) | 
|---|
| 212 | ) | 
|---|
| 213 | { | 
|---|
| 214 | /* | 
|---|
| 215 | * -data and param is locked (do we need to lock the request packet too ?).- | 
|---|
| 216 | * Create new 32-bit init packet - Parameter pointer is thunked. | 
|---|
| 217 | */ | 
|---|
| 218 | RP32INIT rp32init; | 
|---|
| 219 |  | 
|---|
| 220 | _fmemcpy(&rp32init, ((PD16R0INITPARAM)pRp->ParmPacket)->pRpInitIn, sizeof(RPINITIN)); | 
|---|
| 221 | if (((PD16R0INITPARAM)pRp->ParmPacket)->pRpInitIn->InitArgs == NULL || | 
|---|
| 222 | !DevHelp_VirtToLin(SELECTOROF(rp32init.InitArgs), OFFSETOF(rp32init.InitArgs), (PLIN)&rp32init.InitArgs) | 
|---|
| 223 | ) | 
|---|
| 224 | {   /* call 32-bit init routine and set 32 bit rc. */ | 
|---|
| 225 | usRc = CallR0Init32(SSToDS_16(&rp32init)); | 
|---|
| 226 | ((PD16R0INITDATA)pRp->DataPacket)->usRcInit32 = usRc; | 
|---|
| 227 |  | 
|---|
| 228 | /* set status to done (success).  (R0Init32 RC is return as usRcInit32.)  */ | 
|---|
| 229 | usRc = STATUS_DONE; | 
|---|
| 230 | } | 
|---|
| 231 | else | 
|---|
| 232 | usRc |= STERR | ERROR_I24_INVALID_PARAMETER; | 
|---|
| 233 |  | 
|---|
| 234 |  | 
|---|
| 235 | /* | 
|---|
| 236 | * finished - unlock data and parm; | 
|---|
| 237 | */ | 
|---|
| 238 | DevHelp_VMUnLock((LIN)SSToDS_16(&hLockParm[0])); | 
|---|
| 239 | DevHelp_VMUnLock((LIN)SSToDS_16(&hLockData[0])); | 
|---|
| 240 | } | 
|---|
| 241 | else | 
|---|
| 242 | usRc |= STERR | ERROR_I24_INVALID_PARAMETER; | 
|---|
| 243 | } | 
|---|
| 244 | else | 
|---|
| 245 | usRc |= STERR | ERROR_I24_INVALID_PARAMETER; | 
|---|
| 246 | } | 
|---|
| 247 | else | 
|---|
| 248 | usRc |= STERR | ERROR_I24_GEN_FAILURE; | 
|---|
| 249 |  | 
|---|
| 250 | return usRc; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 |  | 
|---|
| 254 |  | 
|---|
| 255 | /** | 
|---|
| 256 | * Gets the data we need from the DosTables. | 
|---|
| 257 | * This data is TKSSBase16, R0FlatCS16 and R0FlatDS16. | 
|---|
| 258 | * @returns   Same as DevHelp_GetDosVar. | 
|---|
| 259 | * @status    completely implemented. | 
|---|
| 260 | * @author    knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 261 | * @remark    If you are not sure if TKSSBase16 is set or not, call this. | 
|---|
| 262 | *            After R0Init16 is called TKSSBase16 _is_ set. | 
|---|
| 263 | *            IMPORTANT! This function must _not_ be called after the initiation of the second device driver!!! | 
|---|
| 264 | *                       (Since this is init code not present after init...) | 
|---|
| 265 | */ | 
|---|
| 266 | USHORT NEAR  initGetDosTableData(void) | 
|---|
| 267 | { | 
|---|
| 268 | APIRET     rc; | 
|---|
| 269 | PDOSTABLE  pDT; | 
|---|
| 270 | PDOSTABLE2 pDT2; | 
|---|
| 271 |  | 
|---|
| 272 | if (TKSSBase16 != 0) | 
|---|
| 273 | return NO_ERROR; | 
|---|
| 274 | /* | 
|---|
| 275 | _asm { | 
|---|
| 276 | int 3; | 
|---|
| 277 | } | 
|---|
| 278 | */ | 
|---|
| 279 |  | 
|---|
| 280 | /* First we're to get the DosTable2 stuff. */ | 
|---|
| 281 | rc = DevHelp_GetDOSVar(9, 0, &pDT); | 
|---|
| 282 | if (rc == NO_ERROR) | 
|---|
| 283 | { | 
|---|
| 284 | pDT2 = (PDOSTABLE2)((char FAR *)pDT + pDT->cul*4 + 1); | 
|---|
| 285 | TKSSBase16 = (ULONG)pDT2->pTKSSBase; | 
|---|
| 286 | R0FlatCS16 = (USHORT)pDT2->R0FlatCS; | 
|---|
| 287 | R0FlatDS16 = (USHORT)pDT2->R0FlatDS; | 
|---|
| 288 | } | 
|---|
| 289 | return rc; | 
|---|
| 290 | } | 
|---|