2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/fstub/disksup.c
5 * PURPOSE: I/O HAL Routines for Disk Access
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
8 * Casper S. Hornstrup (chorns@users.sourceforge.net)
11 /* INCLUDES ******************************************************************/
16 #include <internal/hal.h>
18 /* DEPRECATED FUNCTIONS ******************************************************/
21 const WCHAR DiskMountString
[] = L
"\\DosDevices\\%C:";
23 #define AUTO_DRIVE MAXULONG
25 #define PARTITION_MAGIC 0xaa55
27 #define EFI_PMBR_OSTYPE_EFI 0xEE
31 typedef struct _REG_DISK_MOUNT_INFO
34 LARGE_INTEGER StartingOffset
;
35 } REG_DISK_MOUNT_INFO
, *PREG_DISK_MOUNT_INFO
;
39 typedef enum _DISK_MANAGER
47 HalpAssignDrive(IN PUNICODE_STRING PartitionName
,
51 IN LARGE_INTEGER StartingOffset
,
53 IN PUNICODE_STRING BootDevice
,
54 OUT PUCHAR NtSystemPath
)
56 WCHAR DriveNameBuffer
[16];
57 UNICODE_STRING DriveName
;
60 REG_DISK_MOUNT_INFO DiskMountInfo
;
62 DPRINT("HalpAssignDrive()\n");
64 if ((DriveNumber
!= AUTO_DRIVE
) && (DriveNumber
< 26))
66 /* Force assignment */
67 KeAcquireGuardedMutex(&ObpDeviceMapLock
);
68 if ((ObSystemDeviceMap
->DriveMap
& (1 << DriveNumber
)) != 0)
70 DbgPrint("Drive letter already used!\n");
71 KeReleaseGuardedMutex(&ObpDeviceMapLock
);
74 KeReleaseGuardedMutex(&ObpDeviceMapLock
);
78 /* Automatic assignment */
79 DriveNumber
= AUTO_DRIVE
;
80 KeAcquireGuardedMutex(&ObpDeviceMapLock
);
81 for (i
= 2; i
< 26; i
++)
83 if ((ObSystemDeviceMap
->DriveMap
& (1 << i
)) == 0)
89 KeReleaseGuardedMutex(&ObpDeviceMapLock
);
91 if (DriveNumber
== AUTO_DRIVE
)
93 DbgPrint("No drive letter available!\n");
98 DPRINT("DriveNumber %d\n", DriveNumber
);
100 /* Build drive name */
101 swprintf(DriveNameBuffer
,
104 RtlInitUnicodeString(&DriveName
,
107 DPRINT(" %wZ ==> %wZ\n",
111 /* Create symbolic link */
112 Status
= IoCreateSymbolicLink(&DriveName
,
116 DriveType
== DOSDEVICE_DRIVE_FIXED
&&
119 DiskMountInfo
.Signature
= Signature
;
120 DiskMountInfo
.StartingOffset
= StartingOffset
;
121 swprintf(DriveNameBuffer
, DiskMountString
, L
'A' + DriveNumber
);
122 RtlInitUnicodeString(&DriveName
, DriveNameBuffer
);
124 Status
= ZwSetValueKey(hKey
,
129 sizeof(DiskMountInfo
));
130 if (!NT_SUCCESS(Status
))
132 DPRINT1("ZwCreateValueKey failed for %wZ, status=%x\n", &DriveName
, Status
);
136 /* Check if this is a boot partition */
137 if (RtlCompareUnicodeString(PartitionName
, BootDevice
, FALSE
) == 0)
139 /* Set NtSystemPath to that partition's disk letter */
140 *NtSystemPath
= (UCHAR
)('A' + DriveNumber
);
147 xHalpGetRDiskCount(VOID
)
150 UNICODE_STRING ArcName
;
151 PWCHAR ArcNameBuffer
;
152 OBJECT_ATTRIBUTES ObjectAttributes
;
153 HANDLE DirectoryHandle
;
154 POBJECT_DIRECTORY_INFORMATION DirectoryInfo
;
159 BOOLEAN First
= TRUE
;
162 DirectoryInfo
= ExAllocatePoolWithTag(PagedPool
, 2 * PAGE_SIZE
, TAG_FILE_SYSTEM
);
163 if (DirectoryInfo
== NULL
)
168 RtlInitUnicodeString(&ArcName
, L
"\\ArcName");
169 InitializeObjectAttributes(&ObjectAttributes
,
175 Status
= ZwOpenDirectoryObject (&DirectoryHandle
,
176 SYMBOLIC_LINK_ALL_ACCESS
,
178 if (!NT_SUCCESS(Status
))
180 DPRINT1("ZwOpenDirectoryObject for %wZ failed, status=%lx\n", &ArcName
, Status
);
181 ExFreePoolWithTag(DirectoryInfo
, TAG_FILE_SYSTEM
);
187 while (NT_SUCCESS(Status
))
189 Status
= NtQueryDirectoryObject (DirectoryHandle
,
197 if (NT_SUCCESS(Status
))
200 while (DirectoryInfo
[Count
].Name
.Buffer
)
202 DPRINT("Count %x\n", Count
);
203 DirectoryInfo
[Count
].Name
.Buffer
[DirectoryInfo
[Count
].Name
.Length
/ sizeof(WCHAR
)] = 0;
204 ArcNameBuffer
= DirectoryInfo
[Count
].Name
.Buffer
;
205 if (DirectoryInfo
[Count
].Name
.Length
>= sizeof(L
"multi(0)disk(0)rdisk(0)") - sizeof(WCHAR
) &&
206 !_wcsnicmp(ArcNameBuffer
, L
"multi(0)disk(0)rdisk(", (sizeof(L
"multi(0)disk(0)rdisk(") - sizeof(WCHAR
)) / sizeof(WCHAR
)))
208 DPRINT("%S\n", ArcNameBuffer
);
209 ArcNameBuffer
+= (sizeof(L
"multi(0)disk(0)rdisk(") - sizeof(WCHAR
)) / sizeof(WCHAR
);
211 while (iswdigit(*ArcNameBuffer
))
213 CurrentRDisk
= CurrentRDisk
* 10 + *ArcNameBuffer
- L
'0';
216 if (!_wcsicmp(ArcNameBuffer
, L
")") &&
217 CurrentRDisk
>= RDiskCount
)
219 RDiskCount
= CurrentRDisk
+ 1;
226 ExFreePoolWithTag(DirectoryInfo
, TAG_FILE_SYSTEM
);
231 xHalpGetDiskNumberFromRDisk(ULONG RDisk
, PULONG DiskNumber
)
233 WCHAR NameBuffer
[80];
234 UNICODE_STRING ArcName
;
235 UNICODE_STRING LinkName
;
236 OBJECT_ATTRIBUTES ObjectAttributes
;
241 L
"\\ArcName\\multi(0)disk(0)rdisk(%lu)",
244 RtlInitUnicodeString(&ArcName
, NameBuffer
);
245 InitializeObjectAttributes(&ObjectAttributes
,
250 Status
= ZwOpenSymbolicLinkObject(&LinkHandle
,
251 SYMBOLIC_LINK_ALL_ACCESS
,
253 if (!NT_SUCCESS(Status
))
255 DPRINT1("ZwOpenSymbolicLinkObject failed for %wZ, status=%lx\n", &ArcName
, Status
);
259 LinkName
.Buffer
= NameBuffer
;
261 LinkName
.MaximumLength
= sizeof(NameBuffer
);
262 Status
= ZwQuerySymbolicLinkObject(LinkHandle
,
266 if (!NT_SUCCESS(Status
))
268 DPRINT1("ZwQuerySymbolicLinkObject failed, status=%lx\n", Status
);
271 if (LinkName
.Length
< sizeof(L
"\\Device\\Harddisk0\\Partition0") - sizeof(WCHAR
) ||
272 LinkName
.Length
>= sizeof(NameBuffer
))
274 return STATUS_UNSUCCESSFUL
;
277 NameBuffer
[LinkName
.Length
/ sizeof(WCHAR
)] = 0;
278 if (_wcsnicmp(NameBuffer
, L
"\\Device\\Harddisk", (sizeof(L
"\\Device\\Harddisk") - sizeof(WCHAR
)) / sizeof(WCHAR
)))
280 return STATUS_UNSUCCESSFUL
;
282 LinkName
.Buffer
+= (sizeof(L
"\\Device\\Harddisk") - sizeof(WCHAR
)) / sizeof(WCHAR
);
284 if (!iswdigit(*LinkName
.Buffer
))
286 return STATUS_UNSUCCESSFUL
;
289 while (iswdigit(*LinkName
.Buffer
))
291 *DiskNumber
= *DiskNumber
* 10 + *LinkName
.Buffer
- L
'0';
294 if (_wcsicmp(LinkName
.Buffer
, L
"\\Partition0"))
296 return STATUS_UNSUCCESSFUL
;
298 return STATUS_SUCCESS
;
303 xHalQueryDriveLayout(IN PUNICODE_STRING DeviceName
,
304 OUT PDRIVE_LAYOUT_INFORMATION
*LayoutInfo
)
306 IO_STATUS_BLOCK StatusBlock
;
307 DISK_GEOMETRY DiskGeometry
;
308 PDEVICE_OBJECT DeviceObject
= NULL
;
309 PFILE_OBJECT FileObject
;
314 DPRINT("xHalpQueryDriveLayout %wZ %p\n",
318 /* Get the drives sector size */
319 Status
= IoGetDeviceObjectPointer(DeviceName
,
320 FILE_READ_ATTRIBUTES
,
323 if (!NT_SUCCESS(Status
))
325 DPRINT("Status %x\n", Status
);
329 KeInitializeEvent(&Event
,
333 Irp
= IoBuildDeviceIoControlRequest(IOCTL_DISK_GET_DRIVE_GEOMETRY
,
338 sizeof(DISK_GEOMETRY
),
344 ObDereferenceObject(FileObject
);
345 return(STATUS_INSUFFICIENT_RESOURCES
);
348 Status
= IoCallDriver(DeviceObject
,
350 if (Status
== STATUS_PENDING
)
352 KeWaitForSingleObject(&Event
,
357 Status
= StatusBlock
.Status
;
359 if (!NT_SUCCESS(Status
))
361 if (DeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
)
363 DiskGeometry
.BytesPerSector
= 512;
367 ObDereferenceObject(FileObject
);
372 DPRINT("DiskGeometry.BytesPerSector: %d\n",
373 DiskGeometry
.BytesPerSector
);
375 if (DeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
)
377 PDRIVE_LAYOUT_INFORMATION Buffer
;
379 /* Allocate a partition list for a single entry. */
380 Buffer
= ExAllocatePoolWithTag(NonPagedPool
,
381 sizeof(DRIVE_LAYOUT_INFORMATION
), TAG_FILE_SYSTEM
);
384 RtlZeroMemory(Buffer
,
385 sizeof(DRIVE_LAYOUT_INFORMATION
));
386 Buffer
->PartitionCount
= 1;
387 *LayoutInfo
= Buffer
;
389 Status
= STATUS_SUCCESS
;
393 Status
= STATUS_UNSUCCESSFUL
;
398 /* Read the partition table */
399 Status
= IoReadPartitionTable(DeviceObject
,
400 DiskGeometry
.BytesPerSector
,
405 ObDereferenceObject(FileObject
);
412 xHalIoAssignDriveLetters(IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
413 IN PSTRING NtDeviceName
,
414 OUT PUCHAR NtSystemPath
,
415 OUT PSTRING NtSystemPathString
)
417 PDRIVE_LAYOUT_INFORMATION
*LayoutArray
;
418 PCONFIGURATION_INFORMATION ConfigInfo
;
419 OBJECT_ATTRIBUTES ObjectAttributes
;
420 IO_STATUS_BLOCK StatusBlock
;
421 UNICODE_STRING UnicodeString1
;
422 UNICODE_STRING UnicodeString2
;
432 PKEY_VALUE_PARTIAL_INFORMATION PartialInformation
;
433 PREG_DISK_MOUNT_INFO DiskMountInfo
;
435 UNICODE_STRING BootDevice
;
437 Status
= RtlAnsiStringToUnicodeString(&BootDevice
,
441 DPRINT("xHalIoAssignDriveLetters()\n");
443 ConfigInfo
= IoGetConfigurationInformation();
445 RDiskCount
= xHalpGetRDiskCount();
447 DPRINT("RDiskCount %d\n", RDiskCount
);
449 Buffer1
= (PWSTR
)ExAllocatePoolWithTag(PagedPool
,
450 64 * sizeof(WCHAR
), TAG_FILE_SYSTEM
);
451 Buffer2
= (PWSTR
)ExAllocatePoolWithTag(PagedPool
,
452 32 * sizeof(WCHAR
), TAG_FILE_SYSTEM
);
454 PartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)ExAllocatePoolWithTag(PagedPool
,
455 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(REG_DISK_MOUNT_INFO
), TAG_FILE_SYSTEM
);
457 if (!Buffer1
|| !Buffer2
|| !PartialInformation
) return;
459 DiskMountInfo
= (PREG_DISK_MOUNT_INFO
) PartialInformation
->Data
;
461 /* Open or Create the 'MountedDevices' key */
462 RtlInitUnicodeString(&UnicodeString1
, L
"\\Registry\\Machine\\SYSTEM\\MountedDevices");
463 InitializeObjectAttributes(&ObjectAttributes
,
465 OBJ_CASE_INSENSITIVE
,
468 Status
= ZwOpenKey(&hKey
,
471 if (!NT_SUCCESS(Status
))
473 Status
= ZwCreateKey(&hKey
,
478 REG_OPTION_NON_VOLATILE
,
481 if (!NT_SUCCESS(Status
))
484 DPRINT("ZwCreateKey failed for %wZ, status=%x\n", &UnicodeString1
, Status
);
487 /* Create PhysicalDrive links */
488 DPRINT("Physical disk drives: %d\n", ConfigInfo
->DiskCount
);
489 for (i
= 0; i
< ConfigInfo
->DiskCount
; i
++)
492 L
"\\Device\\Harddisk%d\\Partition0",
494 RtlInitUnicodeString(&UnicodeString1
,
497 InitializeObjectAttributes(&ObjectAttributes
,
503 Status
= ZwOpenFile(&FileHandle
,
504 FILE_READ_DATA
| SYNCHRONIZE
,
508 FILE_SYNCHRONOUS_IO_NONALERT
);
509 if (NT_SUCCESS(Status
))
514 L
"\\??\\PhysicalDrive%d",
516 RtlInitUnicodeString(&UnicodeString2
,
519 DPRINT("Creating link: %S ==> %S\n",
523 IoCreateSymbolicLink(&UnicodeString2
,
528 /* Initialize layout array */
529 if (ConfigInfo
->DiskCount
== 0)
530 goto end_assign_disks
;
531 LayoutArray
= ExAllocatePoolWithTag(NonPagedPool
,
532 ConfigInfo
->DiskCount
* sizeof(PDRIVE_LAYOUT_INFORMATION
), TAG_FILE_SYSTEM
);
535 ExFreePoolWithTag(PartialInformation
, TAG_FILE_SYSTEM
);
536 ExFreePoolWithTag(Buffer2
, TAG_FILE_SYSTEM
);
537 ExFreePoolWithTag(Buffer1
, TAG_FILE_SYSTEM
);
538 if (hKey
) ZwClose(hKey
);
541 RtlZeroMemory(LayoutArray
,
542 ConfigInfo
->DiskCount
* sizeof(PDRIVE_LAYOUT_INFORMATION
));
543 for (i
= 0; i
< ConfigInfo
->DiskCount
; i
++)
546 L
"\\Device\\Harddisk%d\\Partition0",
548 RtlInitUnicodeString(&UnicodeString1
,
551 Status
= xHalQueryDriveLayout(&UnicodeString1
,
553 if (!NT_SUCCESS(Status
))
555 DbgPrint("xHalQueryDriveLayout() failed (Status = 0x%lx)\n",
557 LayoutArray
[i
] = NULL
;
560 /* We don't use the RewritePartition value while mounting the disks.
561 * We use this value for marking pre-assigned (registry) partitions.
563 for (j
= 0; j
< LayoutArray
[i
]->PartitionCount
; j
++)
565 LayoutArray
[i
]->PartitionEntry
[j
].RewritePartition
= FALSE
;
570 /* Dump layout array */
571 for (i
= 0; i
< ConfigInfo
->DiskCount
; i
++)
573 DPRINT("Harddisk %d:\n",
576 if (LayoutArray
[i
] == NULL
)
579 DPRINT("Logical partitions: %d\n",
580 LayoutArray
[i
]->PartitionCount
);
582 for (j
= 0; j
< LayoutArray
[i
]->PartitionCount
; j
++)
584 DPRINT(" %d: nr:%x boot:%x type:%x startblock:%I64u count:%I64u\n",
586 LayoutArray
[i
]->PartitionEntry
[j
].PartitionNumber
,
587 LayoutArray
[i
]->PartitionEntry
[j
].BootIndicator
,
588 LayoutArray
[i
]->PartitionEntry
[j
].PartitionType
,
589 LayoutArray
[i
]->PartitionEntry
[j
].StartingOffset
.QuadPart
,
590 LayoutArray
[i
]->PartitionEntry
[j
].PartitionLength
.QuadPart
);
595 /* Assign pre-assigned (registry) partitions */
598 for (k
= 2; k
< 26; k
++)
600 swprintf(Buffer1
, DiskMountString
, L
'A' + k
);
601 RtlInitUnicodeString(&UnicodeString1
, Buffer1
);
602 Status
= ZwQueryValueKey(hKey
,
604 KeyValuePartialInformation
,
606 sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + sizeof(REG_DISK_MOUNT_INFO
),
608 if (NT_SUCCESS(Status
) &&
609 PartialInformation
->Type
== REG_BINARY
&&
610 PartialInformation
->DataLength
== sizeof(REG_DISK_MOUNT_INFO
))
612 DPRINT("%wZ => %08x:%08x%08x\n", &UnicodeString1
, DiskMountInfo
->Signature
,
613 DiskMountInfo
->StartingOffset
.u
.HighPart
, DiskMountInfo
->StartingOffset
.u
.LowPart
);
615 BOOLEAN Found
= FALSE
;
616 for (i
= 0; i
< ConfigInfo
->DiskCount
; i
++)
618 DPRINT("%x\n", LayoutArray
[i
]->Signature
);
619 if (LayoutArray
[i
] &&
620 LayoutArray
[i
]->Signature
&&
621 LayoutArray
[i
]->Signature
== DiskMountInfo
->Signature
)
623 for (j
= 0; j
< LayoutArray
[i
]->PartitionCount
; j
++)
625 if (LayoutArray
[i
]->PartitionEntry
[j
].StartingOffset
.QuadPart
== DiskMountInfo
->StartingOffset
.QuadPart
)
627 if (IsRecognizedPartition(LayoutArray
[i
]->PartitionEntry
[j
].PartitionType
) &&
628 LayoutArray
[i
]->PartitionEntry
[j
].RewritePartition
== FALSE
)
631 L
"\\Device\\Harddisk%d\\Partition%d",
633 LayoutArray
[i
]->PartitionEntry
[j
].PartitionNumber
);
634 RtlInitUnicodeString(&UnicodeString2
,
638 DPRINT(" %wZ\n", &UnicodeString2
);
639 Found
= HalpAssignDrive(&UnicodeString2
,
641 DOSDEVICE_DRIVE_FIXED
,
642 DiskMountInfo
->Signature
,
643 DiskMountInfo
->StartingOffset
,
647 /* Mark the partition as assigned */
648 LayoutArray
[i
]->PartitionEntry
[j
].RewritePartition
= TRUE
;
657 /* We didn't find a partition for this entry, remove them. */
658 Status
= ZwDeleteValueKey(hKey
, &UnicodeString1
);
665 /* Assign bootable partition on first harddisk */
666 DPRINT("Assigning bootable primary partition on first harddisk:\n");
669 Status
= xHalpGetDiskNumberFromRDisk(0, &DiskNumber
);
670 if (NT_SUCCESS(Status
) &&
671 DiskNumber
< ConfigInfo
->DiskCount
&&
672 LayoutArray
[DiskNumber
])
674 /* Search for bootable partition */
675 for (j
= 0; j
< NUM_PARTITION_TABLE_ENTRIES
&& j
< LayoutArray
[DiskNumber
]->PartitionCount
; j
++)
677 if ((LayoutArray
[DiskNumber
]->PartitionEntry
[j
].BootIndicator
== TRUE
) &&
678 IsRecognizedPartition(LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionType
))
680 if (LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
== FALSE
)
683 L
"\\Device\\Harddisk%lu\\Partition%d",
685 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionNumber
);
686 RtlInitUnicodeString(&UnicodeString2
,
690 DPRINT(" %wZ\n", &UnicodeString2
);
691 HalpAssignDrive(&UnicodeString2
,
693 DOSDEVICE_DRIVE_FIXED
,
694 LayoutArray
[DiskNumber
]->Signature
,
695 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].StartingOffset
,
699 /* Mark the partition as assigned */
700 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
= TRUE
;
708 /* Assign remaining primary partitions */
709 DPRINT("Assigning remaining primary partitions:\n");
710 for (RDisk
= 0; RDisk
< RDiskCount
; RDisk
++)
712 Status
= xHalpGetDiskNumberFromRDisk(RDisk
, &DiskNumber
);
713 if (NT_SUCCESS(Status
) &&
714 DiskNumber
< ConfigInfo
->DiskCount
&&
715 LayoutArray
[DiskNumber
])
717 /* Search for primary partitions */
718 for (j
= 0; (j
< NUM_PARTITION_TABLE_ENTRIES
) && (j
< LayoutArray
[DiskNumber
]->PartitionCount
); j
++)
720 if (LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
== FALSE
&&
721 IsRecognizedPartition(LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionType
))
724 L
"\\Device\\Harddisk%d\\Partition%d",
726 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionNumber
);
727 RtlInitUnicodeString(&UnicodeString2
,
733 HalpAssignDrive(&UnicodeString2
,
735 DOSDEVICE_DRIVE_FIXED
,
736 LayoutArray
[DiskNumber
]->Signature
,
737 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].StartingOffset
,
741 /* Mark the partition as assigned */
742 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
= TRUE
;
748 /* Assign extended (logical) partitions */
749 DPRINT("Assigning extended (logical) partitions:\n");
750 for (RDisk
= 0; RDisk
< RDiskCount
; RDisk
++)
752 Status
= xHalpGetDiskNumberFromRDisk(RDisk
, &DiskNumber
);
753 if (NT_SUCCESS(Status
) &&
754 DiskNumber
< ConfigInfo
->DiskCount
&&
755 LayoutArray
[DiskNumber
])
757 /* Search for extended partitions */
758 for (j
= NUM_PARTITION_TABLE_ENTRIES
; j
< LayoutArray
[DiskNumber
]->PartitionCount
; j
++)
760 if (IsRecognizedPartition(LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionType
) &&
761 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
== FALSE
&&
762 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionNumber
!= 0)
765 L
"\\Device\\Harddisk%d\\Partition%d",
767 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionNumber
);
768 RtlInitUnicodeString(&UnicodeString2
,
774 HalpAssignDrive(&UnicodeString2
,
776 DOSDEVICE_DRIVE_FIXED
,
777 LayoutArray
[DiskNumber
]->Signature
,
778 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].StartingOffset
,
782 /* Mark the partition as assigned */
783 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
= TRUE
;
789 /* Assign remaining primary partitions without an arc-name */
790 DPRINT("Assigning remaining primary partitions:\n");
791 for (DiskNumber
= 0; DiskNumber
< ConfigInfo
->DiskCount
; DiskNumber
++)
793 if (LayoutArray
[DiskNumber
])
795 /* Search for primary partitions */
796 for (j
= 0; (j
< NUM_PARTITION_TABLE_ENTRIES
) && (j
< LayoutArray
[DiskNumber
]->PartitionCount
); j
++)
798 if (LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
== FALSE
&&
799 IsRecognizedPartition(LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionType
))
802 L
"\\Device\\Harddisk%d\\Partition%d",
804 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionNumber
);
805 RtlInitUnicodeString(&UnicodeString2
,
811 HalpAssignDrive(&UnicodeString2
,
813 DOSDEVICE_DRIVE_FIXED
,
814 LayoutArray
[DiskNumber
]->Signature
,
815 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].StartingOffset
,
819 /* Mark the partition as assigned */
820 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
= TRUE
;
826 /* Assign extended (logical) partitions without an arc-name */
827 DPRINT("Assigning extended (logical) partitions:\n");
828 for (DiskNumber
= 0; DiskNumber
< ConfigInfo
->DiskCount
; DiskNumber
++)
830 if (LayoutArray
[DiskNumber
])
832 /* Search for extended partitions */
833 for (j
= NUM_PARTITION_TABLE_ENTRIES
; j
< LayoutArray
[DiskNumber
]->PartitionCount
; j
++)
835 if (IsRecognizedPartition(LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionType
) &&
836 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
== FALSE
&&
837 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionNumber
!= 0)
840 L
"\\Device\\Harddisk%d\\Partition%d",
842 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].PartitionNumber
);
843 RtlInitUnicodeString(&UnicodeString2
,
849 HalpAssignDrive(&UnicodeString2
,
851 DOSDEVICE_DRIVE_FIXED
,
852 LayoutArray
[DiskNumber
]->Signature
,
853 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].StartingOffset
,
857 /* Mark the partition as assigned */
858 LayoutArray
[DiskNumber
]->PartitionEntry
[j
].RewritePartition
= TRUE
;
864 /* Assign removable disk drives */
865 DPRINT("Assigning removable disk drives:\n");
866 for (i
= 0; i
< ConfigInfo
->DiskCount
; i
++)
870 /* Search for virtual partitions */
871 if (LayoutArray
[i
]->PartitionCount
== 1 &&
872 LayoutArray
[i
]->PartitionEntry
[0].PartitionType
== 0)
875 L
"\\Device\\Harddisk%d\\Partition1",
877 RtlInitUnicodeString(&UnicodeString2
,
883 HalpAssignDrive(&UnicodeString2
,
885 DOSDEVICE_DRIVE_REMOVABLE
,
887 RtlConvertLongToLargeInteger(0),
895 /* Free layout array */
896 for (i
= 0; i
< ConfigInfo
->DiskCount
; i
++)
898 if (LayoutArray
[i
] != NULL
)
899 ExFreePoolWithTag(LayoutArray
[i
], TAG_FILE_SYSTEM
);
901 ExFreePoolWithTag(LayoutArray
, TAG_FILE_SYSTEM
);
904 /* Assign floppy drives */
905 DPRINT("Floppy drives: %d\n", ConfigInfo
->FloppyCount
);
906 for (i
= 0; i
< ConfigInfo
->FloppyCount
; i
++)
909 L
"\\Device\\Floppy%d",
911 RtlInitUnicodeString(&UnicodeString1
,
914 /* Assign drive letters A: or B: or first free drive letter */
917 HalpAssignDrive(&UnicodeString1
,
918 (i
< 2) ? i
: AUTO_DRIVE
,
919 DOSDEVICE_DRIVE_REMOVABLE
,
921 RtlConvertLongToLargeInteger(0),
927 /* Assign cdrom drives */
928 DPRINT("CD-Rom drives: %d\n", ConfigInfo
->CdRomCount
);
929 for (i
= 0; i
< ConfigInfo
->CdRomCount
; i
++)
932 L
"\\Device\\CdRom%d",
934 RtlInitUnicodeString(&UnicodeString1
,
937 /* Assign first free drive letter */
938 DPRINT(" %wZ\n", &UnicodeString1
);
939 HalpAssignDrive(&UnicodeString1
,
941 DOSDEVICE_DRIVE_CDROM
,
943 RtlConvertLongToLargeInteger(0),
949 /* Anything else to do? */
951 ExFreePoolWithTag(PartialInformation
, TAG_FILE_SYSTEM
);
952 ExFreePoolWithTag(Buffer2
, TAG_FILE_SYSTEM
);
953 ExFreePoolWithTag(Buffer1
, TAG_FILE_SYSTEM
);
962 /* PRIVATE FUNCTIONS *********************************************************/
966 HalpGetFullGeometry(IN PDEVICE_OBJECT DeviceObject
,
967 IN PDISK_GEOMETRY Geometry
,
968 OUT PULONGLONG RealSectorCount
)
971 IO_STATUS_BLOCK IoStatusBlock
;
974 PARTITION_INFORMATION PartitionInfo
;
977 /* Allocate a non-paged event */
978 Event
= ExAllocatePoolWithTag(NonPagedPool
,
981 if (!Event
) return STATUS_INSUFFICIENT_RESOURCES
;
984 KeInitializeEvent(Event
, NotificationEvent
, FALSE
);
987 Irp
= IoBuildDeviceIoControlRequest(IOCTL_DISK_GET_DRIVE_GEOMETRY
,
992 sizeof(DISK_GEOMETRY
),
998 /* Fail, free the event */
999 ExFreePoolWithTag(Event
, TAG_FILE_SYSTEM
);
1000 return STATUS_INSUFFICIENT_RESOURCES
;
1003 /* Call the driver and check if it's pending */
1004 Status
= IoCallDriver(DeviceObject
, Irp
);
1005 if (Status
== STATUS_PENDING
)
1007 /* Wait on the driver */
1008 KeWaitForSingleObject(Event
, Executive
, KernelMode
, FALSE
, NULL
);
1009 Status
= IoStatusBlock
.Status
;
1012 /* Check if the driver returned success */
1013 if(NT_SUCCESS(Status
))
1015 /* Build another IRP */
1016 Irp
= IoBuildDeviceIoControlRequest(IOCTL_DISK_GET_PARTITION_INFO
,
1021 sizeof(PARTITION_INFORMATION
),
1027 /* Fail, free the event */
1028 ExFreePoolWithTag(Event
, TAG_FILE_SYSTEM
);
1029 return STATUS_INSUFFICIENT_RESOURCES
;
1032 /* Call the driver and check if it's pending */
1033 Status
= IoCallDriver(DeviceObject
, Irp
);
1034 if (Status
== STATUS_PENDING
)
1036 /* Wait on the driver */
1037 KeWaitForSingleObject(Event
, Executive
, KernelMode
, FALSE
, NULL
);
1038 Status
= IoStatusBlock
.Status
;
1041 /* Check if the driver returned success */
1042 if(NT_SUCCESS(Status
))
1044 /* Get the number of sectors */
1045 *RealSectorCount
= (PartitionInfo
.PartitionLength
.QuadPart
/
1046 Geometry
->BytesPerSector
);
1050 /* Free the event and return the Status */
1051 ExFreePoolWithTag(Event
, TAG_FILE_SYSTEM
);
1057 HalpIsValidPartitionEntry(IN PPARTITION_DESCRIPTOR Entry
,
1058 IN ULONGLONG MaxOffset
,
1059 IN ULONGLONG MaxSector
)
1061 ULONGLONG EndingSector
;
1064 /* Unused partitions are considered valid */
1065 if (Entry
->PartitionType
== PARTITION_ENTRY_UNUSED
) return TRUE
;
1067 /* Get the last sector of the partition */
1068 EndingSector
= GET_STARTING_SECTOR(Entry
) + GET_PARTITION_LENGTH(Entry
);
1070 /* Check if it's more then the maximum sector */
1071 if (EndingSector
> MaxSector
)
1073 /* Invalid partition */
1074 DPRINT1("FSTUB: entry is invalid\n");
1075 DPRINT1("FSTUB: offset %#08lx\n", GET_STARTING_SECTOR(Entry
));
1076 DPRINT1("FSTUB: length %#08lx\n", GET_PARTITION_LENGTH(Entry
));
1077 DPRINT1("FSTUB: end %#I64x\n", EndingSector
);
1078 DPRINT1("FSTUB: max %#I64x\n", MaxSector
);
1081 else if(GET_STARTING_SECTOR(Entry
) > MaxOffset
)
1083 /* Invalid partition */
1084 DPRINT1("FSTUB: entry is invalid\n");
1085 DPRINT1("FSTUB: offset %#08lx\n", GET_STARTING_SECTOR(Entry
));
1086 DPRINT1("FSTUB: length %#08lx\n", GET_PARTITION_LENGTH(Entry
));
1087 DPRINT1("FSTUB: end %#I64x\n", EndingSector
);
1088 DPRINT1("FSTUB: maxOffset %#I64x\n", MaxOffset
);
1092 /* It's fine, return success */
1098 HalpCalculateChsValues(IN PLARGE_INTEGER PartitionOffset
,
1099 IN PLARGE_INTEGER PartitionLength
,
1100 IN CCHAR ShiftCount
,
1101 IN ULONG SectorsPerTrack
,
1102 IN ULONG NumberOfTracks
,
1103 IN ULONG ConventionalCylinders
,
1104 OUT PPARTITION_DESCRIPTOR PartitionDescriptor
)
1106 LARGE_INTEGER FirstSector
, SectorCount
;
1107 ULONG LastSector
, Remainder
, SectorsPerCylinder
;
1108 ULONG StartingCylinder
, EndingCylinder
;
1109 ULONG StartingTrack
, EndingTrack
;
1110 ULONG StartingSector
, EndingSector
;
1113 /* Calculate the number of sectors for each cylinder */
1114 SectorsPerCylinder
= SectorsPerTrack
* NumberOfTracks
;
1116 /* Calculate the first sector, and the sector count */
1117 FirstSector
.QuadPart
= PartitionOffset
->QuadPart
>> ShiftCount
;
1118 SectorCount
.QuadPart
= PartitionLength
->QuadPart
>> ShiftCount
;
1120 /* Now calculate the last sector */
1121 LastSector
= FirstSector
.LowPart
+ SectorCount
.LowPart
- 1;
1123 /* Calculate the first and last cylinders */
1124 StartingCylinder
= FirstSector
.LowPart
/ SectorsPerCylinder
;
1125 EndingCylinder
= LastSector
/ SectorsPerCylinder
;
1127 /* Set the default number of cylinders */
1128 if (!ConventionalCylinders
) ConventionalCylinders
= 1024;
1130 /* Normalize the values */
1131 if (StartingCylinder
>= ConventionalCylinders
)
1133 /* Set the maximum to 1023 */
1134 StartingCylinder
= ConventionalCylinders
- 1;
1136 if (EndingCylinder
>= ConventionalCylinders
)
1138 /* Set the maximum to 1023 */
1139 EndingCylinder
= ConventionalCylinders
- 1;
1142 /* Calculate the starting head and sector that still remain */
1143 Remainder
= FirstSector
.LowPart
% SectorsPerCylinder
;
1144 StartingTrack
= Remainder
/ SectorsPerTrack
;
1145 StartingSector
= Remainder
% SectorsPerTrack
;
1147 /* Calculate the ending head and sector that still remain */
1148 Remainder
= LastSector
% SectorsPerCylinder
;
1149 EndingTrack
= Remainder
/ SectorsPerTrack
;
1150 EndingSector
= Remainder
% SectorsPerTrack
;
1152 /* Set cylinder data for the MSB */
1153 PartitionDescriptor
->StartingCylinderMsb
= (UCHAR
)StartingCylinder
;
1154 PartitionDescriptor
->EndingCylinderMsb
= (UCHAR
)EndingCylinder
;
1156 /* Set the track data */
1157 PartitionDescriptor
->StartingTrack
= (UCHAR
)StartingTrack
;
1158 PartitionDescriptor
->EndingTrack
= (UCHAR
)EndingTrack
;
1160 /* Update cylinder data for the LSB */
1161 StartingCylinder
= ((StartingSector
+ 1) & 0x3F) |
1162 ((StartingCylinder
>> 2) & 0xC0);
1163 EndingCylinder
= ((EndingSector
+ 1) & 0x3F) |
1164 ((EndingCylinder
>> 2) & 0xC0);
1166 /* Set the cylinder data for the LSB */
1167 PartitionDescriptor
->StartingCylinderLsb
= (UCHAR
)StartingCylinder
;
1168 PartitionDescriptor
->EndingCylinderLsb
= (UCHAR
)EndingCylinder
;
1173 xHalGetPartialGeometry(IN PDEVICE_OBJECT DeviceObject
,
1174 IN PULONG ConventionalCylinders
,
1175 IN PLONGLONG DiskSize
)
1177 PDISK_GEOMETRY DiskGeometry
= NULL
;
1178 PIO_STATUS_BLOCK IoStatusBlock
= NULL
;
1179 PKEVENT Event
= NULL
;
1184 *ConventionalCylinders
= 0;
1187 /* Allocate the structure in nonpaged pool */
1188 DiskGeometry
= ExAllocatePoolWithTag(NonPagedPool
,
1189 sizeof(DISK_GEOMETRY
),
1191 if (!DiskGeometry
) goto Cleanup
;
1193 /* Allocate the status block in nonpaged pool */
1194 IoStatusBlock
= ExAllocatePoolWithTag(NonPagedPool
,
1195 sizeof(IO_STATUS_BLOCK
),
1197 if (!IoStatusBlock
) goto Cleanup
;
1199 /* Allocate the event in nonpaged pool too */
1200 Event
= ExAllocatePoolWithTag(NonPagedPool
,
1203 if (!Event
) goto Cleanup
;
1205 /* Initialize the event */
1206 KeInitializeEvent(Event
, NotificationEvent
, FALSE
);
1209 Irp
= IoBuildDeviceIoControlRequest(IOCTL_DISK_GET_DRIVE_GEOMETRY
,
1214 sizeof(DISK_GEOMETRY
),
1218 if (!Irp
) goto Cleanup
;
1220 /* Now call the driver */
1221 Status
= IoCallDriver(DeviceObject
, Irp
);
1222 if (Status
== STATUS_PENDING
)
1224 /* Wait for it to complete */
1225 KeWaitForSingleObject(Event
, Executive
, KernelMode
, FALSE
, NULL
);
1226 Status
= IoStatusBlock
->Status
;
1229 /* Check driver status */
1230 if (NT_SUCCESS(Status
))
1232 /* Return the cylinder count */
1233 *ConventionalCylinders
= DiskGeometry
->Cylinders
.LowPart
;
1235 /* Make sure it's not larger then 1024 */
1236 if (DiskGeometry
->Cylinders
.LowPart
>= 1024)
1238 /* Otherwise, normalize the value */
1239 *ConventionalCylinders
= 1024;
1242 /* Calculate the disk size */
1243 *DiskSize
= DiskGeometry
->Cylinders
.QuadPart
*
1244 DiskGeometry
->TracksPerCylinder
*
1245 DiskGeometry
->SectorsPerTrack
*
1246 DiskGeometry
->BytesPerSector
;
1250 /* Free all the pointers */
1251 if (Event
) ExFreePoolWithTag(Event
, TAG_FILE_SYSTEM
);
1252 if (IoStatusBlock
) ExFreePoolWithTag(IoStatusBlock
, TAG_FILE_SYSTEM
);
1253 if (DiskGeometry
) ExFreePoolWithTag(DiskGeometry
, TAG_FILE_SYSTEM
);
1259 xHalExamineMBR(IN PDEVICE_OBJECT DeviceObject
,
1260 IN ULONG SectorSize
,
1261 IN ULONG MbrTypeIdentifier
,
1262 OUT PVOID
*MbrBuffer
)
1264 LARGE_INTEGER Offset
;
1268 IO_STATUS_BLOCK IoStatusBlock
;
1270 PPARTITION_DESCRIPTOR PartitionDescriptor
;
1272 PIO_STACK_LOCATION IoStackLocation
;
1273 Offset
.QuadPart
= 0;
1275 /* Assume failure */
1278 /* Normalize the buffer size */
1279 BufferSize
= max(SectorSize
, 512);
1281 /* Allocate the buffer */
1282 Buffer
= ExAllocatePoolWithTag(NonPagedPool
,
1283 PAGE_SIZE
> BufferSize
?
1284 PAGE_SIZE
: BufferSize
,
1286 if (!Buffer
) return;
1288 /* Initialize the Event */
1289 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
1292 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
1302 ExFreePoolWithTag(Buffer
, TAG_FILE_SYSTEM
);
1306 /* Make sure to override volume verification */
1307 IoStackLocation
= IoGetNextIrpStackLocation(Irp
);
1308 IoStackLocation
->Flags
|= SL_OVERRIDE_VERIFY_VOLUME
;
1310 /* Call the driver */
1311 Status
= IoCallDriver(DeviceObject
, Irp
);
1312 if (Status
== STATUS_PENDING
)
1314 /* Wait for completion */
1315 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
1316 Status
= IoStatusBlock
.Status
;
1319 /* Check driver Status */
1320 if (NT_SUCCESS(Status
))
1322 /* Validate the MBR Signature */
1323 if (((PUSHORT
)Buffer
)[BOOT_SIGNATURE_OFFSET
] != BOOT_RECORD_SIGNATURE
)
1326 ExFreePoolWithTag(Buffer
, TAG_FILE_SYSTEM
);
1330 /* Get the partition entry */
1331 PartitionDescriptor
= (PPARTITION_DESCRIPTOR
)
1332 &(((PUSHORT
)Buffer
)[PARTITION_TABLE_OFFSET
]);
1334 /* Make sure it's what the caller wanted */
1335 if (PartitionDescriptor
->PartitionType
!= MbrTypeIdentifier
)
1337 /* It's not, free our buffer */
1338 ExFreePoolWithTag(Buffer
, TAG_FILE_SYSTEM
);
1342 /* Check if this is a secondary entry */
1343 if (PartitionDescriptor
->PartitionType
== 0x54)
1345 /* Return our buffer, but at sector 63 */
1346 *(PULONG
)Buffer
= 63;
1347 *MbrBuffer
= Buffer
;
1349 else if (PartitionDescriptor
->PartitionType
== 0x55)
1351 /* EZ Drive, return the buffer directly */
1352 *MbrBuffer
= Buffer
;
1356 /* Otherwise crash on debug builds */
1357 ASSERT(PartitionDescriptor
->PartitionType
== 0x55);
1365 FstubFixupEfiPartition(IN PPARTITION_DESCRIPTOR PartitionDescriptor
,
1366 IN ULONGLONG MaxOffset
)
1368 ULONG PartitionMaxOffset
, PartitionLength
;
1371 /* Compute partition length (according to MBR entry) */
1372 PartitionMaxOffset
= GET_STARTING_SECTOR(PartitionDescriptor
) + GET_PARTITION_LENGTH(PartitionDescriptor
);
1373 /* In case the partition length goes beyond disk size... */
1374 if (PartitionMaxOffset
> MaxOffset
)
1376 /* Resize partition to its maximum real length */
1377 PartitionLength
= (ULONG
)(PartitionMaxOffset
- GET_STARTING_SECTOR(PartitionDescriptor
));
1378 SET_PARTITION_LENGTH(PartitionDescriptor
, PartitionLength
);
1384 xHalIoReadPartitionTable(IN PDEVICE_OBJECT DeviceObject
,
1385 IN ULONG SectorSize
,
1386 IN BOOLEAN ReturnRecognizedPartitions
,
1387 IN OUT PDRIVE_LAYOUT_INFORMATION
*PartitionBuffer
)
1390 IO_STATUS_BLOCK IoStatusBlock
;
1392 PPARTITION_DESCRIPTOR PartitionDescriptor
;
1395 PPARTITION_INFORMATION PartitionInfo
;
1396 PUCHAR Buffer
= NULL
;
1397 ULONG BufferSize
= 2048, InputSize
;
1398 PDRIVE_LAYOUT_INFORMATION DriveLayoutInfo
= NULL
;
1399 LONG j
= -1, i
= -1, k
;
1400 DISK_GEOMETRY DiskGeometry
;
1401 LONGLONG EndSector
, MaxSector
, StartOffset
;
1402 ULONGLONG MaxOffset
;
1403 LARGE_INTEGER Offset
, VolumeOffset
;
1404 BOOLEAN IsPrimary
= TRUE
, IsEzDrive
= FALSE
, MbrFound
= FALSE
;
1405 BOOLEAN IsValid
, IsEmpty
= TRUE
;
1407 PIO_STACK_LOCATION IoStackLocation
;
1408 PBOOT_SECTOR_INFO BootSectorInfo
= (PBOOT_SECTOR_INFO
)Buffer
;
1409 UCHAR PartitionType
;
1410 LARGE_INTEGER HiddenSectors64
;
1411 VolumeOffset
.QuadPart
= Offset
.QuadPart
= 0;
1414 /* Allocate the buffer */
1415 *PartitionBuffer
= ExAllocatePoolWithTag(NonPagedPool
,
1418 if (!(*PartitionBuffer
)) return STATUS_INSUFFICIENT_RESOURCES
;
1420 /* Normalize the buffer size */
1421 InputSize
= max(512, SectorSize
);
1423 /* Check for EZ Drive */
1424 HalExamineMBR(DeviceObject
, InputSize
, 0x55, &MbrBuffer
);
1427 /* EZ Drive found, bias the offset */
1429 ExFreePoolWithTag(MbrBuffer
, TAG_FILE_SYSTEM
);
1430 Offset
.QuadPart
= 512;
1433 /* Get drive geometry */
1434 Status
= HalpGetFullGeometry(DeviceObject
, &DiskGeometry
, &MaxOffset
);
1435 if (!NT_SUCCESS(Status
))
1437 ExFreePoolWithTag(*PartitionBuffer
, TAG_FILE_SYSTEM
);
1438 *PartitionBuffer
= NULL
;
1442 /* Get the end and maximum sector */
1443 EndSector
= MaxOffset
;
1444 MaxSector
= MaxOffset
<< 1;
1445 DPRINT("FSTUB: MaxOffset = %#I64x, MaxSector = %#I64x\n",
1446 MaxOffset
, MaxSector
);
1448 /* Allocate our buffer */
1449 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, InputSize
, TAG_FILE_SYSTEM
);
1452 /* Fail, free the input buffer */
1453 ExFreePoolWithTag(*PartitionBuffer
, TAG_FILE_SYSTEM
);
1454 *PartitionBuffer
= NULL
;
1455 return STATUS_INSUFFICIENT_RESOURCES
;
1458 /* Start partition loop */
1461 /* Assume the partition is valid */
1464 /* Initialize the event */
1465 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
1467 /* Clear the buffer and build the IRP */
1468 RtlZeroMemory(Buffer
, InputSize
);
1469 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
1479 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1483 /* Make sure to disable volume verification */
1484 IoStackLocation
= IoGetNextIrpStackLocation(Irp
);
1485 IoStackLocation
->Flags
|= SL_OVERRIDE_VERIFY_VOLUME
;
1487 /* Call the driver */
1488 Status
= IoCallDriver(DeviceObject
, Irp
);
1489 if (Status
== STATUS_PENDING
)
1491 /* Wait for completion */
1492 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
1493 Status
= IoStatusBlock
.Status
;
1496 /* Normalize status code and check for failure */
1497 if (Status
== STATUS_NO_DATA_DETECTED
) Status
= STATUS_SUCCESS
;
1498 if (!NT_SUCCESS(Status
)) break;
1500 /* If we biased for EZ-Drive, unbias now */
1501 if (IsEzDrive
&& (Offset
.QuadPart
== 512)) Offset
.QuadPart
= 0;
1503 /* Make sure this is a valid MBR */
1504 if (((PUSHORT
)Buffer
)[BOOT_SIGNATURE_OFFSET
] != BOOT_RECORD_SIGNATURE
)
1506 /* It's not, fail */
1507 DPRINT1("FSTUB: (IoReadPartitionTable) No 0xaa55 found in "
1508 "partition table %d\n", j
+ 1);
1512 /* At this point we have a valid MBR */
1515 /* Check if we weren't given an offset */
1516 if (!Offset
.QuadPart
)
1518 /* Then read the signature off the disk */
1519 (*PartitionBuffer
)->Signature
= ((PULONG
)Buffer
)
1520 [PARTITION_TABLE_OFFSET
/ 2 - 1];
1523 /* Get the partition descriptor array */
1524 PartitionDescriptor
= (PPARTITION_DESCRIPTOR
)
1525 &(((PUSHORT
)Buffer
)[PARTITION_TABLE_OFFSET
]);
1527 /* Start looping partitions */
1529 DPRINT("FSTUB: Partition Table %d:\n", j
);
1530 for (Entry
= 1, k
= 0; Entry
<= 4; Entry
++, PartitionDescriptor
++)
1532 /* Get the partition type */
1533 PartitionType
= PartitionDescriptor
->PartitionType
;
1535 /* Print debug messages */
1536 DPRINT("Partition Entry %d,%d: type %#x %s\n",
1540 (PartitionDescriptor
->ActiveFlag
) ? "Active" : "");
1541 DPRINT("\tOffset %#08lx for %#08lx Sectors\n",
1542 GET_STARTING_SECTOR(PartitionDescriptor
),
1543 GET_PARTITION_LENGTH(PartitionDescriptor
));
1545 /* Check whether we're facing a protective MBR */
1546 if (PartitionType
== EFI_PMBR_OSTYPE_EFI
)
1548 /* Partition length might be bigger than disk size */
1549 FstubFixupEfiPartition(PartitionDescriptor
,
1553 /* Make sure that the partition is valid, unless it's the first */
1554 if (!(HalpIsValidPartitionEntry(PartitionDescriptor
,
1556 MaxSector
)) && !(j
))
1558 /* It's invalid, so fail */
1563 /* Check if it's a container */
1564 if (IsContainerPartition(PartitionType
))
1566 /* Increase the count of containers */
1569 /* More then one table is invalid */
1570 DPRINT1("FSTUB: Multiple container partitions found in "
1571 "partition table %d\n - table is invalid\n",
1578 /* Check if the partition is supposedly empty */
1581 /* But check if it actually has a start and/or length */
1582 if ((GET_STARTING_SECTOR(PartitionDescriptor
)) ||
1583 (GET_PARTITION_LENGTH(PartitionDescriptor
)))
1585 /* So then it's not really empty */
1590 /* Check if the caller wanted only recognized partitions */
1591 if (ReturnRecognizedPartitions
)
1593 /* Then check if this one is unused, or a container */
1594 if ((PartitionType
== PARTITION_ENTRY_UNUSED
) ||
1595 IsContainerPartition(PartitionType
))
1597 /* Skip it, since the caller doesn't want it */
1602 /* Increase the structure count and check if they can fit */
1603 if ((sizeof(DRIVE_LAYOUT_INFORMATION
) +
1604 (++i
* sizeof(PARTITION_INFORMATION
))) >
1607 /* Allocate a new buffer that's twice as big */
1608 DriveLayoutInfo
= ExAllocatePoolWithTag(NonPagedPool
,
1611 if (!DriveLayoutInfo
)
1613 /* Out of memory, unto this extra structure */
1615 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1619 /* Copy the contents of the old buffer */
1620 RtlMoveMemory(DriveLayoutInfo
,
1624 /* Free the old buffer and set this one as the new one */
1625 ExFreePoolWithTag(*PartitionBuffer
, TAG_FILE_SYSTEM
);
1626 *PartitionBuffer
= DriveLayoutInfo
;
1628 /* Double the size */
1632 /* Now get the current structure being filled and initialize it */
1633 PartitionInfo
= &(*PartitionBuffer
)->PartitionEntry
[i
];
1634 PartitionInfo
->PartitionType
= PartitionType
;
1635 PartitionInfo
->RewritePartition
= FALSE
;
1637 /* Check if we're dealing with a partition that's in use */
1638 if (PartitionType
!= PARTITION_ENTRY_UNUSED
)
1640 /* Check if it's bootable */
1641 PartitionInfo
->BootIndicator
= PartitionDescriptor
->
1645 /* Check if its' a container */
1646 if (IsContainerPartition(PartitionType
))
1648 /* Then don't recognize it and use the volume offset */
1649 PartitionInfo
->RecognizedPartition
= FALSE
;
1650 StartOffset
= VolumeOffset
.QuadPart
;
1654 /* Then recognize it and use the partition offset */
1655 PartitionInfo
->RecognizedPartition
= TRUE
;
1656 StartOffset
= Offset
.QuadPart
;
1659 /* Get the starting offset */
1660 PartitionInfo
->StartingOffset
.QuadPart
=
1662 UInt32x32To64(GET_STARTING_SECTOR(PartitionDescriptor
),
1665 /* Calculate the number of hidden sectors */
1666 HiddenSectors64
.QuadPart
= (PartitionInfo
->
1667 StartingOffset
.QuadPart
-
1670 PartitionInfo
->HiddenSectors
= HiddenSectors64
.LowPart
;
1672 /* Get the partition length */
1673 PartitionInfo
->PartitionLength
.QuadPart
=
1674 UInt32x32To64(GET_PARTITION_LENGTH(PartitionDescriptor
),
1677 /* FIXME: REACTOS HACK */
1678 PartitionInfo
->PartitionNumber
= i
+ 1;
1682 /* Otherwise, clear all the relevant fields */
1683 PartitionInfo
->BootIndicator
= FALSE
;
1684 PartitionInfo
->RecognizedPartition
= FALSE
;
1685 PartitionInfo
->StartingOffset
.QuadPart
= 0;
1686 PartitionInfo
->PartitionLength
.QuadPart
= 0;
1687 PartitionInfo
->HiddenSectors
= 0;
1689 /* FIXME: REACTOS HACK */
1690 PartitionInfo
->PartitionNumber
= 0;
1694 /* Finish debug log, and check for failure */
1696 if (!NT_SUCCESS(Status
)) break;
1698 /* Also check if we hit an invalid entry here */
1701 /* We did, so break out of the loop minus one entry */
1706 /* Reset the offset */
1707 Offset
.QuadPart
= 0;
1709 /* Go back to the descriptor array and loop it */
1710 PartitionDescriptor
= (PPARTITION_DESCRIPTOR
)
1711 &(((PUSHORT
)Buffer
)[PARTITION_TABLE_OFFSET
]);
1712 for (Entry
= 1; Entry
<= 4; Entry
++, PartitionDescriptor
++)
1714 /* Check if this is a container partition, since we skipped them */
1715 if (IsContainerPartition(PartitionDescriptor
->PartitionType
))
1717 /* Get its offset */
1718 Offset
.QuadPart
= VolumeOffset
.QuadPart
+
1720 GET_STARTING_SECTOR(PartitionDescriptor
),
1723 /* If this is a primary partition, this is the volume offset */
1724 if (IsPrimary
) VolumeOffset
= Offset
;
1726 /* Also update the maximum sector */
1727 MaxSector
= GET_PARTITION_LENGTH(PartitionDescriptor
);
1728 DPRINT1("FSTUB: MaxSector now = %#08lx\n", MaxSector
);
1733 /* Loop the next partitions, which are not primary anymore */
1735 } while (Offset
.HighPart
| Offset
.LowPart
);
1737 /* Check if this is a removable device that's probably a super-floppy */
1738 if ((DiskGeometry
.MediaType
== RemovableMedia
) &&
1743 /* Read the jump bytes to detect super-floppy */
1744 if ((BootSectorInfo
->JumpByte
[0] == 0xeb) ||
1745 (BootSectorInfo
->JumpByte
[0] == 0xe9))
1747 /* Super floppes don't have typical MBRs, so skip them */
1748 DPRINT1("FSTUB: Jump byte %#x found along with empty partition "
1749 "table - disk is a super floppy and has no valid MBR\n",
1750 BootSectorInfo
->JumpByte
);
1755 /* Check if we're still at partition -1 */
1758 /* The likely cause is the super floppy detection above */
1759 if ((MbrFound
) || (DiskGeometry
.MediaType
== RemovableMedia
))
1761 /* Print out debugging information */
1762 DPRINT1("FSTUB: Drive %#p has no valid MBR. Make it into a "
1765 DPRINT1("FSTUB: Drive has %#08lx sectors and is %#016I64x "
1767 EndSector
, EndSector
* DiskGeometry
.BytesPerSector
);
1769 /* We should at least have some sectors */
1772 /* Get the entry we'll use */
1773 PartitionInfo
= &(*PartitionBuffer
)->PartitionEntry
[0];
1775 /* Fill it out with data for a super-floppy */
1776 PartitionInfo
->RewritePartition
= FALSE
;
1777 PartitionInfo
->RecognizedPartition
= TRUE
;
1778 PartitionInfo
->PartitionType
= PARTITION_FAT_16
;
1779 PartitionInfo
->BootIndicator
= FALSE
;
1780 PartitionInfo
->HiddenSectors
= 0;
1781 PartitionInfo
->StartingOffset
.QuadPart
= 0;
1782 PartitionInfo
->PartitionLength
.QuadPart
= (EndSector
*
1786 /* FIXME: REACTOS HACK */
1787 PartitionInfo
->PartitionNumber
= 0;
1789 /* Set the signature and set the count back to 0 */
1790 (*PartitionBuffer
)->Signature
= 1;
1796 /* Otherwise, this isn't a super floppy, so set an invalid count */
1801 /* Set the partition count */
1802 (*PartitionBuffer
)->PartitionCount
= ++i
;
1804 /* If we have no count, delete the signature */
1805 if (!i
) (*PartitionBuffer
)->Signature
= 0;
1807 /* Free the buffer and check for success */
1808 if (Buffer
) ExFreePoolWithTag(Buffer
, TAG_FILE_SYSTEM
);
1809 if (!NT_SUCCESS(Status
)) ExFreePoolWithTag(*PartitionBuffer
, TAG_FILE_SYSTEM
);
1817 xHalIoSetPartitionInformation(IN PDEVICE_OBJECT DeviceObject
,
1818 IN ULONG SectorSize
,
1819 IN ULONG PartitionNumber
,
1820 IN ULONG PartitionType
)
1824 IO_STATUS_BLOCK IoStatusBlock
;
1826 LARGE_INTEGER Offset
, VolumeOffset
;
1827 PUCHAR Buffer
= NULL
;
1831 PPARTITION_DESCRIPTOR PartitionDescriptor
;
1832 BOOLEAN IsPrimary
= TRUE
, IsEzDrive
= FALSE
;
1834 PIO_STACK_LOCATION IoStackLocation
;
1835 VolumeOffset
.QuadPart
= Offset
.QuadPart
= 0;
1838 /* Normalize the buffer size */
1839 BufferSize
= max(512, SectorSize
);
1841 /* Check for EZ Drive */
1842 HalExamineMBR(DeviceObject
, BufferSize
, 0x55, &MbrBuffer
);
1845 /* EZ Drive found, bias the offset */
1847 ExFreePoolWithTag(MbrBuffer
, TAG_FILE_SYSTEM
);
1848 Offset
.QuadPart
= 512;
1851 /* Allocate our partition buffer */
1852 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, PAGE_SIZE
, TAG_FILE_SYSTEM
);
1853 if (!Buffer
) return STATUS_INSUFFICIENT_RESOURCES
;
1855 /* Initialize the event we'll use and loop partitions */
1856 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
1859 /* Reset the event since we reuse it */
1860 KeResetEvent(&Event
);
1862 /* Build the read IRP */
1863 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
1873 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1877 /* Make sure to disable volume verification */
1878 IoStackLocation
= IoGetNextIrpStackLocation(Irp
);
1879 IoStackLocation
->Flags
|= SL_OVERRIDE_VERIFY_VOLUME
;
1881 /* Call the driver */
1882 Status
= IoCallDriver(DeviceObject
, Irp
);
1883 if (Status
== STATUS_PENDING
)
1885 /* Wait for completion */
1886 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
1887 Status
= IoStatusBlock
.Status
;
1890 /* Check for failure */
1891 if (!NT_SUCCESS(Status
)) break;
1893 /* If we biased for EZ-Drive, unbias now */
1894 if (IsEzDrive
&& (Offset
.QuadPart
== 512)) Offset
.QuadPart
= 0;
1896 /* Make sure this is a valid MBR */
1897 if (((PUSHORT
)Buffer
)[BOOT_SIGNATURE_OFFSET
] != BOOT_RECORD_SIGNATURE
)
1899 /* It's not, fail */
1900 Status
= STATUS_BAD_MASTER_BOOT_RECORD
;
1904 /* Get the partition descriptors and loop them */
1905 PartitionDescriptor
= (PPARTITION_DESCRIPTOR
)
1906 &(((PUSHORT
)Buffer
)[PARTITION_TABLE_OFFSET
]);
1907 for (Entry
= 1; Entry
<= 4; Entry
++, PartitionDescriptor
++)
1909 /* Check if it's unused or a container partition */
1910 if ((PartitionDescriptor
->PartitionType
==
1911 PARTITION_ENTRY_UNUSED
) ||
1912 (IsContainerPartition(PartitionDescriptor
->PartitionType
)))
1914 /* Go to the next one */
1918 /* It's a valid partition, so increase the partition count */
1919 if (++i
== PartitionNumber
)
1921 /* We found a match, set the type */
1922 PartitionDescriptor
->PartitionType
= (UCHAR
)PartitionType
;
1924 /* Reset the reusable event */
1925 KeResetEvent(&Event
);
1927 /* Build the write IRP */
1928 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_WRITE
,
1938 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1942 /* Disable volume verification */
1943 IoStackLocation
= IoGetNextIrpStackLocation(Irp
);
1944 IoStackLocation
->Flags
|= SL_OVERRIDE_VERIFY_VOLUME
;
1946 /* Call the driver */
1947 Status
= IoCallDriver(DeviceObject
, Irp
);
1948 if (Status
== STATUS_PENDING
)
1950 /* Wait for completion */
1951 KeWaitForSingleObject(&Event
,
1956 Status
= IoStatusBlock
.Status
;
1959 /* We're done, break out of the loop */
1964 /* If we looped all the partitions, break out */
1965 if (Entry
<= NUM_PARTITION_TABLE_ENTRIES
) break;
1967 /* Nothing found yet, get the partition array again */
1968 PartitionDescriptor
= (PPARTITION_DESCRIPTOR
)
1969 &(((PUSHORT
)Buffer
)[PARTITION_TABLE_OFFSET
]);
1970 for (Entry
= 1; Entry
<= 4; Entry
++, PartitionDescriptor
++)
1972 /* Check if this was a container partition (we skipped these) */
1973 if (IsContainerPartition(PartitionDescriptor
->PartitionType
))
1975 /* Update the partition offset */
1976 Offset
.QuadPart
= VolumeOffset
.QuadPart
+
1977 GET_STARTING_SECTOR(PartitionDescriptor
) *
1980 /* If this was the primary partition, update the volume too */
1981 if (IsPrimary
) VolumeOffset
= Offset
;
1986 /* Check if we already searched all the partitions */
1987 if (Entry
> NUM_PARTITION_TABLE_ENTRIES
)
1989 /* Then we failed to find a good MBR */
1990 Status
= STATUS_BAD_MASTER_BOOT_RECORD
;
1994 /* Loop the next partitions, which are not primary anymore */
1996 } while (i
< PartitionNumber
);
1998 /* Everything done, cleanup */
1999 if (Buffer
) ExFreePoolWithTag(Buffer
, TAG_FILE_SYSTEM
);
2005 xHalIoWritePartitionTable(IN PDEVICE_OBJECT DeviceObject
,
2006 IN ULONG SectorSize
,
2007 IN ULONG SectorsPerTrack
,
2008 IN ULONG NumberOfHeads
,
2009 IN PDRIVE_LAYOUT_INFORMATION PartitionBuffer
)
2012 IO_STATUS_BLOCK IoStatusBlock
;
2014 NTSTATUS Status
= STATUS_SUCCESS
;
2018 PPARTITION_TABLE PartitionTable
;
2019 LARGE_INTEGER Offset
, NextOffset
, ExtendedOffset
, SectorOffset
;
2020 LARGE_INTEGER StartOffset
, PartitionLength
;
2023 BOOLEAN IsEzDrive
= FALSE
, IsSuperFloppy
= FALSE
, DoRewrite
= FALSE
, IsMbr
;
2024 ULONG ConventionalCylinders
;
2026 PDISK_LAYOUT DiskLayout
= (PDISK_LAYOUT
)PartitionBuffer
;
2028 UCHAR PartitionType
;
2029 PIO_STACK_LOCATION IoStackLocation
;
2030 PPARTITION_INFORMATION PartitionInfo
= PartitionBuffer
->PartitionEntry
;
2031 PPARTITION_INFORMATION TableEntry
;
2032 ExtendedOffset
.QuadPart
= NextOffset
.QuadPart
= Offset
.QuadPart
= 0;
2035 /* Normalize the buffer size */
2036 BufferSize
= max(512, SectorSize
);
2038 /* Get the partial drive geometry */
2039 xHalGetPartialGeometry(DeviceObject
, &ConventionalCylinders
, &DiskSize
);
2041 /* Check for EZ Drive */
2042 HalExamineMBR(DeviceObject
, BufferSize
, 0x55, &MbrBuffer
);
2045 /* EZ Drive found, bias the offset */
2047 ExFreePoolWithTag(MbrBuffer
, TAG_FILE_SYSTEM
);
2048 Offset
.QuadPart
= 512;
2051 /* Get the number of bits to shift to multiply by the sector size */
2052 for (k
= 0; k
< 32; k
++) if ((SectorSize
>> k
) == 1) break;
2054 /* Check if there's only one partition */
2055 if (PartitionBuffer
->PartitionCount
== 1)
2057 /* Check if it has no starting offset or hidden sectors */
2058 if (!(PartitionInfo
->StartingOffset
.QuadPart
) &&
2059 !(PartitionInfo
->HiddenSectors
))
2061 /* Then it's a super floppy */
2062 IsSuperFloppy
= TRUE
;
2064 /* Which also means it must be non-bootable FAT-16 */
2065 if ((PartitionInfo
->PartitionNumber
) ||
2066 (PartitionInfo
->PartitionType
!= PARTITION_FAT_16
) ||
2067 (PartitionInfo
->BootIndicator
))
2069 /* It's not, so we fail */
2070 return STATUS_INVALID_PARAMETER
;
2073 /* Check if it needs a rewrite, and disable EZ drive for sure */
2074 if (PartitionInfo
->RewritePartition
) DoRewrite
= TRUE
;
2079 /* Count the number of partition tables */
2080 DiskLayout
->TableCount
= (PartitionBuffer
->PartitionCount
+ 4 - 1) / 4;
2082 /* Allocate our partition buffer */
2083 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, PAGE_SIZE
, TAG_FILE_SYSTEM
);
2084 if (!Buffer
) return STATUS_INSUFFICIENT_RESOURCES
;
2086 /* Loop the entries */
2087 Entry
= (PPTE
)&Buffer
[PARTITION_TABLE_OFFSET
];
2088 for (i
= 0; i
< DiskLayout
->TableCount
; i
++)
2090 /* Set if this is the MBR partition */
2093 /* Initialize th event */
2094 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
2096 /* Build the read IRP */
2097 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
2107 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2111 /* Make sure to disable volume verification */
2112 IoStackLocation
= IoGetNextIrpStackLocation(Irp
);
2113 IoStackLocation
->Flags
|= SL_OVERRIDE_VERIFY_VOLUME
;
2115 /* Call the driver */
2116 Status
= IoCallDriver(DeviceObject
, Irp
);
2117 if (Status
== STATUS_PENDING
)
2119 /* Wait for completion */
2120 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
2121 Status
= IoStatusBlock
.Status
;
2124 /* Check for failure */
2125 if (!NT_SUCCESS(Status
)) break;
2127 /* If we biased for EZ-Drive, unbias now */
2128 if (IsEzDrive
&& (Offset
.QuadPart
== 512)) Offset
.QuadPart
= 0;
2130 /* Check if this is a normal disk */
2133 /* Set the boot record signature */
2134 Buffer
[BOOT_SIGNATURE_OFFSET
] = BOOT_RECORD_SIGNATURE
;
2136 /* By default, don't require a rewrite */
2139 /* Check if we don't have an offset */
2140 if (!Offset
.QuadPart
)
2142 /* Check if the signature doesn't match */
2143 if (((PULONG
)Buffer
)[PARTITION_TABLE_OFFSET
/ 2 - 1] !=
2144 PartitionBuffer
->Signature
)
2146 /* Then write the signature and now w need a rewrite */
2147 ((PULONG
)Buffer
)[PARTITION_TABLE_OFFSET
/ 2 - 1] =
2148 PartitionBuffer
->Signature
;
2153 /* Loop the partition table entries */
2154 PartitionTable
= &DiskLayout
->PartitionTable
[i
];
2155 for (j
= 0; j
< 4; j
++)
2157 /* Get the current entry and type */
2158 TableEntry
= &PartitionTable
->PartitionEntry
[j
];
2159 PartitionType
= TableEntry
->PartitionType
;
2161 /* Check if the entry needs a rewrite */
2162 if (TableEntry
->RewritePartition
)
2164 /* Then we need one too */
2167 /* Save the type and if it's a bootable partition */
2168 Entry
[j
].PartitionType
= TableEntry
->PartitionType
;
2169 Entry
[j
].ActiveFlag
= TableEntry
->BootIndicator
? 0x80 : 0;
2171 /* Make sure it's used */
2172 if (PartitionType
!= PARTITION_ENTRY_UNUSED
)
2174 /* Make sure it's not a container (unless primary) */
2175 if ((IsMbr
) || !(IsContainerPartition(PartitionType
)))
2177 /* Use the partition offset */
2178 StartOffset
.QuadPart
= Offset
.QuadPart
;
2182 /* Use the extended logical partition offset */
2183 StartOffset
.QuadPart
= ExtendedOffset
.QuadPart
;
2186 /* Set the sector offset */
2187 SectorOffset
.QuadPart
= TableEntry
->
2188 StartingOffset
.QuadPart
-
2189 StartOffset
.QuadPart
;
2191 /* Now calculate the starting sector */
2192 StartOffset
.QuadPart
= SectorOffset
.QuadPart
>> k
;
2193 Entry
[j
].StartingSector
= StartOffset
.LowPart
;
2195 /* As well as the length */
2196 PartitionLength
.QuadPart
= TableEntry
->PartitionLength
.
2198 Entry
[j
].PartitionLength
= PartitionLength
.LowPart
;
2200 /* Calculate the CHS values */
2201 HalpCalculateChsValues(&TableEntry
->StartingOffset
,
2202 &TableEntry
->PartitionLength
,
2206 ConventionalCylinders
,
2207 (PPARTITION_DESCRIPTOR
)
2212 /* Otherwise set up an empty entry */
2213 Entry
[j
].StartingSector
= 0;
2214 Entry
[j
].PartitionLength
= 0;
2215 Entry
[j
].StartingTrack
= 0;
2216 Entry
[j
].EndingTrack
= 0;
2217 Entry
[j
].StartingCylinder
= 0;
2218 Entry
[j
].EndingCylinder
= 0;
2222 /* Check if this is a container partition */
2223 if (IsContainerPartition(PartitionType
))
2225 /* Then update the offset to use */
2226 NextOffset
= TableEntry
->StartingOffset
;
2231 /* Check if we need to write back the buffer */
2234 /* We don't need to do this again */
2237 /* Initialize the event */
2238 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
2240 /* If we unbiased for EZ-Drive, rebias now */
2241 if ((IsEzDrive
) && !(Offset
.QuadPart
)) Offset
.QuadPart
= 512;
2243 /* Build the write IRP */
2244 Irp
= IoBuildSynchronousFsdRequest(IRP_MJ_WRITE
,
2254 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2258 /* Make sure to disable volume verification */
2259 IoStackLocation
= IoGetNextIrpStackLocation(Irp
);
2260 IoStackLocation
->Flags
|= SL_OVERRIDE_VERIFY_VOLUME
;
2262 /* Call the driver */
2263 Status
= IoCallDriver(DeviceObject
, Irp
);
2264 if (Status
== STATUS_PENDING
)
2266 /* Wait for completion */
2267 KeWaitForSingleObject(&Event
,
2272 Status
= IoStatusBlock
.Status
;
2275 /* Check for failure */
2276 if (!NT_SUCCESS(Status
)) break;
2278 /* If we biased for EZ-Drive, unbias now */
2279 if (IsEzDrive
&& (Offset
.QuadPart
== 512)) Offset
.QuadPart
= 0;
2282 /* Update the partition offset and set the extended offset if needed */
2283 Offset
= NextOffset
;
2284 if (IsMbr
) ExtendedOffset
= NextOffset
;
2287 /* If we had a buffer, free it, then return status */
2288 if (Buffer
) ExFreePoolWithTag(Buffer
, TAG_FILE_SYSTEM
);
2292 /* PUBLIC FUNCTIONS **********************************************************/
2299 HalExamineMBR(IN PDEVICE_OBJECT DeviceObject
,
2300 IN ULONG SectorSize
,
2301 IN ULONG MbrTypeIdentifier
,
2302 OUT PVOID
*MbrBuffer
)
2304 HALDISPATCH
->HalExamineMBR(DeviceObject
,
2315 IoReadPartitionTable(IN PDEVICE_OBJECT DeviceObject
,
2316 IN ULONG SectorSize
,
2317 IN BOOLEAN ReturnRecognizedPartitions
,
2318 IN OUT PDRIVE_LAYOUT_INFORMATION
*PartitionBuffer
)
2320 return HALDISPATCH
->HalIoReadPartitionTable(DeviceObject
,
2322 ReturnRecognizedPartitions
,
2331 IoSetPartitionInformation(IN PDEVICE_OBJECT DeviceObject
,
2332 IN ULONG SectorSize
,
2333 IN ULONG PartitionNumber
,
2334 IN ULONG PartitionType
)
2336 return HALDISPATCH
->HalIoSetPartitionInformation(DeviceObject
,
2347 IoWritePartitionTable(IN PDEVICE_OBJECT DeviceObject
,
2348 IN ULONG SectorSize
,
2349 IN ULONG SectorsPerTrack
,
2350 IN ULONG NumberOfHeads
,
2351 IN PDRIVE_LAYOUT_INFORMATION PartitionBuffer
)
2353 return HALDISPATCH
->HalIoWritePartitionTable(DeviceObject
,
2365 IoAssignDriveLetters(IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
2366 IN PSTRING NtDeviceName
,
2367 OUT PUCHAR NtSystemPath
,
2368 OUT PSTRING NtSystemPathString
)
2370 HALDISPATCH
->HalIoAssignDriveLetters(LoaderBlock
,
2373 NtSystemPathString
);