nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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