Skip to content

Commit

Permalink
[USETUP] Blur the boundaries between "Primary" and "Logical" partitio…
Browse files Browse the repository at this point in the history
…ns (in MBR disks)

Do not do that yet for extended partitions (containers).

This is possible, because when creating partitions, we do that on
unpartitioned space that is already "tagged" as either being "logical"
or not, and the partition style is inherited from that.

The resulting code is simpler, yet working as it should.
This will also help in the future for supporting other platforms, where
the concept of "primary", "extended" and "logical" partitions do not
exist (basically all platforms except BIOS-based PC-AT).
  • Loading branch information
HBelusca committed Oct 26, 2023
1 parent e413136 commit 0aa99aa
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 245 deletions.
219 changes: 88 additions & 131 deletions base/setup/lib/utils/partlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -2782,36 +2782,110 @@ GetNextUnpartitionedEntry(
return NULL;
}

ERROR_NUMBER
PartitionCreationChecks(
_In_ PPARTENTRY PartEntry)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;

if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT1("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return ERROR_WARN_PARTITION;
}

/* Fail if the partition is already in use */
if (PartEntry->IsPartitioned)
return ERROR_NEW_PARTITION;

/*
* For primary partitions
*/
if (!PartEntry->LogicalPartition)
{
/* Only one primary partition is allowed on super-floppy */
if (IsSuperFloppy(DiskEntry))
return ERROR_PARTITION_TABLE_FULL;

/* Fail if there are already 4 primary partitions in the list */
if (GetPrimaryPartitionCount(DiskEntry) >= 4)
return ERROR_PARTITION_TABLE_FULL;
}
/*
* For logical partitions
*/
else
{
// TODO: Check that we are inside an extended partition!!
// Then the following check will be useless.

/* Only one (primary) partition is allowed on super-floppy */
if (IsSuperFloppy(DiskEntry))
return ERROR_PARTITION_TABLE_FULL;
}

return ERROR_SUCCESS;
}

ERROR_NUMBER
ExtendedPartitionCreationChecks(
_In_ PPARTENTRY PartEntry)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;

if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT1("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return ERROR_WARN_PARTITION;
}

/* Fail if the partition is already in use */
if (PartEntry->IsPartitioned)
return ERROR_NEW_PARTITION;

/* Only one primary partition is allowed on super-floppy */
if (IsSuperFloppy(DiskEntry))
return ERROR_PARTITION_TABLE_FULL;

/* Fail if there are already 4 primary partitions in the list */
if (GetPrimaryPartitionCount(DiskEntry) >= 4)
return ERROR_PARTITION_TABLE_FULL;

/* Fail if there is another extended partition in the list */
if (DiskEntry->ExtendedPartition != NULL)
return ERROR_ONLY_ONE_EXTENDED;

return ERROR_SUCCESS;
}

