arrfab / rpms / shim

Forked from rpms/shim 4 years ago
Clone
Blob Blame History Raw
From 382a0b66e6c06ccf6775e3c05d5f9ce5f0eab5db Mon Sep 17 00:00:00 2001
From: Gary Ching-Pang Lin <glin@suse.com>
Date: Thu, 6 Mar 2014 11:58:36 +0800
Subject: [PATCH 26/74] [fallback] Avoid duplicate old BootOrder

set_boot_order() already copies the old BootOrder to the variable,
bootorder. Besides, we can adjust BootOrder when adding the newly
generated boot option. So, we don't have to copy the old one again
in update_boot_order(). This avoid the duplicate entries in BootOrder.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
---
 fallback.c | 37 ++++++++++++-------------------------
 1 file changed, 12 insertions(+), 25 deletions(-)

diff --git a/fallback.c b/fallback.c
index bc9a3c9..4bde9c1 100644
--- a/fallback.c
+++ b/fallback.c
@@ -204,12 +204,12 @@ add_boot_option(EFI_DEVICE_PATH *hddp, EFI_DEVICE_PATH *fulldp,
 				return EFI_OUT_OF_RESOURCES;
 
 			int j = 0;
+			newbootorder[0] = i & 0xffff;
 			if (nbootorder) {
 				for (j = 0; j < nbootorder; j++)
-					newbootorder[j] = bootorder[j];
+					newbootorder[j+1] = bootorder[j];
 				FreePool(bootorder);
 			}
-			newbootorder[j] = i & 0xffff;
 			bootorder = newbootorder;
 			nbootorder += 1;
 #ifdef DEBUG_FALLBACK
@@ -307,28 +307,17 @@ set_boot_order(void)
 EFI_STATUS
 update_boot_order(void)
 {
-	CHAR16 *oldbootorder;
 	UINTN size;
+	UINTN len = 0;
 	EFI_GUID global = EFI_GLOBAL_VARIABLE;
 	CHAR16 *newbootorder = NULL;
+	EFI_STATUS rc;
 
-	oldbootorder = LibGetVariableAndSize(L"BootOrder", &global, &size);
-	if (oldbootorder) {
-		int n = size / sizeof (CHAR16) + nbootorder;
-
-		newbootorder = AllocateZeroPool(n * sizeof (CHAR16));
-		if (!newbootorder)
-			return EFI_OUT_OF_RESOURCES;
-		CopyMem(newbootorder, bootorder, nbootorder * sizeof (CHAR16));
-		CopyMem(newbootorder + nbootorder, oldbootorder, size);
-		size = n * sizeof (CHAR16);
-	} else {
-		size = nbootorder * sizeof(CHAR16);
-		newbootorder = AllocateZeroPool(size);
-		if (!newbootorder)
-			return EFI_OUT_OF_RESOURCES;
-		CopyMem(newbootorder, bootorder, size);
-	}
+	size = nbootorder * sizeof(CHAR16);
+	newbootorder = AllocateZeroPool(size);
+	if (!newbootorder)
+		return EFI_OUT_OF_RESOURCES;
+	CopyMem(newbootorder, bootorder, size);
 
 #ifdef DEBUG_FALLBACK
 	Print(L"nbootorder: %d\nBootOrder: ", size / sizeof (CHAR16));
@@ -337,13 +326,11 @@ update_boot_order(void)
 		Print(L"%04x ", newbootorder[j]);
 	Print(L"\n");
 #endif
-
-	if (oldbootorder) {
+	rc = uefi_call_wrapper(RT->GetVariable, 5, L"BootOrder", &global,
+			       NULL, &len, NULL);
+	if (rc == EFI_BUFFER_TOO_SMALL)
 		LibDeleteVariable(L"BootOrder", &global);
-		FreePool(oldbootorder);
-	}
 
-	EFI_STATUS rc;
 	rc = uefi_call_wrapper(RT->SetVariable, 5, L"BootOrder", &global,
 					EFI_VARIABLE_NON_VOLATILE |
 					 EFI_VARIABLE_BOOTSERVICE_ACCESS |
-- 
1.9.3