Static layout: Do not update PNOR when host is on

Static layout only has 1 active and functional PNOR. When host is
running, do not update PNOR.
This is done by checking the return value of freeSpace(), and if it
returns false, it means there is no space for PNOR because the host is
running and erase() returns false.

Tested: Verify the status becomes Failed when trying to activate a PNOR
        while host is running.

Change-Id: Ie2986b0c6fd29557685f67eb77ccc29709e1669a
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/item_updater.hpp b/item_updater.hpp
index 8b1bf0d..e27cccc 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -127,8 +127,10 @@
      *         needs to delete any PNOR version(s) it will delete the
      *         version(s) with the highest priority, skipping the
      *         functional PNOR version.
+     *
+     *  @return - Return if space is freed or not
      */
-    virtual void freeSpace() = 0;
+    virtual bool freeSpace() = 0;
 
     /** @brief Creates an active association to the
      *  newly active software image
diff --git a/static/activation_static.cpp b/static/activation_static.cpp
index 9768d41..193d6b3 100644
--- a/static/activation_static.cpp
+++ b/static/activation_static.cpp
@@ -19,6 +19,7 @@
 auto ActivationStatic::activation(Activations value) -> Activations
 {
 
+    auto ret = value;
     if (value != softwareServer::Activation::Activations::Active)
     {
         redundancyPriority.reset(nullptr);
@@ -26,9 +27,14 @@
 
     if (value == softwareServer::Activation::Activations::Activating)
     {
-        parent.freeSpace();
-        startActivation();
-        return softwareServer::Activation::activation(value);
+        if (parent.freeSpace())
+        {
+            startActivation();
+        }
+        else
+        {
+            ret = softwareServer::Activation::Activations::Failed;
+        }
     }
     else
     {
@@ -36,7 +42,7 @@
         activationProgress.reset(nullptr);
     }
 
-    return softwareServer::Activation::activation(value);
+    return softwareServer::Activation::activation(ret);
 }
 
 void ActivationStatic::startActivation()
diff --git a/static/item_updater_static.cpp b/static/item_updater_static.cpp
index 628f824..140d5cc 100644
--- a/static/item_updater_static.cpp
+++ b/static/item_updater_static.cpp
@@ -335,7 +335,7 @@
     // There is no implementation for this interface
 }
 
-void ItemUpdaterStatic::freeSpace()
+bool ItemUpdaterStatic::freeSpace()
 {
     // For now assume static layout only has 1 active PNOR,
     // so erase the active PNOR
@@ -344,10 +344,10 @@
         if (iter.second.get()->activation() ==
             server::Activation::Activations::Active)
         {
-            erase(iter.second->versionId);
-            break;
+            return erase(iter.second->versionId);
         }
     }
+    return false;
 }
 
 void ItemUpdaterStatic::updateFunctionalAssociation(
diff --git a/static/item_updater_static.hpp b/static/item_updater_static.hpp
index 75afeda..c3f273d 100644
--- a/static/item_updater_static.hpp
+++ b/static/item_updater_static.hpp
@@ -33,7 +33,7 @@
 
     void deleteAll() override;
 
-    void freeSpace() override;
+    bool freeSpace() override;
 
     void updateFunctionalAssociation(const std::string& versionId) override;
 
diff --git a/ubi/item_updater_ubi.cpp b/ubi/item_updater_ubi.cpp
index 875847f..61a8e01 100644
--- a/ubi/item_updater_ubi.cpp
+++ b/ubi/item_updater_ubi.cpp
@@ -357,8 +357,9 @@
 }
 
 // TODO: openbmc/openbmc#1402 Monitor flash usage
-void ItemUpdaterUbi::freeSpace()
+bool ItemUpdaterUbi::freeSpace()
 {
+    bool isSpaceFreed = false;
     //  Versions with the highest priority in front
     std::priority_queue<std::pair<int, std::string>,
                         std::vector<std::pair<int, std::string>>,
@@ -393,7 +394,9 @@
         erase(versionsPQ.top().second);
         versionsPQ.pop();
         count--;
+        isSpaceFreed = true;
     }
+    return isSpaceFreed;
 }
 
 std::string ItemUpdaterUbi::determineId(const std::string& symlinkPath)
diff --git a/ubi/item_updater_ubi.hpp b/ubi/item_updater_ubi.hpp
index 5de51d3..ce1b5b8 100644
--- a/ubi/item_updater_ubi.hpp
+++ b/ubi/item_updater_ubi.hpp
@@ -35,7 +35,7 @@
 
     void deleteAll() override;
 
-    void freeSpace() override;
+    bool freeSpace() override;
 
     bool isVersionFunctional(const std::string& versionId) override;