Add MMC layout

Add support for eMMC layout. The actual update of the image will
be handled by the BMC updater since the image file will be
packaged in a single tarball, but the eMMC still needs to have
some services to setup the host firmware mounts for the virtual
pnor configuration to use. Therefore the functions are empty.

Change-Id: Iaa052342a2bb66e46ab7b59bdcd5631f3dfc2999
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index fc31b9f..24ba434 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,12 +24,16 @@
 if UBIFS_LAYOUT
 include ubi/Makefile.am.include
 else
+if MMC_LAYOUT
+include mmc/Makefile.am.include
+else
 include static/Makefile.am.include
 if HAVE_SYSTEMD
   systemdsystemunit_DATA += \
     openpower-pnor-update@.service
 endif
 endif
+endif
 
 if WANT_SIGNATURE_VERIFY_BUILD
 openpower_update_manager_SOURCES += \
diff --git a/configure.ac b/configure.ac
index a08bf94..ce7bc64 100755
--- a/configure.ac
+++ b/configure.ac
@@ -167,12 +167,23 @@
     [Path to the active preserved pnor partitions])
 
 # Setup ubifs layout support
-# For now make it default enabled
 AC_ARG_ENABLE([ubifs_layout],
-    AS_HELP_STRING([--disable-ubifs_layout], [Disable ubifs support.]))
-AS_IF([test "x$enable_ubifs_layout" != "xno"], \
+    AS_HELP_STRING([--enable-ubifs_layout], [Enable ubifs support.]))
+AS_IF([test "x$enable_ubifs_layout" == "xyes"], \
     [AC_DEFINE([UBIFS_LAYOUT],[],[Enable ubifs support.])])
-AM_CONDITIONAL([UBIFS_LAYOUT], [test "x$enable_ubifs_layout" != "xno"])
+AM_CONDITIONAL([UBIFS_LAYOUT], [test "x$enable_ubifs_layout" == "xyes"])
+
+# Setup MMC layout support
+AC_ARG_ENABLE([mmc_layout],
+    AS_HELP_STRING([--enable-mmc_layout], [Enable MMC support.]))
+AS_IF([test "x$enable_mmc_layout" == "xyes"], \
+    [AC_DEFINE([MMC_LAYOUT],[],[Enable MMC support.])])
+AM_CONDITIONAL([MMC_LAYOUT], [test "x$enable_mmc_layout" == "xyes"])
+
+# Setup static layout support (default)
+AS_IF([test "x$enable_ubifs_layout" != "xyes" -a "x$enable_mmc_layout" != "xyes"], \
+    [AC_DEFINE([STATIC_LAYOUT],[],[Enable static layout support.])])
+AM_CONDITIONAL([STATIC_LAYOUT], [test "x$enable_ubifs_layout" != "xyes" -a "x$enable_mmc_layout" != "xyes"])
 
 AC_ARG_VAR(ACTIVE_PNOR_MAX_ALLOWED, [The maximum allowed active pnor versions])
 AS_IF([test "x$ACTIVE_PNOR_MAX_ALLOWED" == "x"], [ACTIVE_PNOR_MAX_ALLOWED=2])
diff --git a/item_updater_main.cpp b/item_updater_main.cpp
index 1792ad4..e2b5793 100644
--- a/item_updater_main.cpp
+++ b/item_updater_main.cpp
@@ -3,6 +3,8 @@
 #ifdef UBIFS_LAYOUT
 #include "ubi/item_updater_ubi.hpp"
 #include "ubi/watch.hpp"
+#elif defined MMC_LAYOUT
+#include "mmc/item_updater_mmc.hpp"
 #else
 #include "static/item_updater_static.hpp"
 #endif
@@ -32,6 +34,8 @@
 
 #ifdef UBIFS_LAYOUT
     ItemUpdaterUbi updater(bus, SOFTWARE_OBJPATH);
