3 * Copyright (C) 2002, 2003, 2004, 2005 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /* COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS text-mode setup
21 * FILE: subsys/system/usetup/partlist.c
22 * PURPOSE: Partition list functions
23 * PROGRAMMER: Eric Kohl
24 * Casper S. Hornstrup (chorns@users.sourceforge.net)
34 //#define DUMP_PARTITION_TABLE
36 /* FUNCTIONS ****************************************************************/
38 #ifdef DUMP_PARTITION_TABLE
44 PPARTITION_INFORMATION PartitionInfo
;
47 for (i
= 0; i
< DiskEntry
->LayoutBuffer
->PartitionCount
; i
++)
49 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[i
];
50 DPRINT1("\n%lu: %12I64u %12I64u %10lu %2lu %2x %c %c\n",
52 PartitionInfo
->StartingOffset
.QuadPart
,
53 PartitionInfo
->PartitionLength
.QuadPart
,
54 PartitionInfo
->HiddenSectors
,
55 PartitionInfo
->PartitionNumber
,
56 PartitionInfo
->PartitionType
,
57 PartitionInfo
->BootIndicator
? '*': ' ',
58 PartitionInfo
->RewritePartition
? 'Y': 'N');
71 Temp
= Value
/ Alignment
;
73 return Temp
* Alignment
;
79 IN ULONGLONG Dividend
,
82 return (Dividend
+ Divisor
/ 2) / Divisor
;
91 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
95 RtlInitUnicodeString(&DiskEntry
->DriverName
,
99 L
"\\Scsi\\Scsi Port %lu",
102 RtlZeroMemory(&QueryTable
,
105 QueryTable
[0].Name
= L
"Driver";
106 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
107 QueryTable
[0].EntryContext
= &DiskEntry
->DriverName
;
109 Status
= RtlQueryRegistryValues(RTL_REGISTRY_DEVICEMAP
,
114 if (!NT_SUCCESS(Status
))
116 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
126 PDISKENTRY DiskEntry
;
127 PPARTENTRY PartEntry
;
134 /* Assign drive letters to primary partitions */
135 Entry1
= List
->DiskListHead
.Flink
;
136 while (Entry1
!= &List
->DiskListHead
)
138 DiskEntry
= CONTAINING_RECORD(Entry1
, DISKENTRY
, ListEntry
);
140 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
141 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
143 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
145 PartEntry
->DriveLetter
= 0;
147 if (PartEntry
->IsPartitioned
&&
148 !IsContainerPartition(PartEntry
->PartitionType
))
150 if (IsRecognizedPartition(PartEntry
->PartitionType
) ||
151 (PartEntry
->PartitionType
== PARTITION_ENTRY_UNUSED
&&
152 PartEntry
->SectorCount
.QuadPart
!= 0LL))
156 PartEntry
->DriveLetter
= Letter
;
162 Entry2
= Entry2
->Flink
;
165 Entry1
= Entry1
->Flink
;
168 /* Assign drive letters to logical drives */
169 Entry1
= List
->DiskListHead
.Flink
;
170 while (Entry1
!= &List
->DiskListHead
)
172 DiskEntry
= CONTAINING_RECORD(Entry1
, DISKENTRY
, ListEntry
);
174 Entry2
= DiskEntry
->LogicalPartListHead
.Flink
;
175 while (Entry2
!= &DiskEntry
->LogicalPartListHead
)
177 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
179 PartEntry
->DriveLetter
= 0;
181 if (PartEntry
->IsPartitioned
)
183 if (IsRecognizedPartition(PartEntry
->PartitionType
) ||
184 (PartEntry
->PartitionType
== PARTITION_ENTRY_UNUSED
&&
185 PartEntry
->SectorCount
.QuadPart
!= 0LL))
189 PartEntry
->DriveLetter
= Letter
;
195 Entry2
= Entry2
->Flink
;
198 Entry1
= Entry1
->Flink
;
205 UpdatePartitionNumbers(
206 PDISKENTRY DiskEntry
)
208 PPARTENTRY PartEntry
;
210 // ULONG PartitionNumber = 1;
211 ULONG PartitionIndex
= 0;
213 Entry
= DiskEntry
->PrimaryPartListHead
.Flink
;
214 while (Entry
!= &DiskEntry
->PrimaryPartListHead
)
216 PartEntry
= CONTAINING_RECORD(Entry
,
220 if (PartEntry
->IsPartitioned
== FALSE
)
222 // PartEntry->PartitionNumber = 0;
223 PartEntry
->PartitionIndex
= (ULONG
)-1;
227 if (IsContainerPartition(PartEntry
->PartitionType
))
229 // PartEntry->PartitionNumber = 0;
231 else if (PartEntry
->PartitionType
== PARTITION_ENTRY_UNUSED
&&
232 PartEntry
->SectorCount
.QuadPart
== 0ULL)
234 // PartEntry->PartitionNumber = 0;
238 // PartEntry->PartitionNumber = PartitionNumber++;
241 PartEntry
->PartitionIndex
= PartitionIndex
++;
244 Entry
= Entry
->Flink
;
251 DiskIdentifierQueryRoutine(
259 PBIOSDISKENTRY BiosDiskEntry
= (PBIOSDISKENTRY
)Context
;
260 UNICODE_STRING NameU
;
262 if (ValueType
== REG_SZ
&&
263 ValueLength
== 20 * sizeof(WCHAR
))
265 NameU
.Buffer
= (PWCHAR
)ValueData
;
266 NameU
.Length
= NameU
.MaximumLength
= 8 * sizeof(WCHAR
);
267 RtlUnicodeStringToInteger(&NameU
, 16, &BiosDiskEntry
->Checksum
);
269 NameU
.Buffer
= (PWCHAR
)ValueData
+ 9;
270 RtlUnicodeStringToInteger(&NameU
, 16, &BiosDiskEntry
->Signature
);
272 return STATUS_SUCCESS
;
275 return STATUS_UNSUCCESSFUL
;
281 DiskConfigurationDataQueryRoutine(
289 PBIOSDISKENTRY BiosDiskEntry
= (PBIOSDISKENTRY
)Context
;
290 PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor
;
291 PCM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry
;
294 if (ValueType
!= REG_FULL_RESOURCE_DESCRIPTOR
||
295 ValueLength
< sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
296 return STATUS_UNSUCCESSFUL
;
298 FullResourceDescriptor
= (PCM_FULL_RESOURCE_DESCRIPTOR
)ValueData
;
300 /* Hm. Version and Revision are not set on Microsoft Windows XP... */
302 if (FullResourceDescriptor
->PartialResourceList
.Version
!= 1 ||
303 FullResourceDescriptor
->PartialResourceList
.Revision
!= 1)
304 return STATUS_UNSUCCESSFUL
;
307 for (i
= 0; i
< FullResourceDescriptor
->PartialResourceList
.Count
; i
++)
309 if (FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].Type
!= CmResourceTypeDeviceSpecific
||
310 FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].u
.DeviceSpecificData
.DataSize
!= sizeof(CM_DISK_GEOMETRY_DEVICE_DATA
))
313 DiskGeometry
= (PCM_DISK_GEOMETRY_DEVICE_DATA
)&FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
+ 1];
314 BiosDiskEntry
->DiskGeometry
= *DiskGeometry
;
316 return STATUS_SUCCESS
;
319 return STATUS_UNSUCCESSFUL
;
325 SystemConfigurationDataQueryRoutine(
333 PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor
;
334 PCM_INT13_DRIVE_PARAMETER
* Int13Drives
= (PCM_INT13_DRIVE_PARAMETER
*)Context
;
337 if (ValueType
!= REG_FULL_RESOURCE_DESCRIPTOR
||
338 ValueLength
< sizeof (CM_FULL_RESOURCE_DESCRIPTOR
))
339 return STATUS_UNSUCCESSFUL
;
341 FullResourceDescriptor
= (PCM_FULL_RESOURCE_DESCRIPTOR
)ValueData
;
343 /* Hm. Version and Revision are not set on Microsoft Windows XP... */
345 if (FullResourceDescriptor
->PartialResourceList
.Version
!= 1 ||
346 FullResourceDescriptor
->PartialResourceList
.Revision
!= 1)
347 return STATUS_UNSUCCESSFUL
;
350 for (i
= 0; i
< FullResourceDescriptor
->PartialResourceList
.Count
; i
++)
352 if (FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].Type
!= CmResourceTypeDeviceSpecific
||
353 FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].u
.DeviceSpecificData
.DataSize
% sizeof(CM_INT13_DRIVE_PARAMETER
) != 0)
356 *Int13Drives
= (CM_INT13_DRIVE_PARAMETER
*) RtlAllocateHeap(ProcessHeap
, 0, FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].u
.DeviceSpecificData
.DataSize
);
357 if (*Int13Drives
== NULL
)
358 return STATUS_NO_MEMORY
;
361 &FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
+ 1],
362 FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].u
.DeviceSpecificData
.DataSize
);
363 return STATUS_SUCCESS
;
366 return STATUS_UNSUCCESSFUL
;
370 #define ROOT_NAME L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter"
373 EnumerateBiosDiskEntries(
376 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
381 PCM_INT13_DRIVE_PARAMETER Int13Drives
;
382 PBIOSDISKENTRY BiosDiskEntry
;
384 memset(QueryTable
, 0, sizeof(QueryTable
));
386 QueryTable
[1].Name
= L
"Configuration Data";
387 QueryTable
[1].QueryRoutine
= SystemConfigurationDataQueryRoutine
;
389 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
390 L
"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System",
394 if (!NT_SUCCESS(Status
))
396 DPRINT1("Unable to query the 'Configuration Data' key in '\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System', status=%lx\n", Status
);
403 swprintf(Name
, L
"%s\\%lu", ROOT_NAME
, AdapterCount
);
404 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
409 if (!NT_SUCCESS(Status
))
414 swprintf(Name
, L
"%s\\%lu\\DiskController", ROOT_NAME
, AdapterCount
);
415 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
420 if (NT_SUCCESS(Status
))
424 swprintf(Name
, L
"%s\\%lu\\DiskController\\0", ROOT_NAME
, AdapterCount
);
425 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
430 if (!NT_SUCCESS(Status
))
432 RtlFreeHeap(ProcessHeap
, 0, Int13Drives
);
436 swprintf(Name
, L
"%s\\%lu\\DiskController\\0\\DiskPeripheral", ROOT_NAME
, AdapterCount
);
437 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
442 if (NT_SUCCESS(Status
))
444 QueryTable
[0].Name
= L
"Identifier";
445 QueryTable
[0].QueryRoutine
= DiskIdentifierQueryRoutine
;
446 QueryTable
[1].Name
= L
"Configuration Data";
447 QueryTable
[1].QueryRoutine
= DiskConfigurationDataQueryRoutine
;
452 BiosDiskEntry
= (BIOSDISKENTRY
*) RtlAllocateHeap(ProcessHeap
, HEAP_ZERO_MEMORY
, sizeof(BIOSDISKENTRY
));
453 if (BiosDiskEntry
== NULL
)
458 swprintf(Name
, L
"%s\\%lu\\DiskController\\0\\DiskPeripheral\\%lu", ROOT_NAME
, AdapterCount
, DiskCount
);
459 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
462 (PVOID
)BiosDiskEntry
,
464 if (!NT_SUCCESS(Status
))
466 RtlFreeHeap(ProcessHeap
, 0, BiosDiskEntry
);
470 BiosDiskEntry
->DiskNumber
= DiskCount
;
471 BiosDiskEntry
->Recognized
= FALSE
;
473 if (DiskCount
< Int13Drives
[0].NumberDrives
)
475 BiosDiskEntry
->Int13DiskData
= Int13Drives
[DiskCount
];
479 DPRINT1("Didn't find int13 drive datas for disk %u\n", DiskCount
);
482 InsertTailList(&PartList
->BiosDiskListHead
, &BiosDiskEntry
->ListEntry
);
484 DPRINT("DiskNumber: %lu\n", BiosDiskEntry
->DiskNumber
);
485 DPRINT("Signature: %08lx\n", BiosDiskEntry
->Signature
);
486 DPRINT("Checksum: %08lx\n", BiosDiskEntry
->Checksum
);
487 DPRINT("BytesPerSector: %lu\n", BiosDiskEntry
->DiskGeometry
.BytesPerSector
);
488 DPRINT("NumberOfCylinders: %lu\n", BiosDiskEntry
->DiskGeometry
.NumberOfCylinders
);
489 DPRINT("NumberOfHeads: %lu\n", BiosDiskEntry
->DiskGeometry
.NumberOfHeads
);
490 DPRINT("DriveSelect: %02x\n", BiosDiskEntry
->Int13DiskData
.DriveSelect
);
491 DPRINT("MaxCylinders: %lu\n", BiosDiskEntry
->Int13DiskData
.MaxCylinders
);
492 DPRINT("SectorsPerTrack: %d\n", BiosDiskEntry
->Int13DiskData
.SectorsPerTrack
);
493 DPRINT("MaxHeads: %d\n", BiosDiskEntry
->Int13DiskData
.MaxHeads
);
494 DPRINT("NumberDrives: %d\n", BiosDiskEntry
->Int13DiskData
.NumberDrives
);
500 RtlFreeHeap(ProcessHeap
, 0, Int13Drives
);
508 RtlFreeHeap(ProcessHeap
, 0, Int13Drives
);
516 PDISKENTRY DiskEntry
,
517 ULONG PartitionIndex
,
518 BOOLEAN LogicalPartition
)
520 PPARTITION_INFORMATION PartitionInfo
;
521 PPARTENTRY PartEntry
;
523 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[PartitionIndex
];
524 if (PartitionInfo
->PartitionType
== 0 ||
525 (LogicalPartition
== TRUE
&& IsContainerPartition(PartitionInfo
->PartitionType
)))
528 PartEntry
= RtlAllocateHeap(ProcessHeap
,
531 if (PartEntry
== NULL
)
536 PartEntry
->DiskEntry
= DiskEntry
;
538 PartEntry
->StartSector
.QuadPart
= (ULONGLONG
)PartitionInfo
->StartingOffset
.QuadPart
/ DiskEntry
->BytesPerSector
;
539 PartEntry
->SectorCount
.QuadPart
= (ULONGLONG
)PartitionInfo
->PartitionLength
.QuadPart
/ DiskEntry
->BytesPerSector
;
541 PartEntry
->BootIndicator
= PartitionInfo
->BootIndicator
;
542 PartEntry
->PartitionType
= PartitionInfo
->PartitionType
;
543 PartEntry
->HiddenSectors
= PartitionInfo
->HiddenSectors
;
545 PartEntry
->LogicalPartition
= LogicalPartition
;
546 PartEntry
->IsPartitioned
= TRUE
;
547 PartEntry
->PartitionNumber
= PartitionInfo
->PartitionNumber
;
548 PartEntry
->PartitionIndex
= PartitionIndex
;
550 if (IsContainerPartition(PartEntry
->PartitionType
))
552 PartEntry
->FormatState
= Unformatted
;
554 if (LogicalPartition
== FALSE
&& DiskEntry
->ExtendedPartition
== NULL
)
555 DiskEntry
->ExtendedPartition
= PartEntry
;
557 else if ((PartEntry
->PartitionType
== PARTITION_FAT_12
) ||
558 (PartEntry
->PartitionType
== PARTITION_FAT_16
) ||
559 (PartEntry
->PartitionType
== PARTITION_HUGE
) ||
560 (PartEntry
->PartitionType
== PARTITION_XINT13
) ||
561 (PartEntry
->PartitionType
== PARTITION_FAT32
) ||
562 (PartEntry
->PartitionType
== PARTITION_FAT32_XINT13
))
565 if (CheckFatFormat())
567 PartEntry
->FormatState
= Preformatted
;
571 PartEntry
->FormatState
= Unformatted
;
574 PartEntry
->FormatState
= Preformatted
;
576 else if (PartEntry
->PartitionType
== PARTITION_EXT2
)
579 if (CheckExt2Format())
581 PartEntry
->FormatState
= Preformatted
;
585 PartEntry
->FormatState
= Unformatted
;
588 PartEntry
->FormatState
= Preformatted
;
590 else if (PartEntry
->PartitionType
== PARTITION_IFS
)
593 if (CheckNtfsFormat())
595 PartEntry
->FormatState
= Preformatted
;
597 else if (CheckHpfsFormat())
599 PartEntry
->FormatState
= Preformatted
;
603 PartEntry
->FormatState
= Unformatted
;
606 PartEntry
->FormatState
= Preformatted
;
610 PartEntry
->FormatState
= UnknownFormat
;
613 if (LogicalPartition
)
614 InsertTailList(&DiskEntry
->LogicalPartListHead
,
615 &PartEntry
->ListEntry
);
617 InsertTailList(&DiskEntry
->PrimaryPartListHead
,
618 &PartEntry
->ListEntry
);
624 ScanForUnpartitionedDiskSpace(
625 PDISKENTRY DiskEntry
)
627 ULONGLONG LastStartSector
;
628 ULONGLONG LastSectorCount
;
629 ULONGLONG LastUnusedSectorCount
;
630 PPARTENTRY PartEntry
;
631 PPARTENTRY NewPartEntry
;
634 DPRINT("ScanForUnpartitionedDiskSpace()\n");
636 if (IsListEmpty(&DiskEntry
->PrimaryPartListHead
))
638 DPRINT1("No primary partition!\n");
640 /* Create a partition table that represents the empty disk */
641 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
644 if (NewPartEntry
== NULL
)
647 NewPartEntry
->DiskEntry
= DiskEntry
;
649 NewPartEntry
->IsPartitioned
= FALSE
;
650 NewPartEntry
->StartSector
.QuadPart
= (ULONGLONG
)DiskEntry
->SectorAlignment
;
651 NewPartEntry
->SectorCount
.QuadPart
= Align(DiskEntry
->SectorCount
.QuadPart
, DiskEntry
->SectorAlignment
) -
652 NewPartEntry
->StartSector
.QuadPart
;
654 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
655 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
656 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
658 NewPartEntry
->FormatState
= Unformatted
;
660 InsertTailList(&DiskEntry
->PrimaryPartListHead
,
661 &NewPartEntry
->ListEntry
);
666 /* Start partition at head 1, cylinder 0 */
667 LastStartSector
= DiskEntry
->SectorAlignment
;
668 LastSectorCount
= 0ULL;
669 LastUnusedSectorCount
= 0ULL;
671 Entry
= DiskEntry
->PrimaryPartListHead
.Flink
;
672 while (Entry
!= &DiskEntry
->PrimaryPartListHead
)
674 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
676 if (PartEntry
->PartitionType
!= PARTITION_ENTRY_UNUSED
||
677 PartEntry
->SectorCount
.QuadPart
!= 0ULL)
679 LastUnusedSectorCount
=
680 PartEntry
->StartSector
.QuadPart
- (LastStartSector
+ LastSectorCount
);
682 if (PartEntry
->StartSector
.QuadPart
> (LastStartSector
+ LastSectorCount
) &&
683 LastUnusedSectorCount
>= (ULONGLONG
)DiskEntry
->SectorAlignment
)
685 DPRINT("Unpartitioned disk space %I64u sectors\n", LastUnusedSectorCount
);
687 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
690 if (NewPartEntry
== NULL
)
693 NewPartEntry
->DiskEntry
= DiskEntry
;
695 NewPartEntry
->IsPartitioned
= FALSE
;
696 NewPartEntry
->StartSector
.QuadPart
= LastStartSector
+ LastSectorCount
;
697 NewPartEntry
->SectorCount
.QuadPart
= Align(NewPartEntry
->StartSector
.QuadPart
+ LastUnusedSectorCount
, DiskEntry
->SectorAlignment
) -
698 NewPartEntry
->StartSector
.QuadPart
;
700 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
701 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
702 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
704 NewPartEntry
->FormatState
= Unformatted
;
706 /* Insert the table into the list */
707 InsertTailList(&PartEntry
->ListEntry
,
708 &NewPartEntry
->ListEntry
);
711 LastStartSector
= PartEntry
->StartSector
.QuadPart
;
712 LastSectorCount
= PartEntry
->SectorCount
.QuadPart
;
715 Entry
= Entry
->Flink
;
718 /* Check for trailing unpartitioned disk space */
719 if ((LastStartSector
+ LastSectorCount
) < DiskEntry
->SectorCount
.QuadPart
)
721 LastUnusedSectorCount
= Align(DiskEntry
->SectorCount
.QuadPart
- (LastStartSector
+ LastSectorCount
), DiskEntry
->SectorAlignment
);
723 if (LastUnusedSectorCount
>= (ULONGLONG
)DiskEntry
->SectorAlignment
)
725 DPRINT("Unpartitioned disk space: %I64u sectors\n", LastUnusedSectorCount
);
727 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
730 if (NewPartEntry
== NULL
)
733 NewPartEntry
->DiskEntry
= DiskEntry
;
735 NewPartEntry
->IsPartitioned
= FALSE
;
736 NewPartEntry
->StartSector
.QuadPart
= LastStartSector
+ LastSectorCount
;
737 NewPartEntry
->SectorCount
.QuadPart
= Align(NewPartEntry
->StartSector
.QuadPart
+ LastUnusedSectorCount
, DiskEntry
->SectorAlignment
) -
738 NewPartEntry
->StartSector
.QuadPart
;
740 DPRINT("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
741 DPRINT("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
742 DPRINT("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
744 NewPartEntry
->FormatState
= Unformatted
;
746 /* Append the table to the list */
747 InsertTailList(&DiskEntry
->PrimaryPartListHead
,
748 &NewPartEntry
->ListEntry
);
752 if (DiskEntry
->ExtendedPartition
!= NULL
)
754 if (IsListEmpty(&DiskEntry
->LogicalPartListHead
))
756 DPRINT1("No logical partition!\n");
758 /* Create a partition table entry that represents the empty extended partition */
759 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
762 if (NewPartEntry
== NULL
)
765 NewPartEntry
->DiskEntry
= DiskEntry
;
766 NewPartEntry
->LogicalPartition
= TRUE
;
768 NewPartEntry
->IsPartitioned
= FALSE
;
769 NewPartEntry
->StartSector
.QuadPart
= DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ (ULONGLONG
)DiskEntry
->SectorAlignment
;
770 NewPartEntry
->SectorCount
.QuadPart
= DiskEntry
->ExtendedPartition
->SectorCount
.QuadPart
- (ULONGLONG
)DiskEntry
->SectorAlignment
;
772 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
773 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
774 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
776 NewPartEntry
->FormatState
= Unformatted
;
778 InsertTailList(&DiskEntry
->LogicalPartListHead
,
779 &NewPartEntry
->ListEntry
);
784 /* Start partition at head 1, cylinder 0 */
785 LastStartSector
= DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ (ULONGLONG
)DiskEntry
->SectorAlignment
;
786 LastSectorCount
= 0ULL;
787 LastUnusedSectorCount
= 0ULL;
789 Entry
= DiskEntry
->LogicalPartListHead
.Flink
;
790 while (Entry
!= &DiskEntry
->LogicalPartListHead
)
792 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
794 if (PartEntry
->PartitionType
!= PARTITION_ENTRY_UNUSED
||
795 PartEntry
->SectorCount
.QuadPart
!= 0ULL)
797 LastUnusedSectorCount
=
798 PartEntry
->StartSector
.QuadPart
- (ULONGLONG
)DiskEntry
->SectorAlignment
- (LastStartSector
+ LastSectorCount
);
800 if ((PartEntry
->StartSector
.QuadPart
- (ULONGLONG
)DiskEntry
->SectorAlignment
) > (LastStartSector
+ LastSectorCount
) &&
801 LastUnusedSectorCount
>= (ULONGLONG
)DiskEntry
->SectorAlignment
)
803 DPRINT("Unpartitioned disk space %I64u sectors\n", LastUnusedSectorCount
);
805 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
808 if (NewPartEntry
== NULL
)
811 NewPartEntry
->DiskEntry
= DiskEntry
;
812 NewPartEntry
->LogicalPartition
= TRUE
;
814 NewPartEntry
->IsPartitioned
= FALSE
;
815 NewPartEntry
->StartSector
.QuadPart
= LastStartSector
+ LastSectorCount
;
816 NewPartEntry
->SectorCount
.QuadPart
= Align(NewPartEntry
->StartSector
.QuadPart
+ LastUnusedSectorCount
, DiskEntry
->SectorAlignment
) -
817 NewPartEntry
->StartSector
.QuadPart
;
819 DPRINT("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
820 DPRINT("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
821 DPRINT("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
823 NewPartEntry
->FormatState
= Unformatted
;
825 /* Insert the table into the list */
826 InsertTailList(&PartEntry
->ListEntry
,
827 &NewPartEntry
->ListEntry
);
830 LastStartSector
= PartEntry
->StartSector
.QuadPart
;
831 LastSectorCount
= PartEntry
->SectorCount
.QuadPart
;
834 Entry
= Entry
->Flink
;
837 /* Check for trailing unpartitioned disk space */
838 if ((LastStartSector
+ LastSectorCount
) < DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ DiskEntry
->ExtendedPartition
->SectorCount
.QuadPart
)
840 LastUnusedSectorCount
= Align(DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ DiskEntry
->ExtendedPartition
->SectorCount
.QuadPart
- (LastStartSector
+ LastSectorCount
), DiskEntry
->SectorAlignment
);
842 if (LastUnusedSectorCount
>= (ULONGLONG
)DiskEntry
->SectorAlignment
)
844 DPRINT("Unpartitioned disk space: %I64u sectors\n", LastUnusedSectorCount
);
846 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
849 if (NewPartEntry
== NULL
)
852 NewPartEntry
->DiskEntry
= DiskEntry
;
853 NewPartEntry
->LogicalPartition
= TRUE
;
855 NewPartEntry
->IsPartitioned
= FALSE
;
856 NewPartEntry
->StartSector
.QuadPart
= LastStartSector
+ LastSectorCount
;
857 NewPartEntry
->SectorCount
.QuadPart
= Align(NewPartEntry
->StartSector
.QuadPart
+ LastUnusedSectorCount
, DiskEntry
->SectorAlignment
) -
858 NewPartEntry
->StartSector
.QuadPart
;
860 DPRINT("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
861 DPRINT("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
862 DPRINT("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
864 NewPartEntry
->FormatState
= Unformatted
;
866 /* Append the table to the list */
867 InsertTailList(&DiskEntry
->LogicalPartListHead
,
868 &NewPartEntry
->ListEntry
);
873 DPRINT("ScanForUnpartitionedDiskSpace() done\n");
881 IN PDISKENTRY DiskEntry
)
883 LARGE_INTEGER SystemTime
;
884 TIME_FIELDS TimeFields
;
886 PDISKENTRY DiskEntry2
;
889 Buffer
= (PUCHAR
)&DiskEntry
->LayoutBuffer
->Signature
;
893 NtQuerySystemTime(&SystemTime
);
894 RtlTimeToTimeFields(&SystemTime
, &TimeFields
);
896 Buffer
[0] = (UCHAR
)(TimeFields
.Year
& 0xFF) + (UCHAR
)(TimeFields
.Hour
& 0xFF);
897 Buffer
[1] = (UCHAR
)(TimeFields
.Year
>> 8) + (UCHAR
)(TimeFields
.Minute
& 0xFF);
898 Buffer
[2] = (UCHAR
)(TimeFields
.Month
& 0xFF) + (UCHAR
)(TimeFields
.Second
& 0xFF);
899 Buffer
[3] = (UCHAR
)(TimeFields
.Day
& 0xFF) + (UCHAR
)(TimeFields
.Milliseconds
& 0xFF);
901 if (DiskEntry
->LayoutBuffer
->Signature
== 0)
906 /* check if the signature already exist */
908 * Check also signatures from disks, which are
909 * not visible (bootable) by the bios.
911 Entry2
= List
->DiskListHead
.Flink
;
912 while (Entry2
!= &List
->DiskListHead
)
914 DiskEntry2
= CONTAINING_RECORD(Entry2
, DISKENTRY
, ListEntry
);
916 if (DiskEntry
!= DiskEntry2
&&
917 DiskEntry
->LayoutBuffer
->Signature
== DiskEntry2
->LayoutBuffer
->Signature
)
920 Entry2
= Entry2
->Flink
;
923 if (Entry2
== &List
->DiskListHead
)
931 UpdateDiskSignatures(
935 PDISKENTRY DiskEntry
;
937 /* Print partition lines*/
938 Entry
= List
->DiskListHead
.Flink
;
939 while (Entry
!= &List
->DiskListHead
)
941 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
943 if (DiskEntry
->LayoutBuffer
&&
944 DiskEntry
->LayoutBuffer
->Signature
== 0)
946 SetDiskSignature(List
, DiskEntry
);
947 DiskEntry
->LayoutBuffer
->PartitionEntry
[0].RewritePartition
= TRUE
;
950 Entry
= Entry
->Flink
;
962 DISK_GEOMETRY DiskGeometry
;
963 SCSI_ADDRESS ScsiAddress
;
964 PDISKENTRY DiskEntry
;
965 IO_STATUS_BLOCK Iosb
;
967 PPARTITION_SECTOR Mbr
;
969 LARGE_INTEGER FileOffset
;
970 WCHAR Identifier
[20];
974 PLIST_ENTRY ListEntry
;
975 PBIOSDISKENTRY BiosDiskEntry
;
976 ULONG LayoutBufferSize
;
977 PDRIVE_LAYOUT_INFORMATION NewLayoutBuffer
;
979 Status
= NtDeviceIoControlFile(FileHandle
,
984 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
988 sizeof(DISK_GEOMETRY
));
989 if (!NT_SUCCESS(Status
))
994 if (DiskGeometry
.MediaType
!= FixedMedia
&&
995 DiskGeometry
.MediaType
!= RemovableMedia
)
1000 Status
= NtDeviceIoControlFile(FileHandle
,
1005 IOCTL_SCSI_GET_ADDRESS
,
1009 sizeof(SCSI_ADDRESS
));
1010 if (!NT_SUCCESS(Status
))
1015 Mbr
= (PARTITION_SECTOR
*)RtlAllocateHeap(ProcessHeap
,
1017 DiskGeometry
.BytesPerSector
);
1023 FileOffset
.QuadPart
= 0;
1024 Status
= NtReadFile(FileHandle
,
1030 DiskGeometry
.BytesPerSector
,
1033 if (!NT_SUCCESS(Status
))
1035 RtlFreeHeap(ProcessHeap
,
1038 DPRINT1("NtReadFile failed, status=%x\n", Status
);
1041 Signature
= Mbr
->Signature
;
1043 /* Calculate the MBR checksum */
1045 Buffer
= (PULONG
)Mbr
;
1046 for (i
= 0; i
< 128; i
++)
1048 Checksum
+= Buffer
[i
];
1050 Checksum
= ~Checksum
+ 1;
1052 swprintf(Identifier
, L
"%08x-%08x-A", Checksum
, Signature
);
1053 DPRINT("Identifier: %S\n", Identifier
);
1055 DiskEntry
= RtlAllocateHeap(ProcessHeap
,
1058 if (DiskEntry
== NULL
)
1063 // DiskEntry->Checksum = Checksum;
1064 // DiskEntry->Signature = Signature;
1065 DiskEntry
->BiosFound
= FALSE
;
1067 /* Check if this disk has a valid MBR */
1068 if (Mbr
->BootCode
[0] == 0 && Mbr
->BootCode
[1] == 0)
1069 DiskEntry
->NoMbr
= TRUE
;
1071 DiskEntry
->NoMbr
= FALSE
;
1073 /* Free Mbr sector buffer */
1074 RtlFreeHeap(ProcessHeap
,
1078 ListEntry
= List
->BiosDiskListHead
.Flink
;
1079 while(ListEntry
!= &List
->BiosDiskListHead
)
1081 BiosDiskEntry
= CONTAINING_RECORD(ListEntry
, BIOSDISKENTRY
, ListEntry
);
1083 * Compare the size from bios and the reported size from driver.
1084 * If we have more than one disk with a zero or with the same signatur
1085 * we must create new signatures and reboot. After the reboot,
1086 * it is possible to identify the disks.
1088 if (BiosDiskEntry
->Signature
== Signature
&&
1089 BiosDiskEntry
->Checksum
== Checksum
&&
1090 !BiosDiskEntry
->Recognized
)
1092 if (!DiskEntry
->BiosFound
)
1094 DiskEntry
->BiosDiskNumber
= BiosDiskEntry
->DiskNumber
;
1095 DiskEntry
->BiosFound
= TRUE
;
1096 BiosDiskEntry
->Recognized
= TRUE
;
1102 ListEntry
= ListEntry
->Flink
;
1105 if (!DiskEntry
->BiosFound
)
1108 RtlFreeHeap(ProcessHeap
, 0, DiskEntry
);
1111 DPRINT1("WARNING: Setup could not find a matching BIOS disk entry. Disk %d is not be bootable by the BIOS!\n", DiskNumber
);
1115 InitializeListHead(&DiskEntry
->PrimaryPartListHead
);
1116 InitializeListHead(&DiskEntry
->LogicalPartListHead
);
1118 DiskEntry
->Cylinders
= DiskGeometry
.Cylinders
.QuadPart
;
1119 DiskEntry
->TracksPerCylinder
= DiskGeometry
.TracksPerCylinder
;
1120 DiskEntry
->SectorsPerTrack
= DiskGeometry
.SectorsPerTrack
;
1121 DiskEntry
->BytesPerSector
= DiskGeometry
.BytesPerSector
;
1123 DPRINT("Cylinders %I64u\n", DiskEntry
->Cylinders
);
1124 DPRINT("TracksPerCylinder %I64u\n", DiskEntry
->TracksPerCylinder
);
1125 DPRINT("SectorsPerTrack %I64u\n", DiskEntry
->SectorsPerTrack
);
1126 DPRINT("BytesPerSector %I64u\n", DiskEntry
->BytesPerSector
);
1128 DiskEntry
->SectorCount
.QuadPart
= DiskGeometry
.Cylinders
.QuadPart
*
1129 (ULONGLONG
)DiskGeometry
.TracksPerCylinder
*
1130 (ULONGLONG
)DiskGeometry
.SectorsPerTrack
;
1132 DiskEntry
->SectorAlignment
= DiskGeometry
.SectorsPerTrack
;
1134 DPRINT("SectorCount %I64u\n", DiskEntry
->SectorCount
);
1135 DPRINT("SectorAlignment %lu\n", DiskEntry
->SectorAlignment
);
1137 DiskEntry
->DiskNumber
= DiskNumber
;
1138 DiskEntry
->Port
= ScsiAddress
.PortNumber
;
1139 DiskEntry
->Bus
= ScsiAddress
.PathId
;
1140 DiskEntry
->Id
= ScsiAddress
.TargetId
;
1142 GetDriverName(DiskEntry
);
1144 InsertAscendingList(&List
->DiskListHead
, DiskEntry
, DISKENTRY
, ListEntry
, DiskNumber
);
1146 /* Allocate a layout buffer with 4 partition entries first */
1147 LayoutBufferSize
= sizeof(DRIVE_LAYOUT_INFORMATION
) +
1148 ((4 - ANYSIZE_ARRAY
) * sizeof(PARTITION_INFORMATION
));
1149 DiskEntry
->LayoutBuffer
= RtlAllocateHeap(ProcessHeap
,
1152 if (DiskEntry
->LayoutBuffer
== NULL
)
1154 DPRINT1("Failed to allocate the disk layout buffer!\n");
1160 DPRINT1("Buffer size: %lu\n", LayoutBufferSize
);
1161 Status
= NtDeviceIoControlFile(FileHandle
,
1166 IOCTL_DISK_GET_DRIVE_LAYOUT
,
1169 DiskEntry
->LayoutBuffer
,
1171 if (NT_SUCCESS(Status
))
1174 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
1176 DPRINT1("NtDeviceIoControlFile() failed (Status: 0x%08lx)\n", Status
);
1180 LayoutBufferSize
+= 4 * sizeof(PARTITION_INFORMATION
);
1181 NewLayoutBuffer
= RtlReAllocateHeap(ProcessHeap
,
1183 DiskEntry
->LayoutBuffer
,
1185 if (NewLayoutBuffer
== NULL
)
1187 DPRINT1("Failed to reallocate the disk layout buffer!\n");
1191 DiskEntry
->LayoutBuffer
= NewLayoutBuffer
;
1194 DPRINT1("PartitionCount: %lu\n", DiskEntry
->LayoutBuffer
->PartitionCount
);
1196 #ifdef DUMP_PARTITION_TABLE
1197 DumpPartitionTable(DiskEntry
);
1200 if (DiskEntry
->LayoutBuffer
->PartitionEntry
[0].StartingOffset
.QuadPart
!= 0 &&
1201 DiskEntry
->LayoutBuffer
->PartitionEntry
[0].PartitionLength
.QuadPart
!= 0 &&
1202 DiskEntry
->LayoutBuffer
->PartitionEntry
[0].PartitionType
!= 0)
1204 if ((DiskEntry
->LayoutBuffer
->PartitionEntry
[0].StartingOffset
.QuadPart
/ DiskEntry
->BytesPerSector
) % DiskEntry
->SectorsPerTrack
== 0)
1206 DPRINT("Use %lu Sector alignment!\n", DiskEntry
->SectorsPerTrack
);
1208 else if (DiskEntry
->LayoutBuffer
->PartitionEntry
[0].StartingOffset
.QuadPart
% (1024 * 1024) == 0)
1210 DPRINT1("Use megabyte (%lu Sectors) alignment!\n", (1024 * 1024) / DiskEntry
->BytesPerSector
);
1214 DPRINT1("No matching aligment found! Partiton 1 starts at %I64u\n", DiskEntry
->LayoutBuffer
->PartitionEntry
[0].StartingOffset
.QuadPart
);
1219 DPRINT1("No valid partiton table found! Use megabyte (%lu Sectors) alignment!\n", (1024 * 1024) / DiskEntry
->BytesPerSector
);
1223 if (DiskEntry
->LayoutBuffer
->PartitionCount
== 0)
1225 DiskEntry
->NewDisk
= TRUE
;
1226 DiskEntry
->LayoutBuffer
->PartitionCount
= 4;
1228 for (i
= 0; i
< 4; i
++)
1229 DiskEntry
->LayoutBuffer
->PartitionEntry
[i
].RewritePartition
= TRUE
;
1233 for (i
= 0; i
< 4; i
++)
1235 AddPartitionToDisk(DiskNumber
,
1241 for (i
= 4; i
< DiskEntry
->LayoutBuffer
->PartitionCount
; i
+= 4)
1243 AddPartitionToDisk(DiskNumber
,
1250 ScanForUnpartitionedDiskSpace(DiskEntry
);
1255 CreatePartitionList(
1262 OBJECT_ATTRIBUTES ObjectAttributes
;
1263 SYSTEM_DEVICE_INFORMATION Sdi
;
1264 IO_STATUS_BLOCK Iosb
;
1268 WCHAR Buffer
[MAX_PATH
];
1269 UNICODE_STRING Name
;
1272 List
= (PPARTLIST
)RtlAllocateHeap(ProcessHeap
,
1280 List
->Right
= Right
;
1281 List
->Bottom
= Bottom
;
1286 List
->TopDisk
= (ULONG
)-1;
1287 List
->TopPartition
= (ULONG
)-1;
1289 List
->CurrentDisk
= NULL
;
1290 List
->CurrentPartition
= NULL
;
1292 List
->BootDisk
= NULL
;
1293 List
->BootPartition
= NULL
;
1295 List
->TempDisk
= NULL
;
1296 List
->TempPartition
= NULL
;
1297 List
->FormatState
= Start
;
1299 InitializeListHead(&List
->DiskListHead
);
1300 InitializeListHead(&List
->BiosDiskListHead
);
1302 EnumerateBiosDiskEntries(List
);
1304 Status
= NtQuerySystemInformation(SystemDeviceInformation
,
1306 sizeof(SYSTEM_DEVICE_INFORMATION
),
1308 if (!NT_SUCCESS(Status
))
1310 RtlFreeHeap(ProcessHeap
, 0, List
);
1314 for (DiskNumber
= 0; DiskNumber
< Sdi
.NumberOfDisks
; DiskNumber
++)
1317 L
"\\Device\\Harddisk%d\\Partition0",
1319 RtlInitUnicodeString(&Name
,
1322 InitializeObjectAttributes(&ObjectAttributes
,
1328 Status
= NtOpenFile(&FileHandle
,
1329 FILE_READ_DATA
| FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
1333 FILE_SYNCHRONOUS_IO_NONALERT
);
1334 if (NT_SUCCESS(Status
))
1336 AddDiskToList(FileHandle
,
1340 NtClose(FileHandle
);
1344 UpdateDiskSignatures(List
);
1346 AssignDriveLetters(List
);
1349 List
->TopPartition
= 0;
1351 /* Search for first usable disk and partition */
1352 if (IsListEmpty(&List
->DiskListHead
))
1354 List
->CurrentDisk
= NULL
;
1355 List
->CurrentPartition
= NULL
;
1359 List
->CurrentDisk
= CONTAINING_RECORD(List
->DiskListHead
.Flink
,
1363 if (IsListEmpty(&List
->CurrentDisk
->PrimaryPartListHead
))
1365 List
->CurrentPartition
= 0;
1369 List
->CurrentPartition
= CONTAINING_RECORD(List
->CurrentDisk
->PrimaryPartListHead
.Flink
,
1380 DestroyPartitionList(
1383 PDISKENTRY DiskEntry
;
1384 PBIOSDISKENTRY BiosDiskEntry
;
1385 PPARTENTRY PartEntry
;
1388 /* Release disk and partition info */
1389 while (!IsListEmpty(&List
->DiskListHead
))
1391 Entry
= RemoveHeadList(&List
->DiskListHead
);
1392 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
1394 /* Release driver name */
1395 RtlFreeUnicodeString(&DiskEntry
->DriverName
);
1397 /* Release primary partition list */
1398 while (!IsListEmpty(&DiskEntry
->PrimaryPartListHead
))
1400 Entry
= RemoveHeadList(&DiskEntry
->PrimaryPartListHead
);
1401 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
1403 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
1406 /* Release logical partition list */
1407 while (!IsListEmpty(&DiskEntry
->LogicalPartListHead
))
1409 Entry
= RemoveHeadList(&DiskEntry
->LogicalPartListHead
);
1410 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
1412 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
1415 /* Release layout buffer */
1416 if (DiskEntry
->LayoutBuffer
!= NULL
)
1417 RtlFreeHeap(ProcessHeap
, 0, DiskEntry
->LayoutBuffer
);
1420 /* Release disk entry */
1421 RtlFreeHeap(ProcessHeap
, 0, DiskEntry
);
1424 /* release the bios disk info */
1425 while(!IsListEmpty(&List
->BiosDiskListHead
))
1427 Entry
= RemoveHeadList(&List
->BiosDiskListHead
);
1428 BiosDiskEntry
= CONTAINING_RECORD(Entry
, BIOSDISKENTRY
, ListEntry
);
1430 RtlFreeHeap(ProcessHeap
, 0, BiosDiskEntry
);
1433 /* Release list head */
1434 RtlFreeHeap(ProcessHeap
, 0, List
);
1448 Width
= List
->Right
- List
->Left
- 1;
1449 Height
= List
->Bottom
- List
->Top
- 2;
1451 coPos
.X
= List
->Left
+ 1;
1452 coPos
.Y
= List
->Top
+ 1 + List
->Line
;
1454 if (List
->Line
>= 0 && List
->Line
<= Height
)
1456 FillConsoleOutputAttribute(StdOutput
,
1457 FOREGROUND_WHITE
| BACKGROUND_BLUE
,
1462 FillConsoleOutputCharacterA(StdOutput
,
1477 PDISKENTRY DiskEntry
,
1478 PPARTENTRY PartEntry
)
1480 CHAR LineBuffer
[128];
1485 LARGE_INTEGER PartSize
;
1490 Width
= List
->Right
- List
->Left
- 1;
1491 Height
= List
->Bottom
- List
->Top
- 2;
1493 coPos
.X
= List
->Left
+ 1;
1494 coPos
.Y
= List
->Top
+ 1 + List
->Line
;
1496 if (PartEntry
->IsPartitioned
== FALSE
)
1498 PartSize
.QuadPart
= PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
1500 if (PartSize
.QuadPart
>= 10737418240) /* 10 GB */
1502 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1073741824);
1503 Unit
= MUIGetString(STRING_GB
);
1507 if (PartSize
.QuadPart
>= 10485760) /* 10 MB */
1509 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1048576);
1510 Unit
= MUIGetString(STRING_MB
);
1514 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1024);
1515 Unit
= MUIGetString(STRING_KB
);
1519 MUIGetString(STRING_UNPSPACE
),
1520 PartEntry
->LogicalPartition
? " " : "",
1521 PartEntry
->LogicalPartition
? "" : " ",
1527 /* Determine partition type */
1529 if (PartEntry
->New
== TRUE
)
1531 PartType
= MUIGetString(STRING_UNFORMATTED
);
1533 else if (PartEntry
->IsPartitioned
== TRUE
)
1535 if ((PartEntry
->PartitionType
== PARTITION_FAT_12
) ||
1536 (PartEntry
->PartitionType
== PARTITION_FAT_16
) ||
1537 (PartEntry
->PartitionType
== PARTITION_HUGE
) ||
1538 (PartEntry
->PartitionType
== PARTITION_XINT13
))
1542 else if ((PartEntry
->PartitionType
== PARTITION_FAT32
) ||
1543 (PartEntry
->PartitionType
== PARTITION_FAT32_XINT13
))
1547 else if (PartEntry
->PartitionType
== PARTITION_EXT2
)
1551 else if (PartEntry
->PartitionType
== PARTITION_IFS
)
1553 PartType
= "NTFS"; /* FIXME: Not quite correct! */
1555 else if ((PartEntry
->PartitionType
== PARTITION_EXTENDED
) ||
1556 (PartEntry
->PartitionType
== PARTITION_XINT13_EXTENDED
))
1558 PartType
= MUIGetString(STRING_EXTENDED_PARTITION
);
1562 PartSize
.QuadPart
= PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
1564 if (PartSize
.QuadPart
>= 10737418240) /* 10 GB */
1566 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1073741824);
1567 Unit
= MUIGetString(STRING_GB
);
1571 if (PartSize
.QuadPart
>= 10485760) /* 10 MB */
1573 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1048576);
1574 Unit
= MUIGetString(STRING_MB
);
1578 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1024);
1579 Unit
= MUIGetString(STRING_KB
);
1582 if (PartType
== NULL
)
1585 MUIGetString(STRING_HDDINFOUNK5
),
1586 (PartEntry
->DriveLetter
== 0) ? '-' : PartEntry
->DriveLetter
,
1587 (PartEntry
->DriveLetter
== 0) ? '-' : ':',
1588 PartEntry
->BootIndicator
? '*' : ' ',
1589 PartEntry
->LogicalPartition
? " " : "",
1590 PartEntry
->PartitionType
,
1591 PartEntry
->LogicalPartition
? "" : " ",
1598 "%c%c %c %s%-24s%s %6lu %s",
1599 (PartEntry
->DriveLetter
== 0) ? '-' : PartEntry
->DriveLetter
,
1600 (PartEntry
->DriveLetter
== 0) ? '-' : ':',
1601 PartEntry
->BootIndicator
? '*' : ' ',
1602 PartEntry
->LogicalPartition
? " " : "",
1604 PartEntry
->LogicalPartition
? "" : " ",
1610 Attribute
= (List
->CurrentDisk
== DiskEntry
&&
1611 List
->CurrentPartition
== PartEntry
) ?
1612 FOREGROUND_BLUE
| BACKGROUND_WHITE
:
1613 FOREGROUND_WHITE
| BACKGROUND_BLUE
;
1615 if (List
->Line
>= 0 && List
->Line
<= Height
)
1617 FillConsoleOutputCharacterA(StdOutput
,
1625 if (List
->Line
>= 0 && List
->Line
<= Height
)
1627 FillConsoleOutputAttribute(StdOutput
,
1635 if (List
->Line
>= 0 && List
->Line
<= Height
)
1637 WriteConsoleOutputCharacterA(StdOutput
,
1639 min(strlen(LineBuffer
), Width
),
1652 PDISKENTRY DiskEntry
)
1654 PPARTENTRY PrimaryPartEntry
, LogicalPartEntry
;
1655 PLIST_ENTRY PrimaryEntry
, LogicalEntry
;
1656 CHAR LineBuffer
[128];
1661 ULARGE_INTEGER DiskSize
;
1664 Width
= List
->Right
- List
->Left
- 1;
1665 Height
= List
->Bottom
- List
->Top
- 2;
1667 coPos
.X
= List
->Left
+ 1;
1668 coPos
.Y
= List
->Top
+ 1 + List
->Line
;
1670 DiskSize
.QuadPart
= DiskEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
1671 if (DiskSize
.QuadPart
>= 10737418240) /* 10 GB */
1673 DiskSize
.QuadPart
= RoundingDivide(DiskSize
.QuadPart
, 1073741824);
1674 Unit
= MUIGetString(STRING_GB
);
1678 DiskSize
.QuadPart
= RoundingDivide(DiskSize
.QuadPart
, 1048576);
1679 if (DiskSize
.QuadPart
== 0)
1680 DiskSize
.QuadPart
= 1;
1681 Unit
= MUIGetString(STRING_MB
);
1684 if (DiskEntry
->DriverName
.Length
> 0)
1687 MUIGetString(STRING_HDINFOPARTSELECT
),
1690 DiskEntry
->DiskNumber
,
1694 DiskEntry
->DriverName
.Buffer
);
1699 MUIGetString(STRING_HDDINFOUNK6
),
1702 DiskEntry
->DiskNumber
,
1708 if (List
->Line
>= 0 && List
->Line
<= Height
)
1710 FillConsoleOutputAttribute(StdOutput
,
1711 FOREGROUND_WHITE
| BACKGROUND_BLUE
,
1716 FillConsoleOutputCharacterA(StdOutput
,
1724 if (List
->Line
>= 0 && List
->Line
<= Height
)
1726 WriteConsoleOutputCharacterA(StdOutput
,
1728 min((USHORT
)strlen(LineBuffer
), Width
- 2),
1735 /* Print separator line */
1736 PrintEmptyLine(List
);
1738 /* Print partition lines*/
1739 PrimaryEntry
= DiskEntry
->PrimaryPartListHead
.Flink
;
1740 while (PrimaryEntry
!= &DiskEntry
->PrimaryPartListHead
)
1742 PrimaryPartEntry
= CONTAINING_RECORD(PrimaryEntry
, PARTENTRY
, ListEntry
);
1744 PrintPartitionData(List
,
1748 if (IsContainerPartition(PrimaryPartEntry
->PartitionType
))
1750 LogicalEntry
= DiskEntry
->LogicalPartListHead
.Flink
;
1751 while (LogicalEntry
!= &DiskEntry
->LogicalPartListHead
)
1753 LogicalPartEntry
= CONTAINING_RECORD(LogicalEntry
, PARTENTRY
, ListEntry
);
1755 PrintPartitionData(List
,
1759 LogicalEntry
= LogicalEntry
->Flink
;
1763 PrimaryEntry
= PrimaryEntry
->Flink
;
1766 /* Print separator line */
1767 PrintEmptyLine(List
);
1775 PLIST_ENTRY Entry
, Entry2
;
1776 PDISKENTRY DiskEntry
;
1777 PPARTENTRY PartEntry
= NULL
;
1781 SHORT CurrentDiskLine
;
1782 SHORT CurrentPartLine
;
1784 BOOL CurrentPartLineFound
= FALSE
;
1785 BOOL CurrentDiskLineFound
= FALSE
;
1787 /* Calculate the line of the current disk and partition */
1788 CurrentDiskLine
= 0;
1789 CurrentPartLine
= 0;
1792 Entry
= List
->DiskListHead
.Flink
;
1793 while (Entry
!= &List
->DiskListHead
)
1795 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
1798 if (CurrentPartLineFound
== FALSE
)
1800 CurrentPartLine
+= 2;
1803 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
1804 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
1806 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
1807 if (PartEntry
== List
->CurrentPartition
)
1809 CurrentPartLineFound
= TRUE
;
1812 Entry2
= Entry2
->Flink
;
1813 if (CurrentPartLineFound
== FALSE
)
1821 if (DiskEntry
== List
->CurrentDisk
)
1823 CurrentDiskLineFound
= TRUE
;
1826 Entry
= Entry
->Flink
;
1827 if (Entry
!= &List
->DiskListHead
)
1829 if (CurrentDiskLineFound
== FALSE
)
1832 CurrentDiskLine
= CurrentPartLine
;
1843 /* If it possible, make the disk name visible */
1844 if (CurrentPartLine
< List
->Offset
)
1846 List
->Offset
= CurrentPartLine
;
1848 else if (CurrentPartLine
- List
->Offset
> List
->Bottom
- List
->Top
- 2)
1850 List
->Offset
= CurrentPartLine
- (List
->Bottom
- List
->Top
- 2);
1853 if (CurrentDiskLine
< List
->Offset
&& CurrentPartLine
- CurrentDiskLine
< List
->Bottom
- List
->Top
- 2)
1855 List
->Offset
= CurrentDiskLine
;
1858 /* draw upper left corner */
1859 coPos
.X
= List
->Left
;
1860 coPos
.Y
= List
->Top
;
1861 FillConsoleOutputCharacterA(StdOutput
,
1867 /* draw upper edge */
1868 coPos
.X
= List
->Left
+ 1;
1869 coPos
.Y
= List
->Top
;
1870 if (List
->Offset
== 0)
1872 FillConsoleOutputCharacterA(StdOutput
,
1874 List
->Right
- List
->Left
- 1,
1880 FillConsoleOutputCharacterA(StdOutput
,
1882 List
->Right
- List
->Left
- 5,
1885 coPos
.X
= List
->Right
- 5;
1886 WriteConsoleOutputCharacterA(StdOutput
,
1891 coPos
.X
= List
->Right
- 2;
1892 FillConsoleOutputCharacterA(StdOutput
,
1899 /* draw upper right corner */
1900 coPos
.X
= List
->Right
;
1901 coPos
.Y
= List
->Top
;
1902 FillConsoleOutputCharacterA(StdOutput
,
1908 /* draw left and right edge */
1909 for (i
= List
->Top
+ 1; i
< List
->Bottom
; i
++)
1911 coPos
.X
= List
->Left
;
1913 FillConsoleOutputCharacterA(StdOutput
,
1919 coPos
.X
= List
->Right
;
1920 FillConsoleOutputCharacterA(StdOutput
,
1927 /* draw lower left corner */
1928 coPos
.X
= List
->Left
;
1929 coPos
.Y
= List
->Bottom
;
1930 FillConsoleOutputCharacterA(StdOutput
,
1936 /* draw lower edge */
1937 coPos
.X
= List
->Left
+ 1;
1938 coPos
.Y
= List
->Bottom
;
1939 if (LastLine
- List
->Offset
<= List
->Bottom
- List
->Top
- 2)
1941 FillConsoleOutputCharacterA(StdOutput
,
1943 List
->Right
- List
->Left
- 1,
1949 FillConsoleOutputCharacterA(StdOutput
,
1951 List
->Right
- List
->Left
- 5,
1954 coPos
.X
= List
->Right
- 5;
1955 WriteConsoleOutputCharacterA(StdOutput
,
1956 "(\x19)", // "(down)"
1960 coPos
.X
= List
->Right
- 2;
1961 FillConsoleOutputCharacterA(StdOutput
,
1968 /* draw lower right corner */
1969 coPos
.X
= List
->Right
;
1970 coPos
.Y
= List
->Bottom
;
1971 FillConsoleOutputCharacterA(StdOutput
,
1977 /* print list entries */
1978 List
->Line
= - List
->Offset
;
1980 Entry
= List
->DiskListHead
.Flink
;
1981 while (Entry
!= &List
->DiskListHead
)
1983 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
1985 /* Print disk entry */
1989 Entry
= Entry
->Flink
;
1998 ULONG PartitionNumber
)
2000 PDISKENTRY DiskEntry
;
2001 PPARTENTRY PartEntry
;
2005 /* Check for empty disks */
2006 if (IsListEmpty(&List
->DiskListHead
))
2009 /* Check for first usable entry on next disk */
2010 Entry1
= List
->CurrentDisk
->ListEntry
.Flink
;
2011 while (Entry1
!= &List
->DiskListHead
)
2013 DiskEntry
= CONTAINING_RECORD(Entry1
, DISKENTRY
, ListEntry
);
2015 if (DiskEntry
->DiskNumber
== DiskNumber
)
2017 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
2018 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
2020 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
2022 if (PartEntry
->PartitionNumber
== PartitionNumber
)
2024 List
->CurrentDisk
= DiskEntry
;
2025 List
->CurrentPartition
= PartEntry
;
2026 DrawPartitionList(List
);
2030 Entry2
= Entry2
->Flink
;
2036 Entry1
= Entry1
->Flink
;
2044 ScrollDownPartitionList(
2047 PLIST_ENTRY DiskListEntry
;
2048 PLIST_ENTRY PartListEntry
;
2049 PDISKENTRY DiskEntry
;
2050 PPARTENTRY PartEntry
;
2052 /* Fail, if no disks are available */
2053 if (IsListEmpty(&List
->DiskListHead
))
2056 /* Check for next usable entry on current disk */
2057 if (List
->CurrentPartition
!= NULL
)
2059 if (List
->CurrentPartition
->LogicalPartition
)
2061 /* Logical partition */
2063 PartListEntry
= List
->CurrentPartition
->ListEntry
.Flink
;
2064 if (PartListEntry
!= &List
->CurrentDisk
->LogicalPartListHead
)
2066 /* Next logical partition */
2067 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2069 List
->CurrentPartition
= PartEntry
;
2074 PartListEntry
= List
->CurrentDisk
->ExtendedPartition
->ListEntry
.Flink
;
2075 if (PartListEntry
!= &List
->CurrentDisk
->PrimaryPartListHead
)
2077 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2079 List
->CurrentPartition
= PartEntry
;
2086 /* Primary or extended partition */
2088 if (List
->CurrentPartition
->IsPartitioned
== TRUE
&&
2089 IsContainerPartition(List
->CurrentPartition
->PartitionType
))
2091 /* First logical partition */
2092 PartListEntry
= List
->CurrentDisk
->LogicalPartListHead
.Flink
;
2093 if (PartListEntry
!= &List
->CurrentDisk
->LogicalPartListHead
)
2095 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2097 List
->CurrentPartition
= PartEntry
;
2103 /* Next primary partition */
2104 PartListEntry
= List
->CurrentPartition
->ListEntry
.Flink
;
2105 if (PartListEntry
!= &List
->CurrentDisk
->PrimaryPartListHead
)
2107 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2109 List
->CurrentPartition
= PartEntry
;
2116 /* Search for the first partition entry on the next disk */
2117 DiskListEntry
= List
->CurrentDisk
->ListEntry
.Flink
;
2118 while (DiskListEntry
!= &List
->DiskListHead
)
2120 DiskEntry
= CONTAINING_RECORD(DiskListEntry
, DISKENTRY
, ListEntry
);
2122 PartListEntry
= DiskEntry
->PrimaryPartListHead
.Flink
;
2123 if (PartListEntry
!= &DiskEntry
->PrimaryPartListHead
)
2125 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2127 List
->CurrentDisk
= DiskEntry
;
2128 List
->CurrentPartition
= PartEntry
;
2132 DiskListEntry
= DiskListEntry
->Flink
;
2140 ScrollUpPartitionList(
2143 PLIST_ENTRY DiskListEntry
;
2144 PLIST_ENTRY PartListEntry
;
2145 PDISKENTRY DiskEntry
;
2146 PPARTENTRY PartEntry
;
2148 /* Fail, if no disks are available */
2149 if (IsListEmpty(&List
->DiskListHead
))
2152 /* Check for previous usable entry on current disk */
2153 if (List
->CurrentPartition
!= NULL
)
2155 if (List
->CurrentPartition
->LogicalPartition
)
2157 /* Logical partition */
2158 PartListEntry
= List
->CurrentPartition
->ListEntry
.Blink
;
2159 if (PartListEntry
!= &List
->CurrentDisk
->LogicalPartListHead
)
2161 /* Previous logical partition */
2162 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2166 /* Extended partition*/
2167 PartEntry
= List
->CurrentDisk
->ExtendedPartition
;
2170 List
->CurrentPartition
= PartEntry
;
2175 /* Primary or extended partition */
2177 PartListEntry
= List
->CurrentPartition
->ListEntry
.Blink
;
2178 if (PartListEntry
!= &List
->CurrentDisk
->PrimaryPartListHead
)
2180 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2182 if (PartEntry
->IsPartitioned
== TRUE
&&
2183 IsContainerPartition(PartEntry
->PartitionType
))
2185 PartListEntry
= List
->CurrentDisk
->LogicalPartListHead
.Blink
;
2186 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2189 List
->CurrentPartition
= PartEntry
;
2196 /* Search for the last partition entry on the previous disk */
2197 DiskListEntry
= List
->CurrentDisk
->ListEntry
.Blink
;
2198 while (DiskListEntry
!= &List
->DiskListHead
)
2200 DiskEntry
= CONTAINING_RECORD(DiskListEntry
, DISKENTRY
, ListEntry
);
2202 PartListEntry
= DiskEntry
->PrimaryPartListHead
.Blink
;
2203 if (PartListEntry
!= &DiskEntry
->PrimaryPartListHead
)
2205 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2207 if (PartEntry
->IsPartitioned
== TRUE
&&
2208 IsContainerPartition(PartEntry
->PartitionType
))
2210 PartListEntry
= DiskEntry
->LogicalPartListHead
.Blink
;
2211 if (PartListEntry
!= &DiskEntry
->LogicalPartListHead
)
2213 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2215 List
->CurrentDisk
= DiskEntry
;
2216 List
->CurrentPartition
= PartEntry
;
2222 List
->CurrentDisk
= DiskEntry
;
2223 List
->CurrentPartition
= PartEntry
;
2228 DiskListEntry
= DiskListEntry
->Blink
;
2238 PPARTITION_INFORMATION PartitionInfo
)
2240 if (PartitionInfo
->StartingOffset
.QuadPart
== 0 &&
2241 PartitionInfo
->PartitionLength
.QuadPart
== 0)
2242 // PartitionInfo->PartitionType == 0)
2251 IsSamePrimaryLayoutEntry(
2252 IN PPARTITION_INFORMATION PartitionInfo
,
2253 IN PDISKENTRY DiskEntry
,
2254 IN PPARTENTRY PartEntry
)
2256 if (PartitionInfo
->StartingOffset
.QuadPart
== PartEntry
->StartSector
.QuadPart
* DiskEntry
->BytesPerSector
&&
2257 PartitionInfo
->PartitionLength
.QuadPart
== PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
)
2258 // PartitionInfo->PartitionNumber = PartEntry->PartitionNumber &&
2259 // PartitionInfo->PartitionType == PartEntry->PartitionType
2269 IN PDISKENTRY DiskEntry
)
2271 PPARTITION_INFORMATION PartitionInfo
;
2272 PLIST_ENTRY ListEntry
;
2273 PPARTENTRY PartEntry
;
2275 ULONG PartitionNumber
= 1;
2277 DPRINT1("UpdateDiskLayout()\n");
2279 ListEntry
= DiskEntry
->PrimaryPartListHead
.Flink
;
2280 while (ListEntry
!= &DiskEntry
->PrimaryPartListHead
)
2282 PartEntry
= CONTAINING_RECORD(ListEntry
, PARTENTRY
, ListEntry
);
2284 if (PartEntry
->IsPartitioned
== TRUE
)
2286 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[Index
];
2288 if (!IsSamePrimaryLayoutEntry(PartitionInfo
, DiskEntry
, PartEntry
))
2290 DPRINT1("Updating partition entry %lu\n", Index
);
2292 PartitionInfo
->StartingOffset
.QuadPart
= PartEntry
->StartSector
.QuadPart
* DiskEntry
->BytesPerSector
;
2293 PartitionInfo
->PartitionLength
.QuadPart
= PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
2294 PartitionInfo
->HiddenSectors
= 0;
2295 PartitionInfo
->PartitionNumber
= (!IsContainerPartition(PartEntry
->PartitionType
)) ? PartitionNumber
: 0;
2296 PartitionInfo
->PartitionType
= PartEntry
->PartitionType
;
2297 PartitionInfo
->BootIndicator
= PartEntry
->BootIndicator
;
2298 PartitionInfo
->RecognizedPartition
= FALSE
;
2299 PartitionInfo
->RewritePartition
= TRUE
;
2301 PartEntry
->PartitionNumber
= PartitionNumber
;
2302 PartEntry
->PartitionIndex
= Index
;
2306 else if (!IsEmptyLayoutEntry(PartitionInfo
))
2314 ListEntry
= ListEntry
->Flink
;
2317 for (;Index
< 4; Index
++)
2319 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[Index
];
2321 if (!IsEmptyLayoutEntry(PartitionInfo
))
2323 DPRINT1("Wiping partition entry %lu\n", Index
);
2325 PartitionInfo
->StartingOffset
.QuadPart
= 0;
2326 PartitionInfo
->PartitionLength
.QuadPart
= 0;
2327 PartitionInfo
->HiddenSectors
= 0;
2328 PartitionInfo
->PartitionNumber
= 0;
2329 PartitionInfo
->PartitionType
= 0;
2330 PartitionInfo
->BootIndicator
= FALSE
;
2331 PartitionInfo
->RecognizedPartition
= FALSE
;
2332 PartitionInfo
->RewritePartition
= TRUE
;
2336 #ifdef DUMP_PARTITION_TABLE
2337 DumpPartitionTable(DiskEntry
);
2344 GetPrevUnpartitionedEntry(
2345 PDISKENTRY DiskEntry
,
2346 PPARTENTRY PartEntry
)
2348 PPARTENTRY PrevPartEntry
;
2349 PLIST_ENTRY ListHead
;
2351 if (PartEntry
->LogicalPartition
)
2352 ListHead
= &DiskEntry
->LogicalPartListHead
;
2354 ListHead
= &DiskEntry
->PrimaryPartListHead
;
2356 if (PartEntry
->ListEntry
.Blink
!= ListHead
)
2358 PrevPartEntry
= CONTAINING_RECORD(PartEntry
->ListEntry
.Blink
,
2361 if (PrevPartEntry
->IsPartitioned
== FALSE
)
2362 return PrevPartEntry
;
2371 GetNextUnpartitionedEntry(
2372 PDISKENTRY DiskEntry
,
2373 PPARTENTRY PartEntry
)
2375 PPARTENTRY NextPartEntry
;
2376 PLIST_ENTRY ListHead
;
2378 if (PartEntry
->LogicalPartition
)
2379 ListHead
= &DiskEntry
->LogicalPartListHead
;
2381 ListHead
= &DiskEntry
->PrimaryPartListHead
;
2383 if (PartEntry
->ListEntry
.Flink
!= ListHead
)
2385 NextPartEntry
= CONTAINING_RECORD(PartEntry
->ListEntry
.Flink
,
2388 if (NextPartEntry
->IsPartitioned
== FALSE
)
2389 return NextPartEntry
;
2397 CreatePrimaryPartition(
2399 ULONGLONG SectorCount
,
2402 PDISKENTRY DiskEntry
;
2403 PPARTENTRY PartEntry
;
2404 PPARTENTRY NewPartEntry
;
2406 DPRINT1("CreatePrimaryPartition(%I64u)\n", SectorCount
);
2409 List
->CurrentDisk
== NULL
||
2410 List
->CurrentPartition
== NULL
||
2411 List
->CurrentPartition
->IsPartitioned
== TRUE
)
2416 DiskEntry
= List
->CurrentDisk
;
2417 PartEntry
= List
->CurrentPartition
;
2419 DPRINT1("Current partition sector count: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2421 if (AutoCreate
== TRUE
||
2422 Align(PartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) - PartEntry
->StartSector
.QuadPart
== PartEntry
->SectorCount
.QuadPart
)
2424 DPRINT1("Convert existing partition entry\n");
2426 /* Convert current entry to 'new (unformatted)' */
2427 PartEntry
->IsPartitioned
= TRUE
;
2428 PartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2429 PartEntry
->FormatState
= Unformatted
;
2430 PartEntry
->AutoCreate
= AutoCreate
;
2431 PartEntry
->New
= TRUE
;
2432 PartEntry
->BootIndicator
= FALSE
;
2434 DPRINT1("First Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
);
2435 DPRINT1("Last Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
+ PartEntry
->SectorCount
.QuadPart
- 1);
2436 DPRINT1("Total Sectors: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2440 DPRINT1("Add new partition entry\n");
2442 /* Insert and initialize a new partition entry */
2443 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
2446 if (NewPartEntry
== NULL
)
2449 /* Insert the new entry into the list */
2450 InsertTailList(&PartEntry
->ListEntry
,
2451 &NewPartEntry
->ListEntry
);
2453 NewPartEntry
->DiskEntry
= DiskEntry
;
2455 NewPartEntry
->IsPartitioned
= TRUE
;
2456 NewPartEntry
->StartSector
.QuadPart
= PartEntry
->StartSector
.QuadPart
;
2457 NewPartEntry
->SectorCount
.QuadPart
= Align(NewPartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) -
2458 NewPartEntry
->StartSector
.QuadPart
;
2459 NewPartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2461 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
2462 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
2463 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
2465 NewPartEntry
->New
= TRUE
;
2466 NewPartEntry
->FormatState
= Unformatted
;
2467 NewPartEntry
->BootIndicator
= FALSE
;
2469 PartEntry
->StartSector
.QuadPart
= NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
;
2470 PartEntry
->SectorCount
.QuadPart
-= (PartEntry
->StartSector
.QuadPart
- NewPartEntry
->StartSector
.QuadPart
);
2473 UpdateDiskLayout(DiskEntry
);
2475 DiskEntry
->Dirty
= TRUE
;
2477 UpdatePartitionNumbers(DiskEntry
);
2479 AssignDriveLetters(List
);
2485 AddLogicalDiskSpace(
2486 PDISKENTRY DiskEntry
)
2488 PPARTENTRY NewPartEntry
;
2490 DPRINT1("AddLogicalDiskSpace()\n");
2492 /* Create a partition table entry that represents the empty space in the container partition */
2493 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
2496 if (NewPartEntry
== NULL
)
2499 NewPartEntry
->DiskEntry
= DiskEntry
;
2500 NewPartEntry
->LogicalPartition
= TRUE
;
2502 NewPartEntry
->IsPartitioned
= FALSE
;
2503 NewPartEntry
->StartSector
.QuadPart
= DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ (ULONGLONG
)DiskEntry
->SectorAlignment
;
2504 NewPartEntry
->SectorCount
.QuadPart
= DiskEntry
->ExtendedPartition
->SectorCount
.QuadPart
- (ULONGLONG
)DiskEntry
->SectorAlignment
;
2506 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
2507 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
2508 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
2510 NewPartEntry
->FormatState
= Unformatted
;
2512 InsertTailList(&DiskEntry
->LogicalPartListHead
,
2513 &NewPartEntry
->ListEntry
);
2518 CreateExtendedPartition(
2520 ULONGLONG SectorCount
)
2522 PDISKENTRY DiskEntry
;
2523 PPARTENTRY PartEntry
;
2524 PPARTENTRY NewPartEntry
;
2526 DPRINT1("CreateExtendedPartition(%I64u)\n", SectorCount
);
2529 List
->CurrentDisk
== NULL
||
2530 List
->CurrentPartition
== NULL
||
2531 List
->CurrentPartition
->IsPartitioned
== TRUE
)
2536 DiskEntry
= List
->CurrentDisk
;
2537 PartEntry
= List
->CurrentPartition
;
2539 DPRINT1("Current partition sector count: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2541 if (Align(PartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) - PartEntry
->StartSector
.QuadPart
== PartEntry
->SectorCount
.QuadPart
)
2543 DPRINT1("Convert existing partition entry\n");
2545 /* Convert current entry to 'new (unformatted)' */
2546 PartEntry
->IsPartitioned
= TRUE
;
2547 PartEntry
->FormatState
= Formatted
;
2548 PartEntry
->AutoCreate
= FALSE
;
2549 PartEntry
->New
= FALSE
;
2550 PartEntry
->BootIndicator
= FALSE
;
2552 if (PartEntry
->StartSector
.QuadPart
< 1450560)
2554 /* Partition starts below the 8.4GB boundary ==> CHS partition */
2555 PartEntry
->PartitionType
= PARTITION_EXTENDED
;
2559 /* Partition starts above the 8.4GB boundary ==> LBA partition */
2560 PartEntry
->PartitionType
= PARTITION_XINT13_EXTENDED
;
2563 DiskEntry
->ExtendedPartition
= PartEntry
;
2565 DPRINT1("First Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
);
2566 DPRINT1("Last Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
+ PartEntry
->SectorCount
.QuadPart
- 1);
2567 DPRINT1("Total Sectors: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2571 DPRINT1("Add new partition entry\n");
2573 /* Insert and initialize a new partition entry */
2574 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
2577 if (NewPartEntry
== NULL
)
2580 /* Insert the new entry into the list */
2581 InsertTailList(&PartEntry
->ListEntry
,
2582 &NewPartEntry
->ListEntry
);
2584 NewPartEntry
->DiskEntry
= DiskEntry
;
2586 NewPartEntry
->IsPartitioned
= TRUE
;
2587 NewPartEntry
->StartSector
.QuadPart
= PartEntry
->StartSector
.QuadPart
;
2588 NewPartEntry
->SectorCount
.QuadPart
= Align(NewPartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) -
2589 NewPartEntry
->StartSector
.QuadPart
;
2591 NewPartEntry
->New
= FALSE
;
2592 NewPartEntry
->FormatState
= Formatted
;
2593 NewPartEntry
->BootIndicator
= FALSE
;
2595 if (NewPartEntry
->StartSector
.QuadPart
< 1450560)
2597 /* Partition starts below the 8.4GB boundary ==> CHS partition */
2598 NewPartEntry
->PartitionType
= PARTITION_EXTENDED
;
2602 /* Partition starts above the 8.4GB boundary ==> LBA partition */
2603 NewPartEntry
->PartitionType
= PARTITION_XINT13_EXTENDED
;
2606 DiskEntry
->ExtendedPartition
= NewPartEntry
;
2608 PartEntry
->StartSector
.QuadPart
= NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
;
2609 PartEntry
->SectorCount
.QuadPart
-= (PartEntry
->StartSector
.QuadPart
- NewPartEntry
->StartSector
.QuadPart
);
2611 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
2612 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
2613 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
2616 AddLogicalDiskSpace(DiskEntry
);
2618 UpdateDiskLayout(DiskEntry
);
2620 DiskEntry
->Dirty
= TRUE
;
2622 UpdatePartitionNumbers(DiskEntry
);
2624 AssignDriveLetters(List
);
2629 CreateLogicalPartition(
2631 ULONGLONG SectorCount
)
2633 PDISKENTRY DiskEntry
;
2634 PPARTENTRY PartEntry
;
2635 PPARTENTRY NewPartEntry
;
2637 DPRINT1("CreateLogicalPartition(%I64u)\n", SectorCount
);
2640 List
->CurrentDisk
== NULL
||
2641 List
->CurrentPartition
== NULL
||
2642 List
->CurrentPartition
->IsPartitioned
== TRUE
)
2647 DiskEntry
= List
->CurrentDisk
;
2648 PartEntry
= List
->CurrentPartition
;
2650 DPRINT1("Current partition sector count: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2652 if (Align(PartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) - PartEntry
->StartSector
.QuadPart
== PartEntry
->SectorCount
.QuadPart
)
2654 DPRINT1("Convert existing partition entry\n");
2656 /* Convert current entry to 'new (unformatted)' */
2657 PartEntry
->IsPartitioned
= TRUE
;
2658 PartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2659 PartEntry
->FormatState
= Unformatted
;
2660 PartEntry
->AutoCreate
= FALSE
;
2661 PartEntry
->New
= TRUE
;
2662 PartEntry
->BootIndicator
= FALSE
;
2663 PartEntry
->LogicalPartition
= TRUE
;
2665 DPRINT1("First Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
);
2666 DPRINT1("Last Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
+ PartEntry
->SectorCount
.QuadPart
- 1);
2667 DPRINT1("Total Sectors: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2671 DPRINT1("Add new partition entry\n");
2673 /* Insert and initialize a new partition entry */
2674 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
2677 if (NewPartEntry
== NULL
)
2680 /* Insert the new entry into the list */
2681 InsertTailList(&PartEntry
->ListEntry
,
2682 &NewPartEntry
->ListEntry
);
2684 NewPartEntry
->DiskEntry
= DiskEntry
;
2686 NewPartEntry
->IsPartitioned
= TRUE
;
2687 NewPartEntry
->StartSector
.QuadPart
= PartEntry
->StartSector
.QuadPart
;
2688 NewPartEntry
->SectorCount
.QuadPart
= Align(NewPartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) -
2689 NewPartEntry
->StartSector
.QuadPart
;
2690 NewPartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2692 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
2693 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
2694 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
2696 NewPartEntry
->New
= TRUE
;
2697 NewPartEntry
->FormatState
= Unformatted
;
2698 NewPartEntry
->BootIndicator
= FALSE
;
2699 NewPartEntry
->LogicalPartition
= TRUE
;
2701 PartEntry
->StartSector
.QuadPart
= NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
;
2702 PartEntry
->SectorCount
.QuadPart
-= (PartEntry
->StartSector
.QuadPart
- NewPartEntry
->StartSector
.QuadPart
);
2705 UpdateDiskLayout(DiskEntry
);
2707 DiskEntry
->Dirty
= TRUE
;
2709 UpdatePartitionNumbers(DiskEntry
);
2711 AssignDriveLetters(List
);
2716 DeleteCurrentPartition(
2719 PDISKENTRY DiskEntry
;
2720 PPARTENTRY PartEntry
;
2721 PPARTENTRY PrevPartEntry
;
2722 PPARTENTRY NextPartEntry
;
2723 PPARTENTRY LogicalPartEntry
;
2727 List
->CurrentDisk
== NULL
||
2728 List
->CurrentPartition
== NULL
||
2729 List
->CurrentPartition
->IsPartitioned
== FALSE
)
2734 DiskEntry
= List
->CurrentDisk
;
2735 PartEntry
= List
->CurrentPartition
;
2737 /* Delete all logical partiton entries if an extended partition will be deleted */
2738 if (DiskEntry
->ExtendedPartition
== PartEntry
)
2740 while (!IsListEmpty(&DiskEntry
->LogicalPartListHead
))
2742 Entry
= RemoveHeadList(&DiskEntry
->LogicalPartListHead
);
2743 LogicalPartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
2745 RtlFreeHeap(ProcessHeap
, 0, LogicalPartEntry
);
2748 DiskEntry
->ExtendedPartition
= NULL
;
2751 /* Adjust unpartitioned disk space entries */
2753 /* Get pointer to previous and next unpartitioned entries */
2754 PrevPartEntry
= GetPrevUnpartitionedEntry(DiskEntry
,
2757 NextPartEntry
= GetNextUnpartitionedEntry(DiskEntry
,
2760 if (PrevPartEntry
!= NULL
&& NextPartEntry
!= NULL
)
2762 /* Merge previous, current and next unpartitioned entry */
2764 /* Adjust the previous entries length */
2765 PrevPartEntry
->SectorCount
.QuadPart
+= (PartEntry
->SectorCount
.QuadPart
+ NextPartEntry
->SectorCount
.QuadPart
);
2767 /* Remove the current entry */
2768 RemoveEntryList(&PartEntry
->ListEntry
);
2769 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
2771 /* Remove the next entry */
2772 RemoveEntryList (&NextPartEntry
->ListEntry
);
2773 RtlFreeHeap(ProcessHeap
, 0, NextPartEntry
);
2775 /* Update current partition */
2776 List
->CurrentPartition
= PrevPartEntry
;
2778 else if (PrevPartEntry
!= NULL
&& NextPartEntry
== NULL
)
2780 /* Merge current and previous unpartitioned entry */
2782 /* Adjust the previous entries length */
2783 PrevPartEntry
->SectorCount
.QuadPart
+= PartEntry
->SectorCount
.QuadPart
;
2785 /* Remove the current entry */
2786 RemoveEntryList(&PartEntry
->ListEntry
);
2787 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
2789 /* Update current partition */
2790 List
->CurrentPartition
= PrevPartEntry
;
2792 else if (PrevPartEntry
== NULL
&& NextPartEntry
!= NULL
)
2794 /* Merge current and next unpartitioned entry */
2796 /* Adjust the next entries offset and length */
2797 NextPartEntry
->StartSector
.QuadPart
= PartEntry
->StartSector
.QuadPart
;
2798 NextPartEntry
->SectorCount
.QuadPart
+= PartEntry
->SectorCount
.QuadPart
;
2800 /* Remove the current entry */
2801 RemoveEntryList(&PartEntry
->ListEntry
);
2802 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
2804 /* Update current partition */
2805 List
->CurrentPartition
= NextPartEntry
;
2809 /* Nothing to merge but change current entry */
2810 PartEntry
->IsPartitioned
= FALSE
;
2811 PartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2812 PartEntry
->FormatState
= Unformatted
;
2813 PartEntry
->DriveLetter
= 0;
2816 UpdateDiskLayout(DiskEntry
);
2818 DiskEntry
->Dirty
= TRUE
;
2820 UpdatePartitionNumbers(DiskEntry
);
2822 AssignDriveLetters(List
);
2827 CheckActiveBootPartition(
2830 PDISKENTRY DiskEntry
;
2831 PPARTENTRY PartEntry
;
2832 PLIST_ENTRY ListEntry
;
2834 /* Check for empty disk list */
2835 if (IsListEmpty (&List
->DiskListHead
))
2837 List
->BootDisk
= NULL
;
2838 List
->BootPartition
= NULL
;
2843 if (List
->BootDisk
!= NULL
&&
2844 List
->BootPartition
!= NULL
)
2846 /* We already have an active boot partition */
2851 /* Choose the currently selected disk */
2852 DiskEntry
= List
->CurrentDisk
;
2854 /* Check for empty partition list */
2855 if (IsListEmpty (&DiskEntry
->PrimaryPartListHead
))
2857 List
->BootDisk
= NULL
;
2858 List
->BootPartition
= NULL
;
2862 PartEntry
= CONTAINING_RECORD(DiskEntry
->PrimaryPartListHead
.Flink
,
2866 /* Set active boot partition */
2867 if ((DiskEntry
->NewDisk
== TRUE
) ||
2868 (PartEntry
->BootIndicator
== FALSE
))
2870 PartEntry
->BootIndicator
= TRUE
;
2871 DiskEntry
->LayoutBuffer
->PartitionEntry
[PartEntry
->PartitionIndex
].BootIndicator
= TRUE
;
2872 DiskEntry
->LayoutBuffer
->PartitionEntry
[PartEntry
->PartitionIndex
].RewritePartition
= TRUE
;
2873 DiskEntry
->Dirty
= TRUE
;
2875 /* FIXME: Might be incorrect if partitions were created by Linux FDISK */
2876 List
->BootDisk
= DiskEntry
;
2877 List
->BootPartition
= PartEntry
;
2882 /* Disk is not new, scan all partitions to find a bootable one */
2883 List
->BootDisk
= NULL
;
2884 List
->BootPartition
= NULL
;
2886 ListEntry
= DiskEntry
->PrimaryPartListHead
.Flink
;
2887 while (ListEntry
!= &DiskEntry
->PrimaryPartListHead
)
2889 PartEntry
= CONTAINING_RECORD(ListEntry
,
2893 /* Check if it is partitioned */
2894 if (PartEntry
->IsPartitioned
)
2896 if (PartEntry
->PartitionType
!= PARTITION_ENTRY_UNUSED
&&
2897 PartEntry
->BootIndicator
)
2899 /* Yes, we found it */
2900 List
->BootDisk
= DiskEntry
;
2901 List
->BootPartition
= PartEntry
;
2903 DPRINT("Found bootable partition disk %d, drive letter %c\n",
2904 DiskEntry
->DiskNumber
, PartEntry
->DriveLetter
);
2909 /* Go to the next one */
2910 ListEntry
= ListEntry
->Flink
;
2919 IN PDISKENTRY DiskEntry
)
2921 WCHAR DstPath
[MAX_PATH
];
2922 OBJECT_ATTRIBUTES ObjectAttributes
;
2923 IO_STATUS_BLOCK Iosb
;
2924 UNICODE_STRING Name
;
2926 HANDLE FileHandle
= NULL
;
2929 DPRINT("WritePartitions() Disk: %lu\n", DiskEntry
->DiskNumber
);
2932 L
"\\Device\\Harddisk%d\\Partition0",
2933 DiskEntry
->DiskNumber
);
2934 RtlInitUnicodeString(&Name
,
2936 InitializeObjectAttributes(&ObjectAttributes
,
2942 Status
= NtOpenFile(&FileHandle
,
2943 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
,
2947 FILE_SYNCHRONOUS_IO_NONALERT
);
2948 if (!NT_SUCCESS(Status
))
2950 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status
);
2954 #ifdef DUMP_PARTITION_TABLE
2955 DumpPartitionTable(DiskEntry
);
2958 BufferSize
= sizeof(DRIVE_LAYOUT_INFORMATION
) +
2959 ((DiskEntry
->LayoutBuffer
->PartitionCount
- 1) * sizeof(PARTITION_INFORMATION
));
2960 Status
= NtDeviceIoControlFile(FileHandle
,
2965 IOCTL_DISK_SET_DRIVE_LAYOUT
,
2966 DiskEntry
->LayoutBuffer
,
2970 if (!NT_SUCCESS(Status
))
2972 DPRINT1("IOCTL_DISK_SET_DRIVE_LAYOUT failed (Status 0x%08lx)\n", Status
);
2975 if (FileHandle
!= NULL
)
2976 NtClose(FileHandle
);
2983 WritePartitionsToDisk(
2987 PDISKENTRY DiskEntry
;
2992 Entry
= List
->DiskListHead
.Flink
;
2993 while (Entry
!= &List
->DiskListHead
)
2995 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
2997 if (DiskEntry
->Dirty
== TRUE
)
2999 WritePartitions(List
, DiskEntry
);
3000 DiskEntry
->Dirty
= FALSE
;
3003 Entry
= Entry
->Flink
;
3011 SetMountedDeviceValues(
3014 PLIST_ENTRY Entry1
, Entry2
;
3015 PDISKENTRY DiskEntry
;
3016 PPARTENTRY PartEntry
;
3017 LARGE_INTEGER StartingOffset
;
3024 Entry1
= List
->DiskListHead
.Flink
;
3025 while (Entry1
!= &List
->DiskListHead
)
3027 DiskEntry
= CONTAINING_RECORD(Entry1
,
3031 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
3032 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
3034 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3035 if (PartEntry
->IsPartitioned
)
3037 if (PartEntry
->DriveLetter
)
3039 StartingOffset
.QuadPart
= PartEntry
->StartSector
.QuadPart
* DiskEntry
->BytesPerSector
;
3040 if (!SetMountedDeviceValue(PartEntry
->DriveLetter
,
3041 DiskEntry
->LayoutBuffer
->Signature
,
3049 Entry2
= Entry2
->Flink
;
3052 Entry1
= Entry1
->Flink
;
3061 GetPrimaryPartitionCount(
3062 IN PDISKENTRY DiskEntry
)
3065 PPARTENTRY PartEntry
;
3068 Entry
= DiskEntry
->PrimaryPartListHead
.Flink
;
3069 while (Entry
!= &DiskEntry
->PrimaryPartListHead
)
3071 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
3072 if (PartEntry
->IsPartitioned
== TRUE
)
3075 Entry
= Entry
->Flink
;
3083 PrimaryPartitionCreationChecks(
3086 PDISKENTRY DiskEntry
;
3087 PPARTENTRY PartEntry
;
3089 DiskEntry
= List
->CurrentDisk
;
3090 PartEntry
= List
->CurrentPartition
;
3092 /* Fail if partition is already in use */
3093 if (PartEntry
->IsPartitioned
== TRUE
)
3094 return ERROR_NEW_PARTITION
;
3096 /* Fail if there are more than 4 partitions in the list */
3097 if (GetPrimaryPartitionCount(DiskEntry
) > 4)
3098 return ERROR_PARTITION_TABLE_FULL
;
3100 return ERROR_SUCCESS
;
3105 ExtendedPartitionCreationChecks(
3108 PDISKENTRY DiskEntry
;
3109 PPARTENTRY PartEntry
;
3111 DiskEntry
= List
->CurrentDisk
;
3112 PartEntry
= List
->CurrentPartition
;
3114 /* Fail if partition is already in use */
3115 if (PartEntry
->IsPartitioned
== TRUE
)
3116 return ERROR_NEW_PARTITION
;
3118 /* Fail if there are more than 4 partitions in the list */
3119 if (GetPrimaryPartitionCount(DiskEntry
) > 4)
3120 return ERROR_PARTITION_TABLE_FULL
;
3122 /* Fail if there is another extended partition in the list */
3123 if (DiskEntry
->ExtendedPartition
!= NULL
)
3124 return ERROR_ONLY_ONE_EXTENDED
;
3126 return ERROR_SUCCESS
;
3131 LogicalPartitionCreationChecks(
3134 // PDISKENTRY DiskEntry;
3135 PPARTENTRY PartEntry
;
3137 // DiskEntry = List->CurrentDisk;
3138 PartEntry
= List
->CurrentPartition
;
3140 /* Fail if partition is already in use */
3141 if (PartEntry
->IsPartitioned
== TRUE
)
3142 return ERROR_NEW_PARTITION
;
3144 return ERROR_SUCCESS
;
3149 GetNextUnformattedPartition(
3151 OUT PDISKENTRY
*pDiskEntry
,
3152 OUT PPARTENTRY
*pPartEntry
)
3154 PLIST_ENTRY Entry1
, Entry2
;
3155 PDISKENTRY DiskEntry
;
3156 PPARTENTRY PartEntry
;
3158 Entry1
= List
->DiskListHead
.Flink
;
3159 while (Entry1
!= &List
->DiskListHead
)
3161 DiskEntry
= CONTAINING_RECORD(Entry1
,
3165 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
3166 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
3168 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3169 if (PartEntry
->IsPartitioned
&& PartEntry
->New
)
3171 *pDiskEntry
= DiskEntry
;
3172 *pPartEntry
= PartEntry
;
3176 Entry2
= Entry2
->Flink
;
3179 Entry2
= DiskEntry
->LogicalPartListHead
.Flink
;
3180 while (Entry2
!= &DiskEntry
->LogicalPartListHead
)
3182 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3183 if (PartEntry
->IsPartitioned
&& PartEntry
->New
)
3185 *pDiskEntry
= DiskEntry
;
3186 *pPartEntry
= PartEntry
;
3190 Entry2
= Entry2
->Flink
;
3193 Entry1
= Entry1
->Flink
;
3204 GetNextUncheckedPartition(
3206 OUT PDISKENTRY
*pDiskEntry
,
3207 OUT PPARTENTRY
*pPartEntry
)
3209 PLIST_ENTRY Entry1
, Entry2
;
3210 PDISKENTRY DiskEntry
;
3211 PPARTENTRY PartEntry
;
3213 Entry1
= List
->DiskListHead
.Flink
;
3214 while (Entry1
!= &List
->DiskListHead
)
3216 DiskEntry
= CONTAINING_RECORD(Entry1
,
3220 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
3221 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
3223 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3224 if (PartEntry
->NeedsCheck
== TRUE
)
3226 *pDiskEntry
= DiskEntry
;
3227 *pPartEntry
= PartEntry
;
3231 Entry2
= Entry2
->Flink
;
3234 Entry2
= DiskEntry
->LogicalPartListHead
.Flink
;
3235 while (Entry2
!= &DiskEntry
->LogicalPartListHead
)
3237 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3238 if (PartEntry
->NeedsCheck
== TRUE
)
3240 *pDiskEntry
= DiskEntry
;
3241 *pPartEntry
= PartEntry
;
3245 Entry2
= Entry2
->Flink
;
3248 Entry1
= Entry1
->Flink
;