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: base/setup/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 /* HELPERS FOR PARTITION TYPES **********************************************/
38 typedef struct _PARTITION_TYPE
42 } PARTITION_TYPE
, *PPARTITION_TYPE
;
45 * This partition type list was ripped off the kernelDisk.c module from:
47 * Visopsys Operating System
48 * Copyright (C) 1998-2015 J. Andrew McLaughlin
50 * This program is free software; you can redistribute it and/or modify it
51 * under the terms of the GNU General Public License as published by the Free
52 * Software Foundation; either version 2 of the License, or (at your option)
55 * This program is distributed in the hope that it will be useful, but
56 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
57 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
60 * You should have received a copy of the GNU General Public License along
61 * with this program; if not, write to the Free Software Foundation, Inc.,
62 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
65 * See also https://en.wikipedia.org/wiki/Partition_type#List_of_partition_IDs
66 * and http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
67 * for a complete list.
70 /* This is a table for keeping known partition type codes and descriptions */
71 static PARTITION_TYPE PartitionTypes
[] =
75 { 0x02, "XENIX root" },
76 { 0x03, "XENIX /usr" },
77 { 0x04, "FAT16 (small)" },
80 { 0x07, "NTFS/HPFS/exFAT" },
81 { 0x08, "OS/2 or AIX boot" },
83 { 0x0A, "OS/2 Boot Manager" },
85 { 0x0C, "FAT32 (LBA)" },
86 { 0x0E, "FAT16 (LBA)" },
87 { 0x0F, "Extended (LBA)" },
88 { 0x11, "Hidden FAT12" },
89 { 0x12, "FAT diagnostic" },
90 { 0x14, "Hidden FAT16 (small)" },
91 { 0x16, "Hidden FAT16" },
92 { 0x17, "Hidden HPFS or NTFS" },
93 { 0x1B, "Hidden FAT32" },
94 { 0x1C, "Hidden FAT32 (LBA)" },
95 { 0x1E, "Hidden FAT16 (LBA)" },
98 { 0x3C, "PartitionMagic" },
99 { 0x3D, "Hidden Netware" },
100 { 0x41, "PowerPC PReP" },
101 { 0x42, "Win2K dynamic extended" },
102 { 0x43, "Old Linux" },
105 { 0x4D, "QNX4.x 2nd" },
106 { 0x4D, "QNX4.x 3rd" },
107 { 0x50, "Ontrack R/O" },
108 { 0x51, "Ontrack R/W or Novell" },
110 { 0x63, "GNU HURD or UNIX SysV" },
111 { 0x64, "Netware 2" },
112 { 0x65, "Netware 3/4" },
113 { 0x66, "Netware SMS" },
116 { 0x69, "Netware 5+" },
117 { 0x7E, "Veritas VxVM public" },
118 { 0x7F, "Veritas VxVM private" },
120 { 0x81, "Linux or Minix" },
121 { 0x82, "Linux swap or Solaris" },
123 { 0x84, "Hibernation" },
124 { 0x85, "Linux extended" },
125 { 0x86, "HPFS or NTFS mirrored" },
126 { 0x87, "HPFS or NTFS mirrored" },
127 { 0x8E, "Linux LVM" },
128 { 0x93, "Hidden Linux" },
130 { 0xA0, "Laptop hibernation" },
131 { 0xA1, "Laptop hibernation" },
132 { 0xA5, "BSD, NetBSD, FreeBSD" },
134 { 0xA7, "NeXTSTEP" },
135 { 0xA8, "OS-X UFS" },
137 { 0xAB, "OS-X boot" },
138 { 0xAF, "OS-X HFS" },
139 { 0xB6, "NT corrupt mirror" },
141 { 0xB8, "BSDI swap" },
142 { 0xBE, "Solaris 8 boot" },
143 { 0xBF, "Solaris x86" },
145 { 0xC1, "DR-DOS FAT12" },
146 { 0xC2, "Hidden Linux" },
147 { 0xC3, "Hidden Linux swap" },
148 { 0xC4, "DR-DOS FAT16 (small)" },
149 { 0xC5, "DR-DOS Extended" },
150 { 0xC6, "DR-DOS FAT16" },
151 { 0xC7, "HPFS mirrored" },
152 { 0xCB, "DR-DOS FAT32" },
153 { 0xCC, "DR-DOS FAT32 (LBA)" },
154 { 0xCE, "DR-DOS FAT16 (LBA)" },
156 { 0xD1, "MDOS FAT12" },
157 { 0xD4, "MDOS FAT16 (small)" },
158 { 0xD5, "MDOS Extended" },
159 { 0xD6, "MDOS FAT16" },
161 { 0xDF, "BootIt EMBRM(FAT16/32)" },
162 { 0xEB, "BeOS BFS" },
163 { 0xEE, "EFI GPT protective" },
164 { 0xEF, "EFI filesystem" },
165 { 0xF0, "Linux/PA-RISC boot" },
166 { 0xF2, "DOS 3.3+ second" },
169 { 0xFC, "VmWare swap" },
170 { 0xFD, "Linux RAID" },
171 { 0xFE, "NT hidden" },
175 GetPartTypeStringFromPartitionType(
180 /* Determine partition type */
182 if (IsContainerPartition(partitionType
))
184 StringCchCopy(strPartType
, cchPartType
, MUIGetString(STRING_EXTENDED_PARTITION
));
186 else if (partitionType
== PARTITION_ENTRY_UNUSED
)
188 StringCchCopy(strPartType
, cchPartType
, MUIGetString(STRING_FORMATUNUSED
));
194 /* Do the table lookup */
195 for (i
= 0; i
< ARRAYSIZE(PartitionTypes
); i
++)
197 if (partitionType
== PartitionTypes
[i
].Type
)
199 StringCchCopy(strPartType
, cchPartType
, PartitionTypes
[i
].Description
);
204 /* We are here because the partition type is unknown */
205 StringCchCopy(strPartType
, cchPartType
, MUIGetString(STRING_FORMATUNKNOWN
));
209 /* FUNCTIONS ****************************************************************/
211 #ifdef DUMP_PARTITION_TABLE
215 PDISKENTRY DiskEntry
)
217 PPARTITION_INFORMATION PartitionInfo
;
221 DbgPrint("Index Start Length Hidden Nr Type Boot RW\n");
222 DbgPrint("----- ------------ ------------ ---------- -- ---- ---- --\n");
224 for (i
= 0; i
< DiskEntry
->LayoutBuffer
->PartitionCount
; i
++)
226 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[i
];
227 DbgPrint(" %3lu %12I64u %12I64u %10lu %2lu %2x %c %c\n",
229 PartitionInfo
->StartingOffset
.QuadPart
/ DiskEntry
->BytesPerSector
,
230 PartitionInfo
->PartitionLength
.QuadPart
/ DiskEntry
->BytesPerSector
,
231 PartitionInfo
->HiddenSectors
,
232 PartitionInfo
->PartitionNumber
,
233 PartitionInfo
->PartitionType
,
234 PartitionInfo
->BootIndicator
? '*': ' ',
235 PartitionInfo
->RewritePartition
? 'Y': 'N');
250 Temp
= Value
/ Alignment
;
252 return Temp
* Alignment
;
261 ULONGLONG Temp
, Result
;
263 Temp
= Value
/ Alignment
;
265 Result
= Temp
* Alignment
;
266 if (Value
% Alignment
)
274 IN ULONGLONG Dividend
,
275 IN ULONGLONG Divisor
)
277 return (Dividend
+ Divisor
/ 2) / Divisor
;
284 PDISKENTRY DiskEntry
)
286 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
290 RtlInitUnicodeString(&DiskEntry
->DriverName
,
294 L
"\\Scsi\\Scsi Port %lu",
297 RtlZeroMemory(&QueryTable
,
300 QueryTable
[0].Name
= L
"Driver";
301 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
302 QueryTable
[0].EntryContext
= &DiskEntry
->DriverName
;
304 Status
= RtlQueryRegistryValues(RTL_REGISTRY_DEVICEMAP
,
309 if (!NT_SUCCESS(Status
))
311 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
321 PDISKENTRY DiskEntry
;
322 PPARTENTRY PartEntry
;
329 /* Assign drive letters to primary partitions */
330 Entry1
= List
->DiskListHead
.Flink
;
331 while (Entry1
!= &List
->DiskListHead
)
333 DiskEntry
= CONTAINING_RECORD(Entry1
, DISKENTRY
, ListEntry
);
335 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
336 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
338 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
340 PartEntry
->DriveLetter
= 0;
342 if (PartEntry
->IsPartitioned
&&
343 !IsContainerPartition(PartEntry
->PartitionType
))
345 if (IsRecognizedPartition(PartEntry
->PartitionType
) ||
346 (PartEntry
->PartitionType
== PARTITION_ENTRY_UNUSED
&&
347 PartEntry
->SectorCount
.QuadPart
!= 0LL))
351 PartEntry
->DriveLetter
= Letter
;
357 Entry2
= Entry2
->Flink
;
360 Entry1
= Entry1
->Flink
;
363 /* Assign drive letters to logical drives */
364 Entry1
= List
->DiskListHead
.Flink
;
365 while (Entry1
!= &List
->DiskListHead
)
367 DiskEntry
= CONTAINING_RECORD(Entry1
, DISKENTRY
, ListEntry
);
369 Entry2
= DiskEntry
->LogicalPartListHead
.Flink
;
370 while (Entry2
!= &DiskEntry
->LogicalPartListHead
)
372 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
374 PartEntry
->DriveLetter
= 0;
376 if (PartEntry
->IsPartitioned
)
378 if (IsRecognizedPartition(PartEntry
->PartitionType
) ||
379 (PartEntry
->PartitionType
== PARTITION_ENTRY_UNUSED
&&
380 PartEntry
->SectorCount
.QuadPart
!= 0LL))
384 PartEntry
->DriveLetter
= Letter
;
390 Entry2
= Entry2
->Flink
;
393 Entry1
= Entry1
->Flink
;
400 DiskIdentifierQueryRoutine(
408 PBIOSDISKENTRY BiosDiskEntry
= (PBIOSDISKENTRY
)Context
;
409 UNICODE_STRING NameU
;
411 if (ValueType
== REG_SZ
&&
412 ValueLength
== 20 * sizeof(WCHAR
))
414 NameU
.Buffer
= (PWCHAR
)ValueData
;
415 NameU
.Length
= NameU
.MaximumLength
= 8 * sizeof(WCHAR
);
416 RtlUnicodeStringToInteger(&NameU
, 16, &BiosDiskEntry
->Checksum
);
418 NameU
.Buffer
= (PWCHAR
)ValueData
+ 9;
419 RtlUnicodeStringToInteger(&NameU
, 16, &BiosDiskEntry
->Signature
);
421 return STATUS_SUCCESS
;
424 return STATUS_UNSUCCESSFUL
;
430 DiskConfigurationDataQueryRoutine(
438 PBIOSDISKENTRY BiosDiskEntry
= (PBIOSDISKENTRY
)Context
;
439 PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor
;
440 PCM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry
;
443 if (ValueType
!= REG_FULL_RESOURCE_DESCRIPTOR
||
444 ValueLength
< sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
445 return STATUS_UNSUCCESSFUL
;
447 FullResourceDescriptor
= (PCM_FULL_RESOURCE_DESCRIPTOR
)ValueData
;
449 /* Hm. Version and Revision are not set on Microsoft Windows XP... */
451 if (FullResourceDescriptor
->PartialResourceList
.Version
!= 1 ||
452 FullResourceDescriptor
->PartialResourceList
.Revision
!= 1)
453 return STATUS_UNSUCCESSFUL
;
456 for (i
= 0; i
< FullResourceDescriptor
->PartialResourceList
.Count
; i
++)
458 if (FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].Type
!= CmResourceTypeDeviceSpecific
||
459 FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].u
.DeviceSpecificData
.DataSize
!= sizeof(CM_DISK_GEOMETRY_DEVICE_DATA
))
462 DiskGeometry
= (PCM_DISK_GEOMETRY_DEVICE_DATA
)&FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
+ 1];
463 BiosDiskEntry
->DiskGeometry
= *DiskGeometry
;
465 return STATUS_SUCCESS
;
468 return STATUS_UNSUCCESSFUL
;
474 SystemConfigurationDataQueryRoutine(
482 PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor
;
483 PCM_INT13_DRIVE_PARAMETER
* Int13Drives
= (PCM_INT13_DRIVE_PARAMETER
*)Context
;
486 if (ValueType
!= REG_FULL_RESOURCE_DESCRIPTOR
||
487 ValueLength
< sizeof (CM_FULL_RESOURCE_DESCRIPTOR
))
488 return STATUS_UNSUCCESSFUL
;
490 FullResourceDescriptor
= (PCM_FULL_RESOURCE_DESCRIPTOR
)ValueData
;
492 /* Hm. Version and Revision are not set on Microsoft Windows XP... */
494 if (FullResourceDescriptor
->PartialResourceList
.Version
!= 1 ||
495 FullResourceDescriptor
->PartialResourceList
.Revision
!= 1)
496 return STATUS_UNSUCCESSFUL
;
499 for (i
= 0; i
< FullResourceDescriptor
->PartialResourceList
.Count
; i
++)
501 if (FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].Type
!= CmResourceTypeDeviceSpecific
||
502 FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].u
.DeviceSpecificData
.DataSize
% sizeof(CM_INT13_DRIVE_PARAMETER
) != 0)
505 *Int13Drives
= (CM_INT13_DRIVE_PARAMETER
*) RtlAllocateHeap(ProcessHeap
, 0, FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].u
.DeviceSpecificData
.DataSize
);
506 if (*Int13Drives
== NULL
)
507 return STATUS_NO_MEMORY
;
510 &FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
+ 1],
511 FullResourceDescriptor
->PartialResourceList
.PartialDescriptors
[i
].u
.DeviceSpecificData
.DataSize
);
512 return STATUS_SUCCESS
;
515 return STATUS_UNSUCCESSFUL
;
519 #define ROOT_NAME L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter"
522 EnumerateBiosDiskEntries(
525 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
530 PCM_INT13_DRIVE_PARAMETER Int13Drives
;
531 PBIOSDISKENTRY BiosDiskEntry
;
533 memset(QueryTable
, 0, sizeof(QueryTable
));
535 QueryTable
[1].Name
= L
"Configuration Data";
536 QueryTable
[1].QueryRoutine
= SystemConfigurationDataQueryRoutine
;
538 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
539 L
"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System",
543 if (!NT_SUCCESS(Status
))
545 DPRINT1("Unable to query the 'Configuration Data' key in '\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System', status=%lx\n", Status
);
552 swprintf(Name
, L
"%s\\%lu", ROOT_NAME
, AdapterCount
);
553 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
558 if (!NT_SUCCESS(Status
))
563 swprintf(Name
, L
"%s\\%lu\\DiskController", ROOT_NAME
, AdapterCount
);
564 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
569 if (NT_SUCCESS(Status
))
573 swprintf(Name
, L
"%s\\%lu\\DiskController\\0", ROOT_NAME
, AdapterCount
);
574 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
579 if (!NT_SUCCESS(Status
))
581 RtlFreeHeap(ProcessHeap
, 0, Int13Drives
);
585 swprintf(Name
, L
"%s\\%lu\\DiskController\\0\\DiskPeripheral", ROOT_NAME
, AdapterCount
);
586 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
591 if (NT_SUCCESS(Status
))
593 QueryTable
[0].Name
= L
"Identifier";
594 QueryTable
[0].QueryRoutine
= DiskIdentifierQueryRoutine
;
595 QueryTable
[1].Name
= L
"Configuration Data";
596 QueryTable
[1].QueryRoutine
= DiskConfigurationDataQueryRoutine
;
601 BiosDiskEntry
= (BIOSDISKENTRY
*) RtlAllocateHeap(ProcessHeap
, HEAP_ZERO_MEMORY
, sizeof(BIOSDISKENTRY
));
602 if (BiosDiskEntry
== NULL
)
607 swprintf(Name
, L
"%s\\%lu\\DiskController\\0\\DiskPeripheral\\%lu", ROOT_NAME
, AdapterCount
, DiskCount
);
608 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
611 (PVOID
)BiosDiskEntry
,
613 if (!NT_SUCCESS(Status
))
615 RtlFreeHeap(ProcessHeap
, 0, BiosDiskEntry
);
619 BiosDiskEntry
->DiskNumber
= DiskCount
;
620 BiosDiskEntry
->Recognized
= FALSE
;
622 if (DiskCount
< Int13Drives
[0].NumberDrives
)
624 BiosDiskEntry
->Int13DiskData
= Int13Drives
[DiskCount
];
628 DPRINT1("Didn't find int13 drive datas for disk %u\n", DiskCount
);
631 InsertTailList(&PartList
->BiosDiskListHead
, &BiosDiskEntry
->ListEntry
);
633 DPRINT("DiskNumber: %lu\n", BiosDiskEntry
->DiskNumber
);
634 DPRINT("Signature: %08lx\n", BiosDiskEntry
->Signature
);
635 DPRINT("Checksum: %08lx\n", BiosDiskEntry
->Checksum
);
636 DPRINT("BytesPerSector: %lu\n", BiosDiskEntry
->DiskGeometry
.BytesPerSector
);
637 DPRINT("NumberOfCylinders: %lu\n", BiosDiskEntry
->DiskGeometry
.NumberOfCylinders
);
638 DPRINT("NumberOfHeads: %lu\n", BiosDiskEntry
->DiskGeometry
.NumberOfHeads
);
639 DPRINT("DriveSelect: %02x\n", BiosDiskEntry
->Int13DiskData
.DriveSelect
);
640 DPRINT("MaxCylinders: %lu\n", BiosDiskEntry
->Int13DiskData
.MaxCylinders
);
641 DPRINT("SectorsPerTrack: %d\n", BiosDiskEntry
->Int13DiskData
.SectorsPerTrack
);
642 DPRINT("MaxHeads: %d\n", BiosDiskEntry
->Int13DiskData
.MaxHeads
);
643 DPRINT("NumberDrives: %d\n", BiosDiskEntry
->Int13DiskData
.NumberDrives
);
649 RtlFreeHeap(ProcessHeap
, 0, Int13Drives
);
657 RtlFreeHeap(ProcessHeap
, 0, Int13Drives
);
665 PDISKENTRY DiskEntry
,
666 ULONG PartitionIndex
,
667 BOOLEAN LogicalPartition
)
669 PPARTITION_INFORMATION PartitionInfo
;
670 PPARTENTRY PartEntry
;
672 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[PartitionIndex
];
673 if (PartitionInfo
->PartitionType
== 0 ||
674 (LogicalPartition
== TRUE
&& IsContainerPartition(PartitionInfo
->PartitionType
)))
677 PartEntry
= RtlAllocateHeap(ProcessHeap
,
680 if (PartEntry
== NULL
)
685 PartEntry
->DiskEntry
= DiskEntry
;
687 PartEntry
->StartSector
.QuadPart
= (ULONGLONG
)PartitionInfo
->StartingOffset
.QuadPart
/ DiskEntry
->BytesPerSector
;
688 PartEntry
->SectorCount
.QuadPart
= (ULONGLONG
)PartitionInfo
->PartitionLength
.QuadPart
/ DiskEntry
->BytesPerSector
;
690 PartEntry
->BootIndicator
= PartitionInfo
->BootIndicator
;
691 PartEntry
->PartitionType
= PartitionInfo
->PartitionType
;
692 PartEntry
->HiddenSectors
= PartitionInfo
->HiddenSectors
;
694 PartEntry
->LogicalPartition
= LogicalPartition
;
695 PartEntry
->IsPartitioned
= TRUE
;
696 PartEntry
->PartitionNumber
= PartitionInfo
->PartitionNumber
;
697 PartEntry
->PartitionIndex
= PartitionIndex
;
699 if (IsContainerPartition(PartEntry
->PartitionType
))
701 PartEntry
->FormatState
= Unformatted
;
703 if (LogicalPartition
== FALSE
&& DiskEntry
->ExtendedPartition
== NULL
)
704 DiskEntry
->ExtendedPartition
= PartEntry
;
706 else if ((PartEntry
->PartitionType
== PARTITION_FAT_12
) ||
707 (PartEntry
->PartitionType
== PARTITION_FAT_16
) ||
708 (PartEntry
->PartitionType
== PARTITION_HUGE
) ||
709 (PartEntry
->PartitionType
== PARTITION_XINT13
) ||
710 (PartEntry
->PartitionType
== PARTITION_FAT32
) ||
711 (PartEntry
->PartitionType
== PARTITION_FAT32_XINT13
))
714 if (CheckFatFormat())
716 PartEntry
->FormatState
= Preformatted
;
720 PartEntry
->FormatState
= Unformatted
;
723 PartEntry
->FormatState
= Preformatted
;
725 else if (PartEntry
->PartitionType
== PARTITION_EXT2
)
728 if (CheckExt2Format())
730 PartEntry
->FormatState
= Preformatted
;
734 PartEntry
->FormatState
= Unformatted
;
737 PartEntry
->FormatState
= Preformatted
;
739 else if (PartEntry
->PartitionType
== PARTITION_IFS
)
742 if (CheckNtfsFormat())
744 PartEntry
->FormatState
= Preformatted
;
746 else if (CheckHpfsFormat())
748 PartEntry
->FormatState
= Preformatted
;
752 PartEntry
->FormatState
= Unformatted
;
755 PartEntry
->FormatState
= Preformatted
;
759 PartEntry
->FormatState
= UnknownFormat
;
762 if (LogicalPartition
)
763 InsertTailList(&DiskEntry
->LogicalPartListHead
,
764 &PartEntry
->ListEntry
);
766 InsertTailList(&DiskEntry
->PrimaryPartListHead
,
767 &PartEntry
->ListEntry
);
773 ScanForUnpartitionedDiskSpace(
774 PDISKENTRY DiskEntry
)
776 ULONGLONG LastStartSector
;
777 ULONGLONG LastSectorCount
;
778 ULONGLONG LastUnusedSectorCount
;
779 PPARTENTRY PartEntry
;
780 PPARTENTRY NewPartEntry
;
783 DPRINT("ScanForUnpartitionedDiskSpace()\n");
785 if (IsListEmpty(&DiskEntry
->PrimaryPartListHead
))
787 DPRINT1("No primary partition!\n");
789 /* Create a partition table that represents the empty disk */
790 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
793 if (NewPartEntry
== NULL
)
796 NewPartEntry
->DiskEntry
= DiskEntry
;
798 NewPartEntry
->IsPartitioned
= FALSE
;
799 NewPartEntry
->StartSector
.QuadPart
= (ULONGLONG
)DiskEntry
->SectorAlignment
;
800 NewPartEntry
->SectorCount
.QuadPart
= AlignDown(DiskEntry
->SectorCount
.QuadPart
, DiskEntry
->SectorAlignment
) -
801 NewPartEntry
->StartSector
.QuadPart
;
803 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
804 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
805 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
807 NewPartEntry
->FormatState
= Unformatted
;
809 InsertTailList(&DiskEntry
->PrimaryPartListHead
,
810 &NewPartEntry
->ListEntry
);
815 /* Start partition at head 1, cylinder 0 */
816 LastStartSector
= DiskEntry
->SectorAlignment
;
817 LastSectorCount
= 0ULL;
818 LastUnusedSectorCount
= 0ULL;
820 Entry
= DiskEntry
->PrimaryPartListHead
.Flink
;
821 while (Entry
!= &DiskEntry
->PrimaryPartListHead
)
823 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
825 if (PartEntry
->PartitionType
!= PARTITION_ENTRY_UNUSED
||
826 PartEntry
->SectorCount
.QuadPart
!= 0ULL)
828 LastUnusedSectorCount
=
829 PartEntry
->StartSector
.QuadPart
- (LastStartSector
+ LastSectorCount
);
831 if (PartEntry
->StartSector
.QuadPart
> (LastStartSector
+ LastSectorCount
) &&
832 LastUnusedSectorCount
>= (ULONGLONG
)DiskEntry
->SectorAlignment
)
834 DPRINT("Unpartitioned disk space %I64u sectors\n", LastUnusedSectorCount
);
836 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
839 if (NewPartEntry
== NULL
)
842 NewPartEntry
->DiskEntry
= DiskEntry
;
844 NewPartEntry
->IsPartitioned
= FALSE
;
845 NewPartEntry
->StartSector
.QuadPart
= LastStartSector
+ LastSectorCount
;
846 NewPartEntry
->SectorCount
.QuadPart
= AlignDown(NewPartEntry
->StartSector
.QuadPart
+ LastUnusedSectorCount
, DiskEntry
->SectorAlignment
) -
847 NewPartEntry
->StartSector
.QuadPart
;
849 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
850 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
851 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
853 NewPartEntry
->FormatState
= Unformatted
;
855 /* Insert the table into the list */
856 InsertTailList(&PartEntry
->ListEntry
,
857 &NewPartEntry
->ListEntry
);
860 LastStartSector
= PartEntry
->StartSector
.QuadPart
;
861 LastSectorCount
= PartEntry
->SectorCount
.QuadPart
;
864 Entry
= Entry
->Flink
;
867 /* Check for trailing unpartitioned disk space */
868 if ((LastStartSector
+ LastSectorCount
) < DiskEntry
->SectorCount
.QuadPart
)
870 LastUnusedSectorCount
= AlignDown(DiskEntry
->SectorCount
.QuadPart
- (LastStartSector
+ LastSectorCount
), DiskEntry
->SectorAlignment
);
872 if (LastUnusedSectorCount
>= (ULONGLONG
)DiskEntry
->SectorAlignment
)
874 DPRINT("Unpartitioned disk space: %I64u sectors\n", LastUnusedSectorCount
);
876 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
879 if (NewPartEntry
== NULL
)
882 NewPartEntry
->DiskEntry
= DiskEntry
;
884 NewPartEntry
->IsPartitioned
= FALSE
;
885 NewPartEntry
->StartSector
.QuadPart
= LastStartSector
+ LastSectorCount
;
886 NewPartEntry
->SectorCount
.QuadPart
= AlignDown(NewPartEntry
->StartSector
.QuadPart
+ LastUnusedSectorCount
, DiskEntry
->SectorAlignment
) -
887 NewPartEntry
->StartSector
.QuadPart
;
889 DPRINT("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
890 DPRINT("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
891 DPRINT("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
893 NewPartEntry
->FormatState
= Unformatted
;
895 /* Append the table to the list */
896 InsertTailList(&DiskEntry
->PrimaryPartListHead
,
897 &NewPartEntry
->ListEntry
);
901 if (DiskEntry
->ExtendedPartition
!= NULL
)
903 if (IsListEmpty(&DiskEntry
->LogicalPartListHead
))
905 DPRINT1("No logical partition!\n");
907 /* Create a partition table entry that represents the empty extended partition */
908 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
911 if (NewPartEntry
== NULL
)
914 NewPartEntry
->DiskEntry
= DiskEntry
;
915 NewPartEntry
->LogicalPartition
= TRUE
;
917 NewPartEntry
->IsPartitioned
= FALSE
;
918 NewPartEntry
->StartSector
.QuadPart
= DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ (ULONGLONG
)DiskEntry
->SectorAlignment
;
919 NewPartEntry
->SectorCount
.QuadPart
= DiskEntry
->ExtendedPartition
->SectorCount
.QuadPart
- (ULONGLONG
)DiskEntry
->SectorAlignment
;
921 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
922 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
923 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
925 NewPartEntry
->FormatState
= Unformatted
;
927 InsertTailList(&DiskEntry
->LogicalPartListHead
,
928 &NewPartEntry
->ListEntry
);
933 /* Start partition at head 1, cylinder 0 */
934 LastStartSector
= DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ (ULONGLONG
)DiskEntry
->SectorAlignment
;
935 LastSectorCount
= 0ULL;
936 LastUnusedSectorCount
= 0ULL;
938 Entry
= DiskEntry
->LogicalPartListHead
.Flink
;
939 while (Entry
!= &DiskEntry
->LogicalPartListHead
)
941 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
943 if (PartEntry
->PartitionType
!= PARTITION_ENTRY_UNUSED
||
944 PartEntry
->SectorCount
.QuadPart
!= 0ULL)
946 LastUnusedSectorCount
=
947 PartEntry
->StartSector
.QuadPart
- (ULONGLONG
)DiskEntry
->SectorAlignment
- (LastStartSector
+ LastSectorCount
);
949 if ((PartEntry
->StartSector
.QuadPart
- (ULONGLONG
)DiskEntry
->SectorAlignment
) > (LastStartSector
+ LastSectorCount
) &&
950 LastUnusedSectorCount
>= (ULONGLONG
)DiskEntry
->SectorAlignment
)
952 DPRINT("Unpartitioned disk space %I64u sectors\n", LastUnusedSectorCount
);
954 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
957 if (NewPartEntry
== NULL
)
960 NewPartEntry
->DiskEntry
= DiskEntry
;
961 NewPartEntry
->LogicalPartition
= TRUE
;
963 NewPartEntry
->IsPartitioned
= FALSE
;
964 NewPartEntry
->StartSector
.QuadPart
= LastStartSector
+ LastSectorCount
;
965 NewPartEntry
->SectorCount
.QuadPart
= AlignDown(NewPartEntry
->StartSector
.QuadPart
+ LastUnusedSectorCount
, DiskEntry
->SectorAlignment
) -
966 NewPartEntry
->StartSector
.QuadPart
;
968 DPRINT("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
969 DPRINT("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
970 DPRINT("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
972 NewPartEntry
->FormatState
= Unformatted
;
974 /* Insert the table into the list */
975 InsertTailList(&PartEntry
->ListEntry
,
976 &NewPartEntry
->ListEntry
);
979 LastStartSector
= PartEntry
->StartSector
.QuadPart
;
980 LastSectorCount
= PartEntry
->SectorCount
.QuadPart
;
983 Entry
= Entry
->Flink
;
986 /* Check for trailing unpartitioned disk space */
987 if ((LastStartSector
+ LastSectorCount
) < DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ DiskEntry
->ExtendedPartition
->SectorCount
.QuadPart
)
989 LastUnusedSectorCount
= AlignDown(DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ DiskEntry
->ExtendedPartition
->SectorCount
.QuadPart
- (LastStartSector
+ LastSectorCount
), DiskEntry
->SectorAlignment
);
991 if (LastUnusedSectorCount
>= (ULONGLONG
)DiskEntry
->SectorAlignment
)
993 DPRINT("Unpartitioned disk space: %I64u sectors\n", LastUnusedSectorCount
);
995 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
998 if (NewPartEntry
== NULL
)
1001 NewPartEntry
->DiskEntry
= DiskEntry
;
1002 NewPartEntry
->LogicalPartition
= TRUE
;
1004 NewPartEntry
->IsPartitioned
= FALSE
;
1005 NewPartEntry
->StartSector
.QuadPart
= LastStartSector
+ LastSectorCount
;
1006 NewPartEntry
->SectorCount
.QuadPart
= AlignDown(NewPartEntry
->StartSector
.QuadPart
+ LastUnusedSectorCount
, DiskEntry
->SectorAlignment
) -
1007 NewPartEntry
->StartSector
.QuadPart
;
1009 DPRINT("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
1010 DPRINT("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
1011 DPRINT("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
1013 NewPartEntry
->FormatState
= Unformatted
;
1015 /* Append the table to the list */
1016 InsertTailList(&DiskEntry
->LogicalPartListHead
,
1017 &NewPartEntry
->ListEntry
);
1022 DPRINT("ScanForUnpartitionedDiskSpace() done\n");
1030 IN PDISKENTRY DiskEntry
)
1032 LARGE_INTEGER SystemTime
;
1033 TIME_FIELDS TimeFields
;
1035 PDISKENTRY DiskEntry2
;
1038 Buffer
= (PUCHAR
)&DiskEntry
->LayoutBuffer
->Signature
;
1042 NtQuerySystemTime(&SystemTime
);
1043 RtlTimeToTimeFields(&SystemTime
, &TimeFields
);
1045 Buffer
[0] = (UCHAR
)(TimeFields
.Year
& 0xFF) + (UCHAR
)(TimeFields
.Hour
& 0xFF);
1046 Buffer
[1] = (UCHAR
)(TimeFields
.Year
>> 8) + (UCHAR
)(TimeFields
.Minute
& 0xFF);
1047 Buffer
[2] = (UCHAR
)(TimeFields
.Month
& 0xFF) + (UCHAR
)(TimeFields
.Second
& 0xFF);
1048 Buffer
[3] = (UCHAR
)(TimeFields
.Day
& 0xFF) + (UCHAR
)(TimeFields
.Milliseconds
& 0xFF);
1050 if (DiskEntry
->LayoutBuffer
->Signature
== 0)
1055 /* check if the signature already exist */
1057 * Check also signatures from disks, which are
1058 * not visible (bootable) by the bios.
1060 Entry2
= List
->DiskListHead
.Flink
;
1061 while (Entry2
!= &List
->DiskListHead
)
1063 DiskEntry2
= CONTAINING_RECORD(Entry2
, DISKENTRY
, ListEntry
);
1065 if (DiskEntry
!= DiskEntry2
&&
1066 DiskEntry
->LayoutBuffer
->Signature
== DiskEntry2
->LayoutBuffer
->Signature
)
1069 Entry2
= Entry2
->Flink
;
1072 if (Entry2
== &List
->DiskListHead
)
1080 UpdateDiskSignatures(
1084 PDISKENTRY DiskEntry
;
1086 /* Print partition lines*/
1087 Entry
= List
->DiskListHead
.Flink
;
1088 while (Entry
!= &List
->DiskListHead
)
1090 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
1092 if (DiskEntry
->LayoutBuffer
&&
1093 DiskEntry
->LayoutBuffer
->Signature
== 0)
1095 SetDiskSignature(List
, DiskEntry
);
1096 DiskEntry
->LayoutBuffer
->PartitionEntry
[0].RewritePartition
= TRUE
;
1099 Entry
= Entry
->Flink
;
1111 DISK_GEOMETRY DiskGeometry
;
1112 SCSI_ADDRESS ScsiAddress
;
1113 PDISKENTRY DiskEntry
;
1114 IO_STATUS_BLOCK Iosb
;
1116 PPARTITION_SECTOR Mbr
;
1118 LARGE_INTEGER FileOffset
;
1119 WCHAR Identifier
[20];
1123 PLIST_ENTRY ListEntry
;
1124 PBIOSDISKENTRY BiosDiskEntry
;
1125 ULONG LayoutBufferSize
;
1126 PDRIVE_LAYOUT_INFORMATION NewLayoutBuffer
;
1128 Status
= NtDeviceIoControlFile(FileHandle
,
1133 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
1137 sizeof(DISK_GEOMETRY
));
1138 if (!NT_SUCCESS(Status
))
1143 if (DiskGeometry
.MediaType
!= FixedMedia
&&
1144 DiskGeometry
.MediaType
!= RemovableMedia
)
1149 Status
= NtDeviceIoControlFile(FileHandle
,
1154 IOCTL_SCSI_GET_ADDRESS
,
1158 sizeof(SCSI_ADDRESS
));
1159 if (!NT_SUCCESS(Status
))
1164 Mbr
= (PARTITION_SECTOR
*)RtlAllocateHeap(ProcessHeap
,
1166 DiskGeometry
.BytesPerSector
);
1172 FileOffset
.QuadPart
= 0;
1173 Status
= NtReadFile(FileHandle
,
1179 DiskGeometry
.BytesPerSector
,
1182 if (!NT_SUCCESS(Status
))
1184 RtlFreeHeap(ProcessHeap
,
1187 DPRINT1("NtReadFile failed, status=%x\n", Status
);
1190 Signature
= Mbr
->Signature
;
1192 /* Calculate the MBR checksum */
1194 Buffer
= (PULONG
)Mbr
;
1195 for (i
= 0; i
< 128; i
++)
1197 Checksum
+= Buffer
[i
];
1199 Checksum
= ~Checksum
+ 1;
1201 swprintf(Identifier
, L
"%08x-%08x-A", Checksum
, Signature
);
1202 DPRINT("Identifier: %S\n", Identifier
);
1204 DiskEntry
= RtlAllocateHeap(ProcessHeap
,
1207 if (DiskEntry
== NULL
)
1212 // DiskEntry->Checksum = Checksum;
1213 // DiskEntry->Signature = Signature;
1214 DiskEntry
->BiosFound
= FALSE
;
1216 /* Check if this disk has a valid MBR */
1217 if (Mbr
->BootCode
[0] == 0 && Mbr
->BootCode
[1] == 0)
1218 DiskEntry
->NoMbr
= TRUE
;
1220 DiskEntry
->NoMbr
= FALSE
;
1222 /* Free Mbr sector buffer */
1223 RtlFreeHeap(ProcessHeap
,
1227 ListEntry
= List
->BiosDiskListHead
.Flink
;
1228 while(ListEntry
!= &List
->BiosDiskListHead
)
1230 BiosDiskEntry
= CONTAINING_RECORD(ListEntry
, BIOSDISKENTRY
, ListEntry
);
1232 * Compare the size from bios and the reported size from driver.
1233 * If we have more than one disk with a zero or with the same signatur
1234 * we must create new signatures and reboot. After the reboot,
1235 * it is possible to identify the disks.
1237 if (BiosDiskEntry
->Signature
== Signature
&&
1238 BiosDiskEntry
->Checksum
== Checksum
&&
1239 !BiosDiskEntry
->Recognized
)
1241 if (!DiskEntry
->BiosFound
)
1243 DiskEntry
->BiosDiskNumber
= BiosDiskEntry
->DiskNumber
;
1244 DiskEntry
->BiosFound
= TRUE
;
1245 BiosDiskEntry
->Recognized
= TRUE
;
1251 ListEntry
= ListEntry
->Flink
;
1254 if (!DiskEntry
->BiosFound
)
1257 RtlFreeHeap(ProcessHeap
, 0, DiskEntry
);
1260 DPRINT1("WARNING: Setup could not find a matching BIOS disk entry. Disk %d is not be bootable by the BIOS!\n", DiskNumber
);
1264 InitializeListHead(&DiskEntry
->PrimaryPartListHead
);
1265 InitializeListHead(&DiskEntry
->LogicalPartListHead
);
1267 DiskEntry
->Cylinders
= DiskGeometry
.Cylinders
.QuadPart
;
1268 DiskEntry
->TracksPerCylinder
= DiskGeometry
.TracksPerCylinder
;
1269 DiskEntry
->SectorsPerTrack
= DiskGeometry
.SectorsPerTrack
;
1270 DiskEntry
->BytesPerSector
= DiskGeometry
.BytesPerSector
;
1272 DPRINT("Cylinders %I64u\n", DiskEntry
->Cylinders
);
1273 DPRINT("TracksPerCylinder %I64u\n", DiskEntry
->TracksPerCylinder
);
1274 DPRINT("SectorsPerTrack %I64u\n", DiskEntry
->SectorsPerTrack
);
1275 DPRINT("BytesPerSector %I64u\n", DiskEntry
->BytesPerSector
);
1277 DiskEntry
->SectorCount
.QuadPart
= DiskGeometry
.Cylinders
.QuadPart
*
1278 (ULONGLONG
)DiskGeometry
.TracksPerCylinder
*
1279 (ULONGLONG
)DiskGeometry
.SectorsPerTrack
;
1281 DiskEntry
->SectorAlignment
= DiskGeometry
.SectorsPerTrack
;
1282 DiskEntry
->CylinderAlignment
= DiskGeometry
.TracksPerCylinder
*
1283 DiskGeometry
.SectorsPerTrack
;
1285 DPRINT("SectorCount %I64u\n", DiskEntry
->SectorCount
);
1286 DPRINT("SectorAlignment %lu\n", DiskEntry
->SectorAlignment
);
1288 DiskEntry
->DiskNumber
= DiskNumber
;
1289 DiskEntry
->Port
= ScsiAddress
.PortNumber
;
1290 DiskEntry
->Bus
= ScsiAddress
.PathId
;
1291 DiskEntry
->Id
= ScsiAddress
.TargetId
;
1293 GetDriverName(DiskEntry
);
1295 InsertAscendingList(&List
->DiskListHead
, DiskEntry
, DISKENTRY
, ListEntry
, DiskNumber
);
1297 /* Allocate a layout buffer with 4 partition entries first */
1298 LayoutBufferSize
= sizeof(DRIVE_LAYOUT_INFORMATION
) +
1299 ((4 - ANYSIZE_ARRAY
) * sizeof(PARTITION_INFORMATION
));
1300 DiskEntry
->LayoutBuffer
= RtlAllocateHeap(ProcessHeap
,
1303 if (DiskEntry
->LayoutBuffer
== NULL
)
1305 DPRINT1("Failed to allocate the disk layout buffer!\n");
1311 DPRINT1("Buffer size: %lu\n", LayoutBufferSize
);
1312 Status
= NtDeviceIoControlFile(FileHandle
,
1317 IOCTL_DISK_GET_DRIVE_LAYOUT
,
1320 DiskEntry
->LayoutBuffer
,
1322 if (NT_SUCCESS(Status
))
1325 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
1327 DPRINT1("NtDeviceIoControlFile() failed (Status: 0x%08lx)\n", Status
);
1331 LayoutBufferSize
+= 4 * sizeof(PARTITION_INFORMATION
);
1332 NewLayoutBuffer
= RtlReAllocateHeap(ProcessHeap
,
1334 DiskEntry
->LayoutBuffer
,
1336 if (NewLayoutBuffer
== NULL
)
1338 DPRINT1("Failed to reallocate the disk layout buffer!\n");
1342 DiskEntry
->LayoutBuffer
= NewLayoutBuffer
;
1345 DPRINT1("PartitionCount: %lu\n", DiskEntry
->LayoutBuffer
->PartitionCount
);
1347 #ifdef DUMP_PARTITION_TABLE
1348 DumpPartitionTable(DiskEntry
);
1351 if (DiskEntry
->LayoutBuffer
->PartitionEntry
[0].StartingOffset
.QuadPart
!= 0 &&
1352 DiskEntry
->LayoutBuffer
->PartitionEntry
[0].PartitionLength
.QuadPart
!= 0 &&
1353 DiskEntry
->LayoutBuffer
->PartitionEntry
[0].PartitionType
!= 0)
1355 if ((DiskEntry
->LayoutBuffer
->PartitionEntry
[0].StartingOffset
.QuadPart
/ DiskEntry
->BytesPerSector
) % DiskEntry
->SectorsPerTrack
== 0)
1357 DPRINT("Use %lu Sector alignment!\n", DiskEntry
->SectorsPerTrack
);
1359 else if (DiskEntry
->LayoutBuffer
->PartitionEntry
[0].StartingOffset
.QuadPart
% (1024 * 1024) == 0)
1361 DPRINT1("Use megabyte (%lu Sectors) alignment!\n", (1024 * 1024) / DiskEntry
->BytesPerSector
);
1365 DPRINT1("No matching aligment found! Partition 1 starts at %I64u\n", DiskEntry
->LayoutBuffer
->PartitionEntry
[0].StartingOffset
.QuadPart
);
1370 DPRINT1("No valid partition table found! Use megabyte (%lu Sectors) alignment!\n", (1024 * 1024) / DiskEntry
->BytesPerSector
);
1374 if (DiskEntry
->LayoutBuffer
->PartitionCount
== 0)
1376 DiskEntry
->NewDisk
= TRUE
;
1377 DiskEntry
->LayoutBuffer
->PartitionCount
= 4;
1379 for (i
= 0; i
< 4; i
++)
1380 DiskEntry
->LayoutBuffer
->PartitionEntry
[i
].RewritePartition
= TRUE
;
1384 for (i
= 0; i
< 4; i
++)
1386 AddPartitionToDisk(DiskNumber
,
1392 for (i
= 4; i
< DiskEntry
->LayoutBuffer
->PartitionCount
; i
+= 4)
1394 AddPartitionToDisk(DiskNumber
,
1401 ScanForUnpartitionedDiskSpace(DiskEntry
);
1406 CreatePartitionList(
1413 OBJECT_ATTRIBUTES ObjectAttributes
;
1414 SYSTEM_DEVICE_INFORMATION Sdi
;
1415 IO_STATUS_BLOCK Iosb
;
1419 WCHAR Buffer
[MAX_PATH
];
1420 UNICODE_STRING Name
;
1423 List
= (PPARTLIST
)RtlAllocateHeap(ProcessHeap
,
1431 List
->Right
= Right
;
1432 List
->Bottom
= Bottom
;
1437 List
->CurrentDisk
= NULL
;
1438 List
->CurrentPartition
= NULL
;
1440 List
->SystemDisk
= NULL
;
1441 List
->SystemPartition
= NULL
;
1443 List
->TempDisk
= NULL
;
1444 List
->TempPartition
= NULL
;
1445 List
->FormatState
= Start
;
1447 InitializeListHead(&List
->DiskListHead
);
1448 InitializeListHead(&List
->BiosDiskListHead
);
1450 EnumerateBiosDiskEntries(List
);
1452 Status
= NtQuerySystemInformation(SystemDeviceInformation
,
1454 sizeof(SYSTEM_DEVICE_INFORMATION
),
1456 if (!NT_SUCCESS(Status
))
1458 RtlFreeHeap(ProcessHeap
, 0, List
);
1462 for (DiskNumber
= 0; DiskNumber
< Sdi
.NumberOfDisks
; DiskNumber
++)
1465 L
"\\Device\\Harddisk%d\\Partition0",
1467 RtlInitUnicodeString(&Name
,
1470 InitializeObjectAttributes(&ObjectAttributes
,
1476 Status
= NtOpenFile(&FileHandle
,
1477 FILE_READ_DATA
| FILE_READ_ATTRIBUTES
| SYNCHRONIZE
,
1481 FILE_SYNCHRONOUS_IO_NONALERT
);
1482 if (NT_SUCCESS(Status
))
1484 AddDiskToList(FileHandle
,
1488 NtClose(FileHandle
);
1492 UpdateDiskSignatures(List
);
1494 AssignDriveLetters(List
);
1496 /* Search for first usable disk and partition */
1497 if (IsListEmpty(&List
->DiskListHead
))
1499 List
->CurrentDisk
= NULL
;
1500 List
->CurrentPartition
= NULL
;
1504 List
->CurrentDisk
= CONTAINING_RECORD(List
->DiskListHead
.Flink
,
1508 if (IsListEmpty(&List
->CurrentDisk
->PrimaryPartListHead
))
1510 List
->CurrentPartition
= 0;
1514 List
->CurrentPartition
= CONTAINING_RECORD(List
->CurrentDisk
->PrimaryPartListHead
.Flink
,
1525 DestroyPartitionList(
1528 PDISKENTRY DiskEntry
;
1529 PBIOSDISKENTRY BiosDiskEntry
;
1530 PPARTENTRY PartEntry
;
1533 /* Release disk and partition info */
1534 while (!IsListEmpty(&List
->DiskListHead
))
1536 Entry
= RemoveHeadList(&List
->DiskListHead
);
1537 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
1539 /* Release driver name */
1540 RtlFreeUnicodeString(&DiskEntry
->DriverName
);
1542 /* Release primary partition list */
1543 while (!IsListEmpty(&DiskEntry
->PrimaryPartListHead
))
1545 Entry
= RemoveHeadList(&DiskEntry
->PrimaryPartListHead
);
1546 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
1548 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
1551 /* Release logical partition list */
1552 while (!IsListEmpty(&DiskEntry
->LogicalPartListHead
))
1554 Entry
= RemoveHeadList(&DiskEntry
->LogicalPartListHead
);
1555 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
1557 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
1560 /* Release layout buffer */
1561 if (DiskEntry
->LayoutBuffer
!= NULL
)
1562 RtlFreeHeap(ProcessHeap
, 0, DiskEntry
->LayoutBuffer
);
1565 /* Release disk entry */
1566 RtlFreeHeap(ProcessHeap
, 0, DiskEntry
);
1569 /* release the bios disk info */
1570 while(!IsListEmpty(&List
->BiosDiskListHead
))
1572 Entry
= RemoveHeadList(&List
->BiosDiskListHead
);
1573 BiosDiskEntry
= CONTAINING_RECORD(Entry
, BIOSDISKENTRY
, ListEntry
);
1575 RtlFreeHeap(ProcessHeap
, 0, BiosDiskEntry
);
1578 /* Release list head */
1579 RtlFreeHeap(ProcessHeap
, 0, List
);
1593 Width
= List
->Right
- List
->Left
- 1;
1594 Height
= List
->Bottom
- List
->Top
- 2;
1596 coPos
.X
= List
->Left
+ 1;
1597 coPos
.Y
= List
->Top
+ 1 + List
->Line
;
1599 if (List
->Line
>= 0 && List
->Line
<= Height
)
1601 FillConsoleOutputAttribute(StdOutput
,
1602 FOREGROUND_WHITE
| BACKGROUND_BLUE
,
1607 FillConsoleOutputCharacterA(StdOutput
,
1622 PDISKENTRY DiskEntry
,
1623 PPARTENTRY PartEntry
)
1625 CHAR LineBuffer
[128];
1630 LARGE_INTEGER PartSize
;
1633 CHAR PartTypeString
[32];
1635 PartType
= PartTypeString
;
1637 Width
= List
->Right
- List
->Left
- 1;
1638 Height
= List
->Bottom
- List
->Top
- 2;
1640 coPos
.X
= List
->Left
+ 1;
1641 coPos
.Y
= List
->Top
+ 1 + List
->Line
;
1643 if (PartEntry
->IsPartitioned
== FALSE
)
1645 PartSize
.QuadPart
= PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
1647 if (PartSize
.QuadPart
>= 10737418240) /* 10 GB */
1649 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1073741824);
1650 Unit
= MUIGetString(STRING_GB
);
1654 if (PartSize
.QuadPart
>= 10485760) /* 10 MB */
1656 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1048576);
1657 Unit
= MUIGetString(STRING_MB
);
1661 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1024);
1662 Unit
= MUIGetString(STRING_KB
);
1666 MUIGetString(STRING_UNPSPACE
),
1667 PartEntry
->LogicalPartition
? " " : "",
1668 PartEntry
->LogicalPartition
? "" : " ",
1674 /* Determine partition type */
1675 PartTypeString
[0] = '\0';
1676 if (PartEntry
->New
== TRUE
)
1678 PartType
= MUIGetString(STRING_UNFORMATTED
);
1680 else if (PartEntry
->IsPartitioned
== TRUE
)
1682 GetPartTypeStringFromPartitionType(PartEntry
->PartitionType
,
1685 PartType
= PartTypeString
;
1688 PartSize
.QuadPart
= PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
1690 if (PartSize
.QuadPart
>= 10737418240) /* 10 GB */
1692 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1073741824);
1693 Unit
= MUIGetString(STRING_GB
);
1697 if (PartSize
.QuadPart
>= 10485760) /* 10 MB */
1699 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1048576);
1700 Unit
= MUIGetString(STRING_MB
);
1704 PartSize
.QuadPart
= RoundingDivide(PartSize
.QuadPart
, 1024);
1705 Unit
= MUIGetString(STRING_KB
);
1708 if (strcmp(PartType
, MUIGetString(STRING_FORMATUNKNOWN
)) == 0)
1711 MUIGetString(STRING_HDDINFOUNK5
),
1712 (PartEntry
->DriveLetter
== 0) ? '-' : PartEntry
->DriveLetter
,
1713 (PartEntry
->DriveLetter
== 0) ? '-' : ':',
1714 PartEntry
->BootIndicator
? '*' : ' ',
1715 PartEntry
->LogicalPartition
? " " : "",
1716 PartEntry
->PartitionType
,
1717 PartEntry
->LogicalPartition
? "" : " ",
1724 "%c%c %c %s%-24s%s %6lu %s",
1725 (PartEntry
->DriveLetter
== 0) ? '-' : PartEntry
->DriveLetter
,
1726 (PartEntry
->DriveLetter
== 0) ? '-' : ':',
1727 PartEntry
->BootIndicator
? '*' : ' ',
1728 PartEntry
->LogicalPartition
? " " : "",
1730 PartEntry
->LogicalPartition
? "" : " ",
1736 Attribute
= (List
->CurrentDisk
== DiskEntry
&&
1737 List
->CurrentPartition
== PartEntry
) ?
1738 FOREGROUND_BLUE
| BACKGROUND_WHITE
:
1739 FOREGROUND_WHITE
| BACKGROUND_BLUE
;
1741 if (List
->Line
>= 0 && List
->Line
<= Height
)
1743 FillConsoleOutputCharacterA(StdOutput
,
1751 if (List
->Line
>= 0 && List
->Line
<= Height
)
1753 FillConsoleOutputAttribute(StdOutput
,
1761 if (List
->Line
>= 0 && List
->Line
<= Height
)
1763 WriteConsoleOutputCharacterA(StdOutput
,
1765 min(strlen(LineBuffer
), Width
),
1778 PDISKENTRY DiskEntry
)
1780 PPARTENTRY PrimaryPartEntry
, LogicalPartEntry
;
1781 PLIST_ENTRY PrimaryEntry
, LogicalEntry
;
1782 CHAR LineBuffer
[128];
1787 ULARGE_INTEGER DiskSize
;
1790 Width
= List
->Right
- List
->Left
- 1;
1791 Height
= List
->Bottom
- List
->Top
- 2;
1793 coPos
.X
= List
->Left
+ 1;
1794 coPos
.Y
= List
->Top
+ 1 + List
->Line
;
1796 DiskSize
.QuadPart
= DiskEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
1797 if (DiskSize
.QuadPart
>= 10737418240) /* 10 GB */
1799 DiskSize
.QuadPart
= RoundingDivide(DiskSize
.QuadPart
, 1073741824);
1800 Unit
= MUIGetString(STRING_GB
);
1804 DiskSize
.QuadPart
= RoundingDivide(DiskSize
.QuadPart
, 1048576);
1805 if (DiskSize
.QuadPart
== 0)
1806 DiskSize
.QuadPart
= 1;
1807 Unit
= MUIGetString(STRING_MB
);
1810 if (DiskEntry
->DriverName
.Length
> 0)
1813 MUIGetString(STRING_HDINFOPARTSELECT
),
1816 DiskEntry
->DiskNumber
,
1820 DiskEntry
->DriverName
.Buffer
);
1825 MUIGetString(STRING_HDDINFOUNK6
),
1828 DiskEntry
->DiskNumber
,
1834 if (List
->Line
>= 0 && List
->Line
<= Height
)
1836 FillConsoleOutputAttribute(StdOutput
,
1837 FOREGROUND_WHITE
| BACKGROUND_BLUE
,
1842 FillConsoleOutputCharacterA(StdOutput
,
1850 if (List
->Line
>= 0 && List
->Line
<= Height
)
1852 WriteConsoleOutputCharacterA(StdOutput
,
1854 min((USHORT
)strlen(LineBuffer
), Width
- 2),
1861 /* Print separator line */
1862 PrintEmptyLine(List
);
1864 /* Print partition lines*/
1865 PrimaryEntry
= DiskEntry
->PrimaryPartListHead
.Flink
;
1866 while (PrimaryEntry
!= &DiskEntry
->PrimaryPartListHead
)
1868 PrimaryPartEntry
= CONTAINING_RECORD(PrimaryEntry
, PARTENTRY
, ListEntry
);
1870 PrintPartitionData(List
,
1874 if (IsContainerPartition(PrimaryPartEntry
->PartitionType
))
1876 LogicalEntry
= DiskEntry
->LogicalPartListHead
.Flink
;
1877 while (LogicalEntry
!= &DiskEntry
->LogicalPartListHead
)
1879 LogicalPartEntry
= CONTAINING_RECORD(LogicalEntry
, PARTENTRY
, ListEntry
);
1881 PrintPartitionData(List
,
1885 LogicalEntry
= LogicalEntry
->Flink
;
1889 PrimaryEntry
= PrimaryEntry
->Flink
;
1892 /* Print separator line */
1893 PrintEmptyLine(List
);
1901 PLIST_ENTRY Entry
, Entry2
;
1902 PDISKENTRY DiskEntry
;
1903 PPARTENTRY PartEntry
= NULL
;
1907 SHORT CurrentDiskLine
;
1908 SHORT CurrentPartLine
;
1910 BOOL CurrentPartLineFound
= FALSE
;
1911 BOOL CurrentDiskLineFound
= FALSE
;
1913 /* Calculate the line of the current disk and partition */
1914 CurrentDiskLine
= 0;
1915 CurrentPartLine
= 0;
1918 Entry
= List
->DiskListHead
.Flink
;
1919 while (Entry
!= &List
->DiskListHead
)
1921 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
1924 if (CurrentPartLineFound
== FALSE
)
1926 CurrentPartLine
+= 2;
1929 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
1930 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
1932 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
1933 if (PartEntry
== List
->CurrentPartition
)
1935 CurrentPartLineFound
= TRUE
;
1938 Entry2
= Entry2
->Flink
;
1939 if (CurrentPartLineFound
== FALSE
)
1947 if (DiskEntry
== List
->CurrentDisk
)
1949 CurrentDiskLineFound
= TRUE
;
1952 Entry
= Entry
->Flink
;
1953 if (Entry
!= &List
->DiskListHead
)
1955 if (CurrentDiskLineFound
== FALSE
)
1958 CurrentDiskLine
= CurrentPartLine
;
1969 /* If it possible, make the disk name visible */
1970 if (CurrentPartLine
< List
->Offset
)
1972 List
->Offset
= CurrentPartLine
;
1974 else if (CurrentPartLine
- List
->Offset
> List
->Bottom
- List
->Top
- 2)
1976 List
->Offset
= CurrentPartLine
- (List
->Bottom
- List
->Top
- 2);
1979 if (CurrentDiskLine
< List
->Offset
&& CurrentPartLine
- CurrentDiskLine
< List
->Bottom
- List
->Top
- 2)
1981 List
->Offset
= CurrentDiskLine
;
1984 /* draw upper left corner */
1985 coPos
.X
= List
->Left
;
1986 coPos
.Y
= List
->Top
;
1987 FillConsoleOutputCharacterA(StdOutput
,
1993 /* draw upper edge */
1994 coPos
.X
= List
->Left
+ 1;
1995 coPos
.Y
= List
->Top
;
1996 if (List
->Offset
== 0)
1998 FillConsoleOutputCharacterA(StdOutput
,
2000 List
->Right
- List
->Left
- 1,
2006 FillConsoleOutputCharacterA(StdOutput
,
2008 List
->Right
- List
->Left
- 5,
2011 coPos
.X
= List
->Right
- 5;
2012 WriteConsoleOutputCharacterA(StdOutput
,
2017 coPos
.X
= List
->Right
- 2;
2018 FillConsoleOutputCharacterA(StdOutput
,
2025 /* draw upper right corner */
2026 coPos
.X
= List
->Right
;
2027 coPos
.Y
= List
->Top
;
2028 FillConsoleOutputCharacterA(StdOutput
,
2034 /* draw left and right edge */
2035 for (i
= List
->Top
+ 1; i
< List
->Bottom
; i
++)
2037 coPos
.X
= List
->Left
;
2039 FillConsoleOutputCharacterA(StdOutput
,
2045 coPos
.X
= List
->Right
;
2046 FillConsoleOutputCharacterA(StdOutput
,
2053 /* draw lower left corner */
2054 coPos
.X
= List
->Left
;
2055 coPos
.Y
= List
->Bottom
;
2056 FillConsoleOutputCharacterA(StdOutput
,
2062 /* draw lower edge */
2063 coPos
.X
= List
->Left
+ 1;
2064 coPos
.Y
= List
->Bottom
;
2065 if (LastLine
- List
->Offset
<= List
->Bottom
- List
->Top
- 2)
2067 FillConsoleOutputCharacterA(StdOutput
,
2069 List
->Right
- List
->Left
- 1,
2075 FillConsoleOutputCharacterA(StdOutput
,
2077 List
->Right
- List
->Left
- 5,
2080 coPos
.X
= List
->Right
- 5;
2081 WriteConsoleOutputCharacterA(StdOutput
,
2082 "(\x19)", // "(down)"
2086 coPos
.X
= List
->Right
- 2;
2087 FillConsoleOutputCharacterA(StdOutput
,
2094 /* draw lower right corner */
2095 coPos
.X
= List
->Right
;
2096 coPos
.Y
= List
->Bottom
;
2097 FillConsoleOutputCharacterA(StdOutput
,
2103 /* print list entries */
2104 List
->Line
= - List
->Offset
;
2106 Entry
= List
->DiskListHead
.Flink
;
2107 while (Entry
!= &List
->DiskListHead
)
2109 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
2111 /* Print disk entry */
2115 Entry
= Entry
->Flink
;
2124 ULONG PartitionNumber
)
2126 PDISKENTRY DiskEntry
;
2127 PPARTENTRY PartEntry
;
2131 /* Check for empty disks */
2132 if (IsListEmpty(&List
->DiskListHead
))
2135 /* Check for first usable entry on next disk */
2136 Entry1
= List
->CurrentDisk
->ListEntry
.Flink
;
2137 while (Entry1
!= &List
->DiskListHead
)
2139 DiskEntry
= CONTAINING_RECORD(Entry1
, DISKENTRY
, ListEntry
);
2141 if (DiskEntry
->DiskNumber
== DiskNumber
)
2143 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
2144 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
2146 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
2148 if (PartEntry
->PartitionNumber
== PartitionNumber
)
2150 List
->CurrentDisk
= DiskEntry
;
2151 List
->CurrentPartition
= PartEntry
;
2152 DrawPartitionList(List
);
2156 Entry2
= Entry2
->Flink
;
2162 Entry1
= Entry1
->Flink
;
2170 ScrollDownPartitionList(
2173 PLIST_ENTRY DiskListEntry
;
2174 PLIST_ENTRY PartListEntry
;
2175 PDISKENTRY DiskEntry
;
2176 PPARTENTRY PartEntry
;
2178 /* Fail, if no disks are available */
2179 if (IsListEmpty(&List
->DiskListHead
))
2182 /* Check for next usable entry on current disk */
2183 if (List
->CurrentPartition
!= NULL
)
2185 if (List
->CurrentPartition
->LogicalPartition
)
2187 /* Logical partition */
2189 PartListEntry
= List
->CurrentPartition
->ListEntry
.Flink
;
2190 if (PartListEntry
!= &List
->CurrentDisk
->LogicalPartListHead
)
2192 /* Next logical partition */
2193 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2195 List
->CurrentPartition
= PartEntry
;
2200 PartListEntry
= List
->CurrentDisk
->ExtendedPartition
->ListEntry
.Flink
;
2201 if (PartListEntry
!= &List
->CurrentDisk
->PrimaryPartListHead
)
2203 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2205 List
->CurrentPartition
= PartEntry
;
2212 /* Primary or extended partition */
2214 if (List
->CurrentPartition
->IsPartitioned
== TRUE
&&
2215 IsContainerPartition(List
->CurrentPartition
->PartitionType
))
2217 /* First logical partition */
2218 PartListEntry
= List
->CurrentDisk
->LogicalPartListHead
.Flink
;
2219 if (PartListEntry
!= &List
->CurrentDisk
->LogicalPartListHead
)
2221 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2223 List
->CurrentPartition
= PartEntry
;
2229 /* Next primary partition */
2230 PartListEntry
= List
->CurrentPartition
->ListEntry
.Flink
;
2231 if (PartListEntry
!= &List
->CurrentDisk
->PrimaryPartListHead
)
2233 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2235 List
->CurrentPartition
= PartEntry
;
2242 /* Search for the first partition entry on the next disk */
2243 DiskListEntry
= List
->CurrentDisk
->ListEntry
.Flink
;
2244 while (DiskListEntry
!= &List
->DiskListHead
)
2246 DiskEntry
= CONTAINING_RECORD(DiskListEntry
, DISKENTRY
, ListEntry
);
2248 PartListEntry
= DiskEntry
->PrimaryPartListHead
.Flink
;
2249 if (PartListEntry
!= &DiskEntry
->PrimaryPartListHead
)
2251 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2253 List
->CurrentDisk
= DiskEntry
;
2254 List
->CurrentPartition
= PartEntry
;
2258 DiskListEntry
= DiskListEntry
->Flink
;
2266 ScrollUpPartitionList(
2269 PLIST_ENTRY DiskListEntry
;
2270 PLIST_ENTRY PartListEntry
;
2271 PDISKENTRY DiskEntry
;
2272 PPARTENTRY PartEntry
;
2274 /* Fail, if no disks are available */
2275 if (IsListEmpty(&List
->DiskListHead
))
2278 /* Check for previous usable entry on current disk */
2279 if (List
->CurrentPartition
!= NULL
)
2281 if (List
->CurrentPartition
->LogicalPartition
)
2283 /* Logical partition */
2284 PartListEntry
= List
->CurrentPartition
->ListEntry
.Blink
;
2285 if (PartListEntry
!= &List
->CurrentDisk
->LogicalPartListHead
)
2287 /* Previous logical partition */
2288 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2292 /* Extended partition*/
2293 PartEntry
= List
->CurrentDisk
->ExtendedPartition
;
2296 List
->CurrentPartition
= PartEntry
;
2301 /* Primary or extended partition */
2303 PartListEntry
= List
->CurrentPartition
->ListEntry
.Blink
;
2304 if (PartListEntry
!= &List
->CurrentDisk
->PrimaryPartListHead
)
2306 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2308 if (PartEntry
->IsPartitioned
== TRUE
&&
2309 IsContainerPartition(PartEntry
->PartitionType
))
2311 PartListEntry
= List
->CurrentDisk
->LogicalPartListHead
.Blink
;
2312 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2315 List
->CurrentPartition
= PartEntry
;
2322 /* Search for the last partition entry on the previous disk */
2323 DiskListEntry
= List
->CurrentDisk
->ListEntry
.Blink
;
2324 while (DiskListEntry
!= &List
->DiskListHead
)
2326 DiskEntry
= CONTAINING_RECORD(DiskListEntry
, DISKENTRY
, ListEntry
);
2328 PartListEntry
= DiskEntry
->PrimaryPartListHead
.Blink
;
2329 if (PartListEntry
!= &DiskEntry
->PrimaryPartListHead
)
2331 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2333 if (PartEntry
->IsPartitioned
== TRUE
&&
2334 IsContainerPartition(PartEntry
->PartitionType
))
2336 PartListEntry
= DiskEntry
->LogicalPartListHead
.Blink
;
2337 if (PartListEntry
!= &DiskEntry
->LogicalPartListHead
)
2339 PartEntry
= CONTAINING_RECORD(PartListEntry
, PARTENTRY
, ListEntry
);
2341 List
->CurrentDisk
= DiskEntry
;
2342 List
->CurrentPartition
= PartEntry
;
2348 List
->CurrentDisk
= DiskEntry
;
2349 List
->CurrentPartition
= PartEntry
;
2354 DiskListEntry
= DiskListEntry
->Blink
;
2364 PPARTITION_INFORMATION PartitionInfo
)
2366 if (PartitionInfo
->StartingOffset
.QuadPart
== 0 &&
2367 PartitionInfo
->PartitionLength
.QuadPart
== 0)
2376 IsSamePrimaryLayoutEntry(
2377 IN PPARTITION_INFORMATION PartitionInfo
,
2378 IN PDISKENTRY DiskEntry
,
2379 IN PPARTENTRY PartEntry
)
2381 if (PartitionInfo
->StartingOffset
.QuadPart
== PartEntry
->StartSector
.QuadPart
* DiskEntry
->BytesPerSector
&&
2382 PartitionInfo
->PartitionLength
.QuadPart
== PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
)
2383 // PartitionInfo->PartitionNumber = PartEntry->PartitionNumber &&
2384 // PartitionInfo->PartitionType == PartEntry->PartitionType
2393 GetPrimaryPartitionCount(
2394 IN PDISKENTRY DiskEntry
)
2397 PPARTENTRY PartEntry
;
2400 Entry
= DiskEntry
->PrimaryPartListHead
.Flink
;
2401 while (Entry
!= &DiskEntry
->PrimaryPartListHead
)
2403 PartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
2404 if (PartEntry
->IsPartitioned
== TRUE
)
2407 Entry
= Entry
->Flink
;
2416 GetLogicalPartitionCount(
2417 PDISKENTRY DiskEntry
)
2419 PLIST_ENTRY ListEntry
;
2420 PPARTENTRY PartEntry
;
2423 ListEntry
= DiskEntry
->LogicalPartListHead
.Flink
;
2424 while (ListEntry
!= &DiskEntry
->LogicalPartListHead
)
2426 PartEntry
= CONTAINING_RECORD(ListEntry
, PARTENTRY
, ListEntry
);
2427 if (PartEntry
->IsPartitioned
)
2430 ListEntry
= ListEntry
->Flink
;
2439 ReAllocateLayoutBuffer(
2440 PDISKENTRY DiskEntry
)
2442 PDRIVE_LAYOUT_INFORMATION NewLayoutBuffer
;
2443 ULONG NewPartitionCount
;
2444 ULONG CurrentPartitionCount
= 0;
2445 ULONG LayoutBufferSize
;
2448 DPRINT1("ReAllocateLayoutBuffer()\n");
2450 NewPartitionCount
= 4 + GetLogicalPartitionCount(DiskEntry
) * 4;
2452 if (DiskEntry
->LayoutBuffer
)
2453 CurrentPartitionCount
= DiskEntry
->LayoutBuffer
->PartitionCount
;
2455 DPRINT1("CurrentPartitionCount: %lu NewPartitionCount: %lu\n",
2456 CurrentPartitionCount
, NewPartitionCount
);
2458 if (CurrentPartitionCount
== NewPartitionCount
)
2461 LayoutBufferSize
= sizeof(DRIVE_LAYOUT_INFORMATION
) +
2462 ((NewPartitionCount
- ANYSIZE_ARRAY
) * sizeof(PARTITION_INFORMATION
));
2463 NewLayoutBuffer
= RtlReAllocateHeap(ProcessHeap
,
2465 DiskEntry
->LayoutBuffer
,
2467 if (NewLayoutBuffer
== NULL
)
2469 DPRINT1("Failed to allocate the new layout buffer (size: %lu)\n", LayoutBufferSize
);
2473 /* If the layout buffer grows, make sure the new (empty) entries are written to the disk */
2474 if (NewPartitionCount
> CurrentPartitionCount
)
2476 for (i
= CurrentPartitionCount
; i
< NewPartitionCount
; i
++)
2477 NewLayoutBuffer
->PartitionEntry
[i
].RewritePartition
= TRUE
;
2480 DiskEntry
->LayoutBuffer
= NewLayoutBuffer
;
2481 DiskEntry
->LayoutBuffer
->PartitionCount
= NewPartitionCount
;
2490 IN PDISKENTRY DiskEntry
)
2492 PPARTITION_INFORMATION PartitionInfo
;
2493 PPARTITION_INFORMATION LinkInfo
= NULL
;
2494 PLIST_ENTRY ListEntry
;
2495 PPARTENTRY PartEntry
;
2496 LARGE_INTEGER HiddenSectors64
;
2498 ULONG PartitionNumber
= 1;
2500 DPRINT1("UpdateDiskLayout()\n");
2502 /* Resize the layout buffer if necessary */
2503 if (ReAllocateLayoutBuffer(DiskEntry
) == FALSE
)
2505 DPRINT("ReAllocateLayoutBuffer() failed.\n");
2509 /* Update the primary partition table */
2511 ListEntry
= DiskEntry
->PrimaryPartListHead
.Flink
;
2512 while (ListEntry
!= &DiskEntry
->PrimaryPartListHead
)
2514 PartEntry
= CONTAINING_RECORD(ListEntry
, PARTENTRY
, ListEntry
);
2516 if (PartEntry
->IsPartitioned
== TRUE
)
2518 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[Index
];
2520 if (!IsSamePrimaryLayoutEntry(PartitionInfo
, DiskEntry
, PartEntry
))
2522 DPRINT1("Updating primary partition entry %lu\n", Index
);
2524 PartitionInfo
->StartingOffset
.QuadPart
= PartEntry
->StartSector
.QuadPart
* DiskEntry
->BytesPerSector
;
2525 PartitionInfo
->PartitionLength
.QuadPart
= PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
2526 PartitionInfo
->HiddenSectors
= PartEntry
->StartSector
.LowPart
;
2527 PartitionInfo
->PartitionNumber
= (!IsContainerPartition(PartEntry
->PartitionType
)) ? PartitionNumber
: 0;
2528 PartitionInfo
->PartitionType
= PartEntry
->PartitionType
;
2529 PartitionInfo
->BootIndicator
= PartEntry
->BootIndicator
;
2530 PartitionInfo
->RecognizedPartition
= FALSE
;
2531 PartitionInfo
->RewritePartition
= TRUE
;
2534 PartEntry
->PartitionNumber
= (!IsContainerPartition(PartEntry
->PartitionType
)) ? PartitionNumber
: 0;
2535 PartEntry
->PartitionIndex
= Index
;
2537 if (!IsContainerPartition(PartEntry
->PartitionType
))
2543 ListEntry
= ListEntry
->Flink
;
2546 /* Update the logical partition tables */
2548 ListEntry
= DiskEntry
->LogicalPartListHead
.Flink
;
2549 while (ListEntry
!= &DiskEntry
->LogicalPartListHead
)
2551 PartEntry
= CONTAINING_RECORD(ListEntry
, PARTENTRY
, ListEntry
);
2553 if (PartEntry
->IsPartitioned
)
2555 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[Index
];
2557 DPRINT1("Updating logical partition entry %lu\n", Index
);
2559 PartitionInfo
->StartingOffset
.QuadPart
= PartEntry
->StartSector
.QuadPart
* DiskEntry
->BytesPerSector
;
2560 PartitionInfo
->PartitionLength
.QuadPart
= PartEntry
->SectorCount
.QuadPart
* DiskEntry
->BytesPerSector
;
2561 PartitionInfo
->HiddenSectors
= DiskEntry
->SectorAlignment
;
2562 PartitionInfo
->PartitionNumber
= PartitionNumber
;
2563 PartitionInfo
->PartitionType
= PartEntry
->PartitionType
;
2564 PartitionInfo
->BootIndicator
= FALSE
;
2565 PartitionInfo
->RecognizedPartition
= FALSE
;
2566 PartitionInfo
->RewritePartition
= TRUE
;
2568 PartEntry
->PartitionNumber
= PartitionNumber
;
2569 PartEntry
->PartitionIndex
= Index
;
2571 /* Fill the link entry of the previous partition table */
2572 if (LinkInfo
!= NULL
)
2574 LinkInfo
->StartingOffset
.QuadPart
= (PartEntry
->StartSector
.QuadPart
- DiskEntry
->SectorAlignment
) * DiskEntry
->BytesPerSector
;
2575 LinkInfo
->PartitionLength
.QuadPart
= (PartEntry
->StartSector
.QuadPart
+ DiskEntry
->SectorAlignment
) * DiskEntry
->BytesPerSector
;
2576 HiddenSectors64
.QuadPart
= PartEntry
->StartSector
.QuadPart
- DiskEntry
->SectorAlignment
- DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
;
2577 LinkInfo
->HiddenSectors
= HiddenSectors64
.LowPart
;
2578 LinkInfo
->PartitionNumber
= 0;
2579 LinkInfo
->PartitionType
= PARTITION_EXTENDED
;
2580 LinkInfo
->BootIndicator
= FALSE
;
2581 LinkInfo
->RecognizedPartition
= FALSE
;
2582 LinkInfo
->RewritePartition
= TRUE
;
2585 /* Save a pointer to the link entry of the current partition table */
2586 LinkInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[Index
+ 1];
2592 ListEntry
= ListEntry
->Flink
;
2595 /* Wipe unused primary partition table entries */
2596 for (Index
= GetPrimaryPartitionCount(DiskEntry
); Index
< 4; Index
++)
2598 DPRINT1("Primary partition entry %lu\n", Index
);
2600 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[Index
];
2602 if (!IsEmptyLayoutEntry(PartitionInfo
))
2604 DPRINT1("Wiping primary partition entry %lu\n", Index
);
2606 PartitionInfo
->StartingOffset
.QuadPart
= 0;
2607 PartitionInfo
->PartitionLength
.QuadPart
= 0;
2608 PartitionInfo
->HiddenSectors
= 0;
2609 PartitionInfo
->PartitionNumber
= 0;
2610 PartitionInfo
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2611 PartitionInfo
->BootIndicator
= FALSE
;
2612 PartitionInfo
->RecognizedPartition
= FALSE
;
2613 PartitionInfo
->RewritePartition
= TRUE
;
2617 /* Wipe unused logical partition table entries */
2618 for (Index
= 4; Index
< DiskEntry
->LayoutBuffer
->PartitionCount
; Index
++)
2622 DPRINT1("Logical partition entry %lu\n", Index
);
2624 PartitionInfo
= &DiskEntry
->LayoutBuffer
->PartitionEntry
[Index
];
2626 if (!IsEmptyLayoutEntry(PartitionInfo
))
2628 DPRINT1("Wiping partition entry %lu\n", Index
);
2630 PartitionInfo
->StartingOffset
.QuadPart
= 0;
2631 PartitionInfo
->PartitionLength
.QuadPart
= 0;
2632 PartitionInfo
->HiddenSectors
= 0;
2633 PartitionInfo
->PartitionNumber
= 0;
2634 PartitionInfo
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2635 PartitionInfo
->BootIndicator
= FALSE
;
2636 PartitionInfo
->RecognizedPartition
= FALSE
;
2637 PartitionInfo
->RewritePartition
= TRUE
;
2642 #ifdef DUMP_PARTITION_TABLE
2643 DumpPartitionTable(DiskEntry
);
2650 GetPrevUnpartitionedEntry(
2651 PDISKENTRY DiskEntry
,
2652 PPARTENTRY PartEntry
)
2654 PPARTENTRY PrevPartEntry
;
2655 PLIST_ENTRY ListHead
;
2657 if (PartEntry
->LogicalPartition
)
2658 ListHead
= &DiskEntry
->LogicalPartListHead
;
2660 ListHead
= &DiskEntry
->PrimaryPartListHead
;
2662 if (PartEntry
->ListEntry
.Blink
!= ListHead
)
2664 PrevPartEntry
= CONTAINING_RECORD(PartEntry
->ListEntry
.Blink
,
2667 if (PrevPartEntry
->IsPartitioned
== FALSE
)
2668 return PrevPartEntry
;
2677 GetNextUnpartitionedEntry(
2678 PDISKENTRY DiskEntry
,
2679 PPARTENTRY PartEntry
)
2681 PPARTENTRY NextPartEntry
;
2682 PLIST_ENTRY ListHead
;
2684 if (PartEntry
->LogicalPartition
)
2685 ListHead
= &DiskEntry
->LogicalPartListHead
;
2687 ListHead
= &DiskEntry
->PrimaryPartListHead
;
2689 if (PartEntry
->ListEntry
.Flink
!= ListHead
)
2691 NextPartEntry
= CONTAINING_RECORD(PartEntry
->ListEntry
.Flink
,
2694 if (NextPartEntry
->IsPartitioned
== FALSE
)
2695 return NextPartEntry
;
2703 CreatePrimaryPartition(
2705 ULONGLONG SectorCount
,
2708 PDISKENTRY DiskEntry
;
2709 PPARTENTRY PartEntry
;
2710 PPARTENTRY NewPartEntry
;
2712 DPRINT1("CreatePrimaryPartition(%I64u)\n", SectorCount
);
2715 List
->CurrentDisk
== NULL
||
2716 List
->CurrentPartition
== NULL
||
2717 List
->CurrentPartition
->IsPartitioned
== TRUE
)
2722 DiskEntry
= List
->CurrentDisk
;
2723 PartEntry
= List
->CurrentPartition
;
2725 DPRINT1("Current partition sector count: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2727 if (AutoCreate
== TRUE
||
2728 AlignDown(PartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) - PartEntry
->StartSector
.QuadPart
== PartEntry
->SectorCount
.QuadPart
)
2730 DPRINT1("Convert existing partition entry\n");
2732 /* Convert current entry to 'new (unformatted)' */
2733 PartEntry
->IsPartitioned
= TRUE
;
2734 PartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2735 PartEntry
->FormatState
= Unformatted
;
2736 PartEntry
->AutoCreate
= AutoCreate
;
2737 PartEntry
->New
= TRUE
;
2738 PartEntry
->BootIndicator
= FALSE
;
2740 DPRINT1("First Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
);
2741 DPRINT1("Last Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
+ PartEntry
->SectorCount
.QuadPart
- 1);
2742 DPRINT1("Total Sectors: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2746 DPRINT1("Add new partition entry\n");
2748 /* Insert and initialize a new partition entry */
2749 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
2752 if (NewPartEntry
== NULL
)
2755 /* Insert the new entry into the list */
2756 InsertTailList(&PartEntry
->ListEntry
,
2757 &NewPartEntry
->ListEntry
);
2759 NewPartEntry
->DiskEntry
= DiskEntry
;
2761 NewPartEntry
->IsPartitioned
= TRUE
;
2762 NewPartEntry
->StartSector
.QuadPart
= PartEntry
->StartSector
.QuadPart
;
2763 NewPartEntry
->SectorCount
.QuadPart
= AlignDown(NewPartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) -
2764 NewPartEntry
->StartSector
.QuadPart
;
2765 NewPartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2767 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
2768 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
2769 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
2771 NewPartEntry
->New
= TRUE
;
2772 NewPartEntry
->FormatState
= Unformatted
;
2773 NewPartEntry
->BootIndicator
= FALSE
;
2775 PartEntry
->StartSector
.QuadPart
= NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
;
2776 PartEntry
->SectorCount
.QuadPart
-= (PartEntry
->StartSector
.QuadPart
- NewPartEntry
->StartSector
.QuadPart
);
2779 UpdateDiskLayout(DiskEntry
);
2781 DiskEntry
->Dirty
= TRUE
;
2783 AssignDriveLetters(List
);
2789 AddLogicalDiskSpace(
2790 PDISKENTRY DiskEntry
)
2792 PPARTENTRY NewPartEntry
;
2794 DPRINT1("AddLogicalDiskSpace()\n");
2796 /* Create a partition table entry that represents the empty space in the container partition */
2797 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
2800 if (NewPartEntry
== NULL
)
2803 NewPartEntry
->DiskEntry
= DiskEntry
;
2804 NewPartEntry
->LogicalPartition
= TRUE
;
2806 NewPartEntry
->IsPartitioned
= FALSE
;
2807 NewPartEntry
->StartSector
.QuadPart
= DiskEntry
->ExtendedPartition
->StartSector
.QuadPart
+ (ULONGLONG
)DiskEntry
->SectorAlignment
;
2808 NewPartEntry
->SectorCount
.QuadPart
= DiskEntry
->ExtendedPartition
->SectorCount
.QuadPart
- (ULONGLONG
)DiskEntry
->SectorAlignment
;
2810 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
2811 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
2812 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
2814 NewPartEntry
->FormatState
= Unformatted
;
2816 InsertTailList(&DiskEntry
->LogicalPartListHead
,
2817 &NewPartEntry
->ListEntry
);
2822 CreateExtendedPartition(
2824 ULONGLONG SectorCount
)
2826 PDISKENTRY DiskEntry
;
2827 PPARTENTRY PartEntry
;
2828 PPARTENTRY NewPartEntry
;
2830 DPRINT1("CreateExtendedPartition(%I64u)\n", SectorCount
);
2833 List
->CurrentDisk
== NULL
||
2834 List
->CurrentPartition
== NULL
||
2835 List
->CurrentPartition
->IsPartitioned
== TRUE
)
2840 DiskEntry
= List
->CurrentDisk
;
2841 PartEntry
= List
->CurrentPartition
;
2843 DPRINT1("Current partition sector count: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2845 if (AlignDown(PartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) - PartEntry
->StartSector
.QuadPart
== PartEntry
->SectorCount
.QuadPart
)
2847 DPRINT1("Convert existing partition entry\n");
2849 /* Convert current entry to 'new (unformatted)' */
2850 PartEntry
->IsPartitioned
= TRUE
;
2851 PartEntry
->FormatState
= Formatted
;
2852 PartEntry
->AutoCreate
= FALSE
;
2853 PartEntry
->New
= FALSE
;
2854 PartEntry
->BootIndicator
= FALSE
;
2856 if (PartEntry
->StartSector
.QuadPart
< 1450560)
2858 /* Partition starts below the 8.4GB boundary ==> CHS partition */
2859 PartEntry
->PartitionType
= PARTITION_EXTENDED
;
2863 /* Partition starts above the 8.4GB boundary ==> LBA partition */
2864 PartEntry
->PartitionType
= PARTITION_XINT13_EXTENDED
;
2867 DiskEntry
->ExtendedPartition
= PartEntry
;
2869 DPRINT1("First Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
);
2870 DPRINT1("Last Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
+ PartEntry
->SectorCount
.QuadPart
- 1);
2871 DPRINT1("Total Sectors: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2875 DPRINT1("Add new partition entry\n");
2877 /* Insert and initialize a new partition entry */
2878 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
2881 if (NewPartEntry
== NULL
)
2884 /* Insert the new entry into the list */
2885 InsertTailList(&PartEntry
->ListEntry
,
2886 &NewPartEntry
->ListEntry
);
2888 NewPartEntry
->DiskEntry
= DiskEntry
;
2890 NewPartEntry
->IsPartitioned
= TRUE
;
2891 NewPartEntry
->StartSector
.QuadPart
= PartEntry
->StartSector
.QuadPart
;
2892 NewPartEntry
->SectorCount
.QuadPart
= AlignDown(NewPartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) -
2893 NewPartEntry
->StartSector
.QuadPart
;
2895 NewPartEntry
->New
= FALSE
;
2896 NewPartEntry
->FormatState
= Formatted
;
2897 NewPartEntry
->BootIndicator
= FALSE
;
2899 if (NewPartEntry
->StartSector
.QuadPart
< 1450560)
2901 /* Partition starts below the 8.4GB boundary ==> CHS partition */
2902 NewPartEntry
->PartitionType
= PARTITION_EXTENDED
;
2906 /* Partition starts above the 8.4GB boundary ==> LBA partition */
2907 NewPartEntry
->PartitionType
= PARTITION_XINT13_EXTENDED
;
2910 DiskEntry
->ExtendedPartition
= NewPartEntry
;
2912 PartEntry
->StartSector
.QuadPart
= NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
;
2913 PartEntry
->SectorCount
.QuadPart
-= (PartEntry
->StartSector
.QuadPart
- NewPartEntry
->StartSector
.QuadPart
);
2915 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
2916 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
2917 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
2920 AddLogicalDiskSpace(DiskEntry
);
2922 UpdateDiskLayout(DiskEntry
);
2924 DiskEntry
->Dirty
= TRUE
;
2926 AssignDriveLetters(List
);
2931 CreateLogicalPartition(
2933 ULONGLONG SectorCount
,
2936 PDISKENTRY DiskEntry
;
2937 PPARTENTRY PartEntry
;
2938 PPARTENTRY NewPartEntry
;
2940 DPRINT1("CreateLogicalPartition(%I64u)\n", SectorCount
);
2943 List
->CurrentDisk
== NULL
||
2944 List
->CurrentPartition
== NULL
||
2945 List
->CurrentPartition
->IsPartitioned
== TRUE
)
2950 DiskEntry
= List
->CurrentDisk
;
2951 PartEntry
= List
->CurrentPartition
;
2953 DPRINT1("Current partition sector count: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2955 if (AutoCreate
== TRUE
||
2956 AlignDown(PartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) - PartEntry
->StartSector
.QuadPart
== PartEntry
->SectorCount
.QuadPart
)
2958 DPRINT1("Convert existing partition entry\n");
2960 /* Convert current entry to 'new (unformatted)' */
2961 PartEntry
->IsPartitioned
= TRUE
;
2962 PartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2963 PartEntry
->FormatState
= Unformatted
;
2964 PartEntry
->AutoCreate
= FALSE
;
2965 PartEntry
->New
= TRUE
;
2966 PartEntry
->BootIndicator
= FALSE
;
2967 PartEntry
->LogicalPartition
= TRUE
;
2969 DPRINT1("First Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
);
2970 DPRINT1("Last Sector: %I64u\n", PartEntry
->StartSector
.QuadPart
+ PartEntry
->SectorCount
.QuadPart
- 1);
2971 DPRINT1("Total Sectors: %I64u\n", PartEntry
->SectorCount
.QuadPart
);
2975 DPRINT1("Add new partition entry\n");
2977 /* Insert and initialize a new partition entry */
2978 NewPartEntry
= RtlAllocateHeap(ProcessHeap
,
2981 if (NewPartEntry
== NULL
)
2984 /* Insert the new entry into the list */
2985 InsertTailList(&PartEntry
->ListEntry
,
2986 &NewPartEntry
->ListEntry
);
2988 NewPartEntry
->DiskEntry
= DiskEntry
;
2990 NewPartEntry
->IsPartitioned
= TRUE
;
2991 NewPartEntry
->StartSector
.QuadPart
= PartEntry
->StartSector
.QuadPart
;
2992 NewPartEntry
->SectorCount
.QuadPart
= AlignDown(NewPartEntry
->StartSector
.QuadPart
+ SectorCount
, DiskEntry
->SectorAlignment
) -
2993 NewPartEntry
->StartSector
.QuadPart
;
2994 NewPartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
2996 DPRINT1("First Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
);
2997 DPRINT1("Last Sector: %I64u\n", NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
- 1);
2998 DPRINT1("Total Sectors: %I64u\n", NewPartEntry
->SectorCount
.QuadPart
);
3000 NewPartEntry
->New
= TRUE
;
3001 NewPartEntry
->FormatState
= Unformatted
;
3002 NewPartEntry
->BootIndicator
= FALSE
;
3003 NewPartEntry
->LogicalPartition
= TRUE
;
3005 PartEntry
->StartSector
.QuadPart
= NewPartEntry
->StartSector
.QuadPart
+ NewPartEntry
->SectorCount
.QuadPart
;
3006 PartEntry
->SectorCount
.QuadPart
-= (PartEntry
->StartSector
.QuadPart
- NewPartEntry
->StartSector
.QuadPart
);
3009 UpdateDiskLayout(DiskEntry
);
3011 DiskEntry
->Dirty
= TRUE
;
3013 AssignDriveLetters(List
);
3018 DeleteCurrentPartition(
3021 PDISKENTRY DiskEntry
;
3022 PPARTENTRY PartEntry
;
3023 PPARTENTRY PrevPartEntry
;
3024 PPARTENTRY NextPartEntry
;
3025 PPARTENTRY LogicalPartEntry
;
3029 List
->CurrentDisk
== NULL
||
3030 List
->CurrentPartition
== NULL
||
3031 List
->CurrentPartition
->IsPartitioned
== FALSE
)
3036 DiskEntry
= List
->CurrentDisk
;
3037 PartEntry
= List
->CurrentPartition
;
3039 /* Delete all logical partition entries if an extended partition will be deleted */
3040 if (DiskEntry
->ExtendedPartition
== PartEntry
)
3042 while (!IsListEmpty(&DiskEntry
->LogicalPartListHead
))
3044 Entry
= RemoveHeadList(&DiskEntry
->LogicalPartListHead
);
3045 LogicalPartEntry
= CONTAINING_RECORD(Entry
, PARTENTRY
, ListEntry
);
3047 RtlFreeHeap(ProcessHeap
, 0, LogicalPartEntry
);
3050 DiskEntry
->ExtendedPartition
= NULL
;
3053 /* Adjust unpartitioned disk space entries */
3055 /* Get pointer to previous and next unpartitioned entries */
3056 PrevPartEntry
= GetPrevUnpartitionedEntry(DiskEntry
,
3059 NextPartEntry
= GetNextUnpartitionedEntry(DiskEntry
,
3062 if (PrevPartEntry
!= NULL
&& NextPartEntry
!= NULL
)
3064 /* Merge previous, current and next unpartitioned entry */
3066 /* Adjust the previous entries length */
3067 PrevPartEntry
->SectorCount
.QuadPart
+= (PartEntry
->SectorCount
.QuadPart
+ NextPartEntry
->SectorCount
.QuadPart
);
3069 /* Remove the current entry */
3070 RemoveEntryList(&PartEntry
->ListEntry
);
3071 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
3073 /* Remove the next entry */
3074 RemoveEntryList (&NextPartEntry
->ListEntry
);
3075 RtlFreeHeap(ProcessHeap
, 0, NextPartEntry
);
3077 /* Update current partition */
3078 List
->CurrentPartition
= PrevPartEntry
;
3080 else if (PrevPartEntry
!= NULL
&& NextPartEntry
== NULL
)
3082 /* Merge current and previous unpartitioned entry */
3084 /* Adjust the previous entries length */
3085 PrevPartEntry
->SectorCount
.QuadPart
+= PartEntry
->SectorCount
.QuadPart
;
3087 /* Remove the current entry */
3088 RemoveEntryList(&PartEntry
->ListEntry
);
3089 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
3091 /* Update current partition */
3092 List
->CurrentPartition
= PrevPartEntry
;
3094 else if (PrevPartEntry
== NULL
&& NextPartEntry
!= NULL
)
3096 /* Merge current and next unpartitioned entry */
3098 /* Adjust the next entries offset and length */
3099 NextPartEntry
->StartSector
.QuadPart
= PartEntry
->StartSector
.QuadPart
;
3100 NextPartEntry
->SectorCount
.QuadPart
+= PartEntry
->SectorCount
.QuadPart
;
3102 /* Remove the current entry */
3103 RemoveEntryList(&PartEntry
->ListEntry
);
3104 RtlFreeHeap(ProcessHeap
, 0, PartEntry
);
3106 /* Update current partition */
3107 List
->CurrentPartition
= NextPartEntry
;
3111 /* Nothing to merge but change current entry */
3112 PartEntry
->IsPartitioned
= FALSE
;
3113 PartEntry
->PartitionType
= PARTITION_ENTRY_UNUSED
;
3114 PartEntry
->FormatState
= Unformatted
;
3115 PartEntry
->DriveLetter
= 0;
3118 UpdateDiskLayout(DiskEntry
);
3120 DiskEntry
->Dirty
= TRUE
;
3122 AssignDriveLetters(List
);
3127 CheckActiveSystemPartition(
3130 PDISKENTRY DiskEntry
;
3131 PPARTENTRY PartEntry
;
3132 PLIST_ENTRY ListEntry
;
3134 /* Check for empty disk list */
3135 if (IsListEmpty(&List
->DiskListHead
))
3137 List
->SystemDisk
= NULL
;
3138 List
->SystemPartition
= NULL
;
3143 if (List
->SystemDisk
!= NULL
&&
3144 List
->SystemPartition
!= NULL
)
3146 /* We already have an active system partition */
3151 /* Choose the currently selected disk */
3152 DiskEntry
= List
->CurrentDisk
;
3154 /* Check for empty partition list */
3155 if (IsListEmpty(&DiskEntry
->PrimaryPartListHead
))
3157 List
->SystemDisk
= NULL
;
3158 List
->SystemPartition
= NULL
;
3163 * Check the first partition of the disk in case it is fresh new,
3164 * and if so, use it as the system partition.
3167 PartEntry
= CONTAINING_RECORD(DiskEntry
->PrimaryPartListHead
.Flink
,
3171 /* Set active system partition */
3172 if ((DiskEntry
->NewDisk
== TRUE
) ||
3173 (PartEntry
->BootIndicator
== FALSE
))
3175 PartEntry
->BootIndicator
= TRUE
;
3176 DiskEntry
->LayoutBuffer
->PartitionEntry
[PartEntry
->PartitionIndex
].BootIndicator
= TRUE
;
3177 DiskEntry
->LayoutBuffer
->PartitionEntry
[PartEntry
->PartitionIndex
].RewritePartition
= TRUE
;
3178 DiskEntry
->Dirty
= TRUE
;
3180 /* FIXME: Might be incorrect if partitions were created by Linux FDISK */
3181 List
->SystemDisk
= DiskEntry
;
3182 List
->SystemPartition
= PartEntry
;
3187 /* Disk is not new, scan all partitions to find a bootable one */
3188 List
->SystemDisk
= NULL
;
3189 List
->SystemPartition
= NULL
;
3191 ListEntry
= DiskEntry
->PrimaryPartListHead
.Flink
;
3192 while (ListEntry
!= &DiskEntry
->PrimaryPartListHead
)
3194 PartEntry
= CONTAINING_RECORD(ListEntry
,
3198 /* Check if it is partitioned */
3199 if (PartEntry
->IsPartitioned
)
3201 if (PartEntry
->PartitionType
!= PARTITION_ENTRY_UNUSED
&&
3202 PartEntry
->BootIndicator
)
3204 /* Yes, we found it */
3205 List
->SystemDisk
= DiskEntry
;
3206 List
->SystemPartition
= PartEntry
;
3208 DPRINT("Found bootable partition disk %d, drive letter %c\n",
3209 DiskEntry
->DiskNumber
, PartEntry
->DriveLetter
);
3214 /* Go to the next one */
3215 ListEntry
= ListEntry
->Flink
;
3224 IN PDISKENTRY DiskEntry
)
3226 WCHAR DstPath
[MAX_PATH
];
3227 OBJECT_ATTRIBUTES ObjectAttributes
;
3228 IO_STATUS_BLOCK Iosb
;
3229 UNICODE_STRING Name
;
3231 HANDLE FileHandle
= NULL
;
3234 DPRINT("WritePartitions() Disk: %lu\n", DiskEntry
->DiskNumber
);
3237 L
"\\Device\\Harddisk%d\\Partition0",
3238 DiskEntry
->DiskNumber
);
3239 RtlInitUnicodeString(&Name
,
3241 InitializeObjectAttributes(&ObjectAttributes
,
3247 Status
= NtOpenFile(&FileHandle
,
3248 GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
,
3252 FILE_SYNCHRONOUS_IO_NONALERT
);
3253 if (!NT_SUCCESS(Status
))
3255 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status
);
3259 #ifdef DUMP_PARTITION_TABLE
3260 DumpPartitionTable(DiskEntry
);
3263 BufferSize
= sizeof(DRIVE_LAYOUT_INFORMATION
) +
3264 ((DiskEntry
->LayoutBuffer
->PartitionCount
- 1) * sizeof(PARTITION_INFORMATION
));
3265 Status
= NtDeviceIoControlFile(FileHandle
,
3270 IOCTL_DISK_SET_DRIVE_LAYOUT
,
3271 DiskEntry
->LayoutBuffer
,
3275 if (!NT_SUCCESS(Status
))
3277 DPRINT1("IOCTL_DISK_SET_DRIVE_LAYOUT failed (Status 0x%08lx)\n", Status
);
3280 if (FileHandle
!= NULL
)
3281 NtClose(FileHandle
);
3288 WritePartitionsToDisk(
3292 PDISKENTRY DiskEntry
;
3297 Entry
= List
->DiskListHead
.Flink
;
3298 while (Entry
!= &List
->DiskListHead
)
3300 DiskEntry
= CONTAINING_RECORD(Entry
, DISKENTRY
, ListEntry
);
3302 if (DiskEntry
->Dirty
== TRUE
)
3304 WritePartitions(List
, DiskEntry
);
3305 DiskEntry
->Dirty
= FALSE
;
3308 Entry
= Entry
->Flink
;
3316 SetMountedDeviceValues(
3319 PLIST_ENTRY Entry1
, Entry2
;
3320 PDISKENTRY DiskEntry
;
3321 PPARTENTRY PartEntry
;
3322 LARGE_INTEGER StartingOffset
;
3329 Entry1
= List
->DiskListHead
.Flink
;
3330 while (Entry1
!= &List
->DiskListHead
)
3332 DiskEntry
= CONTAINING_RECORD(Entry1
,
3336 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
3337 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
3339 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3340 if (PartEntry
->IsPartitioned
)
3342 if (PartEntry
->DriveLetter
)
3344 StartingOffset
.QuadPart
= PartEntry
->StartSector
.QuadPart
* DiskEntry
->BytesPerSector
;
3345 if (!SetMountedDeviceValue(PartEntry
->DriveLetter
,
3346 DiskEntry
->LayoutBuffer
->Signature
,
3354 Entry2
= Entry2
->Flink
;
3357 Entry1
= Entry1
->Flink
;
3365 PrimaryPartitionCreationChecks(
3368 PDISKENTRY DiskEntry
;
3369 PPARTENTRY PartEntry
;
3371 DiskEntry
= List
->CurrentDisk
;
3372 PartEntry
= List
->CurrentPartition
;
3374 /* Fail if partition is already in use */
3375 if (PartEntry
->IsPartitioned
== TRUE
)
3376 return ERROR_NEW_PARTITION
;
3378 /* Fail if there are more than 4 partitions in the list */
3379 if (GetPrimaryPartitionCount(DiskEntry
) > 4)
3380 return ERROR_PARTITION_TABLE_FULL
;
3382 return ERROR_SUCCESS
;
3387 ExtendedPartitionCreationChecks(
3390 PDISKENTRY DiskEntry
;
3391 PPARTENTRY PartEntry
;
3393 DiskEntry
= List
->CurrentDisk
;
3394 PartEntry
= List
->CurrentPartition
;
3396 /* Fail if partition is already in use */
3397 if (PartEntry
->IsPartitioned
== TRUE
)
3398 return ERROR_NEW_PARTITION
;
3400 /* Fail if there are more than 4 partitions in the list */
3401 if (GetPrimaryPartitionCount(DiskEntry
) > 4)
3402 return ERROR_PARTITION_TABLE_FULL
;
3404 /* Fail if there is another extended partition in the list */
3405 if (DiskEntry
->ExtendedPartition
!= NULL
)
3406 return ERROR_ONLY_ONE_EXTENDED
;
3408 return ERROR_SUCCESS
;
3413 LogicalPartitionCreationChecks(
3416 // PDISKENTRY DiskEntry;
3417 PPARTENTRY PartEntry
;
3419 // DiskEntry = List->CurrentDisk;
3420 PartEntry
= List
->CurrentPartition
;
3422 /* Fail if partition is already in use */
3423 if (PartEntry
->IsPartitioned
== TRUE
)
3424 return ERROR_NEW_PARTITION
;
3426 return ERROR_SUCCESS
;
3431 GetNextUnformattedPartition(
3433 OUT PDISKENTRY
*pDiskEntry
,
3434 OUT PPARTENTRY
*pPartEntry
)
3436 PLIST_ENTRY Entry1
, Entry2
;
3437 PDISKENTRY DiskEntry
;
3438 PPARTENTRY PartEntry
;
3440 Entry1
= List
->DiskListHead
.Flink
;
3441 while (Entry1
!= &List
->DiskListHead
)
3443 DiskEntry
= CONTAINING_RECORD(Entry1
,
3447 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
3448 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
3450 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3451 if (PartEntry
->IsPartitioned
&& PartEntry
->New
)
3453 *pDiskEntry
= DiskEntry
;
3454 *pPartEntry
= PartEntry
;
3458 Entry2
= Entry2
->Flink
;
3461 Entry2
= DiskEntry
->LogicalPartListHead
.Flink
;
3462 while (Entry2
!= &DiskEntry
->LogicalPartListHead
)
3464 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3465 if (PartEntry
->IsPartitioned
&& PartEntry
->New
)
3467 *pDiskEntry
= DiskEntry
;
3468 *pPartEntry
= PartEntry
;
3472 Entry2
= Entry2
->Flink
;
3475 Entry1
= Entry1
->Flink
;
3486 GetNextUncheckedPartition(
3488 OUT PDISKENTRY
*pDiskEntry
,
3489 OUT PPARTENTRY
*pPartEntry
)
3491 PLIST_ENTRY Entry1
, Entry2
;
3492 PDISKENTRY DiskEntry
;
3493 PPARTENTRY PartEntry
;
3495 Entry1
= List
->DiskListHead
.Flink
;
3496 while (Entry1
!= &List
->DiskListHead
)
3498 DiskEntry
= CONTAINING_RECORD(Entry1
,
3502 Entry2
= DiskEntry
->PrimaryPartListHead
.Flink
;
3503 while (Entry2
!= &DiskEntry
->PrimaryPartListHead
)
3505 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3506 if (PartEntry
->NeedsCheck
== TRUE
)
3508 *pDiskEntry
= DiskEntry
;
3509 *pPartEntry
= PartEntry
;
3513 Entry2
= Entry2
->Flink
;
3516 Entry2
= DiskEntry
->LogicalPartListHead
.Flink
;
3517 while (Entry2
!= &DiskEntry
->LogicalPartListHead
)
3519 PartEntry
= CONTAINING_RECORD(Entry2
, PARTENTRY
, ListEntry
);
3520 if (PartEntry
->NeedsCheck
== TRUE
)
3522 *pDiskEntry
= DiskEntry
;
3523 *pPartEntry
= PartEntry
;
3527 Entry2
= Entry2
->Flink
;
3530 Entry1
= Entry1
->Flink
;