a5bd9f6
From 7916d7c3a1e92dfbd8720a6275d7f23025abe612 Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Tue, 26 Mar 2013 11:34:56 +0100
a5bd9f6
Subject: [PATCH 236/364] 	* grub-core/kern/efi/mm.c
a5bd9f6
 (grub_efi_finish_boot_services): 	Try terminating EFI services several
a5bd9f6
 times due to quirks in some 	implementations.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog               |  6 ++++++
a5bd9f6
 grub-core/kern/efi/mm.c | 46 ++++++++++++++++++++++++++++++----------------
a5bd9f6
 2 files changed, 36 insertions(+), 16 deletions(-)
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index 07a69bc..58c2242 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,3 +1,9 @@
a5bd9f6
+2013-03-26  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
+	* grub-core/kern/efi/mm.c (grub_efi_finish_boot_services):
a5bd9f6
+	Try terminating EFI services several times due to quirks in some
a5bd9f6
+	implementations.
a5bd9f6
+
a5bd9f6
 2013-03-26  Colin Watson  <cjwatson@ubuntu.com>
a5bd9f6
 
a5bd9f6
 	* grub-core/commands/acpihalt.c (skip_ext_op): Add support for
a5bd9f6
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
a5bd9f6
index 351317b..77c9384 100644
a5bd9f6
--- a/grub-core/kern/efi/mm.c
a5bd9f6
+++ b/grub-core/kern/efi/mm.c
a5bd9f6
@@ -160,27 +160,41 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
a5bd9f6
 			   apple, sizeof (apple)) == 0);
a5bd9f6
 #endif
a5bd9f6
 
a5bd9f6
-  if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
a5bd9f6
-			       &finish_desc_size, &finish_desc_version) < 0)
a5bd9f6
-    return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
a5bd9f6
+  while (1)
a5bd9f6
+    {
a5bd9f6
+      if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
a5bd9f6
+				   &finish_desc_size, &finish_desc_version) < 0)
a5bd9f6
+	return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
a5bd9f6
 
a5bd9f6
-  if (outbuf && *outbuf_size < finish_mmap_size)
a5bd9f6
-    return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
a5bd9f6
+      if (outbuf && *outbuf_size < finish_mmap_size)
a5bd9f6
+	return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
a5bd9f6
 
a5bd9f6
-  finish_mmap_buf = grub_malloc (finish_mmap_size);
a5bd9f6
-  if (!finish_mmap_buf)
a5bd9f6
-    return grub_errno;
a5bd9f6
+      finish_mmap_buf = grub_malloc (finish_mmap_size);
a5bd9f6
+      if (!finish_mmap_buf)
a5bd9f6
+	return grub_errno;
a5bd9f6
 
a5bd9f6
-  if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
a5bd9f6
-			       &finish_desc_size, &finish_desc_version) <= 0)
a5bd9f6
-    return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
a5bd9f6
+      if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
a5bd9f6
+				   &finish_desc_size, &finish_desc_version) <= 0)
a5bd9f6
+	{
a5bd9f6
+	  grub_free (finish_mmap_buf);
a5bd9f6
+	  return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
a5bd9f6
+	}
a5bd9f6
 
a5bd9f6
-  b = grub_efi_system_table->boot_services;
a5bd9f6
-  status = efi_call_2 (b->exit_boot_services, grub_efi_image_handle,
a5bd9f6
-		       finish_key);
a5bd9f6
-  if (status != GRUB_EFI_SUCCESS)
a5bd9f6
-    return grub_error (GRUB_ERR_IO, "couldn't terminate EFI services");
a5bd9f6
+      b = grub_efi_system_table->boot_services;
a5bd9f6
+      status = efi_call_2 (b->exit_boot_services, grub_efi_image_handle,
a5bd9f6
+			   finish_key);
a5bd9f6
+      if (status == GRUB_EFI_SUCCESS)
a5bd9f6
+	break;
a5bd9f6
 
a5bd9f6
+      if (status != GRUB_EFI_INVALID_PARAMETER)
a5bd9f6
+	{
a5bd9f6
+	  grub_free (finish_mmap_buf);
a5bd9f6
+	  return grub_error (GRUB_ERR_IO, "couldn't terminate EFI services");
a5bd9f6
+	}
a5bd9f6
+
a5bd9f6
+      grub_free (finish_mmap_buf);
a5bd9f6
+      grub_printf ("Trying to terminate EFI services again\n");
a5bd9f6
+    }
a5bd9f6
   grub_efi_is_finished = 1;
a5bd9f6
   if (outbuf_size)
a5bd9f6
     *outbuf_size = finish_mmap_size;
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6