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