cca0c4
From a8367596ca63156509eed0b332a077247765b811 Mon Sep 17 00:00:00 2001
cca0c4
From: Robert Marshall <rmarshall@redhat.com>
cca0c4
Date: Tue, 28 Feb 2017 18:31:56 -0500
cca0c4
Subject: [PATCH 40/55] Fix initial saved_entry read issue (#1285601)
cca0c4
cca0c4
If a system has never had a kernel upgrade/rollback then grubby
cca0c4
does not read saved_entry properly.  Added a guard to catch
cca0c4
saved_entry and read it to pick the proper index in these cases.
cca0c4
cca0c4
Resolves: rhbz#1285601
cca0c4
---
cca0c4
 grubby.c | 29 +++++++++++++++++++++++++++--
cca0c4
 1 file changed, 27 insertions(+), 2 deletions(-)
cca0c4
cca0c4
diff --git a/grubby.c b/grubby.c
cca0c4
index 9ccad2735dd..974b81c7864 100644
cca0c4
--- a/grubby.c
cca0c4
+++ b/grubby.c
cca0c4
@@ -2522,6 +2522,9 @@ void setDefaultImage(struct grubConfig *config, int isAddingBootEntry,
cca0c4
 	struct singleEntry *bootEntry, *newDefault;
cca0c4
 	int indexToVerify, firstKernelEntryIndex, currentLookupIndex;
cca0c4
 
cca0c4
+        /* initialize */
cca0c4
+        currentLookupIndex = FIRST_ENTRY_INDEX;
cca0c4
+
cca0c4
 	/* handle the two cases where the user explictly picks the default
cca0c4
 	 * boot entry index as it would exist post-modification */
cca0c4
 
cca0c4
@@ -2587,8 +2590,30 @@ void setDefaultImage(struct grubConfig *config, int isAddingBootEntry,
cca0c4
 			config->defaultImage++;
cca0c4
 		}
cca0c4
 	} else {
cca0c4
-		/* use pre-existing default entry */
cca0c4
-		currentLookupIndex = config->defaultImage;
cca0c4
+                /* check to see if the default is stored in the environment */
cca0c4
+                if (config->defaultImage < FIRST_ENTRY_INDEX) {
cca0c4
+                    if (config->defaultImage == DEFAULT_SAVED || config->defaultImage == DEFAULT_SAVED_GRUB2)
cca0c4
+                    {
cca0c4
+                        if (config->cfi->defaultIsSaved) {
cca0c4
+                            if (config->cfi->getEnv) {
cca0c4
+                                char *defaultTitle = config->cfi->getEnv(config->cfi, "saved_entry");
cca0c4
+
cca0c4
+                                if (defaultTitle) {
cca0c4
+                                    if (isnumber(defaultTitle)) {
cca0c4
+                                        currentLookupIndex = atoi(defaultTitle);
cca0c4
+                                    } else {
cca0c4
+                                        findEntryByTitle(config, defaultTitle, &currentLookupIndex);
cca0c4
+                                    }
cca0c4
+                                    /* set the default Image to an actual index */
cca0c4
+                                    config->defaultImage = currentLookupIndex;
cca0c4
+                                }
cca0c4
+                            }
cca0c4
+                         }
cca0c4
+                    }
cca0c4
+                } else {
cca0c4
+                        /* use pre-existing default entry from the file*/
cca0c4
+                        currentLookupIndex = config->defaultImage;
cca0c4
+                }
cca0c4
 
cca0c4
 		if (isAddingBootEntry
cca0c4
 		    && (newBootEntryIndex <= config->defaultImage)) {
cca0c4
-- 
cca0c4
2.17.1
cca0c4