Blame SOURCES/0212-blscfg-remove-logic-to-read-the-grubenv-file-and-set.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Javier Martinez Canillas <javierm@redhat.com>
8631a2
Date: Sat, 28 Jul 2018 23:57:15 +0200
8631a2
Subject: [PATCH] blscfg: remove logic to read the grubenv file and set the
8631a2
 blsdir variable
8631a2
8631a2
The BLS grub2 support has a blsdir environment variable that can be set by
8631a2
users to override the BLS fragment default path.
8631a2
8631a2
Currently the BLS parsing code reads the grubenv file and sets the blsdir
8631a2
variable, but it shouldn't be the responsability of the blscfg module to
8631a2
do this and instead just use it if the variable has been set (either from
8631a2
the grub.cfg file or the grub shell).
8631a2
8631a2
This makes the find_entry() function much simpler and consistent for EFI,
8631a2
BIOS and grub-emu. It also fixes a bug that caused having menu entries to
8631a2
be repeated for each sub-directory that existed under the /EFI directory.
8631a2
8631a2
So for example having three different operating systems sharing the ESP,
8631a2
would lead to the boot menu entries being repeated three times for grub.
8631a2
8631a2
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
8631a2
---
8631a2
 grub-core/commands/blscfg.c | 179 ++++----------------------------------------
8631a2
 1 file changed, 16 insertions(+), 163 deletions(-)
8631a2
8631a2
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
8631a2
index 69bfb5db295..bdb1c5a95aa 100644
8631a2
--- a/grub-core/commands/blscfg.c
8631a2
+++ b/grub-core/commands/blscfg.c
8631a2
@@ -45,13 +45,6 @@ GRUB_MOD_LICENSE ("GPLv3+");
8631a2
 #define GRUB_BOOT_DEVICE "($root)"
8631a2
 #endif
8631a2
 
8631a2
-enum
8631a2
-  {
8631a2
-    PLATFORM_EFI,
8631a2
-    PLATFORM_EMU,
8631a2
-    PLATFORM_BIOS,
8631a2
-  };
8631a2
-
8631a2
 #define grub_free(x) ({grub_dprintf("blscfg", "%s freeing %p\n", __func__, x); grub_free(x); })
8631a2
 
8631a2
 struct keyval
8631a2
@@ -666,137 +659,37 @@ finish:
8631a2
 }
8631a2
 
8631a2
 struct find_entry_info {
8631a2
+	const char *devid;
8631a2
 	grub_device_t dev;
8631a2
 	grub_fs_t fs;
8631a2
 	int platform;
8631a2
 };
8631a2
 
8631a2
 /*
8631a2
- * filename: if the directory is /EFI/something/ , filename is "something"
8631a2
- * info: unused
8631a2
- * data: the filesystem object the file is on.
8631a2
+ * info: the filesystem object the file is on.
8631a2
  */
8631a2
-static int find_entry (const char *filename,
8631a2
-		       const struct grub_dirhook_info *dirhook_info UNUSED,
8631a2
-		       void *data)
8631a2
+static int find_entry (struct find_entry_info *info)
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
-  const 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
   bool use_version = true;
8631a2
   int fallback = 0;
8631a2
   int r = 0;
