item_updater: erase: Call resetUbootEnvVars() first

During erase, move the resetUbootEnvVars() call from the end to
the beginning so that the BMC points to a valid image to boot
from. If resetUbootEnvVars() is called after the image is actually
deleted from the BMC flash, there'd be a time window where the BMC
would be pointing to a non-existent image to boot from.

Change-Id: I98c69033ee0e9c6e8752200f65f54cfe0f56d130
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index 8b860b5..4b133f3 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -332,7 +332,30 @@
                             entry("VERSIONID=%s", entryId.c_str()));
             return;
         }
+    }
 
+    // First call resetUbootEnvVars() so that the BMC points to a valid image to
+    // boot from. If resetUbootEnvVars() is called after the image is actually
+    // deleted from the BMC flash, there'd be a time window where the BMC would
+    // be pointing to a non-existent image to boot from.
+    // Need to remove the entries from the activations map before that call so
+    // that resetUbootEnvVars() doesn't use the version to be deleted.
+    auto iteratorActivations = activations.find(entryId);
+    if (iteratorActivations == activations.end())
+    {
+        log<level::ERR>("Error: Failed to find version in item updater "
+                        "activations map. Unable to remove.",
+                        entry("VERSIONID=%s", entryId.c_str()));
+    }
+    else
+    {
+        removeAssociations(iteratorActivations->second->path);
+        this->activations.erase(entryId);
+    }
+    ItemUpdater::resetUbootEnvVars();
+
+    if (it != versions.end())
+    {
         // Delete ReadOnly partitions if it's not active
         removeReadOnlyPartition(entryId);
         removePersistDataDirectory(entryId);
@@ -353,20 +376,6 @@
 
     helper.clearEntry(entryId);
 
-    // Removing entry in activations map
-    auto ita = activations.find(entryId);
-    if (ita == activations.end())
-    {
-        log<level::ERR>("Error: Failed to find version in item updater "
-                        "activations map. Unable to remove.",
-                        entry("VERSIONID=%s", entryId.c_str()));
-    }
-    else
-    {
-        removeAssociations(ita->second->path);
-        this->activations.erase(entryId);
-    }
-    ItemUpdater::resetUbootEnvVars();
     return;
 }