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/image_manager.cpp b/image_manager.cpp
index 357d3c2..a64c826 100644
--- a/image_manager.cpp
+++ b/image_manager.cpp
@@ -184,6 +184,15 @@
     {
         return;
     }
+
+    if (it->second->isFunctional())
+    {
+        log<level::ERR>(("Error: Version " + entryId + \
+                         " is currently running on the BMC." \
+                         " Unable to remove.").c_str());
+        return;
+    }
+
     // Delete image dir
     fs::path imageDirPath = (*(it->second)).path();
     if (fs::exists(imageDirPath))
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);
 }
diff --git a/version.cpp b/version.cpp
index b564416..e6dd9de 100644
--- a/version.cpp
+++ b/version.cpp
@@ -4,6 +4,7 @@
 #include <fstream>
 #include <stdexcept>
 #include <phosphor-logging/log.hpp>
+#include "config.h"
 #include "version.hpp"
 
 namespace phosphor
@@ -101,6 +102,11 @@
     return version;
 }
 
+bool Version::isFunctional()
+{
+    return versionStr == getBMCVersion(OS_RELEASE_FILE);
+}
+
 } // namespace manager
 } // namespace software
 } // namepsace phosphor
diff --git a/version.hpp b/version.hpp
index 3d17213..1e69a6d 100644
--- a/version.hpp
+++ b/version.hpp
@@ -39,7 +39,7 @@
                 const std::string& versionId,
                 VersionPurpose versionPurpose,
                 const std::string& filePath) : VersionInherit(
-                    bus, (objPath).c_str(), true)
+                    bus, (objPath).c_str(), true), versionStr(versionId)
         {
             // Set properties.
             purpose(versionPurpose);
@@ -73,6 +73,17 @@
          * @return The version identifier.
          */
         static std::string getBMCVersion(const std::string& releaseFilePath);
+
+        /* @brief Check if this version matches the currently running version
+         *
+         * @return - Returns true if this version matches the currently running
+         *           version.
+         */
+        bool isFunctional();
+
+    private:
+        /** @brief This Version's version string */
+        const std::string versionStr;
 };
 
 } // namespace manager