8631a2
-  const char *devid = grub_env_get ("boot");
8631a2
-
8631a2
-  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
-  if (filename && (!grub_strcmp (filename, ".") ||
8631a2
-		   !grub_strcmp (filename, "..")))
8631a2
-    return 0;
8631a2
-
8631a2
-  if (info->platform == PLATFORM_EFI && !grub_strcasecmp (filename, "boot"))
8631a2
-    return 0;
8631a2
-
8631a2
-  saved_env_buf = grub_malloc (512);
8631a2
-
8631a2
-  // set a default blsdir
8631a2
-  if (info->platform == PLATFORM_EMU)
8631a2
-    default_blsdir = GRUB_BOOT_DEVICE GRUB_BLS_CONFIG_PATH;
8631a2
-  else
8631a2
-    default_blsdir = GRUB_BLS_CONFIG_PATH;
8631a2
-
8631a2
-  grub_env_set ("blsdir", default_blsdir);
8631a2
-  grub_dprintf ("blscfg", "default_blsdir: \"%s\"\n", default_blsdir);
8631a2
-
8631a2
-  /*
8631a2
-   * try to load a grubenv from /EFI/wherever/grubenv
8631a2
-   */
8631a2
-  if (info->platform == PLATFORM_EFI)
8631a2
-    grubenv_path = grub_xasprintf ("(%s)/EFI/%s/grubenv", devid, filename);
8631a2
-  else
8631a2
-    grubenv_path = grub_xasprintf ("(%s)/grub2/grubenv", devid);
8631a2
-
8631a2
-  grub_dprintf ("blscfg", "looking for \"%s\"\n", grubenv_path);
8631a2
-  f = grub_file_open (grubenv_path);
8631a2
-
8631a2
-  grub_dprintf ("blscfg", "%s it\n", f ? "found" : "did not find");
8631a2
-  grub_free (grubenv_path);
8631a2
-  if (f)
8631a2
-    {
8631a2
-      grub_off_t sz;
8631a2
-
8631a2
-      grub_dprintf ("blscfg", "getting size\n");
8631a2
-      sz = grub_file_size (f);
8631a2
-      if (sz == GRUB_FILE_SIZE_UNKNOWN || sz > 1024*1024)
8631a2
-	goto finish;
8631a2
-
8631a2
-      grub_dprintf ("blscfg", "reading env\n");
8631a2
-      env = read_envblk_file (f);
8631a2
-      if (!env)
8631a2
-	goto finish;
8631a2
-      grub_dprintf ("blscfg", "read env file\n");
8631a2
-
8631a2
-      grub_memset (saved_env_buf, '#', 512);
8631a2
-      grub_memcpy (saved_env_buf, GRUB_ENVBLK_SIGNATURE,
8631a2
-		   sizeof (GRUB_ENVBLK_SIGNATURE));
8631a2
-      grub_dprintf ("blscfg", "saving env\n");
8631a2
-      saved_env = grub_envblk_open (saved_env_buf, 512);
8631a2
-      if (!saved_env)
8631a2
-	goto finish;
8631a2
-
8631a2
-      // save everything listed in "env" with values from our existing grub env
8631a2
-      grub_envblk_iterate (env, NULL, save_var);
8631a2
-      // set everything from our loaded grubenv into the real grub env
8631a2
-      grub_envblk_iterate (env, NULL, set_var);
8631a2
-    }
8631a2
-  else
8631a2
-    {
8631a2
-      grub_err_t e;
8631a2
-      grub_dprintf ("blscfg", "no such file\n");
8631a2
-      do
8631a2
-	{
8631a2
-	  e = grub_error_pop();
8631a2
-	} while (e);
8631a2
-
8631a2
-    }
8631a2
 
8631a2
   blsdir = grub_env_get ("blsdir");
8631a2
   if (!blsdir)
8631a2
-    goto finish;
8631a2
+    blsdir = GRUB_BLS_CONFIG_PATH;
8631a2
 
8631a2
-  grub_dprintf ("blscfg", "blsdir: \"%s\"\n", blsdir);
8631a2
-  blsdir = grub_strdup (blsdir);
8631a2
-
8631a2
-  if (!blsdir)
8631a2
-    goto finish;
8631a2
-
8631a2
-  grub_dprintf ("blscfg", "blsdir: \"%s\"\n", 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
+  grub_dprintf ("blscfg", "scanning blsdir: %s\n", GRUB_BLS_CONFIG_PATH);
8631a2
+
8631a2
+  blsdir_dev = info->dev;
8631a2
+  blsdir_fs = info->fs;
8631a2
+  read_entry_info.devid = info->devid;
8631a2
+
8631a2
 read_fallback:
8631a2
   r = blsdir_fs->dir (blsdir_dev, read_entry_info.dirname, read_entry,
8631a2
 		      &read_entry_info);
8631a2
@@ -809,7 +702,7 @@ read_fallback:
8631a2
 	} while (e);
8631a2
   }
8631a2
 
