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

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Javier Martinez Canillas <javierm@redhat.com>
5593c8
Date: Mon, 30 Aug 2021 12:31:18 +0200
5593c8
Subject: [PATCH] normal/main: Discover the device to read the config from as a
5593c8
 fallback
5593c8
5593c8
The GRUB core.img is generated locally, when this is done the grub2-probe
5593c8
tool figures out the device and partition that needs to be read to parse
5593c8
the GRUB configuration file.
5593c8
5593c8
But in some cases the core.img can't be generated on the host and instead
5593c8
has to be done at package build time. For example, if needs to get signed
5593c8
with a key that's only available on the package building infrastructure.
5593c8
5593c8
If that's the case, the prefix variable won't have a device and partition
5593c8
but only a directory path. So there's no way for GRUB to know from which
5593c8
device has to read the configuration file.
5593c8
5593c8
To allow GRUB to continue working on that scenario, fallback to iterating
5593c8
over all the available devices, if reading the config failed when using
5593c8
the prefix and fw_path variables.
5593c8
5593c8
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
5593c8
---
5593c8
 grub-core/normal/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++------
5593c8
 1 file changed, 51 insertions(+), 7 deletions(-)
5593c8
5593c8
diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
d3c3ab
index 155bf366da2..f9ccca502ee 100644
5593c8
--- a/grub-core/normal/main.c
5593c8
+++ b/grub-core/normal/main.c
5593c8
@@ -339,18 +339,13 @@ grub_enter_normal_mode (const char *config)
5593c8
 }
5593c8
 
5593c8
 static grub_err_t
5593c8
-grub_try_normal (const char *variable)
5593c8
+grub_try_normal_prefix (const char *prefix)
5593c8
 {
5593c8
     char *config;
5593c8
-    const char *prefix;
5593c8
     grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
5593c8
     const char *net_search_cfg;
5593c8
     int disable_net_search = 0;
5593c8
 
5593c8
-    prefix = grub_env_get (variable);
5593c8
-    if (!prefix)
5593c8
-      return GRUB_ERR_FILE_NOT_FOUND;
5593c8
-
5593c8
     net_search_cfg = grub_env_get ("feature_net_search_cfg");
5593c8
     if (net_search_cfg && net_search_cfg[0] == 'n')
5593c8
       disable_net_search = 1;
5593c8
@@ -364,7 +359,7 @@ grub_try_normal (const char *variable)
5593c8
        config = grub_malloc (config_len);
5593c8
 
5593c8
        if (! config)
5593c8
-         return GRUB_ERR_FILE_NOT_FOUND;
5593c8
+         return err;
5593c8
 
5593c8
        grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
5593c8
        err = grub_net_search_config_file (config);
5593c8
@@ -393,6 +388,53 @@ grub_try_normal (const char *variable)
5593c8
     return err;
5593c8
 }
5593c8
 
5593c8
+static int
5593c8
+grub_try_normal_dev (const char *name, void *data)
5593c8
+{
5593c8
+  grub_err_t err;
5593c8
+  const char *prefix = grub_xasprintf ("(%s)%s", name, (char *)data);
5593c8
+
5593c8
+  if (!prefix)
5593c8
+    return 0;
5593c8
+
5593c8
+  err = grub_try_normal_prefix (prefix);
5593c8
+  if (err == GRUB_ERR_NONE)
5593c8
+    return 1;
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+grub_try_normal_discover (void)
5593c8
+{
5593c8
+  char *prefix = grub_env_get ("prefix");
5593c8
+  grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
5593c8
+
5593c8
+  if (!prefix)
5593c8
+    return err;
5593c8
+
5593c8
+  if (grub_device_iterate (grub_try_normal_dev, (void *)prefix))
5593c8
+    return GRUB_ERR_NONE;
5593c8
+
5593c8
+  return err;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+grub_try_normal (const char *variable)
5593c8
+{
5593c8
+  grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
5593c8
+  const char *prefix;
5593c8
+
5593c8
+  if (!variable)
5593c8
+    return err;
5593c8
+
5593c8
+  prefix = grub_env_get (variable);
5593c8
+  if (!prefix)
5593c8
+    return err;
5593c8
+
5593c8
+  return grub_try_normal_prefix (prefix);
5593c8
+}
5593c8
+
5593c8
 /* Enter normal mode from rescue mode.  */
5593c8
 static grub_err_t
5593c8
 grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
5593c8
@@ -407,6 +449,8 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
5593c8
       err = grub_try_normal ("fw_path");
5593c8
       if (err == GRUB_ERR_FILE_NOT_FOUND)
5593c8
         err = grub_try_normal ("prefix");
5593c8
+      if (err == GRUB_ERR_FILE_NOT_FOUND)
5593c8
+        err = grub_try_normal_discover ();
5593c8
       if (err == GRUB_ERR_FILE_NOT_FOUND)
5593c8
         grub_enter_normal_mode (0);
5593c8
     }