Skip to content

Commit f206063

Browse files
committed
[DISKPART] Implement creation and deletion of GPT partitions
- Implement creation and deletion of GPT partitions. - Adjust the active, clean, detail, inactive, list, select, setid and uniqueid commands as needed. - Add a 2^32 sector count limit for MBR partition tables.
1 parent 1a17364 commit f206063

29 files changed

+1497
-423
lines changed

base/system/diskpart/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ list(APPEND SOURCE
2424
filesystems.c
2525
format.c
2626
gpt.c
27+
guid.c
2728
help.c
2829
import.c
2930
inactive.c

base/system/diskpart/active.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ active_main(
3535

3636
if (CurrentDisk->PartitionStyle == PARTITION_STYLE_MBR)
3737
{
38-
if (CurrentPartition->BootIndicator)
38+
if (CurrentPartition->Mbr.BootIndicator)
3939
{
4040
ConResPuts(StdOut, IDS_ACTIVE_ALREADY);
4141
return TRUE;
4242
}
4343

44-
CurrentPartition->BootIndicator = TRUE;
44+
CurrentPartition->Mbr.BootIndicator = TRUE;
4545
CurrentDisk->Dirty = TRUE;
46-
UpdateDiskLayout(CurrentDisk);
47-
Status = WritePartitions(CurrentDisk);
46+
UpdateMbrDiskLayout(CurrentDisk);
47+
Status = WriteMbrPartitions(CurrentDisk);
4848
if (NT_SUCCESS(Status))
4949
{
5050
ConResPuts(StdOut, IDS_ACTIVE_SUCCESS);

base/system/diskpart/clean.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ clean_main(
6363
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
6464

6565
/* Dismount the logical partition */
66-
if (PartEntry->PartitionType != 0)
66+
if (PartEntry->Mbr.PartitionType != 0)
6767
{
6868
DismountVolume(PartEntry);
6969
VolumeEntry = GetVolumeFromPartition(PartEntry);
@@ -82,8 +82,8 @@ clean_main(
8282
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
8383

8484
/* Dismount the primary partition */
85-
if ((PartEntry->PartitionType != 0) &&
86-
(IsContainerPartition(PartEntry->PartitionType) == FALSE))
85+
if ((PartEntry->Mbr.PartitionType != 0) &&
86+
(IsContainerPartition(PartEntry->Mbr.PartitionType) == FALSE))
8787
{
8888
DismountVolume(PartEntry);
8989
VolumeEntry = GetVolumeFromPartition(PartEntry);

base/system/diskpart/convert.c

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ CreateDisk(
2323
IO_STATUS_BLOCK Iosb;
2424
NTSTATUS Status;
2525

26-
DPRINT1("CreateDisk(%lu %p)\n", DiskNumber, DiskInfo);
26+
DPRINT("CreateDisk(%lu %p)\n", DiskNumber, DiskInfo);
2727

2828
StringCchPrintfW(DstPath, ARRAYSIZE(DstPath),
2929
L"\\Device\\Harddisk%lu\\Partition0",
@@ -64,23 +64,17 @@ CreateDisk(
6464
goto done;
6565
}
6666

67-
CurrentDisk->PartitionStyle = DiskInfo->PartitionStyle;
68-
6967
/* Free the layout buffer */
7068
if (CurrentDisk->LayoutBuffer)
71-
{
7269
RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentDisk->LayoutBuffer);
73-
CurrentDisk->LayoutBuffer = NULL;
74-
CurrentDisk->ExtendedPartition = NULL;
75-
}
76-
77-
ReadLayoutBuffer(FileHandle, CurrentDisk);
7870

79-
DPRINT1("PartitionCount: %lu\n", CurrentDisk->LayoutBuffer->PartitionCount);
71+
CurrentDisk->LayoutBuffer = NULL;
72+
CurrentDisk->ExtendedPartition = NULL;
73+
CurrentDisk->Dirty = FALSE;
74+
CurrentDisk->NewDisk = TRUE;
75+
CurrentDisk->PartitionStyle = DiskInfo->PartitionStyle;
8076

81-
#ifdef DUMP_PARTITION_TABLE
82-
DumpPartitionTable(CurrentDisk);
83-
#endif
77+
ReadLayoutBuffer(FileHandle, CurrentDisk);
8478

8579
done:
8680
if (FileHandle)
@@ -112,7 +106,7 @@ ConvertGPT(
112106
return TRUE;
113107
}
114108

115-
if (CurrentDisk->LayoutBuffer->PartitionCount != 0)
109+
if (GetPrimaryPartitionCount(CurrentDisk) != 0)
116110
{
117111
ConResPuts(StdOut, IDS_CONVERT_GPT_NOT_EMPTY);
118112
return TRUE;
@@ -134,13 +128,18 @@ ConvertGPT(
134128
Status = CreateDisk(CurrentDisk->DiskNumber, &DiskInfo);
135129
if (!NT_SUCCESS(Status))
136130
{
137-
138-
}
139-
else
140-
{
141-
ConResPuts(StdOut, IDS_CONVERT_GPT_SUCCESS);
131+
DPRINT1("CreateDisk() failed!\n");
132+
return TRUE;
142133
}
143134

135+
CurrentDisk->StartSector.QuadPart = AlignDown(CurrentDisk->LayoutBuffer->Gpt.StartingUsableOffset.QuadPart / CurrentDisk->BytesPerSector,
136+
CurrentDisk->SectorAlignment) + (ULONGLONG)CurrentDisk->SectorAlignment;
137+
CurrentDisk->EndSector.QuadPart = AlignDown(CurrentDisk->StartSector.QuadPart + (CurrentDisk->LayoutBuffer->Gpt.UsableLength.QuadPart / CurrentDisk->BytesPerSector) - 1,
138+
CurrentDisk->SectorAlignment);
139+
140+
ScanForUnpartitionedGptDiskSpace(CurrentDisk);
141+
ConResPuts(StdOut, IDS_CONVERT_GPT_SUCCESS);
142+
144143
return TRUE;
145144
}
146145

@@ -168,7 +167,7 @@ ConvertMBR(
168167
return TRUE;
169168
}
170169

171-
if (CurrentDisk->LayoutBuffer->PartitionCount != 0)
170+
if (GetPrimaryPartitionCount(CurrentDisk) != 0)
172171
{
173172
ConResPuts(StdOut, IDS_CONVERT_MBR_NOT_EMPTY);
174173
return TRUE;
@@ -180,12 +179,15 @@ ConvertMBR(
180179
Status = CreateDisk(CurrentDisk->DiskNumber, &DiskInfo);
181180
if (!NT_SUCCESS(Status))
182181
{
183-
184-
}
185-
else
186-
{
187-
ConResPuts(StdOut, IDS_CONVERT_MBR_SUCCESS);
182+
DPRINT1("CreateDisk() failed!\n");
183+
return TRUE;
188184
}
189185

186+
CurrentDisk->StartSector.QuadPart = (ULONGLONG)CurrentDisk->SectorAlignment;
187+
CurrentDisk->EndSector.QuadPart = min(CurrentDisk->SectorCount.QuadPart, 0x100000000) - 1;
188+
189+
ScanForUnpartitionedMbrDiskSpace(CurrentDisk);
190+
ConResPuts(StdOut, IDS_CONVERT_MBR_SUCCESS);
191+
190192
return TRUE;
191193
}

0 commit comments

Comments
 (0)