Scan directories that store PSU images on start

When the service starts, scan the directories that store PSU images,
including the built-in images, and the saved images during PSU update.

When the scanned image is different than the running images, create
activation/version object;
When the scanned image is the same as the running images, update the
version object's path to indicate the PSU image path, so it could be
used for future update in case a PSU is replaced with a different
software.

Tested: On Witherspoon, fake create a dummy PSU image with a different
        version than running PSU, verify a new object is created on
        restart;
        fake creating a dummy PSU image with a same version as a running
        PSU, verify no new object is created, but the "Path" property is
        set to the PSU image directory.

Signed-off-by: Lei YU <mine260309@gmail.com>
Change-Id: I860b978250a718eb82d948a1c88bd8f41bb2b2e3
diff --git a/src/item_updater.cpp b/src/item_updater.cpp
index 8d5695c..6472fe1 100644
--- a/src/item_updater.cpp
+++ b/src/item_updater.cpp
@@ -11,8 +11,9 @@
 
 namespace
 {
-constexpr auto EXTENDED_VERSION = "extended_version";
-}
+constexpr auto MANIFEST_VERSION = "version";
+constexpr auto MANIFEST_EXTENDED_VERSION = "extended_version";
+} // namespace
 
 namespace phosphor
 {
@@ -21,7 +22,6 @@
 namespace updater
 {
 namespace server = sdbusplus::xyz::openbmc_project::Software::server;
-namespace fs = std::filesystem;
 
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 using namespace phosphor::logging;
@@ -95,7 +95,7 @@
     {
         // Determine the Activation state by processing the given image dir.
         AssociationList associations;
-        auto activationState = server::Activation::Activations::Ready;
+        auto activationState = Activation::Status::Ready;
 
         associations.emplace_back(std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
                                                   ACTIVATION_REV_ASSOCIATION,
@@ -103,14 +103,8 @@
 
         fs::path manifestPath(filePath);
         manifestPath /= MANIFEST_FILE;
-        std::string extendedVersion;
-        auto values =
-            Version::getValues(manifestPath.string(), {EXTENDED_VERSION});
-        const auto it = values.find(EXTENDED_VERSION);
-        if (it != values.end())
-        {
-            extendedVersion = it->second;
-        }
+        std::string extendedVersion =
+            Version::getValue(manifestPath, {MANIFEST_EXTENDED_VERSION});
 
         auto activation =
             createActivationObject(path, versionId, extendedVersion,
@@ -186,9 +180,7 @@
 
 std::unique_ptr<Activation> ItemUpdater::createActivationObject(
     const std::string& path, const std::string& versionId,
-    const std::string& extVersion,
-    sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations
-        activationStatus,
+    const std::string& extVersion, Activation::Status activationStatus,
     const AssociationList& assocs, const std::string& filePath)
 {
     return std::make_unique<Activation>(bus, path, versionId, extVersion,
@@ -217,7 +209,7 @@
     {
         // Create a new object for running PSU inventory
         AssociationList associations;
-        auto activationState = server::Activation::Activations::Active;
+        auto activationState = Activation::Status::Active;
 
         associations.emplace_back(std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
                                                   ACTIVATION_REV_ASSOCIATION,
@@ -346,6 +338,80 @@
     }
 }
 
+void ItemUpdater::processStoredImage()
+{
+    scanDirectory(IMG_DIR_BUILTIN);
+    scanDirectory(IMG_DIR_PERSIST);
+}
+
+void ItemUpdater::scanDirectory(const fs::path& dir)
+{
+    // The directory shall put PSU images in directories named with model
+    if (!fs::exists(dir))
+    {
+        // Skip
+        return;
+    }
+    if (!fs::is_directory(dir))
+    {
+        log<level::ERR>("The path is not a directory",
+                        entry("PATH=%s", dir.c_str()));
+        return;
+    }
+    for (const auto& d : fs::directory_iterator(dir))
+    {
+        // If the model in manifest does not match the dir name
+        // Log a warning and skip it
+        auto path = d.path();
+        auto manifest = path / MANIFEST_FILE;
+        if (fs::exists(manifest))
+        {
+            auto ret = Version::getValues(
+                manifest.string(),
+                {MANIFEST_VERSION, MANIFEST_EXTENDED_VERSION});
+            auto version = ret[MANIFEST_VERSION];
+            auto extVersion = ret[MANIFEST_EXTENDED_VERSION];
+            auto info = Version::getExtVersionInfo(extVersion);
+            auto model = info["model"];
+            if (path.stem() != model)
+            {
+                log<level::ERR>("Unmatched model",
+                                entry("PATH=%s", path.c_str()),
+                                entry("MODEL=%s", model.c_str()));
+                continue;
+            }
+            auto versionId = utils::getVersionId(version);
+            auto it = activations.find(versionId);
+            if (it == activations.end())
+            {
+                // This is a version that is different than the running PSUs
+                auto activationState = Activation::Status::Ready;
+                auto purpose = VersionPurpose::PSU;
+                auto objPath = std::string(SOFTWARE_OBJPATH) + "/" + versionId;
+
+                auto activation = createActivationObject(
+                    objPath, versionId, extVersion, activationState, {}, path);
+                activations.emplace(versionId, std::move(activation));
+
+                auto versionPtr =
+                    createVersionObject(objPath, versionId, version, purpose);
+                versions.emplace(versionId, std::move(versionPtr));
+            }
+            else
+            {
+                // This is a version that a running PSU is using, set the path
+                // on the version object
+                it->second->path(path);
+            }
+        }
+        else
+        {
+            log<level::ERR>("No MANIFEST found",
+                            entry("PATH=%s", path.c_str()));
+        }
+    }
+}
+
 } // namespace updater
 } // namespace software
 } // namespace phosphor