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

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