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