File: discover-the-device-to-read-the-config-from-as-fallback.patch

package info (click to toggle)
grub2 2.14~git20250718.0e36779-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 60,688 kB
  • sloc: ansic: 541,811; asm: 68,074; sh: 9,803; cpp: 2,095; makefile: 1,895; python: 1,518; sed: 446; lex: 393; yacc: 268; awk: 85; lisp: 54; perl: 31
file content (125 lines) | stat: -rw-r--r-- 3,792 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
From: Javier Martinez Canillas <javierm@redhat.com>
Date: Mon, 9 Jan 2023 18:30:44 -0500
Subject: normal/main: Discover the device to read the config from as a
 fallback

When core.img is generated locally, the grub2-probe tool figures out the
device and partition that needs to be read to parse the GRUB
configuration file.

But in some cases the core.img can't be generated on the host and
instead has to be done at package build time.  In particular, this will
be true when it needs to be signed with a key that's only available on
the package building infrastructure.

In that case, the prefix variable won't have a device and partition but
only a directory path.  So there's no way for GRUB to know from which
device has to read the configuration file.

To allow GRUB to continue working on that scenario, fallback to
iterating over all the available devices if reading the config failed
when using the prefix and fw_path variables.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Co-authored-by: Robbie Harwood <rharwood@redhat.com>
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
 grub-core/normal/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 51 insertions(+), 7 deletions(-)

diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
index 355c2ab..7c6c048 100644
--- a/grub-core/normal/main.c
+++ b/grub-core/normal/main.c
@@ -311,18 +311,13 @@ grub_enter_normal_mode (const char *config)
 }
 
 static grub_err_t
-grub_try_normal (const char *variable)
+grub_try_normal_prefix (const char *prefix)
 {
     char *config;
-    const char *prefix;
     grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
     const char *net_search_cfg;
     int disable_net_search = 0;
 
-    prefix = grub_env_get (variable);
-    if (!prefix)
-      return GRUB_ERR_FILE_NOT_FOUND;
-
     net_search_cfg = grub_env_get ("feature_net_search_cfg");
     if (net_search_cfg && net_search_cfg[0] == 'n')
       disable_net_search = 1;
@@ -336,7 +331,7 @@ grub_try_normal (const char *variable)
        config = grub_malloc (config_len);
 
        if (! config)
-         return GRUB_ERR_FILE_NOT_FOUND;
+         return err;
 
        grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
        err = grub_net_search_config_file (config, config_len);
@@ -365,6 +360,53 @@ grub_try_normal (const char *variable)
     return err;
 }
 
+static int
+grub_try_normal_dev (const char *name, void *data)
+{
+  grub_err_t err;
+  const char *prefix = grub_xasprintf ("(%s)%s", name, (char *) data);
+
+  if (!prefix)
+    return 0;
+
+  err = grub_try_normal_prefix (prefix);
+  if (err == GRUB_ERR_NONE)
+    return 1;
+
+  return 0;
+}
+
+static grub_err_t
+grub_try_normal_discover (void)
+{
+  const char *prefix = grub_env_get ("prefix");
+  grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
+
+  if (!prefix)
+    return err;
+
+  if (grub_device_iterate (grub_try_normal_dev, (void *) prefix))
+    return GRUB_ERR_NONE;
+
+  return err;
+}
+
+static grub_err_t
+grub_try_normal (const char *variable)
+{
+  grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
+  const char *prefix;
+
+  if (!variable)
+    return err;
+
+  prefix = grub_env_get (variable);
+  if (!prefix)
+    return err;
+
+  return grub_try_normal_prefix (prefix);
+}
+
 /* Enter normal mode from rescue mode.  */
 static grub_err_t
 grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
@@ -379,6 +421,8 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
       err = grub_try_normal ("fw_path");
       if (err == GRUB_ERR_FILE_NOT_FOUND)
         err = grub_try_normal ("prefix");
+      if (err == GRUB_ERR_FILE_NOT_FOUND)
+        err = grub_try_normal_discover ();
       if (err == GRUB_ERR_FILE_NOT_FOUND)
         grub_enter_normal_mode (0);
     }