Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 1 | #include "bmc_state_manager.hpp" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 2 | |
Andrew Geissler | 98e64e6 | 2022-01-25 16:02:56 -0600 | [diff] [blame] | 3 | #include "utils.hpp" |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 5 | |
Andrew Geissler | 98e64e6 | 2022-01-25 16:02:56 -0600 | [diff] [blame] | 6 | #include <gpiod.h> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 7 | #include <sys/sysinfo.h> |
| 8 | |
| 9 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 11 | #include <sdbusplus/exception.hpp> |
| 12 | |
| 13 | #include <cassert> |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 14 | #include <filesystem> |
| 15 | #include <fstream> |
| 16 | #include <iostream> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 17 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace state |
| 21 | { |
| 22 | namespace manager |
| 23 | { |
| 24 | |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 25 | PHOSPHOR_LOG2_USING; |
| 26 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 27 | // When you see server:: you know we're referencing our base class |
| 28 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 29 | |
| 30 | using namespace phosphor::logging; |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 31 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 32 | |
Andrew Geissler | 9eb0e44 | 2022-02-18 10:21:15 -0600 | [diff] [blame] | 33 | constexpr auto obmcQuiesceTarget = "obmc-bmc-service-quiesce@0.target"; |
Anthony Wilson | eef31f8 | 2019-04-23 17:04:09 -0500 | [diff] [blame] | 34 | constexpr auto obmcStandbyTarget = "multi-user.target"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 35 | constexpr auto signalDone = "done"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 36 | constexpr auto activeState = "active"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 37 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 38 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 39 | const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = { |
| 40 | {server::BMC::Transition::Reboot, "reboot.target"}}; |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 41 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 42 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 43 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 44 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 45 | constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 46 | |
Andrew Geissler | 2774c78 | 2022-02-17 16:57:14 -0600 | [diff] [blame] | 47 | std::string BMC::getUnitState(const std::string& unitToCheck) |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 48 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 49 | std::variant<std::string> currentState; |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 50 | sdbusplus::message::object_path unitTargetPath; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 51 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 52 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 53 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 54 | |
Andrew Geissler | 2774c78 | 2022-02-17 16:57:14 -0600 | [diff] [blame] | 55 | method.append(unitToCheck); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 56 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 57 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 58 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 59 | auto result = this->bus.call(method); |
| 60 | result.read(unitTargetPath); |
| 61 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 62 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 63 | { |
Andrew Geissler | 37d3631 | 2022-03-09 16:24:45 -0600 | [diff] [blame] | 64 | // Not all input units will have been loaded yet so just return an |
| 65 | // empty string if an exception is caught in this path |
| 66 | info("Unit {UNIT} not found: {ERROR}", "UNIT", unitToCheck, "ERROR", e); |
Andrew Geissler | 2774c78 | 2022-02-17 16:57:14 -0600 | [diff] [blame] | 67 | return std::string{}; |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 68 | } |
| 69 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 70 | method = this->bus.new_method_call( |
| 71 | SYSTEMD_SERVICE, |
| 72 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 73 | SYSTEMD_PRP_INTERFACE, "Get"); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 74 | |
| 75 | method.append("org.freedesktop.systemd1.Unit", "ActiveState"); |
| 76 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 77 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 78 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 79 | auto result = this->bus.call(method); |
| 80 | |
Andrew Geissler | 37d3631 | 2022-03-09 16:24:45 -0600 | [diff] [blame] | 81 | // Is input target active or inactive? |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 82 | result.read(currentState); |
| 83 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 84 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 85 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 86 | info("Error in ActiveState Get: {ERROR}", "ERROR", e); |
Andrew Geissler | 2774c78 | 2022-02-17 16:57:14 -0600 | [diff] [blame] | 87 | return std::string{}; |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 88 | } |
Andrew Geissler | 2774c78 | 2022-02-17 16:57:14 -0600 | [diff] [blame] | 89 | return (std::get<std::string>(currentState)); |
| 90 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 91 | |
Andrew Geissler | 2774c78 | 2022-02-17 16:57:14 -0600 | [diff] [blame] | 92 | void BMC::discoverInitialState() |
| 93 | { |
Andrew Geissler | 9eb0e44 | 2022-02-18 10:21:15 -0600 | [diff] [blame] | 94 | |
| 95 | // First look to see if the BMC quiesce target is active |
| 96 | auto currentStateStr = getUnitState(obmcQuiesceTarget); |
| 97 | if (currentStateStr == activeState) |
| 98 | { |
| 99 | info("Setting the BMCState field to BMC_QUIESCED"); |
| 100 | this->currentBMCState(BMCState::Quiesced); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // If not quiesced, then check standby target |
| 105 | currentStateStr = getUnitState(obmcStandbyTarget); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 106 | if (currentStateStr == activeState) |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 107 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 108 | info("Setting the BMCState field to BMC_READY"); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 109 | this->currentBMCState(BMCState::Ready); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 110 | } |
| 111 | else |
| 112 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 113 | info("Setting the BMCState field to BMC_NOTREADY"); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 114 | this->currentBMCState(BMCState::NotReady); |
| 115 | } |
| 116 | |
| 117 | return; |
| 118 | } |
| 119 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 120 | void BMC::subscribeToSystemdSignals() |
| 121 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 122 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 123 | SYSTEMD_INTERFACE, "Subscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 124 | |
| 125 | try |
| 126 | { |
| 127 | this->bus.call(method); |
| 128 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 129 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 130 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 131 | error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e); |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 132 | elog<InternalFailure>(); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 133 | } |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 134 | |
| 135 | return; |
| 136 | } |
| 137 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 138 | void BMC::executeTransition(const Transition tranReq) |
| 139 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 140 | // HardReboot does not shutdown any services and immediately transitions |
| 141 | // into the reboot process |
| 142 | if (server::BMC::Transition::HardReboot == tranReq) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 143 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 144 | auto method = this->bus.new_method_call( |
| 145 | SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Reboot"); |
| 146 | try |
| 147 | { |
| 148 | this->bus.call(method); |
| 149 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 150 | catch (const sdbusplus::exception::exception& e) |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 151 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 152 | info("Error in HardReboot: {ERROR}", "ERROR", e); |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 153 | } |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 154 | } |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 155 | else |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 156 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 157 | // Check to make sure it can be found |
| 158 | auto iter = SYSTEMD_TABLE.find(tranReq); |
| 159 | if (iter == SYSTEMD_TABLE.end()) |
| 160 | return; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 161 | |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 162 | const auto& sysdUnit = iter->second; |
| 163 | |
| 164 | auto method = this->bus.new_method_call( |
| 165 | SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit"); |
| 166 | // The only valid transition is reboot and that |
| 167 | // needs to be irreversible once started |
| 168 | |
| 169 | method.append(sysdUnit, "replace-irreversibly"); |
| 170 | |
| 171 | try |
| 172 | { |
| 173 | this->bus.call(method); |
| 174 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 175 | catch (const sdbusplus::exception::exception& e) |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 176 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 177 | info("Error in StartUnit - replace-irreversibly: {ERROR}", "ERROR", |
| 178 | e); |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 184 | int BMC::bmcStateChange(sdbusplus::message::message& msg) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 185 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 186 | uint32_t newStateID{}; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 187 | sdbusplus::message::object_path newStateObjPath; |
| 188 | std::string newStateUnit{}; |
| 189 | std::string newStateResult{}; |
| 190 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 191 | // Read the msg and populate each variable |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 192 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 193 | |
Andrew Geissler | 9eb0e44 | 2022-02-18 10:21:15 -0600 | [diff] [blame] | 194 | if ((newStateUnit == obmcQuiesceTarget) && (newStateResult == signalDone)) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 195 | { |
Andrew Geissler | 9eb0e44 | 2022-02-18 10:21:15 -0600 | [diff] [blame] | 196 | error("BMC has entered BMC_QUIESCED state"); |
| 197 | this->currentBMCState(BMCState::Quiesced); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 198 | |
Andrew Geissler | 9eb0e44 | 2022-02-18 10:21:15 -0600 | [diff] [blame] | 199 | // There is no getting out of Quiesced once entered (other then BMC |
| 200 | // reboot) so stop watching for signals |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 201 | auto method = |
| 202 | this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 203 | SYSTEMD_INTERFACE, "Unsubscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 204 | |
| 205 | try |
| 206 | { |
| 207 | this->bus.call(method); |
| 208 | this->stateSignal.release(); |
| 209 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 210 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 211 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 212 | info("Error in Unsubscribe: {ERROR}", "ERROR", e); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 213 | } |
Andrew Geissler | 9eb0e44 | 2022-02-18 10:21:15 -0600 | [diff] [blame] | 214 | |
Andrew Geissler | 71e538f | 2022-03-21 11:22:43 -0500 | [diff] [blame] | 215 | // disable the system state change object as well |
| 216 | stateSignal.reset(); |
| 217 | |
Andrew Geissler | 9eb0e44 | 2022-02-18 10:21:15 -0600 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | // Caught the signal that indicates the BMC is now BMC_READY |
| 222 | if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone)) |
| 223 | { |
| 224 | info("BMC_READY"); |
| 225 | this->currentBMCState(BMCState::Ready); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 231 | BMC::Transition BMC::requestedBMCTransition(Transition value) |
| 232 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 233 | info("Setting the RequestedBMCTransition field to " |
| 234 | "{REQUESTED_BMC_TRANSITION}", |
| 235 | "REQUESTED_BMC_TRANSITION", value); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 236 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 237 | executeTransition(value); |
| 238 | return server::BMC::requestedBMCTransition(value); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 239 | } |
| 240 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 241 | BMC::BMCState BMC::currentBMCState(BMCState value) |
| 242 | { |
Andrew Geissler | 429100a | 2021-09-09 12:50:24 -0500 | [diff] [blame] | 243 | |
| 244 | info("Setting the BMCState field to {CURRENT_BMC_STATE}", |
| 245 | "CURRENT_BMC_STATE", value); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 246 | |
| 247 | return server::BMC::currentBMCState(value); |
| 248 | } |
| 249 | |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 250 | BMC::RebootCause BMC::lastRebootCause(RebootCause value) |
| 251 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 252 | info("Setting the RebootCause field to {LAST_REBOOT_CAUSE}", |
| 253 | "LAST_REBOOT_CAUSE", value); |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 254 | |
| 255 | return server::BMC::lastRebootCause(value); |
| 256 | } |
| 257 | |
Matt Spinler | e6710b7 | 2018-07-12 16:05:55 -0500 | [diff] [blame] | 258 | uint64_t BMC::lastRebootTime() const |
| 259 | { |
| 260 | using namespace std::chrono; |
| 261 | struct sysinfo info; |
| 262 | |
| 263 | auto rc = sysinfo(&info); |
| 264 | assert(rc == 0); |
| 265 | |
| 266 | // Since uptime is in seconds, also get the current time in seconds. |
| 267 | auto now = time_point_cast<seconds>(system_clock::now()); |
| 268 | auto rebootTime = now - seconds(info.uptime); |
| 269 | |
| 270 | return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count(); |
| 271 | } |
| 272 | |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 273 | void BMC::discoverLastRebootCause() |
| 274 | { |
| 275 | uint64_t bootReason = 0; |
| 276 | std::ifstream file; |
| 277 | auto bootstatusPath = "/sys/class/watchdog/watchdog0/bootstatus"; |
| 278 | |
| 279 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
| 280 | std::ifstream::eofbit); |
| 281 | |
| 282 | try |
| 283 | { |
| 284 | file.open(bootstatusPath); |
| 285 | file >> bootReason; |
| 286 | } |
| 287 | catch (const std::exception& e) |
| 288 | { |
| 289 | auto rc = errno; |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 290 | error("Failed to read sysfs file {FILE} with errno {ERRNO}", "FILE", |
| 291 | bootstatusPath, "ERRNO", rc); |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | switch (bootReason) |
| 295 | { |
| 296 | case WDIOF_EXTERN1: |
| 297 | this->lastRebootCause(RebootCause::Watchdog); |
Andrew Geissler | 98e64e6 | 2022-01-25 16:02:56 -0600 | [diff] [blame] | 298 | return; |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 299 | case WDIOF_CARDRESET: |
| 300 | this->lastRebootCause(RebootCause::POR); |
Andrew Geissler | 98e64e6 | 2022-01-25 16:02:56 -0600 | [diff] [blame] | 301 | return; |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 302 | default: |
| 303 | this->lastRebootCause(RebootCause::Unknown); |
Andrew Geissler | 98e64e6 | 2022-01-25 16:02:56 -0600 | [diff] [blame] | 304 | // Continue below to see if more details can be found |
| 305 | // on reason for reboot |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | |
Andrew Geissler | 98e64e6 | 2022-01-25 16:02:56 -0600 | [diff] [blame] | 309 | // If the above code could not detect a reason, look for a the |
| 310 | // reset-cause-pinhole gpio to see if it is the reason for the reboot |
| 311 | auto gpioval = |
| 312 | phosphor::state::manager::utils::getGpioValue("reset-cause-pinhole"); |
| 313 | |
Andrew Geissler | 2e352a2 | 2022-03-02 11:31:40 -0600 | [diff] [blame] | 314 | // A 0 indicates a pinhole reset occurred |
| 315 | if (0 == gpioval) |
Andrew Geissler | 98e64e6 | 2022-01-25 16:02:56 -0600 | [diff] [blame] | 316 | { |
| 317 | info("The BMC reset was caused by a pinhole reset"); |
| 318 | this->lastRebootCause(RebootCause::PinholeReset); |
Andrew Geissler | a2a7e12 | 2022-01-26 13:30:18 -0600 | [diff] [blame] | 319 | |
| 320 | // Generate log telling user a pinhole reset has occurred |
| 321 | const std::string errorMsg = "xyz.openbmc_project.State.PinholeReset"; |
| 322 | phosphor::state::manager::utils::createError( |
| 323 | this->bus, errorMsg, |
| 324 | sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level:: |
| 325 | Notice); |
Andrew Geissler | 98e64e6 | 2022-01-25 16:02:56 -0600 | [diff] [blame] | 326 | } |
| 327 | |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 331 | } // namespace manager |
| 332 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 333 | } // namespace phosphor |