* Call RtlCreateUnicodeString() instead of RtlInitUnicodeString() when
[reactos.git] / reactos / ntoskrnl / io / driver.c
1 /* $Id: driver.c,v 1.3 2002/06/12 14:05:03 chorns Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/driver.c
6 * PURPOSE: Manage devices
7 * PROGRAMMER: David Welch (welch@cwcom.net)
8 * UPDATE HISTORY:
9 * 15/05/98: Created
10 */
11
12 /* INCLUDES ****************************************************************/
13
14 #include <limits.h>
15 #include <ddk/ntddk.h>
16 #include <internal/io.h>
17 #include <internal/po.h>
18 #include <internal/ldr.h>
19 #include <internal/id.h>
20 #include <internal/pool.h>
21 #include <internal/registry.h>
22
23 #include <roscfg.h>
24
25 #define NDEBUG
26 #include <internal/debug.h>
27
28 /* GLOBALS *******************************************************************/
29
30 POBJECT_TYPE EXPORTED IoDriverObjectType = NULL;
31
32 #define TAG_DRIVER TAG('D', 'R', 'V', 'R')
33 #define TAG_DRIVER_EXTENSION TAG('D', 'R', 'V', 'E')
34
35 #define DRIVER_REGISTRY_KEY_BASENAME L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\"
36
37
38 /* FUNCTIONS ***************************************************************/
39
40 NTSTATUS STDCALL
41 IopCreateDriver(PVOID ObjectBody,
42 PVOID Parent,
43 PWSTR RemainingPath,
44 POBJECT_ATTRIBUTES ObjectAttributes)
45 {
46 DPRINT("LdrCreateModule(ObjectBody %x, Parent %x, RemainingPath %S)\n",
47 ObjectBody,
48 Parent,
49 RemainingPath);
50 if (RemainingPath != NULL && wcschr(RemainingPath + 1, '\\') != NULL)
51 {
52 return(STATUS_UNSUCCESSFUL);
53 }
54
55 return(STATUS_SUCCESS);
56 }
57
58
59 VOID
60 IopInitDriverImplementation(VOID)
61 {
62 /* Register the process object type */
63 IoDriverObjectType = ExAllocatePool(NonPagedPool, sizeof(OBJECT_TYPE));
64 IoDriverObjectType->Tag = TAG('D', 'R', 'V', 'R');
65 IoDriverObjectType->TotalObjects = 0;
66 IoDriverObjectType->TotalHandles = 0;
67 IoDriverObjectType->MaxObjects = ULONG_MAX;
68 IoDriverObjectType->MaxHandles = ULONG_MAX;
69 IoDriverObjectType->PagedPoolCharge = 0;
70 IoDriverObjectType->NonpagedPoolCharge = sizeof(DRIVER_OBJECT);
71 IoDriverObjectType->Dump = NULL;
72 IoDriverObjectType->Open = NULL;
73 IoDriverObjectType->Close = NULL;
74 IoDriverObjectType->Delete = NULL;
75 IoDriverObjectType->Parse = NULL;
76 IoDriverObjectType->Security = NULL;
77 IoDriverObjectType->QueryName = NULL;
78 IoDriverObjectType->OkayToClose = NULL;
79 IoDriverObjectType->Create = IopCreateDriver;
80 IoDriverObjectType->DuplicationNotify = NULL;
81 RtlInitUnicodeString(&IoDriverObjectType->TypeName, L"Driver");
82 }
83
84 /**********************************************************************
85 * NAME EXPORTED
86 * NtLoadDriver
87 *
88 * DESCRIPTION
89 * Loads a device driver.
90 *
91 * ARGUMENTS
92 * DriverServiceName
93 * Name of the service to load (registry key).
94 *
95 * RETURN VALUE
96 * Status.
97 *
98 * REVISIONS
99 */
100 NTSTATUS STDCALL
101 NtLoadDriver(IN PUNICODE_STRING DriverServiceName)
102 {
103 PDEVICE_NODE DeviceNode;
104 NTSTATUS Status;
105
106 PMODULE_OBJECT ModuleObject;
107 WCHAR Buffer[MAX_PATH];
108 ULONG Length;
109 LPWSTR Start;
110 LPWSTR Ext;
111
112 /* FIXME: this should lookup the filename from the registry */
113
114 /* Use IopRootDeviceNode for now */
115 Status = IopCreateDeviceNode(IopRootDeviceNode, NULL, &DeviceNode);
116 if (!NT_SUCCESS(Status))
117 {
118 return(Status);
119 }
120
121 Status = LdrLoadModule(DriverServiceName, &ModuleObject);
122 if (!NT_SUCCESS(Status))
123 {
124 DPRINT1("LdrLoadModule() failed (Status %lx)\n", Status);
125 IopFreeDeviceNode(DeviceNode);
126 return(Status);
127 }
128
129 /* Set a service name for the device node */
130
131 /* Get the service name from the module name */
132 Start = wcsrchr(ModuleObject->BaseName.Buffer, L'\\');
133 if (Start == NULL)
134 Start = ModuleObject->BaseName.Buffer;
135 else
136 Start++;
137
138 Ext = wcsrchr(ModuleObject->BaseName.Buffer, L'.');
139 if (Ext != NULL)
140 Length = Ext - Start;
141 else
142 Length = wcslen(Start);
143
144 wcsncpy(Buffer, Start, Length);
145 RtlCreateUnicodeString(&DeviceNode->ServiceName, Buffer);
146
147 Status = IopInitializeDriver(ModuleObject->EntryPoint, DeviceNode);
148 if (!NT_SUCCESS(Status))
149 {
150 DPRINT1("IopInitializeDriver() failed (Status %lx)\n", Status);
151 ObDereferenceObject(ModuleObject);
152 IopFreeDeviceNode(DeviceNode);
153 }
154
155 return(Status);
156 }
157
158
159 NTSTATUS STDCALL
160 NtUnloadDriver(IN PUNICODE_STRING DriverServiceName)
161 {
162 UNIMPLEMENTED;
163 }
164
165 /* EOF */