Add support for full BMC FW flash image

Add BMC flash file name list for full flash image.
Save the information for which images are being updated.

Tested: Update '.static.mtd.all.tar' with redfish API (
        UpdateService.SimpleUpdate).
        Verified the code update works well.

Change-Id: Icb47e518db61a8d17998179aed328d0cf56db6f5
Signed-off-by: Bright Cheng <bright_cheng@wiwynn.com>
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/images.hpp b/images.hpp
index 83c2dd2..7801ea3 100644
--- a/images.hpp
+++ b/images.hpp
@@ -13,6 +13,8 @@
 // BMC flash image file name list.
 const std::vector<std::string> bmcImages = {"image-kernel", "image-rofs",
                                             "image-rwfs", "image-u-boot"};
+// BMC flash image file name list for full flash image (image-bmc)
+const std::string bmcFullImages = {"image-bmc"};
 
 std::vector<std::string> getOptionalImages();
 
diff --git a/item_updater.cpp b/item_updater.cpp
index 4b133f3..e6dd298 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -402,26 +402,24 @@
 ItemUpdater::ActivationStatus
     ItemUpdater::validateSquashFSImage(const std::string& filePath)
 {
-    bool invalid = false;
+    bool valid = true;
 
-    for (auto& bmcImage : bmcImages)
+    // Record the images which are being updated
+    // First check for the fullimage, then check for images with partitions
+    imageUpdateList.push_back(bmcFullImages);
+    valid = checkImage(filePath, imageUpdateList);
+    if (!valid)
     {
-        fs::path file(filePath);
-        file /= bmcImage;
-        std::ifstream efile(file.c_str());
-        if (efile.good() != 1)
+        imageUpdateList.clear();
+        imageUpdateList.assign(bmcImages.begin(), bmcImages.end());
+        valid = checkImage(filePath, imageUpdateList);
+        if (!valid)
         {
-            log<level::ERR>("Failed to find the BMC image.",
-                            entry("IMAGE=%s", bmcImage.c_str()));
-            invalid = true;
+            log<level::ERR>("Failed to find the needed BMC images.");
+            return ItemUpdater::ActivationStatus::invalid;
         }
     }
 
-    if (invalid)
-    {
-        return ItemUpdater::ActivationStatus::invalid;
-    }
-
     return ItemUpdater::ActivationStatus::ready;
 }
 
@@ -720,6 +718,26 @@
     helper.mirrorAlt();
 }
 
+bool ItemUpdater::checkImage(const std::string& filePath,
+                             const std::vector<std::string>& imageList)
+{
+    bool valid = true;
+
+    for (auto& bmcImage : imageList)
+    {
+        fs::path file(filePath);
+        file /= bmcImage;
+        std::ifstream efile(file.c_str());
+        if (efile.good() != 1)
+        {
+            valid = false;
+            break;
+        }
+    }
+
+    return valid;
+}
+
 } // namespace updater
 } // namespace software
 } // namespace phosphor
diff --git a/item_updater.hpp b/item_updater.hpp
index b1d1c63..721d371 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -11,6 +11,7 @@
 #include <xyz/openbmc_project/Control/FieldMode/server.hpp>
 
 #include <string>
+#include <vector>
 
 namespace phosphor
 {
@@ -161,6 +162,9 @@
      * version id */
     std::map<std::string, std::unique_ptr<VersionClass>> versions;
 
+    /** @brief Vector of needed BMC images in the tarball*/
+    std::vector<std::string> imageUpdateList;
+
   private:
     /** @brief Callback function for Software.Version match.
      *  @details Creates an Activation D-Bus object.
@@ -238,6 +242,17 @@
      *  alternate chip.
      */
     void mirrorUbootToAlt();
+
+    /** @brief Check the required image files
+     *
+     * @param[in] filePath - BMC tarball file path
+     * @param[in] imageList - Image filenames included in the BMC tarball
+     * @param[out] result - Boolean
+     *                      true if all image files are found in BMC tarball
+     *                      false if one of image files is missing
+     */
+    bool checkImage(const std::string& filePath,
+                    const std::vector<std::string>& imageList);
 };
 
 } // namespace updater
diff --git a/static/flash.cpp b/static/flash.cpp
index 9bb2bba..101828b 100644
--- a/static/flash.cpp
+++ b/static/flash.cpp
@@ -4,6 +4,7 @@
 
 #include "activation.hpp"
 #include "images.hpp"
+#include "item_updater.hpp"
 
 #include <filesystem>
 
@@ -20,6 +21,7 @@
 {
 
 namespace fs = std::filesystem;
+using namespace phosphor::software::image;
 
 void Activation::flashWrite()
 {
@@ -28,7 +30,8 @@
     // the image to flash during reboot.
     fs::path uploadDir(IMG_UPLOAD_DIR);
     fs::path toPath(PATH_INITRAMFS);
-    for (auto& bmcImage : phosphor::software::image::bmcImages)
+
+    for (const auto& bmcImage : parent.imageUpdateList)
     {
         fs::copy_file(uploadDir / versionId / bmcImage, toPath / bmcImage,
                       fs::copy_options::overwrite_existing);