Skip to content

Commit

Permalink
bmc-state-manager: Add support BMC Reboot Cause feature
Browse files Browse the repository at this point in the history
Below is patch supporting this change.
https://gerrit.openbmc-project.xyz/c/openbmc/phosphor-dbus-interfaces/+/41359

Tested:
busctl get-property xyz.openbmc_project.State.BMC /xyz/openbmc_project/state/bmc0 xyz.openbmc_project.State.BMC LastRebootCause

When unplug/plug in BMC power cable then return as below:
s "xyz.openbmc_project.State.BMC.BMCResetCause.POR"

When executing reboot command then return as below:
s "xyz.openbmc_project.State.BMC.BMCResetCause.Watchdog"

Signed-off-by: Tim Lee <timlee660101@gmail.com>
Change-Id: I4e12f1df96cc1321beee0c4eae648d71e2d4a3b9
  • Loading branch information
timlee66 authored and geissonator committed Sep 14, 2021
1 parent 0a67521 commit 2bfb1ef
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
51 changes: 51 additions & 0 deletions bmc_state_manager.cpp
Expand Up @@ -9,6 +9,9 @@
#include <sdbusplus/exception.hpp>

#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>

namespace phosphor
{
Expand Down Expand Up @@ -230,6 +233,15 @@ BMC::BMCState BMC::currentBMCState(BMCState value)
return server::BMC::currentBMCState(value);
}

BMC::RebootCause BMC::lastRebootCause(RebootCause value)
{
log<level::INFO>(
"Setting the RebootCause field",
entry("LAST_REBOOT_CAUSE=0x%s", convertForMessage(value).c_str()));

return server::BMC::lastRebootCause(value);
}

uint64_t BMC::lastRebootTime() const
{
using namespace std::chrono;
Expand All @@ -245,6 +257,45 @@ uint64_t BMC::lastRebootTime() const
return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
}

void BMC::discoverLastRebootCause()
{
uint64_t bootReason = 0;
std::ifstream file;
auto bootstatusPath = "/sys/class/watchdog/watchdog0/bootstatus";

file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
std::ifstream::eofbit);

try
{
file.open(bootstatusPath);
file >> bootReason;
}
catch (const std::exception& e)
{
auto rc = errno;
log<level::ERR>((std::string("Failed to read sysfs file "
"errno=") +
std::to_string(rc) + " FILENAME=" + bootstatusPath)
.c_str());
}

switch (bootReason)
{
case WDIOF_EXTERN1:
this->lastRebootCause(RebootCause::Watchdog);
break;
case WDIOF_CARDRESET:
this->lastRebootCause(RebootCause::POR);
break;
default:
this->lastRebootCause(RebootCause::Unknown);
break;
}

return;
}

} // namespace manager
} // namespace state
} // namespace phosphor
11 changes: 11 additions & 0 deletions bmc_state_manager.hpp
Expand Up @@ -2,6 +2,8 @@

#include "xyz/openbmc_project/State/BMC/server.hpp"

#include <linux/watchdog.h>

#include <sdbusplus/bus.hpp>

#include <functional>
Expand Down Expand Up @@ -43,6 +45,7 @@ class BMC : public BMCInherit
{
subscribeToSystemdSignals();
discoverInitialState();
discoverLastRebootCause();
this->emit_object_added();
};

Expand All @@ -62,6 +65,9 @@ class BMC : public BMCInherit
*/
uint64_t lastRebootTime() const override;

/** @brief Set value of LastRebootCause **/
RebootCause lastRebootCause(RebootCause value) override;

private:
/**
* @brief discover the state of the bmc
Expand Down Expand Up @@ -94,6 +100,11 @@ class BMC : public BMCInherit

/** @brief Used to subscribe to dbus system state changes **/
std::unique_ptr<sdbusplus::bus::match_t> stateSignal;

/**
* @brief discover the last reboot cause of the bmc
**/
void discoverLastRebootCause();
};

} // namespace manager
Expand Down

0 comments on commit 2bfb1ef

Please sign in to comment.