1 | /* $Id: VBoxGuest-win-legacy.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuest-win-legacy - Windows NT4 specifics.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include "VBoxGuest-win.h"
|
---|
23 | #include "VBoxGuestInternal.h"
|
---|
24 | #include <VBox/err.h>
|
---|
25 | #include <VBox/log.h>
|
---|
26 | #include <VBox/version.h>
|
---|
27 | #include <VBox/VBoxGuestLib.h>
|
---|
28 | #include <iprt/string.h>
|
---|
29 |
|
---|
30 |
|
---|
31 | /*********************************************************************************************************************************
|
---|
32 | * Defined Constants And Macros *
|
---|
33 | *********************************************************************************************************************************/
|
---|
34 | #ifndef PCI_MAX_BUSES
|
---|
35 | # define PCI_MAX_BUSES 256
|
---|
36 | #endif
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Internal Functions *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | RT_C_DECLS_BEGIN
|
---|
43 | static NTSTATUS vbgdNt4FindPciDevice(PULONG pulBusNumber, PPCI_SLOT_NUMBER pSlotNumber);
|
---|
44 | RT_C_DECLS_END
|
---|
45 |
|
---|
46 | #ifdef ALLOC_PRAGMA
|
---|
47 | # pragma alloc_text(INIT, vbgdNt4CreateDevice)
|
---|
48 | # pragma alloc_text(INIT, vbgdNt4FindPciDevice)
|
---|
49 | #endif
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Legacy helper function to create the device object.
|
---|
54 | *
|
---|
55 | * @returns NT status code.
|
---|
56 | *
|
---|
57 | * @param pDrvObj The driver object.
|
---|
58 | * @param pDevObj Unused. NULL. Dunno why it's here, makes no sense.
|
---|
59 | * @param pRegPath The driver registry path.
|
---|
60 | */
|
---|
61 | NTSTATUS vbgdNt4CreateDevice(PDRIVER_OBJECT pDrvObj, PDEVICE_OBJECT pDevObj, PUNICODE_STRING pRegPath)
|
---|
62 | {
|
---|
63 | Log(("VBoxGuest::vbgdNt4CreateDevice: pDrvObj=%p, pDevObj=%p, pRegPath=%p\n", pDrvObj, pDevObj, pRegPath));
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Find our virtual PCI device
|
---|
67 | */
|
---|
68 | ULONG uBusNumber;
|
---|
69 | PCI_SLOT_NUMBER SlotNumber;
|
---|
70 | NTSTATUS rc = vbgdNt4FindPciDevice(&uBusNumber, &SlotNumber);
|
---|
71 | if (NT_ERROR(rc))
|
---|
72 | {
|
---|
73 | Log(("VBoxGuest::vbgdNt4CreateDevice: Device not found!\n"));
|
---|
74 | return rc;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Create device.
|
---|
79 | */
|
---|
80 | UNICODE_STRING szDevName;
|
---|
81 | RtlInitUnicodeString(&szDevName, VBOXGUEST_DEVICE_NAME_NT);
|
---|
82 | PDEVICE_OBJECT pDeviceObject = NULL;
|
---|
83 | rc = IoCreateDevice(pDrvObj, sizeof(VBOXGUESTDEVEXTWIN), &szDevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDeviceObject);
|
---|
84 | if (NT_SUCCESS(rc))
|
---|
85 | {
|
---|
86 | Log(("VBoxGuest::vbgdNt4CreateDevice: Device created\n"));
|
---|
87 |
|
---|
88 | UNICODE_STRING DosName;
|
---|
89 | RtlInitUnicodeString(&DosName, VBOXGUEST_DEVICE_NAME_DOS);
|
---|
90 | rc = IoCreateSymbolicLink(&DosName, &szDevName);
|
---|
91 | if (NT_SUCCESS(rc))
|
---|
92 | {
|
---|
93 | Log(("VBoxGuest::vbgdNt4CreateDevice: Symlink created\n"));
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Setup the device extension.
|
---|
97 | */
|
---|
98 | Log(("VBoxGuest::vbgdNt4CreateDevice: Setting up device extension ...\n"));
|
---|
99 |
|
---|
100 | PVBOXGUESTDEVEXTWIN pDevExt = (PVBOXGUESTDEVEXTWIN)pDeviceObject->DeviceExtension;
|
---|
101 | RT_ZERO(*pDevExt);
|
---|
102 |
|
---|
103 | Log(("VBoxGuest::vbgdNt4CreateDevice: Device extension created\n"));
|
---|
104 |
|
---|
105 | /* Store a reference to ourself. */
|
---|
106 | pDevExt->pDeviceObject = pDeviceObject;
|
---|
107 |
|
---|
108 | /* Store bus and slot number we've queried before. */
|
---|
109 | pDevExt->busNumber = uBusNumber;
|
---|
110 | pDevExt->slotNumber = SlotNumber.u.AsULONG;
|
---|
111 |
|
---|
112 | #ifdef VBOX_WITH_GUEST_BUGCHECK_DETECTION
|
---|
113 | rc = hlpRegisterBugCheckCallback(pDevExt);
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | /* Do the actual VBox init ... */
|
---|
117 | if (NT_SUCCESS(rc))
|
---|
118 | {
|
---|
119 | rc = vbgdNtInit(pDrvObj, pDeviceObject, pRegPath);
|
---|
120 | if (NT_SUCCESS(rc))
|
---|
121 | {
|
---|
122 | Log(("VBoxGuest::vbgdNt4CreateDevice: Returning rc = 0x%x (succcess)\n", rc));
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* bail out */
|
---|
127 | }
|
---|
128 | IoDeleteSymbolicLink(&DosName);
|
---|
129 | }
|
---|
130 | else
|
---|
131 | Log(("VBoxGuest::vbgdNt4CreateDevice: IoCreateSymbolicLink failed with rc = %#x\n", rc));
|
---|
132 | IoDeleteDevice(pDeviceObject);
|
---|
133 | }
|
---|
134 | else
|
---|
135 | Log(("VBoxGuest::vbgdNt4CreateDevice: IoCreateDevice failed with rc = %#x\n", rc));
|
---|
136 | Log(("VBoxGuest::vbgdNt4CreateDevice: Returning rc = 0x%x\n", rc));
|
---|
137 | return rc;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Helper function to handle the PCI device lookup.
|
---|
143 | *
|
---|
144 | * @returns NT status code.
|
---|
145 | *
|
---|
146 | * @param pulBusNumber Where to return the bus number on success.
|
---|
147 | * @param pSlotNumber Where to return the slot number on success.
|
---|
148 | */
|
---|
149 | static NTSTATUS vbgdNt4FindPciDevice(PULONG pulBusNumber, PPCI_SLOT_NUMBER pSlotNumber)
|
---|
150 | {
|
---|
151 | Log(("VBoxGuest::vbgdNt4FindPciDevice\n"));
|
---|
152 |
|
---|
153 | PCI_SLOT_NUMBER SlotNumber;
|
---|
154 | SlotNumber.u.AsULONG = 0;
|
---|
155 |
|
---|
156 | /* Scan each bus. */
|
---|
157 | for (ULONG ulBusNumber = 0; ulBusNumber < PCI_MAX_BUSES; ulBusNumber++)
|
---|
158 | {
|
---|
159 | /* Scan each device. */
|
---|
160 | for (ULONG deviceNumber = 0; deviceNumber < PCI_MAX_DEVICES; deviceNumber++)
|
---|
161 | {
|
---|
162 | SlotNumber.u.bits.DeviceNumber = deviceNumber;
|
---|
163 |
|
---|
164 | /* Scan each function (not really required...). */
|
---|
165 | for (ULONG functionNumber = 0; functionNumber < PCI_MAX_FUNCTION; functionNumber++)
|
---|
166 | {
|
---|
167 | SlotNumber.u.bits.FunctionNumber = functionNumber;
|
---|
168 |
|
---|
169 | /* Have a look at what's in this slot. */
|
---|
170 | PCI_COMMON_CONFIG PciData;
|
---|
171 | if (!HalGetBusData(PCIConfiguration, ulBusNumber, SlotNumber.u.AsULONG, &PciData, sizeof(ULONG)))
|
---|
172 | {
|
---|
173 | /* No such bus, we're done with it. */
|
---|
174 | deviceNumber = PCI_MAX_DEVICES;
|
---|
175 | break;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (PciData.VendorID == PCI_INVALID_VENDORID)
|
---|
179 | /* We have to proceed to the next function. */
|
---|
180 | continue;
|
---|
181 |
|
---|
182 | /* Check if it's another device. */
|
---|
183 | if ( PciData.VendorID != VMMDEV_VENDORID
|
---|
184 | || PciData.DeviceID != VMMDEV_DEVICEID)
|
---|
185 | continue;
|
---|
186 |
|
---|
187 | /* Hooray, we've found it! */
|
---|
188 | Log(("VBoxGuest::vbgdNt4FindPciDevice: Device found!\n"));
|
---|
189 |
|
---|
190 | *pulBusNumber = ulBusNumber;
|
---|
191 | *pSlotNumber = SlotNumber;
|
---|
192 | return STATUS_SUCCESS;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | return STATUS_DEVICE_DOES_NOT_EXIST;
|
---|
198 | }
|
---|
199 |
|
---|