Add checks for currently running bmc image before erasing

- Check if the image to be erased is currently running on the BMC. If it
  is, fail to erase the image.

Change-Id: Ief2ba2e5e16f6664eeb429699b826cae78ef9291
Signed-off-by: Eddie James <eajames@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index 4507bcf..6b4ff5e 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -235,9 +235,33 @@
 
 void ItemUpdater::erase(std::string entryId)
 {
-    // Delete ReadOnly partitions
-    removeReadOnlyPartition(entryId);
-    removeFile(entryId);
+    // Find entry in versions map
+    auto it = versions.find(entryId);
+    if (it != versions.end())
+    {
+        if (it->second->isFunctional())
+        {
+            log<level::ERR>(("Error: Version " + entryId + \
+                             " is currently running on the BMC." \
+                             " Unable to remove.").c_str());
+             return;
+        }
+
+        // Delete ReadOnly partitions if it's not active
+        removeReadOnlyPartition(entryId);
+        removeFile(entryId);
+    }
+    else
+    {
+        // Delete ReadOnly partitions even if we can't find the version
+        removeReadOnlyPartition(entryId);
+        removeFile(entryId);
+
+        log<level::ERR>(("Error: Failed to find version " + entryId + \
+                         " in item updater versions map." \
+                         " Unable to remove.").c_str());
+        return;
+    }
 
     // Remove the priority environment variable.
     auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
@@ -250,14 +274,6 @@
     bus.call_noreply(method);
 
     // Removing entry in versions map
-    auto it = versions.find(entryId);
-    if (it == versions.end())
-    {
-        log<level::ERR>(("Error: Failed to find version " + entryId + \
-                         " in item updater versions map." \
-                         " Unable to remove.").c_str());
-        return;
-    }
     this->versions.erase(entryId);
 
     // Removing entry in activations map
@@ -269,9 +285,6 @@
                          " Unable to remove.").c_str());
         return;
     }
-    // TODO: openbmc/openbmc#1986
-    //       Test if this is the currently running image
-    //       If not, don't continue.
 
     this->activations.erase(entryId);
 }