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)
10 /* INCLUDES ******************************************************************/
16 /* GLOBALS *******************************************************************/
18 UNICODE_STRING IoArcHalDeviceName
, IoArcBootDeviceName
;
19 PCHAR IoLoaderArcBootDeviceName
;
20 extern PROS_LOADER_PARAMETER_BLOCK KeRosLoaderBlock
;
22 /* FUNCTIONS *****************************************************************/
27 IopApplyRosCdromArcHack(IN ULONG i
)
29 ULONG DeviceNumber
= -1;
30 OBJECT_ATTRIBUTES ObjectAttributes
;
31 ANSI_STRING InstallName
;
32 UNICODE_STRING DeviceName
;
33 CHAR Buffer
[128], RosSysPath
[16];
34 FILE_BASIC_INFORMATION FileInfo
;
37 PCONFIGURATION_INFORMATION ConfigInfo
= IoGetConfigurationInformation();
38 extern BOOLEAN InitIsWinPEMode
, ExpInTextModeSetup
;
40 /* Change this if you want ROS to boot properly from another directory */
41 sprintf(RosSysPath
, "%s", "reactos");
43 /* Only ARC Name left - Build full ARC Name */
44 p
= strstr(KeLoaderBlock
->ArcBootDeviceName
, "cdrom");
47 /* Build installer name */
48 sprintf(Buffer
, "\\Device\\CdRom%lu\\%s\\ntoskrnl.exe", i
, RosSysPath
);
49 RtlInitAnsiString(&InstallName
, Buffer
);
50 Status
= RtlAnsiStringToUnicodeString(&DeviceName
, &InstallName
, TRUE
);
51 if (!NT_SUCCESS(Status
)) return FALSE
;
53 /* Try to find the installer */
54 InitializeObjectAttributes(&ObjectAttributes
,
59 Status
= ZwQueryAttributesFile(&ObjectAttributes
, &FileInfo
);
62 RtlFreeUnicodeString(&DeviceName
);
64 /* Check if we found the file */
65 if (NT_SUCCESS(Status
))
67 /* We did, save the device number */
72 /* Build live CD kernel name */
74 "\\Device\\CdRom%lu\\%s\\system32\\ntoskrnl.exe",
76 RtlInitAnsiString(&InstallName
, Buffer
);
77 Status
= RtlAnsiStringToUnicodeString(&DeviceName
,
80 if (!NT_SUCCESS(Status
)) return FALSE
;
83 InitializeObjectAttributes(&ObjectAttributes
,
88 Status
= ZwQueryAttributesFile(&ObjectAttributes
, &FileInfo
);
89 if (NT_SUCCESS(Status
)) DeviceNumber
= i
;
92 RtlFreeUnicodeString(&DeviceName
);
98 sprintf(p
, "cdrom(%lu)", DeviceNumber
);
100 /* Adjust original command line */
106 sprintf(p
, "cdrom(%lu)", DeviceNumber
);
112 /* OK, how many disks are there? */
113 DeviceNumber
+= ConfigInfo
->DiskCount
;
115 /* Return whether this is the CD or not */
116 if ((InitIsWinPEMode
) || (ExpInTextModeSetup
))
128 IopGetDiskInformation(IN ULONG i
,
130 OUT PULONG Signature
,
131 OUT PULONG PartitionCount
,
132 OUT PDEVICE_OBJECT
*DiskDeviceObject
)
135 ANSI_STRING TempString
;
137 UNICODE_STRING DeviceName
;
139 PDEVICE_OBJECT DeviceObject
;
140 PFILE_OBJECT FileObject
;
141 DISK_GEOMETRY DiskGeometry
;
142 PDRIVE_LAYOUT_INFORMATION DriveLayout
;
145 IO_STATUS_BLOCK StatusBlock
;
146 LARGE_INTEGER PartitionOffset
;
147 PULONG PartitionBuffer
;
150 sprintf(Buffer
, "\\Device\\Harddisk%lu\\Partition0", i
);
152 /* Convert it to Unicode */
153 RtlInitAnsiString(&TempString
, Buffer
);
154 Status
= RtlAnsiStringToUnicodeString(&DeviceName
, &TempString
, TRUE
);
155 if (!NT_SUCCESS(Status
)) return FALSE
;
157 /* Get the device pointer */
158 Status
= IoGetDeviceObjectPointer(&DeviceName
,
159 FILE_READ_ATTRIBUTES
,
162 *DiskDeviceObject
= DeviceObject
;
164 /* Free the string */
165 RtlFreeUnicodeString(&DeviceName
);
167 /* Move on if we failed */
168 if (!NT_SUCCESS(Status
)) return FALSE
;
170 /* Build an IRP to determine the sector size */
171 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
172 Irp
= IoBuildDeviceIoControlRequest(IOCTL_DISK_GET_DRIVE_GEOMETRY
,
177 sizeof(DISK_GEOMETRY
),
184 ObDereferenceObject(FileObject
);
188 /* Call the driver and check if we have to wait on it */
189 Status
= IoCallDriver(DeviceObject
, Irp
);
190 if (Status
== STATUS_PENDING
)
192 /* Wait on the driver */
193 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
194 Status
= StatusBlock
.Status
;
197 /* Check if we failed */
198 if (!NT_SUCCESS(Status
))
201 ObDereferenceObject(FileObject
);
205 /* Read the partition table */
206 Status
= IoReadPartitionTable(DeviceObject
,
207 DiskGeometry
.BytesPerSector
,
211 /* Dereference the file object */
212 ObDereferenceObject(FileObject
);
213 if (!NT_SUCCESS(Status
)) return FALSE
;
215 /* Set the offset to 0 */
216 PartitionOffset
.QuadPart
= 0;
218 /* Allocate a buffer for the partition */
219 PartitionBuffer
= ExAllocatePoolWithTag(NonPagedPool
,
220 DiskGeometry
.BytesPerSector
,
222 if (!PartitionBuffer
)
225 ExFreePoolWithTag(DriveLayout
, TAG_FILE_SYSTEM
);
229 /* Build an IRP to read the partition sector */
230 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
231 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
234 DiskGeometry
.BytesPerSector
,
241 ExFreePoolWithTag(PartitionBuffer
, TAG_IO
);
242 ExFreePoolWithTag(DriveLayout
, TAG_FILE_SYSTEM
);
246 /* Call the driver and check if we have to wait */
247 Status
= IoCallDriver(DeviceObject
, Irp
);
248 if (Status
== STATUS_PENDING
)
250 /* Wait for completion */
251 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
252 Status
= StatusBlock
.Status
;
255 /* Check if we failed */
256 if (!NT_SUCCESS(Status
))
259 ExFreePoolWithTag(PartitionBuffer
, TAG_IO
);
260 ExFreePoolWithTag(DriveLayout
, TAG_FILE_SYSTEM
);
264 /* Calculate the MBR checksum */
266 for (j
= 0; j
< 128; j
++) Checksum
+= PartitionBuffer
[j
];
268 /* Save the signature and checksum */
269 *CheckSum
= ~Checksum
+ 1;
270 *Signature
= DriveLayout
->Signature
;
271 *PartitionCount
= DriveLayout
->PartitionCount
;
273 /* Free the buffer */
274 ExFreePoolWithTag(PartitionBuffer
, TAG_IO
);
275 ExFreePoolWithTag(DriveLayout
, TAG_FILE_SYSTEM
);
282 IopAssignArcNamesToCdrom(IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
287 ANSI_STRING TempString
;
288 UNICODE_STRING DeviceName
, ArcName
;
290 LARGE_INTEGER PartitionOffset
;
292 IO_STATUS_BLOCK IoStatusBlock
;
294 ULONG i
, CheckSum
= 0;
295 PDEVICE_OBJECT DeviceObject
;
296 PFILE_OBJECT FileObject
;
297 PARC_DISK_INFORMATION ArcDiskInfo
= LoaderBlock
->ArcDiskInformation
;
298 PLIST_ENTRY NextEntry
;
299 PARC_DISK_SIGNATURE ArcDiskEntry
;
300 BOOLEAN IsBootCdRom
= FALSE
;
302 /* Build the device name */
303 sprintf(ArcBuffer
, "\\Device\\CdRom%lu", DiskNumber
);
305 /* Convert it to Unicode */
306 RtlInitAnsiString(&TempString
, ArcBuffer
);
307 Status
= RtlAnsiStringToUnicodeString(&DeviceName
, &TempString
, TRUE
);
308 if (!NT_SUCCESS(Status
)) return FALSE
;
310 /* Get the device for it */
311 Status
= IoGetDeviceObjectPointer(&DeviceName
,
312 FILE_READ_ATTRIBUTES
,
315 if (!NT_SUCCESS(Status
))
317 /* Free the string and fail */
318 RtlFreeUnicodeString(&DeviceName
);
322 /* Setup the event */
323 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
325 /* Set the offset and build the read IRP */
326 PartitionOffset
.QuadPart
= 0x8000;
327 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
336 /* Free the string and fail */
337 RtlFreeUnicodeString(&DeviceName
);
341 /* Call the driver and check if we have to wait on it */
342 Status
= IoCallDriver(DeviceObject
, Irp
);
343 if (Status
== STATUS_PENDING
)
345 /* Wait for completion */
346 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
347 Status
= IoStatusBlock
.Status
;
350 /* Dereference the file object */
351 ObDereferenceObject(FileObject
);
352 if (!NT_SUCCESS(Status
)) return FALSE
;
354 /* Now calculate the checksum */
355 for (i
= 0; i
< 2048 / sizeof(ULONG
); i
++) CheckSum
+= Buffer
[i
];
358 if (KeRosLoaderBlock
) goto freeldrhack
;
361 /* Search if this device is the actual boot CD */
362 for (NextEntry
= ArcDiskInfo
->DiskSignatureListHead
.Flink
;
363 NextEntry
!= &ArcDiskInfo
->DiskSignatureListHead
;
364 NextEntry
= NextEntry
->Flink
)
366 /* Get the current ARC disk signature entry */
367 ArcDiskEntry
= CONTAINING_RECORD(NextEntry
,
370 /* And check if checksums and arc names match */
371 if (CheckSum
== ArcDiskEntry
->CheckSum
&&
372 strcmp(KeLoaderBlock
->ArcBootDeviceName
, ArcDiskEntry
->ArcName
) == 0)
384 * FIXME: In normal conditions, NTLDR/FreeLdr sends the *proper* CDROM
385 * ARC Path name, and what happens here is a comparision of both checksums
386 * in order to see if this is the actual boot CD.
388 * In ReactOS this doesn't currently happen, instead we have a hack on top
389 * of this file which scans the CD for the ntoskrnl.exe file, then modifies
390 * the LoaderBlock's ARC Path with the right CDROM path. Consequently, we
391 * get the same state as if NTLDR had properly booted us, except that we do
392 * not actually need to check the signature, since the hack already did the
393 * check for ntoskrnl.exe, which is just as good.
395 * The signature code stays however, because eventually FreeLDR will work
396 * like NTLDR, and, conversly, we do want to be able to be booted by NTLDR.
398 IsBootCdRom
= IopApplyRosCdromArcHack(DiskNumber
);
403 /* This is the boot CD-ROM, build the ARC name */
404 sprintf(ArcBuffer
, "\\ArcName\\%s", KeLoaderBlock
->ArcBootDeviceName
);
406 /* Convert it to Unicode */
407 RtlInitAnsiString(&TempString
, ArcBuffer
);
408 Status
= RtlAnsiStringToUnicodeString(&ArcName
, &TempString
, TRUE
);
409 if (!NT_SUCCESS(Status
)) return FALSE
;
411 /* Create the symbolic link and free the strings */
412 IoAssignArcName(&ArcName
, &DeviceName
);
413 RtlFreeUnicodeString(&ArcName
);
414 RtlFreeUnicodeString(&DeviceName
);
416 /* Let caller know that we've found the boot CD */
420 /* No boot CD found */
427 IopCreateArcNames(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
429 PCONFIGURATION_INFORMATION ConfigInfo
= IoGetConfigurationInformation();
430 PARC_DISK_INFORMATION ArcDiskInfo
= LoaderBlock
->ArcDiskInformation
;
432 ANSI_STRING ArcBootString
, ArcSystemString
, ArcString
;
433 UNICODE_STRING ArcName
, BootPath
, DeviceName
;
436 PDEVICE_OBJECT DeviceObject
;
437 ULONG Signature
, Checksum
, PartitionCount
;
438 PLIST_ENTRY NextEntry
;
439 PARC_DISK_SIGNATURE ArcDiskEntry
;
441 BOOLEAN FoundBoot
= FALSE
;
442 PULONG PartitionBuffer
;
444 /* Check if we only have one disk on the machine */
445 SingleDisk
= ArcDiskInfo
->DiskSignatureListHead
.Flink
->Flink
==
446 (&ArcDiskInfo
->DiskSignatureListHead
);
448 /* Create the global HAL partition name */
449 sprintf(Buffer
, "\\ArcName\\%s", LoaderBlock
->ArcHalDeviceName
);
450 RtlInitAnsiString(&ArcString
, Buffer
);
451 RtlAnsiStringToUnicodeString(&IoArcHalDeviceName
, &ArcString
, TRUE
);
453 /* Create the global system partition name */
454 sprintf(Buffer
, "\\ArcName\\%s", LoaderBlock
->ArcBootDeviceName
);
455 RtlInitAnsiString(&ArcString
, Buffer
);
456 RtlAnsiStringToUnicodeString(&IoArcBootDeviceName
, &ArcString
, TRUE
);
458 /* Allocate memory for the string */
459 Length
= strlen(LoaderBlock
->ArcBootDeviceName
) + sizeof(ANSI_NULL
);
460 IoLoaderArcBootDeviceName
= ExAllocatePoolWithTag(PagedPool
,
463 if (IoLoaderArcBootDeviceName
)
466 RtlCopyMemory(IoLoaderArcBootDeviceName
,
467 LoaderBlock
->ArcBootDeviceName
,
471 /* Check if we only found a disk, but we're booting from CD-ROM */
472 if ((SingleDisk
) && strstr(LoaderBlock
->ArcBootDeviceName
, "cdrom"))
474 /* Then disable single-disk mode, since there's a CD drive out there */
478 /* Build the boot strings */
479 RtlInitAnsiString(&ArcBootString
, LoaderBlock
->ArcBootDeviceName
);
480 RtlInitAnsiString(&ArcSystemString
, LoaderBlock
->ArcHalDeviceName
);
482 /* Loop every detected disk */
483 for (i
= 0; i
< ConfigInfo
->DiskCount
; i
++)
485 /* Get information about the disk */
486 if (!IopGetDiskInformation(i
,
497 for (NextEntry
= ArcDiskInfo
->DiskSignatureListHead
.Flink
;
498 NextEntry
!= &ArcDiskInfo
->DiskSignatureListHead
;
499 NextEntry
= NextEntry
->Flink
)
501 /* Get the current ARC disk signature entry */
502 ArcDiskEntry
= CONTAINING_RECORD(NextEntry
,
507 * Now check if the signature and checksum match, unless this is
508 * the only disk that was in the ARC list, and also in the device
509 * tree, in which case the check is bypassed and we accept the disk
511 if (((SingleDisk
) && (ConfigInfo
->DiskCount
== 1)) ||
512 ((Checksum
== ArcDiskEntry
->CheckSum
) &&
513 (Signature
== ArcDiskEntry
->Signature
)))
515 /* Build the NT Device Name */
516 sprintf(Buffer
, "\\Device\\Harddisk%lu\\Partition0", i
);
518 /* Convert it to Unicode */
519 RtlInitAnsiString(&ArcString
, Buffer
);
520 Status
= RtlAnsiStringToUnicodeString(&DeviceName
,
523 if (!NT_SUCCESS(Status
)) continue;
525 /* Build the ARC Device Name */
526 sprintf(Buffer
, "\\ArcName\\%s", ArcDiskEntry
->ArcName
);
528 /* Convert it to Unicode */
529 RtlInitAnsiString(&ArcString
, Buffer
);
530 Status
= RtlAnsiStringToUnicodeString(&ArcName
,
533 if (!NT_SUCCESS(Status
)) continue;
535 /* Create the symbolic link and free the strings */
536 IoAssignArcName(&ArcName
, &DeviceName
);
537 RtlFreeUnicodeString(&ArcName
);
538 RtlFreeUnicodeString(&DeviceName
);
540 /* Loop all the partitions */
541 for (j
= 0; j
< PartitionCount
; j
++)
543 /* Build the partition device name */
545 "\\Device\\Harddisk%lu\\Partition%lu",
549 /* Convert it to Unicode */
550 RtlInitAnsiString(&ArcString
, Buffer
);
551 Status
= RtlAnsiStringToUnicodeString(&DeviceName
,
554 if (!NT_SUCCESS(Status
)) continue;
556 /* Build the partial ARC name for this partition */
559 ArcDiskEntry
->ArcName
,
561 RtlInitAnsiString(&ArcString
, Buffer
);
563 /* Check if this is the boot device */
564 if (RtlEqualString(&ArcString
, &ArcBootString
, TRUE
))
566 /* Remember that we found a Hard Disk Boot Device */
570 /* Check if it's the system boot partition */
571 if (RtlEqualString(&ArcString
, &ArcSystemString
, TRUE
))
573 /* It is, create a Unicode string for it */
574 RtlInitAnsiString(&ArcString
,
575 LoaderBlock
->NtHalPathName
);
576 Status
= RtlAnsiStringToUnicodeString(&BootPath
,
579 if (NT_SUCCESS(Status
))
581 /* FIXME: Save in registry */
583 /* Free the string now */
584 RtlFreeUnicodeString(&BootPath
);
588 /* Build the full ARC name */
590 "\\ArcName\\%spartition(%lu)",
591 ArcDiskEntry
->ArcName
,
594 /* Convert it to Unicode */
595 RtlInitAnsiString(&ArcString
, Buffer
);
596 Status
= RtlAnsiStringToUnicodeString(&ArcName
,
599 if (!NT_SUCCESS(Status
)) continue;
601 /* Create the symbolic link and free the strings */
602 IoAssignArcName(&ArcName
, &DeviceName
);
603 RtlFreeUnicodeString(&ArcName
);
604 RtlFreeUnicodeString(&DeviceName
);
610 /* Check if we didn't find the boot disk */
613 /* Allocate a buffer for the CD-ROM MBR */
614 PartitionBuffer
= ExAllocatePoolWithTag(NonPagedPool
, 2048, TAG_IO
);
615 if (!PartitionBuffer
) return STATUS_INSUFFICIENT_RESOURCES
;
617 /* Loop every CD-ROM */
618 for (i
= 0; i
< ConfigInfo
->CdRomCount
; i
++)
620 /* Give it an ARC name */
621 if (IopAssignArcNamesToCdrom(LoaderBlock
, PartitionBuffer
, i
)) break;
624 /* Free the buffer */
625 ExFreePoolWithTag(PartitionBuffer
, TAG_IO
);
629 return STATUS_SUCCESS
;
634 IopReassignSystemRoot(IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
635 OUT PANSI_STRING NtBootPath
)
637 OBJECT_ATTRIBUTES ObjectAttributes
;
639 CHAR Buffer
[256], AnsiBuffer
[256];
640 WCHAR ArcNameBuffer
[64];
641 ANSI_STRING TargetString
, ArcString
, TempString
;
642 UNICODE_STRING LinkName
, TargetName
, ArcName
;
645 /* Create the Unicode name for the current ARC boot device */
646 sprintf(Buffer
, "\\ArcName\\%s", LoaderBlock
->ArcBootDeviceName
);
647 RtlInitAnsiString(&TargetString
, Buffer
);
648 Status
= RtlAnsiStringToUnicodeString(&TargetName
, &TargetString
, TRUE
);
649 if (!NT_SUCCESS(Status
)) return FALSE
;
651 /* Initialize the attributes and open the link */
652 InitializeObjectAttributes(&ObjectAttributes
,
654 OBJ_CASE_INSENSITIVE
,
657 Status
= NtOpenSymbolicLinkObject(&LinkHandle
,
658 SYMBOLIC_LINK_ALL_ACCESS
,
660 if (!NT_SUCCESS(Status
))
662 /* We failed, free the string */
663 RtlFreeUnicodeString(&TargetName
);
667 /* Query the current \\SystemRoot */
668 ArcName
.Buffer
= ArcNameBuffer
;
670 ArcName
.MaximumLength
= sizeof(ArcNameBuffer
);
671 Status
= NtQuerySymbolicLinkObject(LinkHandle
, &ArcName
, NULL
);
672 if (!NT_SUCCESS(Status
))
674 /* We failed, free the string */
675 RtlFreeUnicodeString(&TargetName
);
679 /* Convert it to Ansi */
680 ArcString
.Buffer
= AnsiBuffer
;
681 ArcString
.Length
= 0;
682 ArcString
.MaximumLength
= sizeof(AnsiBuffer
);
683 Status
= RtlUnicodeStringToAnsiString(&ArcString
, &ArcName
, FALSE
);
684 AnsiBuffer
[ArcString
.Length
] = ANSI_NULL
;
686 /* Close the link handle and free the name */
687 ObCloseHandle(LinkHandle
, KernelMode
);
688 RtlFreeUnicodeString(&TargetName
);
690 /* Setup the system root name again */
691 RtlInitAnsiString(&TempString
, "\\SystemRoot");
692 Status
= RtlAnsiStringToUnicodeString(&LinkName
, &TempString
, TRUE
);
693 if (!NT_SUCCESS(Status
)) return FALSE
;
695 /* Open the symbolic link for it */
696 InitializeObjectAttributes(&ObjectAttributes
,
698 OBJ_CASE_INSENSITIVE
,
701 Status
= NtOpenSymbolicLinkObject(&LinkHandle
,
702 SYMBOLIC_LINK_ALL_ACCESS
,
704 if (!NT_SUCCESS(Status
)) return FALSE
;
707 NtMakeTemporaryObject(LinkHandle
);
708 ObCloseHandle(LinkHandle
, KernelMode
);
710 /* Now create the new name for it */
711 sprintf(Buffer
, "%s%s", ArcString
.Buffer
, LoaderBlock
->NtBootPathName
);
713 /* Copy it into the passed parameter and null-terminate it */
714 RtlCopyString(NtBootPath
, &ArcString
);
715 Buffer
[strlen(Buffer
) - 1] = ANSI_NULL
;
717 /* Setup the Unicode-name for the new symbolic link value */
718 RtlInitAnsiString(&TargetString
, Buffer
);
719 InitializeObjectAttributes(&ObjectAttributes
,
721 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
724 Status
= RtlAnsiStringToUnicodeString(&ArcName
, &TargetString
, TRUE
);
725 if (!NT_SUCCESS(Status
)) return FALSE
;
728 Status
= NtCreateSymbolicLinkObject(&LinkHandle
,
729 SYMBOLIC_LINK_ALL_ACCESS
,
733 /* Free all the strings and close the handle and return success */
734 RtlFreeUnicodeString(&ArcName
);
735 RtlFreeUnicodeString(&LinkName
);
736 ObCloseHandle(LinkHandle
, KernelMode
);