[DISKPART] Prevent deletion of boot and system partitions

This commit is contained in:
Eric Kohl
2026-01-12 15:29:54 +01:00
parent fad471ec3e
commit a40bd103fc
15 changed files with 99 additions and 4 deletions

View File

@@ -324,6 +324,12 @@ DeletePartition(
}
}
if (CurrentPartition->IsBoot || CurrentPartition->IsSystem)
{
ConResPuts(StdOut, IDS_DELETE_PARTITION_SYSTEM);
return EXIT_SUCCESS;
}
if (CurrentDisk->PartitionStyle == PARTITION_STYLE_MBR)
{
DeleteMbrPartition(bOverride);

View File

@@ -109,7 +109,6 @@ typedef enum _FORMATSTATE
typedef enum _VOLUME_TYPE
{
VOLUME_TYPE_CDROM,
// VOLUME_TYPE_DVD,
VOLUME_TYPE_PARTITION,
VOLUME_TYPE_REMOVABLE,
VOLUME_TYPE_UNKNOWN
@@ -167,6 +166,10 @@ typedef struct _PARTENTRY
BOOLEAN NeedsCheck;
struct _FILE_SYSTEM_ITEM *FileSystem;
BOOL IsSystem;
BOOL IsBoot;
} PARTENTRY, *PPARTENTRY;

View File

@@ -69,7 +69,8 @@ END
STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDie gewählte Partition konnte nicht gelöscht werden.\nVergewissern Sie sich, dass die Partition wirklich gelöscht werden kann.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDer gewählte Partition wurde erfolgreich gelöscht.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDie gewählte Partition wurde erfolgreich gelöscht.\n"
IDS_DELETE_PARTITION_SYSTEM "\nDie gewählte Partition ist möglicherweise für den Betrieb des Computers erforderlich und kann nicht gelöscht werden.\n"
END
/* Disk Information Labels */

View File

@@ -70,6 +70,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart failed to delete the selected partition.\nPlease make sure the selected partition is valid to delete.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart successfully deleted the selected partition.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -77,6 +77,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart ha fallito ad eliminare la partizione selezionata.\nAssicurarsi che la partizione selezionata sia valida.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart ha eliminato con successo la partizione selezionata.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -70,6 +70,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart nie może usunąć wybranej partycji.\nUpewnij się, że wybrana partycja nadaje się do usunięcia.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart pomyślnie usunął wybraną partycję.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -72,6 +72,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart failed to delete the selected partition.\nPlease make sure the selected partition is valid to delete.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart successfully deleted the selected partition.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -78,6 +78,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart nu a reușit să șteargă partiția selectată.\nAsigurați-vă că partiția selectată este validă pentru ștergere.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart a șters cu succes partiția selectată.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -72,6 +72,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart failed to delete the selected partition.\nPlease make sure the selected partition is valid to delete.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart successfully deleted the selected partition.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -74,6 +74,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart failed to delete the selected partition.\nPlease make sure the selected partition is valid to delete.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart successfully deleted the selected partition.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -80,6 +80,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart, seçilen bölümü silemedi.\nLütfen seçilen bölümün silmek için geçerli olduğundan emin olun.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart seçilen bölümü başarıyla sildi.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -79,6 +79,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart failed to delete the selected partition.\nPlease make sure the selected partition is valid to delete.\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart successfully deleted the selected partition.\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -79,6 +79,7 @@ STRINGTABLE
BEGIN
IDS_DELETE_PARTITION_FAIL "\nDiskPart 無法刪除指定的磁碟分割。\n請確認已選擇的磁碟分割能夠正確地刪除。\n"
IDS_DELETE_PARTITION_SUCCESS "\nDiskPart 成功刪除指定的磁碟分割。\n"
IDS_DELETE_PARTITION_SYSTEM "\nThe selected partition may be neccessary to the operation of your computer, and may not be deleted.\n"
END
/* Disk Information Labels */

View File

@@ -1865,12 +1865,75 @@ GetDiskForVolume(
}
static
PPARTENTRY
GetPartitionForVolume(
_In_ PVOLENTRY VolumeEntry)
{
PLIST_ENTRY Entry1, Entry2;
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry;
INT i;
DPRINT("GetPartitionFromVolume(%p)\n", VolumeEntry);
DPRINT("Extents: %p\n", VolumeEntry->pExtents);
if (VolumeEntry->pExtents == NULL)
return NULL;
DPRINT("Extents: %lu\n", VolumeEntry->pExtents->NumberOfDiskExtents);
Entry1 = DiskListHead.Flink;
while (Entry1 != &DiskListHead)
{
DiskEntry = CONTAINING_RECORD(Entry1, DISKENTRY, ListEntry);
for (i = 0; i < VolumeEntry->pExtents->NumberOfDiskExtents; i++)
{
DPRINT("DiskNumber: %lu -- %lu\n", VolumeEntry->pExtents->Extents[i].DiskNumber, DiskEntry->DiskNumber);
if (VolumeEntry->pExtents->Extents[i].DiskNumber == DiskEntry->DiskNumber)
{
Entry2 = DiskEntry->PrimaryPartListHead.Flink;
while (Entry2 != &DiskEntry->PrimaryPartListHead)
{
PartEntry = CONTAINING_RECORD(Entry2, PARTENTRY, ListEntry);
if ((VolumeEntry->pExtents->Extents[i].StartingOffset.QuadPart == PartEntry->StartSector.QuadPart * PartEntry->DiskEntry->BytesPerSector) &&
(VolumeEntry->pExtents->Extents[i].ExtentLength.QuadPart == PartEntry->SectorCount.QuadPart * PartEntry->DiskEntry->BytesPerSector))
return PartEntry;
Entry2 = Entry2->Flink;
}
Entry2 = DiskEntry->LogicalPartListHead.Flink;
while (Entry2 != &DiskEntry->LogicalPartListHead)
{
PartEntry = CONTAINING_RECORD(Entry2, PARTENTRY, ListEntry);
if ((VolumeEntry->pExtents->Extents[i].StartingOffset.QuadPart == PartEntry->StartSector.QuadPart * PartEntry->DiskEntry->BytesPerSector) &&
(VolumeEntry->pExtents->Extents[i].ExtentLength.QuadPart == PartEntry->SectorCount.QuadPart * PartEntry->DiskEntry->BytesPerSector))
return PartEntry;
Entry2 = Entry2->Flink;
}
}
}
Entry1 = Entry1->Flink;
}
return NULL;
}
static
VOID
IsVolumeSystem(
_In_ PVOLENTRY VolumeEntry)
{
WCHAR szSystemPartition[MAX_PATH];
PPARTENTRY PartEntry;
HKEY hKey;
DWORD dwError, dwLength;
@@ -1904,11 +1967,17 @@ IsVolumeSystem(
return;
}
DPRINT1("SystemPartition: %S\n", szSystemPartition);
DPRINT1("DeviceName: %S\n", VolumeEntry->DeviceName);
DPRINT("SystemPartition: %S\n", szSystemPartition);
DPRINT("DeviceName: %S\n", VolumeEntry->DeviceName);
if (_wcsnicmp(szSystemPartition, VolumeEntry->DeviceName, wcslen(szSystemPartition)) == 0)
{
VolumeEntry->IsSystem = TRUE;
PartEntry = GetPartitionForVolume(VolumeEntry);
if (PartEntry)
PartEntry->IsSystem = TRUE;
}
}
@@ -1919,6 +1988,7 @@ IsVolumeBoot(
{
WCHAR szSystemDir[MAX_PATH];
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry;
DPRINT("IsVolumeBoot()\n");
@@ -1937,6 +2007,10 @@ IsVolumeBoot(
{
VolumeEntry->IsBoot = TRUE;
PartEntry = GetPartitionForVolume(VolumeEntry);
if (PartEntry)
PartEntry->IsBoot = TRUE;
DiskEntry = GetDiskForVolume(VolumeEntry);
if (DiskEntry)
DiskEntry->IsBoot = TRUE;

View File

@@ -50,6 +50,7 @@
#define IDS_DELETE_PARTITION_FAIL 1070
#define IDS_DELETE_PARTITION_SUCCESS 1071
#define IDS_DELETE_PARTITION_SYSTEM 1072
#define IDS_DETAIL_DISK_DESCRIPTION 1106
#define IDS_DETAIL_DISK_ID 1107