[SETUPLIB][USETUP] Some cleanup for partition code.

- Make the Create*Partition helpers take a size in bytes, not in sectors.

  This allows them to be easier to use by the caller, alleviating the
  need for making the size conversion into sectors. Instead it is done
  internally by the helpers.

- Introduce helper macros to easily retrieve the size of a partition
  entry or a disk in bytes, from their internal representation in number
  of sectors.

- The 'AutoCreate' variable being USETUP-specific, remove it from the
  PARTENTRY structure and use instead a flag set into the 'New' member.

- Rename IsDiskSizeValid to IsPartitionLargeEnough, to better describe
  what the function is for.
This commit is contained in:
Hermès Bélusca-Maïto 2023-12-07 23:29:28 +01:00
parent b2ec78673d
commit b7ad4a2298
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
5 changed files with 143 additions and 131 deletions

View file

@ -806,12 +806,11 @@ CreateNTOSInstallationsList(
ASSERT(PartEntry->DiskEntry == DiskEntry);
DPRINT(" Primary Partition #%d, index %d - Type 0x%02x, IsLogical = %s, IsPartitioned = %s, IsNew = %s, AutoCreate = %s, FormatState = %lu -- Should I check it? %s\n",
DPRINT(" Primary Partition #%d, index %d - Type 0x%02x, IsLogical = %s, IsPartitioned = %s, IsNew = %s, FormatState = %lu -- Should I check it? %s\n",
PartEntry->PartitionNumber, PartEntry->PartitionIndex,
PartEntry->PartitionType, PartEntry->LogicalPartition ? "TRUE" : "FALSE",
PartEntry->IsPartitioned ? "TRUE" : "FALSE",
PartEntry->New ? "Yes" : "No",
PartEntry->AutoCreate ? "Yes" : "No",
PartEntry->FormatState,
ShouldICheckThisPartition(PartEntry) ? "YES!" : "NO!");
@ -828,12 +827,11 @@ CreateNTOSInstallationsList(
ASSERT(PartEntry->DiskEntry == DiskEntry);
DPRINT(" Logical Partition #%d, index %d - Type 0x%02x, IsLogical = %s, IsPartitioned = %s, IsNew = %s, AutoCreate = %s, FormatState = %lu -- Should I check it? %s\n",
DPRINT(" Logical Partition #%d, index %d - Type 0x%02x, IsLogical = %s, IsPartitioned = %s, IsNew = %s, FormatState = %lu -- Should I check it? %s\n",
PartEntry->PartitionNumber, PartEntry->PartitionIndex,
PartEntry->PartitionType, PartEntry->LogicalPartition ? "TRUE" : "FALSE",
PartEntry->IsPartitioned ? "TRUE" : "FALSE",
PartEntry->New ? "Yes" : "No",
PartEntry->AutoCreate ? "Yes" : "No",
PartEntry->FormatState,
ShouldICheckThisPartition(PartEntry) ? "YES!" : "NO!");

View file

@ -552,7 +552,7 @@ IsSuperFloppy(
}
/* The partition lengths should agree */
PartitionLengthEstimate = DiskEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
PartitionLengthEstimate = GetDiskSizeInBytes(DiskEntry);
if (PartitionInfo->PartitionLength.QuadPart != PartitionLengthEstimate)
{
DPRINT1("PartitionLength = %I64u is different from PartitionLengthEstimate = %I64u\n",
@ -676,26 +676,46 @@ CreateInsertBlankRegion(
static
BOOLEAN
InitializePartitionEntry(
IN OUT PPARTENTRY PartEntry,
IN ULONGLONG SectorCount,
IN BOOLEAN AutoCreate)
_Inout_ PPARTENTRY PartEntry,
_In_opt_ ULONGLONG SizeBytes)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;
ULONGLONG SectorCount;
DPRINT1("Current partition sector count: %I64u\n", PartEntry->SectorCount.QuadPart);
DPRINT1("Current entry sector count: %I64u\n", PartEntry->SectorCount.QuadPart);
/* Fail if we try to initialize this partition entry with more sectors than what it actually contains */
/* The entry must not be already partitioned and not be void */
ASSERT(!PartEntry->IsPartitioned);
ASSERT(PartEntry->SectorCount.QuadPart);
/* Convert the size in bytes to sector count. SizeBytes being
* zero means the caller wants to use all the empty space. */
if ((SizeBytes == 0) || (SizeBytes == GetPartEntrySizeInBytes(PartEntry)))
{
/* Use all of the unpartitioned disk space */
SectorCount = PartEntry->SectorCount.QuadPart;
}
else
{
SectorCount = SizeBytes / DiskEntry->BytesPerSector;
if (SectorCount == 0)
{
/* SizeBytes was certainly less than the minimal size, so fail */
DPRINT1("Partition size %I64u too small\n", SizeBytes);
return FALSE;
}
}
DPRINT1(" New sector count: %I64u\n", SectorCount);
/* Fail if we request more sectors than what the entry actually contains */
if (SectorCount > PartEntry->SectorCount.QuadPart)
return FALSE;
/* Fail if the partition is already in use */
ASSERT(!PartEntry->IsPartitioned);
if ((AutoCreate != FALSE) ||
if ((SectorCount == 0) ||
(AlignDown(PartEntry->StartSector.QuadPart + SectorCount, DiskEntry->SectorAlignment) -
PartEntry->StartSector.QuadPart == PartEntry->SectorCount.QuadPart))
{
PartEntry->AutoCreate = AutoCreate;
/* Reuse the whole current entry */
}
else
{
@ -703,7 +723,8 @@ InitializePartitionEntry(
ULONGLONG SectorCount2;
PPARTENTRY NewPartEntry;
/* Create a partition entry that represents the remaining space after the partition to be initialized */
/* Create a partition entry that represents the remaining space
* after the partition to be initialized */
StartSector = AlignDown(PartEntry->StartSector.QuadPart + SectorCount, DiskEntry->SectorAlignment);
SectorCount2 = PartEntry->StartSector.QuadPart + PartEntry->SectorCount.QuadPart - StartSector;
@ -735,7 +756,6 @@ InitializePartitionEntry(
PartEntry->FormatState = Unformatted;
PartEntry->FileSystem[0] = L'\0';
// PartEntry->AutoCreate = AutoCreate;
PartEntry->BootIndicator = FALSE;
DPRINT1("First Sector : %I64u\n", PartEntry->StartSector.QuadPart);
@ -2390,37 +2410,24 @@ GetPrevPartition(
return NULL;
}
// static
FORCEINLINE
static inline
BOOLEAN
IsEmptyLayoutEntry(
IN PPARTITION_INFORMATION PartitionInfo)
_In_ PPARTITION_INFORMATION PartitionInfo)
{
if (PartitionInfo->StartingOffset.QuadPart == 0 &&
PartitionInfo->PartitionLength.QuadPart == 0)
{
return TRUE;
}
return FALSE;
return (PartitionInfo->StartingOffset.QuadPart == 0 &&
PartitionInfo->PartitionLength.QuadPart == 0);
}
// static
FORCEINLINE
static inline
BOOLEAN
IsSamePrimaryLayoutEntry(
IN PPARTITION_INFORMATION PartitionInfo,
IN PDISKENTRY DiskEntry,
IN PPARTENTRY PartEntry)
_In_ PPARTITION_INFORMATION PartitionInfo,
_In_ PPARTENTRY PartEntry)
{
if (PartitionInfo->StartingOffset.QuadPart == PartEntry->StartSector.QuadPart * DiskEntry->BytesPerSector &&
PartitionInfo->PartitionLength.QuadPart == PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector)
return ((PartitionInfo->StartingOffset.QuadPart == GetPartEntryOffsetInBytes(PartEntry)) &&
(PartitionInfo->PartitionLength.QuadPart == GetPartEntrySizeInBytes(PartEntry)));
// PartitionInfo->PartitionType == PartEntry->PartitionType
{
return TRUE;
}
return FALSE;
}
static
@ -2578,12 +2585,12 @@ UpdateDiskLayout(
PartEntry->OnDiskPartitionNumber = (!IsContainerPartition(PartEntry->PartitionType) ? PartitionNumber : 0);
if (!IsSamePrimaryLayoutEntry(PartitionInfo, DiskEntry, PartEntry))
if (!IsSamePrimaryLayoutEntry(PartitionInfo, PartEntry))
{
DPRINT1("Updating primary partition entry %lu\n", Index);
PartitionInfo->StartingOffset.QuadPart = PartEntry->StartSector.QuadPart * DiskEntry->BytesPerSector;
PartitionInfo->PartitionLength.QuadPart = PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
PartitionInfo->StartingOffset.QuadPart = GetPartEntryOffsetInBytes(PartEntry);
PartitionInfo->PartitionLength.QuadPart = GetPartEntrySizeInBytes(PartEntry);
PartitionInfo->HiddenSectors = PartEntry->StartSector.LowPart;
PartitionInfo->PartitionNumber = PartEntry->PartitionNumber;
PartitionInfo->PartitionType = PartEntry->PartitionType;
@ -2624,8 +2631,8 @@ UpdateDiskLayout(
DPRINT1("Updating logical partition entry %lu\n", Index);
PartitionInfo->StartingOffset.QuadPart = PartEntry->StartSector.QuadPart * DiskEntry->BytesPerSector;
PartitionInfo->PartitionLength.QuadPart = PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
PartitionInfo->StartingOffset.QuadPart = GetPartEntryOffsetInBytes(PartEntry);
PartitionInfo->PartitionLength.QuadPart = GetPartEntrySizeInBytes(PartEntry);
PartitionInfo->HiddenSectors = DiskEntry->SectorAlignment;
PartitionInfo->PartitionNumber = PartEntry->PartitionNumber;
PartitionInfo->PartitionType = PartEntry->PartitionType;
@ -2862,12 +2869,11 @@ BOOLEAN
CreatePartition(
_In_ PPARTLIST List,
_Inout_ PPARTENTRY PartEntry,
_In_ ULONGLONG SectorCount,
_In_ BOOLEAN AutoCreate)
_In_opt_ ULONGLONG SizeBytes)
{
ERROR_NUMBER Error;
DPRINT1("CreatePartition(%I64u)\n", SectorCount);
DPRINT1("CreatePartition(%I64u bytes)\n", SizeBytes);
if (List == NULL || PartEntry == NULL ||
PartEntry->DiskEntry == NULL || PartEntry->IsPartitioned)
@ -2883,7 +2889,7 @@ CreatePartition(
}
/* Initialize the partition entry, inserting a new blank region if needed */
if (!InitializePartitionEntry(PartEntry, SectorCount, AutoCreate))
if (!InitializePartitionEntry(PartEntry, SizeBytes))
return FALSE;
UpdateDiskLayout(PartEntry->DiskEntry);
@ -2924,11 +2930,11 @@ BOOLEAN
CreateExtendedPartition(
_In_ PPARTLIST List,
_Inout_ PPARTENTRY PartEntry,
_In_ ULONGLONG SectorCount)
_In_opt_ ULONGLONG SizeBytes)
{
ERROR_NUMBER Error;
DPRINT1("CreateExtendedPartition(%I64u)\n", SectorCount);
DPRINT1("CreateExtendedPartition(%I64u bytes)\n", SizeBytes);
if (List == NULL || PartEntry == NULL ||
PartEntry->DiskEntry == NULL || PartEntry->IsPartitioned)
@ -2944,7 +2950,7 @@ CreateExtendedPartition(
}
/* Initialize the partition entry, inserting a new blank region if needed */
if (!InitializePartitionEntry(PartEntry, SectorCount, FALSE))
if (!InitializePartitionEntry(PartEntry, SizeBytes))
return FALSE;
ASSERT(PartEntry->LogicalPartition == FALSE);
@ -3933,7 +3939,7 @@ SetMountedDeviceValues(
/* Assign a "\DosDevices\#:" mount point to this partition */
if (PartEntry->DriveLetter)
{
StartingOffset.QuadPart = PartEntry->StartSector.QuadPart * DiskEntry->BytesPerSector;
StartingOffset.QuadPart = GetPartEntryOffsetInBytes(PartEntry);
if (!SetMountedDeviceValue(PartEntry->DriveLetter,
DiskEntry->LayoutBuffer->Signature,
StartingOffset))
@ -3956,7 +3962,7 @@ SetMountedDeviceValues(
/* Assign a "\DosDevices\#:" mount point to this partition */
if (PartEntry->DriveLetter)
{
StartingOffset.QuadPart = PartEntry->StartSector.QuadPart * DiskEntry->BytesPerSector;
StartingOffset.QuadPart = GetPartEntryOffsetInBytes(PartEntry);
if (!SetMountedDeviceValue(PartEntry->DriveLetter,
DiskEntry->LayoutBuffer->Signature,
StartingOffset))

View file

@ -70,9 +70,6 @@ typedef struct _PARTENTRY
/* Partition is new, table does not exist on disk yet */
BOOLEAN New;
/* Partition was created automatically */
BOOLEAN AutoCreate;
/* Partition must be checked */
BOOLEAN NeedsCheck;
@ -227,6 +224,16 @@ RoundingDivide(
IN ULONGLONG Divisor);
#define GetPartEntryOffsetInBytes(PartEntry) \
((PartEntry)->StartSector.QuadPart * (PartEntry)->DiskEntry->BytesPerSector)
#define GetPartEntrySizeInBytes(PartEntry) \
((PartEntry)->SectorCount.QuadPart * (PartEntry)->DiskEntry->BytesPerSector)
#define GetDiskSizeInBytes(DiskEntry) \
((DiskEntry)->SectorCount.QuadPart * (DiskEntry)->BytesPerSector)
BOOLEAN
IsSuperFloppy(
IN PDISKENTRY DiskEntry);
@ -306,14 +313,13 @@ BOOLEAN
CreatePartition(
_In_ PPARTLIST List,
_Inout_ PPARTENTRY PartEntry,
_In_ ULONGLONG SectorCount,
_In_ BOOLEAN AutoCreate);
_In_opt_ ULONGLONG SizeBytes);
BOOLEAN
CreateExtendedPartition(
_In_ PPARTLIST List,
_Inout_ PPARTENTRY PartEntry,
_In_ ULONGLONG SectorCount);
_In_opt_ ULONGLONG SizeBytes);
NTSTATUS
DismountVolume(

View file

@ -156,7 +156,7 @@ PartitionDescription(
PCSTR Unit;
/* Get the partition size */
PartSize = PartEntry->SectorCount.QuadPart * PartEntry->DiskEntry->BytesPerSector;
PartSize = GetPartEntrySizeInBytes(PartEntry);
PrettifySize2(&PartSize, &Unit);
if (PartEntry->IsPartitioned == FALSE)
@ -307,7 +307,7 @@ DiskDescription(
PCSTR Unit;
/* Get the disk size */
DiskSize = DiskEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
DiskSize = GetDiskSizeInBytes(DiskEntry);
PrettifySize1(&DiskSize, &Unit);
//

View file

@ -79,6 +79,9 @@ static enum {
PartTypeExtended // MBR-disk container
} PartCreateType = PartTypeData;
/* Flag set in PARTENTRY::New when a partition is created automatically */
#define PARTITION_NEW_AUTOCREATE 0x80
/* List of supported file systems for the partition to be formatted */
static PFILE_SYSTEM_LIST FileSystemList = NULL;
@ -1503,18 +1506,18 @@ LayoutSettingsPage(PINPUT_RECORD Ir)
}
static BOOL
IsDiskSizeValid(PPARTENTRY PartEntry)
static BOOLEAN
IsPartitionLargeEnough(
_In_ PPARTENTRY PartEntry)
{
ULONGLONG size;
/* Retrieve the maximum size in MB (rounded up) */
ULONGLONG PartSize = RoundingDivide(GetPartEntrySizeInBytes(PartEntry), MB);
size = PartEntry->SectorCount.QuadPart * PartEntry->DiskEntry->BytesPerSector;
size = (size + (512 * KB)) / MB; /* in MBytes */
if (size < USetupData.RequiredPartitionDiskSpace)
if (PartSize < USetupData.RequiredPartitionDiskSpace)
{
/* Partition is too small so ask for another one */
DPRINT1("Partition is too small (size: %I64u MB), required disk space is %lu MB\n", size, USetupData.RequiredPartitionDiskSpace);
DPRINT1("Partition is too small (size: %I64u MB), required disk space is %lu MB\n",
PartSize, USetupData.RequiredPartitionDiskSpace);
return FALSE;
}
else
@ -1608,13 +1611,15 @@ SelectPartitionPage(PINPUT_RECORD Ir)
ASSERT(CurrentPartition != NULL);
ASSERT(!IsContainerPartition(CurrentPartition->PartitionType));
/* Automatically create the partition on the whole empty space;
* it will be formatted later with default parameters */
CreatePartition(PartitionList,
CurrentPartition,
CurrentPartition->SectorCount.QuadPart,
TRUE);
0ULL);
CurrentPartition->New |= PARTITION_NEW_AUTOCREATE;
// FIXME?? Aren't we going to enter an infinite loop, if this test fails??
if (!IsDiskSizeValid(CurrentPartition))
if (!IsPartitionLargeEnough(CurrentPartition))
{
MUIDisplayError(ERROR_INSUFFICIENT_PARTITION_SIZE, Ir, POPUP_WAIT_ANY_KEY,
USetupData.RequiredPartitionDiskSpace);
@ -1632,7 +1637,7 @@ SelectPartitionPage(PINPUT_RECORD Ir)
DrawPartitionList(&ListUi); // FIXME: Doesn't make much sense...
// FIXME?? Aren't we going to enter an infinite loop, if this test fails??
if (!IsDiskSizeValid(InstallPartition))
if (!IsPartitionLargeEnough(InstallPartition))
{
MUIDisplayError(ERROR_INSUFFICIENT_PARTITION_SIZE, Ir, POPUP_WAIT_ANY_KEY,
USetupData.RequiredPartitionDiskSpace);
@ -1737,13 +1742,15 @@ SelectPartitionPage(PINPUT_RECORD Ir)
return SELECT_PARTITION_PAGE;
}
/* Automatically create the partition on the whole empty space;
* it will be formatted later with default parameters */
CreatePartition(PartitionList,
CurrentPartition,
0ULL,
TRUE);
0ULL);
CurrentPartition->New |= PARTITION_NEW_AUTOCREATE;
}
if (!IsDiskSizeValid(CurrentPartition))
if (!IsPartitionLargeEnough(CurrentPartition))
{
MUIDisplayError(ERROR_INSUFFICIENT_PARTITION_SIZE, Ir, POPUP_WAIT_ANY_KEY,
USetupData.RequiredPartitionDiskSpace);
@ -2040,11 +2047,9 @@ CreatePartitionPage(PINPUT_RECORD Ir)
PPARTENTRY PartEntry;
PDISKENTRY DiskEntry;
ULONG uID;
BOOLEAN Quit;
BOOLEAN Cancel;
ULONG MaxSize;
ULONGLONG PartSize;
ULONGLONG SectorCount;
ULONGLONG MaxPartSize, PartSize;
BOOLEAN Quit, Cancel;
WCHAR InputBuffer[50];
CHAR LineBuffer[100];
@ -2078,15 +2083,17 @@ CreatePartitionPage(PINPUT_RECORD Ir)
CONSOLE_SetStatusText(MUIGetString(STRING_CREATEPARTITION));
MaxPartSize = GetPartEntrySizeInBytes(PartEntry);
while (TRUE)
{
MaxSize = (PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector) / MB; /* in MBytes (rounded) */
if (MaxSize > PARTITION_MAXSIZE)
MaxSize = PARTITION_MAXSIZE;
/* Retrieve the maximum size in MB (rounded up)
* and cap it with what the user can enter */
MaxSize = (ULONG)RoundingDivide(MaxPartSize, MB);
MaxSize = min(MaxSize, PARTITION_MAXSIZE);
ShowPartitionSizeInputBox(12, 14, xScreen - 12, 17, /* left, top, right, bottom */
ShowPartitionSizeInputBox(12, 14, xScreen - 12, 17,
MaxSize, InputBuffer, &Quit, &Cancel);
if (Quit)
{
if (ConfirmQuit(Ir))
@ -2097,50 +2104,41 @@ CreatePartitionPage(PINPUT_RECORD Ir)
{
return SELECT_PARTITION_PAGE;
}
else
PartSize = _wcstoui64(InputBuffer, NULL, 10);
/* Retry if too small or too large */
if ((PartSize < 1) || (PartSize > MaxSize))
continue;
/*
* If the input size, given in MB, specifies the maximum partition
* size, it may slightly under- or over-estimate it due to rounding
* error. In this case, use all of the unpartitioned disk space.
* Otherwise, directly convert the size to bytes.
*/
if (PartSize == MaxSize)
PartSize = MaxPartSize;
else // if (PartSize < MaxSize)
PartSize *= MB;
DPRINT("Partition size: %I64u bytes\n", PartSize);
ASSERT(PartSize <= MaxPartSize);
if (PartCreateType == PartTypeData)
{
PartSize = _wcstoui64(InputBuffer, NULL, 10);
/* Retry if too small or too large */
if (PartSize < 1)
continue;
if (PartSize > MaxSize)
continue;
/* Convert to bytes */
if (PartSize == MaxSize)
{
/* Use all of the unpartitioned disk space */
SectorCount = PartEntry->SectorCount.QuadPart;
}
else
{
/* Calculate the sector count from the size in MB */
SectorCount = PartSize * MB / DiskEntry->BytesPerSector;
/* But never get larger than the unpartitioned disk space */
if (SectorCount > PartEntry->SectorCount.QuadPart)
SectorCount = PartEntry->SectorCount.QuadPart;
}
DPRINT("Partition size: %I64u bytes\n", PartSize);
if (PartCreateType == PartTypeData)
{
CreatePartition(PartitionList,
CurrentPartition,
SectorCount,
FALSE);
}
else // if (PartCreateType == PartTypeExtended)
{
CreateExtendedPartition(PartitionList,
CurrentPartition,
SectorCount);
}
return SELECT_PARTITION_PAGE;
CreatePartition(PartitionList,
CurrentPartition,
PartSize);
}
else // if (PartCreateType == PartTypeExtended)
{
CreateExtendedPartition(PartitionList,
CurrentPartition,
PartSize);
}
return SELECT_PARTITION_PAGE;
}
return CREATE_PARTITION_PAGE;
@ -2392,10 +2390,14 @@ SelectFileSystemPage(PINPUT_RECORD Ir)
*/
if (!SystemPartition->IsPartitioned)
{
/* Automatically create the partition; it will be
* formatted later with default parameters */
// FIXME: Don't use the whole empty space, but a minimal size
// specified from the TXTSETUP.SIF or unattended setup.
CreatePartition(PartitionList,
SystemPartition,
0LL, // SystemPartition->SectorCount.QuadPart,
TRUE);
0ULL);
SystemPartition->New |= PARTITION_NEW_AUTOCREATE;
ASSERT(SystemPartition->IsPartitioned);
}
@ -2548,9 +2550,9 @@ SelectFileSystemPage(PINPUT_RECORD Ir)
MUIDisplayPage(SELECT_FILE_SYSTEM_PAGE);
if (PartEntry->AutoCreate)
if (PartEntry->New & PARTITION_NEW_AUTOCREATE)
{
PartEntry->AutoCreate = FALSE;
PartEntry->New &= ~PARTITION_NEW_AUTOCREATE;
CONSOLE_SetTextXY(6, 8, MUIGetString(STRING_NEWPARTITION));
}