activation: Create ubi write function

Move the code that creates the ubi volumes into a separate
write flash function to make it easier to implement
different flash write functions for other flash layouts.

To implement a different write flash function:

1. Modify the Makefile.am file to include the desired code
location. Ex:
  if UBI
    include ubi/Makefile.am.include
  else
    include <other>/Makefile.am.include
2. Implement the Activation::flashWrite in <other>/flash.cpp

Tested: Verified code update still worked.

Change-Id: Ide4d135695dad27e0dc1b5a776a276dfb2ca9aa6
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 9b6b508..adba0e4 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -39,6 +39,8 @@
 	item_updater.cpp \
 	item_updater_main.cpp
 
+include ubi/Makefile.am.include
+
 if WANT_SIGNATURE_VERIFY_BUILD
 noinst_HEADERS += image_verify.hpp
 phosphor_image_updater_SOURCES += image_verify.cpp
diff --git a/activation.cpp b/activation.cpp
index ee0bcf8..b81a7e2 100644
--- a/activation.cpp
+++ b/activation.cpp
@@ -99,17 +99,7 @@
             }
 #endif
 
-            auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                              SYSTEMD_INTERFACE, "StartUnit");
-            method.append("obmc-flash-bmc-ubirw.service", "replace");
-            bus.call_noreply(method);
-
-            auto roServiceFile =
-                "obmc-flash-bmc-ubiro@" + versionId + ".service";
-            method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                         SYSTEMD_INTERFACE, "StartUnit");
-            method.append(roServiceFile, "replace");
-            bus.call_noreply(method);
+            flashWrite();
 
             activationProgress->progress(10);
         }
diff --git a/activation.hpp b/activation.hpp
index 51756f2..55377b1 100644
--- a/activation.hpp
+++ b/activation.hpp
@@ -3,6 +3,7 @@
 #include <sdbusplus/server.hpp>
 #include <xyz/openbmc_project/Software/Activation/server.hpp>
 #include <xyz/openbmc_project/Software/ActivationBlocksTransition/server.hpp>
+#include "flash.hpp"
 #include "xyz/openbmc_project/Software/RedundancyPriority/server.hpp"
 #include "xyz/openbmc_project/Software/ActivationProgress/server.hpp"
 #include "org/openbmc/Associations/server.hpp"
@@ -188,7 +189,7 @@
  *  @details A concrete implementation for
  *  xyz.openbmc_project.Software.Activation DBus API.
  */
-class Activation : public ActivationInherit
+class Activation : public ActivationInherit, Flash
 {
   public:
     /** @brief Constructs Activation Software Manager
@@ -245,6 +246,9 @@
     RequestedActivations
         requestedActivation(RequestedActivations value) override;
 
+    /** @brief Overloaded write flash function */
+    void flashWrite() override;
+
     /** @brief Check if systemd state change is relevant to this object
      *
      * Instance specific interface to handle the detected systemd state
diff --git a/flash.hpp b/flash.hpp
new file mode 100644
index 0000000..470d99c
--- /dev/null
+++ b/flash.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <sdbusplus/bus.hpp>
+
+namespace phosphor
+{
+namespace software
+{
+namespace updater
+{
+
+/**
+ *  @class Flash
+ *  @brief Contains flash management functions.
+ *  @details The software class that contains functions to interact
+ *           with the flash.
+ */
+class Flash
+{
+  public:
+    /**
+     * @brief Writes the image file(s) to flash
+     */
+    virtual void flashWrite() = 0;
+};
+
+} // namespace updater
+} // namespace software
+} // namespace phosphor
diff --git a/ubi/Makefile.am.include b/ubi/Makefile.am.include
new file mode 100644
index 0000000..7bcfcb2
--- /dev/null
+++ b/ubi/Makefile.am.include
@@ -0,0 +1,2 @@
+phosphor_image_updater_SOURCES += \
+	%reldir%/flash.cpp
diff --git a/ubi/flash.cpp b/ubi/flash.cpp
new file mode 100644
index 0000000..39015bd
--- /dev/null
+++ b/ubi/flash.cpp
@@ -0,0 +1,29 @@
+#include "activation.hpp"
+#include "config.h"
+
+namespace phosphor
+{
+namespace software
+{
+namespace updater
+{
+
+void Activation::flashWrite()
+{
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append("obmc-flash-bmc-ubirw.service", "replace");
+    bus.call_noreply(method);
+
+    auto roServiceFile = "obmc-flash-bmc-ubiro@" + versionId + ".service";
+    method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                 SYSTEMD_INTERFACE, "StartUnit");
+    method.append(roServiceFile, "replace");
+    bus.call_noreply(method);
+
+    return;
+}
+
+} // namespace updater
+} // namespace software
+} // namepsace phosphor