Blob Blame History Raw
From 332ec67718959920c26283854ec9280e4023a437 Mon Sep 17 00:00:00 2001
From: Robert Marshall <rmarshall@redhat.com>
Date: Mon, 6 Mar 2017 15:57:54 -0500
Subject: [PATCH] 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 de76b56..4d3834b 100644
--- a/grubby.c
+++ b/grubby.c
@@ -2620,6 +2620,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 */
 
@@ -2685,8 +2688,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.9.3