Blame SOURCES/0450-loader-efi-chainloader-grub_load_and_start_image-doe.patch

d18179
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d18179
From: Chris Coulson <chris.coulson@canonical.com>
d18179
Date: Thu, 28 Apr 2022 21:53:36 +0100
d18179
Subject: [PATCH] loader/efi/chainloader: grub_load_and_start_image doesn't
d18179
 load and start
d18179
d18179
grub_load_and_start_image only loads an image - it still requires the
d18179
caller to start it. This renames it to grub_load_image.
d18179
d18179
It's called from 2 places:
d18179
- grub_cmd_chainloader when not using the shim protocol.
d18179
- grub_secureboot_chainloader_boot if handle_image returns an error.
d18179
In this case, the image is loaded and then nothing else happens which
d18179
seems strange. I assume the intention is that it falls back to LoadImage
d18179
and StartImage if handle_image fails, so I've made it do that.
d18179
d18179
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
d18179
(cherry picked from commit b4d70820a65c00561045856b7b8355461a9545f6)
d18179
(cherry picked from commit 05b16a6be50b1910609740a66b561276fa490538)
d18179
(cherry picked from commit 16486a34f3aa41a94e334e86db1a1e21e9b0a45f)
d18179
(cherry picked from commit 0bbd693019567b2c55e8be121378d4875f44369a)
d18179
[rharwood: chainloader handle_image casting]
d18179
---
d18179
 grub-core/loader/efi/chainloader.c | 18 ++++++++++++++----
d18179
 1 file changed, 14 insertions(+), 4 deletions(-)
d18179
d18179
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
d18179
index 001766bdf6..eeef52acde 100644
d18179
--- a/grub-core/loader/efi/chainloader.c
d18179
+++ b/grub-core/loader/efi/chainloader.c
d18179
@@ -840,7 +840,7 @@ grub_secureboot_chainloader_unload (void)
d18179
 }
d18179
 
d18179
 static grub_err_t
d18179
-grub_load_and_start_image(void *boot_image)
d18179
+grub_load_image(void *boot_image)
d18179
 {
d18179
   grub_efi_boot_services_t *b;
d18179
   grub_efi_status_t status;
d18179
@@ -882,13 +882,23 @@ grub_load_and_start_image(void *boot_image)
d18179
 static grub_err_t
d18179
 grub_secureboot_chainloader_boot (void)
d18179
 {
d18179
+  grub_efi_boot_services_t *b;
d18179
   int rc;
d18179
-  rc = handle_image ((void *)address, fsize);
d18179
+
d18179
+  rc = handle_image ((void *)(unsigned long)address, fsize);
d18179
   if (rc == 0)
d18179
     {
d18179
-      grub_load_and_start_image((void *)address);
d18179
+      /* We weren't able to attempt to execute the image, so fall back
d18179
+       * to LoadImage / StartImage.
d18179
+       */
d18179
+      rc = grub_load_image((void *)(unsigned long)address);
d18179
+      if (rc == 0)
d18179
+        grub_chainloader_boot ();
d18179
     }
d18179
 
d18179
+  b = grub_efi_system_table->boot_services;
d18179
+  efi_call_1 (b->unload_image, image_handle);
d18179
+
d18179
   grub_loader_unset ();
d18179
   return grub_errno;
d18179
 }
d18179
@@ -1076,7 +1086,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
d18179
     }
d18179
   else if (rc == 0)
d18179
     {
d18179
-      grub_load_and_start_image(boot_image);
d18179
+      grub_load_image(boot_image);
d18179
       grub_file_close (file);
d18179
       grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
d18179