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