Blame SOURCES/0160-Use-boot-loader-entries-as-BLS-directory-path-also-o.patch

d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Javier Martinez Canillas <javierm@redhat.com>
d9d99f
Date: Mon, 25 Jun 2018 11:45:33 +0200
d9d99f
Subject: [PATCH] Use /boot/loader/entries as BLS directory path also on EFI
d9d99f
 systems
d9d99f
d9d99f
For EFI systems, the BLS fragments were stored in the EFI System Partition
d9d99f
(ESP) while in non-EFI systems it was stored in /boot.
d9d99f
d9d99f
For consistency, it's better to always store the BLS fragments in the same
d9d99f
path regardless of the firmware interface used.
d9d99f
d9d99f
Also change the grub2-switch-to-blscfg script default BLS directory.
d9d99f
d9d99f
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
d9d99f
---
d9d99f
 grub-core/commands/blscfg.c   | 48 ++++++++++++++++++++++++++++++++-----------
d9d99f
 util/grub-switch-to-blscfg.in |  4 ++--
d9d99f
 2 files changed, 38 insertions(+), 14 deletions(-)
d9d99f
d9d99f
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
d9d99f
index 831cdcaccdf..70939a81826 100644
d9d99f
--- a/grub-core/commands/blscfg.c
d9d99f
+++ b/grub-core/commands/blscfg.c
d9d99f
@@ -381,9 +381,14 @@ static int bls_cmp(const void *p0, const void *p1, void *state UNUSED)
d9d99f
   return rc;
d9d99f
 }
d9d99f
 
d9d99f
+struct read_entry_info {
d9d99f
+  const char *devid;
d9d99f
+  const char *dirname;
d9d99f
+};
d9d99f
+
d9d99f
 static int read_entry (
d9d99f
     const char *filename,
d9d99f
-    const struct grub_dirhook_info *info UNUSED,
d9d99f
+    const struct grub_dirhook_info *dirhook_info UNUSED,
d9d99f
     void *data)
