Blame SOURCES/0206-Allow-chainloading-EFI-apps-from-loop-mounts.patch

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Dimitri John Ledkov <xnox@ubuntu.com>
5593c8
Date: Fri, 11 Jun 2021 13:51:20 +0200
5593c8
Subject: [PATCH] Allow chainloading EFI apps from loop mounts.
5593c8
5593c8
---
5593c8
 grub-core/disk/loopback.c          |  9 +--------
5593c8
 grub-core/loader/efi/chainloader.c | 23 +++++++++++++++++++++++
5593c8
 include/grub/loopback.h            | 30 ++++++++++++++++++++++++++++++
5593c8
 3 files changed, 54 insertions(+), 8 deletions(-)
5593c8
 create mode 100644 include/grub/loopback.h
5593c8
5593c8
diff --git a/grub-core/disk/loopback.c b/grub-core/disk/loopback.c
5593c8
index 41bebd14fe3..99f47924ec2 100644
5593c8
--- a/grub-core/disk/loopback.c
5593c8
+++ b/grub-core/disk/loopback.c
5593c8
@@ -21,20 +21,13 @@
5593c8
 #include <grub/misc.h>
5593c8
 #include <grub/file.h>
5593c8
 #include <grub/disk.h>
5593c8
+#include <grub/loopback.h>
5593c8
 #include <grub/mm.h>
5593c8
 #include <grub/extcmd.h>
5593c8
 #include <grub/i18n.h>
5593c8
 
5593c8
 GRUB_MOD_LICENSE ("GPLv3+");
5593c8
 
5593c8
-struct grub_loopback
5593c8
-{
5593c8
-  char *devname;
5593c8
-  grub_file_t file;
5593c8
-  struct grub_loopback *next;
5593c8
-  unsigned long id;
5593c8
-};
5593c8
-
5593c8
 static struct grub_loopback *loopback_list;
5593c8
 static unsigned long last_id = 0;
5593c8
 
5593c8
diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c
5593c8
index d41e8ea14a8..3af6b122926 100644
5593c8
--- a/grub-core/loader/efi/chainloader.c
5593c8
+++ b/grub-core/loader/efi/chainloader.c
5593c8
@@ -24,6 +24,7 @@
5593c8
 #include <grub/err.h>
5593c8
 #include <grub/device.h>
5593c8
 #include <grub/disk.h>
5593c8
+#include <grub/loopback.h>
5593c8
 #include <grub/misc.h>
5593c8
 #include <grub/charset.h>
5593c8
 #include <grub/mm.h>
5593c8
@@ -901,6 +902,7 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
   grub_efi_status_t status;
5593c8
   grub_efi_boot_services_t *b;
5593c8
   grub_device_t dev = 0;
5593c8
+  grub_device_t orig_dev = 0;
5593c8
   grub_efi_device_path_t *dp = 0;
5593c8
   char *filename;
5593c8
   void *boot_image = 0;
5593c8
@@ -958,6 +960,15 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
   if (! dev)
5593c8
     goto fail;
5593c8
 
5593c8
+  /* if device is loopback, use underlying dev */
5593c8
+  if (dev->disk->dev->id == GRUB_DISK_DEVICE_LOOPBACK_ID)
5593c8
+    {
5593c8
+      struct grub_loopback *d;
5593c8
+      orig_dev = dev;
5593c8
+      d = dev->disk->data;
5593c8
+      dev = d->file->device;
5593c8
+    }
5593c8
+
5593c8
   if (dev->disk)
5593c8
     dev_handle = grub_efidisk_get_device_handle (dev->disk);
5593c8
   else if (dev->net && dev->net->server)
5593c8
@@ -1065,6 +1076,12 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
     }
5593c8
 #endif
5593c8
 
5593c8
+  if (orig_dev)
5593c8
+    {
5593c8
+      dev = orig_dev;
5593c8
+      orig_dev = 0;
5593c8
+    }
5593c8
+
5593c8
   rc = grub_linuxefi_secure_validate((void *)(unsigned long)address, fsize);
5593c8
   grub_dprintf ("chain", "linuxefi_secure_validate: %d\n", rc);
5593c8
   if (rc > 0)
5593c8
@@ -1087,6 +1104,12 @@ grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
5593c8
   // -1 fall-through to fail
5593c8
 
5593c8
 fail:
5593c8
+  if (orig_dev)
5593c8
+    {
5593c8
+      dev = orig_dev;
5593c8
+      orig_dev = 0;
5593c8
+    }
5593c8
+
5593c8
   if (dev)
5593c8
     grub_device_close (dev);
5593c8
 
5593c8
diff --git a/include/grub/loopback.h b/include/grub/loopback.h
5593c8
new file mode 100644
5593c8
index 00000000000..3b9a9e32e80
5593c8
--- /dev/null
5593c8
+++ b/include/grub/loopback.h
5593c8
@@ -0,0 +1,30 @@
5593c8
+/*
5593c8
+ *  GRUB  --  GRand Unified Bootloader
5593c8
+ *  Copyright (C) 2019  Free Software Foundation, Inc.
5593c8
+ *
5593c8
+ *  GRUB is free software: you can redistribute it and/or modify
5593c8
+ *  it under the terms of the GNU General Public License as published by
5593c8
+ *  the Free Software Foundation, either version 3 of the License, or
5593c8
+ *  (at your option) any later version.
5593c8
+ *
5593c8
+ *  GRUB is distributed in the hope that it will be useful,
5593c8
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
5593c8
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5593c8
+ *  GNU General Public License for more details.
5593c8
+ *
5593c8
+ *  You should have received a copy of the GNU General Public License
5593c8
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
5593c8
+ */
5593c8
+
5593c8
+#ifndef GRUB_LOOPBACK_HEADER
5593c8
+#define GRUB_LOOPBACK_HEADER	1
5593c8
+
5593c8
+struct grub_loopback
5593c8
+{
5593c8
+  char *devname;
5593c8
+  grub_file_t file;
5593c8
+  struct grub_loopback *next;
5593c8
+  unsigned long id;
5593c8
+};
5593c8
+
5593c8
+#endif /* ! GRUB_LOOPBACK_HEADER */