BOOLEAN
CreatePrimaryPartition(
IN PPARTLIST List,
IN OUT PPARTENTRY PartEntry,
IN ULONGLONG SectorCount,
IN BOOLEAN AutoCreate)
CreatePartition(
_In_ PPARTLIST List,
_Inout_ PPARTENTRY PartEntry,
_In_ ULONGLONG SectorCount,
_In_ BOOLEAN AutoCreate)
{
ERROR_NUMBER Error;

DPRINT1("CreatePrimaryPartition(%I64u)\n", SectorCount);
DPRINT1("CreatePartition(%I64u)\n", SectorCount);

if (List == NULL || PartEntry == NULL ||
PartEntry->DiskEntry == NULL || PartEntry->IsPartitioned)
{
return FALSE;
}

Error = PrimaryPartitionCreationChecks(PartEntry);
Error = PartitionCreationChecks(PartEntry);
if (Error != NOT_AN_ERROR)
{
DPRINT1("PrimaryPartitionCreationChecks() failed with error %lu\n", Error);
DPRINT1("PartitionCreationChecks() failed with error %lu\n", Error);
return FALSE;
}

/* Initialize the partition entry, inserting a new blank region if needed */
if (!InitializePartitionEntry(PartEntry, SectorCount, AutoCreate))
return FALSE;

ASSERT(PartEntry->LogicalPartition == FALSE);

UpdateDiskLayout(PartEntry->DiskEntry);
AssignDriveLetters(List);

Expand All @@ -2821,7 +2895,7 @@ CreatePrimaryPartition(
static
VOID
AddLogicalDiskSpace(
IN PDISKENTRY DiskEntry)
_In_ PDISKENTRY DiskEntry)
{
ULONGLONG StartSector;
ULONGLONG SectorCount;
Expand All @@ -2848,9 +2922,9 @@ AddLogicalDiskSpace(

BOOLEAN
CreateExtendedPartition(
IN PPARTLIST List,
IN OUT PPARTENTRY PartEntry,
IN ULONGLONG SectorCount)
_In_ PPARTLIST List,
_Inout_ PPARTENTRY PartEntry,
_In_ ULONGLONG SectorCount)
{
ERROR_NUMBER Error;

Expand Down Expand Up @@ -2900,42 +2974,6 @@ CreateExtendedPartition(
return TRUE;
}

BOOLEAN
CreateLogicalPartition(
IN PPARTLIST List,
IN OUT PPARTENTRY PartEntry,
IN ULONGLONG SectorCount,
IN BOOLEAN AutoCreate)
{
ERROR_NUMBER Error;

DPRINT1("CreateLogicalPartition(%I64u)\n", SectorCount);

if (List == NULL || PartEntry == NULL ||
PartEntry->DiskEntry == NULL || PartEntry->IsPartitioned)
{
return FALSE;
}

Error = LogicalPartitionCreationChecks(PartEntry);
if (Error != NOT_AN_ERROR)
{
DPRINT1("LogicalPartitionCreationChecks() failed with error %lu\n", Error);
return FALSE;
}

/* Initialize the partition entry, inserting a new blank region if needed */
if (!InitializePartitionEntry(PartEntry, SectorCount, AutoCreate))
return FALSE;

ASSERT(PartEntry->LogicalPartition == TRUE);

UpdateDiskLayout(PartEntry->DiskEntry);
AssignDriveLetters(List);

return TRUE;
}

NTSTATUS
DismountVolume(
IN PPARTENTRY PartEntry)
Expand Down Expand Up @@ -3950,87 +3988,6 @@ SetMBRPartitionType(
DiskEntry->LayoutBuffer->PartitionEntry[PartEntry->PartitionIndex].RewritePartition = TRUE;
}

ERROR_NUMBER
PrimaryPartitionCreationChecks(
IN PPARTENTRY PartEntry)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;

if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT1("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return ERROR_WARN_PARTITION;
}

/* Fail if the partition is already in use */
if (PartEntry->IsPartitioned)
return ERROR_NEW_PARTITION;

/* Only one primary partition is allowed on super-floppy */
if (IsSuperFloppy(DiskEntry))
return ERROR_PARTITION_TABLE_FULL;

/* Fail if there are already 4 primary partitions in the list */
if (GetPrimaryPartitionCount(DiskEntry) >= 4)
return ERROR_PARTITION_TABLE_FULL;

return ERROR_SUCCESS;
}

ERROR_NUMBER
ExtendedPartitionCreationChecks(
IN PPARTENTRY PartEntry)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;

if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT1("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return ERROR_WARN_PARTITION;
}

/* Fail if the partition is already in use */
if (PartEntry->IsPartitioned)
return ERROR_NEW_PARTITION;

/* Only one primary partition is allowed on super-floppy */
if (IsSuperFloppy(DiskEntry))
return ERROR_PARTITION_TABLE_FULL;

/* Fail if there are already 4 primary partitions in the list */
if (GetPrimaryPartitionCount(DiskEntry) >= 4)
return ERROR_PARTITION_TABLE_FULL;

/* Fail if there is another extended partition in the list */
if (DiskEntry->ExtendedPartition != NULL)
return ERROR_ONLY_ONE_EXTENDED;

return ERROR_SUCCESS;
}

ERROR_NUMBER
LogicalPartitionCreationChecks(
IN PPARTENTRY PartEntry)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;

if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT1("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return ERROR_WARN_PARTITION;
}

/* Fail if the partition is already in use */
if (PartEntry->IsPartitioned)
return ERROR_NEW_PARTITION;

/* Only one primary partition is allowed on super-floppy */
if (IsSuperFloppy(DiskEntry))
return ERROR_PARTITION_TABLE_FULL;

return ERROR_SUCCESS;
}

BOOLEAN
GetNextUnformattedPartition(
IN PPARTLIST List,
Expand Down
43 changes: 16 additions & 27 deletions base/setup/lib/utils/partlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,25 +294,26 @@ GetPrevPartition(
IN PPARTLIST List,
IN PPARTENTRY CurrentPart OPTIONAL);

BOOLEAN
CreatePrimaryPartition(
IN PPARTLIST List,
IN OUT PPARTENTRY PartEntry,
IN ULONGLONG SectorCount,
IN BOOLEAN AutoCreate);
ERROR_NUMBER
PartitionCreationChecks(
_In_ PPARTENTRY PartEntry);

ERROR_NUMBER
ExtendedPartitionCreationChecks(
_In_ PPARTENTRY PartEntry);

BOOLEAN
CreateExtendedPartition(
IN PPARTLIST List,
IN OUT PPARTENTRY PartEntry,
IN ULONGLONG SectorCount);
CreatePartition(
_In_ PPARTLIST List,
_Inout_ PPARTENTRY PartEntry,
_In_ ULONGLONG SectorCount,
_In_ BOOLEAN AutoCreate);

BOOLEAN
CreateLogicalPartition(
IN PPARTLIST List,
IN OUT PPARTENTRY PartEntry,
IN ULONGLONG SectorCount,
IN BOOLEAN AutoCreate);
CreateExtendedPartition(
_In_ PPARTLIST List,
_Inout_ PPARTENTRY PartEntry,
_In_ ULONGLONG SectorCount);

NTSTATUS
DismountVolume(
Expand Down Expand Up @@ -360,18 +361,6 @@ SetMBRPartitionType(
IN PPARTENTRY PartEntry,
IN UCHAR PartitionType);

ERROR_NUMBER
PrimaryPartitionCreationChecks(
IN PPARTENTRY PartEntry);

ERROR_NUMBER
ExtendedPartitionCreationChecks(
IN PPARTENTRY PartEntry);

ERROR_NUMBER
LogicalPartitionCreationChecks(
IN PPARTENTRY PartEntry);

BOOLEAN
GetNextUnformattedPartition(
IN PPARTLIST List,
Expand Down
Loading

0 comments on commit 0aa99aa

Please sign in to comment.