Fix build issues from multihost changes

The support functions for NPCM7XX (Firmware + Loader) time feature are
added back. Erroneous boottime_api usage are corrected.

Google-Bug-Id: 386135750
Change-Id: Idab4da9609ebe03ad222590961e552087946b188
diff --git a/include/gen.hpp b/include/gen.hpp
index 8ba5e54..8a90959 100644
--- a/include/gen.hpp
+++ b/include/gen.hpp
@@ -95,7 +95,7 @@
 inline dbus::DbusConfig GenDbusConfig(const NodeConfig& nodeConfig)
 {
     return {.dbusObjPath = fmt::format("/xyz/openbmc_project/time/boot/{}",
-                                       nodeConfig.nodeName)};
+                                       nodeConfig.node_name)};
 }
 
 /**
diff --git a/include/systemd_handler.hpp b/include/systemd_handler.hpp
index da2e625..9d874f7 100644
--- a/include/systemd_handler.hpp
+++ b/include/systemd_handler.hpp
@@ -9,6 +9,7 @@
 #include <sdbusplus/bus/match.hpp>
 
 #include <functional>
+#include <optional>
 #include <vector>
 
 namespace boot_time_monitor
@@ -67,6 +68,27 @@
     void CheckBmcFinish(const boost::system::error_code& ec,
                         std::shared_ptr<sdbusplus::asio::connection>& conn);
 
+#ifdef NPCM7XX_OR_NEWER
+    /**
+     * The below 2 functions are to support the (Firmware + Loader) time
+     * logging for NPCM7XX or newer Nuvoton BMCs.
+     */
+
+    /**
+     * @brief Read 4 byte value from given memory address.
+     *
+     * @param target Memory address to read from.
+     * @return The value from the memory address. Return nullopt if failed.
+     */
+    std::optional<uint32_t> readMem4Bytes(uint32_t target);
+    /**
+     * @brief Get system uptime from /proc/uptime.
+     *
+     * @return System uptime in milliseconds. Return nullopt if failed.
+     */
+    std::optional<int64_t> getUpTimeInMs();
+#endif
+
     /** @brief Shared pointer to the central boot time monitoring API. */
     std::shared_ptr<btm::api::IBoottimeApi> mApi;
     /** @brief ASIO timer used to periodically check for systemd boot
diff --git a/src/systemd_handler.cpp b/src/systemd_handler.cpp
index acbc4a5..77fe4c4 100644
--- a/src/systemd_handler.cpp
+++ b/src/systemd_handler.cpp
@@ -4,6 +4,9 @@
 
 #include <fmt/printf.h>
 
+#include <boost/interprocess/file_mapping.hpp>
+#include <boost/interprocess/mapped_region.hpp>
+
 #include <regex>
 
 namespace boot_time_monitor
@@ -218,11 +221,11 @@
     // can get (Firmware + Loader) time by `value[SEC_CNT_ADDR] - uptime`.
     constexpr uint32_t SEC_CNT_ADDR = 0xF0801068;
 
-    auto powerOnSec = util->readMem4Bytes(SEC_CNT_ADDR);
-    auto upTimeMS = util->getUpTimeInMs();
+    auto powerOnSec = readMem4Bytes(SEC_CNT_ADDR);
+    auto upTimeMS = getUpTimeInMs();
     if (powerOnSec != std::nullopt && upTimeMS != std::nullopt)
     {
-        status = mApi->SetNodeDurationByTag(
+        status = mApi->SetNodesDurationByTag(
             btm::kBootTimeTagBMC, "FirmwarePlusLoader",
             powerOnSec.value() * 1000 - upTimeMS.value());
         btm::log::LogIfError(status);
@@ -239,5 +242,63 @@
     btm::log::LogIfError(status);
 #endif
 }
+
+#ifdef NPCM7XX_OR_NEWER
+std::optional<uint32_t> Handler::readMem4Bytes(uint32_t target)
+{
+    // Typically the pageSize will be 4K/8K for 32 bit operating systems
+    uint32_t pageSize = boost::interprocess::mapped_region::get_page_size();
+    const uint32_t kPageMask = pageSize - 1;
+
+    uint32_t pageOffset = target & (~kPageMask);
+    uint32_t offsetInPage = target & kPageMask;
+
+    // Map `/dev/mem` to a region.
+    std::unique_ptr<boost::interprocess::file_mapping> fileMapping;
+    std::unique_ptr<boost::interprocess::mapped_region> MappedRegion;
+    try
+    {
+        fileMapping = std::make_unique<boost::interprocess::file_mapping>(
+            "/dev/mem", boost::interprocess::read_only);
+        // No need to unmap in the end.
+        MappedRegion = std::make_unique<boost::interprocess::mapped_region>(
+            *fileMapping, boost::interprocess::read_only, pageOffset,
+            pageSize * 2);
+
+        // MappedRegion->get_address() returns (void*) which needs to covert
+        // into (char*) to make `+ offsetInPage` work.
+        // Then converts it again into (uint32_t*) since we read 4 bytes.
+        return *(reinterpret_cast<uint32_t*>(
+            static_cast<char*>(MappedRegion->get_address()) + offsetInPage));
+    }
+    catch (const std::exception& e)
+    {
+        fmt::print(stderr, "[{}]: Throw exception: %s\n", __FUNCTION__,
+                   e.what());
+        return std::nullopt;
+    }
+
+    fmt::print(stderr, "[{}]: Shouldn't go to this line.\n", __FUNCTION__);
+    return std::nullopt;
+}
+
+std::optional<int64_t> Handler::getUpTimeInMs()
+{
+    constexpr int32_t kMillisecondPerSecond = 1000;
+    std::ifstream fin("/proc/uptime");
+    if (!fin.is_open())
+    {
+        fmt::print(stderr, "[{}]: Can not read \"/proc/uptime\"\n",
+                   __FUNCTION__);
+        return std::nullopt;
+    }
+    double uptimeSec;
+    fin >> uptimeSec;
+    fin.close();
+
+    return static_cast<int64_t>(uptimeSec * kMillisecondPerSecond);
+}
+#endif // NPCM7XX_OR_NEWER
+
 } // namespace systemd
 } // namespace boot_time_monitor