1From 1469983ebb9674753ad333d37087fb8cb20e1dce Mon Sep 17 00:00:00 2001
2From: Chris Coulson <chris.coulson@canonical.com>
3Date: Tue, 5 Apr 2022 10:02:04 +0100
4Subject: [PATCH] loader/efi/chainloader: Simplify the loader state
5
6The chainloader command retains the source buffer and device path passed
7to LoadImage(), requiring the unload hook passed to grub_loader_set() to
8free them. It isn't required to retain this state though - they aren't
9required by StartImage() or anything else in the boot hook, so clean them
10up before grub_cmd_chainloader() finishes.
11
12Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
13Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
14
15Upstream-Status: Backport
16
17Reference to upstream patch:
18https://git.savannah.gnu.org/cgit/grub.git/commit/?id=1469983ebb9674753ad333d37087fb8cb20e1dce
19
20Signed-off-by: Xiangyu Chen <xiangyu.chen@windriver.com>
21---
22 grub-core/loader/efi/chainloader.c | 38 +++++++++++++++++-------------
23 1 file changed, 21 insertions(+), 17 deletions(-)
24
25diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
26index 2bd80f4db..d1602c89b 100644
27--- a/grub-core/loader/efi/chainloader.c
28+++ b/grub-core/loader/efi/chainloader.c
29@@ -44,25 +44,20 @@ GRUB_MOD_LICENSE ("GPLv3+");
30
31 static grub_dl_t my_mod;
32
33-static grub_efi_physical_address_t address;
34-static grub_efi_uintn_t pages;
35-static grub_efi_device_path_t *file_path;
36 static grub_efi_handle_t image_handle;
37-static grub_efi_char16_t *cmdline;
38
39 static grub_err_t
40 grub_chainloader_unload (void)
41 {
42+  grub_efi_loaded_image_t *loaded_image;
43   grub_efi_boot_services_t *b;
44
45+  loaded_image = grub_efi_get_loaded_image (image_handle);
46+  if (loaded_image != NULL)
47+    grub_free (loaded_image->load_options);
48+
49   b = grub_efi_system_table->boot_services;
50   efi_call_1 (b->unload_image, image_handle);
51-  efi_call_2 (b->free_pages, address, pages);
52-
53-  grub_free (file_path);
54-  grub_free (cmdline);
55-  cmdline = 0;
56-  file_path = 0;
57
58   grub_dl_unref (my_mod);
59   return GRUB_ERR_NONE;
60@@ -140,7 +135,7 @@ make_file_path (grub_efi_device_path_t *dp, const char *filename)
61   char *dir_start;
62   char *dir_end;
63   grub_size_t size;
64-  grub_efi_device_path_t *d;
65+  grub_efi_device_path_t *d, *file_path;
66
67   dir_start = grub_strchr (filename, ')');
68   if (! dir_start)
69@@ -222,11 +217,14 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
70   grub_efi_status_t status;
71   grub_efi_boot_services_t *b;
72   grub_device_t dev = 0;
73-  grub_efi_device_path_t *dp = 0;
74+  grub_efi_device_path_t *dp = NULL, *file_path = NULL;
75   grub_efi_loaded_image_t *loaded_image;
76   char *filename;
77   void *boot_image = 0;
78   grub_efi_handle_t dev_handle = 0;
79+  grub_efi_physical_address_t address = 0;
80+  grub_efi_uintn_t pages = 0;
81+  grub_efi_char16_t *cmdline = NULL;
82
83   if (argc == 0)
84     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
85@@ -234,11 +232,6 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
86
87   grub_dl_ref (my_mod);
88
89-  /* Initialize some global variables.  */
90-  address = 0;
91-  image_handle = 0;
92-  file_path = 0;
93-
94   b = grub_efi_system_table->boot_services;
95
96   file = grub_file_open (filename, GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE);
97@@ -408,6 +401,10 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
98   grub_file_close (file);
99   grub_device_close (dev);
100
101+  /* We're finished with the source image buffer and file path now. */
102+  efi_call_2 (b->free_pages, address, pages);
103+  grub_free (file_path);
104+
105   grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
106   return 0;
107
108@@ -419,11 +416,18 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
109   if (file)
110     grub_file_close (file);
111
112+  grub_free (cmdline);
113   grub_free (file_path);
114
115   if (address)
116     efi_call_2 (b->free_pages, address, pages);
117
118+  if (image_handle != NULL)
119+    {
120+      efi_call_1 (b->unload_image, image_handle);
121+      image_handle = NULL;
122+    }
123+
124   grub_dl_unref (my_mod);
125
126   return grub_errno;
127--
1282.34.1
129
130