Blob Blame History Raw
From a8367596ca63156509eed0b332a077247765b811 Mon Sep 17 00:00:00 2001
From: Robert Marshall <rmarshall@redhat.com>
Date: Tue, 28 Feb 2017 18:31:56 -0500
Subject: [PATCH 40/55] Fix initial saved_entry read issue (#1285601)

If a system has never had a kernel upgrade/rollback then grubby
does not read saved_entry properly.  Added a guard to catch
saved_entry and read it to pick the proper index in these cases.

Resolves: rhbz#1285601
---
 grubby.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/grubby.c b/grubby.c
index 9ccad2735dd..974b81c7864 100644
--- a/grubby.c
+++ b/grubby.c
@@ -2522,6 +2522,9 @@ void setDefaultImage(struct grubConfig *config, int isAddingBootEntry,
 	struct singleEntry *bootEntry, *newDefault;
 	int indexToVerify, firstKernelEntryIndex, currentLookupIndex;
 
+        /* initialize */
+        currentLookupIndex = FIRST_ENTRY_INDEX;
+
 	/* handle the two cases where the user explictly picks the default
 	 * boot entry index as it would exist post-modification */
 
@@ -2587,8 +2590,30 @@ void setDefaultImage(struct grubConfig *config, int isAddingBootEntry,
 			config->defaultImage++;
 		}
 	} else {
-		/* use pre-existing default entry */
-		currentLookupIndex = config->defaultImage;
+                /* check to see if the default is stored in the environment */
+                if (config->defaultImage < FIRST_ENTRY_INDEX) {
+                    if (config->defaultImage == DEFAULT_SAVED || config->defaultImage == DEFAULT_SAVED_GRUB2)
+                    {
+                        if (config->cfi->defaultIsSaved) {
+                            if (config->cfi->getEnv) {
+                                char *defaultTitle = config->cfi->getEnv(config->cfi, "saved_entry");
+
+                                if (defaultTitle) {
+                                    if (isnumber(defaultTitle)) {
+                                        currentLookupIndex = atoi(defaultTitle);
+                                    } else {
+                                        findEntryByTitle(config, defaultTitle, &currentLookupIndex);
+                                    }
+                                    /* set the default Image to an actual index */
+                                    config->defaultImage = currentLookupIndex;
+                                }
+                            }
+                         }
+                    }
+                } else {
+                        /* use pre-existing default entry from the file*/
+                        currentLookupIndex = config->defaultImage;
+                }
 
 		if (isAddingBootEntry
 		    && (newBootEntryIndex <= config->defaultImage)) {
-- 
2.17.1