f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Peter Jones <pjones@redhat.com>
f725e3
Date: Fri, 3 Oct 2014 11:08:03 -0400
f725e3
Subject: [PATCH] Try $prefix if $fw_path doesn't work.
f725e3
f725e3
Related: rhbz#1148652
f725e3
f725e3
Signed-off-by: Peter Jones <pjones@redhat.com>
f725e3
---
f725e3
 grub-core/kern/ieee1275/init.c |  28 +++++-----
f725e3
 grub-core/net/net.c            |   2 +-
f725e3
 grub-core/normal/main.c        | 120 ++++++++++++++++++++---------------------
f725e3
 3 files changed, 75 insertions(+), 75 deletions(-)
f725e3
f725e3
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
f725e3
index 8191f8cd658..8ca4bf79f09 100644
f725e3
--- a/grub-core/kern/ieee1275/init.c
f725e3
+++ b/grub-core/kern/ieee1275/init.c
f725e3
@@ -131,23 +131,25 @@ grub_machine_get_bootlocation (char **device, char **path)
f725e3
       grub_free (canon);
f725e3
     }
f725e3
   else
f725e3
-    *device = grub_ieee1275_encode_devname (bootpath);
f725e3
-  grub_free (type);
f725e3
-
f725e3
-  filename = grub_ieee1275_get_filename (bootpath);
f725e3
-  if (filename)
f725e3
     {
f725e3
-      char *lastslash = grub_strrchr (filename, '\\');
f725e3
-
f725e3
-      /* Truncate at last directory.  */
f725e3
-      if (lastslash)
f725e3
+      filename = grub_ieee1275_get_filename (bootpath);
f725e3
+      if (filename)
f725e3
         {
f725e3
-	  *lastslash = '\0';
f725e3
-	  grub_translate_ieee1275_path (filename);
f725e3
+          char *lastslash = grub_strrchr (filename, '\\');
f725e3
 
f725e3
-	  *path = filename;
f725e3
-	}
f725e3
+          /* Truncate at last directory.  */
f725e3
+          if (lastslash)
f725e3
+            {
f725e3
+              *lastslash = '\0';
f725e3
+              grub_translate_ieee1275_path (filename);
f725e3
+
f725e3
+              *path = filename;
f725e3
+            }
f725e3
+        }
f725e3
+      *device = grub_ieee1275_encode_devname (bootpath);
f725e3
     }
f725e3
+
f725e3
+  grub_free (type);
f725e3
   grub_free (bootpath);
f725e3
 }
f725e3
 
f725e3
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
f725e3
index 578e057e18f..10bfed31b2b 100644
f725e3
--- a/grub-core/net/net.c
f725e3
+++ b/grub-core/net/net.c
f725e3
@@ -1817,7 +1817,7 @@ grub_net_search_configfile (char *config)
f725e3
   /* Remove the remaining minus sign at the end. */
f725e3
   config[config_len] = '\0';
f725e3
 
f725e3
-  return GRUB_ERR_NONE;
f725e3
+  return GRUB_ERR_FILE_NOT_FOUND;
f725e3
 }
f725e3
 
f725e3
 static struct grub_preboot *fini_hnd;
f725e3
diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
f725e3
index 4190985ad37..aa14499996b 100644
f725e3
--- a/grub-core/normal/main.c
f725e3
+++ b/grub-core/normal/main.c
f725e3
@@ -331,74 +331,72 @@ grub_enter_normal_mode (const char *config)
f725e3
   grub_boot_time ("Exiting normal mode");
f725e3
 }
f725e3
 