d9d99f
 {
d9d99f
   grub_size_t n;
d9d99f
@@ -391,8 +396,7 @@ static int read_entry (
d9d99f
   grub_file_t f = NULL;
d9d99f
   grub_off_t sz;
d9d99f
   struct bls_entry *entry;
d9d99f
-  const char *dirname= (const char *)data;
d9d99f
-  const char *devid = grub_env_get ("boot");
d9d99f
+  struct read_entry_info *info = (struct read_entry_info *)data;
d9d99f
 
d9d99f
   grub_dprintf ("blscfg", "filename: \"%s\"\n", filename);
d9d99f
 
d9d99f
@@ -406,7 +410,7 @@ static int read_entry (
d9d99f
   if (grub_strcmp (filename + n - 5, ".conf") != 0)
d9d99f
     return 0;
d9d99f
 
d9d99f
-  p = grub_xasprintf ("(%s)%s/%s", devid, dirname, filename);
d9d99f
+  p = grub_xasprintf ("(%s)%s/%s", info->devid, info->dirname, filename);
d9d99f
 
d9d99f
   f = grub_file_open (p);
d9d99f
   if (!f)
d9d99f
@@ -655,10 +659,13 @@ static int find_entry (const char *filename,
d9d99f
 		       void *data)
d9d99f
 {
d9d99f
   struct find_entry_info *info = (struct find_entry_info *)data;
d9d99f
+  struct read_entry_info read_entry_info;
d9d99f
   grub_file_t f = NULL;
d9d99f
   char *grubenv_path = NULL;
d9d99f
   grub_envblk_t env = NULL;
d9d99f
   char *default_blsdir = NULL;
d9d99f
+  grub_fs_t blsdir_fs = NULL;
d9d99f
+  grub_device_t blsdir_dev = NULL;
d9d99f
   const char *blsdir = NULL;
d9d99f
   char *saved_env_buf = NULL;
d9d99f
   int r = 0;
d9d99f
@@ -678,9 +685,6 @@ static int find_entry (const char *filename,
d9d99f
   if (info->platform == PLATFORM_EMU)
d9d99f
     default_blsdir = grub_xasprintf ("%s%s", GRUB_BOOT_DEVICE,
d9d99f
 				     GRUB_BLS_CONFIG_PATH);
d9d99f
-  else if (info->platform == PLATFORM_EFI)
d9d99f
-    default_blsdir = grub_xasprintf ("/EFI/%s%s", filename,
d9d99f
-				     GRUB_BLS_CONFIG_PATH);
d9d99f
   else
d9d99f
     default_blsdir = grub_xasprintf ("%s", GRUB_BLS_CONFIG_PATH);
d9d99f
 
d9d99f
@@ -744,16 +748,33 @@ static int find_entry (const char *filename,
d9d99f
     goto finish;
d9d99f
 
d9d99f
   grub_dprintf ("blscfg", "blsdir: \"%s\"\n", blsdir);
d9d99f
-  if (blsdir[0] != '/' && info->platform == PLATFORM_EFI)
d9d99f
-    blsdir = grub_xasprintf ("/EFI/%s/%s/", filename, blsdir);
d9d99f
-  else
d9d99f
-    blsdir = grub_strdup (blsdir);
d9d99f
+  blsdir = grub_strdup (blsdir);
d9d99f
 
d9d99f
   if (!blsdir)
d9d99f
     goto finish;
d9d99f
 
d9d99f
   grub_dprintf ("blscfg", "blsdir: \"%s\"\n", blsdir);
d9d99f
-  r = info->fs->dir (info->dev, blsdir, read_entry, (char *)blsdir);
d9d99f
+  if (info->platform == PLATFORM_EFI) {
d9d99f
+    read_entry_info.devid = grub_env_get ("root");
d9d99f
+    if (!read_entry_info.devid)
d9d99f
+      goto finish;
d9d99f
+
d9d99f
+    blsdir_dev = grub_device_open (read_entry_info.devid);
d9d99f
+    if (!blsdir_dev)
d9d99f
+      goto finish;
d9d99f
+
d9d99f
+    blsdir_fs = grub_fs_probe (blsdir_dev);
d9d99f
+    if (!blsdir_fs)
d9d99f
+      goto finish;
d9d99f
+
d9d99f
+  } else {
d9d99f
+    read_entry_info.devid = devid;
d9d99f
+    blsdir_dev = info->dev;
d9d99f
+    blsdir_fs = info->fs;
d9d99f
+  }
d9d99f
+  read_entry_info.dirname = blsdir;
d9d99f
+
d9d99f
+  r = blsdir_fs->dir (blsdir_dev, blsdir, read_entry, &read_entry_info);
d9d99f
   if (r != 0) {
d9d99f
       grub_dprintf ("blscfg", "read_entry returned error\n");
d9d99f
       grub_err_t e;
d9d99f
@@ -773,6 +794,9 @@ static int find_entry (const char *filename,
d9d99f
   for (r = 0; r < nentries; r++)
d9d99f
       bls_free_entry (entries[r]);
d9d99f
 finish:
d9d99f
+  if (info->platform == PLATFORM_EFI && blsdir_dev)
d9d99f
+    grub_device_close (blsdir_dev);
d9d99f
+
d9d99f
   nentries = 0;
d9d99f
 
d9d99f
   grub_free (entries);
d9d99f
diff --git a/util/grub-switch-to-blscfg.in b/util/grub-switch-to-blscfg.in
d9d99f
index 884cf45b1e1..2f37a1f740b 100644
d9d99f
--- a/util/grub-switch-to-blscfg.in
d9d99f
+++ b/util/grub-switch-to-blscfg.in
d9d99f
@@ -44,13 +44,13 @@ EFIDIR=$(grep ^ID= /etc/os-release | sed -e 's/^ID=//' -e 's/rhel/redhat/')
d9d99f
 if [ -d /sys/firmware/efi/efivars/ ]; then
d9d99f
     startlink=/etc/grub2-efi.cfg
d9d99f
     grubdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/" | sed 's,//*,/,g'`
d9d99f
-    blsdir=`echo "/@bootdirname@/efi/EFI/${EFIDIR}/loader/entries" | sed 's,//*,/,g'`
d9d99f
 else
d9d99f
     startlink=/etc/grub2.cfg
d9d99f
     grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
d9d99f
-    blsdir=`echo "/@bootdirname@/loader/entries" | sed 's,//*,/,g'`
d9d99f
 fi
d9d99f
 
d9d99f
+blsdir=`echo "/@bootdirname@/loader/entries" | sed 's,//*,/,g'`
d9d99f
+
d9d99f
 backupsuffix=.bak
d9d99f
 
d9d99f
 export TEXTDOMAIN=@PACKAGE@