8631a2
-  if (!nentries && !fallback && info->platform != PLATFORM_EMU) {
8631a2
+  if (!nentries && !fallback) {
8631a2
     read_entry_info.dirname = "/boot" GRUB_BLS_CONFIG_PATH;
8631a2
     grub_dprintf ("blscfg", "Entries weren't found in %s, fallback to %s\n",
8631a2
 		  blsdir, read_entry_info.dirname);
8631a2
@@ -832,41 +725,12 @@ read_fallback:
8631a2
 
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
   entries = NULL;
8631a2
 
8631a2
-  grub_free ((char *)blsdir);
8631a2
-
8631a2
-  grub_env_unset ("blsdir");
8631a2
-
8631a2
-  if (saved_env)
8631a2
-    {
8631a2
-      // remove everything from the real environment that's defined in env
8631a2
-      grub_envblk_iterate (env, NULL, unset_var);
8631a2
-
8631a2
-      // re-set the things from our original environment
8631a2
-      grub_envblk_iterate (saved_env, NULL, set_var);
8631a2
-      grub_envblk_close (saved_env);
8631a2
-      saved_env = NULL;
8631a2
-    }
8631a2
-  else if (saved_env_buf)
8631a2
-    {
8631a2
-      // if we have a saved environment, grub_envblk_close() freed this.
8631a2
-      grub_free (saved_env_buf);
8631a2
-    }
8631a2
-
8631a2
-  if (env)
8631a2
-    grub_envblk_close (env);
8631a2
-
8631a2
-  if (f)
8631a2
-    grub_file_close (f);
8631a2
-
8631a2
   return 0;
8631a2
 }
8631a2
 
8631a2
@@ -883,7 +747,6 @@ grub_cmd_blscfg (grub_extcmd_context_t ctxt UNUSED,
8631a2
     {
8631a2
       .dev = NULL,
8631a2
       .fs = NULL,
8631a2
-      .platform = PLATFORM_BIOS,
8631a2
     };
8631a2
 
8631a2
 
8631a2
@@ -891,13 +754,14 @@ grub_cmd_blscfg (grub_extcmd_context_t ctxt UNUSED,
8631a2
 
8631a2
 #ifdef GRUB_MACHINE_EMU
8631a2
   devid = "host";
8631a2
-  grub_env_set ("boot", devid);
8631a2
+#elif defined(GRUB_MACHINE_EFI)
8631a2
+  devid = grub_env_get ("root");
8631a2
 #else
8631a2
   devid = grub_env_get ("boot");
8631a2
+#endif
8631a2
   if (!devid)
8631a2
     return grub_error (GRUB_ERR_FILE_NOT_FOUND,
8631a2
 		       N_("variable `%s' isn't set"), "boot");
8631a2
-#endif
8631a2
 
8631a2
   grub_dprintf ("blscfg", "opening %s\n", devid);
8631a2
   dev = grub_device_open (devid);
8631a2
@@ -912,21 +776,10 @@ grub_cmd_blscfg (grub_extcmd_context_t ctxt UNUSED,
8631a2
       goto finish;
8631a2
     }
8631a2
 
8631a2
+  info.devid = devid;
8631a2
   info.dev = dev;
8631a2
   info.fs = fs;
8631a2
-#ifdef GRUB_MACHINE_EFI
8631a2
-  info.platform = PLATFORM_EFI;
8631a2
-  grub_dprintf ("blscfg", "scanning /EFI/\n");
8631a2
-  r = fs->dir (dev, "/EFI/", find_entry, &info;;
8631a2
-#elif defined(GRUB_MACHINE_EMU)
8631a2
-  info.platform = PLATFORM_EMU;
8631a2
-  grub_dprintf ("blscfg", "scanning %s%s\n", GRUB_BOOT_DEVICE,
8631a2
-		GRUB_BLS_CONFIG_PATH);
8631a2
-  find_entry(NULL, NULL, &info;;
8631a2
-#else
8631a2
-  grub_dprintf ("blscfg", "scanning %s\n", GRUB_BLS_CONFIG_PATH);
8631a2
-  find_entry(NULL, NULL, &info;;
8631a2
-#endif
8631a2
+  find_entry(&info;;
8631a2
 
8631a2
 finish:
8631a2
   if (dev)