ItemUpdater: Validate PNOR squashfs image

- Validate the image dir by checking its content.
- Set Activation to "Ready" if validation passed or
  else set it to "Invalid".

Resolves openbmc/openbmc#1356

Change-Id: Ica73ae5604e74db11cd58c223d82265309ea76a6
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/activation.hpp b/activation.hpp
index 5c6c5fe..4945531 100755
--- a/activation.hpp
+++ b/activation.hpp
@@ -32,16 +32,19 @@
          * @param[in] path   - The Dbus object path
          * @param[in] versionId  - The software version id
          * @param[in] extVersion - The extended version
+         * @param[in] activationStatus - The status of Activation
          */
         Activation(sdbusplus::bus::bus& bus, const std::string& path,
                    std::string& versionId,
-                   std::string& extVersion) :
+                   std::string& extVersion,
+                   sdbusplus::xyz::openbmc_project::Software::
+                   server::Activation::Activations activationStatus) :
                    ActivationInherit(bus, path.c_str(), true),
                    versionId(versionId)
         {
             // Set Properties.
             extendedVersion(extVersion);
-
+            activation(activationStatus);
             // Emit deferred signal.
             emit_object_added();
         }
diff --git a/configure.ac b/configure.ac
index c8c90a7..5253703 100755
--- a/configure.ac
+++ b/configure.ac
@@ -64,9 +64,13 @@
     [The item updater DBus busname to own.])
 
 AC_ARG_VAR(MANIFEST_FILE, [The path to the MANIFEST file])
-AS_IF([test "x$MANIFEST_FILE" == "x"], [MANIFEST_FILE="/tmp/pnor/MANIFEST"])
+AS_IF([test "x$MANIFEST_FILE" == "x"], [MANIFEST_FILE="/tmp/images/MANIFEST"])
 AC_DEFINE_UNQUOTED([MANIFEST_FILE], ["$MANIFEST_FILE"], [The path to the MANIFEST file])
 
+AC_ARG_VAR(IMAGE_DIR, [The path to all the IMAGE files])
+AS_IF([test "x$IMAGE_DIR" == "x"], [IMAGE_DIR="/tmp/images/"])
+AC_DEFINE_UNQUOTED([IMAGE_DIR], ["$IMAGE_DIR"], [The path to all the IMAGE files])
+
 AC_DEFINE(MAPPER_BUSNAME, "xyz.openbmc_project.ObjectMapper",
     [The object mapper busname.])
 AC_DEFINE(MAPPER_PATH, "/xyz/openbmc_project/object_mapper",
diff --git a/item_updater.cpp b/item_updater.cpp
index 42e7729..7a5629d 100755
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -3,6 +3,7 @@
 #include <phosphor-logging/log.hpp>
 #include "config.h"
 #include "item_updater.hpp"
+#include "activation.hpp"
 
 namespace openpower
 {
@@ -11,8 +12,13 @@
 namespace updater
 {
 
+// When you see server:: you know we're referencing our base class
+namespace server = sdbusplus::xyz::openbmc_project::Software::server;
+
 using namespace phosphor::logging;
 
+constexpr auto squashFSImage = "pnor.xz.squashfs";
+
 int ItemUpdater::createActivation(sd_bus_message* msg,
                                   void* userData,
                                   sd_bus_error* retErr)
@@ -59,6 +65,14 @@
         }
 
         auto versionId = resp.substr(pos + 1);
+
+        // Determine the Activation state by processing the given image dir.
+        auto activationState = server::Activation::Activations::Invalid;
+        if (ItemUpdater::validateSquashFSImage(versionId) == 0)
+        {
+            activationState = server::Activation::Activations::Ready;
+        }
+
         if (updater->activations.find(versionId) == updater->activations.end())
         {
             updater->activations.insert(std::make_pair(
@@ -67,7 +81,8 @@
                             updater->busItem,
                             resp,
                             versionId,
-                            extendedVersion)));
+                            extendedVersion,
+                            activationState)));
         }
     }
     return 0;
@@ -109,10 +124,26 @@
     {
         log<level::ERR>("Error in reading Host MANIFEST file");
     }
-
     return extendedVersion;
 }
 
+int ItemUpdater::validateSquashFSImage(const std::string& versionId)
+{
+    auto file = IMAGE_DIR + versionId + "/" +
+                std::string(squashFSImage);
+    std::ifstream efile(file.c_str());
+
+    if (efile.good() == 1)
+    {
+        return 0;
+    }
+    else
+    {
+        log<level::ERR>("Failed to find the SquashFS image.");
+        return -1;
+    }
+}
+
 } // namespace updater
 } // namespace software
 } // namespace openpower
diff --git a/item_updater.hpp b/item_updater.hpp
index 5afb96d..8e79d1d 100755
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -61,6 +61,14 @@
          */
         static std::string getExtendedVersion(const std::string&
                                                manifestFilePath);
+        /**
+         * @brief Validates the presence of SquashFS iamge in the image dir.
+         *
+         * @param[in]  versionId - The software version ID.
+         * @param[out] result    - 0 --> if validation was successful
+         *                       - -1--> Otherwise
+         */
+        static int validateSquashFSImage(const std::string& versionId);
 
         /** @brief Persistent sdbusplus DBus bus connection. */
         sdbusplus::bus::bus& busItem;