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

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