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