item_updater_mmc: Implement bios factory reset

Implemented the method ItemUpdaterMMC::reset. It clears files in
/media/hostfw/ related to managing console and virtualization
information, then recreates those files using bus calls to service
files. Added checks to obmc-flash-bios so it doesn't cause problems
when its methods are run multiple times. Modified latest services to
allow them to run more than once. Services which depend on the init
service will run after it.

Tested: Created extra files in the directories that the reset method
removes. Called the reset method using busctl. Verified that the
added files were removed and that the removed files that belong were
recreated. Verified that all services called were run.

Signed-off-by: Isaac Kurth <blisaac91@gmail.com>
Change-Id: I1e04e97a7c5c9e3fa8b5ee7af47b7320ff3e91d3
diff --git a/mmc/item_updater_mmc.cpp b/mmc/item_updater_mmc.cpp
index 3e58612..ad91b16 100644
--- a/mmc/item_updater_mmc.cpp
+++ b/mmc/item_updater_mmc.cpp
@@ -5,6 +5,8 @@
 #include "activation_mmc.hpp"
 #include "version.hpp"
 
+#include <filesystem>
+
 namespace openpower
 {
 namespace software
@@ -51,7 +53,47 @@
 {}
 
 void ItemUpdaterMMC::reset()
-{}
+{
+    // Do not reset read-only files needed for reset or ext4 default files
+    const std::vector<std::string> exclusionList = {
+        "alternate", "hostfw-a", "hostfw-b", "lost+found", "running-ro"};
+    std::filesystem::path dirPath(std::string(MEDIA_DIR "hostfw/"));
+    // Delete all files in /media/hostfw/ except for those on exclusionList
+    for (const auto& p : std::filesystem::directory_iterator(dirPath))
+    {
+        if (std::find(exclusionList.begin(), exclusionList.end(),
+                      p.path().stem().string()) == exclusionList.end())
+        {
+            std::filesystem::remove_all(p);
+        }
+    }
+
+    // Recreate default files.
+    auto bus = sdbusplus::bus::new_default();
+    constexpr auto initService = "obmc-flash-bios-init.service";
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append(initService, "replace");
+    bus.call_noreply(method);
+
+    constexpr auto patchService = "obmc-flash-bios-patch.service";
+    method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                 SYSTEMD_INTERFACE, "StartUnit");
+    method.append(patchService, "replace");
+    bus.call_noreply(method);
+
+    constexpr auto processService = "openpower-process-host-firmware.service";
+    method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                 SYSTEMD_INTERFACE, "StartUnit");
+    method.append(processService, "replace");
+    bus.call_noreply(method);
+
+    constexpr auto updateService = "openpower-update-bios-attr-table.service";
+    method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                 SYSTEMD_INTERFACE, "StartUnit");
+    method.append(updateService, "replace");
+    bus.call_noreply(method);
+}
 
 bool ItemUpdaterMMC::isVersionFunctional(const std::string& versionId)
 {