blob: 3118b4a3de144c63bdd9091cbdb4b1b8e0f5c143 [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "config.h"
2
3#include "host_state_manager.hpp"
4
Andrew Geissler0d1c3f12021-07-27 16:21:01 -04005#include "host_check.hpp"
Andrew Geissler1ab2b6c2022-03-10 16:16:15 -06006#include "utils.hpp"
Andrew Geissler0d1c3f12021-07-27 16:21:01 -04007
Andrew Geisslere426b582020-05-28 12:40:55 -05008#include <systemd/sd-bus.h>
9
10#include <cereal/archives/json.hpp>
11#include <cereal/cereal.hpp>
12#include <cereal/types/string.hpp>
13#include <cereal/types/tuple.hpp>
14#include <cereal/types/vector.hpp>
15#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050016#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050017#include <sdbusplus/exception.hpp>
18#include <sdbusplus/server.hpp>
19#include <xyz/openbmc_project/Common/error.hpp>
20#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Andrew Geissler765446a2023-05-25 15:38:20 -040021#include <xyz/openbmc_project/State/Host/error.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050022
Andrew Geissler131d04a2021-03-12 11:02:41 -060023#include <filesystem>
Patrick Williams78c066f2024-02-13 12:25:58 -060024#include <format>
Andrew Geisslere426b582020-05-28 12:40:55 -050025#include <fstream>
Andrew Geissler36529022016-11-29 15:23:54 -060026#include <iostream>
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060027#include <map>
Andrew Geissler54ce6392024-02-23 10:09:09 -060028#include <set>
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060029#include <string>
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050030
Vishwanatha Subbanna0a838732017-10-05 12:43:19 +053031// Register class version with Cereal
Andrew Geissler769a62f2019-12-06 13:36:08 -060032CEREAL_CLASS_VERSION(phosphor::state::manager::Host, CLASS_VERSION)
Andrew Geissler36529022016-11-29 15:23:54 -060033
34namespace phosphor
35{
36namespace state
37{
38namespace manager
39{
40
Andrew Geissler8ffdb262021-09-20 15:25:19 -050041PHOSPHOR_LOG2_USING;
42
Andrew Geissler7b90a622017-08-08 11:41:08 -050043// When you see server:: or reboot:: you know we're referencing our base class
Patrick Williams7e969cb2023-08-23 16:24:23 -050044namespace server = sdbusplus::server::xyz::openbmc_project::state;
45namespace reboot = sdbusplus::server::xyz::openbmc_project::control::boot;
46namespace bootprogress = sdbusplus::server::xyz::openbmc_project::state::boot;
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -050047namespace osstatus =
Patrick Williams7e969cb2023-08-23 16:24:23 -050048 sdbusplus::server::xyz::openbmc_project::state::operating_system;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060049using namespace phosphor::logging;
Patrick Williams6ed41ea2022-04-05 15:50:21 -050050namespace fs = std::filesystem;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050051using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060052
Josh D. King929ef702017-03-02 10:58:11 -060053constexpr auto ACTIVE_STATE = "active";
54constexpr auto ACTIVATING_STATE = "activating";
55
Andrew Geissler58a18012018-01-19 19:36:05 -080056constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
57constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060058constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
59
Josh D. King929ef702017-03-02 10:58:11 -060060constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
61constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
62
Andrew Geissleref3c1842016-12-01 12:33:09 -060063void Host::determineInitialState()
64{
Allen.Wang79b45002022-02-10 17:59:20 +080065 if (stateActive(getTarget(server::Host::HostState::Running)) ||
Potin Lai70f36d82022-03-15 10:25:39 +080066 isHostRunning(id))
Andrew Geissleref3c1842016-12-01 12:33:09 -060067 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050068 info("Initial Host State will be Running");
Peter Yin73d7ad02024-03-14 13:41:11 +080069 server::Host::currentHostState(HostState::Running, true);
70 server::Host::requestedHostTransition(Transition::On, true);
Andrew Geissleref3c1842016-12-01 12:33:09 -060071 }
72 else
73 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050074 info("Initial Host State will be Off");
Peter Yin73d7ad02024-03-14 13:41:11 +080075 server::Host::currentHostState(HostState::Off, true);
76 server::Host::requestedHostTransition(Transition::Off, true);
Andrew Geissleref3c1842016-12-01 12:33:09 -060077 }
78
Allen.Wangba182f02022-03-23 19:01:53 +080079 if (!deserialize())
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050080 {
Andrew Geissler58a18012018-01-19 19:36:05 -080081 // set to default value.
Peter Yin73d7ad02024-03-14 13:41:11 +080082 server::Host::requestedHostTransition(Transition::Off, true);
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050083 }
Andrew Geissleref3c1842016-12-01 12:33:09 -060084 return;
85}
86
Andrew Geissler54ce6392024-02-23 10:09:09 -060087void Host::setupSupportedTransitions()
88{
89 std::set<Transition> supportedTransitions = {
Andrew Geissler31cddb72024-02-27 14:20:26 -060090 Transition::On,
91 Transition::Off,
92 Transition::Reboot,
93 Transition::GracefulWarmReboot,
94#if ENABLE_FORCE_WARM_REBOOT
95 Transition::ForceWarmReboot,
96#endif
97 };
Andrew Geissler54ce6392024-02-23 10:09:09 -060098 server::Host::allowedHostTransitions(supportedTransitions);
99}
100
Allen.Wang79b45002022-02-10 17:59:20 +0800101void Host::createSystemdTargetMaps()
102{
103 stateTargetTable = {
Patrick Williams78c066f2024-02-13 12:25:58 -0600104 {HostState::Off, std::format("obmc-host-stop@{}.target", id)},
105 {HostState::Running, std::format("obmc-host-startmin@{}.target", id)},
106 {HostState::Quiesced, std::format("obmc-host-quiesce@{}.target", id)},
Allen.Wang79b45002022-02-10 17:59:20 +0800107 {HostState::DiagnosticMode,
Patrick Williams78c066f2024-02-13 12:25:58 -0600108 std::format("obmc-host-diagnostic-mode@{}.target", id)}};
Allen.Wang79b45002022-02-10 17:59:20 +0800109
110 transitionTargetTable = {
Patrick Williams78c066f2024-02-13 12:25:58 -0600111 {Transition::Off, std::format("obmc-host-shutdown@{}.target", id)},
112 {Transition::On, std::format("obmc-host-start@{}.target", id)},
113 {Transition::Reboot, std::format("obmc-host-reboot@{}.target", id)},
Allen.Wang79b45002022-02-10 17:59:20 +0800114// Some systems do not support a warm reboot so just map the reboot
115// requests to our normal cold reboot in that case
116#if ENABLE_WARM_REBOOT
117 {Transition::GracefulWarmReboot,
Patrick Williams78c066f2024-02-13 12:25:58 -0600118 std::format("obmc-host-warm-reboot@{}.target", id)},
Allen.Wang79b45002022-02-10 17:59:20 +0800119 {Transition::ForceWarmReboot,
Patrick Williams1b2c3c02024-08-16 15:20:29 -0400120 std::format("obmc-host-force-warm-reboot@{}.target", id)}};
Allen.Wang79b45002022-02-10 17:59:20 +0800121#else
122 {Transition::GracefulWarmReboot,
Patrick Williams78c066f2024-02-13 12:25:58 -0600123 std::format("obmc-host-reboot@{}.target", id)},
Allen.Wang79b45002022-02-10 17:59:20 +0800124 {Transition::ForceWarmReboot,
Patrick Williams1b2c3c02024-08-16 15:20:29 -0400125 std::format("obmc-host-reboot@{}.target", id)}};
Allen.Wang79b45002022-02-10 17:59:20 +0800126#endif
Patrick Williams78c066f2024-02-13 12:25:58 -0600127 hostCrashTarget = std::format("obmc-host-crash@{}.target", id);
Allen.Wang79b45002022-02-10 17:59:20 +0800128}
129
130const std::string& Host::getTarget(HostState state)
131{
132 return stateTargetTable[state];
133};
134
135const std::string& Host::getTarget(Transition tranReq)
136{
137 return transitionTargetTable[tranReq];
138};
139
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600140void Host::executeTransition(Transition tranReq)
141{
Pavithra Barithaya319eda42024-06-21 11:54:43 -0500142 const auto& sysdUnit = getTarget(tranReq);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600143
Andrew Geissler58a18012018-01-19 19:36:05 -0800144 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
145 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600146
147 method.append(sysdUnit);
148 method.append("replace");
149
Andrew Geissler4da7e002017-01-24 15:21:40 -0600150 this->bus.call_noreply(method);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600151
152 return;
153}
154
Josh D. King929ef702017-03-02 10:58:11 -0600155bool Host::stateActive(const std::string& target)
156{
Patrick Williams2975e262020-05-13 18:01:09 -0500157 std::variant<std::string> currentState;
Josh D. King929ef702017-03-02 10:58:11 -0600158 sdbusplus::message::object_path unitTargetPath;
159
Andrew Geissler58a18012018-01-19 19:36:05 -0800160 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
161 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King929ef702017-03-02 10:58:11 -0600162
163 method.append(target);
Josh D. King929ef702017-03-02 10:58:11 -0600164
Anthony Wilson32c532e2018-10-25 21:56:07 -0500165 try
Josh D. King929ef702017-03-02 10:58:11 -0600166 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500167 auto result = this->bus.call(method);
168 result.read(unitTargetPath);
169 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500170 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500171 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500172 error("Error in GetUnit call: {ERROR}", "ERROR", e);
Josh D. King929ef702017-03-02 10:58:11 -0600173 return false;
174 }
175
Andrew Geissler58a18012018-01-19 19:36:05 -0800176 method = this->bus.new_method_call(
177 SYSTEMD_SERVICE,
178 static_cast<const std::string&>(unitTargetPath).c_str(),
179 SYSTEMD_PROPERTY_IFACE, "Get");
Josh D. King929ef702017-03-02 10:58:11 -0600180
181 method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
Josh D. King929ef702017-03-02 10:58:11 -0600182
Anthony Wilson32c532e2018-10-25 21:56:07 -0500183 try
Josh D. King929ef702017-03-02 10:58:11 -0600184 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500185 auto result = this->bus.call(method);
186 result.read(currentState);
187 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500188 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500189 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500190 error("Error in ActiveState Get: {ERROR}", "ERROR", e);
Josh D. King929ef702017-03-02 10:58:11 -0600191 return false;
192 }
193
Patrick Williams37413dc2020-05-13 11:29:54 -0500194 const auto& currentStateStr = std::get<std::string>(currentState);
William A. Kennington III7a0689a2018-11-12 17:19:33 -0800195 return currentStateStr == ACTIVE_STATE ||
196 currentStateStr == ACTIVATING_STATE;
Josh D. King929ef702017-03-02 10:58:11 -0600197}
198
Michael Tritz206a8332017-02-06 16:01:23 -0600199bool Host::isAutoReboot()
200{
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500201 using namespace settings;
Michael Tritz206a8332017-02-06 16:01:23 -0600202
Andrew Geissler7f620832020-10-23 08:25:52 -0500203 /* The logic here is to first check the one-time AutoReboot setting.
204 * If this property is true (the default) then look at the persistent
205 * user setting in the non one-time object, otherwise honor the one-time
206 * setting and do not auto reboot.
207 */
208 auto methodOneTime = bus.new_method_call(
Andrew Geissler58a18012018-01-19 19:36:05 -0800209 settings.service(settings.autoReboot, autoRebootIntf).c_str(),
Andrew Geissler7f620832020-10-23 08:25:52 -0500210 settings.autoRebootOneTime.c_str(), SYSTEMD_PROPERTY_IFACE, "Get");
211 methodOneTime.append(autoRebootIntf, "AutoReboot");
212
213 auto methodUserSetting = bus.new_method_call(
214 settings.service(settings.autoReboot, autoRebootIntf).c_str(),
215 settings.autoReboot.c_str(), SYSTEMD_PROPERTY_IFACE, "Get");
216 methodUserSetting.append(autoRebootIntf, "AutoReboot");
Michael Tritz206a8332017-02-06 16:01:23 -0600217
Anthony Wilson32c532e2018-10-25 21:56:07 -0500218 try
Michael Tritz206a8332017-02-06 16:01:23 -0600219 {
Andrew Geissler7f620832020-10-23 08:25:52 -0500220 auto reply = bus.call(methodOneTime);
Patrick Williams2975e262020-05-13 18:01:09 -0500221 std::variant<bool> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500222 reply.read(result);
Patrick Williams37413dc2020-05-13 11:29:54 -0500223 auto autoReboot = std::get<bool>(result);
Andrew Geissler7f620832020-10-23 08:25:52 -0500224
225 if (!autoReboot)
226 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500227 info("Auto reboot (one-time) disabled");
Andrew Geissler7f620832020-10-23 08:25:52 -0500228 return false;
229 }
230 else
231 {
232 // one-time is true so read the user setting
233 reply = bus.call(methodUserSetting);
234 reply.read(result);
235 autoReboot = std::get<bool>(result);
236 }
237
Anthony Wilson32c532e2018-10-25 21:56:07 -0500238 auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft();
239
240 if (autoReboot)
Saqib Khancbe08d12017-03-10 01:29:20 -0600241 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500242 if (rebootCounterParam > 0)
243 {
244 // Reduce BOOTCOUNT by 1
Andrew Geisslerad65b2d2021-09-21 12:53:29 -0500245 info(
246 "Auto reboot enabled and boot count at {BOOTCOUNT}, rebooting",
247 "BOOTCOUNT", rebootCounterParam);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500248 return true;
249 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500250 else
251 {
Andrew Geissler43284792021-09-23 10:32:15 -0500252 // We are at 0 so reset reboot counter and go to quiesce state
253 info("Auto reboot enabled but HOST BOOTCOUNT already set to 0");
NodeMan97b4cbfac2022-05-09 23:56:39 -0500254 attemptsLeft(reboot::RebootAttempts::retryAttempts());
Andrew Geissler1ab2b6c2022-03-10 16:16:15 -0600255
256 // Generate log since we will now be sitting in Quiesce
257 const std::string errorMsg =
258 "xyz.openbmc_project.State.Error.HostQuiesce";
259 utils::createError(this->bus, errorMsg,
260 sdbusplus::xyz::openbmc_project::Logging::
261 server::Entry::Level::Critical);
Andrew Geissler5bcaee12022-04-19 15:40:40 -0400262
263 // Generate BMC dump to assist with debug
264 utils::createBmcDump(this->bus);
265
Anthony Wilson32c532e2018-10-25 21:56:07 -0500266 return false;
267 }
Saqib Khand5ac6352017-04-04 09:53:59 -0500268 }
269 else
270 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500271 info("Auto reboot disabled.");
Saqib Khancbe08d12017-03-10 01:29:20 -0600272 return false;
273 }
Michael Tritz206a8332017-02-06 16:01:23 -0600274 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500275 catch (const sdbusplus::exception_t& e)
Saqib Khand5ac6352017-04-04 09:53:59 -0500276 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500277 error("Error in AutoReboot Get, {ERROR}", "ERROR", e);
Saqib Khand5ac6352017-04-04 09:53:59 -0500278 return false;
279 }
Michael Tritz206a8332017-02-06 16:01:23 -0600280}
281
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500282void Host::sysStateChangeJobRemoved(sdbusplus::message_t& msg)
Andrew Geissler4da7e002017-01-24 15:21:40 -0600283{
Andrew Geissler58a18012018-01-19 19:36:05 -0800284 uint32_t newStateID{};
Andrew Geissler4da7e002017-01-24 15:21:40 -0600285 sdbusplus::message::object_path newStateObjPath;
286 std::string newStateUnit{};
287 std::string newStateResult{};
288
Andrew Geissler58a18012018-01-19 19:36:05 -0800289 // Read the msg and populate each variable
Patrick Williamsd22706f2017-05-04 05:42:49 -0500290 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600291
Allen.Wang79b45002022-02-10 17:59:20 +0800292 if ((newStateUnit == getTarget(server::Host::HostState::Off)) &&
Andrew Geissler969b2612018-03-29 10:16:51 -0700293 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800294 (!stateActive(getTarget(server::Host::HostState::Running))))
Andrew Geissleref621162016-12-08 12:56:21 -0600295 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500296 info("Received signal that host is off");
Andrew Geissler4da7e002017-01-24 15:21:40 -0600297 this->currentHostState(server::Host::HostState::Off);
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -0500298 this->bootProgress(bootprogress::Progress::ProgressStages::Unspecified);
299 this->operatingSystemState(osstatus::Status::OSStatus::Inactive);
Andrew Geissleref621162016-12-08 12:56:21 -0600300 }
Allen.Wang79b45002022-02-10 17:59:20 +0800301 else if ((newStateUnit == getTarget(server::Host::HostState::Running)) &&
Andrew Geissler58a18012018-01-19 19:36:05 -0800302 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800303 (stateActive(getTarget(server::Host::HostState::Running))))
Andrew Geissler58a18012018-01-19 19:36:05 -0800304 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500305 info("Received signal that host is running");
Andrew Geissler58a18012018-01-19 19:36:05 -0800306 this->currentHostState(server::Host::HostState::Running);
Andrew Geissler131d04a2021-03-12 11:02:41 -0600307
308 // Remove temporary file which is utilized for scenarios where the
309 // BMC is rebooted while the host is still up.
310 // This file is used to indicate to host related systemd services
311 // that the host is already running and they should skip running.
312 // Once the host state is back to running we can clear this file.
Patrick Williams78c066f2024-02-13 12:25:58 -0600313 std::string hostFile = std::format(HOST_RUNNING_FILE, 0);
314 if (std::filesystem::exists(hostFile))
Andrew Geissler131d04a2021-03-12 11:02:41 -0600315 {
Patrick Williams78c066f2024-02-13 12:25:58 -0600316 std::filesystem::remove(hostFile);
Andrew Geissler131d04a2021-03-12 11:02:41 -0600317 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800318 }
Allen.Wang79b45002022-02-10 17:59:20 +0800319 else if ((newStateUnit == getTarget(server::Host::HostState::Quiesced)) &&
Josh D. King29d025d2017-04-27 12:40:22 -0500320 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800321 (stateActive(getTarget(server::Host::HostState::Quiesced))))
Andrew Geissler58a18012018-01-19 19:36:05 -0800322 {
323 if (Host::isAutoReboot())
324 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500325 info("Beginning reboot...");
Andrew Geissler58a18012018-01-19 19:36:05 -0800326 Host::requestedHostTransition(server::Host::Transition::Reboot);
327 }
328 else
329 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500330 info("Maintaining quiesce");
Andrew Geissler58a18012018-01-19 19:36:05 -0800331 this->currentHostState(server::Host::HostState::Quiesced);
332 }
333 }
Andrew Geissleref621162016-12-08 12:56:21 -0600334}
335
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500336void Host::sysStateChangeJobNew(sdbusplus::message_t& msg)
Andrew Geissler47b96122020-02-11 16:13:13 -0600337{
338 uint32_t newStateID{};
339 sdbusplus::message::object_path newStateObjPath;
340 std::string newStateUnit{};
341
342 // Read the msg and populate each variable
343 msg.read(newStateID, newStateObjPath, newStateUnit);
344
Allen.Wang79b45002022-02-10 17:59:20 +0800345 if (newStateUnit == getTarget(server::Host::HostState::DiagnosticMode))
Andrew Geissler47b96122020-02-11 16:13:13 -0600346 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500347 info("Received signal that host is in diagnostice mode");
Andrew Geissler47b96122020-02-11 16:13:13 -0600348 this->currentHostState(server::Host::HostState::DiagnosticMode);
349 }
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400350 else if ((newStateUnit == hostCrashTarget) &&
351 (server::Host::currentHostState() ==
352 server::Host::HostState::Running))
353 {
354 // Only decrease the boot count if host was running when the host crash
355 // target was started. Systemd will sometimes trigger multiple
356 // JobNew events for the same target. This seems to be related to
357 // how OpenBMC utilizes the targets in the reboot scenario
358 info("Received signal that host has crashed, decrement reboot count");
359
360 // A host crash can cause a reboot of the host so decrement the reboot
361 // count
362 decrementRebootCount();
363 }
Andrew Geissler47b96122020-02-11 16:13:13 -0600364}
365
Andrew Geissler7b90a622017-08-08 11:41:08 -0500366uint32_t Host::decrementRebootCount()
367{
368 auto rebootCount = reboot::RebootAttempts::attemptsLeft();
Andrew Geissler58a18012018-01-19 19:36:05 -0800369 if (rebootCount > 0)
Andrew Geissler7b90a622017-08-08 11:41:08 -0500370 {
Andrew Geissler58a18012018-01-19 19:36:05 -0800371 return (reboot::RebootAttempts::attemptsLeft(rebootCount - 1));
Andrew Geissler7b90a622017-08-08 11:41:08 -0500372 }
373 return rebootCount;
374}
375
Allen.Wangba182f02022-03-23 19:01:53 +0800376fs::path Host::serialize()
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500377{
Patrick Williams78c066f2024-02-13 12:25:58 -0600378 fs::path path{std::format(HOST_STATE_PERSIST_PATH, id)};
Allen.Wangba182f02022-03-23 19:01:53 +0800379 std::ofstream os(path.c_str(), std::ios::binary);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500380 cereal::JSONOutputArchive oarchive(os);
381 oarchive(*this);
Allen.Wangba182f02022-03-23 19:01:53 +0800382 return path;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500383}
384
Allen.Wangba182f02022-03-23 19:01:53 +0800385bool Host::deserialize()
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500386{
Patrick Williams78c066f2024-02-13 12:25:58 -0600387 fs::path path{std::format(HOST_STATE_PERSIST_PATH, id)};
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500388 try
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500389 {
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500390 if (fs::exists(path))
391 {
392 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
393 cereal::JSONInputArchive iarchive(is);
394 iarchive(*this);
395 return true;
396 }
397 return false;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500398 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500399 catch (const cereal::Exception& e)
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500400 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500401 error("deserialize exception: {ERROR}", "ERROR", e);
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500402 fs::remove(path);
403 return false;
404 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500405}
406
Andrew Geissleref3c1842016-12-01 12:33:09 -0600407Host::Transition Host::requestedHostTransition(Transition value)
408{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500409 info("Host state transition request of {REQ}", "REQ", value);
Andrew Geissler765446a2023-05-25 15:38:20 -0400410
411#if ONLY_ALLOW_BOOT_WHEN_BMC_READY
412 if ((value != Transition::Off) && (!utils::isBmcReady(this->bus)))
413 {
414 info("BMC State is not Ready so no host on operations allowed");
415 throw sdbusplus::xyz::openbmc_project::State::Host::Error::
416 BMCNotReady();
417 }
418#endif
419
Andrew Geissler7b90a622017-08-08 11:41:08 -0500420 // If this is not a power off request then we need to
421 // decrement the reboot counter. This code should
422 // never prevent a power on, it should just decrement
423 // the count to 0. The quiesce handling is where the
424 // check of this count will occur
Andrew Geissler58a18012018-01-19 19:36:05 -0800425 if (value != server::Host::Transition::Off)
Andrew Geissler7b90a622017-08-08 11:41:08 -0500426 {
427 decrementRebootCount();
428 }
429
Andrew Geisslera27a6e82017-07-27 16:44:43 -0500430 executeTransition(value);
Andrew Geissler7b90a622017-08-08 11:41:08 -0500431
Andrew Geissler58a18012018-01-19 19:36:05 -0800432 auto retVal = server::Host::requestedHostTransition(value);
Allen.Wangba182f02022-03-23 19:01:53 +0800433
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500434 serialize();
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500435 return retVal;
Andrew Geissleref3c1842016-12-01 12:33:09 -0600436}
437
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -0500438Host::ProgressStages Host::bootProgress(ProgressStages value)
439{
440 auto retVal = bootprogress::Progress::bootProgress(value);
441 serialize();
442 return retVal;
443}
444
445Host::OSStatus Host::operatingSystemState(OSStatus value)
446{
447 auto retVal = osstatus::Status::operatingSystemState(value);
448 serialize();
449 return retVal;
450}
451
Andrew Geissleref3c1842016-12-01 12:33:09 -0600452Host::HostState Host::currentHostState(HostState value)
453{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500454 info("Change to Host State: {STATE}", "STATE", value);
Andrew Geissleref621162016-12-08 12:56:21 -0600455 return server::Host::currentHostState(value);
Andrew Geissleref3c1842016-12-01 12:33:09 -0600456}
457
Andrew Geissler36529022016-11-29 15:23:54 -0600458} // namespace manager
459} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700460} // namespace phosphor