ReactOS  0.4.15-dev-3217-gc6d1646
ramdisk.c File Reference
#include <ntoskrnl.h>
#include <initguid.h>
#include <ntddrdsk.h>
#include <debug.h>
Include dependency graph for ramdisk.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS NTAPI IopStartRamdisk (IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 

Variables

KEVENT PiEnumerationFinished
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file ramdisk.c.

Function Documentation

◆ IopStartRamdisk()

NTSTATUS NTAPI IopStartRamdisk ( IN PLOADER_PARAMETER_BLOCK  LoaderBlock)

Definition at line 26 of file ramdisk.c.

27 {
30  PCHAR CommandLine, Offset, OffsetValue, Length, LengthValue;
32  RAMDISK_CREATE_INPUT RamdiskCreate;
35  PLIST_ENTRY ListHead, NextEntry;
37  WCHAR SourceString[54];
38 
39  //
40  // Scan memory descriptors
41  //
43  ListHead = &LoaderBlock->MemoryDescriptorListHead;
44  NextEntry = ListHead->Flink;
45  while (NextEntry != ListHead)
46  {
47  //
48  // Get the descriptor
49  //
52  ListEntry);
53 
54  //
55  // Needs to be a ROM/RAM descriptor
56  //
57  if (MemoryDescriptor->MemoryType == LoaderXIPRom) break;
58 
59  //
60  // Keep trying
61  //
62  NextEntry = NextEntry->Flink;
63  }
64 
65  //
66  // Nothing found?
67  //
68  if (NextEntry == ListHead)
69  {
70  //
71  // Bugcheck -- no data
72  //
73  KeBugCheckEx(RAMDISK_BOOT_INITIALIZATION_FAILED,
76  0,
77  0);
78  }
79 
80  //
81  // Setup the input buffer
82  //
83  RtlZeroMemory(&RamdiskCreate, sizeof(RamdiskCreate));
84  RamdiskCreate.Version = sizeof(RamdiskCreate);
85  RamdiskCreate.DiskType = RAMDISK_BOOT_DISK;
86  RamdiskCreate.BasePage = MemoryDescriptor->BasePage;
87  RamdiskCreate.DiskOffset = 0;
88  RamdiskCreate.DiskLength.QuadPart = MemoryDescriptor->PageCount << PAGE_SHIFT;
89  RamdiskCreate.DiskGuid = RAMDISK_BOOTDISK_GUID;
90  RamdiskCreate.DriveLetter = L'C';
91  RamdiskCreate.Options.Fixed = TRUE;
92 
93  //
94  // Check for commandline parameters
95  //
96  CommandLine = LoaderBlock->LoadOptions;
97  if (CommandLine)
98  {
99  //
100  // Make everything upper case
101  //
102  _strupr(CommandLine);
103 
104  //
105  // Check for offset parameter
106  //
107  Offset = strstr(CommandLine, "RDIMAGEOFFSET");
108  if (Offset)
109  {
110  //
111  // Get to the actual value
112  //
113  OffsetValue = strstr(Offset, "=");
114  if (OffsetValue)
115  {
116  //
117  // Set the offset
118  //
119  RamdiskCreate.DiskOffset = atol(OffsetValue + 1);
120  }
121  }
122 
123  //
124  // Reduce the disk length
125  //
126  RamdiskCreate.DiskLength.QuadPart -= RamdiskCreate.DiskOffset;
127 
128  //
129  // Check for length parameter
130  //
131  Length = strstr(CommandLine, "RDIMAGELENGTH");
132  if (Length)
133  {
134  //
135  // Get to the actual value
136  //
137  LengthValue = strstr(Length, "=");
138  if (LengthValue)
139  {
140  //
141  // Set the offset
142  //
143  RamdiskCreate.DiskLength.QuadPart = _atoi64(LengthValue + 1);
144  }
145  }
146  }
147 
148  //
149  // Setup object attributes
150  //
151  RtlInitUnicodeString(&ObjectName, L"\\Device\\Ramdisk");
153  &ObjectName,
155  NULL,
156  NULL);
157 
158  //
159  // Open a handle to the driver
160  //
164  &IoStatusBlock,
168  {
169  //
170  // Bugcheck -- no driver
171  //
172  KeBugCheckEx(RAMDISK_BOOT_INITIALIZATION_FAILED,
175  0,
176  0);
177  }
178 
179  //
180  // Send create command
181  //
183  NULL,
184  NULL,
185  NULL,
186  &IoStatusBlock,
188  &RamdiskCreate,
189  sizeof(RamdiskCreate),
190  NULL,
191  0);
194  {
195  //
196  // Bugcheck -- driver failed
197  //
198  KeBugCheckEx(RAMDISK_BOOT_INITIALIZATION_FAILED,
201  0,
202  0);
203  }
204 
205  //
206  // Convert the GUID
207  //
208  Status = RtlStringFromGUID(&RamdiskCreate.DiskGuid, &GuidString);
209  if (!NT_SUCCESS(Status))
210  {
211  //
212  // Bugcheck -- GUID convert failed
213  //
214  KeBugCheckEx(RAMDISK_BOOT_INITIALIZATION_FAILED,
216  Status,
217  0,
218  0);
219  }
220 
221  //
222  // Build the symbolic link name and target
223  //
225  sizeof(SourceString)/sizeof(WCHAR),
226  L"\\Device\\Ramdisk%wZ",
227  &GuidString);
228  SymbolicLinkName.Length = 38;
229  SymbolicLinkName.MaximumLength = 38 + sizeof(UNICODE_NULL);
230  SymbolicLinkName.Buffer = L"\\ArcName\\ramdisk(0)";
231 
232  //
233  // Create the symbolic link
234  //
235  RtlInitUnicodeString(&DeviceString, SourceString);
236  Status = IoCreateSymbolicLink(&SymbolicLinkName, &DeviceString);
238  if (!NT_SUCCESS(Status))
239  {
240  //
241  // Bugcheck -- symlink create failed
242  //
243  KeBugCheckEx(RAMDISK_BOOT_INITIALIZATION_FAILED,
245  Status,
246  0,
247  0);
248  }
249 
250  //
251  // ReactOS hack (drive letter should not be hardcoded, and maybe set by mountmgr.sys)
252  //
253  {
254  ANSI_STRING AnsiPath;
255  CHAR Buffer[256];
257  UNICODE_STRING DriveLetter = RTL_CONSTANT_STRING(L"\\??\\X:");
258 
259  AnsiPath.Length = sprintf(Buffer, "X:%s", LoaderBlock->NtBootPathName);
260  AnsiPath.MaximumLength = AnsiPath.Length + 1;
261  AnsiPath.Buffer = Buffer;
262  RtlInitEmptyUnicodeString(&NtSystemRoot,
263  SharedUserData->NtSystemRoot,
264  sizeof(SharedUserData->NtSystemRoot));
266  if (!NT_SUCCESS(Status))
267  {
268  KeBugCheckEx(RAMDISK_BOOT_INITIALIZATION_FAILED,
270  Status,
271  0,
272  0);
273  }
274  IoCreateSymbolicLink(&DriveLetter, &DeviceString);
275  }
276 
277  //
278  // Wait for ramdisk relations being initialized
279  //
280 
282 
283  //
284  // We made it
285  //
286  return STATUS_SUCCESS;
287 }
signed char * PCHAR
Definition: retypes.h:7
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
static PWSTR GuidString
Definition: apphelp.c:93
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define GENERIC_ALL
Definition: nt_native.h:92
#define FSCTL_CREATE_RAM_DISK
Definition: ntddrdsk.h:45
NTSYSAPI NTSTATUS NTAPI ZwDeviceIoControlFile(IN HANDLE DeviceHandle, IN HANDLE Event OPTIONAL, IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL, IN PVOID UserApcContext OPTIONAL, OUT PIO_STATUS_BLOCK IoStatusBlock, IN ULONG IoControlCode, IN PVOID InputBuffer, IN ULONG InputBufferSize, OUT PVOID OutputBuffer, IN ULONG OutputBufferSize)
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
UNICODE_STRING NtSystemRoot
Definition: init.c:73
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
__MINGW_EXTENSION _Check_return_ _CRTIMP __int64 __cdecl _atoi64(_In_z_ const char *_String)
#define TRUE
Definition: types.h:120
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
#define RD_NO_RAMDISK_DRIVER
Definition: io.h:70
char CHAR
Definition: xmlstorage.h:175
LONG NTSTATUS
Definition: precomp.h:26
#define RD_NO_XIPROM_DESCRIPTOR
Definition: io.h:66
_Must_inspect_result_ _In_ WDFUSBDEVICE _In_opt_ WDFREQUEST _In_opt_ PWDF_REQUEST_SEND_OPTIONS _In_ PWDF_USB_CONTROL_SETUP_PACKET _In_opt_ PWDF_MEMORY_DESCRIPTOR MemoryDescriptor
Definition: wdfusb.h:1331
#define RD_SYSROOT_INIT_FAILED
Definition: io.h:86
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
_In_ PVOID _Out_opt_ PULONG_PTR _Outptr_opt_ PCUNICODE_STRING * ObjectName
Definition: cmfuncs.h:62
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define FILE_SHARE_READ
Definition: compat.h:136
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define FALSE
Definition: types.h:117
#define UNICODE_NULL
int _snwprintf(wchar_t *buffer, size_t count, const wchar_t *format,...)
Definition: bufpool.h:45
static NDIS_HANDLE DriverHandle
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define RAMDISK_BOOT_DISK
Definition: ntddrdsk.h:52
Status
Definition: gdiplustypes.h:24
USHORT MaximumLength
Definition: env_spec_w32.h:377
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define RD_GUID_CONVERT_FAILED
Definition: io.h:78
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
_Check_return_ long __cdecl atol(_In_z_ const char *_Str)
ULONG_PTR BasePage
Definition: ntddrdsk.h:89
RAMDISK_CREATE_OPTIONS Options
Definition: ntddrdsk.h:76
NTSYSAPI NTSTATUS NTAPI ZwOpenFile(_Out_ PHANDLE FileHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes, _Out_ PIO_STATUS_BLOCK IoStatusBlock, _In_ ULONG ShareAccess, _In_ ULONG OpenOptions)
#define SharedUserData
_Out_ _Inout_ POEM_STRING _In_ PCUNICODE_STRING SourceString
Definition: rtlfuncs.h:1868
static const WCHAR L[]
Definition: oid.c:1250
Definition: typedefs.h:119
#define SYNCHRONIZE
Definition: nt_native.h:61
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
_CRTIMP char *__cdecl _strupr(_Inout_z_ char *_String)
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
KEVENT PiEnumerationFinished
Definition: devaction.c:50
NTSYSAPI NTSTATUS WINAPI RtlStringFromGUID(REFGUID, PUNICODE_STRING)
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define NULL
Definition: types.h:112
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define RD_SYMLINK_CREATE_FAILED
Definition: io.h:82
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define STATUS_SUCCESS
Definition: shellext.h:65
#define RD_FSCTL_FAILED
Definition: io.h:74
VOID NTAPI KeBugCheckEx(_In_ ULONG BugCheckCode, _In_ ULONG_PTR BugCheckParameter1, _In_ ULONG_PTR BugCheckParameter2, _In_ ULONG_PTR BugCheckParameter3, _In_ ULONG_PTR BugCheckParameter4)
Definition: rtlcompat.c:108
LARGE_INTEGER DiskLength
Definition: ntddrdsk.h:77
LONGLONG QuadPart
Definition: typedefs.h:114
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING SymbolicLinkName
Definition: wdfdevice.h:3736
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14

Referenced by IoInitSystem().

Variable Documentation

◆ PiEnumerationFinished

KEVENT PiEnumerationFinished

Definition at line 50 of file devaction.c.

Referenced by IopStartRamdisk().