2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/iomgr/arcname.c
5 * PURPOSE: ARC Path Initialization Functions
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Eric Kohl (ekohl@rz-online.de)
10 /* INCLUDES ******************************************************************/
16 /* GLOBALS *******************************************************************/
18 UNICODE_STRING IoArcHalDeviceName
, IoArcBootDeviceName
;
19 PCHAR IoLoaderArcBootDeviceName
;
21 /* FUNCTIONS *****************************************************************/
26 IopApplyRosCdromArcHack(IN ULONG i
)
28 ULONG DeviceNumber
= -1;
29 OBJECT_ATTRIBUTES ObjectAttributes
;
30 ANSI_STRING InstallName
;
31 UNICODE_STRING DeviceName
;
33 FILE_BASIC_INFORMATION FileInfo
;
36 PCONFIGURATION_INFORMATION ConfigInfo
= IoGetConfigurationInformation();
37 extern BOOLEAN InitIsWinPEMode
, ExpInTextModeSetup
;
39 /* Only ARC Name left - Build full ARC Name */
40 p
= strstr(KeLoaderBlock
->ArcBootDeviceName
, "cdrom");
43 /* Build installer name */
44 sprintf(Buffer
, "\\Device\\CdRom%lu\\reactos\\ntoskrnl.exe", i
);
45 RtlInitAnsiString(&InstallName
, Buffer
);
46 Status
= RtlAnsiStringToUnicodeString(&DeviceName
, &InstallName
, TRUE
);
47 if (!NT_SUCCESS(Status
)) return FALSE
;
49 /* Try to find the installer */
50 InitializeObjectAttributes(&ObjectAttributes
,
55 Status
= ZwQueryAttributesFile(&ObjectAttributes
, &FileInfo
);
58 RtlFreeUnicodeString(&DeviceName
);
60 /* Check if we found the file */
61 if (NT_SUCCESS(Status
))
63 /* We did, save the device number */
68 /* Build live CD kernel name */
70 "\\Device\\CdRom%lu\\reactos\\system32\\ntoskrnl.exe",
72 RtlInitAnsiString(&InstallName
, Buffer
);
73 Status
= RtlAnsiStringToUnicodeString(&DeviceName
,
76 if (!NT_SUCCESS(Status
)) return FALSE
;
79 InitializeObjectAttributes(&ObjectAttributes
,
84 Status
= ZwQueryAttributesFile(&ObjectAttributes
, &FileInfo
);
85 if (NT_SUCCESS(Status
)) DeviceNumber
= i
;
88 RtlFreeUnicodeString(&DeviceName
);
94 sprintf(p
, "cdrom(%lu)", DeviceNumber
);
96 /* Adjust original command line */
102 sprintf(p
, "cdrom(%lu)", DeviceNumber
);
108 /* OK, how many disks are there? */
109 DeviceNumber
+= ConfigInfo
->DiskCount
;
111 /* Return whether this is the CD or not */
112 if ((InitIsWinPEMode
) || (ExpInTextModeSetup
))
114 /* Hack until IoAssignDriveLetters is fixed */
115 swprintf(SharedUserData
->NtSystemRoot
, L
"%c:\\reactos", 'C' + DeviceNumber
);
126 IopGetDiskInformation(IN ULONG i
,
128 OUT PULONG Signature
,
129 OUT PULONG PartitionCount
,
130 OUT PDEVICE_OBJECT
*DiskDeviceObject
)
133 ANSI_STRING TempString
;
135 UNICODE_STRING DeviceName
;
137 PDEVICE_OBJECT DeviceObject
;
138 PFILE_OBJECT FileObject
;
139 DISK_GEOMETRY DiskGeometry
;
140 PDRIVE_LAYOUT_INFORMATION DriveLayout
;
143 IO_STATUS_BLOCK StatusBlock
;
144 LARGE_INTEGER PartitionOffset
;
145 PULONG PartitionBuffer
;
148 sprintf(Buffer
, "\\Device\\Harddisk%lu\\Partition0", i
);
150 /* Convert it to Unicode */
151 RtlInitAnsiString(&TempString
, Buffer
);
152 Status
= RtlAnsiStringToUnicodeString(&DeviceName
, &TempString
, TRUE
);
153 if (!NT_SUCCESS(Status
)) return FALSE
;
155 /* Get the device pointer */
156 Status
= IoGetDeviceObjectPointer(&DeviceName
,
157 FILE_READ_ATTRIBUTES
,
160 *DiskDeviceObject
= DeviceObject
;
162 /* Free the string */
163 RtlFreeUnicodeString(&DeviceName
);
165 /* Move on if we failed */
166 if (!NT_SUCCESS(Status
)) return FALSE
;
168 /* Build an IRP to determine the sector size */
169 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
170 Irp
= IoBuildDeviceIoControlRequest(IOCTL_DISK_GET_DRIVE_GEOMETRY
,
175 sizeof(DISK_GEOMETRY
),
182 ObDereferenceObject(FileObject
);
186 /* Call the driver and check if we have to wait on it */
187 Status
= IoCallDriver(DeviceObject
, Irp
);
188 if (Status
== STATUS_PENDING
)
190 /* Wait on the driver */
191 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
192 Status
= StatusBlock
.Status
;
195 /* Check if we failed */
196 if (!NT_SUCCESS(Status
))
199 ObDereferenceObject(FileObject
);
203 /* Read the partition table */
204 Status
= IoReadPartitionTable(DeviceObject
,
205 DiskGeometry
.BytesPerSector
,
209 /* Dereference the file object */
210 ObDereferenceObject(FileObject
);
211 if (!NT_SUCCESS(Status
)) return FALSE
;
213 /* Set the offset to 0 */
214 PartitionOffset
.QuadPart
= 0;
216 /* Allocate a buffer for the partition */
217 PartitionBuffer
= ExAllocatePoolWithTag(NonPagedPool
,
218 DiskGeometry
.BytesPerSector
,
220 if (!PartitionBuffer
) return FALSE
;
222 /* Build an IRP to read the partition sector */
223 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
224 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
227 DiskGeometry
.BytesPerSector
,
232 /* Call the driver and check if we have to wait */
233 Status
= IoCallDriver(DeviceObject
, Irp
);
234 if (Status
== STATUS_PENDING
)
236 /* Wait for completion */
237 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
238 Status
= StatusBlock
.Status
;
241 /* Check if we failed */
242 if (!NT_SUCCESS(Status
))
245 ExFreePoolWithTag(PartitionBuffer
, TAG_IO
);
246 ExFreePoolWithTag(DriveLayout
, TAG_FILE_SYSTEM
);
250 /* Calculate the MBR checksum */
252 for (j
= 0; j
< 128; j
++) Checksum
+= PartitionBuffer
[j
];
254 /* Save the signature and checksum */
255 *CheckSum
= ~Checksum
+ 1;
256 *Signature
= DriveLayout
->Signature
;
257 *PartitionCount
= DriveLayout
->PartitionCount
;
259 /* Free the buffer */
260 ExFreePoolWithTag(PartitionBuffer
, TAG_IO
);
261 ExFreePoolWithTag(DriveLayout
, TAG_FILE_SYSTEM
);
268 IopAssignArcNamesToCdrom(IN PULONG Buffer
,
272 ANSI_STRING TempString
;
273 UNICODE_STRING DeviceName
, ArcName
;
275 LARGE_INTEGER PartitionOffset
;
277 IO_STATUS_BLOCK IoStatusBlock
;
279 ULONG i
, CheckSum
= 0;
280 PDEVICE_OBJECT DeviceObject
;
281 PFILE_OBJECT FileObject
;
283 /* Build the device name */
284 sprintf(ArcBuffer
, "\\Device\\CdRom%lu", DiskNumber
);
286 /* Convert it to Unicode */
287 RtlInitAnsiString(&TempString
, ArcBuffer
);
288 Status
= RtlAnsiStringToUnicodeString(&DeviceName
, &TempString
, TRUE
);
289 if (!NT_SUCCESS(Status
)) return FALSE
;
291 /* Get the device for it */
292 Status
= IoGetDeviceObjectPointer(&DeviceName
,
293 FILE_READ_ATTRIBUTES
,
296 if (!NT_SUCCESS(Status
))
298 /* Free the string and fail */
299 RtlFreeUnicodeString(&DeviceName
);
303 /* Setup the event */
304 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
306 /* Set the offset and build the read IRP */
307 PartitionOffset
.QuadPart
= 0x8000;
308 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
317 /* Free the string and fail */
318 RtlFreeUnicodeString(&DeviceName
);
322 /* Call the driver and check if we have to wait on it */
323 Status
= IoCallDriver(DeviceObject
, Irp
);
324 if (Status
== STATUS_PENDING
)
326 /* Wait for completion */
327 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
328 Status
= IoStatusBlock
.Status
;
331 /* Dereference the file object */
332 ObDereferenceObject(FileObject
);
333 if (!NT_SUCCESS(Status
)) return FALSE
;
335 /* Now calculate the checksum */
336 for (i
= 0; i
< 2048 / sizeof(ULONG
); i
++) CheckSum
+= Buffer
[i
];
339 * FIXME: In normal conditions, NTLDR/FreeLdr sends the *proper* CDROM
340 * ARC Path name, and what happens here is a comparision of both checksums
341 * in order to see if this is the actual boot CD.
343 * In ReactOS this doesn't currently happen, instead we have a hack on top
344 * of this file which scans the CD for the ntoskrnl.exe file, then modifies
345 * the LoaderBlock's ARC Path with the right CDROM path. Consequently, we
346 * get the same state as if NTLDR had properly booted us, except that we do
347 * not actually need to check the signature, since the hack already did the
348 * check for ntoskrnl.exe, which is just as good.
350 * The signature code stays however, because eventually FreeLDR will work
351 * like NTLDR, and, conversly, we do want to be able to be booted by NTLDR.
353 if (IopApplyRosCdromArcHack(DiskNumber
))
355 /* This is the boot CD-ROM, build the ARC name */
356 sprintf(ArcBuffer
, "\\ArcName\\%s", KeLoaderBlock
->ArcBootDeviceName
);
358 /* Convert it to Unicode */
359 RtlInitAnsiString(&TempString
, ArcBuffer
);
360 Status
= RtlAnsiStringToUnicodeString(&ArcName
, &TempString
, TRUE
);
361 if (!NT_SUCCESS(Status
)) return FALSE
;
363 /* Create the symbolic link and free the strings */
364 IoAssignArcName(&ArcName
, &DeviceName
);
365 RtlFreeUnicodeString(&ArcName
);
366 RtlFreeUnicodeString(&DeviceName
);
368 /* Let caller know that we've found the boot CD */
372 /* No boot CD found */
379 IopCreateArcNames(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
381 PCONFIGURATION_INFORMATION ConfigInfo
= IoGetConfigurationInformation();
382 PARC_DISK_INFORMATION ArcDiskInfo
= LoaderBlock
->ArcDiskInformation
;
384 ANSI_STRING ArcBootString
, ArcSystemString
, ArcString
;
385 UNICODE_STRING ArcName
, BootPath
, DeviceName
;
388 PDEVICE_OBJECT DeviceObject
;
389 ULONG Signature
, Checksum
, PartitionCount
;
390 PLIST_ENTRY NextEntry
;
391 PARC_DISK_SIGNATURE ArcDiskEntry
;
393 BOOLEAN FoundBoot
= FALSE
;
394 PULONG PartitionBuffer
;
396 /* Check if we only have one disk on the machine */
397 SingleDisk
= ArcDiskInfo
->DiskSignatureListHead
.Flink
->Flink
==
398 (&ArcDiskInfo
->DiskSignatureListHead
);
400 /* Create the global HAL partition name */
401 sprintf(Buffer
, "\\ArcName\\%s", LoaderBlock
->ArcHalDeviceName
);
402 RtlInitAnsiString(&ArcString
, Buffer
);
403 RtlAnsiStringToUnicodeString(&IoArcHalDeviceName
, &ArcString
, TRUE
);
405 /* Create the global system partition name */
406 sprintf(Buffer
, "\\ArcName\\%s", LoaderBlock
->ArcBootDeviceName
);
407 RtlInitAnsiString(&ArcString
, Buffer
);
408 RtlAnsiStringToUnicodeString(&IoArcBootDeviceName
, &ArcString
, TRUE
);
410 /* Allocate memory for the string */
411 Length
= strlen(LoaderBlock
->ArcBootDeviceName
) + sizeof(ANSI_NULL
);
412 IoLoaderArcBootDeviceName
= ExAllocatePoolWithTag(PagedPool
,
415 if (IoLoaderArcBootDeviceName
)
418 RtlCopyMemory(IoLoaderArcBootDeviceName
,
419 LoaderBlock
->ArcBootDeviceName
,
423 /* Check if we only found a disk, but we're booting from CD-ROM */
424 if ((SingleDisk
) && strstr(LoaderBlock
->ArcBootDeviceName
, "cdrom"))
426 /* Then disable single-disk mode, since there's a CD drive out there */
430 /* Build the boot strings */
431 RtlInitAnsiString(&ArcBootString
, LoaderBlock
->ArcBootDeviceName
);
432 RtlInitAnsiString(&ArcSystemString
, LoaderBlock
->ArcHalDeviceName
);
434 /* Loop every detected disk */
435 for (i
= 0; i
< ConfigInfo
->DiskCount
; i
++)
437 /* Get information about the disk */
438 if (!IopGetDiskInformation(i
,
449 for (NextEntry
= ArcDiskInfo
->DiskSignatureListHead
.Flink
;
450 NextEntry
!= &ArcDiskInfo
->DiskSignatureListHead
;
451 NextEntry
= NextEntry
->Flink
)
453 /* Get the current ARC disk signature entry */
454 ArcDiskEntry
= CONTAINING_RECORD(NextEntry
,
459 * Now check if the signature and checksum match, unless this is
460 * the only disk that was in the ARC list, and also in the device
461 * tree, in which case the check is bypassed and we accept the disk
463 if (((SingleDisk
) && (ConfigInfo
->DiskCount
== 1)) ||
464 ((Checksum
== ArcDiskEntry
->CheckSum
) &&
465 (Signature
== ArcDiskEntry
->Signature
)))
467 /* Build the NT Device Name */
468 sprintf(Buffer
, "\\Device\\Harddisk%lu\\Partition0", i
);
470 /* Convert it to Unicode */
471 RtlInitAnsiString(&ArcString
, Buffer
);
472 Status
= RtlAnsiStringToUnicodeString(&DeviceName
,
475 if (!NT_SUCCESS(Status
)) continue;
477 /* Build the ARC Device Name */
478 sprintf(Buffer
, "\\ArcName\\%s", ArcDiskEntry
->ArcName
);
480 /* Convert it to Unicode */
481 RtlInitAnsiString(&ArcString
, Buffer
);
482 Status
= RtlAnsiStringToUnicodeString(&ArcName
,
485 if (!NT_SUCCESS(Status
)) continue;
487 /* Create the symbolic link and free the strings */
488 IoAssignArcName(&ArcName
, &DeviceName
);
489 RtlFreeUnicodeString(&ArcName
);
490 RtlFreeUnicodeString(&DeviceName
);
492 /* Loop all the partitions */
493 for (j
= 0; j
< PartitionCount
; j
++)
495 /* Build the partition device name */
497 "\\Device\\Harddisk%lu\\Partition%lu",
501 /* Convert it to Unicode */
502 RtlInitAnsiString(&ArcString
, Buffer
);
503 Status
= RtlAnsiStringToUnicodeString(&DeviceName
,
506 if (!NT_SUCCESS(Status
)) continue;
508 /* Build the partial ARC name for this partition */
511 ArcDiskEntry
->ArcName
,
513 RtlInitAnsiString(&ArcString
, Buffer
);
515 /* Check if this is the boot device */
516 if (RtlEqualString(&ArcString
, &ArcBootString
, TRUE
))
518 /* Remember that we found a Hard Disk Boot Device */
522 /* Check if it's the system boot partition */
523 if (RtlEqualString(&ArcString
, &ArcSystemString
, TRUE
))
525 /* It is, create a Unicode string for it */
526 RtlInitAnsiString(&ArcString
,
527 LoaderBlock
->NtHalPathName
);
528 Status
= RtlAnsiStringToUnicodeString(&BootPath
,
531 if (NT_SUCCESS(Status
))
533 /* FIXME: Save in registry */
535 /* Free the string now */
536 RtlFreeUnicodeString(&BootPath
);
540 /* Build the full ARC name */
542 "\\ArcName\\%spartition(%lu)",
543 ArcDiskEntry
->ArcName
,
546 /* Convert it to Unicode */
547 RtlInitAnsiString(&ArcString
, Buffer
);
548 Status
= RtlAnsiStringToUnicodeString(&ArcName
,
551 if (!NT_SUCCESS(Status
)) continue;
553 /* Create the symbolic link and free the strings */
554 IoAssignArcName(&ArcName
, &DeviceName
);
555 RtlFreeUnicodeString(&ArcName
);
556 RtlFreeUnicodeString(&DeviceName
);
562 /* Check if we didn't find the boot disk */
565 /* Allocate a buffer for the CD-ROM MBR */
566 PartitionBuffer
= ExAllocatePoolWithTag(NonPagedPool
, 2048, TAG_IO
);
567 if (!PartitionBuffer
) return STATUS_INSUFFICIENT_RESOURCES
;
569 /* Loop every CD-ROM */
570 for (i
= 0; i
< ConfigInfo
->CdRomCount
; i
++)
572 /* Give it an ARC name */
573 if (IopAssignArcNamesToCdrom(PartitionBuffer
, i
)) break;
576 /* Free the buffer */
577 ExFreePoolWithTag(PartitionBuffer
, TAG_IO
);
581 return STATUS_SUCCESS
;
586 IopReassignSystemRoot(IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
587 OUT PANSI_STRING NtBootPath
)
589 OBJECT_ATTRIBUTES ObjectAttributes
;
591 CHAR Buffer
[256], AnsiBuffer
[256];
592 WCHAR ArcNameBuffer
[64];
593 ANSI_STRING TargetString
, ArcString
, TempString
;
594 UNICODE_STRING LinkName
, TargetName
, ArcName
;
597 /* Create the Unicode name for the current ARC boot device */
598 sprintf(Buffer
, "\\ArcName\\%s", LoaderBlock
->ArcBootDeviceName
);
599 RtlInitAnsiString(&TargetString
, Buffer
);
600 Status
= RtlAnsiStringToUnicodeString(&TargetName
, &TargetString
, TRUE
);
601 if (!NT_SUCCESS(Status
)) return FALSE
;
603 /* Initialize the attributes and open the link */
604 InitializeObjectAttributes(&ObjectAttributes
,
606 OBJ_CASE_INSENSITIVE
,
609 Status
= NtOpenSymbolicLinkObject(&LinkHandle
,
610 SYMBOLIC_LINK_ALL_ACCESS
,
612 if (!NT_SUCCESS(Status
))
614 /* We failed, free the string */
615 RtlFreeUnicodeString(&TargetName
);
619 /* Query the current \\SystemRoot */
620 ArcName
.Buffer
= ArcNameBuffer
;
622 ArcName
.MaximumLength
= sizeof(ArcNameBuffer
);
623 Status
= NtQuerySymbolicLinkObject(LinkHandle
, &ArcName
, NULL
);
624 if (!NT_SUCCESS(Status
))
626 /* We failed, free the string */
627 RtlFreeUnicodeString(&TargetName
);
631 /* Convert it to Ansi */
632 ArcString
.Buffer
= AnsiBuffer
;
633 ArcString
.Length
= 0;
634 ArcString
.MaximumLength
= sizeof(AnsiBuffer
);
635 Status
= RtlUnicodeStringToAnsiString(&ArcString
, &ArcName
, FALSE
);
636 AnsiBuffer
[ArcString
.Length
] = ANSI_NULL
;
638 /* Close the link handle and free the name */
639 ObCloseHandle(LinkHandle
, KernelMode
);
640 RtlFreeUnicodeString(&TargetName
);
642 /* Setup the system root name again */
643 RtlInitAnsiString(&TempString
, "\\SystemRoot");
644 Status
= RtlAnsiStringToUnicodeString(&LinkName
, &TempString
, TRUE
);
645 if (!NT_SUCCESS(Status
)) return FALSE
;
647 /* Open the symbolic link for it */
648 InitializeObjectAttributes(&ObjectAttributes
,
650 OBJ_CASE_INSENSITIVE
,
653 Status
= NtOpenSymbolicLinkObject(&LinkHandle
,
654 SYMBOLIC_LINK_ALL_ACCESS
,
656 if (!NT_SUCCESS(Status
)) return FALSE
;
659 NtMakeTemporaryObject(LinkHandle
);
660 ObCloseHandle(LinkHandle
, KernelMode
);
662 /* Now create the new name for it */
663 sprintf(Buffer
, "%s%s", ArcString
.Buffer
, LoaderBlock
->NtBootPathName
);
665 /* Copy it into the passed parameter and null-terminate it */
666 RtlCopyString(NtBootPath
, &ArcString
);
667 Buffer
[strlen(Buffer
) - 1] = ANSI_NULL
;
669 /* Setup the Unicode-name for the new symbolic link value */
670 RtlInitAnsiString(&TargetString
, Buffer
);
671 InitializeObjectAttributes(&ObjectAttributes
,
673 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
676 Status
= RtlAnsiStringToUnicodeString(&ArcName
, &TargetString
, TRUE
);
677 if (!NT_SUCCESS(Status
)) return FALSE
;
680 Status
= NtCreateSymbolicLinkObject(&LinkHandle
,
681 SYMBOLIC_LINK_ALL_ACCESS
,
685 /* Free all the strings and close the handle and return success */
686 RtlFreeUnicodeString(&ArcName
);
687 RtlFreeUnicodeString(&LinkName
);
688 ObCloseHandle(LinkHandle
, KernelMode
);