bmc-reset: ensure error is created if host up

A separate commit will be submitted to phosphor-logging to get this new
error into the PEL registry.

Tested:
- Verified error is generated when CFAM register is A5000001

Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Change-Id: I40a1fafbffb80aaa77ed38fe93edfd5e4a59f18c
diff --git a/phalerror/create_pel.cpp b/phalerror/create_pel.cpp
index 4ca8bb0..b6e9c74 100644
--- a/phalerror/create_pel.cpp
+++ b/phalerror/create_pel.cpp
@@ -27,11 +27,12 @@
 
 namespace pel
 {
+
+constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
+constexpr auto loggingInterface = "xyz.openbmc_project.Logging.Create";
+
 void createBootErrorPEL(const FFDCData& ffdcData, const json& calloutData)
 {
-    constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
-    constexpr auto loggingInterface = "xyz.openbmc_project.Logging.Create";
-
     std::map<std::string, std::string> additionalData;
     auto bus = sdbusplus::bus::new_default();
     additionalData.emplace("_PID", std::to_string(getpid()));
@@ -85,6 +86,45 @@
     }
 }
 
+void createHostRunningPEL()
+{
+    std::map<std::string, std::string> additionalData;
+    auto bus = sdbusplus::bus::new_default();
+    additionalData.emplace("_PID", std::to_string(getpid()));
+
+    try
+    {
+        static constexpr auto bootErrorMessage =
+            "org.open_power.PHAL.Error.HostRunning";
+        std::string service =
+            util::getService(bus, loggingObjectPath, loggingInterface);
+        auto method = bus.new_method_call(service.c_str(), loggingObjectPath,
+                                          loggingInterface, "Create");
+        auto level =
+            sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
+                sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
+                    Error);
+        method.append(bootErrorMessage, level, additionalData);
+        auto resp = bus.call(method);
+    }
+    catch (const sdbusplus::exception::SdBusError& e)
+    {
+        log<level::ERR>("sdbusplus D-Bus call exception",
+                        entry("OBJPATH=%s", loggingObjectPath),
+                        entry("INTERFACE=%s", loggingInterface),
+                        entry("EXCEPTION=%s", e.what()));
+
+        throw std::runtime_error(
+            "Error in invoking D-Bus logging create interface");
+    }
+    catch (std::exception& e)
+    {
+        log<level::ERR>("D-bus call exception",
+                        entry("EXCEPTION=%s", e.what()));
+        throw e;
+    }
+}
+
 FFDCFile::FFDCFile(const json& pHALCalloutData) :
     calloutData(pHALCalloutData.dump()),
     calloutFile("/tmp/phalPELCalloutsJson.XXXXXX"), fileFD(-1)
diff --git a/phalerror/create_pel.hpp b/phalerror/create_pel.hpp
index bf005c1..98d6ded 100644
--- a/phalerror/create_pel.hpp
+++ b/phalerror/create_pel.hpp
@@ -21,6 +21,14 @@
 void createBootErrorPEL(const FFDCData& ffdcData, const json& calloutData);
 
 /**
+ * Create host running error PEL
+ *
+ * The host should not be running so create an error to report this if it
+ * is found the host is up.
+ */
+void createHostRunningPEL();
+
+/**
  * @class FFDCFile
  * @brief This class is used to create ffdc data file and to get fd
  */
diff --git a/procedures/phal/check_host_running.cpp b/procedures/phal/check_host_running.cpp
index a72a812..a36369a 100644
--- a/procedures/phal/check_host_running.cpp
+++ b/procedures/phal/check_host_running.cpp
@@ -5,6 +5,7 @@
 
 #include "common_utils.hpp"
 #include "p10_cfam.hpp"
+#include "phalerror/create_pel.hpp"
 #include "procedures/phal/common_utils.hpp"
 #include "registration.hpp"
 
@@ -81,7 +82,8 @@
             log<level::ERR>("CFAM read indicates host is running");
         }
 
-        // TODO - Create Error
+        // Create an error so user knows system is in a bad state
+        openpower::pel::createHostRunningPEL();
 
         // Create file for host instance and create in filesystem to
         // indicate to services that host is running.