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