Blame SOURCES/0486-normal-main-Discover-the-device-to-read-the-config-f.patch

6bd801
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
6bd801
From: Javier Martinez Canillas <javierm@redhat.com>
6bd801
Date: Thu, 8 Jul 2021 16:39:39 +0200
6bd801
Subject: [PATCH] normal/main: Discover the device to read the config from as a
6bd801
 fallback
6bd801
6bd801
The GRUB core.img is generated locally, when this is done the grub2-probe
6bd801
tool figures out the device and partition that needs to be read to parse
6bd801
the GRUB configuration file.
6bd801
6bd801
But in some cases the core.img can't be generated on the host and instead
6bd801
has to be done at package build time. For example, if needs to get signed
6bd801
with a key that's only available on the package building infrastructure.
6bd801
6bd801
If that's the case, the prefix variable won't have a device and partition
6bd801
but only a directory path. So there's no way for GRUB to know from which
6bd801
device has to read the configuration file.
6bd801
6bd801
To allow GRUB to continue working on that scenario, fallback to iterating
6bd801
over all the available devices, if reading the config failed when using
6bd801
the prefix and fw_path variables.
6bd801
6bd801
Related: rhbz#1899864
6bd801
6bd801
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
6bd801
---
6bd801
 grub-core/normal/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++------
6bd801
 1 file changed, 51 insertions(+), 7 deletions(-)
6bd801
6bd801
diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
b71686
index 49141039f..93f33c167 100644
6bd801
--- a/grub-core/normal/main.c
6bd801
+++ b/grub-core/normal/main.c
6bd801
@@ -333,16 +333,11 @@ grub_enter_normal_mode (const char *config)
6bd801
 }
6bd801
 
6bd801
 static grub_err_t
6bd801
-grub_try_normal (const char *variable)
6bd801
+grub_try_normal_prefix (const char *prefix)
6bd801
 {
6bd801
     char *config;
6bd801
-    const char *prefix;
6bd801
     grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
6bd801
 
6bd801
-    prefix = grub_env_get (variable);
6bd801
-    if (!prefix)
6bd801
-      return GRUB_ERR_FILE_NOT_FOUND;
6bd801
-
6bd801
     if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) == 0)
6bd801
       {
6bd801
 	grub_size_t config_len;
6bd801
@@ -351,7 +346,7 @@ grub_try_normal (const char *variable)
6bd801
 	config = grub_malloc (config_len);
6bd801
 
6bd801
 	if (! config)
6bd801
-	  return GRUB_ERR_FILE_NOT_FOUND;
6bd801
+	  return err;
6bd801
 
6bd801
 	grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
6bd801
 	err = grub_net_search_configfile (config);
6bd801
@@ -380,6 +375,53 @@ grub_try_normal (const char *variable)
6bd801
     return err;
6bd801
 }
6bd801
 
6bd801
+static int
6bd801
+grub_try_normal_dev (const char *name, void *data)
6bd801
+{
6bd801
+  grub_err_t err;
6bd801
+  const char *prefix = grub_xasprintf ("(%s)%s", name, (char *)data);
6bd801
+
6bd801
+  if (!prefix)
6bd801
+    return 0;
6bd801
+
6bd801
+  err = grub_try_normal_prefix (prefix);
6bd801
+  if (err == GRUB_ERR_NONE)
6bd801
+    return 1;
6bd801
+
6bd801
+  return 0;
6bd801
+}
6bd801
+
6bd801
+static grub_err_t
6bd801
+grub_try_normal_discover (void)
6bd801
+{
6bd801
+  char *prefix = grub_env_get ("prefix");
6bd801
+  grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
6bd801
+
6bd801
+  if (!prefix)
6bd801
+    return err;
6bd801
+
6bd801
+  if (grub_device_iterate (grub_try_normal_dev, (void *)prefix))
6bd801
+    return GRUB_ERR_NONE;
6bd801
+
6bd801
+  return err;
6bd801
+}
6bd801
+
6bd801
+static grub_err_t
6bd801
+grub_try_normal (const char *variable)
6bd801
+{
6bd801
+  grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
6bd801
+  const char *prefix;
6bd801
+
6bd801
+  if (!variable)
6bd801
+    return err;
6bd801
+
6bd801
+  prefix = grub_env_get (variable);
6bd801
+  if (!prefix)
6bd801
+    return err;
6bd801
+
6bd801
+  return grub_try_normal_prefix (prefix);
6bd801
+}
6bd801
+
6bd801
 /* Enter normal mode from rescue mode.  */
6bd801
 static grub_err_t
6bd801
 grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
6bd801
@@ -394,6 +436,8 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
6bd801
       err = grub_try_normal ("fw_path");
6bd801
       if (err == GRUB_ERR_FILE_NOT_FOUND)
6bd801
         err = grub_try_normal ("prefix");
6bd801
+      if (err == GRUB_ERR_FILE_NOT_FOUND)
6bd801
+        err = grub_try_normal_discover ();
6bd801
       if (err == GRUB_ERR_FILE_NOT_FOUND)
6bd801
         grub_enter_normal_mode (0);
6bd801
     }