Blame SOURCES/0040-Fix-initial-saved_entry-read-issue-1285601.patch

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