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