f725e3
+static grub_err_t
f725e3
+grub_try_normal (const char *variable)
f725e3
+{
f725e3
+    char *config;
f725e3
+    const char *prefix;
f725e3
+    grub_err_t err = GRUB_ERR_FILE_NOT_FOUND;
f725e3
+
f725e3
+    prefix = grub_env_get (variable);
f725e3
+    if (!prefix)
f725e3
+      return GRUB_ERR_FILE_NOT_FOUND;
f725e3
+
f725e3
+    if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) == 0)
f725e3
+      {
f725e3
+	grub_size_t config_len;
f725e3
+	config_len = grub_strlen (prefix) +
f725e3
+	  sizeof ("/grub.cfg-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
f725e3
+	config = grub_malloc (config_len);
f725e3
+
f725e3
+	if (! config)
f725e3
+	  return GRUB_ERR_FILE_NOT_FOUND;
f725e3
+
f725e3
+	grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
f725e3
+	err = grub_net_search_configfile (config);
f725e3
+      }
f725e3
+
f725e3
+    if (err != GRUB_ERR_NONE)
f725e3
+      {
f725e3
+	config = grub_xasprintf ("%s/grub.cfg", prefix);
f725e3
+	if (config)
f725e3
+	  {
f725e3
+	    grub_file_t file;
f725e3
+	    file = grub_file_open (config);
f725e3
+	    if (file)
f725e3
+	      {
f725e3
+		grub_file_close (file);
f725e3
+		err = GRUB_ERR_NONE;
f725e3
+	      }
f725e3
+	  }
f725e3
+      }
f725e3
+
f725e3
+    if (err == GRUB_ERR_NONE)
f725e3
+      grub_enter_normal_mode (config);
f725e3
+
f725e3
+    grub_errno = 0;
f725e3
+    grub_free (config);
f725e3
+    return err;
f725e3
+}
f725e3
+
f725e3
 /* Enter normal mode from rescue mode.  */
f725e3
 static grub_err_t
f725e3
 grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
f725e3
 		 int argc, char *argv[])
f725e3
 {
f725e3
-  if (argc == 0)
f725e3
-    {
f725e3
-      /* Guess the config filename. It is necessary to make CONFIG static,
f725e3
-	 so that it won't get broken by longjmp.  */
f725e3
-      char *config;
f725e3
-      const char *prefix;
f725e3
-
f725e3
-      prefix = grub_env_get ("fw_path");
f725e3
-      if (! prefix)
f725e3
-	      prefix = grub_env_get ("prefix");
f725e3
-
f725e3
-      if (prefix)
f725e3
-	{
f725e3
-	  if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) == 0)
f725e3
-	    {
f725e3
-	      grub_size_t config_len;
f725e3
-	      config_len = grub_strlen (prefix) +
f725e3
-		sizeof ("/grub.cfg-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
f725e3
-	      config = grub_malloc (config_len);
f725e3
-
f725e3
-	      if (! config)
f725e3
-		goto quit;
f725e3
-
f725e3
-	      grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
f725e3
-
f725e3
-	      grub_net_search_configfile (config);
f725e3
-
f725e3
-	      grub_enter_normal_mode (config);
f725e3
-	      grub_free (config);
f725e3
-	      config = NULL;
f725e3
-	    }
f725e3
-
f725e3
-	  if (!config)
f725e3
-	    {
f725e3
-	      config = grub_xasprintf ("%s/grub.cfg", prefix);
f725e3
-	      if (config)
f725e3
-		{
f725e3
-		  grub_file_t file;
f725e3
-
f725e3
-		  file = grub_file_open (config);
f725e3
-		  if (file)
f725e3
-		    {
f725e3
-		      grub_file_close (file);
f725e3
-		      grub_enter_normal_mode (config);
f725e3
-		    }
f725e3
-		  else
f725e3
-		    {
f725e3
-		      /*  Ignore all errors.  */
f725e3
-		      grub_errno = 0;
f725e3
-		    }
f725e3
-		  grub_free (config);
f725e3
-		}
f725e3
-	    }
f725e3
-	}
f725e3
-      else
f725e3
-	{
f725e3
-	  grub_enter_normal_mode (0);
f725e3
-	}
f725e3
-    }
f725e3
-  else
f725e3
+  if (argc)
f725e3
     grub_enter_normal_mode (argv[0]);
f725e3
+  else
f725e3
+    {
f725e3
+      /* Guess the config filename. */
f725e3
+      grub_err_t err;
f725e3
+      err = grub_try_normal ("fw_path");
f725e3
+      if (err == GRUB_ERR_FILE_NOT_FOUND)
f725e3
+        err = grub_try_normal ("prefix");
f725e3
+      if (err == GRUB_ERR_FILE_NOT_FOUND)
f725e3
+        grub_enter_normal_mode (0);
f725e3
+    }
f725e3
 
f725e3
-quit:
f725e3
   return 0;
f725e3
 }
f725e3