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

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