From 7639cb750ab238420f409e59f65a44dbbbaeacb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Thu, 4 Jul 2024 15:28:53 +0200 Subject: [PATCH] [SETUPLIB][USETUP] Reduce duplicated code - ScrollDownPartitionList() and ScrollUpPartitionList() --> ScrollUpDownPartitionList() - GetPrimaryPartitionCount() and GetLogicalPartitionCount() --> generic GetPartitionCount() and macros. - GetPrevUnpartitionedEntry() and GetNextUnpartitionedEntry() --> GetAdjUnpartitionedEntry() ("Adj" == Adjacent) --- base/setup/lib/utils/arcname.c | 6 +- base/setup/lib/utils/partlist.c | 140 ++++++++++++-------------------- base/setup/usetup/partlist.c | 32 +++----- base/setup/usetup/partlist.h | 13 ++- base/setup/usetup/usetup.c | 4 +- 5 files changed, 73 insertions(+), 122 deletions(-) diff --git a/base/setup/lib/utils/arcname.c b/base/setup/lib/utils/arcname.c index 77e7b579c58..a29076a2fdf 100644 --- a/base/setup/lib/utils/arcname.c +++ b/base/setup/lib/utils/arcname.c @@ -702,9 +702,6 @@ ResolveArcNameManually( BOOLEAN UseSignature; SIZE_T NameLength; - PDISKENTRY DiskEntry; - PPARTENTRY PartEntry = NULL; - if (NtName->MaximumLength < sizeof(UNICODE_NULL)) return STATUS_BUFFER_TOO_SMALL; @@ -757,6 +754,9 @@ ResolveArcNameManually( else if (PeripheralType == RDiskPeripheral) { + PDISKENTRY DiskEntry; + PPARTENTRY PartEntry = NULL; + if (UseSignature) { /* The disk signature is stored in AdapterKey */ diff --git a/base/setup/lib/utils/partlist.c b/base/setup/lib/utils/partlist.c index a0e955548ef..914fe69ccb3 100644 --- a/base/setup/lib/utils/partlist.c +++ b/base/setup/lib/utils/partlist.c @@ -2432,59 +2432,39 @@ IsSamePrimaryLayoutEntry( // PartitionInfo->PartitionType == PartEntry->PartitionType } + +/** + * @brief + * Counts the number of partitioned disk regions in a given partition list. + **/ static ULONG -GetPrimaryPartitionCount( - IN PDISKENTRY DiskEntry) +GetPartitionCount( + _In_ PLIST_ENTRY PartListHead) { PLIST_ENTRY Entry; PPARTENTRY PartEntry; ULONG Count = 0; - if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT) - { - DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n"); - return 0; - } - - for (Entry = DiskEntry->PrimaryPartListHead.Flink; - Entry != &DiskEntry->PrimaryPartListHead; + for (Entry = PartListHead->Flink; + Entry != PartListHead; Entry = Entry->Flink) { PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry); if (PartEntry->IsPartitioned) - Count++; + ++Count; } return Count; } -static -ULONG -GetLogicalPartitionCount( - IN PDISKENTRY DiskEntry) -{ - PLIST_ENTRY ListEntry; - PPARTENTRY PartEntry; - ULONG Count = 0; +#define GetPrimaryPartitionCount(DiskEntry) \ + GetPartitionCount(&(DiskEntry)->PrimaryPartListHead) - if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT) - { - DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n"); - return 0; - } +#define GetLogicalPartitionCount(DiskEntry) \ + (((DiskEntry)->DiskStyle == PARTITION_STYLE_MBR) \ + ? GetPartitionCount(&(DiskEntry)->LogicalPartListHead) : 0) - for (ListEntry = DiskEntry->LogicalPartListHead.Flink; - ListEntry != &DiskEntry->LogicalPartListHead; - ListEntry = ListEntry->Flink) - { - PartEntry = CONTAINING_RECORD(ListEntry, PARTENTRY, ListEntry); - if (PartEntry->IsPartitioned) - Count++; - } - - return Count; -} static BOOLEAN @@ -2727,73 +2707,53 @@ UpdateDiskLayout( #endif } +/** + * @brief + * Retrieves, if any, the unpartitioned disk region that is adjacent + * (next or previous) to the specified partition. + * + * @param[in] PartEntry + * Partition from where to find the adjacent unpartitioned region. + * + * @param[in] Direction + * TRUE or FALSE to search the next or previous region, respectively. + * + * @return The adjacent unpartitioned region, if it exists, or NULL. + **/ static PPARTENTRY -GetPrevUnpartitionedEntry( - IN PPARTENTRY PartEntry) +GetAdjUnpartitionedEntry( + _In_ PPARTENTRY PartEntry, + _In_ BOOLEAN Direction) { PDISKENTRY DiskEntry = PartEntry->DiskEntry; - PPARTENTRY PrevPartEntry; - PLIST_ENTRY ListHead; + PLIST_ENTRY ListHead, AdjEntry; - if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT) + /* In case of MBR disks only, check the logical partitions if necessary */ + if ((DiskEntry->DiskStyle == PARTITION_STYLE_MBR) && + PartEntry->LogicalPartition) { - DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n"); - return NULL; + ListHead = &DiskEntry->LogicalPartListHead; + } + else + { + ListHead = &DiskEntry->PrimaryPartListHead; } - if (PartEntry->LogicalPartition) - ListHead = &DiskEntry->LogicalPartListHead; + if (Direction) + AdjEntry = PartEntry->ListEntry.Flink; // Next region. else - ListHead = &DiskEntry->PrimaryPartListHead; + AdjEntry = PartEntry->ListEntry.Blink; // Previous region. - if (PartEntry->ListEntry.Blink != ListHead) + if (AdjEntry != ListHead) { - PrevPartEntry = CONTAINING_RECORD(PartEntry->ListEntry.Blink, - PARTENTRY, - ListEntry); - if (!PrevPartEntry->IsPartitioned) + PartEntry = CONTAINING_RECORD(AdjEntry, PARTENTRY, ListEntry); + if (!PartEntry->IsPartitioned) { - ASSERT(PrevPartEntry->PartitionType == PARTITION_ENTRY_UNUSED); - return PrevPartEntry; + ASSERT(PartEntry->PartitionType == PARTITION_ENTRY_UNUSED); + return PartEntry; } } - - return NULL; -} - -static -PPARTENTRY -GetNextUnpartitionedEntry( - IN PPARTENTRY PartEntry) -{ - PDISKENTRY DiskEntry = PartEntry->DiskEntry; - PPARTENTRY NextPartEntry; - PLIST_ENTRY ListHead; - - if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT) - { - DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n"); - return NULL; - } - - if (PartEntry->LogicalPartition) - ListHead = &DiskEntry->LogicalPartListHead; - else - ListHead = &DiskEntry->PrimaryPartListHead; - - if (PartEntry->ListEntry.Flink != ListHead) - { - NextPartEntry = CONTAINING_RECORD(PartEntry->ListEntry.Flink, - PARTENTRY, - ListEntry); - if (!NextPartEntry->IsPartitioned) - { - ASSERT(NextPartEntry->PartitionType == PARTITION_ENTRY_UNUSED); - return NextPartEntry; - } - } - return NULL; } @@ -3094,8 +3054,8 @@ DeletePartition( /* Adjust the unpartitioned disk space entries */ /* Get pointer to previous and next unpartitioned entries */ - PrevPartEntry = GetPrevUnpartitionedEntry(PartEntry); - NextPartEntry = GetNextUnpartitionedEntry(PartEntry); + PrevPartEntry = GetAdjUnpartitionedEntry(PartEntry, FALSE); + NextPartEntry = GetAdjUnpartitionedEntry(PartEntry, TRUE); if (PrevPartEntry != NULL && NextPartEntry != NULL) { diff --git a/base/setup/usetup/partlist.c b/base/setup/usetup/partlist.c index 72ae328cbde..570008638b2 100644 --- a/base/setup/usetup/partlist.c +++ b/base/setup/usetup/partlist.c @@ -833,28 +833,22 @@ DrawPartitionList( } } +/** + * @param[in] Direction + * TRUE or FALSE to scroll to the next (down) or previous (up) entry, respectively. + **/ VOID -ScrollDownPartitionList( - IN PPARTLIST_UI ListUi) +ScrollUpDownPartitionList( + _In_ PPARTLIST_UI ListUi, + _In_ BOOLEAN Direction) { - PPARTENTRY NextPart = GetNextPartition(ListUi->List, ListUi->CurrentPartition); - if (NextPart) + PPARTENTRY PartEntry = + (Direction ? GetNextPartition + : GetPrevPartition)(ListUi->List, ListUi->CurrentPartition); + if (PartEntry) { - ListUi->CurrentPartition = NextPart; - ListUi->CurrentDisk = NextPart->DiskEntry; - DrawPartitionList(ListUi); - } -} - -VOID -ScrollUpPartitionList( - IN PPARTLIST_UI ListUi) -{ - PPARTENTRY PrevPart = GetPrevPartition(ListUi->List, ListUi->CurrentPartition); - if (PrevPart) - { - ListUi->CurrentPartition = PrevPart; - ListUi->CurrentDisk = PrevPart->DiskEntry; + ListUi->CurrentPartition = PartEntry; + ListUi->CurrentDisk = PartEntry->DiskEntry; DrawPartitionList(ListUi); } } diff --git a/base/setup/usetup/partlist.h b/base/setup/usetup/partlist.h index df459c141d9..2b9d2e54fb6 100644 --- a/base/setup/usetup/partlist.h +++ b/base/setup/usetup/partlist.h @@ -85,16 +85,13 @@ InitPartitionListUi( IN SHORT Right, IN SHORT Bottom); -VOID -ScrollDownPartitionList( - IN PPARTLIST_UI ListUi); - -VOID -ScrollUpPartitionList( - IN PPARTLIST_UI ListUi); - VOID DrawPartitionList( IN PPARTLIST_UI ListUi); +VOID +ScrollUpDownPartitionList( + _In_ PPARTLIST_UI ListUi, + _In_ BOOLEAN Direction); + /* EOF */ diff --git a/base/setup/usetup/usetup.c b/base/setup/usetup/usetup.c index 3e11cda1658..81187430563 100644 --- a/base/setup/usetup/usetup.c +++ b/base/setup/usetup/usetup.c @@ -1689,12 +1689,12 @@ SelectPartitionPage(PINPUT_RECORD Ir) else if ((Ir->Event.KeyEvent.uChar.AsciiChar == 0x00) && (Ir->Event.KeyEvent.wVirtualKeyCode == VK_DOWN)) /* DOWN */ { - ScrollDownPartitionList(&ListUi); + ScrollUpDownPartitionList(&ListUi, TRUE); } else if ((Ir->Event.KeyEvent.uChar.AsciiChar == 0x00) && (Ir->Event.KeyEvent.wVirtualKeyCode == VK_UP)) /* UP */ { - ScrollUpPartitionList(&ListUi); + ScrollUpDownPartitionList(&ListUi, FALSE); } else if (Ir->Event.KeyEvent.wVirtualKeyCode == VK_RETURN) /* ENTER */ {