+#elif defined MMC_LAYOUT
+    ItemUpdaterMMC updater(bus, SOFTWARE_OBJPATH);
 #else
     ItemUpdaterStatic updater(bus, SOFTWARE_OBJPATH);
 #endif
diff --git a/mmc/Makefile.am.include b/mmc/Makefile.am.include
new file mode 100644
index 0000000..78c3150
--- /dev/null
+++ b/mmc/Makefile.am.include
@@ -0,0 +1,3 @@
+openpower_update_manager_SOURCES += \
+	%reldir%/activation_mmc.cpp \
+	%reldir%/item_updater_mmc.cpp
diff --git a/mmc/activation_mmc.cpp b/mmc/activation_mmc.cpp
new file mode 100644
index 0000000..67ad55a
--- /dev/null
+++ b/mmc/activation_mmc.cpp
@@ -0,0 +1,30 @@
+#include "activation_mmc.hpp"
+
+namespace openpower
+{
+namespace software
+{
+namespace updater
+{
+namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
+
+auto ActivationMMC::activation(Activations value) -> Activations
+{
+    return softwareServer::Activation::activation(value);
+}
+
+void ActivationMMC::startActivation()
+{
+}
+
+void ActivationMMC::unitStateChange(sdbusplus::message::message& msg)
+{
+}
+
+void ActivationMMC::finishActivation()
+{
+}
+
+} // namespace updater
+} // namespace software
+} // namespace openpower
diff --git a/mmc/activation_mmc.hpp b/mmc/activation_mmc.hpp
new file mode 100644
index 0000000..68caf36
--- /dev/null
+++ b/mmc/activation_mmc.hpp
@@ -0,0 +1,32 @@
+#pragma once
+
+#include "activation.hpp"
+
+namespace openpower
+{
+namespace software
+{
+namespace updater
+{
+
+namespace fs = std::filesystem;
+
+/** @class ActivationMMC
+ *  @brief Implementation for eMMC PNOR layout
+ */
+class ActivationMMC : public Activation
+{
+  public:
+    using Activation::Activation;
+    ~ActivationMMC() = default;
+    Activations activation(Activations value) override;
+
+  private:
+    void unitStateChange(sdbusplus::message::message& msg) override;
+    void startActivation() override;
+    void finishActivation() override;
+};
+
+} // namespace updater
+} // namespace software
+} // namespace openpower
diff --git a/mmc/item_updater_mmc.cpp b/mmc/item_updater_mmc.cpp
new file mode 100644
index 0000000..5671a61
--- /dev/null
+++ b/mmc/item_updater_mmc.cpp
@@ -0,0 +1,86 @@
+#include "config.h"
+
+#include "item_updater_mmc.hpp"
+
+#include "activation_mmc.hpp"
+#include "version.hpp"
+
+namespace openpower
+{
+namespace software
+{
+namespace updater
+{
+
+// These functions are just a stub (empty) because the current eMMC
+// implementation uses the BMC updater (repo phosphor-bmc-code-mgmt) to write
+// the new host FW to flash since it's delivered as a "System" image in the
+// same BMC tarball as the BMC image.
+
+std::unique_ptr<Activation> ItemUpdaterMMC::createActivationObject(
+    const std::string& path, const std::string& versionId,
+    const std::string& extVersion,
+    sdbusplus::xyz::openbmc_project::Software::server::Activation::Activations
+        activationStatus,
+    AssociationList& assocs)
+{
+    return std::make_unique<ActivationMMC>(
+        bus, path, *this, versionId, extVersion, activationStatus, assocs);
+}
+
+std::unique_ptr<Version> ItemUpdaterMMC::createVersionObject(
+    const std::string& objPath, const std::string& versionId,
+    const std::string& versionString,
+    sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
+        versionPurpose,
+    const std::string& filePath)
+{
+    auto version = std::make_unique<Version>(
+        bus, objPath, *this, versionId, versionString, versionPurpose, filePath,
+        std::bind(&ItemUpdaterMMC::erase, this, std::placeholders::_1));
+    version->deleteObject = std::make_unique<Delete>(bus, objPath, *version);
+    return version;
+}
+
+bool ItemUpdaterMMC::validateImage(const std::string& path)
+{
+    return true;
+}
+
+void ItemUpdaterMMC::processPNORImage()
+{
+}
+
+void ItemUpdaterMMC::reset()
+{
+}
+
+bool ItemUpdaterMMC::isVersionFunctional(const std::string& versionId)
+{
+    return versionId == functionalVersionId;
+}
+
+void ItemUpdaterMMC::freePriority(uint8_t value, const std::string& versionId)
+{
+}
+
+void ItemUpdaterMMC::deleteAll()
+{
+}
+
+bool ItemUpdaterMMC::freeSpace()
+{
+    return true;
+}
+
+void ItemUpdaterMMC::updateFunctionalAssociation(const std::string& versionId)
+{
+}
+
+void GardResetMMC::reset()
+{
+}
+
+} // namespace updater
+} // namespace software
+} // namespace openpower
diff --git a/mmc/item_updater_mmc.hpp b/mmc/item_updater_mmc.hpp
new file mode 100644
index 0000000..76dfcaa
--- /dev/null
+++ b/mmc/item_updater_mmc.hpp
@@ -0,0 +1,86 @@
+#pragma once
+
+#include "item_updater.hpp"
+
+namespace openpower
+{
+namespace software
+{
+namespace updater
+{
+
+class GardResetMMC : public GardReset
+{
+  public:
+    using GardReset::GardReset;
+    virtual ~GardResetMMC() = default;
+
+  protected:
+    /**
+     * @brief GARD factory reset - clears the PNOR GARD partition.
+     */
+    void reset() override;
+};
+
+/** @class ItemUpdaterMMC
+ *  @brief Manages the activation of the host version items for static layout
+ */
+class ItemUpdaterMMC : public ItemUpdater
+{
+  public:
+    ItemUpdaterMMC(sdbusplus::bus::bus& bus, const std::string& path) :
+        ItemUpdater(bus, path)
+    {
+        processPNORImage();
+        gardReset = std::make_unique<GardResetMMC>(bus, GARD_PATH);
+        volatileEnable = std::make_unique<ObjectEnable>(bus, volatilePath);
+
+        // Emit deferred signal.
+        emit_object_added();
+    }
+    virtual ~ItemUpdaterMMC() = default;
+
+    void freePriority(uint8_t value, const std::string& versionId) override;
+
+    void processPNORImage() override;
+
+    void deleteAll() override;
+
+    bool freeSpace() override;
+
+    void updateFunctionalAssociation(const std::string& versionId) override;
+
+    bool isVersionFunctional(const std::string& versionId) override;
+
+  private:
+    /** @brief Create Activation object */
+    std::unique_ptr<Activation> createActivationObject(
+        const std::string& path, const std::string& versionId,
+        const std::string& extVersion,
+        sdbusplus::xyz::openbmc_project::Software::server::Activation::
+            Activations activationStatus,
+        AssociationList& assocs) override;
+
+    /** @brief Create Version object */
+    std::unique_ptr<Version>
+        createVersionObject(const std::string& objPath,
+                            const std::string& versionId,
+                            const std::string& versionString,
+                            sdbusplus::xyz::openbmc_project::Software::server::
+                                Version::VersionPurpose versionPurpose,
+                            const std::string& filePath) override;
+
+    /** @brief Validate if image is valid or not */
+    bool validateImage(const std::string& path);
+
+    /** @brief Host factory reset - clears PNOR partitions for each
+     * Activation D-Bus object */
+    void reset() override;
+
+    /** @brief The functional version ID */
+    std::string functionalVersionId;
+};
+
+} // namespace updater
+} // namespace software
+} // namespace openpower