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