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