OpenCoreUefi: Support deduplicating BootOrder at boot
@@ -15,6 +15,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <OpenCore.h>
#include <Guid/OcVariables.h>
+#include <Guid/GlobalVariable.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
@@ -36,6 +37,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/OcUnicodeCollationEngLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Protocol/DevicePath.h>
@@ -482,6 +484,12 @@ OcLoadUefiSupport (
IN OC_CPU_INFO *CpuInfo
)
{
+ EFI_STATUS Status;
+ UINTN Index;
+ UINTN Index2;
+ UINT16 *BootOrder;
+ UINTN BootOrderSize;
+ BOOLEAN BootOrderChanged;
BOOLEAN AgiExitBs;
OcReinstallProtocols (Config);
@@ -526,6 +534,52 @@ OcLoadUefiSupport (
&Config->Uefi.Quirks.RequestBootVarFallback
);
+ if (Config->Uefi.Quirks.RequestBootVarFallback) {
+ Status = GetVariable2 (
+ EFI_BOOT_ORDER_VARIABLE_NAME,
+ &gEfiGlobalVariableGuid,
+ (VOID **) &BootOrder,
+ &BootOrderSize
+ );
+
+ //
+ // Deduplicate BootOrder variable contents.
+ //
+ if (!EFI_ERROR (Status) && BootOrderSize > 0 && BootOrderSize % sizeof (BootOrder[0]) == 0) {
+ BootOrderChanged = FALSE;
+
+ for (Index = 1; Index < BootOrderSize / sizeof (BootOrder[0]); ++Index) {
+ for (Index2 = 0; Index2 < Index; ++Index2) {
+ if (BootOrder[Index] == BootOrder[Index2]) {
+ //
+ // Found duplicate.
+ //
+ BootOrderChanged = TRUE;
+ CopyMem (
+ &BootOrder[Index],
+ &BootOrder[Index + 1],
+ BootOrderSize - sizeof (BootOrder[0]) * (Index + 1)
+ );
+ BootOrderSize -= sizeof (BootOrder[0]);
+ --Index;
+ break;
+ }
+ }
+
+ if (BootOrderChanged) {
+ DEBUG ((DEBUG_INFO, "OC: Performed BootOrder deduplication\n"));
+ gRT->SetVariable (
+ EFI_BOOT_ORDER_VARIABLE_NAME,
+ &gEfiGlobalVariableGuid,
+ EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
+ BootOrderSize,
+ BootOrder
+ );
+ }
+ }
+ }
+ }
+
if (Config->Uefi.Quirks.ReleaseUsbOwnership
|| Config->Uefi.Quirks.ExitBootServicesDelay > 0
|| AgiExitBs) {
