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