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

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